Пример #1
0
 public function parse()
 {
     $batch = 10;
     while (($data = $this->getLine()) !== false) {
         try {
             array_walk($data, function (&$value) {
                 $value = trim($value);
                 if (!strlen($value)) {
                     $value = null;
                 }
             });
             if (($product = $this->isProductExist($data[3])) === false) {
                 $product = new Entity\Product();
             }
             $product->setCustomer($this->_customer);
             $product->setName($data[3]);
             $product->setTnved($data[5]);
             $product->setHscode($data[6]);
             $product->setDescription($data[7]);
             $product->setDescriptionInvoice($data[8]);
             $product->setExtraData($data[9]);
             $product->setNetWeight($data[10]);
             $product->setStatus($this->_status);
             $product->setCustomText1($data[13]);
             $this->_em->persist($product);
             if ($this->_count % $batch === 0) {
                 echo memory_get_usage() / 1024, "\n";
                 $this->_em->flush();
                 $this->_em->clear($product);
             }
             $this->_count++;
         } catch (\Exception $ex) {
             $logger = $this->get('logger');
             $logger->error($ex->getMessage());
         }
     }
     $this->_em->flush();
     $this->_em->clear();
 }
Пример #2
0
 public function updateAction()
 {
     if (!$this->checkAccess()) {
         return new JsonResponse(['fail' => 'Wrong API key']);
     }
     $em = $this->getDoctrine()->getManager();
     $request = $this->container->get('request');
     $productName = $request->get('name');
     if (empty($productName)) {
         return new JsonResponse(['fail' => 'Product name must be defined']);
     }
     $productCustomerAbbr = $request->get('customer_abbr');
     $customer = $em->getRepository('CoreBundle:Customer')->findOneBy(['abbr' => $productCustomerAbbr]);
     if (empty($customer)) {
         return new JsonResponse(['fail' => 'Customer not found']);
     }
     $product = $em->getRepository('CoreBundle:Product')->findBy(['customer' => $customer, 'name' => $productName]);
     if (empty($product)) {
         if (!$request->get('_force')) {
             return new JsonResponse(['fail' => 'Product does not exist. If you want to create new one use _force parameter.']);
         }
         $product = new Entity\Product();
     }
     $product->setName($productName);
     $product->setDescription($request->get('description'));
     $product->setCustomer($customer);
     if ($request->get('status')) {
         $status = $em->getRepository('CoreBundle:ProductStatus')->findOneBy(['code' => $request->get('status')]);
     }
     if (empty($status)) {
         $status = $em->getRepository('CoreBundle:ProductStatus')->findOneBy(['code' => Entity\ProductStatus::TYPE_ON_CHECKING]);
     }
     $product->setStatus($status);
     if ($request->get('origin_countries')) {
         foreach ((array) $request->get('origin_countries') as $originCountyCode) {
             $country = $em->getRepository('CoreBundle:Country')->findOneBy(['code' => $originCountyCode]);
         }
         if (!empty($country)) {
             $product->addOriginCountry($country);
         }
     }
     if ($request->get('net_weight') && $request->get('net_weight_uom')) {
         $netWeightUom = $em->getRepository('CoreBundle:UOM')->findOneBy(['name' => $request->get('net_weight_uom')]);
         $netWeight = $request->get('net_weight');
         if (!empty($netWeightUom) && !empty($netWeight)) {
             $product->setNetWeight($netWeight);
             $product->setNetWeightUOM($netWeightUOM);
         }
     }
     if ($request->get('gross_weight') && $request->get('gross_weight_uom')) {
         $grossWeightUom = $em->getRepository('CoreBundle:UOM')->findOneBy(['name' => $request->get('gross_weight_uom')]);
         $grossWeight = $request->get('gross_weight');
         if (!empty($grossWeightUom) && !empty($grossWeight)) {
             $product->setGrossWeight($grossWeight);
             $product->setGrossWeightUOM($grossWeightUOM);
         }
     }
     if ($request->get('customer_description')) {
         $product->setCustomerDescription($request->get('customer_description'));
     }
     if ($request->get('price')) {
         $product->setPrice($price);
     }
     if ($request->get('currency')) {
         $currency = $em->getRepository('CoreBundle:Currency')->findOneBy(['codeAlpha3' => $request->get('currency')]);
         $product->setCurrency($currency);
     }
     for ($i = 1; $i <= 10; $i++) {
         if (!is_null($request->get('custom_date_' . $i))) {
             $setterMethod = 'setCustomDate' . $i;
             try {
                 $date = new \DateTime($request->get('custom_date_' . $i));
                 $product->{$setterMethod}($date);
             } catch (\Exception $e) {
                 // Skip error
             }
         }
         if (!is_null($request->get('custom_text_' . $i))) {
             $setterMethod = 'setCustomText' . $i;
             $product->{$setterMethod}($request->get('custom_text_' . $i));
         }
         if (!is_null($request->get('custom_number_' . $i))) {
             $setterMethod = 'setCustomNumber' . $i;
             $product->{$setterMethod}($request->get('custom_number_' . $i));
         }
     }
     if (!is_null($request->get('extra_data'))) {
         $product->setExtraData(json_encode($request->get('extra_data')));
     }
     $customsPointDataArray = $request->get('customs_point_data');
     if (!empty($customsPointDataRaw) && !empty($customsPointDataRaw['code'])) {
         $customsPoint = $em->getRepository('CoreBundle:CustomsPoint')->findOneBy(['code' => $customsPointDataRaw['code']]);
         if (!empty($customsPoint)) {
             $customsPointData = $product->getCustomsPointDataByCustomsPoint($customsPoint);
             if (empty($customsPointData)) {
                 $customsPointData = new Entity\ProductCustomsPoint();
                 $customsPointData->setCustomsPoint($customsPoint);
                 $customsPointData->setProduct($product);
             }
             if (isset($customsPointDataRaw['eccn'])) {
                 $customsPointData->setEccn($customsPointDataRaw['eccn']);
             }
             if (isset($customsPointDataRaw['hscode'])) {
                 $customsPointData->setEccn($customsPointDataRaw['hscode']);
             }
             if (isset($customsPointDataRaw['tnved'])) {
                 $customsPointData->setTnved($customsPointDataRaw['tnved']);
             }
             if (isset($customsPointDataRaw['status'])) {
                 $status = $em->getRepository('CoreBundle:ProductStatus')->findOneBy(['code' => $customsPointDataRaw['status']]);
                 $customsPointData->setStatus($status);
             }
             if (isset($customsPointDataRaw['description_ccd'])) {
                 $customsPointData->setDescriptionCCD($customsPointDataRaw['description_ccd']);
             }
             if (isset($customsPointDataRaw['manufacturer'])) {
                 $customsPointData->setManufacturer($customsPointDataRaw['manufacturer']);
             }
             for ($i = 1; $i <= 10; $i++) {
                 if ($i < 6) {
                     if (!is_null($customsPointDataRaw['custom_date_' . $i])) {
                         $setterMethod = 'setCustomDate' . $i;
                         try {
                             $date = new \DateTime($customsPointDataRaw['custom_date_' . $i]);
                             $customsPointData->{$setterMethod}($date);
                         } catch (\Exception $e) {
                             // Skip error
                         }
                     }
                 }
                 if (!is_null($customsPointDataRaw['custom_text_' . $i])) {
                     $setterMethod = 'setCustomText' . $i;
                     $customsPointData->{$setterMethod}($customsPointDataRaw['custom_text_' . $i]);
                 }
                 if (!is_null($customsPointDataRaw['custom_number_' . $i])) {
                     $setterMethod = 'setCustomNumber' . $i;
                     $customsPointData->{$setterMethod}($customsPointDataRaw['custom_number_' . $i]);
                 }
             }
             $em->persist($customsPointData);
         }
     }
     $em->persist($product);
     $em->flush();
     return new JsonResponse(['success' => $this->exportProduct($product)]);
 }
Пример #3
0
 private function createNewProduct($data)
 {
     $product = new Entity\Product();
     $product->setCustomer($this->_customer);
     $product->setName($data[3]);
     $product->setTnved($data[5]);
     $product->setHscode($data[6]);
     $product->setDescription($data[7]);
     $product->setDescriptionInvoice($data[8]);
     $product->setExtraData($data[9]);
     $product->setNetWeight($data[10]);
     $product->setStatus($this->_status);
     $product->setCustomText1($data[13]);
     $this->_em->persist($product);
     return $product;
 }
Пример #4
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);
 }
 protected function addProduct($name, $description, $color = null, $size = null, $altKey = null, $seller = null, $parent = null)
 {
     if (($product = $this->isProductExist($name)) === false) {
         $product = new Entity\Product();
     }
     $product->setName($name);
     $product->setDescription($description);
     $product->setCustomer($this->_customer);
     $product->setCustomText1($color);
     $product->setCustomText2($size);
     $product->setCustomText3($altKey);
     $product->setCustomText4($seller);
     $product->setParent($parent);
     $this->_em->persist($product);
     return $product;
 }