<?php 

error_reporting(0);



function RECEIPT_FETCH($no,$payType){
	include '../Admin_config/connection.php';
	
	 $sql= "SELECT * FROM `sc_receipt` WHERE `rc_number`='$no' AND `vvp_amount` IS NULL";
	if($payType=="admin"){
	    	$sql= "SELECT * FROM `sc_receipt` WHERE `rc_number`='$no' AND `vvp_amount` NOT LIKE ''";

	}
	$result=mysqli_query($conn,$sql);
	$count=mysqli_num_rows($result);

	if ($count > 0) {
			while($row = mysqli_fetch_assoc($result))
			{
				$receipt_id = $row;
			}
	}
	return($receipt_id);
}


function REPORT_FETCH_client_done($tstays){
	include '../Admin_config/connection.php';
	
	 $sql= "SELECT * FROM `payment_by_admin`";

	
	$result=mysqli_query($conn,$sql);
	$count=mysqli_num_rows($result);

	if ($count > 0) {
			while($row = mysqli_fetch_assoc($result))
			{
			    $no=$row['rc_no'];
			    $vvp_amount=$row['vvp_amount'];

			    	    	$sql1= "SELECT `sr_id`, `receipt_id`, `rc_number`, `rc_type`, `rc_fromBranch`, `rc_toBranch`, `rc_date`, `rc_from`, `rc_to`, `rc_mob`, `rc_gst`, `rc_particulars`, `rc_kg`, `rc_parcels`, `rc_vvp`, `rc_cour_total`, `rc_gst_total`, `rc_total`, `rc_remarks`, `vvp_amount`, `vvp_date`, `to_name`, `vvp_payment_status`, `cc_status`, `vpp_charges`,'$vvp_amount' AS compAmt FROM `sc_receipt` WHERE `rc_number`='$no'";
	                        $result1=mysqli_query($conn,$sql1);
	                        $row1 = mysqli_fetch_assoc($result1);
                    //print_r($row1);

				$receipt_id[] = $row1;
			}
	}
	return($receipt_id);
}


function REPORT_FETCH_client_done_filter($tstays,$fromDate,$toDate){
	include '../Admin_config/connection.php';
	
	 $sql= "SELECT * FROM `payment_by_admin` WHERE `vvp_date` BETWEEN '$fromDate' AND '$toDate'";

	
	$result=mysqli_query($conn,$sql);
	$count=mysqli_num_rows($result);

	if ($count > 0) {
			while($row = mysqli_fetch_assoc($result))
			{
			     $no=$row['rc_no'];
			    $vvp_amount=$row['vvp_amount'];
			    	    	$sql1= "SELECT `sr_id`, `receipt_id`, `rc_number`, `rc_type`, `rc_fromBranch`, `rc_toBranch`, `rc_date`, `rc_from`, `rc_to`, `rc_mob`, `rc_gst`, `rc_particulars`, `rc_kg`, `rc_parcels`, `rc_vvp`, `rc_cour_total`, `rc_gst_total`, `rc_total`, `rc_remarks`, `vvp_amount`, `vvp_date`, `to_name`, `vvp_payment_status`, `cc_status`, `vpp_charges`,'$vvp_amount' AS compAmt FROM `sc_receipt` WHERE `rc_number`='$no'";
	                        $result1=mysqli_query($conn,$sql1);
	                        $row1 = mysqli_fetch_assoc($result1);
				$receipt_id[] = $row1;
			}
	}
	return($receipt_id);
}


function RECEIPT_ADD($rc_no,$rc_type,$br_from,$br_to,$date,$from,$to,$mob,$gst,$particulars,$kg,$parcel_no,$vvp_status,$cour_total,$gst_total,$rc_total,$remarks,$status,$cc_status,$vpp_charges,$to_mobile){
	include '../Admin_config/connection.php';
	
	$desc="SELECT (MAX(receipt_id)) AS `last_id` FROM sc_receipt";
	$desc_res=mysqli_query($conn,$desc);
	$row = mysqli_fetch_assoc($desc_res);
	$temp = explode('_', $row['last_id']);
	$getId = end($temp);
	$incId = $getId + 1;

	if ($incId < 10) {
		$newId = "rc_0".$incId;
	}else{
		$newId = "rc_".$incId;
	}

	$cust="UPDATE `sc_customers` SET `cust_rc_no`='$rc_no' WHERE `cust_fullname`='$from'";
	$cust_res=mysqli_query($conn,$cust);

     $sql= "INSERT INTO `sc_receipt`(`receipt_id`, `rc_number`, `rc_type`, `rc_fromBranch`, `rc_toBranch`, `rc_date`, `rc_from`, `rc_to`, `rc_mob`, `rc_gst`, `rc_particulars`, `rc_kg`, `rc_parcels`, `rc_vvp`, `rc_cour_total`, `rc_gst_total`, `rc_total`, `rc_remarks`, `vvp_payment_status`, `cc_status`,`vpp_charges`,`to_mobile`) VALUES ('$newId','$rc_no','$rc_type','$br_from','$br_to','$date','$from','$to','$mob','$gst','$particulars','$kg','$parcel_no',$vvp_status,'$cour_total','$gst_total','$rc_total','$remarks','$status','$cc_status','$vpp_charges','$to_mobile')";
	$result=mysqli_query($conn,$sql)or die(mysqli_error($conn));

	if ($result) {
	     $des=$particulars;
         $ex=explode('_', $des);
        $new = strtoupper(end($ex));
	
		
		$links='../views/test.php?rc='.$rc_no.'&&from='.$from.'&&to='.$to.'&&kg='.$kg.'&&brfrom='.$br_from.'&&brto='.$br_to.'&&date='.$date.'&&no_par='.$parcel_no.'&&vpp='.$vvp_status.'&&tot='.$rc_total.'&&type='.$particulars.'&&ccstatus='.$cc_status.'&&gstno='.$gst.'&&mob='.$mob.'&&to_mobile='.$to_mobile;
			echo "<script>window.open('$links','_blank')</script>";
	    
	    
	}
}

function RECEIPT_EDIT($receipt_no, $to_branch, $vvp, $kg, $parcel_no, $cc, $gst, $tot){
	include '../Admin_config/connection.php';

	$sql= "UPDATE `sc_receipt` SET `rc_toBranch`='$to_branch',`rc_kg`='$kg',`rc_parcels`='$parcel_no',`rc_vvp`='$vvp',`rc_cour_total`='$cc',`rc_gst_total`='$gst',`rc_total`='$tot' WHERE `rc_number`='$receipt_no'";
	$result=mysqli_query($conn,$sql);

	if ($result) 
	{	
		echo '<script type="text/javascript">alert("Receipt Updated Successfully");
		window.location.href="../views/editReceipt.php";
		</script>';
	}

}


function RECEIPT_UPDATE($rc, $vvp, $date, $name){
	include '../Admin_config/connection.php';

	$sql= "UPDATE `sc_receipt` SET `vvp_amount`='$vvp',`vvp_date`='$date',`to_name`='$name',`vvp_payment_status`='done' WHERE `rc_number`='$rc'";
	$result=mysqli_query($conn,$sql);

	if ($result) 
	{	
		echo '<script type="text/javascript">alert("Receipt Updated Successfully");
		window.location.href="../views/payment.php";
		</script>';
	}

}
function RECEIPT_UPDATE_company($rc, $vvp, $date, $name,$admin_userName){
	include '../Admin_config/connection.php';
	 $sql1= "SELECT * FROM `payment_by_admin` WHERE `rc_no`='$rc'";
	$result1=mysqli_query($conn,$sql1);
    if(mysqli_num_rows($result1)=="0"){
	 $sql= "INSERT INTO `payment_by_admin`(`vvp_amount`, `vvp_date`, `to_name`, `rc_no`, `payment_status`, `payment_by`, `created_at`) VALUES ('$vvp','$date','$name','$rc','Done','$admin_userName','$log_date')";
	$result=mysqli_query($conn,$sql);

	if ($result) 
	{	
		echo '<script type="text/javascript">alert("Payment Updated Successfully");
		window.location.href="../views/payment_by_admin.php";
		</script>';
	}
}else{
    	echo '<script type="text/javascript">alert("Payment Already Recived");
		window.location.href="../views/payment_by_admin.php";
		</script>';
}
}

function REPORT_FETCH($status){	
	include '../Admin_config/connection.php';

	$sql= "SELECT * FROM `sc_receipt` WHERE `vvp_payment_status`='$status'";
	$result=mysqli_query($conn,$sql);
	$count=mysqli_num_rows($result);
	
	if ($count > 0) 
	{
		$report_data = array();
		while ($row = mysqli_fetch_assoc($result)) 
		{
			$report_data[] = $row;
		}
	}
	return ($report_data);

}

function REPORT_FETCH_company($status){	
	include '../Admin_config/connection.php';

	$sql= "SELECT * FROM `sc_receipt`";
	$result=mysqli_query($conn,$sql);
	$count=mysqli_num_rows($result);
	
	if ($count > 0) 
	{
		$report_data = array();
		while ($row = mysqli_fetch_assoc($result)) 
		{
		    
		    $rc_no=$row['rc_no'];
            $sql1= "SELECT * FROM `payment_by_admin` WHERE `rc_no`='$rc_no'";
            $result1=mysqli_query($conn,$sql1);
            if(mysqli_num_rows($result1)=="0"){
                			$report_data[] = $row;

            }
		    
		    
		}
	}
	return ($report_data);

}

function REPORT_vpp_FETCH($status,$fromDate,$toDate){	
	include '../Admin_config/connection.php';

	 $sql= "SELECT * FROM `sc_receipt` WHERE `vvp_payment_status`='$status' AND `rc_date` BETWEEN '$fromDate' AND '$toDate'";
	$result=mysqli_query($conn,$sql);
	$count=mysqli_num_rows($result);
	
	if ($count > 0) 
	{
		$report_data = array();
		while ($row = mysqli_fetch_assoc($result)) 
		{
			$report_data[] = $row;
		}
	}
	return ($report_data);

}


function REPORT_FETCH_DATE($fromDate,$toDate){
	include '../Admin_config/connection.php';
	$sql= "SELECT * FROM `sc_receipt` WHERE `rc_date` BETWEEN '$fromDate' AND '$toDate'";
	$result=mysqli_query($conn,$sql);
	$count=mysqli_num_rows($result);
	
	if ($count > 0) 
	{
		$report_data = array();
		while ($row = mysqli_fetch_assoc($result)) 
		{
			$report_data[] = $row;
		}
	}
	return ($report_data);

}


function sunshine_balance($username){
	include '../Admin_config/connection.php';
		$todays=date("Y-m-d");

	$opening_bal=$paid_bal=$todaypaid_bal=0;
	$sql= "SELECT SUM(`vvp_amount`) AS openingBalance FROM `sc_receipt`";
	$result=mysqli_query($conn,$sql);
	$count=mysqli_num_rows($result);
	
	if ($count > 0) 
	{
		$report_data = array();
	$row = mysqli_fetch_assoc($result);
	
			$opening_bal += $row['openingBalance'];
	
	}
	
	$sql1= "SELECT SUM(`vvp_amount`) AS closingBal FROM `payment_by_admin` WHERE DATE_FORMAT(`created_at`,'%Y-%m-%d')<'$todays'";
	$result1=mysqli_query($conn,$sql1);
	$count1=mysqli_num_rows($result1);
	
	if ($count1 > 0) 
	{
	$row1 = mysqli_fetch_assoc($result1);
	
			$paid_bal += $row1['closingBal'];
	
	}
	
	 $sql2= "SELECT SUM(`vvp_amount`) AS closingBal FROM `payment_by_admin` WHERE DATE_FORMAT(`created_at`,'%Y-%m-%d')='$todays'";
	$result2=mysqli_query($conn,$sql2);
	$count2=mysqli_num_rows($result2);
	
	if ($count2 > 0) 
	{
	$row2 = mysqli_fetch_assoc($result2);
	
			$todaypaid_bal += $row2['closingBal'];
	
	}
	
	$result=array("open"=>$opening_bal,"close"=>$paid_bal,"today"=>$todaypaid_bal);
// 	echo ($opening_bal);echo ($paid_bal);echo ($todaypaid_bal);
return($result);
}

?>
