<?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="mrp-grid" model="com.axelor.apps.supplychain.db.Mrp" title="Mrp">
        <hilite if="statusSelect == 1" strong="true" color="warning"/>
        <hilite if="statusSelect == 2" strong="true" color="success"/>
        <field name="mrpSeq"/>
        <field name="createdOn"/>
        <field name="createdBy"/>
        <field name="stockLocation" form-view="stock-location-form" grid-view="stock-location-grid" colSpan="6"/>
        <field name="stockLocation.company"/>
        <field name="endDate" colSpan="6"/>
        <field name="statusSelect"/>
	 </grid>
    
	<form name="mrp-form" model="com.axelor.apps.supplychain.db.Mrp" title="Mrp" width="large"
		onLoad="action-attrs-mrp-generate-all-proposals">
	    <toolbar>
	        <button name="runCalculationBtn" title="Run calculation" onClick="save,action-method-mrp-run-calculation" showIf="statusSelect != 1" prompt="Are you sure you want run the calculation ? Calculation can take a long time."/>
   	        <button name="generateAllProposalsBtn" title="Generate all proposals" onClick="save,action-mrp-view-generate-proposal-wizard" showIf="statusSelect == 2" prompt="Are you sure you want generate all proposals ?"/>
            <button name="printWeeksBtn" title="Print (weekly breakdown)" icon="fa-print" onClick="save,action-method-mrp-print-weeks" showIf="statusSelect == 2"/>
            <button name="printListBtn" title="Print (list)" icon="fa-print" onClick="save,action-method-mrp-print-list" showIf="statusSelect == 2"/>
	    </toolbar>
	    <panel name="infoPanel">
			<field name="mrpSeq" showTitle="false" colSpan="12">
				<viewer depends="mrpSeq"><![CDATA[
					<h3>
						<span ng-show="record.mrpSeq" x-translate>MRP</span>
						{{record.mrpSeq}}
					</h3>
				]]></viewer>
			</field>
			<field name="createdOn" colSpan="4"/>
			<field name="createdBy" colSpan="4"/>
			<field name="endDate" colSpan="4"/>
			<field name="stockLocation" form-view="stock-location-form" grid-view="stock-location-grid" colSpan="4" domain="self.typeSelect != 3" canEdit="false"/>
			<field name="stockLocation.company" colSpan="4"/>
	    </panel>
	    <panel name="filtersPanel" title="Filters" collapseIf="statusSelect != 0" colSpan="12">
			<field name="id"/><spacer/>
	        <field name="createdOn" colSpan="4"/>
	        <field name="startDateTime" colSpan="4" readonly="true"/>
	        <field name="endDateTime" colSpan="4" readonly="true"/>
	        <field name="createdBy"/>
	        <field name="endDate" colSpan="6"/>
	        <field name="stockLocation" form-view="stock-location-form" grid-view="stock-location-grid" colSpan="6" domain="self.typeSelect != 3 AND (self.isNotInMrp = false OR self.isNotInMrp = null)" canEdit="false"/>
	        <field name="stockLocation.company"/>
	        <field name="displayProductWithoutProposal" widget="boolean-switch"/>
	        <panel-related name="productSetPanel" field="productSet" canEdit="false" form-view="product-form" grid-view="product-grid" canNew="false" colSpan="12" domain="self.productTypeSelect = 'storable' AND self.excludeFromMrp = false AND self.stockManaged = true"/>
	        <panel-related name="productCategorySetPanel" field="productCategorySet" canEdit="false" form-view="product-category-form" grid-view="product-category-grid" canNew="false" colSpan="12"/>
	        <panel-related name="productFamilySetPanel" field="productFamilySet" canEdit="false" form-view="product-family-form" grid-view="product-family-grid" canNew="false" colSpan="12"/>
	        <panel-related name="saleOrderLineSetPanel" field="saleOrderLineSet" canEdit="false" form-view="sale-order-line-menu-form" grid-view="sale-order-line-menu-grid" canNew="false" colSpan="12" onSelect="action-mrp-attrs-sale-order-domain"/>
	        <panel-related name="mrpForecastSetPanel" field="mrpForecastSet" form-view="mrp-forecast-form" grid-view="mrp-forecast-grid" colSpan="12" domain="self.statusSelect = 2 AND self.product.productTypeSelect = 'storable' AND self.product.excludeFromMrp = false AND self.forecastDate &gt;= :__date__  AND self.product.stockManaged = true"/>
	    </panel>
	    <panel name="statusPanel" showIf="statusSelect == 1" colSpan="12">
	        <field name="statusSelect" hidden="true"/>
      	    <label name="calculationInProgressLabel" title="Calculation in progress" colSpan="12"/>
	    </panel>
        <panel-dashlet name="mrpLinePanel" action="action-mrp-view-mrp-line" colSpan="12" showIf="statusSelect == 2" canSearch="true" height="800"/>
	 </form>
	 
	 
	 <form name="mrp-generate-proposal-wizard-form" onNew="action-mrp-wizard-attrs-set-default-onnew" model="com.axelor.apps.base.db.Wizard" title="Mrp" >
		 <panel name="wizardPanel">
		 	<field name="$consolidateProposalsPerSupplier" title="Consolidate the proposals per supplier" type="boolean" widget="boolean-switch" />
	 		<button name="generateProposalsBtn" title="Generate all proposals" onClick="action-method-mrp-generate-all-proposal,close"/>
		 </panel>
	 </form>
	 
	<action-method name="action-method-mrp-run-calculation">
	    <call class="com.axelor.apps.supplychain.web.MrpController" method="runCalculation"/>
	</action-method>
	
	<action-method name="action-method-mrp-generate-all-proposal">
	    <call class="com.axelor.apps.supplychain.web.MrpController" method="generateAllProposals"/>
	</action-method>

	<action-method name="action-method-mrp-print-weeks">
		<call class="com.axelor.apps.supplychain.web.MrpController" method="printWeeks"/>
	</action-method>

	<action-method name="action-method-mrp-print-list">
		<call class="com.axelor.apps.supplychain.web.MrpController" method="printList"/>
	</action-method>
	 
	<action-view name="action-mrp-view-mrp-line" title="Results" model="com.axelor.apps.supplychain.db.MrpLine">
	    <view type="grid" name="mrp-line-grid"/>
	    <view type="form" name="mrp-line-form"/>
		<view-param name="limit" value="200"/>
		<domain>
			(self.mrp.displayProductWithoutProposal = true AND self.mrp.id = :_mrpId)
				OR
			(self.mrp.displayProductWithoutProposal = false AND self.product.id IN (select m.product from MrpLine as m where m.mrp.id = :_mrpId AND m.mrpLineType.elementSelect in (:_proposalElementSelect)))
		</domain>
		<context name="_mrpId" expr="eval: __this__.id"/>
		<context name="_proposalElementSelect" expr="eval: [__repo__(MrpLineType).ELEMENT_PURCHASE_PROPOSAL,__repo__(MrpLineType).ELEMENT_MANUFACTURING_PROPOSAL,__repo__(MrpLineType).ELEMENT_MANUFACTURING_PROPOSAL_NEED]"/>
	</action-view>

	<action-view name="action-mrp-view-generate-proposal-wizard" title="Mrp" model="com.axelor.apps.base.db.Wizard">
		<view type="form" name="mrp-generate-proposal-wizard-form"/>
		<view-param name="popup" value="reload"/>
	    <view-param name="show-confirm" value="false" />
	    <view-param name="show-toolbar" value="false" />
   	  	<view-param name="popup-save" value="false"/>
	</action-view>
	
	<action-attrs name="action-mrp-wizard-attrs-set-default-onnew">
		<attribute name="value" for="$consolidateProposalsPerSupplier" expr="eval:true"/>
	</action-attrs>
	
	<action-attrs name="action-attrs-mrp-generate-all-proposals"
		model="com.axelor.apps.supplychain.db.Mrp">
		<attribute name="readonly" for="generateAllProposalsBtn"
			expr="eval: !(__repo__(MrpLine).all().filter(&quot; self.mrp.id= ? AND self.proposalGenerated is false AND self.mrpLineType.elementSelect in (5,8) &quot;,__this__.id).count() > 0)"/>
	</action-attrs>
	
	<action-attrs name="action-mrp-attrs-sale-order-domain">
  		<attribute name="domain" for="saleOrderLineSet" expr="eval: &quot;self.product.productTypeSelect = 'storable' AND self.product.excludeFromMrp = false AND self.product.stockManaged = true AND self.deliveryState != 3 AND self.saleOrder.company.id = ${stockLocation?.company?.id} AND self.saleOrder.stockLocation is not null&quot;"/>
	</action-attrs>
	
</object-views>


