<?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="supplychain-batch-grid" title="Supplychain batches" model="com.axelor.apps.supplychain.db.SupplychainBatch">
    <field name="actionSelect" if="!__config__.app.getApp('supplychain').getGenerateInvoiceFromStockMove()" />
		<field name="actionSelect" selection="supplychain.batch.invoice.stock.moves.action.select" if="__config__.app.getApp('supplychain').getGenerateInvoiceFromStockMove()"/>
    <field name="code" x-bind="{{code|unaccent|uppercase}}" />
		<field name="company" form-view="company-form" grid-view="company-grid" if="__config__.app.getApp('base').getEnableMultiCompany()"/>
		<field name="createdOn"/>
		<field name="createdBy" form-view="user-form" grid-view="user-grid"/>
  </grid>

	<form name="supplychain-batch-form" title="Supplychain Batch"
		model="com.axelor.apps.supplychain.db.SupplychainBatch" onNew="action-supplychain-batch-record-on-new" onLoad="action-group-supplychain-batch-group-onload">
		<panel name="mainPanel" >
			<field name="actionSelect" if="!__config__.app.getApp('supplychain').getGenerateInvoiceFromStockMove()"/>
			<field name="actionSelect" selection="supplychain.batch.invoice.stock.moves.action.select" if="__config__.app.getApp('supplychain').getGenerateInvoiceFromStockMove()"/>
			<field name="code" x-bind="{{code|unaccent|uppercase}}" onChange="action-base-batch-condition-check-unique-code"/>
			<field name="company" canEdit="false" widget="SuggestBox" form-view="company-form" grid-view="company-grid"/>
			<field name="archived" title="Archived"/>
		</panel>
		<panel-tabs name="mainPanelTab">
			<panel name="invoiceOrdersConfigurationPanel" title="Invoice orders configuration" showIf="actionSelect == 3">
				<field name="invoiceOrdersTypeSelect" if="!__config__.app.getApp('supplychain').getGenerateInvoiceFromSaleOrder()" />
				<field name="invoiceOrdersTypeSelect" selection="supplychain.supplychain.batch.sale.orders.invoice.orders.type.select" if="__config__.app.getApp('supplychain').getGenerateInvoiceFromSaleOrder()" />
				<panel name="subInvoiceOrderConfig1Panel" showIf="invoiceOrdersTypeSelect == 1" colSpan="12">
					<field name="salespersonOrBuyerSet" widget="TagSelect" title="Salespersons" />
					<field name="team" />
					<field name="orderUpToDate" />
					<field name="deliveryOrReceiptState" widget="MultiSelect" selection="sale.order.delivery.state" title="Delivery state" />
					<field name="statusSelect" widget="MultiSelect" selection="sale.order.status.select" readonly="true" />
				</panel>
				<panel name="subInvoiceOrderConfig2Panel" showIf="invoiceOrdersTypeSelect == 2" colSpan="12">
					<field name="salespersonOrBuyerSet" widget="TagSelect" title="Buyers" />
					<field name="team" />
					<field name="orderUpToDate" />
					<field name="deliveryOrReceiptState" widget="MultiSelect" selection="purchase.order.receipt.state" title="Receipt state" />
					<field name="statusSelect" widget="MultiSelect" selection="purchase.purchase.order.status.select" readonly="true" />
				</panel>
			</panel>
			<panel name="accountingCutOffConfigPanel" title="Accounting cut-off configuration" showIf="actionSelect == 1" colSpan="12">
				<field name="accountingCutOffTypeSelect"/>
				<field name="moveDescription" requiredIf="actionSelect == 1"/>
				<panel colSpan="6" itemSpan="12">
					<field name="moveDate" requiredIf="actionSelect == 1" onChange="action-supplychain-batch-attrs-show-label"/>
					<label name="adviseUser" title="This batch has already been running this month" hidden="true" css="label-danger" />
				</panel>
				<panel colSpan="6" itemSpan="12">
					<field name="reverseMoveDate" requiredIf="actionSelect == 1"/>
				</panel>
				<field name="recoveredTax"/>
				<field name="ati" showIf="!recoveredTax"/>
				<field name="includeNotStockManagedProduct"/>
			</panel>	
			<panel name="informationPanel" title="Information"  >
				<field name="createdOn" title="Created on"/>
				<field name="createdBy" title="Created by"  form-view="user-form" grid-view="user-grid"/>
				<field name="description" showTitle="false"  />
		      	<panel-related name="batchListPanel" field="batchList" colSpan="12" form-view="batch-form" grid-view="batch-grid" readonly="true"/>
			</panel>
		</panel-tabs>
		<panel sidebar="true" name="actionsPanel" title="Actions">
		    <button name="accountingCutOffBatchBtn" title="Run accounting cut-off" showIf="actionSelect == 1" onClick="save,action-supplychain-batch-group-accounting-cut-off" colSpan="12"/>
		    <button name="invoiceOutgoingStockMoveBatchBtn" title="Invoice outgoing stock moves" showIf="actionSelect == 2"  onClick="save,action-supplychain-batch-method-invoice-outgoing-stock-moves" colSpan="12" if="__config__.app.getApp('supplychain').getGenerateInvoiceFromStockMove()"/>
		    <button name="invoiceOrdersBatchBtn" title="Invoice orders" showIf="actionSelect == 3" onClick="save,action-supplychain-batch-method-invoice-orders" colSpan="12"/>
		</panel>
	</form>
	
	<!-- ACTION GROUP -->
	<action-group name="action-supplychain-batch-group-accounting-cut-off">
		<action name="action-supplychain-batch-validate-twice-in-a-month"/>
		<action name="action-supplychain-batch-method-accounting-cut-off"/>
	</action-group>
	
	<action-group name="action-group-supplychain-batch-group-onload">
		<action name="action-supplychain-batch-attrs-show-label"/>
	</action-group>
	

	<!-- ACTION ATTRS -->
	<action-attrs name="action-supplychain-batch-attrs-show-label">
		<attribute name="hidden" expr="eval: !(moveDate &amp;&amp; batchList &amp;&amp; batchList?.find{it.startDate.month == __date__.month &amp;&amp; it.startDate.year == __date__.year} != null)" for="adviseUser"/>
	</action-attrs>
	
	<!-- ACTION VALIDATE -->
	<action-validate name="action-supplychain-batch-validate-twice-in-a-month">
   		<alert message="The batch has already been running this month. Are you sure to continue ?" if="eval: moveDate &amp;&amp; batchList &amp;&amp; batchList?.find{it.startDate.month == __date__.month &amp;&amp; it.startDate.year == __date__.year} != null" />
   	</action-validate>
   	
    <!-- ACTION RECORD -->
    <action-record name="action-supplychain-batch-record-on-new" model="com.axelor.apps.base.db.MailBatch">
		<field name="createdOn" expr="eval:__config__.app.getTodayDate()"/>
		<field name="createdBy" expr="eval:__user__"/>
		<field name="company"  expr="eval:__user__.activeCompany" if="__user__.activeCompany != null"/>
    	<field name="company"  expr="eval:__repo__(Company).all().fetchOne()" if="__user__.activeCompany == null &amp;&amp; __repo__(Company).all().fetch().size == 1"/>
	</action-record>

    <!-- ACTION METHOD -->

	<action-method name="action-supplychain-batch-method-invoice-outgoing-stock-moves">
		<call class="com.axelor.apps.supplychain.web.SupplychainBatchController" method="invoiceOutgoingStockMoves"/>
	</action-method>

	<action-method name="action-supplychain-batch-method-invoice-orders">
		<call class="com.axelor.apps.supplychain.web.SupplychainBatchController" method="invoiceOrders"/>
	</action-method>
	
	<action-method name="action-supplychain-batch-method-accounting-cut-off">
		<call class="com.axelor.apps.supplychain.web.SupplychainBatchController" method="accountingCutOff"/>
	</action-method>


	<!-- BATCH -->
	
	<action-view name="action-batch-show-stock-move" title="Stock moves"
		model="com.axelor.apps.stock.db.StockMove">
		<view type="grid" name="stock-move-in-grid" if="supplychainBatch.accountingCutOffTypeSelect == 1"/>
		<view type="grid" name="stock-move-out-grid" if="supplychainBatch.accountingCutOffTypeSelect == 2"/>
		<view type="form" name="stock-move-form"/>
		<domain>:batch MEMBER OF self.batchSet</domain>
		<context name="batch" expr="eval: __self__"/>
	</action-view>

	<action-view name="action-batch-show-stock-move-lines" title="Stock move lines"
				 model="com.axelor.apps.stock.db.StockMoveLine">
		<view type="grid" name="stock-move-line-all-grid"/>
		<view type="form" name="stock-move-line-all-form"/>
		<domain>self.id IN :stockMovelineIdList</domain>
		<context name="stockMovelineIdList" expr="call:com.axelor.apps.supplychain.service.AccountingCutOffService:getStockMoveLines(__self__)"/>
	</action-view>
	
	

</object-views>