<?php

$where = " WHERE 1=1";
$next_page_condition = "";

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

$qot_id = $_REQUEST['qot_id'];

$query = "SELECT * FROM quotation where id  = '".$qot_id."' ";
$sql= mysqli_query($conn,$query);
$result= mysqli_num_rows($sql);
$row = mysqli_fetch_assoc($sql);
$row = utility::validate_crypt_data($row);
$customer_name = $gsi->getCustomerById($row['customer_id']);
$part_number =  $row['part_number'];
$part_description =  $row['part_description']; 
$drawing_issue =  $row['drawing_issue']; 
$model_number =  $row['model_number'];
$volume =  $row['volume']; 
$quotation_number =  $row['quotation_number']; 

$query_brought_out_part = "SELECT * FROM quot_brought WHERE quot_id ='".$qot_id."' ";
$sql_brought_out_part= mysqli_query($conn,$query_brought_out_part);
$result_brought_out_part= mysqli_num_rows($sql_brought_out_part);
$row_brought_out_part = mysqli_fetch_assoc($sql_brought_out_part);
$row_brought_out_part = utility::validate_crypt_data($row_brought_out_part);
$totals =  $row_brought_out_part['totals'];
$avg =  $row_brought_out_part['avg']; 
 
$query_brought_total = "SELECT * FROM quot_brought_total WHERE quot_id ='".$qot_id."' ";
$sql_brought_total= mysqli_query($conn,$query_brought_total);
$result_brought_total= mysqli_num_rows($sql_brought_total);
$row_brought_total = mysqli_fetch_assoc($sql_brought_total);
$row_brought_total = utility::validate_crypt_data($row_brought_total);
$totals_brought =  $row_brought_total['totals_brought'];
$average_brought =  $row_brought_total['average_brought'];
 
$query_raw_materials = "SELECT * FROM quot_raw_materials WHERE quot_id ='".$qot_id."' ";
$sql_raw_materials=mysqli_query($conn,$query_raw_materials);
$result_raw_materials=mysqli_num_rows($sql_raw_materials);

$query_operation = "SELECT * FROM quot_operations WHERE quot_id ='".$qot_id."' ";
$sql_operation=mysqli_query($conn,$query_operation);
$result_operation=mysqli_num_rows($sql_operation);

$query_other = "SELECT * FROM quot_other WHERE quot_id ='".$qot_id."' ";
$sql_other= mysqli_query($conn,$query_other);
$result_other= mysqli_num_rows($sql_other);
$row_other = mysqli_fetch_assoc($sql_other);
$row_other = utility::validate_crypt_data($row_other);
$grade_general_overheads =  $row_other['grade_general_overheads'];
$cost_general_overheads =  $row_other['cost_general_overheads']; 
$rate_to_use_profit =  $row_other['rate_to_use_profit']; 
$grade_other =  $row_other['grade_other']; 
$grade_profit =  $row_other['grade_profit']; 
$cost_profit =  $row_other['cost_profit']; 
$cost_other = $row_other['cost_other'];
 
$querytool = "SELECT * FROM quot_tooling WHERE quot_id ='".$qot_id."' ";
$sqltool= mysqli_query($conn,$querytool);
$resulttool= mysqli_num_rows($sqltool);
$total_tool_cost_sum = 0;
while($row_tooling =mysqli_fetch_array($sqltool)){ 
$row_tooling=utility::validate_crypt_data($row_tooling);
 $total_tool_cost_sum += $row_tooling['total_cost'];
}
$sql_log= "SELECT * FROM quot_logistics WHERE quot_id ='".$qot_id."' ";
$result_log = mysqli_query($conn,$sql_log);
$row_log = mysqli_fetch_array($result_log);
$row_log = utility::validate_crypt_data($row_log);
$transport  = $row_log['transport'];
$pcost  = $row_log['pcost'];
$packaging  = $row_log['packaging'];
$pacost  = $row_log['pacost'];
$pallet  = $row_log['pallet'];
$othcost  = $row_log['othcost'];
$logistics  = $row_log['logistics'];
$palcost  = $row_log['palcost'];


$sql_feasibility= "SELECT * FROM quot_feasibility WHERE quot_id ='".$qot_id."' ";
$result_feasibility = mysqli_query($conn,$sql_feasibility);
$row_feasibility = mysqli_fetch_array($result_feasibility);
$row_feasibility = utility::validate_crypt_data($row_feasibility);
$issue_concern  = $row_feasibility['issue_concern'];
$fsb_type  = $row_feasibility['fsb_type'];
$hide_button  = $_REQUEST['hide_top'];
if($hide_button == 1){
	$hide ="hide";
}else{
	$hide ="";
};


?>

<style type="text/css">
	.custom-bordered td {
		border: 1px solid #dee2e6;
			}

	.custom-bordered tfoot td[colspan="6"].text-end {
			border-left: none !important;
			border-bottom: none !important;
		}
    .label {
			display: inline-block;
			width: 120px; /* Adjust this width as needed */
			font-weight: ;
		}
	@page {
		margin: 0;
		/* size: landscape; */
		size: potrait;
		/* DIN A4 standard, Europe */
	}

	@media print {
		.bgblack {
			background-color: #000;
		}

		.font50print40 {
			font-size: 40px;
		}

		.content-wrapper,
		.right-side {
			background-color: #fff;
		}

		.box {
			border-top: 0 solid #d2d6de !important;
			box-shadow: none !important;
		}

		p {
			white-space: inherit !important;
		}

		.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%;
		}
	}

	.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>
<div class="col-lg-12 <?php $hide?>" style="">
  <?if($_SERVER['HTTP_HOST'] =="localhost"){?>
		 <button type="button" id="back" name="back" value="back"  style="float:right; margin-top:-1%;margin-bottom:8px;" class="btn btn-success <?=$hide;?>"><a href="<?=$config->frontEndURL?>/admin/quotation_management/index?page=front_sheet_pdf&qot_id=<?=$_REQUEST['id'];?>&hide_top=1&hide_left=1" target="_blank" style="color:white;">Generate PDF</a></button>
	<?}else{?>
		<button type="button" id="back" name="back" value="back"  style="float:right; color:white; margin-top:-1%;margin-bottom:8px;" class="btn btn-success <?=$hide;?>"><a href="<?=$config->frontEndURL;?>quotation_front_sheet_pdf.php?qot_id=<?=$_REQUEST['id'];?>&pdf_name=front_sheet_pdf" target="_blank"> <i class="fa fa-fw fa-file-pdf"></i>Download PDF</a></button>
	<?}?>
	
</div>
<div class="container mt-3">
	<div class="row justify-content-center">
		<div class="col-sm-12">
			<div class="card">
				<div class="card-body">
					<div class="row pb-2 p-2">
						<div class="col-sm-10">
							<h3 class="text-center font-weight-bold mb-1">Quotation Analysis Form</h3>

						</div>
						<div class="col-sm-2">
							<h3 class="text-right font-weight-bold mb-1"><?php echo $quotation_number;?></h3>

						</div>
					</div>
					<div class="row" style="margin-left: -2.5px;">    
						<div class="col-sm-4">
							<p class="mb-0"><strong>Thermotec Plastics Ltd</strong></p>
							<p class="mb-0">Unit 8A, Electra Park Ind Est</p>
							<p class="mb-0">Electric Avenue</p>
							<p class="mb-0">Witton, Birmingham.</p>
							<p>B6 7EB</p>
							<p class="mb-0">Tel: 0121 327 7800</p>
							<p>Fax: 0121 327 4799</p>
						</div>
					<div class="col-sm-4">
						<p class="mb-0"><span class="label">Customer:</span> <?php echo $customer_name; ?></p>
						<p class="mb-0"><span class="label">Supplier Code:</span> <?php echo $hh; ?></p>
						<p class="mb-0"><span class="label">Part Number:</span> <?php echo $part_number; ?></p>
						<p class="mb-0"><span class="label">Description:</span> <?php echo $part_description; ?></p>
						<p class="mb-0"><span class="label">Drawing Issue:</span> <?php echo $drawing_issue; ?></p>
						<p class="mb-0"><span class="label">Model:</span> <?php echo $model_number; ?></p>
						<p style= "background-color: #FFFFE0;" class="mb-0"><span class="label">Pcr/Aims:</span> <?php echo $hh; ?></p>
						<p class="mb-0"><span class="label">Vol:</span> <?php echo $volume; ?></p>
					</div>

					<div class="col-sm-4">
						<p style= "background-color: #FFFFE0;" class="mb-0"><span class="label">Quote:</span> <?php echo $quote; ?></p>
						<p style= "background-color: #FFFFE0;" class="mb-0"><span class="label">Sheet:</span> <?php echo $sheet; ?></p><br>
						<p style= "background-color: #FFFFE0;" class="mb-0"><span class="label">Economic Level:</span> <?php echo $economic_level; ?></p>
						<p class="mb-0"><span class="label">Currency:</span> GB STERLING</p>
						<p style= "background-color: #FFFFE0;" class="mb-0"><span class="label">Pack:</span> <?php echo $pack; ?></p>
						<p class="mb-0"><span class="label">Manager:</span> Gary Hingley</p>
					</div>

					</div>
					<div class="my-4"></div>
					<div class="container">
						<div class="row" style="border: 1px solid #ccc;margin-right: 3.5px;margin-left: -2.5px;">
							<div class="col-sm-5">
								<h5> 1. Procured & Made in Parts (From Sheet 2)</h5>
							</div>
							<div class="col-sm-3" style="border-left: 1px solid #ccc; text-align: center;">
								<p><strong>Procured Scrap Value : <?php echo !empty($average_brought) ? $average_brought : 0; ?></strong></p>
							</div>
							<div class="col-sm-4" style="border-left: 1px solid #ccc; text-align: center;">
								<p><strong>Sub Total (1) Procured = <?php echo !empty($totals_brought) ? $totals_brought : 0; ?></strong></p>
							</div>
						</div>
					</div>
					
					

					<div class="my-4"></div>
					<div class="container">
						<h5>2. Material Costs</h2>
							<table class="table custom-bordered">
								<thead>
									<tr>
										<td>TYPE / SPEC / GRADE / SIZE / SUPPLIER</td>
										<td>MOE (Y/N)</td>
										<td>UOM (e.g. KG)</td>
										<td>Gross Usage</td>
										<td>Net Usage</td>
										<td>Price / UOM</td>
										<td>Scrap (%)</td>
										<td style= "background-color: #FFFFE0;">Reclaim</td>
										<td>Unit Cost</td>
									</tr>
								</thead>
								<tbody>
								<?
								 $total_unit_cost=0;
								 $total_raw_scrap_value=0;
								while($row_raw_materials =mysqli_fetch_array($sql_raw_materials)){ 
									$row_raw_materials=utility::validate_crypt_data($row_raw_materials);
									 $total_unit_cost += $row_raw_materials['unit_cost'];
									 $total_raw_scrap_value += $row_raw_materials['raw_scrap_value'];
					         	?>
									<tr>
										<td>
											<?php echo $gsi->getCustomerById($row_raw_materials['type_id']) . ' Grade ' . $row_raw_materials['grade'] . ' Spec ' . $row_raw_materials['spec'] . '<br>' . $row_raw_materials['length'] . 'X' . $row_raw_materials['width'] . 'X' . $row_raw_materials['thickness'] . ' Mks ' . $row_raw_materials['mks'] . ' Supp =  ' . $row_raw_materials['supplier']; ?>
										</td>
										<td><?php echo $gsi->getYesNoById($row_raw_materials['mpe']);?></td>
										<td><?php echo $row_raw_materials['uom']?></td>
										<td><?php echo $row_raw_materials['gross']?></td>
										<td><?php echo $row_raw_materials['nett']?></td>
										<td><?php echo $row_raw_materials['price_uom']?></td>
										<td><?php echo $row_raw_materials['scrap_percent']?></td>
										<td><?php echo $row_raw_materials['reclaim']?></td>
										<td><?php echo $row_raw_materials['unit_cost']?></td>
									</tr>
								<? } ?>
								</tbody>
								<tfoot>
									<tr>
										<td colspan="1" class="text-end" style="border-left: none !important; border-bottom: none !important;"></td>
										<td colspan="4" class="text-end"  style="text-align: center;"><strong>Material Scrap Value: <?php echo !empty($total_raw_scrap_value) ? $total_raw_scrap_value : 0; ?></strong></td>
										<td colspan="7" class="text-end"  style="text-align: center;"><strong>Sub Total (2) Material = <?php echo !empty($total_unit_cost) ? $total_unit_cost : 0; ?></strong></td>
									</tr>
								</tfoot>
							</table>
					</div>
					<div class="container">
						<h5 class="mt-4">3. Process Costs</h2>
							<table class="table custom-bordered">
								<thead>
									<tr>
										<td>Operation</td>
										<td>Facility Type</td>
										<td style= "background-color: #FFFFE0;">Sub Con?</td>
										<td>Work Hrs / Week</td>
										<td>Heads per Hour</td>
										<td>Num per Hour</td>
										<td>Labour Rate per hr</td>
										<td>Facility Rate per hr</td>
										<td>Scrap (%)</td>
										<td style= "background-color: #FFFFE0;">Capital Depr. / Hour</td>
										<td>Unit Cost</td>
									</tr>
								</thead>
								<tbody>
								<?
								$total_scrap_value=0;
								$total_total_process=0;
								while($row_operation =mysqli_fetch_array($sql_operation)){ 
									$row_operation=utility::validate_crypt_data($row_operation);
									 $total_scrap_value += $row_operation['scrap_value'];
									 $total_total_process += $row_operation['total'];
					         	?>
									<tr>
										<td><?php echo $row_operation['operation']?></td>
										<td><?php echo $row_operation['work_station_description']?></td>
										<td><?php echo $row_operation['sub_con']?></td>
										<td><?php echo $row_operation['w_hrs']?></td>
										<td><?php echo $row_operation['operator']?></td>
										<td><?php echo $row_operation['units_per_hour']?></td>
										<td><?php echo $row_operation['labour_rate']?></td>
										<td><?php echo $row_operation['fac_rate']?></td>
										<td><?php echo $row_operation['scrap_percent']?></td>
										<td><?php echo !empty($row_operation['pricecapital_depr']) ? $row_operation['pricecapital_depr'] : 0.00; ?></td>
										<td><?php echo $row_operation['total']?></td>
									</tr>
								<? } ?>	
								</tbody>
								<tfoot>
									<tr>
										<td colspan="2" class="text-end" style="border-left: none !important; border-bottom: none !important;"></td>
										<td colspan="5" class="text-end"  style="text-align: center;"><strong>Process Scrap Value = <?php echo !empty($total_scrap_value) ? $total_scrap_value : 0; ?></strong></td>
										<td colspan="5" class="text-end"  style="text-align: center;"><strong>Sub Total (3) Process = <?php echo !empty($total_total_process) ? $total_total_process : 0; ?></strong></td>
									</tr>
								</tfoot>
							</table>
					</div>

					<br>
					<div class="container">
						<div class="row">
							<div class="col-sm-4">
								<h5>4. Other Costs</h5>
							</div>
							<div class="col-sm-4">
								<p>Cost Calculation</p>
							</div>
							<div class="col-sm-4">
								<p>Separate Overhead Analysis QAF - Yes / No</p>
							</div>
						</div>
						<table class="table custom-bordered">
							<thead>
							</thead>
							<tbody>
								<tr>
									<td>General Overheads: </td>
									<td><?php echo $grade_general_overheads;?></td>
									<td style="text-align: center;"><?php echo $cost_general_overheads;?></td>
								</tr>
								<tr>
									<td>Other:</td>
									<td><?php echo $grade_other;?></td>
									<td style="text-align: center;"><?php echo $cost_other;?></td>
								</tr>
								<tr>
									<td>Profit:</td>
									<td><?php echo $grade_profit;?></td>
									<td class ="cost_profit" style="text-align: center;"><?php echo $cost_profit;?></td>
								</tr>
							</tbody>
							<tfoot>
								<tr>
								    <?php $total_subtotal_other=0 ?>
									<td colspan="2" class="text-end" style="border-left: none !important; border-bottom: none !important;"></td>
									<td colspan="1" class="text-end"  style="text-align: center;"><strong>Sub Total (4) Other = <?php echo $total_subtotal_other= ($cost_general_overheads + $cost_other + $cost_profit);?></strong></td>
								</tr>
							</tfoot>
						</table>
					</div>
					<div class="container" style="">
						<h5>5. Tooling Costs</h5>
						<div class="row" style="border: 1px solid #ccc;margin-right: 3.5px;margin-left: -2.5px;">
							<div class="col-sm-6" style="">
								<p>Tooling Cost (From Sheet 2) =  <?php echo $total_tool_cost_sum?></p>
							</div>
							<div class="col-sm-6" style="border-left: 1px solid #ccc;">
								<p style="text-align: center; background-color: #FFFFE0;"><strong>Sub Total (5) Tooling Amortisation = 0000<?php echo $total_amortisation?></strong></p>
							</div>
						</div>
					</div>
					<div class="my-4"></div>
					<div class="container">
						<div class="row" style="border: 1px solid #ccc;margin-right: 3.5px;margin-left: -2.5px;">
							<div class="col-sm-5">
								<h5>FEASIBILITY TYPE</h5>
								<p><?php echo $gsi->getFeasibilityTypeListById($fsb_type);?></p>
							</div>
							<div class="col-sm-5" style="border-left: 1px solid #ccc; text-align: center;"><strong>
								<p>TOTAL MANUFACTURING COST</p>
								<p>(1)+(2)+(3)+(4)+(5)</p></strong>
							</div>
							<div class="col-sm-2" style="border-left: 1px solid #ccc; text-align: center;"><strong>
								<p><?php echo $total_feas_manu_cost= ($total_unit_cost+$total_total_process+$total_subtotal_other+$totals_brought);?></p></strong>
							</div>
						</div>
					</div>
					<div class="my-4"></div>
					<div class="container" style="">
						<h5>Issues / Concerns:</h5>
						<div class="row" style="border: 1px solid #ccc; margin-right: 3.5px;margin-left: -2.5px;">
							<div class="col-sm-4" style="">
								<p>Quote valid for 3 months from date below <br> <?php echo $issue_concern;?></p>
							</div>
							<div class="col-sm-8" style="border-left: 1px solid #ccc;">
								<div class="row">
									<div class="col-sm-6">
										<h5>6. LOGISTIC COSTS</h5>
										<p class="mb-0">Transport:&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $transport;?></p>
										<p class="mb-0">Packaging:&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $packaging;?></p>
										<p class="mb-0">Pallet Amortisation: &nbsp;&nbsp;<?php echo $pallet;?></p>
										<p class="">Total Pallet Cost: &nbsp;&nbsp;&nbsp;&nbsp;<?php echo $othcost;?></p>
									</div>

									<div class="col-sm-6">
									    <?php $total_logistic_cost=0 ?>
										<p>Separate Logistics Analysis QAF?&nbsp;&nbsp;<?php echo $gsi->getYesNoById($logistics);?></p>
										<p class="mb-0" style="text-align: center;">&nbsp;<?php echo $pcost;?></p>
										<p class="mb-0" style="text-align: center;">&nbsp;<?php echo $pacost;?></p>
										<p class="mb-0" style="text-align: center;">&nbsp;<?php echo $palcost;?></p>
										<p class="mb-0" style="text-align: center;">&nbsp;</p>
										<p class="mb-0"><strong>Sub Total (6)=&nbsp;<?php echo $total_logistic_cost= ($pcost+$pacost+$palcost+$othcost);?></strong></p>
									</div>

								</div>
							</div>
						</div>
					</div>

					<div class="my-4"></div>
					<div class="container">
						<div class="row" style="border: 1px solid #ccc;margin-right: 3.5px;margin-left: -2.5px;">
							<div class="col-sm-5">
								<p>Signed / Date: <?php echo date("d-m-Y") ?></p>
								<h6
									style="font-family: 'Great Vibes', cursive; font-size: 1.5em; color: #5A3E85; font-style: italic; letter-spacing: 1px; text-align: center;">
									Gary Hingley</h6>
							</div>
							<div class="col-sm-5" style="border-left: 1px solid #ccc; text-align: center;"><strong>
								<p>Total Selling Price per Part </p>
								<p> ( 1 + 2 + 3 + 4 + 5 + 6 )</p></strong>
							</div>
							<div class="col-sm-2" style="border-left: 1px solid #ccc; text-align: center;"><strong>
								<p>&nbsp;&pound;<?php echo $total_logistic_cost + $total_feas_manu_cost ;?></p>
								<p>EACH</p></strong>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript">
	/*$(function(){
	
		const totalUnitCost = <?php echo $total_unit_cost; ?>;
		const totalRawScrapValue = <?php echo $total_raw_scrap_value; ?>;
		const totalTotal = <?php echo $total_total_process; ?>;
		const totalScrapValue = <?php echo $total_scrap_value; ?>;
		const rateToUseProfit = <?php echo $rate_to_use_profit; ?>;
		const costGeneralOverheads = <?php echo $cost_general_overheads; ?>;

		function recalculateCostProfit() {
			let baseProfitCalculation = totalUnitCost - totalRawScrapValue + totalTotal - totalScrapValue + costGeneralOverheads;
			let calculatedProfit = (baseProfitCalculation * rateToUseProfit) / 100;
			
			$('#cost_profit').val(calculatedProfit.toFixed(2));
		}
        recalculateCostProfit();
	  
	});*/
</script>