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

  <entity name="SaleOrder" lang="java">
  	
  	<many-to-one name="company" ref="com.axelor.apps.base.db.Company" required="true" title="Company"/>
	<string name="fullName" namecolumn="true"/>
  	<string name="saleOrderSeq"  title="Internal Number" readonly="true"/>
  	<many-to-one name="currency" ref="com.axelor.apps.base.db.Currency" title="Currency" massUpdate="true"/>
  	<string name="externalReference" title="Customer reference"/>
  	<date name="creationDate" title="Creation date" required="true" readonly="true"/>
  	<datetime name="confirmationDateTime" title="Confirmation date" readonly="true"/>
	<many-to-many name="batchSet" ref="com.axelor.apps.base.db.Batch" title="Batchs"/>
	<boolean name="blockedOnCustCreditExceed" title="Blocked"/>
	<boolean name="manualUnblock" title="Manual Unblock"/>
	<string name="title" title="Title"/>
	
<!--   	Onglet Coordonnées client -->
  	<many-to-one name="clientPartner" ref="com.axelor.apps.base.db.Partner" title="Customer" massUpdate="true"/>
  	<many-to-one name="contactPartner" ref="com.axelor.apps.base.db.Partner" title="Customer contact"/>
  	<many-to-one name="mainInvoicingAddress" ref="com.axelor.apps.base.db.Address" title="Main/Invoicing address"/>

	<string name="mainInvoicingAddressStr" title="Main/Invoicing address" large="true" multiline="true"/>

	<many-to-one name="priceList" ref="com.axelor.apps.base.db.PriceList" title="Price list"/>
    <many-to-one name="deliveryAddress" ref="com.axelor.apps.base.db.Address" title="Delivery address"/>
	<string name="deliveryAddressStr" title="Delivery address" large="true" multiline="true"/>
  	<boolean name="isToPrintLineSubTotal" title="Print subtotal per line"/>
  	<boolean name="hasSubLineDefaultValue" title="Default value for sublines"/>
  	<boolean name="inAti" title="In ATI"/>
  	<many-to-one name="duration" ref="com.axelor.apps.base.db.Duration" title="Validity duration"/>
  	<date name="endOfValidityDate" title="End of validity"/>
  	
<!--   	Ligne de devis -->
	<one-to-many name="saleOrderLineList" ref="com.axelor.apps.sale.db.SaleOrderLine" mappedBy="saleOrder" title="Sale order lines" orderBy="sequence"/>

<!--   	Ligne de Taxe -->	
	<one-to-many name="saleOrderLineTaxList" ref="com.axelor.apps.sale.db.SaleOrderLineTax" mappedBy="saleOrder" title="Tax Lines" 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="advanceTotal" title="Advance payment total" scale="2" precision="20" readonly="true"/>
	<decimal name="amountToBeSpreadOverTheTimetable" title="Amount to be spread over the timetable" scale="2" precision="20" readonly="true" copy="false"/>
    
    <decimal name="companyExTaxTotal" title="Total W.T." scale="2" precision="20" readonly="true"/>
  	<decimal name="companyCostTotal" title="Total cost in company currency" hidden="true"/>
    
    <integer name="statusSelect" title="Status" selection="sale.order.status.select" readonly="true"/>
    
<!-- 	Margins -->
	<decimal name="accountedRevenue" title="Accounted Revenue"/>
	<decimal name="totalCostPrice" title="Total cost price"/>
	<decimal name="totalGrossMargin" title="Gross Profit"/>
	<decimal name="marginRate" title="Margin (%)"/>
	<decimal name="markup" title="Markup (%)"/>
  	
<!--   	Onglet Autres informations -->
	<many-to-one name="salemanUser" ref="com.axelor.auth.db.User" title="Salesman" massUpdate="true"/>
	<many-to-one name="team" ref="com.axelor.team.db.Team" title="Team" massUpdate="true"/>
	
<!-- 	Onglet description -->
	<string name="description" title="Description to display" large="true"/>
	<string name="internalNote" title="Internal note" large="true"/>

<!-- 	Onglet Commande Client -->
    <date name="orderDate" title="Order Date"/>
    <string name="orderNumber" title="Order Number"/>
    
<!--     Onglet scheduler -->
	<date name="invoicedFirstDate" title="Invoiced first date"/>
	<date name="nextInvPeriodStartDate" title="Next invoiced period start date" readonly="true"/>
    
    <many-to-one name="confirmedByUser" ref="com.axelor.auth.db.User" readonly="true" title="Confirmed by"/>
    
    <boolean name="template" title="Template" hidden="true"/>
    <integer name="versionNumber" title="Version Number" readonly="true" default="1"/>
    <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-one name="templateUser" ref="com.axelor.auth.db.User" title="Template created by" readonly="true"/>
    
    <boolean name="hideDiscount" title="Hide Discount on prints"/>
    
	<date name="deliveryDate" title="Estimated shipping date"/>
	<string name="deliveryCondition" title="Delivery conditions" large="true" multiline="true"/>
	<string name="specificPackage" title="Specific package" large="true" multiline="true"/>
	
	<one-to-many name="advancePaymentList" ref="com.axelor.apps.sale.db.AdvancePayment" mappedBy="saleOrder" title="Advance Payments"/>
	<boolean name="advancePaymentNeeded" title="Advance payment needed"/>
	<decimal name="advancePaymentAmountNeeded" title="Advance payment amount needed"/>
	
	<many-to-one name="companyBankDetails" ref="com.axelor.apps.base.db.BankDetails" title="Company bank"/>
	
	<!-- Impression -->
    <string name="specificNotes" title="Specific notes" large="true"/>

	<boolean name="orderBeingEdited" />

    <many-to-one name="opportunity" ref="com.axelor.apps.crm.db.Opportunity" />

	<many-to-one name="tradingName" ref="com.axelor.apps.base.db.TradingName"/>
	<many-to-one name="printingSettings" ref="com.axelor.apps.base.db.PrintingSettings"/>
	
	<!-- Subscription fields -->
	<integer name="periodicityTypeSelect" selection="sale.subscription.periodicity.type.select" title="Periodicity type" default="2"/>
	<integer name="numberOfPeriods" title="Number of periods" min="1" default="1" />
	<date name="contractStartDate" title="Contract start date" />
	<integer name="contractPeriodInMonths" title="Contract period in months" />
	<date name="currentContractPeriodEndDate" title="End of current contract period" />
	<date name="contractEndDate" title="Contract end date" />
	<boolean name="isTacitAgreement" title="Tacit agreement" />
	<integer name="noticePeriodInDays" title="Notice period in days" />
	<date name="nextInvoicingDate" title="Next invoicing date" />
	<date name="nextInvoicingStartPeriodDate" title="Next invoicing start period date" />
	<date name="nextInvoicingEndPeriodDate" title="Next invoicing end period date" />
	<string name="subscriptionComment" title="Comment" large="true" />
	<string name="subscriptionText" title="Subscription text to display" large="true" />

	<string name="invoiceComments" title="Comment to display on invoice" large="true"/>
	<string name="deliveryComments" title="Comment to display on delivery" large="true"/>
	<string name="pickingOrderComments" title="Comment to display on picking order" large="true"/>
	
	<string name="proformaComments" title="Comment to display on proforma" large="true"/>
	
 	<unique-constraint columns="saleOrderSeq,company"/>

    <extra-code>
   		<![CDATA[
            // STATUS   
			public static final int STATUS_DRAFT_QUOTATION = 1;
			public static final int STATUS_FINALIZED_QUOTATION = 2;
			public static final int STATUS_ORDER_CONFIRMED = 3;
			public static final int STATUS_ORDER_COMPLETED = 4;
			public static final int STATUS_CANCELED = 5;
   		]]>
   	</extra-code>
   	
   	 <track>
      <field name="saleOrderSeq" />
      <field name="clientPartner" />
      <field name="statusSelect"/>
      <field name="creationDate" on="CREATE" />
      <field name="confirmationDateTime" on="UPDATE"/>
      <field name="inTaxTotal" />
      <message if="true" on="CREATE">Quotation/sale order created</message>
      <message if="statusSelect == 1" tag="important">Draft quotation</message>
      <message if="statusSelect == 2" tag="info">Finalized quotation</message>
      <message if="statusSelect == 3" tag="success">Order confirmed</message>
      <message if="statusSelect == 4" tag="success">Order completed</message>
      <message if="statusSelect == 5" tag="warning">Canceled</message>
    </track>
    
  </entity>

</domain-models>
