Commit 1c49a1ac authored by Nicolas Wavrant's avatar Nicolas Wavrant

erp5_accounting: closing an Accounting Period generates one Balance...

erp5_accounting: closing an Accounting Period generates one Balance Transaction per accounting ledger

BalanceTransaction.py updated to correctly index Balance Transactions with their ledgers.
_computeStockDifferenceList also now returns a correct result
parent 880248d8
"""Creates a balance transaction to open the next period.
"""
from Products.ZSQLCatalog.SQLCatalog import Query
portal = context.getPortalObject()
Base_translateString = portal.Base_translateString
......@@ -61,7 +63,9 @@ section_list = [section]
if group_value is not None:
section_list.extend(getDependantSectionList(group_value, section))
def createBalanceTransaction(section):
ledger_list = portal.portal_categories.ledger.getCategoryChildValueList(is_self_excluded=1) + [None,]
def createBalanceTransaction(section, ledger=None):
balance_date = at_date + 1
# We discard hours, minutes and seconds and at the same time, make sure the date
# is in its "normal timezone". For example, when at_date is the day of a dailight saving
......@@ -69,13 +73,38 @@ def createBalanceTransaction(section):
balance_date = DateTime(balance_date.year(), balance_date.month(), balance_date.day())
return portal.accounting_module.newContent(
portal_type='Balance Transaction',
ledger=ledger,
start_date=balance_date,
title=context.getTitle() or Base_translateString('Balance Transaction'),
destination_section_value=section,
resource=section_currency,
causality_value=context)
getInventoryList = portal.portal_simulation.getInventoryList
with context.defaultActivateParameterDict({'tag': activity_tag}, placeless=True):
# List ledgers on which there are movements
inventory_ledger_uid_list = [inventory.ledger_uid for inventory \
in getInventoryList(at_date=at_date.latestTime(),
portal_type=portal.getPortalAccountingMovementTypeList(),
group_by_ledger=True)]
for ledger in ledger_list:
# If there are no movements within this ledger, we can
# directly go to another
if ledger is None and None not in inventory_ledger_uid_list:
continue
elif ledger is not None and ledger.getUid() not in inventory_ledger_uid_list:
continue
if ledger is not None:
ledger_uid = ledger.getUid()
ledger_url = ledger.getCategoryRelativeUrl()
else:
ledger_uid = Query(ledger_uid=None)
ledger_url = ''
for section in section_list:
section_uid = section.getUid()
balance_transaction = None
......@@ -102,13 +131,12 @@ with context.defaultActivateParameterDict({'tag': activity_tag}, placeless=True)
else:
group_by_node_node_category_list.append(node_category_url)
getInventoryList = portal.portal_simulation.getInventoryList
inventory_param_dict = dict(section_uid=section_uid,
simulation_state=('delivered',),
precision=section_currency_precision,
portal_type=portal.getPortalAccountingMovementTypeList(),
at_date=at_date.latestTime(),)
at_date=at_date.latestTime(),
ledger_uid=ledger_uid)
# Calculate the sum of profit and loss accounts balances for that period.
# This must match the difference between assets, liability and equity accounts.
......@@ -144,7 +172,7 @@ with context.defaultActivateParameterDict({'tag': activity_tag}, placeless=True)
profit_and_loss_quantity += total_price
if balance_transaction is None:
balance_transaction = createBalanceTransaction(section)
balance_transaction = createBalanceTransaction(section, ledger_url)
balance_transaction.newContent(
id='%03d' % line_count,
portal_type='Balance Transaction Line',
......@@ -162,7 +190,7 @@ with context.defaultActivateParameterDict({'tag': activity_tag}, placeless=True)
if inventory.node_relative_url != profit_and_loss_account:
profit_and_loss_quantity += total_price
if balance_transaction is None:
balance_transaction = createBalanceTransaction(section)
balance_transaction = createBalanceTransaction(section, ledger_url)
balance_transaction.newContent(
id='%03d' % line_count,
portal_type='Balance Transaction Line',
......@@ -188,7 +216,7 @@ with context.defaultActivateParameterDict({'tag': activity_tag}, placeless=True)
if inventory.resource_uid != section_currency_uid:
if balance_transaction is None:
balance_transaction = createBalanceTransaction(section)
balance_transaction = createBalanceTransaction(section, ledger_url)
balance_transaction.newContent(
id='%03d' % line_count,
portal_type='Balance Transaction Line',
......@@ -202,7 +230,7 @@ with context.defaultActivateParameterDict({'tag': activity_tag}, placeless=True)
raise ValueError('Different price: %s != %s ' % (
total_price, quantity))
if balance_transaction is None:
balance_transaction = createBalanceTransaction(section)
balance_transaction = createBalanceTransaction(section, ledger_url)
balance_transaction.newContent(
id='%03d' % line_count,
portal_type='Balance Transaction Line',
......@@ -230,7 +258,7 @@ with context.defaultActivateParameterDict({'tag': activity_tag}, placeless=True)
if inventory.resource_uid != section_currency_uid:
if balance_transaction is None:
balance_transaction = createBalanceTransaction(section)
balance_transaction = createBalanceTransaction(section, ledger_url)
balance_transaction.newContent(
id='%03d' % line_count,
portal_type='Balance Transaction Line',
......@@ -244,7 +272,7 @@ with context.defaultActivateParameterDict({'tag': activity_tag}, placeless=True)
raise ValueError('Different price: %s != %s ' % (
total_price, quantity))
if balance_transaction is None:
balance_transaction = createBalanceTransaction(section)
balance_transaction = createBalanceTransaction(section, ledger_url)
balance_transaction.newContent(
id='%03d' % line_count,
portal_type='Balance Transaction Line',
......@@ -260,7 +288,7 @@ with context.defaultActivateParameterDict({'tag': activity_tag}, placeless=True)
# create a balance transaction that notes that the current balance of profit
# and loss account is 0, so that the delta gets indexed.
if profit_and_loss_accounts_balance:
balance_transaction = createBalanceTransaction(section)
balance_transaction = createBalanceTransaction(section, ledger_url)
balance_transaction.newContent(
portal_type='Balance Transaction Line',
destination=profit_and_loss_account,
......@@ -273,6 +301,9 @@ with context.defaultActivateParameterDict({'tag': activity_tag}, placeless=True)
- roundCurrency(selected_profit_and_loss_account_balance, section_currency)
- roundCurrency(profit_and_loss_quantity, section_currency), section_currency)
# If profit_and_loss_quantity equals 0 then we are on a
# ledger which no accounting transactions are member of
if profit_and_loss_quantity != 0.:
# add a final line for p&l
balance_transaction.newContent(
id='%03d' % (line_count + 1),
......
......@@ -32,6 +32,7 @@ from AccessControl import ClassSecurityInfo
from Products.ERP5Type import Permissions, PropertySheet
from Products.ERP5.Document.Inventory import Inventory
from Products.ERP5.Document.AccountingTransaction import AccountingTransaction
from Products.ZSQLCatalog.SQLCatalog import Query
import types
......@@ -136,6 +137,10 @@ class BalanceTransaction(AccountingTransaction, Inventory):
getInventoryList = self.getPortalObject()\
.portal_simulation.getInventoryList
section_uid = self.getDestinationSectionUid()
ledger_uid = self.getLedgerUid()
if ledger_uid is None:
ledger_uid = Query(ledger_uid=None)
precision = 2
if section_uid is not None:
accounting_currency = \
......@@ -150,7 +155,8 @@ class BalanceTransaction(AccountingTransaction, Inventory):
section_uid=section_uid,
precision=precision,
portal_type=self.getPortalAccountingMovementTypeList(),
simulation_state=('delivered', ))
simulation_state=('delivered', ),
ledger_uid=ledger_uid)
# node
for movement in self._getGroupByNodeMovementList():
......@@ -267,7 +273,8 @@ class BalanceTransaction(AccountingTransaction, Inventory):
relative_url=movement.getRelativeUrl(),
quantity=movement.getQuantity(),
total_price=movement\
.getDestinationInventoriatedTotalAssetPrice(), ))
.getDestinationInventoriatedTotalAssetPrice(),
ledger_uid=movement.getLedgerUid(), ))
# mirror section
for movement in self._getGroupByMirrorSectionMovementList():
......@@ -291,7 +298,8 @@ class BalanceTransaction(AccountingTransaction, Inventory):
relative_url=movement.getRelativeUrl(),
quantity=movement.getQuantity(),
total_price=movement\
.getDestinationInventoriatedTotalAssetPrice(), ))
.getDestinationInventoriatedTotalAssetPrice(),
ledger_uid=movement.getLedgerUid(), ))
# payment
for movement in self._getGroupByPaymentMovementList():
......@@ -315,7 +323,8 @@ class BalanceTransaction(AccountingTransaction, Inventory):
relative_url=movement.getRelativeUrl(),
quantity=movement.getQuantity(),
total_price=movement\
.getDestinationInventoriatedTotalAssetPrice(), ))
.getDestinationInventoriatedTotalAssetPrice(),
ledger_uid=movement.getLedgerUid(), ))
return new_stock
......
This diff is collapsed.
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