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

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

class EIAError
{

    const TABLE_NAME = 'ipordersimport_errors';
    const TABLE_NAME_WITH_PREFIX = _DB_PREFIX_ . self::TABLE_NAME;

    public static function createErrorsFile($id_process)
    {
        $errors = self::getErrors($id_process);
        if ($errors) {
            $write_fd = fopen(_PS_MODULE_DIR_ . 'ipexportimport/error/error_logs_' . $id_process . '.csv', 'a+');
            foreach ($errors as $error) {
                if (@$write_fd !== false) {
                    $line = "";
                    if ($error['row']) {
                        $line .= EIATranslatorWrapper::l('Number row in file: ') . $error['row'];
                    }
                    if ($error['id_order']) {
                        $line .= ", " . EIATranslatorWrapper::l('Order ID in Store: ') . $error['id_order'];
                    }
                    $line .= " - " . EIATranslatorWrapper::l('Error message: ') . $error['message'];
                    $line .= "\r\n";
                    fwrite($write_fd, $line);
                }
            }
            fclose($write_fd);
        }
        return false;
    }

    public static function getErrors($id_process)
    {
        $sql = "
          SELECT * 
          FROM " . self::TABLE_NAME_WITH_PREFIX . " as e
          WHERE e.id_process = " . (int) $id_process . "
        ";
        return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
    }

    public static function getErrorsData($id_process)
    {
        $sql = "
          SELECT count(*) as count_errors 
          FROM " . self::TABLE_NAME_WITH_PREFIX . " as e
          WHERE e.id_process = " . (int) $id_process . "
        ";
        return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql);
    }

    public static function setErrorToDb($error = false, $id_order = false, $row = false, $id_process = false)
    {
        $data = array();
        if ($error) {
            $data['message'] = $error;
        }
        if ($error) {
            $data['id_order'] = $id_order;
        }
        if ($row) {
            $data['row'] = $row;
        }
        if ($id_process) {
            $data['id_process'] = $id_process;
        }
        $data['date_add'] = date('Y-m-d H:i:s');
        Db::getInstance(_PS_USE_SQL_SLAVE_)->insert(self::TABLE_NAME, $data);
        return false;
    }

    public static function cleareErrorsFile()
    {
        $write_fd = fopen(_PS_MODULE_DIR_ . 'ipexportimport/error/error_logs.csv', 'w');
        fclose($write_fd);
    }

    public static function removeImportErrors()
    {
        Db::getInstance(_PS_USE_SQL_SLAVE_)->delete(self::TABLE_NAME);
    }

}