<?php
class ModelModuleLimitedtimediscount extends Model
{
    private $module = array();

    public function __construct($registry)
    {
        parent::__construct($registry);

        $this->config->load('isenselabs/limitedtimediscount');
        $this->module = $this->config->get('limitedtimediscount');

        $this->load->model('setting/setting');

        // Module setting
        $setting = $this->model_setting_setting->getSetting($this->module['setting_code'], $this->config->get('config_store_id'));
        $this->module['setting'] = array_replace_recursive(
            $this->module['setting'],
            !empty($setting[$this->module['setting_key']]) ? $setting[$this->module['setting_key']] : array()
        );
    }

    public function addActivity($ip, $params)
    {
        $this->db->query("DELETE FROM `" . DB_PREFIX . "ltd_activity` WHERE `ip_address` = '" . $this->db->escape($ip) . "'");
        $this->db->query("INSERT INTO `" . DB_PREFIX . "ltd_activity` SET `ip_address` = '" . $this->db->escape($ip) . "', `params` = '" . $this->db->escape(json_encode($params)) . "'");
    }

    public function getActivity($ip)
    {
        $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "ltd_activity` WHERE `ip_address` = '" . $this->db->escape($ip) . "'");
        return $query->row;
    }

    /**
     * Clone of admin marketing coupon model
     */
    public function addCoupon($data) {
        $this->db->query("INSERT INTO " . DB_PREFIX . "coupon SET name = '" . $this->db->escape($data['name']) . "', code = '" . $this->db->escape($data['code']) . "', discount = '" . (float)$data['discount'] . "', type = '" . $this->db->escape($data['type']) . "', total = '" . (float)$data['total'] . "', logged = '" . (int)$data['logged'] . "', shipping = '" . (int)$data['shipping'] . "', date_start = '" . $this->db->escape($data['date_start']) . "', date_end = '" . $this->db->escape($data['date_end']) . "', uses_total = '" . (int)$data['uses_total'] . "', uses_customer = '" . (int)$data['uses_customer'] . "', status = '" . (int)$data['status'] . "', date_added = NOW()");

        $coupon_id = $this->db->getLastId();

        if (isset($data['coupon_product'])) {
            foreach ($data['coupon_product'] as $product_id) {
                $this->db->query("INSERT INTO " . DB_PREFIX . "coupon_product SET coupon_id = '" . (int)$coupon_id . "', product_id = '" . (int)$product_id . "'");
            }
        }

        if (isset($data['coupon_category'])) {
            foreach ($data['coupon_category'] as $category_id) {
                $this->db->query("INSERT INTO " . DB_PREFIX . "coupon_category SET coupon_id = '" . (int)$coupon_id . "', category_id = '" . (int)$category_id . "'");
            }
        }

        return $coupon_id;
    }

    public function checkCoupon($code)
    {
        $query = $this->db->query("SELECT DISTINCT * FROM `" . DB_PREFIX . "coupon` WHERE `code` = '" . $this->db->escape($code) . "'");
        return $query->row;
    }

    public function disableCoupon($code)
    {
        // Validate that coupon is not applied to valid order
        $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_total` ot LEFT JOIN `" . DB_PREFIX . "order` o ON ot.order_id = o.order_id  WHERE ot.title LIKE '%" . $this->db->escape($code) . "%' AND o.order_status_id != 0");

        if (!$query->rows) {
            $this->db->query("UPDATE `" . DB_PREFIX . "coupon` SET `status`='0' WHERE `code` = '" . $this->db->escape($code) . "'");
        }
    }
}
