From 79541e08711998bae496a532b168d1de27c6089a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com>
Date: Thu, 19 Jun 2008 16:36:18 +0000
Subject: [PATCH] when checking if a transaction is balanced, only look at
 lines where an account is defined on this side.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@21720 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 .../AccountingTransactionBalance.py           |  6 +++--
 product/ERP5/tests/testAccounting.py          | 26 +++++++++++++++++++
 2 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/product/ERP5/Constraint/AccountingTransactionBalance.py b/product/ERP5/Constraint/AccountingTransactionBalance.py
index 2dbe0c3dfc..edb8b9f488 100644
--- a/product/ERP5/Constraint/AccountingTransactionBalance.py
+++ b/product/ERP5/Constraint/AccountingTransactionBalance.py
@@ -50,8 +50,10 @@ class AccountingTransactionBalance(Constraint):
     destination_sum = 0
     for line in obj.getMovementList(
           portal_type=obj.getPortalAccountingMovementTypeList()):
-      source_sum += line.getSourceInventoriatedTotalAssetPrice() or 0
-      destination_sum += \
+      if line.getSourceValue() is not None:
+        source_sum += line.getSourceInventoriatedTotalAssetPrice() or 0
+      if line.getDestinationValue() is not None:
+        destination_sum += \
           line.getDestinationInventoriatedTotalAssetPrice() or 0
     
     source_section = obj.getSourceSectionValue()
diff --git a/product/ERP5/tests/testAccounting.py b/product/ERP5/tests/testAccounting.py
index 3640ae5b20..6802d15331 100644
--- a/product/ERP5/tests/testAccounting.py
+++ b/product/ERP5/tests/testAccounting.py
@@ -487,6 +487,32 @@ class TestTransactionValidation(AccountingTestCase):
         line.setDestinationAssetDebit(38.99)
     self.portal.portal_workflow.doActionFor(transaction, 'stop_action')
 
+  def test_NonBalancedDestinationAccountingTransactionNoAccount(self):
+    # Accounting Transactions have to be balanced to be validated,
+    # also for destination
+    transaction = self._makeOne(
+               portal_type='Accounting Transaction',
+               start_date=DateTime('2007/01/02'),
+               destination_section_value=self.organisation_module.client_1,
+               resource='currency_module/yen',
+               lines=(dict(source_value=self.account_module.payable,
+                           destination_asset_debit=39,
+                           source_debit=500),
+                      dict(source_value=self.account_module.receivable,
+                           destination_value=self.account_module.receivable,
+                           destination_asset_credit=38.99,
+                           source_credit=500)))
+    # This is not balanced
+    but there are no accounts on destination
+    self.assertRaises(ValidationFailed,
+        self.portal.portal_workflow.doActionFor,
+        transaction, 'stop_action')
+    for line in transaction.getMovementList():
+      if line.getDestinationId() == 'receivable':
+        line.setDestination(None)
+    # but if there are no accounts defined it's not a problem
+    self.portal.portal_workflow.doActionFor(transaction, 'stop_action')
+
   def test_AccountingTransactionValidationRefusedWithCategoriesAsSections(self):
     # Validating a transaction with categories as sections is refused.
     # See http://wiki.erp5.org/Discussion/AccountingProblems
-- 
2.30.9