Advertisement
arlendafitranto

BudgetController

Sep 17th, 2023
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.08 KB | Source Code | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers\Expense;
  4.  
  5. use App\Http\Controllers\Controller;
  6. use Implements\Expense\BudgetRepository;
  7. use Illuminate\Http\Request;
  8. use App\Models\Expense\Budget;
  9. use App\Models\Expense\Order;
  10. use Yajra\Datatables\Datatables;
  11. use Illuminate\Support\Facades\DB;
  12. use Illuminate\Support\Facades\Log;
  13. use Illuminate\Support\Facades\Validator;
  14. use Illuminate\Support\Facades\Gate;
  15. use App\Http\Requests\Expense\BudgetRequest;
  16. use App\Helpers\JsonResponse; /** Return response(bool $success, array $data = [], string $message = 'Success', int $statusCode = 200) */
  17.  
  18. class BudgetController extends Controller
  19. {
  20.  
  21.     protected $budgetRepository;
  22.  
  23.     public function __construct(BudgetRepository $budgetRepository) {
  24.         $this->budgetRepository = $budgetRepository;
  25.     }
  26.  
  27.  
  28.     /**
  29.      * Display a listing of the resource.
  30.      */
  31.     public function index(Request $request)
  32.     {
  33.         if ($request->ajax()) {
  34.             return $this->budgetRepository->datatable($request->input('year'), $request->input('ccBy'));
  35.         }
  36.  
  37.         // $sum_budgets = Order::groupBy('budget_id')->selectRaw('budget_id, SUM(tot_price) as tot_plan')->get();
  38.         // $budgets = Budget::with(['cc:id,cc', 'category', 'picExp', 'orders'])->where('year', date('Y'))->get();
  39.         $tot_budget = Budget::selectRaw('SUM(amount) as tot_amount')->get();  
  40.         $tot_plan = Order::where('status', 'in-progress')->selectRaw('SUM(tot_price) as tot_plan')->get();
  41.         $tot_actual = Order::where('status', 'finish')->selectRaw('SUM(tot_price) as tot_actual')->get();
  42.  
  43.         $tot_forecast = $tot_plan[0]->tot_plan + $tot_actual[0]->tot_actual;
  44.         $balance = $tot_budget[0]->tot_amount - $tot_forecast;
  45.  
  46.         $tot_budget = number_format($tot_budget[0]->tot_amount, 0, '.', ',');
  47.         $tot_forecast = number_format($tot_forecast, 0, '.', ',');
  48.         $balance = number_format($balance, 0, '.', ',');
  49.  
  50.         // $cc = DB::table('exp_cc')->select('id', 'cc')->get();
  51.         $cost_center = DB::table('exp_cc')->select('id', 'cc')->get();
  52.         $categories = DB::table('exp_categories')->select('id', 'cat_name')->get();
  53.  
  54.         return view('expenses.budget', compact('tot_budget', 'tot_forecast', 'balance', 'categories', 'cost_center'));
  55.     }
  56.  
  57.     /**
  58.      * Show the form for creating a new resource.
  59.      */
  60.     public function create()
  61.     {
  62.         //
  63.     }
  64.  
  65.     /**
  66.      * Store a newly created resource in storage.
  67.      */
  68.     public function store(Request $request)
  69.     {
  70.         //
  71.     }
  72.  
  73.     /**
  74.      * Display the specified resource.
  75.      */
  76.     public function show(string $id)
  77.     {
  78.         $budget = $this->budgetRepository->getData($id);
  79.  
  80.         return JsonResponse::response(true, $budget, "Sukses", 200);
  81.     }
  82.  
  83.     /**
  84.      * Show the form for editing the specified resource.
  85.      */
  86.     public function edit(string $id)
  87.     {
  88.         //
  89.     }
  90.  
  91.     /**
  92.      * Update the specified resource in storage.
  93.      */
  94.     public function update(BudgetRequest $request, string $id)
  95.     {
  96.         $budget = $this->budgetRepository->update($request->all());
  97.  
  98.         return JsonResponse::response(true, $budget, "Sukses", 200);
  99.     }
  100.  
  101.     /**
  102.      * Remove the specified resource from storage.
  103.      */
  104.     public function destroy(string $id)
  105.     {
  106.         if(! Gate::allows('app-dev')) {
  107.             return JsonResponse::response(false, [], "Anda tidak memiliki authorize untuk melakukan action ini");
  108.         }
  109.  
  110.         $budget = Budget::find($id);
  111.         $budget->delete();
  112.  
  113.         return JsonResponse::response(true, $budget, 'Budget berhasil dihapus dari database.');
  114.     }
  115.  
  116.     public function upload(Request $request)
  117.     {
  118.        
  119.         $validator = Validator::make($request->all(),[
  120.             'csv_file' => ['required', 'file', 'mimes:txt,csv'],
  121.             'cc_id_upload' => 'required',
  122.             'year_upload' => 'required',
  123.  
  124.         ], [
  125.             'csv_file.required' => 'CSV budget file diperlukan',
  126.             'cc_id_upload.required' => 'Pilih cost center budget',
  127.             'year_upload.required' => 'Pilih tahun budget',
  128.         ]);
  129.  
  130.         if ($validator->fails()) {
  131.             return redirect()->back()->withError($validator->errors()->first());
  132.         }
  133.        
  134.         if($request->hasFile('csv_file')) {
  135.             $file = $request->file('csv_file');
  136.             $path = $file->getPathname();
  137.             $delimeter = $request->input('delimiter');
  138.             $is_new = $request->input('is_new') == 'new' ? true : false;
  139.             $cc = $request->input('cc_id_upload');
  140.             $year = $request->input('year_upload');
  141.  
  142.            
  143.             $csv_upload = $this->budgetRepository->upload($path, $delimeter, $is_new, $cc, $year);
  144.            
  145.             if ($csv_upload) {
  146.                 return redirect()->back()->withSuccess('Data berhasil ditambahkan kedalam database.');
  147.             }
  148.  
  149.         }
  150.  
  151.         return redirect()->back()->withError('Data gagal ditambahkan kedalam database. Cek isi data (baru / revisi) atau delimiter csv file yang anda upload.');
  152.     }
  153. }
  154.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement