<?php 
$where 	= " WHERE 1=1 ";
$query_string = "";

$next_page_condition 	= "";

if($_REQUEST['hide_top']){
	$next_page_condition.="&hide_top=1";
}
if($_REQUEST['hide_left']){
	$next_page_condition.="&hide_left=1";
}

if($_REQUEST['sales_id']){
	$sales_id = $_REQUEST['sales_id'];
echo	$where .= " AND id = '$sales_id' ";
}


echo $sql = "SELECT * FROM sales_invoices $where ";
$rs = mysqli_query($conn,$sql);
$row = mysqli_fetch_assoc($rs);
$sales_invoice_number = $row["invoice_number"];
$customer_invoice = $gsi->getCustomerById($row["customer_id"]);

$invoice_address = "";
if(isset($row["delivery_address_1"]) && $row["delivery_address_1"] !=""){
	$invoice_address .= $row["delivery_address_1"]."<br/>";
}
if(isset($row["delivery_address_2"]) && $row["delivery_address_2"] !=""){
	$invoice_address .= $row["delivery_address_2"]."<br/>";
}
if(isset($row["delivery_address_3"]) && $row["delivery_address_3"] !=""){
	$invoice_address .= $row["delivery_address_3"]."<br/>";
}
if(isset($row["delivery_add_postcode"]) && $row["delivery_add_postcode"] !=""){
	$invoice_address .= $row["delivery_add_postcode"];
}
$delivery_address = "";
if(isset($row["delivery_address_1"]) && $row["delivery_address_1"] !=""){
	$delivery_address .= $row["delivery_address_1"]."<br/>";
}
if(isset($row["delivery_address_2"]) && $row["delivery_address_2"] !=""){
	$delivery_address .= $row["delivery_address_2"]."<br/>";
}
if(isset($row["delivery_address_3"]) && $row["delivery_address_3"] !=""){
	$delivery_address .= $row["delivery_address_3"]."<br/>";
}
if(isset($row["delivery_add_postcode"]) && $row["delivery_add_postcode"] !=""){
	$delivery_address .= $row["delivery_add_postcode"];
}


 
$created_by=$gsi->getEmployeeById($row['created_by']); 
$ship_method=$gsi->getShipStatusById($row['ship_id']); 
$customer_code =  utility::getTableFieldById("client_management","customer_code","id",$row['customer_id']);
$vender_id = $row['vendor_id'];
$delivery_date = $row['delivery_date'];
$order_notes = $row['order_notes'];
$deliver_to = $row['deliver_to'];
//$supplier_sql = "SELECT id,address_1,address_2,address_3,supplier_postcode FROM supplier_management WHERE id = '$supplier_id' ";
$site_name = "Thermotec Plastivs Ltd";
/* 
$supplier_rs = mysqli_query($conn,$supplier_sql);
if(mysqli_num_rows($supplier_rs) > 0){
	$supplier_row = mysqli_fetch_assoc($supplier_rs);
	$supplier_row = utility::validate_crypt_data($supplier_row);
	
	$supplier_address = "";
	if(isset($supplier_row["address_1"]) && $supplier_row["address_1"] !=""){
		$supplier_address .= $supplier_row["address_1"]."<br/>";
	}
	if(isset($supplier_row["address_2"]) && $supplier_row["address_2"] !=""){
		$supplier_address .= $supplier_row["address_2"]."<br/>";
	}
	if(isset($supplier_row["address_3"]) && $supplier_row["address_3"] !=""){
		$supplier_address .= $supplier_row["address_3"]."<br/>";
	}
	if(isset($supplier_row["supplier_postcode"]) && $supplier_row["supplier_postcode"] !=""){
		$supplier_address .= $supplier_row["supplier_postcode"];
	}
	
}
	 */	

?>
<style>
body{
	font-size:12px;
}
table.equip_table{
	width: 100%;
	border-collapse: collapse;
	margin: 15px 0;
	border: 2px solid #000;
}
table.equip_table_3{
	width: 100%;
	border-collapse: collapse;
	margin: 15px 0;
	border: 2px solid #000;
}
table.equip_table_3, table.equip_table_3 td {
	border: 2px solid #000;
	padding: 10px;
}
table.equip_table, table.equip_table th {
	border: 2px solid #000;
	padding: 10px;
}
table.equip_table td {
	border-right: 2px solid #000;
	padding: 10px;
}

.col-sm-1, .col-sm-2, .col-sm-3, .col-sm-4, .col-sm-5, .col-sm-6, .col-sm-7, .col-sm-8, .col-sm-9, .col-sm-10, .col-sm-11, .col-sm-12 {
	float: left;
}
.col-sm-12 {
	width: 100%;
}
.col-sm-11 {
	width: 91.66666667%;
}
.col-sm-10 {
	width: 83.33333333%;
}
.col-sm-9 {
	width: 75%;
}
.col-sm-8 {
	width: 66.66666667%;
}
.col-sm-7 {
	width: 58.33333333%;
}
.col-sm-6 {
	width: 50%;
}
.col-sm-5 {
	width: 41.66666667%;
}
.col-sm-4 {
	width: 33.33333333%;
}
.col-sm-3 {
	width: 25%;
}
.col-sm-2 {
	width: 16.66666667%;
}
.col-sm-1 {
	width: 8.33333333%;
}

@page {
	size: A4;
   margin: 0;
   /* size: portrait; */
}

@media print{

    table.equip_table{
		width: 100%;
		border-collapse: collapse;
		margin: 15px 0;
		border: 2px solid #000;
	}
	table.equip_table_3{
		width: 100%;
		border-collapse: collapse;
		margin: 15px 0;
		border: 2px solid #000;
	}
	table.equip_table_3, table.equip_table_3 td {
		border: 2px solid #000;
		padding: 10px;
	}
	table.equip_table, table.equip_table th, table.equip_table td {
		border: 2px solid #000;
		padding: 10px;
	}
   
   .col-sm-1, .col-sm-2, .col-sm-3, .col-sm-4, .col-sm-5, .col-sm-6, .col-sm-7, .col-sm-8, .col-sm-9, .col-sm-10, .col-sm-11, .col-sm-12 {
        float: left;
   }
   .col-sm-12 {
        width: 100%;
   }
   .col-sm-11 {
        width: 91.66666667%;
   }
   .col-sm-10 {
        width: 83.33333333%;
   }
   .col-sm-9 {
        width: 75%;
   }
   .col-sm-8 {
        width: 66.66666667%;
   }
   .col-sm-7 {
        width: 58.33333333%;
   }
   .col-sm-6 {
        width: 50%;
   }
   .col-sm-5 {
        width: 41.66666667%;
   }
   .col-sm-4 {
        width: 33.33333333%;
   }
   .col-sm-3 {
        width: 25%;
   }
   .col-sm-2 {
        width: 16.66666667%;
   }
   .col-sm-1 {
        width: 8.33333333%;
   }
}
</style>
<section class="content" id="print_section">
	<div class="row">
		<div class="col-lg-12">
              <div class="card box-primary" style="box-shadow: none;">
                <form class="form-horizontal" method="post" name="form1" id="form1" action="#" onsubmit="return validateForm(this)" enctype="multipart/form-data">
                  <div class="card-body" style="padding:0;">
					<div class="form-group">
						<div class="col-sm-12 no-padding">
							<div class="row mb-2">
								<div class="col-sm-4">
									<img src="../images/thermo_po.jpg" style="width: 280px;margin-top: 0;"/>
								</div>
								<div class="col-sm-4" style="text-align:center;">
									<b><h1 style="text-align:center;margin-top: 0;font-weight:bold;">Delivery Note</h1></b>
									<b>Delivery Note : </b><? echo $sales_invoice_number;?>
								</div>
								<div class="col-sm-4">
									&nbsp;
								</div>
								<div class="col-sm-12" style="text-align:center;">
									<hr style="margin-top: 15px;border-top: 2px solid #000;" />
								</div>
							</div>
							<div class="row mb-2">
								<div class="col-sm-4 ">
									<div style="border:2px solid #000;height: 245px;width: 100%;float: left;">
										<b style="display:block;text-transform:uppercase;border-bottom:2px solid #000;padding: 5px;">DELIVERY TO</b> 
										<span style="padding: 5px;float:left;text-transform:uppercase;font-weight:bold;"><? echo $customer_invoice;?></span><br>
										<p style="padding: 5px;margin-bottom: 0;margin-top: 5px;">
										<? echo $delivery_address;?>
										</p>
									</div>
								</div>
								<div class="col-sm-4 ">
									&nbsp;
								</div>
								<div class="col-sm-4" style="">
									<div style="border:2px solid #000;height: 245px;width: 100%;float: left;">
										<b style="display:block;text-transform:uppercase;border-bottom:2px solid #000;padding: 5px; ">DELIVERY DETAILS</b><br>
										<div style="padding: 5px; margin-right: 5px;">
										<p><strong>Delivery Note : </strong>&nbsp;<? echo $sales_invoice_number;?></p>
										<p><strong>Delivery Date:</strong>&nbsp;<? echo utility::getFormatedDate($delivery_date);?></p>
										
										<p><strong>Ship Method :</strong>&nbsp;<? echo $ship_method;?></p>
										<p><strong>Generated By :</strong>&nbsp;<? echo ucfirst($created_by);?></p>
										<p><strong>Page No:</strong>&nbsp;1 of 1</p>		</div>		
									</div>
								</div>
							</div>
							<?	
								$final_total=0;
								$sales_item_mapping_query = "SELECT * FROM sales_invoice_mapping WHERE sales_id='".$sales_id."'ORDER BY id ASC";
								$sales_item_mapping_sql=mysqli_query($conn,$sales_item_mapping_query);
								$sales_item_mapping_count=mysqli_num_rows($sales_item_mapping_sql);
							?>
							
							<div class="row mb-4">
								<div class="col-sm-12 col-12">
									<table class="equip_table">
										<thead>
										  <tr>
											<th style="text-align:center;">Item</th>
											<th style="width:10%;">Part Number</th>
											<th style="width:20%;">Your Order</th>
											<th style="width:20%;">Issue</th>
											<th style="width:30%;">Description</th>
											<th style="">Qty Deliverd</th>
										  </tr>
										</thead>
										<tbody>
										<? 
										$i = 1; 
										$final_total = 0; 
										$total_vat = 0;
										$grand_total = 0; 

										if ($sales_item_mapping_count > 0) {
											while ($row_si_item_mapping = mysqli_fetch_array($sales_item_mapping_sql)) { 
												
												$total = $row_si_item_mapping['unit_price'] * $row_si_item_mapping['quantity'];
												
												$final_total += $total;

												$total_vat = $final_total * 0.2;
												$grand_total = $final_total + $total_vat;
												?>
												<tr>
													<td><?php echo $i; ?></td>
													<td style="text-align:center;"><?php echo $gsi->getPartNumberById($row_si_item_mapping['part_number']); ?></td>
													<td><?php echo $gsi->getworkOrderById($row_si_item_mapping['order_number_mpp']); ?></td>
													<td><?php echo stripslashes($row_si_item_mapping['issue']); ?></td>
													<td><?php echo stripslashes($row_si_item_mapping['description']); ?></td>
													<td><?php echo $row_si_item_mapping['quantity']; ?></td>
												</tr>
												<?php
												$i++; // Increment row counter
											}
										}
										// Add empty rows if less than 15 rows
											if ($i <= 15) {
												for ($j = $i; $j <= 15; $j++) {
													?>
													<tr>
														<td></td>
														<td style="text-align:center;">&nbsp;</td>
														<td>&nbsp;</td>
														<td>&nbsp;</td>
														<td>&nbsp;</td>
														<td>&nbsp;</td>
													</tr>
													<?php
												}
											}
										?>
										</tbody>

									</table>
								</div>
							</div>
							
							
							
							<div class="row mb-2" style="">
								<div class="col-sm-4">
									<div style="border:2px solid #000;height: 100%;width: 100%;float: left;">
										<b style="display:block;text-transform:uppercase;border-bottom:2px solid #000;padding: 5px;">OUR CONTACT DETAILS</b> 
										<div style="padding: 5px; margin-right: 5px;">
											 <?$id = 1; // ID to fetch
													$query = "SELECT our_details FROM invoice_footer_detais WHERE id = $id";
													$result = mysqli_query($conn, $query);

													// Fetch the row
													$invoiceDetails = '';
													if ($result && mysqli_num_rows($result) > 0) {
														$row = mysqli_fetch_assoc($result);
														echo $invoiceDetails = $row['our_details'];
													}?>
										</div>
									</div>
								</div>
							<div class="col-sm-8">
    <div style="border: 2px solid #000; height: 250px; width: 100%; font-family: Arial, sans-serif; padding: 10px; margin: auto;">
        <p style="border-bottom: 2px solid #000; font-weight: bold; text-align: left; padding: 0px;">
            SIGN & DATE ON RECEIPT
        </p>
        <table style="width: 100%; border-spacing: 0; margin-top: 20px; font-size: 14px;">
            <!-- Sign section with increased height -->
            <tr>
                <td style="width: 20%; padding: 10px 5px;">Sign</td>
                <td style="width: 80%; border-bottom: 1px solid #000; height: 80px; vertical-align: bottom; padding: 10px 5px;"></td>
            </tr>
            <!-- Print section -->
            <tr>
                <td style="width: 20%; padding: 10px 5px;">Print</td>
                <td style="width: 80%; border-bottom: 1px solid #000; padding: 10px 5px;"></td>
            </tr>
            <!-- Date section -->
            <tr>
                <td style="width: 20%; padding: 10px 5px;">Date</td>
                <td style="width: 80%; padding: 10px 5px;"></td>
            </tr>
        </table>
    </div>
</div>



							</div>

							</div>
							
						</div>
					</div>
                  </div>
                </form>
              </div>
		</div>
	</div>
</section>
