<?php $__env->startSection('title'); ?>
	@parent
	Статистика по заявкам
<?php $__env->stopSection(); ?>

<?php $__env->startSection('breadcrumbs'); ?>
	<?php echo Breadcrumbs::render('stat.leads'); ?>

<?php $__env->stopSection(); ?>

<?php $__env->startSection('js'); ?>
	<script src="<?php echo asset('js/kmk-support.js'); ?>"></script>
	<script>
		function reload () {
			var params = kmkGiveGetParams(location.search);
			params['projectId'] = '<?php echo $input['projectId']; ?>';
			params['startDate'] = $('#startDate').val();
			params['endDate'] = $('#endDate').val();
			if ($('#useLeadDate:checked').length) {
				params['useLeadDate'] = 1;
			}
			else {
				params['useLeadDate'] = 0;
			}
			kmkSetBrowserAddressParams(kmkObjectToGetString(params, false));
			location.reload();
		}

		$(document).ready(function(){
			$('#useLeadDate').change(function(){
				reload();
			});

			var startDate = $('#startDate')
				.datepicker({
					onClose: function( selectedDate ) {
						$("#endDate").datepicker( "option", "minDate", selectedDate );
						reload();
					}
				});
			var endDate = $('#endDate')
				.datepicker({
					onClose: function( selectedDate ) {
						$( "#startDate" ).datepicker( "option", "maxDate", selectedDate );
						reload();
					}
				});

			var today = new Date();
			$("#todayDate").data('date', today.getDate()+'.'+(parseInt(today.getMonth())+1)+'.'+today.getFullYear());
			today.setDate(today.getDate()-1);
			$("#yeasterdayDate").data('date', today.getDate()+'.'+(parseInt(today.getMonth())+1)+'.'+today.getFullYear());

			$('.btn-set-date').unbind('click').on('click', function(){
				$('#startDate').val($(this).data('date'));
				$('#endDate').val($(this).data('date'));
				reload();
				return false;
			});
		});
	</script>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('content'); ?>
	<div class="row">
		<div class="col-xs-12 col-sm-8 col-md-8 col-lg-8">
			<h1 class="page-title txt-color-blueDark">
				<i class="fa fa-bar-chart-o fa-fw"></i>
				Статистика по заявкам
			</h1>
		</div>
	</div>

	<section id="widget-grid-1">
		<div class="row">
			<article class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
				<div class="well well-sm">
					<form class="form-inline" role="form">
						<div class="form-group">
							<label for="startDate">Дата оформления с</label> <input id="startDate" name="startDate" class="form-control" value="<?php echo $input['startDate']; ?>"/>
						</div>
						<div class="form-group">
							<label for="endDate">по</label> <input id="endDate" name="endDate" class="form-control" value="<?php echo $input['endDate']; ?>"/>
						</div>
						<div class="form-group">
							<button id="yeasterdayDate" class="btn btn-default btn-set-date" data-date="">Вчера</button>
							<button id="todayDate" class="btn btn-default btn-set-date">Сегодня</button>
						</div>
					</form>
					<br/>
					<?php foreach($user->projects()->whereUsecrm(1)->whereType(Project::TYPE_SHOP)->get() as $currentProject): ?>
					<a href="/stat/leads?projectId=<?php echo $currentProject->id; ?>" class="btn btn-sm btn-default <?php if($input['projectId'] == $currentProject->id): ?> active <?php endif; ?>" data-id="<?php echo $currentProject->id; ?>"><?php echo $currentProject->name; ?></a>
					<?php endforeach; ?>
				</div>
			</article>

			<article class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
				<div class="jarviswidget" id="wid-id-1" data-widget-colorbutton="false" data-widget-editbutton="false" data-widget-sortable="false" data-widget-deletebutton="false" data-widget-togglebutton="false">
					<header>
						<span class="widget-icon"> <i class="fa fa-bar-chart-o"></i> </span>
						<h2>Статистика по заявкам</h2>
					</header>

					<!-- widget div-->
					<div>
						<!-- widget edit box -->
						<div class="jarviswidget-editbox">
							<!-- This area used as dropdown edit box -->
						</div>
						<!-- end widget edit box -->

						<!-- widget content -->
						<div class="widget-body">
							<div class="table-responsive">
								<table class="table table-striped table-hover">
									<thead>
										<tr>
											<th>Проект</th>
											<th class="text-center">Заявки</th>
											<th class="text-center">Не обработано</th>
											<th class="text-center">Отказ</th>
											<th class="text-center">Повтор</th>
											<th class="text-center">Неверный номер</th>
											<th class="text-center">В работе</th>
											<th class="text-center">Ожидают оплаты</th>
											<th class="text-center">Оплачены</th>
											<th class="text-center">Оформлены</th>
											<th class="text-center">Средняя доставка/средний чек</th>
										</tr>
									</thead>
									<tbody>
										<?php foreach($projects as $project): ?>
											<tr>
												<td>
													<p><?php echo $project->name; ?></p>
												</td>
												<td>
													<?php
														$leadsCnt = $project->leadsWithTrashed()->whereRaw($dateWhere2)->count(DB::raw('distinct visitor_id'));
														$leadsCnt2 = $project->leadsWithTrashed()->whereRaw($dateWhere2)->count();
														$leadsCntDiff = 0;
														//$leadsCntDiff = $leadsCnt - $project->leadsWithTrashed()->whereRaw('TIME(created_at)<TIME(NOW()) AND DATE(created_at)=DATE(DATE_SUB(NOW(), INTERVAL 1 DAY))')->count(DB::raw('distinct visitor_id'));
													?>
													<p class="text-center"><?php echo $leadsCnt; ?> (<?php echo $leadsCnt2; ?>) <?php if($leadsCntDiff): ?><span class="<?php if($leadsCntDiff<0): ?> diff-minus <?php elseif($leadsCntDiff>0): ?> diff-plus <?php endif; ?>"><?php if($leadsCntDiff>0): ?>+<?php endif; ?><?php echo $leadsCntDiff; ?></span><?php endif; ?></p>
												</td>
												<td>
													<p class="text-center"><?php echo $project->leads()->whereRaw($dateWhere2)->count(); ?></p>
												</td>
												<td>
													<p class="text-center"><?php echo $project->partners()->checkStatus(array('statusType'=>'reject'))->whereRaw($dateWhere)->count(); ?></p>
												</td>
												<td>
													<p class="text-center"><?php echo $project->partners()->whereStatus(Partner::STATUS_REPEATED)->whereRaw($dateWhere)->count(); ?></p>
												</td>
												<td>
													<p class="text-center"><?php echo $project->partners()->whereStatus(Partner::STATUS_TRASH_PHONE)->whereRaw($dateWhere)->count(); ?></p>
												</td>
												<td>
													<p class="text-center"><?php echo $project->partners()->checkStatus(array('statusType'=>'work'))->whereRaw($dateWhere)->count(); ?></p>
												</td>
												<td>
													<p class="text-center"><?php echo $project->partners()->checkStatus(array('statusType'=>'paywaiting'))->whereRaw($dateWhere)->count(); ?></p>
												</td>
												<td>
													<p class="text-center"><?php echo $project->partners()->whereIn('status', array(Partner::STATUS_DELIVERY_AFTER_REGISTRATION, Partner::STATUS_DELIVERY_AFTER_WAIT_DELIVERING))->whereRaw($dateWhere)->count(); ?></p>
												</td>
												<td>
													<p class="text-center">
														<?php
															$count = $project->partners()->checkStatus(array('statusType'=>'agreed', 'type'=>'all'))->whereRaw($dateWhere)->count();
															$deliveryCount = $project->partners()->checkStatus(array('statusType'=>'agreed', 'type'=>'all'))->whereTransportType(Partner::TRANSPORT_TYPE_CDEK)->where('cdek_receiverCityId', '!=', 44)->whereRaw($dateWhere)->count();
															$sum = $project->partners()->checkStatus(array('statusType'=>'agreed', 'type'=>'all'))->whereRaw($dateWhere)->sum('cartprice');
															$deliverySum = $project->partners()->checkStatus(array('statusType'=>'agreed', 'type'=>'all'))->whereTransportType(Partner::TRANSPORT_TYPE_CDEK)->where('cdek_receiverCityId', '!=', 44)->whereRaw($dateWhere)->sum('deliveryprice');
														?>
														<?php echo $count; ?><br/>
														<span class="to-humanize" title="Сумма"><?php echo $sum; ?></span> <i class="fa fa-rub"></i><br/>
														<span class="to-humanize" title="Прибыль"><?php echo $sum - $project->partners()->checkStatus(array('statusType'=>'agreed', 'type'=>'all'))->whereRaw($dateWhere)->sum('purchaseprice'); ?></span> <i class="fa fa-rub"></i><br/>
													</p>
												</td>
												<td>
													<p class="text-center">
														<?php
															$deliveryAvg = 0;
															$sumAvg = 0;
															if ($deliveryCount) {
																$deliveryAvg = $deliverySum / $deliveryCount;
															}
															if ($count) {
																$sumAvg = $sum / $count;
															}
														?>
														<span class="to-humanize" title="Средняя доставка"><?php echo round($deliveryAvg, 0); ?></span> <i class="fa fa-rub"></i><br/>
														<span class="to-humanize" title="Средний чек"><?php echo round($sumAvg, 0); ?></span> <i class="fa fa-rub"></i><br/>
													</p>
												</td>
											</tr>
											<?php
												$entrypoints = $project->leads()->withTrashed()->whereRaw($dateWhere2)->groupBy('entrypoint')->orderBy('entrypoint', 'ASC')->get(array(DB::raw('COUNT(*) as cnt, entrypoint')));
											?>
											<?php foreach($entrypoints as $entrypoint): ?>
												<tr>
													<td>
														<p>&nbsp;&nbsp;&nbsp;<small><?php echo $entrypoint->entrypoint; ?></small></p>
													</td>
													<td>
														<?php
															$leadsCnt = $project->leadsWithTrashed()->whereEntrypoint($entrypoint->entrypoint)->whereRaw($dateWhere2)->count(DB::raw('distinct visitor_id'));
															$leadsCnt2 = $project->leadsWithTrashed()->whereEntrypoint($entrypoint->entrypoint)->whereRaw($dateWhere2)->count();
															$leadsCntDiff = 0;
															//$leadsCntDiff = $leadsCnt - $project->leadsWithTrashed()->whereEntrypoint($entrypoint->entrypoint)->whereRaw('TIME(created_at)<TIME(NOW()) AND DATE(created_at)=DATE(DATE_SUB(NOW(), INTERVAL 1 DAY))')->count(DB::raw('distinct visitor_id'));
														?>
														<p class="text-center"><?php echo $leadsCnt; ?> (<?php echo $leadsCnt2; ?>) <?php if($leadsCntDiff): ?><span class="<?php if($leadsCntDiff<0): ?> diff-minus <?php elseif($leadsCntDiff>0): ?> diff-plus <?php endif; ?>"><?php if($leadsCntDiff>0): ?>+<?php endif; ?><?php echo $leadsCntDiff; ?></span><?php endif; ?></p>
													</td>
													<td>
														<p class="text-center"><?php echo $project->leads()->whereEntrypoint($entrypoint->entrypoint)->whereRaw($dateWhere2)->count(); ?></p>
													</td>
													<td>
														<p class="text-center"><?php echo $project->partners()->checkStatus(array('statusType'=>'reject'))->whereRaw($dateWhere)->whereRaw(DB::raw('lead_id IN (SELECT id FROM leads WHERE '.$dateWhere2.' AND entrypoint=?)'), array($entrypoint->entrypoint))->count(); ?></p>
													</td>
													<td>
														<p class="text-center"><?php echo $project->partners()->whereStatus(Partner::STATUS_REPEATED)->whereRaw($dateWhere)->whereRaw(DB::raw('lead_id IN (SELECT id FROM leads WHERE '.$dateWhere2.' AND entrypoint=?)'), array($entrypoint->entrypoint))->count(); ?></p>
													</td>
													<td>
														<p class="text-center"><?php echo $project->partners()->whereStatus(Partner::STATUS_TRASH_PHONE)->whereRaw($dateWhere)->whereRaw(DB::raw('lead_id IN (SELECT id FROM leads WHERE '.$dateWhere2.' AND entrypoint=?)'), array($entrypoint->entrypoint))->count(); ?></p>
													</td>
													<td>
														<p class="text-center"><?php echo $project->partners()->checkStatus(array('statusType'=>'work'))->whereRaw($dateWhere)->whereRaw(DB::raw('lead_id IN (SELECT id FROM leads WHERE '.$dateWhere2.' AND entrypoint=?)'), array($entrypoint->entrypoint))->count(); ?></p>
													</td>
													<td>
														<p class="text-center"><?php echo $project->partners()->checkStatus(array('statusType'=>'paywaiting'))->whereRaw($dateWhere)->whereRaw(DB::raw('lead_id IN (SELECT id FROM leads WHERE '.$dateWhere2.' AND entrypoint=?)'), array($entrypoint->entrypoint))->count(); ?></p>
													</td>
													<td>
														<p class="text-center"><?php echo $project->partners()->whereIn('status', array(Partner::STATUS_DELIVERY_AFTER_REGISTRATION, Partner::STATUS_DELIVERY_AFTER_WAIT_DELIVERING))->whereRaw($dateWhere)->whereRaw(DB::raw('lead_id IN (SELECT id FROM leads WHERE '.$dateWhere2.' AND entrypoint=?)'), array($entrypoint->entrypoint))->count(); ?></p>
													</td>
													<td>
														<p class="text-center">
															<?php
																$count = $project->partners()->checkStatus(array('statusType'=>'agreed', 'type'=>'all'))->whereRaw($dateWhere)->whereRaw(DB::raw('lead_id IN (SELECT id FROM leads WHERE '.$dateWhere2.' AND entrypoint=?)'), array($entrypoint->entrypoint))->count();
																$deliveryCount = $project->partners()->checkStatus(array('statusType'=>'agreed', 'type'=>'all'))->whereTransportType(Partner::TRANSPORT_TYPE_CDEK)->where('cdek_receiverCityId', '!=', 44)->whereRaw($dateWhere)->whereRaw(DB::raw('lead_id IN (SELECT id FROM leads WHERE '.$dateWhere2.' AND entrypoint=?)'), array($entrypoint->entrypoint))->count();
																$sum = $project->partners()->checkStatus(array('statusType'=>'agreed', 'type'=>'all'))->whereRaw($dateWhere)->whereRaw(DB::raw('lead_id IN (SELECT id FROM leads WHERE '.$dateWhere2.' AND entrypoint=?)'), array($entrypoint->entrypoint))->sum('cartprice');
																$deliverySum = $project->partners()->checkStatus(array('statusType'=>'agreed', 'type'=>'all'))->whereTransportType(Partner::TRANSPORT_TYPE_CDEK)->where('cdek_receiverCityId', '!=', 44)->whereRaw($dateWhere)->whereRaw(DB::raw('lead_id IN (SELECT id FROM leads WHERE '.$dateWhere2.' AND entrypoint=?)'), array($entrypoint->entrypoint))->sum('deliveryprice');
															?>
															<?php echo $count; ?><br/>
															<span class="to-humanize" title="Сумма"><?php echo $sum; ?></span> <i class="fa fa-rub"></i><br/>
															<span class="to-humanize" title="Прибыль"><?php echo $sum - $project->partners()->checkStatus(array('statusType'=>'agreed', 'type' => 'all'))->whereRaw($dateWhere)->whereRaw(DB::raw('lead_id IN (SELECT id FROM leads WHERE '.$dateWhere2.' AND entrypoint=?)'), array($entrypoint->entrypoint))->sum('purchaseprice'); ?></span> <i class="fa fa-rub"></i><br/>
														</p>
													</td>
													<td>
														<p class="text-center">
															<?php
																$deliveryAvg = 0;
																$sumAvg = 0;
																if ($deliveryCount) {
																	$deliveryAvg = $deliverySum / $deliveryCount;
																}
																if ($count) {
																	$sumAvg = $sum / $count;
																}
															?>
															<span class="to-humanize" title="Средняя доставка"><?php echo round($deliveryAvg, 0); ?></span> <i class="fa fa-rub"></i><br/>
															<span class="to-humanize" title="Средний чек"><?php echo round($sumAvg, 0); ?></span> <i class="fa fa-rub"></i><br/>
														</p>
													</td>
												</tr>
											<?php endforeach; ?>
										<?php endforeach; ?>
									</tbody>
								</table>
							</div>
						</div>
						<!-- end widget content -->
					</div>
					<!-- end widget div -->
				</div>
				<!-- end widget -->
			</article>
		</div>
	</section>

<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.default', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>