<?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="PaymentVoucher" sequential="true" lang="java">

		<string name="ref" title="Reference" namecolumn="true" required="false"/>
		<many-to-one name="partner" title="Partner" ref="com.axelor.apps.base.db.Partner" required="true"/>
		<many-to-one name="account" ref="com.axelor.apps.account.db.Account" title="Partner account"/>
		<many-to-one name="paymentMode" ref="com.axelor.apps.account.db.PaymentMode" title="Payment mode" required="true"/>
	   	<many-to-one name="companyBankDetails" ref="com.axelor.apps.base.db.BankDetails" title="Company bank details"/>
	    <date name="paymentDate" title="Payment date"/>
		<decimal name="paidAmount" title="Amount paid" />
		<many-to-one name="user" column="user_id" ref="com.axelor.auth.db.User" title="User" readonly="true"/>
		<many-to-one name="moveLine" ref="com.axelor.apps.account.db.MoveLine" title="Overdue moveline" />
		<decimal name="remainingAllocatedAmount" title="Amount Remaining to allocate" />
		<decimal name="allocatedAmount" title="Allocated amount" />
		<one-to-many name="payVoucherDueElementList" ref="com.axelor.apps.account.db.PayVoucherDueElement" title="List of invoices/schedule lines with remaining amount to pay" mappedBy="paymentVoucher" />
		<one-to-many name="payVoucherElementToPayList" ref="com.axelor.apps.account.db.PayVoucherElementToPay" title="List of invoices to pay" mappedBy="paymentVoucher" />
		<integer name="statusSelect" title="Status" default="1" readonly="true" selection="iaccount.payment.voucher.status.select"/>
		<many-to-one name="generatedMove" ref="com.axelor.apps.account.db.Move" title="Generated move" readonly="true"/>
		<boolean name="hasAutoInput" title="Auto. input." default="false"/>
	 	<many-to-one name="company" ref="com.axelor.apps.base.db.Company" title="Company" required="true"/>
	 	<string name="bankCardTransactionNumber" title="CC Transaction N°" readonly="true"/>
	 	<string name="receiptNo" title="Receipt N°" readonly="true"/>
	 	<string name="email" title="Email to use"/>
	 	<boolean name="toSaveEmailOk" title="Save email sent to contact" default="true"/>
	 	<boolean name="defaultEmailOk" title="Default email address" default="false"/>
 	   	<many-to-many name="batchSet" ref="com.axelor.apps.base.db.Batch" title="Batchs"/>
	  	<many-to-one name="currency" ref="com.axelor.apps.base.db.Currency" required="true" title="Currency"/>
	  	<decimal name="paidAmountInCurrency"  title="Amount paid in currency"/>
	 	
<!-- 	 	<integer name="operationTypeSelect" title="Type de saisie paiement" required="true" selection="ipayment.operation.type.select"/> -->
	 	<integer name="operationTypeSelect" title="Payment type" required="true" selection="iinvoice.operation.type.select"/>
	 	
		<decimal name="remainingAmount" title="Amount remaining to allocate">
			<![CDATA[
            BigDecimal totalElementToPay = BigDecimal.ZERO;
            if (getPaidAmount().signum() != 0) {
                if (getPayVoucherElementToPayList() != null || !getPayVoucherElementToPayList().isEmpty() ) {
                    for (PayVoucherElementToPay payVoucherElementToPay : getPayVoucherElementToPayList()) {
                        if (payVoucherElementToPay != null) {
                            totalElementToPay = totalElementToPay.add(payVoucherElementToPay.getAmountToPayCurrency());
                        }
                    }
                }
                return getPaidAmount().subtract(totalElementToPay);
            } else {
                return totalElementToPay;
            }
		    ]]>  
		</decimal>
		
        <string name="chequeNumber" />
        <string name="chequeBank" />
        <string name="chequeOwner" />
        <date name="chequeDate" title="Bank deposit date" />
        <many-to-one name="depositSlip" ref="com.axelor.apps.account.db.DepositSlip" />
		
		
		<string name="label" />
		
		<many-to-one name="confirmedByUser" ref="com.axelor.auth.db.User" readonly="true" title="Confirmed by"/> 
		<date name="confirmationDate" title="Confirmation date" readonly="true"/>
		
		<many-to-one name="acceptedByUser" ref="com.axelor.auth.db.User" readonly="true" title="Accepted by"/> 
		<date name="acceptanceDate" title="acceptance date" readonly="true"/>

		<many-to-one name="requestedByUser" ref="com.axelor.auth.db.User" readonly="true" title="requested by"/> 
		<date name="requestDate" title="request Date" />

		<many-to-one name="recipientUser" ref="com.axelor.auth.db.User" readonly="true" title="recipient"/> 

        
        <unique-constraint columns="ref,company"/>
		<unique-constraint columns="receiptNo,company"/>

		<extra-code><![CDATA[
		
	   	// STATUS SELECT
		public static final int STATUS_DRAFT = 1;
        public static final int STATUS_WAITING_FOR_DEPOSIT_SLIP = 2;
		public static final int STATUS_CONFIRMED = 3;
		
	   	// 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;
		
		]]></extra-code>
	
  	</entity>

</domain-models>
