From 62ebc57321f3513bc37a883f56f1f75f1cec8d9b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com>
Date: Mon, 17 Sep 2007 13:22:17 +0000
Subject: [PATCH] Make sure balance transaction line from
 getMovementHistoryList can be retrieved with getObject

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@16407 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/Document/BalanceTransaction.py |  28 ++++-
 product/ERP5/tests/testAccounting.py        | 116 ++++++++++++++++++++
 2 files changed, 142 insertions(+), 2 deletions(-)

diff --git a/product/ERP5/Document/BalanceTransaction.py b/product/ERP5/Document/BalanceTransaction.py
index 12cbbbbfc5..c296a711ba 100644
--- a/product/ERP5/Document/BalanceTransaction.py
+++ b/product/ERP5/Document/BalanceTransaction.py
@@ -227,6 +227,8 @@ class BalanceTransaction(AccountingTransaction, Inventory):
               dict(destination_uid=node_uid,
                    destination_section_uid=section_uid,
                    resource_uid=movement.getResourceUid(),
+                   uid=movement.getUid(),
+                   relative_url=movement.getRelativeUrl(),
                    quantity=movement.getQuantity(),
                    total_price=movement\
                     .getDestinationInventoriatedTotalAssetPrice(), ))
@@ -246,6 +248,8 @@ class BalanceTransaction(AccountingTransaction, Inventory):
                    destination_section_uid=section_uid,
                    source_section_uid=mirror_section_uid,
                    resource_uid=movement.getResourceUid(),
+                   uid=movement.getUid(),
+                   relative_url=movement.getRelativeUrl(),
                    quantity=movement.getQuantity(),
                    total_price=movement\
                     .getDestinationInventoriatedTotalAssetPrice(), ))
@@ -265,6 +269,8 @@ class BalanceTransaction(AccountingTransaction, Inventory):
                    destination_section_uid=section_uid,
                    destination_payment_uid=payment_uid,
                    resource_uid=movement.getResourceUid(),
+                   uid=movement.getUid(),
+                   relative_url=movement.getRelativeUrl(),
                    quantity=movement.getQuantity(),
                    total_price=movement\
                     .getDestinationInventoriatedTotalAssetPrice(), ))
@@ -288,7 +294,7 @@ class BalanceTransaction(AccountingTransaction, Inventory):
         matching_diff = None
         for diff in stock_diff_list:
           for prop in [k for k in diff.keys() if k not in ('quantity',
-                          'total_price')]:
+                          'total_price', 'uid', 'relative_url')]:
             if diff[prop] != new_stock.get(prop):
               break
           else:
@@ -347,10 +353,26 @@ class BalanceTransaction(AccountingTransaction, Inventory):
     def factory(*args, **kw):
       doc = newTempBalanceTransactionLine(self, self.getId(),
                                          uid=self.getUid())
+      relative_url = kw.pop('relative_url', None)
       destination_total_asset_price = kw.pop('total_price', None)
       if destination_total_asset_price is not None:
         kw['destination_total_asset_price'] = destination_total_asset_price
       doc._edit(*args, **kw)
+
+      if relative_url:
+        
+        def URLGetter(url):
+          def getRelativeUrl():
+            return url
+          return getRelativeUrl
+        doc.getRelativeUrl = URLGetter(relative_url)
+        
+        def PathGetter(path):
+          def getPath():
+            return path
+          return getPath
+        doc.getPath = PathGetter(relative_url)
+
       return doc
 
     return factory
@@ -396,6 +418,8 @@ class BalanceTransaction(AccountingTransaction, Inventory):
     
     # Catalog differences calculated from lines
     self.portal_catalog.catalogObjectList(stock_object_list,
-         method_id_list=('z_catalog_stock_list',),
+         method_id_list=('z_catalog_stock_list',
+                         'z_catalog_object_list',
+                         'z_catalog_movement_category_list'),
          disable_cache=1, check_uid=0)
     
diff --git a/product/ERP5/tests/testAccounting.py b/product/ERP5/tests/testAccounting.py
index faafe92b5d..dec15f41f2 100644
--- a/product/ERP5/tests/testAccounting.py
+++ b/product/ERP5/tests/testAccounting.py
@@ -219,6 +219,13 @@ class AccountingTestCase(ERP5TypeTestCase):
 class TestClosingPeriod(AccountingTestCase):
   """Various tests for closing the period.
   """
+  def beforeTearDown(self):
+    # we manually remove the content of stock table, because unindexObject
+    # might not work correctly on Balance Transaction, and we don't want
+    # leave something in stock table that will change the next test.
+    self.portal.erp5_sql_connection.manage_test('truncate stock')
+    get_transaction().commit()
+
   def test_createBalanceOnNode(self):
     period = self.section.newContent(portal_type='Accounting Period')
     period.setStartDate(DateTime(2006, 1, 1))
@@ -944,7 +951,116 @@ class TestClosingPeriod(AccountingTestCase):
                               node_uid=node_uid))
     
 
+  def test_BalanceTransactionLineBrainGetObject(self):
+    # Balance Transaction Line can be retrieved using Brain.getObject
+    balance = self.accounting_module.newContent(
+                          portal_type='Balance Transaction',
+                          destination_section_value=self.section,
+                          start_date=DateTime(2006, 12, 31),
+                          resource_value=self.currency_module.euro,)
+    balance_line = balance.newContent(
+                portal_type='Balance Transaction Line',
+                destination_value=self.account_module.receivable,
+                destination_debit=100,)
+    balance_line2 = balance.newContent(
+                portal_type='Balance Transaction Line',
+                destination_value=self.account_module.payable,
+                destination_credit=100,)
+    balance.stop()
+    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))
+    # there is one line in getMovementHistoryList:
+    mvt_history_list = stool.getMovementHistoryList(
+                              section_uid=self.section.getUid(),
+                              node_uid=node_uid)
+    self.assertEquals(1, len(mvt_history_list))
+    self.assertEquals(mvt_history_list[0].getObject(),
+                      balance_line)
+
+    # There is also one line on payable account
+    node_uid = self.account_module.payable.getUid()
+    mvt_history_list = stool.getMovementHistoryList(
+                              section_uid=self.section.getUid(),
+                              node_uid=node_uid)
+    self.assertEquals(1, len(mvt_history_list))
+    self.assertEquals(mvt_history_list[0].getObject(),
+                      balance_line2)
+
+  def test_BalanceTransactionDate(self):
+    # check that dates are correctly used for Balance Transaction indexing
+    balance = self.accounting_module.newContent(
+                          portal_type='Balance Transaction',
+                          destination_section_value=self.section,
+                          start_date=DateTime(2006, 12, 31),
+                          resource_value=self.currency_module.euro,)
+    balance_line = balance.newContent(
+                portal_type='Balance Transaction Line',
+                destination_value=self.account_module.receivable,
+                destination_debit=100,)
+    balance.stop()
+    get_transaction().commit()
+    self.tic()
+    
+    stool = self.portal.portal_simulation
+    # the account 'receivable' has a balance of 100 after 2006/12/31
+    node_uid = self.account_module.receivable.getUid()
+    self.assertEquals(100, stool.getInventory(
+                              at_date=DateTime(2006, 12, 31),
+                              section_uid=self.section.getUid(),
+                              node_uid=node_uid))
+    self.assertEquals(1, len(stool.getMovementHistoryList(
+                              at_date=DateTime(2006, 12, 31),
+                              section_uid=self.section.getUid(),
+                              node_uid=node_uid)))
+    # and 0 before
+    self.assertEquals(0, stool.getInventory(
+                              at_date=DateTime(2005, 12, 31),
+                              section_uid=self.section.getUid(),
+                              node_uid=node_uid))
+    self.assertEquals(0, len(stool.getMovementHistoryList(
+                              at_date=DateTime(2005, 12, 31),
+                              section_uid=self.section.getUid(),
+                              node_uid=node_uid)))
+
+
+  def test_BalanceTransactionLineInventoryAPIParentPortalType(self):
+    # related keys like parent_portal_type= can be used in inventory API to get
+    # balance transaction lines
+    balance = self.accounting_module.newContent(
+                          portal_type='Balance Transaction',
+                          destination_section_value=self.section,
+                          start_date=DateTime(2006, 12, 31),
+                          resource_value=self.currency_module.euro,)
+    balance_line = balance.newContent(
+                portal_type='Balance Transaction Line',
+                destination_value=self.account_module.receivable,
+                destination_debit=100,)
+    balance.stop()
+    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(
+                              parent_portal_type='Balance Transaction',
+                              section_uid=self.section.getUid(),
+                              node_uid=node_uid))
+    # there is one line in getMovementHistoryList:
+    mvt_history_list = stool.getMovementHistoryList(
+                              parent_portal_type='Balance Transaction',
+                              section_uid=self.section.getUid(),
+                              node_uid=node_uid)
+    self.assertEquals(1, len(mvt_history_list))
 
+  # TODO : test deletion ?
 
 class TestAccounting(ERP5TypeTestCase):
   """The first test for Accounting
-- 
2.30.9