<?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="stock-rules-grid" title="Stock rules" model="com.axelor.apps.stock.db.StockRules" orderBy="stockLocation.name,product.code,useCaseSelect">
        <toolbar>
        <button name="checkDuplicateBtn" title="Check Duplicate" onClick="action-base-method-show-duplicate"/>
        </toolbar>
        <field name="stockLocation" form-view="stock-location-form" grid-view="stock-location-grid"/>
        <field name="product.code" width="120" title="Product code" form-view="product-form" grid-view="product-grid"/>
        <field name="product.name" title="Product name" form-view="product-form" grid-view="product-grid"/>
        <field name="minQty" aggregate="min"/>
        <field name="idealQty" aggregate="min"/>
        <field name="maxQty" aggregate="min"/>
        <field name="reOrderQty" aggregate="sum"/>
		<field name="useCaseSelect"/>
        <field name="typeSelect"/>
        <field name="orderAlertSelect"/>
    </grid>
<grid name="stock-rule-product-grid" title="Stock rules" model="com.axelor.apps.stock.db.StockRules" orderBy="stockLocation.name,useCaseSelect">
    	<field name="stockLocation" form-view="stock-location-form" grid-view="stock-location-grid"/>
        <field name="minQty" aggregate="min"/>
        <field name="idealQty" aggregate="min"/>
        <field name="maxQty"/>
        <field name="reOrderQty" aggregate="sum"/>
		<field name="useCaseSelect"/>
        <field name="orderAlertSelect"/>
    </grid>
    <form name="stock-rules-form" title="Stock rules" model="com.axelor.apps.stock.db.StockRules"
    	onNew="action-stock-rules-record-new" width="large">
		<panel name="mainPanel" >
        	<field name="stockLocation" form-view="stock-location-form" grid-view="stock-location-grid" canEdit="false" colSpan="3"/>
	    	<field name="product" canEdit="false" onChange="action-stock-rules-record-product-change" domain="self.productTypeSelect = 'storable' and self.stockManaged = true " form-view="product-form" grid-view="product-grid" colSpan="3"/>
        	<field name="useCaseSelect" colSpan="3" onChange="action-stock-rules-record-useCaseSelect-change"/>
        	<field name="typeSelect" showIf="useCaseSelect == 2" colSpan="3" requiredIf="useCaseSelect == 2"/>
	    	<field name="orderAlertSelect" selection-in="[1 * (useCaseSelect != 1), 2 * (product.procurementMethodSelect != 'produce'), 3 * (product.procurementMethodSelect != 'buy')]" colSpan="3"/>
            <field name="stockRuleMessageTemplate" colSpan="3" showIf="useCaseSelect == 2 &amp;&amp; orderAlertSelect == 1"/>
	    	<field name="user" canNew="false" canView="false" canEdit="false" form-view="user-form" grid-view="user-grid" showIf="orderAlertSelect == 1" colSpan="3"/>
	    	<field name="team" canEdit="false" form-view="team-form" grid-view="team-grid" showIf="orderAlertSelect == 1" colSpan="3"/>
        	<panel name="quantitiesPanel" title="Quantities" colSpan="6">
	        	<field name="minQty" colSpan="6" onChange="action-stock-rules-minqty-change" validIf="$number(minQty) >= 0"/>
	        	<field name="reOrderQty" colSpan="6" validIf="$number(reOrderQty) >= 0"/>
	        	<field name="useIdealQty" colSpan="6"/>
				<field name="idealQty" showIf="useIdealQty" colSpan="6" onChange="action-stock-rules-idealqty-change" validIf="!useIdealQty &amp;&amp; $number(idealQty) == 0 || $number(idealQty) >= $number(minQty) &amp;&amp; (!useMaxQty || $number(idealQty) &lt;= $number(maxQty))"/>
		    	<field name="useMaxQty" colSpan="6"/>
				<field name="maxQty" showIf="useMaxQty" colSpan="6" onChange="action-stock-rules-maxqty-change" validIf="!useMaxQty &amp;&amp; $number(maxQty) == 0 || $number(maxQty) >= $number(minQty)"/>
			</panel>
			<field name="product.procurementMethodSelect" hidden="true"/>
		</panel>
		<panel-mail name="mailPanel">
	    	<mail-messages limit="4"/>
	    	<mail-followers/>
	  	</panel-mail>
    </form>

    <action-record name="action-stock-rules-record-new" model="com.axelor.apps.stock.db.StockRules">
	    <field name="minQty" expr="1"/>
	    <field name="idealQty" expr="1"/>
	    <field name="orderAlertSelect" expr="2"/>
	    <field name="useCaseSelect" expr="1"/>
	    <field name="user" expr="eval:__user__"/>
	    <field name="team" expr="call:com.axelor.apps.base.service.user.UserService:getUserActiveTeam()"/>
	</action-record>

	<action-record name="action-stock-rules-minqty-change"
		model="com.axelor.apps.stock.db.StockRules">
		<field name="idealQty" expr="eval: minQty" if="minQty > idealQty" />
		<field name="maxQty" expr="eval: minQty" if="minQty > maxQty" />
	</action-record>

	<action-record name="action-stock-rules-idealqty-change"
		model="com.axelor.apps.stock.db.StockRules">
		<field name="minQty" expr="eval: idealQty" if="minQty > idealQty" />
		<field name="maxQty" expr="eval: idealQty" if="idealQty > maxQty" />
	</action-record>

	<action-record name="action-stock-rules-maxqty-change"
		model="com.axelor.apps.stock.db.StockRules">
		<field name="minQty" expr="eval: maxQty" if="minQty > maxQty" />
		<field name="idealQty" expr="eval: maxQty" if="idealQty > maxQty" />
	</action-record>
	
	<action-record name="action-stock-rules-record-product-change"
		model="com.axelor.apps.stock.db.StockRules">
		<field name="orderAlertSelect" expr="eval: 1" if="product?.procurementMethodSelect == 'buyAndProduce' &amp;&amp; useCaseSelect != 1"/>
		<field name="orderAlertSelect" expr="eval: 3" if="product?.procurementMethodSelect == 'buyAndProduce' &amp;&amp; useCaseSelect == 1"/>
		<field name="orderAlertSelect" expr="eval: 2" if="product?.procurementMethodSelect == 'buy'"/>
		<field name="orderAlertSelect" expr="eval: 3" if="product?.procurementMethodSelect == 'produce'"/>
	</action-record>
	
	<action-record name="action-stock-rules-record-useCaseSelect-change"
		model="com.axelor.apps.stock.db.StockRules">
		<field name="orderAlertSelect" expr="eval: null" if="useCaseSelect == 1 &amp;&amp; orderAlertSelect == 1"/>
	</action-record>

	<action-record name="action-stock-rules-record-set-stock-rule-message-template" model="com.axelor.apps.stock.db.StockRules">
		<field name="stockRuleMessageTemplate" expr="eval: __repo__(StockConfig).all().filter('self.company.id = ?1',__this__.stockLocation?.company?.id).fetchOne()?.getStockRuleMessageTemplate()" if="!stockRuleMessageTemplate"/>
	</action-record>

</object-views>
