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

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

if(isset($_GET['custom'])){
    
    $custom = base64_decode($_GET['custom']);
    
}else{
    $custom = '';
}


$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 rp.*,
			c.customer_name, c.mobile_no,c.address, c.state,
			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_repair_voucher` AS rp 
			LEFT JOIN tbl_customers c ON c.id=rp.customer_id
			LEFT JOIN tbl_ids_products ON tbl_ids_products.id=rp.product_ids 
            LEFT JOIN tbl_products_name AS pn ON pn.id = rp.product_name 
			LEFT JOIN tbl_users sp ON sp.id=rp.sales_man
			WHERE rp.id=?");
    $statement->execute(array($ids));
    $datas = $statement->fetchAll(PDO::FETCH_ASSOC);
    $totalData = $statement->rowCount();
    if($totalData>0){
    
        $repair_id = $datas[0]['id'];
        $repair_date = date('d/m/Y', strtotime($datas[0]['date']));
        $due_date = date('d/m/Y', strtotime($datas[0]['due_date']));
    
        $customer_name = $datas[0]['customer_name'];
        $customer_address = $datas[0]['address'];
        $mobile_no = $datas[0]['mobile_no'];
        $remarks = $datas[0]['remarks'];  
        
        $main_product_name = $datas[0]['ids_product_name'];
        $product_name = $datas[0]['product_name'];
        $repair_type = $datas[0]['type'];
        $product_ids = $datas[0]['product_ids'];
        $repair_completed_date = $datas[0]['repair_completed_date'];
        $completed_date = '';
        if($repair_completed_date != '0000-00-00' && !empty($repair_completed_date)){
            $completed_date = $repair_completed_date;
        }
              
        
        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';
        }
        if($repair_type == 1){
            $repair_item = 'Repair';
        }elseif($repair_type == 2){
            $repair_item = 'Soliding';
        }else{
            $repair_item = 'Other';
        }
        
        
        $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'];
        }
        
        if($custom == 'customer'){
            $dynamic_head = 'CUSTOMER';
        }elseif($custom == 'company'){
            $dynamic_head = 'COMPANY';
        }elseif($custom == 'close'){
            $dynamic_head = 'CLOSE';
        }


?>


<style>
    .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;
	}
	.signature-section {
        display: flex;
        justify-content: space-around;
        padding:25px 10px 5px;
        border-left: 1px solid black;
        border-right: 1px solid black;
        border-bottom: 1px solid black;
        border-collapse: collapse;
        vertical-align: top;
    }
    .signature-line {
        border: 1px solid #dcdcdc;
        width: 250px;
        margin: 0 auto;
        padding:20px
    }
    .signature-section .center {
        text-align: center;
    }
</style>


<p>Duplicate For Recipient</p>
<table style="width: 100%;margin-bottom:10px;">
    <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" alt="SPM Gold" style="text-align:center; width: 50%;object-fit:contain">
            </td>
            <td style="width: 35%;text-align: right;vertical-align: top;"> </td>

        </tr>
    </tbody>
</table>
<table style="width:100%;border-collapse: collapse;">
	<tr style="border-top: 1px solid black;border-left: 1px solid black;">
		<td style="border-top: 1px solid black;border-left: 1px solid black;border-right: 1px solid #000;">
			<center><b style="font-family: system-ui;"><?=$dynamic_head?> REPAIR RECEIPT</b></center>
		</td>
	</tr>
	<tr style="border-bottom: 1px solid blackborder-left: 1px solid black;border-right: 1px solid black;">
		<td style="border-bottom: 1px solid black;border-left: 1px solid black;padding:3px">
			<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="border-bottom: 1px solid black;border-left: 1px solid #000;width:50%;font-size: 11px;">
			<table>
				<tr>
					<td style="width:30%;" class="titless">Billed To</td>
					<td style="width:2%;"  class="colon">:</td>
					<td style="width:68%;" class="valuess"><?=$customer_name?></td>
				</tr>
				<tr>
					<td class="titless">Customer Address</td>
					<td class="colon">:</td>
					<td class="valuess"><?=nl2br($customer_address)?></td>
				</tr>
				<tr>
					<td class="titless">Customer 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>
			
			

			</table>
		</td>
		<td style="border-bottom: 1px solid black; border-left: 1px solid black;border-right: 1px solid #000;vertical-align: top;width:50%;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"><?=$datas[0]['bill_no']?></td>
				</tr>
			
				<tr>
					<td class="titless">Repair Date</td>
					<td class="colon">:</td>
					<td class="valuess"><?=$repair_date?></td>
				</tr>
				<?php if(!empty($completed_date)){ ?>
				<tr>
					<td class="titless">Completed Date</td>
					<td class="colon">:</td>
					<td class="valuess"><?=$completed_date?></td>
				</tr>
				<?php } ?>
				<tr>
					<td class="titless">Expected Due Date</td>
					<td class="colon">:</td>
					<td class="valuess"><?=$due_date?></td>
				</tr>
			
				<tr>
					<td class="titless">Currency</td>
					<td class="colon">:</td>
					<td class="valuess">INR</td>
				</tr>
				<tr>
					<td class="titless">Max <?=$product_item?> weight</td>
					<td class="colon">:</td>
					<td class="valuess"><?=number_format($datas[0]['weight'],2)?></td>
				</tr>
				<tr>
					<td class="titless">Place of Supply</td>
					<td class="colon">:</td>
					<td class="valuess">Tamil Nadu</td>
				</tr>
			
			</table>
		
		</td>
	</tr>
</table>
<table style="width: 100%;border-collapse:collapse;">
    
	<tr style="border-bottom: 1px solid black;border-right: 1px solid black;border-left: 1px solid black;border-collapse: collapse;">
	    
		<!--<td style="width: 4%; border-bottom: 1px solid black;border-left: 1px solid black; font-size: 11px;font-weight:bold; text-align: center;"><b>S.No</b></td>-->
		<td style="width: 10%; border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px;font-weight:bold; text-align: center;"><b>Product</b></td>
		<td style="width: 7%; border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px;font-weight:bold; text-align: center;"><b>Product Details</b></td>
		<td style="width: 5%; border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px;font-weight:bold; text-align: center;"><b>Weight</b></td>
		<td style="width: 4%; border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px;font-weight:bold; text-align: center;"><b>Repair Type</b></td>
		<td style="width: 4%; border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px;font-weight:bold; text-align: center;"><b>Remarks</b></td>
	
    </tr>
    
	

	<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;border-left: 1px solid black;font-size: 11px; text-align: center;"></?=$i++?></td>-->
			<td style="border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px; text-align: center;"><?=$main_product_name?></td>
			<td style="border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px; text-align: center;"><?=$product_name?></td>
			<td style="border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px; text-align: right;"><?=number_format($datas[0]['weight'],2)?></td>
			<td style="border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px; text-align: right;"><?=$repair_item?></td>
			<td style="border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px; text-align: right;"><?=$remarks?></td>

	</tr>

	
</table>
<table style="width:100%;border-collapse:collapse;">

    <!-- Signature Section -->
    <tr style="border-collapse: collapse;">
        <td class="signature-section">
            <div>
                <br><br>
                <strong>Customer Signatory</strong>
            </div>
            <div>
                <span >Incharge Signature :</span>
                <div class="signature-line"></div>
            </div>
            <div class="center">
                <br><br>
                <strong>Authorised Signatory
            </div>
        </td>
    </tr>

</table>
<p style="font-size: 10px;">
    I acknowledge all the terms and conditions of this repair
</p>
<p5 style="padding-left:70%;font-size: 11px;">Sales Person: <?=$datas[0]['sales_person_name']?><p5><br>
<p5 style="padding-left:70%;font-size: 11px;">Generated by: <?=$session_user_name?><p5><br>
<p5 style="padding-left:70%;font-size: 11px;">Print date : <?=$current_date_time?><p5>


<?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>
