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

class Sales_Return extends CI_Controller {

    var $main_menu_name = "sales";
	var $sub_menu_name = "sales_return";

	public function __construct()
	{
		parent::__construct();

		$this->load->model('Sales_Model');
		$this->load->model('Sales_Return_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('Sequerty_Model');
		$this->load->model('User_Model');
		$this->load->model('Purchases_Model');
		
	}
	
	//Sales reterun list page
	public function index()
	{
		$data['sales'] = array();//$this->Sales_Model->get_all_sales();
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = $this->sub_menu_name;
		$data['warehouse_list'] = $this->Warehouse_Model->get_all_warehouse();
        $this->load->view('sales_return',$data);
	}
	
	public function help()
	{
		$data=array();
		//echo "222222";
		 $this->load->view('help_pos',$data);
		 //echo "Page Short Cut <br/> Add Item-F1 <br/> Quantity field to Discount Field-Tab <br/> Selling Price-F2";
	}

	public function invoice_print()
	{
		$sl_rtn_id=$this->uri->segment('3');
		$data['sale_rtn_details']= $this->Sales_Return_Model->get_sale_return_info($sl_rtn_id);
		//get sale item list
		$data['sale_rtn_item_list']= $this->Sales_Return_Model->get_sale_return_item_list($sl_rtn_id);
		
		$data['customer_details']= $this->Customer_Model->get_customer_info($data['sale_rtn_details']['customer_id']);
		$data['warehouse_details']=  $this->Warehouse_Model->get_warehouse_info($data['sale_rtn_details']['warehouse_id']);
		//$data['sale_payments_list']= $this->Sales_Model->get_sale_payments_by_sale_id($sale_id);
		
		
		$data['cr_limit_list'] = $this->Common_Model->get_all_cr_limit();
        $this->load->view('models/print_sales_return_invoice',$data);
	}
		
	//Sales reterun get sold qty
	public function get_avalable_product_qty_for_return()
    {	
		$product_id=$this->input->get('product_id');
		$warehouse_id=$this->input->get('warehouse_id');
		$sale_id=$this->input->get('sale_id');
		$qty=$this->input->get('qty');
		$saleqty=$this->Sales_Return_Model->get_avalable_product_qty_for_return($product_id,$warehouse_id,$sale_id);
		$salereturnqty=$this->Sales_Return_Model->get_sales_return_product_qty($product_id,$warehouse_id,$sale_id);
		$totalRemaningQty=$saleqty-$salereturnqty;
		//echo "qty:$qty , remmnaingQty:$saleqty , salereturnqty:$salereturnqty";
		if($qty<=$totalRemaningQty){
			$remmnaingQty=$saleqty;
		}else {
			$remmnaingQty=0;
		}
		
		echo json_encode(array('remmnaingQty'=>$remmnaingQty));
	}
	
	//Add sales return form
    public function sales_return_add()
    {
		$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->Sales_Model->get_sale_item_list_by_sale_id($sale_id);
		$data['sale_details']= $this->Sales_Model->get_sale_info($sale_id);
		$data['sale_id']=$sale_id;
		//get suppliers list
		$data['suppliers'] = $this->Supplier_Model->get_all_supplier();
		$data['warehouse_list'] = $this->Warehouse_Model->get_all_warehouse();
		$data['tax_rates_list'] = $this->Tax_Rates_Model->get_all_tax_rates();
		$data['customer_list'] = $this->Customer_Model->get_all_customers();
		$data['status_list'] = $this->Common_Model->get_all_status();
		
        $this->load->view('sales_return_add',$data);
    }
	
	
	//Save sales return
	//Insert data to payment table with type 'sales_return'
	public function save_sales_return()
	
	{
		$rowCount=$this->input->post('rowCount');
		$sl_rtn_datetime_1=$this->input->post('sl_rtn_datetime');
		$sl_rtn_datetime=date('Y-m-d H:i:s', strtotime($sl_rtn_datetime_1));
		$user_id=$this->session->userdata('ss_user_id');
		$warehouse_id=$this->input->post('warehouse_id');
		$add_new_sale=$this->input->post('add_new_sale');
		$return_by=$this->input->post('return_by');
		//echo "add_new_sale:$add_new_sale";
		//die();
		
		$error='';
		$disMsg='';
		$lastid='';
		$sl_rtn_id='';
		
		//sl_rtn_reference_no generate 
		//start get next referance no
		$warehouse_dtls=$this->Warehouse_Model->get_warehouse_info($warehouse_id);
		$warehouse_code=$warehouse_dtls['code'];
		$warehouse_last_id=(1+$this->Common_Model->get_next_warehouse_ref_no($warehouse_id,'warehouse_id','warehouse_last_id','sales_return'));
		$sl_rtn_reference_no=$warehouse_code.'/SR/'.sprintf("%04d",$warehouse_last_id);
		//echo "sl_rtn_reference_no:".$sl_rtn_reference_no;
		//exit();
		//end get next referance no
		
		
		//$sl_rtn_reference_no=
		
		/* payment validation start */
		$sale_pymnt_amount=$this->input->post('sale_pymnt_amount');
		$sale_id=$this->input->post('sale_id');
		$cost_total=$this->input->post('cost_total');
		
		$sale_pymnt_ref_no=$this->input->post('sale_pymnt_ref_no');
		
		$sale_pymnt_paying_by=$this->input->post('sale_pymnt_paying_by');
		$sale_pymnt_date_time=$sl_rtn_datetime;
		$sale_pymnt_date_time_send=date('Y-m-d H:i:s', strtotime($sale_pymnt_date_time));
		$sale_pymnt_cheque_no=$this->input->post('sale_pymnt_cheque_no');
		$sale_pymnt_crdt_card_no=$this->input->post('sale_pymnt_crdt_card_no');
		$sale_pymnt_crdt_card_holder_name=$this->input->post('sale_pymnt_crdt_card_holder_name');
		$sale_pymnt_crdt_card_month=$this->input->post('sale_pymnt_crdt_card_month');
		$sale_pymnt_crdt_card_year=$this->input->post('sale_pymnt_crdt_card_year');
		$sale_pymnt_crdt_card_type=$this->input->post('sale_pymnt_crdt_card_type');
		
		$sale_pymnt_note=$this->input->post('sale_pymnt_note');
		$user_id=$this->session->userdata('ss_user_id');
		$sale_pymnt_added_date_time=date("Y-m-d H:i:s");
		$sale_pymnt_id='';
		
        $this->load->library('form_validation'); //form validation lib
        $this->form_validation->set_rules('sale_pymnt_amount', 'Amount', 'required');
		if($sale_pymnt_paying_by=='Credit Card'){
			$this->form_validation->set_rules('sale_pymnt_crdt_card_type', 'Card Type', 'required');
			$this->form_validation->set_rules('sale_pymnt_crdt_card_no', 'Credit Card No', 'required');
			$this->form_validation->set_rules('sale_pymnt_crdt_card_holder_name', 'Holder Name', 'required');
			$this->form_validation->set_rules('sale_pymnt_crdt_card_month', 'Month', 'required');
			$this->form_validation->set_rules('sale_pymnt_crdt_card_year', 'Year', 'required');
		}
		if($sale_pymnt_paying_by=='Cheque'){
			$this->form_validation->set_rules('sale_pymnt_cheque_no', 'Cheque No', 'required');
		}
		$this->form_validation->set_rules('sale_id', 'System Error', 'required');
		
		if($sale_pymnt_amount){
		$tmp=number_format($sale_pymnt_amount, 2, '.','');
		}else{
			$tmp=0;
		}
		//echo $tmp;
        if ($this->form_validation->run() == FALSE)
        {
           $st = array('status' =>0,'validation' => validation_errors());
		   $disMsg=validation_errors();
		   $error=true;
        }
		//else if($tmp!=$this->input->post('total_paid_amount')){
		//	$error=true;
		//	$disMsg='Invalid Amount';
		//}
		/* end payment validation start */
		
		
		
		
		if(!$error){
			$data=array(
				'sl_rtn_reference_no'=>$sl_rtn_reference_no,
				'warehouse_last_id'=>$warehouse_last_id,
				'sale_id'=>$this->input->post('sale_id'),
				'customer_id'=>$this->input->post('customer_id'),
				'warehouse_id'=>$warehouse_id,
				'sl_rtn_datetime'=>$sl_rtn_datetime,
				'sl_rtn_inv_discount'=>$this->input->post('sl_rtn_inv_discount'),
				'sl_rtn_total'=>$this->input->post('sl_rtn_total'),
				'sl_rtn_datetime_created'=>date('Y-m-d H:i:s'),
				'sl_rtn_inv_discount_amount'=>$this->input->post('sl_rtn_inv_discount_amount'),
				'sl_rtn_note'=>$this->input->post('sl_rtn_note'),
				'cost_total'=>$cost_total,
				'user_id'=>$user_id,
				'return_by'=>$return_by
			);
			$_insert=$this->Sales_Return_Model->save_sales_return($data,$sl_rtn_id);
			$lastid=$this->db->insert_id();
			$sl_rtn_id=$lastid;
			$disMsg='Sale return successfully added';
			
			//insert sale item data
			$row=$this->input->post('row');
			$rowCount=$this->input->post('rowCount');
			//print_r($row);
			$data_item=array();
			
			//echo "rowCount:$rowCount";
			for($i=1; $i<=$rowCount; $i++){
				if(isset($row[$i]['product_id'][0]))
				{
					
				$data_item=array(
					'sl_rtn_id'=>$sl_rtn_id,
					'product_id'=>$row[$i]['product_id'][0],
					'quantity'=>$row[$i]['qty'][0],
					'discount'=>$row[$i]['discount'][0],
					'unit_price'=>$row[$i]['unit_price'][0],
					'item_cost'=>$row[$i]['item_cost'][0],
					'discount_val'=>$row[$i]['discount_val'][0], 
					'gross_total'=>$row[$i]['gross_total'][0]
				);
				$slrtnitmid=$this->Sales_Return_Model->save_sales_return_item($data_item);
				$slrtnitmid=$this->db->insert_id();
				
				
				//add record for f4 table
				
				//check serial no exists
				if($row[$i]['product_sn'][0])
				{
				//add serial numbers
				//exploid serial numbers
					$pieces_sn = explode(",", $row[$i]['product_sn'][0]);
					for($h=0; $h<count($pieces_sn); $h++){
						$sn_data=array(
							'product_id'=>$row[$i]['product_id'][0],	
							//'product_w'=>$row[$i]['product_w'][0],	
							'sl_rtn_id'=>$sl_rtn_id,
							'sale_rtn_item_id'=>$slrtnitmid,
							'sris_number'=>$pieces_sn[$h],
						);
				  		$this->Sales_Return_Model->save_sale_return_items_serial_numbers($sn_data);
						
						
				//update grn serial number sold status
				$pis_number=$pieces_sn[$h];
				$sn_data=array(	
					'sale_inv_id'=>0,
					'pis_sold'=>0,
					'product_avalable'=>1,
					'avalable_warehouse_id'=>$this->input->post('warehouse_id'),

				);
				$this->Purchases_Model->update_grn_serial_numbers($sn_data,$pis_number);
				
						
					}
                    //end add serial numbers
				}
				
				/*
				$type='sl_rtn';
				$ref_id=$slrtnitmid;
				$product=$row[$i]['product_id'][0];
				$quantity=$row[$i]['qty'][0];
				$unit_cost=$row[$i]['unit_price'][0];
				$this->Common_Model->add_fi_table($type,$ref_id,$product,$quantity,$unit_cost);
				*/
				}
			}
			
			
			
			
				
				if(!empty($pis_number)){
				//insert user activity
				$this->Common_Model->add_user_activitie("Added Sales Return, (Serial No:$pis_number)");
				}
				
				
				$shift_date=date("Y-m-d");
$active_shift_des=$this->Common_Model->get_active_shift_by_warehouse_id_date($warehouse_id,$shift_date);

//print_r($active_shift_des);

if(!count($active_shift_des)){
						 redirect(base_url("sales/error"));
						    die();
						}else{
						    $acctrnss_id=$active_shift_des['acctrnss_id'];
						}
				
				
				//add sales return payments cash
				$data=array(
				'sale_pymnt_amount'=>$sale_pymnt_amount,	
				'sale_pymnt_ref_no'=>$sale_pymnt_ref_no,
				'sale_pymnt_paying_by'=>$sale_pymnt_paying_by,
				'sale_pymnt_date_time'=>$sale_pymnt_date_time_send,
				'sale_pymnt_note'=>$sale_pymnt_note,
				'user_id'=>$user_id,
				'sale_id'=>$sl_rtn_id,
				'sale_pymnt_added_date_time'=>date('Y-m-d H:i:s'),
			//	'sale_pymnt_cheque_no'=>$sale_pymnt_cheque_no,
			//	'sale_pymnt_crdt_card_no'=>$sale_pymnt_crdt_card_no,
			//	'sale_pymnt_crdt_card_holder_name'=>$sale_pymnt_crdt_card_holder_name,
				//'sale_pymnt_crdt_card_type'=>$sale_pymnt_crdt_card_type,
			//	'sale_pymnt_crdt_card_month'=>$sale_pymnt_crdt_card_month,
			//	'sale_pymnt_crdt_card_year'=>$sale_pymnt_crdt_card_year,
				'sale_payment_type' => 'sales_return',
					'acctrnss_id_p'=>$acctrnss_id,
			);
				
					if($sale_pymnt_amount){
               if ($this->Sales_Model->save_sale_payments($data,$sale_pymnt_id)) {
                   
               } else {
                    $error=true;
               }
					}
				//end add return payments cash
				
				
					/* start 2nd payment */
				$sale_pymnt_amount_2=$this->input->post('sale_pymnt_amount_2');
		        $sale_pymnt_paying_by_2=$this->input->post('sale_pymnt_paying_by_2');
				

				$data=array(
				'sale_pymnt_amount'=>$sale_pymnt_amount_2,	
			//	'sale_pymnt_ref_no'=>$sale_pymnt_ref_no,
				'sale_pymnt_paying_by'=>$sale_pymnt_paying_by_2,
				'sale_pymnt_date_time'=>$sale_pymnt_date_time_send,
				'credit_card_last_digits'=>$this->input->post('credit_card_last_digits'),
				'user_id'=>$this->session->userdata('ss_user_id'),
				'sale_id'=>$sale_id,
				'sale_pymnt_added_date_time'=>date("Y-m-d H:i:s"),
				'sale_payment_type' => 'sales_return',
				'sale_pymnt_crdt_card_type'=>$this->input->post('sale_pymnt_crdt_card_type'),
			);
			
			
			//print_r($data);
			if($sale_pymnt_amount_2)
			{
               if ($this->Sales_Model->save_sale_payments($data,'')) { }
			}
				/* end 2nd payment */
				
				
				
				/* start 3nd payment */
				$sale_pymnt_amount_3=$this->input->post('sale_pymnt_amount_3');
				$bank_id=$this->input->post('bank_id');
				$sale_pymnt_paying_by_3=$this->input->post('sale_pymnt_paying_by_3');
				$rea_date=date('Y-m-d H:i:s', strtotime($this->input->post('rea_date')));
				$data=array(
				'sale_pymnt_amount'=>$sale_pymnt_amount_3,	
				'bank_id'=>$bank_id,
				'sale_pymnt_paying_by'=>$sale_pymnt_paying_by_3,
				'sale_pymnt_date_time'=>$rea_date,
				'sale_pymnt_cheque_no'=>$this->input->post('sale_pymnt_cheque_no'),
				'user_id'=>$this->session->userdata('ss_user_id'),
				'sale_id'=>$sale_id,
				'sale_pymnt_added_date_time'=>date("Y-m-d H:i:s"),
				'sale_payment_type' => 'sales_return',
				//'sale_pymnt_crdt_card_type'=>$this->input->post('sale_pymnt_crdt_card_type'),
			);
			
			
			//print_r($data);
			if($sale_pymnt_amount_3)
			{
               if ($this->Sales_Model->save_sale_payments($data,'')) { }
			}
				/* end 3nd payment */
				
				
		
		}else {
			$error=true;
		}
		
						
		echo json_encode(array('sale_id'=>$lastid,'error'=>$error,'disMsg'=>$disMsg,'add_new_sale'=>$add_new_sale));
		
	}	

	//Sales return details page
	public function sales_return_details()
	{
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = '';
		
		//get sale id
		$sl_rtn_id=$this->uri->segment('3');
		$data['sale_rtn_item_list']= $this->Sales_Return_Model->get_sale_return_item_list($sl_rtn_id);
		$data['sale_rtn_details']= $this->Sales_Return_Model->get_sale_return_info($sl_rtn_id);
		$data['total_paid_amount']=$this->Sales_Model->get_total_paid_by_sale_id($sl_rtn_id);
		$data['customer_details']= $this->Customer_Model->get_customer_info($data['sale_rtn_details']['customer_id']);
		$data['warehouse_details']= $this->Warehouse_Model->get_warehouse_info($data['sale_rtn_details']['warehouse_id']);
		$data['sale_payments_list']= $this->Sales_Model->get_sale_return_payments_by_sale_id($sl_rtn_id);
		$data['sl_rtn_id']=$sl_rtn_id;
        $this->load->view('sales_return_details',$data);
	}	
	
	//Sales return details list 
	public function list_sales_return()
	{
		$requestData= $_REQUEST;
		$search_key=$this->input->get('search');
	$warehouse_id=$this->input->get('warehouse_id');

	$search_key_val=$search_key['value'];
	$start=$this->input->get('start');
	$length=$this->input->get('length');
		$data = array();
		//$sales = $this->Sales_Return_Model->get_all_sales_return();
		$sales_tot = $this->Sales_Return_Model->get_all_sales_return("","","","","",$warehouse_id);
		$sales = $this->Sales_Return_Model->get_all_sales_return($start,$length,$search_key_val,"","",$warehouse_id);
		$totalData =count($sales_tot);
		$totalFiltered = $totalData;  
		
		foreach ($sales as $row){
			$nestedData=array(); 
			$sl_rtn_id=$row['sl_rtn_id'];
			$sale_id=$row['sale_id'];
			$total_paid_amount=$this->Sales_Model->get_total_paid_by_sale_return_id($sl_rtn_id);
			$nestedData[] =display_date_time_format($row['sl_rtn_datetime']);
			$nestedData[] = $row['sl_rtn_reference_no'];
			$nestedData[] = $row['sale_reference_no'];
			$nestedData[] = $row['cus_name'];
		//	$nestedData[] = number_format($row['sls_rtn_total_paid'], 2, '.', ',');
		
			$nestedData[] = $total_paid_amount;//number_format($row['sls_rtn_total_paid'], 2, '.', ',');
			
			
			$nestedData[] = '<div class="btn-group text-left">
								<button data-toggle="dropdown" class="btn btn-default btn-xs btn-primary dropdown-toggle" type="button">Actions <span class="caret"></span></button>
								<ul role="menu" class="dropdown-menu pull-right">
								<li><a href="'.base_url().'sales/sales_return_details/'.$sl_rtn_id.'"><i class="fa fa-file-text-o"></i> Return Sale Details</a></li>
								</ul></div>';
	
			$data[] = $nestedData;
	}
	
		$json_data = array(
				"recordsTotal"    => intval( $totalData ),  
				"recordsFiltered" => intval( $totalFiltered ),
				"data"            => $data 
				);
	
		echo json_encode($json_data); 
	}
}