<?php
ob_start();
session_start();
include "config/config.php";
include "config/session.php";

	if(!isset($session)) {
		header('location: login.php');exit;
	}

	$statement_users = $pdo->prepare("SELECT * FROM  tbl_users WHERE tbl_users.id=?");
	$statement_users->execute(array($session_id));
	$user_datas = $statement_users->fetchAll(PDO::FETCH_ASSOC);
	$session_user_name = $user_datas[0]['name'];
	$current_date_time = date("d/m/Y h:i:s A");

	$ids = $_GET['id'];

	$statement = $pdo->prepare("SELECT cs.*,c.*, tbl_schemes.name AS 'scheme_name',tbl_schemes.type AS 'scheme_type',
					COALESCE(payment_counts.payment_paid_month_counts, 0) AS payment_paid_month_counts,
					payment_counts.total_paid_scheme_weight,
					(SELECT COALESCE(SUM(amount),0) FROM `tbl_payments` p WHERE p.accounts=3 and p.scheme_id=cs.id) as paid_total_amount
					FROM `tbl_customer_scheme` cs
					LEFT JOIN tbl_customers c ON c.id=cs.customer_id
					LEFT JOIN tbl_schemes ON tbl_schemes.id=cs.scheme_id
					LEFT JOIN (
						SELECT scheme_id, COUNT(DISTINCT month) AS payment_paid_month_counts, SUM(scheme_weight) as total_paid_scheme_weight
						FROM tbl_payments
						WHERE accounts = 3
						GROUP BY scheme_id
					) AS payment_counts ON payment_counts.scheme_id = cs.id
					WHERE cs.id=?");
	$statement->execute(array($ids));
	$datas = $statement->fetchAll(PDO::FETCH_ASSOC);
	$totalData = $statement->rowCount();

	if($totalData>0){

		$location_id =$datas[0]['location_id'];

		$customer_id = $datas[0]['customer_id'];
        $customer_name = $datas[0]['customer_name'];
        $customer_address = $datas[0]['address'];
        $mobile_no = $datas[0]['mobile_no'];

		$scheme_id = $datas[0]['scheme_id'];
		$scheme_no = $datas[0]['scheme_no'];
        $scheme_added_date = date('d/m/Y', strtotime($datas[0]['scheme_added_date']));
		$scheme_amount = $datas[0]['scheme_amount'];
        $scheme_months = $datas[0]['scheme_months'];
		$scheme_type = $datas[0]['scheme_type'];

		$statement_locations = $pdo->prepare("SELECT * FROM tbl_business_branch b WHERE b.id=?");
        $statement_locations->execute(array($location_id));
        $business_locations_datas = $statement_locations->fetchAll(PDO::FETCH_ASSOC);
        $business_loations_totalData = $statement_locations->rowCount();

        $business_name = isset($business_locations_datas[0]['business_name']) ? $business_locations_datas[0]['business_name'] : '';
        $business_gst = isset($business_locations_datas[0]['business_gst']) ? $business_locations_datas[0]['business_gst'] : '';

		$statement_price = $pdo->prepare("SELECT tbl_prices.* FROM `tbl_prices` WHERE tbl_prices.product_id=? AND tbl_prices.purity_id=?");
		$statement_price->execute(array(1,2));
		$price_data = $statement_price->fetchAll(PDO::FETCH_ASSOC);

		$scheme_weight = ($scheme_amount/$price_data[0]['amount']);
		$scheme_weight = $scheme_type == 2 ? number_format($scheme_weight,3) : '--';
		$total_sc_wt  = $scheme_type ==2 ? number_format($datas[0]['total_paid_scheme_weight'], 3) : '--';


?>

<style>
	.payment_table {
		width: 5%; 
		border-bottom: 1px solid black;
		border-left: 1px solid black;
		font-size: 11px;
		font-weight:bold; 
		text-align: center;
		padding:8px
	}
</style>

<table style="width: 100%;">
    <tbody>
        <tr>
            <td style="width: 30%; vertical-align: top;"></td>
            <td style="width: 35%;text-align: center;vertical-align: top;">
                <img src="logo/spm-logos.png" style="text-align:center;width:50%;object-fit:contain">
            </td>
            <td style="width: 35%;text-align: right;vertical-align: top;"> </td>

        </tr>
    </tbody>
</table>
<p><strong>Original For Recipient</strong></p>
<table style="width:100%;border-collapse: collapse;">
	<tr style="border-left:1px solid black;border-right:1px solid black;">
		<td style="border-top: 1px solid black;padding-bottom:10px">
			<center><b style="font-family: system-ui;"><?php echo($scheme_type== 1 ? 'PAYMENT' : 'WEIGHT'); ?> SCHEME RECEIPT</b></center>
		</td>
	</tr>
	<tr style="border-left:1px solid black;border-right:1px solid black;">
		<td style="border-bottom: 1px solid black;">
			<center>
			    <span style="font-size: 12px;">
			    Section 31(3)(d) and Rule 50 of CGST Act and Rules.2017
			    </span>
			</center>
		</td>
	</tr>
</table>

<table style="width: 100%;border-collapse: collapse;">
	<tr style="border-bottom: 1px solid black;">
		<td style="width:50%;border-bottom: 1px solid black;font-size: 11px;border-left: 1px solid black">
			<table>
				<tr>
					<td style="width:30%;" class="titless">Billed To</td>
					<td style="width:2%;"  class="colon">:</td>
					<td style="width:68%;" class="valuess"><?php echo ucwords($customer_name);?></td>
				</tr>
				<tr>
					<td class="titless">Address</td>
					<td class="colon">:</td>
					<td class="valuess"><?=nl2br($customer_address)?></td>
				</tr>
				<tr>
					<td class="titless">Mobile</td>
					<td class="colon">:</td>
					<td class="valuess"><?=$mobile_no?></td>
				</tr>
				<tr>
					<td class="titless">State</td>
					<td class="colon">:</td>
					<td class="valuess"><?=$datas[0]['state']?></td>
				</tr>
				<tr>
					<td class="titless">Id Card No</td>
					<td class="colon">:</td>
					<td class="valuess">AIKPM7706P</td>
				</tr>

			</table>
		</td>
		<td style="width: 50%;border-bottom: 1px solid black; border-left: 1px solid black;border-right: 1px solid black;vertical-align: top;font-size: 11px;">
			<table style="width:100%;vertical-align: top;">
				<tr>
					<td style="width:40%;" class="titless">Receipt ID</td>
					<td style="width:2%;" class="colon">:</td>
					<td style="width:58%;" class="valuess"><?=$scheme_no?></td>
				</tr>
				<tr>
					<td class="titless">Application ID</td>
					<td class="colon">:</td>
					<td class="valuess"><?=$scheme_no?></td>
				</tr>
				<tr>
					<td class="titless">Date</td>
					<td class="colon">:</td>
					<td class="valuess"><?=$scheme_added_date?></td>
				</tr>
				<tr>
					<td>GSTIN</td>
					<td>:</td>
					<td><?=$business_gst?></td>
				</tr>
				<tr>
					<td class="titless">Currency</td>
					<td class="colon">:</td>
					<td class="valuess">INR</td>
				</tr>
				
				<tr>
					<td class="titless">Place of Supply</td>
					<td class="colon">:</td>
					<td class="valuess">Tamil Nadu</td>
				</tr>
				<!--<tr>
					<td>Tax is payable On Reverse Charge</td>
					<td>:</td>
					<td>No</td>
				</tr>-->
				
			</table>
		</td>
	</tr>
</table>

<table style="width:100%; border-collapse: collapse;border-left:1px solid black;border-right: 1px solid black;font-size: 11px;">
			<tr>
                <th class="payment_table">Gold Rate</th>
                <th class="payment_table">Gold Wt(Grm)</th>
                <th class="payment_table">Amount(Rs)</th>
                <th class="payment_table">Cust Amt(Rs)</th>
                <th class="payment_table">Cust Gold Wt(Grm)</th>
                <th class="payment_table">Net Amt(Rs)</th>
            </tr>
            <tr>
                <td class="payment_table"><?=number_format($price_data[0]['amount'], 2)?></td>
				<td class="payment_table"><?=$scheme_weight?></td>
                <td class="payment_table"><?=number_format($scheme_amount, 2)?></td>
                <td class="payment_table"><?=number_format($datas[0]['paid_total_amount'], 2)?></td>
                <td class="payment_table"><?=$total_sc_wt?></td>
				<td class="payment_table"><?=number_format($datas[0]['paid_total_amount'], 2)?></td>
            </tr>
</table>


<table style="width:100%; font-size:11px;border-collapse:collapse;border-left:1px solid black;border-right:1px solid black">
	<tr style="border-collapse: collapse;">
		<?php if(!$datas[0]['payment_paid_month_counts'] > 0){ ?>
		<td style="width:50%;padding-top:20px">
			<table>
				<tr>
					<td><b>Payment Mode:</b></td>
                </tr>
				<tr>
					<td style="width:30%;border-collapse:collapse;border-top:1px solid black;border-left:1px solid black;border-right:1px solid black;"><?=$datas[0]['payment_name']?></td>
				</tr>
				<tr>
					<td style="width:30%;border-collapse:collapse;border-top:1px solid black;border-left:1px solid black;border-right:1px solid black;border-bottom:1px solid black"><?=number_format($scheme_amount, 2)?></td>
				</tr>
			</table>
		</td>
		<?php } ?>
		<td style="width:50%;padding-top:20px">
			<table>
				<tr>
					<b style="padding-top:5px">Effective gold per rate gram : <?=number_format($price_data[0]['amount'], 2)?></b><br>
					<b style="">For ligible gold weight : <?=$total_sc_wt?></b><br>
					<b style=""><b>Proposed date of sales: <?=$current_date_time?></b>
				</tr>
			</table>
		</td>
	</tr>
</table>

<table style="width:100%;font-size:11px;border-collapse:collapse;border-bottom:1px solid black;border-left:1px solid black;border-right:1px solid black">
	<tr style="border-bottom: 1px solid black;border-right: 1px solid black;border-left: 1px solid black;border-collapse: collapse;">
	
		<td style="border-bottom: 1px solid black;font-weight: bold;padding:40px 0px;text-align: center;">
			<b>FOR <?=$business_name?></b>
			<br><br><br>
			<b>Authorised Signatory</b>
		</td>
	</tr>
</table>

<?php } else { ?>
	<center>No Records Found</center>
<?php } ?>


<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js" ></script>
<script> 
    window.print(); 
    setTimeout(function(){window.close();},5000);
</script>
