* @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 = Order::getOrdersWithInformations(); $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', $exception->getMessage()); RetailcrmLogger::writeNoCaller($exception->getTraceAsString()); RetailcrmLogger::output($exception->getMessage()); } time_nanosleep(0, 500000000); } unset($orderRecords); $orders = array_chunk($orders, 50); foreach ($orders as $chunk) { $api->ordersUpload($chunk); } } return true; } /** * @inheritDoc */ public function getName() { return 'RetailcrmExportEvent'; } }