Files
wyczarujprezent.pl/modules/ecsgtmpro/classes/EcsGtmProOrder.php
2024-10-28 22:14:22 +01:00

188 lines
5.8 KiB
PHP

<?php
/**
* 2022 ECSoft
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to dev.ecsoft@gmail.com so we can send you a copy immediately.
*
* @author ECSoft <dev.ecsoft@gmail.com>
* @copyright 2022 ECSoft
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of ECSoft
*/
class EcsGtmProOrder extends ObjectModel
{
const TABLE_NAME = 'ecsgtmpro_order';
public $id;
public $id_order;
public $id_shop;
public $refund;
public $sent;
public $resent;
public $datalayer;
public $date_add;
public $date_upd;
public static $definition = array(
'table' => self::TABLE_NAME,
'primary' => 'id',
'fields' => array(
'id_order' => array('type' => self::TYPE_INT),
'id_shop' => array('type' => self::TYPE_INT),
'refund' => array('type' => self::TYPE_STRING),
'sent' => array('type' => self::TYPE_BOOL),
'resent' => array('type' => self::TYPE_INT),
'datalayer' => array('type' => self::TYPE_STRING),
'date_add' => array('type' => self::TYPE_DATE),
'date_upd' => array('type' => self::TYPE_DATE)
)
);
public static function installTable()
{
$sql = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . self::TABLE_NAME . '` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`id_order` INT(11) NOT NULL,
`id_shop` INT(11) NOT NULL,
`refund` VARCHAR(128) NULL DEFAULT NULL,
`sent` TINYINT(1) NOT NULL DEFAULT "0",
`resent` TINYINT(1) NOT NULL DEFAULT "0",
`datalayer` TEXT NOT NULL,
`date_add` DATETIME NULL DEFAULT NULL,
`date_upd` DATETIME NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `id_order` (`id_order`)
)
COLLATE="utf8_general_ci"';
return Db::getInstance()->execute($sql);
}
public static function uninstallTable()
{
return Db::getInstance()->execute('DROP TABLE IF EXISTS `' . _DB_PREFIX_ . self::TABLE_NAME . '`');
}
public static function getByOrderId($id_order, $id_shop = null)
{
$sql = "SELECT id from `" . _DB_PREFIX_ . self::TABLE_NAME . "` WHERE id_order = '".(int)$id_order."'";
if (!is_null($id_shop)) {
$sql .= " and id_shop = '".(int)$id_shop."'";
}
$id = (int) Db::getInstance()->getValue($sql);
if ($id) {
return new self($id);
}
return null;
}
public static function buildWhere($where)
{
$sql = '';
if ($where !== null && is_array($where) && count($where)) {
foreach ($where as $key => $value) {
$sql .= " AND `" . pSQL($key) . "` = '" . pSQL($value) . "'";
}
}
return $sql;
}
public static function countByOrderId($id_order, $id_shop, $where = null)
{
$sql = "SELECT count(*) as nb from `" . _DB_PREFIX_ . self::TABLE_NAME . "` WHERE id_order = '".(int)$id_order."' and id_shop = '" . (int)$id_shop . "'" . self::buildWhere($where);
$result = Db::getInstance()->getRow($sql);
$count = 0;
if (isset($result['nb'])) {
$count = (int) $result['nb'];
}
return $count;
}
public static function isRefundable($id_order, $id_shop, $id_product_with_attribute = null)
{
$order = self::getByOrderId($id_order, $id_shop);
if (!$order) {
return false;
}
if (!$order->sent && !$order->resent) {
return false;
}
if (!$order->refund) {
return true;
}
if ($order->refund == "all") {
return false;
}
$products = explode(',', $order->refund);
if (in_array($id_product_with_attribute, $products)) {
return false;
} else {
return true;
}
}
public static function getNotSent($id_shop = null, $days = null, $limit = null)
{
$sql = "SELECT t.id, t.id_order, t.id_shop from `" . _DB_PREFIX_ . self::TABLE_NAME."` t
LEFT JOIN `" . _DB_PREFIX_ . "orders` o ON (o.id_order = t.id_order)
WHERE sent = 0 AND resent = 0";
if (!is_null($id_shop)) {
$sql .= " AND gtmol.id_shop = '".(int)$id_shop."'";
}
if (!is_null($days)) {
$date = date("Y-m-d", strtotime("- ".(int) $days ." days"));
$sql .= " AND o.date_add >= '".$date."'";
}
if (!is_null($limit)) {
$sql .= " LIMIT ".(int) $limit;
}
return DB::getInstance()->executeS($sql);
}
public static function isSent($id_order, $id_shop)
{
$sql = "SELECT count(*) as nb from `" . _DB_PREFIX_ . self::TABLE_NAME . "`
WHERE id_order = '".(int)$id_order."' AND (sent = 1 OR resent = 1) and id_shop = '".(int)$id_shop."'";
$result = Db::getInstance()->getRow($sql);
$count = 0;
if (isset($result['nb'])) {
$count = (int) $result['nb'];
}
return ($count > 0);
}
public static function cleanUp($maxAge = 365, $where = null)
{
$maxAge = (int)$maxAge;
if ($maxAge < 0) {
$maxAge = 1;
}
$sql = "DELETE FROM `" . _DB_PREFIX_.self::TABLE_NAME . "` WHERE datediff(now(), `date_add`) > " . $maxAge . self::buildWhere($where);
return Db::getInstance()->execute($sql);
}
}