<?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="account-grid" title="Accounting.Accounts" model="com.axelor.apps.account.db.Account">
	  <field name="code" x-bind="{{code|unaccent|uppercase}}"/>
	  <field name="name"/>
    <field name="accountType" form-view="account-type-form" grid-view="account-type-grid"/> 
	  <field name="reconcileOk"/>
	  <field name="parentAccount" form-view="account-form" grid-view="account-grid"/>
	  <field name="company" form-view="company-form" grid-view="company-grid" if="__config__.app.getApp('base').getEnableMultiCompany()"/>
	</grid>
	
	<grid name="account-simple-grid" title="Accounting.Accounts" model="com.axelor.apps.account.db.Account">
        <field name="code" x-bind="{{code|unaccent|uppercase}}"/>
        <field name="name"/>
    </grid>
    
    <tree name="account-tree" title="Accounting account">
        <column name="name" type="string"/>
         <column name="code" type="string"/>
         <column name="company" type="string"/>
         
        <node model="com.axelor.apps.account.db.Account" domain="self.parentAccount is null">
            <field as="name" name="name"/>
             <field as="code" name="code"/>
             <field as="company" name="company.name"/>
        </node>
        
        <node model="com.axelor.apps.account.db.Account" parent="parentAccount" draggable="true" onClick="action-account-view-account-onchange-view" >
            <field as="name" name="name"/>
            <field as="code" name="code"/>
            <field as="company" name="company.name"/>
        </node>
    </tree>

	<form name="account-form" title="Accounting.Account" model="com.axelor.apps.account.db.Account" width="large"
		onNew="action-account-record-new" onLoad="action-account-method-compute-balance">
		<panel name="mainPanel">
			<panel name="accountDetailsPanel" colSpan="8">
				<field name="code" x-bind="{{code|unaccent|uppercase}}" colSpan="3" css="bold"/>
				<field name="name" colSpan="9" css="bold"/>
			</panel>
			<panel name="balancePanel" colSpan="4" showIf="id">
				<button name="$balanceBtn" title="Total balance" icon="fa-money" widget="info-button" onClick="action-account-view-show-move-lines" x-currency="company.currency.symbol"/>
			</panel>
			<panel name="settingsPanel" title="Settings" colSpan="12">
				<field name="accountType" form-view="account-type-form" grid-view="account-type-grid"/>
				<field name="company" canEdit="false" widget="SuggestBox" onChange="action-account-validate" readonlyIf="parentAccount != null" form-view="company-form" grid-view="company-grid"/>
				<field name="parentAccount" onChange="action-account-validate" domain="self.company = :company" form-view="account-form" grid-view="account-grid"/>
				<spacer name="parentAccountSpacer" colSpan="6"/>
				<field name="useForPartnerBalance" colSpan="3"/>
				<field name="reconcileOk" colSpan="3"/>
				<field name="compatibleAccountSet" widget="TagSelect" showIf="reconcileOk"/>
				<field name="analyticDistributionAuthorized" widget="boolean-switch" colSpan="4"/>
				<field name="analyticDistributionRequiredOnInvoiceLines" widget="boolean-switch" colSpan="4" showIf="analyticDistributionAuthorized"/>
				<field name="analyticDistributionRequiredOnMoveLines" widget="boolean-switch" colSpan="4" showIf="analyticDistributionAuthorized"/>
				<field name="isTaxAuthorizedOnMoveLine" widget="boolean-switch" colSpan="4"/>
				<field name="isTaxRequiredOnMoveLine" showIf="isTaxAuthorizedOnMoveLine" widget="boolean-switch" colSpan="4"/>
			</panel>
			<panel name="showMoveLinesPanel" colSpan="12" showIf="id">
				<button name="showMoveLineBtn" title="Show move lines" colSpan="3" onClick="action-account-view-show-move-lines"/>      
      		</panel>
		</panel>
		<panel-mail name="mailPanel">
			<mail-messages limit="4"/>
		</panel-mail>
	</form>

    <form name="customer-account-form" model="com.axelor.apps.account.db.Account" width="large" onNew="action-account-group-new-customer-account" title="New customer account">
        <panel-include view="account-form"/>
    </form>

    <form name="supplier-account-form" model="com.axelor.apps.account.db.Account" width="large" onNew="action-account-group-new-supplier-account" title="New supplier account">
        <panel-include view="account-form"/>
    </form>

    <form name="employee-account-form" model="com.axelor.apps.account.db.Account" width="large" onNew="action-account-group-new-employee-account" title="New employee account">
        <panel-include view="account-form"/>
    </form>

	<form name="account-info-form" title="Accounting.Account" model="com.axelor.apps.account.db.Account" width="large">
		<panel name="mainPanel" title="Account details" readonly="true">
			<field name="code" x-bind="{{code|unaccent|uppercase}}" colSpan="3" css="bold"/>
			<field name="name" colSpan="9" css="bold"/>
			<field name="accountType" form-view="account-type-form" grid-view="account-type-grid"/>
			<field name="company" canEdit="false"  widget="SuggestBox" onChange="action-account-validate" readonlyIf="parentAccount != null" form-view="company-form" grid-view="company-grid"/>
			<field name="parentAccount" onChange="action-account-validate" domain="self.company = :company" form-view="account-form" grid-view="account-grid"/>
			<spacer name="parentAccountSpacer" colSpan="6"/>
			<field name="useForPartnerBalance" colSpan="3"/>
			<field name="reconcileOk" colSpan="3"/>
			<field name="compatibleAccountSet" widget="TagSelect" showIf="reconcileOk"/>
		</panel>
	</form>
	
	<action-view name="action-account-view-account-onchange-view" title="Financial Accounts"
		model="com.axelor.apps.account.db.Account" >
		<view type="form" name="account-info-form" />
		<view-param name="popup" value="reload"/>
        <view-param name="show-toolbar" value="false"/>
  		<view-param name="show-confirm" value="false" />
 	  	<view-param name="popup-save" value="false"/>
    	<context name="_showRecord" expr="eval: id"/>
	</action-view>
	
    <action-group name="action-account-group-new-customer-account">
        <action  name="action-account-record-new-from-partner"/>
        <action  name="action-account-record-new-customer-account"/>
    </action-group>

    <action-group name="action-account-group-new-supplier-account">
        <action  name="action-account-record-new-from-partner"/>
        <action  name="action-account-record-new-supplier-account"/>
    </action-group>

    <action-group name="action-account-group-new-employee-account">
        <action  name="action-account-record-new-from-partner"/>
        <action  name="action-account-record-new-employee-account"/>
    </action-group>
    
    <action-record name="action-account-record-new" model="com.axelor.apps.account.db.Account">
    	<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-validate name="action-account-validate">
     	<error message="The parent is associated to another company. Please make sure to select the right parent in the company chart before changing the company." if="parentAccount != null &amp;&amp; company != null &amp;&amp; parentAccount?.company?.id != company?.id"/>
    </action-validate>
    
    <!-- action-record shared among customer/supplier/employee account -->
    <action-record name="action-account-record-new-from-partner" model="com.axelor.apps.account.db.Account">
        <field name="name" expr="eval:__parent__.partner.fullName"/>
        <field name="company" expr="eval: __parent__.company"/>
        <field name="useForPartnerBalance" expr="eval: true"/>
        <field name="reconcileOk" expr="eval: true"/>
        <field name="compatibleAccountSet" expr="eval: new java.util.HashSet()"/>
    </action-record>

    <action-record name="action-account-record-new-customer-account" model="com.axelor.apps.account.db.Account">
        <field name="parentAccount" expr="eval:__repo__(AccountConfig).all().filter('self.company = ?', __parent__.company).fetchOne().customerAccount"/>
        <field name="accountType" expr="eval:__repo__(AccountConfig).all().filter('self.company = ?', __parent__.company).fetchOne().customerAccount?.accountType"/>
    </action-record>

    <action-record name="action-account-record-new-supplier-account" model="com.axelor.apps.account.db.Account">
        <field name="parentAccount" expr="eval:__repo__(AccountConfig).all().filter('self.company = ?', __parent__.company).fetchOne().supplierAccount"/>
        <field name="accountType" expr="eval:__repo__(AccountConfig).all().filter('self.company = ?', __parent__.company).fetchOne().supplierAccount?.accountType"/>
    </action-record>

    <action-record name="action-account-record-new-employee-account" model="com.axelor.apps.account.db.Account">
        <field name="parentAccount" expr="eval:__repo__(AccountConfig).all().filter('self.company = ?', __parent__.company).fetchOne().employeeAccount"/>
        <field name="accountType" expr="eval:__repo__(AccountConfig).all().filter('self.company = ?', __parent__.company).fetchOne().employeeAccount?.accountType"/>
    </action-record>
    
    <action-method name="action-account-method-compute-balance">
    	<call class="com.axelor.apps.account.web.AccountController" method="computeBalance"/>
    </action-method>
    
</object-views>