Current File : /var/www/vinorea/modules/ipexportimport/classes/import/order/EIAOrder.php
<?php
/**
 *
 * NOTICE OF LICENSE
 *
 *  @author    SmartPresta <tehran.alishov@gmail.com>
 *  @copyright 2024 SmartPresta
 *  @license   Commercial License
 */

if (!defined('_PS_VERSION_')) {
    exit;
}

require_once(dirname(__FILE__) . '/EIACart.php');
require_once(dirname(__FILE__) . '/EIAOrderInvoice.php');
require_once(dirname(__FILE__) . '/EIAOrderCarrier.php');
require_once(dirname(__FILE__) . '/EIAOrderComment.php');
require_once(dirname(__FILE__) . '/EIAMessage.php');
require_once(dirname(__FILE__) . '/EIAOrderCartRule.php');
require_once(dirname(__FILE__) . '/EIAOrderSlip.php');

class EIAOrder
{

    public $id;
    public $id_lang;
    public $id_shop;
    public $id_cart;
    public $baseOrderData;
    public $invoiceAddress;
    public $id_carrier;
    public $id_currency;
    public $id_customer;
    public $id_address_delivery;
    public $id_address_invoice;
    public $reference;
    public $recyclable;
    public $gift;
    public $gift_message;
    public $conversion_rate;
    public $payment;
    public $module;
    public $total_paid;
    public $total_paid_tax_incl;
    public $total_paid_tax_excl;
    public $total_paid_real;
    public $total_products;
    public $total_products_wt;
    public $total_discounts_tax_incl;
    public $total_discounts_tax_excl;
    public $total_shipping;
    public $total_shipping_tax_incl;
    public $total_shipping_tax_excl;
    public $total_wrapping_tax_incl;
    public $total_wrapping_tax_excl;
    public $carrier_tax_rate;
    public $valid;
    public $mobile_theme;
    public $secure_key;
    public $shipping_number;
    public $delivery_number;
    public $delivery_date;
    public $date_add;
    public $date_upd;
    public $invoice_number;
    public $invoice_date;
    public $current_state;
    public $identify_order;
    public $row;
    public $id_process;
    public $validateOnly;
    private $obj;

    public function __construct($id = null, $identify_order = null, $orderData = null, $row = null, $id_process = null, $validateOnly = false)
    {
        $this->baseOrderData = $orderData['order'];
        $this->invoiceAddress = $orderData['address_invoice'];
        $this->identify_order = (int) $identify_order;
        $this->id_process = $id_process;
        $this->row = $row;
        $this->id = is_numeric($id) ? $id : null;
        $this->obj = new Order($this->id);
        $this->id_shop = $this->getShopId();
        $this->id_lang = $this->getLangId();
        $this->id_customer = $this->obj->id_customer;
        $this->validateOnly = $validateOnly;
    }

    public function update()
    {
        $obj = $this->obj;
        $this->getObjectVariables($this->baseOrderData, $obj);
        $obj->id_shop = $this->id_shop;
        $obj->id_lang = $this->id_lang;
        $obj->reference = $this->reference;
        $obj->id_currency = $this->id_currency;
        $obj->id_carrier = $this->id_carrier;
        $obj->id_customer = $this->id_customer;
        $obj->id_address_invoice = $this->id_address_invoice;
        $obj->id_address_delivery = $this->id_address_delivery;
        $obj->recyclable = $this->recyclable;
        $obj->gift = $this->gift;
        $obj->gift_message = $this->gift_message;
        $obj->conversion_rate = $this->conversion_rate;
        $obj->payment = $this->payment;
        $obj->module = $this->module;
        $obj->total_paid = $this->total_paid;
        $obj->total_paid_tax_incl = $this->total_paid_tax_incl;
        $obj->total_paid_tax_excl = $this->total_paid_tax_excl;
        $obj->total_paid_real = $this->total_paid_real;
        $obj->total_products = $this->total_products;
        $obj->total_products_wt = $this->total_products_wt;
        $obj->total_discounts_tax_incl = $this->total_discounts_tax_incl;
        $obj->total_discounts_tax_excl = $this->total_discounts_tax_excl;
        $obj->total_shipping = $this->total_shipping;
        $obj->total_shipping_tax_incl = $this->total_shipping_tax_incl;
        $obj->total_shipping_tax_excl = $this->total_shipping_tax_excl;
        $obj->total_wrapping_tax_incl = $this->total_wrapping_tax_incl;
        $obj->total_wrapping_tax_excl = $this->total_wrapping_tax_excl;
        $obj->carrier_tax_rate = $this->carrier_tax_rate;
        $obj->valid = $this->valid;
        $obj->mobile_theme = $this->mobile_theme;
        $obj->secure_key = $this->secure_key;
        $obj->shipping_number = $this->shipping_number;
        $obj->delivery_number = $this->delivery_number;
        $obj->delivery_date = $this->delivery_date;
        $obj->date_add = $this->date_add;
        $obj->date_upd = $this->date_upd;
        $obj->invoice_number = $this->invoice_number;
        $obj->invoice_date = $this->invoice_date;
        $obj->current_state = $this->current_state;

        if (!$obj->id_cart) {
            $obj->id_cart = $this->createCart($obj);
        }

        if (isset($this->baseOrderData['id_order']) && $this->baseOrderData['id_order'] && !$this->id && ($this->identify_order == EIAIdentifyOrder::IDENTIFY_ORDER_ID)) {
            $obj->force_id = true;
            $obj->id = (int) $this->baseOrderData['id_order'];
            if (($error = $obj->validateFields(false, true)) !== true) {
                throw new PrestaShopException("Error: $error. ID: $obj->id. Row in file: $this->row.");
            }
            if (!$this->validateOnly) {
                $obj->add();
            }
            if ($this->date_add || $this->date_upd) {
                $obj->date_add = $this->date_add;
                $obj->date_upd = $this->date_upd;
                if (!$this->validateOnly) {
                    $obj->save();
                }
            }
        } else {
            if (($error = $obj->validateFields(false, true)) !== true) {
                throw new PrestaShopException("Error: $error. ID: $obj->id. Row in file: $this->row.");
            }
            if (!$this->validateOnly) {
                $obj->save();
            }
        }

        $this->id = $obj->id;

        if ($obj->id_customer && $obj->id) {
//            $this->saveOrderComment(); // This is for updating the ps_order_message table, which contains message templates
            $this->saveOrderMessage($obj->id_cart);
        }

        if ($obj->id_customer && $obj->id) {
            $this->saveOrderSlip();
        }

        if (isset($this->baseOrderData['cart_rule_name']) && $this->baseOrderData['cart_rule_name']) {
            $this->saveOrderCartRule();
        }

        return $this->id;
    }

    private function saveOrderCartRule()
    {
        $objCarrier = new EIAOrderCartRule($this->id, $this->baseOrderData, $this->row, $this->id_process, $this->validateOnly);
        $objCarrier->save();
    }

    private function saveOrderComment()
    {
        if (isset($this->baseOrderData['message']) && $this->baseOrderData['message']) {
            $objComment = new EIAOrderComment($this->id, $this->id_shop, $this->id_lang, $this->id_customer, $this->baseOrderData, $this->row, $this->id_process, $this->validateOnly);
            $objComment->save();
        }
    }

    private function saveOrderMessage($id_cart)
    {
        if (isset($this->baseOrderData['message']) && $this->baseOrderData['message']) {
            $objmessage = new EIAMessage($this->id, $id_cart, $this->id_customer, $this->baseOrderData, $this->row, $this->id_process, $this->validateOnly);
            $objmessage->save();
        }
    }

    private function saveOrderSlip()
    {
        if ((isset($this->baseOrderData['order_slip_total_products_tax_excl']) && $this->baseOrderData['order_slip_total_products_tax_excl']) || (isset($this->baseOrderData['order_slip_total_products_tax_incl']) && $this->baseOrderData['order_slip_total_products_tax_incl'])) {
            $objSlip = new EIAOrderSlip($this->id, $this->id_customer, $this->id_currency, $this->baseOrderData, $this->row, $this->id_process, $this->validateOnly);
            $objSlip->save();
        }
    }

    public function createCart($obj)
    {
        $cart = new EIACart();
        return $this->id_cart = $cart->save($obj, $this->row, $this->id_process, $this->validateOnly);
    }

    private function getOrderStatus($id_status = false, $status_name = "")
    {
        if ($id_status) {
            return $id_status;
        }
        if ($status_name) {
            $sql = "
                SELECT *
                FROM " . _DB_PREFIX_ . "order_state_lang as ol
                WHERE ol.name = '" . $status_name . "'
                AND ol.id_lang = " . $this->id_lang . "
               ";
            $status = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
            if (isset($status['id_order_state']) && $status['id_order_state']) {
                return $status['id_order_state'];
            }
        }
        return Configuration::get('PS_OS_CHEQUE');
    }

    private function orderNumbersFromString($number)
    {
        preg_match('/\d+/', $number, $matches);
        if (isset($matches[0]) && $matches[0]) {
            return $matches[0];
        }
        return 0;
    }

    private function isInstalledCurrency()
    {
        $sql = (new DbQuery())
                ->select('1')
                ->from('currency', 'c')
                ->innerJoin('currency_shop', 'cs', 'c.`id_currency` = cs.`id_currency`')
                ->where('c.`id_currency` = ' . (int) $this->id_currency)
                ->where('cs.`id_shop` = ' . (int) $this->id_shop)
//                ->where('c.`deleted` = 0')
//                ->where('c.`active` = 1')
                ;

        return (bool) Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql);
    }

    private function getObjectVariables($data, $obj)
    {
        if (isset($data['recyclable'])) {
            $this->recyclable = $data['recyclable'] ? (int) $data['recyclable'] : 0;
        } else {
            $this->recyclable = $obj->recyclable;
        }
        if (isset($data['id_currency'])) {
            $this->id_currency = $data['id_currency'] ? $data['id_currency'] : 0;
        } else {
            $this->id_currency = $obj->id_currency;
        }

        if (!$this->isInstalledCurrency()) {
            $this->id_currency = Configuration::get('PS_CURRENCY_DEFAULT');
        }

        if (!$this->id_address_invoice) {
            $this->id_address_invoice = $obj->id_address_invoice;
        }
        if (!$this->id_address_delivery) {
            $this->id_address_delivery = $obj->id_address_delivery;
        }
        
        if (isset($data['id_carrier'])) {
            $this->id_carrier = $data['id_carrier'] ? $data['id_carrier'] : 0;
        } elseif (!empty($data['carrier_name'])) {
            $obj->id_carrier = EIAOrdersImport::getCarrierIdByName($data['carrier_name']);
        } elseif (!empty($data['id_reference'])) {
            $obj->id_carrier = EIAOrdersImport::getCarrierIdByReference($data['id_reference']);
        } else {
            $this->id_carrier = $obj->id_carrier;
        }
        if (isset($data['gift'])) {
            $this->gift = $data['gift'] ? $data['gift'] : 0;
        } else {
            $this->gift = $obj->gift;
        }
        if (isset($data['reference'])) {
            $this->reference = $data['reference'] ? $data['reference'] : '';
        } else {
            $this->reference = $obj->reference;
        }

        if (!$this->reference) {
            $this->reference = strtoupper(Tools::passwdGen(9, 'NO_NUMERIC'));
        }

        if (isset($data['gift_message'])) {
            $this->gift_message = $data['gift_message'] ? $data['gift_message'] : '';
        } else {
            $this->gift_message = $obj->gift_message;
        }
        if (isset($data['conversion_rate'])) {
            $this->conversion_rate = $data['conversion_rate'] ? EIATools::preparePrice($data['conversion_rate']) : 0;
        } else {
            $this->conversion_rate = $obj->conversion_rate;
        }
        if (isset($data['payment'])) {
            $this->payment = $data['payment'] ? $data['payment'] : '';
        } else {
            $this->payment = $obj->payment;
        }
        if (isset($data['module'])) {
            $this->module = $data['module'] ? $data['module'] : '';
        } else {
            $this->module = $obj->module;
        }
        if (isset($data['total_paid'])) {
            $this->total_paid = $data['total_paid'] ? EIATools::preparePrice($data['total_paid']) : 0;
        } else {
            $this->total_paid = $obj->total_paid;
        }
        if (isset($data['total_paid_tax_incl'])) {
            $this->total_paid_tax_incl = $data['total_paid_tax_incl'] ? EIATools::preparePrice($data['total_paid_tax_incl']) : 0;
        } else {
            $this->total_paid_tax_incl = $obj->total_paid_tax_incl;
        }
        if (isset($data['total_paid_tax_excl'])) {
            $this->total_paid_tax_excl = $data['total_paid_tax_excl'] ? EIATools::preparePrice($data['total_paid_tax_excl']) : 0;
        } else {
            $this->total_paid_tax_excl = $obj->total_paid_tax_excl;
        }
        if (isset($data['total_paid_real'])) {
            $this->total_paid_real = $data['total_paid_real'] ? EIATools::preparePrice($data['total_paid_real']) : 0;
        } else {
            $this->total_paid_real = $obj->total_paid_real;
        }
        if (!$this->total_paid_real) {
            $this->total_paid_real = 0;
        }
        if (isset($data['total_products'])) {
            $this->total_products = $data['total_products'] ? EIATools::preparePrice($data['total_products']) : 0;
        } else {
            $this->total_products = $obj->total_products;
        }
        if (isset($data['total_products_wt'])) {
            $this->total_products_wt = $data['total_products_wt'] ? EIATools::preparePrice($data['total_products_wt']) : 0;
        } else {
            $this->total_products_wt = $obj->total_products_wt;
        }
        if (isset($data['total_discounts_tax_incl'])) {
            $this->total_discounts_tax_incl = $data['total_discounts_tax_incl'] ? EIATools::preparePrice($data['total_discounts_tax_incl']) : 0;
        } else {
            $this->total_discounts_tax_incl = $obj->total_discounts_tax_incl;
        }
        if (isset($data['total_discounts_tax_excl'])) {
            $this->total_discounts_tax_excl = $data['total_discounts_tax_excl'] ? EIATools::preparePrice($data['total_discounts_tax_excl']) : 0;
        } else {
            $this->total_discounts_tax_excl = $obj->total_discounts_tax_excl;
        }
        if (isset($data['total_shipping'])) {
            $this->total_shipping = $data['total_shipping'] ? EIATools::preparePrice($data['total_shipping']) : 0;
        } else {
            $this->total_shipping = $obj->total_shipping;
        }
        if (isset($data['total_shipping_tax_incl'])) {
            $this->total_shipping_tax_incl = $data['total_shipping_tax_incl'] ? EIATools::preparePrice($data['total_shipping_tax_incl']) : 0;
        } else {
            $this->total_shipping_tax_incl = $obj->total_shipping_tax_incl;
        }
        if (isset($data['total_shipping_tax_excl'])) {
            $this->total_shipping_tax_excl = $data['total_shipping_tax_excl'] ? EIATools::preparePrice($data['total_shipping_tax_excl']) : 0;
        } else {
            $this->total_shipping_tax_excl = $obj->total_shipping_tax_excl;
        }
        if (isset($data['total_wrapping_tax_incl'])) {
            $this->total_wrapping_tax_incl = $data['total_wrapping_tax_incl'] ? EIATools::preparePrice($data['total_wrapping_tax_incl']) : 0;
        } else {
            $this->total_wrapping_tax_incl = $obj->total_wrapping_tax_incl;
        }
        if (isset($data['total_wrapping_tax_excl'])) {
            $this->total_wrapping_tax_excl = $data['total_wrapping_tax_excl'] ? EIATools::preparePrice($data['total_wrapping_tax_excl']) : 0;
        } else {
            $this->total_wrapping_tax_excl = $obj->total_wrapping_tax_excl;
        }
        if (isset($data['carrier_tax_rate'])) {
            $this->carrier_tax_rate = $data['carrier_tax_rate'] ? EIATools::preparePrice($data['carrier_tax_rate']) : 0;
        } else {
            $this->carrier_tax_rate = $obj->carrier_tax_rate;
        }
        if (isset($data['valid'])) {
            $this->valid = $data['valid'] ? $data['valid'] : 0;
        } else {
            $this->valid = $obj->valid;
        }
        if (isset($data['mobile_theme'])) {
            $this->mobile_theme = $data['mobile_theme'] ? $data['mobile_theme'] : 0;
        } else {
            $this->mobile_theme = $obj->mobile_theme;
        }
        if (isset($data['secure_key'])) {
            $this->secure_key = $data['secure_key'] ? $data['secure_key'] : '';
        } else {
            $this->secure_key = $obj->secure_key;
        }
        if (isset($data['shipping_number'])) {
            $this->shipping_number = $data['shipping_number'] ? $data['shipping_number'] : '';
        } else {
            $this->shipping_number = isset($obj->shipping_number) ? $obj->shipping_number : 0;
        }
        if (isset($data['delivery_date'])) {
            $this->delivery_date = $data['delivery_date'] ? $data['delivery_date'] : '';
        } else {
            $this->delivery_date = $obj->delivery_date;
        }
        if (isset($data['date_add'])) {
            $this->date_add = $data['date_add'] ? $data['date_add'] : '';
        } else {
            $this->date_add = $obj->date_add;
        }
        if (isset($data['date_upd'])) {
            $this->date_upd = $data['date_upd'] ? $data['date_upd'] : '';
        } else {
            $this->date_upd = $obj->date_upd;
        }
        if (isset($data['delivery_number'])) {
            $this->delivery_number = $data['delivery_number'] ? $this->orderNumbersFromString($data['delivery_number']) : 0;
        } else {
            $this->delivery_number = $obj->delivery_number;
        }
        if (isset($data['invoice_number'])) {
            $this->invoice_number = $data['invoice_number'] ? $this->orderNumbersFromString($data['invoice_number']) : 0;
        } else {
            $this->invoice_number = $obj->invoice_number;
        }
        if (isset($data['invoice_date'])) {
            $this->invoice_date = $data['invoice_date'] ? $data['invoice_date'] : '';
        } else {
            $this->invoice_date = $obj->invoice_date;
        }
        if (isset($data['current_state']) || isset($data['current_state_name'])) {
            $this->current_state = $this->getOrderStatus(isset($data['current_state']) ? $data['current_state'] : 0, isset($data['current_state_name']) ? $data['current_state_name'] : '');
        } else {
            $this->current_state = $obj->current_state;
        }
    }
    
    private function getShopId()
    {
        if (isset($this->baseOrderData['id_shop']) && (int) $this->baseOrderData['id_shop']) {
            $id_shop = (int) $this->baseOrderData['id_shop'];
        } else {
            $id_shop = isset($this->obj->id_shop) ? $this->obj->id_shop : 0;
        }
        
        if (!$id_shop) {
            $id_shop = Tools::getValue('shops');
            if (is_array($id_shop)) {
               $id_shop = $id_shop[0];
            }
        }
        
        return $id_shop;
    }
    
    private function getLangId()
    {
        if (isset($this->baseOrderData['id_lang']) && (int) $this->baseOrderData['id_lang']) {
            $id_lang = (int) $this->baseOrderData['id_lang'];
        } else {
            $id_lang = isset($this->obj->id_lang) ? $this->obj->id_lang : 0;
        }
        
        if (!$id_lang) {
            $id_lang = Tools::getValue('language');
        }
        
        return $id_lang;
    }

}