Advertisement
arlendafitranto

BudgetAct

Sep 28th, 2023
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.69 KB | Source Code | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use Illuminate\Http\Request;
  6. use App\Models\Expense\Budget;
  7. use App\Models\Expense\Order;
  8. use App\Models\Expense\MonthlyBudget;
  9. use App\Enum\OrderStatusEnum;
  10.  
  11. class TestingController extends Controller
  12. {
  13.     /**
  14.      * Handle the incoming request.
  15.      */
  16.     public function __invoke(Request $request)
  17.     {
  18.        
  19.         $this_month = intval(date('n'));
  20.         $this_year = date('Y');
  21.        
  22.         /* --------------------------------- Budget --------------------------------- */
  23.         $ori_budget = Budget::with(['cc:id,cc'])->where('year', $this_year)->groupBy('id')->select('id', 'amount', 'exp_no', 'cc_id', 'theme')->get();
  24.        
  25.         $budget = [];
  26.  
  27.         foreach ($ori_budget as $item) {
  28.             $budget[$item->id] = [
  29.                 'cc_name' => $item->cc['cc'],
  30.                 'exp_no' => $item->exp_no,
  31.                 'theme' => $item->theme,
  32.                 'amount' => $item->amount
  33.             ];
  34.         }
  35.  
  36.         /* ----------------------------- Monthly Budget ----------------------------- */
  37.         $monthly_budget = MonthlyBudget::where('month', '<=', $this_month)->whereYear('created_at', $this_year)->groupBy('budget_id')
  38.                                         ->select('budget_id',  \DB::raw('SUM(amount) as total'))
  39.                                         ->get()
  40.                                         ->pluck('total', 'budget_id');
  41.        
  42.         /* --------------------------------- Orders --------------------------------- */
  43.         $orders = Order::where('status', OrderStatusEnum::FINISH)->where('act_eta', '<=', $this_month)
  44.                         ->groupBy('budget_id')->select('budget_id', \DB::raw('SUM(tot_price) as total'))
  45.                         ->get()
  46.                         ->pluck('total', 'budget_id');
  47.  
  48.         /* -------------------------------- All Joint ------------------------------- */
  49.         $map_budget = [];
  50.         $map_by_cc = [];
  51.         foreach ($budget as $key => $value) {
  52.             $act_order = $orders->filter(function($value, $key_order) use($key){
  53.                 return $key_order == $key;
  54.             });
  55.             $act_budget_now = $monthly_budget->filter(function($value, $key_monthly) use($key){
  56.                 return $key_monthly == $key;
  57.             });
  58.            
  59.  
  60.             $map_budget [$key] = ['cc_name' => $value['cc_name'], 'exp_no' => $value['exp_no'], 'theme' => $value['theme'], 'plan_end_year' => $value['amount'], 'plan_this_month' => intval($act_budget_now[$key] ?? 0), 'act' => intval($act_order[$key] ?? 0)];
  61.             $map_by_cc[] = ['budget_id' => $key, 'cc_name' => $value['cc_name'], 'exp_no' => $value['exp_no'], 'theme' => $value['theme'], 'plan_end_year' => $value['amount'], 'plan_this_month' => intval($act_budget_now[$key] ?? 0), 'act' => intval($act_order[$key] ?? 0)];
  62.         }
  63.  
  64.         $budget_group_by_cc = [];
  65.  
  66.         foreach ($map_by_cc as $value) {
  67.             $cc_name = $value['cc_name'];
  68.             if (! isset($budget_group_by_cc[$cc_name])) {
  69.                 $budget_group_by_cc[$cc_name] = [];
  70.             }
  71.  
  72.             $budget_group_by_cc[$cc_name][] = $value;
  73.         }
  74.         // dd($budget_group_by_cc);
  75.         $forecast_orders = Order::where('status', OrderStatusEnum::PROGRESS)
  76.                             ->select('cc_id', \DB::raw('SUM(tot_price) as total'))
  77.                             ->groupBy('cc_id')
  78.                             ->get()->toArray();
  79.        
  80.         $forecast_actuals = Order::where('status', OrderStatusEnum::FINISH)
  81.                             ->select('cc_id', \DB::raw('SUM(tot_price) as total'))
  82.                             ->groupBy('cc_id')
  83.                             ->get();
  84.         // dd($forecast_orders);
  85.         $total_forecast = [];
  86.  
  87.         foreach ($forecast_actuals as $actual) {
  88.             // return dd($actual->toArray());
  89.             $cc_actual = $actual['cc_id'];
  90.             $cc_name = \DB::table('exp_cc')->where('id', $cc_actual)->get()->first()->cc;
  91.             $temp_forecast = 0;
  92.             for ($i=0; $i < count($forecast_orders); $i++) {
  93.                 if ($forecast_orders[$i]['cc_id'] == $cc_actual) {
  94.                     $temp_forecast = $forecast_orders[$i]['total'];
  95.                     break;
  96.                 }
  97.             }
  98.             // $budget_group_by_cc[$cc_name]['grand_total'] = intval($actual->total) + intval($temp_forecast);
  99.             $total_forecast[$cc_name] = ['cc_id' => $cc_actual, 'cc_name' => $cc_name, 'forecast_total' => intval($actual->total) + intval($temp_forecast)];
  100.         }
  101.  
  102.         $total_budget = Budget::groupBy('cc_id')->select('cc_id', \DB::raw('SUM(amount) as total_amount'))->get()->toArray();
  103.         foreach ($total_budget as $key => $value) {
  104.             $total_budget[$key]['cc_name'] = \DB::table('exp_cc')->where('id', $value['cc_id'])->get()->first()->cc;
  105.         }
  106.  
  107.         $total_budget_key = array_column($total_budget, 'cc_name');
  108.         $total_budget = array_combine($total_budget_key, $total_budget);
  109.         // dd($total_budget);
  110.         // dd(array_filter($total_forecast, function($item) {
  111.         //     return $item['cc_name'] == 9557;
  112.         // })[9557]['forecast_total']);
  113.         // dd($forecast_orders->toArray());
  114.        
  115.         return view('emails.expenses.budget-plan-actual', compact('budget_group_by_cc', 'total_forecast', 'total_budget'));
  116.         return view('test-view', compact('budget_group_by_cc'));
  117.         // dd($monthly_budget->toArray());
  118.         // dd($ori_budget->toArray());
  119.         // dd($map_budget);
  120.         // dd($budget);
  121.         // dd($budget->toArray());
  122.         // dd($orders->toArray());
  123.         // dd($this_month);
  124.         // dd($test);
  125.     }
  126. }
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement