@extends('layouts.default')

@section('title')
	@parent
	Статистика :: Упаковка
@stop

@section('js')
	<script src="{{ asset('js/bootstrap-datepicker.js') }}"></script>
	<script src="{{ asset('js/kmk-support.js') }}"></script>
	<script src="{{ asset('js/crm.js') }}"></script>
	<script>
		$(document).ready(function(){
			setTimeout(function(){
				window.location = "/production/stat";
			}, 5*60*1000);
		});
	</script>
@stop

@section('content')
<div class="row">
  <div class="col-md-12">
		<div class="row">
			<div class="panel panel-default panel-production">
				<div class="panel-heading">
					<h3>Результат работы</h3>
				</div>
				<div class="panel-body">
					<table class="table table-striped table-hover">
						<thead>
							<th>Мастер</th>
							<th class="text-center">Сегодня</th>
							<th class="text-center">В этом месяце</th>
							<th class="text-center">В прошлом месяце</th>
						</thead>
						<tbody>
							@foreach ($masters as $masters)
							<tr>
								<td>
									<h4>{{ $masters->name }}</h4>
									@foreach (ProductType::get() as $productType)
										<p>&nbsp;&nbsp;&nbsp;{{ $productType->name }}</p>
									@endforeach
								</td>
								<td class="text-center">
									<?php
										if ($masters->isseamstress) {
											$quant = Production::whereRaw('DATE(production_end_at)=CURRENT_DATE()')->count();
											$quantDiff = $quant - Production::whereRaw('TIME(production_end_at)<TIME(NOW()) AND DATE(production_end_at)=DATE(DATE_SUB(NOW(), INTERVAL 1 DAY))')->count();
										}
										else {
											$quant = Production::whereProductionMasterId($masters->id)->whereRaw('DATE(production_end_at)=CURRENT_DATE()')->count();
											$quantDiff = $quant - Production::whereProductionMasterId($masters->id)->whereRaw('TIME(production_end_at)<TIME(NOW()) AND DATE(production_end_at)=DATE(DATE_SUB(NOW(), INTERVAL 1 DAY))')->count();
										}
									?>
									<h4>{{ $quant }}@if ($quantDiff)<span class="@if ($quantDiff<0) diff-minus @elseif ($quantDiff>0) diff-plus @endif">@if($quantDiff>0)+@endif{{ $quantDiff }}</span>@endif</h4>
									@foreach (ProductType::get() as $productType)
										<?php
											if ($masters->isseamstress) {
												$quant = Production::whereProductTypeId($productType->id)->whereRaw('DATE(production_end_at)=CURRENT_DATE()')->count();
												$quantDiff = $quant - Production::whereProductTypeId($productType->id)->whereRaw('TIME(production_end_at)<TIME(NOW()) AND DATE(production_end_at)=DATE(DATE_SUB(NOW(), INTERVAL 1 DAY))')->count();
											}
											else {
												$quant = Production::whereProductionMasterId($masters->id)->whereProductTypeId($productType->id)->whereRaw('DATE(production_end_at)=CURRENT_DATE()')->count();
												$quantDiff = $quant - Production::whereProductionMasterId($masters->id)->whereProductTypeId($productType->id)->whereRaw('TIME(production_end_at)<TIME(NOW()) AND DATE(production_end_at)=DATE(DATE_SUB(NOW(), INTERVAL 1 DAY))')->count();
											}
										?>
										<p>{{ $quant }}@if ($quantDiff)<span class="@if ($quantDiff<0) diff-minus @elseif ($quantDiff>0) diff-plus @endif">@if($quantDiff>0)+@endif{{ $quantDiff }}</span>@endif</p>
									@endforeach
								</td>
								<td class="text-center">
									@if ($masters->isseamstress)
										<h4>{{ Production::whereRaw('DATE_FORMAT(production_end_at, "%Y%m")=DATE_FORMAT(CURRENT_DATE(), "%Y%m")')->count() }}</h4>
										@foreach (ProductType::get() as $productType)
											<p>{{ Production::whereProductTypeId($productType->id)->whereRaw('DATE_FORMAT(production_end_at, "%Y%m")=DATE_FORMAT(CURRENT_DATE(), "%Y%m")')->count() }}</p>
										@endforeach
									@else
										<h4>{{ Production::whereProductionMasterId($masters->id)->whereRaw('DATE_FORMAT(production_end_at, "%Y%m")=DATE_FORMAT(CURRENT_DATE(), "%Y%m")')->count() }}</h4>
										@foreach (ProductType::get() as $productType)
											<p>{{ Production::whereProductionMasterId($masters->id)->whereProductTypeId($productType->id)->whereRaw('DATE_FORMAT(production_end_at, "%Y%m")=DATE_FORMAT(CURRENT_DATE(), "%Y%m")')->count() }}</p>
										@endforeach
									@endif
								</td>
								<td class="text-center">
									@if ($masters->isseamstress)
										<h4>{{ Production::whereRaw('DATE_FORMAT(production_end_at, "%Y%m")=DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH), "%Y%m")')->count() }}</h4>
										@foreach (ProductType::get() as $productType)
											<p>{{ Production::whereProductTypeId($productType->id)->whereRaw('DATE_FORMAT(production_end_at, "%Y%m")=DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH), "%Y%m")')->count() }}</p>
										@endforeach
									@else
										<h4>{{ Production::whereProductionMasterId($masters->id)->whereRaw('DATE_FORMAT(production_end_at, "%Y%m")=DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH), "%Y%m")')->count() }}</h4>
										@foreach (ProductType::get() as $productType)
											<p>{{ Production::whereProductionMasterId($masters->id)->whereProductTypeId($productType->id)->whereRaw('DATE_FORMAT(production_end_at, "%Y%m")=DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH), "%Y%m")')->count() }}</p>
										@endforeach
									@endif
								</td>
							</tr>
							@endforeach
						</tbody>
					</table>
				</div>
			</div>
		</div>
  </div>
</div>
@stop
