testERP5BankingCheckPayment.py 17.7 KB
Newer Older
Yoshinori Okuji's avatar
Yoshinori Okuji committed
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
##############################################################################
#
# Copyright (c) 2005-2006 Nexedi SARL and Contributors. All Rights Reserved.
#                    Yoshinori Okuji <yo@nexedi.com>
#
# 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.
#
##############################################################################


# import requested python module
import os
from zLOG import LOG
from DateTime import DateTime
from Products.CMFCore.utils import getToolByName
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.ERP5Type.tests.Sequence import SequenceList
from Products.DCWorkflow.DCWorkflow import Unauthorized, ValidationFailed
from Testing.ZopeTestCase.PortalTestCase import PortalTestCase
39
from Products.ERP5Banking.tests.TestERP5BankingMixin import TestERP5BankingMixin
Yoshinori Okuji's avatar
Yoshinori Okuji committed
40 41 42 43 44 45 46 47 48 49 50

# Needed in order to have a log file inside the current folder
os.environ['EVENT_LOG_FILE']     = os.path.join(os.getcwd(), 'zLOG.log')
# Define the level of log we want, here is all
os.environ['EVENT_LOG_SEVERITY'] = '-300'

# Define how to launch the script if we don't use runUnitTest script
if __name__ == '__main__':
  execfile(os.path.join(sys.path[0], 'framework.py'))


51
class TestERP5BankingCheckPayment(TestERP5BankingMixin, ERP5TypeTestCase):
Yoshinori Okuji's avatar
Yoshinori Okuji committed
52
  """
53
  Unit test class for the check payment module
Yoshinori Okuji's avatar
Yoshinori Okuji committed
54
  """
55

Yoshinori Okuji's avatar
Yoshinori Okuji committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
  login = PortalTestCase.login

  # pseudo constants
  RUN_ALL_TEST = 1 # we want to run all test
  QUIET = 0 # we don't want the test to be quiet


  def getTitle(self):
    """
      Return the title of the test
    """
    return "ERP5BankingCheckPayment"


  def getBusinessTemplateList(self):
    """
      Return the list of business templates we need to run the test.
      This method is called during the initialization of the unit test by
      the unit test framework in order to know which business templates
      need to be installed to run the test on.
    """
77
    return ('erp5_base',
78 79
            'erp5_trade',
            'erp5_accounting',
80 81 82
            'erp5_banking_core',
            'erp5_banking_inventory',
            'erp5_banking_check')
Yoshinori Okuji's avatar
Yoshinori Okuji committed
83 84 85 86 87 88


  def afterSetUp(self):
    """
      Method called before the launch of the test to initialize some data
    """
89
    self.initDefaultVariable()
Yoshinori Okuji's avatar
Yoshinori Okuji committed
90 91 92 93 94 95 96 97 98 99 100 101
    # the check payment module
    self.check_payment_module = self.getCheckPaymentModule()
    # the checkbook module
    self.checkbook_module = self.getCheckbookModule()

    # Create a user and login as manager to populate the erp5 portal with objects for tests.
    self.createManagerAndLogin()

    # Define static values (only use prime numbers to prevent confusions like 2 * 6 == 3 * 4)
    # variation list is the list of years for banknotes and coins
    self.variation_list = ('variation/1992', 'variation/2003')

102 103 104 105 106 107 108
    self.createFunctionGroupSiteCategory()
    self.createBanknotesAndCoins()

    # Before the test, we need to input the inventory
    inventory_dict_line_1 = {'id' : 'inventory_line_1',
                             'resource': self.billet_10000,
                             'variation_id': ('emission_letter', 'cash_status', 'variation'),
Aurel's avatar
Aurel committed
109
                             'variation_value': ('emission_letter/p', 'cash_status/valid') + self.variation_list,
110
                             'quantity': self.quantity_10000}
111

112 113 114
    inventory_dict_line_2 = {'id' : 'inventory_line_2',
                             'resource': self.billet_200,
                             'variation_id': ('emission_letter', 'cash_status', 'variation'),
Aurel's avatar
Aurel committed
115
                             'variation_value': ('emission_letter/p', 'cash_status/valid') + self.variation_list,
116 117 118 119 120
                             'quantity': self.quantity_200}

    inventory_dict_line_3 = {'id' : 'inventory_line_3',
                             'resource': self.billet_5000,
                             'variation_id': ('emission_letter', 'cash_status', 'variation'),
Aurel's avatar
Aurel committed
121
                             'variation_value': ('emission_letter/p', 'cash_status/valid') + self.variation_list,
122
                             'quantity': self.quantity_5000}
123

124
    line_list = [inventory_dict_line_1, inventory_dict_line_2, inventory_dict_line_3]
125 126 127
    self.bi_counter = self.paris.surface.banque_interne
    self.bi_counter_vault = self.paris.surface.banque_interne.guichet_1.encaisse_des_billets_et_monnaies.sortante
    self.createCashInventory(source=None, destination=self.bi_counter_vault, currency=self.currency_1,
128
                             line_list=line_list)
Aurel's avatar
Aurel committed
129
    self.stepTic()
130 131 132 133 134 135 136
    # create a person and a bank account
    self.person_1 = self.createPerson(id='person_1',
                                      first_name='toto',
                                      last_name='titi')
    self.bank_account_1 = self.createBankAccount(person=self.person_1,
                                                 account_id='bank_account_1',
                                                 currency=self.currency_1,
137
                                                 amount=100000)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
138

139 140
    # create a check
    self.checkbook_1 = self.createCheckbook(id= 'checkbook_1',
Aurel's avatar
Aurel committed
141
                                            vault=self.bi_counter,
142 143
                                            bank_account=self.bank_account_1,
                                            min=50,
144
                                            max=100,
145 146 147 148 149
                                            )

    self.check_1 = self.createCheck(id='check_1',
                                    reference='50',
                                    checkbook=self.checkbook_1)
150

151
    # open counter date and counter
Aurel's avatar
Aurel committed
152
    self.openCounterDate(site=self.paris)
153 154
    self.openCounter(site=self.bi_counter_vault)

155 156
    # now we need to create a user as Manager to do the test
    # in order to have an assigment defined which is used to do transition
Yoshinori Okuji's avatar
Yoshinori Okuji committed
157
    # Create an Organisation that will be used for users assignment
158
    self.checkUserFolderType()
Yoshinori Okuji's avatar
Yoshinori Okuji committed
159
    self.organisation = self.organisation_module.newContent(id='baobab_org', portal_type='Organisation',
Aurel's avatar
Aurel committed
160
                          function='banking', group='baobab',  site='testsite/paris')
161
    # define the user
Yoshinori Okuji's avatar
Yoshinori Okuji committed
162
    user_dict = {
163
        'super_user' : [['Manager'], self.organisation, 'banking/comptable', 'baobab', 'testsite/paris/surface/banque_interne/guichet_1']
Yoshinori Okuji's avatar
Yoshinori Okuji committed
164 165 166 167
      }
    # call method to create this user
    self.createERP5Users(user_dict)
    self.logout()
168
    self.login('super_user')
Yoshinori Okuji's avatar
Yoshinori Okuji committed
169 170 171 172 173 174 175 176


  def stepCheckObjects(self, sequence=None, sequence_list=None, **kwd):
    """
    Check that all the objects we created in afterSetUp or
    that were added by the business template and that we rely
    on are really here.
    """
177
    self.checkResourceCreated()
Yoshinori Okuji's avatar
Yoshinori Okuji committed
178 179 180 181 182 183 184 185 186 187 188 189
    # check that Check Payment Module was created
    self.assertEqual(self.check_payment_module.getPortalType(), 'Check Payment Module')
    # check check payment module is empty
    self.assertEqual(len(self.check_payment_module.objectValues()), 0)


  def stepCheckInitialInventory(self, sequence=None, sequence_list=None, **kwd):
    """
    Check the initial inventory before any operations
    """
    self.simulation_tool = self.getSimulationTool()
    # check we have 5 banknotes of 10000 in encaisse_billets_et_monnaies
190 191
    self.assertEqual(self.simulation_tool.getCurrentInventory(node=self.bi_counter_vault.getRelativeUrl(), resource = self.billet_10000.getRelativeUrl()), 5.0)
    self.assertEqual(self.simulation_tool.getFutureInventory(node=self.bi_counter_vault.getRelativeUrl(), resource = self.billet_10000.getRelativeUrl()), 5.0)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
192
    # check we have 12 coin of 200 in encaisse_billets_et_monnaies
193 194
    self.assertEqual(self.simulation_tool.getCurrentInventory(node=self.bi_counter_vault.getRelativeUrl(), resource = self.billet_200.getRelativeUrl()), 12.0)
    self.assertEqual(self.simulation_tool.getFutureInventory(node=self.bi_counter_vault.getRelativeUrl(), resource = self.billet_200.getRelativeUrl()), 12.0)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
195
    # check we have 24 banknotes of 200 in encaisse_billets_et_monnaies
196 197
    self.assertEqual(self.simulation_tool.getCurrentInventory(node=self.bi_counter_vault.getRelativeUrl(), resource = self.billet_5000.getRelativeUrl()), 24.0)
    self.assertEqual(self.simulation_tool.getFutureInventory(node=self.bi_counter_vault.getRelativeUrl(), resource = self.billet_5000.getRelativeUrl()), 24.0)
198 199 200
    # check the inventory of the bank account
    self.assertEqual(self.simulation_tool.getCurrentInventory(payment=self.bank_account_1.getRelativeUrl()), 100000)
    self.assertEqual(self.simulation_tool.getFutureInventory(payment=self.bank_account_1.getRelativeUrl()), 100000)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
201 202 203 204 205 206 207 208


  def stepCreateCheckPayment(self, sequence=None, sequence_list=None, **kwd):
    """
    Create a check payment document and check it
    """
    self.check_payment = self.check_payment_module.newContent(id = 'check_payment', portal_type = 'Check Payment',
                                         destination_payment_value = self.bank_account_1,
209 210 211
                                         # aggregate_value = self.check_1,
                                         resource_value = self.currency_1,
                                         aggregate_free_text = "50",
Aurel's avatar
Aurel committed
212
                                         # source_value = self.bi_counter,
213
                                         start_date = DateTime().Date(),
Yoshinori Okuji's avatar
Yoshinori Okuji committed
214
                                         source_total_asset_price = 20000.0)
215
    # call set source to go into the interaction workflow to update local roles
Aurel's avatar
Aurel committed
216
    self.check_payment._setSource(self.bi_counter.getRelativeUrl())
Yoshinori Okuji's avatar
Yoshinori Okuji committed
217 218 219
    self.assertNotEqual(self.check_payment, None)
    self.assertEqual(self.check_payment.getTotalPrice(), 0.0)
    self.assertEqual(self.check_payment.getDestinationPayment(), self.bank_account_1.getRelativeUrl())
220
    self.assertEqual(self.check_payment.getAggregateFreeText(), self.check_1.getReference())
Yoshinori Okuji's avatar
Yoshinori Okuji committed
221
    self.assertEqual(self.check_payment.getSourceTotalAssetPrice(), 20000.0)
Aurel's avatar
Aurel committed
222
    self.assertEqual(self.check_payment.getSource(), self.bi_counter.getRelativeUrl())
Yoshinori Okuji's avatar
Yoshinori Okuji committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238

    # the initial state must be draft
    self.assertEqual(self.check_payment.getSimulationState(), 'draft')

    # source reference must be automatically generated
    self.check_payment.setSourceReference(self.check_payment.Baobab_getUniqueReference())
    self.assertNotEqual(self.check_payment.getSourceReference(), None)
    self.assertNotEqual(self.check_payment.getSourceReference(), '')

  def stepCheckConsistency(self, sequence=None, sequence_list=None, **kwd):
    """
    Check the consistency of the check payment

    FIXME: check if the transition fails when a category or property is invalid.
    """
    self.workflow_tool.doActionFor(self.check_payment, 'plan_action', wf_id='check_payment_workflow')
239
    self.assertNotEqual(self.check_payment.getAggregateValue(), None)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
240 241
    self.assertEqual(self.check_payment.getSimulationState(), 'planned')

242

Yoshinori Okuji's avatar
Yoshinori Okuji committed
243 244 245 246 247 248 249 250 251 252 253 254
  def stepSendToCounter(self, sequence=None, sequence_list=None, **kwd):
    """
    Send the check payment to the counter

    FIXME: check if the transition fails when a category or property is invalid.
    """
    self.workflow_tool.doActionFor(self.check_payment, 'confirm_action', wf_id='check_payment_workflow')
    self.assertEqual(self.check_payment.getSimulationState(), 'confirmed')

    self.assertEqual(self.check_payment.getSourceTotalAssetPrice(),
                     - self.check_payment.getTotalPrice(portal_type = 'Banking Operation Line'))

255 256 257 258 259 260
  def stepCheckConfirmedInventory(self, sequence=None, sequence_list=None, **kwd):
    """
    Check the inventoryinb state confirmed
    """
    self.simulation_tool = self.getSimulationTool()
    # check we have 5 banknotes of 10000 in encaisse_billets_et_monnaies
261 262
    self.assertEqual(self.simulation_tool.getCurrentInventory(node=self.bi_counter_vault.getRelativeUrl(), resource = self.billet_10000.getRelativeUrl()), 5.0)
    self.assertEqual(self.simulation_tool.getFutureInventory(node=self.bi_counter_vault.getRelativeUrl(), resource = self.billet_10000.getRelativeUrl()), 5.0)
263
    # check we have 12 coin of 200 in encaisse_billets_et_monnaies
264 265
    self.assertEqual(self.simulation_tool.getCurrentInventory(node=self.bi_counter_vault.getRelativeUrl(), resource = self.billet_200.getRelativeUrl()), 12.0)
    self.assertEqual(self.simulation_tool.getFutureInventory(node=self.bi_counter_vault.getRelativeUrl(), resource = self.billet_200.getRelativeUrl()), 12.0)
266
    # check we have 24 banknotes of 200 in encaisse_billets_et_monnaies
267 268
    self.assertEqual(self.simulation_tool.getCurrentInventory(node=self.bi_counter_vault.getRelativeUrl(), resource = self.billet_5000.getRelativeUrl()), 24.0)
    self.assertEqual(self.simulation_tool.getFutureInventory(node=self.bi_counter_vault.getRelativeUrl(), resource = self.billet_5000.getRelativeUrl()), 24.0)
269 270 271
    # check the inventory of the bank account, must be planned to be decrease by 20000
    self.assertEqual(self.simulation_tool.getCurrentInventory(payment=self.bank_account_1.getRelativeUrl()), 100000)
    self.assertEqual(self.simulation_tool.getFutureInventory(payment=self.bank_account_1.getRelativeUrl()), 80000)
272

273

Yoshinori Okuji's avatar
Yoshinori Okuji committed
274 275 276 277 278 279
  def stepInputCashDetails(self, sequence=None, sequence_list=None, **kwd):
    """
    Input cash details
    """
    self.addCashLineToDelivery(self.check_payment, 'line_1', 'Cash Delivery Line', self.billet_10000,
            ('emission_letter', 'cash_status', 'variation'),
Aurel's avatar
Aurel committed
280
            ('emission_letter/p', 'cash_status/valid') + self.variation_list[1:],
Yoshinori Okuji's avatar
Yoshinori Okuji committed
281 282 283 284 285
            {self.variation_list[1] : 1})
    self.assertEqual(self.check_payment.line_1.getPrice(), 10000)

    self.addCashLineToDelivery(self.check_payment, 'line_2', 'Cash Delivery Line', self.billet_5000,
            ('emission_letter', 'cash_status', 'variation'),
Aurel's avatar
Aurel committed
286
            ('emission_letter/p', 'cash_status/valid') + self.variation_list[1:],
Yoshinori Okuji's avatar
Yoshinori Okuji committed
287 288 289 290 291 292 293 294 295 296 297
            {self.variation_list[1] : 2})
    self.assertEqual(self.check_payment.line_2.getPrice(), 5000)

  def stepPay(self, sequence=None, sequence_list=None, **kwd):
    """
    Pay the check payment

    FIXME: check if the transition fails when a category or property is invalid.
    """
    self.assertEqual(self.check_payment.getSourceTotalAssetPrice(),
                     self.check_payment.getTotalPrice(portal_type = 'Cash Delivery Cell'))
298
    self.workflow_tool.doActionFor(self.check_payment, 'deliver_action', wf_id='check_payment_workflow')
Yoshinori Okuji's avatar
Yoshinori Okuji committed
299 300
    self.assertEqual(self.check_payment.getSimulationState(), 'delivered')

301 302 303 304 305 306 307

  def stepCheckFinalInventory(self, sequence=None, sequence_list=None, **kwd):
    """
    Check the initial inventory before any operations
    """
    self.simulation_tool = self.getSimulationTool()
    # check we have 5 banknotes of 10000 in encaisse_billets_et_monnaies
308 309
    self.assertEqual(self.simulation_tool.getCurrentInventory(node=self.bi_counter_vault.getRelativeUrl(), resource = self.billet_10000.getRelativeUrl()), 4.0)
    self.assertEqual(self.simulation_tool.getFutureInventory(node=self.bi_counter_vault.getRelativeUrl(), resource = self.billet_10000.getRelativeUrl()), 4.0)
310
    # check we have 12 coin of 200 in encaisse_billets_et_monnaies
311 312
    self.assertEqual(self.simulation_tool.getCurrentInventory(node=self.bi_counter_vault.getRelativeUrl(), resource = self.billet_200.getRelativeUrl()), 12.0)
    self.assertEqual(self.simulation_tool.getFutureInventory(node=self.bi_counter_vault.getRelativeUrl(), resource = self.billet_200.getRelativeUrl()), 12.0)
313
    # check we have 24 banknotes of 200 in encaisse_billets_et_monnaies
314 315
    self.assertEqual(self.simulation_tool.getCurrentInventory(node=self.bi_counter_vault.getRelativeUrl(), resource = self.billet_5000.getRelativeUrl()), 22.0)
    self.assertEqual(self.simulation_tool.getFutureInventory(node=self.bi_counter_vault.getRelativeUrl(), resource = self.billet_5000.getRelativeUrl()), 22.0)
316 317 318 319
    # check the final inventory of the bank account
    self.assertEqual(self.simulation_tool.getCurrentInventory(payment=self.bank_account_1.getRelativeUrl()), 80000)
    self.assertEqual(self.simulation_tool.getFutureInventory(payment=self.bank_account_1.getRelativeUrl()), 80000)

Yoshinori Okuji's avatar
Yoshinori Okuji committed
320 321 322 323 324 325 326 327 328 329 330 331

  def test_01_ERP5BankingCheckPayment(self, quiet=QUIET, run=RUN_ALL_TEST):
    """
    Define the sequence of step that will be play
    """
    if not run: return
    sequence_list = SequenceList()
    # define the sequence
    sequence_string = 'Tic CheckObjects Tic CheckInitialInventory ' \
                      'CreateCheckPayment Tic ' \
                      'CheckConsistency Tic ' \
                      'SendToCounter Tic ' \
332
                      'CheckConfirmedInventory ' \
Yoshinori Okuji's avatar
Yoshinori Okuji committed
333
                      'InputCashDetails Tic ' \
334 335
                      'Pay Tic ' \
                      'CheckFinalInventory '
Yoshinori Okuji's avatar
Yoshinori Okuji committed
336 337 338 339 340 341 342 343 344 345 346 347 348
    sequence_list.addSequenceString(sequence_string)
    # play the sequence
    sequence_list.play(self)

# define how we launch the unit test
if __name__ == '__main__':
  framework()
else:
  import unittest
  def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(TestERP5BankingCheckPayment))
    return suite