<?php   
    require "commonincludefiles.php";
	error_reporting(1);
	
	 $action 				= $_REQUEST['action'];
	 $voucher_date 			= $_REQUEST['datevoucher'];
	 $voucher_type 			= $_REQUEST['val_Riceipttype'];
   	 $voucher_check 		= $_REQUEST['cash'];
     $voucher_receiptno 	= $_REQUEST['receiptcredit'];
	 $voucher_voucherno   	= $_REQUEST['receiptcredit'];
	 $voucher_debitdetail   = $_REQUEST['textcredit'];
     $voucher_creaditdetail = $_REQUEST['textdebit'];
     $voucher_totalamount   = $_REQUEST['totalamount'];
	
	 $accountid=json_decode(stripslashes($_REQUEST['creditaccountid']));
     $amountarray=json_decode(stripslashes($_REQUEST['camountarray']));
	 $debitaccountid=json_decode(stripslashes($_REQUEST['debittaccountid']));
     $debitamountarray=json_decode(stripslashes($_REQUEST['damountarray']));
		
	if($_SESSION['cCompany_id']!="" && isset($_SESSION['cCompany_id']))
	{
		$VoucherMasterMgmt = new VoucherMasterMgmt();
		$VoucherDetailMgmt = new VoucherDetailMgmt();
		
		     
			$VoucherMasterMgmt->voucher_date			=$voucher_date;
			$VoucherMasterMgmt->voucher_type			=$voucher_type;
			$VoucherMasterMgmt->voucher_check			=$voucher_check;
			$VoucherMasterMgmt->receipt_no				=$voucher_receiptno;
			$VoucherMasterMgmt->voucher_no				=$voucher_voucherno;
			$VoucherMasterMgmt->voucher_debitdetail		=$voucher_debitdetail;  
			$VoucherMasterMgmt->voucher_creditdetail    =$voucher_creaditdetail;  
		    $VoucherMasterMgmt->voucher_totalamount	  	=$voucher_totalamount; 
			
			if($_REQUEST['ID']==0) {
				$VoucherMasterMgmt->add();	
			}
		   
		    else{
					$VoucherDetailMgmt->voucher_id = $_REQUEST["ID"]; 
					$VoucherMasterMgmt->voucher_id = $_REQUEST["ID"]; 
				   
					$VoucherMasterMgmt->edit();
					$VoucherDetailMgmt->delete();
			}
			
			  $j=count($accountid);
			  $m=count($debitaccountid);
				
		
				    for($i=0;$i<$j;$i++){
						$VoucherDetailMgmt->voucher_id = $VoucherMasterMgmt->voucher_id;
						//print_r($amountarray[$i]);
						$VoucherDetailMgmt->account_id				=$accountid[$i];
						$VoucherDetailMgmt->voucher_creditamount	=$amountarray[$i];					
						$VoucherDetailMgmt->voucher_debitamount		=0;
						$VoucherDetailMgmt->add();	
					}
			
			 
				    for($n=0;$n<$m;$n++){ 
						//print_r($debitaccountid[$n]);
					 
					  $VoucherDetailMgmt->account_id				=$debitaccountid[$n];
					  $VoucherDetailMgmt->voucher_debitamount		=$debitamountarray[$n];
					  $VoucherDetailMgmt->voucher_creditamount		=0;
					  $VoucherDetailMgmt->add();
	   	 		    }
					  
			    echo '{"success":"true","msg":"Data has been saved..."}';	
			
	} 
	else {
		echo '{"success":"false","msg":"session expiry"}';
	}
?>