<?xml version="1.0" encoding="UTF-8"?>
<domain-models xmlns="http://axelor.com/xml/ns/domain-models" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://axelor.com/xml/ns/domain-models http://axelor.com/xml/ns/domain-models/domain-models_5.2.xsd">
  
  <module name="account" package="com.axelor.apps.account.db"/>
  
  <entity name="MoveLine" lang="java">
  
    <many-to-one name="move" ref="Move" title="Move" initParam="true"/>
    
    <many-to-one name="partner" ref="com.axelor.apps.base.db.Partner" title="Partner" initParam="true"/>
    <many-to-one name="account" ref="Account" required="true" title="Accounting.Account" initParam="true"/>
    <date name="date" column="date_val" title="Date" initParam="true"/>
    <date name="dueDate" title="Due Date" initParam="true"/>
    <integer name="counter" title="Line Nbr." initParam="true"/>
    <decimal name="debit" title="Debit" initParam="true"/>
    <decimal name="credit" title="Credit" initParam="true"/>
    <string name="description" title="Description" initParam="true"/>
    <string name="origin" title="Origin" initParam="true"/>
    
    <one-to-many name="debitReconcileList" ref="Reconcile" title="Debit Reconcile list" mappedBy="debitMoveLine" readonly="false"/>
    <one-to-many name="creditReconcileList" ref="Reconcile" title="Credit Reconcile List" mappedBy="creditMoveLine" readonly="false"/>
    <decimal name="amountPaid" title="MoveLine.amountReconciled" default="0.0" readonly="true"/>
    
    <many-to-one name="invoiceReject" ref="com.axelor.apps.account.db.Invoice" title="Invoice rejected"/>
    <boolean name="exportedDirectDebitOk" title="Exported Direct Debit" default="false"/>
    <many-to-one name="interbankCodeLine" ref="com.axelor.apps.account.db.InterbankCodeLine" title="Rejection reason"/>
    
    <integer name="reimbursementStatusSelect" title="Reimbursement status" default="0" selection="move.line.reimbursement.status.select"/>
    <string name="passageReason" title="Irrecoverable Shift Reason" readonly="true"/>
    <boolean name="usherPassageOk" title="Transfer(ed) to Usher"/>
    <many-to-one name="taxLine" ref="com.axelor.apps.account.db.TaxLine" title="Tax" massUpdate="true"/>
    <many-to-one name="sourceTaxLine" ref="com.axelor.apps.account.db.TaxLine" title="Source Tax"/>
   
    <decimal name="currencyRate" title="Currency Rate" precision="20" scale="5" initParam="true"/>
    <decimal name="currencyAmount" title="Amount in Acc. currency" initParam="true"/>
    
    <!-- 	Field not displayed, used during the payment voucher input -->
	<decimal name="maxAmountToReconcile" title="Max Amount to reconcile" />
	<many-to-one name="paymentScheduleLine" ref="com.axelor.apps.account.db.PaymentScheduleLine" title="Payment Schedule line"/>
	
	<!-- 	Field not displayed, used for direct debit only -->
	<decimal name="amountExportedInDirectDebit" title="Amount exported for direct debit"/>
	
	<!--  Field not displayed, used for account clearance -->
	<many-to-one name="accountClearance" ref="com.axelor.apps.account.db.AccountClearance" title="Account clearance"/>
	
	<integer name="irrecoverableStatusSelect" title="Irrecoverable status" default="0" readonly="true" selection="iaccount.account.schedule.irrecoverable.status.select"/>
	<many-to-one name="managementObject" ref="com.axelor.apps.account.db.ManagementObject" title="Irrecoverable shift reason"/>
    
    <one-to-many name="analyticMoveLineList" ref="com.axelor.apps.account.db.AnalyticMoveLine" title="Analytic distribution lines" mappedBy="moveLine"/>
    <many-to-one name="analyticDistributionTemplate" title="Analytic distribution template" ref="com.axelor.apps.account.db.AnalyticDistributionTemplate"/>
    
    <string name="accountCode" readonly="true"/>
    <string name="accountName" readonly="true"/>
    <string name="partnerFullName" readonly="true"/>
    <string name="partnerSeq" readonly="true"/>
    
<!--     Used for update partner balances when we change the partner of a move line in daybook mode -->
    <long name="partnerId"/>
    <long name="accountId"/>
    
    <decimal name="taxRate"  scale="3" precision="20" readonly="true"/>
    <string name="taxCode" readonly="true"/>
      
    <decimal name="taxAmount" scale="2" precision="20" title="Tax amount" readonly="true"/>
    <one-to-many name="taxPaymentMoveLineList" mappedBy="moveLine" ref="com.axelor.apps.account.db.TaxPaymentMoveLine"/>

    <many-to-one name="reconcileGroup" title="Reconcile Group"
                 ref="com.axelor.apps.account.db.ReconcileGroup"/>
	
	<date name="originDate" title="Origin date" initParam="true"/>
	
    <string name="name" title="Name" namecolumn="true">
    	<![CDATA[
    	if (move != null && move.getReference() != null){
    		return move.getReference() + "-" + Integer.toString(counter);
    	}
		else {
			return Integer.toString(counter);
		 }
	    ]]>
    </string>
    <decimal name="amountRemaining" title="To pay / To use" >
    	<![CDATA[
    	if (account != null && account.getReconcileOk()){
		    if (credit.compareTo(BigDecimal.ZERO) == 1) return credit.subtract(amountPaid);
		    else if (debit.compareTo(BigDecimal.ZERO) == 1) return debit.subtract(amountPaid);
		    else return BigDecimal.ZERO;
	    }
		else return BigDecimal.ZERO;
	    ]]>
    </decimal>

    <finder-method name="findByReconcileGroup" all="true" using="reconcileGroup"/>

  	<extra-code><![CDATA[

		// REIMBURSEMENT STATUS
		public static final int REIMBURSEMENT_STATUS_NULL = 0;
		public static final int REIMBURSEMENT_STATUS_REIMBURSING = 1;
		public static final int REIMBURSEMENT_STATUS_REIMBURSED = 2;
	
		// IRRECOVERABLE STATUS SELECT
		public static final int IRRECOVERABLE_STATUS_NOT_IRRECOUVRABLE = 0;
		public static final int IRRECOVERABLE_STATUS_TO_PASS_IN_IRRECOUVRABLE = 1;
		public static final int IRRECOVERABLE_STATUS_PASSED_IN_IRRECOUVRABLE = 2;
	
	]]></extra-code>

	<track>
		 <field name="date"/>
		 <field name="partner"/>
		 <field name="account"/>
		 <field name="debit"/>
		 <field name="credit"/>
		 <field name="origin"/>
		 <field name="description"/>
		 <message if="true" on="CREATE">Move line created</message>
	</track>
	
  </entity>

</domain-models>
