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

class Tmp_Sl extends CI_Controller {

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

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

		$this->load->model('Sales_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('Cus_Pricing_Model');
		$this->load->model('Country_Model');
		$this->load->model('Transfer_Model');
		$this->load->model('Purchases_Model');
		$this->load->model('User_Model');
		$this->load->model('Product_Damage_Model');//
		$this->load->model('Product_Models');//
		$this->load->model('category_models');
			$this->load->model('Department_Model');
		
	}
	

	
	
	public function save_details()
	{
	    echo "<br/>Save Details <br/>";
	    
	    // Start Sale Items //
	    
	    /////////////////////
	    $sale_id='20037'; //
		$rep_id=123;//130-Ayas, 132-kasun , 128-Danila ,129-thilina ,  123-shifan , 144-lahiru , 146-Buddhika , 145-saumya, 139-Vihanga , 121-Indunil , 120-manju , 142-Dineshika , 131-Isuru, 141-dher
		// 118-chanaka, 125-Sashika , 126-Sachira, 115-Pasindu, 137-Madhuranga , 124-Viranga
		$tech_id=0;
		$customer_id=1;
		$warehouse_last_id=9530; //
		$sale_item_id='27519';//
	    $sis_number='20080609582559';//
	    $unit_price=1750;//
	    $discount=0;//
	    $w_id=0; // 3- 1 year limited , 5- 3 month w
	    $sale_total=1750;
	     //
	    
	    
	    
	    /////////////////////
	    
	    $sale_reference_no ="L/S/$warehouse_last_id";
	    $quantity=1;//
	    
	    $sale_pymnt_paying_by='Cash';//'Credit Card';// Cash
	    $sale_datetime='2020-09-02 12:43:00'; //
	    
	    
	     $acctrnss_id=639;//'639-02'; //
	     $user_id=123;//
	    $warehouse_id=1; //
	    	$sl_rtn_id=0;
	    	
	    
	    $second_item='';
	    
	   
	    
	     $th_item='';
	    
	    /////////////////
	    
	    
	    $gross_total=$unit_price-$discount;
	    
		
		
		
		
		
		
		
		
		
		
		
		//sale
	    
	    
	     if(!empty($sis_number)){
	    $sn_data = $this->Sales_Model->check_serial_no_valid($sis_number,'');
	    $item_cost=$sn_data[0]->pis_cost_price;
	    $product_id=$sn_data[0]->product_id; //111111 : 7259 , 222222 : 4165
	     }else{
	     $item_cost=0;
	     $product_id='7259';
	     }
	     
	    
	    $cost_total=$item_cost;//0; //
	    	//echo "Searial Data: <br/>";
	    //print_r($sn_data); echo "<br/><br/>";
	  
	    
	    
	    $data_item=array(
					'sale_id'=>$sale_id,
					'id'=>$sale_item_id,
					'product_id'=>$product_id,
					//'product_sn'=>$row[$i]['product_sn'][0],
					'quantity'=>$quantity,
					'discount'=>$discount,
					'unit_price'=>$unit_price,
					'item_cost'=>$item_cost,
					'unit_price'=>$unit_price,
					'discount_val'=>$discount, 
					'gross_total'=>$gross_total,
					'w_id'=>$w_id,
				);
				
				echo "Sale Item <br/>";
				print_r($data_item); echo "<br/><br/>";
				$this->Sales_Model->save_sales_item($data_item);
	    
	    // End Sale Items //
	    
	    
	    // star second item //
	    if(!empty($second_item)){
	        
	        $sale_item_id='';//
	    $sis_number='';//
	    
	    $unit_price='';//
	    $discount='';//
	    $w_id=0; // 3- 1 year limited
	        $sn_data = $this->Sales_Model->check_serial_no_valid($sis_number,'');
	    $item_cost=$sn_data[0]->pis_cost_price;
	    $product_id=$sn_data[0]->product_id;
	    
	    $cost_total=$cost_total+$item_cost;//0; //
	    $gross_total=$unit_price-$discount;
	    	//echo "Searial Data: <br/>";
	    //print_r($sn_data); echo "<br/><br/>";
	  
	    
	    
	    $data_item=array(
					'sale_id'=>$sale_id,
					'id'=>$sale_item_id,
					'product_id'=>$product_id,
					//'product_sn'=>$row[$i]['product_sn'][0],
					'quantity'=>$quantity,
					'discount'=>$discount,
					'unit_price'=>$unit_price,
					'item_cost'=>$item_cost,
					'unit_price'=>$unit_price,
					'discount_val'=>$discount, 
					'gross_total'=>$gross_total,
					'w_id'=>$w_id,
				);
				
				echo "Sale Item <br/>";
				print_r($data_item); echo "<br/><br/>";
				$this->Sales_Model->save_sales_item($data_item);
	        
	    }
	    
	    
	    // end second item //
	    
	    
	    // star therd item //
	    if(!empty($th_item)){
	        
	        $sale_item_id='';//
	    $sis_number='';//
	    
	    $unit_price='';//
	    $discount=0;//
	    $w_id=0; // 3- 1 year limited
	        $sn_data = $this->Sales_Model->check_serial_no_valid($sis_number,'');
	    $item_cost=$sn_data[0]->pis_cost_price;
	    $product_id=$sn_data[0]->product_id;
	    
	    $cost_total=$cost_total+$item_cost;//0; //
	    $gross_total=$unit_price-$discount;
	    	//echo "Searial Data: <br/>";
	    //print_r($sn_data); echo "<br/><br/>";
	  
	    
	    
	    $data_item=array(
					'sale_id'=>$sale_id,
					'id'=>$sale_item_id,
					'product_id'=>$product_id,
					//'product_sn'=>$row[$i]['product_sn'][0],
					'quantity'=>$quantity,
					'discount'=>$discount,
					'unit_price'=>$unit_price,
					'item_cost'=>$item_cost,
					'unit_price'=>$unit_price,
					'discount_val'=>$discount, 
					'gross_total'=>$gross_total,
					'w_id'=>$w_id,
				);
				
				echo "Sale Item <br/>";
				print_r($data_item); echo "<br/><br/>";
				$this->Sales_Model->save_sales_item($data_item);
	        
	    }
	    
	    
	    // end therd item //
	    
	    
	  
	    
	    //Start Save Sales Table //
	    
	    

		
		
		
	
		$sale_inv_discount=0;
	
		
	
		$sale_pymnt_amount=0;
		
		
		
		$item_name	='';
		$imei_number='';
		$remarks	='';
	
		

			$sle_data=array(
			    'sale_id'=>$sale_id,
				'sale_reference_no'=>$sale_reference_no,
				'warehouse_id'=>$warehouse_id,
				'customer_id'=>$customer_id,
				'warehouse_id'=>$warehouse_id,
				'sale_datetime'=>$sale_datetime,
				'sale_total'=>$sale_total,
			
				'sale_datetime_created'=>$sale_datetime,
				'rep_id'=>$rep_id,
				'warehouse_last_id'=>$warehouse_last_id,
				'user'=>$user_id,
				'sl_rtn_id'=>$sl_rtn_id,
			
				'item_name' => $item_name,
				'imei_number' => $imei_number,
				'remarks'	=> $remarks,
				'tech_id'	=> $tech_id,
				'acctrnss_id'=>$acctrnss_id,
				'cost_total'=>$cost_total,
			
			);
			
			 echo "<br/><br/> Sale <br/>";
			print_r($sle_data);
			$this->Sales_Model->save_sales($sle_data,'');
		//	echo $this->db->last_query();
	    
	    //End Save Sales Table //
	    
	    
	     //Start Save Payment Table //
	     
	     $data_p=array(
				'sale_pymnt_amount'=>$sale_total,	
				'sale_pymnt_paying_by'=>$sale_pymnt_paying_by,
				'sale_pymnt_date_time'=>$sale_datetime,
				'user_id'=>$user_id,
				'sale_id'=>$sale_id,
				'sale_pymnt_added_date_time'=>$sale_datetime,
				'sale_payment_type' => 'sale',
			);
			
			if($sale_total){
              
              echo "<br/><br/> Payment <br/>";
              	print_r($data_p);
              $this->Sales_Model->save_sale_payments($data_p,'');
              echo $this->db->last_query();
                   
                   
               
			}
	     
	     //End Save Payment Table //
	    
	}
	    
	
}
