<?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="payment-schedule-grid" title="Payment schedule"
		model="com.axelor.apps.account.db.PaymentSchedule" orderBy="-paymentScheduleSeq">
		<field name="paymentScheduleSeq" width="120"/>
		<field name="creationDate" />
		<field name="partner" form-view="partner-form" grid-view="partner-grid" />
		<field name="company" form-view="company-form" grid-view="company-grid" if="__config__.app.getApp('base').getEnableMultiCompany()"/>
    <field name="typeSelect"/>
		<field name="inTaxAmount" aggregate="sum"/>
		<field name="nbrTerm" />
		<field name="bankDetails" form-view="bank-details-form" grid-view="bank-details-grid"/>
		<field name="paymentMode" grid-view="payment-mode-grid" form-view="payment-mode-form" />
		<field name="statusSelect" />
	</grid>

	<form name="payment-schedule-form" title="Payment schedule"
		model="com.axelor.apps.account.db.PaymentSchedule" onNew="action-account-payment-schedule-onnew-group"
		onLoad="action-payment-schedule-attrs-collapse-irrecoverable-group"
		onSave="action-payment-schedule-payment-schedule-id">
		
		<panel name="mainPanel">
		    <field name="statusSelect" widget="NavSelect" colSpan="12" />
		    <field name="paymentScheduleSeq" />
			<field name="typeSelect" readonlyIf="statusSelect != 1" />
			<field name="creationDate" readonlyIf="statusSelect != 1" />
			<field name="startDate" readonlyIf="statusSelect != 1" required="true" />
			<panel colSpan="12" name="companyPanel">
				<field name="company" canEdit="false" readonlyIf="statusSelect != 1" widget="SuggestBox" form-view="company-form" grid-view="company-grid" />
				<field name="companyBankDetails" form-view="bank-details-form" grid-view="bank-details-grid" onSelect="action-payment-schedule-attrs-company-bank-details-domain" if="__config__.app.getApp('base').getManageMultiBanks()" />
			</panel>
			<panel colSpan="12" name="partnerDetailPanel">
				<field name="partner" form-view="partner-form" grid-view="partner-grid" readonlyIf="statusSelect != 1" onChange="action-payment-schedule-group-onchange-partner" domain="self.isContact = false AND :company member of self.companySet" />
				<field name="bankDetails" form-view="bank-details-form" grid-view="bank-details-grid" onSelect="action-payment-schedule-attrs-bank-details-domain" onChange="action-payment-schedule-method-fill-company-bank-details" />
			</panel>
      <field name="paymentMode" grid-view="payment-mode-grid" form-view="payment-mode-form" readonlyIf="statusSelect != 1" onChange="action-payment-schedule-method-fill-company-bank-details"/>
			<field name="currency" canEdit="false" readonlyIf="statusSelect != 1" form-view="currency-form" grid-view="currency-grid"/>
			<field name="inTaxAmount" readonlyIf="statusSelect != 1 || typeSelect == 1" />
            <field name="nbrTerm" readonlyIf="statusSelect != 1" />
		</panel>
		
		<panel-related name="invoiceSetPanel" title="Invoices" field="invoiceSet" grid-view="invoice-account-grid" form-view="invoice-form" 
			domain="self.partner = :partner and self.schedulePaymentOk = 'f' and self.company=:company and self.statusSelect = 3 and self.companyInTaxTotalRemaining > 0.0 AND self.currency = :currency AND self.operationTypeSelect = 3"
			readonlyIf="statusSelect != 1" onChange="action-payment-schedule-record-in-tax-amount" hideIf="typeSelect == 2" canNew="false" />
			
		<panel-related name="paymentScheduleLineListPanel" title="Payment schedule lines" field="paymentScheduleLineList" editable="true" grid-view="payment-schedule-line-payment-schedule-grid" form-view="payment-schedule-line-payment-schedule-form"
			readonlyIf="statusSelect != 1" />

		<panel name="actionsPanel" sidebar="true">
			<button name="validateBtn" title="Validate" onClick="action-group-account-payment-schedule-validate-click" hideIf="!paymentScheduleLineList || paymentScheduleLineList.length == 0 || statusSelect != 1" colSpan="12" />
			<button name="cancelBtn" title="Cancel" onClick="save,action-payment-schedule-method-cancel,save" hideIf="statusSelect == 4" colSpan="12" />
			<button name="createPaymentScheduleLinesBtn" title="Create schedule lines" onClick="action-group-account-payment-schedule-create-click" showIf="statusSelect == 1" colSpan="12" />
		</panel>
		<panel name="irrecoverablePanel" title="Irrecoverable" sidebar="true">
			<field name="irrecoverableStatusSelect" />
			<field name="managementObject" form-view="management-object-form" grid-view="management-object-grid" />
			<button name="passInIrrecoverableBtn" title="Shift into irrecoverable" onClick="com.axelor.apps.account.web.PaymentScheduleController:passInIrrecoverable" colSpan="6" />
			<button name="notPassInIrrecoverableBtn" title="Do not shift into irrecoverable" onClick="com.axelor.apps.account.web.PaymentScheduleController:notPassInIrrecoverable" colSpan="6" />
		</panel>

	</form>

	<!-- ACTION GROUPs -->
	<action-group name="action-account-payment-schedule-onnew-group">
		<action name="default-payment-schedule-record" />
		<action name="action-payment-schedule-attrs-collapse-irrecoverable-group" />
	</action-group>

	<action-group name="action-group-account-payment-schedule-create-click">
		<action name="save" />
		<action name="action-payment-schedule-create-payment-schedule-lines" />
		<action name="save" />
	</action-group>

	<action-group name="action-group-account-payment-schedule-validate-click">
		<action name="save" />
		<action name="action-payment-schedule-payment-schedule-id" />
        <action name="save" />
		<action name="action-payment-schedule-method-validate" />
		<action name="save" />
	</action-group>

  <action-group name="action-payment-schedule-group-onchange-partner">
    <action name="action-payment-schedule-record-partner" />
    <action name="action-payment-schedule-method-fill-partner-bank-details" />
    <action name="action-payment-schedule-method-fill-company-bank-details" />
  </action-group>

	<!-- ACTION RECORD -->
	<action-record name="default-payment-schedule-record"
		model="com.axelor.apps.account.db.PaymentSchedule">
		<field name="creationDate" expr="eval:__date__" />
		<field name="startDate" expr="eval:__date__" />
		<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"/>
		<field name="statusSelect" expr="eval: 1" />
	</action-record>

	<action-record name="action-payment-schedule-record-partner"
		model="com.axelor.apps.account.db.PaymentSchedule">
		<field name="paymentMode" expr="eval:partner?.inPaymentMode" />
		<field name="currency" expr="eval:partner?.currency" />
    <field name="paymentScheduleLineList" expr="eval: []" />
	</action-record>

	<action-record name="action-payment-schedule-record-in-tax-amount"
		model="com.axelor.apps.account.db.PaymentSchedule">
		<field name="inTaxAmount"
			expr="eval: invoiceSet?.collect(){ it?.companyInTaxTotalRemaining }?.sum()" />
	</action-record>


	<!-- ACTION ATTRS -->

	<action-attrs
		name="action-payment-schedule-attrs-collapse-irrecoverable-group">
		<attribute name="collapse" for="irrecoverable"
			expr="eval: irrecoverableStatusSelect == null || irrecoverableStatusSelect == 0" />
	</action-attrs>

	<action-attrs
		name="action-payment-schedule-attrs-bank-details-domain">
		<attribute name="domain" for="bankDetails"
			expr="eval: &quot;self.id IN (${partner.bankDetailsList.collect{it.id}.join(',')}) AND self.active = TRUE&quot;"
			if="partner &amp;&amp; partner.bankDetailsList" />
	</action-attrs>

	<action-attrs
		name="action-payment-schedule-attrs-company-bank-details-domain">
		<attribute name="domain" for="companyBankDetails"
			expr="eval: &quot;self.id IN (${company.bankDetailsSet.collect{it.id}.join(',')})&quot;"
			if="company &amp;&amp; company.bankDetailsSet" />
	</action-attrs>

	<!-- ACTION METHOD -->
	<action-method name="action-payment-schedule-method-validate">
		<call class="com.axelor.apps.account.web.PaymentScheduleController"
			method="validate" />
	</action-method>

	<action-method name="action-payment-schedule-create-payment-schedule-lines">
		<call class="com.axelor.apps.account.web.PaymentScheduleController"
			method="createPaymentScheduleLines" />
	</action-method>

	<action-method name="action-payment-schedule-payment-schedule-id">
		<call class="com.axelor.apps.account.web.PaymentScheduleController"
			method="paymentScheduleScheduleId" />
	</action-method>

	<action-method name="action-payment-schedule-method-cancel">
		<call class="com.axelor.apps.account.web.PaymentScheduleController"
			method="cancel" />
	</action-method>

  <action-method name="action-payment-schedule-method-fill-partner-bank-details">
    <call class="com.axelor.apps.account.web.PaymentScheduleController" method="fillPartnerBankDetails" />
  </action-method>

  <action-method name="action-payment-schedule-method-fill-company-bank-details">
    <call class="com.axelor.apps.account.web.PaymentScheduleController" method="fillCompanyBankDetails" />
  </action-method>

	<search-filters name="payment-schedule-filters"
		model="com.axelor.apps.account.db.PaymentSchedule" title="Payment schedule filters">
		<filter title="Ongoing Schedules">
			<domain>self.startDate &lt; CURRENT_DATE</domain>
		</filter>
		<filter title="Old Schedules">
			<domain>self.startDate &gt; CURRENT_DATE</domain>
		</filter>
	</search-filters>

</object-views>


    