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

class Items 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('Item_model');
    }

    public function index(){
        $data['item_list'] = $this->Item_model->get_items();
        $items_autocomplete = array();
        $items_barcode = array();
        if (count($data['item_list'])>0) {
            foreach ($data['item_list'] as $key => $value) {
                $items_autocomplete[$key] = new stdClass();
                $items_autocomplete[$key]->id = $value->item_id;
                $items_autocomplete[$key]->name = $value->item_name;

                $items_barcode[$value->item_no] = $value;
            }
        }
        $data['items_autocomplete'] = json_encode($items_autocomplete);
        $data['items_barcode'] = json_encode($items_barcode);
        $this->load->view('item_list',$data);
    }

    public function newitem(){
        $data['item_id'] = !empty($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        if ($data['item_id'] > 0) {
            $data['item_details'] = $this->Item_model->get_item_details($data['item_id']);
        }
        $this->load->view('new_item',$data);
    }
    
    public function add_items_name_check(){
       $item_name = $this->input->post('item_name');
        if ($item_name != "") {
            $res = $this->Item_model->items_name_check($item_name);
        }else{
          
          echo 0;  
            
        }
        echo $res;
    }
    

    public function add_items(){
        $item_id = $this->input->post('item_id');
        $item_name = $this->input->post('item_name');
        $item_description = $this->input->post('item_description');
        $unit = $this->input->post('unit');
        $rate_purchase = $this->input->post('rate_purchase');
        $hsn_code = $this->input->post('hsn_code');
        $gst_rate = $this->input->post('gst_rate');
        $metal_used = $this->input->post('metal_used');
        $stone_used = $this->input->post('stone_used');
        $rate_sale = $this->input->post('rate_sale');
        $manufacturing_charge = $this->input->post('manufacturing_charge');
        $rhodium_charge = $this->input->post('rhodium_charge');
        $uploadPath = "";
        //$image=$_FILES['image']['name'];
        //$image2=$_FILES['image']['name'];

        if (isset($_FILES['image'])) {
            $file = $_FILES['image'];
            $uploadDir = 'designimages/';
            $uploadPath = $uploadDir . basename($file['name']);
          
            if (move_uploaded_file($file['tmp_name'], $uploadPath)) {
              //echo 'File uploaded successfully';
            } else {
              //echo 'File upload failed';
            }
          } else {
            //echo 'No file uploaded';
          }

        $item_data = array(
                'item_name' => $item_name,
                'item_description' => $item_description,
                'unit' => $unit,
                'hsn_code' => $hsn_code,
                'rate_sale' => $rate_sale,
                'rate_purchase' => $rate_purchase,
                'gst_rate' => $gst_rate,
                'metal_used' => $metal_used,
                'stone_used' => $stone_used,
                'manufacturing_charge' => $manufacturing_charge,
                'rhodium_charge' => $rhodium_charge,
                'image' => $uploadPath,
                'date_added' => date('Y-m-d'),
                'date_edited' => date('Y-m-d'),
                'status' => 1
            );
        if ($item_id == 0) {
            $item_data['item_no'] = uniqid();
            $this->generate_barcode($item_data['item_no']);
            $this->Item_model->insert_data('items',$item_data);
        } else {
            // unset($item_data['manufacturing_charge']);
            // unset($item_data['rhodium_charge']);
            $this->Item_model->update_data('items',$item_data,array('item_id' => $item_id));
        }
        echo 1;
    }

    public function get_item_stock(){
        $item = $this->input->post('item_id');
        $item_stock = $this->Item_model->get_stock($item);
        echo !empty($item_stock->stock) ? $item_stock->stock : 0;
    }

    function generate_barcode($item_code){
        // $item_code = '265fgfghp54';
        $this->load->library('zend');
        $this->zend->load('Zend/Barcode');
        // $barcode         = $this->input->post('location');
        $barimage = Zend_Barcode::draw('code128', 'image', array('text' => $item_code), array());
        var_dump($barimage);
        imagejpeg($barimage, 'assets/barcodeImg/'.$item_code.'.jpg', 100);
        return true; 
    }

    function print_barcode($item_id){
        $data['item_details'] = $this->Item_model->get_item_details($item_id);
        $this->load->view('barcode_print',$data);
    }

    private function item_no(){
        $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
        return substr(str_shuffle(str_repeat($pool, ceil(8 / strlen($pool)))), 0, 8);
    }

    public function add_stock(){
        $item = $this->input->post('item_id');
        $qty = $this->input->post('quantity');

        if (!empty($item) && !empty($qty)) {
            $item_add = array('item_id' => $item,'qty_sale'=>0,'qty_purchase'=>$qty,'inv_bill_id'=>0,'date_added'=>date('Y-m-d'),'is_note'=>2);
            $stock_id = $this->Item_model->insert_data('item_stock',$item_add);
            $item_details = $this->Item_model->get_item_details($item);
            $metal_stock = array(
                                'purchase_expence_id'  =>  $stock_id,
                                'material_item_id'  =>  1,
                                'material_weight_in_gm'  =>  ($item_details->metal_used * $qty),
                                'purchase_expance'  =>  2,
                                'date_added'  =>  date('Y-m-d'),
                                'date_edited'  =>  date('Y-m-d'),
                                'status'  =>  1
                            );
            $this->Item_model->insert_data('raw_material_stock',$metal_stock);
            $stone_stock = array(
                                'purchase_expence_id'  =>  $stock_id,
                                'material_item_id'  =>  2,
                                'material_weight_in_gm'  =>  ($item_details->stone_used * $qty),
                                'purchase_expance'  =>  2,
                                'date_added'  =>  date('Y-m-d'),
                                'date_edited'  =>  date('Y-m-d'),
                                'status'  =>  1
                            );
            $this->Item_model->insert_data('raw_material_stock',$stone_stock);

            $production_expence = array(
                                    'item_stock_id' => $stock_id,
                                    'manufacturing_charge' => ($item_details->manufacturing_charge * $qty),
                                    'rhodium_charge' => ($item_details->rhodium_charge * $qty)
                                );
            $this->Item_model->insert_data('production_expence',$production_expence);
        }
        echo 1;
    }
}
?>