<?php
	//Connects to your Database
	include "../db_connection/db_connection.php";
	include "../config/config.php";		
	//$page_id	=	base64_decode(base64_decode(base64_decode($_GET['page_id']))); //THIS IS PAGE ID  
	                  
	//co_cod,bill_dt,advn_dt,advn_amt,to_bil_amt_st,bill_typ_id
	
	$bill_typ_id	=	$_POST['bill_typ_id']; 
	if($bill_typ_id==1) //this is for checking bill type None Tax billing	
	{ 		
		$tot_cr_amt_viw = 0;
		$co_cod	=	base64_decode(base64_decode(base64_decode($_POST['co_cod']))); //THIS IS for party ID 				
		$bill_typ_id	=	$_POST['bill_typ_id'];	
		
		$SLCTaap = "SELECT * FROM party_det WHERE party_det_id='$co_cod'";			
		$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;
		
		
		list($tot_bil_amt,$tot_re_amt,$tot_adv_amt,$tot_pen_amt,$tot_dis_amt) = total_acc_call($co_cod);
				
						
		$tot_cr_amt_viw = $tot_cr_amt_viw+$tot_adv_amt;
			
		$bill_yr_no 	=	$_POST['bill_yr_no'];
		
		$bil_no			=	$_POST['bil_no'];
		$bill_dt		=	$_POST['bill_dt'];
		$bill_dt		= 	str_replace("/", "-", $bill_dt);
		$advn_dt		=	$_POST['advn_dt'];
		$advn_dt		= 	str_replace("/", "-", $advn_dt);
		$advn_amt		=	$_POST['advn_amt']; 
		$to_bil_amt 	=	$_POST['to_bil_amt_tx'];
		$cour_wgt	 	=	$_POST['cour_wgt'];
		$cour_per_amt	=	$_POST['cour_amt_for_1'];
		$cour_amt	 	=	$_POST['cour_amt'];
		$tot_bil_cur 	=	$_POST['to_bil_amt'];
		//$loop_cnt	=	$_POST['loop_cnt'];  
		//echo $loop_cnt;
		
		$bill_tp = "Non Tax";
		
		$INSRT		= "INSERT INTO billing_det(`bill_type`, `party_det_id`, `bill_yr_no`,`bill_no`, `bill_date`, `adv_date`, `adv_amt`, `bill_amt`,`cour_wgt`,`cour_per_amt`,`courier_amt`,`total_bill_amt`, `post_dt`) VALUES('$bill_tp','$co_cod','$bill_yr_no','$bil_no','$bill_dt','$advn_dt','$advn_amt','$tot_bil_cur','$cour_wgt','$cour_per_amt','$cour_amt','$tot_bil_cur','$cur_time')";		
		//echo $INSRT;
		$QURY		= mysqli_query($connect,$INSRT);
		
		
		$SLCT = "SELECT * FROM ac_party_det WHERE party_det_id='$co_cod'";			
		$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_bil_cur;
			
			$tot_pe_up = $FTCH->total_pending + $tot_bil_cur; 
				
			
			$UPDATE_1 = "UPDATE ac_party_det SET `total_bill_amt`='$tot_bl_up', `total_pending`='$tot_pe_up', update_dt = '$cur_time' WHERE party_det_id='$co_cod'";		
			//echo $UPDATE_1;
			$QRY_1    = mysqli_query($connect,$UPDATE_1); 
						
		}
		else
		{
			$INSRT		= "INSERT INTO ac_party_det(`party_det_id`, `total_bill_amt`,`total_pending`, `post_dt`) VALUES('$co_cod','$tot_bil_cur','$tot_bil_cur','$cur_time')";		
			$QURY		= mysqli_query($connect,$INSRT);
								
		}		
		
		$SLCT_sk = "SELECT  MAX(billing_det_id) as max_ids FROM billing_det ORDER BY billing_det_id ASC";
		$QURY_sk = mysqli_query($connect,$SLCT_sk);  
		$FTCH_sk = mysqli_fetch_object($QURY_sk);
		$max_id = $FTCH_sk->max_ids;
		
		$SLCT_ac = "SELECT * FROM acc_rep_temp WHERE party_det_id = '$co_cod' ORDER BY acc_rep_temp_id DESC";
		//echo "SELECT * FROM acc_rep_temp WHERE party_det_id = '$co_cod' 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 + $tot_bil_cur;
			}
			else if($FTCH_ac->acc_amt>0)
			{
				$acc_amt = $FTCH_ac->acc_amt - $tot_bil_cur;
			}
			else if($FTCH_ac->acc_amt==0)
			{
				$acc_amt = $tot_bil_cur;
			}
		}
		else
		{
			$acc_amt = $tot_bil_cur;
		}
		
		$INSRTac		= "INSERT INTO acc_rep_temp(`billing_det_id`, `party_det_id`,`data_type`, `entry_date`, `acc_amt`, `post_dt`) VALUES('$max_id','$co_cod','billing','$bill_dt','$acc_amt','$cur_time')";		
		//echo $INSRT;
		$QURYac		= mysqli_query($connect,$INSRTac);		
		
		$l=1;
		foreach($_POST['cat_id'] as $cat_cl)
		{
			//echo $l;		
			//cat_id,cat_name,nofdes,unitpr,tot_pr,
			//echo $cat_cl."<br/>";
			$po_del_id	=	$_POST['po_del_id'][$l];
			
			$po_ids_s	=	$_POST['po_ids_s'][$l];
			$cat_id		=	$_POST['cat_id'][$l];
			$cat_name	=	$_POST['cat_name'][$l];
			$nofdes		=	$_POST['nofdes'][$l];
			$unitpr		=	$_POST['unitpr'][$l];
			$tot_pr		=	$_POST['tot_pr'][$l];
			
			$INSRT	= "INSERT INTO billing_po_det(`billing_det_id`, `po_entry_id`,`po_delivery_id`, `master_cat_id`, `master_cat_name`, `no_of_des`, `per_uint_pr`, `tot_amt`, `post_dt`) VALUES('$max_id','$po_ids_s','$po_del_id','$cat_id','$cat_name','$nofdes','$unitpr','$tot_pr','$cur_time')";		
			$QURY		= mysqli_query($connect,$INSRT);
			//echo $INSRT;
			
			$SLCT_skt = "SELECT  MAX(billing_po_det_id) as max_ids FROM billing_po_det ORDER BY billing_po_det_id ASC";
			$QURY_skt = mysqli_query($connect,$SLCT_skt);  
			$FTCH_skt = mysqli_fetch_object($QURY_skt);
			$max_idt = $FTCH_skt->max_ids;
			
			$SLCT_dd = "SELECT * FROM po_delivery_det,po_entry_det WHERE po_delivery_det.po_delivery_id = '$po_del_id' AND po_entry_det.design_det_id = po_delivery_det.design_det_id AND po_entry_det.master_cat_id = '$cat_id' AND po_entry_det.po_entry_id = '$po_ids_s' AND po_entry_det.del_status = 'yes' AND po_entry_det.bill_status = 'no'";
			$QURY_dd = mysqli_query($connect,$SLCT_dd);
			while($FTCH_dd = mysqli_fetch_object($QURY_dd))
			{
				//echo $cat_id."and".$po_ids_s."<br/>";
				$INSRT	= "INSERT INTO billing_po_design_det(`billing_po_det_id`,`po_entry_id`,`master_cat_id`,`design_det_id`, `post_dt`) VALUES('$max_idt','$po_ids_s','$cat_id','$FTCH_dd->design_det_id','$cur_time')";		
				$QURY		= mysqli_query($connect,$INSRT);
				
				$UPDATE = "UPDATE po_entry_det SET bill_status = 'yes',update_dt = '$cur_time' WHERE po_entry_id = '$po_ids_s' AND design_det_id='$FTCH_dd->design_det_id'";
				//echo $UPDATE; 			
				$QRY    = mysqli_query($connect,$UPDATE); 	
				
				
			}
			$l++;
		}
		if($tot_cr_amt_viw>0)
		{
			$pa_id=$co_cod;
			$amt=$tot_bil_cur;
			$bill_id=$max_id;
			$bill_dt=$bill_dt;
			
			$bill_cost 		= 	$amt - $tot_cr_amt_viw;
			$cre_bl_amt 	=	$tot_cr_amt_viw;
			
			if($bill_cost<0)
			{
				$advan_amt = trim($bill_cost,"-");
				$pend_amt  = 0;
				$cl_amt_vl = $amt;
				$bill_sts  = 'Closed';
				$statu_s  = 'yes';
			}
			else if($bill_cost>0)
			{
				$advan_amt = 0;
				$pend_amt  = trim($bill_cost);
				$cl_amt_vl = $tot_cr_amt_viw;
				$bill_sts  = 'Pending';
				$statu_s  = 'no';
			}
			else if($bill_cost==0)
			{
				$advan_amt = 0;
				$pend_amt  = 0;
				$cl_amt_vl = $tot_cr_amt_viw;
				$bill_sts  = 'Closed';
				$statu_s  = 'yes';
			}
			
			$SLCT_blk = "SELECT * FROM billing_det WHERE billing_det_id = '$max_id'";
			//echo $SLCT_blk;
			$QURY_blk = mysqli_query($connect,$SLCT_blk);  
			$FTCH_blk = mysqli_fetch_object($QURY_blk);
			
			$bill_no = $FTCH_blk->bill_no;
			
			
			include "../mail/collection_mail_billing.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($bill_cost>0)
			{
				$SLCTaa = "SELECT * FROM ac_party_det WHERE party_det_id='$co_cod'";			
				$QURYaa = mysqli_query($connect,$SLCTaa);		
				$FTCHaa = mysqli_fetch_object($QURYaa);	
				$ac_party_det_id = $FTCHaa->ac_party_det_id;
						
				$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','$bill_dt','Bill','','$cl_amt_vl','$cur_time')";		
				//echo $INSRT_re;
				$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;
			
				$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); 
							
				$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','$amt','$cl_amt_vl','$pend_amt','$statu_s','$cur_time')";		
				//echo $INSRT_bil."<br>";
				$QURY_bil		= mysqli_query($connect,$INSRT_bil);
			}
			else if($bill_cost<=0)
			{
				$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','0','$bill_id','$amt','$cl_amt_vl','$pend_amt','$statu_s','$cur_time')";		
				//echo $INSRT_bil."<br>";
				$QURY_bil		= mysqli_query($connect,$INSRT_bil);
			}
			
			if($mobile_no!='')
			{
				$to = $mobile_no;
				if($advan_amt==0)
				{
					$sms = "Thahaayaa thanks to you for your payment of Rs.".$cl_amt_vl."/- on ".$bill_dt.". Balance Amount is Rs.".$pend_amt."/-. Further contact - 9843214151 Accounts Dept";
				}
				else if($advan_amt!=0)
				{
					$sms = "Thahaayaa thanks to you for your payment of Rs.".$cl_amt_vl."/- on ".$bill_dt.". Advance Amount is Rs.".$advan_amt."/-. Further contact - 9843214151 Accounts Dept";
				}
				//echo $sms;
				//$sms = "Thahaayaa thanks to you for your payment of Rs.".$cl_amt_vl."/- on ".$payment_dt.". Balance Amount is Rs.".$tot_pe_up."/-. Further contact - 9843214151 Accounts Dept";
				//$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);
			}
		}
	}
	else if($bill_typ_id==2) //this is for checking bill type and for Tax billing
	{ 	
		$tot_cr_amt_viw = 0;
		//bil_no,bill_dt,tax_p	
		$co_cod	=	base64_decode(base64_decode(base64_decode($_POST['co_cod']))); //THIS IS for party ID 				
		$bill_typ_id	=	$_POST['bill_typ_id'];	
		
		$SLCTaap = "SELECT * FROM party_det WHERE party_det_id='$co_cod'";			
		$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;
		
		
		list($tot_bil_amt,$tot_re_amt,$tot_adv_amt,$tot_pen_amt,$tot_dis_amt) = total_acc_call($co_cod);
				
						
		$tot_cr_amt_viw = $tot_cr_amt_viw+$tot_adv_amt;	
	
		$bil_no			=	$_POST['bil_no'];
		$bill_dt		=	$_POST['bill_dt'];
		$bill_dt		= 	str_replace("/", "-", $bill_dt); 
		$to_bil_amt 	=	$_POST['bl_amt_wo_cur'];
		$cour_wgt	 	=	$_POST['cour_wgt'];
		$cour_per_amt	=	$_POST['cour_amt_for_1'];
		$cour_amt	 	=	$_POST['cour_amt'];
		$tot_bil_cur 	=	$_POST['to_bil_amt'];
		
		$roundOff = explode(".",$tot_bil_cur);
		
		if($roundOff[1] == '')
		{
			$round_amt = "(-)0.00";
			$tot_bil_cur_roundOff = $tot_bil_cur;
		}
		else if($roundOff[1] < 50)
		{

			$round_amt = "(-)0.".$roundOff[1];
			$round_amt1 = "0.".$roundOff[1];
			$tot_bil_cur_roundOff = floatval($tot_bil_cur) - floatval($round_amt1);
		}
		else if($roundOff[1] >= 50)
		{
			$rndOff1 = "0.".$roundOff[1];
			$rndOff2 = floatval(1)-floatval($rndOff1);
			$round_amt = "(+)".number_format($rndOff2, 2);
			$tot_bil_cur_roundOff = floatval($tot_bil_cur) + floatval($rndOff2);
		}
		
		
		$gst_tax_lable	= $_POST['gst_tax_lable'];
		$tax_p 			=	$_POST['tax_p'];
		
		if($gst_tax_lable == "IGST")
		{
			$fin_tax_amt 	=	($_POST['fin_tax_amt'] + (($cour_amt/100) * 18));
		} 
		else if($gst_tax_lable == "TAX")
		{
			$fin_tax_amt = 0;
		}
		else
		{
			$fin_tax_amt = ($_POST['netamt_cour_tax_amt'] + (($cour_amt/100) * 9)).", ".($_POST['netamt_cour_tax_amt'] + (($cour_amt/100) * 9));
		}
		
		if($gst_tax_lable == "IGST")
		{
			$bill_tot_tax_amt  = $fin_tax_amt;
		}
		else if($gst_tax_lable == "SGST,  CGST")
		{
			$bill_tot_tax_amt1 = split(", ", $fin_tax_amt);
			$bill_tot_tax_amt  = $bill_tot_tax_amt1[0] * 2;
		}
		else
		{	
			$bill_tot_tax_amt  = "0";
		}		
		
		$bill_tp = "Tax";
		
		$INSRT		= "INSERT INTO billing_det(`bill_type`, `party_det_id`,`bill_no`, `bill_date`, `bill_amt`, `bill_tax_lable`, `bill_tax`, `bill_tax_amt`, `bill_tot_tax_amt`,`cour_wgt`,`cour_per_amt`,`courier_amt`,`round_off`,`total_bill_amt`,`post_dt`) VALUES('$bill_tp','$co_cod','$bil_no','$bill_dt','$tot_bil_cur_roundOff','$gst_tax_lable','$tax_p','$fin_tax_amt', '$bill_tot_tax_amt','$cour_wgt','$cour_per_amt','$cour_amt','$round_amt','$tot_bil_cur','$cur_time')";
//		echo $INSRT;

		$QURY		= mysqli_query($connect,$INSRT);	
		
		$SLCT = "SELECT * FROM ac_party_det WHERE party_det_id='$co_cod'";			
		$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_bil_cur;
			
			$tot_pe_up = $FTCH->total_pending + $tot_bil_cur; 
			
			//$tot_pe_up = $FTCH->total_rece_amt + $tot_bil_cur; 
				
			
			$UPDATE_1 = "UPDATE ac_party_det SET `total_bill_amt`='$tot_bl_up', `total_pending`='$tot_pe_up', update_dt = '$cur_time' WHERE party_det_id='$co_cod'";		
			//echo $UPDATE_1;
			$QRY_1    = mysqli_query($connect,$UPDATE_1); 
						
		}
		else
		{
			$INSRT		= "INSERT INTO ac_party_det(`party_det_id`, `total_bill_amt`,`total_pending`, `post_dt`) VALUES('$co_cod','$tot_bil_cur','$tot_bil_cur','$cur_time')";		
			$QURY		= mysqli_query($connect,$INSRT);
								
		}			
		
		$SLCT_sk = "SELECT  MAX(billing_det_id) as max_ids FROM billing_det ORDER BY billing_det_id ASC";
		$QURY_sk = mysqli_query($connect,$SLCT_sk);  
		$FTCH_sk = mysqli_fetch_object($QURY_sk);
		$max_id = $FTCH_sk->max_ids;
		
		$SLCT_ack = "SELECT acc_amt FROM acc_rep_temp WHERE party_det_id = '$co_cod' ORDER BY acc_rep_temp_id DESC";
		//echo "SELECT * FROM acc_rep_temp WHERE party_det_id = '$co_cod' ORDER BY acc_rep_temp_id DESC";
		$QURY_ack = mysqli_query($connect,$SLCT_ack);  
		if(mysqli_num_rows($QURY_ack)>=0)
		{
			$FTCH_ack = mysqli_fetch_object($QURY_ack);
			if($FTCH_ack->acc_amt<0)
			{
				$acc_amt = $FTCH_ack->acc_amt + $tot_bil_cur;
			}
			else if($FTCH_ack->acc_amt>0)
			{
				$acc_amt = $FTCH_ack->acc_amt - $tot_bil_cur;
			}
			else if($FTCH_ack->acc_amt==0)
			{
				$acc_amt = $tot_bil_cur;
			}
		}
		else
		{
			$acc_amt = $tot_bil_cur;
		}
		
		$INSRTac		= "INSERT INTO acc_rep_temp(`billing_det_id`, `party_det_id`,`data_type`, `entry_date`,`acc_amt`, `post_dt`) VALUES('$max_id','$co_cod','billing','$bill_dt','$acc_amt','$cur_time')";		
		//echo $INSRT;
		$QURYac		= mysqli_query($connect,$INSRTac);	
		
		$l=1;
		foreach($_POST['cat_id'] as $cat_cl)
		{
			$po_del_id	=	$_POST['po_del_id'][$l];
			$po_ids_s	=	$_POST['po_ids_s'][$l];
			$cat_id		=	$_POST['cat_id'][$l];
			$cat_name	=	$_POST['cat_name'][$l];
			$nofdes		=	$_POST['nofdes'][$l];
			$unitpr		=	$_POST['unitpr'][$l];
			$tot_pr		=	$_POST['tot_pr'][$l];
			
			$INSRT	= "INSERT INTO billing_po_det(`billing_det_id`, `po_entry_id`,`po_delivery_id`, `master_cat_id`, `master_cat_name`, `no_of_des`, `per_uint_pr`, `tot_amt`, `post_dt`) VALUES('$max_id','$po_ids_s','$po_del_id','$cat_id','$cat_name','$nofdes','$unitpr','$tot_pr','$cur_time')";		
			$QURY		= mysqli_query($connect,$INSRT);
			
			$SLCT_skt = "SELECT  MAX(billing_po_det_id) as max_ids FROM billing_po_det ORDER BY billing_po_det_id ASC";
			$QURY_skt = mysqli_query($connect,$SLCT_skt);  
			$FTCH_skt = mysqli_fetch_object($QURY_skt);
			$max_idt = $FTCH_skt->max_ids;
			
			$SLCT_dd = "SELECT * FROM po_delivery_det,po_entry_det WHERE po_delivery_det.po_delivery_id = '$po_del_id' AND po_entry_det.design_det_id = po_delivery_det.design_det_id AND po_entry_det.master_cat_id = '$cat_id' AND po_entry_det.po_entry_id = '$po_ids_s' AND po_entry_det.del_status = 'yes' AND po_entry_det.bill_status = 'no'";
			$QURY_dd = mysqli_query($connect,$SLCT_dd);
			while($FTCH_dd = mysqli_fetch_object($QURY_dd))
			{
				$INSRT	= "INSERT INTO billing_po_design_det(`billing_po_det_id`,`po_entry_id`,`master_cat_id`, `design_det_id`, `post_dt`) VALUES('$max_idt','$po_ids_s','$cat_id','$FTCH_dd->design_det_id','$cur_time')";		
				$QURY		= mysqli_query($connect,$INSRT);
								
				$UPDATE = "UPDATE po_entry_det SET bill_status = 'yes',update_dt = '$cur_time' WHERE po_entry_id = '$po_ids_s' AND design_det_id='$FTCH_dd->design_det_id'";
				$QRY    = mysqli_query($connect,$UPDATE);
			}
			$l++;
		}
		
		if($tot_cr_amt_viw>0)
		{
			$pa_id=$co_cod;
			$amt=$tot_bil_cur;
			$bill_id=$max_id;
			$bill_dt=$bill_dt;
			
			$bill_cost 		= 	$amt - $tot_cr_amt_viw;
			$cre_bl_amt 	=	$tot_cr_amt_viw;
			
			if($bill_cost<0)
			{
				$advan_amt = trim($bill_cost,"-");
				$pend_amt  = 0;
				$cl_amt_vl = $amt;
				$bill_sts  = 'Closed';
				$statu_s  = 'yes';
			}
			else if($bill_cost>0)
			{
				$advan_amt = 0;
				$pend_amt  = trim($bill_cost);
				$cl_amt_vl = $tot_cr_amt_viw;
				$bill_sts  = 'Pending';
				$statu_s  = 'no';
			}
			else if($bill_cost==0)
			{
				$advan_amt = 0;
				$pend_amt  = 0;
				$cl_amt_vl = $tot_cr_amt_viw;
				$bill_sts  = 'Closed';
				$statu_s  = 'yes';
			}
			
			$SLCT_blk = "SELECT * FROM billing_det WHERE billing_det_id = '$max_id'";
			//echo $SLCT_blk;
			$QURY_blk = mysqli_query($connect,$SLCT_blk);  
			$FTCH_blk = mysqli_fetch_object($QURY_blk);
			
			$bill_no = $FTCH_blk->bill_no;
			
			include "../mail/collection_mail_billing.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."<br>";
			
			if($bill_cost>0)
			{
				$SLCTaa = "SELECT * FROM ac_party_det WHERE party_det_id='$co_cod'";			
				$QURYaa = mysqli_query($connect,$SLCTaa);		
				$FTCHaa = mysqli_fetch_object($QURYaa);	
				$ac_party_det_id = $FTCHaa->ac_party_det_id;
						
				$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','$bill_dt','Bill','','$cl_amt_vl','$cur_time')";		
				//echo $INSRT_re;
				$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;
			
				$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); 
							
				$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','$amt','$cl_amt_vl','$pend_amt','$statu_s','$cur_time')";		
				//echo $INSRT_bil."<br>";
				$QURY_bil		= mysqli_query($connect,$INSRT_bil);
			}
			else if($bill_cost<=0)
			{
				$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','0','$bill_id','$amt','$cl_amt_vl','$pend_amt','$statu_s','$cur_time')";		
				//echo $INSRT_bil."<br>";
				$QURY_bil		= mysqli_query($connect,$INSRT_bil);
			}
			
			if($mobile_no!='')
			{
				$to = $mobile_no;
				if($advan_amt==0)
				{
					$sms = "Thahaayaa thanks to you for your payment of Rs.".$cl_amt_vl."/- on ".$bill_dt.". Balance Amount is Rs.".$pend_amt."/-. Further contact - 9843214151 Accounts Dept";
				}
				if($advan_amt!=0)
				{
					$sms = "Thahaayaa thanks to you for your payment of Rs.".$cl_amt_vl."/- on ".$bill_dt.". Advance Amount is Rs.".$advan_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='../billing_list.php';
</script>