<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<object-views xmlns="http://axelor.com/xml/ns/object-views"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://axelor.com/xml/ns/object-views http://axelor.com/xml/ns/object-views/object-views_5.2.xsd">
    
    <grid name="tracking-number-grid" title="Tracking Number" model="com.axelor.apps.stock.db.TrackingNumber">
		<hilite if="$moment(perishableExpirationDate).isBefore(__date__) || $moment(warrantyExpirationDate).isBefore(__date__)" color="danger" />
		<hilite if="$moment(perishableExpirationDate).isBefore($moment(_datePlusTwoMonths)) || $moment(warrantyExpirationDate).isBefore($moment(_datePlusTwoMonths))" color="warning" />
		<hilite if="$moment(perishableExpirationDate).isAfter($moment(_datePlusTwoMonths)) || $moment(warrantyExpirationDate).isAfter($moment(_datePlusTwoMonths))" color="success" />
    	<field name="trackingNumberSeq" width="120"/>
    	<field name="product" form-view="product-form" grid-view="product-grid"/>
        <field name="warrantyExpirationDate"/>
        <field name="perishableExpirationDate"/>
        <field name="$availableQty" title="Available qty" type="decimal" width="120"/>
    </grid>
    
    <form name="tracking-number-form" title="Tracking Number" model="com.axelor.apps.stock.db.TrackingNumber"
    	onLoad="action-tracking-number-attrs-hide-fields" >
    	<panel name="mainPanel">
	    	<field name="trackingNumberSeq"/>
	        <field name="product" hidden="true" onChange="action-tracking-number-attrs-hide-fields" form-view="product-form" grid-view="product-grid"/>
	        <field name="warrantyExpirationDate"/>
	        <field name="perishableExpirationDate"/>
	        <field name="counter"/>
        </panel>
    </form>

	<form name="tracking-number-stock-move-line-form" title="Tracking Number" model="com.axelor.apps.stock.db.TrackingNumber"
		  onLoad="action-tracking-number-attrs-hide-fields" onNew="action-tracking-number-attrs-product-stock-move-line,action-tracking-number-group-from-stock-move-line" >
		<panel name="mainPanel">
			<field name="trackingNumberSeq"/>
			<field name="product" onChange="action-tracking-number-attrs-hide-fields" form-view="product-form" grid-view="product-grid"  readonlyIf="$popup() || id != null"/>
			<field name="warrantyExpirationDate" readonly="false"/>
			<field name="perishableExpirationDate" readonly="false"/>
			<field name="counter" hidden="true"/>
		</panel>
	</form>

	<search name="tracking-number-search" title="Tracking number search" limit="50">
		<search-fields>
			<field name="product" type="reference" target="com.axelor.apps.base.db.Product"/>
			<field name="trackingNumber" type="reference" target="com.axelor.apps.stock.db.TrackingNumber" widget="SuggestBox" domain="self.product = :product"/>
		</search-fields>

		<result-fields>
			<field name="object" width="75"/>
			<field name="trackingNumber" type="reference" target="com.axelor.apps.stock.db.TrackingNumber"/>
			<field name="product" type="string"/>
			<field name="futureQty" title="Expected/Future Quantity" type="decimal"/>
			<field name="currentQty" title="Real/Current Quantity" type="decimal"/>
			<field name="stockLocation" title="Stock location" type="string"/>
			<field name="moveLine" title="Stock move line" type="string"/>
			<field name="date" type="date"/>
		</result-fields>

		<select model="com.axelor.apps.stock.db.StockMove" title="Stock move">
			<field name="stockMoveLineList[].trackingNumber" as="trackingNumber"/>
			<field name="stockMoveLineList[].product.fullName" as="product"/>
			<field name="stockMoveLineList[].productName" as="moveLine"/>
			<field name="stockMoveLineList[].realQty" as="currentQty"/>
			<field name="stockMoveLineList[].qty" as="futureQty"/>
			<field name="stockMoveLineList[].stockMove.realDate" as="date"/>
			<where>
				<input name="trackingNumber" field="stockMoveLineList[].trackingNumber"/>
			</where>
		</select>
		<select model="com.axelor.apps.stock.db.StockLocation" title="Stock location">
			<field name="detailsStockLocationLineList[].trackingNumber" as="trackingNumber"/>
			<field name="detailsStockLocationLineList[].product.name" as="product"/>
			<field name="detailsStockLocationLineList[].detailsStockLocation.name" as="stockLocation"/>
			<field name="detailsStockLocationLineList[].currentQty" as="currentQty"/>
			<field name="detailsStockLocationLineList[].futureQty" as="futureQty"/>
			<where>
				<input name="trackingNumber" field="detailsStockLocationLineList[].trackingNumber"/>
			</where>
		</select>
	</search>
	
	<action-group name="action-tracking-number-group-from-stock-move-line">
	  <action name="action-tracking-number-record-from-stock-move-line"/>
	  <action name="action-tracking-number-attrs-from-stock-move-line"/>
	</action-group>
	
	<action-record name="action-tracking-number-record-from-stock-move-line" model="com.axelor.apps.stock.db.TrackingNumber">
	  <field if="eval: __parent__?.stockMove == null || __parent__?.stockMove?.statusSelect == 1" name="counter" expr="eval: __parent__?.qty"/>
	  <field if="eval: __parent__?.stockMove?.statusSelect == 2" name="counter" expr="eval: __parent__?.realQty"/>
	</action-record>
	
	<action-attrs name="action-tracking-number-attrs-from-stock-move-line">
	  <attribute name="hidden" expr="eval: !__parent__?.product?.hasWarranty" for="warrantyExpirationDate"/>
	  <attribute name="hidden" expr="eval: !__parent__?.product?.isPerishable" for="perishableExpirationDate"/>
	</action-attrs>
	
	<action-attrs name="action-tracking-number-attrs-product-stock-move-line">
	  <attribute for="product" name="value" expr="eval: __parent__.product" if="product == null"/>
	</action-attrs>
	
</object-views>