<?php
class ModelEntegrasyonSync extends Model {



    public function addMarketProduct($marketplace_data, $code)
    {


       // print_r($marketplace_data);


        $downloaded = 0;



            $brandId=isset($marketplace_data['custom_data']['brandId'])?$marketplace_data['custom_data']['brandId']:0;
            $this->db->query("INSERT INTO market_product SET  code='" . $code . "',  `name`='" . $this->db->escape($marketplace_data['name']) . "',  `brand`='" . $brandId . "', model='" . $this->db->escape($marketplace_data['model']) . "', barcode='" . $this->db->escape($marketplace_data['barcode']) . "', stock_code='" . $this->db->escape($marketplace_data['stock_code']) . "', sale_price='" . $marketplace_data['sale_price'] . "', list_price='" . $marketplace_data['list_price'] . "', quantity='" . $marketplace_data['quantity'] . "', sale_status='" . $marketplace_data['sale_status'] . "', approval_status='" . $marketplace_data['approval_status'] . "', custom_data='".$this->db->escape(serialize($marketplace_data['custom_data']))."', marketplace_product_id='" . $marketplace_data['market_id'] . "', date_added=NOW() ");

            $org_barcode=str_replace(array(strtoupper($this->config->get('hb_setting_model_prefix')),$this->config->get('ty_setting_model_prefix'),$this->config->get('cs_setting_model_prefix'),'ULU5','C2','YENI-SERI-2023','CM43','YENI-SERI-2024',"MR05"),'',$marketplace_data['barcode']);

            // echo "select count(*) as total from product_url where user_id='".$this->user->getId()."' and (barcode like '" .  $this->db->escape($org_barcode.$this->config->get('hb_setting_model_prefix')) . "' or barcode like '" .  $this->db->escape($org_barcode.$this->config->get('cs_setting_model_prefix')) . "' or barcode like '" .  $this->db->escape($org_barcode.$this->config->get('ty_setting_model_prefix')) . "') ";

            $query1= $this->db->query("select count(*) as total,id from product_url where  barcode like '" .  $this->db->escape($marketplace_data['barcode']) . "' ");


            if($query1->row['total']==1){
                $this->db->query("update product_url SET $code=1 where id='".$query1->row['id']."' ");
            }else {
                $query2= $this->db->query("select count(*) as total,id from product_url where  barcode like '" .  $this->db->escape($org_barcode) . "%' ");
                if($query2->row['total']==1){
                    $this->db->query("update product_url SET $code=1 where id='".$query2->row['id']."' ");
                }
            }


            $downloaded = 1;




        return array('downloaded' => $downloaded);
        // $this->deleteError($product_id, $code);

    }




    private function addAndUpdateMarketProduct($code, $product)
    {


        if (isset($product['link'])) {
            $url = $this->getMarketPlaceUrl($code, $product['market_id'], $product['link']);


        } else {

            $url = $this->getMarketPlaceUrl($code, $product['market_id'], "yok");

        }


        $data = array('commission' => 0, 'sale_status' => $product['sale_status'], 'approval_status' => $product['approval_status'], 'barcode' => $product['barcode'], 'product_id' => $product['market_id'], 'sale_price' => number_format($product['sale_price'], 2), 'url' => $url);


        if (isset($product['product_status'])) {
            $data['product_status'] = $product['product_status'];
        }

        if (isset($product['message'])) {
            $data['message'] = $product['message'];
        }

        if (isset($product['request_id'])) {
            $data['request_id'] = $product['request_id'];
        }


        if ($code == 'n11') {

            $data['stock_id'] = $product['stock_id'];
        }


        $this->addMarketplaceProduct($product['product_id'], $data, $code);


    }

    public function addMarketplaceProduct($product_id, $marketplace_data, $code)
    {

        /* $query = $this->db->query("select product_id from product_to_marketplace where product_id='" . $product_id . "'");


         if ($query->num_rows) {

             $this->db->query("update product_to_marketplace SET  $code='" . $this->db->escape(json_encode($marketplace_data)) . "', date_modified=NOW() where product_id='" . $product_id . "' ");

         } else {



             $this->db->query("INSERT INTO product_to_marketplace SET product_id='" . $product_id . "', $code='" . $this->db->escape(json_encode($marketplace_data)) . "', date_added=NOW() ");

         }

         $this->load->model("entegrasyon/error");
         $this->model_entegrasyon_error->deleteError($product_id, $code);
 */

    }

    public function addMarketplaceVariant($product_variant_id, $marketplace_data, $code)
    {

        $query = $this->db->query("select * from variant_to_marketplace where product_variant_id='" . $product_variant_id . "'");

        if ($query->num_rows) {

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

        } else {

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

        }

        //$this->load->model("entegrasyon/error");
        //$this->model_entegrasyon_error->deleteError($product_variant_id, $code);

    }

    public function getMarketPlaceUrl($code, $market_id, $cs_link = false)
    {


        if ($code == 'gg') {

            return 'http://urun.gittigidiyor.com/' . $market_id;

        } else if ($code == 'n11') {

            return 'http://urun.n11.com/xxx-P' . $market_id;

        } else if ($code == 'ty') {

            return 'https://www.trendyol.com/p/p-p-p-' . $market_id;
        } else if ($code == 'hb') {

            return 'https://www.hepsiburada.com/product-p-' . $market_id;

        } else if ($code == 'eptt') {

            return 'https://www.epttavm.com/item/' . $market_id . '_p';

        } else if ($code == 'cs') {

            return $cs_link;

        } else {

            return false;
        }


    }




}