* @copyright 2020 DIGITAL RETAIL TECHNOLOGIES SL * @license https://opensource.org/licenses/MIT The MIT License * * Don't forget to prefix your containers with your own identifier * to avoid any conflicts with others containers. */ require_once(dirname(__FILE__) . '/../RetailcrmPrestashopLoader.php'); class RetailcrmExportEvent extends RetailcrmAbstractEvent implements RetailcrmEventInterface { /** * @inheritDoc */ public function execute() { if ($this->isRunning()) { return false; } $this->setRunning(); $shops = $this->getShops(); foreach ($shops as $shop) { RetailcrmTools::setShopContext(intval($shop['id_shop'])); $api = RetailcrmTools::getApiClient(); if (empty($api)) { RetailcrmLogger::writeCaller(__METHOD__, 'Set API key & URL first'); continue; } $orders = array(); $orderRecords = RetailcrmExport::getOrdersIds(); $orderBuilder = new RetailcrmOrderBuilder(); $orderBuilder->defaultLangFromConfiguration()->setApi($api); foreach ($orderRecords as $record) { $orderBuilder->reset(); $order = new Order($record['id_order']); $orderCart = new Cart($order->id_cart); $orderCustomer = new Customer($order->id_customer); $orderBuilder->setCmsOrder($order); if (!empty($orderCart->id)) { $orderBuilder->setCmsCart($orderCart); } else { $orderBuilder->setCmsCart(null); } if (!empty($orderCustomer->id)) { $orderBuilder->setCmsCustomer($orderCustomer); } else { //TODO // Caused crash before because of empty RetailcrmOrderBuilder::cmsCustomer. // Current version *shouldn't* do this, but I suggest more tests for guest customers. $orderBuilder->setCmsCustomer(null); } try { $orders[] = $orderBuilder->buildOrderWithPreparedCustomer(); } catch (\InvalidArgumentException $exception) { RetailcrmLogger::writeCaller('export', sprintf('Error while building %s: %s', $record['id_order'], $exception->getMessage())); RetailcrmLogger::writeNoCaller($exception->getTraceAsString()); RetailcrmLogger::output($exception->getMessage()); } time_nanosleep(0, 250000000); if (count($orders) == 50) { $api->ordersUpload($orders); $orders = array(); } } if (count($orders)) { $api->ordersUpload($orders); } $customers = array(); $customersRecords = RetailcrmExport::getCustomersIds(); foreach ($customersRecords as $record) { $customerId = $record['id_customer']; $addressId = $record['id_address']; $cmsCustomer = new Customer($customerId); if (Validate::isLoadedObject($cmsCustomer)) { if ($addressId) { $cmsAddress = new Address($addressId); $addressBuilder = new RetailcrmAddressBuilder(); $address = $addressBuilder ->setAddress($cmsAddress) ->build() ->getDataArray(); } else { $address = array(); } try { $customers[] = RetailcrmOrderBuilder::buildCrmCustomer($cmsCustomer, $address); } catch (\Exception $exception) { RetailcrmLogger::writeCaller('export', sprintf('Error while building %s: %s', $customerId, $exception->getMessage())); RetailcrmLogger::writeNoCaller($exception->getTraceAsString()); RetailcrmLogger::output($exception->getMessage()); } if (count($customers) == 50) { $api->customersUpload($customers); $customers = array(); time_nanosleep(0, 250000000); } } } if (count($customers)) { $api->customersUpload($customers); } } return true; } /** * @inheritDoc */ public function getName() { return 'RetailcrmExportEvent'; } }