<?php
class Inventory_stock_report extends REF_Controller {
    
    function __construct(){
        parent::__construct();        
        $this->load->model('reports/inventory_stock_report_model');        
    }
    
    function index(){
        
        check_permission();
        $actions = $this->actions();
        
        $data = array(
            'title'          => 'Inventory Stock Report:',
            'controller'     => $this->controller,
            'actions'        => $actions,            
            'success'        => $this->session->flashdata('success'),
            'error'          => $this->session->flashdata('error'),
        );
        
        $whr			= array();
		$whr['type'] 	= 3;
		$whr['status'] 	= 1;
        $post 			= $this->input->post();
        
		//echo '<pre>'; print_r( $post ); 
        if( !empty( $post['submit'] ) ) :
            $f = $post['filter'];
            if( !empty( $f['category'] ) )
                $whr['inv_cat_id'] = $f['category'];
            
            if( !empty( $f['item'] ) )
                $whr['inv_item_id'] = $f['item'];
				
			if( !empty( $f['itemcode'] ) )
                $whr['inv_item_id'] = $f['itemcode'];
        endif;        
        
        $filter = array(
            'category' 	=> $f['category'],
            'item'      => $f['item'],
			'itemcode'  => $f['itemcode'],   
        );
        
        $data['filter'] 	= $filter;        
       
	    $data['categories'] = _get(TBL_INVENTORY_CATEGORY, array('status' => 1), array('order_by' => 'name', 'select' => 'id, name'));
		$data['items'] 		= _get(TBL_INVENTORY_ITEM, array('status' => 1), array('order_by' => 'item_name', 'select' => 'id, code, category_id, item_name'));
        $data['results'] 	= _get(TBL_INV_LOG, $whr, array('order_by' => 'id'));
        
        $filters 			= array();
        if(!empty($this->input->post('filter_columns'))) {
            foreach($this->input->post('filter_columns') as $key=>$value) {
                array_push($filters,$this->input->post($value));
            } 
        }
        $data['filters'] = array_filter($filters);
        
        if($this->input->post('submit') == 'PDF Data'){            
			$filename = 'inventory_stock_report_'.time();
			$html 	  = $this->load->view('reports/inventory_stock/pdf', $data, TRUE);		  
			$header		= '';
			$footer		= '';
			//echo $header.$html.$footer; exit;
			$this->load->helper(array('My_Pdf'));   //  Load helper
			create_pdf($header, $html, $footer, $filename);
			redirect('inventory_stock_report', 'refresh');           
        } elseif(((!empty($this->input->post('submit'))) && ($this->input->post('submit') == 'Excel Data'))) {
		    $this->load->view('reports/inventory_stock/excel', $data);
        }else {
            $this->render($data, 'reports/inventory_stock/list');
        }        
    }  
	
	public function get_item_records(){
		$catId	= $_POST['catId'];	
		$items	= _get(TBL_INVENTORY_ITEM, array('status' => 1, 'category_id' => $catId), array('order_by' => 'item_name', 'select' => 'id, code, category_id, item_name'));
		
		$htmlItemCode = '<option value="">Select Item Code</option>';
	  	foreach($items as $k => $r ) :
	  		$htmlItemCode .= '<option value="'.$r['id'].'">'.$r['code'].'</option>';
	  	endforeach; 
		
		$htmlItem = '<option value="">Select Item</option>';
	  	foreach($items as $k => $r ) :
	  		$htmlItem .= '<option value="'.$r['id'].'">'.$r['item_name'].'</option>';
	  	endforeach;
		
		echo $htmlItem."SPLIT".$htmlItemCode;
	}
}