Commit f4639fde authored by Jérome Perrin's avatar Jérome Perrin

accounting: Properly support initial balance in GL export

parent c8dee291
......@@ -52,6 +52,7 @@
<key> <string>_body</string> </key>
<value> <string encoding="cdata"><![CDATA[
from Products.ERP5Type.Document import newTempBase\n
from Products.ZSQLCatalog.SQLCatalog import Query, ComplexQuery\n
from Products.ERP5Type.Message import translateString\n
from Products.ERP5Type.Log import log\n
......@@ -123,6 +124,7 @@ net_balance = 0.0\n
is_pl_account = False\n
if params.get(\'node_uid\'):\n
if context.getUid() == params[\'node_uid\']:\n
node = context\n
is_pl_account = context.isMemberOf(\'account_type/expense\')\\\n
or context.isMemberOf(\'account_type/income\')\n
else:\n
......@@ -168,72 +170,89 @@ if from_date or is_pl_account:\n
# I don\'t think this should happen\n
log(\'from_date not passed, defaulting to period_start_date\')\n
initial_balance_from_date = period_start_date\n
\n
\n
# Get previous debit and credit\n
if initial_balance_from_date == period_start_date and is_pl_account:\n
previous_total_debit = previous_total_credit = 0\n
else:\n
if not (initial_balance_from_date == period_start_date and is_pl_account):\n
getInventoryAssetPrice = portal.portal_simulation.getInventoryAssetPrice\n
# first to the balance at the period start date\n
if is_pl_account:\n
period_openning_balance = 0\n
else:\n
period_openning_balance = getInventoryAssetPrice(\n
to_date=min(period_start_date,\n
initial_balance_from_date),\n
**get_inventory_kw)\n
\n
# then all movement between period_start_date and from_date\n
previous_total_debit = getInventoryAssetPrice(omit_asset_decrease=True,\n
from_date=period_start_date,\n
to_date=initial_balance_from_date,\n
**get_inventory_kw) + max(period_openning_balance, 0)\n
previous_total_credit = getInventoryAssetPrice(omit_asset_increase=True,\n
from_date=period_start_date,\n
to_date=initial_balance_from_date,\n
**get_inventory_kw) - max(-period_openning_balance, 0)\n
for section_uid in list(params.get(\'section_uid\', [])):\n
# We add one initial balance line per section. The main reason is to be able\n
# to know the section_title for the GL export.\n
# XXX we may also want detail by resource or analytic columns sometimes.\n
get_inventory_kw[\'section_uid\'] = section_uid\n
# Initial balance calculation uses the same logic as Trial Balance.\n
# first to the balance at the period start date\n
if is_pl_account:\n
period_openning_balance = 0\n
else:\n
period_openning_balance = getInventoryAssetPrice(\n
to_date=min(period_start_date,\n
initial_balance_from_date),\n
**get_inventory_kw)\n
\n
# then all movements between period_start_date and from_date\n
previous_total_debit = getInventoryAssetPrice(omit_asset_decrease=True,\n
from_date=period_start_date,\n
to_date=initial_balance_from_date,\n
**get_inventory_kw) + max(period_openning_balance, 0)\n
previous_total_credit = getInventoryAssetPrice(omit_asset_increase=True,\n
from_date=period_start_date,\n
to_date=initial_balance_from_date,\n
**get_inventory_kw) - max(-period_openning_balance, 0)\n
\n
if previous_total_credit != 0:\n
previous_total_credit = - previous_total_credit\n
\n
# Show the previous balance if not empty\n
if previous_total_credit != 0 or previous_total_debit != 0:\n
net_balance = previous_total_debit - previous_total_credit\n
previous_balance = newTempBase(portal, \'_temp_accounting_transaction\')\n
previous_balance.edit(\n
uid=\'new_000\',\n
date=initial_balance_from_date,\n
simulation_state_title="",\n
credit_price=previous_total_credit,\n
debit_price=previous_total_debit,\n
total_price=net_balance,\n
credit=previous_total_credit,\n
debit=previous_total_debit,\n
total_quantity=net_balance,\n
running_total_price=net_balance,\n
is_previous_balance=True,\n
Movement_getSpecificReference=u\'%s\' % translateString(\'Previous Balance\'),\n
Movement_getExplanationTitle=u\'%s\' % translateString(\'Previous Balance\'),\n
Movement_getExplanationTranslatedPortalType=\'\',\n
Movement_getExplanationReference=\'\',\n
Movement_getMirrorSectionTitle=\'\',\n
Movement_getNodeGapId=\'\',\n
getListItemUrl=lambda *args,**kw: None,\n
Movement_getExplanationUrl=lambda **kw:None,\n
Movement_getFundingTitle=lambda: \'\',\n
Movement_getFunctionTitle=lambda: \'\',\n
Movement_getProjectTitle=lambda: \'\',\n
Node_statAccountingBalance=\'\',\n
getTranslatedSimulationStateTitle=\'\',\n
modification_date=\'\',\n
)\n
\n
if previous_total_credit != 0:\n
previous_total_credit = - previous_total_credit\n
if context.getPortalType() == \'Account\':\n
previous_balance.edit(Movement_getExplanationTitle=\'\')\n
if params.get(\'node_uid\'):\n
previous_balance.edit(\n
Movement_getNodeGapId=node.Account_getGapId(),\n
node_translated_title=node.getTranslatedTitle()\n
)\n
section = portal.portal_catalog.getObject(section_uid)\n
previous_balance.edit(\n
Movement_getSectionPriceCurrency=section.getPriceCurrencyReference(),\n
resource_reference=section.getPriceCurrencyReference(),\n
section_title=section.getTitle(),\n
)\n
new_result.append(previous_balance)\n
\n
if \'group_by\' in kw:\n
params[\'group_by\'] = kw[\'group_by\']\n
\n
# Show the previous balance if not empty\n
if previous_total_credit != 0 or previous_total_debit != 0:\n
from Products.ERP5Type.Document import newTempBase\n
\n
net_balance = previous_total_debit - previous_total_credit\n
previous_balance = newTempBase(portal, \'_temp_accounting_transaction\')\n
previous_balance.edit(\n
uid=\'new_000\',\n
date=initial_balance_from_date,\n
simulation_state_title="",\n
credit_price=previous_total_credit,\n
debit_price=previous_total_debit,\n
total_price=net_balance,\n
running_total_price=net_balance,\n
is_previous_balance=True,\n
Movement_getSpecificReference=u\'%s\' % translateString(\'Previous Balance\'),\n
Movement_getExplanationTitle=u\'%s\' % translateString(\'Previous Balance\'),\n
Movement_getExplanationTranslatedPortalType=\'\',\n
Movement_getExplanationReference=\'\',\n
Movement_getMirrorSectionTitle=\'\',\n
Movement_getNodeGapId=\'\',\n
getListItemUrl=lambda *args,**kw: None,\n
Movement_getExplanationUrl=lambda **kw:None,\n
Movement_getFundingTitle=lambda: \'\',\n
Movement_getFunctionTitle=lambda: \'\',\n
Movement_getProjectTitle=lambda: \'\',\n
Node_statAccountingBalance=\'\',\n
getTranslatedSimulationStateTitle=\'\',\n
)\n
if context.getPortalType() == \'Account\':\n
previous_balance.edit(Movement_getExplanationTitle=\'\')\n
\n
new_result = [previous_balance]\n
new_result.extend(\n
portal.portal_simulation.getMovementHistoryList(\n
new_result.extend(\n
portal.portal_simulation.getMovementHistoryList(\n
from_date=from_date,\n
initial_running_total_price=net_balance,\n
# initial_running_quantity=net_balance, TODO\n
......@@ -242,7 +261,7 @@ if from_date or is_pl_account:\n
sort_on=sort_on,\n
ignore_group_by=True,\n
**params))\n
return new_result\n
return new_result\n
\n
# We try not to convert to a list, hence the copy & paste\n
return portal.portal_simulation.getMovementHistoryList(\n
......
......@@ -1619,7 +1619,11 @@ class TestAccountingReports(AccountingTestCase, ERP5ReportTestCase):
self.checkLineProperties(data_line_list[0],
Movement_getSpecificReference='Previous Balance',
date=DateTime(2006, 2, 2),
section_title='My Organisation',
Movement_getExplanationTitleAndAnalytics=None,
grouping_date=None,
grouping_reference=None,
modification_date='',
debit_price=300,
credit_price=21,
running_total_price=279)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment