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

    <!-- TaxAuthorityCategory methods -->
    <simple-method method-name="deleteTaxAuthorityCategory" short-description="delete a TaxAuthorityCategory">
        <set field="taxAuthorityRateProductMap.taxAuthGeoId" from-field="parameters.taxAuthGeoId"/>
        <set field="taxAuthorityRateProductMap.taxAuthPartyId" from-field="parameters.taxAuthPartyId"/>
        <set field="taxAuthorityRateProductMap.productCategoryId" from-field="parameters.productCategoryId"/>
        <find-by-and entity-name="TaxAuthorityRateProduct" map="taxAuthorityRateProductMap" list="taxAuthorityRateProductList"/>
        <if-empty field="taxAuthorityRateProductList">
            <entity-one entity-name="TaxAuthorityCategory" value-field="lookedUpValue"/>
            <remove-value value-field="lookedUpValue"/>
            <else>
                <add-error>
                    <fail-property resource="AccountingUiLabels" property="AccountingTaxAuthorityRateProductUseThisProductCategory"/>
                </add-error>
            </else>
        </if-empty>
        <check-errors/>
    </simple-method>

    <!-- PartyTaxAuthInfo methods -->
    <simple-method method-name="createPartyTaxAuthInfo" short-description="create a PartyTaxAuthInfo">
        <call-simple-method method-name="validatePartyTaxIdInline"/>

        <entity-one entity-name="TaxAuthority" value-field="taxAuth"/>
        <if-empty field="taxAuth">
            <add-error>
                <fail-property resource="PartyUiLabels" property="PartyTaxAuthPartyAndGeoNotAvailable"/>
            </add-error>
        </if-empty>

        <!-- done checking preliminary constraints, see if there are any errors -->
        <check-errors/>

        <make-value entity-name="PartyTaxAuthInfo" value-field="newEntity"/>
        <set-pk-fields map="parameters" value-field="newEntity"/>
        <if-empty field="newEntity.fromDate"><now-timestamp field="newEntity.fromDate"/></if-empty>
        <set-nonpk-fields map="parameters" value-field="newEntity"/>

        <create-value value-field="newEntity"/>
    </simple-method>
    <simple-method method-name="updatePartyTaxAuthInfo" short-description="update a PartyTaxAuthInfo">
        <call-simple-method method-name="validatePartyTaxIdInline"/>

        <!-- done checking preliminary constraints, see if there are any errors -->
        <check-errors/>

        <entity-one entity-name="PartyTaxAuthInfo" value-field="lookedUpValue"/>
        <set-nonpk-fields map="parameters" value-field="lookedUpValue"/>
        <store-value value-field="lookedUpValue"/>
    </simple-method>
    <simple-method method-name="validatePartyTaxIdInline" short-description="validatePartyTaxIdInline">
        <!-- validate the partyTaxId based on TaxAuthority.taxIdFormatPattern if not empty -->
        <!-- If the party validated is in the UE, we could to a better job using the SOAP service from http://ec.europa.eu/taxation_customs/vies/faqvies.do#item16 -->
        <entity-one entity-name="TaxAuthority" value-field="taxAuthority"/>
        <if>
            <condition>
                <and>
                    <not><if-empty field="taxAuthority.taxIdFormatPattern"/></not>
                    <not><if-empty field="parameters.partyTaxId"/></not>
                    <not><if-regexp field="parameters.partyTaxId" expr="${taxAuthority.taxIdFormatPattern}"/></not>
                </and>
            </condition>
            <then>
                <add-error>
                    <fail-property resource="AccountingErrorUiLabels" property="AccountingTaxIdInvalidFormat"/>
                </add-error>
            </then>
        </if>
    </simple-method>

    <simple-method method-name="createCustomerTaxAuthInfo" short-description="Create a Customer PartyTaxAuthInfo">
        <script>groovy:
            taxAuthPartyGeoIds = parameters.get("taxAuthPartyGeoIds")
            parameters.put("taxAuthPartyId", taxAuthPartyGeoIds.substring(0, taxAuthPartyGeoIds.indexOf("::")))
            parameters.put("taxAuthGeoId", taxAuthPartyGeoIds.substring(taxAuthPartyGeoIds.indexOf("::") + 2))
        </script>
        <set-service-fields service-name="createPartyTaxAuthInfo" map="parameters" to-map="createPartyTaxAuthInfoMap"/>
        <call-service service-name="createPartyTaxAuthInfo" in-map-name="createPartyTaxAuthInfoMap" include-user-login="true"/>
    </simple-method>
</simple-methods>
