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

<div class="filter">
	<div class="filter-left">&nbsp;</div>
	<div class="filter-middle">
		<?php echo form_tag('@katao_order_filter') ?>
			<?php echo select_tag('filter_status', options_for_select(KataoOrderPeer::getAllStatuses(), $filter_status, array('include_custom' => '&mdash;&nbsp;Etat&nbsp;&mdash;'))) ?>
			<?php echo image_tag('separator02.gif', array('alt' => '')) ?>&nbsp;<?php echo select_tag('filter_supplier', options_for_select(KataoSupplierPeer::getAllSimple(), $filter_supplier, array('include_custom' => '&mdash;&nbsp;Fournisseur&nbsp;&mdash;'))) ?>
			<?php echo image_tag('separator02.gif', array('alt' => '')) ?>&nbsp;<?php echo select_tag('filter_node', options_for_select(KataoNodePeer::getAllArray(), $filter_node, array('include_custom' => '&mdash;&nbsp;Noeud&nbsp;&mdash;'))) ?>
			<?php echo image_tag('separator02.gif', array('alt' => '')) ?>&nbsp;<?php echo select_tag('filter_period', options_for_select(KataoPeriodPeer::getAll(), $filter_period, array('include_custom' => '&mdash;&nbsp;Période&nbsp;&mdash;'))) ?>
			<?php echo submit_tag('', array('class' => 'btn-search', 'title' => 'Filtrer')) ?><div style="float:right"><a href="<?php echo url_for('@katao_order_filter_reset') ?>">[annuler]</a></div>
		</form>
	</div>
	<div class="filter-right">&nbsp;</div>
</div>

<?php if (empty($katao_orderList)): ?>
	<p>Aucune commande.</p>
<?php else : ?>
  <?php use_helper('Javascript') ?>

  <?php echo link_to('Marquer les commandes sélectionnées (' . multiple_action_info_selected('katao_order') . ') comme ajustées', '@katao_order_adjust_multiple', array('class' => 'adjust', 'confirm' => 'Êtes-vous sûr de vouloir marquer les commandes sélectionnées comme ajustées ?')) ?>
  <?php echo link_to('Générer les factures adhérents', '@katao_order_generate_invoices', array('class' => 'invoice')) ?>
  <div style="padding-top:4px">
  	<span class="pdf">Exporter les commandes : </span><?php echo link_to('toutes', '@katao_order_export_pdf_all') ?> | <?php echo link_to('sélectionnées (' . multiple_action_info_selected('katao_order') . ')', '@katao_order_export_pdf_selected') ?>
  	<?php echo link_to('Supprimer les commandes sélectionnées (' . multiple_action_info_selected('katao_order') . ')', '@katao_order_delete_multiple', array('class' => 'delete', 'confirm' => 'Êtes-vous sûr de vouloir supprimer les commandes sélectionnées ?')) ?>
  </div>
  <table class="admin-list">
    <tr>
	  <th nowrap="nowrap" width="1%"><?php echo multiple_action_checkbox_all('katao_order') ?></th>
	  <th nowrap="nowrap">N&deg; <?php echo sort_links($sf_request, 'no', 'kataoOrder/index') ?></th>
	  <th nowrap="nowrap">Date <?php echo sort_links($sf_request, 'date', 'kataoOrder/index') ?></th>
	  <th nowrap="nowrap">Etat <?php echo sort_links($sf_request, 'status', 'kataoOrder/index') ?></th>
	  <th nowrap="nowrap">Fournisseur <?php echo sort_links($sf_request, 'supplier', 'kataoOrder/index') ?></th>
	  <th nowrap="nowrap">Montant / Min.</th>
	  <th nowrap="nowrap">Noeud <?php echo sort_links($sf_request, 'node', 'kataoOrder/index') ?></th>
	  <th nowrap="nowrap">Période <?php echo sort_links($sf_request, 'period', 'kataoOrder/index') ?></th>
	  <th nowrap="nowrap" width="1%">Actions</th>
	</tr>
	<?php foreach ($katao_orderList as/*(KataoOrder)*/ $katao_order): ?>

	<?php
		$criteria = new Criteria();
		$criteria->addAscendingOrderByColumn(KataoOrderProductPeer::PRODUCT_NAME);
		$katao_order_products = $katao_order->getKataoOrderProductsJoinKataoProduct($criteria);

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

        $quantity_adjusted = $katao_order_product->getQuantity();
        $quantity_received = $katao_order_product->getHasBeenReceived()?$katao_order_product->getQuantityReceived():CONST_N_A;
        $quantity_delivered = $katao_order_product->getHasBeenDelivered()?$katao_order_product->getQuantityDelivered():CONST_N_A;

        $quantity = $quantity_delivered;
        if (CONST_N_A === $quantity) {
            $quantity = $quantity_received;
        }
        if (CONST_N_A === $quantity) {
            $quantity = $quantity_adjusted;
        }

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

        $katao_order_products_data[] = array('id' => $katao_order_product->getId(),
            'product_name' => $katao_order_product->getProductName(),
            'quantity_adjusted' => $quantity_adjusted,
            'quantity_received' => $quantity_received,
            'quantity_delivered' => $quantity_delivered,
            '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" class="line">
		  <td class="center"><?php echo multiple_action_checkbox_line('katao_order', $katao_order->getId()) ?></td>
		  <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 nowrap="nowrap"><?php echo $katao_order->getStatusStr() ?></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 ?>
			<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>
		  <td nowrap="nowrap">
	      <?php if (KataoOrder::STATUS_GENERATED == $katao_order->getStatus()): ?>
		    <span id="order-link-adjust-<?php echo $katao_order->getId() ?>" style="display: <?php echo $is_adjustable?'inline':'none' ?>"><?php echo catalyz_link_to_image_tag('@katao_order_adjust?id=' . $katao_order->getId(), 'actions/adjust.png', array('title' => 'Marquer comme ajustée', 'confirm' => 'Êtes-vous sûr de vouloir marquer cette commande comme ajustée ?'), array('alt' => 'Marquer comme ajustée')) ?></span>
		    <?php if (!$sf_user->isJustDelegate()): ?>
		    <?php echo catalyz_link_to_image_tag('@katao_order_force_adjustment?id=' . $katao_order->getId(), 'actions/force.png', array('title' => 'Forcer l\'ajustement', 'confirm' => 'Êtes-vous sûr de vouloir forcer l\'ajustement de cette commande ?\nATTENTION : il se peut que le colisage des produits ne soit pas complet\net que le montant minimum de la commande ne soit pas atteint.'), array('alt' => 'Forcer l\'ajustement')) ?>
		    <?php endif ?>
		  <?php else: ?>
		    <?php echo catalyz_link_to_image_tag('@katao_order_pdf?id=' . $katao_order->getId(), 'actions/pdf.png', array('title' => 'Exporter en PDF'), array('alt' => 'Exporter en PDF')) ?>
		  <?php endif ?>
		  <?php if (KataoOrder::STATUS_ADJUSTED == $katao_order->getStatus()): ?>
		    <?php echo catalyz_link_to_image_tag('@katao_order_send?id=' . $katao_order->getId(), 'actions/send.png', array('title' => 'Envoyer au fournisseur', 'confirm' => 'Êtes-vous sûr de vouloir envoyer la commande au fournisseur ?'), array('alt' => 'Envoyer au fournisseur')) ?>
		  <?php elseif (KataoOrder::STATUS_SENT == $katao_order->getStatus()): ?>
		  	<?php echo catalyz_link_to_image_tag('@katao_order_receive?id=' . $katao_order->getId(), 'actions/receive.png', array('title' => 'Marquer comme réceptionnée'), array('alt' => 'Marquer comme réceptionnée')) ?>
		  <?php elseif (KataoOrder::STATUS_RECEIVED == $katao_order->getStatus() && $katao_order->checkIfAllProductsHaveBeenDelivered()): ?>
		  	<?php echo catalyz_link_to_image_tag('@katao_order_deliver?id=' . $katao_order->getId(), 'actions/deliver.png', array('title' => 'Marquer comme délivrée', 'confirm' => 'Êtes-vous sûr de vouloir marquer cette commande comme délivrée ?'), array('alt' => 'Marquer comme délivrée')) ?>
		  <?php endif ?>
			<?php echo catalyz_link_to_image_tag('@katao_order_delete?id=' . $katao_order->getId(), 'actions/delete.png', array('title' => 'Supprimer', 'confirm' => 'Êtes-vous sûr de vouloir supprimer cette commande ?'), array('alt' => 'Supprimer')) ?>
	  </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%"><?php echo image_tag('actions/adjust.png', array('alt' => 'Quantité commandée', 'title' => 'Quantité commandée')) ?></th>
			<th nowrap="nowrap" width="1%"><?php echo image_tag('actions/receive.png', array('alt' => 'Quantité réceptionnée', 'title' => 'Quantité réceptionnée')) ?></th>
			<th nowrap="nowrap" width="1%"><?php echo image_tag('actions/deliver.png', array('alt' => 'Quantité délivrée', 'title' => 'Quantité délivrée')) ?></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">
	  		<td>
	  			<?php echo $katao_order_product_infos['product_name'] ?>
	  			<?php echo catalyz_link_to_image_tag('@katao_order_product_adjust?id=' . $katao_order_product_infos['id'], 'actions/adjust.png', array('title' => 'Ajuster'), array('alt' => 'Ajuster', 'align' => 'top')) ?>
			<?php if (!empty($katao_order_product_infos['last_incomplete_package'])): ?>
				<?php echo javascript_tag('$("order-status-red-' . $katao_order->getId() . '").style.display = "inline"; $("order-status-green-' . $katao_order->getId() . '").style.display = "none"; $("order-link-adjust-' . $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_adjusted'] ?></td>
	  		<td nowrap="nowrap" style="text-align:right!important"><?php echo $katao_order_product_infos['quantity_received'] ?></td>
	  		<td nowrap="nowrap" style="text-align:right!important"><?php echo $katao_order_product_infos['quantity_delivered'] ?></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">
			<?php if (KataoOrder::STATUS_SENT == $katao_order->getStatus()): ?>
	  			<?php echo link_to('Ajouter un nouveau produit à cette commande', '@katao_order_receive_new_product?id=' . $katao_order->getId(), array('class' => 'add')) ?>
		  	<?php endif ?>
			</td>
			<th colspan="4" nowrap="nowrap" style="text-align:right!important">MONTANT 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 ?>
  </table>
  <?php echo pager($pager, sprintf('kataoOrder/index?sort=%s%s', $sf_request->getParameter('sort', 'supplier'), $sf_request->hasParameter('desc')?'&desc=on':''), 'katao_order') ?>
<?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') ?>';
	}
}

/* ]]> */
</script>