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

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

class EIAOrderComment
{

    public $id_order;
    public $id_customer;
    public $baseOrderData;
    public $id_shop;
    public $id_lang;
    public $id_customer_thread;
    public $row;
    public $id_process;
    public $validateOnly; 

    public function __construct($id_order = null, $id_shop = null, $id_lang = null, $id_customer = null, $baseOrderData = null, $row = null, $id_process = null, $validateOnly = false)
    {
        $this->id_lang = (int) $id_lang;
        $this->id_shop = (int) $id_shop;
        $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_customer_thread = $this->getIdCustomerThead();
        $obj = new CustomerThread($id_customer_thread);
        $obj->id_shop = $this->id_shop;
        $obj->id_lang = $this->id_lang;
        $obj->id_order = $this->id_order;
        $obj->id_customer = $this->id_customer;
        $obj->id_contact = 0;
        $obj->status = 'open';
        $obj->token = Tools::passwdGen(12);
        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();
        $this->id_customer_thread = $obj->id;
        $this->saveMessage();
    }

    private function saveMessage()
    {
        $id_customer_message = $this->getIdCustomerMessage();
        $obj = new CustomerMessage($id_customer_message);
        if (isset($this->baseOrderData['message'])) {
            $obj->message = $this->baseOrderData['message'] ? $this->baseOrderData['message'] : '';
            $obj->id_employee = 0;
            $obj->id_customer_thread = $this->id_customer_thread;
            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 getIdCustomerMessage()
    {
        $sql = "
            SELECT *
            FROM " . _DB_PREFIX_ . "customer_message as o
            WHERE o.id_customer_thread = " . (int) $this->id_customer_thread . "
         
           ";
        $customer_message = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
        if (isset($customer_message['id_customer_message']) && $customer_message['id_customer_message']) {
            return $customer_message['id_customer_message'];
        }
        return false;
    }

    private function getIdCustomerThead()
    {
        $sql = "
            SELECT *
            FROM " . _DB_PREFIX_ . "customer_thread as o
            WHERE o.id_order = " . (int) $this->id_order . "
            AND o.id_customer = " . (int) $this->id_customer . "
           ";
        $customer_thread = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
        if (isset($customer_thread['id_customer_thread']) && $customer_thread['id_customer_thread']) {
            return $customer_thread['id_customer_thread'];
        }
        return false;
    }

}