svn commit: r1796731 - in /ofbiz/ofbiz-framework/trunk/applications/accounting: groovyScripts/admin/AcctgAdminServices.groovy minilang/admin/ servicedef/services_admin.xml

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1796731 - in /ofbiz/ofbiz-framework/trunk/applications/accounting: groovyScripts/admin/AcctgAdminServices.groovy minilang/admin/ servicedef/services_admin.xml

pgil
Author: pgil
Date: Tue May 30 07:14:27 2017
New Revision: 1796731

URL: http://svn.apache.org/viewvc?rev=1796731&view=rev
Log:

Fixed: Convert AcctgAdminServices.xml mini-lang to groovyDSL (OFBIZ-9372)
Related to task OFBIZ-9350 Deprecate Mini Lang, convert services createPartyAcctgPreference, getPartyAccountingPreferences, setAcctgCompany, updateFXConversion and getFXConversion from mini-lang to groovyDSL
Commited slightly modified patch from Leila, implementing Jacques and Deepak recommendations.

Thanks to Jacques and Deepak for the review, Leila for the provided patch.

Added:
    ofbiz/ofbiz-framework/trunk/applications/accounting/groovyScripts/admin/AcctgAdminServices.groovy   (with props)
Removed:
    ofbiz/ofbiz-framework/trunk/applications/accounting/minilang/admin/
Modified:
    ofbiz/ofbiz-framework/trunk/applications/accounting/servicedef/services_admin.xml

Added: ofbiz/ofbiz-framework/trunk/applications/accounting/groovyScripts/admin/AcctgAdminServices.groovy
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/applications/accounting/groovyScripts/admin/AcctgAdminServices.groovy?rev=1796731&view=auto
==============================================================================
--- ofbiz/ofbiz-framework/trunk/applications/accounting/groovyScripts/admin/AcctgAdminServices.groovy (added)
+++ ofbiz/ofbiz-framework/trunk/applications/accounting/groovyScripts/admin/AcctgAdminServices.groovy Tue May 30 07:14:27 2017
@@ -0,0 +1,171 @@
+/*
+ * 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.
+ */
+
+import java.sql.Timestamp
+import org.apache.ofbiz.base.util.UtilDateTime
+import org.apache.ofbiz.base.util.UtilProperties
+import org.apache.ofbiz.entity.condition.EntityConditionBuilder
+import org.apache.ofbiz.entity.GenericValue
+import org.apache.ofbiz.entity.util.EntityUtil
+
+def createPartyAcctgPreference() {
+    //check that the party is an INTERNAL_ORGANIZATION, as defined in PartyRole
+    partyRole = select().from('PartyRole').where([partyId:parameters.partyId,roleTypeId:"INTERNAL_ORGANIZATIO"]).queryOne()
+    if (!partyRole) {
+        String errorMessage = UtilProperties.getMessage("AccountingUiLabels","AccountingPartyMustBeInternalOrganization", locale)
+        logError(errorMessage)
+        return error(errorMessage)
+    }
+    //Does not check if the Party is actually a company because real people have to pay taxes too
+
+    //TODO: maybe check to make sure that all fields are not null
+    newEntity = delegator.makeValidValue("PartyAcctgPreference", parameters)
+    delegator.create(newEntity)
+    return success()
+}
+
+def getPartyAccountingPreferences() {
+    Map result = success()
+    GenericValue aggregatedPartyAcctgPref = delegator.makeValidValue("PartyAcctgPreference", parameters)
+    String currentOrganizationPartyId = parameters.organizationPartyId
+    Boolean containsEmptyFields = true
+
+    while (currentOrganizationPartyId && containsEmptyFields) {
+        GenericValue currentPartyAcctgPref = select().from('PartyAcctgPreference').where([partyId:currentOrganizationPartyId]).queryOne()
+        containsEmptyFields = false
+        if (currentPartyAcctgPref) {
+            for (String entityKey : currentPartyAcctgPref.keySet()) {
+                Object entityValue = currentPartyAcctgPref.get(entityKey)
+                if (entityValue) {
+                    aggregatedPartyAcctgPref.put(entityKey,entityValue)
+                } else {
+                    containsEmptyFields = true
+                }
+            }
+        } else {
+            containsEmptyFields = true
+        }
+        List<GenericValue> parentPartyRelationships = select().from('PartyRelationship').where([partyIdTo:currentOrganizationPartyId, partyRelationshipTypeId:"GROUP_ROLLUP", roleTypeIdFrom:"_NA_", roleTypeIdTo:"_NA_"]).filterByDate().queryList()
+        if (parentPartyRelationships) {
+            GenericValue parentPartyRelationship = EntityUtil.getFirst(parentPartyRelationships)
+            currentOrganizationPartyId = parentPartyRelationship.partyIdFrom
+        } else {
+            currentOrganizationPartyId = null
+        }
+    }
+
+    if (aggregatedPartyAcctgPref) {
+        aggregatedPartyAcctgPref.put("partyId",parameters.organizationPartyId)
+        result.put("partyAccountingPreference", aggregatedPartyAcctgPref)
+    }
+    return result
+}
+
+def setAcctgCompany() {
+    Map result = success()
+    //Set user preference
+    GenericValue partyAcctgPreference = select().from('PartyAcctgPreference').where([partyId: parameters.organizationPartyId]).queryOne()
+    if (partyAcctgPreference) {
+        result = runService("setUserPreference", [userPrefValue: parameters.organizationPartyId, userPrefGroupTypeId: "GLOBAL_PREFERENCES", userPrefTypeId: "ORGANIZATION_PARTY"])
+    }
+    result.put("organizationPartyId", parameters.organizationPartyId)
+
+    return result
+}
+
+def updateFXConversion() {
+    //Set the FX rate changes as of now
+    Timestamp nowTimestamp = parameters.asOfTimestamp
+    if (!nowTimestamp) {
+        nowTimestamp = UtilDateTime.nowTimestamp()
+    }
+
+    //find the existing exchange rates for this currency pair
+    exprBldr = new EntityConditionBuilder()
+    condition = exprBldr.AND() {
+        EQUALS(uomId: parameters.uomId)
+        EQUALS(uomIdTo: parameters.uomIdTo)
+    }
+    if (parameters.purposeEnumId) {
+        condition = exprBldr.AND(condition) {
+            EQUALS(purposeEnumId: parameters.purposeEnumId)
+        }
+    }
+    List<GenericValue> uomConversions = select().from('UomConversionDated').where(condition).orderBy("-fromDate").filterByDate().queryList()
+
+    //expire all of them
+    for (GenericValue uomConversion : uomConversions) {
+        if (!parameters.fromDate) {
+            uomConversion.put("thruDate", nowTimestamp)
+        } else {
+            uomConversion.put("thruDate", parameters.fromDate)
+        }
+    }
+    delegator.storeAll(uomConversions)
+
+    //now create a new conversion relationship
+    Map createParams = dispatcher.getDispatchContext().makeValidContext("createUomConversionDated", "IN", parameters)
+    if (!parameters.fromDate) {
+        createParams.put("fromDate", nowTimestamp)
+    }
+    result = runService("createUomConversionDated", createParams)
+
+    return success()
+}
+
+def getFXConversion() {
+    Map result = success()
+    Timestamp asOfTimestamp = parameters.asOfTimestamp
+    if (!asOfTimestamp) {
+        asOfTimestamp = UtilDateTime.nowTimestamp()
+    }
+
+    //find the existing exchange rates
+    exprBldr = new EntityConditionBuilder()
+    thruDateCondition = exprBldr.OR() {
+        EQUALS(thruDate: null)
+        GREATER_THAN_EQUAL_TO(thruDate: asOfTimestamp)
+    }
+    condition = exprBldr.AND(thruDateCondition) {
+        EQUALS(uomId: parameters.uomId)
+        EQUALS(uomIdTo: parameters.uomIdTo)
+        LESS_THAN_EQUAL_TO(fromDate: asOfTimestamp)
+    }
+    if (parameters.purposeEnumId) {
+        condition = exprBldr.AND(condition) {
+            EQUALS(purposeEnumId: parameters.purposeEnumId)
+        }
+    }
+    List<GenericValue> rates = select().from('UomConversionDated').where(condition).orderBy("-fromDate").filterByDate().queryList()
+
+    BigDecimal conversionRate
+    int decimalScale = 2
+    int roundingMode = BigDecimal.ROUND_HALF_UP
+    if (rates) {
+        conversionFactor = EntityUtil.getFirst(rates).getBigDecimal("conversionFactor")
+        BigDecimal originalValue = BigDecimal.ONE
+        conversionRate = originalValue.divide(conversionFactor, decimalScale, roundingMode)
+    } else {
+        String errorMessage = "Could not find conversion rate"
+        logError(errorMessage)
+        return error(errorMessage)
+    }
+    result.put("conversionRate",conversionRate)
+    return result
+}
\ No newline at end of file

Propchange: ofbiz/ofbiz-framework/trunk/applications/accounting/groovyScripts/admin/AcctgAdminServices.groovy
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: ofbiz/ofbiz-framework/trunk/applications/accounting/groovyScripts/admin/AcctgAdminServices.groovy
------------------------------------------------------------------------------
    svn:keywords = Date Rev Author URL Id

Propchange: ofbiz/ofbiz-framework/trunk/applications/accounting/groovyScripts/admin/AcctgAdminServices.groovy
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: ofbiz/ofbiz-framework/trunk/applications/accounting/servicedef/services_admin.xml
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/applications/accounting/servicedef/services_admin.xml?rev=1796731&r1=1796730&r2=1796731&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/applications/accounting/servicedef/services_admin.xml (original)
+++ ofbiz/ofbiz-framework/trunk/applications/accounting/servicedef/services_admin.xml Tue May 30 07:14:27 2017
@@ -23,8 +23,9 @@ under the License.
     <description>Accounting Services - Miscellaneous</description>
     <vendor>OFBiz</vendor>
     <version>1.0</version>
-    <service name="createPartyAcctgPreference" default-entity-name="PartyAcctgPreference" engine="simple"
-        location="component://accounting/minilang/admin/AcctgAdminServices.xml" invoke="createPartyAcctgPreference" auth="true">
+
+    <service name="createPartyAcctgPreference" default-entity-name="PartyAcctgPreference" engine="groovy"
+        location="component://accounting/groovyScripts/admin/AcctgAdminServices.groovy" invoke="createPartyAcctgPreference" auth="true">
         <description>Create accounting preferences for a party (organization)</description>
         <permission-service service-name="acctgPrefPermissionCheck" main-action="CREATE"/>
         <auto-attributes include="pk" mode="IN" optional="false"/>
@@ -58,22 +59,24 @@ under the License.
             <exclude field-name="lastOrderNumber"/>
         </auto-attributes>
     </service>
-    <service name="getPartyAccountingPreferences" default-entity-name="PartyAcctgPreference" engine="simple"
-            location="component://accounting/minilang/admin/AcctgAdminServices.xml" invoke="getPartyAccountingPreferences" auth="true">
+
+    <service name="getPartyAccountingPreferences" default-entity-name="PartyAcctgPreference" engine="groovy"
+            location="component://accounting/groovyScripts/admin/AcctgAdminServices.groovy" invoke="getPartyAccountingPreferences" auth="true">
         <description>Get accounting preferences for a party (organization)</description>
         <permission-service service-name="acctgPrefPermissionCheck" main-action="VIEW"/>
         <attribute type="String" mode="IN" name="organizationPartyId" optional="false"/>
         <attribute name="partyAccountingPreference" type="org.apache.ofbiz.entity.GenericValue" mode="OUT" optional="true"/>
     </service>
-    <service name="setAcctgCompany" engine="simple"
-        location="component://accounting/minilang/admin/AcctgAdminServices.xml" invoke="setAcctgCompany" auth="true">
+
+    <service name="setAcctgCompany" engine="groovy"
+        location="component://accounting/groovyScripts/admin/AcctgAdminServices.groovy" invoke="setAcctgCompany" auth="true">
         <description>Set Accounting Company when select</description>
         <permission-service service-name="acctgPrefPermissionCheck" main-action="CREATE"/>
         <attribute type="String" mode="INOUT" name="organizationPartyId" optional="true"/>
     </service>
 
-    <service name="updateFXConversion" engine="simple"
-        location="component://accounting/minilang/admin/AcctgAdminServices.xml" invoke="updateFXConversion">
+    <service name="updateFXConversion" engine="groovy"
+        location="component://accounting/groovyScripts/admin/AcctgAdminServices.groovy" invoke="updateFXConversion">
         <description>Update the conversion rate between two currencies and expire the old conversion rates</description>
         <permission-service service-name="acctgFxPermissionCheck" main-action="UPDATE"/>
         <attribute type="String" mode="IN" name="uomId" optional="false"/>
@@ -128,8 +131,8 @@ under the License.
         <auto-attributes mode="IN" entity-name="PaymentMethodTypeGlAccount" include="pk" optional="false"/>
     </service>
 
-    <service name="getFXConversion" engine="simple"
-        location="component://accounting/minilang/admin/AcctgAdminServices.xml" invoke="getFXConversion">
+    <service name="getFXConversion" engine="groovy"
+        location="component://accounting/groovyScripts/admin/AcctgAdminServices.groovy" invoke="getFXConversion">
         <description>get the conversion rate</description>
         <attribute type="String" mode="IN" name="uomId" optional="false"/>
         <attribute type="String" mode="IN" name="uomIdTo" optional="false"/>