<?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="bank-reconciliation-grid" title="Bank reconciliations" model="com.axelor.apps.bankpayment.db.BankReconciliation" orderBy="-fromDate,-toDate,company.code,bankDetails.iban">
		<field name="name"/>
		<field name="fromDate"/>
		<field name="toDate"/>
		<field name="company" if="__config__.app.getApp('base').getEnableMultiCompany()"/>
		<field name="bankDetails" form-view="bank-details-form" grid-view="bank-details-grid"/>
		<field name="currency" canEdit="false" form-view="currency-form" grid-view="currency-grid"/>
		<field name="journal" form-view="journal-form" grid-view="journal-grid"/>
		<field name="startingBalance" />
		<field name="endingBalance" />
    <field name="statusSelect"/> 
  </grid>
  
	<form name="bank-reconciliation-form" title="Bank reconciliation" model="com.axelor.apps.bankpayment.db.BankReconciliation" width="large" 
	onNew="action-bank-reconciliation-onnew-group" >
        <toolbar>
          <button title="Print the bank reconciliation" name="printBtn" onClick="save,action-bank-reconciliation-method-print-bank-reconciliation"/>
        </toolbar>
		<panel name="mainPanel" colSpan="12">
			<panel name="detailsPanel" readonlyIf="statusSelect == 2" colSpan="12">
				<field name="statusSelect" showTitle="false" widget="NavSelect" colSpan="8"/>
				<field name="validatedByUser" colSpan="2"/>
				<field name="validatedDate" colSpan="2"/>
				<field name="name"/>
				<field name="bankStatement" domain="self.statusSelect = 2"/>
				<field name="company" canEdit="false" widget="SuggestBox" onChange="action-bank-reconciliation-set-currency" form-view="company-form" grid-view="company-grid" required="true"/>
				<field name="currency" canEdit="false" form-view="currency-form" grid-view="currency-grid" required="true"/>
				<field name="bankDetails" canEdit="false" form-view="bank-details-form" grid-view="bank-details-grid" onSelect="action-bank-reconciliation-method-set-bank-details-domain" required="true"/>
				<field name="includeOtherBankStatements"/>
				<field name="fromDate"/>
				<field name="toDate"/>
				<field name="journal" canEdit="false" domain="self.statusSelect = 1 AND self.company = :company" form-view="journal-form" grid-view="journal-grid"/>
				<field name="cashAccount" canEdit="false" domain="self.company = :company AND self.accountType.technicalTypeSelect ='cash'" form-view="account-form" grid-view="account-grid"/>
			</panel>
			<panel colSpan="12" name="actionsPanel">
				<button name="loadBankStatementBtn" title="Load bank statements" onClick="save,action-bank-reconciliation-method-load-bank-statement" showIf="bankStatement &amp;&amp; !bankReconciliationLineList.length > 0" colSpan="3"/>
				<button name="loadOtherBankStatementBtn" title="Load other bank statements" onClick="save,action-bank-reconciliation-method-load-other-bank-statement" readonlyIf="statusSelect == 2" showIf="bankStatement &amp;&amp; bankReconciliationLineList.length > 0 &amp;&amp; !includeOtherBankStatements" colSpan="3"/>
				<button name="validateBtn" title="Validate" hideIf="statusSelect == 2" onClick="save,action-group-bankreconciliation-validate-click" colSpan="3"/>
				<button name="showReconciledMoveLineBtn" title="Show reconciled move lines" showIf="statusSelect == 2" onClick="action-bank-reconciliation-view-show-reconciled-move-lines" colSpan="3"/>
			</panel>
		</panel>
		<panel name="bankReconciliationPanel" title="Bank reconciliation" colSpan="12" readonlyIf="statusSelect == 2">
			<panel name="balancePanel" title="Balance" colSpan="12">
				<field name="totalPaid"/>
				<field name="totalCashed"/>
				<separator title="Bank statements"/>
				<separator title="Account"/>
				<field name="startingBalance" colSpan="3" readonlyIf="bankStatement"/>
				<field name="endingBalance" colSpan="3" readonlyIf="bankStatement"/>
				<field name="accountBalance" colSpan="3" readonlyIf="bankStatement"/>
				<field name="computedBalance" colSpan="3"/>
				<label name="balanceEqualLabel" colSpan="12" css="label-danger" title="Computed balance and Ending Balance should be equal" showIf="computedBalance = endingBalance"/>
				<button name="computeBtn" title="Compute" hideIf="statusSelect == 2" onClick="action-group-bankreconciliation-compute-click" colSpan="3"/>
				<button name="multipleReconcileBtn" title="Multiple reconciles" onClick="save,action-bank-reconciliation-view-multiple-reconcile-wizard" colSpan="3"/>
			</panel>
			<panel-related name="bankReconciliationLineListPanel" editable="true" field="bankReconciliationLineList" colSpan="12" grid-view="bank-reconciliation-line-grid" form-view="bank-reconciliation-line-form" x-can-copy="true" onChange="action-group-bankreconciliation-compute-click"/>
			<panel-dashlet name="showUnreconciledMoveLinesPanel" action="action-bank-reconciliation-view-show-unreconciled-move-lines" colSpan="12"/>
		</panel>
	</form>
	
	<form name="bank-reconciliation-multiple-reconcile-wizard-form" title="Multiple reconcile" model="com.axelor.apps.bankpayment.db.BankReconciliation" 
	onNew="action-bank-reconciliation-group-multiple-reconcile-wizard-new" >
		<panel name="mainPanel" colSpan="12">
			<panel name="bankStatementDetailPanel" title="Bank statement details" colSpan="12">
				<field name="$bankStatementDate" title="Date" type="date" readonly="true"/>
				<panel>
					<field name="$bankStatementDebit" title="Debit" type="decimal" readonly="true" showIf="$bankStatementDebit &gt; 0"/>
					<field name="$bankStatementCredit" title="Credit" type="decimal" readonly="true" showIf="$bankStatementCredit &gt; 0"/>
				</panel>
				<field name="$bankStatementName" title="Name" type="String" readonly="true"/>
				<field name="$bankStatementReference" title="Reference" type="String" readonly="true"/>
				<field name="$bankStatementAmountRemainToReconcile" title="Amount remaining to reconcile" type="decimal" readonly="true"/>
				
				<field name="_selectedBankReconciliationLine" hidden="true"/>
			</panel>
			<panel-related name="$toReconcileMoveLineSetPanel" field="$toReconcileMoveLineSet" title="Select move lines to reconciles" colSpan="12" type="many-to-many" target="com.axelor.apps.account.db.MoveLine" 
			onSelect="action-bank-reconciliation-attrs-move-line-domain-on-wizard" canNew="false" canEdit="false" form-view="move-line-form" grid-view="move-line-grid"
			onChange="action-bank-reconciliation-attrs-multiple-reconcile-wizard-compute-total-of-selected-move-lines"/>
			<field name="$selectedMoveLineTotal" title="Total of selected move lines" type="decimal" readonly="true"/>
			<button name="validateBtn" title="Validate" hideIf="statusSelect == 2" onClick="action-bank-reconciliation-method-validate-multiple-reconciles" showIf="$selectedMoveLineTotal &gt; 0"/>
		</panel>
	</form>

 
   <action-group name="action-bank-reconciliation-onnew-group">
	   <action name="action-bank-reconciliation-active-company"/>
	   <action name="action-bank-reconciliation-set-months"/>
	   <action name="action-bank-reconciliation-set-currency"/>
   </action-group>
 
   <action-group name="action-group-bankreconciliation-validate-click">
   		<action name="save"/>
   		<action name="action-bank-reconciliation-method-compute"/>
   		<action name="action-bank-reconciliation-check-incomplete-bankreconciliation-line"/>
   		<action name="action-bank-reconciliation-method-validate"/>
   </action-group>	
   
   <action-group name="action-group-bankreconciliation-compute-click">
   		<action name="save"/>
   		<action name="action-bank-reconciliation-method-compute"/>
   </action-group>
   	
   <action-record model="com.axelor.apps.bankpayment.db.BankReconciliation" name="action-bank-reconciliation-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-record model="com.axelor.apps.bankpayment.db.BankReconciliation" name="action-bank-reconciliation-set-months">
       <field name="fromDate" expr="eval: LocalDate.parse(todayDate).withDayOfMonth(1)"/>
       <field name="toDate" expr="eval: LocalDate.parse(todayDate).with(java.time.temporal.TemporalAdjusters.lastDayOfMonth())"/>
   </action-record>
     
   <action-record model="com.axelor.apps.bankpayment.db.BankReconciliation" name="action-bank-reconciliation-set-currency">
       <field name="currency" expr="eval: company?.currency"/>
   </action-record>
   
   <action-method name="action-bank-reconciliation-method-set-bank-details-domain">
     <call class="com.axelor.apps.bankpayment.web.BankReconciliationController" method="setBankDetailsDomain"/>
   </action-method>
   
   <action-method name="action-bank-reconciliation-method-load-bank-statement">
     <call class="com.axelor.apps.bankpayment.web.BankReconciliationController" method="loadBankStatement"/>
   </action-method>
   
   <action-method name="action-bank-reconciliation-method-load-other-bank-statement">
     <call class="com.axelor.apps.bankpayment.web.BankReconciliationController" method="loadOtherBankStatement"/>
   </action-method>
	
   <action-method name="action-bank-reconciliation-method-compute">
     <call class="com.axelor.apps.bankpayment.web.BankReconciliationController" method="compute"/>
   </action-method>
	
   <action-method name="action-bank-reconciliation-method-validate">
     <call class="com.axelor.apps.bankpayment.web.BankReconciliationController" method="validate"/>
   </action-method>
	
   <action-method name="action-bank-reconciliation-method-print-bank-reconciliation">
     <call class="com.axelor.apps.bankpayment.web.BankReconciliationController" method="printBankReconciliation"/>
   </action-method>
  
   <action-view name="action-bank-reconciliation-view-show-unreconciled-move-lines" title="Unreconciled move lines" model="com.axelor.apps.account.db.MoveLine">
     <view type="grid" name="move-line-bank-reconciliation-grid"/>
     <view type="form" name="move-line-form"/>
     <domain>self.move.journal.id = :journal and ((self.debit &gt; 0 and self.bankReconciledAmount &lt; self.debit) or (self.credit &gt; 0 and self.bankReconciledAmount &lt; self.credit)) and self.account.id = :cashAccount and self.move.statusSelect != 4</domain>
   </action-view>	 
   
   <action-view name="action-bank-reconciliation-view-show-reconciled-move-lines" title="Reconciled move lines" model="com.axelor.apps.account.db.MoveLine">
   	 <view type="grid" name="move-line-bank-reconciliation-grid"/>
     <view type="form" name="move-line-form"/>
     <domain>self.move.id in (select ML.move from BankReconciliationLine as BRL left join BRL.moveLine as ML where BRL.bankReconciliation.id = :bankReconciliationId)</domain>
     <context name="bankReconciliationId" expr="eval: id"/>
   </action-view>	
   
   <action-view name="action-bank-reconciliation-view-multiple-reconcile-wizard" title="Multiple reconciles"  model="com.axelor.apps.bankpayment.db.BankReconciliation">
   		<view type="form" name="bank-reconciliation-multiple-reconcile-wizard-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"/>
  		<view-param name="forceEdit" value="true"/>
	  	<context name="_bankReconciliation" expr="eval: __this__" />
  		<context name="_selectedBankReconciliationLineId" expr="eval: __this__.bankReconciliationLineList.find{it.isSelected()}?.id"/>
   </action-view>
   
   <action-attrs name="action-bank-reconciliation-attrs-move-line-domain-on-wizard">
		<attribute name="domain" for="$toReconcileMoveLineSet" 
		expr="eval: &quot;(self.bankReconciledAmount &lt; self.debit or self.bankReconciledAmount &lt; self.credit) 
		and ((self.debit &gt; 0 and ${bankStatementCredit} &gt; 0) or (self.credit &gt; 0 and ${bankStatementDebit} &gt; 0)) 
		and self.move.company.id = ${_bankReconciliation?.company?.id} AND self.account.id = ${_bankReconciliation?.cashAccount?.id} 
		AND self.move.journal.id = ${_bankReconciliation?.journal?.id}&quot;" />
	</action-attrs>
	
	<action-group name="action-bank-reconciliation-group-multiple-reconcile-wizard-new">
		<action name="action-bank-reconciliation-attrs-multiple-reconcile-wizard-selected-bank-reconciliation-line" if="_selectedBankReconciliationLineId"/>
		<action name="action-bank-reconciliation-attrs-multiple-reconcile-wizard-new" if="_selectedBankReconciliationLineId"/>
	</action-group>
	
	<action-attrs name="action-bank-reconciliation-attrs-multiple-reconcile-wizard-selected-bank-reconciliation-line" >
		<attribute name="value" for="_selectedBankReconciliationLine" expr="eval: __repo__(BankReconciliationLine).find(_selectedBankReconciliationLineId)"/>
	</action-attrs>
	
	<action-attrs name="action-bank-reconciliation-attrs-multiple-reconcile-wizard-new" >
		<attribute name="value" for="$bankStatementDate" expr="eval: _selectedBankReconciliationLine.effectDate"/>
		<attribute name="value" for="$bankStatementDebit" expr="eval: _selectedBankReconciliationLine.debit"/>
		<attribute name="value" for="$bankStatementCredit" expr="eval: _selectedBankReconciliationLine.credit"/>
		<attribute name="value" for="$bankStatementName" expr="eval: _selectedBankReconciliationLine.name"/>
		<attribute name="value" for="$bankStatementReference" expr="eval: _selectedBankReconciliationLine.reference"/>
		<attribute name="value" for="$bankStatementAmountRemainToReconcile" expr="eval: _selectedBankReconciliationLine.bankStatementLine.amountRemainToReconcile"/>
	</action-attrs>
	
	<action-attrs name="action-bank-reconciliation-attrs-multiple-reconcile-wizard-compute-total-of-selected-move-lines" >
		<attribute name="value" for="$selectedMoveLineTotal" expr="eval: toReconcileMoveLineSet.collect{new BigDecimal(it.debit).add(new BigDecimal(it.credit))}.sum()"/>
	</action-attrs>
	
	<action-method name="action-bank-reconciliation-method-validate-multiple-reconciles">
		<call class="com.axelor.apps.bankpayment.web.BankReconciliationController" method="validateMultipleReconcile"/>
	</action-method>
	
	<action-validate name="action-bank-reconciliation-check-incomplete-bankreconciliation-line">
   		<alert message="There is at least one incomplete bank reconciliation. Are you sure you want to continue ?" if="bankReconciliationLineList != null &amp;&amp; bankReconciliationLineList.find{it.moveLine == null} != null" />
   	</action-validate>
    
</object-views>