diff --git a/app/Classes/Modules/Exports/Services/ExportsSalesInvoicesReport.php b/app/Classes/Modules/Exports/Services/ExportsSalesInvoicesReport.php index 49f8f540..b3681930 100644 --- a/app/Classes/Modules/Exports/Services/ExportsSalesInvoicesReport.php +++ b/app/Classes/Modules/Exports/Services/ExportsSalesInvoicesReport.php @@ -72,7 +72,7 @@ class ExportsSalesInvoicesReport implements FromQuery, WithHeadings, WithHeading 'owner.owner.companyModule.employees' ]); - + /* if($this->isUnpaid){ $approvalStatus = ApprovalStatus::COMPLETED; $query->whereIn('type', [TransactionType::SHIPPING_INVOICE, TransactionType::STORAGE_INVOICE]) @@ -84,38 +84,57 @@ class ExportsSalesInvoicesReport implements FromQuery, WithHeadings, WithHeading $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 ($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]) - // ->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]); - // } + 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->whereNotIn('status', [$approvalStatus]) + ->whereBetween('created_at', [$start_date, $end_date]); + }) + + ->orWhere(function ($sub) use ($approvalStatus, $start_date, $end_date) { + $sub->where('status', $approvalStatus) + ->whereBetween('created_at', [$start_date, $end_date]); + }); + }); + + } return $query; } @@ -123,6 +142,8 @@ class ExportsSalesInvoicesReport implements FromQuery, WithHeadings, WithHeading 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[];