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

class Transactions  extends CI_Controller {
	var $main_menu_name = "settings";
	var $sub_menu_name = "transactions_list";
	
	public function __construct()
	{
	  parent::__construct();
	  $this->load->model('Transactions_Model');	
	   $this->load->model('Fixed_Assets_Model');	
	  $this->load->model('Common_Model');
	  $this->load->model('Warehouse_Model');
	   $this->load->model('Sales_Model');
    }
	public function index()
	{
	$data['main_menu_name'] = $this->main_menu_name;
    $data['sub_menu_name'] = 'transactions_list';
	$data['warehouse_list'] = $this->Warehouse_Model->get_all_warehouse();
	$this->load->view('transactions_list',$data);
	}
	
	public function cash_transfer_active()
	{
		$data=array();
	 	$acctrnss_id=$this->uri->segment(3);
		if(!$acctrnss_id) die;
		$data['transactions_details']=$this->Transactions_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->Transactions_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_transactions()
	{
        
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = 'transactions_list';
		$data['warehouse_list'] = $this->Warehouse_Model->get_all_warehouse();
		$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 Transactions';
			$data['btnText']='Update';
			$data['transactions_details']=$this->Transactions_Model->get_transactions_details($acctrnss_id);	
		}
		else {
			$data['acctrnss_id']='';
			$data['type']='A';
			if($ss){
			    	$data['pageName']='Shift Start';
			}else{
			$data['pageName']='ADD Transactions';
			}
			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_transactions',$data);	
	}

public function save_transactions()
	{
		$acctrnss_id=$this->input->post('acctrnss_id');
		$type=$this->input->post('type');
		$fxd_ass_id=$this->input->post('fxd_ass_id');
		$acctrnss_amount=$this->input->post('acctrnss_amount');
		$warehouse_id=$this->input->post('warehouse_id');
		//print_r($warehouse_id);
		$to_warehouse_id=$this->input->post('to_warehouse_id');
		$shift_status=1;
		
		
		
		
		
		
		
		$acctrnss_date=date('Y-m-d',strtotime($this->input->post('acctrnss_date')));
		$user_id=$this->session->userdata('ss_user_id');
		$acctrnss_details=$this->input->post('acctrnss_details');
		
		
		//check allredy ship started
		$ss_user_id=$this->session->userdata('ss_user_id');
						$shift_date=date("Y-m-d");
						$active_shift_des=$this->Common_Model->get_active_shift_by_user_id_date($ss_user_id,$shift_date);
					//	print_r($active_shift_des);
						
				
		if($fxd_ass_id==1)	{			
		if(count($active_shift_des)){
		    $error=1;
		    $error_msg="Shift Allredy Started";
			echo json_encode(array('error'=>$error,'type'=>$type,'status' =>'0','validation' =>'','error_msg'=>$error_msg));
			die();
		
		}
		}
		
		
		
		$this->load->library('form_validation'); //form validation lib
		if($type=='A')
		{
			$this->form_validation->set_rules('fxd_ass_id','required');
		}
		else if($type=='E')
		{
			$this->form_validation->set_rules('fxd_ass_id','required');
		}
		
		if($fxd_ass_id==3){
		    
		    	//$this->form_validation->set_rules('to_warehouse_id','required');
		    	if($to_warehouse_id==''){
		    	    
		    	    die();
		    	}
		    		if($to_warehouse_id==$warehouse_id){
		    	    
		    	    die();
		    	}
		}
		
		
		
		
		
		if ($this->form_validation->run() == FALSE)
        {
           $st = array('status' =>0,'validation' => validation_errors());
           echo json_encode($st);
        }
        else
        {
			
			
		for ($k=0; $k<count($warehouse_id); $k++)
		{
			$w_id=$warehouse_id[$k];
			$data=array(
			'fxd_ass_id'=>$fxd_ass_id,
			'acctrnss_amount'=>$acctrnss_amount,	
			'acctrnss_date'=>$acctrnss_date,
			'acctrnss_details'=>$acctrnss_details,
			'user_id'=>$user_id,
			'warehouse_id'=>$w_id,
			'to_warehouse_id'=>$to_warehouse_id,
				'added_date_time'=>date("Y-m-d H:i:s"),	
				'shift_status'=>$shift_status,
			
			);
			$_insert=$this->Transactions_Model->save_transactions($data,$acctrnss_id);
		}	
			
			
		
		
		
		
		
		
		
	//	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 transactions_print()
{
    $acctrnss_id=$this->input->get('acctrnss_id');
   // echo "acctrnss_id:$acctrnss_id";
    $data['transactions_details']=$this->Transactions_Model->get_transactions_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']);
    
     
		
       // $this->load->view('sales',$data);
    
    $this->load->view('models/transactions_print',$data);
}
	
	
	
	
public function transactions_load()
{
$transactions_list=$this->Transactions_Model->get_transactions_list();
$data=array();
$x=0;
foreach($transactions_list as $row){
	$x++;
	$nestedData=array();
	$nestedData[]=$x;
	$nestedData[]=$row['fxd_ass_name'];
	$nestedData[]=$row['name'];
	$nestedData[]=$row['fa_type_name'];
	$nestedData[]=$row['fam_name'];
	$nestedData[]=site_date($row['acctrnss_date']);
	$nestedData[]=$row['acctrnss_amount'];
	//$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_transactions('.$row['acctrnss_id'].'); return false;"><i class="fa fa-file-text-o"></i>Print Transactions</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);	
}	
}