<?php 
	$this->Html->css(array('ui/jquery-ui.min'), '', array('inline'=>false));
	$this->Html->script(array('ui/jquery-ui-custom.min'),  array('inline'=>false));
	$query = $this->request->query;
?>
<h2 class="title"><?php echo __('Summary Report per Depot (From %s to %s)',$d1, $d2) ?></h2>
<?php $export = Hash::merge($query,array('export' => 'xls')); ?>
<div class="links">
	<ul>
		<li> <?php echo $this->Permission->link(__('Export'),array_merge($this->params['named'], array('controller'=>'reports', 'action'=>'summary_reports','?'=>$export)), array('class' => 'button','title'=>__('Export'))); ?> </li>
	</ul>
</div>


<div class="search-frm">
	<?php 
		echo $this->Form->create('Stock', array('id'=>'filter-form', 'novalidate' => true, 'type' => 'get', 'error'=>true, 'inputDefaults'=>array('div'=>false, 'label'=>false), 'errorMessage' => false)); 
		
		//echo $this->Form->input(null, array('name'=>'dtype', 'options' => depot_type(), 'value' =>isset($this->request->query['dtype'])?$this->request->query['dtype']:'', 'empty'=>__('All'), 'class'=>'field-inline '));
		
		echo $this->Form->input(null, array('name'=>'br', 'options' => $brands, 'value' =>isset($this->request->query['br'])?$this->request->query['br']:'', 'empty'=>__('All Brands'), 'class'=>'field-inline '));
		?>
		<?php  if ($user['Role']['grade'] <= 2 && $user['Role']['id'] != DIVISIONMANAGER):?>
            <div class="inline">
                <label><?php echo __('Division:') ?></label>
                <?php echo $this->Form->input(null, array('name'=>'dv', 'options' => $divisions, 'id'=>'sdivision', 'value' =>isset($this->request->query['dv'])?$this->request->query['dv']:'', 'empty'=>__('All Divisions'), 'class'=>'field-inline ')); ?>
            </div>
        <?php endif; ?>
        <?php if ($user['Role']['grade'] <= 2): ?>
            <div class="inline">
                <label><?php echo __('Region:') ?></label>
                <?php echo $this->Form->input(null, array('name'=>'rg', 'id' => 'sregion', 'options' => $regions, 'value' =>isset($this->request->query['rg'])?$this->request->query['rg']:'', 'empty'=>__('All Regions'), 'class'=>'field-inline '));
                ?>
            </div>
        <?php endif; ?>
        <?php if ($user['Role']['grade'] <= 3 ): ?>
            <div class="inline">
                <label><?php echo __('Depot:') ?></label>
                <?php echo $this->Form->input(null, array('name'=>'dp','id'=>'sdepot', 'options' => $depoList, 'empty'=>__('All'), 'class'=>'field-inline',  'value' => isset($this->request->query['dp'])?$this->request->query['dp']:''));?>
            </div>
		<?php endif; ?>
		<div class="inline">
			<label><?php echo __('From:')?></label>
			<?php echo $this->Form->input(null, array('name'=>'from', 'id'=>'from-date', 'type'=>'text', 'class'=>'field-inline', 'value' => $d1));?>
		</div>
		<div class="inline">
			<label><?php echo __('To:') ?></label>
			<?php echo $this->Form->input(null, array('name'=>'to', 'id'=>'to-date', 'type'=>'text', 'class'=>'field-inline', 'value' => $d2));?>
		</div>
		<div class="inline">
			<label><?php echo __('Status:') ?></label>
			<?php echo $this->Form->input(null, array('name'=>'sts', 'options'=>vStatus(), 'class'=>'field-inline', 'value' => isset($this->request->query['sts'])?$this->request->query['sts']:'', 'empty'=>__('All'),));?>
		</div>
		<?php echo $this->Form->submit('Filter', array('class'=>'field-inline'));
		echo $this->Form->end(); ?> 
</div>
<?php //echo $this->element('reports/depot_reports') ?>
<section>
<?php 
//pr($depots);
$params = $this->Paginator->params();


		if (!empty($depots)): ?>
		    <div>
				<?php echo $this->Paginator->counter(__('Page {:page} of {:pages}, showing {:current} Depot Stocks Report out of {:count} total, starting on record {:start}, ending on {:end}') ); ?>
			</div>
			<div id="paging" class="paging">
			<?php 
			if($this->Paginator->hasPrev()):
				echo $this->Paginator->prev(__('<< Prev ') , array(), null, array('class' => 'prev disabled'));
			endif;
				echo $this->Paginator->numbers(); 
			if($this->Paginator->hasNext()):
				echo $this->Paginator->next(__(' Next >>'), array(), null, array('class' => 'next disabled'));
			endif;
			?>
			</div>
			<table class="tbl-list" cellpadding="0" cellspacing="0" border="0">
				<tr>
					<th>#</th>
					<th><?php echo $this->Paginator->sort('title', __('Depot'));?></th>
					<th><?php echo __('Brand');?></th>
					<th><?php echo __('Sales Quantity in Cases'); ?> </th>
					<th><?php echo __('Sales Value per Brand');?></th>
					<th><?php echo __('Total Sales Value');?></th>
					<th><?php echo __('Expenses');?></th>
					<th><?php echo __('Payments');?></th>
				</tr>			
				
				<?php 				
				
				$i = ($params['page'] - 1) * $params['limit'];
				foreach($depots as $depot): 
					$did = $depot['Depot']['id'];
					$dtype = 2;//$depot['Depot']['type'];
					$brandFirst = array();
					$brandRest = array();
					
					if($dtype == 1){
						$rows = isset($reportData[$did]['rdsales']['brand'])?count($reportData[$did]['rdsales']['brand']):1;
						
						if(isset($reportData[$did]['rdsales']['brand'])){
							$brandFirst = array_slice($reportData[$did]['rdsales']['brand'],0, 1, true);
							$brandRest = array_slice($reportData[$did]['rdsales']['brand'], 1,$rows, true);
						}
						$indkey = 'rdsales';
					}elseif($dtype == 2){
						$rows = isset($reportData[$did]['sales']['brand'])?count($reportData[$did]['sales']['brand']):1;
						if(isset($reportData[$did]['sales']['brand'])){ //pr($reportData[$did]['sales']['brand']);
							$brandFirst = array_slice($reportData[$did]['sales']['brand'],0, 1, true);
							$brandRest = array_slice($reportData[$did]['sales']['brand'], 1,$rows, true);
						}
						$indkey = 'sales';
					}
					else{
						$rows = isset($reportData[$did]['sale_redis_add']['brand'])?count($reportData[$did]['sale_redis_add']['brand']):1;
						if(isset($reportData[$did]['sale_redis_add']['brand'])){
							$brandFirst = array_slice($reportData[$did]['sale_redis_add']['brand'],0, 1, true);
							$brandRest = array_slice($reportData[$did]['sale_redis_add']['brand'], 1,$rows, true);
						}
						$indkey = 'sale_redis_add';
					}
					//pr($brandFirst);
					//pr($brandRest);
				?>
				<tr>
					<td <?php echo ($rows > 1)?"rowspan='$rows'":""?> ><?php echo ++$i; ?></td>
					<td <?php echo ($rows > 1)?"rowspan='$rows'":""?> ><?php echo $depot['Depot']['title']; ?></td>				
					<?php 
					if(!empty($brandFirst)):
					foreach($brandFirst as $bidF => $brandF): ?>
					<td class="no-lborder"><?php echo isset($brands[$bidF])?$brands[$bidF]:"" ?></td>
					<td align="right"><?php echo isset($brandF['total_qty'])? number_format($brandF['total_qty'], QTY_DECIMALS) :"" ?></td>
					<td align="right"><?php echo isset($brandF['total_amt'])? number_format($brandF['total_amt'], AMT_DECIMALS) :"" ?></td>	
					<?php endforeach; else: ?>
					<td class="no-lborder"></td>
					<td></td>
					<td></td>
					<?php endif; ?>
					<td <?php echo ($rows > 1)?"rowspan='$rows'":""?> align="right" ><?php echo isset($reportData[$did][$indkey]['sales_value'])? number_format($reportData[$did][$indkey]['sales_value'],2) :"" ; ?></td>
					<td <?php echo ($rows > 1)?"rowspan='$rows'":""?> align="right"><?php echo isset($reportData[$did]['expense']['total_amt'])? number_format($reportData[$did]['expense']['total_amt'],AMT_DECIMALS) :"" ?></td>
					<td <?php echo ($rows > 1)?"rowspan='$rows'":""?> align="right"><?php echo isset($reportData[$did]['payment']['total_amt'])? number_format($reportData[$did]['payment']['total_amt'],AMT_DECIMALS) :"" ?></td>
				</tr>	
				<?php 
				if($rows > 1): 
				foreach($brandRest as $bidR=>$brandR): ?>
				
				<tr>
					<td class="no-lborder"><?php echo isset($brands[$bidR])?$brands[$bidR]:"" ?></td>
					<td align="right"><?php echo isset($brandR['total_qty'])? number_format($brandR['total_qty'], QTY_DECIMALS) :"" ?></td>
					<td align="right"><?php echo isset($brandR['total_amt'])? number_format($brandR['total_amt'], AMT_DECIMALS) :"" ?></td>				
				</tr>	
				
				
				<?php
					endforeach; endif;?>
					
				
				<?php endforeach; ?>
			
			</table>
			
			<div id="paging" class="paging">
			<?php 
			if($this->Paginator->hasPrev()):
				echo $this->Paginator->prev(__('<< Prev ') , array(), null, array('class' => 'prev disabled'));
			endif;
				echo $this->Paginator->numbers(); 
			if($this->Paginator->hasNext()):
				echo $this->Paginator->next(__(' Next >>'), array(), null, array('class' => 'next disabled'));
			endif;
			?>
			</div>
		<?php else: ?>
			<p><?php echo __('Data not found') ?></p>
		<?php endif; ?>

</section>


<script>
jQuery(document).ready(function(){
	
	$( "#from-date" ).datepicker({	
		changeMonth: true,
		changeYear: true,
		dateFormat: 'yy-mm-dd',     
		onClose: function( selectedDate ) {
			$( "#to-date" ).datepicker( "option", "minDate", selectedDate );
		 }
    });
	
	$( "#to-date" ).datepicker({
		changeMonth: true,
		changeYear: true,
		dateFormat: 'yy-mm-dd',
		onClose: function( selectedDate ) {
			$( "#from-date" ).datepicker( "option", "maxDate", selectedDate );
		}
    });
});
</script>