<table cellpadding="0" cellspacing="0" class="shipments">
    <tr>
	<th class="actions"><?php __('Actions');?></th>
	<th><?php echo $paginator->sort('created');?></th>
	<th><?php echo $paginator->sort('date_arrived');?></th>
	<th><?php echo $paginator->sort('date_dispatched');?></th>
	<th><?php echo $paginator->sort('type');?></th>
	<th>Principle(s)</th>
	<th>PO(s)</th>
	<th>Jobs</th>
	<th><?php echo $paginator->sort('# Boxes','box_count');?></th>
	<th>Box Details<br>
		LxWxH (cm)
	</th>
	<th>Weight (kg)</th>
	<th><?php echo $paginator->sort('freight_forwarder_id');?></th>
	<th><?php echo $paginator->sort('airway_bill');?></th>
	<th># Invoices</th>
	<th>Issued By</th>
	<th>Invoice Amount</th>
	<th>Deferred GST</th>
	<th>Approved?</th>
	<th>Paid?</th>
	<th>Date Paid</th>


	<th><?php echo $paginator->sort('comments');?></th>
	<th class="actions"><?php __('Actions');?></th>
    </tr>
    <?php
    $i = 0;
    foreach ($shipments as $shipment):
	?>
    <tr>
	<td class="actions">
	    <button id="<?=$shipment['Shipment']['id']?>" class="editButton">Edit</button>
		
		<button class="deleteButton">Delete</button>
	</td>
	<td>
	    <?php
	    $unixtime = strtotime($shipment['Shipment']['created']);
	    $dateString = date('j M Y', $unixtime);
	    echo $dateString;
	    ?>
	
	</td>

	<td>
		<?php
		echo $this->element('isEmptyDate', array('date'=>$shipment['Shipment']['date_arrived']));
		?>
	</td>
	<td>
		<?php
		echo $this->element('isEmptyDate', array('date'=>$shipment['Shipment']['date_dispatched']));
		?>
	</td>

	<td>
		<?php echo $shipmentTypeShort[$shipment['Shipment']['type']]; ?>
	</td>

	<td>
		<?php foreach($shipment['Principle'] as $principle):?>
		    <? if(!empty($principle['short_name'])) {
			echo $html->link($principle['short_name'], array('controller'=>'principles','action'=>'view',$principle['id']));
		    }
		    else {
			echo $html->link($principle['name'], array('controller'=>'principles','action'=>'view',$principle['id']));
		    }
		    ?>

	    <br>
		<?php endforeach;?>
	</td>

	<td>
		<?php foreach($shipment['PurchaseOrder'] as $po):?>
		    <?=$html->link($po['title'], array('controller'=>'purchase_orders', 'action'=>'view', $po['id']));?>
	    <br>
		<?endforeach;?>
	</td>

	<td>
		<?php foreach($shipment['Job'] as $job):?>
		    <?=$html->link($job['title'], array('controller'=>'jobs', 'action'=>'view', $job['id']));?>
	    <br>
		<?endforeach;?>
	</td>

	<td>
		<?php echo $shipment['Shipment']['box_count']; ?>
	</td>

	<td  class="boxDimensions">

		<?php

		$totalWeight = 0;
		$weightString = '';

		foreach($shipment['Box'] as $box):?>
		    <?=$box['length']?>x<?=$box['width']?>x<?=$box['height']?><br>
		    <?
		    $weightString .= $box['weight'].' kg <br>';
		    $totalWeight += $box['weight']; ?>
		<?php endforeach;?>

	</td>

	<td>    <?

		$boxCount = $shipment['Shipment']['box_count'];
		if($boxCount > 1) {
		    echo $weightString;
		    echo '<hr>';

		}?>
		<? if($boxCount != 0): ?>
	    <span class="totalWeight"><?=$totalWeight;?> kg</span></td>
	    <? endif;?>
	<td>
		<?php echo $html->link($shipment['FreightForwarder']['name'], array('controller' => 'freight_forwarders', 'action' => 'view', $shipment['FreightForwarder']['id'])); ?>
	</td>


	<td>
		<?php echo $shipment['Shipment']['airway_bill']; ?>
	</td>

	<td>
		<?=$shipment['Shipment']['shipment_invoice_count'];?>
	</td>
	<td><?
		foreach($shipment['ShipmentInvoice'] as $si) {
		    if(!empty($si['principle_id'])) {
			echo $principles[$si['principle_id']];
		    }
		    else if(!empty($si['freight_forwarder_id'])) {
			echo $freightForwarders[$si['freight_forwarder_id']];
		    }
		    else if($si['invoice_type'] == 2) {
			echo "CUSTOMS";
		    }
		    else {
			echo "";
		    }
		    echo "<br>";
		}
		?>
	</td>

	<td class="boxDimensions"><? foreach($shipment['ShipmentInvoice'] as $si) {
		    echo $currencies[$si['currency_id']]." ".$si['invoice_amount']."<br />";
		}?>
	</td>
	<td><?

		foreach($shipment['ShipmentInvoice'] as $si) {

		    if($si['deferred_gst'] == 1) {
			echo $currencies[$si['currency_id']]." ".$si['deferred_gst_amount']."<br />";
		    }
		}
		?>
	</td>

	<td><?

		foreach($shipment['ShipmentInvoice'] as $si) {
		    echo $this->element('booleanTick', array('bool'=>$si['approved'])); }
		?>
	</td>
	<td><? foreach($shipment['ShipmentInvoice'] as $si) {
		    echo $this->element('booleanTick', array('bool'=>$si['paid']));
		}?>
	</td>
	<td><? foreach($shipment['ShipmentInvoice'] as $si) {
		    echo $this->element('isEmptyDate', array('date'=>$si['date_paid']));
		}
		?>
	</td>
	<td>
		<?php echo $shipment['Shipment']['comments']; ?>
	</td>
	<td class="actions">
	    <button id="<?=$shipment['Shipment']['id']?>" class="editButton">Edit</button>
	</td>
    </tr>
    <?php endforeach; ?>
</table>

<?php debug($currencies); ?>
