<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
class upload extends CI_Controller
{
    var $main_menu_name = "people";
    var $sub_menu_name = "suppliers";
    public function __construct()
    {
        parent::__construct();
        $this->load->model('country_model');
        $this->load->model('Common_Model');
        $this->load->model('product_models');
        $this->load->model('purchases_model');
        $this->load->model('Manual_Query_With_Grn_Model');
        $this->load->model('category_models');
        $this->load->model('Product_Discount_Model');
    }
    public function index()
    {
        $data['main_menu_name'] = $this->main_menu_name;
        $data['sub_menu_name']  = $this->sub_menu_name;
        echo "<br/>";
        $data['all_data'] = $this->Manual_Query_With_Grn_Model->get_all_data();
        $data_list        = array(
            'reference_no' => $this->Common_Model->gen_ref_number('id', 'purchases', 'GRN'),
            'warehouse_id' => 1,
            'supplier_id' => 1,
            'date' => date("Y-m-d H:i:s")
        );
        //$this->purchases_model->save_grn($data_list);
        $grn_id           = ''; //$this->db->insert_id();
        $tot_cost_grn     = 0;
        $count            = 0;
        $added            = 0;
        $updated          = 0;
        foreach ($data['all_data'] as $row) {
            ++$count;
            echo "<hr>" . $count . ")</br>";
            if (trim($row['name'])) {
                $p_data = $this->product_models->get_product_by_name($row['name']);
                if (!$p_data) { //continue;
                    $product_code = $this->Common_Model->gen_ref_number('product_id', 'product', 'PD');
                    if ($row['barcode'] != "NONE") {
                        $product_code = $row['barcode'];
                    }
                    $cat_id   = '';
                    $cat_data = $this->category_models->get_category_by_name($row['category']);
                    if ($cat_data) {
                        $cat_id = $cat_data[0]->cat_id;
                    } else {
                        $category_code = $this->Common_Model->gen_ref_number('cat_id', 'product_category', 'PC');
                        $category_name = $row['category'];
                        $this->category_models->category_save($category_code, $category_name);
                        $cat_id = $this->db->insert_id();
                    }
                    $brand_id   = '';
                    $brand_data = $this->category_models->get_brand_by_name($row['brand']);
                    if ($brand_data) {
                        $brand_id = $brand_data[0]->brand_id;
                    } else {
                        $brand_code = $this->Common_Model->gen_ref_number('brand_id', 'master_brand', 'BD');
                        $brand_name = $row['brand'];
                        $this->category_models->brand_save($brand_code, $brand_name);
                        $brand_id = $this->db->insert_id();
                    }
                    $sub_cat_id   = '';
                    $sub_cat_data = $this->category_models->get_sub_category_by_name($row['sub_category']);
                    if ($sub_cat_data) {
                        $sub_cat_id = $sub_cat_data[0]->sub_cat_id;
                    } else {
                        $sub_cat_code = $this->Common_Model->gen_ref_number('cat_id', 'product_sub_category', 'PSC');
                        $sub_cat_name = $row['sub_category'];
                        $this->category_models->category_sub_save($cat_id, $sub_cat_code, $sub_cat_name);
                        $sub_cat_id = $this->db->insert_id();
                    }
                    $pd_list = array(
                        'cat_id' => $cat_id,
                        'brand_id' => $brand_id,
                        'sub_cat_id' => $sub_cat_id,
                        'product_name' => $row['name'],
                        'product_code' => $product_code,
                        'product_thumb' => 'no-image.jpg',
                        'product_image' => 'no-image.jpg',
                        'product_alert_qty' => 5,
                        'product_max_qty' => 15,
                        'product_unit' => 6,
                        'product_cost' => $row['cost'],
                        'wholesale_price' => $row['wholesale_price'],
                        'product_price' => $row['price'],
                        'credit_salling_price' => $row['credit_price'],
                        'retail_cash' => $row['retail_cash'],
                        'retail_credit' => $row['retail_credit'],
                        'retail_wholesale' => $row['retail_wholesale'],
                        'product_alert_qty' => $row['min_qty'],
                        'product_max_qty' => $row['max_qty'],
                        'item_code' => $row['code_1'] . $row['code_2'],
                        'capacity' => $row['capacity'],
                        'color' => $row['color'],
                        'product_weight'=>$row['weight_new'],
                        'pd_supply_type'=>$row['type'],
                    );
                    $this->Manual_Query_With_Grn_Model->save_data($pd_list);
                    $product_id = $this->db->insert_id();
                    $this->enable_discount($product_id, 1, $row['cash_range_start'], $row['cash_range_end'], $row['cash_discount']);
                    $this->enable_discount($product_id, 2, $row['credit_range_start'], $row['credit_range_end'], $row['credit_discount']);
                    $this->enable_discount($product_id, 3, 0, 0, 0);
                    echo "<span style=\"color:green\">" . ++$added . " -> Item Added:" . $row['name'] . "</span><br/>";
                    /*if ($row['qty'] > 0) {
                    $sub_total = 0;
                    $sub_total = $row['qty'] * $row['cost'];
                    $tot_cost_grn += $sub_total;
                    $data_item = array(
                    'purchase_id' => $grn_id,
                    'product_id' => $product_id,
                    'product_code' => $product_code,
                    'product_name' => $row['name'],
                    'quantity' => $row['qty'],
                    'unit_price' => $row['cost'],
                    'sub_total' => $sub_total
                    );
                    //$this->purchases_model->save_grn_item($data_item);
                    //echo "<br/>Item Added To GRN :" . $row['name'] . "<br/>";
                    }*/
                } else {
                    echo "Already Added:" . $row['name'] . ". Skipped...<br/>";
                /*   
				//$p_data = $this->product_models->get_product_by_id($p_data->product_id);
				$product_code = $this->Common_Model->gen_ref_number('product_id', 'product', 'PD');
                  
				$pd_list = array(
					'product_code' => $product_code
				);
				
				echo "<pre>";
				print_r($pd_list);
				echo "</pre>";
				$this->Manual_Query_With_Grn_Model->update_product($p_data->product_id, $pd_list);*/
				
				}
                //$this->purchases_model->save_grn($data_list, $grn_id);
            }
        }
    }
    function update(){
        echo "<br/>";
        $data['all_data'] = $this->Manual_Query_With_Grn_Model->get_updated_data();
        $count            = 0;
        $added            = 0;
        $updated          = 0;
        foreach ($data['all_data'] as $row) {
            ++$count;
            echo "<hr>#" . $count . ")</br>";
            if (trim($row['product_id'])) {
                $p_data = $this->product_models->get_product_by_id($row['product_id']);
                if (!$p_data) {
                    echo "<br/>New item skipped : " . $row['name'] . "<br/>";
                    continue;
                } else {
                    $cat_id   = '';
                    $cat_data = $this->category_models->get_category_by_name($row['product_cat']);
                    if ($cat_data) {
                        $cat_id = $cat_data[0]->cat_id;
                    } else {
                        $category_code = $this->Common_Model->gen_ref_number('cat_id', 'product_category', 'PC');
                        $category_name = $row['product_cat'];
                        $this->category_models->category_save($category_code, $category_name);
                        $cat_id = $this->db->insert_id();
                    }
                    $sub_cat_id   = '';
                    $sub_cat_data = $this->category_models->get_sub_category_by_name($row['product_sub_cat']);
                    if ($sub_cat_data) {
                        $sub_cat_id = $sub_cat_data[0]->sub_cat_id;
                    } else {
                        $sub_cat_code = $this->Common_Model->gen_ref_number('cat_id', 'product_sub_category', 'PSC');
                        $sub_cat_name = $row['product_sub_cat'];
                        $this->category_models->category_sub_save($cat_id, $sub_cat_code, $sub_cat_name);
                        $sub_cat_id = $this->db->insert_id();
                    }
                    $pd_list = array(
                        'cat_id' => $cat_id,
                        'sub_cat_id'	=>	$sub_cat_id,
                        'product_name'	=>	$row['product_name'],
						'product_cost'	=>	$row['cost'],
						'retail_cash'	=>	$row['hw_cash'],
						'retail_credit'	=>	$row['hw_credit'],
						'retail_wholesale'	=>	$row['hw_wholesale']
                    );
					echo "<pre>";
					print_r($pd_list);
                    $this->Manual_Query_With_Grn_Model->update_product($row['product_id'], $pd_list);
					echo "</pre>";
                }
            }
        }
    }
    public function get_all_country()
    {
        $data['id']             = 1;
        $data['main_menu_name'] = $this->main_menu_name;
        $data['sub_menu_name']  = 'create_supplier';
        $data['all_country']    = $this->country_model->get_all_country();
    }
    function enable_discount($product_id, $invoice_type_id, $start_range_value, $end_range_value, $discount_value)
    {
        $discountData = array(
            'product_id' => $product_id,
            'invoice_type_id' => $invoice_type_id,
            'start_range_value' => $start_range_value,
            'end_range_value' => $end_range_value,
            'discount_value' => $discount_value,
            'is_precentage' => 0
        );
        //print_r($discountData);
        $result       = $this->Product_Discount_Model->insert_update_master_table($discountData, '');
        if ($result == true) {
            $st = array(
                'error' => 0,
                'status' => 1,
                'message' => "Done"
            );
        } else {
            $st = array(
                'error' => 1,
                'status' => 0,
                'message' => "Error ! Data not saved"
            );
        }
        //echo json_encode($st);
    }
    
    function fix_product_codes(){
		$products = $this->Manual_Query_With_Grn_Model->get_empty_coded_products();
		foreach($products as $row){
			$product_code = $this->Common_Model->gen_ref_number('product_id', 'product', 'PD');
			$pd_data = array(
				'product_code'	=>	$product_code,
			);
			$this->Manual_Query_With_Grn_Model->update_product($row['product_id'], $pd_data);
			echo "Fixed product: ".$row['product_name']."</br>";
		}
	}
}