public function importPackage($lpn, $shipment)
 {
     $new = false;
     $packageNumber = $lpn->getAttribute('Id');
     if (false === ($package = $this->isPackageExist($packageNumber, $this->_customer))) {
         $package = new Entity\Package();
         $package->setNumber($packageNumber);
         $package->setCustomer($this->_customer);
         $this->_em->persist($package);
         $new = true;
     }
     $package->setQuantity($this->getValueByAttribute('Measures', 'Qty', $lpn));
     if (true === $new) {
         $shipment->addPackage($package);
     } else {
         foreach ($package->getShipments() as $pacShipment) {
             if ($pacShipment->getId() !== $shipment->getId()) {
                 $pacShipment->removePackage($package);
                 $shipment->addPackage($package);
                 //send notification
                 $this->_error[] = "Same package#{$package->getNumber()} was found at Shipment#{$pacShipment->getMbl()}. Removed. Added to {$shipment->getMbl()}";
             }
         }
     }
     return $package;
 }
Exemplo n.º 2
0
 public function parse()
 {
     $importLog = ['invoice' => ['inserted' => 0, 'updated' => 0, 'failed' => 0], 'product' => ['inserted' => 0], 'debug' => ['start_time' => microtime(true), 'end_time' => 0]];
     $invoiceData = $this->getArrayAssoc();
     $invoiceData = $this->groupArrayByKey($invoiceData, 'PURCHASE_ORDER_H');
     foreach ($invoiceData as $invoiceItem) {
         if ($this->_batch % $this->_insertLimit === 0) {
             $this->_customer = $this->getCustomerByAbbr(self::CUSTOMER_CODE);
             if ($this->_customer == null) {
                 throw new Exception('Customer "' . self::CUSTOMER_CODE . '"" was not found in DB');
             }
         }
         $invoiceSplit = $this->splitInvoice($invoiceItem);
         if ($invoiceSplit == false) {
             $importLog['invoice']['failed']++;
             continue;
         }
         $headers = $invoiceSplit['header'];
         $invoiceHeader = $headers[1];
         // Check if invoice exists
         if (($invoice = $this->isInvoiceExist($invoiceHeader['PURCHASE_ORDER_H'], self::CUSTOMER_CODE)) == false) {
             $invoice = new Entity\Invoice();
             $importLog['invoice']['inserted']++;
             $invoiceUpdates = 0;
         } else {
             // cancel all invoice line items if exists
             $this->_em->getRepository('CoreBundle:Invoice')->setLineItemStatus($invoice, $this->getStatusByCode('CXL'));
             $importLog['invoice']['updated']++;
             $invoiceUpdates = $invoice->getCustomNumber10() + 1;
         }
         $invoice->setCustomer($this->_customer);
         $invoice->setCustomText1($invoiceHeader['CUSTOMS_INVOICE_H']);
         $invoice->setCustomText2($invoiceHeader['HP_INFORMATION_H']);
         $invoice->setCustomText3($this->getInvoiceJson($invoiceHeader));
         $invoice->setIssuedAt(new \DateTime($invoiceHeader['ORDER_DATE_H']));
         $invoice->setIncoterms($this->getIncotermsByName(strtoupper($invoiceHeader['INCO_TERMS_H'])));
         $invoice->setIncotermsDetails($invoiceHeader['INCO_TERMS_DETAILS_H']);
         $invoice->setCustomText6($invoiceHeader['UNIQUE_GOODS_IDENTIFIER_H']);
         $invoice->setNumber($invoiceHeader['PURCHASE_ORDER_H']);
         $invoice->setCustomText7($invoiceHeader['DELIVERY_ORDER_H']);
         $invoice->setCustomText8($invoiceHeader['RELATED_DOCUMENT_H']);
         $invoice->setCustomText9($invoiceHeader['CUSTOMS_DECLARATION_H']);
         $invoice->setCustomText10($invoiceHeader['HP_INT_TRANSPORT_ORDER_H']);
         $invoice->setCurrency($this->getCurrencyByCode(strtoupper($invoiceHeader['HP_INVOICE_CURRENCY_H'])));
         $invoice->setTotalAmount($invoiceHeader['HP_INVOICE_TOTAL_H']);
         $invoice->setCustomNumber11($invoiceHeader['HP_LOGISTICAL_CHARGE_H']);
         $invoice->setTotalPackages($invoiceHeader['TOTAL_NUMBER_OF_CARTON_H']);
         $invoice->setCustomText11($invoiceHeader['HP_BILL_TO_CODE_H']);
         $invoice->setCustomText12($invoiceHeader['HPDOC220_H']);
         $invoice->setCustomText5($invoiceHeader['SHIPPER_REFERENCE_L']);
         $invoice->setCustomNumber10($invoiceUpdates);
         $invoice->setStatus($this->getStatusByCode('PBV'));
         $invoice->setType($this->getInvoiceTypeByCode('COMM'));
         // Get HP contract number and date
         if (preg_match('#.*\\sOF\\s.*#', $invoiceHeader['HP_CONTRACTRFFCT_H'])) {
             list($contractNum, $contractDate) = explode(' OF ', $invoiceHeader['HP_CONTRACTRFFCT_H']);
         } else {
             $contractNum = null;
             $contractDate = null;
         }
         $formattedDate = new \DateTime();
         $formattedDate = $formattedDate->createFromFormat('d/m/Y', trim($contractDate));
         $invoice->setCustomText4(trim($contractNum));
         $invoice->setCustomDate1($formattedDate);
         // START reference table updates ( shipper, consignee, delivery and it's addresses )
         if (($shipper = $this->isOrganizationExist($invoiceHeader['SHIPPER_NAME_H'])) == false) {
             $shipper = $this->addOrganization($invoiceHeader['SHIPPER_NAME_H']);
         }
         if (($shipperAddr = $this->isOrganizationLocationExist($invoiceHeader['SHIPPER_POSTCODE_H'])) == false) {
             $shipperAddr = $this->addOrganizationLocation($shipper, $invoiceHeader['SHIPPER_COUNTRY_H'], $invoiceHeader['SHIPPER_CITY_H'], $invoiceHeader['SHIPPER_ADDRESS2_H'], $invoiceHeader['SHIPPER_ADDRESS3_H'], $invoiceHeader['SHIPPER_ALIAS_CODE_H'], $invoiceHeader['SHIPPER_POSTCODE_H']);
         }
         if (($consignee = $this->isOrganizationExist($invoiceHeader['CONSIGNEE_NAME_H'])) == false) {
             $consignee = $this->addOrganization($invoiceHeader['CONSIGNEE_NAME_H']);
         }
         if (($consigneeAddr = $this->isOrganizationLocationExist($invoiceHeader['CONSIGNEE_POSTCODE_H'])) == false) {
             $consigneeAddr = $this->addOrganizationLocation($consignee, $invoiceHeader['CONSIGNEE_COUNTRY_H'], $invoiceHeader['CONSIGNEE_CITY_H'], $invoiceHeader['CONSIGNEE_ADDRESS2_H'], $invoiceHeader['CONSIGNEE_ADDRESS3_H'], $invoiceHeader['CONSIGNEE_ALIAS_CODE_H'], $invoiceHeader['CONSIGNEE_POSTCODE_H']);
         }
         if (($delivery = $this->isOrganizationExist($invoiceHeader['DELIVERY_NAME_H'])) == false) {
             $delivery = $this->addOrganization($invoiceHeader['DELIVERY_NAME_H']);
         }
         if (($deliveryAddr = $this->isOrganizationLocationExist($invoiceHeader['DELIVERY_POSTCODE_H'])) == false) {
             $deliveryAddr = $this->addOrganizationLocation($delivery, $invoiceHeader['DELIVERY_COUNTRY_H'], $invoiceHeader['DELIVERY_CITY_H'], $invoiceHeader['DELIVERY_ADDRESS2_H'], $invoiceHeader['DELIVERY_ADDRESS3_H'], $invoiceHeader['DELIVERY_ALIAS_CODE_H'], $invoiceHeader['DELIVERY_POSTCODE_H']);
         }
         // END reference table updates
         $invoice->setShipper($shipper);
         $invoice->setShipperAddress($shipperAddr);
         $invoice->setConsignee($consignee);
         $invoice->setConsigneeAddress($consigneeAddr);
         $invoice->setDelivery($delivery);
         $invoice->setDeliveryAddress($deliveryAddr);
         $elements = $invoiceSplit['elements'];
         $totalGrossWeight = 0;
         // Go through invoice line items and packages
         foreach ($elements as $elementId => $elementData) {
             $lines = $elementData['lines'];
             try {
                 foreach ($lines as $lineId => $lineData) {
                     // Add product info if missing
                     if (($product = $this->isProductExist($lineData['ITEM_DESCRIPTION_L'])) == false) {
                         $product = new Entity\Product();
                         $product->setName($lineData['ITEM_DESCRIPTION_L']);
                         $product->setHscode($headers[$elementId]['BARCODE_L']);
                         $product->setDescription($lineData['CUSTOM_TEXT_4_L']);
                         $product->setCustomText1($headers[$elementId]['SHIPPING_FLAG_L']);
                         $product->setCustomer($this->_customer);
                         $this->_em->persist($product);
                         $importLog['product']['inserted']++;
                     }
                     $totalGrossWeight += floatval($lineData['GROSS_WEIGHT_L']);
                     // Check if invoice line item exists
                     $invoiceId = $invoice->getId();
                     if ($invoiceId !== null && $invoiceId > 0) {
                         if (($invoiceLineItem = $this->isLineItemExist($invoiceId, $lineData['PANALPINA_REF_L'])) == false) {
                             $invoiceLineItem = new Entity\InvoiceLineItem();
                         } else {
                             $items = $invoiceLineItem->getPackages();
                             foreach ($items as $id => $item) {
                                 $invoiceLineItem->removePackage($item);
                             }
                         }
                     } else {
                         $invoiceLineItem = new Entity\InvoiceLineItem();
                     }
                     if (isset($invoiceSplit['elements'][$lineId]['boxes'])) {
                         $boxes = $invoiceSplit['elements'][$lineId]['boxes'];
                         $boxUOM = null;
                         foreach ($boxes as $box) {
                             if (($package = $this->isPackageExist($box['CUSTOM_CODE_4_L'], $box['CUSTOM_TEXT_3_L'])) == false) {
                                 $package = new Entity\Package();
                             } else {
                                 $items = $package->getProducts();
                                 foreach ($items as $item) {
                                     $package->removeProduct($item);
                                 }
                             }
                             if (is_null($boxUOM)) {
                                 $boxUOM = $this->getUOMByCode(strtoupper($box['CUSTOM_TEXT_2_L']));
                             } elseif ($boxUOM->getCode() != strtoupper($box['CUSTOM_TEXT_2_L'])) {
                                 throw new \Exception('Invoice ' . $invoiceHeader['CUSTOMS_INVOICE_H'] . ': boxes have different UOMs');
                             }
                             $package->setNumber($box['CUSTOM_CODE_4_L']);
                             $package->setPackageType($this->getUOMByCode('CT'));
                             $package->setCustomer($this->_customer);
                             $package->setGrossWeight($box['CUSTOM_TEXT_1_L']);
                             $package->setGrossWeightUOM($boxUOM);
                             $package->setCustomText4($box['CUSTOM_TEXT_3_L']);
                             $package->addProduct($product);
                             //$package->addInvoiceLineItem($invoiceLineItem);
                             $this->_em->persist($package);
                             $invoiceLineItem->addPackage($package);
                         }
                     }
                     $invoiceLineItem->setLineNumber($lineData['PANALPINA_REF_L']);
                     $invoiceLineItem->setGrossWeight($lineData['GROSS_WEIGHT_L']);
                     $invoiceLineItem->setGrossWeightUOM($boxUOM);
                     $invoiceLineItem->setCustomText4($lineData['ITEM_NUMBER_L']);
                     $invoiceLineItem->setQuantity($lineData['LINE_QTY_ORD_L']);
                     $invoiceLineItem->setCustomNumber11(floatval($headers[$elementId]['CUSTOM_TEXT_1_L']));
                     $invoiceLineItem->setPrice(floatval($headers[$elementId]['LINE_VALUE_L']));
                     $invoiceLineItem->setOriginCountry($this->getCountryByCode($lineData['ORIGIN_COUNTRY_L']));
                     $invoiceLineItem->setProduct($product);
                     //$invoiceLineItem->setInvoice($invoice);
                     $invoiceLineItem->setStatus($this->getStatusByCode('OPN'));
                     //$this->_em->persist($invoiceLineItem);
                     $invoice->addItem($invoiceLineItem);
                 }
             } catch (\Exception $ex) {
                 $message = $ex->getMessage() . ' IN ' . $ex->getFile() . ' LINE ' . $ex->getLine();
                 $this->saveImportLog($this->getImportName(), $message, self::STATUS_ERR);
             }
         }
         $invoice->setGrossWeight($totalGrossWeight);
         $invoice->setGrossWeightUOM($boxUOM);
         $this->_em->persist($invoice);
         //$this->flush();
         if (++$this->_batch % $this->_insertLimit === 0) {
             $this->_em->flush();
             $this->_em->clear();
             //gc_collect_cycles();
         }
         //gc_collect_cycles();
     }
     $importLog['debug']['end_time'] = microtime(true);
     $message = "Invoices added: " . $importLog['invoice']['inserted'] . "\r\n" . "Invoices updated: " . $importLog['invoice']['updated'] . "\r\n" . "Invoices failed: " . $importLog['invoice']['failed'] . "\r\n" . "Products added: " . $importLog['product']['inserted'] . "\r\n" . "Start time: " . date('d.m.Y H:i:s', $importLog['debug']['start_time']) . "\r\n" . "End time: " . date('d.m.Y H:i:s', $importLog['debug']['end_time']) . "\r\n" . "Time elapsed: " . ceil($importLog['debug']['end_time'] - $importLog['debug']['start_time']) . " seconds\r\n";
     $this->saveImportLog($this->getImportName(), $message, self::STATUS_OK);
 }