mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-20 13:04:02 +00:00
67 lines
2.6 KiB
PHP
67 lines
2.6 KiB
PHP
<?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()->json([
|
|
'status' => 'Failed',
|
|
'message' => 'No invoices found for this customer in the given date range.',
|
|
]);
|
|
}
|
|
} else {
|
|
return response()->json([
|
|
'status' => 'Failed',
|
|
'message' => 'Customer not found.',
|
|
]);
|
|
}
|
|
}
|
|
} |