From b46392f147171a4d76fb7f45ed4925c95751fc06 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com>
Date: Mon, 29 Jan 2007 10:42:25 +0000
Subject: [PATCH] Guess the applicable currency and use this currency precision
 to round values and set 'precision' on float fields.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@12420 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 .../Base_getCurrencyForSection.xml            | 182 ++++++++++++++++++
 .../ERP5Accounting_getParams.xml              |  10 +-
 .../Node_getAccountingTransactionList.xml     |   4 +
 bt5/erp5_accounting/bt/revision               |   2 +-
 4 files changed, 196 insertions(+), 2 deletions(-)
 create mode 100644 bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/Base_getCurrencyForSection.xml

diff --git a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/Base_getCurrencyForSection.xml b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/Base_getCurrencyForSection.xml
new file mode 100644
index 0000000000..4a8eff0aa9
--- /dev/null
+++ b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/Base_getCurrencyForSection.xml
@@ -0,0 +1,182 @@
+<?xml version="1.0"?>
+<ZopeData>
+  <record id="1" aka="AAAAAAAAAAE=">
+    <pickle>
+      <tuple>
+        <tuple>
+          <string>Products.PythonScripts.PythonScript</string>
+          <string>PythonScript</string>
+        </tuple>
+        <none/>
+      </tuple>
+    </pickle>
+    <pickle>
+      <dictionary>
+        <item>
+            <key> <string>Python_magic</string> </key>
+            <value>
+              <none/>
+            </value>
+        </item>
+        <item>
+            <key> <string>Script_magic</string> </key>
+            <value> <int>3</int> </value>
+        </item>
+        <item>
+            <key> <string>__ac_local_roles__</string> </key>
+            <value>
+              <none/>
+            </value>
+        </item>
+        <item>
+            <key> <string>_bind_names</string> </key>
+            <value>
+              <object>
+                <klass>
+                  <global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
+                </klass>
+                <tuple/>
+                <state>
+                  <dictionary>
+                    <item>
+                        <key> <string>_asgns</string> </key>
+                        <value>
+                          <dictionary>
+                            <item>
+                                <key> <string>name_container</string> </key>
+                                <value> <string>container</string> </value>
+                            </item>
+                            <item>
+                                <key> <string>name_context</string> </key>
+                                <value> <string>context</string> </value>
+                            </item>
+                            <item>
+                                <key> <string>name_m_self</string> </key>
+                                <value> <string>script</string> </value>
+                            </item>
+                            <item>
+                                <key> <string>name_subpath</string> </key>
+                                <value> <string>traverse_subpath</string> </value>
+                            </item>
+                          </dictionary>
+                        </value>
+                    </item>
+                  </dictionary>
+                </state>
+              </object>
+            </value>
+        </item>
+        <item>
+            <key> <string>_body</string> </key>
+            <value> <string>"""Returns the most suitable currency for a section.\n
+\n
+If the section is an organisation, returns this organisation\'s accounting\n
+currency.\n
+If the section is a category, if the category have a mapping organisation,\n
+return this organisation\'s currency.\n
+"""\n
+\n
+def getCurrencyForSection(section_url):\n
+  portal = context.getPortalObject()\n
+  section = portal.portal_categories.restrictedTraverse(section_url)\n
+\n
+  if section.getPortalType() == \'Organisation\':\n
+    return section.getPriceCurrency()\n
+\n
+  if section.getPortalType() == \'Category\':\n
+    member_list = section.getGroupRelatedValueList(portal_type=\'Organisation\')\n
+    if len(member_list) == 1:\n
+      return member_list[0].getPriceCurrency()\n
+    mapping = section.getMappingValue(portal_type=\'Organisation\')\n
+    if mapping is not None:\n
+      return mapping.getPriceCurrency()\n
+\n
+  # nothing found ... returns the currency from preferences.\n
+  return portal.portal_preferences.getPreferredAccountingTransactionCurrency()\n
+\n
+from Products.ERP5Type.Cache import CachingMethod\n
+getCurrencyForSection = CachingMethod(getCurrencyForSection,\n
+                                      id=script.getId())\n
+return getCurrencyForSection(section_url)\n
+</string> </value>
+        </item>
+        <item>
+            <key> <string>_code</string> </key>
+            <value>
+              <none/>
+            </value>
+        </item>
+        <item>
+            <key> <string>_filepath</string> </key>
+            <value>
+              <none/>
+            </value>
+        </item>
+        <item>
+            <key> <string>_owner</string> </key>
+            <value>
+              <none/>
+            </value>
+        </item>
+        <item>
+            <key> <string>_params</string> </key>
+            <value> <string>section_url</string> </value>
+        </item>
+        <item>
+            <key> <string>errors</string> </key>
+            <value>
+              <tuple/>
+            </value>
+        </item>
+        <item>
+            <key> <string>func_code</string> </key>
+            <value>
+              <object>
+                <klass>
+                  <global name="FuncCode" module="Shared.DC.Scripts.Signature"/>
+                </klass>
+                <tuple/>
+                <state>
+                  <dictionary>
+                    <item>
+                        <key> <string>co_argcount</string> </key>
+                        <value> <int>1</int> </value>
+                    </item>
+                    <item>
+                        <key> <string>co_varnames</string> </key>
+                        <value>
+                          <tuple>
+                            <string>section_url</string>
+                            <string>getCurrencyForSection</string>
+                            <string>Products.ERP5Type.Cache</string>
+                            <string>CachingMethod</string>
+                            <string>_getattr_</string>
+                            <string>script</string>
+                          </tuple>
+                        </value>
+                    </item>
+                  </dictionary>
+                </state>
+              </object>
+            </value>
+        </item>
+        <item>
+            <key> <string>func_defaults</string> </key>
+            <value>
+              <none/>
+            </value>
+        </item>
+        <item>
+            <key> <string>id</string> </key>
+            <value> <string>Base_getCurrencyForSection</string> </value>
+        </item>
+        <item>
+            <key> <string>warnings</string> </key>
+            <value>
+              <tuple/>
+            </value>
+        </item>
+      </dictionary>
+    </pickle>
+  </record>
+</ZopeData>
diff --git a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/ERP5Accounting_getParams.xml b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/ERP5Accounting_getParams.xml
index 71f00b2056..8d427b6133 100644
--- a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/ERP5Accounting_getParams.xml
+++ b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/ERP5Accounting_getParams.xml
@@ -80,11 +80,11 @@ caveats:\n
 """\n
 \n
 # do we have a cache already?\n
-# XXX take selection into account ?\n
 if not ignore_cache:\n
   params_cache = context.REQUEST.other.get(\n
           \'ERP5Accounting_getParams\', None)\n
   if params_cache is not None:\n
+    # return a copy\n
     return dict(params_cache)\n
 \n
 params = {}\n
@@ -108,12 +108,19 @@ section_category = selection_params.get(\'section_category\',\n
                  preference.getPreferredAccountingTransactionSectionCategory())\n
 if section_category:\n
   params[\'section_category\'] = section_category\n
+  currency = context.Base_getCurrencyForSection(section_category)\n
+  # getQuantityPrecisionFromResource is defined on Base, but the portal is not \n
+  # an instance of Base, so we call it on account_module.\n
+  params[\'precision\'] = context.account_module\\\n
+                            .getQuantityPrecisionFromResource(currency)\n
+\n
 \n
 simulation_state = selection_params.get(\'simulation_state\',\n
              preference.getPreferredAccountingTransactionSimulationStateList())\n
 if simulation_state:\n
   params[\'simulation_state\'] = simulation_state\n
 \n
+\n
 if not ignore_cache:\n
   context.REQUEST.other[\'ERP5Accounting_getParams\'] = params\n
 return dict(params)\n
@@ -174,6 +181,7 @@ return dict(params)\n
                             <string>_write_</string>
                             <string>at_date</string>
                             <string>section_category</string>
+                            <string>currency</string>
                             <string>simulation_state</string>
                           </tuple>
                         </value>
diff --git a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/Node_getAccountingTransactionList.xml b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/Node_getAccountingTransactionList.xml
index de23b419f2..9426c8837d 100644
--- a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/Node_getAccountingTransactionList.xml
+++ b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/Node_getAccountingTransactionList.xml
@@ -75,6 +75,10 @@ portal = context.getPortalObject()\n
 params = portal.ERP5Accounting_getParams(selection)\n
 N_ = lambda msg: Message(\'erp5_ui\', msg)\n
 \n
+if params.get(\'precision\', None) is not None:\n
+  # listbox editable float fields uses request/precision to format the value.\n
+  context.REQUEST.set(\'precision\', params[\'precision\'])\n
+\n
 if not from_date:\n
   from_date = portal.portal_preferences\\\n
                 .getPreferredAccountingTransactionFromDate()\n
diff --git a/bt5/erp5_accounting/bt/revision b/bt5/erp5_accounting/bt/revision
index 7f3a7cc66c..59f3135037 100644
--- a/bt5/erp5_accounting/bt/revision
+++ b/bt5/erp5_accounting/bt/revision
@@ -1 +1 @@
-161
\ No newline at end of file
+165
\ No newline at end of file
-- 
2.30.9