![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/vendor/extmag/dhlshipping2/Helper/Request/ |
<?php /** * Copyright © Extmag. All rights reserved. */ namespace Extmag\DhlShipping\Helper\Request; use Exception; use Extmag\Shiplab\Model\ShipperAccount; use Extmag\Shiplab\Model\ThirdParty; use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\Exception\NoSuchEntityException; class Ship extends Dhl { /** * @var string */ protected $requestType = 'Ship'; /** * Native Scheme * * @param int|null $currentPackage * @return array * @throws Exception */ public function formNativeScheme($currentPackage = null) { /* Delivery pickup date*/ $timeStamp = $this->getTimezoneByAccount(); if (empty($timeStamp)) { $timeStamp = $this->date->timestamp(); } else { $timeStamp = $timeStamp->getTimestamp() + $timeStamp->getOffset(); } if (implode("", $this->data['pickup_close_time']) < $this->date->date('Hi', $timeStamp)) { $timeStamp += 86400; } for ($i = 0; $i < 7; $i++) { if (empty($this->data['pickup_days_of_week']) || !in_array($this->date->date("w", $timeStamp), $this->data['pickup_days_of_week']) || ($this->data['saturday_pickup'] == 1 && $this->date->date("w", $timeStamp) == 6) ) { break; } $timeStamp += 86400; } /* END Delivery pickup date*/ $billToRegistrationNumbers = null; if (!empty($this->data['sold_to_tax_number'])) { $billToRegistrationNumbers['RegistrationNumber'][] = [ 'Number' => $this->data['sold_to_tax_number'], 'NumberTypeCode' => 'VAT', 'NumberIssuerCountryCode' => $this->data['sold_to_country_code'], ]; } if (!empty($this->data['sold_to_eori_number'])) { $billToRegistrationNumbers['RegistrationNumber'][] = [ 'Number' => $this->data['sold_to_eori_number'], 'NumberTypeCode' => 'EOR', 'NumberIssuerCountryCode' => $this->data['sold_to_country_code'], ]; } return [ 'Request' => $this->formDhlSecurity(), 'LanguageCode' => 'en', 'Billing' => [ 'ShipperAccountNumber' => $this->data['account_shipper_number'], 'ShippingPaymentType' => $this->getShippingPaymentType(), 'BillingAccountNumber' => $this->getBillingAccountNumber(), 'DutyAccountNumber' => ($this->isInternational() && $this->route == 'shipment' ? $this->getDutyAccountNumber() : null), ], 'Consignee' => $this->route === 'shipment' ? $this->getShipTo() : $this->getShipFrom(), 'Dutiable' => ($this->isInternational() ? [ 'DeclaredValue' => $this->getTotalPrice(), 'DeclaredCurrency' => $this->data['currency'], 'ScheduleB' => null, 'ExportLicense' => null, 'ImportLicense' => null, 'TermsOfTrade' => $this->data['invoice_terms_of_shipment'] ?? null, 'CommerceLicensed' => null, 'Filing' => '', ] : null), 'UseDHLInvoice' => ($this->isInternationalInvoice() ? 'Y' : 'N'), 'DHLInvoiceLanguageCode' => ($this->isInternationalInvoice() ? 'en' : null), 'DHLInvoiceType' => ($this->isInternationalInvoice() ? $this->data['invoice_type'] : null), 'ExportDeclaration' => ($this->isInternationalInvoice() ? [ 'InterConsignee' => null, 'IsPartiesRelation' => null, 'ECCN' => null, 'SignatureName' => null, 'SignatureTitle' => null, 'ExportReason' => $this->data['invoice_reason_for_export'] ?? null, 'ExportReasonCode' => 'P', 'SedNumber' => null, 'SedNumberType' => null, 'MxStateCode' => ($this->data['ship_to_country_code'] == 'MX' ? $this->data['ship_to_state_code'] : null), 'InvoiceNumber' => $this->data['invoice_invoice_number'], 'InvoiceDate' => $this->date->date("Y-m-d", $this->data['invoice_invoice_date']), 'BillToCompanyName' => $this->data['sold_to_company_name'] ?? null, 'BillToContactName' => $this->data['sold_to_name'] ?? null, 'BillToAddressLine1' => $this->data['sold_to_street_1'] ?? null, 'BillToAddressLine2' => $this->data['sold_to_street_2'] ?? null, 'BillToAddressLine3' => $this->data['sold_to_street_3'] ?? null, 'BillToCity' => $this->data['sold_to_city'] ?? null, 'BillToPostcode' => $this->data['sold_to_postal_code'] ?? null, 'BillToSuburb' => null, 'BillToState' => $this->data['sold_to_state_code'] ?? null, 'BillToCountryCode' => $this->data['sold_to_country_code'] ?? null, 'BillToCountryName' => $this->getCountryName( $this->data['sold_to_country_code'] ?? null ), 'BillToPhoneNumber' => $this->data['sold_to_phone'] ?? null, 'BillToPhoneNumberExtn' => null, 'BillToFaxNumber' => null, 'BillToRegistrationNumbers' => $billToRegistrationNumbers, 'Remarks' => null, 'OtherCharges1' => null, 'OtherCharges2' => null, 'OtherCharges3' => null, 'DestinationPort' => null, 'TermsOfPayment' => null, 'SignatureImage' => $this->getSignatureImage(), 'ReceiverReference' => null, 'ExporterCode' => null, 'PackageMarks' => null, 'OtherRemarks2' => null, 'OtherRemarks3' => null, 'RUBankINN' => null, 'RUBankKPP' => null, 'RUBankOKPO' => null, 'RUBankOGRN' => null, 'RUBankSettlementAcctNumUSDEUR' => null, 'RUBankSettlementAcctNumRUR' => null, 'RUBankName' => null, 'AddDeclText1' => null, 'AddDeclText2' => null, 'AddDeclText3' => null, 'ExportLineItem' => $this->getExportLineItem(), 'ShipmentDocument' => null, 'PlaceOfIncoterm' => $this->data['invoice_place_of_incoterm'] ?? '', ] : null), 'Reference' => ($this->data['reference_id'] != "" ? [ 'ReferenceID' => $this->data['reference_id'], 'ReferenceType' => null, ] : null), 'ShipmentDetails' => [ 'Pieces' => $this->getPackages($currentPackage), 'WeightUnit' => ($this->data['unit_weight_for_shipper'] == 'KG' ? 'K' : 'L'), 'GlobalProductCode' => $this->data['service_code'], 'LocalProductCode' => null, 'Date' => $this->date->date('Y-m-d', $timeStamp), 'Contents' => $this->data['shipping_description'] ?: 'Ship', 'DimensionUnit' => ($this->data['unit_dimension_for_shipper'] == 'CM' ? 'C' : 'I'), 'PackageType' => null, 'IsDutiable' => $this->isDutiable(), 'CurrencyCode' => $this->data['currency'], 'CustData' => null, 'ShipmentCharges' => null/*$this->getFreightCharges()*/, ], 'Shipper' => $this->getShipper( $this->route === 'shipment' ? 'ship_from_' : 'ship_to_' ), 'SpecialService' => $this->getSpecialServices($timeStamp), 'Notification' => $this->getNotification(), 'Place' => $this->route === 'shipment' ? $this->getPlaceFrom() : $this->getPlaceTo(), 'EProcShip' => 'N', 'Airwaybill' => null, 'DocImages' => null, 'LabelImageFormat' => $this->data['printing_type'] ?? 'PDF', 'RequestArchiveDoc' => ($this->data['archive_doc'] == 1 ? 'Y' : null), 'NumberOfArchiveDoc' => (($this->data['archive_doc'] == 1 && !empty($this->data['archive_doc_number'])) ? (int)$this->data['archive_doc_number'] : null ), 'Label' => [ 'HideAccount' => null, 'LabelTemplate' => $this->data['printing_template'], 'ReceiptTemplate' => null, 'ShipmentReceiptWithLabels' => null, 'CustomsInvoiceTemplate' => null, 'Logo' => null, 'CustomerLogo' => null, 'Resolution' => $this->data['printing_dpi'], 'CustomerBarcodeType' => null, 'CustomerBarcodeCode' => null, 'CustomerBarcodeText' => null, 'QRCodeTemplate' => null, 'QRCodeImageFormat' => null, ], 'ODDLinkReq' => 'N', 'DGs' => $this->getDangerousGoods($currentPackage), 'GetPriceEstimate' => 'Y', 'SinglePieceImage' => 'Y', ]; } /** * Dangerous Goods * * @param int|null $currentPackage * @return array|null */ protected function getDangerousGoods($currentPackage = null) { $dgProducts = []; $uniqueProdId = []; foreach ($this->data['packages'] as $key => $package) { try { if ($currentPackage !== null && $key != $currentPackage) { continue; } foreach ($package['products']??[] as $product) { if (!in_array($product['id'], $uniqueProdId)) { $origProduct = $this->data['products'][$product['id']]; if (!empty($origProduct['dg_category'])) { $category = $this->categoryRepository->get($origProduct['dg_category']); $dgProducts['DG'][] = [ 'DG_ContentID' => $category->getExtmagDgDhlContentId(), 'DG_LabelDesc' => $category->getExtmagDgDhlLabelDesc(), 'DG_UNCode' => $category->getExtmagDgDhlUnCode(), ]; } $uniqueProdId[] = $product['id']; } } if ($currentPackage !== null && $key == $currentPackage) { break; } } catch (NoSuchEntityException $e) { $this->manager->warning($e->getMessage()); } } if (!empty($dgProducts)) { return $dgProducts; } return null; } /** * Get Packages * * @param int|null $currentPackage * @return array[]|null */ protected function getPackages($currentPackage = null) { $data = ['Piece' => []]; foreach ($this->data['packages'] as $key => $package) { if ($currentPackage !== null && $key != $currentPackage) { continue; } $item = [ 'PieceID' => $key, 'PackageType' => $package['type'], ]; $item['Weight'] = round((float)$package['weight'], 1); if (!empty($package['volumetric_weight'])) { $volumeWeight = $this->getVolumeWeight($package['volumetric_weight']); if ((float)$package['weight'] < $volumeWeight) { $item['Weight'] = (string)$volumeWeight; } } if (!empty($package['lengths']) && !empty($package['width']) && !empty($package['height'])) { $item['Width'] = round((float)$package['width']); $item['Height'] = round((float)$package['height']); $item['Depth'] = round((float)$package['lengths']); } elseif (!empty($package['volumetric_weight'])) { $item['Width'] = 1; $item['Height'] = 1; $item['Depth'] = 1; } $item['PieceContents'] = null; /* Reference number */ $item['PieceReference'] = !empty($package['reference_value_1']) ? [ 'ReferenceID' => $package['reference_value_1'], 'ReferenceType' => null, ] : null; /* END Reference number */ $item['AdditionalInformation'] = $this->getPackageAdditionalInformation($package); $data['Piece'][] = $item; if ($currentPackage !== null && $key == $currentPackage) { break; } } return !empty($data['Piece']) ? $data : null; } /** * Package Additional Information * * @param array $package * @return array */ protected function getPackageAdditionalInformation(array $package) { $addInfo = null; if (!empty($package['barcode_1']) && !empty($package['reference_value_1'])) { $addInfo['CustomerBarcodes'] = [ 'BarcodeType' => 128, 'BarcodeContent' => $package['reference_value_1'], 'TextBelowBarcode' => $package['reference_value_1'], ]; } return $addInfo; } /** * Notification * * @return array|null */ protected function getNotification() { if (!empty($this->data['notification_active']) && !empty($this->data['notification_message'])) { return [ 'EmailAddress' => $this->data['ship_to_email'], 'Message' => $this->data['notification_message'], ]; } return null; } /** * Shipping Payment Type * * @return string */ protected function getShippingPaymentType() { if ($this->data['shipment_payer'] == 'shipper') { return 'S'; } else { return 'T'; } } /** * Billing Account Number * * @return string|null */ protected function getBillingAccountNumber() { if ($this->data['shipment_payer'] != 'shipper') { /** * @var ThirdParty $thirdAccount */ $thirdAccount = $this->thirdPartyRepository->getById($this->data['shipment_payer']); if ($thirdAccount && $thirdAccount->getCarrierCode() == $this->carrier) { return $thirdAccount->getAccountNumber(); } } return null; } /** * Duty Account Number * * @return string|null */ protected function getDutyAccountNumber() { if ($this->data['shipment_payer'] != 'shipper' && $this->data['shipment_payer'] != 'customer') { /** * @var ThirdParty $thirdAccount */ $thirdAccount = $this->thirdPartyRepository->getById($this->data['shipment_payer']); if ($thirdAccount && $thirdAccount->getCarrierCode() == $this->carrier) { return $thirdAccount->getAccountNumber(); } } return null; } /** * Special Services * * @param int $timeStamp * @return array|null */ protected function getSpecialServices($timeStamp) { $specialCodes = []; $shipmentCOD = $this->getCOD(); if (count($shipmentCOD) > 0) { $shipmentCOD['ChargeValue'] = $this->getTotalPrice(); $shipmentCOD['CurrencyCode'] = $this->data['currency']; $specialCodes[] = $shipmentCOD; } elseif (!empty($this->data['delivery_confirmation'])) { $specialCodes[] = ['SpecialServiceType' => $this->data['delivery_confirmation']]; } /*if ($this->data['notification_active'] == 1) { $specialCodes[] = ['SpecialServiceType' => 'JA']; }*/ if ($this->data['saturday_delivery'] == 1) { $specialCodes[] = ['SpecialServiceType' => 'AA']; } if ($this->data['saturday_pickup'] == 1 && $this->date->date("w", $timeStamp) == 6) { $specialCodes[] = ['SpecialServiceType' => 'AB']; } if ($this->isDutiable() == 'Y' && $this->isInternationalInvoice()) { $specialCodes[] = ['SpecialServiceType' => 'WY']; } if ($this->data['insurance'] == 1) { $insurance = ['SpecialServiceType' => 'II']; $insurance['ChargeValue'] = $this->getTotalPrice(); $insurance['CurrencyCode'] = $this->data['currency']; $specialCodes[] = $insurance; } return $specialCodes ?: null; } /** * Export Line Item * * @return array */ protected function getExportLineItem() { $data = null; foreach ($this->data['invoice_products'] as $key => $invoiceProduct) { $product = $this->data['products'][$invoiceProduct['id']]; $item = [ 'LineNumber' => $key + 1, 'Quantity' => (string)(int)$product['qty'], 'QuantityUnit' => 'PCS', 'Description' => substr($invoiceProduct['description'], 0, 75), 'Value' => $invoiceProduct['amount'], 'IsDomestic' => null, 'CommodityCode' => (!empty($invoiceProduct['commodity_code']) ? $invoiceProduct['commodity_code'] : null), 'ScheduleB' => (!empty($invoiceProduct['scheduleb_code']) ? $invoiceProduct['scheduleb_code'] : null), 'ECCN' => null, 'Weight' => [ 'Weight' => round($product['weight_for_shipment'], 3), 'WeightUnit' => ($this->data['unit_weight_for_shipper'] == 'KG' ? 'K' : 'L'), ], 'GrossWeight' => [ 'Weight' => round($product['weight_for_shipment'], 3), 'WeightUnit' => ($this->data['unit_weight_for_shipper'] == 'KG' ? 'K' : 'L'), ], 'License' => null, 'LicenseSymbol' => null, 'ManufactureCountryCode' => $invoiceProduct['country_manufacturer'] ?: null, 'ManufactureCountryName' => null, 'ImportTaxManagedOutsideDhlExpress' => null, ]; $data[] = $item; } return $data; } /** * Signature Image * * @return string|null */ protected function getSignatureImage() { /** * @var ShipperAccount $account */ $account = $this->shipperAccountRepository->getById($this->data['account_id']); $signatureData = $account->getSignature(); if (!empty($signatureData)) { $signature = json_decode($signatureData, true); if (isset($signature[0]['tmp_name'])) { $mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA); $filePath = $mediaDirectory->getAbsolutePath('extmag/upload/dhl/' . $signature[0]['file']); } else { $mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::PUB); $filePath = $mediaDirectory->getAbsolutePath($signature[0]['url']); } if ($this->driver->isExists($filePath)) { $fileData = $this->driver->fileGetContents($filePath); if (!empty($fileData)) { return base64_encode($fileData); } } } return null; } }