testConversionInSimulation.py 37.9 KB
Newer Older
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
#############################################################################
#
# Copyright (c) 2006 Nexedi SARL and Contributors. All Rights Reserved.
#                    Aurelien Calonne <aurel@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.
#
##############################################################################
28

29
import unittest
30

31 32 33 34 35
from DateTime import DateTime
from zLOG import LOG
from Products.ERP5Type.UnrestrictedMethod import UnrestrictedMethod
from Testing import ZopeTestCase
from Products.ERP5.tests.testAccounting import AccountingTestCase
36
from Products.ERP5.tests.utils import newSimulationExpectedFailure
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
from AccessControl.SecurityManagement import newSecurityManager
QUIET = False
run_all_test = True



def printAndLog(msg):
  """
  A utility function to print a message
  to the standard output and to the LOG
  at the same time
  """
  msg = str(msg)
  ZopeTestCase._print('\n ' + msg)
  LOG('Testing... ', 0, msg)

53 54
class TestConversionInSimulation(AccountingTestCase):

55 56 57 58 59 60
  username = 'username'
  default_region = "europe/west/france"
  vat_gap = 'fr/pcg/4/44/445/4457/44571'
  vat_rate = 0.196
  sale_gap = 'fr/pcg/7/70/707/7071/70712'
  customer_gap = 'fr/pcg/4/41/411'
61 62 63 64
  mail_delivery_mode = 'by_mail'
  cpt_incoterm = 'cpt'
  unit_piece_quantity_unit = 'unit/piece'
  mass_quantity_unit = 'mass/kg'
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

  # (account_id, account_gap, account_type)
  account_definition_list = (
      ('receivable_vat', vat_gap, 'liability/payable/collected_vat',),
      ('sale', sale_gap, 'income'),
      ('customer', customer_gap, 'asset/receivable'),
      ('refundable_vat', vat_gap, 'asset/receivable/refundable_vat'),
      ('purchase', sale_gap, 'expense'),
      ('supplier', customer_gap, 'liability/payable'),
      )
  # (line_id, source_account_id, destination_account_id, line_quantity)
  transaction_line_definition_list = (
      ('income', 'sale', 'purchase', 1.0),
      ('receivable', 'customer', 'supplier', -1.0 - vat_rate),
      ('collected_vat', 'receivable_vat', 'refundable_vat', vat_rate),
      )
81

82 83 84 85 86 87 88 89 90
  def createCategoriesInCategory(self, category, category_id_list):
    for category_id in category_id_list:
      child = category
      for id in category_id.split('/'):
        try:
          child = child[id]
        except KeyError:
          child = child.newContent(id)

91 92
  def createCategories(self):
    """Create the categories for our test. """
93 94 95 96 97 98 99 100 101 102 103
    category_tool = self.getCategoryTool()
    _ = self.createCategoriesInCategory
    _(category_tool.region, [self.default_region])
    _(category_tool.gap, [self.vat_gap, self.sale_gap, self.customer_gap])
    _(category_tool.delivery_mode, [self.mail_delivery_mode])
    _(category_tool.incoterm, [self.cpt_incoterm])
    _(category_tool.quantity_unit,
      [self.unit_piece_quantity_unit, self.mass_quantity_unit])
    _(category_tool.trade_phase, ['default'])
    _(category_tool.trade_phase.default,
      ['accounting', 'delivery', 'invoicing', 'discount', 'tax', 'payment'])
104
    _(category_tool.product_line, ['apparel'])
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

  def _solveDivergence(self, obj, property, decision, group='line'):
    """
      Check if simulation movement are disconnected
    """
    kw = {'%s_group_listbox' % group:{}}
    for divergence in obj.getDivergenceList():
      if divergence.getProperty('tested_property') != property:
        continue
      sm_url = divergence.getProperty('simulation_movement').getRelativeUrl()
      kw['line_group_listbox']['%s&%s' % (sm_url, property)] = {
        'choice':decision}
    self.portal.portal_workflow.doActionFor(
      obj,
      'solve_divergence_action',
      **kw)
121

122
  def afterSetUp(self):
123 124
    super(TestConversionInSimulation, self).afterSetUp()
    super(TestConversionInSimulation, self).login()
125
    self.createCategories()
126
    self.createAndValidateAccounts()
127 128 129
    self.login()

  def beforeTearDown(self):
130
    self.abort()
131 132 133 134 135 136 137 138 139 140 141
    # clear modules if necessary
    currency_list = ('euro', 'yen', 'usd')
    module = self.portal.currency_module
    module.manage_delObjects([x for x in module.objectIds()
                        if x not in currency_list])
    for currency_id in currency_list:
      currency = self.currency_module._getOb(currency_id, None)
      if currency is not None:
        currency.manage_delObjects([x.getId() for x in
                currency.objectValues(
                  portal_type='Currency Exchange Line')])
142 143 144 145
    if getattr(self, 'business_process', None) is not None:
      self.business_process.getParentValue()._delObject(
        self.business_process.getId()
      )
146
    self.tic()
147 148
    super(TestConversionInSimulation, self).beforeTearDown()

149 150 151 152 153 154
  def login(self,name=username, quiet=0, run=run_all_test):
    uf = self.getPortal().acl_users
    uf._doAddUser(self.username, '', ['Assignee', 'Assignor',
            'Author'], [])
    user = uf.getUserById(self.username).__of__(uf)
    newSecurityManager(None, user)
155

156 157 158 159 160 161 162 163
  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.
    """
    return ('erp5_base',
164 165
            # XXX erp5_core is still not clean. Remove when not needed
            'erp5_core_proxy_field_legacy',
166
            'erp5_pdm',
167
            'erp5_simulation',
168
            'erp5_trade',
169
            'erp5_accounting',
170 171 172
            'erp5_accounting_ui_test',
            'erp5_invoicing',
            'erp5_simplified_invoicing',
173 174 175 176
            'erp5_configurator_standard_solver',
            'erp5_configurator_standard_trade_template',
            'erp5_configurator_standard_accounting_template',
            'erp5_configurator_standard_invoicing_template',
177
            'erp5_simulation_test',
178 179
            )

180 181
  def createAndValidateAccounts(self):
    account_module = self.portal.account_module
182 183 184
    for account_id, account_gap, account_type \
               in self.account_definition_list:
      if not account_id in account_module.objectIds():
185 186 187
        account = account_module.newContent(account_id,
           gap=account_gap, account_type=account_type)
        self.portal.portal_workflow.doActionFor(account, 'validate_action')
188

189
  def createBusinessProcess(self, resource=None):
190 191 192 193 194 195 196 197 198 199 200 201 202 203
    module = self.portal.business_process_module
    name = self.__class__.__name__ + '_' + self._testMethodName
    self.business_process = business_process = module.newContent(
      name,
      reference=name,
    )
    # copy business links from the default erp5 Business Process
    source = module['erp5_default_business_process']
    business_link_id_list = [obj.getId()
                             for obj in source.objectValues()
                             if obj.getPortalType() == 'Business Link']
    business_process.manage_pasteObjects(
      source.manage_copyObjects(business_link_id_list)
    )
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    trade_phase = self.getCategoryTool().trade_phase
    kw = dict(portal_type='Trade Model Path',
              trade_date='trade_phase/default/order')
    business_process.newContent(
      reference='default_path',
      trade_phase_value_list=[x for x in trade_phase.default.objectValues()
                              if x.getId() != 'accounting'],
      **kw)
    kw.update(trade_phase='default/accounting',
              resource_value=resource,
              membership_criterion_base_category_list=(
                'destination_region',
                'product_line'),
              membership_criterion_category_list=(
                'destination_region/region/' + self.default_region,
                'product_line/apparel'))
220 221
    for line_id, line_source_id, line_destination_id, line_ratio in \
        self.transaction_line_definition_list:
222
      trade_model_path = business_process.newContent(
223 224 225 226 227
        reference='acounting_' + line_id,
        efficiency=line_ratio,
        source='account_module/' + line_source_id,
        destination='account_module/' + line_destination_id,
        **kw)
228 229 230 231 232
      # A trade model path already exist for root simulation movements
      # (Accounting Transaction Root Simulation Rule).
      # The ones we are creating are for Invoice Transaction Simulation Rule.
      trade_model_path._setCriterionPropertyList(('portal_type',))
      trade_model_path.setCriterion('portal_type', 'Simulation Movement')
233 234
    self.tic()

Kazuhiko Shiozaki's avatar
Kazuhiko Shiozaki committed
235 236 237 238
  def stepPackingListBuilderAlarm(self, sequence=None,
                                  sequence_list=None, **kw):
    self.portal.portal_alarms.packing_list_builder_alarm.activeSense()

239 240 241 242 243 244 245 246 247 248 249 250 251
  def test_01_simulation_movement_destination_asset_price(self,quiet=0,
          run=run_all_test):
    """
    tests that when resource on simulation movements is different
     from the price currency of the destination section, that
      destination_asset_price is set on the movement
      """
    if not run: return
    if not quiet:
       printAndLog('test_01_simulation_movement_destination_asset_price')
    resource = self.portal.product_module.newContent(
                    portal_type='Product',
                    title='Resource',
252
                    product_line='apparel')
253 254 255 256 257 258 259 260 261 262 263
    currency = self.portal.currency_module.newContent(
                                portal_type='Currency',
                                title='euro')
    currency.setBaseUnitQuantity(0.01)
    new_currency = \
           self.portal.currency_module.newContent(portal_type='Currency')
    new_currency.setReference('XOF')
    new_currency.setTitle('Francs CFA')
    new_currency.setBaseUnitQuantity(1.00)
    self.tic()#execute transaction
    x_curr_ex_line = currency.newContent(
264 265
                                  portal_type='Currency Exchange Line',
                        price_currency=new_currency.getRelativeUrl())
266 267 268
    x_curr_ex_line.setTitle('Euro to Francs CFA')
    x_curr_ex_line.setBasePrice(655.957)
    x_curr_ex_line.setStartDate(DateTime(2008,10,21))
269
    x_curr_ex_line.setStopDate(DateTime(2008,10,22))
270
    x_curr_ex_line.validate()
271
    self.createBusinessProcess(currency)
272
    self.tic()#execute transaction
273 274 275
    client = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
                            title='Client',
276
                price_currency=new_currency.getRelativeUrl(),
277
                        default_address_region=self.default_region)
278 279 280
    vendor = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
                            title='Vendor',
281
                         default_address_region=self.default_region)
282 283 284 285 286 287 288 289
    order = self.portal.sale_order_module.newContent(
                              portal_type='Sale Order',
                              source_value=vendor,
                              source_section_value=vendor,
                              destination_value=client,
                              destination_section_value=client,
                              start_date=DateTime(2008,10, 21),
                              price_currency_value=currency,
290
                              specialise_value=self.business_process,
291 292 293 294 295
                              title='Order')
    order_line = order.newContent(portal_type='Sale Order Line',
                                  resource_value=resource,
                                  quantity=1,
                                  price=2)
296

297 298
    order.confirm()
    self.tic()
Kazuhiko Shiozaki's avatar
Kazuhiko Shiozaki committed
299 300
    self.stepPackingListBuilderAlarm()
    self.tic()
301

302 303
    related_applied_rule = order.getCausalityRelatedValue(
                             portal_type='Applied Rule')
304 305 306
    order_movement = related_applied_rule.contentValues()[0]
    delivery_applied_rule = order_movement.contentValues()[0]
    delivery_movement = delivery_applied_rule.contentValues()[0]
307 308 309 310 311 312 313 314 315 316
    invoice_applied_rule = delivery_movement.contentValues()[0]
    invoice_movement = invoice_applied_rule.contentValues()[0]
    invoice_transaction_applied_rule = invoice_movement.contentValues()[0]
    invoice_transaction_movement_1 =\
         invoice_transaction_applied_rule.contentValues()[0]
    self.assertEquals(currency,
          invoice_transaction_movement_1.getResourceValue())
    self.assertEquals(currency,
          delivery_movement.getPriceCurrencyValue())
    self.assertEquals\
317
     (invoice_transaction_movement_1.getDestinationTotalAssetPrice(),
318
        round(655.957*delivery_movement.getTotalPrice()))
319 320
    self.assertEquals\
        (invoice_transaction_movement_1.getSourceTotalAssetPrice(),
321
        None)
322 323 324 325 326 327 328 329
    invoice_transaction_movement_2 =\
         invoice_transaction_applied_rule.contentValues()[1]
    self.assertEquals(currency,
          invoice_transaction_movement_2.getResourceValue())
    self.assertEquals(currency,
          delivery_movement.getPriceCurrencyValue())
    self.assertEquals\
        (invoice_transaction_movement_2.getDestinationTotalAssetPrice(),
330 331
        round(655.957*delivery_movement.getTotalPrice()))

332 333 334 335 336 337 338 339 340 341 342 343 344
  def test_01_simulation_movement_source_asset_price(self,quiet=0,
          run=run_all_test):
    """
    tests that when resource on simulation movements is different
     from the price currency of the source section, that
      source_asset_price is set on the movement
    """
    if not run: return
    if not quiet:
       printAndLog('test_01_simulation_movement_source_asset_price')
    resource = self.portal.product_module.newContent(
                    portal_type='Product',
                    title='Resource',
345
                    product_line='apparel')
346 347 348 349 350 351 352 353 354 355 356
    currency = self.portal.currency_module.newContent(
                                portal_type='Currency',
                                title='euro')
    currency.setBaseUnitQuantity(0.01)
    new_currency = \
           self.portal.currency_module.newContent(portal_type='Currency')
    new_currency.setReference('XOF')
    new_currency.setTitle('Francs CFA')
    new_currency.setBaseUnitQuantity(1.00)
    self.tic()#execute transaction
    x_curr_ex_line = currency.newContent(
357 358
                              portal_type='Currency Exchange Line',
                price_currency=new_currency.getRelativeUrl())
359 360 361
    x_curr_ex_line.setTitle('Euro to Francs CFA')
    x_curr_ex_line.setBasePrice(655.957)
    x_curr_ex_line.setStartDate(DateTime(2008,10,21))
362
    x_curr_ex_line.setStopDate(DateTime(2008,10,22))
363
    x_curr_ex_line.validate()
364
    self.createBusinessProcess(currency)
365 366 367
    self.tic()#execute transactio
    client = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
368
                            title='Client',
369 370 371 372
                            default_address_region=self.default_region)
    vendor = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
                            title='Vendor',
373
                            price_currency=new_currency.getRelativeUrl(),
374 375 376 377 378 379 380 381 382
                            default_address_region=self.default_region)
    order = self.portal.sale_order_module.newContent(
                              portal_type='Sale Order',
                              source_value=vendor,
                              source_section_value=vendor,
                              destination_value=client,
                              destination_section_value=client,
                              start_date=DateTime(2008,10, 21),
                              price_currency_value=currency,
383
                              specialise_value=self.business_process,
384 385 386 387 388
                              title='Order')
    order_line = order.newContent(portal_type='Sale Order Line',
                                  resource_value=resource,
                                  quantity=1,
                                  price=2)
389

390 391
    order.confirm()
    self.tic()
Kazuhiko Shiozaki's avatar
Kazuhiko Shiozaki committed
392 393
    self.stepPackingListBuilderAlarm()
    self.tic()
394

395 396
    related_applied_rule = order.getCausalityRelatedValue(
                             portal_type='Applied Rule')
397 398 399
    order_movement = related_applied_rule.contentValues()[0]
    delivery_applied_rule = order_movement.contentValues()[0]
    delivery_movement = delivery_applied_rule.contentValues()[0]
400 401 402 403 404 405 406 407 408 409 410
    invoice_applied_rule = delivery_movement.contentValues()[0]
    invoice_movement = invoice_applied_rule.contentValues()[0]
    invoice_transaction_applied_rule = invoice_movement.contentValues()[0]
    invoice_transaction_movement =\
         invoice_transaction_applied_rule.contentValues()[0]
    self.assertEquals(currency,
          invoice_transaction_movement.getResourceValue())
    self.assertEquals(currency,
          delivery_movement.getPriceCurrencyValue())
    self.assertEquals\
        (invoice_transaction_movement.getSourceTotalAssetPrice(),
411
        round(655.957*delivery_movement.getTotalPrice()))
412 413
    self.assertEquals\
        (invoice_transaction_movement.getDestinationTotalAssetPrice(),
414 415
        None)

416
  @newSimulationExpectedFailure
417 418 419
  def test_01_destination_total_asset_price_on_accounting_lines(self,quiet=0,
          run=run_all_test):
    """
420
    tests that the delivery builder of the invoice transaction lines
421 422 423 424 425 426
    copies the destination asset price on the accounting_lines of the invoice
    """
    if not run: return
    if not quiet:
       printAndLog(
       'test_01_destination_total_asset_price_on_accounting_lines')
427

428 429 430
    resource = self.portal.product_module.newContent(
                    portal_type='Product',
                    title='Resource',
431
                    product_line='apparel')
432 433 434 435 436 437 438 439 440 441 442
    currency = self.portal.currency_module.newContent(
                                portal_type='Currency',
                                title='euro')
    currency.setBaseUnitQuantity(0.01)
    new_currency = \
           self.portal.currency_module.newContent(portal_type='Currency')
    new_currency.setReference('XOF')
    new_currency.setTitle('Francs CFA')
    new_currency.setBaseUnitQuantity(1.00)
    self.tic()#execute transaction
    x_curr_ex_line = currency.newContent(
443 444
                                  portal_type='Currency Exchange Line',
                        price_currency=new_currency.getRelativeUrl())
445 446 447
    x_curr_ex_line.setTitle('Euro to Francs CFA')
    x_curr_ex_line.setBasePrice(655.957)
    x_curr_ex_line.setStartDate(DateTime(2008,10,21))
448
    x_curr_ex_line.setStopDate(DateTime(2008,10,22))
449
    x_curr_ex_line.validate()
450
    self.createBusinessProcess(currency)
451 452 453
    self.tic()#execute transaction
    client = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
454 455
                            title='Client',
                price_currency=new_currency.getRelativeUrl(),
456
                       default_address_region=self.default_region)
457 458 459
    vendor = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
                            title='Vendor',
460
                         default_address_region=self.default_region)
461 462 463 464 465 466 467 468
    order = self.portal.sale_order_module.newContent(
                              portal_type='Sale Order',
                              source_value=vendor,
                              source_section_value=vendor,
                              destination_value=client,
                              destination_section_value=client,
                              start_date=DateTime(2008,10, 21),
                              price_currency_value=currency,
469
                              specialise_value=self.business_process,
470 471 472 473 474 475 476
                              title='Order')
    order_line = order.newContent(portal_type='Sale Order Line',
                                  resource_value=resource,
                                  quantity=1,
                                  price=2)
    order.confirm()
    self.tic()
Kazuhiko Shiozaki's avatar
Kazuhiko Shiozaki committed
477 478
    self.stepPackingListBuilderAlarm()
    self.tic()
479 480 481 482 483 484 485 486
    related_packing_list = order.getCausalityRelatedValue(
                                portal_type='Sale Packing List')
    self.assertNotEquals(related_packing_list, None)
    related_packing_list.start()
    related_packing_list.stop()
    self.tic()
    related_applied_rule = order.getCausalityRelatedValue(
                             portal_type='Applied Rule')
487 488 489
    order_movement = related_applied_rule.contentValues()[0]
    delivery_applied_rule = order_movement.contentValues()[0]
    delivery_movement = delivery_applied_rule.contentValues()[0]
490
    related_invoice = related_packing_list.getCausalityRelatedValue(
491
                            portal_type='Sale Invoice Transaction')
492 493 494 495
    self.assertNotEquals(related_invoice, None)
    related_invoice.start()
    self.tic()
    line_list= related_invoice.contentValues(
496
      portal_type=self.portal.getPortalAccountingMovementTypeList())
497 498 499 500
    self.assertNotEquals(line_list, None)
    for line in line_list:
       self.assertEquals(line.getDestinationTotalAssetPrice(),
              round(655.957*delivery_movement.getTotalPrice()))
501

502
  @newSimulationExpectedFailure
503
  def test_01_diverged_sale_packing_list_destination_total_asset_price(
504
          self,quiet=0,run=run_all_test):
505 506 507 508 509 510 511 512 513 514 515
    """
    tests that when the sale packing list is divergent on the quantity and
    that the resource on simulation movements is different
     from the price currency of the source section,
     source_asset_price is updated as we solve the divergence and
     accept the decision
    """
    if not run: return
    if not quiet:
     printAndLog(
        'test_01_diverged_sale_packing_list_destination_total_asset_price')
516

517 518 519
    resource = self.portal.product_module.newContent(
                    portal_type='Product',
                    title='Resource',
520
                    product_line='apparel')
521 522 523 524 525 526 527 528 529 530 531
    currency = self.portal.currency_module.newContent(
                                portal_type='Currency',
                                title='euro')
    currency.setBaseUnitQuantity(0.01)
    new_currency = \
           self.portal.currency_module.newContent(portal_type='Currency')
    new_currency.setReference('XOF')
    new_currency.setTitle('Francs CFA')
    new_currency.setBaseUnitQuantity(1.00)
    self.tic()#execute transaction
    x_curr_ex_line = currency.newContent(
532 533
                              portal_type='Currency Exchange Line',
               price_currency=new_currency.getRelativeUrl())
534 535 536
    x_curr_ex_line.setTitle('Euro to Francs CFA')
    x_curr_ex_line.setBasePrice(655.957)
    x_curr_ex_line.setStartDate(DateTime(2008,10,21))
537
    x_curr_ex_line.setStopDate(DateTime(2008,10,22))
538
    x_curr_ex_line.validate()
539
    self.createBusinessProcess(currency)
540 541 542
    self.tic()#execute transaction
    client = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
543 544
                            title='Client',
                price_currency=new_currency.getRelativeUrl(),
545
                        default_address_region=self.default_region)
546 547 548
    vendor = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
                            title='Vendor',
549
                         default_address_region=self.default_region)
550 551 552 553 554 555 556 557
    order = self.portal.sale_order_module.newContent(
                              portal_type='Sale Order',
                              source_value=vendor,
                              source_section_value=vendor,
                              destination_value=client,
                              destination_section_value=client,
                              start_date=DateTime(2008,10, 21),
                              price_currency_value=currency,
558
                              specialise_value=self.business_process,
559 560 561 562 563 564 565
                              title='Order')
    order_line = order.newContent(portal_type='Sale Order Line',
                                  resource_value=resource,
                                  quantity=5,
                                  price=2)
    order.confirm()
    self.tic()
Kazuhiko Shiozaki's avatar
Kazuhiko Shiozaki committed
566 567
    self.stepPackingListBuilderAlarm()
    self.tic()
568 569 570 571 572 573 574 575
    related_packing_list = order.getCausalityRelatedValue(
                                portal_type='Sale Packing List')
    self.assertNotEquals(related_packing_list, None)
    related_packing_list_line_list=related_packing_list.getMovementList()
    related_packing_list_line= related_packing_list_line_list[0]
    self.assertEquals(related_packing_list_line.getQuantity(),5.0)
    old_destination_asset_price = \
          round(655.957*related_packing_list_line.getTotalPrice())
576

577 578 579
    related_packing_list_line.edit(quantity=3.0)
    self.tic()
    self.assertEquals(related_packing_list.getCausalityState(),
580
                             'diverged')
581 582 583 584 585 586 587
    self._solveDivergence(related_packing_list, 'quantity', 'accept')
    self.tic()
    related_packing_list.start()
    related_packing_list.stop()
    self.tic()

    related_applied_rule = order.getCausalityRelatedValue(
588
                            portal_type='Applied Rule')
589 590 591
    order_movement = related_applied_rule.contentValues()[0]
    delivery_applied_rule = order_movement.contentValues()[0]
    delivery_movement = delivery_applied_rule.contentValues()[0]
592 593 594 595 596 597
    invoice_applied_rule = delivery_movement.contentValues()[0]
    invoice_movement = invoice_applied_rule.contentValues()[0]
    invoice_transaction_applied_rule = invoice_movement.contentValues()[0]
    invoice_transaction_movement =\
         invoice_transaction_applied_rule.contentValues()[0]
    self.assertEquals(
598
       invoice_transaction_movement.getDestinationTotalAssetPrice(),
599
                old_destination_asset_price *(3.0/5.0))
600 601


602
  def test_01_diverged_purchase_packing_list_source_total_asset_price(
603
           self,quiet=0,run=run_all_test):
604 605 606 607 608 609 610 611 612 613 614
    """
    tests that when the purchase packing list is divergent on the quantity
      and that the resource on simulation movements is different
     from the price currency of the destination section,
     destination_asset_price is updated as we solve the divergence and
     accept the decision
    """
    if not run: return
    if not quiet:
     printAndLog(
      'test_01_diverged_purchase_packing_list_source_total_asset_price')
615

616 617 618
    resource = self.portal.product_module.newContent(
                    portal_type='Product',
                    title='Resource',
619
                    product_line='apparel')
620 621 622 623 624
    currency = self.portal.currency_module.newContent(
                                portal_type='Currency',
                                title='euro')
    currency.setBaseUnitQuantity(0.01)
    new_currency = \
625
      self.portal.currency_module.newContent(portal_type='Currency')
626 627 628 629 630
    new_currency.setReference('XOF')
    new_currency.setTitle('Francs CFA')
    new_currency.setBaseUnitQuantity(1.00)
    self.tic()#execute transaction
    x_curr_ex_line = currency.newContent(
631 632
                               portal_type='Currency Exchange Line',
                price_currency=new_currency.getRelativeUrl())
633 634 635
    x_curr_ex_line.setTitle('Euro to Francs CFA')
    x_curr_ex_line.setBasePrice(655.957)
    x_curr_ex_line.setStartDate(DateTime(2008,10,21))
636
    x_curr_ex_line.setStopDate(DateTime(2008,10,22))
637
    x_curr_ex_line.validate()
638
    self.createBusinessProcess(currency)
639 640 641 642
    self.tic()#execute transaction
    client = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
                            title='Client',
643
                         default_address_region=self.default_region)
644 645 646
    vendor = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
                            title='Vendor',
647
                price_currency=new_currency.getRelativeUrl(),
648
                        default_address_region=self.default_region)
649 650 651 652 653 654 655 656
    order = self.portal.purchase_order_module.newContent(
                              portal_type='Purchase Order',
                              source_value=vendor,
                              source_section_value=vendor,
                              destination_value=client,
                              destination_section_value=client,
                              start_date=DateTime(2008,10, 21),
                              price_currency_value=currency,
657
                              specialise_value=self.business_process,
658 659 660 661 662 663 664
                              title='Order')
    order_line = order.newContent(portal_type='Purchase Order Line',
                                  resource_value=resource,
                                  quantity=5,
                                  price=2)
    order.confirm()
    self.tic()
Kazuhiko Shiozaki's avatar
Kazuhiko Shiozaki committed
665 666
    self.stepPackingListBuilderAlarm()
    self.tic()
667 668 669 670 671 672 673 674
    related_packing_list = order.getCausalityRelatedValue(
                                portal_type='Purchase Packing List')
    self.assertNotEquals(related_packing_list, None)
    related_packing_list_line_list=related_packing_list.getMovementList()
    related_packing_list_line= related_packing_list_line_list[0]
    self.assertEquals(related_packing_list_line.getQuantity(),5.0)
    old_source_asset_price = \
          round(655.957*related_packing_list_line.getTotalPrice())
675

676 677 678 679
    related_packing_list_line.edit(quantity=3.0)
    self.tic()
    self.assertEquals(related_packing_list.getCausalityState(),
                             'diverged')
680

681
    self._solveDivergence(related_packing_list, 'quantity','accept')
682 683 684 685 686 687
    self.tic()
    related_packing_list.start()
    related_packing_list.stop()
    self.tic()

    related_applied_rule = order.getCausalityRelatedValue(
688
                             portal_type='Applied Rule')
689 690 691
    order_movement = related_applied_rule.contentValues()[0]
    delivery_applied_rule = order_movement.contentValues()[0]
    delivery_movement = delivery_applied_rule.contentValues()[0]
692 693 694 695 696 697
    invoice_applied_rule = delivery_movement.contentValues()[0]
    invoice_movement = invoice_applied_rule.contentValues()[0]
    invoice_transaction_applied_rule = invoice_movement.contentValues()[0]
    invoice_transaction_movement =\
         invoice_transaction_applied_rule.contentValues()[0]
    self.assertEquals(invoice_transaction_movement.\
698 699
        getSourceTotalAssetPrice(),
        old_source_asset_price *(3.0/5.0))
700

701
  @newSimulationExpectedFailure
702
  def test_01_delivery_mode_on_sale_packing_list_and_invoice(
703
          self,quiet=0,run=run_all_test):
704 705 706 707 708 709 710 711 712 713 714
    """
    tests that when the sale packing list is divergent on the quantity and
    that the resource on simulation movements is different
     from the price currency of the source section,
     source_asset_price is updated as we solve the divergence and
     accept the decision
    """
    if not run: return
    if not quiet:
     printAndLog(
        'test_01_delivery_mode_on_sale_packing_list_and_invoice')
715

716 717 718
    resource = self.portal.product_module.newContent(
                    portal_type='Product',
                    title='Resource',
719
                    product_line='apparel')
720 721 722 723 724 725 726 727 728 729 730
    currency = self.portal.currency_module.newContent(
                                portal_type='Currency',
                                title='euro')
    currency.setBaseUnitQuantity(0.01)
    new_currency = \
           self.portal.currency_module.newContent(portal_type='Currency')
    new_currency.setReference('XOF')
    new_currency.setTitle('Francs CFA')
    new_currency.setBaseUnitQuantity(1.00)
    self.tic()#execute transaction
    x_curr_ex_line = currency.newContent(
731 732
                                  portal_type='Currency Exchange Line',
                        price_currency=new_currency.getRelativeUrl())
733 734 735 736 737
    x_curr_ex_line.setTitle('Euro to Francs CFA')
    x_curr_ex_line.setBasePrice(655.957)
    x_curr_ex_line.setStartDate(DateTime(2008,10,21))
    x_curr_ex_line.setStopDate(DateTime(2008,10,22))
    x_curr_ex_line.validate()
738
    self.createBusinessProcess(currency)
739 740 741
    self.tic()#execute transaction
    client = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
742 743
                            title='Client',
                price_currency=new_currency.getRelativeUrl(),
744 745 746 747 748 749 750 751 752 753 754 755 756
                       default_address_region=self.default_region)
    vendor = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
                            title='Vendor',
                         default_address_region=self.default_region)
    order = self.portal.sale_order_module.newContent(
                              portal_type='Sale Order',
                              source_value=vendor,
                              source_section_value=vendor,
                              destination_value=client,
                              destination_section_value=client,
                              start_date=DateTime(2008,10, 21),
                              price_currency_value=currency,
757 758
                              delivery_mode=self.mail_delivery_mode,
                              incoterm=self.cpt_incoterm,
759
                              specialise_value=self.business_process,
760 761 762 763 764 765 766
                              title='Order')
    order_line = order.newContent(portal_type='Sale Order Line',
                                  resource_value=resource,
                                  quantity=5,
                                  price=2)
    order.confirm()
    self.tic()
Kazuhiko Shiozaki's avatar
Kazuhiko Shiozaki committed
767 768
    self.stepPackingListBuilderAlarm()
    self.tic()
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
    related_packing_list = order.getCausalityRelatedValue(
                                portal_type='Sale Packing List')
    self.assertNotEquals(related_packing_list, None)
    self.assertEquals(related_packing_list.getDeliveryMode(),
                         order.getDeliveryMode())
    self.assertEquals(related_packing_list.getIncoterm(),
                         order.getIncoterm())
    related_packing_list.start()
    related_packing_list.stop()
    self.tic()
    related_invoice = related_packing_list.getCausalityRelatedValue(
                             portal_type='Sale Invoice Transaction')
    self.assertNotEquals(related_invoice, None)
    self.assertEquals(related_invoice.getDeliveryMode(),
                         order.getDeliveryMode())
    self.assertEquals(related_invoice.getIncoterm(),
                         order.getIncoterm())
786

787
  @newSimulationExpectedFailure
788 789 790 791 792 793 794 795 796
  def test_01_quantity_unit_on_sale_packing_list(
      self,quiet=0,run=run_all_test):
    """
    tests that when a resource uses different quantity unit that the
    """
    if not run: return
    if not quiet:
     printAndLog(
        'test_01_quantity_unit_on_sale_packing_list')
797

798 799 800 801 802 803 804 805 806 807
    resource = self.portal.product_module.newContent(
                    portal_type='Product',
                    title='Resource',
                    product_line='apparel')
    resource.setQuantityUnitList([self.unit_piece_quantity_unit,
                                 self.mass_quantity_unit])
    currency = self.portal.currency_module.newContent(
                                portal_type='Currency',
                                title='euro')
    currency.setBaseUnitQuantity(0.01)
808
    self.createBusinessProcess(currency)
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
    self.tic()#execute transaction
    client = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
                            title='Client',
                         default_address_region=self.default_region)
    vendor = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
                            title='Vendor',
                         default_address_region=self.default_region)
    order = self.portal.sale_order_module.newContent(
                              portal_type='Sale Order',
                              source_value=vendor,
                              source_section_value=vendor,
                              destination_value=client,
                              destination_section_value=client,
                              start_date=DateTime(2008,10, 21),
                              price_currency_value=currency,
                              delivery_mode=self.mail_delivery_mode,
                              incoterm=self.cpt_incoterm,
828
                              specialise_value=self.business_process,
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
                              title='Order')
    first_order_line = order.newContent(
                        portal_type='Sale Order Line',
                                  resource_value=resource,
                      quantity_unit = self.unit_piece_quantity_unit,
                                  quantity=5,
                                  price=3)
    second_order_line = order.newContent(
                      portal_type='Sale Order Line',
                                  resource_value=resource,
                             quantity_unit=self.mass_quantity_unit,
                                  quantity=1.5,
                                  price=2)
    order.confirm()
    self.tic()
Kazuhiko Shiozaki's avatar
Kazuhiko Shiozaki committed
844 845
    self.stepPackingListBuilderAlarm()
    self.tic()
846 847 848 849
    related_packing_list = order.getCausalityRelatedValue(
                                portal_type='Sale Packing List')
    self.assertNotEquals(related_packing_list, None)
    movement_list = related_packing_list.getMovementList()
850
    movement_list.sort(key=lambda x:x.getCausalityId())
851 852 853 854 855
    self.assertEquals(len(movement_list),2)
    self.assertEquals(movement_list[0].getQuantityUnit(),
                         first_order_line.getQuantityUnit())
    self.assertEquals(movement_list[1].getQuantityUnit(),
                         second_order_line.getQuantityUnit())
856 857 858 859 860

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