<?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="accepted-purchase-request-grid" title="All requests accepted" model="com.axelor.apps.purchase.db.PurchaseRequest" edit-icon="true">
		<toolbar>
			<button name="generatePoBtn" title="Generate PO" onClick="action-purchase-request-open-generate-po-wizard"/>
		</toolbar>
		<field name="company" if="__config__.app.getApp('base').getEnableMultiCompany()"/>
		<field  name="statusSelect"/>
	</grid>
    
	<grid name="sent-purchase-request-grid" title="All requests sent" model="com.axelor.apps.purchase.db.PurchaseRequest">
		<toolbar>
			<button name="acceptRequestBtn" title="Accept and generate PO" onClick="action-purchase-request-method-accept-request"/>
		</toolbar>
		<field name="company" if="__config__.app.getApp('base').getEnableMultiCompany()"/>
		<field  name="statusSelect"/>
	</grid>
    
	<grid name="my-current-cart-purchase-request-grid" title="My Purchase request" model="com.axelor.apps.purchase.db.PurchaseRequest">
		<toolbar>
			<button name="confirmMyCartBtn" title="Confirm my cart" onClick="action-purchase-request-method-confirm-cart"/>
		</toolbar>
		<field name="company" if="__config__.app.getApp('base').getEnableMultiCompany()"/>
		<field  name="statusSelect"/>
	</grid>
    
    <grid name="purchase-request-grid" title="Purchase request" model="com.axelor.apps.purchase.db.PurchaseRequest">
    	<toolbar>
    		<button name="generatePoBtn" title="Generate PO" onClick="action-purchase-request-open-generate-po-wizard" readonlyIf="!_ids"/>
    	</toolbar>
    	<hilite if="statusSelect == 2" color="info"/>
    	<hilite if="statusSelect == 3" color="primary"/>
    	<hilite if="statusSelect == 4 || statusSelect == 5 || statusSelect == 6" color="success"/>
    	<hilite if="statusSelect == 7" color="warning"  />
    	<hilite if="statusSelect == 8" color="danger"/>
        <field name="company"/>
    	<field name="purchaseRequestSeq"/>
        <field name="supplierUser"/>
        <field name="stockLocation"/>
        <field name="createdOn"/>
        <field name="createdBy"/>
        <field name="statusSelect"/>
    </grid>

	<form name="purchase-request-form" title="Purchase request"
		model="com.axelor.apps.purchase.db.PurchaseRequest" onNew="action-purchase-request-group-onnew"
		width="large">
		<panel name="statusSelectPanel" colSpan="12">
			<field name="statusSelect" readonly="true" colSpan="12" widget="nav-select"
				showTitle="false" />
			<field name="purchaseRequestSeq" showTitle="false" readonly="true">
				<viewer depends="purchaseRequestSeq"><![CDATA[
					<h3>
						<span ng-show="record.purchaseRequestSeq" x-translate>Purchase request</span>
						{{record.purchaseRequestSeq}}
					</h3>
				]]></viewer>
			</field>
		</panel>
		<panel name="mainPanel" readonlyIf="!$contains([1,2],statusSelect)">
			<field name="company" form-view="company-form" grid-view="company-grid" canEdit="false" onChange="action-purchase-request-group-company-onchange"/>
      <panel-related field="purchaseOrderSet" canEdit="false" canNew="false" colSpan="12"/>
			<field name="$product" hidden="true" />
		</panel>
		<panel-related field="purchaseRequestLineList" form-view="purchase-request-line-form" grid-view="purchase-request-line-grid"/>
		<panel-tabs name="descriptionPanelTab">
			<panel name="descriptionPanel" title="Description">
				<field name="description" colSpan="12" widget="html"
					showTitle="false" />
			</panel>
		</panel-tabs>

		<panel name="detailPanel" sidebar="true">
			<button name="requestBtn" title="Request" icon="fa-bullhorn"
				showIf="statusSelect == 1" onClick="save,action-purchase-request-set-status-requested,save" />
			<button name="acceptBtn" title="Accept" icon="fa-check" showIf="statusSelect == 2"
				onClick="save,action-purchase-request-set-status-accepted,save" />
			<button name="purchaseBtn" title="Purchase" icon="fa-shopping-cart"
				showIf="statusSelect == 3" onClick="action-purchase-request-group-update-status" />
			<button name="deliverPartiallyBtn" title="Deliver Partially"
				icon="fa-truck" showIf="statusSelect == 4"
				onClick="save,action-purchase-request-set-status-delivered-partially,save" />
			<button name="deliveredBtn" title="Deliver"
				showIf="statusSelect == 5 || statusSelect == 4" icon="fa-truck"
				onClick="save,action-purchase-request-set-status-delivered,save" />
			<button name="refuseBtn" title="Refuse" icon="fa-times" css="btn-danger"
				showIf="statusSelect == 2" onClick="save,action-purchase-request-set-status-refused,save" />
			<button name="cancelBtn" title="Cancel" icon="fa-times-circle"
				css="btn-danger"
				onClick="save,action-purchase-request-set-status-cancel,save" hideIf="statusSelect == 8"/>
			<button name="draftBtn" title="Draft" icon="fa-edit" showIf="statusSelect == 8"
				onClick="save,action-purchase-request-set-status-draft, save" />
		</panel>
		<panel sidebar="true" name="characteristicsPanel" title="Characteristics"
			canCollapse="true">
			<field name="stockLocation"  canEdit="false" domain="self.company = :company and self.typeSelect in (1,2) and self.usableOnPurchaseOrder = true" 
				if-module="axelor-supplychain" colSpan="12" form-view="stock-location-form" grid-view="stock-location-grid" if="__config__.app.isApp('supplychain')" />
			<field name="supplierUser" domain="self.isSupplier = true" colSpan="12" canEdit="false" form-view="partner-form" grid-view="partner-grid"/>
		</panel>
		<panel sidebar="true" name="follow-upPanel" title="Follow-up"
			canCollapse="true">
			<field name="purchaseOrder" showIf="purchaseOrder" colSpan="6" />
		</panel>
		<panel name="attrsPanel">
			<field name="attrs" colSpan="12" />
		</panel>
		<panel-mail name="mailPanel">
			<mail-messages limit="4" />
			<mail-followers />
		</panel-mail>
	</form>
    
    <form name="purchase-request-wizard-generate-po-form"  title="Generate PO" model="com.axelor.apps.purchase.db.PurchaseRequest" >
    	<panel name="mainPanel">
    		<field name="$groupBySupplier" title="Group by supplier" type="boolean"/>
    		<spacer name="supplierSpacer"/>
    		<field name="$groupByProduct" title="Group by product" type="boolean" onChange="action-purchase-request-set-group-by-supplier"/>
    		<spacer name="productSpacer"/>
    		<button name="generatePOBtn" title="Generate PO" onClick="action-purchase-request-method-generate-po"/>
    	</panel>
    </form>

    <action-attrs name="action-purchase-request-set-group-by-supplier">
    	<attribute name="value" for="$groupBySupplier" expr="eval: groupByProduct"/>
    	<attribute name="readonly" for="$groupBySupplier" expr="eval: groupByProduct"/>
    </action-attrs>
	
	<action-record name="action-purchase-request-record-default" model="com.axelor.apps.purchase.db.PurchaseRequest">
		<field name="company" expr="eval:__user__.activeCompany"/>
		<field name="supplierUser" expr="eval:__repo__(Product).find(_product?.id)?.getDefaultSupplierPartner()" if="_product"/>
	</action-record>

	<!-- update status -->
	<action-record name="action-purchase-request-set-status-requested" model="com.axelor.apps.purchase.db.PurchaseRequest">
    	<field name="statusSelect" expr="eval:2"/>
  	</action-record>
  	
  	<action-record name="action-purchase-request-set-status-accepted" model="com.axelor.apps.purchase.db.PurchaseRequest">
    	<field name="statusSelect" expr="eval:3"/>
  	</action-record>
  	
  	<action-record name="action-purchase-request-set-status-purchased" model="com.axelor.apps.purchase.db.PurchaseRequest">
    	<field name="statusSelect" expr="eval:4"/>
  	</action-record>
  	
  	<action-record name="action-purchase-request-set-status-delivered-partially" model="com.axelor.apps.purchase.db.PurchaseRequest">
    	<field name="statusSelect" expr="eval:5"/>
  	</action-record>
	
	<action-record name="action-purchase-request-set-status-delivered" model="com.axelor.apps.purchase.db.PurchaseRequest">
    	<field name="statusSelect" expr="eval:6"/>
  	</action-record>
  	
  	<action-record name="action-purchase-request-set-status-refused" model="com.axelor.apps.purchase.db.PurchaseRequest">
    	<field name="statusSelect" expr="eval:7"/>
  	</action-record>
  	
  	<action-record name="action-purchase-request-set-status-cancel" model="com.axelor.apps.purchase.db.PurchaseRequest">
    	<field name="statusSelect" expr="eval:8"/>
  	</action-record>

  	<action-record name="action-purchase-request-set-status-draft" model="com.axelor.apps.purchase.db.PurchaseRequest">
		<field name="statusSelect" expr="eval:1"/>
  	</action-record>
  	<!-- update status end -->
	
	<action-method name="action-purchase-request-method-confirm-cart">
		<call class="com.axelor.apps.purchase.web.PurchaseRequestController" method="confirmCart"/>
	</action-method>
	
	<action-method name="action-purchase-request-method-accept-request">
		<call class="com.axelor.apps.purchase.web.PurchaseRequestController" method="acceptRequest"/>
	</action-method>
	
	<action-method name="action-purchase-request-method-generate-po">
		<call class="com.axelor.apps.purchase.web.PurchaseRequestController" method="generatePo"/>
	</action-method>
	
	<action-view name="action-purchase-request-open-generate-po-wizard" title="Generate PO" model="com.axelor.apps.purchase.db.PurchaseRequest">
		<view type="form" name="purchase-request-wizard-generate-po-form"/>
		<view-param name="popup" value="true"/>
		<view-param name="show-toolbar" value="false"/>
		<view-param name="show-confirm" value="false" />
		<view-param name="popup-save" value="false" />
		<context name="_ids" expr="eval: _ids"/>
	</action-view>
	
	<action-group name="action-purchase-request-group-update-status">
		<action name="save"/>
		<action name="action-purchase-request-validate-check-supplier-user" if="!supplierUser"/>
		<action name="action-purchase-request-set-status-purchased" if="supplierUser"/>
		<action name="save"/>
	</action-group>

	<action-group name="action-purchase-request-group-onnew">
		<action name="action-purchase-request-record-default"/>
		<action name="action-purchase-request-method-get-stock-location" if="__config__.app.isApp('supplychain')"/>
	</action-group>

	<action-group name="action-purchase-request-group-company-onchange">
		<action name="action-purchase-request-method-get-stock-location" if="__config__.app.isApp('supplychain')"/>
	</action-group>

	<action-validate name="action-purchase-request-validate-check-supplier-user">
		<error message="Supplier is required to generate a purchase order." />
	</action-validate>
	
</object-views>