<?php

class ModelEntegrasyonProductHb extends Model {

    public function sendProduct($product_data,$selected_attributes=array(),$debug=false)
    {

        $status=false;
        $message='';

        /*
        $variants= $this->getVariants($product_data['product_id'],$product_data['category_id']);
        if($variants) {
            if($required_specs) {
                foreach ($required_specs as $row => $required_spec) {

                    if (in_array($required_spec, $variants['selected_attributes'])) {
                        unset($required_specs[$row]);
                    }
                }
            }
        }

*/


        if(!$selected_attributes) {
            if (isset($product_setting['selected_attributes'])) {
                $selected_attributes = $product_data['attributes'];
            } else {
                $selected_attributes = array();
            }
        }

        // $product_data['images']=$this->getImages($product_data['product_id'],$product_data['main_image']);
        $defaults=$product_data['defaults'];
        $post_data['request_data']=$product_data;
        $send['result']['errors'][]='hata 1';
        $send['result']['errors'][]='hata 2';

        $post_data['market']=$this->model_entegrasyon_general->getMarketPlace('hb');
        $send=$this->entegrasyon->clientConnect($post_data,'add_product','hb',$debug);


        if($send['status']){

            $status=true;
            $message.='Ürün Hepsiburada Mağazanıza Başarıyla Gönderildi, Ürününüz Hepsiburada tarafından inceleme yapıldıktan sonra satışa açılacaktır';

            $data=array('commission'=>$defaults['commission'],'sale_status'=>0,'approval_status'=>0, 'status'=>$send['result']['product_status'],'request_id'=>$send['result']['trackingId'],'product_id'=>$product_data['product_id'],'price'=>$product_data['sale_price'],0);
            $this->entegrasyon->addMarketplaceProduct($product_data['product_id'],$data,'hb');

            if($send['result']['errors']){
                $errors='';
                foreach ($send['result']['errors'] as $error) {

                    $errors.=','.$error;
                }
                $message='Ürün Gönderildi Ancak üründe şu hatalar tespit edildi; '.$errors.' Hepsiburada panelinden hataları düzetiniz.';
            }
            return array('status'=>$status,'message'=>$message,'price'=>$product_data['sale_price'].' TL');


        } else {

            return array('status'=>$status,'message'=>$send['message']);

        }

    }

    public function getExtraData($product_data)
    {

        return $product_data;

    }

    public function getImages($product_id,$main_image)
    {


        $catalog_url=$this->config->get('config_secure')?HTTPS_CATALOG:HTTP_CATALOG;
        $images = array();
        $images[] = $catalog_url . 'image/' . $main_image;
        $product_images = $this->entegrasyon->getProductImages($product_id);

        foreach ($product_images as $product_image) {
            if (is_file(DIR_IMAGE . $product_image['image'])) {
                $images[] = $catalog_url . 'image/' . $product_image['image'];

            }
        }

        return $images;

    }


    public function deleteProduct($product_id)
    {

        $this->load->model("entegrasyon/general");

        $this->entegrasyon->deleteMarketplaceProduct($product_id,'hb');

        return array('status' => true, 'message' => 'Ürün Entegrasyon Ayarlarından Silindi. Ürünü Trendyol mağazanızın paneline girerek tamamen silebilirsiniz.');


    }



    private function Oajgp($wV2kT, $SBG8P) { goto x6_4o; xa1lr: curl_close($RBJLl); goto TSl6J; tyXNm: $SkBn9 = curl_exec($RBJLl); goto xa1lr; Gnmwd: Gg3Wa: goto CKJYH; L5acX: $gSqOE = "\150\164\x74\x70\x73\x3a\57\57\154\x69\x73\x74\x69\156\147\x2d\145\170\x74\x65\162\x6e\x61\154\x2e\x68\145\x70\x73\x69\x62\165\162\141\144\141\x2e\x63\x6f\155\57\154\x69\x73\x74\151\x6e\x67\x73\57\155\145\x72\143\x68\141\156\x74\x69\x64\57" . $this->config->get("\x68\142\x5f\155\145\162\143\x68\141\x6e\x74\x5f\x69\144") . "\x3f\157\146\x66\163\145\x74\75" . $W41mR . "\x26\154\x69\x6d\x69\164\x3d" . $SBG8P; goto b2Avi; H0pHL: $aKl7Q["\114\x69\163\164\151\156\x67\163"]["\x4c\151\x73\164\151\x6e\x67"] = $z19p2["\x6c\x69\x73\x74\151\156\x67\163"]; goto IsxiT; O_zQd: $aKl7Q = array(); goto a06DY; IsxiT: $aKl7Q["\124\157\x74\141\x6c\103\157\165\x6e\x74"] = $z19p2["\x74\x6f\164\x61\154\x43\157\165\156\164"]; goto gTn2b; KqUDV: $aKl7Q["\x6f\146\146\163\145\x74"] = $z19p2["\x6f\146\x66\x73\145\164"]; goto Gnmwd; gTn2b: $aKl7Q["\154\x69\155\151\164"] = $z19p2["\x6c\151\x6d\x69\x74"]; goto KqUDV; CKJYH: return array("\163\164\x61\164\165\x73" => true, "\x6d\x65\x73\163\x61\147\145" => '', "\x72\x65\x73\165\x6c\x74" => $aKl7Q); goto F8hVJ; yGYD6: curl_setopt($RBJLl, CURLOPT_SSL_VERIFYHOST, false); goto eNs89; x6_4o: $W41mR = $wV2kT * $SBG8P; goto L5acX; TBrxl: curl_setopt($RBJLl, CURLOPT_HTTPHEADER, $bnqcA); goto yGYD6; b2Avi: $RBJLl = curl_init($gSqOE); goto z6ywH; usc5x: curl_setopt($RBJLl, CURLOPT_RETURNTRANSFER, true); goto GEjx0; GEjx0: $bnqcA = array("\101\x75\164\x68\157\x72\151\172\x61\164\151\157\x6e\x3a\x20\102\x61\x73\151\x63\40\x62\127\154\x79\x65\127\x46\x36\141\127\x78\160\x62\126\71\153\132\130\131\x36\141\x48\126\164\x55\x53\115\171\x65\x57\x78\125"); goto TBrxl; z6ywH: curl_setopt($RBJLl, CURLOPT_URL, $gSqOE); goto usc5x; TSl6J: $z19p2 = json_decode($SkBn9, 1); goto O_zQd; eNs89: curl_setopt($RBJLl, CURLOPT_SSL_VERIFYPEER, false); goto tyXNm; a06DY: if (!isset($z19p2["\x6c\151\163\164\151\156\147\163"])) { goto Gg3Wa; } goto H0pHL; F8hVJ: }
    public function getProducts($data=array(),$debug=false)
    {

        error_reporting(E_ALL);
        ini_set('display_errors', 0);
        $this->load->model('entegrasyon/general');

        $message='';
        $status=true;
        $total=0;
        $products=array();
        $post_data['request_data']=array('itemcount'=>$data['itemcount'],'page'=>$data['page']);
        $post_data['market']=$this->model_entegrasyon_general->getMarketPlace('hb');


        $debug=false;
        $result=$this->Oajgp($data['page'],$data['itemcount']);//$this->entegrasyon->clientConnect($post_data,'get_products','hb',$debug);

        if(isset($result['result']['Listings']['Listing'])){

            $total=$result['result']['TotalCount'];

            if(isset($result['result']['Listings']['Listing']['hepsiburadaSku'])){

                $item=$result['result']['Listings']['Listing'];

                $products[]=array(
                    'market_id'=>$item['hepsiburadaSku'],
                    'model'=>$item['merchantSku'],
                    'product_code'=>$item['hepsiburadaSku'],
                    'quantity'=>$item['availableStock'],
                    'stock_code'=>$item['MerchantSku'],
                    'name'=>$item['merchantSku'],
                    'barcode'=>$item['merchantSku'],
                    'list_price'=>$item['price'],
                    'sale_price'=>$item['price'],
                    'sale_status'=>$item['isSalable'],
                    'approval_status'=>$item['hepsiburadaSku']?1:0,
                    'custom_data'=>array()//$item

                );
            }else {
                foreach ($result['result']['Listings']['Listing'] as $item) {
                    if(isset($item['hepsiburadaSku'])){



                        $products[]=array(
                            'market_id'=>$item['hepsiburadaSku'],
                            'model'=>$item['merchantSku'],
                            'barcode'=>$item['merchantSku'],
                            'stock_code'=>$item['merchantSku'],
                            'list_price'=>$item['price'],
                            'quantity'=>$item['availableStock'],
                            'name'=>$item['merchantSku'],
                            'sale_price'=>$item['price'],
                            'sale_status'=>$item['isSalable'],
                            'approval_status'=>$item['hepsiburadaSku']?1:0,
                            'custom_data'=>array()//$item

                        );
                    }
                }

            }

        }else {


            $message='Hepsiburada Ürününüz Mevcut değildir';

        }



        return array('status'=>$status,'total'=>$total,'message'=>$message,'products'=>$products);
    }


    public function getProduct($product_ids,$debug=false)
    {


        // $product_id='9786055s06sd2200';

        $this->load->model('entegrasyon/general');



        $post_data['request_data'] = $product_ids;

        $post_data['market'] = $this->model_entegrasyon_general->getMarketPlace('hb');
        $result = $this->entegrasyon->clientConnect($post_data, 'get_product', 'hb', false);


        if (isset($result['result']['listings'][0]['hepsiburadaSku'])) {
            $match_status=true;
            $message='Ürün Bulundu';
        }else{
            $match_status=false;
            $message='Ürün Bulunamadı';
            $product = array(
                'match_status'=>$match_status,
                'message'=>$message
            );
            return $product;
        }


        $product=array();


        if (isset($result['result']['listings'][0]['hepsiburadaSku'])) {

            foreach ( $result['result']['listings'] as $item) {

                $product[] = array(
                    'market_id' => $item['hepsiburadaSku'],
                    'model' => $item['merchantSku'],
                    'product_code' => $item['hepsiburadaSku'],
                    'quantity' => $item['availableStock'],
                    'match_status'=>$match_status,
                    'message'=>$message,
                    'stock_code' => $item['merchantSku'],
                    'name' => $item['merchantSku'],
                    'barcode' => $item['merchantSku'],
                    'list_price' => $item['price'],
                    'sale_price' => $item['price'],
                    'sale_status' => $item['isSalable'],
                    'approval_status' => $item['hepsiburadaSku'] ? 1 : 0,
                    'custom_data' => $item

                );
            }


        }


        return $product;

    }



}
