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

class Specical_Permission extends CI_Controller {

    var $main_menu_name = "specical_permission";
	var $sub_menu_name = "specical_permission";

	public function __construct()
	{
		parent::__construct();
		
		
		$this->load->model('Route_Model');
		$this->load->model('Specical_Permission_Model');
		$this->load->model('Supplier_Model');
		$this->load->model('Warehouse_Model');
		$this->load->model('Common_Model');
		$this->load->model('Tax_Rates_Model');
		$this->load->model('Customer_Model');
		$this->load->model('Sales_Return_Model');
		$this->load->model('User_Model');
		$this->load->model('Transfer_Model');
		$this->load->model('Purchases_Model');
		$this->load->model('Product_Damage_Model');
		$this->load->model('Sales_Model');
		
	}
	
	//Sales list page load
	public function index()
	{
		
		//$data['sales'] = $this->Specical_Permission_Model->get_all_sales();
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = $this->sub_menu_name;
        $this->load->view('delete_invoice',$data);
	}	

	

	public function cancelled_invoices()
	{
		
		//$data['sales'] = $this->Specical_Permission_Model->get_all_sales();
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = 'specical_permission_cancel';
        $this->load->view('deleted_invoices',$data);
	}
	
	public function delete_invoice()
	{
		$sale_id = $this->input->get('id');

		$master_data=$this->Specical_Permission_Model->get_all_sale_data($sale_id);
		$item_data=$this->Specical_Permission_Model->get_all_item_data($sale_id);
		$total_paid_amount=$this->Sales_Model->get_total_paid_by_sale_id($sale_id);
		$result= $this->Specical_Permission_Model->add_calncell_mastertable($master_data);
		foreach($item_data as $row){
			$result= $this->Specical_Permission_Model->add_calncell_itemtable($row);			
		}
		$result=$this->Specical_Permission_Model->delete_invoice($sale_id);
		$result=$this->Specical_Permission_Model->delete_invoice_items($sale_id);
		echo $result;		
	}
	
	public function delete_payments()
	{
		$payment_id=$this->input->get('id');
	 	$result= $this->Specical_Permission_Model->delete_payments($payment_id);
	 
	}
	
	//Sales details view
	public function view()
	{
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = '';
		
		//get sale id
		$sale_id=$this->uri->segment('3');
		$data['sale_item_list']= $this->Specical_Permission_Model->get_sale_item_list_by_sale_id($sale_id);
		$data['sale_details']= $this->Specical_Permission_Model->get_sale_info($sale_id);
		
		$data['total_paid_amount']=$this->Specical_Permission_Model->get_total_paid_by_sale_id($sale_id);
		
		$data['customer_details']= $this->Customer_Model->get_customer_info($data['sale_details']['customer_id']);
		$data['warehouse_details']= $this->Warehouse_Model->get_warehouse_info($data['sale_details']['warehouse_id']);		
		$data['sale_payments_list']= $this->Specical_Permission_Model->get_sale_payments_by_sale_id($sale_id);
		
		$old_payment_tot=0;
		$retured_payment_tot=0;
		$retured_payment_msg_this='';
		$old_payments_dis_msg_this='';
		
		//check return payments
		$return_sales_details=$this->Sales_Return_Model->get_return_sale_info_sale_id($sale_id);
		
		
		foreach ($return_sales_details as $row)
		{
	 		//echo "sale id:$row->sale_id";
			//echo "sale_total:$row->sale_total";

				$this_balance_pament=0;
				$this_trn_amt=$row->sl_rtn_total;
				$retured_payment_tot=$retured_payment_tot+$this_trn_amt;

				$retured_payment_msg_this=$retured_payment_msg_this.' -'.$this_trn_amt.' ,';
			
			
 		}
		
		
		$old_payment_tot=$old_payment_tot-$retured_payment_tot;
		
		$data['old_payments']=$old_payment_tot;
		$data['old_payments_dis_msg']="Return Total Amount ($old_payments_dis_msg_this $retured_payment_msg_this)";
		
		$data['sale_id']=$sale_id;
        $this->load->view('delete_sale_payments',$data);
	}

	
	//Sales payment page 
	public function payments()
	{
        $data['sale_id'] = $this->input->get('id');
        $data['sale_type'] = $this->input->get('sale_type');
        $this->load->view('models/sales_payment',$data);	
	}
	public function get_old_credit_amount()
	{
		$cus_id=$this->input->get('cus_id');
		$invocie_data=$this->Sales_Model->get_all_sales_bu_cus_id($cus_id);
		$balance_sub_tot_each=0;
		if(count($invocie_data)){ 
	$balance_sub_tot=0;
	 
	 	
foreach ($invocie_data as $inv)
 {
	 
	 $total_paid_amount=$this->Sales_Model->get_total_paid_by_sale_id($inv->sale_id);
	 $balance_h=0;
	 $balance_h=$inv->sale_total-$total_paid_amount;
	 

	 

	
	if($balance_h){
			$balance_sub_tot_each+=$balance_h;
			
			//reduse this sale return amount
			$sale_pymnt_ruturn_amount=0;
			$sale_pymnt_ruturn_amount=$this->Sales_Return_Model->get_total_return_by_sale_id($inv->sale_id);
			$balance_sub_tot_each-=$sale_pymnt_ruturn_amount;
	}
	 //end check date range
 }
		}
		//$balance_sub_tot_each=500;
		//echo $balance_sub_tot_each;
	//	$old_credit_amount=$balance_sub_tot_each;
		echo json_encode(array('old_credit_amount'=>$balance_sub_tot_each));
	}
	
	
	public function check_recod()
	{
		 $CRN = $this->input->get('CRN');
		$query = $this->Sales_Model->get_count($CRN);
      	$resulttemp  		= $query->row();
		 $result=$resulttemp->counts;
		
		echo json_encode($result);
		
	}
	
	
	//Sales save 
	//Sales item save
	//Add sales items to 54 table
	

	
	
	
	
	
	
	public function sale_details()
	{
		$sale_type = 0;
		$sale_id=$this->input->get('sale_id');
		$sale_type = $this->input->get('type');
		$data['sale_details']= $this->Sales_Model->get_sale_info($sale_id);
		$data['sale_type'] = $sale_type;
		//get sale item list
		$data['sale_item_list']= $this->Sales_Model->get_sale_item_list_by_sale_id($sale_id);
		
		$data['customer_details']= $this->Customer_Model->get_customer_info($data['sale_details']['customer_id']);
		$data['warehouse_details']= $this->Warehouse_Model->get_warehouse_info($data['sale_details']['warehouse_id']);
		$data['sale_payments_list']= $this->Sales_Model->get_sale_payments_by_sale_id($sale_id);
		
		//get old payments amounts
		$cus_sales_details=$this->Sales_Model->get_sale_info_by_customer_id($data['sale_details']['customer_id']);
		//print_r($cus_sales_details);
		$old_payment_tot=0;
		$retured_payment_tot=0;
		$retured_payment_msg_this='';
		$old_payments_dis_msg_this='';
		
		foreach ($cus_sales_details as $row)
		{
	 		
			if($row->sale_id!=$sale_id){
			//get paid amount
			$paid_amount=$this->Sales_Model->get_total_paid_by_sale_id($row->sale_id);
			if($row->sale_total!=$paid_amount){
				
			}
			}
 		}
		
		//check return payments
		$return_sales_details=$this->Sales_Return_Model->get_return_sale_info_sale_id($sale_id);
		
		
		foreach ($return_sales_details as $row)
		{
	 		//echo "sale id:$row->sale_id";
			//echo "sale_total:$row->sale_total";

				$this_balance_pament=0;
				$this_trn_amt=$row->sl_rtn_total;
				$retured_payment_tot=$retured_payment_tot+$this_trn_amt;

				$retured_payment_msg_this=$retured_payment_msg_this.' -'.$this_trn_amt.' ,';
			
			
 		}
		
		
		$old_payment_tot=$old_payment_tot-$retured_payment_tot;
		
		$data['old_payments']=$old_payment_tot;
		$data['old_payments_dis_msg']="Return Total Amount ($old_payments_dis_msg_this $retured_payment_msg_this)";
		
		
		$data['cr_limit_list'] = $this->Common_Model->get_all_cr_limit();
        $this->load->view('models/view_sales',$data);
	}	
	
	//Sales list
	public function list_sales()
	{
	$requestData= $_REQUEST;
		
	$start=$this->input->get('start');
	$length=$this->input->get('length');
	$search=$this->input->get('search');
	
	$count = $this->Specical_Permission_Model->get_all_sales("","",$search);
	
	$data = array();
	$sales = $this->Specical_Permission_Model->get_all_sales($start,$length,$search);
	$totalData = $count;
	$totalFiltered = $totalData;  
	
	foreach ($sales as $row){
		$nestedData=array(); 
		$sale_id=$row['sale_id'];
		$total_paid_amount=0;
		$total_paid_amount=$this->Specical_Permission_Model->get_total_paid_by_sale_id($sale_id);
		
		
		$return_tot=$this->Sales_Return_Model->get_relevant_return_by_sale_id($sale_id);
		
		
		if(isset($return_tot['sl_rtn_total']))
		{
		$return_tot_amt=$return_tot['sl_rtn_total'];
		}
		else
		{
			$return_tot_amt=0;
		}
		
		if(isset($return_tot['sls_rtn_total_paid']))
		{
		$return_tot_paid=$return_tot['sls_rtn_total_paid'];
		}
		else
		{
			$return_tot_paid=0;
		}
		
		
		
		
		$to_be_paid=$row['sale_total']-$return_tot_amt;
		$nestedData[] =display_date_time_format($row['sale_datetime']);
		$nestedData[] = $row['sale_reference_no']." / ".$row['card_ref_number'];
		$nestedData[] = $row['cus_name'];
		$nestedData[] = $row['sale_total'];
		$nestedData[] = $return_tot_amt;
		$nestedData[] = $to_be_paid;
		$nestedData[] = $total_paid_amount;
		$invoice_balance=$total_paid_amount-$return_tot_paid;
		$nestedData[] = $to_be_paid-$invoice_balance;
		
		if (empty($total_paid_amount)) {
		  $pay_st = '<span class="label label-success">Pending</span>';
		  $action='<div class="btn-group text-left">
                            <button data-toggle="dropdown" class="btn btn-default btn-xs btn-success dropdown-toggle" type="button">Actions <span class="caret"></span></button>
                            <ul role="menu" class="dropdown-menu pull-right">
                            <li><a href="'.base_url().'specical_permission/view/'.$sale_id.'"><i class="fa fa-file-text-o"></i> Sale Details</a></li>
                           <li><a onClick="delete_invoice('.$row['sale_id'].')" data-toggle="modal" href="#" data-placement="top" data-original-title="Edit suppliers"><i class="fa fa-print"></i> Cancel Invoice</a></li>
							 
                            </ul></div>';
		}else{
		  if ($total_paid_amount >= $to_be_paid) {
			$pay_st = '<span class="label label-warning">Paid</span>';
			$action='<div class="btn-group text-left">
                            <button data-toggle="dropdown" class="btn btn-default btn-xs btn-warning dropdown-toggle" type="button">Actions <span class="caret"></span></button>
                            <ul role="menu" class="dropdown-menu pull-right">
                            
                            <li><a href="'.base_url().'specical_permission/view/'.$sale_id.'"><i class="fa fa-file-text-o"></i> Sale Details</a></li>
							 
                            </ul></div>';
			
		  }else{
			$pay_st = '<span class="label label-warning">Partial</span>';
			$action='<div class="btn-group text-left">
                            <button data-toggle="dropdown" class="btn btn-default btn-xs btn-warning dropdown-toggle" type="button">Actions <span class="caret"></span></button>
                            <ul role="menu" class="dropdown-menu pull-right">
                           
                             <li><a href="'.base_url().'specical_permission/view/'.$sale_id.'"><i class="fa fa-file-text-o"></i> Sale Details</a></li>
							 
                            </ul></div>';
		  }
		}
		
		 if ($return_tot_amt>0) 
		 {
			$pay_st = '<span class="label label-danger">Returned</span>';
			$action='<div class="btn-group text-left">
                            <button data-toggle="dropdown" class="btn btn-default btn-xs btn-danger dropdown-toggle" type="button">Actions <span class="caret"></span></button>
                            <ul role="menu" class="dropdown-menu pull-right">
                            <li><a href="'.base_url().'specical_permission/view/'.$sale_id.'"><i class="fa fa-file-text-o"></i> Sale Details</a></li>
                           
							 
                            </ul></div>';
		 }
		
		$nestedData[]=$pay_st;
		
		
		
	
	 if ($return_tot_amt>0) 
		 {
			$nestedData[] =$action; 
		 }
	 else
		 {
			 
			$nestedData[] = $action; 
		 }
	
	
	
	$data[] = $nestedData;
}

	$json_data = array(
			//"draw"            => intval( $requestData['draw'] ),  
			"recordsTotal"    => intval( $totalData ),  
			"recordsFiltered" => intval( $totalFiltered ),
			"data"            => $data 
			);

	echo json_encode($json_data); 
	}

	public function get_all_sales_deleted()
	{
	
	$data = array();
	$sales = $this->Specical_Permission_Model->get_all_sales_deleted();

	
	foreach ($sales as $row){
		$nestedData=array(); 
		$sale_id=$row['sale_id'];
		
		$to_be_paid=$row['sale_total'];
		$nestedData[] =display_date_time_format($row['sale_datetime']);
		$nestedData[] = $row['sale_reference_no']." / ".$row['card_ref_number'];
		$nestedData[] = $row['cus_name'];
		$nestedData[] = $row['sale_inv_gross_total'];
		$nestedData[] =$row['sale_total'];
		$nestedData[] =$row['sale_inv_discount_amount'];
		$nestedData[] =$row['cost_total'];
		$nestedData[] =$row['due_date'];
		$nestedData[] =$row['delete_date_time'];
		$nestedData[] ='';
		$data[] = $nestedData;
	}	
		$json_data = array(
				"data"            => $data 
				);
	
		echo json_encode($json_data); 

}
	







}