<?php
	//Connects to your Database
	include "../db_connection/db_connection.php";
	include "../config/config.php";		
	
	$pa_id 		=	$_POST['pa_id'];
	$pod 		=	$_POST['pod'];
	$payment_dt	=	$_POST['pod'];
	$pod		= 	str_replace("/", "-", $pod); 
	$pay_tp 	=	$_POST['pay_tp'];
	$ad_amt		=	'';
	$disc_amt	=	'';
	
	$SLCTaap = "SELECT * FROM party_det WHERE party_det_id='$pa_id'";			
	$QURYaap = mysqli_query($connect,$SLCTaap);		
	$FTCHaap = mysqli_fetch_object($QURYaap);
	$cli_emailid = $FTCHaap->email_id_1;
	$com_name 	=	$FTCHaap->cmp_name;  
	$mobile_no 	=	$FTCHaap->con_no_1;
	
	if(isset($_POST['ad_amt']))
	{
		$cre_bl_amt = $_POST['ad_amt'];			
		list($acc_bil_amt,$acc_re_amt,$acc_adv_amt,$acc_pen_amt,$acc_dis_amt) = total_acc_call($pa_id);
		$tot_cr_up = $acc_adv_amt + $cre_bl_amt;
		$SLCT = "SELECT * FROM ac_party_det WHERE party_det_id='$pa_id'";			
		$QURY = mysqli_query($connect,$SLCT);		
		if(mysqli_num_rows($QURY)>0)
		{
			$FTCH = mysqli_fetch_object($QURY);
			$ac_party_det_id = $FTCH->ac_party_det_id;
			
			$UPDATE_1 = "UPDATE ac_party_det SET `tot_cre_bln`='$tot_cr_up', update_dt = '$cur_time' WHERE ac_party_det_id = '$ac_party_det_id'";		
			//echo $UPDATE_1."<br>";
			$QRY_1    = mysqli_query($connect,$UPDATE_1); 
			
			$UPDATE_2 = "UPDATE ac_received_det SET update_status = 'yes',update_dt = '$cur_time' WHERE ac_party_det_id = '$ac_party_det_id' AND update_status = 'no'";		
			//echo $UPDATE_2."<br>";
			$QRY_2    = mysqli_query($connect,$UPDATE_2); 
		}
		else
		{
			$INSRT		= "INSERT INTO ac_party_det(`party_det_id`, `tot_cre_bln`, `post_dt`) VALUES('$pa_id','$cre_bl_amt','$cur_time')";		
			//echo $INSRT."<br>";
			$QURY		= mysqli_query($connect,$INSRT);
			$SLCT_skt = "SELECT  MAX(ac_party_det_id) as max_ids FROM ac_party_det ORDER BY ac_party_det_id ASC";
			$QURY_skt = mysqli_query($connect,$SLCT_skt);  
			$FTCH_skt = mysqli_fetch_object($QURY_skt);
			$ac_party_det_id = $FTCH_skt->max_ids;				
		}
		
		$ad_amt 	=	$_POST['ad_amt'];
	
		if($pay_tp=='cash')
		{
			$e_name		=	$_POST['e_name']; 				
			$INSRT_re		= "INSERT INTO ac_received_det(`ac_party_det_id`, `received_date`, `payment_type`,`emp_det_id`,`received_amt`, `post_dt`) VALUES('$ac_party_det_id','$pod','$pay_tp','$e_name','$ad_amt','$cur_time')";		
			$QURY_re		= mysqli_query($connect,$INSRT_re);								
		}
		else if($pay_tp=='cheque')
		{
			$chk_col_id	=	$_POST['chk_col_id']; 
			$chk_rec 	=	$_POST['rec'];
			$e_name 	=	$chk_rec;	 
			$chq_no 	=	$_POST['chq_no'];
			$bn_name 	=	$_POST['bn_name'];
			$bran_name 	=	$_POST['bran_name'];				
			$INSRT_re	= 	"INSERT INTO ac_received_det(`ac_party_det_id`, `received_date`, `payment_type`, `cheque_no`, `bank_name`, `branch_name`, `emp_det_id`,`received_amt`, `post_dt`) VALUES ('$ac_party_det_id','$pod','$pay_tp','$chq_no','$bn_name','$bran_name','$chk_rec','$ad_amt','$cur_time')";		
			//echo $INSRT_re."<br>";
			$QURY_re	= 	mysqli_query($connect,$INSRT_re);
			
			$UPD_CHK 	= 	"UPDATE cheque_collection_list SET reminder_status='closed', cheque_status='closed', updated_date='$cur_time' WHERE cheque_collection_list_id='$chk_col_id'";
			//echo $UPD_CHK."<br>";
			$QURY_CHK	= 	mysqli_query($connect,$UPD_CHK);
		}
		else if($pay_tp=='neft')
		{	
			$bn_name_ne 	=	$_POST['bn_name_ne'];
			$bn_name		=	$bn_name_ne;		
			$e_name 		=	'';			
			$INSRT_re		= 	"INSERT INTO ac_received_det(`ac_party_det_id`, `received_date`, `payment_type`, `bank_name`,`emp_det_id`, `received_amt`, `post_dt`) VALUES('$ac_party_det_id','$pod','$pay_tp','$bn_name','$e_name','$ad_amt','$cur_time')";		
			$QURY_re		= 	mysqli_query($connect,$INSRT_re);
		}
		$SLCT_sk = "SELECT  MAX(ac_received_det_id) as max_id FROM ac_received_det ORDER BY ac_received_det_id ASC";
		$QURY_sk = mysqli_query($connect,$SLCT_sk);  
		$FTCH_sk = mysqli_fetch_object($QURY_sk);
		$ac_rec_det_id = $FTCH_sk->max_id;
		$paid_amt = $ad_amt; 
	}
	else if(isset($_POST['amt']))
	{
		$amt 			=	$_POST['amt'];
		$disc_amt 		=	$_POST['disc_amt'];
		$tot_bl_amt 	=	$_POST['tot_bl_amt'];
		$tot_pen_amt 	=	$_POST['tot_pen_amt'];
		$tot_cl_amt 	=	$_POST['tot_cl_amt'];
		$cre_bl_amt 	=	$_POST['cre_bl_amt'];
		$tot_rec_amt 	= 	$amt + $disc_amt;
		$tot_p_amt 		= 	$tot_pen_amt - $tot_rec_amt;
		
		$SLCT = "SELECT * FROM ac_party_det WHERE party_det_id='$pa_id'";	
		//echo $SLCT;		
		$QURY = mysqli_query($connect,$SLCT);		
		if(mysqli_num_rows($QURY)>0)
		{
			$FTCH = mysqli_fetch_object($QURY);
			$ac_party_det_id = $FTCH->ac_party_det_id;
			$tot_bl_up = $FTCH->total_bill_amt; 
			$tot_rece_up = $FTCH->total_rece_amt + $amt; 
			$tot_pe_up = $FTCH->total_bill_amt - $tot_rece_up; 
			$tot_cr_up = $FTCH->tot_cre_bln + $cre_bl_amt;  
			$tot_dis_amt = $FTCH->tot_dis_amt + $disc_amt;
			$tot_pe_up = $tot_pe_up - $tot_dis_amt;
			if($tot_pe_up<0)
			{ 
				$tot_pe_up =0; 
			}
			$UPDATE_1 = "UPDATE ac_party_det SET `total_bill_amt`='$tot_bl_up', `total_rece_amt`='$tot_rece_up',`tot_dis_amt`='$tot_dis_amt', `total_pending`='$tot_pe_up', `tot_cre_bln`='$tot_cr_up',update_dt = '$cur_time' WHERE ac_party_det_id = '$ac_party_det_id'";		
			$QRY_1    = mysqli_query($connect,$UPDATE_1); 
			
			$UPDATE_2 = "UPDATE ac_received_det SET update_status = 'yes',update_dt = '$cur_time' WHERE ac_party_det_id = '$ac_party_det_id' AND update_status = 'no'";		
			$QRY_2    = mysqli_query($connect,$UPDATE_2); 
			
			$cre_bl_amt 	=	$_POST['cre_bl_amt'];
			$bl_amt 		=	$_POST['bl_amt'];
			
			if($pay_tp=='cash')
			{
				$e_name		=	$_POST['e_name']; 				
				$INSRT_re	= 	"INSERT INTO ac_received_det(`ac_party_det_id`, `received_date`, `payment_type`,`emp_det_id`, `received_amt`, `dis_amt`,`cred_amt`, `post_dt`) VALUES('$ac_party_det_id','$pod','$pay_tp','$e_name','$amt','$disc_amt','$cre_bl_amt','$cur_time')";		
				$QURY_re	= 	mysqli_query($connect,$INSRT_re);				
				
			}
			else if($pay_tp=='cheque')
			{
				$chk_col_id	=	$_POST['chk_col_id'];				
				$chk_rec 	=	$_POST['rec'];
				$e_name 	=	$chk_rec;
				$chq_no 	=	$_POST['chq_no'];
				$bn_name 	=	$_POST['bn_name'];
				$bran_name 	=	$_POST['bran_name'];
				
				$INSRT_re	= 	"INSERT INTO ac_received_det(`ac_party_det_id`, `received_date`, `payment_type`, `cheque_no`, `bank_name`, `branch_name`, `emp_det_id`, `received_amt`, `dis_amt`,`cred_amt`,`post_dt`) VALUES('$ac_party_det_id','$pod','$pay_tp','$chq_no','$bn_name','$bran_name','$chk_rec','$amt','$disc_amt','$cre_bl_amt','$cur_time')";		
				//echo $INSRT_re."<br>";
				$QURY_re	= 	mysqli_query($connect,$INSRT_re);
				
				$UPD_CHK 	= 	"UPDATE cheque_collection_list SET reminder_status='closed', cheque_status='closed', updated_date='$cur_time' WHERE cheque_collection_list_id='$chk_col_id'";
				//echo $UPD_CHK."<br>";
				$QURY_CHK	= 	mysqli_query($connect,$UPD_CHK);
				
			}
			else if($pay_tp=='neft')
			{	
				$bn_name_ne 	=	$_POST['bn_name_ne'];
				$bn_name			=	$bn_name_ne;
				$e_name 	=	'';
				
				$INSRT_re		= "INSERT INTO ac_received_det(`ac_party_det_id`, `received_date`, `payment_type`, `bank_name`,`emp_det_id`, `received_amt`, `dis_amt`,`cred_amt`, `post_dt`) VALUES('$ac_party_det_id','$pod','$pay_tp','$bn_name','$e_name','$amt','$disc_amt','$cre_bl_amt','$cur_time')";		
				$QURY_re		= mysqli_query($connect,$INSRT_re);
			}
			$SLCT_sk = "SELECT  MAX(ac_received_det_id) as max_id FROM ac_received_det ORDER BY ac_received_det_id ASC";
			$QURY_sk = mysqli_query($connect,$SLCT_sk);  
			$FTCH_sk = mysqli_fetch_object($QURY_sk);
			$ac_rec_det_id = $FTCH_sk->max_id;
		}
		$paid_amt = $amt; 
	}
	
	if($pay_tp!='neft')
	{
		$SLCTaae = "SELECT * FROM emp_det WHERE emp_det_id='$e_name'";			
		$QURYaae = mysqli_query($connect,$SLCTaae);		
		$FTCHaae = mysqli_fetch_object($QURYaae);	
		$emp_name = $FTCHaae->name;
	}
	else if($pay_tp=='neft')
	{ 
		$pay_tp='NEFT'; 
		$emp_name = '';
	}
	
	$SLCT_ac = "SELECT * FROM acc_rep_temp WHERE party_det_id = '$pa_id' ORDER BY acc_rep_temp_id DESC";
	$QURY_ac = mysqli_query($connect,$SLCT_ac);  
	if(mysqli_num_rows($QURY_ac)>0)
	{
		$FTCH_ac = mysqli_fetch_object($QURY_ac);
		if($FTCH_ac->acc_amt<0)
		{
			$acc_amt = $FTCH_ac->acc_amt + $paid_amt;
		}
		else if($FTCH_ac->acc_amt>0)
		{
			$acc_amt = $FTCH_ac->acc_amt - $paid_amt;
		}
		else if($FTCH_ac->acc_amt==0)
		{
			$acc_amt = $paid_amt;
		}
	}
	else
	{
		$acc_amt = $paid_amt;
	}
	
	$INSRTac		= "INSERT INTO acc_rep_temp(`party_det_id`,`ac_received_det_id`, `data_type`, `entry_date`, `acc_amt`, `post_dt`) VALUES('$pa_id','$ac_rec_det_id','received','$pod','$acc_amt','$cur_time')";		
	//echo $INSRTac;
	$QURYac		= mysqli_query($connect,$INSRTac);	
	
	if(isset($_POST['amt']))
	{
		$cre_bl_amt 	=	$_POST['cre_bl_amt'];
		$bl_amt 		=	$_POST['bl_amt'];
		
		if($cre_bl_amt!=0)
		{
			$cre_bl_amt = number_format($cre_bl_amt,2);
		}
		else
		{ 
			$cre_bl_amt = '0.00';
		}
		
		if($bl_amt!=0)
		{
			$bl_amt = number_format($bl_amt,2);
		}
		else
		{ 
			$bl_amt = '0.00';
		}
		
		$tot_rows 	=	$_POST['tot_rows']; //this is for getting number of bill detail count
		$bill_body ='<tbody>';
		for($i = 1; $i<$tot_rows; $i++)
		{
			
			$bill_id	=	$_POST['bill_id'][$i];
			$bill_amt	=	$_POST['bill_amt'][$i];
			$pen_amt	=	$_POST['pen_amt'][$i];
			$pen_sts	=	$_POST['pen_sts'][$i];
			$cl_amt_vl	=	$_POST['cl_amt_vl'][$i];
			$cur_pen_amt = $pen_amt - $cl_amt_vl;
			
			$UPDATE = "UPDATE ac_bills_det SET close_status = 'yes',update_dt = '$cur_time' WHERE party_det_id ='$pa_id' AND billing_det_id = '$bill_id' AND close_status = 'no'";		
			$QRY    = mysqli_query($connect,$UPDATE); 
			
			if($pen_amt == $cl_amt_vl)
			{ 
				$statu_s = 'yes'; 
			}
			else
			{ 
				$statu_s = 'no'; 
			}
			
			$INSRT_bil		= "INSERT INTO ac_bills_det(`ac_party_det_id`, `party_det_id`, `ac_received_det_id`, `billing_det_id`, `bill_amt`, `received_amt`, `pending_amt`, `close_status`, `post_dt`) VALUES('$ac_party_det_id','$pa_id','$ac_rec_det_id','$bill_id','$bill_amt','$cl_amt_vl','$cur_pen_amt','$statu_s','$cur_time')";		
			$QURY_bil		= mysqli_query($connect,$INSRT_bil);
			
			if($cl_amt_vl!=0)
			{
				$cl_amt_vl = number_format($cl_amt_vl,2);
			}
			else
			{ 
				$cl_amt_vl = '0.00';
			}
			
			$SLCT_BLL = "SELECT * FROM billing_det WHERE billing_det_id='$bill_id'";			
			$QURY_BLL = mysqli_query($connect,$SLCT_BLL);		
			$FTCH_BLL = mysqli_fetch_object($QURY_BLL);
			$bill_no  = $FTCH_BLL->bill_no;
			$bill_dat =	$FTCH_BLL->bill_date;
			
			$bill_body.='
				<tr>
					<td align="center" style="border:solid #89ba00 1px;border-left:none; border-top:none;padding:5px">'.$i.'</td>
					<td align="center" style="border:solid #89ba00 1px;border-left:none; border-top:none;padding:5px">'.$bill_no.'</td>
					<td align="center" style="border:solid #89ba00 1px;border-left:none; border-top:none;padding:5px">'.$bill_dat.'</td>        
					<td align="right" style="border:solid #89ba00 1px;border-left:none; border-top:none;padding:5px">'.number_format($bill_amt,2).'</td>
					<td align="right" style="border:solid #89ba00 1px;border-left:none; border-top:none;padding:5px">'.number_format($pen_amt,2).'</td>
					<td align="right" style="border:solid #89ba00 1px;border-left:none; border-top:none; padding:5px">'.$cl_amt_vl.'</td>
					<td align="center" style="border:solid #89ba00 1px;border-left:none; border-right:none; border-top:none;padding:5px">'.$pen_sts.'</td>
				</tr>			
				';
		}
		$bill_body .='
		<tr>
			<td colspan="4" align="center" style="border:solid #89ba00 1px;border-left:none; border-right:none; border-top:none;padding:5px">&nbsp;</td>
			<td colspan="2" align="center" style="border:solid #89ba00 1px;border-left:none; border-top:none; padding:5px"><strong>Total Balance Amount:</strong></td>
			<td align="right" style="border:solid #89ba00 1px;border-left:none; border-right:none; border-top:none;padding:5px"><strong>'.$bl_amt.'</strong></td>
		</tr>
		<tr >
			<td align="center" colspan="4" style="padding:5px;">&nbsp;</td>
			<td align="center" colspan="2" style="border-right:solid #89ba00 1px;padding:5px"><strong>Total Advance Amount:</strong></td>
			<td align="right" style="padding:5px"><strong>'.$cre_bl_amt.'</strong></td>
		</tr>
		</tbody>';
	}
	
	include "../mail/collection_mail.php";
	//echo $mail_body;
	$tto=$cli_emailid;
	$subject="Reg:Payment Received Details";
	$headers = "MIME-Version: 1.0" . "\r\n";
	$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
	// More headers
	$headers .= 'From: Thahaayaa Accounts <accounts@thahaayaa.com>' . "\r\n";		
	$headers .= 'Bcc:'.$cc_id. "\r\n";	
	
	mail("$tto",$subject,$mail_body,$headers);
	//echo $mail_body;
	if($mobile_no!='')
	{
		$to = $mobile_no;
		if(isset($_POST['ad_amt']))
		{
			$sms = "Thahaayaa thanks to you for your payment of Rs.".$paid_amt."/- on ".$payment_dt.". Advance Amount is Rs.".$tot_cr_up."/-. Further contact - 9843214151 Accounts Dept";
		}
		else if(isset($_POST['amt']))
		{
			if($cre_bl_amt==0)
			{
				$sms = "Thahaayaa thanks to you for your payment of Rs.".$paid_amt."/- on ".$payment_dt.". Balance Amount is Rs.".$bl_amt."/-. Further contact - 9843214151 Accounts Dept";
			}
			else if($cre_bl_amt!=0)
			{
				$sms = "Thahaayaa thanks to you for your payment of Rs.".$paid_amt."/- on ".$payment_dt.". Advance Amount is Rs.".$cre_bl_amt."/-. Further contact - 9843214151 Accounts Dept";
			}
		}
		//echo $sms;	
		//$sms = "Thahaayaa thanks to you for new order & Order No:".$po_no.", we took over it into processing, you will receive the delivery shortly. Further contact-9894347273";
		//$sms_url = sprintf("http://alerts.kapsystem.com/api/web2sms.php?workingkey=%s&to=%s&sender=%s&message=%s", $workingkey, $to , $sender, urlencode($sms) );
		$sms_url   	= 	sprintf($sms_domain, $workingkey, $to , $senderid, urlencode($sms));
		openurl($sms_url);
	}
?>
<script type="text/javascript">
	//alert("Data inserted successfully");
	//history.back();
	window.location='../collection_list.php';
</script>
<?php
	
