<?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'];
	$where .= " AND id = '$sales_id' ";
}


$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']); */
$customer_code =  utility::getTableFieldById("client_management","customer_code","id",$row['customer_id']);
$vender_id = $row['vendor_id'];
$invoice_date = $row['invoice_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 {
    margin: 0 !important;
    padding: 0 !important;
    font-size: 12px !important;
}

.table {
    margin: 0 !important;
    padding: 0 !important;
}

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;">Sales Invoice</h1></b>
									<b>Invoice Number: </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;">INVOICE 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">
									<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>
										<p style="padding: 5px;margin-bottom: 0;margin-top: 5px;line-height:2;"><strong>
										<? echo $customer_invoice;?></strong><br/>
										<div style="padding: 5px;">
										<p style="padding: 5px;margin-bottom: 0;margin-top: 5px;">
										<? echo $invoice_address;?>
										</p>
										</div>
									</div>
								</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; ">INVOICE DETAILS</b><br>
										<div style="padding: 5px; margin-right: 5px;">
										<p><strong>Invoice No: </strong>&nbsp;<? echo $sales_invoice_number;?></p>
										<p><strong>Invoice Date:</strong>&nbsp;<? echo utility::getFormatedDate($invoice_date);?></p>
										
										<p><strong>Customer Code:</strong>&nbsp;<? echo $customer_code;?></p>
										<p><strong>Vendor ID:</strong>&nbsp;<? echo $vender_id;?></p>
										<p><strong>Page No:</strong>&nbsp;<? echo $page_no;?></p>		</div>		
									</div>
								</div>
							</div>
							<?	
								$final_total=0;
								echo $sales_item_mapping_query = "SELECT sim.*, pp.RefSalesCode 
			FROM sales_invoice_mapping sim
			LEFT JOIN production_planning pp ON sim.order_number_mpp = pp.id
			WHERE sim.sales_id='".$sales_id."' ORDER BY sim.id ASC";die;
								$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" style="min-height: 550px !important;">

								<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="width:10%">Unit Price</th>
											<th style="">Qty</th>
											<th style="width:30%;">Total Price</th>
										  </tr>
										</thead>
										<tbody>
										<?php 
											$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['unit_price']; ?></td>
														<td><?php echo $row_si_item_mapping['quantity']; ?></td>
														<td><?php echo number_format($total, 2); ?></td>
													</tr>
													<?php
													$i++; // Increment row counter
												}
											}

											// Add empty rows if less than 15 rows
											if ($i <= 12) {
												for ($j = $i; $j <= 12; $j++) {
													?>
													<tr>
														<td></td>
														<td style="text-align:center;">&nbsp;</td>
														<td>&nbsp;</td>
														<td>&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: 290px;width: 100%;float: left;">
										<b style="display:block;text-transform:uppercase;border-bottom:2px solid #000;padding: 5px;">OUR 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-4">
									<div style="border:2px solid #000;height: 290px;width: 100%;float: left;">
										<b style="display:block;text-transform:uppercase;border-bottom:2px solid #000;padding: 5px;">Accreditations</b> 
										<div style="padding: 5px; margin-right: 5px;">
											       <?$id = 1; // ID to fetch
													$query = "SELECT our_accreditations FROM invoice_footer_detais WHERE id = $id";
													$result = mysqli_query($conn, $query);

													// Fetch the row
													$our_accreditations = '';
													if ($result && mysqli_num_rows($result) > 0) {
														$row = mysqli_fetch_assoc($result);
														echo $our_accreditations = $row['our_accreditations'];
													}?>
										</div>
									</div>
								</div>

								
								<div class="col-sm-4" style="display: flex; flex-direction: column;">
								<!-- Invoice Totals Section -->
								<div style="border: 2px solid #000; width: 100%; margin-top: 0px; font-family: Arial, sans-serif;">
									<div style="border-bottom: 2px solid #000; padding: 6px; font-weight: bold; text-transform: uppercase; text-align: center;">
										Invoice Totals - All Shown in GBP
									</div>
									<div style="padding: 1px; display: flex; justify-content: space-between; font-size: 1.2em;">
										<div class="col-sm-8">
											<p style="margin: 0px 0;">Total Goods</p>
											<p style="margin: 5px 0;">Total Charges</p>
											<p style="margin: 5px 0;">Total Discounts</p>
											<p style="margin: 5px 0;">Total VAT</p>
											<p style="margin: 5px 0; font-weight: bold;">Grand Total</p>
										</div>
										<div class="">
											<p style="margin: 5px 0;">£<?php echo number_format($final_total, 2); ?></p>
											<p style="margin: 5px 0;">£0.00</p>
											<p style="margin: 5px 0;">£0.00</p>
											<p style="margin: 5px 0;">£<?php echo number_format($total_vat, 2); ?></p>
											<p style="margin: 5px 0; font-weight: bold;">£<?php echo number_format($grand_total, 2); ?></p>
										</div>
									</div>
								</div>

								<!-- Payment Terms Section -->
								<div style="border: 2px solid #000; width: 100%; margin-top: 20px; font-family: Arial, sans-serif; padding: 15px;">
									<div style="padding-bottom: 10px; font-weight: bold;">Payment Terms:</div>
									<p>DDP Delivery Duty Paid (Delivery Address)</p>
								</div>
							</div>

							</div>
							
						</div>
					</div>
                  </div>
				  <div class="row">
							<div class="col-sm-12 ps-3" style="padding-left:15px;">
							   <?$id = 1; // ID to fetch
								$query = "SELECT bottom_text FROM invoice_footer_detais WHERE id = $id";
								$result = mysqli_query($conn, $query);

								// Fetch the row
								$bottom_text = '';
								if ($result && mysqli_num_rows($result) > 0) {
									$row = mysqli_fetch_assoc($result);
									echo $bottom_text = $row['bottom_text'];
								}?>
					         </div>
					         </div>
                </form>
              </div>
		</div>
	</div>
</section>
