Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers\Expense;
- use App\Http\Controllers\Controller;
- use Implements\Expense\BudgetRepository;
- use Illuminate\Http\Request;
- use App\Models\Expense\Budget;
- use App\Models\Expense\Order;
- use Yajra\Datatables\Datatables;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Validator;
- use Illuminate\Support\Facades\Gate;
- use App\Http\Requests\Expense\BudgetRequest;
- use App\Helpers\JsonResponse; /** Return response(bool $success, array $data = [], string $message = 'Success', int $statusCode = 200) */
- class BudgetController extends Controller
- {
- protected $budgetRepository;
- public function __construct(BudgetRepository $budgetRepository) {
- $this->budgetRepository = $budgetRepository;
- }
- /**
- * Display a listing of the resource.
- */
- public function index(Request $request)
- {
- if ($request->ajax()) {
- return $this->budgetRepository->datatable($request->input('year'), $request->input('ccBy'));
- }
- // $sum_budgets = Order::groupBy('budget_id')->selectRaw('budget_id, SUM(tot_price) as tot_plan')->get();
- // $budgets = Budget::with(['cc:id,cc', 'category', 'picExp', 'orders'])->where('year', date('Y'))->get();
- $tot_budget = Budget::selectRaw('SUM(amount) as tot_amount')->get();
- $tot_plan = Order::where('status', 'in-progress')->selectRaw('SUM(tot_price) as tot_plan')->get();
- $tot_actual = Order::where('status', 'finish')->selectRaw('SUM(tot_price) as tot_actual')->get();
- $tot_forecast = $tot_plan[0]->tot_plan + $tot_actual[0]->tot_actual;
- $balance = $tot_budget[0]->tot_amount - $tot_forecast;
- $tot_budget = number_format($tot_budget[0]->tot_amount, 0, '.', ',');
- $tot_forecast = number_format($tot_forecast, 0, '.', ',');
- $balance = number_format($balance, 0, '.', ',');
- // $cc = DB::table('exp_cc')->select('id', 'cc')->get();
- $cost_center = DB::table('exp_cc')->select('id', 'cc')->get();
- $categories = DB::table('exp_categories')->select('id', 'cat_name')->get();
- return view('expenses.budget', compact('tot_budget', 'tot_forecast', 'balance', 'categories', 'cost_center'));
- }
- /**
- * Show the form for creating a new resource.
- */
- public function create()
- {
- //
- }
- /**
- * Store a newly created resource in storage.
- */
- public function store(Request $request)
- {
- //
- }
- /**
- * Display the specified resource.
- */
- public function show(string $id)
- {
- $budget = $this->budgetRepository->getData($id);
- return JsonResponse::response(true, $budget, "Sukses", 200);
- }
- /**
- * Show the form for editing the specified resource.
- */
- public function edit(string $id)
- {
- //
- }
- /**
- * Update the specified resource in storage.
- */
- public function update(BudgetRequest $request, string $id)
- {
- $budget = $this->budgetRepository->update($request->all());
- return JsonResponse::response(true, $budget, "Sukses", 200);
- }
- /**
- * Remove the specified resource from storage.
- */
- public function destroy(string $id)
- {
- if(! Gate::allows('app-dev')) {
- return JsonResponse::response(false, [], "Anda tidak memiliki authorize untuk melakukan action ini");
- }
- $budget = Budget::find($id);
- $budget->delete();
- return JsonResponse::response(true, $budget, 'Budget berhasil dihapus dari database.');
- }
- public function upload(Request $request)
- {
- $validator = Validator::make($request->all(),[
- 'csv_file' => ['required', 'file', 'mimes:txt,csv'],
- 'cc_id_upload' => 'required',
- 'year_upload' => 'required',
- ], [
- 'csv_file.required' => 'CSV budget file diperlukan',
- 'cc_id_upload.required' => 'Pilih cost center budget',
- 'year_upload.required' => 'Pilih tahun budget',
- ]);
- if ($validator->fails()) {
- return redirect()->back()->withError($validator->errors()->first());
- }
- if($request->hasFile('csv_file')) {
- $file = $request->file('csv_file');
- $path = $file->getPathname();
- $delimeter = $request->input('delimiter');
- $is_new = $request->input('is_new') == 'new' ? true : false;
- $cc = $request->input('cc_id_upload');
- $year = $request->input('year_upload');
- $csv_upload = $this->budgetRepository->upload($path, $delimeter, $is_new, $cc, $year);
- if ($csv_upload) {
- return redirect()->back()->withSuccess('Data berhasil ditambahkan kedalam database.');
- }
- }
- return redirect()->back()->withError('Data gagal ditambahkan kedalam database. Cek isi data (baru / revisi) atau delimiter csv file yang anda upload.');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement