<?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="deleteRequirementAndRelated" short-description="Delete a requirement after deleting related entity records">
        <entity-one entity-name="Requirement" value-field="requirement" auto-field-map="true"/>
        <check-errors/>
        <remove-related value-field="requirement" relation-name="RequirementRole"/>
        <remove-related value-field="requirement" relation-name="RequirementCustRequest"/>
        <remove-value value-field="requirement"/>
    </simple-method>

    <simple-method method-name="associateRequirementWithRequestItem" short-description="Associate a requirement and CustRequestItem">
        <make-value entity-name="RequirementCustRequest" value-field="newEntity"/>
        <set-pk-fields map="parameters" value-field="newEntity"/>
        <set-nonpk-fields map="parameters" value-field="newEntity"/>
        <create-value value-field="newEntity"/>
    </simple-method>

    <simple-method method-name="addTaskToRequirement" short-description="Add a task to a requirement">
        <make-value entity-name="WorkRequirementFulfillment" value-field="newEntity"/>
        <set-pk-fields map="parameters" value-field="newEntity"/>
        <create-value value-field="newEntity"/>
    </simple-method>

    <simple-method method-name="autoAssignRequirementToSupplier" short-description="If the requirement is a product requirement (purchasing) try to assign it to the primary supplier">
        <entity-one entity-name="Requirement" value-field="requirement" auto-field-map="true"/>
        <check-errors/>
        <if-compare field="requirement.requirementTypeId" operator="equals" value="PRODUCT_REQUIREMENT">
            <if-not-empty field="requirement.productId">
                <if-not-empty field="requirement.quantity">
                <entity-condition entity-name="SupplierProduct" list="supplierProducts">
                    <condition-list>
                        <condition-expr field-name="productId" from-field="requirement.productId"/>
                        <condition-expr field-name="minimumOrderQuantity" operator="less-equals" from-field="requirement.quantity"/>
                    </condition-list>
                    <order-by field-name="lastPrice"/>
                    <order-by field-name="supplierPrefOrderId"/>
                </entity-condition>
                <filter-list-by-date list="supplierProducts" valid-date="requirement.requiredByDate" from-field-name="availableFromDate" thru-field-name="availableThruDate"/>
                <first-from-list list="supplierProducts" entry="supplierProduct"/>
                <if-not-empty field="supplierProduct.partyId">
                    <make-value entity-name="RequirementRole" value-field="requirementSupplier"/>
                    <set field="requirementSupplier.requirementId" from-field="requirement.requirementId"/>
                    <set field="requirementSupplier.partyId" from-field="supplierProduct.partyId"/>
                    <set field="requirementSupplier.roleTypeId" value="SUPPLIER"/>
                    <now-timestamp field="requirementSupplier.fromDate"/>
                    <create-value value-field="requirementSupplier"/>
                </if-not-empty>
                </if-not-empty>
            </if-not-empty>
        </if-compare>
    </simple-method>

    <!-- create a new RequirementRole -->
    <simple-method method-name="createRequirementRole" short-description="Create a RequirementRole">
        <check-permission permission="ORDERMGR" action="_CREATE">
            <fail-property resource="OrderErrorUiLabels" property="OrderErrorCreatePermissionError"/>
        </check-permission>
        <check-errors/>
        <set from-field="parameters.partyId" field="lookupPKMap.partyId"/>
        <set from-field="parameters.roleTypeId" field="lookupPKMap.roleTypeId"/>
        <find-by-primary-key entity-name="PartyRole" map="lookupPKMap" value-field="partyRole"/>
        <if-empty field="partyRole.partyId">
            <check-permission permission="PARTYMGR" action="_CREATE">
                <fail-property resource="OrderErrorUiLabels" property="OrderErrorCreatePermissionError"/>
            </check-permission>
            <check-permission permission="PARTYMGR" action="_UPDATE">
                <fail-property resource="OrderErrorUiLabels" property="OrderErrorCreatePermissionError"/>
            </check-permission>
            <check-errors/>
            <make-value entity-name="PartyRole" value-field="partyRole"/>
            <set-pk-fields map="lookupPKMap" value-field="partyRole"/>
            <create-value value-field="partyRole"/>
        </if-empty>
        <!-- create new entity and create all the fields -->
        <make-value entity-name="RequirementRole" value-field="newEntity"/>
        <set-pk-fields map="parameters" value-field="newEntity"/>
        <set-nonpk-fields map="parameters" value-field="newEntity"/>
        <if-empty field="newEntity.fromDate">
            <now-timestamp field="newEntity.fromDate"/>
        </if-empty>
        <create-value value-field="newEntity"/>
        <check-errors/>
    </simple-method>

    <!-- update an existing RequirementRole -->
    <simple-method method-name="updateRequirementRole" short-description="Update an existing RequirementRole">
        <check-permission permission="ORDERMGR" action="_CREATE">
            <fail-property resource="OrderErrorUiLabels" property="OrderErrorCreatePermissionError"/></check-permission>
        <check-errors/>
        <entity-one entity-name="RequirementRole" value-field="requirementRole" auto-field-map="true"/>
        <check-errors/>
        <set-nonpk-fields map="parameters" value-field="requirementRole"/>
        <store-value value-field="requirementRole"/>
        <check-errors/>
    </simple-method>

    <!-- remove an existing RequirementRole -->
    <simple-method method-name="removeRequirementRole" short-description="Remove an existing RequirementRole">
        <check-permission permission="ORDERMGR" action="_CREATE">
            <fail-property resource="OrderErrorUiLabels" property="OrderErrorCreatePermissionError"/>
        </check-permission>
        <check-errors/>
        <entity-one entity-name="RequirementRole" value-field="requirementRole" auto-field-map="true"/>
        <check-errors/>
        <remove-value value-field="requirementRole"/>
        <check-errors/>
    </simple-method>

    <simple-method method-name="createTransferFromRequirement" short-description="Create the inventory transfers required to fulfill the requirement.">
        <check-permission permission="ORDERMGR" action="_CREATE">
            <fail-property resource="OrderErrorUiLabels" property="OrderErrorCreatePermissionError"/>
        </check-permission>
        <check-errors/>
        <entity-one entity-name="Requirement" value-field="requirement" auto-field-map="true"/>
        <check-errors/>
        <set field="inputMap.productId" from-field="requirement.productId"/>
        <set field="inputMap.facilityId" from-field="parameters.fromFacilityId"/>
        <set field="inputMap.facilityIdTo" from-field="requirement.facilityId"/>
        <set field="inputMap.quantity" from-field="requirement.quantity"/>
        <set field="inputMap.sendDate" from-field="requirement.requiredByDate"/>
        <call-service service-name="createInventoryTransfersForProduct" in-map-name="inputMap">
            <result-to-field result-name="quantityNotTransferred" field="quantityNotTransferred"/>
        </call-service>
        <check-errors/>
        <if-compare field="quantityNotTransferred" value="0.0" operator="greater" type="BigDecimal">
            <!-- we create a new requirement for the quantity not trasferred (because not available) -->
            <clear-field field="inputMap"/>
            <set-service-fields service-name="createRequirement" map="requirement" to-map="inputMap"/>
            <set field="inputMap.quantity" from-field="quantityNotTransferred"/>
            <call-service service-name="createRequirement" in-map-name="inputMap"/>
            <check-errors/>
        </if-compare>
        <check-errors/>
        <clear-field field="inputMap"/>
        <set field="inputMap.requirementId" from-field="requirement.requirementId"/>
        <set field="inputMap.statusId" value="REQ_ORDERED"/>
        <call-service service-name="updateRequirement" in-map-name="inputMap"/>
    </simple-method>

</simple-methods>
