<?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');
		$this->load->model('Batch_Process_Model');
		$this->load->model('Stock_Counter_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()
	{
	    
	    $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,
	            );
	            $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,
	            );
	            //$this->Manual_Function_Model->save_batch($data);
	            //$this->Manual_Function_Model->update_product($row['product_id']);
	            echo $row['product_name']." <br>";
	    }
	}
	
	
	function add_manual_batch_grn(){
	    $this->load->model('Batch_Process_Model');
	    //$this->Batch_Process_Model->add_manual_batch_grn();	

	}
	
	
		function delete_grn_1(){
	    $this->load->model('Manual_Function_Model');
	    //$this->Manual_Function_Model->delete_grn_1();	

	}
	
	
	function manual_grn_batch_enable_disable(){
	    $this->Batch_Process_Model->auto_enable_disable_zero_batch_qty(1550);
	}

	



}