test.erp5.testBankReconciliation.py 31.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
#############################################################################
#
# Copyright (c) 2014 Nexedi SA and Contributors. All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################

"""Tests Bank Reconciliation
"""

import unittest

from DateTime import DateTime

from Products.DCWorkflow.DCWorkflow import ValidationFailed

from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5ReportTestCase
from erp5.component.test.testAccounting import AccountingTestCase

class TestBankReconciliation(AccountingTestCase, ERP5ReportTestCase):
  """Test Bank Reconciliation

  """
  def getBusinessTemplateList(self):
    return AccountingTestCase.getBusinessTemplateList(self) + (
        'erp5_bank_reconciliation',)

  def afterSetUp(self):
    AccountingTestCase.afterSetUp(self)
    self.bank_account = self.section.newContent(
        portal_type='Bank Account',
        price_currency_value=self.portal.currency_module.euro)
    self.bank_account.validate()
    self.tic()

  def test_BankReconciliation_getAccountingTransactionLineList(self):
    account_module = self.account_module
    payment1 = self._makeOne(
              portal_type='Payment Transaction',
              simulation_state='delivered',
              title='First',
              reference='P1',
              source_payment_value=self.bank_account,
              destination_section_value=self.organisation_module.client_1,
              start_date=DateTime(2014, 1, 1),
              lines=(dict(source_value=account_module.bank,
                          source_debit=100,
                          id='bank'),
                     dict(source_value=account_module.receivable,
                          source_credit=100)))

    payment2 = self._makeOne(
              portal_type='Payment Transaction',
              simulation_state='delivered',
              title='Second',
              reference='P2',
              source_payment_value=self.bank_account,
              destination_section_value=self.organisation_module.client_2,
              start_date=DateTime(2014, 1, 2),
              lines=(dict(source_value=account_module.bank,
                          source_debit=200,
                          id='bank'),
                     dict(source_value=account_module.receivable,
                          source_credit=200)))

    self._makeOne(
              portal_type='Payment Transaction',
              simulation_state='delivered',
              title='Not in range',
              source_payment_value=self.bank_account,
              start_date=DateTime(2014, 2, 1),
              lines=(dict(source_value=account_module.bank,
                          source_debit=700,
                          id='bank'),
                     dict(source_value=account_module.receivable,
                          source_credit=700)))

    bank_reconciliation = self.portal.bank_reconciliation_module.newContent(
        portal_type='Bank Reconciliation',
        source_section_value=self.section,
        source_payment_value=self.bank_account,
        stop_date=DateTime(2014, 1, 31))
    self.tic()

    line_list = bank_reconciliation.BankReconciliation_getAccountingTransactionLineList()
    self.assertEqual([payment1.bank, payment2.bank],
                     [line.getObject() for line in line_list])

    # The user can search in the listbox
    line_list = bank_reconciliation.BankReconciliation_getAccountingTransactionLineList(
        Movement_getExplanationTitle='First', # XXX this is the column name
    )
    self.assertEqual([payment1.bank, ],
                     [line.getObject() for line in line_list])

    line_list = bank_reconciliation.BankReconciliation_getAccountingTransactionLineList(
        Movement_getExplanationReference='P2',
    )
    self.assertEqual([payment2.bank, ],
                     [line.getObject() for line in line_list])

    line_list = bank_reconciliation.BankReconciliation_getAccountingTransactionLineList(
        Movement_getMirrorSectionTitle=self.portal.organisation_module.client_2.getTitle(),
    )
    self.assertEqual([payment2.bank, ],
                     [line.getObject() for line in line_list])

    # We manually reconcile.
    payment1.bank.setAggregateValue(bank_reconciliation)
    self.tic()
    # Now the listbox only show non reconciled transactions
    line_list = bank_reconciliation.BankReconciliation_getAccountingTransactionLineList()
    self.assertEqual([payment2.bank, ],
                     [line.getObject() for line in line_list])

    # This listbox can also be used to unreconcile some previously reconciled
    # transactions.
    line_list = bank_reconciliation.BankReconciliation_getAccountingTransactionLineList(
       reconciliation_mode="unreconcile",
    )
    self.assertEqual([payment1.bank, ],
                     [line.getObject() for line in line_list])


  def test_BankReconciliation_getReconciledAccountBalance(self):
    account_module = self.account_module
    payment1 = self._makeOne(
              portal_type='Payment Transaction',
              simulation_state='delivered',
              source_payment_value=self.bank_account,
              start_date=DateTime(2014, 1, 1),
              lines=(dict(source_value=account_module.bank,
                          source_debit=100,
                          id='bank'),
                     dict(source_value=account_module.receivable,
                          source_credit=100)))

    payment2 = self._makeOne(
              portal_type='Payment Transaction',
              simulation_state='delivered',
              source_payment_value=self.bank_account,
              start_date=DateTime(2014, 2, 1),
              lines=(dict(source_value=account_module.bank,
                          source_debit=200,
                          id='bank'),
                     dict(source_value=account_module.receivable,
                          source_credit=200)))

    bank_reconciliation = self.portal.bank_reconciliation_module.newContent(
        portal_type='Bank Reconciliation',
        source_section_value=self.section,
        source_payment_value=self.bank_account,
        stop_date=DateTime(2014, 1, 31))
    self.tic()

    self.assertEqual(100,
        bank_reconciliation.BankReconciliation_getAccountBalance())

    # At this point nothing is reconciled.
    self.assertEqual(0,
        bank_reconciliation.BankReconciliation_getReconciledAccountBalance())

    # Reconciling sets an aggregate relation from payment line to Bank
    # Reconciliation
    payment1.bank.setAggregateValue(bank_reconciliation)
    self.tic()
    # Once the payment line is reconciled, the it is counted in reconciled
    # balance
    self.assertEqual(100,
        bank_reconciliation.BankReconciliation_getReconciledAccountBalance())

    # Payment lines reconciled later are not counted.
    another_bank_reconciliation = self.portal.bank_reconciliation_module.newContent(
        portal_type='Bank Reconciliation',
        source_section_value=self.section,
        source_payment_value=self.bank_account,
        stop_date=DateTime(2014, 3, 31))
    payment2.bank.setAggregateValue(another_bank_reconciliation)
    self.tic()

    self.assertEqual(100,
        bank_reconciliation.BankReconciliation_getReconciledAccountBalance())
    self.assertEqual(100 + 200,
        another_bank_reconciliation.BankReconciliation_getReconciledAccountBalance())

    # "simple" account balance is same as reconciled, as everything is
    # reconciled in this test
    self.assertEqual(100,
        bank_reconciliation.BankReconciliation_getAccountBalance())
    self.assertEqual(100 + 200,
        another_bank_reconciliation.BankReconciliation_getAccountBalance())


  def test_BankReconciliation_fastInput(self):
    account_module = self.account_module
    payment1 = self._makeOne(
              portal_type='Payment Transaction',
              simulation_state='delivered',
              title='First',
              reference='P1',
              source_payment_value=self.bank_account,
              destination_section_value=self.organisation_module.client_1,
              start_date=DateTime(2014, 1, 1),
              lines=(dict(source_value=account_module.bank,
                          source_debit=100,
                          id='bank'),
                     dict(source_value=account_module.receivable,
                          source_credit=100)))

    payment2 = self._makeOne(
              portal_type='Payment Transaction',
              simulation_state='delivered',
              title='Second',
              reference='P2',
              source_payment_value=self.bank_account,
              destination_section_value=self.organisation_module.client_2,
              start_date=DateTime(2014, 1, 2),
              lines=(dict(source_value=account_module.bank,
                          source_debit=200,
                          id='bank'),
                     dict(source_value=account_module.receivable,
                          source_credit=200)))

    bank_reconciliation = self.portal.bank_reconciliation_module.newContent(
        portal_type='Bank Reconciliation',
        source_section_value=self.section,
        source_payment_value=self.bank_account,
        stop_date=DateTime(2014, 1, 31))
    self.tic()

    # View the dialog, to make sure we can display it and to reset selection
    self.portal.REQUEST.set('reset', 1)
    bank_reconciliation.BankReconciliation_viewBankReconciliationFastInputDialog()
    # Call the fast input action script
    list_selection_name = bank_reconciliation\
        .BankReconciliation_viewBankReconciliationFastInputDialog.listbox.get_value(
            'selection_name')
    bank_reconciliation.BankReconciliation_reconcileTransactionList(
        list_selection_name=list_selection_name,
        uids=(payment1.bank.getUid(),),
        reconciliation_mode='reconcile')
    self.tic()

    self.assertEqual(bank_reconciliation, payment1.bank.getAggregateValue())
    self.assertEqual(None, payment2.bank.getAggregateValue())

    # View the dialog, to make sure we can display it and to reset selection
    self.portal.REQUEST.set('reset', 1)
    bank_reconciliation.BankReconciliation_viewBankReconciliationFastInputDialog()
    # Call the fast input action script
    list_selection_name = bank_reconciliation\
        .BankReconciliation_viewBankReconciliationFastInputDialog.listbox.get_value(
            'selection_name')
    bank_reconciliation.BankReconciliation_reconcileTransactionList(
        list_selection_name=list_selection_name,
        uids=(payment1.bank.getUid(),),
        reconciliation_mode='unreconcile')
    self.tic()

    self.assertEqual(None, payment1.bank.getAggregateValue())
    self.assertEqual(None, payment2.bank.getAggregateValue())

  def test_BankReconciliation_Workflow(self):
    account_module = self.account_module
    payment1 = self._makeOne(
              portal_type='Payment Transaction',
              simulation_state='delivered',
              source_payment_value=self.bank_account,
              start_date=DateTime(2014, 1, 1),
              lines=(dict(source_value=account_module.bank,
                          source_debit=100,
                          id='bank'),
                     dict(source_value=account_module.receivable,
                          source_credit=100)))

    bank_reconciliation = self.portal.bank_reconciliation_module.newContent(
        portal_type='Bank Reconciliation',
        source_section_value=self.section,
        source_payment_value=self.bank_account,
        quantity_range_max=0,
        stop_date=DateTime(2014, 1, 31))
    self.tic()

    doActionFor = self.portal.portal_workflow.doActionFor
    self.assertEqual('draft', bank_reconciliation.getValidationState())
    doActionFor(bank_reconciliation, 'open_action')
    self.assertEqual('open', bank_reconciliation.getValidationState())
    doActionFor(bank_reconciliation, 'close_action')
    self.assertEqual('closed', bank_reconciliation.getValidationState())
    doActionFor(bank_reconciliation, 'open_action')
    self.assertEqual('open', bank_reconciliation.getValidationState())

    # Cancel has an interaction to remove all the reconciliations
    payment1.bank.setAggregateValue(bank_reconciliation)
    self.tic()

    doActionFor(bank_reconciliation, 'cancel_action')
    self.assertEqual('cancelled', bank_reconciliation.getValidationState())
    self.tic()
    self.assertEqual(None, payment1.bank.getAggregateValue())

  def test_BankReconciliation_Constraint(self):
    # Add the property sheet for this test.
    self._addPropertySheet('Bank Reconciliation',
        'BankReconciliationConstraint')

    account_module = self.account_module
    previous_bank_reconciliation = self.portal.bank_reconciliation_module.newContent(
        portal_type='Bank Reconciliation',
        source_section_value=self.section,
        source_payment_value=self.bank_account,
        quantity_range_max=100,
        stop_date=DateTime(2014, 1, 31))
    previous_bank_reconciliation.open()

    reconcilied_payment = self._makeOne(
              portal_type='Payment Transaction',
              simulation_state='delivered',
              source_payment_value=self.bank_account,
              start_date=DateTime(2014, 1, 1),
              lines=(dict(source_value=account_module.bank,
                          source_debit=100,
                          aggregate_value=previous_bank_reconciliation,
                          id='bank'),
                     dict(source_value=account_module.receivable,
                          source_credit=100)))

    self._makeOne(
              portal_type='Payment Transaction',
              simulation_state='delivered',
              source_payment_value=self.bank_account,
              start_date=DateTime(2014, 2, 1),
              lines=(dict(source_value=account_module.bank,
                          source_debit=200,
                          id='bank'),
                     dict(source_value=account_module.receivable,
                          source_credit=200)))

    bank_reconciliation = self.portal.bank_reconciliation_module.newContent(
        portal_type='Bank Reconciliation',
        source_section_value=self.section,
        source_payment_value=self.bank_account,
        stop_date=DateTime(2014, 2, 28))
    bank_reconciliation.open()

    constraint = self.portal.portal_property_sheets.BankReconciliationConstraint
    self.assertEqual([], constraint.checkConsistency(bank_reconciliation))

    # reconciled balance must match
    bank_reconciliation.setQuantityRangeMax(10)
    self.assertEqual(
        [str(m.getTranslatedMessage()) for m in bank_reconciliation.checkConsistency()],
        ["Bank statement balance does not match reconciled balance"])
    bank_reconciliation.setQuantityRangeMax(100)
    self.assertEqual(
        [str(m.getTranslatedMessage()) for m in bank_reconciliation.checkConsistency()], [])

    self.assertEqual(
        [str(m.getTranslatedMessage()) for m in previous_bank_reconciliation.checkConsistency()], [])
    previous_bank_reconciliation.setQuantityRangeMax(10)
    self.assertEqual(
        [str(m.getTranslatedMessage()) for m in previous_bank_reconciliation.checkConsistency()],
        ['Bank statement balance does not match reconciled balance'])
    previous_bank_reconciliation.setQuantityRangeMax(100)

    # Previous reconciled balance must match as well
    bank_reconciliation.setStartDate(DateTime(2014, 1, 31))
    bank_reconciliation.setQuantityRangeMin(10)
    self.assertEqual(
        [str(m.getTranslatedMessage()) for m in bank_reconciliation.checkConsistency()],
        ['Previous bank statement balance does not match reconciled balance at previous bank statement date'])

    # edge case, payments on the day of previous statement date, but after 00:00 are reconciled at the
    # previous bank statement date.
    reconcilied_payment.setStartDate(DateTime(2014, 1, 31, 12, 34))
    bank_reconciliation.setQuantityRangeMin(100)
    self.tic()
    self.assertEqual(
        [str(m.getTranslatedMessage()) for m in bank_reconciliation.checkConsistency()], [])
    bank_reconciliation.setQuantityRangeMin(10)

    # These constraints are only verified when we go from open to close state.
    # (that is why the bank reconciliation have been openned for the
    # assertions above)
    with self.assertRaisesRegexp(
        ValidationFailed,
        "Previous bank statement balance does not match reconciled balance at previous bank statement date"):
      self.portal.portal_workflow.doActionFor(
        bank_reconciliation,
        'close_action')

    bank_reconciliation.setQuantityRangeMin(100)
    self.assertEqual(
        [str(m.getTranslatedMessage()) for m in bank_reconciliation.checkConsistency()], [])
    self.portal.portal_workflow.doActionFor(
        bank_reconciliation,
        'close_action')

    bank_reconciliation.setQuantityRangeMax(10)
    # we can pass the transition
    self.portal.portal_workflow.doActionFor(
        bank_reconciliation,
        'open_action')

  def test_BankReconciliation_Report(self):
    account_module = self.account_module
    bank_reconciliation = self.portal.bank_reconciliation_module.newContent(
        portal_type='Bank Reconciliation',
        source_section_value=self.section,
        source_payment_value=self.bank_account,
        quantity_range_max=100,
        stop_date=DateTime(2014, 1, 31))
    bank_reconciliation.open()

    self._makeOne(
              portal_type='Payment Transaction',
              simulation_state='delivered',
              source_payment_value=self.bank_account,
              start_date=DateTime(2014, 1, 1),
              lines=(dict(source_value=account_module.bank,
                          source_debit=100,
                          aggregate_value=bank_reconciliation,
                          id='bank'),
                     dict(source_value=account_module.receivable,
                          source_credit=100)))

    self._makeOne(
              portal_type='Payment Transaction',
              simulation_state='delivered',
              source_payment_value=self.bank_account,
              start_date=DateTime(2014, 1, 2),
              lines=(dict(source_value=account_module.bank,
                          source_debit=200,
                          id='bank'),
                     dict(source_value=account_module.receivable,
                          source_credit=200)))

    report_section_list = self.getReportSectionList(
                               bank_reconciliation,
                               'BankReconciliation_viewBankReconciliationReport')
    self.assertEqual(3, len(report_section_list))

    # First report is just the bank reconciliation view
    self.assertEqual('BankReconciliation_view', report_section_list[0].form_id)

    # Then we have the reconciled lines
    self.assertEqual({'reconciliation_mode': 'unreconcile',
                      'title': 'Reconciled Transactions'},
                      report_section_list[1].selection_params)
    line_list = self.getListBoxLineList(report_section_list[1])
    data_line_list = [l for l in line_list if l.isDataLine()]
    self.assertEqual(1, len(data_line_list))
    self.checkLineProperties(data_line_list[0],
                             debit=100, credit=0)

    # And finally the non reconciled lines
    line_list = self.getListBoxLineList(report_section_list[2])
    self.assertEqual({'reconciliation_mode': 'reconcile',
                      'title': 'Not Reconciled Transactions'},
                      report_section_list[2].selection_params)
    data_line_list = [l for l in line_list if l.isDataLine()]
    self.assertEqual(1, len(data_line_list))
    self.checkLineProperties(data_line_list[0],
                             debit=200, credit=0)

  def test_BankReconciliation_selectNonReconciled(self):
    account_module = self.account_module
    payment1 = self._makeOne(
              portal_type='Payment Transaction',
              simulation_state='delivered',
              source_payment_value=self.bank_account,
              start_date=DateTime(2014, 1, 1),
              lines=(dict(source_value=account_module.bank,
                          source_debit=100,
                          id='bank'),
                     dict(source_value=account_module.receivable,
                          source_credit=100)))

    payment2 = self._makeOne(
              portal_type='Payment Transaction',
              simulation_state='delivered',
              source_payment_value=self.bank_account,
              start_date=DateTime(2014, 2, 1),
              lines=(dict(source_value=account_module.bank,
                          source_debit=200,
                          id='bank'),
                     dict(source_value=account_module.receivable,
                          source_credit=200)))

    bank_reconciliation = self.portal.bank_reconciliation_module.newContent(
        portal_type='Bank Reconciliation',
        source_section_value=self.section,
        source_payment_value=self.bank_account,
        stop_date=DateTime(2014, 1, 31))
    self.tic()

    # we can display the dialog without error
    bank_reconciliation.BankReconciliation_viewSelectNonReconciledTransactionListDialog()
    bank_reconciliation.BankReconciliation_selectNonReconciledTransactionList()
    self.tic()

    # All lines with date < stop_date are reconciled
    self.assertEqual(bank_reconciliation, payment1.bank.getAggregateValue())
    self.assertEqual(None, payment2.bank.getAggregateValue())

  def test_BankReconciliation_multiple_section_using_same_bank_account(self):
    account_module = self.account_module
    self.bank_account.invalidate()
    main_section_bank_account = self.main_section.newContent(
        portal_type='Bank Account',
        price_currency_value=self.portal.currency_module.euro)
    main_section_bank_account.validate()
    
    payment1 = self._makeOne(
              portal_type='Payment Transaction',
              simulation_state='delivered',
              source_payment_value=main_section_bank_account,
              start_date=DateTime(2014, 1, 1),
              lines=(dict(source_value=account_module.bank,
                          source_debit=100,
                          id='bank'),
                     dict(source_value=account_module.receivable,
                          source_credit=100)))

    payment2 = self._makeOne(
              portal_type='Payment Transaction',
              simulation_state='delivered',
              source_section_value=self.main_section,
              source_payment_value=main_section_bank_account,
              start_date=DateTime(2014, 1, 2),
              lines=(dict(source_value=account_module.bank,
                          source_debit=200,
                          id='bank'),
                     dict(source_value=account_module.receivable,
                          source_credit=200)))

    bank_reconciliation = self.portal.bank_reconciliation_module.newContent(
        portal_type='Bank Reconciliation',
        source_section_value=self.main_section,
        source_payment_value=main_section_bank_account,
        stop_date=DateTime(2014, 1, 31))
    self.tic()

    self.assertEqual(300, bank_reconciliation.BankReconciliation_getAccountBalance())
    self.assertEqual(
      [payment1.bank, payment2.bank],
      [x.getObject() for x in bank_reconciliation.BankReconciliation_getAccountingTransactionLineList()])
    
    list_selection_name = bank_reconciliation\
        .BankReconciliation_viewBankReconciliationFastInputDialog.listbox.get_value(
            'selection_name')
    bank_reconciliation.BankReconciliation_reconcileTransactionList(
        list_selection_name=list_selection_name,
        uids=(payment1.bank.getUid(), ),
        reconciliation_mode='reconcile')
    self.tic()
    self.assertEqual(100, bank_reconciliation.BankReconciliation_getReconciledAccountBalance())

  def test_BankReconciliation_internal_transaction(self):
    # Allow internal invoice in accounting module
    module_allowed_type_list = self.portal.portal_types[
        'Accounting Transaction Module'].getTypeAllowedContentTypeList()
    self.portal.portal_types[
        'Accounting Transaction Module'].setTypeAllowedContentTypeList(
          module_allowed_type_list + ['Internal Invoice Transaction',])

    account_module = self.account_module
    main_section_bank_account = self.main_section.newContent(
        portal_type='Bank Account',
        price_currency_value=self.portal.currency_module.euro)
    main_section_bank_account.validate()
    
    internal_transaction = self.portal.accounting_module.newContent(
        portal_type='Internal Invoice Transaction',
        source_section_value=self.section,
        source_payment_value=self.bank_account,
        destination_section_value=self.main_section,
        destination_payment_value=main_section_bank_account,
        resource_value=self.portal.currency_module.euro,
        start_date=DateTime(2014, 1, 1))
    internal_transaction.newContent(
        portal_type='Internal Invoice Transaction Line',
        source_value=account_module.bank,
        destination_value=account_module.bank,
        source_debit=100,
        id='bank')

    internal_transaction.newContent(
        portal_type='Internal Invoice Transaction Line',
        destination_value=account_module.payable,
        source_value=account_module.receivable,
        source_credit=100)
    internal_transaction.start()
    internal_transaction.stop()

    # this internal_transaction.bank line exists in both reconciliation for
    # `section` and for `main_section` and can be reconciled independently by
    # each section
    bank_reconciliation_for_main_section = self.portal.bank_reconciliation_module.newContent(
        portal_type='Bank Reconciliation',
        title='Bank Reconcilisation for Main Section',
        source_section_value=self.main_section,
        source_payment_value=main_section_bank_account,
        stop_date=DateTime(2014, 1, 31))
    self.tic()

    self.assertEqual(-100, bank_reconciliation_for_main_section.BankReconciliation_getAccountBalance())
    self.assertEqual(
        [internal_transaction.bank, ],
        [x.getObject() for x in
            bank_reconciliation_for_main_section.BankReconciliation_getAccountingTransactionLineList()])

    bank_reconciliation_for_section = self.portal.bank_reconciliation_module.newContent(
        portal_type='Bank Reconciliation',
        title='Bank Reconcilisation for Section',
        source_section_value=self.section,
        source_payment_value=self.bank_account,
        stop_date=DateTime(2014, 1, 31))
    self.tic()

    self.assertEqual(100, bank_reconciliation_for_section.BankReconciliation_getAccountBalance())
    self.assertEqual(
        [internal_transaction.bank, ],
        [x.getObject() for x in
            bank_reconciliation_for_section.BankReconciliation_getAccountingTransactionLineList()])
    
    # if `section` reconciles, the line is not reconciled for `main_section`
    list_selection_name = bank_reconciliation_for_section\
        .BankReconciliation_viewBankReconciliationFastInputDialog.listbox.get_value(
            'selection_name')
    bank_reconciliation_for_section.BankReconciliation_reconcileTransactionList(
        list_selection_name=list_selection_name,
        uids=(internal_transaction.bank.getUid(), ),
        reconciliation_mode='reconcile')
    self.tic()
    # reconciled for `section`
    self.assertEqual(100, bank_reconciliation_for_section.BankReconciliation_getReconciledAccountBalance())
    self.assertEqual(
        [],
        [x.getObject() for x in
            bank_reconciliation_for_section.BankReconciliation_getAccountingTransactionLineList()])
    # Not reconciled for `main_section`
    self.assertEqual(0, bank_reconciliation_for_main_section.BankReconciliation_getReconciledAccountBalance())
    self.assertEqual(
        [internal_transaction.bank, ],
        [x.getObject() for x in
            bank_reconciliation_for_main_section.BankReconciliation_getAccountingTransactionLineList()])

    self.assertItemsEqual(
        [bank_reconciliation_for_section],
        internal_transaction.bank.getAggregateValueList())

    # if `main_section` also reconcile, line will be reconciled for both
    list_selection_name = bank_reconciliation_for_main_section\
        .BankReconciliation_viewBankReconciliationFastInputDialog.listbox.get_value(
            'selection_name')
    bank_reconciliation_for_main_section.BankReconciliation_reconcileTransactionList(
        list_selection_name=list_selection_name,
        uids=(internal_transaction.bank.getUid(), ),
        reconciliation_mode='reconcile')
    self.tic()
    # Reconciled for `main_section`
    self.assertEqual(-100, bank_reconciliation_for_main_section.BankReconciliation_getReconciledAccountBalance())
    self.assertEqual(
        [],
        [x.getObject() for x in
            bank_reconciliation_for_main_section.BankReconciliation_getAccountingTransactionLineList()])
    # Still reconciled for `section`
    self.assertEqual(100, bank_reconciliation_for_section.BankReconciliation_getReconciledAccountBalance())
    self.assertEqual(
        [],
        [x.getObject() for x in
            bank_reconciliation_for_section.BankReconciliation_getAccountingTransactionLineList()])

    self.assertItemsEqual(
        [bank_reconciliation_for_section, bank_reconciliation_for_main_section],
        internal_transaction.bank.getAggregateValueList())


    # if `section` un-reconcile, line will be not reconciled for `section`, but still reconciled for `main_section`
    list_selection_name = bank_reconciliation_for_section\
        .BankReconciliation_viewBankReconciliationFastInputDialog.listbox.get_value(
            'selection_name')
    bank_reconciliation_for_section.BankReconciliation_reconcileTransactionList(
        list_selection_name=list_selection_name,
        uids=(internal_transaction.bank.getUid(), ),
        reconciliation_mode='unreconcile')
    self.tic()
    # no longer reconciled for `section`
    self.assertEqual(0, bank_reconciliation_for_section.BankReconciliation_getReconciledAccountBalance())
    self.assertEqual(
        [internal_transaction.bank,],
        [x.getObject() for x in
            bank_reconciliation_for_section.BankReconciliation_getAccountingTransactionLineList()])
    # Still reconciled for `main_section`
    self.assertEqual(-100, bank_reconciliation_for_main_section.BankReconciliation_getReconciledAccountBalance())
    self.assertEqual(
        [],
        [x.getObject() for x in
            bank_reconciliation_for_main_section.BankReconciliation_getAccountingTransactionLineList()])

    self.assertItemsEqual(
        [bank_reconciliation_for_main_section],
        internal_transaction.bank.getAggregateValueList())


def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestBankReconciliation))
  return suite