<?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();
        ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
        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);
	}
    function check_dublicate_stn_issue(){
        $id=$this->input->get('id');
	    $result=$this->Manual_Function_Model->duplicate_stn_siiue_list($id);
	    $data['main_menu_name']=$this->main_menu_name;
		$data['sub_menu_name']='';
	    $data['result']=$result;
		$this->load->view('duplicate_return_remove',$data);
    }
    function check_dublicate_stn_issue_delete(){
        //for($i=2000;$i++;$i<2100){
        $id=$this->input->get('id');
	    $result=$this->Manual_Function_Model->duplicate_stn_siiue_list($id);
	    $data['main_menu_name']=$this->main_menu_name;
		$data['sub_menu_name']='';
	    $data['result']=$result;
	    $dataset=array();
	    foreach($result as $r){
	      $dataset[]=$r->psio_id;  
	    }
	    if(count($dataset)>0){
	       $this->Manual_Function_Model->delete_dublicate_stn($dataset); 	    }
		$this->load->view('duplicate_return_remove',$data);
       // }
    }
    function check_dublicate_stn_delete(){
        $id=$this->input->get('id');
        //echo "ID : MAIN ITEM".$id."<hr>";
	    $result=$this->Manual_Function_Model->duplicate_stn_item($id);
	    $dataset=array();
	    foreach($result as $r){
	      $dataset[]=$r->psni_id; 
	      //echo $r->psni_id."<br>";
	    }
	    if(count($dataset)>0){
	            $this->Manual_Function_Model->delete_dublicate_stn_item($dataset); 
	    }
	    $result2=$this->Manual_Function_Model->duplicate_stn_sub_item($id);
	    
	    //echo "SUB ITEM  : ".$id."<hr>";
	    
	    $dataset2=array();
	     foreach($result2 as $r){
	      $dataset2[]=$r->psnsi_id; 
	    //  echo $r->psnsi_id."<br>";
	    }
	     if(count($dataset2)>0){
	       $this->Manual_Function_Model->delete_dublicate_stn_sub_item($dataset2); 
	    }
    }
    function disable_no_stock_batch(){
        $this->Manual_Function_Model->auto_enable_disable_zero_batch_qty();
        
    }
    function fill_batch_code_to_sti_table(){
       $nullList= $this->Manual_Function_Model->get_null_batch_list(); 
       
       echo "COUNT : ".count($nullList);
        echo "<br>";
       foreach ($nullList as $row){
           echo $row['product_code']." Name : ".$row['product_name']." Batch : ".$row['pbi_ref_no']." qty : ".$row['quantity'];
          $fbatch_id= $this->Manual_Function_Model->check_finish_batch_table($row['pbi_ref_no']);
           echo "<br>";
           if($fbatch_id==0){
               $data=array(
	            'bfp_product_id'=>$row['product_id'],
	            'bfp_batch_code'=>$row['pbi_ref_no'],
	            'bfp_date_time'=>$row['stm_date_time'],
	            'bfp_user_id'=>77,
	            'bfp_type'=>"CONVERT",
	            'bfp_status'=>1,
	            'bfp_refer_id'=>0
	            );
	            //$id=$this->Manual_Function_Model->save_fbatch($data); 
           }
           echo " CONVERTED BATCH ID : " .$fbatch_id;
           echo "<br>";
           echo " PRODUCT ID : " .$row['product_id'];
           echo "<br>";
            echo " PRODUCT MATERIAL ID : " .$row['product_mat_id'];
           echo "<br>";
            echo " PRODUCT MATERIAL BATCH ID : " .$row['pgbatch_id'];
           echo "<br>";
            $data=array(
	            'pc_prodcut_id'=>$row['product_id'],
	            'pc_parduct_mat_id'=>$row['product_mat_id'],
	            'pc_date_time'=>$row['stm_date_time'],
	            'pc_product_mat_batch_id'=>$row['product_mat_id'],
	            'converted_batch_id'=>$fbatch_id,
	            'pc_qty'=>$row['quantity'],
	            'pc_warehouse'=>4,
	            'pc_user_id'=>77,
	            );
           //$this->Manual_Function_Model->save_convert($data);
           $data=array(
	            'batch_code'=>$row['pbi_ref_no'],
	            'batch_id'=>$fbatch_id,
	            );
	       //   $this->Manual_Function_Model->update_sti_table($data,$row['sti_id']);  
           echo "<hr>";
       }
    }
    
    function extra_update(){
        $batchList= $this->Manual_Function_Model->get_finished_batch_list(); 
        foreach($batchList as $row){
            $extra_qty=0;
            $extra_value=0;
            if($row['output_qty']>$row['pbi_qty']){
                $extra_qty=$row['output_qty']-$row['pbi_qty'];
                $extra_value=$extra_qty*($row['pbi_unit_cost']);
                echo $row['pbi_ref_no']." ---- ".$extra_qty ."----".$extra_value;
                echo "<br>";
                $data=array('extra_output_qty'=>$extra_qty,'extra_output_value'=>$extra_value);
                $this->Manual_Function_Model->update_pbi_table($data,$row['pbi_id']);
            }
        }
    }
   

}