<?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="partner-stock-settings-grid" title="Partner stock settings" editable="true" model="com.axelor.apps.stock.db.PartnerStockSettings" onNew="action-partner-stock-settings-record-set-default-company">
    <field name="company" form-view="company-form" grid-view="company-grid" onChange="action-partner-stock-settings-group-company-onchange"
      canEdit="false" if="__config__.app.getApp('base').getEnableMultiCompany()"/>
    <field name="defaultStockLocation" form-view="stock-location-form" grid-view="stock-location-grid"
      onSelect="action-partner-default-stock-location-form-domain"/>
    <field name="plannedStockMoveAutomaticMail"/>
    <field name="plannedStockMoveMessageTemplate" requiredIf="plannedStockMoveAutomaticMail" showIf="plannedStockMoveAutomaticMail"
      form-view="template-form" grid-view="template-grid" domain="self.metaModel.name='StockMove'"/>
    <field name="realStockMoveAutomaticMail"/>
    <field name="realStockMoveMessageTemplate" requiredIf="realStockMoveAutomaticMail" showIf="realStockMoveAutomaticMail"
      form-view="template-form" grid-view="template-grid" domain="self.metaModel.name='StockMove'"/>
    <field name="incoterm" canEdit="false" form-view="incoterm-form" grid-view="incoterm-grid"/>
    <field name="$stockConfig" type="many-to-one" target="com.axelor.apps.stock.db.StockConfig" hidden="true"/>
  </grid>

    <form name="partner-stock-settings-form"
          title="Partner stock settings"
          model="com.axelor.apps.stock.db.PartnerStockSettings" onNew="action-partner-stock-settings-record-set-default-company">
        <panel name="mainPanel">
            <field name="company" form-view="company-form"
                   grid-view="company-grid" onChange="action-partner-stock-settings-group-company-onchange"/>
            <field name="$stockConfig" type="many-to-one" target="com.axelor.apps.stock.db.StockConfig" hidden="true"/>
            <field name="defaultStockLocation" form-view="stock-location-form"
                   grid-view="stock-location-grid"
                   onSelect="action-partner-default-stock-location-form-domain"/>
            <panel name="plannedStockMovePanel" colSpan="12">
                <field name="plannedStockMoveAutomaticMail"/>
                <field name="plannedStockMoveMessageTemplate"
                  requiredIf="plannedStockMoveAutomaticMail"
                  showIf="plannedStockMoveAutomaticMail"
                  form-view="template-form" grid-view="template-grid" domain="self.metaModel.name='StockMove'"/>
            </panel>
            <panel name="realStockMovePanel" colSpan="12">
                <field name="realStockMoveAutomaticMail"/>
                <field name="realStockMoveMessageTemplate"
                  requiredIf="realStockMoveAutomaticMail"
                  showIf="realStockMoveAutomaticMail"
                  form-view="template-form" grid-view="template-grid" domain="self.metaModel.name='StockMove'"/>
            </panel>
            <field name="incoterm" canEdit="false" form-view="incoterm-form" grid-view="incoterm-grid"/>
        </panel>
    </form>

    <action-attrs name="action-partner-default-stock-location-form-domain"
                  model="com.axelor.apps.stock.db.PartnerStockSettings">
        <attribute for="defaultStockLocation" name="domain" expr="eval: &quot; self.company = :company AND (self.typeSelect = 1 OR (self.typeSelect = 2 AND self.partner.id = ${__parent__?.id})) AND (self.usableOnSaleOrder = true OR self.usableOnPurchaseOrder = true) &quot;"/>
    </action-attrs>
    
    <action-group name="action-partner-stock-settings-group-company-onchange">
    	<action name="action-partner-stock-settings-record-set-stock-config"/>
    	<action name="action-partner-stock-settings-record-default-value"/>
    </action-group>
    
    <action-record name="action-partner-stock-settings-record-set-stock-config" model="com.axelor.apps.stock.db.PartnerStockSettings">
    	<field name="$stockConfig" expr="eval: __repo__(StockConfig).all().filter('self.company = ?', company).fetchOne()"/>
    </action-record>
    
    <action-record name="action-partner-stock-settings-record-default-value" model="com.axelor.apps.stock.db.PartnerStockSettings">
    	<field name="plannedStockMoveAutomaticMail" expr="eval: null" if="$stockConfig==null"/>
    	<field name="plannedStockMoveAutomaticMail" expr="eval: $stockConfig?.plannedStockMoveAutomaticMail" if="$stockConfig!=null"/>
    	<field name="plannedStockMoveMessageTemplate" expr="eval: null" if="$stockConfig==null"/>
    	<field name="plannedStockMoveMessageTemplate" expr="eval: $stockConfig?.plannedStockMoveMessageTemplate" if="$stockConfig!=null"/>
    	<field name="realStockMoveAutomaticMail" expr="eval: null" if="$stockConfig==null"/>
    	<field name="realStockMoveAutomaticMail" expr="eval: $stockConfig?.realStockMoveAutomaticMail" if="$stockConfig!=null"/>
    	<field name="realStockMoveMessageTemplate" expr="eval: null" if="$stockConfig==null"/>
    	<field name="realStockMoveMessageTemplate" expr="eval: $stockConfig?.realStockMoveMessageTemplate" if="$stockConfig!=null"/>
    </action-record>

    <action-record name="action-partner-stock-settings-record-set-default-company" model="com.axelor.apps.stock.db.PartnerStockSettings">
		<field name="company" expr="eval:__repo__(Company).all().fetchOne()" if="!(__config__.app.getApp('base').getEnableMultiCompany())"/>
    </action-record>

</object-views>
