<?php

	require_once("../include/config.php");	
	require_once("../include/startup.php");

$obj  							=  new Product();
$invoice_obj					=	new Invoice();
$client_obj  					=	new Client();
$memberpaymentdetail_obj	=	new Memberpaymentdetail();
$memberpaymentheader_obj	=	new Memberpaymentheader();
   // print_r($_POST);
// die();
if($_POST['SUBMIT'])
{ 
	//echo "<pre>";
	//print_r($_POST);
//die();
		$memberinvoicesid 							= $_POST['memberinvoicesid'];
	//	$billing_membercompanyid		= sanitize($_POST['billing_membercompanyid'],SQL);
		$billed_membercompanyid			= sanitize($_POST['billed_membercompanyid'],SQL);

			$payment_count 				= $_POST['entered_payment_count'];

			$paymentheaderarray['billed_membercompanyid'] 		= $billed_membercompanyid;
			$paymentheaderarray['memberinvoicesid'] 				= $memberinvoicesid;
			$paymentheaderarray['paymentmode'] 						= $_POST['radiopaymentmode'];
			$paymentheaderarray['paymentdate'] 						= date("Y-m-d");	
			$paymentheaderarray['head_receipt_no']   			 	= $_POST['txtReceiptno'];
			$paymentheaderarray['head_receipt_date']				= date("Y-m-d",strtotime($_POST['txtReceiptdate']));
			$header_total = 0;
			$tds_total 	  = 0;
			for($i=1;$i<=$payment_count;$i++) 
				{	
						$transition_amt 				= intval($_POST['txttransitionamount'.$i]);
						$cheque_amt 				= intval($_POST['txtchequeamount'.$i]);
						$tds_amt					= intval($_POST['txttds'.$i]);
			   			$tds_amt_online					= intval($_POST['txttdss'.$i]);
					if($_POST['txtchequeamount'.$i] > '') 
					{						   
			   
			  			 $header_total = $header_total + $cheque_amt;
					
					}
						if($_POST['txttransitionamount'.$i] > '') 
					{						   
			   
			  			 $header_total = $header_total + $transition_amt;
					
					}
					if($_POST['txttds'.$i] > '') 
					{						   
			   
			  			 $tds_total = $tds_total + $tds_amt;
					
					}
					if($_POST['txttdss'.$i] > '') 
					{						   
			   
			  			 $tds_totals = $tds_total + $tds_amt_online;
					
					}
					if($_POST['radiopaymentmode'.$i] == 'Offline_Pay') 
					{						   
			   
			  			$paymentheaderarray['tds'] 					= $tds_total;
					
					}
					else
					{
						$paymentheaderarray['tds'] 					= $tds_totals;
					}

				}
			
			$paymentheaderarray['paymentamount'] 		= '';
			$paymentheaderarray['servicetaxamount'] 	= '';
			$paymentheaderarray['TotalAmount'] 			= $header_total;
			
			//$paymentheaderarray['tds'] 					= $tds_total;
			$add_paymentheader = $memberpaymentheader_obj -> AddMemberpaymentheader($paymentheaderarray);
		
			$sql2 ="select * from user_company_payment_header where billed_membercompanyid = $billed_membercompanyid order by memberpaymentheaderid desc limit 0,1";
			$get_paymentheaderlist = $memberpaymentheader_obj -> getMemberpaymentheader('',$sql2,'','','','');
			$memberpaymentheaderid	= $get_paymentheaderlist[0]['memberpaymentheaderid'];
			  
			if($memberpaymentheaderid > '')
			{
				for($i=1;$i<=$payment_count;$i++) 
				{	
						$paymentdetailarray['billed_membercompanyid'] 	= $billed_membercompanyid;
						$paymentdetailarray['memberpaymentheaderid'] 	= $memberpaymentheaderid;
						$paymentdetailarray['memberinvoicesid'] 			= $memberinvoicesid;
					 	
					 	$paymentdetailarray['chequeno'] 						= trim(sanitize($_POST['txtchequeno'.$i],SQL));
						$paymentdetailarray['chequedate'] 					= date("Y-m-d", strtotime($_POST['txtchequedate'.$i]));
						$paymentdetailarray['chequeamount'] 				= $_POST['txtchequeamount'.$i];

						$paymentdetailarray['transition_no'] 						= trim(sanitize($_POST['txttransitionno'.$i],SQL));
						$paymentdetailarray['transition_date'] 					= date("Y-m-d", strtotime($_POST['txttransitiondate'.$i]));
						$paymentdetailarray['transition_amount'] 				= $_POST['txttransitionamount'.$i];
						//$paymentdetailarray['tds'] 					= $_POST['txttds'.$i];

						$paymentdetailarray['bankname'] 						= trim(sanitize($_POST['txtbankname'.$i],SQL));
						$paymentdetailarray['branch'] 						= trim(sanitize($_POST['txtbranch'.$i],SQL));
						$paymentdetailarray['mode_of_payment'] 			= $_POST['radiopaymentmode'.$i];
						$paymentdetailarray['receipt_no'] 					= $_POST['txtReceiptno'];
						$paymentdetailarray['receipt_date'] 					=  $_POST['txtReceiptno'];
						if($_POST['radiopaymentmode'.$i] == 'Offline_Pay') 
					{						   
			   
			  			$paymentdetailarray['tds'] 					= $_POST['txttds'.$i];
					
					}
					else
					{
						$paymentdetailarray['tds'] 					= $_POST['txttdss'.$i];
					}

			   
					if($_POST['txtchequeamount'.$i] > '' && $_POST['txtchequeno'.$i] > '') 
					{						   
			  			 $add_paymentdetail = $memberpaymentdetail_obj -> AddMemberpaymentdetail($paymentdetailarray);
					
					}
					if($_POST['txttransitionamount'.$i] > '' && $_POST['txttransitionno'.$i] > '') 
					{						   
			  			 $add_paymentdetail = $memberpaymentdetail_obj -> AddMemberpaymentdetail($paymentdetailarray);
					
					}
				
				}				 
			 	
			}
		   	if($add_paymentdetail)
		 		{
					$redirect_url = DOC_ROOT."admin/viewInvoice.php";
					header("Location: $redirect_url");
	    			exit;
				 }
		   }

?>