<div class="content-add01">
	<div class="heading01"><h1><?php echo $katao_supplier_invoice->isNew()?'Ajout':'Modification' ?> d'une facture fournisseur</h1></div>
</div>

<?php echo form_tag('kataoSupplierInvoice/update?id=' . $katao_supplier_invoice->getId()) ?>

<table>
<tr valign="top">
	<th align="left"><?php echo label_for('period', 'Période') ?></th>
	<td><?php echo select_tag('period', options_for_select(KataoPeriodPeer::getAll(), $katao_supplier_invoice->getKataoPeriodId()?$katao_supplier_invoice->getKataoPeriodId():KataoPeriodPeer::getActivePeriod()->getId()), array('onchange' => 'updateOrdersList();')) ?></td>
</tr>
<tr valign="top">
	<th align="left"><?php echo label_for('supplier', 'Fournisseur') ?></th>
	<td><?php echo select_tag('supplier', options_for_select(KataoSupplierPeer::getAllSimple(), $katao_supplier_invoice->getKataoSupplierId(), array('include_custom' => '&mdash;&nbsp;choisir&nbsp;&mdash;')), array('onchange' => 'updateOrdersList();')) ?></td>
</tr>
<tr valign="top">
	<th align="left"><?php echo label_for('reference', 'Référence') ?></th>
	<td><?php echo input_tag('reference', $katao_supplier_invoice->getReference()) ?></td>
</tr>
<tr valign="top">
	<th align="left"><?php echo label_for('invoice_date', 'Date') ?></th>
	<td><?php echo input_date_tag('invoice_date', !$katao_supplier_invoice->isNew()?$katao_supplier_invoice->getInvoiceDate():time(), array('rich' => true, 'calendar_button_img' => '/sf/sf_admin/images/date.png', 'css' => 'skins/aqua/theme', 'format' => 'dd/MM/yyyy', 'culture' => 'fr', 'style' => 'width: 56px')) ?></td>
</tr>
<tr valign="top">
	<th align="left"><?php echo label_for('due_date', 'Echéance') ?></th>
	<td><?php echo input_date_tag('due_date', !$katao_supplier_invoice->isNew()?$katao_supplier_invoice->getDueDate():time(), array('rich' => true, 'calendar_button_img' => '/sf/sf_admin/images/date.png', 'css' => 'skins/aqua/theme', 'format' => 'dd/MM/yyyy', 'culture' => 'fr', 'style' => 'width: 56px')) ?></td>
</tr>
<tr valign="top">
	<th align="left"><label>Commande(s)</label></th>
	<td id="orders"><i>Merci de choisir une période et un fournisseur</i></td>
</tr>
<tr valign="top">
	<th align="left"><label>Observations</label></th>
	<td><?php echo textarea_tag('comment', $katao_supplier_invoice->getComment(), array('rows' => 4, 'cols' => 5, 'style' => 'width:525px;height:80px')) ?></td>
</tr>
</table>

<div id="products"><br /><i>Merci de sélectionner des commandes</i></div>

<br /><input type="submit" value="Sauver" onclick="return submitForm();" /> ou <a href="<?php echo url_for('kataoSupplierInvoice/index') ?>">annuler</a>

</form>

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

function updateOrdersList(){
	if ($F('period') && $F('supplier')) {
		new Ajax.Request('<?php echo url_for('kataoSupplierInvoice/updateOrdersList') ?>', {
		 	method: 'get',
			parameters: {
				id: <?php echo (int)$katao_supplier_invoice->getId() ?>,
				period: $F('period'),
				supplier: $F('supplier')
			},
			onSuccess: updateOrdersListResponse
		});
	}
}

function updateOrdersListResponse(xhr){
	$('orders').innerHTML = xhr.responseText;
	updateProductsList();
}

function updateProductsList(){
	var orders = new Array();
	var supplier_invoice_orders = $$('.supplier-invoice-order');
	for(var i = 0; i < supplier_invoice_orders.length; i++){
		if (supplier_invoice_orders[i].checked) {
			orders.push(supplier_invoice_orders[i].value);
		}
	}

	new Ajax.Request('<?php echo url_for('kataoSupplierInvoice/updateProductsList') ?>', {
	 	method: 'get',
		parameters: {
			id: <?php echo (int)$katao_supplier_invoice->getId() ?>,
			orders: orders.join(',')
		},
		onSuccess: updateProductsListResponse
	});
}

function updateProductsListResponse(xhr){
	$('products').innerHTML = xhr.responseText;
}

function updateProductTotalPrice(id){
	$('product_price_total_'+id).value = round2decimals(parseFloat(parseInt($F('product_quantity_'+id)) * parseFloat($F('product_price_unit_'+id))));
	updateTotalPrices();
}

function updateTotalPricesHT(){
<?php $tva_rates = array('0.196' => 1, '0.055' => 2, '0' => 0) ?>
<?php $total_by_tva_rate = array('0.196' => 0, '0.055' => 0, '0' => 0) ?>
<?php $discount_by_tva_rate = $total_by_tva_rate ?>
<?php foreach ($total_by_tva_rate as $tva_rate => $tva_rate_total): ?>
	<?php $tva_rate_discount = $discount_by_tva_rate[$tva_rate] ?>
	$('without_taxes_by_tva_rate_<?php echo $tva_rates[(string)$tva_rate] ?>').value = 0;
	var total_price_by_tva_rate = $$('.product-price-total-<?php echo $tva_rates[(string)$tva_rate] ?>');
	for(var i = 0; i < total_price_by_tva_rate.length; i++){
		$('without_taxes_by_tva_rate_<?php echo $tva_rates[(string)$tva_rate] ?>').value = round2decimals(parseFloat($F('without_taxes_by_tva_rate_<?php echo $tva_rates[(string)$tva_rate] ?>')) + parseFloat(total_price_by_tva_rate[i].value));
	}
<?php endforeach ?>

	updateTotalHT();
}

function updateTotalPricesTTC(){
<?php $tva_rates = array('0.196' => 1, '0.055' => 2, '0' => 0) ?>
<?php $total_by_tva_rate = array('0.196' => 0, '0.055' => 0, '0' => 0) ?>
<?php $discount_by_tva_rate = $total_by_tva_rate ?>
	var without_taxes_total = 0;
	var tva_total = 0;
<?php foreach ($total_by_tva_rate as $tva_rate => $tva_rate_total): ?>
	<?php $tva_rate_discount = $discount_by_tva_rate[$tva_rate] ?>
	$('total_by_tva_rate_<?php echo $tva_rates[(string)$tva_rate] ?>').value = round2decimals(parseFloat(<?php echo $tva_rate ?> * ($F('without_taxes_by_tva_rate_<?php echo $tva_rates[(string)$tva_rate] ?>') - $F('discount_by_tva_rate_<?php echo $tva_rates[(string)$tva_rate] ?>'))));
	without_taxes_total += parseFloat($F('without_taxes_by_tva_rate_<?php echo $tva_rates[(string)$tva_rate] ?>'));
	tva_total += parseFloat($F('total_by_tva_rate_<?php echo $tva_rates[(string)$tva_rate] ?>'));
<?php endforeach ?>

	without_taxes_total = without_taxes_total + parseFloat($F('fees_shipping_ht')) + parseFloat($F('fees_billing_ht'));
	tva_total = tva_total + parseFloat($F('fees_shipping_tva')) + parseFloat($F('fees_billing_tva'));

	$('without_taxes_total').value = without_taxes_total;
	$('tva_total').value = tva_total;

	updateTotalTTC();
}

function updateFeesTVA(){
	$('fees_shipping_tva').value = round2decimals(parseFloat($F('fees_shipping_ht')) * parseFloat($F('fees_shipping_rate')));
	$('fees_billing_tva').value = round2decimals(parseFloat($F('fees_billing_ht')) * parseFloat($F('fees_billing_rate')));

	updateTotalHT();
}

function updateTotalHT(){
<?php $tva_rates = array('0.196' => 1, '0.055' => 2, '0' => 0) ?>
<?php $total_by_tva_rate = array('0.196' => 0, '0.055' => 0, '0' => 0) ?>
<?php $discount_by_tva_rate = $total_by_tva_rate ?>
	var without_taxes_total = 0;
<?php foreach ($total_by_tva_rate as $tva_rate => $tva_rate_total): ?>
	<?php $tva_rate_discount = $discount_by_tva_rate[$tva_rate] ?>
	without_taxes_total += parseFloat($F('without_taxes_by_tva_rate_<?php echo $tva_rates[(string)$tva_rate] ?>'));
<?php endforeach ?>

	$('without_taxes_total').value = round2decimals(without_taxes_total + parseFloat($F('fees_shipping_ht')) + parseFloat($F('fees_billing_ht')));

	updateTotalTVA();
}

function updateTotalTVA(){
<?php $tva_rates = array('0.196' => 1, '0.055' => 2, '0' => 0) ?>
<?php $total_by_tva_rate = array('0.196' => 0, '0.055' => 0, '0' => 0) ?>
<?php $discount_by_tva_rate = $total_by_tva_rate ?>
	var tva_total = 0;
<?php foreach ($total_by_tva_rate as $tva_rate => $tva_rate_total): ?>
	<?php $tva_rate_discount = $discount_by_tva_rate[$tva_rate] ?>
	tva_total += parseFloat($F('total_by_tva_rate_<?php echo $tva_rates[(string)$tva_rate] ?>'));
<?php endforeach ?>

	$('tva_total').value = round2decimals(tva_total + parseFloat($F('fees_shipping_tva')) + parseFloat($F('fees_billing_tva')));

	updateTotalTTC();
}

function updateTotalTTC(){
	$('ttc_total').innerHTML = round2decimals(parseFloat($F('without_taxes_total')) + parseFloat($F('tva_total')));
}

function updateTotalPrices(){
	updateTotalPricesHT();
	updateTotalPricesTTC();
}

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

function submitForm(){
	if (!$F('supplier')) {
		alert('Merci de choisir un fournisseur')
		return false;
	}

	if ('' == $F('reference')) {
		alert('Merci de saisir la référence de la facture fournisseur')
		return false;
	}

	var supplier_invoice_orders_checked = 0;
	var supplier_invoice_orders = $$('.supplier-invoice-order');
	for(var i = 0; i < supplier_invoice_orders.length; i++){
		if (supplier_invoice_orders[i].checked) {
			supplier_invoice_orders_checked++;
		}
	}
	if (0 == supplier_invoice_orders_checked) {
		alert('Merci de sélectionner au moins une commande')
		return false;
	}

	return true;
}

updateOrdersList();

/* ]]> */
</script>