<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
    include('product_material_core.php');  
class Product_Material extends Product_Material_Core
{
    public function __construct()
    {
        parent::__construct();
    }
    function  loadListProductMaterial(){
        $data=array();
        $data['main_menu_name'] = $this->main_menu_name;
        $data['sub_menu_name']  = 'listProductMaterial';
        $this->load->view('product_material/list_product_material',$data);
    }
    function  loadListProductMaterialAvailability(){
        $this->load->model('warehouse_model');
        $data=array();
        $data['main_menu_name'] = $this->main_menu_name;
        $data['sub_menu_name']  = 'loadListProductMaterialAvailability';
        $data['warehouses']  = $this->warehouse_model->get_all_warehouse();
        $this->load->view('product_material/list_product_material_availability',$data);
    }
   function loadAddProductMaterial(){
       $data=array();
       $data['main_menu_name'] = $this->main_menu_name;
       $data['sub_menu_name']  = 'addProductMaterial';
       $data                   =$this->setBasicSelectOptionDataForMaterial($data);
       $this->load->view('product_material/add_product_material', $data);
   }
   function save_qty(){
       $data = $this->input->post();
       echo json_encode($data);
   }
    function loadSaveProductMaterial(){
       $this->saveProductMaterial();
       return false;
   }
   function loadEditProductMaterial(){
        $data=array();
        $data['main_menu_name']  =$this->main_menu_name;
        $data['sub_menu_name']   ='listProductMaterial';
        $data                    =$this->setBasicSelectOptionDataForMaterial($data);
        $product_id              =$this->input->get('pid');
        $data                    =$this->setMAterialBasicDataUsingMasterID($product_id,$data);
        $data['sub_category']    =$this->category_models->getSubCategory($data['product_details']->mat_cat_id);
        $this->load->view('product_material/edit_product_material', $data);
   }
    function loadUpdateProductMaterial(){
       $this->updateProductMaterial();
       return false;
   }
  
    function loadGetProductMaterialList(){
        $request_data=$this->getClientSideDataTableParameters();
        $result_array=$this->getMaterialDataListForBasicDataTableOnServerSide($request_data);
        $values=$result_array['values'];
        $value_count=$result_array['value_count'];
        $totalData     = $value_count;
        $totalFiltered = $totalData;
        $data          = array();
        if (!empty($values)) {
            foreach ($values as $products) {
                $k = '';
                if ($products->product_mat_status == 0) {
                    $k = "btn-danger";
                    $m = "fa-minus-circle";
                } else {
                    $k = "btn-green";
                    $m = "fa-check";
                }
                if ($products->product_mat_status == 0) {
                        $open = '<a class="list-group-item list-group-item-danger">';
                        $close = '</a>';
                    } else if ($products->product_mat_status == 1) {
                        $open = '<a class="list-group-item" style="border: none; background: inherit">';
                        $close = '</a>';
                    }
                $retVal              = (empty($products->sub_cat_name)) ? "--:--" : $products->sub_cat_name;
                $row                 = array();
                $ss_warehouse_id =  "";
                $qty=$this->Stock_Counter_Model->get_stock_balance_batch($ss_warehouse_id,$products->product_mat_id);
               
                // $row[]               = '<div style="margin-bottom: 0px; width: 50px; height: 50px;" class="fileupload-new thumbnail"><img alt="" src="' . asset_url() . "uploads/thumbs/" . $products->product_mat_thumb . '"></div>';
                $row[]               = $open.$products->product_mat_id.$close;
                $row[]               = $open.$products->product_mat_code.$close;
                $row[]               = $open.$products->product_mat_name.$close;
                $row[]               = $open.$products->cat_name.$close;
                $row[]               = $retVal;
                $row[]               = $open.$products->unit_name.$close;
                $bbreak = '';
                if($this->common_model->check_permission(117,$this->session->userdata( 'ss_group_id' ), 1 )){ 
                    $row[] = $products->product_mat_cost;
                    //$bbreak = '<li><a onclick="break_product(' . $products->product_id . '); return false;" href="#"><i class="fa fa-star"></i> Break Product</a></li>';
                }else{
                    $row[] = "";
                }
                $row[]  = $open.$qty.$close;
                $actdes = '';
                $disable = '<li><a style="cursor: pointer;" onclick="disable_product(' . $products->product_mat_id . ')"><i class="glyphicon fa fa-minus-circle"></i> Disable </a></li>';
                $enable = '<li><a style="cursor: pointer;" onclick="enable_product(' . $products->product_mat_id . ')"><i class="glyphicon fa fa-plus-circle"></i> Enable </a></li>';
                
                $actdes = $actdes . '<div class="btn-group text-left">
                            <button data-toggle="dropdown" class="btn btn-default btn-xs ' . $k . ' dropdown-toggle" type="button">Actions <span class="caret"></span></button>
                            <ul role="menu" class="dropdown-menu pull-right">
                            <li><a href="' . base_url('product_material/productMaterialDetails?id='. $products->product_mat_id).'"><i class="fa fa-file-text-o"></i> Details</a></li>
                            ';
                    if($this->common_model->check_permission(113,$this->session->userdata( 'ss_group_id' ), 1 )){
                        if ($products->product_mat_status == 0) {
                            $actdes .= $enable;
                        } else if ($products->product_mat_status == 1) {
                            $actdes .= $disable;
                        }
                    }
                    
                    if($products->product_mat_type_id==2||$products->product_mat_type_id==2){
                        //$actdes.='<li class="divider"></li><li><a onclick="create_new_block(' . $products->product_mat_id . '); return false;" href="#"><i class="fa fa-trash-o"></i> Create New Block</a></li>';
                    }
                    
                    
                    if($this->common_model->check_permission(112,$this->session->userdata( 'ss_group_id' ), 1 )&&$products->product_mat_type_id==1){
                        $actdes = $actdes . '
                            <li><a href="' . base_url('product_material/loadEditProductMaterial?pid='.$products->product_mat_id). '"><i class="fa fa-edit"></i> Edit</a></li>
                            
                           <!-- <li class="divider"></li><li><a onclick="product_delete(' . $products->product_mat_id . '); return false;" href="#"><i class="fa fa-trash-o"></i> Delete </a></li> -->
                            ';
                    }else{
                        
                    }
                $actdes = $actdes . '    
                           <!-- <li><a onclick=" print_barcode(' . $products->product_mat_id . '); return false;" href="#"><i class="fa fa-print"></i>  Print Barcode</a></li>-->
                            '.$bbreak.'
                            </ul></div>';
                            
                if($this->common_model->check_permission(112,$this->session->userdata( 'ss_group_id' ), 1 )){
                       $row[]  = $actdes;
                    }else{
                      $row[]  = "";  
                    }
                $data[] = $row;
            }
            $output = array(
                'data' => $data,
                "recordsTotal" => intval($totalData),
                "recordsFiltered" => intval($totalFiltered)
            );
            echo json_encode($output);
        } else {
            $output = array(
                'data' => ''
            );
            echo json_encode($output);
        }
   }
    function loadGetProductMaterialListAvailability(){
        $this->load->model('warehouse_model');
        $request_data=$this->getClientSideDataTableParameters();
        $result_array=$this->getMaterialDataListForBasicDataTableOnServerSide($request_data);
        $values=$result_array['values'];
        $value_count=$result_array['value_count'];
        $totalData     = $value_count;
        $totalFiltered = $totalData;
        $data          = array();
        if (!empty($values)) {
            foreach ($values as $products) {
                $k = '';
                if ($products->product_mat_status == 0) {
                    $k = "btn-danger";
                    $m = "fa-minus-circle";
                } else {
                    $k = "btn-green";
                    $m = "fa-check";
                }
                if ($products->product_mat_status == 0) {
                        $open = '<a class="list-group-item list-group-item-danger">';
                        $close = '</a>';
                    } else if ($products->product_mat_status == 1) {
                        $open = '<a class="list-group-item" style="border: none; background: inherit">';
                        $close = '</a>';
                    }
                $retVal              = (empty($products->sub_cat_name)) ? "--:--" : $products->sub_cat_name;
                $row                 = array();
                $ss_warehouse_id =  "";
                $qty=$this->Stock_Counter_Model->get_stock_balance($ss_warehouse_id,$products->product_mat_id);
               
                // $row[]               = '<div style="margin-bottom: 0px; width: 50px; height: 50px;" class="fileupload-new thumbnail"><img alt="" src="' . asset_url() . "uploads/thumbs/" . $products->product_mat_thumb . '"></div>';
                //$row[]               = $open.$products->product_mat_id.$close;
                $row[]          = $open.'<input name="product_mat_code[]" value="'.$products->product_mat_code.'" readonly>'.$close;
                $row[]          = $open.$products->product_mat_name.$close;
                $row[]          = '<span class="total_order" data-id="'.$products->product_mat_id.'" data-current_qty="current_qty_'.$products->product_mat_id.'" data-new_qty="new_qty_'.$products->product_mat_id.'">'.$products->product_mat_id.'</span>';
                $row[]          = '<input name="final_qty[]" id="final_qty_'.$products->product_mat_id.'" readonly>';
                
                $warehouses  = $this->warehouse_model->get_all_warehouse();
                foreach($warehouses as $wh){
                    $row[]               = '<input name="wh_qty_'.$wh->id.'[]" class="input-sm new_qty_'.$products->product_mat_id.'" data-final_qty="final_qty_'.$products->product_mat_id.'" data-class_name="new_qty_'.$products->product_mat_id.'" value="'.$products->product_mat_id.'"> | <span class="current_qty_'.$products->product_mat_id.'">'.$products->product_mat_id.'</span>';
                }
                $actdes = '<button class="btn btn-default btn-sm" type="button" onClick="check_qty(this,\'final_qty_'.$products->product_mat_id.'\',\''.$products->product_mat_code.'\')"><i class="fa fa-file-text-o"></i> Check</button>';
                $row[]  = $actdes;
                $data[] = $row;
            }
            $output = array(
                'data' => $data,
                "recordsTotal" => intval($totalData),
                "recordsFiltered" => intval($totalFiltered)
            );
            echo json_encode($output);
        } else {
            $output = array(
                'data' => ''
            );
            echo json_encode($output);
        }
   }
   function check_qty(){
        $output = array(
            'success' => true
        );
        echo json_encode($output);
   }
   public function productMaterialDetails()
    {
        $this->load->model('Batch_Process_Model');
        $data['main_menu_name']     = $this->main_menu_name;
        $data['sub_menu_name']      = $this->sub_menu_name;
        $product_id=$this->input->get('id');
        $data =$this->setMAterialBasicDataUsingMasterID($product_id,$data);
        $data['warehouse_batch_qty_array'] =array();
        $data['warehouses']=array();
        $data['available_qty'] = "";
        
        
        $bl=$this->Batch_Process_Model->get_batch_list_by_product_id($product_id);
        $batch_list=array();
        foreach($bl as $b){
            
            $stock=$this->Stock_Counter_Model->get_stock_balance_batch('',$product_id,$b->batch_id);
            if($stock==0){
              $bdata=array(
		            'batch_status'=>0,
		            );
		        $insert_result=$this->Batch_Process_Model->update_batch_product_table($bdata,$b->batch_id);  
            }
            if($stock==0&&$b->batch_status==0){
              continue;  
            }
            
            $nestedArray=array(
                'batch_ref_number'=>$b->batch_ref_number,
                'product_cost'=>$b->product_cost,
                'retail_cash'=>$b->retail_cash,
                'retail_wholesale'=>$b->retail_wholesale,
                'batch_stock'=>$stock,
                'batch_status'=>$b->batch_status,
                'batch_id'=>$b->batch_id,
                );
                
                $batch_list[]=$nestedArray;
        }
        
        
        $data['batch_list'] =$batch_list;
        $this->load->view('product_material/view_product_material', $data);
    }
    public function enableProductMaterial(){
        $id = $this->input->get('product_id');
        $data=array('product_mat_status'=>1);
        $result=$this->updateProductMaterialTable($id,$data);
        $output = array(
                'status' => $result,
                "message" => "Done"
            );
            echo json_encode($output);
    }
    public function disableProductMaterial(){
        $id = $this->input->get('product_id');
        $data=array('product_mat_status'=>0);
        $result=$this->updateProductMaterialTable($id,$data);
        $output = array(
                'status' => $result,
                "message" => "Done"
            );
            echo json_encode($output);
    }
   
  
  
  
  
   
}