@extends('layouts.print')
 
@section('content')
<style>
    table.m-table th,table.m-table td{
        text-align: center !important;
    }
    .table-inv-head td {
        border: none;
    }
    span.boq-header{
        margin-left:20px !important;
    }
    table.boq-header-tb tr th,table.boq-header-tb tr td{
        line-height:150% !important;
        font-size: 18px !important;
    }
    .report-capital.layout-print .table tr td{
        font-size: 17px !important;
    }
    .report-capital.layout-print .table tr th,.fdiv {
        font-size: 18px !important;
    }
    /* table tbody tr td.description,table thead tr th.description{
        width: 100px !important;
    } */
</style>
<div class="main-content-wrapper">
	<div class="main-box">
		<div class="main-content">
			<div class="container">
				<div class="page">
					<div class="row">
						<div class="report-capital layout-print">
							<div class="header">
								<div class="logo">
									<img src="{{asset('assets/images/resize')}}/{{$branch->branch_img}}" alt="" width="150px">
								</div>
								<div class="title">
									<h2>{{$branch->branch_kh}}</h2>
									<h2>{{$branch->branch_en}}</h2>
                                    {{-- 
                                        <h3>របាយការណ៍ សំភារៈទឹកសម្រាប់ប្រើប្រាស់លើអគារ{{ $data->building_type }}</h3>
                                     --}}
                                     <h3>{{ $data_main->title }}</h3>
								</div>  
                                
                                <div>
                                    <table border='0' class='boq-header-tb'>
                                        <tr>
                                            <td style="width: 200px;">
                                                NUMBER
                                            </td>
                                            <td>: <span class="boq-header">{{strtoupper(str_pad($data_main->id,6,"0",STR_PAD_LEFT))}}</span></td>
                                        </tr>
                                        <tr>
                                            <td style="width: 200px;">
                                                PROJECT NAME
                                            </td>
                                            <td>: <span class="boq-header">{{ DB::table('nso007_project_stock')->where('id',$data_main->project_id)->first()->project_name }}</span></td>
                                        </tr>
                                        <tr>
                                            <td style="width: 200px;">
                                                PREPARED BY
                                            </td>
                                            <td>: <span class="boq-header">{{ $data_main->prepared_by }}</span></td>
                                        </tr>
                                        <tr>
                                            <td style="width: 200px;">
                                               BUILDING TYPE
                                            </td>
                                            <td>: <span class="boq-header">{{ $data_main->building_type }}</span></td>
                                        </tr>
                                        <tr>
                                            <td style="width: 250px;">
                                                AMOUNT OF HOUSE USED
                                            </td>
                                            <td>: <span class="boq-header">{{ strtoupper($data_main->amount_of_house_used." units") }}</span></td>
                                        </tr>
                                    </table>
                                </div>
								<br/>
                                <div class="table">
                                    <div class="row">
                                        <div class="col-xs-12">
                                            <h4><u>BOQ</u></h4>
                                            <table class="m-table">
                                                <thead >
                                                    <tr >
                                                        <th>ល.រ</th>
                                                        <th class="description">បរិយាយ</th>
                                                        <th>Labour</th>
                                                        <th>Material</th>
                                                        <th>Total</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php $index = 1; $total_boq_labour = 0; $total_boq_material = 0; $total_boq_total = 0;?>
                                                        @foreach($data_Details as $k=>$val)
                                                        <?php $total_boq_labour += $val->boq_labour; $total_boq_material += $val->boq_material; $total_boq_total += $val->boq_total;?>
                                                            <tr>
                                                                <td>{{ $k+1 }}</td>
                                                                <td class="description">{{ $val->name }}</td>
                                                                <td>${{ number_format($val->boq_labour,2) }}</td>
                                                                <td>${{ number_format($val->boq_material,2) }}</td>
                                                                <td>${{ number_format($val->boq_total,2) }}</td>
                                                            </tr>
                                                        @endforeach
                                                        <td colspan="2">Total</td>
                                                        <td>${{ number_format($total_boq_labour,2) }}</td>
                                                        <td>${{ number_format($total_boq_material,2) }}</td>
                                                        <td>${{ number_format($total_boq_total,2) }}</td>
                                                </tbody>
                                            </table>
                                        </div>
                                        <div class="col-xs-12" style="margin-top: 20px;">
                                            <h4><u>Actual Used</u></h4>
                                            <table class="m-table">
                                                <thead >
                                                    <tr >
                                                        <th>ល.រ</th>
                                                        <th class="description">បរិយាយ</th>
                                                        <th>Labour</th>
                                                        <th>Material</th>
                                                        <th>Total</th>
                                                        <th>Balance</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php $index = 1; $total_actual_labour = 0;$total_actual_material=0;$total_actual_total =0;$total_balance = 0;$count_index = 0;$sum_expense_material = 0;$sum_expense_labour = 0;?>
                                                        @foreach($data_Details as $index=>$value)
                                                            @php
                                                                $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",$data_main->project_id)
                                                                    ->where("nso007_request_warehouse_detail_construction.zone",$data_main->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",$data_main->project_id)
                                                                    ->where("nso007_construction_table.zone_name",$data_main->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_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",$data_main->project_id)
                                                                    ->where("nso007_construction_table.zone_name",$data_main->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;
                                                                }
                                                                    
                                                                $total_actual_labour += $actual_labour;
                                                                $total_actual_material += $actual_material;

                                                                $count_index = $index+1;

                                                            @endphp
                                                            <tr>
                                                                <td>{{ $index+1 }}</td>
                                                                <td class="description">{{ $value->name }}</td>
                                                                <td>${{ !empty($actual_labour)?number_format($actual_labour,2):"0" }}</td>
                                                                <td>${{ !empty($actual_material)?number_format($actual_material,2):"0" }}</td>
                                                                <td>${{ number_format(($actual_labour+$actual_material),2) }}</td>
                                                                <td>${{ number_format($value->boq_total - ($actual_labour + $actual_material),2) }}</td>
                                                                @php
                                                                    $total_balance += ($value->boq_total - ($actual_labour + $actual_material));
                                                                @endphp
                                                            </tr>
                                                        @endforeach

                                                        @foreach ($data_other_epxenses as $exp_key=>$expense)
                                                            <tr>
                                                                <td>{{ $count_index + $exp_key + 1 }}</td>
                                                                <td class="description">{{ $expense->expense_description }}</td>
                                                                @if($expense->type==2)
                                                                <td>${{ !empty($expense->amount)?number_format($expense->amount,2):"0" }}</td>
                                                                @php
                                                                    $sum_expense_labour += $expense->amount;
                                                                @endphp
                                                                @else
                                                                <td>${{ 0 }}</td>
                                                                @endif
                                                                @if($expense->type==1)
                                                                <td>${{ !empty($expense->amount)?number_format($expense->amount,2):"0" }}</td>
                                                                @php
                                                                    $sum_expense_material += $expense->amount;
                                                                @endphp
                                                                @else
                                                                <td>${{ 0 }}</td>
                                                                @endif
                                                                <td>${{ number_format(($expense->amount),2) }}</td>
                                                                <td>${{ number_format((0 - $expense->amount),2) }}</td>
                                                                @php
                                                                    $total_balance += (0 - $expense->amount);
                                                                @endphp
                                                            </tr>
                                                        @endforeach

                                                        <tr>
                                                            <td colspan="2">Total</td>
                                                            <td>${{ number_format($total_actual_labour + $sum_expense_labour,2) }}</td>
                                                            <td>${{ number_format($total_actual_material + $sum_expense_material,2) }}</td>
                                                            <td>${{ number_format(($total_actual_labour + $total_actual_material + $sum_expense_labour + $sum_expense_material),2) }}</td>
                                                            <td>${{ number_format(($total_balance),2) }}</td>
                                                            
                                                        </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
								</div>
                                <div class="table">
                                    <div class="make-date fdiv">
										<p>ធ្វើនៅ​ <strong>ភ្នំពេញ</strong> ថ្ងៃទី <strong>{{\Globals::date_khmers(date('Y-m-d'))}}</strong></p>
                                    </div>
                                    
									<div class="row fdiv" style="text-align: center;">
                                        <div class="col-xs-4">គណនា និងរៀបរៀងដោយ</div>
                                        <div class="col-xs-4">ត្រួតពិនិត្យដោយ</div>
                                        <div class="col-xs-4" style="font-weight: bold;">ឯកភាពដោយ</div>
                                    </div>
                                    <br><br/><br/><br>
                                    {{-- <div class="row fdiv" style="text-align: center;">
                                        <div class="col-xs-4"><p>_________________</p></div>
                                        <div class="col-xs-4"><p>_________________</p></div>
                                        <div class="col-xs-4"><p>_________________</p></div>
                                    </div> --}}
                                    <div class="row fdiv" style="text-align: center;">
                                        <div class="col-xs-4"><p>{{ $data_main->prepared_by }}</p></div>
                                        <div class="col-xs-4"><p>ប្រធានគម្រោង</p></div>
                                        <div class="col-xs-4"><p>អគ្គនាយករង</p></div>
                                    </div>
                                </div>
							</div>
						</div>
							
						
					</div>
					
					
				</div>
			</div>
		</div>
	</div>
</div>
<style>
</style>

@endsection


