<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Uploadproduct 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('Bank_model');
        $this->load->model('Invoice_model');
        $this->load->model('Product_model');
    }

    /*public function index(){
        $data['bank_list'] = $this->Bank_model->get_bank_list();		
		$bank_deposit_list = $this->Bank_model->get_bank_deposit_details();
        $data['bank_deposit_list'] = array();
        if (count($bank_deposit_list) > 0) {
            foreach ($bank_deposit_list as $key => $value) {
                $data['bank_deposit_list'][$value->account_id]['debit'] = $value->debit;
                $data['bank_deposit_list'][$value->account_id]['credit'] = $value->credit;
            }
        }		
        $this->load->view('bank_list',$data);		
    }*/
    
    
    
    public function index(){
        $data['vendor_list'] = $this->Product_model->get_vendor();		
		
       /* echo "<pre>";
        print_r($data);
        exit(); */		
		
        $this->load->view('upload_product',$data);		
    }
    

    public function newbank(){
        $data['bank_id'] = !empty($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        if ($data['bank_id'] > 0) {
            $data['bank_data'] = $this->Bank_model->get_bank_details($data['bank_id']);
        }
        $this->load->view('new_bank',$data);
    }

    public function importnew(){      

        
                $vendor_name = "Surya";
                $vendor_id = 55;
                $vendor_prefix_code = "1510";
                $design_prev = '';
                $today = date("Y-m-d");

                $attributes_json = array();
                $variations_json = array();
                $search_suggestion = array();

                $updates_file = './sample_raw_data_surya.csv';
                $target_file = $updates_file;
                $row = array();
                $updates_handle = fopen($target_file, 'r');

                if ($updates_handle) {
                    $count = 0;
                    $count2 = 0;
                    
                    while ($row = fgetcsv($updates_handle, 500000, ",")) {
                        if ($count != 0) {

                                
                            if($design_prev == $row[4])
                            {   
                                $fl1++;                

                                    $variations_json[$fl1]['GS1'] = $row[3];
                                    $variations_json[$fl1]['MSRP'] = $row[13];
                                    $variations_json[$fl1]['origMap'] = "";
                                    $variations_json[$fl1]['length'] = $row[21];
                                    $variations_json[$fl1]['width'] = $row[22];
                                    $variations_json[$fl1]['height'] = $row[23];
                                    $variations_json[$fl1]['oldPrice'] = "";
                                    $variations_json[$fl1]['option'] = "";
                                    $variations_json[$fl1]['price'] = $row[12];
                                    $variations_json[$fl1]['psf'] = "";
                                    $variations_json[$fl1]['sale'] = "";
                                    $variations_json[$fl1]['searchSize'] = str_replace("'","",$row[19]);
                                    $variations_json[$fl1]['searchSizeFloor'] = "";
                                    $variations_json[$fl1]['aliasSize'] = str_replace("'","",$row[19]);
                                    $variations_json[$fl1]['size'] = $row[19];
                                    $variations_json[$fl1]['shape'] = $row[18];
                                    $variations_json[$fl1]['sizeWeight'] = "";
                                    $variations_json[$fl1]['shippingHeight'] = $row[28];
                                    $variations_json[$fl1]['shippingLength'] = $row[26];
                                    $variations_json[$fl1]['shippingWidth'] = $row[27];
                                    $variations_json[$fl1]['shippingWeight'] = $row[25];
                                    $variations_json[$fl1]['shippingType'] = $row[30];
                                    $variations_json[$fl1]['sizeSku'] = $row[1];
                                    $variations_json[$fl1]['active'] = 1;
                                    $variations_json[$fl1]['stock'] = 5;
                                    $variations_json[$fl1]['stock30days'] = 0;
                                    $variations_json[$fl1]['stock60days'] = 0;
                                    $variations_json[$fl1]['stockover60days'] = 0;
                                    $variations_json[$fl1]['wholesale'] = 0;
                                

                            }else{
                                    
                                if($count2 != 0)
                                {

                                    $variations = json_encode($variations_json);            

                                    $product_data = array(
                                        'photo_id' => $photo_id, 
                                        'vendor_sku' => $vendor_sku,
                                        'name' => $name,  
                                        'orig_name' => $orig_name,        
                                        'description' => $description,
                                        'category' => $category,    
                                        '_category' => $_category,  
                                        'vendor' => $vendor,
                                        '_vendor' => $_vendor,
                                        'designer' => $designer,
                                        '_designer' => $_designer,
                                        'made_in' => $made_in,
                                        'color' => $color,
                                        'colors' => $colors,
                                        'material' => $material,
                                        'backing' => $backing,
                                        'quality' => $quality,     
                                        'new_arrival' => $new_arrival,
                                        'attribute_set' => $attribute_set,
                                        'isResidential' => $isResidential,
                                        'isCommercial' => $isCommercial,
                                        'isFurniture' => $isFurniture,
                                        'sector' => $sector,
                                        'search_suggestion' => $search_suggestion,
                                        'image' => $image,
                                        'thumb_pic' => $thumb_pic,
                                        'picture' => $picture,
                                        'large_pic' => $large_pic,
                                        'attributes' => $attributes,
                                        'variations' => $variations,
                                        'qualities' => $qualities,
                                        'publish_date' => $today,
                                        'sort_random' => "",
                                        'date_added' => $today,
                                        'keywords' => "",
                                        'keyword_additions' => ""
                                    );

                                    echo "<pre>";
                                    print_r($product_data);
                                    $this->Bank_model->import_product($product_data);
                                    // exit();

                                    $attributes_json = array();
                                    $variations_json = array();
                                    $search_suggestion = array();

                                } 
                                
                                $fl1 = 0;

                                    $photo_id = $vendor_prefix_code."_".str_replace(" ", "", $row[1]);    
                                    $vendor_sku = $row[1];
                                    $name = $row[6]." ".$row[4];      
                                    $orig_name = "";
                                    $description = $row[7];
                                    $category  = $row[6];
                                    $_category = ""; 
                                    $collection = $row[6];
                                    $_collection = "";
                                    $comm_category = $row[6];
                                    $comm__category = "";
                                    $comm_collection = $row[6];
                                    $comm__collection = "";
                                    $vendor = "Surya";
                                    $_vendor = "55";
                                    $designer = "";
                                    $_designer = "";
                                    $made_in = $row[33];
                                    $color = $row[14];
                                    $colors = $row[14];
                                    $material = $row[15];
                                    $backing = $row[40];
                                    $quality = $row[34];
                                    $new_arrival = 1;
                                    $attribute_set = "Rugs";
                                    $isResidential = "";
                                    $isCommercial = "";
                                    $isFurniture = "";
                                    $sector = "";
                                    // $search_suggestion =json();
                                    $image = $photo_id.".jpg";
                                    $thumb_pic = "/rug_images/".$photo_id."_small.jpg";
                                    $picture = "/rug_images/".$photo_id."_medium.jpg";
                                    $large_pic = "/rug_images/".$photo_id."_large.jpg";
                                    // $attributes = json();
                                    // $variations = json();
                                    $qualities = "";
                                    $design_prev = $row[4];


                                    $search_suggestion['search_suggestions'][0] = "";
                                    $search_suggestion['search_suggestions'][1] = "";
                                    $search_suggestion['search_suggestions'][2] = "";
                                    $search_suggestion['search_suggestions'][3] = "";
                                    $search_suggestion['search_suggestions'][4] = "";


                                    $attributes_json[0]['name'] = $row[15];
                                    $attributes_json[0]['value'] = $row[15];
                                    $attributes_json[0]['group'] = "material";

                                    $attributes_json[1]['name'] = $row[17];
                                    $attributes_json[1]['value'] = $row[17];
                                    $attributes_json[1]['group'] = "Style";

                                    $attributes_json[2]['name'] = $row[34];
                                    $attributes_json[2]['value'] = $row[34];
                                    $attributes_json[2]['group'] = "Construction";

                                // {"GS1":"764262973877","MSRP":130,"origMap":90,"length":36,"width":24,"height":0,"oldPrice":127000,"option":1,"price":90000,"psf":13.3333333333333,"sale":"0.3","searchSize":"2 x 3","searchSizeFloor":null,"aliasSize":"2 x 3","size":"2' x 3'","shape":"Rectangle","sizeWeight":0,"shippingHeight":4,"shippingLength":24,"shippingWidth":4,"shippingWeight":0,"shippingType":"Small Parcel","sizeSku":"COS8818-23","active":1,"stock":0,"stock30days":0,"stock60days":0,"stockover60days":0,"wholesale":41}

                                    $variations_json[$fl1]['GS1'] = $row[3];
                                    $variations_json[$fl1]['MSRP'] = $row[13];
                                    $variations_json[$fl1]['origMap'] = "";
                                    $variations_json[$fl1]['length'] = $row[21];
                                    $variations_json[$fl1]['width'] = $row[22];
                                    $variations_json[$fl1]['height'] = $row[23];
                                    $variations_json[$fl1]['oldPrice'] = "";
                                    $variations_json[$fl1]['option'] = "";
                                    $variations_json[$fl1]['price'] = $row[12];
                                    $variations_json[$fl1]['psf'] = "";
                                    $variations_json[$fl1]['sale'] = "";
                                    $variations_json[$fl1]['searchSize'] = str_replace("'","",$row[19]);
                                    $variations_json[$fl1]['searchSizeFloor'] = "";
                                    $variations_json[$fl1]['aliasSize'] = str_replace("'","",$row[19]);
                                    $variations_json[$fl1]['size'] = $row[19];
                                    $variations_json[$fl1]['shape'] = $row[18];
                                    $variations_json[$fl1]['sizeWeight'] = "";
                                    $variations_json[$fl1]['shippingHeight'] = $row[28];
                                    $variations_json[$fl1]['shippingLength'] = $row[26];
                                    $variations_json[$fl1]['shippingWidth'] = $row[27];
                                    $variations_json[$fl1]['shippingWeight'] = $row[25];
                                    $variations_json[$fl1]['shippingType'] = $row[30];
                                    $variations_json[$fl1]['sizeSku'] = $row[1];
                                    $variations_json[$fl1]['active'] = 1;
                                    $variations_json[$fl1]['stock'] = 5;
                                    $variations_json[$fl1]['stock30days'] = 0;
                                    $variations_json[$fl1]['stock60days'] = 0;
                                    $variations_json[$fl1]['stockover60days'] = 0;
                                    $variations_json[$fl1]['wholesale'] = 0;


                                    $search_suggestion = json_encode($search_suggestion);
                                    $attributes = json_encode($attributes_json);
                                    // $variations = json();


                            }

                            
                            $count2++;
                            //exit();
                                
                                
                        } 
                        
                        
                        $count++;
                        
                    }



                    $variations = json_encode($variations_json);            

                                    $product_data = array(
                                        'photo_id' => $photo_id,  
                                        'vendor_sku' => $vendor_sku,
                                        'name' => $name,  
                                        'orig_name' => $orig_name,        
                                        'description' => $description,
                                        'category' => $category,    
                                        '_category' => $_category,  
                                        'vendor' => $vendor,
                                        '_vendor' => $_vendor,
                                        'designer' => $designer,
                                        '_designer' => $_designer,
                                        'made_in' => $made_in,
                                        'color' => $color,
                                        'colors' => $colors,
                                        'material' => $material,
                                        'backing' => $backing,
                                        'quality' => $quality,     
                                        'new_arrival' => $new_arrival,
                                        'attribute_set' => $attribute_set,
                                        'isResidential' => $isResidential,
                                        'isCommercial' => $isCommercial,
                                        'isFurniture' => $isFurniture,
                                        'sector' => $sector,
                                        'search_suggestion' => $search_suggestion,
                                        'image' => $image,
                                        'thumb_pic' => $thumb_pic,
                                        'picture' => $picture,
                                        'large_pic' => $large_pic,
                                        'attributes' => $attributes,
                                        'variations' => $variations,
                                        'qualities' => $qualities,
                                        'publish_date' => $today,
                                        'sort_random' => "",
                                        'date_added' => $today,
                                        'keywords' => "",
                                        'keyword_additions' => ""
                                    );

                                    echo "<pre>";
                                    print_r($product_data);
                                    
                                    $this->Bank_model->import_product($product_data);


                }



       
        echo 1;   
    }
	
	public function deposit(){
        $data['bank_id'] = !empty($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $where_bank = array('status' => 1);
        $data['accounts'] = $this->Invoice_model->get_from_table('bank_accounts','*',$where_bank);
        $this->load->view('new_bank_deposit',$data);
    }

    public function deposit_amount(){
        $deposit_arr['to_acc_id'] = $this->input->post('bank_id'); 
        $deposit_arr['from_acc_id'] = $this->input->post('from_acc_id'); 
        $deposit_arr['receive_via'] = $this->input->post('receive_via'); 
        $deposit_arr['reference'] = $this->input->post('reference'); 
        $deposit_arr['amount'] = $this->input->post('amount'); 
        $deposit_arr['notes'] = $this->input->post('notes'); 
        $t_date = $this->input->post('tran_date');
        $arr1 = explode("/",$t_date);
        $deposit_arr['tran_date'] = $arr1[2]."-".$arr1[1]."-".$arr1[0];
        $deposit_arr['date_added'] = $arr1[2]."-".$arr1[1]."-".$arr1[0];
        $this->Bank_model->insert_bank_deposit($deposit_arr);
        echo 1;
    }
	
}
?>