@extends('layouts.report-print')
@section('css')
	<style type="text/css">
	/* size landscape for landscape paper */
		@page{
			/* size: landscape; */
		}
	</style>
@endsection
@section('content')
	<div class="container-print">
		<div class="row">
			<div class="col-md-12">
				<div class="UpperTitle"><img src="{{asset('assets/images/resize')}}/{{$branch->branch_img}}" alt="Logo">
					<div class="title">
						<h2 class="com-name">{{$branch->branch_kh}}</h2>
						<h2>{{$branch->branch_en}}</h2>
						<h4>Report Budget & Actual Use</h4>
					</div>
				</div>
			</div>
		</div>
		<div class="row">
			<div class="col-md-12">
				<hr>
			</div>
		</div>
		<div class="row">
			<div class="col-md-12">
				<table class="table table-print" width="100%">
					<tbody>
						<tr>
							<th>NO</th>
							<th>Date</th>
							<th>Other Type</th>
							<th>Project</th>
							<th>Zone</th>
							<th>Prepared By</th>
							<th>Title</th>
							<th>Status Finish</th>
							<th>BOQ Labour</th>
							<th>BOQ Material</th>
							<th>Actual Labour</th>
							<th>Actual Material</th>
							<th>Labour Balance</th>
							<th>Material Balance</th>
						</tr>
						<?php $total_boq_labour = 0;$total_boq_material = 0;$total_actual_labour = 0; $total_actual_material = 0;$total_balance_labour = 0;
						$total_balance_material = 0 ?>
						@foreach($data as $key => $value)
						<?php 
							$status_finish = "";

							if($value->status_close==1) $status_finish = "<span style='color:rgb(184, 95, 11)'>Pendding</span>";
							else if($value->status_close==2) $status_finish = "<span style='color:rgb(42, 156, 8)'>Financial Approved</span>";
							else if($value->status_close==3) $status_finish = "<span style='color:rgb(185, 51, 17)'>Financial Reject (".$value->description.")</span>"; 
							else if($value->status_close==4) $status_finish = "<span style='color:rgb(42, 156, 8)'>CEO Approved</span>";
							else if($value->status_close==5) $status_finish = "<span style='color:rgb(185, 51, 17)'>CEO Reject(".$value->description.")</span>";
							else {
								$status_finish = "";
							}  


							$total_boq_labour += $value->boq_labour;
							$total_boq_material += $value->boq_material;


							//Other Expense
							$other_expense_material = 0;$other_expense_labour = 0;
							$data_other_epxenses = DB::table("nso007_budget_other_expense_detail_construction")
								->join("nso007_budget_other_expense_construction","nso007_budget_other_expense_construction.id","nso007_budget_other_expense_detail_construction.parent_id")
								->where("project_id",$value->project_id)->where("zone_id",$value->zone_id)->get();

							foreach($data_other_epxenses as $other_expense){
								if($other_expense->type==1){//Material
									$other_expense_material += $other_expense->amount;
								}
								else{
									$other_expense_labour += $other_expense->amount;
								}
							}
							//End


							###Actual Use Material
							$actual_material = 0;

							$actual_material_po = DB::table("nso007_request_warehouse_detail_construction")
								->join("nso007_request_warehouse_construction","nso007_request_warehouse_detail_construction.request_warehouse_id","nso007_request_warehouse_construction.id")
								->where("project_id",$value->project_id)
								->where("nso007_request_warehouse_detail_construction.zone",$value->zone_id)
								// ->where("nso007_request_warehouse_construction.work_group_type_id",$value->boq_work_type)
								->select("nso007_request_warehouse_detail_construction.*")
								->get();

							foreach($actual_material_po as $material_po){
								$actual_material += $material_po->amount;
							}

							$actual_materials_contract = DB::table("nso007_task_contractor_work_construction")
								->join('nso007_construction_table','nso007_task_contractor_work_construction.construction_id','nso007_construction_table.id')
								->where("nso007_construction_table.project_id",$value->project_id)
								->where("nso007_construction_table.zone_name",$value->zone_id)
								// ->where("nso007_construction_table.work_group_type_id",$value->boq_work_type)
								->where("nso007_task_contractor_work_construction.task_type",2)
								->select("nso007_task_contractor_work_construction.*")
								->get();

							foreach($actual_materials_contract as $material){
								$actual_material += $material->paid_amount;
							}

							$actual_material += $other_expense_material;

							$total_actual_material += $actual_material;

							###End Material

							##Actual Use Labour

							$actual_labour = 0;
							$actual_labour_contract = DB::table("nso007_task_contractor_work_construction")
								->join('nso007_construction_table','nso007_task_contractor_work_construction.construction_id','nso007_construction_table.id')
								->where("nso007_construction_table.project_id",$value->project_id)
								->where("nso007_construction_table.zone_name",$value->zone_id)
								// ->where("nso007_construction_table.work_group_type_id",$value->boq_work_type)
								->where("nso007_task_contractor_work_construction.task_type",1)
								->select("nso007_task_contractor_work_construction.*")
								->get();

							foreach($actual_labour_contract as $labour_contract){
								$actual_labour += $labour_contract->paid_amount;
							}

							$actual_labour += $other_expense_labour;

							$total_actual_labour += $actual_labour;
							##End

							$total_balance_labour += $value->boq_labour - $actual_labour;
							$total_balance_material += $value->boq_material - $actual_material;
						?>
						<tr>
							<td>{{ $key+1 }}</td>
							<td>{{ date("d-m-Y",strtotime($value->date)) }}</td>
							<td>{{ $value->building_type }}</td>
							<td>{{ $value->project_name }}</td>
							<td>{{ $value->zone_name }}</td>
							<td>{{ $value->prepared_by }}</td>
							<td>{{ $value->title }}</td>
							<td>{{ $status_finish }}</td>
							<td>${{ number_format($value->boq_labour,2) }}</td>
							<td>${{ number_format($value->boq_material,2) }}</td>
							<td>${{ number_format($actual_labour,2) }}</td>
							<td>${{ number_format($actual_material,2) }}</td>
							<td>${{ number_format($value->boq_labour - $actual_labour,2) }}</td>
							<td>${{ number_format($value->boq_material - $actual_material,2) }}</td>
						</tr>
						@endforeach()
						<tr class="tr-right">
							<td colspan="8" id="text-algin-right">Total : </td>
							<td>${{ number_format($total_boq_labour,2) }}</td>
							<td>${{ number_format($total_boq_material,2) }}</td>
							<td>${{ number_format($total_actual_labour,2) }}</td>
							<td>${{ number_format($total_actual_material,2) }}</td>
							<td>${{ number_format($total_balance_labour,2) }}</td>
							<td>${{ number_format($total_balance_material,2) }}</td>
						</tr>
					</tbody>
				</table>
			</div>	
		</div>
		<div class="row">
			<div class="col-md-12">
				<table width="100%"​ class="tb-sign">
					<tr align="right">
						<td>
							<p>ធ្វើនៅ​ <strong>ភ្នំពេញ</strong> ថ្ងៃទី <strong>{{\Globals::date_khmers(date('Y-m-d'))}}</strong></p>
							<p style="padding-bottom: 80px;padding-right:40px">ហត្ថលេខា</p>
							<p>______________________</p>
						</td>
					</tr>	
				</table>
			</div>
		</div>
	</div>
@endsection
<style>
	#text-algin-right{
		text-align: right !important;
	}
</style>



