createsKeyValuePair = $createsKeyValuePair; $this->updatesKeyValuePair = $updatesKeyValuePair; } /** * @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') { $optionalColumn = 'einvoicevalidationlink'; if ( $normalizedHeader !== $salesInvoiceHeader && $normalizedHeader !== [...$salesInvoiceHeader, $optionalColumn] ) { throw new MalformedRequestException('Uploaded Excel file format is incorrect. Column headers do not match expected format.'); } } elseif ($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, ]); $booking = Booking::where('marking', $ref)->first(); if($booking){ if($docNo != "" && $docNo != "<>"){ $this->updateOrCreateKeyValuePair($booking, KVPKey::AUTOCOUNT_DOCNO, $docNo); } if($eInvoiceValidationLink){ $this->updateOrCreateKeyValuePair($booking, KVPKey::AUTOCOUNT_EINVOICE_VALIDATION_LINK, $eInvoiceValidationLink); } } } } return $this->response([]); } private function updateOrCreateKeyValuePair($booking, $key, $value) { $keyValuePairObject = new KeyValuePairObject($key, $value); $metadata = $booking->attributesKVP()->where('key', $key)->first(); if ($metadata) { $this->updatesKeyValuePair->execute($metadata, $keyValuePairObject); } else { $this->createsKeyValuePair->execute($booking, $keyValuePairObject); } } }