<?php

class ModelEntegrasyonProduct extends Model
{

    public function addProduct($data)
    {


      
 $varianter=isset($data['product_variant'])?1:0;
        $this->udb->query("INSERT INTO  product SET model = '" . $this->udb->escape($data['model']) . "', barcode = '" . $this->udb->escape($data['barcode']) . "', stock_code = '" . $this->udb->escape($data['stock_code']) . "', quantity = '" . (int)$data['quantity'] . "', sale_price = '" . (int)$data['sale_price'] . "', list_price = '" . (int)$data['list_price'] . "', manufacturer_id = '" . (int)$data['manufacturer_id'] . "', manufacturer_name = '" . $this->db->escape($data['manufacturer_name']) . "',  category_id = '" . (int)$data['category_id'] . "', varianter='".$varianter."', category_name = '" . $this->db->escape($data['category_name']) . "',  desi = '" . (float)$data['desi'] . "', shipping = '" . (int)1 . "',  status = '" . (int)$data['status'] . "', tax = '" . (int)$data['tax'] . "', sort_order = '" . (int)1 . "', date_added = NOW(), date_modified = NOW()");
        $product_id = $this->udb->getLastId();

        if (isset($data['product_image'][0])) {
            $this->udb->query("UPDATE product SET image = '" . $this->udb->escape($data['product_image'][0]) . "' WHERE product_id = '" . (int)$product_id . "'");

            unset($data['product_image'][0]);

        }

        foreach ($data['product_description'] as $language_id => $value) {
            $sql = "INSERT INTO product_description SET product_id = '" . (int)$product_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "', description = '" . $this->db->escape($value['description']) . "'";
            $this->udb->query($sql);

        }


        if (isset($data['product_image'])) {
            foreach ($data['product_image'] as $index => $product_image) {
                if ($index != 0) {
                    $this->udb->query("INSERT INTO  product_image SET product_id = '" . (int)$product_id . "', image = '" . $this->udb->escape($product_image) . "', sort_order = '" . (int)$index . "'");

                }
            }
        }

        if (isset($data['product_variant'])) {
            foreach ($data['product_variant'] as $index => $product_variant) {

                $this->udb->query("INSERT INTO  product_variant SET product_id = '" . (int)$product_id . "', image = '" . $this->udb->escape($product_variant['image']) . "',barcode='" . $this->db->escape($product_variant['barcode']) . "',stock_code='" . $this->db->escape($product_variant['stock_code']) . "',quantity='" . (int)$product_variant['quantity'] . "',sale_price='" . $product_variant['sale_price'] . "', sort_order = '" . (int)$index . "'");


                $product_variant_id = $this->udb->getLastId();
                $index = 0;
                foreach ($product_variant['option'] as $attribute_id => $option) {

                    $sql = "INSERT INTO product_variant_value SET product_variant_id='" . $product_variant_id . "', attribute_id='" . $attribute_id . "',  ";
                    if (is_numeric($option)) {
                        $sql .= " attribute_value_id='" . $option . "'";
                    } else {
                        $sql .= " attribute_value_id=(SELECT `attribute_value_id` FROM multimarket.`oc_attribute_value`   WHERE `attribute_id` = '" . $attribute_id . "' AND `name` LIKE '" . $option . "')";
                    }
                    $sql . " sort_order='" . $index . "'";

                    $this->udb->query($sql);


                    $index++;

                }


                if (isset($product_variant['marketplace_data'])) {

                    $query = $this->udb->query("select * from variant_to_marketplace where product_variant_id='" . $product_variant_id . "'");
                    $code = $product_variant['code'];

                    if ($query->num_rows) {

                        $this->udb->query("update variant_to_marketplace SET  $code='" . $this->db->escape(json_encode($product_variant['marketplace_data'])) . "', date_modified=NOW() where product_variant_id='" . $product_variant_id . "' ");

                    } else {


                        $this->udb->query("INSERT INTO variant_to_marketplace SET product_variant_id='" . $product_variant_id . "', $code='" . $this->db->escape(json_encode($product_variant['marketplace_data'])) . "', date_added=NOW() ");

                    }

                }


            }


        }


        // $this->cache->delete('product');

        return $product_id;
    }


    public function deleteProduct($product_id)
    {
        $this->udb->query("DELETE FROM product WHERE product_id = '" . (int)$product_id . "'");
        $this->udb->query("DELETE FROM product_description WHERE product_id = '" . (int)$product_id . "'");
        $this->udb->query("DELETE FROM product_image WHERE product_id = '" . (int)$product_id . "'");
        $this->udb->query("DELETE FROM product_to_marketplace WHERE product_id = '" . (int)$product_id . "'");
        $this->udb->query("DELETE FROM `product_variant_value`  WHERE  product_variant_id IN(select product_variant_id from product_variant where product_id= '" . (int)$product_id . "')");
        $this->udb->query("DELETE FROM product_variant WHERE product_id = '" . (int)$product_id . "'");
        $this->udb->query("DELETE FROM product_setting WHERE product_id = '" . (int)$product_id . "'");
        $this->udb->query("DELETE FROM product_error WHERE product_id = '" . (int)$product_id . "'");

        $this->cache->delete('product');
    }


    public function addVariant($product_variant)
    {


        $this->udb->query("INSERT INTO  product_variant SET product_id = '" . (int)$product_variant['product_id'] . "', image = '" . $this->udb->escape($product_variant['image']) . "',barcode='" . $this->db->escape($product_variant['barcode']) . "',stock_code='" . $this->db->escape($product_variant['stock_code']) . "',quantity='" . (int)$product_variant['quantity'] . "',sale_price='" . $product_variant['sale_price'] . "'");
        $product_variant_id = $this->udb->getLastId();
        $index = 0;
        foreach ($product_variant['option'] as $attribute_id => $option) {

            $this->udb->query("INSERT INTO product_variant_value SET product_variant_id='" . $product_variant_id . "', attribute_id='" . $attribute_id . "', attribute_value_id='" . $option . "',sort_order='" . $index . "'  ");
            $index++;

        }
        if (isset($product_variant['marketplace_data'])) {


            $query = $this->udb->query("select * from variant_to_marketplace where product_variant_id='" . $product_variant_id . "'");
            $code = $product_variant['code'];

            if ($query->num_rows) {

                $this->udb->query("update variant_to_marketplace SET  $code='" . $this->db->escape(json_encode($product_variant['marketplace_data'])) . "', date_modified=NOW() where product_variant_id='" . $product_variant_id . "' ");

            } else {

                $this->udb->query("INSERT INTO variant_to_marketplace SET product_variant_id='" . $product_variant_id . "', $code='" . $this->db->escape(json_encode($product_variant['marketplace_data'])) . "', date_added=NOW() ");

            }

        }

        return $product_variant_id;
    }


    public function productSettingDelete($code)
    {


        if ($code == "all") {

            $sql = "DELETE FROM product ";


        } else {
            $sql = "UPDATE product SET " . $code . "= ''";

        }

        $this->udb->query($sql);

    }


    public function editProduct($product_id, $data)
    {



        $varianter=isset($data['product_variant'])?1:0;

        $this->udb->query("UPDATE product SET model = '" . $this->udb->escape($data['model']) . "', barcode = '" . $this->udb->escape($data['barcode']) . "', varianter='".$varianter."', stock_code = '" . $this->db->escape($data['stock_code']) . "', quantity = '" . (int)$data['quantity'] . "', category_id = '" . (int)$data['category_id'] . "', category_name = '" . $this->udb->escape($data['category_name']) . "', manufacturer_id = '" . (int)$data['manufacturer_id'] . "',manufacturer_name = '" . $this->udb->escape($data['manufacturer_name']) . "',  sale_price = '" . (float)$data['sale_price'] . "', list_price = '" . (int)$data['list_price'] . "', desi = '" . (float)$data['desi'] . "',   status = '" . (int)$data['status'] . "', tax = " . (int)1 . ", date_modified = ' " . date("Y-m-d H:i:s") . "' WHERE product_id = '" . (int)$product_id . "'");

        if (isset($data['product_image'][0])) {
            $this->udb->query("UPDATE product SET image = '" . $this->udb->escape($data['product_image'][0]) . "' WHERE product_id = '" . (int)$product_id . "'");
            unset($data['product_image'][0]);

        }

        $this->udb->query("DELETE FROM product_description WHERE product_id = '" . (int)$product_id . "'");

        foreach ($data['product_description'] as $language_id => $value) {
            $this->udb->query("INSERT INTO product_description SET product_id = '" . (int)$product_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->udb->escape($value['name']) . "', description = '" . $this->udb->escape($value['description']) . "'");
        }


        $this->udb->query("DELETE FROM product_image WHERE product_id = '" . (int)$product_id . "'");

        if (isset($data['product_image'])) {
            foreach ($data['product_image'] as $index => $product_image) {
                if ($index != 0) {
                    $this->udb->query("INSERT INTO  product_image SET product_id = '" . (int)$product_id . "', image = '" . $this->udb->escape($product_image) . "', sort_order = '" . (int)$index . "'");

                }
            }
        }



        $query = $this->udb->query("select product_variant_id from product_variant where product_id= '" . (int)$product_id . "'");

       if($query->num_rows){
           $this->udb->query("DELETE FROM `product_variant_value`  WHERE  product_variant_id IN(select product_variant_id from product_variant where product_id= '" . (int)$product_id . "')");
       }
        //  $this->udb->query("DELETE FROM product_variant WHERE product_id = '" . (int)$product_id . "'");

        if (isset($data['product_variant'])) {


            $query = $this->udb->query("select * from product_variant where product_id='".$product_id."'");
            foreach ($query->rows as $row) {
                if(!$this->find_in_array($row,$data['product_variant'])){
                    $this->udb->query("DELETE FROM `product_variant`  WHERE  product_variant_id='".$row['product_variant_id']."'");

                }

               }

            foreach ($data['product_variant'] as $index => $product_variant) {




                if ($product_variant['stock_code']) {

                    if (isset($product_variant['product_variant_id'])) {
                        $product_variant_id = $product_variant['product_variant_id'];
                        $this->udb->query("UPDATE product_variant SET product_id = '" . (int)$product_id . "', image = '" . $this->udb->escape($product_variant['image']) . "',barcode='" . $this->udb->escape($product_variant['barcode']) . "',stock_code='" . $this->udb->escape($product_variant['stock_code']) . "',quantity='" . (int)$product_variant['quantity'] . "',sale_price='" . $product_variant['sale_price'] . "', sort_order = '" . (int)$index . "' where product_variant_id='" . $product_variant_id . "'");

                    } else {
                        $this->udb->query("INSERT INTO  product_variant SET product_id = '" . (int)$product_id . "', image = '" . $this->udb->escape($product_variant['image']) . "',barcode='" . $this->udb->escape($product_variant['barcode']) . "',stock_code='" . $this->udb->escape($product_variant['stock_code']) . "',quantity='" . (int)$product_variant['quantity'] . "',sale_price='" . $product_variant['sale_price'] . "', sort_order = '" . (int)$index . "'");
                        $product_variant_id = $this->udb->getLastId();
                    }


                    $index = 0;
                    foreach ($product_variant['option'] as $attribute_id => $option) {

                        $this->udb->query("INSERT INTO product_variant_value SET product_variant_id='" . $product_variant_id . "', attribute_id='" . $attribute_id . "', attribute_value_id=(SELECT `attribute_value_id` FROM multimarket.`oc_attribute_value`   WHERE `attribute_id` = '" . $attribute_id . "' AND `name` = '" . $option . "'),sort_order='" . $index . "'  ");

                        $index++;

                    }
                }
            }


        }else {

            $this->udb->query("DELETE FROM `product_variant`  WHERE  product_id='".$product_id."'");

        }


        //$this->cache->delete('product');
    }

    public function find_in_array($igne,$samanlik)
    {
        foreach ($samanlik as $item) {
            if($igne['stock_code']==$item['stock_code']){
                return true;
            }
        }

        return  false;

    }


    public function getProduct($product_id)
    {
        $query = $this->udb->query("SELECT DISTINCT * FROM product p LEFT JOIN product_description pd ON (p.product_id = pd.product_id) WHERE p.product_id = '" . (int)$product_id . "' AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "'");

        return $query->row;
    }






    public function updateProductOptions($data, $product_id)
    {

        $this->udb->query("DELETE FROM product_option WHERE product_id = '" . (int)$product_id . "'");
        $this->udb->query("DELETE FROM product_option_value WHERE product_id = '" . (int)$product_id . "'");

        if (isset($data['product_option'])) {
            foreach ($data['product_option'] as $product_option) {
                if ($product_option['type'] == 'select' || $product_option['type'] == 'radio' || $product_option['type'] == 'checkbox' || $product_option['type'] == 'image') {
                    if (isset($product_option['product_option_value'])) {
                        $this->udb->query("INSERT INTO product_option SET product_id = '" . (int)$product_id . "', option_id = '" . (int)$product_option['option_id'] . "', required = '" . (int)$product_option['required'] . "'");

                        $product_option_id = $this->udb->getLastId();

                        foreach ($product_option['product_option_value'] as $product_option_value) {
                            $this->udb->query("INSERT INTO product_option_value SET product_option_id = '" . (int)$product_option_id . "', product_id = '" . (int)$product_id . "', option_id = '" . (int)$product_option['option_id'] . "', option_value_id = '" . (int)$product_option_value['option_value_id'] . "', quantity = '" . (int)$product_option_value['quantity'] . "', subtract = '" . (int)$product_option_value['subtract'] . "', price = '" . (float)$product_option_value['price'] . "'");
                        }
                    }
                } else {
                    $this->udb->query("INSERT INTO product_option SET product_id = '" . (int)$product_id . "', option_id = '" . (int)$product_option['option_id'] . "', value = '" . $this->udb->escape($product_option['value']) . "', required = '" . (int)$product_option['required'] . "'");
                }
            }
        }


    }





    public function getProductErrors($data)
    {
        $sql = "select p.product_id, pd.name,p.model,pe.code,pe.type,pe.error,pe.date_modified from product_error pe LEFT JOIN product p ON(p.product_id=pe.product_id) LEFT JOIN product_description pd ON(p.product_id=pd.product_id) WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "'";


        if (!empty($data['filter_marketplace'])) {
            if ($data['filter_marketplace'] != '*') {
                $sql .= " AND pe.code='" . $data['filter_marketplace'] . "'";
            }
        }

        if (!empty($data['filter_model'])) {
            $sql .= " AND p.model LIKE '" . $this->udb->escape($data['filter_model']) . "%'";
        }

        if (!empty($data['filter_name'])) {
            $sql .= " AND pd.name LIKE '%" . $this->udb->escape($data['filter_name']) . "%'";
        }


        $sql .= " ORDER BY pe.date_added DESC";

        if (isset($data['start']) || isset($data['limit'])) {
            if ($data['start'] < 0) {
                $data['start'] = 0;
            }

            if ($data['limit'] < 1) {
                $data['limit'] = 20;
            }
        }

        //  $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];

        try {
            $query = $this->udb->query($sql);
            return $query->rows;
        } catch (Exception $exception) {

            echo $exception->getMessage();
        }


    }

    public function getTotalProductErrors($data)
    {
        $sql = "select COUNT(*) as total  from product_error pe LEFT JOIN product p ON(p.product_id=pe.product_id) LEFT JOIN product_description pd ON(p.product_id=pd.product_id) WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "'";


        if (!empty($data['filter_marketplace'])) {

            if ($data['filter_marketplace'] != '*') {
                $sql .= " AND pe.code='" . $data['filter_marketplace'] . "'";
            }
        }

        if (!empty($data['filter_model'])) {
            $sql .= " AND p.model LIKE '" . $this->udb->escape($data['filter_model']) . "%'";
        }

        if (!empty($data['filter_name'])) {
            $sql .= " AND pd.name LIKE '%" . $this->udb->escape($data['filter_name']) . "%'";
        }

        try {
            $query = $this->udb->query($sql);
            return $query->row['total'];

        } catch (Exception $exception) {

            echo $exception->getMessage();
        }


    }







    public function getProducts($data = array(), $for_bulk = null)
    {


        $sql = "SELECT p.product_id,p.varianter,p.manufacturer_id, p.manufacturer_name,p.category_id,p.category_name, p.model,p.manufacturer_id,p.sale_price, p.list_price, p.image,p2m.n11,p2m.gg,p2m.cs,p2m.ty,p2m.amz,p2m.hb,p2m.eptt,p.status,pd.name,p.quantity,p.date_added";


        if (method_exists($this->currency, 'getCodeOrDefault')) {
            $sql .= ", p.currency_id ";
        }

        $sql .= " FROM product p LEFT JOIN product_description pd ON (p.product_id = pd.product_id) LEFT JOIN product_to_marketplace p2m ON(p2m.product_id=p.product_id)   WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "'";


        if (!empty($data['filter_stock_prefix'])) {


            if ($data['filter_stock_prefix'] != '*') {
                if ($data['filter_stock'] !== false) {

                    $sql .= " AND p.quantity " . $data['filter_stock_prefix'] . " " . $data['filter_stock'] . " ";
                }
            }
        }


        if (isset($data['filter_status']) && $data['filter_status'] !== '*') {
            $sql .= " AND p.status = '" . (int)$data['filter_status'] . "'";
        }

        if (!empty($data['filter_name'])) {
            $sql .= " AND pd.name LIKE '%" . $this->udb->escape($data['filter_name']) . "%'";
        }


        if (!empty($data['filter_except'])) {

            $sql .= " AND (p2m." . $data['filter_except'] . "='' or  p2m." . $data['filter_except'] . " is null) ";
        }


        if (!empty($data['filter_model'])) {
            $sql .= " AND p.model LIKE '" . $this->udb->escape($data['filter_model']) . "%'";


        }

        if (!empty($data['filter_manufacturer_id'])) {
            $sql .= " AND p.manufacturer_id = '" . (int)$data['filter_manufacturer_id'] . "'";
        }

        if (!empty($data['filter_price'])) {
            $sql .= " AND p.sale_price LIKE '" . $this->udb->escape($data['filter_price']) . "%'";
        }

        if (isset($data['filter_quantity']) && $data['filter_quantity'] !== '') {
            $sql .= " AND p.quantity = '" . (int)$data['filter_quantity'] . "'";
        }


        if (!empty($data['filter_category'])) {
            $sql .= " AND p.category_id='" . $data['filter_category'] . "' ";
        }
        if (!empty($data['filter_manufacturer'])) {
            $sql .= " AND p.manufacturer_id='" . $data['filter_manufacturer'] . "' ";
        }


        if (isset($data['filter_product'])) {
            $sql .= " AND p.product_id = '" . (int)$data['filter_product'] . "'";
        }

        if (!empty($data['filter_marketplace'])) {

            if ($data['filter_marketplace'] != '*') {

                if (isset($data['filter_marketplace_do'])) {

                    if ($data['filter_marketplace_do'] == 1) {
                        if ($data['filter_marketplace'] == 'n11') {
                            $status = '"sale_status":true';
                            $alternatif_status = '"sale_status":true';

                        } else {
                            $status = '"sale_status":true';
                            $alternatif_status = '"sale_status":true';

                        }
                        $sql .= " AND (p2m." . $data['filter_marketplace'] . " LIKE '%" . $status . "%'  or  p2m." . $data['filter_marketplace'] . " LIKE '%" . $alternatif_status . "%'  )  ";


                    } elseif ($data['filter_marketplace_do'] == 2) {
                        $status = 'sale_status";i:0';
                        $alternatif_status = '"sale_status":false';

                        $sql .= " AND ( p2m." . $data['filter_marketplace'] . " LIKE '%" . $status . "%'  or  p2m." . $data['filter_marketplace'] . " LIKE '%" . $alternatif_status . "%')    ";


                    } else {

                        $sql .= " AND (p2m." . $data['filter_marketplace'] . "='' or p2m." . $data['filter_marketplace'] . " is null )";

                    }

                } else {
                    $sql .= " AND (p2m." . $data['filter_marketplace'] . "='' or p2m." . $data['filter_marketplace'] . " is null )";

                }
            }
        }


        $sql .= " GROUP BY p.product_id";

        $sort_data = array(
            'pd.name',
            'p.model',
            'p.price',
            'p.quantity',
            'p.status',
            'p.date_added',
            'p.sort_order'
        );

        if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
            $sql .= " ORDER BY " . $data['sort'];
        } else {
            $sql .= " ORDER BY pd.name";
        }

        if (isset($data['order']) && ($data['order'] == 'DESC')) {
            $sql .= " DESC";
        } else {
            $sql .= " ASC";
        }


        if ($for_bulk == "for_bulk") {

        } else {

            if (isset($data['start']) || isset($data['limit'])) {
                if ($data['start'] < 0) {
                    $data['start'] = 0;
                }

                if ($data['limit'] < 1) {
                    $data['limit'] = 20;
                }

                $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
            }

        }





        try {
            $query = $this->udb->query($sql);
            return $query->rows;

        } catch (Exception $exception) {

            echo $exception->getMessage();
        }

    }


    public function getProductImages($product_id)
    {
        $query = $this->udb->query("SELECT * FROM product_image WHERE product_id = '" . (int)$product_id . "' ORDER BY sort_order ASC");

        return $query->rows;
    }


    public function getTotalProducts($data = array())
    {
        $sql1 = "SELECT COUNT(DISTINCT p.product_id) AS total FROM product p   ";

        $sql2 = " WHERE p.product_id is not NULL ";

        if (!empty($data['filter_name'])) {

            $sql1 .= "LEFT JOIN product_description pd ON (p.product_id = pd.product_id) ";
            $sql2 .= " AND pd.name LIKE '%" . $this->udb->escape($data['filter_name']) . "%'";
        }
        if (!empty($data['filter_in_notname'])) {
            $sql1 .= "LEFT JOIN product_description pd ON (p.product_id = pd.product_id) ";

            $sql2 .= " and pd.name not like ( '%" . $data['filter_in_notname'] . "%') ";

        }

        if (!empty($data['filter_marketplace'])) {


            if ($data['filter_marketplace'] != '*') {
                $sql1 .= " LEFT JOIN product_to_marketplace p2m ON(p2m.product_id=p.product_id) ";

                if (isset($data['filter_marketplace_do'])) {

                    if ($data['filter_marketplace_do'] == 1) {
                        if ($data['filter_marketplace'] == 'n11') {
                            $status = '"sale_status";i:2';
                            $alternatif_status = '"sale_status";b:2';

                        } else {
                            $status = '"sale_status";i:1';
                            $alternatif_status = '"sale_status";b:1';

                        }
                        $sql2 .= " AND ( p2m." . $data['filter_marketplace'] . " LIKE '%" . $status . "%'  or  p2m." . $data['filter_marketplace'] . " LIKE '%" . $alternatif_status . "%' )   ";


                    } elseif ($data['filter_marketplace_do'] == 2) {
                        $status = 'sale_status";i:0';
                        $alternatif_status = 'sale_status";b:0';

                        $sql2 .= " AND p2m." . $data['filter_marketplace'] . " LIKE '%" . $status . "%'  or  p2m." . $data['filter_marketplace'] . " LIKE '%" . $alternatif_status . "%'    ";


                    } else {

                        $sql2 .= " AND p2m." . $data['filter_marketplace'] . "=''";

                    }

                } else {
                    $sql2 .= " AND p2m." . $data['filter_marketplace'] . "!=''";

                }
            }
        }


        if (isset($data['filter_status']) && $data['filter_status'] !== '*') {
            $sql2 .= " AND p.status = '" . (int)$data['filter_status'] . "'";
        }

        if (!empty($data['filter_stock_prefix'])) {

            if ($data['filter_stock']) {


                if ($data['filter_stock_prefix'] != '*') {

                    $sql2 .= " AND p.quantity " . $data['filter_stock_prefix'] . " " . $data['filter_stock'] . " ";

                }
            }

        }

        if (!empty($data['filter_model'])) {
            $sql2 .= " AND p.model LIKE '" . $this->udb->escape($data['filter_model']) . "%'";
        }

        if (!empty($data['filter_category'])) {

            $sql2 .= " AND p.category_id='" . $data['filter_category'] . "' ";
        }
        if (!empty($data['filter_manufacturer'])) {
            $sql2 .= " AND p.manufacturer_id='" . $data['filter_manufacturer'] . "' ";
        }

        if (isset($data['filter_price']) && !is_null($data['filter_price'])) {
            $sql2 .= " AND p.sale_price LIKE '" . $this->udb->escape($data['filter_price']) . "%'";
        }

        if (isset($data['filter_quantity']) && $data['filter_quantity'] !== '') {
            $sql2 .= " AND p.quantity = '" . (int)$data['filter_quantity'] . "'";
        }

        if (isset($data['filter_status']) && $data['filter_status'] !== '*') {
            $sql2 .= " AND p.status = '" . (int)$data['filter_status'] . "'";
        }

        $sql = $sql1 . $sql2;
        //  echo $sql;
        //  return ;
        try {
            $query = $this->udb->query($sql);

            return $query->row['total'];
        } catch (Exception $exception) {
            echo $exception->getMessage();

        }

    }








    public function addProductVariants($datas, $product_id)
    {


        foreach ($datas as $data) {


            try {
                $this->udb->query("INSERT INTO product_variant SET product_id='" . $product_id . "', barcode='" . $this->udb->escape($data['barcode']) . "',model='" . $this->udb->escape($data['barcode']) . "', `name`='" . $this->udb->escape($data['name']) . "',`variant_info`='" . $this->udb->escape($data['variant_info']) . "',`variant_count`='" . $data['variant_count'] . "',image='" . $this->udb->escape($data['image']) . "',quantity='" . $data['quantity'] . "',price='" . $data['price'] . "' ");

            } catch (Exception $exception) {

                echo $exception->getMessage();
            }
        }
    }


}
