<?php $katao_product = $form->getObject() ?>
<div class="content-add01">
	<div class="heading01"><h1><?php echo $katao_product->isNew() ? 'Ajout' : 'Modification' ?> d'un produit</h1></div>
</div>

<form action="<?php echo url_for('kataoProduct/update' . (!$katao_product->isNew() ? '?id=' . $katao_product->getId() : '')) ?>" method="post" <?php $form->isMultipart() and print 'enctype="multipart/form-data" ' ?>>
  <table>
    <tfoot>
      <tr>
        <td colspan="2">
          <br /><input type="submit" value="Sauver" onclick="if(checkPrices()){computeMaxSolAmount();return true;}else{return false;}" /> ou <a href="<?php echo url_for('kataoProduct/index') ?>">annuler</a><?php if (!$katao_product->isNew()): ?> ou <a href="<?php echo url_for('@katao_product_edit_presentation?id=' . $katao_product->getId()) ?>">modifier sa présentation</a><?php endif ?>
        </td>
      </tr>
    </tfoot>
    <tbody>
      <?php echo $form->renderGlobalErrors() ?>
      <tr valign="top">
	  	<td colspan="2" align="left"><u>Produit</u></td>
	  </tr>
      <tr valign="top">
        <th align="left"><?php echo $form['name']->renderLabel() ?></th>
        <td>
          <?php echo $form['name'] ?>
		  <?php echo $form['name']->renderError() ?>
        </td>
      </tr>
	  <tr valign="top">
        <th align="left"><?php echo $form['reference']->renderLabel() ?></th>
        <td>
          <?php echo $form['reference'] ?>
		  <?php echo $form['reference']->renderError() ?>
        </td>
      </tr>
      <tr valign="top">
        <th align="left"><?php echo $form['katao_product_family_id']->renderLabel() ?></th>
        <td>
          <?php echo $form['katao_product_family_id'] ?>
		  <?php echo $form['katao_product_family_id']->renderError() ?>
        </td>
      </tr>
      <tr valign="top">
        <th align="left"><?php echo $form['picture']->renderLabel() ?></th>
        <td>
          <?php echo $form['picture'] ?>
		  <?php echo $form['picture']->renderError() ?>
        </td>
      </tr>
      <tr valign="top">
	  	<td colspan="2" align="left"><br /><u>Fournisseur</u></td>
	  </tr>
	<?php if (!$sf_user->isJustSupplier()): ?>
	  <tr valign="top">
        <th align="left"><?php echo $form['katao_supplier_id']->renderLabel() ?></th>
        <td>
          <?php echo $form['katao_supplier_id'] ?>
		  <?php echo $form['katao_supplier_id']->renderError() ?>
        </td>
      </tr>
	<?php endif ?>
      <tr valign="top">
        <th align="left"><?php echo $form['min_order_number']->renderLabel() ?></th>
        <td>
          <?php echo $form['min_order_number'] ?>
		  <?php echo $form['min_order_number']->renderError() ?>
        </td>
      </tr>
      <tr valign="top">
        <th align="left"><?php echo $form['delivery_delay']->renderLabel() ?></th>
        <td>
          <?php echo $form['delivery_delay'] ?> jours
		  <?php echo $form['delivery_delay']->renderError() ?>
        </td>
      </tr>
      <tr valign="top">
        <th align="left"><?php echo $form['is_archived']->renderLabel() ?></th>
        <td>
          <?php echo $form['is_archived'] ?>
		  <?php echo $form['is_archived']->renderError() ?>

        <?php echo $form['id'] ?>

    <?php if ($sf_user->isJustSupplier()): ?>
    	  <?php echo input_hidden_tag('katao_product[katao_supplier_id]', $sf_user->getInstance()->getKataoSupplierId()) ?>
    	  <?php echo input_hidden_tag('katao_product[margin]', $form['margin']->getValue()) ?>
    	  <?php echo input_hidden_tag('katao_product[tva_rate]', $form['tva_rate']->getValue()) ?>
    <?php endif ?>
        </td>
      </tr>
      <tr valign="top">
	  	<td colspan="2" align="left"><br /><u>Tarification</u></td>
	  </tr>
      <tr valign="top">
        <th align="left"><?php echo $form['unit_price_euro']->renderLabel() ?></th>
        <td>
          <?php echo $form['unit_price_euro'] ?> €
      <?php if (!$sf_user->isJustSupplier()): ?>
		  &nbsp;<a href="javascript://" onclick="computeUnitPriceWithTaxes();" title="Calculer le prix de vente TTC"><?php echo image_tag('actions/calculator.png', array('alt' => 'Calculer le prix de vente TTC', 'align' => 'top')) ?></a>
	  <?php endif ?>
		  <?php echo $form['unit_price_euro']->renderError() ?>
        </td>
      </tr>
  <?php if (!$sf_user->isJustSupplier()): ?>
      <tr valign="top">
        <th align="left"><?php echo $form['margin']->renderLabel() ?></th>
        <td>
          <?php echo $form['margin'] ?> %
      <?php if (!$sf_user->isJustSupplier()): ?>
		  &nbsp;<a href="javascript://" onclick="computeMargin();" title="Calculer la marge"><?php echo image_tag('actions/calculator.png', array('alt' => 'Calculer la marge', 'align' => 'top')) ?></a>
	  <?php endif ?>
		  <?php echo $form['margin']->renderError() ?>
        </td>
      </tr>
  <?php endif ?>
      <tr valign="top">
        <th align="left"><?php echo $form['tva_rate']->renderLabel() ?></th>
        <td>
          <?php echo $form['tva_rate'] ?>
		  <?php echo $form['tva_rate']->renderError() ?>
        </td>
      </tr>
  <?php if (!$sf_user->isJustSupplier()): ?>
      <tr valign="top">
        <th align="left"><label>Prix de vente TTC</label></th>
        <td>
          <?php echo input_tag('sell_price', '', array('style' => 'width: 54px')) ?> €
          &nbsp;<a href="javascript://" onclick="computeUnitPriceWithoutTaxes();" title="Calculer le prix d'achat HT"><?php echo image_tag('actions/calculator.png', array('alt' => 'Calculer le prix d\'achat HT', 'align' => 'top')) ?></a>
        </td>
      </tr>
  <?php endif ?>
      <tr valign="top">
	  	<td colspan="2" align="left"><br /><u>Monnaie complémentaire</u></td>
	  </tr>
  <?php if (!$sf_user->isJustSupplier()): ?>
      <tr valign="top">
        <th align="left"><label>Pourcentage</label></th>
        <td>
          <?php echo input_tag('sol_percent', '', array('style' => 'width: 54px')) ?> %
        </td>
      </tr>
  <?php endif ?>
	  <tr valign="top">
        <th align="left"><?php echo $form['max_sol_amount']->renderLabel() ?></th>
        <td>
          <?php echo $form['max_sol_amount'] ?> <?php echo wpConfig::getAdditionalCurrencyName() ?>
        <?php if (!$sf_user->isJustSupplier()): ?>
		  &nbsp;<a href="javascript://" onclick="computeMaxSolAmount();" title="Calculer le montant maxi de la monnaie complémentaire"><?php echo image_tag('actions/calculator.png', array('alt' => 'Calculer le montant maxi de la monnaie complémentaire', 'align' => 'top')) ?></a>
		<?php endif ?>
		  <?php echo $form['max_sol_amount']->renderError() ?>
        </td>
      </tr>
  <?php if (!$sf_user->isJustSupplier()): ?>
      <tr valign="top">
	  	<td colspan="2" align="left"><br /><u>Codes comptables</u></td>
	  </tr>
      <tr valign="top">
        <th align="left"><?php echo $form['accounting_code_purchase']->renderLabel() ?></th>
        <td>
          <?php echo $form['accounting_code_purchase'] ?>
		  <?php echo $form['accounting_code_purchase']->renderError() ?>
        </td>
      </tr>
      <tr valign="top">
        <th align="left"><?php echo $form['accounting_code_sell']->renderLabel() ?></th>
        <td>
          <?php echo $form['accounting_code_sell'] ?>
		  <?php echo $form['accounting_code_sell']->renderError() ?>
        </td>
      </tr>
      <tr><td colspan="2"><i>Laisser vide pour utiliser celui du rayon</i></td></tr>
  <?php endif ?>
    </tbody>
  </table>
</form>

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

function checkPrices(){
	var price_ht_displayed = parseFloat($F('katao_product_unit_price_euro'));
	var price_ht_calculated = round3decimals(parseFloat($F('sell_price')) / (1+parseFloat($F('katao_product_margin'))/100) / (1+parseFloat($F('katao_product_tva_rate'))));
	if (price_ht_displayed != price_ht_calculated) {
		alert('ATTENTION : Le prix d\'achat HT ('+price_ht_displayed+' €) ne correspond pas à celui calculé ('+price_ht_calculated+' €).\n\nMerci de bien vouloir le modifier ou le calculer en cliquant sur la calculette\nà côté du champ "Prix de vente TTC".\n');
		return false;
	}
	return true;
}

function computeUnitPriceWithTaxes(){
	$('sell_price').value = round2decimals(parseFloat($F('katao_product_unit_price_euro')) * (1+parseFloat($F('katao_product_margin'))/100) * (1+parseFloat($F('katao_product_tva_rate'))));
}

function computeUnitPriceWithoutTaxes(){
	$('katao_product_unit_price_euro').value = round3decimals(parseFloat($F('sell_price')) / (1+parseFloat($F('katao_product_margin'))/100) / (1+parseFloat($F('katao_product_tva_rate'))));
}

function computeMaxSolAmount(){
	if (!$F('sol_percent')) {
		$('sol_percent').value = 0;
	}
	if (!$F('sell_price')) {
		computeUnitPriceWithTaxes();
	}
	$('katao_product_max_sol_amount').value = Math.floor(10 * parseFloat($F('sell_price')) * parseFloat($F('sol_percent')) / 100);
}

function computeSolPercent(){
	$('sol_percent').value = Math.round(100 * parseFloat($F('katao_product_max_sol_amount')) / parseFloat($F('sell_price')) / 10);
}

function computeMargin(){
	$('katao_product_margin').value = Math.round(100 * parseFloat($F('sell_price')) / (parseFloat($F('katao_product_unit_price_euro')) * (parseFloat($F('katao_product_tva_rate')) + 1)) - 100);
}

function round2decimals(value){
	return Math.round(value*100)/100;
}

function round3decimals(value){
	return Math.round(value*1000)/1000;
}

var margin = parseFloat($F('katao_product_margin'));
if (1 >= margin) {
	margin = 100 * margin;
}
$('katao_product_margin').value = margin;
<?php if (!$sf_user->isJustSupplier()): ?>
<?php if ($katao_product->isNew() && !$sf_request->isMethod('post')): ?>
$('sell_price').value = '';
$('sol_percent').value = <?php echo wpConfig::getDefaultProductSolPercent() ?>
<?php else: ?>
computeUnitPriceWithTaxes();
computeSolPercent();
<?php endif ?>
<?php endif ?>

/* ]]> */
</script>