add test batches
This commit is contained in:
@@ -10,7 +10,8 @@ class FinancialSummaryService
|
||||
{
|
||||
public function __construct(
|
||||
private FinancialDonationRecipientService $donationRecipients
|
||||
) {}
|
||||
) {
|
||||
}
|
||||
|
||||
public function getSummary(?string $dateFrom, ?string $dateTo, ?string $schoolYear): array
|
||||
{
|
||||
@@ -18,10 +19,10 @@ class FinancialSummaryService
|
||||
$derivedDateFrom = null;
|
||||
$derivedDateTo = null;
|
||||
|
||||
if (! empty($schoolYear) && empty($dateFrom) && empty($dateTo)) {
|
||||
if (!empty($schoolYear) && empty($dateFrom) && empty($dateTo)) {
|
||||
if (preg_match('/^(\\d{4})[^\\d]?(\\d{4})$/', $schoolYear, $m)) {
|
||||
$derivedDateFrom = $m[1].'-01-01';
|
||||
$derivedDateTo = $m[2].'-12-31';
|
||||
$derivedDateFrom = $m[1] . '-01-01';
|
||||
$derivedDateTo = $m[2] . '-12-31';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,10 +30,10 @@ class FinancialSummaryService
|
||||
$invoiceDateTo = $dateTo ?: $derivedDateTo;
|
||||
|
||||
$invoiceQuery = DB::table('invoices')->where('school_year', $schoolYear);
|
||||
if (! empty($invoiceDateFrom)) {
|
||||
if (!empty($invoiceDateFrom)) {
|
||||
$invoiceQuery->whereRaw('DATE(COALESCE(issue_date, created_at)) >= ?', [$invoiceDateFrom]);
|
||||
}
|
||||
if (! empty($invoiceDateTo)) {
|
||||
if (!empty($invoiceDateTo)) {
|
||||
$invoiceQuery->whereRaw('DATE(COALESCE(issue_date, created_at)) <= ?', [$invoiceDateTo]);
|
||||
}
|
||||
$invoices = $invoiceQuery->get()->map(fn ($row) => (array) $row)->all();
|
||||
@@ -40,7 +41,6 @@ class FinancialSummaryService
|
||||
|
||||
$invoiceIds = array_values(array_unique(array_filter(array_map(static function ($row) {
|
||||
$id = (int) ($row['id'] ?? 0);
|
||||
|
||||
return $id > 0 ? $id : null;
|
||||
}, $invoices))));
|
||||
|
||||
@@ -54,7 +54,7 @@ class FinancialSummaryService
|
||||
$paidByInvoice = [];
|
||||
$discountByInvoice = [];
|
||||
$refundByInvoice = [];
|
||||
if (! empty($invoiceIds)) {
|
||||
if (!empty($invoiceIds)) {
|
||||
$paidByInvoice = $this->paymentsByInvoiceIds($invoiceIds, $schoolYear, $invoiceDateFrom, $invoiceDateTo);
|
||||
$discountByInvoice = $this->discountsByInvoiceIds($invoiceIds, $dateFrom, $dateTo);
|
||||
$refundByInvoice = $this->refundsByInvoiceIds($invoiceIds, $dateFrom, $dateTo);
|
||||
@@ -99,15 +99,14 @@ class FinancialSummaryService
|
||||
->where('ac.school_year', $schoolYear)
|
||||
->where('ac.status', '!=', 'void');
|
||||
|
||||
if (! empty($dateFrom)) {
|
||||
if (!empty($dateFrom)) {
|
||||
$query->whereRaw('DATE(ac.due_date) >= ?', [$dateFrom]);
|
||||
}
|
||||
if (! empty($dateTo)) {
|
||||
if (!empty($dateTo)) {
|
||||
$query->whereRaw('DATE(ac.due_date) <= ?', [$dateTo]);
|
||||
}
|
||||
|
||||
$row = (array) $query->first();
|
||||
|
||||
return (float) ($row['amount'] ?? 0);
|
||||
}
|
||||
|
||||
@@ -124,10 +123,10 @@ class FinancialSummaryService
|
||||
})
|
||||
->groupBy('ac.parent_id');
|
||||
|
||||
if (! empty($dateFrom)) {
|
||||
if (!empty($dateFrom)) {
|
||||
$query->whereRaw('DATE(ac.due_date) >= ?', [$dateFrom]);
|
||||
}
|
||||
if (! empty($dateTo)) {
|
||||
if (!empty($dateTo)) {
|
||||
$query->whereRaw('DATE(ac.due_date) <= ?', [$dateTo]);
|
||||
}
|
||||
|
||||
@@ -147,10 +146,10 @@ class FinancialSummaryService
|
||||
private function sumPayments(string $schoolYear, ?string $dateFrom, ?string $dateTo): float
|
||||
{
|
||||
$query = DB::table('payments')->where('school_year', $schoolYear);
|
||||
if (! empty($dateFrom)) {
|
||||
if (!empty($dateFrom)) {
|
||||
$query->whereRaw('DATE(payment_date) >= ?', [$dateFrom]);
|
||||
}
|
||||
if (! empty($dateTo)) {
|
||||
if (!empty($dateTo)) {
|
||||
$query->whereRaw('DATE(payment_date) <= ?', [$dateTo]);
|
||||
}
|
||||
|
||||
@@ -167,7 +166,6 @@ class FinancialSummaryService
|
||||
}
|
||||
|
||||
$row = (array) $query->selectRaw('COALESCE(SUM(paid_amount), 0) AS paid_amount')->first();
|
||||
|
||||
return (float) ($row['paid_amount'] ?? 0);
|
||||
}
|
||||
|
||||
@@ -178,10 +176,10 @@ class FinancialSummaryService
|
||||
->whereIn('invoice_id', $invoiceIds)
|
||||
->where('school_year', $schoolYear);
|
||||
|
||||
if (! empty($dateFrom)) {
|
||||
if (!empty($dateFrom)) {
|
||||
$query->whereRaw('DATE(payment_date) >= ?', [$dateFrom]);
|
||||
}
|
||||
if (! empty($dateTo)) {
|
||||
if (!empty($dateTo)) {
|
||||
$query->whereRaw('DATE(payment_date) <= ?', [$dateTo]);
|
||||
}
|
||||
|
||||
@@ -205,7 +203,6 @@ class FinancialSummaryService
|
||||
$map[$invoiceId] = (float) ($row['total_paid'] ?? 0);
|
||||
}
|
||||
}
|
||||
|
||||
return $map;
|
||||
}
|
||||
|
||||
@@ -215,10 +212,10 @@ class FinancialSummaryService
|
||||
->selectRaw('invoice_id, COALESCE(SUM(discount_amount),0) AS total_disc')
|
||||
->whereIn('invoice_id', $invoiceIds);
|
||||
|
||||
if (! empty($dateFrom)) {
|
||||
if (!empty($dateFrom)) {
|
||||
$query->whereRaw('DATE(COALESCE(used_at, created_at)) >= ?', [$dateFrom]);
|
||||
}
|
||||
if (! empty($dateTo)) {
|
||||
if (!empty($dateTo)) {
|
||||
$query->whereRaw('DATE(COALESCE(used_at, created_at)) <= ?', [$dateTo]);
|
||||
}
|
||||
|
||||
@@ -230,7 +227,6 @@ class FinancialSummaryService
|
||||
$map[$invoiceId] = (float) ($row['total_disc'] ?? 0);
|
||||
}
|
||||
}
|
||||
|
||||
return $map;
|
||||
}
|
||||
|
||||
@@ -241,10 +237,10 @@ class FinancialSummaryService
|
||||
->whereIn('invoice_id', $invoiceIds)
|
||||
->whereIn('status', ['Partial', 'Paid']);
|
||||
|
||||
if (! empty($dateFrom)) {
|
||||
if (!empty($dateFrom)) {
|
||||
$query->whereRaw('DATE(COALESCE(refunded_at, created_at)) >= ?', [$dateFrom]);
|
||||
}
|
||||
if (! empty($dateTo)) {
|
||||
if (!empty($dateTo)) {
|
||||
$query->whereRaw('DATE(COALESCE(refunded_at, created_at)) <= ?', [$dateTo]);
|
||||
}
|
||||
|
||||
@@ -256,22 +252,20 @@ class FinancialSummaryService
|
||||
$map[$invoiceId] = (float) ($row['total_refund'] ?? 0);
|
||||
}
|
||||
}
|
||||
|
||||
return $map;
|
||||
}
|
||||
|
||||
private function sumExpenses(string $schoolYear, ?string $dateFrom, ?string $dateTo): float
|
||||
{
|
||||
$query = DB::table('expenses')->where('school_year', $schoolYear);
|
||||
if (! empty($dateFrom)) {
|
||||
if (!empty($dateFrom)) {
|
||||
$query->whereRaw('DATE(created_at) >= ?', [$dateFrom]);
|
||||
}
|
||||
if (! empty($dateTo)) {
|
||||
if (!empty($dateTo)) {
|
||||
$query->whereRaw('DATE(created_at) <= ?', [$dateTo]);
|
||||
}
|
||||
|
||||
$row = (array) $query->selectRaw('COALESCE(SUM(amount), 0) AS amount')->first();
|
||||
|
||||
return (float) ($row['amount'] ?? 0);
|
||||
}
|
||||
|
||||
@@ -283,7 +277,7 @@ class FinancialSummaryService
|
||||
->selectRaw('COALESCE(SUM(r.amount),0) AS amount')
|
||||
->leftJoin('expenses as e', 'e.id', '=', 'r.expense_id');
|
||||
|
||||
if (! empty($schoolYear)) {
|
||||
if (!empty($schoolYear)) {
|
||||
$query->where(function ($q) use ($schoolYear, $normalizedYear) {
|
||||
$q->where(function ($q2) use ($schoolYear, $normalizedYear) {
|
||||
$q2->where('r.school_year', $schoolYear)
|
||||
@@ -304,10 +298,10 @@ class FinancialSummaryService
|
||||
});
|
||||
}
|
||||
|
||||
if (! empty($dateFrom)) {
|
||||
if (!empty($dateFrom)) {
|
||||
$query->whereRaw('DATE(COALESCE(r.created_at, e.created_at)) >= ?', [$dateFrom]);
|
||||
}
|
||||
if (! empty($dateTo)) {
|
||||
if (!empty($dateTo)) {
|
||||
$query->whereRaw('DATE(COALESCE(r.created_at, e.created_at)) <= ?', [$dateTo]);
|
||||
}
|
||||
|
||||
@@ -323,7 +317,7 @@ class FinancialSummaryService
|
||||
->whereNull('bi.unassigned_at')
|
||||
->whereNull('r.id');
|
||||
|
||||
if (! empty($schoolYear)) {
|
||||
if (!empty($schoolYear)) {
|
||||
$batchFallback->where(function ($q) use ($schoolYear, $normalizedYear) {
|
||||
$q->where('e.school_year', $schoolYear)
|
||||
->orWhereRaw(
|
||||
@@ -333,10 +327,10 @@ class FinancialSummaryService
|
||||
});
|
||||
}
|
||||
|
||||
if (! empty($dateFrom)) {
|
||||
if (!empty($dateFrom)) {
|
||||
$batchFallback->whereRaw('DATE(COALESCE(e.created_at, b.closed_at)) >= ?', [$dateFrom]);
|
||||
}
|
||||
if (! empty($dateTo)) {
|
||||
if (!empty($dateTo)) {
|
||||
$batchFallback->whereRaw('DATE(COALESCE(e.created_at, b.closed_at)) <= ?', [$dateTo]);
|
||||
}
|
||||
|
||||
@@ -352,10 +346,10 @@ class FinancialSummaryService
|
||||
->where('school_year', $schoolYear)
|
||||
->where('category', 'Donation');
|
||||
|
||||
if (! empty($dateFrom)) {
|
||||
if (!empty($dateFrom)) {
|
||||
$expenseQuery->whereRaw('DATE(created_at) >= ?', [$dateFrom]);
|
||||
}
|
||||
if (! empty($dateTo)) {
|
||||
if (!empty($dateTo)) {
|
||||
$expenseQuery->whereRaw('DATE(created_at) <= ?', [$dateTo]);
|
||||
}
|
||||
|
||||
@@ -364,14 +358,14 @@ class FinancialSummaryService
|
||||
|
||||
$donationReimb = 0.0;
|
||||
$recipientIds = $this->donationRecipients->recipientIds();
|
||||
if (! empty($recipientIds)) {
|
||||
if (!empty($recipientIds)) {
|
||||
$normalizedYear = preg_replace('/[^0-9]/', '', (string) $schoolYear);
|
||||
$donationQuery = DB::table('reimbursements as r')
|
||||
->selectRaw('COALESCE(SUM(r.amount),0) AS amount')
|
||||
->leftJoin('expenses as e', 'e.id', '=', 'r.expense_id')
|
||||
->whereIn('r.reimbursed_to', $recipientIds);
|
||||
|
||||
if (! empty($schoolYear)) {
|
||||
if (!empty($schoolYear)) {
|
||||
$donationQuery->where(function ($q) use ($schoolYear, $normalizedYear) {
|
||||
$q->where(function ($q2) use ($schoolYear, $normalizedYear) {
|
||||
$q2->where('r.school_year', $schoolYear)
|
||||
@@ -392,10 +386,10 @@ class FinancialSummaryService
|
||||
});
|
||||
}
|
||||
|
||||
if (! empty($dateFrom)) {
|
||||
if (!empty($dateFrom)) {
|
||||
$donationQuery->whereRaw('DATE(COALESCE(r.created_at, e.created_at)) >= ?', [$dateFrom]);
|
||||
}
|
||||
if (! empty($dateTo)) {
|
||||
if (!empty($dateTo)) {
|
||||
$donationQuery->whereRaw('DATE(COALESCE(r.created_at, e.created_at)) <= ?', [$dateTo]);
|
||||
}
|
||||
|
||||
@@ -416,15 +410,14 @@ class FinancialSummaryService
|
||||
->whereIn('status', ['Partial', 'Paid'])
|
||||
->whereNotNull('refund_paid_amount');
|
||||
|
||||
if (! empty($dateFrom)) {
|
||||
if (!empty($dateFrom)) {
|
||||
$query->whereRaw('DATE(COALESCE(refunded_at, created_at)) >= ?', [$dateFrom]);
|
||||
}
|
||||
if (! empty($dateTo)) {
|
||||
if (!empty($dateTo)) {
|
||||
$query->whereRaw('DATE(COALESCE(refunded_at, created_at)) <= ?', [$dateTo]);
|
||||
}
|
||||
|
||||
$row = (array) $query->selectRaw('COALESCE(SUM(refund_paid_amount),0) AS refund_paid_amount')->first();
|
||||
|
||||
return (float) ($row['refund_paid_amount'] ?? 0);
|
||||
}
|
||||
|
||||
@@ -434,15 +427,14 @@ class FinancialSummaryService
|
||||
->join('invoices as i', 'i.id', '=', 'du.invoice_id')
|
||||
->where('i.school_year', $schoolYear);
|
||||
|
||||
if (! empty($dateFrom)) {
|
||||
if (!empty($dateFrom)) {
|
||||
$query->whereRaw('DATE(COALESCE(du.used_at, du.created_at)) >= ?', [$dateFrom]);
|
||||
}
|
||||
if (! empty($dateTo)) {
|
||||
if (!empty($dateTo)) {
|
||||
$query->whereRaw('DATE(COALESCE(du.used_at, du.created_at)) <= ?', [$dateTo]);
|
||||
}
|
||||
|
||||
$row = (array) $query->selectRaw('COALESCE(SUM(du.discount_amount),0) AS discount_amount')->first();
|
||||
|
||||
return (float) ($row['discount_amount'] ?? 0);
|
||||
}
|
||||
|
||||
@@ -462,14 +454,14 @@ class FinancialSummaryService
|
||||
$ref = (float) ($refundByInvoice[$invoiceId] ?? 0);
|
||||
$balance = max(0.0, round($total - $disc - $paid - $ref, 2));
|
||||
|
||||
if (! isset($parentBalances[$parentId])) {
|
||||
if (!isset($parentBalances[$parentId])) {
|
||||
$parentBalances[$parentId] = 0.0;
|
||||
}
|
||||
$parentBalances[$parentId] += $balance;
|
||||
}
|
||||
|
||||
foreach ($extraByParent as $parentId => $extra) {
|
||||
if (! isset($parentBalances[$parentId])) {
|
||||
if (!isset($parentBalances[$parentId])) {
|
||||
$parentBalances[$parentId] = 0.0;
|
||||
}
|
||||
$parentBalances[$parentId] += (float) $extra;
|
||||
|
||||
Reference in New Issue
Block a user