<?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-move-manuf-order-grid" title="Stock moves" model="com.axelor.apps.stock.db.StockMove" orderBy="-estimatedDate">
        <field name="stockMoveSeq"/>
        <field name="fromStockLocation" form-view="stock-location-form" grid-view="stock-location-grid"/>
        <field name="estimatedDate"/>
        <field name="realDate"/>
        <field name="fromAddressStr" hidden="true"/>
        <field name="toAddressStr"/>
		<field name="tradingName" form-view="trading-name-form" grid-view="trading-name-grid" if="__config__.app.getApp('base').enableTradingNamesManagement"/>
        <field name="partner" form-view="partner-form" grid-view="partner-grid"/>
        <field name="statusSelect"/>
        <field name="typeSelect" hidden="true" />
        <button name="printStockMoveBtn" title="Print" icon="fa-print" onClick="action-print-stock-move" readonlyIf="statusSelect == 1"/>
        <button name="viewDirectionBtn" icon="fa-location-arrow" onClick="action-stock-move-validate-address,action-stock-move-method-view-direction" readonlyIf="typeSelect != 2 || statusSelect == 1"/>
    </grid>

    <grid name="stock-move-sale-order-grid" title="Stock moves" model="com.axelor.apps.stock.db.StockMove" orderBy="-estimatedDate">
        <field name="stockMoveSeq"/>
        <field name="fromStockLocation" form-view="stock-location-form" grid-view="stock-location-grid"/>
        <field name="estimatedDate"/>
        <field name="realDate"/>
        <field name="fromAddressStr" hidden="true"/>
        <field name="tradingName" form-view="trading-name-form" grid-view="trading-name-grid" if="__config__.app.getApp('base').enableTradingNamesManagement"/>
        <field name="statusSelect"/>
        <field name="availableStatusSelect">
          <hilite strong="true" color="success" if="availableStatusSelect == 1"/>
          <hilite strong="true" color="warning" if="availableStatusSelect == 2"/>
          <hilite strong="true" color="danger" if="availableStatusSelect == 3"/>
        </field>
        <field name="availabilityRequest">
          <hilite strong="true" color="warning" if="statusSelect==2"/>
          <hilite strong="true" color="success" if="statusSelect==3"/>
        </field>
        <button name="printStockMoveBtn" title="Print" icon="fa-print" onClick="action-print-stock-move" readonlyIf="statusSelect == 1"/>
        <button name="viewDirectionBtn" icon="fa-location-arrow" onClick="action-stock-move-validate-address,action-stock-move-method-view-direction" readonlyIf="typeSelect != 2 || statusSelect == 1"/>
    </grid>

	<form name="stock-move-multi-sale-invoicing-form" model="com.axelor.apps.stock.db.StockMove" title="Mass Cust. Stock Move Invoicing" >
		<panel-related name="$customerStockMoveToInvoicePanel" field="$customerStockMoveToInvoice" title="Customer Stock Move to invoice" colSpan="12" height="25" type="many-to-many" target="com.axelor.apps.stock.db.StockMove" domain="self.statusSelect = 3 AND ((self.typeSelect = 2 AND self.isReversion = false) OR (self.typeSelect = 3 AND self.isReversion = true)) AND (self.id IN (SELECT stockMove.id FROM StockMoveLine GROUP BY stockMove.id HAVING SUM(qtyInvoiced) = 0))" canNew="false" canEdit="false" grid-view="stock-move-mass-invoicing-out-grid" form-view="stock-move-form"/>
		<panel sidebar="true" name="actionsPanel" title="Actions">
			<button name="generateInvoiceConcatStockMoveBtn" title="Generate single invoice" onClick="action-validate-supplychain-outgoing-stock-move-check-selection,action-supplychain-stock-move-method-generate-concat-cust-invoice-check-fields"/>
			<button name="generateMultiInvoiceBtn" title="Generate one invoice per outgoing stockMove" onClick="action-validate-supplychain-outgoing-stock-move-check-selection,action-supplychain-stock-move-method-generate-multi-customer-invoice"/>
	  	</panel>
    </form>
    
    <form name="stock-move-multi-purchase-invoicing-form" model="com.axelor.apps.stock.db.StockMove" title="Mass Suppl. Stock Move Invoicing" >
		<panel-related name="$supplierStockMoveToInvoicePanel" field="$supplierStockMoveToInvoice" title="Supplier Stock Move to invoice" colSpan="12" height="25" type="many-to-many" target="com.axelor.apps.stock.db.StockMove" domain="self.statusSelect = 3 AND ((self.typeSelect = 3 AND self.isReversion = false) OR (self.typeSelect = 2 AND self.isReversion = true)) AND (self.id IN (SELECT stockMove.id FROM StockMoveLine GROUP BY stockMove.id HAVING SUM(qtyInvoiced) = 0))" canNew="false" canEdit="false" grid-view="stock-move-mass-invoicing-in-grid" form-view="stock-move-form"/>
		<panel sidebar="true" name="actionsPanel" title="Actions">
			<button name="generateInvoiceConcatStockMoveBtn" title="Generate single invoice" onClick="action-validate-supplychain-incoming-stock-move-check-selection,action-supplychain-stock-move-method-generate-concat-suppl-invoice-check-fields"/>
			<button name="generateMultiInvoiceBtn" title="Generate one invoice per incoming stockMove" onClick="action-validate-supplychain-incoming-stock-move-check-selection,action-supplychain-stock-move-method-generate-multi-supplier-invoice"/>
	  	</panel>
    </form>
    
    <form name="stock-move-supplychain-concat-cust-invoice-confirm-form" model="com.axelor.apps.stock.db.StockMove" title="Confirmation" onNew="action-attrs-set-is-from-wizard-true,action-record-load-dummy-field-generate-invoice">
		<panel name="mainPanel" >
			<field name="$paymentCondition" title="Payment condition" target="com.axelor.apps.account.db.PaymentCondition" colSpan="12" width="200px" widget="SuggestBox" showIf="paymentConditionToCheck"/>
			<field name="$paymentMode" title="Payment mode" target="com.axelor.apps.account.db.PaymentMode" colSpan="12" width="200px" widget="SuggestBox" showIf="paymentModeToCheck"/>
			<field name="$contactPartner" title="Contact partner" target="com.axelor.apps.base.db.Partner" domain="self.isContact IS TRUE AND EXISTS (SELECT 1 FROM Partner part WHERE part.id = :partnerId AND self.id IN (SELECT id FROM part.contactPartnerSet))" colSpan="12" width="200px" widget="SuggestBox" showIf="contactPartnerToCheck"/>
			<button name="confirmBtn" title="Confirm" onClick="action-supplychain-stock-move-method-generate-concat-cust-invoice" colSpan="12" width="200px"/>
			<field name="$isFromWizard" hidden="true"/>
		</panel>
    </form>

	<form name="stock-move-supplychain-concat-suppl-invoice-confirm-form" model="com.axelor.apps.stock.db.StockMove" title="Confirmation" onNew="action-record-load-dummy-field-generate-invoice">
		<panel name="main" >
			<field name="$paymentCondition" title="Payment condition" target="com.axelor.apps.account.db.PaymentCondition" colSpan="12" width="200px" widget="SuggestBox" showIf="paymentConditionToCheck"/>
			<field name="$paymentMode" title="Payment mode" target="com.axelor.apps.account.db.PaymentMode" colSpan="12" width="200px" widget="SuggestBox" showIf="paymentModeToCheck"/>
			<field name="$contactPartner" title="Contact partner" target="com.axelor.apps.base.db.Partner" domain="self.isContact IS TRUE AND EXISTS (SELECT 1 FROM Partner part WHERE part.id = :partnerId AND self.id IN (SELECT id FROM part.contactPartnerSet))" colSpan="12" width="200px" widget="SuggestBox" showIf="contactPartnerToCheck"/>
			<button name="confirm" title="Confirm" onClick="action-supplychain-stock-move-confirm-concat-invoice-check, action-supplychain-stock-move-method-generate-concat-suppl-invoice" colSpan="12" width="200px"/>
		</panel>
	</form>

	<form name="stock-move-invoicing-wizard-form" title="Invoicing"
      model="com.axelor.apps.stock.db.StockMove"
      onNew="action-method-stock-move-invoicing-wizard-onnew">
        <panel name="operationSelectPanel">
            <field if="__repo__(StockMoveLine).all()?.filter('self.stockMove = ?1 AND self.qtyInvoiced > 0',Long.parseLong(_id.toString()))?.count() == 0" name="operationSelect" title="Operation choice" type="selection" required="true" selection="supplychain.stock.move.invoicing.wizard.select"/>
    		<field if="__repo__(StockMoveLine).all()?.filter('self.stockMove = ?1 AND self.qtyInvoiced > 0',Long.parseLong(_id.toString()))?.count() != 0" name="operationSelect" title="Operation choice" type="selection" required="true" selection="supplychain.stock.move.invoicing.wizard.select" selection-in="[2]"/>
        </panel>
        <panel-related name="stockMoveLineListPanel" field="$stockMoveLines" canNew="false" target="com.axelor.apps.stock.db.StockMoveLine" type="one-to-many" editable="true" showIf="operationSelect == 2">
          <field name="productCode"/>
          <field name="productName" readonly="true"/>
          <field name="realQty" readonly="true" aggregate="sum" title="Real Qty"/>
          <field name="qtyInvoiced" readonly="true" title="Invoiced Qty"/>
          <field name="remainingQty" readonly="true" aggregate="sum" title="Remaining Qty"/>
          <field name="qtyToInvoice" title="Qty to invoice" type="decimal" readonlyIf="invoiceAll || isSubline" min="0" validIf="$number(remainingQty) >= $number(qtyToInvoice)"/>
          <field name="invoiceAll" title="Invoice all" type="boolean" onChange="action-attrs-stock-move-line-invoicing-wizard-fill-qty"/>
          <field name="isSubline" hidden="true" />
          <field name="stockMoveLineId" hidden="true"/>
        </panel-related>
        <panel name="actionPanel" >
            <button name="createInvoiceBtn" title="Generate the invoice" onClick="action-supplychain-stock-move-method-generate-invoice" showIf="operationSelect &lt; 3"/>
	    </panel>
    </form>
    
    <action-method name="action-supplychain-stock-move-method-invoicing-wizard">
		<call class="com.axelor.apps.supplychain.web.StockMoveInvoiceController" method="openInvoicingWizard"/>
	</action-method>
	
	<action-method name="action-method-stock-move-invoicing-wizard-onnew">
      <call class="com.axelor.apps.supplychain.web.StockMoveInvoiceController" method="fillDefaultValueWizard"/>
    </action-method>

	<action-method name="action-supplychain-stock-move-method-generate-invoice">
		<call class="com.axelor.apps.supplychain.web.StockMoveInvoiceController" method="generateInvoice"/>
	</action-method>
	
	<action-method name="action-supplychain-stock-move-method-generate-multi-customer-invoice">
		<call class="com.axelor.apps.supplychain.web.StockMoveInvoiceController" method="generateMultiCustomerInvoice"/>
	</action-method>

	<action-method name="action-supplychain-stock-move-method-generate-multi-supplier-invoice">
		<call class="com.axelor.apps.supplychain.web.StockMoveInvoiceController" method="generateMultiSupplierInvoice"/>
	</action-method>

	<action-method name="action-supplychain-stock-move-method-generate-concat-cust-invoice">
		<call class="com.axelor.apps.supplychain.web.StockMoveInvoiceController" method="generateInvoiceConcatOutStockMove"/>
	</action-method>

	<action-method name="action-supplychain-stock-move-method-generate-concat-suppl-invoice">
		<call class="com.axelor.apps.supplychain.web.StockMoveInvoiceController" method="generateInvoiceConcatInStockMove"/>
	</action-method>

	<action-method name="action-supplychain-stock-move-method-generate-concat-cust-invoice-check-fields">
		<call class="com.axelor.apps.supplychain.web.StockMoveInvoiceController" method="generateInvoiceConcatOutStockMoveCheckMissingFields"/>
	</action-method>

	<action-method name="action-supplychain-stock-move-method-generate-concat-suppl-invoice-check-fields">
		<call class="com.axelor.apps.supplychain.web.StockMoveInvoiceController" method="generateInvoiceConcatInStockMoveCheckMissingFields"/>
	</action-method>
	
	<action-validate name="action-validate-supplychain-outgoing-stock-move-check-selection">
		<error message="You have to choose at least one outgoing stock move" if="customerStockMoveToInvoice == null || customerStockMoveToInvoice.size() == 0"/>
	</action-validate>
	
	<action-validate name="action-validate-supplychain-incoming-stock-move-check-selection">
		<error message="You have to choose at least one incoming stock move" if="supplierStockMoveToInvoice == null || supplierStockMoveToInvoice.size() == 0"/>
	</action-validate>

	<action-record name="action-record-load-dummy-field-generate-invoice" model="com.axelor.apps.stock.db.StockMove">
		<field name="paymentConditionToCheck" expr="eval: contextPaymentConditionToCheck"/>
		<field name="paymentModeToCheck" expr="eval: contextPaymentModeToCheck"/>
		<field name="contactPartnerToCheck" expr="eval: contextContactPartnerToCheck"/>
		<field name="partnerId" expr="eval: contextPartnerId"/>
	</action-record>
	
	<action-record name="action-stock-move-record-pfp-validate" model="com.axelor.apps.stock.db.StockMove">
		<field name="pfpValidateStatusSelect" expr="eval: 2"/>
	</action-record>

	<action-record name="action-stock-move-record-pfp-refusal-to-pay" model="com.axelor.apps.stock.db.StockMove">
		<field name="pfpValidateStatusSelect" expr="eval: 3"/>
	</action-record>

	<action-view name="action-stock-move-view-account-moves" title="Moves" model="com.axelor.apps.account.db.Move" if-module="axelor-supplychain">
		<view type="grid" name="move-grid"/>
		<view type="form" name="move-form"/>
		<domain>self.stockMove.id = :id</domain>
	</action-view>

	<action-attrs name="action-attrs-set-is-from-wizard-true">
		<attribute for="$isFromWizard" name="value" expr="eval:true"/>
	</action-attrs>
	
	<action-attrs name="action-attrs-stock-move-line-invoicing-wizard-fill-qty">
      <attribute for="qtyToInvoice" name="value" if="invoiceAll" expr="eval: remainingQty"/>
    </action-attrs>
    
    <action-record name="action-record-supply-chain-stock-move-onnew" model="com.axelor.apps.stock.db.StockMove">
      <field name="partner" expr="eval: __parent__?.partner" />
      <field name="company" expr="eval: __parent__?.company" />
      <field name="typeSelect" expr="3" if="eval: __parent__?.operationTypeSelect == 1 || __parent__?.operationTypeSelect == 2"/>
      <field name="typeSelect" expr="2" if="eval: __parent__?.operationTypeSelect == 3 || __parent__?.operationTypeSelect == 4"/>
    </action-record>
    
    <action-attrs name="action-attrs-supply-chain-stock-move-onnew" model="com.axelor.apps.stock.db.StockMove">
      <attribute name="readonly" for="partner" expr="true"/>
      <attribute name="readonly" for="typeSelect" expr="true"/>
      <attribute name="readonly" for="company" expr="true"/>
    </action-attrs>
    
    <action-record name="action-supplychain-stock-move-record-set-invoice-status" model="com.axelor.apps.stock.db.StockMove">
    	<field name="$invoiceStatus" expr="call: com.axelor.apps.supplychain.service.StockMoveInvoiceService:getStockMoveInvoiceStatus(__self__)"/>
    </action-record>

</object-views>