<?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");
    $current_date = date("d/m/Y");
    
    $ids = $_GET['id'];
    $statement = $pdo->prepare("SELECT ad.*, c.customer_name, c.mobile_no,c.unique_id, 
                                tbl_ids_products.name as ids_product_name, pn.name AS 'product_name',pn.product_hsn_code, sp.name as sales_person_name 
                                FROM `tbl_advance_voucher` AS ad LEFT JOIN tbl_customers c ON c.id=ad.customer_id 
                                LEFT JOIN tbl_ids_products ON tbl_ids_products.id=ad.product_ids 
                                LEFT JOIN tbl_products_name AS pn ON pn.id = ad.product_name 
                                LEFT JOIN tbl_users sp ON sp.id=ad.created_user_id WHERE ad.id=?");
    $statement->execute(array($ids));
    $datas = $statement->fetchAll(PDO::FETCH_ASSOC);
    $totalData = $statement->rowCount();
    
    if($totalData>0){
        
        $advance_no = $datas[0]['advance_no'];
        $estimate_unique_no = $datas[0]['estimate_unique_no'];
        $advance_date = date('d/m/Y', strtotime($datas[0]['date']));
        $expected_date = date('d/m/Y', strtotime($datas[0]['expected_date']));
        $customer_name = $datas[0]['customer_name'];
        $mobile_no = $datas[0]['mobile_no'];
        $customer_unique_id = $datas[0]['unique_id'];
        $net_advance = $datas[0]['advance_amount'];
        $remarks = $datas[0]['remarks'];
        
        $product_ids = $datas[0]['product_ids'];
        
        if($product_ids == 1){
            $product_item = 'Gold';
        }elseif($product_ids == 2){
            $product_item = 'DIAMOND';
        }elseif($product_ids == 3){
            $product_item = 'SILVER';
        }elseif($product_ids == 4){
            $product_item = 'PRECIOUS';
        }
        
        
        $statement_price = $pdo->prepare("SELECT tbl_prices.* FROM `tbl_prices` WHERE tbl_prices.product_id=?");
		$statement_price->execute(array($product_ids));
		$price_data = $statement_price->fetchAll(PDO::FETCH_ASSOC);
        
        
        if($price_data[0]['product_id'] == 1){
            
            $statement_gold_price = $pdo->prepare("SELECT tbl_prices.* FROM `tbl_prices` WHERE tbl_prices.product_id=? AND tbl_prices.purity_id=?");
    		$statement_gold_price->execute(array($product_ids,2));
    		$gold_price_data = $statement_gold_price->fetchAll(PDO::FETCH_ASSOC);
		
		    $gold_rate = $gold_price_data[0]['amount'];
          
        }else{
            $gold_rate = $price_data[0]['amount'];
        }
        
        
    
        $statement_locations = $pdo->prepare("SELECT * FROM tbl_business_branch b WHERE b.id=?");
        $statement_locations->execute(array($datas[0]['location_id']));
        $business_locations_datas = $statement_locations->fetchAll(PDO::FETCH_ASSOC);
        $business_loations_totalData = $statement_locations->rowCount();
        
        if($business_loations_totalData>0){
            $business_name = $business_locations_datas[0]['business_name'];
        }

?>


<style>
    .container{
      width:18cm;
      height: 29.7cm;
      display: block;
      margin: 0 auto;
      margin-bottom: 0.5cm;
    }
    .p10{
        font-size: 10px;
    }
	.titles{
	    font-size: 10px;
	    font-weight: 600;
	    color: black;
	    vertical-align: top;
	}
	.values{
	    font-size: 10px;
	    font-weight: 600;
	    color: black;
	    vertical-align: top;
	}
	.titless{
	    font-size: 10px;
	    font-weight: 600;
	    color: black;
	    vertical-align: top;
	}
	.valuess{
	    font-size: 10px;
	    font-weight: 600;
	    color: black;
	    vertical-align: top;
	}
	.colon{
	    font-size: 10px;
	    font-weight: 600;
	    color: black;
	    vertical-align: top;
	}
	.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>

<div class="container">
<p style="text-align:left">Duplicate For Recipient</p>
<table style="width:100%;border-collapse: collapse;font-size: 11px;">
	<tr style="border-top: 1px solid black;border-right: 1px solid black;border-left: 1px solid black;">
		<td style="border-top: 1px solid black;">
			<center><b style="font-family: system-ui;">ADVANCE ESTIMATE</b></center>
		</td>
	</tr>
	<tr style="border-bottom: 1px solid black;border-right: 1px solid black;border-left: 1px solid black;">
		<td style="border-bottom: 1px solid black;padding:5px;">
			<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;font-size: 11px;">
	<tr style="border-bottom: 1px solid black;border-right: 1px solid black;border-left: 1px solid black;">
	    <td style="border-bottom: 1px solid black;width: 25%;">
		<td style="border-bottom: 1px solid black;width: 50%;font-size: 12px;">
			<table>
				<tr>
					<td style="width:30%;" class="titless">Advance ID</td>
					<td style="width:2%;"  class="colon">:</td>
					<td style="width:68%;" class="valuess"><?=$advance_no?></td>
				</tr>
				<tr>
					<td class="titless">Customer Account</td>
					<td class="colon">:</td>
					<td class="valuess"><?=$customer_unique_id?></td>
				</tr>
				<tr>
					<td class="titless">Customer Name</td>
					<td class="colon">:</td>
					<td class="valuess"><?=$customer_name?></td>
				</tr>
				<tr>
					<td class="titless">Customer Mobile</td>
					<td class="colon">:</td>
					<td class="valuess"><?=$mobile_no?></td>
				</tr>
				<tr>
					<td class="titless"><?=$product_item?> Rate</td>
					<td class="colon">:</td>
					<td class="valuess"><?=$gold_rate?></td>
				</tr>
				
				<tr>
					<td class="titless">Estimate ID</td>
					<td class="colon">:</td>
					<td class="valuess"><?=$estimate_unique_no?></td>
				</tr>
				<tr>
					<td class="titless">Estimate Date</td>
					<td class="colon">:</td>
					<td class="valuess"><?=$current_date?></td>
				</tr>
				

			</table>
		</td>
		<td style="border-bottom: 1px solid black;width: 25%;">
	</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">Advance</th>
                <th class="payment_table">Product</th>
                <th class="payment_table">Weight</th>
                <th class="payment_table">Amount</th>
            </tr>
            <tr>
                <td class="payment_table"><?php echo($datas[0]['ids_product_name'])."Advance"; ?></td>
				<td class="payment_table"><?=$datas[0]['product_name']?></td>
                <td class="payment_table"><?=$datas[0]['weight']?></td>
                <td class="payment_table"><?=number_format($datas[0]['advance_amount'], 2)?></td>
            </tr>
            <tr>
                <td style="border-bottom: 1px solid black;padding:8px"></td>
                <td style="border-bottom: 1px solid black;padding:8px"></td>
                <td style="border-bottom: 1px solid black;padding:8px"></td>
                <td style="border-bottom: 1px solid black;padding:8px"><?=number_format($datas[0]['advance_amount'], 2)?></td>
            </tr>
</table>

<p5 style="padding-left: 55%;font-size: 11px;">Sales Person: <?=$datas[0]['sales_person_name']?><p5><br>
<p5 style="padding-left: 55%;font-size: 11px;">Generated by: <?=$session_user_name?>,<p5><br>
<p5 style="padding-left: 55%;font-size: 11px;">Print date : <?=$current_date_time?><p5>

</div>



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

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