<?php $katao_order = $katao_order_product->getKataoOrder() ?>
<div class="content-add01">
	<div class="heading01"><h1>Livraison de la commande "<?php echo $katao_order->getNumber() ?>" du fournisseur "<?php echo $katao_order->getSupplierName() ?>" pour la période "<?php echo $katao_order->getKataoPeriod()->getName() ?>"</h1></div>
</div>

<?php echo form_tag('@katao_order_product_deliver?id=' . $katao_order_product->getId()) ?>

<h3><?php echo $katao_order_product->getProductName() ?> (quantité réceptionnée: <?php echo $katao_order_product->getQuantityReceived() ?>)</h3>

<table class="admin-sub-list">
<tr>
	<td style="border:none!important">&nbsp;</td>
	<th colspan="3" style="text-align:center!important">Quantités</th>
</tr>
<tr>
	<th>Adhérent</th>
	<th nowrap="nowrap" width="1%" style="text-align:center!important">Commandée</th>
	<th nowrap="nowrap" width="1%" style="text-align:center!important">Ajustée</th>
	<th nowrap="nowrap" width="1%" style="text-align:center!important">Délivrée</th>
</tr>
<?php $total_delivered = 0 ?>
<?php foreach ($katao_order_product->getKataoCartProductsJoinKataoCart() as/*(KataoCartProduct)*/ $katao_cart_product): ?>
	<?php $katao_member = $katao_cart_product->getKataoCart()->getMember() ?>
	<?php $quantity = $katao_order_product->getHasBeenDelivered()?$katao_cart_product->getQuantityDelivered():$katao_cart_product->getQuantityAdjusted() ?>
	<?php $total_delivered += $quantity ?>
<tr>
 	<td><?php echo $katao_member->getFullName() ?></td>
 	<td style="text-align:center!important"><?php echo $katao_cart_product->getQuantity() ?></td>
 	<td style="text-align:center!important"><?php echo $katao_cart_product->getQuantityAdjusted() ?></td>
 	<td nowrap="nowrap" style="text-align:center!important">
 		<a href="javascript://" onclick="document.getElementById('quantity_<?php echo $katao_cart_product->getId() ?>').value = Math.max(0, parseInt(document.getElementById('quantity_<?php echo $katao_cart_product->getId() ?>').value) - 1); updateTotalDelivered();" title="Diminuer la quantité"><?php echo image_tag('minus.png', array('alt' => 'Diminuer la quantité', 'align' => 'top')) ?></a>
		<?php echo input_tag('quantity[' . $katao_cart_product->getId() . ']', $quantity, array('style' => 'width: 20px; text-align: center;', 'class' => 'quantity-delivered', 'onchange' => 'updateTotalDelivered();')) ?>
		<a href="javascript://" onclick="document.getElementById('quantity_<?php echo $katao_cart_product->getId() ?>').value = parseInt(document.getElementById('quantity_<?php echo $katao_cart_product->getId() ?>').value) + 1; updateTotalDelivered();" title="Augmenter la quantité"><?php echo image_tag('plus.png', array('alt' => 'Augmenter la quantité', 'align' => 'top')) ?></a>
	</td>
</tr>
<?php endforeach ?>
<tr>
	<td style="border:none!important">&nbsp;</td>
	<th colspan="2" style="text-align:right!important">Total <?php echo image_tag('actions/deliver.png', array('alt' => 'Quantité délivrée', 'title' => 'Quantité délivrée', 'align' => 'top')) ?> / <?php echo image_tag('actions/receive.png', array('alt' => 'Quantité réceptionnée', 'title' => 'Quantité réceptionnée', 'align' => 'top')) ?></th>
	<th style="text-align:center!important"><span id="total-delivered"><?php echo $total_delivered ?></span> / <?php echo $katao_order_product->getQuantityReceived() ?></th>
</tr>
</table>

<br /><?php echo submit_tag('Délivrer', array('onclick' => 'if(parseInt($("total-delivered").innerHTML)!=' . $katao_order_product->getQuantityReceived() . '){alert("La somme des quantités par adhérent ne correspond pas à la quantité réceptionnée");return false;}')) ?> ou <a href="<?php echo url_for('kataoOrder/index') ?>">annuler</a>

</form>

<script type="text/javascript" language="JavaScript">
/* <![CDATA[ */

function updateTotalDelivered(){
	var total_delivered = 0;
	var delivered_quantities = $$('.quantity-delivered');
	for(var i = 0; i < delivered_quantities.length; i++){
		total_delivered += parseInt(delivered_quantities[i].value);
	}
	$('total-delivered').innerHTML = total_delivered;
}

/* ]]> */
</script>