<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
#[\AllowDynamicProperties]
class Stock_update extends CI_Controller {
    
    public function __construct() {
        parent::__construct();
         if (! $this->session->userdata ( 'user_logged_in' ) || !company_url($this->session->userdata ('company_slag'))) {
            redirect ( 'login', 'refresh' );
        }
        $this->load->vars ( array (
                'global' => 'Available to all views',
                'header' => 'common/header',
                'left' => 'common/left',
                'footer' => 'common/footer' 
        ) ); 
        $this->load->model('Color_model');
        $this->load->model('Product_model');
        $this->load->library('Fileupload');
    } 
    
    public function index(){
        $data = array();
          //$data['import_list'] = $this->Color_model->getAllImportData();			
          //$data['vendor_list'] = $this->Product_model->get_vendor();
//       echo "<pre>";
//        print_r($data);
//        exit();		
		
        $this->load->view('stock_update',$data);
    }
    
   
    public function updateStatus(){
        $data=$this->input->post(); 
        $name = explode(".", $_FILES['file']['name']);

        if($name[1]=='csv'){
             //print_r($_FILES["file"]);exit;
         //if($_FILES["file"]["size"] > 0)
             if(true)
             {
                $filename=$_FILES["file"]["tmp_name"];
                $file = fopen($filename, "r");
                  $n = 0;
                  $vendor_sku_1 = '';
                  $vendor_sku_2 = '';
                  $arr_vender_sku = array();
                  while (($getData = fgetcsv($file)) !== FALSE){
                        if($n == 0){
                            $n++;
                            continue;
                        }

                        $vendor_sku = $getData[2];
                        $sizeSku = $getData[0];
                        $stock = $getData[12];

                        if($vendor_sku == "" || $stock == "") continue;

                        if($vendor_sku_1 !=  $vendor_sku){
                            $vendor_sku_1 = $vendor_sku;
                            $product = $this->db->select('id, photo_id, vendor_sku, variations')
                            ->from('rugs_tmp')
                            //->where('vendor_sku', $vendor_sku_1)
                            ->where('vendor', 'Surya')
                            ->like('vendor_sku', $vendor_sku_1, 'after')
                            ->get()
                            ->row();
                            
                        }

                        if($product){
                            if($vendor_sku_2 !=  $vendor_sku){//Check previous sku
                                 
                                $is_change = false;
                                $is_active = false;
                                /*Main function */
                                if(count($arr_vender_sku) > 0){
                                    foreach($arr_variation as $key=>$variation){
                                        if(isset($variation->sizeSku) && array_key_exists($variation->sizeSku, $arr_vender_sku)){
                                            if($arr_vender_sku[$variation->sizeSku] > 0){
                                                // echo $variation->sizeSku."--".$arr_vender_sku[$variation->sizeSku]."--".$key."<br>";
                                                $arr_variation[$key]->stock = (int)$arr_vender_sku[$variation->sizeSku];
                                                $arr_variation[$key]->active = 1;   
                                                $is_active =  true;                  
                                            }else{
                                               // echo $variation->sizeSku."--".$arr_vender_sku[$variation->sizeSku]."--<br>";
                                                $arr_variation[$key]->active = 0;
                                            }
                                        $is_change = true;
                                        }else{
                                            $arr_variation[$key]->stock = 0;
                                            $arr_variation[$key]->active = 0;
                                        }
                                    }

                                    if($is_change){
                                        if($is_active)
                                        {
                                            $new_variation = json_encode($arr_variation);
                                            // echo $new_variation;
                                            $data = array(
                                                    'variations' => $new_variation,
                                                    'active'=> '1'
                                            );
                                            //$this->db->where('vendor_sku', $vendor_sku_2);
                                            $this->db->where('vendor', 'Surya');
                                            $this->db->like('vendor_sku', $vendor_sku_2, 'after');
                                            $this->db->update('rugs_tmp', $data);
                                            // echo $vendor_sku_2."<br>";   
                                        }else{
                                            $new_variation = json_encode($arr_variation);
                                            $data = array(
                                                    'variations' => $new_variation,
                                                    'active'=> '0'
                                            );
                                            //$this->db->where('vendor_sku', $vendor_sku_2);
                                            $this->db->where('vendor', 'Surya');
                                            $this->db->like('vendor_sku', $vendor_sku_2, 'after');
                                            $this->db->update('rugs_tmp', $data);
                                            // echo $vendor_sku_2."<br>";   
                                        }     
                                    }else{
                                        $data = array(
                                            'active'=> '0'
                                        );
                                        //$this->db->where('vendor_sku', $vendor_sku_2);
                                        $this->db->where('vendor', 'Surya');
                                        $this->db->like('vendor_sku', $vendor_sku_2, 'after');
                                        $this->db->update('rugs_tmp', $data);
                                    }
                                }
                                $arr_variation = json_decode($product->variations);

                                $arr_vender_sku = array();
                                $arr_vender_sku[$sizeSku] = $stock;
                                $vendor_sku_2 = $vendor_sku;
                            }else{
                                $arr_vender_sku[$sizeSku] = $stock;
                            } 

                            
                            /*End main function*/
                        }else{
                            continue;
                        }

                        $result = 1;
                        if(!isset($result))
                        {
                          echo "<script type=\"text/javascript\">
                              alert(\"Invalid File:Please Upload CSV File.\");
                              window.location = \"index.php\"
                              </script>";    
                        }
                        $n++;
                   }

                   /* main function for last sku*/

                   if(count($arr_vender_sku) > 0){
                        foreach($arr_variation as $key=>$variation){
                            if(isset($variation->sizeSku) && array_key_exists($variation->sizeSku, $arr_vender_sku)){
                                if($arr_vender_sku[$variation->sizeSku] > 0){
                                    // echo $variation->sizeSku."--".$arr_vender_sku[$variation->sizeSku]."--".$key."<br>";
                                    $arr_variation[$key]->stock = (int)$arr_vender_sku[$variation->sizeSku];
                                    $arr_variation[$key]->active = 1;                      
                                }else{
                                    // echo $variation->sizeSku."--".$arr_vender_sku[$variation->sizeSku]."--<br>";
                                    $arr_variation[$key]->active = 0;
                                }
                            $is_change = true;
                            }else{
                                $arr_variation[$key]->stock = 0;
                                $arr_variation[$key]->active = 0;
                            }
                        }
                        
                        if($is_change){
                            $new_variation = json_encode($arr_variation);
                            //print_r($arr_variation);
                            $data = array(
                                    'variations' => $new_variation
                            );

                            // $this->db->where('vendor_sku', $vendor_sku);
                            $this->db->where('vendor', 'Surya');
                            $this->db->like('vendor_sku', $vendor_sku, 'after');
                            $this->db->update('rugs_tmp', $data);   
                            echo $vendor_sku."|*|".$sizeSku."|*|".$stock."<br>";         
                        }else{
                            $data = array(
                                'active'=> '0'
                            );
                            //$this->db->where('vendor_sku', $vendor_sku_2);
                            $this->db->where('vendor', 'Surya');
                            $this->db->like('vendor_sku', $vendor_sku, 'after');
                            $this->db->update('rugs_tmp', $data);
                        }
                    }
                    
                    /*End function */

                   $main_url = site_url('stock_update');
                    echo "CSV File has been successfully Imported. <a href='".$main_url."'>Back</a>";
                   fclose($file);  
             }
        }else{           
            redirect(base_url().'Stock_update');
        }
    }
    
}