From c7f00a7ce2b80ad408a5ea62339ed95fe2b68cc3 Mon Sep 17 00:00:00 2001 From: Dillon Ngo Date: Thu, 23 Oct 2025 18:17:56 +0800 Subject: [PATCH] E-Invoice - Update Sales Invoice Report to have paid and unpaid invoices in the report --- .../Services/ExportsSalesInvoicesReport.php | 138 +++++++++--------- 1 file changed, 68 insertions(+), 70 deletions(-) diff --git a/app/Classes/Modules/Exports/Services/ExportsSalesInvoicesReport.php b/app/Classes/Modules/Exports/Services/ExportsSalesInvoicesReport.php index 2ad48c6c..50a07651 100644 --- a/app/Classes/Modules/Exports/Services/ExportsSalesInvoicesReport.php +++ b/app/Classes/Modules/Exports/Services/ExportsSalesInvoicesReport.php @@ -73,39 +73,6 @@ class ExportsSalesInvoicesReport implements FromQuery, WithHeadings, WithHeading ]); - // if($this->isUnpaid){ - // $approvalStatus = ApprovalStatus::COMPLETED; - // $query->whereIn('type', [TransactionType::SHIPPING_INVOICE, TransactionType::STORAGE_INVOICE]) - // ->whereNotIn('status', [$approvalStatus]) - // ->whereBetween('created_at', [$start_date, $end_date]); - // } - // else{ - // $approvalStatus = ApprovalStatus::COMPLETED; - // $query->whereIn('type', [TransactionType::SHIPPING_INVOICE, TransactionType::STORAGE_INVOICE]) - // ->where('status', $approvalStatus); - - // if ($start_date && $end_date) { - // $query->whereHas('transactions', function($transaction) use ($start_date, $end_date) { - // $transaction->where('type', TransactionType::PAYMENT) - // ->whereBetween('updated_at', [$start_date, $end_date]) - // ->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); - // }); - // } elseif ($start_date) { - // $query->whereHas('transactions', function($transaction) use ($start_date) { - // $transaction->where('type', TransactionType::PAYMENT) - // ->where('updated_at', '>=', $start_date) - // ->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); - // }); - // } elseif ($end_date) { - // $query->whereHas('transactions', function($transaction) use ($end_date) { - // $transaction->where('type', TransactionType::PAYMENT) - // ->where('updated_at', '<=', $end_date) - // ->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); - // }); - // } - // } - - if($this->isUnpaid){ $approvalStatus = ApprovalStatus::COMPLETED; $query->whereIn('type', [TransactionType::SHIPPING_INVOICE, TransactionType::STORAGE_INVOICE]) @@ -113,54 +80,85 @@ class ExportsSalesInvoicesReport implements FromQuery, WithHeadings, WithHeading ->whereBetween('created_at', [$start_date, $end_date]); } else{ - //This will query everything both paid and unpaid unlike previously paid only - // $query->whereIn('type', [TransactionType::SHIPPING_INVOICE, TransactionType::STORAGE_INVOICE]) - // ->whereBetween('created_at', [$start_date, $end_date]); - $approvalStatus = ApprovalStatus::COMPLETED; - $invoiceTypes = [TransactionType::SHIPPING_INVOICE, TransactionType::STORAGE_INVOICE]; + $query->whereIn('type', [TransactionType::SHIPPING_INVOICE, TransactionType::STORAGE_INVOICE]) + ->where('status', $approvalStatus); - $query->whereIn('type', $invoiceTypes) - ->where(function ($q) use ($approvalStatus, $start_date, $end_date) { - - $q->where(function ($sub) use ($approvalStatus, $start_date, $end_date) { - $sub->where('status', $approvalStatus); - if ($start_date && $end_date) { - $sub->whereHas('transactions', function($transaction) use ($start_date, $end_date) { - $transaction->where('type', TransactionType::PAYMENT) - ->whereBetween('updated_at', [$start_date, $end_date]) - ->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); - }); - } elseif ($start_date) { - $sub->whereHas('transactions', function($transaction) use ($start_date) { - $transaction->where('type', TransactionType::PAYMENT) - ->where('updated_at', '>=', $start_date) - ->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); - }); - } elseif ($end_date) { - $sub->whereHas('transactions', function($transaction) use ($end_date) { - $transaction->where('type', TransactionType::PAYMENT) - ->where('updated_at', '<=', $end_date) - ->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); - }); - } + if ($start_date && $end_date) { + $query->whereHas('transactions', function($transaction) use ($start_date, $end_date) { + $transaction->where('type', TransactionType::PAYMENT) + ->whereBetween('updated_at', [$start_date, $end_date]) + ->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); }); - - // ->orWhere(function ($sub) use ($approvalStatus, $start_date, $end_date) { - // $sub->whereNotIn('status', [$approvalStatus]) - // ->whereBetween('created_at', [$start_date, $end_date]); - // }); - }); + } elseif ($start_date) { + $query->whereHas('transactions', function($transaction) use ($start_date) { + $transaction->where('type', TransactionType::PAYMENT) + ->where('updated_at', '>=', $start_date) + ->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); + }); + } elseif ($end_date) { + $query->whereHas('transactions', function($transaction) use ($end_date) { + $transaction->where('type', TransactionType::PAYMENT) + ->where('updated_at', '<=', $end_date) + ->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); + }); + } } + + // if($this->isUnpaid){ + // $approvalStatus = ApprovalStatus::COMPLETED; + // $query->whereIn('type', [TransactionType::SHIPPING_INVOICE, TransactionType::STORAGE_INVOICE]) + // ->whereNotIn('status', [$approvalStatus]) + // ->whereBetween('created_at', [$start_date, $end_date]); + // } + // else{ + // //This will query everything both paid and unpaid unlike previously paid only + // // $query->whereIn('type', [TransactionType::SHIPPING_INVOICE, TransactionType::STORAGE_INVOICE]) + // // ->whereBetween('created_at', [$start_date, $end_date]); + + // $approvalStatus = ApprovalStatus::COMPLETED; + // $invoiceTypes = [TransactionType::SHIPPING_INVOICE, TransactionType::STORAGE_INVOICE]; + + // $query->whereIn('type', $invoiceTypes) + // ->where(function ($q) use ($approvalStatus, $start_date, $end_date) { + + // $q->where(function ($sub) use ($approvalStatus, $start_date, $end_date) { + // $sub->where('status', $approvalStatus); + // if ($start_date && $end_date) { + // $sub->whereHas('transactions', function($transaction) use ($start_date, $end_date) { + // $transaction->where('type', TransactionType::PAYMENT) + // ->whereBetween('updated_at', [$start_date, $end_date]) + // ->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); + // }); + // } elseif ($start_date) { + // $sub->whereHas('transactions', function($transaction) use ($start_date) { + // $transaction->where('type', TransactionType::PAYMENT) + // ->where('updated_at', '>=', $start_date) + // ->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); + // }); + // } elseif ($end_date) { + // $sub->whereHas('transactions', function($transaction) use ($end_date) { + // $transaction->where('type', TransactionType::PAYMENT) + // ->where('updated_at', '<=', $end_date) + // ->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); + // }); + // } + // }); + + // // ->orWhere(function ($sub) use ($approvalStatus, $start_date, $end_date) { + // // $sub->whereNotIn('status', [$approvalStatus]) + // // ->whereBetween('created_at', [$start_date, $end_date]); + // // }); + // }); + // } + return $query; } public function map($transaction): array { - Log::info('Transaction: ' . json_encode($transaction)); - $container = $transaction->owner ? $transaction->owner->containers->first() : null; $packingList = $transaction->owner; if(!$packingList) return[];