<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<service-eca xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="https://ofbiz.apache.org/dtds/service-eca.xsd">
    <!-- if a SALES_SHIPMENT is SHIPMENT_CANCELLED, cancel ItemIssuance and OrderShipment -->
    <eca service="updateShipment" event="commit">
        <condition-field field-name="statusId" operator="not-equals" to-field-name="oldStatusId"/>
        <condition field-name="shipmentTypeId" operator="equals" value="SALES_SHIPMENT"/>
        <condition field-name="statusId" operator="equals" value="SHIPMENT_CANCELLED"/>
        <action service="checkCancelItemIssuanceAndOrderShipmentFromShipment" mode="sync"/>
    </eca>
    <!-- if new statusId of a SALES_SHIPMENT is SHIPMENT_PICKED, create invoice -->
    <eca service="updateShipment" event="commit">
        <condition-field field-name="statusId" operator="not-equals" to-field-name="oldStatusId"/>
        <condition field-name="statusId" operator="equals" value="SHIPMENT_PICKED"/>
        <condition field-name="shipmentTypeId" operator="equals" value="SALES_SHIPMENT"/>
        <action service="createInvoicesFromShipment" mode="sync" run-as-user="system"/>
    </eca>

    <eca service="updateShipment" event="commit">
        <condition-field field-name="statusId" operator="not-equals" to-field-name="oldStatusId"/>
        <condition field-name="statusId" operator="equals" value="SHIPMENT_PACKED"/>
        <condition field-name="shipmentTypeId" operator="equals" value="SALES_SHIPMENT"/>
        <action service="createInvoicesFromShipment" mode="sync" run-as-user="system"/>
        <action service="setInvoicesToReadyFromShipment" mode="sync" run-as-user="system"/>
    </eca>

    <eca service="updateShipment" event="commit">
        <condition-field field-name="statusId" operator="not-equals" to-field-name="oldStatusId"/>
        <condition field-name="statusId" operator="equals" value="SHIPMENT_SHIPPED"/>
        <condition field-name="shipmentTypeId" operator="equals" value="SALES_SHIPMENT"/>
        <action service="sendShipmentCompleteNotification" mode="async" persist="true"/>
    </eca>

    <!-- if new statusId of a PURCHASE_SHIPMENT is PURCH_SHIP_RECEIVED, create invoice -->
    <eca service="updateShipment" event="commit">
        <condition-field field-name="statusId" operator="not-equals" to-field-name="oldStatusId"/>
        <condition field-name="statusId" operator="equals" value="PURCH_SHIP_RECEIVED"/>
        <condition field-name="shipmentTypeId" operator="equals" value="PURCHASE_SHIPMENT"/>
        <action service="balanceItemIssuancesForShipment" mode="sync"/>
        <action service="createInvoicesFromShipment" mode="sync" run-as-user="system"/>
    </eca>

    <!-- if new statusId of a DROP_SHIPMENT is PURCH_SHIP_SHIPPED, create purchase invoice -->
    <eca service="updateShipment" event="commit">
        <condition-field field-name="statusId" operator="not-equals" to-field-name="oldStatusId"/>
        <condition field-name="statusId" operator="equals" value="PURCH_SHIP_SHIPPED"/>
        <condition field-name="shipmentTypeId" operator="equals" value="DROP_SHIPMENT"/>
        <action service="createInvoicesFromShipment" mode="sync" run-as-user="system"/>
    </eca>

    <!-- if new statusId of a DROP_SHIPMENT is PURCH_SHIP_RECEIVED, create sales invoice -->
    <eca service="updateShipment" event="commit">
        <condition-field field-name="statusId" operator="not-equals" to-field-name="oldStatusId"/>
        <condition field-name="statusId" operator="equals" value="PURCH_SHIP_RECEIVED"/>
        <condition field-name="shipmentTypeId" operator="equals" value="DROP_SHIPMENT"/>
        <action service="createSalesInvoicesFromDropShipment" mode="sync" run-as-user="system"/>
    </eca>

    <!-- if new statusId of a SALES_RETURN is PURCH_SHIP_RECEIVED, create a return invoice.
         Note that PURCH_SHIP_RECEIVED now means any received shipment. We determine the
         type of shipment by the shipmentTypeId. -->
    <eca service="updateShipment" event="commit">
        <condition-field field-name="statusId" operator="not-equals" to-field-name="oldStatusId"/>
        <condition field-name="statusId" operator="equals" value="PURCH_SHIP_RECEIVED"/>
        <condition field-name="shipmentTypeId" operator="equals" value="SALES_RETURN"/>
        <action service="createInvoicesFromReturnShipment" mode="sync" run-as-user="system"/>
    </eca>

    <!-- if new status of a PURCHASE_RETURN is SHIPMENT_SHIPPED, create a return invoice -->
    <eca service="updateShipment" event="commit">
        <condition-field field-name="statusId" operator="not-equals" to-field-name="oldStatusId"/>
        <condition field-name="statusId" operator="equals" value="SHIPMENT_SHIPPED"/>
        <condition field-name="shipmentTypeId" operator="equals" value="PURCHASE_RETURN"/>
        <action service="createInvoicesFromReturnShipment" mode="sync" run-as-user="system"/>
    </eca>

    <!-- if new statusId is SHIPMENT_SCHEDULED, send notification -->
    <eca service="createShipment" event="commit">
        <condition field-name="statusId" operator="equals" value="SHIPMENT_SCHEDULED"/>
        <action service="sendShipmentScheduledNotification" mode="async"/>
    </eca>
    <eca service="updateShipment" event="commit">
        <condition-field field-name="statusId" operator="not-equals" to-field-name="oldStatusId"/>
        <condition field-name="statusId" operator="equals" value="SHIPMENT_SCHEDULED"/>
        <action service="sendShipmentScheduledNotification" mode="async"/>
    </eca>

    <!-- if new originFacilityId or destinationFacilityId, get settings from facilities -->
    <eca service="createShipment" event="commit">
        <condition field-name="originFacilityId" operator="is-not-empty"/>
        <action service="setShipmentSettingsFromFacilities" mode="sync"/>
    </eca>
    <eca service="createShipment" event="commit">
        <condition field-name="destinationFacilityId" operator="is-not-empty"/>
        <action service="setShipmentSettingsFromFacilities" mode="sync"/>
    </eca>
    <eca service="updateShipment" event="commit">
        <condition-field field-name="originFacilityId" operator="not-equals" to-field-name="oldOriginFacilityId"/>
        <condition field-name="originFacilityId" operator="is-not-empty"/>
        <action service="setShipmentSettingsFromFacilities" mode="sync"/>
    </eca>
    <eca service="updateShipment" event="commit">
        <condition-field field-name="destinationFacilityId" operator="not-equals" to-field-name="oldDestinationFacilityId"/>
        <condition field-name="destinationFacilityId" operator="is-not-empty"/>
        <action service="setShipmentSettingsFromFacilities" mode="sync"/>
    </eca>
    <!-- if new primaryOrderId, get settings from order -->
    <eca service="createShipment" event="commit">
        <condition field-name="primaryOrderId" operator="is-not-empty"/>
        <action service="setShipmentSettingsFromPrimaryOrder" mode="sync"/>
    </eca>
    <eca service="updateShipment" event="commit">
        <condition-field field-name="primaryOrderId" operator="not-equals" to-field-name="oldPrimaryOrderId"/>
        <condition field-name="primaryOrderId" operator="is-not-empty"/>
        <action service="setShipmentSettingsFromPrimaryOrder" mode="sync"/>
    </eca>
    <!-- if estimatedShipDate, create work-effort -->
    <eca service="createShipment" event="commit">
        <condition field-name="estimatedShipDate" operator="is-not-empty"/>
        <action service="checkAndCreateWorkEffort" mode="async"/>
    </eca>
    <eca service="createShipment" event="commit">
        <condition field-name="estimatedArrivalDate" operator="is-not-empty"/>
        <action service="checkAndCreateWorkEffort" mode="async"/>
    </eca>
    <eca service="createShipment" event="commit">
        <condition field-name="statusId" operator="is-not-empty"/>
        <action service="createShipmentStatus" mode="async"/>
    </eca>


    <eca service="createShipmentReceipt" event="commit">
        <condition field-name="returnId" operator="is-not-empty"/>
        <action service="checkDecomposeInventoryItem" mode="sync"/>
        <action service="updateReturnStatusFromReceipt" mode="sync" run-as-user="system"/>
    </eca>
    <eca service="createShipmentReceipt" event="commit">
        <condition field-name="orderId" operator="is-not-empty"/>
        <action service="updateOrderStatusFromReceipt" mode="sync" run-as-user="system"/>
    </eca>
    <eca service="createShipmentReceipt" event="commit">
        <condition field-name="shipmentId" operator="is-not-empty"/>
        <action service="checkDecomposeInventoryItem" mode="sync"/>
        <action service="updatePurchaseShipmentFromReceipt" mode="sync"/>
    </eca>

    <eca service="createShipmentRouteSegment" event="commit">
        <action service="ensureRouteSegPackage" mode="sync"/>
    </eca>
    <eca service="updateShipmentRouteSegment" event="commit">
        <action service="ensureRouteSegPackage" mode="sync"/>
    </eca>

    <eca service="createShipmentPackageContent" event="in-validate">
        <condition field-name="shipmentPackageSeqId" operator="equals" value="New"/>
        <action service="createShipmentPackage" mode="sync"/>
    </eca>

    <!-- picking ecas -->
    <eca service="updatePicklist" event="commit">
        <set field-name="statusIdTo" env-name="statusId"/>
        <set field-name="statusId" env-name="oldStatusId"/>
        <action service="createPicklistStatus" mode="sync"/>
    </eca>
    <eca service="updatePicklistItem" event="commit">
        <condition-field field-name="itemStatusId" operator="not-equals" to-field-name="oldItemStatusId"/>
        <action service="checkPicklistBinItemStatuses" mode="sync"/>
    </eca>

    <!-- packing ecas -->
    <eca service="packBulkItems" event="commit">
        <condition field-name="nextPackageSeq" operator="equals" value="0"/>
        <action service="setNextPackageSeq" mode="sync"/>
    </eca>
</service-eca>
