<div class="content-add01">
	<div class="heading01"><h1>Liste des commandes simulées (<?php echo $pager->getNbResults() ?>)</h1></div>
</div>

<?php if (empty($katao_orderList)): ?>
	<p>Aucune commande simulée.</p>
<?php else : ?>
  <?php echo link_to('Supprimer toutes les commandes simulées', '@katao_order_delete_simulated', array('class' => 'delete', 'confirm' => 'Êtes-vous sûr de vouloir supprimer toutes les commandes simulées ?')) ?>

  <br /><br /><?php echo checkbox_tag('toggle_complete', 1, false, array('onclick' => 'toggleCompleteOrders();')) ?><?php echo label_for('toggle_complete', 'Afficher uniquement les commandes incomplètes') ?>

  <?php use_helper('Javascript') ?>
  <table class="admin-list">
    <tr>
	  <th nowrap="nowrap">N&deg;</th>
	  <th nowrap="nowrap">Date</th>
	  <th nowrap="nowrap">Fournisseur</th>
	  <th nowrap="nowrap">Montant / Min.</th>
	  <th nowrap="nowrap">Noeud</th>
	  <th nowrap="nowrap">Période</th>
	</tr>
	<?php $total_total = 0 ?>
	<?php foreach ($katao_orderList as/*(KataoOrder)*/ $katao_order): ?>

	<?php
        $katao_order_products = $katao_order->getKataoOrderProductsJoinKataoProduct();

    $total = 0;
    $katao_order_products_data = array();
    foreach ($katao_order_products as/*(KataoOrderProduct)*/ $katao_order_product) {
        $katao_product = $katao_order_product->getKataoProduct();

        $quantity = $katao_order_product->getQuantity();

        $price = round($katao_order_product->getProductPriceEuro() * (1 + $katao_order_product->getProductTvaRate()), 2);
        $sub_total = $quantity * $price;
        $total += $sub_total;
        $total_total += $sub_total;

        $katao_order_products_data[] = array('id' => $katao_order_product->getId(),
            'product_name' => $katao_order_product->getProductName(),
            'quantity' => $quantity,
            'price' => $price,
            'sub_total' => $sub_total,
            'last_incomplete_package' => $katao_product->getLastIncompletePackageQuantity($katao_order->getKataoPeriodId(), $katao_order->getKataoNodeId(), true),
            'order_details' => $katao_product->getOrderDetails($katao_order->getKataoPeriodId(), $katao_order->getKataoNodeId(), true),
            );
    }

    ?>

    <tr valign="top" id="tr-<?php echo $katao_order->getId() ?>" class="line complete incomplete">
		  <td nowrap="nowrap">
			<a href="javascript://" onclick="toggleDetails('<?php echo $katao_order->getId() ?>');" title="Détails"><?php echo image_tag('actions/expand.png', array('alt' => 'Détails', 'id' => 'order-icon-' . $katao_order->getId(), 'align' => 'bottom')) ?></a>
		    <?php echo $katao_order->getNumber() ?>
		  </td>
		  <td nowrap="nowrap"><?php echo $katao_order->getCreatedAtStr() ?></td>
		  <td><?php echo $katao_order->getSupplierName() ?></td>
		  <td nowrap="nowrap" style="text-align:right!important">
		  	<?php echo Utils::formatCurrencyEuro($total) ?> / <?php echo Utils::formatCurrencyEuro($katao_order->getKataoSupplier()->getOrderMinAmount()) ?>
		  	<?php $order_min_amount = $katao_order->getKataoSupplier()->getOrderMinAmount() ?>
		  	<?php $is_adjustable = $total >= $order_min_amount ?>
		  	<?php if (!$is_adjustable): ?>
		  		<?php echo javascript_tag('$("tr-' . $katao_order->getId() . '").removeClassName("complete");') ?>
		  	<?php endif ?>
			<span id="order-status-red-<?php echo $katao_order->getId() ?>" style="display: <?php echo $is_adjustable?'none':'inline' ?>"><?php echo image_tag('actions/bullet_red.png', array('alt' => 'Montant minimum NON atteint et/ou colisage NON complet', 'title' => 'Montant minimum NON atteint et/ou colisage NON complet', 'align' => 'top')) ?></span>
		  	<span id="order-status-green-<?php echo $katao_order->getId() ?>" style="display: <?php echo $is_adjustable?'inline':'none' ?>"><?php echo image_tag('actions/bullet_green.png', array('alt' => 'Montant minimum atteint et colisage complet', 'title' => 'Montant minimum atteint et colisage complet', 'align' => 'top')) ?></span>
		  </td>
		  <td nowrap="nowrap"><?php echo $katao_order->getNodeLink() ?></td>
		  <td nowrap="nowrap"><?php echo $katao_order->getPeriodLink() ?></td>
	</tr>
	<tr valign="top" style="display: none" id="order-details-<?php echo $katao_order->getId() ?>">
	  <td colspan="9">
	    <table class="admin-sub-list">
	  	<tr>
	  		<th>Produit</th>
			<th nowrap="nowrap" width="1%">Colisage</th>
			<th nowrap="nowrap" width="1%">Quantité</th>
	  		<th nowrap="nowrap" width="1%">Prix TTC</th>
	  		<th nowrap="nowrap" width="1%">Total</th>
		</tr>
		<?php foreach ($katao_order_products_data as $katao_order_product_infos): ?>
		<tr valign="top" class="sub-line <?php echo !empty($katao_order_product_infos['last_incomplete_package'])?'incomplete':'complete' ?>">
	  		<td>
	  			<?php echo $katao_order_product_infos['product_name'] ?>
	  			<?php echo link_to(image_tag('actions/users.png', array('alt' => 'Voir la liste des adhérents', 'align' => 'top')), '@katao_order_simulated_members?id=' . $katao_order_product_infos['id'], array('title' => 'Voir la liste des adhérents')) ?>
			<?php if (!empty($katao_order_product_infos['last_incomplete_package'])): ?>
				<?php echo javascript_tag('$("tr-' . $katao_order->getId() . '").removeClassName("complete"); $("order-status-red-' . $katao_order->getId() . '").style.display = "inline"; $("order-status-green-' . $katao_order->getId() . '").style.display = "none";') ?>
			<?php endif ?>
	  		</td>
	  		<td nowrap="nowrap"><?php echo $katao_order_product_infos['order_details'] ?></td>
	  		<td nowrap="nowrap" style="text-align:right!important"><?php echo $katao_order_product_infos['quantity'] ?></td>
	  		<td nowrap="nowrap" style="text-align:right!important"><?php echo Utils::formatCurrencyEuro($katao_order_product_infos['price']) ?></td>
	  		<td nowrap="nowrap" style="text-align:right!important"><?php echo Utils::formatCurrencyEuro($katao_order_product_infos['sub_total']) ?></td>
		</tr>
		<?php endforeach ?>
		<tr>
			<td colspan="2" style="border:none!important">&nbsp;</td>
			<th colspan="2" nowrap="nowrap" style="text-align:right!important">TOTAL TTC</th>
			<th nowrap="nowrap" style="text-align:right!important"><?php echo utils::formatCurrencyEuro($total) ?></th>
		</tr>
		</table>
	  <?php if ('' != $katao_order->getDeliveryAddress()): ?>
		<p><u>Adresse de livraison</u> : <?php echo str_replace("\n", ' - ', $katao_order->getDeliveryAddress()) ?></p>
	  <?php endif ?>
	  </td>
	</tr>
  <?php endforeach ?>
    <tr>
	  <td colspan="3" style="border:none!important">&nbsp;</td>
	  <th nowrap="nowrap" style="text-align:right!important"> <?php echo Utils::formatCurrencyEuro($total_total) ?></th>
	  <td colspan="2" style="border:none!important">&nbsp;</td>
	</tr>
  </table>
<?php endif ?>

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

function toggleDetails( id ){
	$('order-details-'+id).toggle();
	if($('order-details-'+id).visible()) {
		$('order-icon-'+id).src = '<?php echo image_path('actions/collapse.png') ?>';
	} else {
		$('order-icon-'+id).src = '<?php echo image_path('actions/expand.png') ?>';
	}
}


function toggleCompleteOrders(){
	var orders = $$('.line');
	for(var i = 0; i < orders.length; i++){
		orders[i].show();
	}

	var products = $$('.sub-line');
	for(var i = 0; i < products.length; i++){
		products[i].show();
	}

	if ($('toggle_complete').checked) {
		var complete_lines = $$('.complete');
		for(var i = 0; i < complete_lines.length; i++){
			complete_lines[i].hide();
		}
	}
}

/* ]]> */
</script>