From 58ae950a719f0b0dab9a4259a0342e345d33b6fa Mon Sep 17 00:00:00 2001 From: JiaSheng Date: Fri, 8 Sep 2023 14:49:39 +0800 Subject: [PATCH 1/2] bulk download invoices --- .../BulkDownloadCustomerInvoicesLogic.php | 55 +++++++++++++++++++ ...BulkDownloadCustomerInvoicesController.php | 20 +++++++ .../customer_invoices_bulk_download.blade.php | 35 ++++++++++++ routes/web.php | 6 ++ 4 files changed, 116 insertions(+) create mode 100644 app/Classes/Modules/Companies/ControllersLogic/BulkDownloadCustomerInvoicesLogic.php create mode 100644 app/Http/Controllers/Companies/BulkDownloadCustomerInvoicesController.php create mode 100644 resources/views/pages/customer_invoices_bulk_download.blade.php diff --git a/app/Classes/Modules/Companies/ControllersLogic/BulkDownloadCustomerInvoicesLogic.php b/app/Classes/Modules/Companies/ControllersLogic/BulkDownloadCustomerInvoicesLogic.php new file mode 100644 index 00000000..25821b03 --- /dev/null +++ b/app/Classes/Modules/Companies/ControllersLogic/BulkDownloadCustomerInvoicesLogic.php @@ -0,0 +1,55 @@ +input('marking'))->first(); + + $startDate = $request->input('startDate'); + $endDate = $request->input('endDate'); + if ($company) { + $invoicebookings = $company->bookings()->whereDate('created_at', '>=', $startDate) + ->whereDate('created_at', '<=', $endDate) + ->whereHas('transactions', function ($query) { + $query->where('transactions.type', TransactionType::INVOICE) + ->whereIn('transactions.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); + })->orderBy('created_at')->get(); + + + $zip_file = "invoices_{$startDate}_to_{$endDate}_{$company->reference}.zip"; + + $zip = new ZipArchive(); + if ($zip->open($zip_file, ZIPARCHIVE::CREATE | ZipArchive::OVERWRITE)) { + foreach($invoicebookings as $booking) { + $invoice_file = $booking->documents()->where('document_type', DocumentType::INVOICE)->whereNull('deleted_at')->first()->files()->first(); + $zip->addFile(Storage::disk('documents')->path($invoice_file->file->file_info->original->file), 'invoice-' . $booking->created_at->format('d_m_Y') . '_' . $booking->marking . '.pdf'); + } + + $zip->close(); + while (ob_get_level()) { + ob_end_clean(); + } + + return response()->download($zip_file); + } + } + } +} diff --git a/app/Http/Controllers/Companies/BulkDownloadCustomerInvoicesController.php b/app/Http/Controllers/Companies/BulkDownloadCustomerInvoicesController.php new file mode 100644 index 00000000..4d6944d4 --- /dev/null +++ b/app/Http/Controllers/Companies/BulkDownloadCustomerInvoicesController.php @@ -0,0 +1,20 @@ +execute($request); + } + +} \ No newline at end of file diff --git a/resources/views/pages/customer_invoices_bulk_download.blade.php b/resources/views/pages/customer_invoices_bulk_download.blade.php new file mode 100644 index 00000000..faae46a5 --- /dev/null +++ b/resources/views/pages/customer_invoices_bulk_download.blade.php @@ -0,0 +1,35 @@ +@extends('layouts.base_portal') +@section('inner_content') +
+
+
+
+
+ @csrf +
+
+ + +
+
+
+ + +
+
+
+
+ + +
+
+
+ +
+
+
+
+
+
+
+@endsection diff --git a/routes/web.php b/routes/web.php index a9e51a50..3a541f2d 100644 --- a/routes/web.php +++ b/routes/web.php @@ -141,6 +141,12 @@ Route::get('/purchase_orders', function () { return view('pages.purchase_orders'); })->name('purchase_orders'); +Route::get('/customers/invoices', function () { + return view('pages.customer_invoices_bulk_download'); +})->name('customers.invoices'); + +Route::post('/customers/invoices', 'Companies\BulkDownloadCustomerInvoicesController@download')->name('customers.invoices'); + Route::get('/support', function () { return view('pages.customer_support', [ 'marking' => null, From be2e00297b1dc379bf0adb105097a0f6f4f0d85f Mon Sep 17 00:00:00 2001 From: JiaSheng Date: Mon, 11 Sep 2023 20:43:21 +0800 Subject: [PATCH 2/2] fix if customer or no invoices found --- .../BulkDownloadCustomerInvoicesLogic.php | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/app/Classes/Modules/Companies/ControllersLogic/BulkDownloadCustomerInvoicesLogic.php b/app/Classes/Modules/Companies/ControllersLogic/BulkDownloadCustomerInvoicesLogic.php index 25821b03..1c9caddb 100644 --- a/app/Classes/Modules/Companies/ControllersLogic/BulkDownloadCustomerInvoicesLogic.php +++ b/app/Classes/Modules/Companies/ControllersLogic/BulkDownloadCustomerInvoicesLogic.php @@ -34,22 +34,28 @@ class BulkDownloadCustomerInvoicesLogic })->orderBy('created_at')->get(); - $zip_file = "invoices_{$startDate}_to_{$endDate}_{$company->reference}.zip"; - - $zip = new ZipArchive(); - if ($zip->open($zip_file, ZIPARCHIVE::CREATE | ZipArchive::OVERWRITE)) { - foreach($invoicebookings as $booking) { - $invoice_file = $booking->documents()->where('document_type', DocumentType::INVOICE)->whereNull('deleted_at')->first()->files()->first(); - $zip->addFile(Storage::disk('documents')->path($invoice_file->file->file_info->original->file), 'invoice-' . $booking->created_at->format('d_m_Y') . '_' . $booking->marking . '.pdf'); - } - - $zip->close(); - while (ob_get_level()) { - ob_end_clean(); - } + if (count($invoicebookings) > 0) { + $zip_file = "invoices_{$startDate}_to_{$endDate}_{$company->reference}.zip"; - return response()->download($zip_file); + $zip = new ZipArchive(); + if ($zip->open($zip_file, ZIPARCHIVE::CREATE | ZipArchive::OVERWRITE)) { + foreach($invoicebookings as $booking) { + $invoice_file = $booking->documents()->where('document_type', DocumentType::INVOICE)->whereNull('deleted_at')->first()->files()->first(); + $zip->addFile(Storage::disk('documents')->path($invoice_file->file->file_info->original->file), 'invoice-' . $booking->created_at->format('d_m_Y') . '_' . $booking->marking . '.pdf'); + } + + $zip->close(); + while (ob_get_level()) { + ob_end_clean(); + } + + return response()->download($zip_file); + } + } else { + return response("No invoices found for this customer in the given date range."); } + } else { + return response("Customer not found"); } } }