<?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="inventory-grid" title="Inventory" model="com.axelor.apps.stock.db.Inventory">
		<field name="inventorySeq" width="120"/>
		<field name="stockLocation" form-view="stock-location-form" grid-view="stock-location-grid"/>
		<field name="typeSelect"/>
		<field name="plannedStartDateT"/>
        <field name="plannedEndDateT"/>
		<field name="statusSelect"/>
		<field name="company" if="__config__.app.getApp('base').getEnableMultiCompany()"/>
  </grid>

    <form name="inventory-form" title="Inventory" model="com.axelor.apps.stock.db.Inventory" width="large" onNew="action-inventory-record-new">
        <menubar>
	        <menu title="Tools" icon="fa-wrench" name="inventoryMenu">
	        	<item name="showStockMoveItem" action="action-inventory-method-show-stockmoves" title="Show stock move(s)" readonlyIf="statusSelect != 5"/>
	        </menu>
        </menubar>
        <panel name="mainPanel">
            <panel name="statusPanel" colSpan="10">
                <field name="statusSelect" showTitle="false" widget="NavSelect" colSpan="12"/>
                <field name="inventorySeq" readonly="true" css="label-bold bold large" showTitle="false"/>
            </panel>

            <panel name="actionsPanel" colSpan="2" itemSpan="12">
                <button name="planBtn" title="Plan" icon="fa-calendar" showIf="statusSelect == 1 &amp;&amp; stockLocation != null" onClick="action-group-stock-inventory-plan-click"/>
                <button name="startBtn" title="Start" icon="fa-play" showIf="statusSelect == 2" onClick="action-group-stock-inventory-start-click"/>
                <button name="completeBtn" title="Complete" icon="fa-check" showIf="statusSelect == 3" onClick="action-group-stock-inventory-complete-click"/>
                <button name="validateBtn" title="Validate" icon="fa-check" showIf="statusSelect == 4" onClick="action-group-stock-inventory-validate-click"/>
                <button name="cancelBtn" title="Cancel" icon="fa-times-circle" css="btn-danger" showIf="statusSelect &lt; 6" onClick="action-group-stock-inventory-cancel-click"/>
                <button name="draftBtn" title="Draft" showIf="statusSelect == 6" onClick="action-group-stock-inventory-draft-click"/>
            </panel>
        </panel>
        
        <panel-tabs name="mainPanelTab">
        	<panel title="Details">
	        	<panel name="detailPanel"  colSpan="12">
              <field name="company" readonlyIf="statusSelect >= 2" onChange="action-inventory-attrs-reset-stock-location" required="true" canEdit="false"/>
							<field name="plannedStartDateT" readonlyIf="statusSelect >= 3" required="true" colSpan="2"/>
							<field name="plannedEndDateT" readonlyIf="statusSelect >= 5" required="true" colSpan="2" validIf="plannedStartDateT &lt; plannedEndDateT"/>
							<field name="createdOn" readonly="true" colSpan="2"/>
							<field name="stockLocation" canEdit="false" onSelect="action-inventory-attrs-set-location-domain" readonlyIf="statusSelect >= 2" grid-view="stock-location-grid" form-view="stock-location-form"/>
							<field name="typeSelect" validIf="typeSelect > 0"/>
							<field name="completedBy" readonly="true" showIf="completedBy"/>
							<field name="validatedBy" readonly="true" showIf="validatedBy"/>
							<field name="validatedOn" readonly="true" showIf="validatedOn"/>
							<field name="description" colSpan="12" height="3"/>
	        	</panel>
	        	
	        	<panel name="exportPanel" title="Export" showIf="statusSelect &gt; 1 &amp;&amp; statusSelect &lt; 6" colSpan="12">
	                <field name="formatSelect"/>
	                <panel name="showinventoryPanel" colSpan="3" stacked="true">
	                    <button name="showInventoryBtn" title="Export" readonlyIf="formatSelect == null" onClick="save,action-inventory-method-show-inventory" showIf="formatSelect != 'csv'"/>
	                    <button name="showInventoryBtn" title="Export" readonlyIf="formatSelect == null" onClick="save,action-inventory-method-export-inventory" showIf="formatSelect == 'csv'"/>
	                </panel>
	            </panel>
	
	            <panel name="importPanel" title="Import" showIf="statusSelect == 3 || statusSelect == 4" colSpan="12">
	                <field name="importFile" widget="binary-link"/>
	                <button name="importFileBtn" title="Import" readonlyIf="importFile == null" onClick="action-group-stock-inventory-importfile-click" colSpan="3"/>
	            </panel>
            </panel>
        	
        	<panel-related name="inventoryLineListPanel" field="inventoryLineList" readonlyIf="statusSelect >= 5 || stockLocation == null" colSpan="12" height="20" grid-view="inventory-line-grid" form-view="inventory-line-form"/>
        	
			<panel name="filterPanel" title="Filters" hideIf="statusSelect &lt; 2" readonly="true">
				<field name="productFamily" grid-view="product-family-grid" form-view="product-family-form"/>
				<field name="productCategory" grid-view="product-category-grid" form-view="product-category-form"/>
				<field name="product" grid-view="product-grid" form-view="product-form"/>
				<field name="excludeOutOfStock" colSpan="3"/>
				<field name="includeObsolete" colSpan="3"/>
				<panel name="racksRangePanel" title="Racks range">
					<field name="fromRack"/>
					<field name="toRack"/>
				</panel>
			</panel>
    </panel-tabs>

    <panel name="fillInventoryPanel" title="Fill inventory with" hideIf="statusSelect >= 2 || stockLocation == null">
			<field name="productFamily" canEdit="false" grid-view="product-family-grid" form-view="product-family-form"/>
			<field name="productCategory" canEdit="false" grid-view="product-category-grid" form-view="product-category-form"/>
			<field name="product" canEdit="false" onSelect="action-inventory-on-select-product" onChange="action-inventory-on-change-product" grid-view="product-grid" form-view="product-form"/>
      <field name="excludeOutOfStock" colSpan="3"/>
      <field name="includeObsolete" colSpan="3"/>
      <panel name="racksRangePanel" title="Racks range">
		    <field name="fromRack"/>
		    <field name="toRack"/>
      </panel>
      <button name="fillInventoryBtn" title="Fill inventory" showIf="stockLocation != null" onClick="action-group-stock-inventory-fillinventoryBtn-click" colSpan="3"/>
    </panel>
  </form>
    
    <calendar name="inventory-calendar" model="com.axelor.apps.stock.db.Inventory" colorBy="statusSelect"  eventStart="plannedStartDateT" eventStop="plannedEndDateT"  title="Inventory Calendar">
		<field name="inventoryTitle"/>
    </calendar>

    <action-group name="action-group-stock-inventory-importfile-click">
    	<action name="save"/>
    	<action name="action-set-inventory-seq"/>
    	<action name="action-inventory-method-import-file"/>
    </action-group>
    
    <action-group name="action-group-stock-inventory-fillinventoryBtn-click">
    	<action name="save"/>
    	<action name="action-inventory-method-fill-inventory"/>
    </action-group>
    
    <action-group name="action-group-stock-inventory-plan-click">
    	<action name="save"/>
    	<action name="action-inventory-record-plan"/>
    	<action name="action-set-inventory-seq"/>    	
    	<action name="save"/>
    </action-group>

    <action-group name="action-group-stock-inventory-start-click">
    	<action name="save"/>
    	<action name="action-inventory-record-start"/>
    	<action name="save"/>
    </action-group>

    <action-group name="action-group-stock-inventory-complete-click">
    	<action name="save"/>
    	<action name="action-inventory-validate-inventory-line-list"/>
    	<action name="action-inventory-record-complete"/>
    	<action name="save"/>
    </action-group>

    <action-group name="action-group-stock-inventory-validate-click">
    	<action name="save"/>
    	<action name="action-inventory-method-realize-stock-move"/>
    </action-group>

    <action-group name="action-group-stock-inventory-cancel-click">
    	<action name="save"/>
    	<action name="action-inventory-method-cancel"/>
    </action-group>

    <action-group name="action-group-stock-inventory-draft-click">
    	<action name="save"/>
    	<action name="action-inventory-record-draft"/>    	
    	<action name="save"/>
    </action-group>

    <action-record name="action-inventory-record-new" model="com.axelor.apps.stock.db.Inventory">
	    <field name="statusSelect" expr="1"/>
	    <field name="formatSelect" expr="pdf"/>
	    <field name="stockLocation" expr="eval:_location"/>
	    <field name="company" expr="eval: _location != null ? __repo__(StockLocation).find(_location.id).company : null"/>
	</action-record>
	
	<action-record name="action-inventory-record-draft" model="com.axelor.apps.stock.db.Inventory">
	    <field name="statusSelect" expr="1"/>
	    <field name="validatedBy" expr="eval: null"/>
	    <field name="validatedOn" expr="eval: null"/>
	</action-record>
	
	<action-record name="action-inventory-record-plan" model="com.axelor.apps.stock.db.Inventory">
	    <field name="statusSelect" expr="2"/>
	</action-record>

	<action-record name="action-inventory-record-start" model="com.axelor.apps.stock.db.Inventory">
	    <field name="statusSelect" expr="3"/>
	</action-record>
	
	<action-record name="action-inventory-record-complete" model="com.axelor.apps.stock.db.Inventory">
	    <field name="statusSelect" expr="4"/>
	    <field name="completedBy" expr="eval:__user__"/>
	</action-record>

	<action-method name="action-inventory-method-show-inventory">
		<call class="com.axelor.apps.stock.web.InventoryController" method="showInventory"/>
	</action-method>

	<action-method name="action-inventory-method-export-inventory">
		<call class="com.axelor.apps.stock.web.InventoryController" method="exportInventory"/>
	</action-method>
	
	<action-method name="action-inventory-method-import-file">
		<call class="com.axelor.apps.stock.web.InventoryController" method="importFile"/>
	</action-method>
	
	<action-method name="action-inventory-method-realize-stock-move">
		<call class="com.axelor.apps.stock.web.InventoryController" method="validateInventory"/>
	</action-method>
	
	<action-method name="action-inventory-method-cancel">
		<call class="com.axelor.apps.stock.web.InventoryController" method="cancel"/>
	</action-method>
        
    <action-method name="action-inventory-method-fill-inventory">
		<call class="com.axelor.apps.stock.web.InventoryController" method="fillInventoryLineList"/>
	</action-method>
	
	<action-method name="action-set-inventory-seq" >
		<call class="com.axelor.apps.stock.web.InventoryController" method="setInventorySequence"/>
	</action-method>
	
	<action-attrs  name="action-inventory-attrs-set-location-domain">
  		<attribute name="domain" for="stockLocation" expr="eval:&quot;(self.typeSelect = 1 OR self.typeSelect = 2) AND self.company.id = $company.id &quot;" />
	</action-attrs>

	<action-attrs name="action-inventory-on-change-product">
		<attribute name="value" for="productFamily" expr="eval:__repo__(ProductFamily).find(product.productFamily.id)" if="productFamily == null &amp;&amp; product.productFamily != null"/>    		
		<attribute name="value" for="productCategory" expr="eval:__repo__(ProductCategory).find(product.productCategory.id)" if="productCategory == null &amp;&amp; product.productCategory != null"/>    
	</action-attrs>
	
	<action-attrs name="action-inventory-on-select-product">
    <attribute name="domain" for="product" expr="eval:&quot;self.productCategory.id = ${productCategory.id} AND self.inventoryTypeSelect = :typeSelect&quot;" if="productCategory != null &amp;&amp; typeSelect == 2"/>
		<attribute name="domain" for="product" expr="eval:&quot;self.productCategory.id = ${productCategory.id}&quot;" if="productCategory != null &amp;&amp; typeSelect != 2"/>
		<attribute name="domain" for="product" expr="eval:&quot;self.productFamily.id = ${productFamily.id} AND self.inventoryTypeSelect = :typeSelect&quot;" if="productCategory == null &amp;&amp; productFamily != null &amp;&amp; typeSelect == 2"/>
		<attribute name="domain" for="product" expr="eval:&quot;self.productFamily.id = ${productFamily.id}&quot;" if="productCategory == null &amp;&amp; productFamily != null &amp;&amp; typeSelect != 2"/>
		<attribute name="domain" for="product" expr="eval:&quot;self.inventoryTypeSelect = :typeSelect&quot;" if="productCategory == null &amp;&amp; productFamily == null &amp;&amp; typeSelect == 2"/>
    <attribute name="domain" for="product" expr="eval:null" if="productCategory == null &amp;&amp; productFamily == null &amp;&amp; typeSelect != 2"/>
	</action-attrs>
	
	<action-method name="action-inventory-method-show-stockmoves">
		<call class="com.axelor.apps.stock.web.InventoryController" method="showStockMoves"/>
	</action-method>

    <search-filters name="inventories-filters" model="com.axelor.apps.stock.db.Inventory" title="Inventories filters">
		<filter title="Planned Inventories">
			<domain>self.statusSelect = 1</domain>
		</filter>
		<filter title="Inventories History">
			<domain>self.statusSelect = 2</domain>
		</filter>
	</search-filters>
	
	<action-attrs name="action-inventory-attrs-reset-stock-location">
		<attribute name="value" for="stockLocation" expr="eval: null"/>
	</action-attrs>
	
	<action-validate name="action-inventory-validate-inventory-line-list">
		<error message="Please fill all inventory lines" if="inventoryLineList.any { it?.realQty == null }"/>
	</action-validate>
	
</object-views>
