From df26f85b0322b0342d94398ecadd0acfab4ed3b0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com>
Date: Wed, 19 Sep 2007 08:56:50 +0000
Subject: [PATCH] When reindexing a balance transaction, only take into account
 balance of accounts < balance transaction date (use to_date, not at_date).

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@16467 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/Document/BalanceTransaction.py |  2 +-
 product/ERP5/tests/testAccounting.py        | 72 +++++++++++++++++++++
 2 files changed, 73 insertions(+), 1 deletion(-)

diff --git a/product/ERP5/Document/BalanceTransaction.py b/product/ERP5/Document/BalanceTransaction.py
index 7ebb382a54..76f3ce1c78 100644
--- a/product/ERP5/Document/BalanceTransaction.py
+++ b/product/ERP5/Document/BalanceTransaction.py
@@ -137,7 +137,7 @@ class BalanceTransaction(AccountingTransaction, Inventory):
     getInventoryList = self.getPortalObject()\
                             .portal_simulation.getInventoryList
     default_inventory_params = dict(
-                        at_date=self.getStartDate(),
+                        to_date=self.getStartDate().earliestTime(),
                         section_uid=self.getDestinationSectionUid(),
                         simulation_state=('delivered', ))
 
diff --git a/product/ERP5/tests/testAccounting.py b/product/ERP5/tests/testAccounting.py
index 2c2094ff04..1016d89890 100644
--- a/product/ERP5/tests/testAccounting.py
+++ b/product/ERP5/tests/testAccounting.py
@@ -994,8 +994,80 @@ class TestClosingPeriod(AccountingTestCase):
     self.assertEquals(mvt_history_list[0].getObject(),
                       balance_line2)
 
+
   def test_BalanceTransactionDate(self):
     # check that dates are correctly used for Balance Transaction indexing
+    organisation_module = self.organisation_module
+
+    transaction1 = self._makeOne(
+        start_date=DateTime(2006, 12, 31),
+        destination_section_value=organisation_module.client_1,
+        portal_type='Sale Invoice Transaction',
+        simulation_state='delivered',
+        lines=(dict(source_value=self.account_module.goods_sales,
+                    source_debit=100),
+               dict(source_value=self.account_module.receivable,
+                    source_credit=100)))
+
+    balance = self.accounting_module.newContent(
+                          portal_type='Balance Transaction',
+                          destination_section_value=self.section,
+                          start_date=DateTime(2007, 1, 1),
+                          resource_value=self.currency_module.euro,)
+    balance_line = balance.newContent(
+                portal_type='Balance Transaction Line',
+                destination_value=self.account_module.equity,
+                destination_debit=100,)
+    balance_line = balance.newContent(
+                portal_type='Balance Transaction Line',
+                source_section_value=organisation_module.client_1,
+                destination_value=self.account_module.receivable,
+                destination_credit=100,)
+    balance.stop()
+    balance.deliver()
+    get_transaction().commit()
+    self.tic()
+
+    stool = self.portal.portal_simulation
+    # the account 'receivable' has a balance of -100
+    node_uid = self.account_module.receivable.getUid()
+    self.assertEquals(-100, stool.getInventory(
+                              section_uid=self.section.getUid(),
+                              node_uid=node_uid))
+    self.assertEquals(1, len(stool.getMovementHistoryList(
+                              section_uid=self.section.getUid(),
+                              node_uid=node_uid)))
+
+    # this is a transaction with the same date as the balance transaction, but
+    # this transaction should not be taken into account when we reindex the
+    # Balance Transaction.
+    transaction2 = self._makeOne(
+        start_date=DateTime(2007, 1, 1),
+        destination_section_value=organisation_module.client_1,
+        portal_type='Sale Invoice Transaction',
+        simulation_state='delivered',
+        lines=(dict(source_value=self.account_module.goods_sales,
+                    source_debit=50),
+               dict(source_value=self.account_module.receivable,
+                    source_credit=50)))
+    get_transaction().commit()
+    self.tic()
+    # let's try to reindex and check if values are still OK
+    balance.reindexObject()
+    get_transaction().commit()
+    self.tic()
+    
+    self.assertEquals(-150, stool.getInventory(
+                              section_uid=self.section.getUid(),
+                              node_uid=node_uid))
+    self.assertEquals(2, len(stool.getMovementHistoryList(
+                              section_uid=self.section.getUid(),
+                              node_uid=node_uid)))
+
+
+  def test_BalanceTransactionDateInInventoryAPI(self):
+    # check that dates are correctly used for Balance Transaction when making
+    # reports using inventory API
     balance = self.accounting_module.newContent(
                           portal_type='Balance Transaction',
                           destination_section_value=self.section,
-- 
2.30.9