<?php
session_start();
// ============
include("../../asset/theme/css/myfunc.php");
include("../../php_crud/my_class.php");
$obj=new my_class();
// ============
$branchid = $_SESSION['LOGIN_BRANCH'];
extract($_POST);


if($opr=='voucher_entry'){

	$date_c = date("Y-m-d");
	$tot = $obj->Total_Count_By_Cond("acc_voucher","DATE(_entry_date)='$date_c'");	

	if($tot==0){
	   $date = date("Ymd");
	   $cons = '00001';
	   $trxn_id = $date.$cons;
	}
	else{
		$mx = $obj->Max_Value_By_Cond("acc_voucher","_trxn_id","DATE(_entry_date)='$date_c'");
		$trxn_id = $mx['max_valu']+1;
	}
// ==============================


	$form_data=array(
		'_br_id'=>$branchid,
		'_trxn_type'=>'D',
		'_trxn_id'=>$trxn_id,
		'_date'=>date("Y:m:d",strtotime($voucher_date)),
		'_voutcher_type'=>$voucher_type,
		'_amount'=>$amount,
		'_voutcher_no'=>$voucher_number,
		'_particullar'=>$particular,
		'_entry_by'=>$_SESSION['LOGIN_USERID'],
		'_entry_date'=>date("Y:m:d h:i:s")

		);

	$bal=$obj->default_bank_balance($branchid);
	extract($bal);
	$balnc=$obj->cash_in_hand_balance($branchid);
	extract($balnc);

	if($voucher_type=='C'){
		if($cash_balance_default<$amount){
			echo "low";
		}
		elseif($cash_balance_default>=$amount){
			$entry_voucher = $obj->Insert_Data_By_Cond("acc_voucher",$form_data,"_voutcher_no='$voucher_number'");
             
	        if($entry_voucher){
				$gl_c=$obj->get_default_cash_gl($branchid);
				extract($gl_c);

	             $form_data_GL_C=array(
				'_br_id'=>$branchid,
				'_voucher_id'=>$entry_voucher,
				'_gl_id'=>$gl_head_id,
				'_amount'=>$amount,
				'_trxn_type'=>'D',
				'_entry_by'=>$_SESSION['LOGIN_USERID'],
				'_entry_date'=>date("Y:m:d h:i:s"),
				'_status'=>'A'
				);

		        $gl_cash = $obj->Insert_Data("acc_voucher_gl",$form_data_GL_C);
		        if($gl_cash){
		        	echo $entry_voucher;
		        }		        
		        else{
	        		echo "dup";
	        	} 
	     	}
	 		else{
	 			echo "dup";
	 		}      
		}
	}
	// ==================================
	elseif ($voucher_type=='B') {
       if($bank_balance_default<$amount){
			echo "low";
		}
		elseif($bank_balance_default>=$amount){			
		    $entry_voucher = $obj->Insert_Data_By_Cond("acc_voucher",$form_data,"_voutcher_no='$voucher_number'");
	    	if($entry_voucher){
				$glb=$obj->get_default_bank_gl($branchid);
				extract($glb);


	            $form_data_GL_B=array(
				'_br_id'=>$branchid,
				'_voucher_id'=>$entry_voucher,
				'_gl_id'=>$gl_head_id,
				'_amount'=>$amount,
				'_trxn_type'=>'D',
				'_entry_by'=>$_SESSION['LOGIN_USERID'],
				'_entry_date'=>date("Y:m:d h:i:s"),
				'_status'=>'A'
				);

	        	$gl_bank = $obj->Insert_Data("acc_voucher_gl",$form_data_GL_B);
		        if($gl_bank){
		        	echo $entry_voucher;
		        }
		        else{
		        	echo "dup";
		        }         
     		}
 			else{
 				echo "dup";
 			}
		}	
	}
	else{
		echo "dup";
	}
}

// =============End Voucher==================

if($opr=='voucher_gl_entry'){

  	if($v_pid!='dup'){
	    if($debit!=0){
	     	$amount = $debit;
	     	$trxn_type = 'D';
	    }
	    if($credit!=0){
	     	$amount = $credit;
	     	$trxn_type = 'C';
	    }

		$form_data2=array(
				'_br_id'=>$branchid,
				'_voucher_id'=>$v_pid,
				'_gl_id'=>$gl_id,
				'_amount'=>$amount,
				'_trxn_type'=>'C',
				'_entry_by'=>$_SESSION['LOGIN_USERID'],
				'_entry_date'=>date("Y:m:d h:i:s"),
				'_status'=>'A'
				);
		$vc_gp = $obj->Insert_Data("acc_voucher_gl",$form_data2);
		if($vc_gp){
			echo $xy = $vc_gp.'-'.$sl.'-'.$trxn_type;
		}
	}
	else{
		echo"Voucher Number Already Existed";
	}
}
// ===============End GL ======================

if($opr=='voucher_sub_gl_entry'){

	$form_data3=array(
		'_br_id'=>$branchid,
		'_voucher_id'=>$v_pid,
		'_voucher_gl_id'=>$vcgl_id,
		'_sub_gl_id'=>$sub_gl_id,
		'_amount'=>$sub_gl_amount,
		'_trxn_type'=>'C',
		'_entry_by'=>$_SESSION['LOGIN_USERID'],
		'_entry_date'=>date("Y:m:d h:i:s"),
		'_status'=>'A'
		);
 	$entry2 = $obj->Insert_Data("acc_voucher_sub_gl",$form_data3);
}

?>