<?xml version="1.0" encoding="UTF-8" ?>
<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="move-template-grid" title="Move template" model="com.axelor.apps.account.db.MoveTemplate">
		<field name="name"/>
		<field name="code" x-bind="{{code|unaccent|uppercase}}" />
		<field name="moveTemplateType" grid-view="move-template-type-grid" form-view="move-template-type-form"/>
		<field name="journal" grid-view="journal-grid" form-view="journal-form"/>
	    <field name="endOfValidityDate"/>
		<field name="isValid"/>
    </grid>
   	
    <form name="move-template-form" title="Move template" model="com.axelor.apps.account.db.MoveTemplate" width="large" onNew="action-move-template-set-default" >
  		<panel name="mainPanel" >
		    <field name="name"/>
		    <field name="code" x-bind="{{code|unaccent|uppercase}}" required="true" colSpan="3"/>
		    <field name="company" canEdit="false" readonlyIf="journal" colSpan="3" canNew="false" form-view="company-form" grid-view="company-grid"/>
		    <field name="journal" canEdit="false" readonlyIf="moveTemplateLineList.length > 0" onSelect="action-move-template-set-journal-domain" grid-view="journal-grid" form-view="journal-form" required="true"/>
   		    <field name="moveTemplateType" canEdit="false" readonlyIf="moveTemplateLineList.length > 0" required="true" grid-view="move-template-type-grid" form-view="move-template-type-form"/>
   		    <field name="moveTemplateType.typeSelect"/>
   		    <field name="endOfValidityDate"/>
	 	</panel>
    	<panel-related name="moveTemplateLineListPanel" field="moveTemplateLineList" colSpan="12" form-view="move-template-line-in-amount-form" grid-view="move-template-line-in-amount-grid" onChange="action-move-template-method-set-is-valid" showIf="moveTemplateType.typeSelect == 2" hidden="true"/>
    	<panel-related name="moveTemplateLineListPanel" field="moveTemplateLineList" colSpan="12" form-view="move-template-line-in-percentage-form" grid-view="move-template-line-in-percentage-grid" onChange="action-move-template-method-set-is-valid" showIf="moveTemplateType.typeSelect == 1" hidden="true"/>
     	<panel sidebar="true" name="actionsPanel" title="Actions">
    		<field name="isValid" readonlyIf="!isValid" onChange="action-move-template-method-set-is-valid" colSpan="12"/>
    		<button name="checkValidityBtn" title="Check validity" readonlyIf="isValid" onClick="save,action-move-template-method-check-validity" colSpan="12"/>
 	 	</panel>
	</form>
    
    <form name="move-template-wizard-form" title="Generate move from template" model="com.axelor.apps.base.db.Wizard" width="large" 
    onNew="action-move-template-wizard-set-active-company" onLoad="action-move-template-wizard-set-active-company" >
        <panel name="mainPanel" >
        	<field name="moveTemplateType" canEdit="false" title="Type" target-name="name" type="many-to-one" target="com.axelor.apps.account.db.MoveTemplateType" grid-view="move-template-type-grid" form-view="move-template-type-form"/>
        	<field name="company"  type="many-to-one" target="com.axelor.apps.base.db.Company" canEdit="false" title="Company" form-view="company-form" grid-view="company-grid" onChange="action-move-template-wizard-attrs-company-onchange"/>
   	 		<field name="moveDate" type="date" title="Move date" requiredIf="moveTemplateType.typeSelect == 2" showIf="moveTemplateType.typeSelect == 2"/>
	        <field name="moveTemplate" canEdit="false" requiredIf="moveTemplateType.typeSelect == 1"  target-name="name" type="many-to-one" target="com.axelor.apps.account.db.MoveTemplate" showIf="moveTemplateType.typeSelect == 1" title="Template" 
	        domain="self.moveTemplateType = :moveTemplateType AND self.isValid = true AND self.journal.company = :company AND self.journal.statusSelect = 1 AND self.endOfValidityDate &gt;= CURRENT_DATE" form-view="move-template-form" grid-view="move-template-grid"/>
	        <panel-related name="dataInputListPanel" field="dataInputList"  type="one-to-many" showIf="moveTemplateType.typeSelect == 1" target="com.axelor.apps.base.db.Wizard" title="Move Lines data input" 
	        form-view="wizard-custom-in-percentage-form" grid-view="wizard-custom-in-percentage-grid" colSpan="12" />
   	        <panel-related name="moveTemplateSetPanel" field="moveTemplateSet"  type="many-to-many" showIf="moveTemplateType.typeSelect == 2" target="com.axelor.apps.account.db.MoveTemplate" title="Move templates" 
   	        form-view="move-template-form" grid-view="move-template-grid" colSpan="12" domain="self.moveTemplateType = :moveTemplateType AND self.isValid = true AND self.journal.company = :company AND self.journal.statusSelect = 1 AND self.endOfValidityDate &gt;= :moveDate"/>
        	<field name="moveTemplateType.typeSelect" hidden="true"/>
        </panel>
    	<panel sidebar="true" name="actionsPanel" title="Actions">
    		<button name="generateMovesBtn" readonlyIf="(dataInputList == null || dataInputList.isEmpty()) &amp;&amp; (moveTemplateSet == null || moveTemplateSet.isEmpty()) || (moveTemplateType == null)" title="Generate Moves" onClick="save,action-move-template-wizard-method-generate-move" colSpan="12" />
    	</panel>
    </form>
    
    <grid name="wizard-custom-in-percentage-grid" title="MoveLines content data input" model="com.axelor.apps.base.db.Wizard">
   		<field name="date" type="date" required="true" title="Date" onChange="action-move-template-wizard-check-value"/>
   		<field name="moveBalance" type="decimal" required="true" title="MoveLine Amount" onChange="action-move-template-wizard-check-value" />
   		<field name="debitPartner" target-name="fullName" title="Partner to Debit" type="many-to-one" onSelect="action-account-attrs-move-template-partner-domain" target="com.axelor.apps.base.db.Partner"/>
 		<field name="creditPartner" target-name="fullName" title="Partner to Credit" type="many-to-one" onSelect="action-account-attrs-move-template-partner-domain" target="com.axelor.apps.base.db.Partner"/>
   	</grid>
   	
	<form name="wizard-custom-in-percentage-form" title="MoveLine content data input" model="com.axelor.apps.base.db.Wizard" 
	onLoad="action-move-template-wizard-line-set-move-template" onNew="action-move-template-wizard-line-set-move-template">
		<panel name="mainPanel" >
	 		<field name="date" type="date" title="Date" required="true" onChange="action-move-template-wizard-check-value"/>
	 		<field name="moveBalance" type="decimal" precision="20" scale="2"  min="0" required="true" title="MoveLine Amount" onChange="action-move-template-wizard-check-value"  />
	 		<field name="debitPartner" target-name="fullName" title="Partner to Debit" onSelect="action-account-attrs-move-template-partner-domain" type="many-to-one" readonlyIf="partnerInputSelect == 0 || partnerInputSelect == 2" target="com.axelor.apps.base.db.Partner"/>
			<field name="creditPartner" target-name="fullName"  title="Partner to Credit" onSelect="action-account-attrs-move-template-partner-domain" type="many-to-one" readonlyIf="partnerInputSelect == 0 || partnerInputSelect == 1" target="com.axelor.apps.base.db.Partner"/>
			<field name="moveTemplate" type="many-to-one" target="com.axelor.apps.account.db.MoveTemplate" hidden="true"/>
			<field name="partnerInputSelect" type="integer" hidden="true" />
		</panel>
	</form>
	
    <action-validate name="action-move-template-wizard-check-value">
    	<error if="moveBalance != null &amp;&amp; (moveBalance as BigDecimal) &lt; 0" message="The Move Balance must be positive" action="action-move-template-wizard-reset-value" />
    </action-validate>
    
    <action-record name="action-move-template-set-default" model="com.axelor.apps.account.db.MoveTemplate">
		<field name="company" expr="eval:__repo__(Company).find(__user__.activeCompany.id)" if="__user__.activeCompany != null"/>        
    </action-record>
    
    <action-record model="com.axelor.apps.base.db.Wizard" name="action-move-template-wizard-reset-value">
    	<field if="date != null &amp;&amp; LocalDate.parse(date).isAfter(__date__)" name="date" expr="eval:null"/>
    	<field name="moveBalance" expr="eval:0" if="moveBalance != null &amp;&amp; (moveBalance as BigDecimal) &lt; 0" />
    </action-record>
    
    <action-record model="com.axelor.apps.base.db.Wizard" name="action-move-template-wizard-set-active-company">
    	<field name="company"  expr="eval:__user__.activeCompany" if="__user__.activeCompany != null"/>
    	<field name="company"  expr="eval:__repo__(Company).all().fetchOne()" if="__user__.activeCompany == null &amp;&amp; __repo__(Company).all().fetch().size == 1"/>
    </action-record>
    
    <action-attrs name="action-move-template-wizard-attrs-company-onchange">
    	<attribute name="value" for="moveTemplateSet" expr="eval: null" />
    	<attribute name="value" for="dataInputList" expr="eval: null" />
    	<attribute name="value" for="moveTemplate" expr="eval: null" />
    </action-attrs>

    <action-method name="action-move-template-method-check-validity">
    	<call method="checkValidity" class="com.axelor.apps.account.web.MoveTemplateController" />
    </action-method>
    
    <action-method name="action-move-template-wizard-method-generate-move">
    	<call method="generateMove" class="com.axelor.apps.account.web.MoveTemplateController" />
    </action-method>
    
    <action-method name="action-move-template-method-set-is-valid">
        <call method="setIsValid" class="com.axelor.apps.account.web.MoveTemplateController" />
    </action-method>
    
    <action-attrs name="action-move-template-set-journal-domain">
    	<attribute name="domain" for="journal" expr="eval:&quot;self.statusSelect = 1 AND self.company.id = ${company?.id} &quot;" />
    </action-attrs>
    
    
    <action-attrs name="action-account-attrs-move-template-partner-domain">
    	<attribute name="domain" for="debitPartner,creditPartner" expr="eval: &quot;self.isContact = false AND ${_parent?.company?.id} member of self.companySet&quot;"/>
    </action-attrs>
    
	<action-record name="action-move-template-wizard-line-set-move-template" model="com.axelor.apps.base.db.Wizard">
	    <field name="date" expr="eval:__config__.app.getTodayDate()" if="date == null"/>
		<field name="moveTemplate" expr="eval:__repo__(MoveTemplate).find(_parent.moveTemplate.id)" />
		<field name="partnerInputSelect" expr="eval:__repo__(MoveTemplate).find(_parent.moveTemplate.id).partnerInputSelect"/>
	</action-record>
	
    
    
</object-views>