<? include 'top.php';
$reqtyep = $_REQUEST['action'];
$tablename = $_REQUEST['hashtags'];
$fetchids = $_REQUEST['fetid'];
$lastval = $_REQUEST['lastval'];

if ($reqtyep == 'getprods') {
	$prodid = $_REQUEST['prodid'];
	$sql = f(q("SELECT * FROM tbl_product WHERE maid='$prodid'"));
	$getmtp = f(q("SELECT * FROM tbl_ptype WHERE ptid='$sql[maptype]'"));

	if ($getmtp['ptname'] != '') {

		$brck = $getmtp['ptname'];
	} else {

		$brck = $sql['maweight'];
	}

	$ret = $sql['maname'] . '(' . $brck . ')|' . $sql['masprice'];

	echo $ret;


}

if ($reqtyep == 'getprice') {
	$prodid = $_REQUEST['prodid'];
	$sql = f(q("SELECT * FROM tbl_pack WHERE pid='$prodid'"));

	$ret = $sql['prc'];

	echo $ret;


}


if ($reqtyep == 'getcust') {
	$prodid = $_REQUEST['prodid'];
	$tp = $_REQUEST['tp'];


	if ($prodid != '') {

		if ($tp == '1') {

			$sql = "SELECT * FROM adduser WHERE custid='$prodid' AND is_cable='1'";

		} else {

			$sql = "SELECT * FROM adduser WHERE custid='$prodid' AND is_cable='0'";

		}



		$getc = f(q($sql));

		$price = $getc['price'];
		$discounts = $getc['discounts'];


		$mso = $getc['mso'];
		$area = $getc['area_name'];

		$getmso = f(q("SELECT * FROM tbl_mso WHERE m_id='$mso'"));
		$msoname = $getmso['m_name'];


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

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

		$lastbill = f(q("SELECT * FROM cbill WHERE blid=(SELECT MAX(blid) FROM cbill WHERE bl_cid='$getc[id]')"));
		//echo "SELECT * FROM cbill WHERE blid=(SELECT MAX(blid) FROM cbill WHERE bl_cid='$getc[id]')";

		$billdisc = $lastbill['bl_disc'];
		$lastbillamt = floatval($lastbill['bl_amt']) + floatval($lastbill['bl_prevdue']);

		$duesss = round(floatval($lastbillamt) - (floatval($lastbill['bl_payamt']) + floatval($billdisc)), 2);
		//echo "Duess".$duesss;
		$totdue = $duesss;


		if ($price == '0') {

			$price = $lastbill['bl_amt'];
		}

		if ($price == '') {

			$price = $lastbill['bl_amt'];
		}


		$cnts = f(q("SELECT COUNT(*) as cnt FROM cbill WHERE bl_cid='$getc[id]'"));
		$totbi = $cnts['cnt'];

		$lineid = $getc['line_ids'];

		if (floatval($totbi) > 0) {

		} else {

			$totdue = round($getc['prevdue'], 2);
		}


		//$duesss=floatval($totdue)-floatval($totpay);
		$gst = floatval($price) * floatval(0.18);
		//$gst=0;
//$price=round(floatval($price)+floatval($gst),2);
//$price=round(floatval($price),2);

		$totals = floatval($price) + floatval($totdue);
		//$price=round(floatval($price)+floatval($duesss));

		if ($totdue == '') {

			$totdue = 0;
		}

		$alavalu = 0;
		// $getpack=mysqli_query($con,"SELECT * FROM tbl_pack WHERE ptype='4'");
//                         while($arr=mysqli_fetch_array($getpack)){
		//echo "SELECT * FROM tbl_change_package_details WHERE pac_type='4'AND pac_id='$arr[pid]' AND customer_id='$getc[id]'";
		$chkp = mysqli_query($con, "SELECT * FROM tbl_change_package_details WHERE pac_type='4' AND customer_id='$getc[id]'");
		while ($arrpppp = mysqli_fetch_array($chkp)) {
			if ($alavalu == 0) {

				$alavalu = $arrpppp['pac_id'];
			} else {

				$alavalu = $alavalu . "," . $arrpppp['pac_id'];
			}

		}

		// if($chkp['pac_id']==$arr['pid']){



		//  }


		// }
		if ($discounts == '') {

			$discounts = 0;
		}
		$totals = floatval($totals) - floatval($discounts);



		$ret = $getc['name'] . '|' . $getc['custid'] . '|' . $getc['phone'] . '|' . $getc['vc'] . '|' . $getc['stb'] . '|' . $price . '|' . $getc['is_cable'] . '|' . $getc['pexpiry'] . '|' . $msoname . '|' . $getc['id'] . "|" . $totdue . "|" . $totals . '|' . $getc['lpaydt'] . '|' . $getc['lpayamt'] . "|" . $area . "|" . $getc['pay_ch'] . "|" . $lineid . "|" . $alavalu . "|" . $discounts;

		echo $ret;

	} else {

		echo "";
	}


}
if ($reqtyep == 'getchmulti') {

	$prodid = $_REQUEST['prodid'];

	$sql = "SELECT * FROM adduser WHERE custid='$prodid' AND is_cable='1'";
	$getc = f(q($sql));
	$custid = $getc['id'];

	?>
	<script src="vendors/scripts/script.js"></script>
	<select name="alacarte[]" id="alacarte" class="custom-select2  form-control" multiple onchange="setids()">
		<?php
		$getpack = mysqli_query($con, "SELECT * FROM tbl_pack WHERE ptype='4'");
		while ($arr = mysqli_fetch_array($getpack)) {
			$chkp = f(q("SELECT * FROM tbl_change_package_details WHERE pac_type='4'AND pac_id='$arr[pid]' AND customer_id='$custid'"));
			//echo "SELECT * FROM tbl_change_package_details WHERE pac_type='4'AND pac_id='$arr[pid]' AND customer_id='$custid'";
			// 	if($chkp['pac_id']==$arr['pid']){
			// 	$alavalu=floatval($alavalu)+floatval($arr['prc']);
			// 	$count++;
			//  }
			?>
			<option value="<?= $arr['pid'] ?>" <?php if ($chkp['pac_id'] == $arr['pid']) { ?> selected <?php } ?>
				data="<?= $arr['prc'] ?>"><?= $arr['pname'] ?></option>

		<?php } ?>

	</select>

<?php }

if ($reqtyep == 'getcustp') {
	$prodid = $_REQUEST['prodid'];
	$tp = $_REQUEST['tp'];

	if ($tp == '1') {

		$sql = "SELECT * FROM adduser WHERE custid='$prodid' OR vc='$prodid' OR stb='$prodid' OR phone='$prodid' OR msoacno='$prodid' AND is_cable='1'";

	} else {

		$sql = "SELECT * FROM adduser WHERE custid='$prodid' OR vc='$prodid' OR stb='$prodid' OR phone='$prodid' or msoacno='$prodid' AND is_cable='0'";

	}



	$getc = f(q($sql));

	$price = $getc['price'];

	$mso = $getc['mso'];
	$area = $getc['address'];

	$getmso = f(q("SELECT * FROM tbl_mso WHERE m_id='$mso'"));
	$msoname = $getmso['m_name'];
	$lineid = $getc['line_ids'];


	/* $getdues=f(q("SELECT bl_amt FROM cbill WHERE bl_cid='$getc[id]' ORDER BY blid DESC LIMIT 1"));
	$totdue=$getdues['bl_amt'];

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

	$lastbill = f(q("SELECT * FROM cbill WHERE blid=(SELECT MAX(blid) FROM cbill WHERE bl_cid='$getc[id]')"));



	$duesss = round((floatval($lastbill['bl_amt']) + floatval($lastbill['bl_prevdue'])) - (floatval($lastbill['bl_payamt']) + floatval($lastbill['bl_disc'])));

	//$duesss=floatval($totdue)-floatval($totpay);
// $gst=floatval($price)*floatval(0.18);
	$gst = 0;
	$price = round(floatval($price) + floatval($gst), 2);
	//$price=round(floatval($duesss));

	//$price=floatval($duesss);

	$ret = $getc['name'] . '|' . $getc['custid'] . '|' . $getc['phone'] . '|' . $getc['vc'] . '|' . $getc['stb'] . '|' . $price . '|' . $getc['is_cable'] . '|' . $getc['dob'] . '|' . $msoname . '|' . $getc['id'] . '|' . $area . "|" . $getc['lpaydt'] . '|' . $getc['lpayamt'] . '|' . $duesss . '|' . $lineid;

	echo $ret;


}


if ($reqtyep == 'getstb') {
	$vcno = $_REQUEST['vcno'];
	$getstb = f(q("SELECT * FROM tbl_stbvc WHERE vcs='$vcno'"));
	echo $getstb['stbs'];
}

if ($reqtyep == 'getvc') {
	$stbs = $_REQUEST['stbs'];
	$getstb = f(q("SELECT * FROM tbl_stbvc WHERE stbs='$stbs'"));
	echo $getstb['vcs'];
}



if ($reqtyep == 'generatebills') {

	$getid = f(q("SELECT * FROM tbl_multi WHERE m_done=0 ORDER BY m_ids DESC LIMIT 1"));
	$custid = $getid['m_custid'];
	//echo $custid;
	$tp = 1;
	$prodid = $custid;
	$mntho = $getid['mnth'];
	$yrss = $getid['yrs'];

	if ($prodid != '') {

		if ($tp == '1') {

			$sql = "SELECT * FROM adduser WHERE id='$prodid' AND is_cable='1'";

		} else {

			$sql = "SELECT * FROM adduser WHERE id='$prodid' AND is_cable='0'";

		}

		//echo $prodid;


		$getc = f(q($sql));

		$price = $getc['price'];
		$mainprice = $price;
		//echo $price;
		$mso = $getc['mso'];
		$area = $getc['area_name'];

		$getmso = f(q("SELECT * FROM tbl_mso WHERE m_id='$mso'"));
		$msoname = $getmso['m_name'];


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

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



		$duesss = floatval($totdue) - floatval($totpay);
		$gst = floatval($price) * floatval(0.18);
		//$gst=0;
		$price = round(floatval($price) + floatval($gst), 2);

		$totals = floatval($price) + floatval($totdue);
		//$price=round(floatval($price)+floatval($duesss));`	

		if ($totdue == '') {

			$totdue = 0;
		}
		$strtdt = $yrss . "-" . $mntho . "-1";


		$ret = $getc['name'] . '|' . $getc['custid'] . '|' . $getc['phone'] . '|' . $getc['vc'] . '|' . $getc['stb'] . '|' . $price . '|' . $getc['is_cable'] . '|' . $getc['pexpiry'] . '|' . $msoname . '|' . $getc['id'] . "|" . $totdue . "|" . $totals . '|' . $getc['lpaydt'] . '|' . $getc['lpayamt'] . "|" . $area . "|" . $getc['pay_ch'];



		//echo $billamt; 
		//die();

		$postpaid = '1';
		//$strtdt=$_POST['strtdt'];
		$enddt = date("Y-m-t", strtotime($strtdt));
		$mnth = date('M', strtotime($strtdt));
		$yrs = date('Y', strtotime($strtdt));
		$csadded = $_SESSION['murgibhai'];
		$txnid = substr(hash('sha256', mt_rand() . microtime()), 0, 20);
		$amtpaid = $totals;
		$billamt = $totals;
		$discamt = 0;
		$sendsms = 0;
		$paymodes = 'CASH';
		$paydt = $strtdt;
		$prevdue = $totdue;
		$cpaychannels = $getc['pay_ch'];
		//$remarks=$_POST['remarks'];

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

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

			$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'];
			$remarks = "Pay Bills with 5 days of bill generation";

			// if(floatval($lastbamt)>0){

			// 	$custpamt=$lastbamt;

			// }else{

			$custpamt = $billamt;
			//}


			$uprice = floatval($billamt);



			//$priceamt=(floatval($uprice)/118)*100;
			$priceamt = $billamt;

			$bgstamt = floatval($uprice) - floatval($priceamt);
			$custpamt = floatval($custpamt) - floatval($bgstamt);
			// if(floatval($discamt)>0){

			// 	$priceamt=floatval($priceamt)-floatval($discamt);

			// }
			//$priceamt=floatval($billamt)-floatval($bgstamt);
			$gstamt = round(floatval($bgstamt), 2);


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

			if ($checkexpiry['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['area_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($discamt))+floatval($prevdue);
				$dues = floatval($dueamt) - floatval($amtpaid);


				// echo "Outstanding: ".$tdue."<br>";
				// echo "Current Bill: ".$currentb."<br>";
				// echo "Current Dues: ".$dues."<br>";

				//die();

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

					// $curdate=new DateTime($dates);
					// $newstdate=new DateTime($strtdt);


					// if($curdate>$newstdate){

					// 	$expdate=$dates;
					// }elseif($curdate==$newstdate){

					// 	$expdate=strtotime('+1 days', strtotime($dates));
					// 	$expdate=date("Y-m-d",$expdate);
					// }elseif($curdate<$newstdate){

					$expdate = $strtdt;
					//}


				}

				$expdate = $dates;

				$newdates = date('Y-m-d', strtotime($dates . ' + 1 days'));



				//echo "Expiry".$expdate; 

				$thirtyDaysUnix = date('Y-m-d', strtotime($newdates . ' + 29 days'));
				//echo "EXPIRY".$thirtyDaysUnix; die();
				$expirydate = $thirtyDaysUnix;                         //$_POST['enddt'];
				$pkid = $checkexpiry['packid'];
				if ($pkid == '') {

					$pkid = '0';
				}
				$currentb = round($currentb);

				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='$currentb',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='$priceamt',bl_mso='$bl_mso',bl_area='$bl_area',bl_remarks='$remarks'";



				} 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='$currentb',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='$priceamt',bl_mso='$bl_mso',bl_area='$bl_area',bl_remarks='$remarks'";

				}
				//echo $sqls; 
				$res = mysqli_query($con, $sqls);

				if ($res) {

					if ($postpaid == '0') {

						$gstval = '18';
						$gstamt = floatval($amtpaid) * (18 / 118);

						$instrn = mysqli_query($con, "INSERT INTO tbl_trans SET trn_ac='$custid',trn_name='$checkexpiry[name]',trn_amt='$amtpaid',trn_date=CURDATE(),trn_type='DEPOSIT AC',trn_gst=1,trn_grate='18',trn_gamt='$gstamt',trn_memo='Bill Amount received for $checkexpiry[name]'");

					}

				}


				if ($res) {


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

					$ress = mysqli_query($con, "UPDATE tbl_multi SET m_done=1 WHERE m_custid='$custid'");
					$complete = $getc['name'] . " Bill Generated";

				}

			}



		}

	} else {

		$complete = "<i class='fa fa-tick'></i>&nbsp;All Bills Generated";

	}


	echo $complete;
}

?>