194 lines
6.5 KiB
PHP
194 lines
6.5 KiB
PHP
<?php
|
|
/**
|
|
* Copyright (C) 2020 Futurenext srl
|
|
*
|
|
* This file is part of Zakeke.
|
|
*
|
|
* Zakeke Interactive Product Designer can not be copied and/or distributed
|
|
* without the express permission of Futurenext srl
|
|
*
|
|
* @author Futurenext srl <help@zakeke.com>
|
|
* @copyright 2019 Futurenext srl
|
|
* @license https://www.zakeke.com/privacy/#general_conditions
|
|
*/
|
|
|
|
class ZakekeOrderService
|
|
{
|
|
/** @var Zakeke Zakeke module instance */
|
|
private $module;
|
|
|
|
/**
|
|
* ZakekeOrderService constructor.
|
|
*
|
|
* @param Zakeke $module
|
|
*/
|
|
public function __construct($module)
|
|
{
|
|
$this->module = $module;
|
|
}
|
|
|
|
/**
|
|
* @param Order $order
|
|
* @throws Exception
|
|
*/
|
|
public function process($order)
|
|
{
|
|
$orderData = array(
|
|
'orderCode' => $order->id,
|
|
'ecommerceOrderNumber' => $order->reference,
|
|
'sessionID' => '1',
|
|
'total' => (float)$order->total_paid_tax_excl,
|
|
'orderStatusID' => 1,
|
|
'details' => array(),
|
|
'compositionDetails' => array()
|
|
);
|
|
|
|
$guestCode = $this->module->getZakekeGuestCode();
|
|
if ($order->id_customer > 0) {
|
|
$orderData['customerID'] = $order->id_customer;
|
|
} elseif ($guestCode) {
|
|
$orderData['visitorID'] = $guestCode;
|
|
}
|
|
|
|
foreach ($order->getProducts() as $id_order_detail => $product) {
|
|
$customizedDatas = $product['customizedDatas'];
|
|
foreach ((array)$customizedDatas as $customizedData) {
|
|
foreach ($customizedData as $data) {
|
|
if (ZakekeConfiguratorEnabled::productEnabledId($product['product_id']) !== false) {
|
|
$item_data = $this->createConfigurationItem($id_order_detail, $product, $data);
|
|
|
|
if ($item_data) {
|
|
$orderData['compositionDetails'][] = $item_data;
|
|
}
|
|
}
|
|
|
|
if (ZakekeEnabled::productEnabledId($product['product_id']) !== false) {
|
|
$item_data = $this->createCustomizationItem($id_order_detail, $product, $data);
|
|
|
|
if ($item_data) {
|
|
$orderData['details'][] = $item_data;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!empty($orderData['details']) || !empty($orderData['compositionDetails'])) {
|
|
$this->module->getZakekeApi()->placeOrder($orderData);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param int $id_order_detail
|
|
* @param Product $product
|
|
* @param array $data
|
|
* @return array|null
|
|
*/
|
|
public function createCustomizationItem($id_order_detail, $product, $data)
|
|
{
|
|
$design = null;
|
|
|
|
if (_PS_VERSION_ < 1.7) {
|
|
foreach ($data['datas'] as $customization) {
|
|
if (count($customization) === 1) {
|
|
$design = $customization[0]['value'];
|
|
$quantity = (int)$data['quantity'];
|
|
} else {
|
|
foreach ($customization as $customizationData) {
|
|
if ($customizationData['name'] === 'Customization'
|
|
&& strpos($customizationData['value'], '-') !== false) {
|
|
$design = $customizationData['value'];
|
|
$quantity = (int)$data['quantity'];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
$id_customization = $data['id_customization'];
|
|
$query = 'SELECT `value` FROM `' . _DB_PREFIX_ . 'customized_data`
|
|
WHERE `id_customization` = ' . (int)($id_customization) . '
|
|
AND `id_module` = ' . (int)($this->module->id);
|
|
$value = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query);
|
|
if (!$value) {
|
|
return null;
|
|
}
|
|
|
|
$zakekeData = json_decode($value, true);
|
|
$design = $zakekeData['d'];
|
|
$quantity = (int)$product['product_quantity'];
|
|
}
|
|
|
|
if (!$design) {
|
|
return null;
|
|
}
|
|
|
|
if (ZakekeItem::zakekeItemId($design) === false) {
|
|
return null;
|
|
}
|
|
|
|
return array(
|
|
'designDocID' => $design,
|
|
'orderDetailCode' => (string)$id_order_detail,
|
|
'quantity' => $quantity,
|
|
'designUnitPrice' => (float)$product['unit_price_tax_excl'] - (float)$product['price'],
|
|
'modelUnitPrice' => (float)$product['price']
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param int $id_order_detail
|
|
* @param Product $product
|
|
* @param array $data
|
|
* @return array|null
|
|
*/
|
|
public function createConfigurationItem($id_order_detail, $product, $data)
|
|
{
|
|
$configuration = null;
|
|
|
|
if (_PS_VERSION_ < 1.7) {
|
|
foreach ($data['datas'] as $customization) {
|
|
if (count($customization) === 1) {
|
|
$configuration = $customization[0]['value'];
|
|
$quantity = (int)$data['quantity'];
|
|
} else {
|
|
foreach ($customization as $customizationData) {
|
|
if ($customizationData['name'] === 'Configuration'
|
|
&& strpos($customizationData['value'], '-') !== false) {
|
|
$configuration = $customizationData['value'];
|
|
$quantity = (int)$data['quantity'];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
$id_customization = $data['id_customization'];
|
|
$query = 'SELECT `value` FROM `' . _DB_PREFIX_ . 'customized_data`
|
|
WHERE `id_customization` = ' . (int)($id_customization) . '
|
|
AND `id_module` = ' . (int)($this->module->id);
|
|
$value = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query);
|
|
if (!$value) {
|
|
return null;
|
|
}
|
|
|
|
$zakekeData = json_decode($value, true);
|
|
$configuration = $zakekeData['c'];
|
|
$quantity = (int)$product['product_quantity'];
|
|
}
|
|
|
|
if (!$configuration) {
|
|
return null;
|
|
}
|
|
|
|
if (ZakekeConfiguratorItem::zakekeConfiguratorItemId($configuration) === false) {
|
|
return null;
|
|
}
|
|
|
|
return array(
|
|
'composition' => $configuration,
|
|
'orderDetailCode' => (string)$id_order_detail,
|
|
'quantity' => $quantity,
|
|
'unitPrice' => (float)$product['price']
|
|
);
|
|
}
|
|
}
|