Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use App\Models\Expense\Budget;
- use App\Models\Expense\Order;
- use App\Models\Expense\MonthlyBudget;
- use App\Enum\OrderStatusEnum;
- class TestingController extends Controller
- {
- /**
- * Handle the incoming request.
- */
- public function __invoke(Request $request)
- {
- $this_month = intval(date('n'));
- $this_year = date('Y');
- /* --------------------------------- Budget --------------------------------- */
- $ori_budget = Budget::with(['cc:id,cc'])->where('year', $this_year)->groupBy('id')->select('id', 'amount', 'exp_no', 'cc_id', 'theme')->get();
- $budget = [];
- foreach ($ori_budget as $item) {
- $budget[$item->id] = [
- 'cc_name' => $item->cc['cc'],
- 'exp_no' => $item->exp_no,
- 'theme' => $item->theme,
- 'amount' => $item->amount
- ];
- }
- /* ----------------------------- Monthly Budget ----------------------------- */
- $monthly_budget = MonthlyBudget::where('month', '<=', $this_month)->whereYear('created_at', $this_year)->groupBy('budget_id')
- ->select('budget_id', \DB::raw('SUM(amount) as total'))
- ->get()
- ->pluck('total', 'budget_id');
- /* --------------------------------- Orders --------------------------------- */
- $orders = Order::where('status', OrderStatusEnum::FINISH)->where('act_eta', '<=', $this_month)
- ->groupBy('budget_id')->select('budget_id', \DB::raw('SUM(tot_price) as total'))
- ->get()
- ->pluck('total', 'budget_id');
- /* -------------------------------- All Joint ------------------------------- */
- $map_budget = [];
- $map_by_cc = [];
- foreach ($budget as $key => $value) {
- $act_order = $orders->filter(function($value, $key_order) use($key){
- return $key_order == $key;
- });
- $act_budget_now = $monthly_budget->filter(function($value, $key_monthly) use($key){
- return $key_monthly == $key;
- });
- $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)];
- $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)];
- }
- $budget_group_by_cc = [];
- foreach ($map_by_cc as $value) {
- $cc_name = $value['cc_name'];
- if (! isset($budget_group_by_cc[$cc_name])) {
- $budget_group_by_cc[$cc_name] = [];
- }
- $budget_group_by_cc[$cc_name][] = $value;
- }
- // dd($budget_group_by_cc);
- $forecast_orders = Order::where('status', OrderStatusEnum::PROGRESS)
- ->select('cc_id', \DB::raw('SUM(tot_price) as total'))
- ->groupBy('cc_id')
- ->get()->toArray();
- $forecast_actuals = Order::where('status', OrderStatusEnum::FINISH)
- ->select('cc_id', \DB::raw('SUM(tot_price) as total'))
- ->groupBy('cc_id')
- ->get();
- // dd($forecast_orders);
- $total_forecast = [];
- foreach ($forecast_actuals as $actual) {
- // return dd($actual->toArray());
- $cc_actual = $actual['cc_id'];
- $cc_name = \DB::table('exp_cc')->where('id', $cc_actual)->get()->first()->cc;
- $temp_forecast = 0;
- for ($i=0; $i < count($forecast_orders); $i++) {
- if ($forecast_orders[$i]['cc_id'] == $cc_actual) {
- $temp_forecast = $forecast_orders[$i]['total'];
- break;
- }
- }
- // $budget_group_by_cc[$cc_name]['grand_total'] = intval($actual->total) + intval($temp_forecast);
- $total_forecast[$cc_name] = ['cc_id' => $cc_actual, 'cc_name' => $cc_name, 'forecast_total' => intval($actual->total) + intval($temp_forecast)];
- }
- $total_budget = Budget::groupBy('cc_id')->select('cc_id', \DB::raw('SUM(amount) as total_amount'))->get()->toArray();
- foreach ($total_budget as $key => $value) {
- $total_budget[$key]['cc_name'] = \DB::table('exp_cc')->where('id', $value['cc_id'])->get()->first()->cc;
- }
- $total_budget_key = array_column($total_budget, 'cc_name');
- $total_budget = array_combine($total_budget_key, $total_budget);
- // dd($total_budget);
- // dd(array_filter($total_forecast, function($item) {
- // return $item['cc_name'] == 9557;
- // })[9557]['forecast_total']);
- // dd($forecast_orders->toArray());
- return view('emails.expenses.budget-plan-actual', compact('budget_group_by_cc', 'total_forecast', 'total_budget'));
- return view('test-view', compact('budget_group_by_cc'));
- // dd($monthly_budget->toArray());
- // dd($ori_budget->toArray());
- // dd($map_budget);
- // dd($budget);
- // dd($budget->toArray());
- // dd($orders->toArray());
- // dd($this_month);
- // dd($test);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement