<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<object-views xmlns="http://axelor.com/xml/ns/object-views"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://axelor.com/xml/ns/object-views http://axelor.com/xml/ns/object-views/object-views_5.2.xsd">
    
	<grid name="timetable-grid" title="Timetables" model="com.axelor.apps.supplychain.db.Timetable" orderBy="estimatedDate" editable="true">
		<field name="estimatedDate" readonlyIf="invoiced" required="true"/>
		<field name="title" readonlyIf="invoiced"/>
		<field name="amount" readonlyIf="invoiced" required="true" onChange="action-timetable-group-on-amount-change"/>
		<field name="percentage" readonlyIf="invoiced" required="true" onChange="action-timetable-record-on-percentage-change"/>
		<field name="comments" readonlyIf="invoiced" width="300%"/>
		<field name="invoiced" readonly="true"/>
		<button name="generateInvoiceBtn" readonlyIf="!id || invoiced" help="Generate Invoice" icon="fa-credit-card" onClick="save, action-timetable-method-generate-invoice" if="__config__.app.isApp('invoice') &amp;&amp; __config__.app.getApp('supplychain').getAllowTimetableInvoicing()"/>
		<field name="invoice" width="120" hidden="true"/>
	</grid>

	<form name="timetable-form" title="Timetable" model="com.axelor.apps.supplychain.db.Timetable">
		<panel name="mainPanel" readonlyIf="invoiced">
			<field name="estimatedDate" colSpan="4"/>
			<field name="$viewerInvoiced" showTitle="false" readonly="true" showIf="invoiced == true" colSpan="8">
				<viewer><![CDATA[
				<h4>
					<span class="label label-info pull-right" x-translate>Invoiced</span>
				</h4>
				]]></viewer>
			</field>
			<field name="title" colSpan="12"/>
			<field name="amount" colSpan="4" required="true" onChange="action-timetable-group-on-amount-change"/>
			<field name="percentage" colSpan="4" required="true" onChange="action-timetable-record-on-percentage-change"/>
			<button name="generateInvoiceBtn" colOffset="9" colSpan="3" title="Generate Invoice" onClick="save, action-timetable-method-generate-invoice" if="__config__.app.isApp('invoice') &amp;&amp; __config__.app.getApp('supplychain').getAllowTimetableInvoicing()" showIf="invoice == null"/>
			<field name="invoice" hideIf="!invoice"/>
			<field name="comments" colSpan="12"/>
		</panel>
	</form>

	<!-- Partial invoicing for purchase doesn't exist yet, so here's a temporary second grid and form view that just remove the invoice button -->
	<grid name="timetable-grid-purchase" title="Timetables" model="com.axelor.apps.supplychain.db.Timetable" orderBy="estimatedDate" editable="true">
		<field name="estimatedDate" readonlyIf="invoiced" required="true"/>
		<field name="title" readonlyIf="invoiced"/>
		<field name="amount" readonlyIf="invoiced" required="true" onChange="action-timetable-group-on-amount-change"/>
		<field name="percentage" readonlyIf="invoiced" required="true" onChange="action-timetable-record-on-percentage-change"/>
		<field name="comments" readonlyIf="invoiced" width="300%"/>
		<field name="invoiced" readonly="true"/>
		<button name="generateInvoiceBtn" readonlyIf="!id || invoiced" help="Generate Invoice" icon="fa-credit-card" onClick="save, action-timetable-method-generate-invoice, action-timetable-attrs-readonly" if="__config__.app.isApp('invoice') &amp;&amp; __config__.app.getApp('supplychain').getAllowTimetableInvoicing()"/>
		<field name="invoice" width="120" hidden="true"/>
	</grid>

	<form name="timetable-form-purchase" title="Timetable" model="com.axelor.apps.supplychain.db.Timetable">
		<panel name="mainPanel" readonlyIf="invoiced">
			<field name="estimatedDate" colSpan="4"/>
			<field name="$viewerInvoiced" showTitle="false" readonly="true" showIf="invoiced == true" colSpan="8">
				<viewer><![CDATA[
				<h4>
					<span class="label label-info pull-right" x-translate>Invoiced</span>
				</h4>
				]]></viewer>
			</field>
			<field name="title" colSpan="12"/>
			<field name="amount" colSpan="4" required="true" onChange="action-timetable-group-on-amount-change"/>
			<field name="percentage" colSpan="4" required="true" onChange="action-timetable-record-on-percentage-change"/>
			<field name="invoiced" hideIf="!invoice"/>
			<field name="comments" colSpan="12"/>
		</panel>
	</form>

	<action-group name="action-timetable-group-on-amount-change">
		<action name="action-timetable-condition-on-amount-change" if="__parent__ != null &amp;&amp; (__parent__._model == 'com.axelor.apps.sale.db.SaleOrder' || __parent__._model == 'com.axelor.apps.purchase.db.PurchaseOrder')"/>
		<action name="action-timetable-record-on-amount-change" if="__parent__ != null &amp;&amp; (__parent__._model == 'com.axelor.apps.sale.db.SaleOrder' || __parent__._model == 'com.axelor.apps.purchase.db.PurchaseOrder')"/>
	</action-group>

	<action-condition name="action-timetable-condition-on-amount-change">
		<check field="amount"
			error="Amount cannot be superior to the order's total excluding taxes"
			if="amount &gt; __parent__.exTaxTotal || __parent__.exTaxTotal == null"/>
	</action-condition>

	<action-record name="action-timetable-record-on-amount-change" model="com.axelor.apps.supplychain.db.Timetable">
		<field name="percentage"
			expr="eval: (__parent__.exTaxTotal != null &amp;&amp; __parent__.exTaxTotal &gt; 0) ? amount * 100 / __parent__.exTaxTotal : 0"/>
	</action-record>

	<action-record name="action-timetable-record-on-percentage-change" model="com.axelor.apps.supplychain.db.Timetable">
		<field name="amount"
			expr="eval: (__parent__.exTaxTotal != null) ? (__parent__.exTaxTotal * percentage / 100).setScale(__config__.app.getApp('base').nbDecimalDigitForUnitPrice, java.math.RoundingMode.HALF_UP) : 0"
			if="__parent__ != null &amp;&amp; (__parent__._model == 'com.axelor.apps.sale.db.SaleOrder' || __parent__._model == 'com.axelor.apps.purchase.db.PurchaseOrder')"/>
	</action-record>

	<action-method name="action-timetable-method-generate-invoice">
		<call class="com.axelor.apps.supplychain.web.TimetableController" method="generateInvoice"/>
	</action-method>

</object-views>