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

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

class EIAOrderSlip
{

    public $id_order;
    public $id_customer;
    public $baseOrderData;
    public $shipping_cost;
    public $shipping_cost_amount;
    public $total_shipping_tax_excl;
    public $total_shipping_tax_incl;
    public $total_products_tax_excl;
    public $total_products_tax_incl;
    public $date_add;
    public $id_currency;
    public $partial;
    public $order_slip_type;
    public $amount;
    public $conversion_rate;
    public $row;
    public $id_process;
    public $validateOnly; 

    public function __construct($id_order = null, $id_customer = null, $id_currency = null, $baseOrderData = null, $row = null, $id_process = null, $validateOnly = false)
    {
        $this->id_currency = (int) $id_currency;
        $this->id_order = (int) $id_order;
        $this->id_customer = (int) $id_customer;
        $this->baseOrderData = $baseOrderData;
        $this->row = $row;
        $this->id_process = $id_process;
        $this->validateOnly = $validateOnly;
    }

    public function save()
    {
//        $id_order_slip = $this->getOrderSlip();
        if (empty($this->baseOrderData['order_slip_amount'])) {
            return false;
        }

        foreach ($this->baseOrderData['order_slip_amount'] as $key => $val) {
            $obj = new OrderSlip(!empty($this->baseOrderData['id_order_slip'][$key]) ? $this->baseOrderData['id_order_slip'][$key] : null);
            $this->getObjectVariables($this->baseOrderData, $obj, $key);
            $obj->id_order = $this->id_order;
            $obj->id_customer = $this->id_customer;
            $obj->total_products_tax_excl = $this->total_products_tax_excl;
            $obj->total_products_tax_incl = $this->total_products_tax_incl;
            $obj->total_shipping_tax_excl = $this->total_shipping_tax_excl;
            $obj->total_shipping_tax_incl = $this->total_shipping_tax_incl;
            $obj->conversion_rate = $this->getConversionRate();
            $obj->amount = $this->amount;
            $obj->shipping_cost = $this->shipping_cost;
            $obj->shipping_cost_amount = $this->shipping_cost_amount;
            $obj->partial = $this->partial;
            $obj->order_slip_type = $this->order_slip_type;
            $obj->date_add = $this->date_add;
            if (($error = $obj->validateFields(false, true)) !== true) {
                throw new PrestaShopException("Error: $error. ID: $this->id_order. Row in file: $this->row.");
            }
            $this->validateOnly || $obj->save();
        }
    }

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

        if (isset($data['order_slip_shipping_cost_amount'][$key])) {
            $this->shipping_cost_amount = $data['order_slip_shipping_cost_amount'][$key] ? EIATools::preparePrice($data['order_slip_shipping_cost_amount'][$key]) : 0;
        } else {
            $this->shipping_cost_amount = $obj->shipping_cost_amount;
        }

        if (isset($data['order_slip_total_shipping_tax_excl'][$key])) {
            $this->total_shipping_tax_excl = $data['order_slip_total_shipping_tax_excl'][$key] ? EIATools::preparePrice($data['order_slip_total_shipping_tax_excl'][$key]) : 0;
        } else {
            $this->total_shipping_tax_excl = $obj->total_shipping_tax_excl;
        }

        if (isset($data['order_slip_total_shipping_tax_incl'][$key])) {
            $this->total_shipping_tax_incl = $data['order_slip_total_shipping_tax_incl'][$key] ? EIATools::preparePrice($data['order_slip_total_shipping_tax_incl'][$key]) : 0;
        } else {
            $this->total_shipping_tax_incl = $obj->total_shipping_tax_incl;
        }

        if (isset($data['order_slip_total_products_tax_excl'][$key])) {
            $this->total_products_tax_excl = $data['order_slip_total_products_tax_excl'][$key] ? EIATools::preparePrice($data['order_slip_total_products_tax_excl'][$key]) : 0;
        } else {
            $this->total_products_tax_excl = $obj->total_products_tax_excl;
        }

        if (isset($data['order_slip_total_products_tax_incl'][$key])) {
            $this->total_products_tax_incl = $data['order_slip_total_products_tax_incl'][$key] ? EIATools::preparePrice($data['order_slip_total_products_tax_incl'][$key]) : 0;
        } else {
            $this->total_products_tax_incl = $obj->total_products_tax_incl;
        }

        if (isset($data['order_slip_date_add'][$key])) {
            $this->date_add = $data['order_slip_date_add'][$key] ? $data['order_slip_date_add'][$key] : date('Y-m-d H:i:s');
        } else {
            $this->date_add = $obj->date_add;
        }

        if (isset($data['order_slip_amount'][$key])) {
            $this->amount = $data['order_slip_amount'][$key] ? EIATools::preparePrice($data['order_slip_amount'][$key]) : 0;
        } else {
            $this->amount = $obj->amount;
        }

        if (isset($data['order_slip_partial'][$key])) {
            $this->partial = (int) $data['order_slip_partial'][$key];
        } else {
            $this->partial = $obj->partial;
        }

        if (isset($data['order_slip_type'][$key])) {
            $this->order_slip_type = (int) $data['order_slip_type'][$key];
        } else {
            $this->order_slip_type = $obj->order_slip_type;
        }
    }

    private function getConversionRate()
    {
        $order = new Order((int) $this->id_order);
        return $order->conversion_rate || ($this->validateOnly ? 1 : null);
    }

    private function getOrderSlip()
    {
        $sql = "
            SELECT *
            FROM " . _DB_PREFIX_ . "order_slip as o
            WHERE o.id_order = " . (int) $this->id_order . "
            AND o.id_customer = " . (int) $this->id_customer . "
           ";
        return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
    }

}