<?php 
ini_set('max_execution_time', 360); 
include "top.php"; 



$getarea=f(q("SELECT * FROM tbl_server_bill WHERE m_done=0 LIMIT 1"));
$month=$getarea['m_month'];


$arid=$getarea['m_area'];
$counts=0;

if(floatval($arid)>0){
	
	//echo "hello";

    $custcount=f(q("SELECT COUNT(*) as cnt FROM adduser WHERE line_ids='$arid' AND actv=1"));
    $totcount=$custcount['cnt'];


	$custbillcount=f(q("SELECT COUNT(*) as cnt FROM cbill WHERE bl_area='$arid' AND bl_month='$getarea[m_month]' AND bl_year='$getarea[m_year]'"));

    $totbillcount=$custbillcount['cnt'];

	$count=$totbillcount;

    $updatetotal=mysqli_query($con,"UPDATE tbl_server_bill SET m_count='$totcount' WHERE m_id='$getarea[m_id]'");


    $customers=mysqli_query($con,"SELECT * FROM adduser WHERE line_ids='$arid' AND actv=1");
    while($arrc=mysqli_fetch_array($customers)){
 
        $price=$arrc['price'];
        $linemanid=$arrc['line_ids'];
        if($linemanid==''){

            $linemanid=0;

        }

		$chkbill=f(q("SELECT COUNT(*) as cnt FROM cbill WHERE bl_cid='$arrc[id]'"));
		if(floatval($chkbill['cnt']>0)){

			$getdues=f(q("SELECT * FROM cbill WHERE blid=(SELECT MAX(blid) FROM cbill WHERE bl_cid='$arrc[id]')"));
			$totdue=$getdues['bl_due'];


		}else{

			$totdue=floatval($arrc['prevdue']);
		}

	
        
		
		$getdues=f(q("SELECT bl_payamt FROM cbill WHERE bl_cid='$arrc[id]' ORDER BY blid DESC LIMIT 1"));
		$totpay=$getdues['bl_payamt'];
		$oldprice=$price;
		
		
		$duesss=floatval($totdue)-floatval($totpay);
		$gst=floatval($price)*floatval(0.18);
        $gst=round($gst,2);
		//$gst=0;
		$price=round(floatval($price)+floatval($gst),2);

        if($totdue==''){
		
			$totdue=0;
		}
		
		$totals=floatval($price)+floatval($totdue);


		$mnth=$getarea['m_month'];
		$yrs=$getarea['m_year'];

        $strtdt=firstday($mnth,$yrs);//date('Y-m-01');//$arrc['pexpiry'];
        $thirtyDaysUnix = date('Y-m-d', strtotime($strtdt. ' + 29 days')); 
		//echo "START DATE".$strtdt;
		$enddt=lastday($mnth,$yrs);//$thirtyDaysUnix;
        
        $custid=$arrc['id'];	
		$csadded=md5('admin');	
        $txnid = substr(hash('sha256', mt_rand() . microtime()), 0, 20);
		$postpaid=1;
		$amtpaid=0;

		$billamt=$totals;
		$discamt=0;
		$sendsms=0;
		$paymodes='CASH';
		$paydt=date('Y-m-d');
		$prevdue=$totdue;
		$cpaychannels='NA';
		$remarks="Bill Generated for Month ".$mnth.",".$yrs;
        $oth_due=0;
		$oth_reason='NA';
		$lineman=$linemanid;
		$nogstval=$oldprice;

		//echo "SELECT * FROM cbill WHERE bl_cid='$custid' AND bl_stdt='$strtdt'";
        $checkbldt=f(q("SELECT * FROM cbill WHERE bl_cid='$custid' AND bl_stdt='$strtdt'"));

		if($checkbldt['bl_cname']==''){

            $counts++;

		$getmaxcbill=f(q("SELECT * FROM cbill WHERE blid=(SELECT MAX(blid) FROM cbill WHERE bl_cid='$custid')"));

		$lastbamt=$getmaxcbill['bl_pak'];
		$lastbdue=$getmaxcbill['bl_due'];
        $custpamt=$billamt;
        $uprice=floatval($billamt);
        $priceamt=$billamt;

		$bgstamt=$gst;
        $gstamt=round(floatval($bgstamt),2);
		$custpamt=floatval($custpamt)-floatval($bgstamt);    
        $checkexpiry=f(q("SELECT * FROM adduser WHERE id='$arrc[id]'"));

		if($arrc['is_cable']=='1'){

			$bl_tp='1';
		}else{

			$bl_tp='0';
		}


        if($checkexpiry['name']!=''){

			$getmax=f(q("SELECT MAX(blid) as maxid FROM cbill"));

			$bl_mso=$checkexpiry['mso'];
			$bl_area=$checkexpiry['line_ids'];
			$comid=$checkexpiry['com_id'];
			

			$maxid=$getmax['maxid'];
			$maxid=floatval($maxid)+1;

			$sumdue=f(q("SELECT bl_amt FROM cbill WHERE bl_cid='$custid'  ORDER BY blid DESC LIMIT 1"));
			$tdue=$sumdue['bl_amt'];

			$getdues=f(q("SELECT bl_payamt FROM cbill WHERE bl_cid='$custid'  ORDER BY blid DESC LIMIT 1"));
			$totpay=$getdues['bl_payamt'];


			$tdue=(floatval($billamt)+floatval($prevdue))-floatval($discamt);

			if($tdue==''){
				$tdue=0;
			}

			if(floatval($prevdue)>0){

				//$tdue=floatval($tdue)+floatval($prevdue);
			}
			$currentb=floatval($billamt);
			$dueamt=$tdue;

			
			$dues=floatval($dueamt)-floatval($amtpaid);


			

			//die();

			$dates = $checkexpiry['pexpiry'];
			if($dates=='2020-09-01'){
			$expdate=$strtdt;
			}else{

					

						$expdate=$strtdt;
			


			}

			

			

			$thirtyDaysUnix = date('Y-m-d', strtotime($expdate. ' + 29 days')); 
			
			$expirydate=$thirtyDaysUnix;
			$pkid=$checkexpiry['packid'];

			if(floatval($oth_due)>0){

				$prevdue=floatval($prevdue)+floatval($oth_due);
			}

			if($pkid==''){

				$pkid='0';
			}


			$alavals=$_POST['billamt'];
			$totalamt=$alavals;
			$packpr=$totalamt;
			$ncfamt=0;
			$chvals=0;
			$bopv=0;
			$chtot=0;
			$accv=0;
			$bpvals=0;
			$noopv=$_POST['countch'];

				
				$checkexpiry=f(q("SELECT * FROM adduser WHERE id='$custid'"));
				if($postpaid=='0'){

					$sqls="INSERT INTO cbill SET bl_cid='$custid',bl_dt=CURDATE(),bl_pid='$pkid',bl_no='$maxid',bl_cname='$checkexpiry[name]',bl_address='$checkexpiry[address]',bl_phone='$checkexpiry[phone]',bl_paych='$checkexpiry[pay_ch]',bl_amt='$price',bl_payamt='$amtpaid',bl_stat='1',bl_paydt='$paydt',bl_paymd='$paymodes',bl_trn='$txnid',bl_payuid='NA',bl_due='$dues',bl_prevdue='$prevdue',bl_user='$csadded',bl_expiry='$expirydate',bl_tp='$bl_tp',bl_month='$mnth',bl_year='$yrs',bl_stdt='$strtdt',bl_disc='$discamt',bl_gst='$gstamt',bl_pak='$oldprice',bl_mso='$bl_mso',bl_area='$bl_area',bl_remarks='$remarks',oth_reason='$oth_reason',oth_due='$oth_due',bl_com='$comid',bl_line='$lineman'";

						

				}else{

					$sqls="INSERT INTO cbill SET bl_cid='$custid',bl_dt=CURDATE(),bl_pid='$pkid',bl_no='$maxid',bl_cname='$checkexpiry[name]',bl_address='$checkexpiry[address]',bl_phone='$checkexpiry[phone]',bl_paych='$checkexpiry[pay_ch]',bl_amt='$price',bl_payamt='0',bl_stat='0',bl_paydt='$paydt',bl_paymd='NA',bl_trn='$txnid',bl_payuid='NA',bl_due='$currentb',bl_prevdue='$prevdue',bl_user='$csadded',bl_expiry='$expirydate',bl_tp='$bl_tp',bl_month='$mnth',bl_year='$yrs',bl_stdt='$strtdt',bl_disc='$discamt',bl_gst='$gstamt',bl_pak='$oldprice',bl_mso='$bl_mso',bl_area='$bl_area',bl_remarks='$remarks',oth_reason='$oth_reason',oth_due='$oth_due',bl_com='$comid',bl_line='$lineman'";
				}
				//echo $sqls; //die();
			$res=mysqli_query($con,$sqls);

			

			
			if($res){


				$res1=mysqli_query($con,"UPDATE adduser SET mnth='$mnth',yrs='$yrs',pexpiry='$expirydate',actv=1,lpaydt='$paydt',lpayamt='$amtpaid',pay_ch='$cpaychannels' WHERE id='$custid'");


				}else{


					mysqli_error($con);
				}

		}



echo "DONE";
//echo $nmonth = date("m", strtotime($month));


        }









       

    }

if($totcount>=$counts){

    $upstat=mysqli_query($con,"UPDATE tbl_server_bill SET m_done='1' WHERE m_id='$getarea[m_id]'");


}


}

?>