<?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 model="com.axelor.apps.supplychain.db.DeclarationOfExchanges" title="Declarations of exchanges" name="declaration-of-exchanges-grid"
    orderBy="createdOn" onNew="action-declaration-of-exchanges-onnew">
    <field name="company" canEdit="false" if="__config__.app.getApp('base').getEnableMultiCompany()"/>
    <field name="country" domain="self IN (SELECT sl.address.addressL7Country FROM StockLocation sl WHERE sl.company = :company)"
      canEdit="false"/>
    <field name="fiscalYear" domain="self.company = :company AND self.typeSelect = :YEAR_TYPE_FISCAL"
      onChange="action-declaration-of-exchanges-fiscal-year-onchange" canEdit="false"/>
    <field name="period" domain="self.year = :fiscalYear OR :fiscalYear IS NULL AND self.year.company = :company AND self.year.typeSelect = :YEAR_TYPE_FISCAL"
      onChange="action-declaration-of-exchanges-period-onchange" canEdit="false"/>
    <field name="productTypeSelect" onChange="action-declaration-of-exchanges-product-type-select-onchange"/>
    <field name="stockMoveTypeSelect" selection-in="[STOCK_MOVE_TYPE_OUTGOING, STOCK_MOVE_TYPE_INCOMING * (productTypeSelect != 'service')]"
      readonlyIf="productTypeSelect == 'service'"/>
    <field name="formatSelect" selection-in="['pdf', 'csv']"/>
    <field name="createdOn"/>
  </grid>

  <form model="com.axelor.apps.supplychain.db.DeclarationOfExchanges" title="Declaration of exchanges"
    name="declaration-of-exchanges-form" onNew="action-declaration-of-exchanges-onnew" width="large">
    <toolbar>
      <button name="printBtn" title="Print" icon="fa-print" onClick="action-declaration-of-exchanges-export"/>
    </toolbar>

    <panel name="mainPanel">
      <field name="company" canEdit="false"/>
      <field name="country"
        domain="self IN (SELECT sl.address.addressL7Country FROM StockLocation sl WHERE sl.company = :company)"
        canEdit="false"/>
      <panel name="periodPanel" colSpan="12" showIf="company">
        <field name="fiscalYear" domain="self.company = :company AND self.typeSelect = :YEAR_TYPE_FISCAL"
          onChange="action-declaration-of-exchanges-fiscal-year-onchange" canEdit="false"/>
        <field name="period"
          domain="self.year = :fiscalYear OR :fiscalYear IS NULL AND self.year.company = :company AND self.year.typeSelect = :YEAR_TYPE_FISCAL"
          onChange="action-declaration-of-exchanges-period-onchange" canEdit="false"/>
      </panel>
      <field name="productTypeSelect" onChange="action-declaration-of-exchanges-product-type-select-onchange" selection-in="['service','storable']"/>
      <field name="stockMoveTypeSelect"
        selection-in="[STOCK_MOVE_TYPE_OUTGOING, STOCK_MOVE_TYPE_INCOMING * (productTypeSelect != 'service')]"
        readonlyIf="productTypeSelect == 'service'"/>
      <field name="formatSelect" selection-in="['pdf', 'csv']"/>
      <field name="createdOn" showIf="createdOn"/>
    </panel>
    <panel-dashlet name="declarationOfExchangesPanel" action="action-stock-move-line-declaration-of-exchanges" readonly="true" canSearch="true" height="500"/>
  </form>

  <action-record name="action-declaration-of-exchanges-onnew" model="com.axelor.apps.supplychain.db.DeclarationOfExchanges">
    <field name="company" expr="eval: __user__.activeCompany"/>
    <field name="country"
      expr="eval: (__user__.activeCompany?.stockConfig?.pickupDefaultStockLocation?.address ?: __user__.activeCompany?.address)?.addressL7Country"/>
  </action-record>

  <action-record name="action-declaration-of-exchanges-fiscal-year-onchange" model="com.axelor.apps.supplychain.db.DeclarationOfExchanges">
    <field name="period" expr="eval: null" if="period?.year != fiscalYear"/>
  </action-record>

  <action-record name="action-declaration-of-exchanges-period-onchange" model="com.axelor.apps.supplychain.db.DeclarationOfExchanges">
    <field name="fiscalYear" expr="eval: period.year" if="!fiscalYear"/>
  </action-record>

  <action-record name="action-declaration-of-exchanges-product-type-select-onchange"
    model="com.axelor.apps.supplychain.db.DeclarationOfExchanges">
    <field name="stockMoveTypeSelect" expr="eval: STOCK_MOVE_TYPE_OUTGOING" if="productTypeSelect == 'service'"/>
  </action-record>

  <action-group name="action-declaration-of-exchanges-export">
    <action name="save"/>
    <action name="action-declaration-of-exchanges-method-export"/>
  </action-group>

  <action-method name="action-declaration-of-exchanges-method-export">
    <call class="com.axelor.apps.supplychain.web.DeclarationOfExchangesController" method="export"/>
  </action-method>
  
  <action-view name="action-stock-move-line-declaration-of-exchanges" 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.stockMove.realDate BETWEEN :fromDate AND :toDate
        AND self.product.productTypeSelect = :productTypeSelect
        AND self.stockMove.typeSelect = :stockMoveTypeSelect
        AND self.stockMove.statusSelect = 3 
        AND self.stockMove.company = :company 
        AND (COALESCE(self.stockMove.fromAddress.addressL7Country, self.stockMove.fromStockLocation.address.addressL7Country) = :country
        OR COALESCE(self.stockMove.toAddress.addressL7Country, self.stockMove.toStockLocation.address.addressL7Country) = :country)
        AND COALESCE(self.stockMove.fromAddress.addressL7Country, self.stockMove.fromStockLocation.address.addressL7Country)
        != COALESCE(self.stockMove.toAddress.addressL7Country, self.stockMove.toStockLocation.address.addressL7Country)
        AND COALESCE(self.stockMove.fromAddress.addressL7Country.economicArea, self.stockMove.fromStockLocation.address.addressL7Country.economicArea)
        = COALESCE(self.stockMove.toAddress.addressL7Country.economicArea, self.stockMove.toStockLocation.address.addressL7Country.economicArea)
		AND self.product.usedInDEB = true
      </domain>
      <context name="fromDate" expr="eval: period?.fromDate"/>
      <context name="toDate" expr="eval: period?.toDate"/>
      <context name="productTypeSelect" expr="eval: productTypeSelect"/>
      <context name="stockMoveTypeSelect" expr="eval: stockMoveTypeSelect"/>
      <context name="country" expr="eval: country"/>
      <context name="company" expr="eval: company"/>
    </action-view>

</object-views>
