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

class Supplier_Payment_Adjustment  extends CI_Controller {
	var $main_menu_name = "settings";
	var $sub_menu_name = "transactions_list";
	
	public function __construct()
	{
	  parent::__construct();
	  $this->load->model('Supplier_Payment_Adjustment_Model');	
	   $this->load->model('Fixed_Assets_Model');	
	  $this->load->model('Common_Model');
	  $this->load->model('Warehouse_Model');
	   $this->load->model('Sales_Model');
	   $this->load->model('purchases_model');
	   
	  
    }
	public function index()
	{
	$data['main_menu_name'] = 'purchases';
    $data['sub_menu_name'] = 'supplier_payment_adjustment';
	$data['warehouse_list'] = $this->Warehouse_Model->get_all_warehouse();
	$this->load->view('supplier_payment_adjustment',$data);
	}
	
	public function cash_transfer_active()
	{
		$data=array();
	 	$acctrnss_id=$this->uri->segment(3);
		if(!$acctrnss_id) die;
		$data['transactions_details']=$this->Supplier_Payment_Adjustment_Model->get_transactions_details($acctrnss_id);	
		//echo "222222";
		$data['acctrnss_id']=$acctrnss_id;
		 $this->load->view('cash_transfer_active',$data);
		 //echo "Page Short Cut <br/> Add Item-F1 <br/> Quantity field to Discount Field-Tab <br/> Selling Price-F2";
	}
	
		public function update_cash_trasfer_status_btn()
	{
        $acctrnss_id = $this->input->get('acctrnss_id');
		$at_status = $this->input->get('val');
		
		
		$data=array(
			'at_status'=>$at_status,			
			'active_user'=>$this->session->userdata('ss_user_id'),			
			'active_date_time'=>date("Y-m-d H:i:s"),			
			);
			$_insert=$this->Supplier_Payment_Adjustment_Model->save_transactions($data,$acctrnss_id);
			
			$disMsg='';
			//end update serial no transer status
			
			//echo $this->db->last_query();
        
        echo json_encode(array('acctrnss_id'=>$acctrnss_id,'error'=>0,'disMsg'=>$disMsg,));
	}
	
	public function create_supplier_payment_adjustment()
	{
        
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = 'transactions_list';
		$data['warehouse_list'] = $this->Warehouse_Model->get_all_warehouse();
		$data['supplier'] = $this->purchases_model->get_supplier();
		$ss=$this->input->get('ss');
			$data['ss'] = $ss;
		
		$acctrnss_id='';
		//print_r($this->input->get());
		//echo "test:".$this->input->get('acctrnss_id');
		
        if ($this->input->get('acctrnss_id')) {
			$acctrnss_id=$this->input->get('acctrnss_id');
		}
		else {
			$acctrnss_id='';
		}
		
		//echo "t:$acctrnss_id";
		if($acctrnss_id){
			$data['acctrnss_id']=$acctrnss_id;
			$data['type']='E';
			$data['pageName']='Update Supplier Payment Adjustment';
			$data['btnText']='Update';
			$data['transactions_details']=$this->Supplier_Payment_Adjustment_Model->get_transactions_details($acctrnss_id);	
		}
		else {
			$data['acctrnss_id']='';
			$data['type']='A';
			if($ss){
			    	$data['pageName']='Shift Start';
			}else{
			$data['pageName']='Add Supplier Payment Adjustment';
			}
			if($ss){
			    $data['btnText']='Shift Start';
			}else{
			$data['btnText']='Add';
			}
			$data['transactions']=array();
		}
		$data['transactions_type']=$this->Fixed_Assets_Model->get_fixed_asset_list();
        $this->load->view('models/create_supplier_payment_adjustment',$data);	
	}

public function save_supplier_payment_adjustment()
	{
		$spa_amount=$this->input->post('spa_amount');
		$spa_datetime=$this->input->post('spa_datetime');
		$supplier_id=$this->input->post('supplier_id');
		$warehouse_id=$this->input->post('warehouse_id');
		$user_id=$this->input->post('user_id');
		$acctrnss_details=$this->input->post('acctrnss_details');
		$spa_debit_credit=$this->input->post('spa_debit_credit');
		$last_warehouse_id=0;
		$type=$this->input->post('type');

		
		$this->load->library('form_validation'); //form validation lib
		if($type=='A')
		{
			$this->form_validation->set_rules('spa_amount','required');
		}
		else if($type=='E')
		{
			$this->form_validation->set_rules('spa_amount','required');
		}
		
		
		if ($this->form_validation->run() == FALSE)
        {
           $st = array('status' =>0,'validation' => validation_errors());
           echo json_encode($st);
        }
        else
        {
			
			$w_id=$warehouse_id;
			$data=array(
			'spa_amount'=>$spa_amount,
			'spa_datetime'=>$spa_datetime,	
			'supplier_id'=>$supplier_id,
			'user_id'=>$user_id,
			'warehouse_id'=>$warehouse_id,
			'spa_debit_credit'=>$spa_debit_credit,
			'spa_added_datetime'=>date("Y-m-d H:i:s"),	
			'last_warehouse_id'=>$last_warehouse_id,
			'acctrnss_details'=>$acctrnss_details
			);
			
			//print_r($data);
			$_insert=$this->Supplier_Payment_Adjustment_Model->save_supplier_payment_adjustment($data);
			
			
			
		
		
		
		
		
		
		
	//	echo $this->db->last_query();
		$lastid=$this->db->insert_id();
		
		
		

		if($type=='A'){
			if ($lastid) {
				echo json_encode(array('id'=>$lastid,'type'=>$type,'status' =>1));
			} else {
				echo json_encode(array('status'=>'0'));
			}
		}
		if($type=='E'){
			echo json_encode(array('type'=>$type,'status' =>1));
		}	
			
		}
	}
	
//print_transactions
public function supplier_payment_adjustment_print()
{
    $acctrnss_id=$this->input->get('acctrnss_id');
   // echo "acctrnss_id:$acctrnss_id";
    $data['transactions_details']=$this->Supplier_Payment_Adjustment_Model->get_supplier_payment_adjustment_details($acctrnss_id);
   // echo $this->db->last_query();
   // $data['test']='test';
   
   //echo "warehouse_id:".$data['transactions_details']['acctrnss_id'];
    
    $data['warehouse_details'] = $this->Warehouse_Model->get_warehouse_info($data['transactions_details']['warehouse_id']);
    
     //print_r($data['transactions_details']);
		
       // $this->load->view('sales',$data);
       
       $data['sale_payments_list']=$this->Supplier_Payment_Adjustment_Model->get_sup_ajes_payments_by_sale_id($acctrnss_id);
       
      // print_r($data['sale_payments_list']);
      // die();
    
    $this->load->view('models/supplier_payment_adjustment_print',$data);
}
	
	
	
	
public function supplier_payment_adjustment_load()
{
$transactions_list=$this->Supplier_Payment_Adjustment_Model->get_supplier_payment_adjustment_list();

//print_r($transactions_list);
//die();
$data=array();
$x=0;


foreach($transactions_list as $row){
	$x++;
	$nestedData=array();
	
	$spa_debit_credit=$row['spa_debit_credit'];
$spa_debit_credit_val='';
if($spa_debit_credit==1) $spa_debit_credit_val="Deduction";
if($spa_debit_credit==0) $spa_debit_credit_val="Addition";

	$nestedData[]=$x;
	$nestedData[]=$row['supp_company_name'];
	$nestedData[]=$row['name'];
	$nestedData[]=$spa_debit_credit_val;
//	$nestedData[]=$row['fam_name'];
	$nestedData[]=site_date($row['spa_datetime']);
	$nestedData[]=$row['spa_amount'];
		$nestedData[]=$row['acctrnss_details'];
	//$nestedData[]=$row['fxd_ass_amount'];
	$status='';
	/*
	if($row['fxd_ass_status']==1)
	{
	$status='<center><span class="label label-success">Enable</span></center>';
	}
	else if($row['fxd_ass_status']==0)
	{
	$status='<center><span class="label label-warning">Disable</span></center>';
	}*/
	//$nestedData[]=$status;
	$action='<center><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="#" onclick="print_supplier_payment_adjustment('.$row['spa_id'].'); return false;"><i class="fa fa-file-text-o"></i>Print</a></li>
							</ul></div></center>';
							// <li><a href="#" onclick="update_transactions('.$row['acctrnss_id'].'); return false;"><i class="fa fa-file-text-o"></i>Update Transactions</a></li>
	$nestedData[]=$action;
	$data[]=$nestedData;
	}
	$json_data = array(
	"data"=>$data
	);
	
	echo json_encode($json_data);	
}	
}