<?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="purchase" package="com.axelor.apps.purchase.db"/>

  <entity name="PurchaseOrder" lang="java">

  	<string name="fullName" namecolumn="true">
  	<![CDATA[
  		if(purchaseOrderSeq==null){
  			return " ";
  		}
  		return purchaseOrderSeq + "-" + supplierPartner.getName();
  	]]>
  	</string>
  	<string name="purchaseOrderSeq" title="Ref." readonly="true"/>
  	<many-to-one name="company" ref="com.axelor.apps.base.db.Company" required="true" title="Company"/>
  	
  	<many-to-one name="supplierPartner" ref="com.axelor.apps.base.db.Partner" required="true" title="Supplier"/>
  	<many-to-one name="contactPartner" ref="com.axelor.apps.base.db.Partner" title="Contact"/>
  	<many-to-one name="priceList" ref="com.axelor.apps.base.db.PriceList" title="Price list"/>
  	
	<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"/>

  	<many-to-one name="team" ref="com.axelor.team.db.Team" title="Team" />
	<many-to-one name="buyerUser" ref="com.axelor.auth.db.User" title="Buyer"/>
  	<many-to-one name="currency" ref="com.axelor.apps.base.db.Currency" title="Currency" required="true"/>
	<date name="deliveryDate" title="Estimated delivery Date"/>
	<date name="orderDate" title="Order Date"/>
  	<one-to-many name="purchaseOrderLineList"  mappedBy="purchaseOrder" ref="com.axelor.apps.purchase.db.PurchaseOrderLine" title="Products list"/>
  	<integer name="statusSelect" title="Status" selection="purchase.purchase.order.status.select" readonly="true"/>
  	<string name="externalReference" title="Supplier ref."/>
    <string name="internalReference" title="Internal Ref."/>
    <integer name="receiptState" title="Receipt State" selection="purchase.order.receipt.state" readonly="true" default="1"/>
  	
	<integer name="orderByState"/>
  	
  	
<!-- Ligne de TVA -->	
	<one-to-many name="purchaseOrderLineTaxList" ref="com.axelor.apps.purchase.db.PurchaseOrderLineTax" mappedBy="purchaseOrder" title="Tax Lines"/>

<!-- Validation and totals -->	
    <many-to-one name="validatedByUser" ref="com.axelor.auth.db.User" readonly="true" title="Validated by"/> 
  	<date name="validationDate" title="Validation date" readonly="true"/>
    <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="amountToBeSpreadOverTheTimetable" title="Amount to be spread over the timetable" scale="2" precision="20" readonly="true"/>
    
    <decimal name="companyExTaxTotal" title="Total W.T." scale="2" precision="20" readonly="true"/>

<!-- Notes -->
	<string name="notes" large="true" multiline="true" title="Description To Display"/>
	<string name="internalNote" large="true" multiline="true" title="Internal Note"/>
	
	<integer name="versionNumber" title="Version Number" readonly="true" default="1"/>
	
	<boolean name="inAti" title="In ATI"/>
	
	<many-to-one name="companyBankDetails" ref="com.axelor.apps.base.db.BankDetails" title="Company bank"/>
	
	<boolean name="displayPriceOnQuotationRequest" title="Display price on requested purchase printing"/>
    
  	<string name="priceRequest" title="Message for requesting prices" large="true"/>

	<many-to-one name="tradingName" ref="com.axelor.apps.base.db.TradingName"/>
	<many-to-one name="printingSettings" ref="com.axelor.apps.base.db.PrintingSettings"/>

	<!-- importation folder -->
	<integer name="importationType" selection="importation.folder.type.select" title="Type d'importation" default="1" />
	<many-to-one name="importationFolder" ref="ImportationFolder" title="Dossier d'importation" />

	<many-to-one name="purchaseRequestOrigin" ref="com.axelor.apps.purchase.db.PurchaseRequest" title="Purchase Request" />
	<many-to-one name="cancelReason" title="Cancel reason" ref="com.axelor.apps.base.db.CancelReason"/>
	<string name="cancelReasonStr" title="Cancel Reason" large="true"/>
	
	<many-to-many name="purchaseRequestSet" ref="com.axelor.apps.purchase.db.PurchaseRequest" mappedBy="purchaseOrderSet" title="Purchase Request" />

	<integer name="purchaseType" title="Purchase Type" selection="purchase.request.project.select"/>
	
	<decimal name="stamp" title="stamp" scale="2" precision="20" />
    <decimal name="fixTax" title="fix Tax" scale="2" precision="20" />
	
	<many-to-one name="requestedByUser" ref="com.axelor.auth.db.User" readonly="true" title="Requested by"/> 
  	<date name="requestDate" title="Request date" readonly="true"/>

	<many-to-one name="approvedByUser" ref="com.axelor.auth.db.User" readonly="true" title="Approved by"/> 
  	<date name="approvalDate" title="Approval date" readonly="true"/>

	<many-to-one name="barCodeSeq" title="Barcode" ref="com.axelor.meta.db.MetaFile" />

  	<unique-constraint columns="purchaseOrderSeq,company"/>
  	
  	<extra-code>
   		<![CDATA[
   		  /** Static purchase order status select */
		  public static final int STATUS_DRAFT = 1;
		
		  public static final int STATUS_REQUESTED = 2;
		  public static final int STATUS_VALIDATED = 3;
		  public static final int STATUS_FINISHED = 4;
		  public static final int STATUS_CANCELED = 5;
		
		  /** Static purchase order receipt status select */
		  public static final int STATE_NOT_RECEIVED = 1;
		
		  public static final int STATE_PARTIALLY_RECEIVED = 2;
		  public static final int STATE_RECEIVED = 3;
		
		  /** Static invoicing type select */
		  public static final int INVOICING_FREE = 1;
		
		  public static final int INVOICING_BY_DELIVERY = 2;
		  public static final int INVOICING_PER_ORDER = 3;
   		]]>
   	</extra-code>
	  
	<track>
	  <field name="cancelReason" />
	  <field name="cancelReasonStr" />
      <field name="purchaseOrderSeq" />
      <field name="supplierPartner" />
      <field name="statusSelect" on="UPDATE"/>
      <message if="true" on="CREATE">Purchase order created</message>
      <message if="statusSelect == 1" tag="important">Draft</message>
      <message if="statusSelect == 2" tag="important">Requested</message>
      <message if="statusSelect == 3" tag="info">Validated</message>
      <message if="statusSelect == 4" tag="success">Finished</message>
      <message if="statusSelect == 5" tag="warning">Canceled</message>
    </track>
	
  </entity>

</domain-models>
