<?php $__env->startSection('content'); ?>
	<?php
		$date = date('Y-m-d',strtotime('last sunday of last month'))." 00:00:00";
		$month = date('n',strtotime($date));
		$transition = 7;
		for($i=0;$i<7;$i++)
		{
			$tempDate = date('Y-m-d',strtotime('+'.$i.' days',strtotime($date)))." 00:00:00";
			$tempMonth = date('n',strtotime($tempDate));
			if($month == $tempMonth)
			{
				$transition = $i+1;
			}
		}

		$timecards = \App\Timecard::where('date','=',$date)->orderBy('created_by')->get();
		$timecardsContainer = array();
		foreach($timecards as &$timecard)
		{
			$timecard->employee = \App\Employee::find($timecard->employee);
			$timecard->user = \App\User::find($timecard->employee->user);
			
			$timecardsContainer[$timecard->id] = [
				'timecard' => $timecard,
				'entries' => array()
			];
		}
		$entries = \App\Entry::whereIn('timecard',array_keys($timecardsContainer))->where('type', '=', 'allocation')->orderBy('timecard','asc')->get();
		foreach($entries as $entry)
		{
			array_push($timecardsContainer[$entry->timecard]['entries'],$entry);
		}
	?>
	<div class="row">
    	<div class="col-lg-12">
    		<h1 class="page-header">Timecard</h1> 
    	</div>
    </div>
    <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <?php if(session('status')): ?>
					<div class="alert alert-success">
						<?php echo e(session('status')); ?>

					</div>
				<?php endif; ?>
	
				<?php if(count($errors) > 0): ?>
					<div class="alert alert-danger">
						<strong>Whoops!</strong> There were some problems with your input.<br><br>
						<ul>
							<?php foreach($errors->all() as $error): ?>
								<li><?php echo e($error); ?></li>
							<?php endforeach; ?>
						</ul>
					</div>
				<?php endif; ?>
                <div class="panel-heading">
                    Allocation Split Report: <?php echo e(strtoupper(date('d-M-Y', strtotime($date)))); ?> through <?php echo e(strtoupper(date('d-M-Y', strtotime('+6 days', strtotime($date))))); ?>

                </div>
                
                <div class="panel-body">
                	<?php 
				        foreach($timecardsContainer as $container)
				        {
				        	$timecard = $container['timecard'];
                			$employee = $timecard->employee;
                			$user = $timecard->user;
                			$timecard->entries = $container['entries'];

							$one = array();
							$two = array();
							$all = array();
							
							foreach($timecard->entries as $entry)
							{
								if($entry->day <= $transition)
								{
									if(array_key_exists($entry->account, $one))
									{
										$one[$entry->account] += $entry->value;
									}
									else
									{
										$one[$entry->account] = $entry->value;
									}
								}
								else 
								{
									if(array_key_exists($entry->account, $two))
									{
										$two[$entry->account] += $entry->value;
									}
									else
									{
										$two[$entry->account] = $entry->value;
									}
								}
								
								if(array_key_exists($entry->account, $all))
								{
									$all[$entry->account] += $entry->value;
								}
								else
								{
									$all[$entry->account] = $entry->value;
								}
							}

							echo "<h3>" . $user->name . "</h3><br>";

							if(count($timecard->entries) > 0)
							{
								foreach($all as $k => $v)
								{
									$allocation = \App\Allocation::find($k);
									$may = array();
									$june = array();
									
									echo "<h4>".$allocation->description."</h4>";
									

									
									if(array_key_exists($k, $one))
									{
										echo "<strong>".date('F',strtotime($date))."</strong><br>";
											
										echo "<table>";
										echo "<tr>";
										echo "<th>Account</th><th>Company</th><th>Description</th><th>Value</td>";
										echo "</tr>";
											
										$may = $allocation->calculate($one[$k]);
										foreach($may as $key => $value)
										{
											$account = \App\Account::find($key);
											echo "<tr>";
											echo "<td>";
											echo $account->number;
											echo "<td>";
											echo $account->company;
											echo "<td>";
											echo $account->description;
											echo "<td>";
											echo $value;
											echo "</tr>";
											
										}
										echo "<tr>";
										echo "<td colspan='3'>&nbsp;</td>";
										echo "<td><strong>";
										echo array_key_exists($k, $one) ? $one[$k] : 0; 
										echo "</strong></td></tr>";
										echo "</table>";
									}
									
									if(array_key_exists($k, $two))
									{
										echo "<strong>".date('F',strtotime('+7 days',strtotime($date)))."</strong><br>";
											
										echo "<table>";
										echo "<tr>";
										echo "<th>Account</th><th>Company</th><th>Description</th><th>Value</td>";
										echo "</tr>";
											
										$june = $allocation->calculate($two[$k]);
										foreach($june as $key => $value)
										{
											$account = \App\Account::find($key);
											echo "<tr>";
											echo "<td>";
											echo $account->number;
											echo "<td>";
											echo $account->company;
											echo "<td>";
											echo $account->description;
											echo "<td>";
											echo $value;
											echo "</tr>";
											
										}
										echo "<tr>";
										echo "<td colspan='3'>&nbsp;</td>";
										echo "<td><strong>";
										echo array_key_exists($k, $two) ? $two[$k] : 0; 
										echo "</strong></td></tr>";
										echo "</table>";
									}
									
									echo "<strong>Total</strong><br>";
										
									echo "<table>";
									echo "<tr>";
									echo "<th>Account</th><th>Company</th><th>Description</th><th>Value</td>";
									echo "</tr>";
										
									foreach($allocation->calculate($v) as $key => $value)
									{
										$account = \App\Account::find($key);
										echo "<tr>";
										echo "<td>";
										echo $account->number;
										echo "<td>";
										echo $account->company;
										echo "<td>";
										echo $account->description;
										echo "<td>";
										echo $value;
										
										$mt = array_key_exists($key, $may) ? $may[$key] : 0;
										$jt = array_key_exists($key, $june) ? $june[$key] : 0;
										if(($mt + $jt) != $value)
										{
											
											echo "&nbsp; <i class='fa fa-exclamation-triangle' style='color: red;'></i>";
										}
										echo "</tr>";
									}
									echo "<tr>";
									echo "<td colspan='3'>&nbsp;</td>";
									echo "<td><strong>";
									echo array_key_exists($k, $all) ? $all[$k] : 0;
									echo "</strong></td></tr>";

									echo "</table>";
									
									echo "<hr>";
								}
									
								
							}
							else
							{
								echo "<p style='padding: 10px;'>No Allocations</p><hr>";
							}
							
							echo "<br><br>";
						} 
        			?>
        		</div>
    		</div>
		</div>
	</div>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('extras'); ?>
	<style>
		td, th {
			padding: 10px;
		}
	</style>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('bottom'); ?>

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