<?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();
        error_reporting(E_ALL);
        ini_set('display_errors', 1);
        $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('Unit_Model');
        
        
    }
    public function index()
    {
        return false;
         echo "ssssssssssssssssss--<br/>";
        $data['main_menu_name'] = $this->main_menu_name;
        $data['sub_menu_name']  = $this->sub_menu_name;
        $data['all_data'] = $this->Manual_Query_With_Grn_Model->get_all_data();
        $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) { 
                    $product_code = $this->Common_Model->gen_ref_number('product_id', 'product', 'PD');
                    if ($row['barcode'] != "") {
                        $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();
                    }
                    $unit_data = $this->Unit_Model->get_unit_by_name($row['unit_id']);
                    if ($unit_data) {
                        $unit_id = $unit_data[0]->unit_id;
                    } else {
                        $unit_name = $row['unit_id'];
                        $this->Unit_Model->unit_save($unit_name, $unit_name);
                        $unit_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' => $row['min_qty'],
                        'product_max_qty' => $row['max_qty'],
                        'product_price' => $row['uomcost'],
                        'product_cost' => $row['uomcost'],
                        'product_unit'=>$unit_id,
                        'system_default_unit'=>$unit_id,
                        'product_type_id'=>3
                    );
                    $this->Manual_Query_With_Grn_Model->save_data($pd_list);
                    $product_id = $this->db->insert_id();
                    echo "<span style=\"color:green\">" . ++$added . " -> Item Added:" . $row['name'] . "</span><br/>";
                } else {
                    echo "Already Added:" . $row['name'] . ". Skipped...<br/>";
                }
        }
        }
    }
    
    
    
    
     public function row_mat_upload()
    {
        return false;
         echo "ssssssssssssssssss--<br/>";
        $data['main_menu_name'] = $this->main_menu_name;
        $data['sub_menu_name']  = $this->sub_menu_name;
        $data['all_data'] = $this->Manual_Query_With_Grn_Model->get_all_row_mat_data();
        $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->Manual_Query_With_Grn_Model->get_mat_product_by_name($row['name']);
                if (!$p_data) { 
                    $product_code ='';// $this->Common_Model->gen_ref_number('product_id', 'product', 'PM');
                    if ($row['barcode'] != "") {
                        $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();
                    }
                    $unit_data = $this->Unit_Model->get_unit_by_name($row['unit_id']);
                    if ($unit_data) {
                        $unit_id = $unit_data[0]->unit_id;
                    } else {
                        $unit_name = $row['unit_id'];
                        $this->Unit_Model->unit_save($unit_name, $unit_name);
                        $unit_id = $this->db->insert_id();
                    }
                    $pd_list = array(
                        'mat_cat_id' => $cat_id,
                        'mat_brand_id' => $brand_id,
                        'mat_sub_cat_id' => $sub_cat_id,
                        'product_mat_name' => $row['name'],
                        'product_mat_code' => $product_code,
                        'product_mat_thumb' => 'no-image.jpg',
                        'product_mat_image' => 'no-image.jpg',
                        'product_mat_alert_qty' => $row['min_qty'],
                        'product_mat_max_qty' => $row['max_qty'],
                        'product_mat_unit'=>$unit_id,
                        'product_mat_type_id'=>1
                    );
                    $this->Manual_Query_With_Grn_Model->save_mat_data($pd_list);
                    $product_id = $this->db->insert_id();
                    echo "<span style=\"color:green\">" . ++$added . " -> Item Added:" . $row['name'] . "</span><br/>";
                } else {
                    echo "Already Added:" . $row['name'] . ". Skipped...<br/>";
                }
        }
        }
    }
  
}