mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-23 14:33:59 +00:00
test bulk download white form
This commit is contained in:
+51
-26
@@ -10,6 +10,7 @@ use App\Classes\ValueObjects\Constants\DocumentType;
|
||||
use App\Models\Company;
|
||||
use App\Models\Group;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class BulkDownloadSupplierWhiteFormsLogic
|
||||
{
|
||||
@@ -20,33 +21,57 @@ class BulkDownloadSupplierWhiteFormsLogic
|
||||
*/
|
||||
public function execute(Request $request)
|
||||
{
|
||||
$startDate = Carbon::createFromFormat('d-m-Y', $request->input('startDate'))->startOfDay();
|
||||
$endDate = Carbon::createFromFormat('d-m-Y', $request->input('endDate'))->endOfDay();
|
||||
|
||||
$companyReference = Company::find($request->input('supplier'))->reference;
|
||||
$groups = Group::where('issuer', $request->input('supplier'))
|
||||
->whereDate('created_at', '>=', $startDate)
|
||||
->whereDate('created_at', '<=', $endDate)
|
||||
->orderBy('created_at', 'DESC')
|
||||
->get();
|
||||
|
||||
if (count($groups) > 0) {
|
||||
$zip_file = "currency_vendor_orders_{$request->input('startDate')}_to_{$request->input('endDate')}_{$companyReference}.zip";
|
||||
|
||||
$zip = new ZipArchive();
|
||||
if ($zip->open($zip_file, ZIPARCHIVE::CREATE | ZipArchive::OVERWRITE)) {
|
||||
foreach($groups as $group) {
|
||||
$document = $group->documents()->where('document_type', DocumentType::CURRENCY_VENDOR_ORDER)->whereNull('deleted_at')->first()->files()->first();
|
||||
$zip->addFile(Storage::disk('documents')->path($document->file->file_info->original->file), $group->reference . '.pdf');
|
||||
}
|
||||
try {
|
||||
|
||||
$startDate = Carbon::createFromFormat('d-m-Y', $request->input('startDate'))->startOfDay();
|
||||
$endDate = Carbon::createFromFormat('d-m-Y', $request->input('endDate'))->endOfDay();
|
||||
|
||||
$zip->close();
|
||||
while (ob_get_level()) {
|
||||
ob_end_clean();
|
||||
}
|
||||
$companyReference = Company::find($request->input('supplier'))->reference;
|
||||
$groups = Group::where('issuer', $request->input('supplier'))
|
||||
->whereDate('created_at', '>=', $startDate)
|
||||
->whereDate('created_at', '<=', $endDate)
|
||||
->orderBy('created_at', 'DESC')
|
||||
->get();
|
||||
|
||||
if (count($groups) > 0) {
|
||||
|
||||
return response()->download($zip_file);
|
||||
}
|
||||
}
|
||||
$zipDirectory = storage_path('app/bulk_whiteform'); // Update this with the actual directory path
|
||||
|
||||
if (!file_exists($zipDirectory)) {
|
||||
mkdir($zipDirectory, 0755, true);
|
||||
}
|
||||
|
||||
$zip_file = "{$zipDirectory}/currency_vendor_orders_{$request->input('startDate')}_to_{$request->input('endDate')}_{$companyReference}.zip";
|
||||
|
||||
$zip = new ZipArchive();
|
||||
if ($zip->open($zip_file, ZIPARCHIVE::CREATE | ZipArchive::OVERWRITE)) {
|
||||
foreach($groups as $group) {
|
||||
$document = $group->documents()->where('document_type', DocumentType::CURRENCY_VENDOR_ORDER)->whereNull('deleted_at')->first()->files()->first();
|
||||
$zip->addFile(Storage::disk('documents')->path($document->file->file_info->original->file), $group->reference . '.pdf');
|
||||
}
|
||||
|
||||
$zip->close();
|
||||
while (ob_get_level()) {
|
||||
ob_end_clean();
|
||||
}
|
||||
|
||||
return response()->download($zip_file);
|
||||
}
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'status' => 'Failed',
|
||||
'message' => 'No white form found for this supplier in the given date range.',
|
||||
]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
// Log the exception for debugging
|
||||
Log::error('Error in BulkDownloadSupplierWhiteFormsLogic: ' . $e->getMessage());
|
||||
|
||||
return response()->json([
|
||||
'status' => 'Failed',
|
||||
'message' => 'An error occurred while processing the request.',
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user