<?php
require_once ("secure_area.php");
class Home extends Secure_area 
{
	function __construct()
	{
		parent::__construct();	
                $this->load->helper('report');
	}
	
	function index()
	{
            
		$data['total_items']=$this->Item->count_all();
		$data['total_item_kits']=$this->Item_kit->count_all();
		$data['total_suppliers']=$this->Supplier->count_all();
		$data['total_customers']=$this->Customer->count_all();
		$data['total_employees']=$this->Employee->count_all();
		$data['total_locations']=$this->Location->count_all();
		$data['total_giftcards']=$this->Giftcard->count_all();
		$data['total_sales']=$this->Sale->count_all();
                
               
                
                $current_location_id = $this->session->userdata('employee_current_location_id');
                /*
                 * Sale Weekly Graph
                 */
               //if($current_location_id == 1){
                    $this->check_action_permission('view_sales');
                    //$start_date=rawurldecode("2017-03-22");
                    //$end_date=date('Y-m-d 23:59:59', strtotime(rawurldecode("2017-03-28")));
                    $start_date=date('Y-m-d', strtotime(' -6 day'));
                    $end_date=date('Y-m-d 23:59:59', strtotime(date('Y-m-d')));
                    $location_id = $this->session->userdata('employee_current_location_id');
                   
				    if(!$location_id){
                        $location_id = 1;
                    }
                    $sale_type = "all";
                    $this->load->model('reports/Summary_sales');
                    $model = $this->Summary_sales;
                    $model->setParams(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type));

                    if (!$this->Sale->create_sales_items_temp_table(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type)))
                    {
                            $this->load->view('reports/rate_limit');
                            return;
                    }
                    $this->load->model('reports/Summary_payments');
                    $model = $this->Summary_payments;
                    $model->setParams(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type));

                    if (!$this->Sale->create_sales_items_temp_table(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type)))
                    {
                            $this->load->view('reports/rate_limit');
                            return;
                    }

                    $data["title"]= "Sale Graph";
                    $data["graph_file"] =site_url("home/show_graph_sale/$start_date/$end_date/$sale_type/$location_id") ;
                    $data["graph_file_bar"] =site_url("home/show_graph_inStock/$start_date/$end_date/$sale_type/$location_id") ;
                    $data["graph_file_bar_highstock"] =site_url("home/show_graph_inStock_highstock/$start_date/$end_date/$sale_type/$location_id") ;
                   // $data["graph_file_bar_Stockout"] =site_url("home/show_graph_outStock/$start_date/$end_date/$sale_type/$location_id") ;
                    $data["subtitle"] = date(get_date_format(), strtotime($start_date)) .'-'.date(get_date_format(), strtotime($end_date));
                    $data["graph_file_receive"] =site_url("home/show_graph_receive/$start_date/$end_date/$sale_type/$location_id") ;
                    $data["graph_payment_due"] =site_url("home/show_payment_due/$start_date/$end_date/$sale_type/$location_id") ;
                    $data["graph_collect_payment"] =site_url("home/show_collect_payment/$start_date/$end_date/$sale_type/$location_id") ;

                /*
                 * End Graph
                 */
                
                
              // }
		
		$this->load->view("home",$data);
	}
        
        function set_employee_current_location_id()
	{
            $this->Employee->set_employee_current_location_id($this->input->post('employee_current_location_id'));
	}
        
        
        //The actual graph data
	function show_graph_sale($start_date, $end_date, $sale_type,$location_id)
	{
		$start_date=rawurldecode($start_date);
		$end_date=date('Y-m-d 23:59:59', strtotime(rawurldecode($end_date)));
		
		$this->load->model('reports/Summary_sales');
		$model = $this->Summary_sales;
		$model->setParams(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type));

		if (!$this->Sale->create_sales_items_temp_table(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type)))
		{
			$this->load->view('reports/rate_limit');
			return;
		}
		$report_data = $model->getData();
                
		$graph_data = array();
		foreach($report_data as $row)
		{
			$graph_data[strtotime($row['sale_date'])]= $row['total'];
		}

		$data = array(
			"title" => lang('reports_sales_summary_report'),
			"data" => $graph_data
		);
                
               
		$this->load->view("home/graphs/sale_line",$data);

	}
        
         //The actual graph data
	function show_graph_receive($start_date, $end_date, $sale_type,$location_id)
	{
		$start_date=rawurldecode($start_date);
		$end_date=date('Y-m-d 23:59:59', strtotime(rawurldecode($end_date)));
		
		$this->load->model('reports/Summary_sales');
		$model = $this->Summary_sales;
		$model->setParams(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type));

		if (!$this->Sale->create_sales_items_temp_table(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type)))
		{
			$this->load->view('reports/rate_limit');
			return;
		}
		$report_data = $model->getData();

		$graph_data = array();
		foreach($report_data as $row)
		{
			$graph_data[strtotime($row['sale_date'])]= $row['total'];
		}

		$data = array(
			"title" => lang('reports_sales_summary_report'),
			"data" => $graph_data
		);
                
               
		$this->load->view("home/graphs/receive_line",$data);

	}
        
         //The actual graph data stockbar
	function show_graph_inStock_highstock($start_date, $end_date, $sale_type,$location_id)
	{
		$start_date=rawurldecode($start_date);
		$end_date=date('Y-m-d 23:59:59', strtotime(rawurldecode($end_date)));
		
		$this->load->model('reports/Summary_items');
		$model = $this->Summary_items;
                
		$model->setParams(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type));
                
		$report_data = ($model->getStock_high($location_id));
		//(krsort($report_data));
		
		$graph_data = array();
		foreach($report_data as $row)
		{
			$graph_data[('<font color="#FF0000">('.number_format($row['quantity'],0).')</font><br>'.$row['product_id'])]= $row['quantity'];
		}
                
		$data = array(
			"title" => lang('reports_sales_summary_report'),
			"data" => $graph_data
		);
                
		$this->load->view("home/graphs/stockbar",$data);

	}
        
         //The actual graph data stockbar
	function show_graph_inStock($start_date, $end_date, $sale_type,$location_id)
	{
		$start_date=rawurldecode($start_date);
		$end_date=date('Y-m-d 23:59:59', strtotime(rawurldecode($end_date)));
		
		$this->load->model('reports/Summary_items');
		$model = $this->Summary_items;
                
		$model->setParams(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type));
                
		$report_data = $model->getStock($location_id);
		
		$graph_data = array();
		foreach($report_data as $row)
		{
			$graph_data[('<font color="#FF0000">('.number_format($row['quantity'],0).')</font><br>'.$row['product_id'])]= $row['quantity'];
		}
                
		$data = array(
			"title" => lang('reports_sales_summary_report'),
			"data" => $graph_data
		);
                
               
		$this->load->view("home/graphs/bar",$data);

	}
        //The actual graph data 
	function show_graph_outStock($start_date, $end_date, $sale_type,$location_id)
	{
		$start_date=rawurldecode($start_date);
		$end_date=date('Y-m-d 23:59:59', strtotime(rawurldecode($end_date)));
		
		$this->load->model('reports/Summary_items');
		$model = $this->Summary_items;
                
		$model->setParams(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type));
                
		$report_data = $model->getStockOut($location_id);
		$graph_data = array();
		foreach($report_data as $row)
		{
			$graph_data[($row['name'])]= $row['quantity'];
		}
                
		$data = array(
			"title" => lang('reports_sales_summary_report'),
			"data" => $graph_data
		);
                
               
		$this->load->view("home/graphs/stockbar2",$data);

	}
        
        function show_payment_due ($start_date, $end_date, $sale_type,$location_id){
            $start_date=rawurldecode($start_date);
            $end_date=date('Y-m-d 23:59:59', strtotime(rawurldecode($end_date)));
            $this->load->model('reports/Summary_items');
            $model = $this->Summary_items;

            $model->setParams(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type));

            $report_data = $model->getPyamentDue($location_id);
            $graph_data = array();
            foreach($report_data as $row)
            {
                    $graph_data[(number_format($row['payment_amount'],2).'<br>'.$row['first_name'])]= $row['payment_amount'];
            }

            $data = array(
                    "title" => lang('reports_sales_summary_report'),
                    "data" => $graph_data
            );
                
               
		$this->load->view("home/graphs/payment_due",$data);
        }
        
        
        
        
        function show_collect_payment($start_date, $end_date, $sale_type,$location_id){
            
            $start_date=rawurldecode($start_date);
            $end_date=date('Y-m-d 23:59:59', strtotime(rawurldecode($end_date)));
            $this->load->model('reports/Summary_items');
            $model = $this->Summary_items;

            $model->setParams(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type));

            $report_data = $model->getCollectedPyament($location_id);
            $graph_data = array();
            foreach($report_data as $row)
            {
                    $graph_data[($row['first_name'])]= $row['payment_amount'];
            }

            $data = array(
                    "title" => lang('reports_sales_summary_report'),
                    "data" => $graph_data
            );
                
               
		$this->load->view("home/graphs/collect_payment",$data);
            
        }
	function logout()
	{
		$this->Employee->logout();
	}
}
?>