<?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="subrogation-release-grid" title="Subrogation releases" model="com.axelor.apps.account.db.SubrogationRelease" orderBy="-sequenceNumber">
		<field name="sequenceNumber" width="120"/>
		<field name="company" if="__config__.app.getApp('base').getEnableMultiCompany()"/>
		<field name="company.accountConfig.factorPartner" />
		<field name="transmissionDate"/>
		<field name="statusSelect" />
	</grid>

	<form name="subrogation-release-form" title="Subrogation release" model="com.axelor.apps.account.db.SubrogationRelease"
		 onNew="action-subrogation-release-onnew"
		 onLoad="action-subrogation-release-onLoad">

		<menubar>
			<menu name="subrogationReleasePrintExportMenu" title="Print/Export" icon="fa-print">
				<item name="printToPDFItem" action="action-subrogation-release-method-print-to-pdf" title="Print to PDF"
					readonlyIf="statusSelect &lt; 2" />
				<item name="exportToCSVItem" action="action-subrogation-release-method-export-to-csv" title="Export to CSV"
					readonlyIf="statusSelect &lt; 2" />
			</menu>
		</menubar>

		<panel name="mainPanel">
			<field name="sequenceNumber" />
			<field name="statusSelect" widget="NavSelect" colSpan="12" />
			<field name="company" form-view="company-form" grid-view="company-grid" canEdit="false"/>
			<field name="company.accountConfig.factorPartner" readonly="true" form-view="partner-form" />
			<panel-related name="invoiceSetPanel" field="invoiceSet" grid-view="invoice-release-details-grid"
				form-view="invoice-form" colSpan="12" canNew="false" canEdit="false" readonlyIf="statusSelect &gt; 1"
				domain="self.company = :company AND self.partner.factorizedCustomer = TRUE
                    AND self.statusSelect = 3 
                    AND self.id not in (select Invoices.id from SubrogationRelease as SR join SR.invoiceSet as Invoices where SR.statusSelect in (2,3,4))
                    AND ((self.amountRemaining &gt; 0 AND self.hasPendingPayments = FALSE) or self.originalInvoice.id in (select Invoices.id from SubrogationRelease as SR join SR.invoiceSet as Invoices where SR.statusSelect in (2,3,4))) 
                    AND self.operationTypeSelect not in (1,2)" />
		</panel>

		<panel name="actionsPanel" title="Actions" sidebar="true">
			<button name="retrieveInvoicesBtn" title="Retrieve invoices from factorized customers"
				onClick="action-subrogation-release-method-retrieve-invoices" readonlyIf="company == null"
				showIf="statusSelect == 1" />
			<button name="transmitReleaseBtn" title="Transmit release"
				onClick="save,action-subrogation-release-method-transmit-release"
				readonlyIf="invoiceSet == null || invoiceSet.length == 0" showIf="statusSelect == 1" />
			<button name="enterReleaseInTheAccountsBtn" title="Enter release in the accounts"
				onClick="save,action-subrogation-release-method-enter-release-in-the-accounts" showIf="statusSelect == 2" />
			<button name="cancelReleaseBtn" title="Cancel release"
				onClick="action-subrogation-release-record-cancel-release,save" showIf="statusSelect &gt; 1 &amp;&amp; statusSelect &lt; 3" />
			<button name="displayMoveLines" title="See Move Lines" onClick="action-subrogation-release-display-move-lines-view" showIf="statusSelect == 3 || statusSelect == 4"/>
		</panel>
		
		<panel sidebar="true">
			<field name="transmissionDate"/>
			<field name="accountingDate" readonlyIf="statusSelect >= 3"/>
			<field name="comments"/>
			<field name="cancelReason" showIf="statusSelect == 5"/>
			<field name="$totalAmountRemaining" title="Total Amount Remaining" readonly="true"/>
		</panel>
		
		<panel-mail>
			<mail-messages limit="4"/>
			<mail-followers/>
		</panel-mail>

	</form>

	<action-method name="action-subrogation-release-method-retrieve-invoices">
		<call class="com.axelor.apps.account.web.SubrogationReleaseController"
			method="retrieveInvoices" />
	</action-method>

	<action-method name="action-subrogation-release-method-transmit-release">
		<call class="com.axelor.apps.account.web.SubrogationReleaseController"
			method="transmitRelease" />
	</action-method>

	<action-method name="action-subrogation-release-method-print-to-pdf">
		<call class="com.axelor.apps.account.web.SubrogationReleaseController"
			method="printToPDF" />
	</action-method>

	<action-method name="action-subrogation-release-method-export-to-csv">
		<call class="com.axelor.apps.account.web.SubrogationReleaseController"
			method="exportToCSV" />
	</action-method>

	<action-method name="action-subrogation-release-method-enter-release-in-the-accounts">
		<call class="com.axelor.apps.account.web.SubrogationReleaseController"
			method="enterReleaseInTheAccounts" />
	</action-method>
	
	<action-method name="action-subrogation-release-display-move-lines-view">
		<call class="com.axelor.apps.account.web.SubrogationReleaseController"
			method="displayMoveLines" />
	</action-method>
	
	<action-record name="action-subrogation-release-onnew"
		model="com.axelor.apps.account.db.SubrogationRelease">
		<field name="company" expr="eval: __user__.getActiveCompany()" />
	</action-record>
	
	<action-record name="action-subrogation-release-onLoad"
		model="com.axelor.apps.account.db.SubrogationRelease">
		<field name="$totalAmountRemaining" expr="eval: invoiceSet?.collect{it->it.amountRemaining}.sum()" />
	</action-record>
	
	<action-record name="action-subrogation-release-record-cancel-release"
		model="com.axelor.apps.account.db.SubrogationRelease">
		<field name="statusSelect" expr="eval: __repo__(SubrogationRelease).STATUS_CANCELED" />
	</action-record>

</object-views>
