month) { $startDate->month($companyStartMonth)->startOfMonth(); $start->month($companyStartMonth)->startOfMonth(); $end->month($companyStartMonth)->endOfMonth(); } else { $startDate->subYear()->month($companyStartMonth)->startOfMonth(); $start->subYear()->month($companyStartMonth)->startOfMonth(); $end->subYear()->month($companyStartMonth)->endOfMonth(); } if ($previousYear) { $startDate->subYear()->startOfMonth(); $start->subYear()->startOfMonth(); $end->subYear()->endOfMonth(); } while ($monthCounter < 12) { $invoiceTotals[] = Invoice::whereBetween( 'invoice_date', [$start->format('Y-m-d'), $end->format('Y-m-d')] ) ->whereCompany() ->whereCustomer($customer->id) ->sum('base_total') ?? 0; $expenseTotals[] = Expense::whereBetween( 'expense_date', [$start->format('Y-m-d'), $end->format('Y-m-d')] ) ->whereCompany() ->whereUser($customer->id) ->sum('base_amount') ?? 0; $receiptTotals[] = Payment::whereBetween( 'payment_date', [$start->format('Y-m-d'), $end->format('Y-m-d')] ) ->whereCompany() ->whereCustomer($customer->id) ->sum('base_amount') ?? 0; $netProfits[] = $receiptTotals[$i] - $expenseTotals[$i]; $i++; $months[] = $start->translatedFormat('M'); $monthCounter++; $end->startOfMonth(); $start->addMonth()->startOfMonth(); $end->addMonth()->endOfMonth(); } $start->subMonth()->endOfMonth(); $salesTotal = Invoice::whereBetween( 'invoice_date', [$startDate->format('Y-m-d'), $start->format('Y-m-d')] ) ->whereCompany() ->whereCustomer($customer->id) ->sum('base_total'); $totalReceipts = Payment::whereBetween( 'payment_date', [$startDate->format('Y-m-d'), $start->format('Y-m-d')] ) ->whereCompany() ->whereCustomer($customer->id) ->sum('base_amount'); $totalExpenses = Expense::whereBetween( 'expense_date', [$startDate->format('Y-m-d'), $start->format('Y-m-d')] ) ->whereCompany() ->whereUser($customer->id) ->sum('base_amount'); return [ 'months' => $months, 'invoiceTotals' => $invoiceTotals, 'expenseTotals' => $expenseTotals, 'receiptTotals' => $receiptTotals, 'netProfit' => (int) $totalReceipts - (int) $totalExpenses, 'netProfits' => $netProfits, 'salesTotal' => $salesTotal, 'totalReceipts' => $totalReceipts, 'totalExpenses' => $totalExpenses, ]; } }