mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-19 12:33:56 +00:00
104 lines
4.1 KiB
PHP
104 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Classes\Modules\Imports\ControllersLogic;
|
|
|
|
|
|
use App\Classes\Exceptions\MalformedRequestException;
|
|
use App\Classes\General\Abstracts\AbstractControllerLogic;
|
|
use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject;
|
|
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
|
|
class ImportExcelLogic extends AbstractControllerLogic
|
|
{
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
protected function notification():array {
|
|
return [
|
|
'title' => 'Import Excel',
|
|
'message' => 'You have successfully imported and updated booking details'
|
|
];
|
|
}
|
|
|
|
|
|
/**
|
|
* @param Request $request
|
|
* @return JsonResponse
|
|
* @throws MalformedRequestException
|
|
*/
|
|
public function logic(Request $request) : JsonResponse
|
|
{
|
|
$reportType = $request->input('report_type');
|
|
|
|
$object = new DocumentObject('', $request->input('files'), '', ApprovalStatus::APPROVED, 'imports');
|
|
foreach ($object->getFiles() as $file){
|
|
$collection = Excel::toCollection(null, json_decode($file)->file_info->original->file, null, null, true);
|
|
|
|
$sheet = $collection->first();
|
|
$header = $sheet->first()->toArray();
|
|
|
|
$normalizedHeader = array_map(fn($h) => strtolower(trim($h)), $header);
|
|
$salesInvoiceHeader = [
|
|
'docno', 'docdate', 'debtorcode', 'ref', 'shipinfo', 'accno',
|
|
'detaildescription', 'furtherdescription', 'classification',
|
|
'deptno', 'qty', 'unitprice', 'submiteinvoice', 'consolidatedeinvoice'
|
|
];
|
|
$customersReportHeader = [
|
|
'tin', 'identityno', 'name', 'identitytype', 'taxclassification', 'msiccode',
|
|
'businessactivitydesc', 'debtorcode', 'tradename', 'address', 'postcode',
|
|
'phone', 'emailaddress', 'city', 'countrycode', 'statecode'
|
|
];
|
|
if (
|
|
($reportType === 'Sales Invoice Report' && $normalizedHeader !== $salesInvoiceHeader) ||
|
|
($reportType === 'Customers Report' && $normalizedHeader !== $customersReportHeader)
|
|
) {
|
|
throw new MalformedRequestException('Uploaded Excel file format is incorrect. Column headers do not match expected format.');
|
|
}
|
|
$rows = $sheet->skip(1);
|
|
|
|
foreach ($rows as $index => $details) {
|
|
$docNo = $details[0] ?? null;
|
|
$docDate = $details[1] ?? null;
|
|
$debtorCode = $details[2] ?? null;
|
|
$ref = $details[3] ?? null;
|
|
$shipInfo = $details[4] ?? null;
|
|
$accNo = $details[5] ?? null;
|
|
$detailDescription = $details[6] ?? null;
|
|
$furtherDescription = $details[7] ?? null;
|
|
$classification = $details[8] ?? null;
|
|
$deptNo = $details[9] ?? null;
|
|
$qty = $details[10] ?? null;
|
|
$unitPrice = $details[11] ?? null;
|
|
$submitEinvoice = $details[12] ?? null;
|
|
$consolidatedEinvoice = $details[13] ?? null;
|
|
$eInvoiceValidationLink = $details[14] ?? null; // Safe access for the new column
|
|
|
|
Log::info("Row {$index} Details:", [
|
|
'DocNo' => $docNo,
|
|
'DocDate' => $docDate,
|
|
'DebtorCode' => $debtorCode,
|
|
'Ref' => $ref,
|
|
'ShipInfo' => $shipInfo,
|
|
'AccNo' => $accNo,
|
|
'DetailDescription' => $detailDescription,
|
|
'FurtherDescription' => $furtherDescription,
|
|
'Classification' => $classification,
|
|
'DeptNo' => $deptNo,
|
|
'Qty' => $qty,
|
|
'UnitPrice' => $unitPrice,
|
|
'SubmitEinvoice' => $submitEinvoice,
|
|
'ConsolidatedEinvoice' => $consolidatedEinvoice,
|
|
'EInvoiceValidationLink' => $eInvoiceValidationLink,
|
|
]);
|
|
}
|
|
}
|
|
|
|
return $this->response([]);
|
|
}
|
|
}
|