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

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

		<many-to-one name="saleOrderLine" ref="com.axelor.apps.sale.db.SaleOrderLine"
			title="Sales order line" />
		<many-to-one name="purchaseOrderLine"
			ref="com.axelor.apps.purchase.db.PurchaseOrderLine" title="Purchase order line" />

		<decimal name="reservedQty" title="Allocated Qty" />
		<decimal name="requestedReservedQty" title="Requested reserved qty"/>
		
		<decimal name="qtyInvoiced" title="Invoiced Qty"/>

		<many-to-one name="parentLine" ref="com.axelor.apps.stock.db.StockMoveLine" />
		<one-to-many name="subLineList" ref="com.axelor.apps.stock.db.StockMoveLine"
			mappedBy="parentLine" title="Pack lines" />

		<many-to-one name="account" ref="com.axelor.apps.account.db.Account" title="Accounting.Account"/>

		<one-to-many name="analyticMoveLineList" ref="com.axelor.apps.account.db.AnalyticMoveLine" title="Analytic distribution lines"  mappedBy="stockMoveLine"/>
	   	<many-to-one name="analyticDistributionTemplate" title="Analytic distribution template" ref="com.axelor.apps.account.db.AnalyticDistributionTemplate"/>
    	<many-to-one name="analyticAccount" ref="com.axelor.apps.account.db.AnalyticAccount" title="Analytic Acc."/>
		<many-to-one name="analyticAxis" ref="com.axelor.apps.account.db.AnalyticAxis" title="Analytic axis" />


		<finder-method name="findAllBySaleOrder"
			using="com.axelor.apps.sale.db.SaleOrder:saleOrder" all="true"
			filter="self.stockMove.originTypeSelect LIKE 'com.axelor.apps.sale.db.SaleOrder' AND self.stockMove.originId = :saleOrder.id" />

		<finder-method name="findAllBySaleOrderAndStatusSelect"
			using="com.axelor.apps.sale.db.SaleOrder:saleOrder,Integer:statusSelect"
			all="true"
			filter="self.stockMove.originTypeSelect LIKE 'com.axelor.apps.sale.db.SaleOrder' AND self.stockMove.originId = :saleOrder.id AND self.stockMove.statusSelect = :statusSelect" />


		<finder-method name="findForDeclarationOfExchanges"
	      using="LocalDate:fromDate,LocalDate:toDate,String:productTypeSelect,int:stockMoveTypeSelect,com.axelor.apps.base.db.Country:country,com.axelor.apps.base.db.Company:company "
	      filter="
	        self.stockMove.realDate BETWEEN :fromDate AND :toDate
	        AND self.product.usedInDEB is true 
	        AND self.product.productTypeSelect = :productTypeSelect
	        AND self.stockMove.typeSelect = :stockMoveTypeSelect
	        AND self.stockMove.statusSelect = 3 
	        AND self.stockMove.company = :company 
	        AND (COALESCE(self.stockMove.fromAddress.addressL7Country, self.stockMove.fromStockLocation.address.addressL7Country) = :country
	        OR COALESCE(self.stockMove.toAddress.addressL7Country, self.stockMove.toStockLocation.address.addressL7Country) = :country)
	        AND COALESCE(self.stockMove.fromAddress.addressL7Country, self.stockMove.fromStockLocation.address.addressL7Country)
	        != COALESCE(self.stockMove.toAddress.addressL7Country, self.stockMove.toStockLocation.address.addressL7Country)
	        AND COALESCE(self.stockMove.fromAddress.addressL7Country.economicArea, self.stockMove.fromStockLocation.address.addressL7Country.economicArea)
	        = COALESCE(self.stockMove.toAddress.addressL7Country.economicArea, self.stockMove.toStockLocation.address.addressL7Country.economicArea)"
	      orderBy="stockMove.realDate,id" all="true"/>
		
		<track on="UPDATE">
			<field name="requestedReservedQty"/>
			<field name="reservedQty"/>
		</track>

	</entity>
</domain-models>
