<?php

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

class Inventoryreport extends Admin_Controller {

    function __construct() {
        parent::__construct();
    }
   public function index() {
    if (!$this->rbac->hasPrivilege('class', 'can_view')) {
        access_denied();
    }

    $this->session->set_userdata('top_menu', 'Inventory Report');
    $this->session->set_userdata('sub_menu', 'inventoryreport/index');
    $data['title_list'] = 'Inventory Stock Management Report';

    // Load item list for dropdown
    $item_result = $this->item_model->get();
    $data['itemlist'] = $item_result;
    $data['receivedqnty']=$this->inventory_model->getInventoryQnty($query);  
    $data['soldqnty']=$this->inventory_model->getInventoryQntyIssued($query1);  
    $data['leftqnty']=$this->inventory_model->getInventoryQntyLeft($qnty_on_hand);

    // Get POST filter values
    $filter_from_date = $this->input->post('filter_from_date') ?? date('Y-m-d');
    $filter_to_date = $this->input->post('filter_to_date') ?? date('Y-m-d');
    $filter_start_item = $this->input->post('filter_start_item');
    $filter_end_item = $this->input->post('filter_end_item');

    // Save back to view
    $data['filter_from_date'] = $filter_from_date;
    $data['filter_to_date'] = $filter_to_date;
    $data['filter_start_item'] = $filter_start_item;
    $data['filter_end_item'] = $filter_end_item;

    // Initialize item list as empty
    $data['itemissueList'] = [];

    // Validate inputs
    if (empty($filter_start_item) || empty($filter_end_item)) {
        $data['error'] = "Both 'Item From' and 'Item To' must be selected.";
    } elseif (strtotime($filter_from_date) > strtotime($filter_to_date)) {
        $data['error'] = "From Date cannot be greater than To Date.";
    } else {
        // Get filtered data from model
        $data['itemissueList'] = $this->itemissue_model->get_filtered(
            $filter_from_date,
            $filter_to_date,
            $filter_start_item,
            $filter_end_item
        );
    }

    $this->load->view('layout/header', $data);
    $this->load->view('inventoryreport/inventoryreport', $data);
    $this->load->view('layout/footer', $data);
}

	public function indexmvmnt() {
        if (!$this->rbac->hasPrivilege('class', 'can_view')) {
            access_denied();
        }
        $this->session->set_userdata('top_menu', 'Inventory Report');
        $this->session->set_userdata('sub_menu', 'inventoryreport/indexmvmnt');
        $data['title_list'] = 'Inventory Stock Movement Report';
        $item_result = $this->item_model->get();
        $data['itemlist'] = $item_result;
        $data['receivedqnty']=$this->inventory_model->getInventoryQnty($query);  
        $data['soldqnty']=$this->inventory_model->getInventoryQntyIssued($query1);  
        $data['leftqnty']=$this->inventory_model->getInventoryQntyLeft($qnty_on_hand); 
    
        // Get filter values from POST or set defaults
        $filter_from_date = $this->input->post('filter_from_date') ? $this->input->post('filter_from_date') : date('Y-m-d');
        $filter_to_date = $this->input->post('filter_to_date') ? $this->input->post('filter_to_date') : date('Y-m-d');
        $filter_start_item = $this->input->post('filter_start_item');
        $filter_end_item = $this->input->post('filter_end_item');
    
        // Pass filter values back to view
        $data['filter_from_date'] = $filter_from_date;
        $data['filter_to_date'] = $filter_to_date;
    
        // Get filtered data
        $itemcategory = $this->itemissue_model->get_filteredmvmnt($filter_from_date, $filter_to_date, $filter_start_item, $filter_end_item);
        $data['itemissueList'] = $itemcategory;
    
        $this->load->view('layout/header', $data);
        $this->load->view('inventoryreport/invenstockmvmnt', $data);
        $this->load->view('layout/footer', $data);
    }
    

  

    public function indexlvl() {
        if (!$this->rbac->hasPrivilege('class', 'can_view')) {
            access_denied();
        }
    
        // set active menu & page title
        $this->session->set_userdata('top_menu', 'Inventory Report');
        $this->session->set_userdata('sub_menu', 'inventoryreport/indexlvl');
        $data['title_list'] = 'Inventory Stock Level Report';
    
        // load full item list for the dropdown
        $data['itemlist'] = $this->item_model->get();
    
        // pull date filters (default to today if none posted)
        $filter_from_date = $this->input->post('filter_from_date')
            ? $this->input->post('filter_from_date')
            : date('Y-m-d');
        $filter_to_date = $this->input->post('filter_to_date')
            ? $this->input->post('filter_to_date')
            : date('Y-m-d');
    
        // pull item-range filters
        $filter_start_item = $this->input->post('filter_start_item');
        $filter_end_item   = $this->input->post('filter_end_item');
    
        // send all filters back to the view so inputs stay populated
        $data['filter_from_date']  = $filter_from_date;
        $data['filter_to_date']    = $filter_to_date;
        $data['filter_start_item'] = $filter_start_item;
        $data['filter_end_item']   = $filter_end_item;
        $data['form_submitted']    = true;
    
        // fetch stock-level data within the given date + item range
        $data['itemissueList'] = $this->itemissue_model->get_filteredlvl(
            $filter_start_item,
            $filter_end_item,
            $filter_from_date,
            $filter_to_date
        );
    
        // load your views
        $this->load->view('layout/header', $data);
        $this->load->view('inventoryreport/investocklvl', $data);
        $this->load->view('layout/footer', $data);
    }
    
	function printInventoryReport() {
        if (!$this->rbac->hasPrivilege('class', 'can_view')) {
            access_denied();
        }
		        $setting_result = $this->setting_model->get();
                $data['settinglist'] = $setting_result;
				
         $data['receivedqnty']=$this->inventory_model->getInventoryQnty($query);  
         $data['soldqnty']=$this->inventory_model->getInventoryQntyIssued($query1);  
         $data['leftqnty']=$this->inventory_model->getInventoryQntyLeft($qnty_on_hand);  
        $item_result = $this->item_model->get();
        $data['itemlist'] = $item_result;
		$itemcategory = $this->itemissue_model->get();    																 																		 
        $data['itemissueList'] = $itemcategory;
         $this->load->view('print/printInventoryReport', $data);
																 
											
    }
  
}
?>