<?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.
-->

<simple-methods xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://ofbiz.apache.org/Simple-Method" xsi:schemaLocation="http://ofbiz.apache.org/Simple-Method http://ofbiz.apache.org/dtds/simple-methods.xsd">

    <simple-method method-name="orderAdjustmentPermissionCheck" short-description="Permission service for the creation and editing of order adjustments">
        <set field="primaryPermission" value="ORDERMGR"/>
        <set field="altPermission" value="ORDERMGR_ROLE"/>
        <set field="mainAction" from-field="parameters.mainAction"/>
        <call-simple-method method-name="genericBasePermissionCheck" xml-resource="component://common/minilang/permission/CommonPermissionServices.xml"/>
        <if-compare field="hasPermission" operator="not-equals" value="true">
            <set field="resourceDescription" from-field="parameters.resourceDescription"/>
            <if-empty field="resourceDescription">
                <property-to-field resource="CommonUiLabels" property="CommonPermissionThisOperation" field="resourceDescription"/>
            </if-empty>
            <if-compare field="mainAction" value="CREATE" operator="equals">
                <property-to-field resource="OrderErrorUiLabels" property="OrderSecurityErrorToRunCreateOrderAdjustement" field="failMessage"/>
            </if-compare>
            <if-compare field="mainAction" value="UPDATE" operator="equals">
                <property-to-field resource="OrderErrorUiLabels" property="OrderSecurityErrorToRunAutoCreateOrderAdjustments" field="failMessage"/>
            </if-compare>
            <set field="hasPermission" type="Boolean" value="false"/>
            <field-to-result field="failMessage"/>
            <else>
                <field-to-result field="hasPermission"/>
            </else>
        </if-compare>
    </simple-method>
    <simple-method method-name="updateOrderStatusFromReceipt" short-description="Update Order Status From ShipmentReceipt">
        <check-permission permission="ORDERMGR" action="_UPDATE"><fail-property resource="OrderErrorUiLabels" property="OrderSecurityErrorToRunUpdateOrderStatusFromReceipt"/></check-permission>
        <check-errors/>

        <entity-one entity-name="OrderHeader" value-field="orderHeader"/>
        <entity-and entity-name="ShipmentReceipt" list="shipmentReceipts">
            <field-map field-name="orderId" from-field="parameters.orderId"/>
        </entity-and>
        <iterate list="shipmentReceipts" entry="receipt">
            <if-empty field="totalsMap[receipt.orderItemSeqId]">
                <set field="totalsMap[receipt.orderItemSeqId]" value="0" type="BigDecimal"/>
            </if-empty>
            <calculate field="totalsMap[receipt.orderItemSeqId]">
                <calcop operator="add" field="totalsMap[receipt.orderItemSeqId]">
                    <calcop operator="get" field="receipt.quantityAccepted"/>
                    <calcop operator="get" field="receipt.quantityRejected"/>
                </calcop>
            </calculate>
            <set from-field="receipt.orderId" field="newLookupMap.orderId"/>
            <set from-field="receipt.orderItemSeqId" field="newLookupMap.orderItemSeqId"/>
            <find-by-primary-key entity-name="OrderItem" map="newLookupMap" value-field="orderItem"/>
            <if-compare field="orderItem.statusId" operator="not-equals" value="ITEM_COMPLETED">
                <if-compare-field field="orderItem.quantity" to-field="totalsMap[receipt.orderItemSeqId]" operator="less-equals" type="BigDecimal">
                    <!-- update the status for the item -->
                    <set field="orderItem.statusId" value="ITEM_COMPLETED"/>
                    <store-value value-field="orderItem"/>
                    <!-- create status change history -->
                    <make-value entity-name="OrderStatus" value-field="newValue"/>
                    <sequenced-id sequence-name="OrderStatus" field="newValue.orderStatusId"/>
                    <set from-field="orderItem.orderItemSeqId" field="newValue.orderItemSeqId"/>
                    <set from-field="orderItem.orderId" field="newValue.orderId"/>
                    <set from-field="orderItem.statusId" field="newValue.statusId"/>
                    <set from-field="parameters.userLogin.userLoginId" field="newValue.statusUserLogin"/>
                    <now-timestamp field="newValue.statusDatetime"/>

                    <create-value value-field="newValue"/>
                </if-compare-field>
            </if-compare>
        </iterate>

        <!-- check to see if all items have been completed -->
        <entity-and entity-name="OrderItem" list="allOrderItems">
            <field-map field-name="orderId" from-field="parameters.orderId"/>
        </entity-and>
        <set value="true" field="allCompleted"/>
        <iterate list="allOrderItems" entry="item">
            <if-compare field="item.statusId" operator="not-equals" value="ITEM_COMPLETED">
                <set value="false" field="allCompleted"/>
            </if-compare>
        </iterate>

        <if-compare field="allCompleted" operator="equals" value="true">
            <!-- update the order header -->
            <set field="orderHeader.statusId" value="ORDER_COMPLETED"/>
            <store-value value-field="orderHeader"/>
            <!-- create the status history -->
            <make-value entity-name="OrderStatus" value-field="newValue"/>
            <sequenced-id sequence-name="OrderStatus" field="newValue.orderStatusId"/>

            <set from-field="orderHeader.orderId" field="newValue.orderId"/>
            <set from-field="orderHeader.statusId" field="newValue.statusId"/>
            <set from-field="parameters.userLogin.userLoginId" field="newValue.statusUserLogin"/>
            <now-timestamp field="newValue.statusDatetime"/>

            <create-value value-field="newValue"/>
        </if-compare>

        <!-- return the current return header status -->
        <field-to-result field="orderHeader.statusId" result-name="currentStatusId"/>
    </simple-method>
</simple-methods>
