<?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 sequential="true" name="Invoice" lang="java">
  
    <!-- NOT DISPLAY -->
  	<many-to-one name="canceledPaymentSchedule" ref="com.axelor.apps.account.db.PaymentSchedule"/> <!--  Field not displayed, used to shift to irrecoverable a payment schedule -->
  	
    <!-- HEADER -->
    <string name="invoiceId" namecolumn="true" title="Invoice N°" readonly="true"/>
	<integer name="operationTypeSelect" title="Document Type" required="true" selection="iinvoice.operation.type.select"/>
    <integer name="operationSubTypeSelect" title="Document Subtype" required="true" default="1" selection="iinvoice.operation.sub.type.select"/>
    <string name="partnerTaxNbr" title="Customer tax number"/>
    
    <!-- Legal information -->
    <many-to-one name="company" ref="com.axelor.apps.base.db.Company" required="true" title="Company" />
    <many-to-one name="partner" ref="com.axelor.apps.base.db.Partner" required="true" title="Partner" />
  	<many-to-one name="contactPartner" ref="com.axelor.apps.base.db.Partner" title="Contact"/>
  	<many-to-one name="paymentMode" ref="com.axelor.apps.account.db.PaymentMode" title="Payment mode" />
    <many-to-one name="paymentCondition" ref="com.axelor.apps.account.db.PaymentCondition" title="Payment condition" />
    <many-to-one name="bankDetails" ref="com.axelor.apps.base.db.BankDetails" title="Bank Details"/>
    <many-to-one name="partnerAccount" ref="com.axelor.apps.account.db.Account" title="Partner account"/>
	<many-to-one name="address" ref="com.axelor.apps.base.db.Address" title="Address" />
    <string name="addressStr" title="Address" large="true" multiline="true"/>
    <string name="internalReference" title="Internal Ref."/>
    <string name="externalReference" title="External Ref."/>
    <many-to-one name="priceList" ref="com.axelor.apps.base.db.PriceList" title="Price list"/>
    <boolean name="hideDiscount" title="Hide Discount on prints"/>
    
    <!-- Dates -->
    <date name="invoiceDate" title="Invoice Date" />
    <date name="dueDate" title="Due Date"/>
    <date name="originDate" title="Origin date"/>
    
    <!-- Accounting information -->
    <boolean name="schedulePaymentOk" title="Belong to a payment schedule" default="false"/>
    <many-to-one name="paymentSchedule" ref="com.axelor.apps.account.db.PaymentSchedule" title="Payment Schedule" readonly="true"/>
    <many-to-one name="journal" ref="com.axelor.apps.account.db.Journal" title="Journal"/>
    <many-to-one name="move" ref="com.axelor.apps.account.db.Move" title="Move" readonly="true"/>
    <many-to-one name="oldMove" ref="com.axelor.apps.account.db.Move" title="Previous Move" readonly="true"/>
    <boolean name="doubtfulCustomerOk" title="Doubtful Customer" default="false"/>
  	<many-to-one name="currency" required="true" ref="com.axelor.apps.base.db.Currency" title="Currency"/>
  	<one-to-many name="invoicePaymentList" ref="com.axelor.apps.account.db.InvoicePayment" title ="Payments" mappedBy="invoice"/>
    <date name="ventilatedDate" readonly="true" title="Ventilated Date"/>
    <many-to-one name="ventilatedByUser" ref="com.axelor.auth.db.User" readonly="true" title="Ventilated by"/>
    
    <!-- Invoice content -->
    <one-to-many name="invoiceLineList" ref="InvoiceLine" mappedBy="invoice" title="Invoice Lines" />
    <one-to-many name="invoiceLineTaxList" ref="InvoiceLineTax" mappedBy="invoice" title="Tax Lines" readonly="true" />
    
    <!-- Configuration -->
    <boolean name="alreadyPrintedOk" title="Already printed" default="true"/>
    <many-to-one name="originalInvoice" ref="Invoice" title="Invoice of origin"/>
    <one-to-many name="refundInvoiceList" ref="Invoice" mappedBy="originalInvoice" title="Refunds"/>
    <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 shifting reason"/>
    <many-to-one name="standardInvoice" ref="Invoice" title="Standard Invoice" />
  	<many-to-many name="batchSet" ref="com.axelor.apps.base.db.Batch" title="Batchs"/>
    
    <!-- Blocking information -->
    <boolean name="debitBlockingOk" title="Direct debit blocking"/>
    <many-to-one name="debitBlockingReason" ref="com.axelor.apps.base.db.StopReason" title="Blocking reason"/>
    <date name="debitBlockingToDate" title="Blocking until"/>
    <many-to-one name="debitBlockingByUser" ref="com.axelor.auth.db.User" title="Blocking done by"/>
    
    <boolean name="debtRecoveryBlockingOk" title="Debt recovery blocking"/>
    <many-to-one name="debtRecoveryBlockingReason" ref="com.axelor.apps.base.db.StopReason" title="Blocking reason"/>
    <date name="debtRecoveryBlockingToDate" title="Blocking until"/>
    <many-to-one name="debtRecoveryBlockingByUser" ref="com.axelor.auth.db.User" title="Blocking done by"/>

    <!-- Payments -->
    <string name="debitNumber" title="Direct Debit N°"/>
    <many-to-one name="directDebitManagement" ref="com.axelor.apps.account.db.DirectDebitManagement" title="Direct debit list"/>
    <many-to-one name="rejectMoveLine" ref="com.axelor.apps.account.db.MoveLine" title="Rejection move line"/>
   	<boolean name="usherPassageOk" title="Transfer(ed) to usher"/>
    <decimal name="amountRejected" title="Rejected amount"/> 
    <date name="rejectDate" title="Rejection date"/>
    <many-to-one name="interbankCodeLine" ref="com.axelor.apps.account.db.InterbankCodeLine" title="Rejection reason"/>
    <decimal name="directDebitAmount" title="Debited amount" readonly="true"/> 
    <many-to-one name="paymentMove" ref="com.axelor.apps.account.db.Move" title="Payment Move"/>
    
	<!--  Impression -->
	<integer name="invoicesCopySelect" default="1" selection="grade.1.up.to.10.select"/>
    <string name="note" title="Notes" large="true"/>
      <string name="specificNotes" title="Specific notes" large="true"/>
    <many-to-one name="printedPDF" ref="com.axelor.meta.db.MetaFile"/>
    
    <!-- Totals & Status -->
    
	<!--     Total based on the partner currency in the company accounts -->
    <decimal name="exTaxTotal" title="Total W.T." scale="2" precision="20" readonly="true"/>
    <decimal name="taxTotal" title="Total Tax" scale="2" precision="20" readonly="true"/>
    <decimal name="inTaxTotal" title="Total A.T.I." scale="2" precision="20" readonly="true"/>
    <decimal name="amountPaid" title="Amount paid" readonly="true" />
    <decimal name="amountRemaining" title="Amount remaining" readonly="true"/>

	<!--     Totals based on the company currency -->
    <decimal name="companyExTaxTotal" title="Total W.T." scale="2" precision="20" readonly="true"/>
    <decimal name="companyTaxTotal" title="Total Tax" scale="2" precision="20" readonly="true"/>
    <decimal name="companyInTaxTotal" title="Total A.T.I." scale="2" precision="20" readonly="true"/>
    <decimal name="companyInTaxTotalRemaining" title="Remaining to pay" readonly="true" />
    
    <boolean name="hasPendingPayments" readonly="true"/>
    <integer name="statusSelect" title="Status" selection="iaccount.invoice.status.select" readonly="true" default="1"/>
    
    <many-to-one name="validatedByUser" ref="com.axelor.auth.db.User" readonly="true" title="Validated by"/>
    <date name="validatedDate" readonly="true" title="Validated Date"/>

    <boolean name="inAti" title="In ATI"/>
    
    <string name="supplierInvoiceNb" title="Supplier invoice number"/>
    
    <many-to-one name="companyBankDetails" ref="com.axelor.apps.base.db.BankDetails" title="Company bank"/>

    <!-- Invoice mail -->
    <boolean name="invoiceAutomaticMail" title="Send email on invoice ventilation" default="false"/>
    <many-to-one name="invoiceMessageTemplate" title="Message template" ref="com.axelor.apps.message.db.Template"/>
    <boolean name="invoiceAutomaticMailOnValidate" title="Send email on invoice validate" default="false" />
	<many-to-one name="invoiceMessageTemplateOnValidate" title="Message template" ref="com.axelor.apps.message.db.Template"/>

    <many-to-many name="advancePaymentInvoiceSet" ref="com.axelor.apps.account.db.Invoice" title="Advance payments"/>

    <many-to-one name="tradingName" ref="com.axelor.apps.base.db.TradingName"/>
    <many-to-one name="printingSettings" ref="com.axelor.apps.base.db.PrintingSettings"/>
    
    <date name="subscriptionFromDate" title="From" />
    <date name="subscriptionToDate" title="To" />
    
    <many-to-one name="subrogationRelease" ref="com.axelor.apps.account.db.SubrogationRelease" title="Subrogation release"/>
    <many-to-one name="subrogationReleaseMove" ref="com.axelor.apps.account.db.Move" title="Subrogation release Move"/>
	
	<date name="decisionPfpTakenDate" readonly="true" title="PFP decision date"/>
	<many-to-one name="pfpValidatorUser" ref="com.axelor.auth.db.User" title="PFP Validator"/>
	<integer name="pfpValidateStatusSelect" title="Status" selection="invoice.pfp.validate.status.select" readonly="true" default="1"/>
	<many-to-one name="reasonOfRefusalToPay" title="Reason of refusal to pay" ref="com.axelor.apps.base.db.CancelReason"/>
	<string name="reasonOfRefusalToPayStr" title="Reason of refusal to pay" large="true"/>
	
	<string name="proformaComments" title="Comment to display on proforma" large="true"/>

  <string name="headOfficeAddress" title="Head office address"/>

  <!-- is approach -->
    <boolean name="isInvoiceApproach" title="Facture d'approche" />

    <decimal name="stamp" title="stamp" scale="2" precision="20" />
    <decimal name="fixTax" title="fix Tax" scale="2" precision="20" />

      	
    <many-to-one name="payerPartner" ref="com.axelor.apps.base.db.Partner" title="Payer Supplier"/>
    <many-to-one name="deliveryPartner" ref="com.axelor.apps.base.db.Partner" title="Delivery Supplier"/>


  	<unique-constraint columns="invoiceId,company"/>

	<extra-code><![CDATA[
	
	   	static final int NONE = 0;

		// OPERATION TYPE SELECT
		public static final int OPERATION_TYPE_SUPPLIER_PURCHASE = 1;
		public static final int OPERATION_TYPE_SUPPLIER_REFUND = 2;
		public static final int OPERATION_TYPE_CLIENT_SALE = 3;
		public static final int OPERATION_TYPE_CLIENT_REFUND = 4;

		// 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;
	
		// STATUS SELECT
		public static final int STATUS_DRAFT = 1;
		public static final int STATUS_VALIDATED = 2;
		public static final int STATUS_VENTILATED = 3;
		public static final int STATUS_CANCELED = 4;

	    // OPERATION TYPE SUB SELECT
	    public static final int OPERATION_SUB_TYPE_DEFAULT = 1;
	    public static final int OPERATION_SUB_TYPE_ADVANCE = 2;
	    public static final int OPERATION_SUB_TYPE_BALANCE = 3;
	  
      public static final int OPERATION_SUB_TYPE_FINANCIAL_DISCOUNT = 9;
	    public static final int OPERATION_SUB_TYPE_FINANCIAL_REFUNDS = 10;
      

	    // REPORT TYPE
		public static final int REPORT_TYPE_PROFORMA = 1;
		public static final int REPORT_TYPE_ORIGINAL_INVOICE = 2;
		public static final int REPORT_TYPE_INVOICE_WITH_PAYMENTS_DETAILS= 3;
		public static final int REPORT_TYPE_UPDATED_COPY = 4;


	    // PFP VALIDATE STATUS SELECT
	    public static final int PFP_STATUS_AWAITING = 1;
	    public static final int PFP_STATUS_VALIDATED = 2;
	    public static final int PFP_STATUS_LITIGATION = 3;

	]]></extra-code>
	
	<track>
      <field name="invoiceId" />
      <field name="partner" />
      <field name="statusSelect" on="UPDATE"/>
      <message if="true" on="CREATE">Invoice created</message>
      <message if="statusSelect == 2" tag="important">Validated</message>
      <message if="statusSelect == 3" tag="success">Ventilated</message>
      <message if="statusSelect == 3 &amp;&amp; dueDate.isAfter(__date__)">Important</message>
    </track>

  </entity>
  
</domain-models>
