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



class Manual_Function extends CI_Controller {



    var $main_menu_name = "";

	var $sub_menu_name = "";



    public function __construct()

    {

        parent::__construct();
        date_default_timezone_set("Asia/Colombo");
        $this->load->model('country_model');
		$this->load->model('Common_Model');
		$this->load->model('Product_Models');
		$this->load->model('Manual_Function_Model');
		$this->load->model('Purchases_Model');
		$this->load->model('Sales_Model');
		$this->load->model('Sales_Return_Model');
		
		
    }

	

	public function index()
	{
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = $this->sub_menu_name;		
	}



	public function disable_not_stocks_product()
	{       
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = $this->sub_menu_name;;
      	$values = $this->Product_Models->getProductsForQTYReport(1, '');
		
		 $data   = array();
        if (!empty($values)) {
            foreach ($values as $products) {
                if ($products->product_status == 1) {					
					$purchased_qty=$this->Purchases_Model->getPurchasedQtyByWarehouseId($this->session->userdata('ss_warehouse_id'),$products->product_id);
					
					if($purchased_qty==0)
					{
						$result=$this->Manual_Function_Model->disable_product($products->product_id);
						echo "disable product : ".$products->product_name." qty = ".$purchased_qty;						
					}	
				}
			}              
         }
		

	}
	
	
	public function enable_all_product()
	{      
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = $this->sub_menu_name;;
      	$values = $this->Product_Models->getProductsForQTYReport(1, '');		
		 $data   = array();
        if (!empty($values)) {
            foreach ($values as $products) {              			
					$purchased_qty=$this->Purchases_Model->getPurchasedQtyByWarehouseId($this->session->userdata('ss_warehouse_id'),$products->product_id);
					$result=$this->Manual_Function_Model->enable_product($products->product_id);	
			}              
         }
	}
	
	
	public function close_paid_invoices(){
		$values = $this->Manual_Function_Model->get_unpaid_sales();	
		 if (!empty($values)) {
            foreach ($values as $row) { 
					$sale_id=$row['sale_id'];
					$total_paid_amount=$this->Sales_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;
						}
				
				 $sale_balance=$row['sale_total']-$return_tot_amt-$total_paid_amount;
				
				if($sale_balance<=0){
				$result=$this->Manual_Function_Model->close_sale($sale_id);	
				echo $row['sale_reference_no']." is Done <br>";
				}
				
				}              
         }
		
	}



	public function close_complete_multiple_payment()
	{
		$values = $this->Manual_Function_Model->get_uncomplte_multiple_payment();	
		if (!empty($values)) {
		   foreach ($values as $row) { 
				   $id=$row['smp_id'];
				   $sale_paid=$this->Manual_Function_Model->get_total_sale_paid_by_smp_id($id);		
				   $return_paid=$this->Manual_Function_Model->get_total_cheque_return_paid_by_smp_id($id);		
				  
			   
				$sale_balance=$row['smp_amount']-$return_paid-$sale_paid;

				if($sale_balance>0)
				{
					echo " overPayment ".$row['smp_amount']." ".$sale_balance." ( ".$row['payment_ref_no'].")<br>";
				}
			   
			   if($sale_balance==0){
			   $result=$this->Manual_Function_Model->close_payment($id);	
			   echo $row['payment_ref_no']." is Done <br>";
			   }
			   
			   }              
		}


	}
	
	
	public function update_initial_batch_product()
	{
	    return false;
	    $values = $this->Manual_Function_Model->get_not_updated_initial_batch();
	    
	    foreach ($values as $row){
	        $ref_code=$this->Common_Model->gen_ref_number('batch_id','batch_product','BTC');
	        $data=array(
	            'batch_ref_number'=>$ref_code,
	            'product_cost'=>$row['product_cost'],
	            'wholesale_price'=>$row['wholesale_price'],
	            'credit_salling_price'=>$row['credit_salling_price'],
	            'product_price'=>$row['product_price'],
	            'retail_cash'=>$row['retail_cash'],
	            'retail_credit'=>$row['retail_credit'],
	            'retail_wholesale'=>$row['retail_wholesale'],
	            'product_id'=>$row['product_id'],
	            );
	            $this->Manual_Function_Model->save_batch($data);
	            $this->Manual_Function_Model->update_product($row['product_id']);
	            echo $row['product_name']." <br>";
	    }
	    
	    
	    
	}
	
	
		public function update_initial_batch_product_new()
	{
	    return false;
	    $values = $this->Manual_Function_Model->get_not_updated_initial_batch();
	    
	    foreach ($values as $row){
	        $ref_code=$this->Common_Model->gen_ref_number('batch_id','batch_product','BTC');
	        
	         $data=array(
	            'purchase_id'=>$row['product_id'],
	            'purchase_id'=>1,
	            'quantity'=>0,
	            'unit_price'=>$row['product_cost'],
	            'sub_total'=>0,
	            'is_batch_created'=>1,
	            'product_id'=>$row['product_id'],
	            );
	            $id=$this->Manual_Function_Model->insert_initial_grn($data);
	            $data=array(
	            'batch_ref_number'=>$ref_code,
	            'product_cost'=>$row['product_cost'],
	            'wholesale_price'=>$row['wholesale_price'],
	            'credit_salling_price'=>$row['credit_salling_price'],
	            'product_price'=>$row['product_price'],
	            'retail_cash'=>$row['retail_cash'],
	            'retail_credit'=>$row['retail_credit'],
	            'retail_wholesale'=>$row['retail_wholesale'],
	            'product_id'=>$row['product_id'],
	            'grn_item_id'=>$id,
	            );
	            $batch_id=$this->Manual_Function_Model->save_batch($data);
	             $data_3=array(
	            'grn_item_id'=>$id,
	            'batch_id'=>$batch_id,
	            'product_id'=>$row['product_id'],
	            );
	            $this->Manual_Function_Model->save_batch_grn($data_3);
	            $this->Manual_Function_Model->update_product($row['product_id']);
	            echo $row['product_name']." <br>";
	    }
	}
	
	
	function update_sale_item_original_price(){
	    return false;
	    $sale_list=$this->Manual_Function_Model->get_sale_item_list();
	    
	    foreach($sale_list as $row){
	        $result=$this->Manual_Function_Model->get_batch_prices($row['batch_id']);
	         $data=array(
	            'original_cash_price'=>$result['product_price'],
	            'original_wholesale_price'=>$result['wholesale_price'],
	            'original_credit_price'=>$result['credit_salling_price'],
	            );
	            $this->Manual_Function_Model->update_sale_item($data,$row['id']);
	    }
	    
	    
	    
	}


	



}