<?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="expense-line-grid"
		  title="Expense lines"
		  model="com.axelor.apps.hr.db.ExpenseLine" orderBy="-expenseDate"
		  onNew="action-expense-line-default, action-kilometric-allowance-record-default">
		<field name="project" requiredIf="toInvoice" canView="false" onSelect="action-expense-line-attrs-domain-project" widget="SuggestBox" width="150" onChange="action-expense-line-attrs-project-on-change"/>
		<field name="expenseProduct" canView="false" onSelect="action-expense-line-attrs-set-product-domain" widget="SuggestBox" form-view="product-expense-form" grid-view="product-expense-grid" width="200"/>
		<field name="expenseDate" onChange="action-expense-line-validate-expenseDate" width="120"/>
		<field name="user" hidden="true" form-view="user-form" grid-view="user-grid" onSelect="action-expense-line-attrs-company"/>
		<field name="totalAmount" aggregate="sum" onChange="action-expense-on-change-tax-untaxt-amount" width="170"/>
		<field name="totalTax" aggregate="sum" readonlyIf="expenseProduct.blockExpenseTax" width="80"
			onChange="action-expense-on-change-tax-untaxt-amount, action-expense-line-method-compute-analytic-distribution"/>
        <field name="toInvoice"/>
		<field name="comments" width="400"/>
		<field name="untaxedAmount" hidden="true"/>
		<field name="expenseProduct.blockExpenseTax" hidden="true"/>
	</grid>
	
	<grid name="expense-line-kilometric-grid" title="Kilometric Allowances" model="com.axelor.apps.hr.db.ExpenseLine" orderBy="-expenseDate"
		  onNew="action-expense-line-default, action-expense-line-method-fill-kilometric-expense-product">
  		<field name="project" requiredIf="toInvoice" canView="false" onSelect="action-expense-line-attrs-domain-project" widget="SuggestBox" grid-view="project-grid" width="120"/>
        <field name="expenseDate" title="Date" width="95"
        	onChange="action-kilometric-allowance-method-compute, action-kilometric-allowance-method-update-kap"/>
		<field name="kilometricAllowParam" canEdit="false" required="true" width="60"
			onChange="action-kilometric-allowance-method-compute" onSelect="action-kilometric-allowance-method-domain-on-select-on-kap"/>
        <field name="kilometricTypeSelect" required="true" width="100"/>
        <field name="distance" required="true" onChange="action-kilometric-allowance-method-compute" width="150"/>
        <field name="fromCity" required="true" onChange="action-kilometric-allowance-method-compute-distance" width="150"/>
        <field name="toCity" required="true" onChange="action-kilometric-allowance-method-compute-distance" width="150"/>
	    <field name="totalAmount" title="Total A.T.I" width="85" readonly="true"
	    	onChange="action-expense-on-change-untaxt-amount, action-expense-line-method-compute-analytic-distribution" />
   	    <field name="comments" width="250"/>
   	    <field name="user" hidden="true" form-view="user-form" grid-view="user-grid" onSelect="action-expense-line-attrs-company" canNew="false" canEdit="false" canView="false"/>
  		<field name="untaxedAmount" hidden="true" />
   		<field name="expenseProduct" hidden="true"/>
	</grid>

    <form name="expense-line-project-form" title="Expense line" model="com.axelor.apps.hr.db.ExpenseLine" readonlyIf="true" canEdit="false">
        <panel-include view="expense-line-form"/>
    </form>

	<form name="expense-line-form" title="Expense line" model="com.axelor.apps.hr.db.ExpenseLine"
		onNew="action-expense-line-default"
		onSave="action-expense-line-validate-expenseDate" width="large">
		
		<panel name="mainPanel" title="Main">
			<field name="user" hidden="true" showIf="expense.multipleUsers" form-view="user-form" grid-view="user-grid" onSelect="action-expense-line-attrs-company" canNew="false" canEdit="false" canView="false"/>
			<field name="project" requiredIf="toInvoice" edit-icon="false" canEdit="false" onSelect="action-expense-line-attrs-domain-project" widget="SuggestBox" form-view="project-form" grid-view="project-grid"/>
			<field name="expenseProduct" edit-icon="false" canEdit="false" onSelect="action-expense-line-attrs-set-product-domain" widget="SuggestBox" form-view="product-expense-form" grid-view="product-expense-grid"/>
			<field name="expenseDate" onChange="action-expense-line-validate-expenseDate"/>
			<field name="totalAmount" onChange="action-expense-on-change-untaxt-amount, action-expense-line-method-compute-analytic-distribution"/>
			<field name="totalTax" onChange="action-expense-on-change-tax-untaxt-amount, action-expense-line-method-compute-analytic-distribution" readonlyIf="expenseProduct.blockExpenseTax == true"/>
			<field name="untaxedAmount" />
            <field name="toInvoice" colSpan="3"/>
            <field name="invoiced" colSpan="3" showIf="toInvoice &amp;&amp; id &amp;&amp; $readonly()" showTitle="false">
                <viewer>
                    <![CDATA[
                        <h4>
                            <span class="label label-success" style="margin: 5px 0 !important; display: inline-table; line-height: initial;" ng-show="record.invoiced" x-translate>Invoiced</span>
                            <span class="label label-important" style="margin: 5px 0 !important; display: inline-table; line-height: initial;" ng-show="!record.invoiced" x-translate>Not invoiced</span>
                        </h4>
                    ]]>
                </viewer>
			</field>
			<field name="justificationMetaFile" widget="binary-link"/>
			<field name="comments" colSpan="12"/>
			<field name="expense" hidden="true"/>
    		<field name="expense.companyCbSelect" hidden="true" />
    		<field name="expense.multipleUsers" hidden="true"/>
		</panel>
				
		<panel name="analyticDistributionPanel" title="Analytic distribution" if="__config__.app.isApp('account') &amp;&amp; __config__.app.getApp('account').getManageAnalyticAccounting()" onTabSelect="action-expense-line-attrs-read-only-distribution-lines">
			<field name="analyticDistributionTemplate" grid-view="analytic-distribution-template-grid" form-view="analytic-distribution-template-form" canEdit="false" onChange="action-expense-line-method-create-distribution"/>
			<panel-related name="analyticMoveLineListPanel" field="analyticMoveLineList" grid-view="analytic-move-line-distribution-grid" form-view="analytic-move-line-distribution-form" colSpan="12"/>
		</panel>

		<panel name="attrsPanel">
			<field name="attrs" colSpan="12" />
		</panel>

	</form>
	
	<form name="expense-line-kilometric-form" title="Kilometric Allowance" model="com.axelor.apps.hr.db.ExpenseLine"
		onNew="action-expense-line-default,action-expense-line-method-fill-kilometric-expense-product"
		onSave="action-expense-line-validate-expenseDate" width="large">

			<panel name="kilometricAllowancePanel" title="Kilometric Allowance">
				<field name="project" requiredIf="toInvoice" edit-icon="false" canEdit="false" onSelect="action-expense-line-attrs-domain-project" widget="SuggestBox" form-view="project-form" grid-view="project-grid"/>
		        <field name="expenseDate"/>
				<field name="kilometricAllowParam" onSelect="action-kilometric-allowance-method-domain-on-select-on-kap" onChange="action-kilometric-allowance-method-compute"/>
		        <field name="kilometricTypeSelect" required="true"/>
		        <field name="distance" onChange="action-kilometric-allowance-method-compute" required="true"/>
		        <panel name="cityDetailsPanel" colSpan="12">
			        <field name="fromCity" required="true" onChange="action-kilometric-allowance-method-compute-distance"/>
					<field name="toCity" required="true" onChange="action-kilometric-allowance-method-compute-distance"/>
		        </panel>
		    	<field name="totalAmount" onChange="action-expense-on-change-untaxt-amount, action-expense-line-method-compute-analytic-distribution" readonly="true"/>
	    		<field name="comments" colSpan="12"/>
				<field name="user"  hidden="true" form-view="user-form" grid-view="user-grid"
					   onSelect="action-expense-line-attrs-company" canNew="false" canEdit="false" canView="false"/>
				<field name="expenseProduct" hidden="true"/>
			</panel>
		
		<panel name="analyticDistributionPanel" title="Analytic distribution" if="__config__.app.isApp('account') &amp;&amp; __config__.app.getApp('account').getManageAnalyticAccounting()" onTabSelect="action-expense-line-attrs-read-only-distribution-lines">
			<field name="analyticDistributionTemplate" grid-view="analytic-distribution-template-grid" form-view="analytic-distribution-template-form" canEdit="false" onChange="action-expense-line-method-create-distribution"/>
			<panel-related name="analyticMoveLineListPanel" field="analyticMoveLineList" grid-view="analytic-move-line-distribution-grid" form-view="analytic-move-line-distribution-form" colSpan="12"/>
		</panel>
	</form>
	
	<action-method name="action-expense-line-method-create-distribution">
		<call class="com.axelor.apps.hr.web.expense.ExpenseController" method="createAnalyticDistributionWithTemplate"/>
	</action-method>
	
	<action-method name="action-expense-line-method-compute-analytic-distribution">
		<call class="com.axelor.apps.hr.web.expense.ExpenseController" method="computeAnalyticDistribution"/>
	</action-method>

	<action-method name="action-kilometric-allowance-method-domain-on-select-on-kap">
		<call class="com.axelor.apps.hr.web.expense.ExpenseController" method="domainOnSelectOnKAP"/>
	</action-method>

	<action-method name="action-kilometric-allowance-method-update-kap">
		<call class="com.axelor.apps.hr.web.expense.ExpenseController" method="updateKAPOfKilometricAllowance"/>
	</action-method>
	
	<action-method name="action-expense-line-method-fill-kilometric-expense-product">
		<call class="com.axelor.apps.hr.web.expense.ExpenseController" method="fillKilometricExpenseProduct"/>
	</action-method>

    <action-method name="action-kilometric-allowance-method-compute">
    	<call class="com.axelor.apps.hr.web.expense.ExpenseController" method="computeKilometricExpense"/>
    </action-method>

	<action-method name="action-kilometric-allowance-method-compute-distance">
		<call class="com.axelor.apps.hr.web.expense.ExpenseController" method="computeDistanceAndKilometricExpense"/>
	</action-method>

	<action-attrs name="action-expense-line-attrs-read-only-distribution-lines">
		<attribute name="readonly" for="analyticMoveLineList" expr="eval: __config__.app.getApp('account').getAnalyticDistributionTypeSelect() != 1"/>
	</action-attrs>

	<action-attrs name="action-expense-line-attrs-domain-project">
		<attribute name="domain" for="project" expr="eval: &quot; ${__user__.id} MEMBER OF self.membersUserSet OR self.assignedTo.id = ${__user__.id} &quot;"/>
	</action-attrs>

	<action-record name="action-expense-line-default" model="com.axelor.apps.hr.db.ExpenseLine">
		<field name="user" expr="eval: _parent?.user"/>
	</action-record>
	
	<action-record name="action-expense-on-change-tax-untaxt-amount" model="com.axelor.apps.hr.db.ExpenseLine">
	    <field name="untaxedAmount" expr="eval:totalAmount-totalTax" />
	</action-record>
	
	<action-record name="action-expense-on-change-untaxt-amount" model="com.axelor.apps.hr.db.ExpenseLine">
		<field name="untaxedAmount" expr="eval: totalAmount-totalTax"/>
	</action-record>
	
	<action-validate name="action-expense-line-validate-expenseDate" model="com.axelor.apps.hr.db.ExpenseLine">
		<error message="Date can't be in the future" if="expenseDate?.isAfter(__date__)" action="action-expense-line-record-expenseDate-invalide"/>
	</action-validate>
	
	<action-record name="action-expense-line-record-expenseDate-invalide" model="com.axelor.apps.hr.db.ExpenseLine">
		<field name="expenseDate" expr="eval :__date__"/>
	</action-record>

	<action-attrs name="action-expense-line-attrs-company" model="com.axelor.apps.hr.db.ExpenseLine">
		<attribute name="domain" for="user" expr="eval: &quot; ${expense.company.id} MEMBER OF self.companySet &quot;" />
	</action-attrs>

	<action-attrs name="action-expense-line-attrs-set-product-domain">
		<attribute name="domain" for="expenseProduct" expr="eval: &quot;self.id IN (0)&quot;"/>
		<attribute name="domain" for="expenseProduct" expr="eval: &quot;self.expense = true&quot;"  if="__user__.employee?.hrManager &amp;&amp; !__parent__.multipleUsers &amp;&amp; __parent__.companyCbSelect != 1"/>
		<attribute name="domain" for="expenseProduct" expr="eval: &quot;self.expense = true AND self.unavailableToUsers = false&quot;"  if="!__user__.employee?.hrManager &amp;&amp; !__parent__.multipleUsers &amp;&amp; __parent__.companyCbSelect != 1"/>
		<attribute name="domain" for="expenseProduct" expr="eval: &quot;self.expense = true AND self.personalExpense = false&quot;" if="__user__.employee?.hrManager &amp;&amp; (__parent__.companyCbSelect == 1 || __parent__.multipleUsers)"/>
		<attribute name="domain" for="expenseProduct" expr="eval: &quot;self.expense = true AND self.unavailableToUsers = false AND self.personalExpense = false&quot;"  if="!__user__.employee?.hrManager &amp;&amp; (__parent__.companyCbSelect == 1 || __parent__.multipleUsers)"/>
	</action-attrs>

	<action-attrs name="action-expense-line-attrs-project-on-change">
		<attribute name="value" for="toInvoice" expr="eval: project?.isInvoicingExpenses"/>
	</action-attrs>
</object-views>
