Merge branch 'bulk-download-invoices' of gitlab.com:CIEFWorldwideSdnBhd/exchange-2.0

This commit is contained in:
edmondlang
2023-09-30 14:24:04 +08:00
4 changed files with 122 additions and 0 deletions
@@ -0,0 +1,61 @@
<?php
namespace App\Classes\Modules\Companies\ControllersLogic;
use App\Classes\ValueObjects\Constants\TransactionType;
use ZipArchive;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\Classes\Exceptions\MalformedRequestException;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\DocumentType;
use App\Models\Company;
class BulkDownloadCustomerInvoicesLogic
{
/**
* @param Request $request
* @return void
* @throws MalformedRequestException
*/
public function execute(Request $request)
{
$company = Company::where('reference', $request->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();
if (count($invoicebookings) > 0) {
$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);
}
} else {
return response("No invoices found for this customer in the given date range.");
}
} else {
return response("Customer not found");
}
}
}
@@ -0,0 +1,20 @@
<?php
namespace App\Http\Controllers\Companies;
use App\Classes\Modules\Companies\ControllersLogic\BulkDownloadCustomerInvoicesLogic;
use Illuminate\Http\Request;
class BulkDownloadCustomerInvoicesController
{
/**
* @param Request $request
* @param BulkDownloadCustomerInvoicesLogic $logic
* @return void
* @throws \App\Classes\Exceptions\MalformedRequestException
*/
public function download(Request $request, BulkDownloadCustomerInvoicesLogic $logic) {
return $logic->execute($request);
}
}
@@ -0,0 +1,35 @@
@extends('layouts.base_portal')
@section('inner_content')
<div class="row">
<div class="col bg-white p-t-15 p-b-15">
<div class="row no-margin">
<div class="col-12">
<form method="post" >
@csrf
<div class="row">
<div class="col">
<label for="marking">Marking:</label>
<input class="form-control" type="text" id="marking" name="marking" placeholder="Marking" required>
</div>
<div class="col">
<div class="row">
<label for="startDate">Start Date:</label>
<input class="form-control" type="date" id="startDate" name="startDate" placeholder="Start Date" required>
</div>
</div>
<div class="col">
<div class="row">
<label for="endDate">End Date:</label>
<input class="form-control" type="date" id="endDate" name="endDate" placeholder="End Date" required>
</div>
</div>
<div class="col-auto row align-items-center">
<button class="btn btn-complete" type="submit" formtarget="_blank">Search</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
@endsection
+6
View File
@@ -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,