testComplexTradeModelLineUseCase.py 34 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 28 29 30 31 32 33 34
# -*- coding: utf-8 -*-
##############################################################################
# Copyright (c) 2009 Nexedi SA and Contributors. All Rights Reserved.
#          Łukasz Nowak <luke@nexedi.com>
#          Fabien Morin <fabien@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility 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
# guarantees 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 unittest
import transaction

from Products.ERP5Type.tests.utils import createZODBPythonScript
from Products.ERP5.tests.testTradeModelLine import TestTradeModelLineMixin
35
from Products.ERP5.PropertySheet.TradeModelLine import TARGET_LEVEL_DELIVERY, TARGET_LEVEL_MOVEMENT
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

class TestComplexTradeModelLineUseCase(TestTradeModelLineMixin):
  """This test provides several complex use cases which are seen in the normal
  shop and make sure that trade model line is capable of real business scene.
  """

  def createOrder(self):
    module = self.portal.getDefaultModule(portal_type=self.order_portal_type)
    return module.newContent(portal_type=self.order_portal_type,
        title=self.id())

  def createTradeCondition(self):
    module = self.portal.getDefaultModule(
        portal_type=self.trade_condition_portal_type)
    trade_condition = module.newContent(
        portal_type=self.trade_condition_portal_type,
        title=self.id())
    return trade_condition

  def getAmount(self, order, reference, return_object=False):
56
    amount_list = []
57
    trade_condition = order.getSpecialiseValue()
58 59 60 61 62 63 64 65 66
    for amount in trade_condition.getAggregatedAmountList(order):
      if amount.getReference() == reference:
        amount_list.append(amount)
    if return_object == True:
      return amount_list
    elif amount_list:
      return sum([amount.getTotalPrice(0) for amount in amount_list])
    else:
      return None
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

  def appendBaseContributionCategory(self, document, new_category):
    base_contribution_value_list = document.getBaseContributionValueList()
    document.setBaseContributionValueList(
      base_contribution_value_list+[new_category])

  def beforeTearDown(self):
    # abort any transaction
    transaction.abort()
    # put non finished activities into ignored state
    activity_connection = self.portal.cmf_activity_sql_connection
    for table in 'message', 'message_queue':
      activity_connection.manage_test(
          'delete from %s where processing_node=-2' % table)

    def removeAll(*args):
      for container in args:
        container.manage_delObjects(ids=list(container.objectIds()))
    removeAll(self.portal.sale_order_module,
              self.portal.purchase_order_module,
              self.portal.sale_trade_condition_module,
              self.portal.purchase_trade_condition_module,
              self.portal.person_module,
              self.portal.organisation_module,
              self.portal.service_module,
              self.portal.product_module,
              self.portal.currency_module,
              self.portal.portal_categories.product_line,
              self.portal.portal_categories.base_amount,
              self.portal.portal_categories.trade_phase,
              self.portal.portal_categories.use,
              self.portal.portal_categories.quantity_unit,
              )

    self.stepTic()

  def afterSetUp(self):
    portal = self.portal

    # inherited method
    self.createCategories()

    self.stepTic()

    # add currency
    jpy = portal.currency_module.newContent(title='Yen', reference='JPY', base_unit_quantity='1')

    self.stepTic()

    # add organisations
    my_company = portal.organisation_module.newContent(title='My Company')
    client_1 = portal.organisation_module.newContent(title='Client 1')

    self.stepTic()

    # add base amount subcategories
    base_amount = portal.portal_categories.base_amount
    self.total_price_of_ordered_items = base_amount.newContent(id='total_price_of_ordered_items')
    self.discount_amount_of_non_vat_taxable = base_amount.newContent(id='discount_amount_of_non_vat_taxable')
    self.discount_amount_of_vat_taxable = base_amount.newContent(id='discount_amount_of_vat_taxable')
    self.vat_taxable = base_amount.newContent(id='vat_taxable')
128
    self.additional_charge = base_amount.newContent('additional_charge')
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    self.total_price_without_vat = base_amount.newContent(id='total_price_without_vat')
    self.total_price_of_vat_taxable = base_amount.newContent(id='total_price_of_vat_taxable')
    self.discount_amount = base_amount.newContent(id='discount_amount')
    self.vat_amount = base_amount.newContent(id='vat_amount')
    self.total_price_with_vat = base_amount.newContent(id='total_price_with_vat')
    self.poster_present_1dvd = base_amount.newContent(id='poster_present_1dvd')
    self.poster_present_3cd = base_amount.newContent(id='poster_present_3cd')
    self.special_discount_3cd = base_amount.newContent(id='special_discount_3cd')
    # add product line subcategories
    product_line = portal.portal_categories.product_line
    audio = product_line.newContent(id='audio')
    audio_cd = audio.newContent(id='cd')
    video = product_line.newContent(id='video')
    video_dvd = video.newContent(id='dvd')
    other_product = product_line.newContent(id='other')
    # add a quantity unit subcategory
    self.unit = portal.portal_categories.quantity_unit.newContent(id='unit')

    self.stepTic()

    # create services
    self.service_vat = portal.service_module.newContent(title='VAT')
151 152
    self.service_discount = portal.service_module.newContent(title='DISCOUNT')
    self.service_shipping_fee = portal.service_module.newContent(title='SHIPPING FEE')
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

    self.stepTic()

    # create products
    def addProductDocument(title, product_line_value):
      return portal.product_module.newContent(
        title=title,
        product_line_value=product_line_value,
        quantity_unit_value=self.unit,
        base_contribution_value_list=[self.vat_taxable,
                                      self.total_price_of_ordered_items])

    self.music_album_1 = addProductDocument('Music Album 1', audio_cd)
    self.movie_dvd_1 = addProductDocument('Movie DVD 1', video_dvd)
    self.music_album_2 = addProductDocument('Movie Album 2', audio_cd)
    self.candy = addProductDocument('Candy', other_product)
    self.poster = addProductDocument('Poster', other_product)
    self.music_album_3 = addProductDocument('Movie Album 3', audio_cd)
    self.movie_dvd_2 = addProductDocument('Movie DVD 2', video_dvd)
    self.music_album_4 = addProductDocument('Movie Album 4', audio_cd)

    self.stepTic()

    # create a trade condition and add several common trade model lines in it.
    self.trade_condition = self.createTradeCondition()
    self.trade_condition.edit(
      source_section_value=my_company,
      source_value=my_company,
      source_decision_value=my_company,
      destination_section_value=client_1,
      destination_value=client_1,
      destination_decision_value=client_1,
      price_currency_value=jpy)
    self.trade_condition.newContent(
      portal_type='Trade Model Line',
      title='Total Price Without VAT',
      reference='TOTAL_PRICE_WITHOUT_VAT',
      price=1,
      quantity=None,
      efficiency=1,
      target_level=TARGET_LEVEL_DELIVERY,
      create_line=True,
      trade_phase=None,
      base_application_value_list=[self.discount_amount_of_non_vat_taxable,
                                   self.discount_amount_of_vat_taxable,
198 199
                                   self.total_price_of_ordered_items,
                                   self.additional_charge],
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
      base_contribution_value_list=[self.total_price_without_vat])
    self.trade_condition.newContent(
      portal_type='Trade Model Line',
      title='Total Price Of VAT Taxable',
      reference='TOTAL_PRICE_OF_VAT_TAXABLE',
      price=1,
      quantity=None,
      efficiency=1,
      target_level=TARGET_LEVEL_DELIVERY,
      create_line=True,
      trade_phase=None,
      base_application_value_list=[self.discount_amount_of_vat_taxable,
                                   self.vat_taxable],
      base_contribution_value_list=[self.total_price_of_vat_taxable])
    self.trade_condition.newContent(
      portal_type='Trade Model Line',
      title='Discount Amount',
      reference='DISCOUNT_AMOUNT',
      resource_value=self.service_discount,
      price=1,
      quantity=None,
      efficiency=1,
      target_level=TARGET_LEVEL_DELIVERY,
      create_line=True,
      trade_phase_value=portal.portal_categories.trade_phase.default.invoicing,
      base_application_value_list=[self.discount_amount_of_vat_taxable,
                                   self.discount_amount_of_non_vat_taxable],
      base_contribution_value_list=[self.discount_amount])
    self.trade_condition.newContent(
      portal_type='Trade Model Line',
      title='VAT Amount',
      reference='VAT_AMOUNT',
      resource_value=self.service_vat,
      price=0.05,
      quantity=None,
      efficiency=1,
      target_level=TARGET_LEVEL_DELIVERY,
      create_line=True,
      trade_phase_value=portal.portal_categories.trade_phase.default.invoicing,
      base_application_value_list=[self.discount_amount_of_vat_taxable,
                                   self.vat_taxable],
      base_contribution_value_list=[self.vat_amount])
    self.trade_condition.newContent(
      portal_type='Trade Model Line',
      title='Total Price With VAT',
      reference='TOTAL_PRICE_WITH_VAT',
      price=1,
      quantity=None,
      efficiency=1,
      target_level=TARGET_LEVEL_DELIVERY,
      create_line=True,
      trade_phase=None,
      base_application_value_list=[self.vat_amount,
                                   self.total_price_without_vat],
      base_contribution_value_list=[self.total_price_with_vat])

    self.stepTic()

  def test_usecase1(self):
    """
    Use case 1 : Buy 3 CDs or more, get 10% off them.

    1 CD   5000 yen
    1 CD   3000 yen
    1 Candy 100 yen
    1 CD   2400 yen
    discount (5000+3000+2400) * 0.1 = 1040 yen
    """
    createZODBPythonScript(
      self.portal.portal_skins.custom,
      'TradeModelLine_calculate3CD10PercentDiscount',
      'current_aggregated_amount_list, current_movement, aggregated_movement_list',
      """\
total_quantity = sum([movement.getQuantity()
                      for movement in aggregated_movement_list])
if total_quantity >= 3:
  return current_movement
else:
  return None
""")
    order = self.createOrder()
    order.edit(specialise_value=self.trade_condition)
    order.Order_applyTradeCondition(order.getSpecialiseValue())
    order.newContent(portal_type='Trade Model Line',
                     reference='3CD_AND_10PERCENT_DISCOUNT_OFF_THEM',
                     resource_value=self.service_discount,
                     price=-0.1,
                     quantity=None,
                     efficiency=1,
                     target_level=TARGET_LEVEL_DELIVERY,
                     calculation_script_id='TradeModelLine_calculate3CD10PercentDiscount',
                     create_line=True,
                     trade_phase=None,
                     base_application_value_list=[self.special_discount_3cd],
                     base_contribution_value_list=[self.discount_amount_of_vat_taxable])

    order_line_1 = order.newContent(portal_type=self.order_line_portal_type,
                                    resource_value=self.music_album_1,
                                    quantity=1,
                                    price=5000)
    self.appendBaseContributionCategory(order_line_1, self.special_discount_3cd)
    order_line_2 = order.newContent(portal_type=self.order_line_portal_type,
                                    resource_value=self.music_album_2,
                                    quantity=1,
                                    price=3000)
    self.appendBaseContributionCategory(order_line_2, self.special_discount_3cd)
    order_line_3 = order.newContent(portal_type=self.order_line_portal_type,
                                    resource_value=self.candy,
                                    quantity=1,
                                    price=100)

    self.stepTic()

    # check the current amount
    self.assertEqual(self.getAmount(order, 'TOTAL_PRICE_WITHOUT_VAT'), 8100)
    self.assertEqual(self.getAmount(order, 'VAT_AMOUNT'), 405)
    self.assertEqual(self.getAmount(order, 'TOTAL_PRICE_WITH_VAT'), 8505)
    # add one more cd, then total is 3. the special discount will be applied.
    order_line_4 = order.newContent(portal_type=self.order_line_portal_type,
                                    resource_value=self.music_album_3,
                                    quantity=1,
                                    price=2400)
    self.appendBaseContributionCategory(order_line_4, self.special_discount_3cd)

    self.stepTic()

    # check again
    self.assertEqual(self.getAmount(order, '3CD_AND_10PERCENT_DISCOUNT_OFF_THEM'),
                     -1040)
    self.assertEqual(self.getAmount(order, 'TOTAL_PRICE_WITHOUT_VAT'), 9460)
    self.assertEqual(self.getAmount(order, 'VAT_AMOUNT'), 473)
    self.assertEqual(self.getAmount(order, 'TOTAL_PRICE_WITH_VAT'), 9933)

  def test_usecase2(self):
    """
    Use case 2 : Buy 3 CDs or more, get 500 yen off.

    1 CD  5000 yen
    1 CD  3000 yen
    1 DVD 3000 yen
    1 CD  2400 yen
    discount 500 yen
    """
    createZODBPythonScript(
      self.portal.portal_skins.custom,
      'TradeModelLine_calculate3CD500YenDiscount',
      'current_aggregated_amount_list, current_movement, aggregated_movement_list',
      """\
total_quantity = sum([movement.getQuantity() for movement in aggregated_movement_list])
if total_quantity >= 3:
  current_movement.setQuantity(-500)
  return current_movement
else:
  return None
""")
    order = self.createOrder()
    order.edit(specialise_value=self.trade_condition)
    order.Order_applyTradeCondition(order.getSpecialiseValue())
    order.newContent(portal_type='Trade Model Line',
                     reference='3CD_AND_500YEN_OFF',
                     resource_value=self.service_discount,
                     price=1,
                     quantity=None,
                     efficiency=1,
                     target_level=TARGET_LEVEL_DELIVERY,
                     calculation_script_id='TradeModelLine_calculate3CD500YenDiscount',
                     create_line=True,
                     trade_phase=None,
                     base_application_value_list=[self.special_discount_3cd],
                     base_contribution_value_list=[self.discount_amount_of_vat_taxable])

    order_line_1 = order.newContent(portal_type=self.order_line_portal_type,
                                    resource_value=self.music_album_1,
                                    quantity=1,
                                    price=5000)
    self.appendBaseContributionCategory(order_line_1, self.special_discount_3cd)
    order_line_2 = order.newContent(portal_type=self.order_line_portal_type,
                                    resource_value=self.music_album_2,
                                    quantity=1,
                                    price=3000)
    self.appendBaseContributionCategory(order_line_2, self.special_discount_3cd)
    order_line_3 = order.newContent(portal_type=self.order_line_portal_type,
                                    resource_value=self.movie_dvd_1,
                                    quantity=1,
                                    price=3000)

    self.stepTic()

    # check the current amount
    self.assertEqual(self.getAmount(order, 'TOTAL_PRICE_WITHOUT_VAT'), 11000)
    self.assertEqual(self.getAmount(order, 'VAT_AMOUNT'), 550)
    self.assertEqual(self.getAmount(order, 'TOTAL_PRICE_WITH_VAT'), 11550)
    # add one more cd, then total is 3. the special discount will be applied.
    order_line_4 = order.newContent(portal_type=self.order_line_portal_type,
                                    resource_value=self.music_album_3,
                                    quantity=1,
                                    price=2400)
    self.appendBaseContributionCategory(order_line_4, self.special_discount_3cd)
    # check again
    self.assertEqual(self.getAmount(order, '3CD_AND_500YEN_OFF'), -500)
    self.assertEqual(self.getAmount(order, 'TOTAL_PRICE_WITHOUT_VAT'), 12900)
    self.assertEqual(self.getAmount(order, 'VAT_AMOUNT'), 645)
    self.assertEqual(self.getAmount(order, 'TOTAL_PRICE_WITH_VAT'), 13545)

  def test_usecase3(self):
    """
    Use case 3 : Buy 3 CDs or more, get 10% off total.

    1 CD  5000 yen
    1 DVD 3000 yen
    1 CD  3000 yen
    1 CD  2400 yen
    discount (5000+3000+3000+2400) * 0.1 = 1340 yen
    """
    createZODBPythonScript(
      self.portal.portal_skins.custom,
      'TradeModelLine_calculate3CD10PercentDiscountFromTotal',
      'current_aggregated_amount_list, current_movement, aggregated_movement_list',
      '''\
special_discount_3cd = context.portal_categories.base_amount.special_discount_3cd
total_quantity = sum([movement.getQuantity() for movement in current_aggregated_amount_list
                      if special_discount_3cd in movement.getBaseContributionValueList()])
if total_quantity >= 3:
  return current_movement
else:
  return None
''')
    order = self.createOrder()
    order.edit(specialise_value=self.trade_condition)
    order.Order_applyTradeCondition(order.getSpecialiseValue())
    order.newContent(portal_type='Trade Model Line',
                     reference='3CD_10PERCENT_OFF_FROM_TOTAL',
                     resource_value=self.service_discount,
                     price=-0.1,
                     quantity=None,
                     efficiency=1,
                     target_level=TARGET_LEVEL_DELIVERY,
                     calculation_script_id='TradeModelLine_calculate3CD10PercentDiscountFromTotal',
                     create_line=True,
                     trade_phase=None,
                     base_application_value_list=[self.total_price_of_ordered_items],
                     base_contribution_value_list=[self.discount_amount_of_vat_taxable])

    order_line_1 = order.newContent(portal_type=self.order_line_portal_type,
                                    resource_value=self.music_album_1,
                                    quantity=1,
                                    price=5000)
    self.appendBaseContributionCategory(order_line_1, self.special_discount_3cd)
    order_line_2 = order.newContent(portal_type=self.order_line_portal_type,
                                    resource_value=self.movie_dvd_1,
                                    quantity=1,
                                    price=3000)
    order_line_3 = order.newContent(portal_type=self.order_line_portal_type,
                                    resource_value=self.music_album_2,
                                    quantity=1,
                                    price=3000)
    self.appendBaseContributionCategory(order_line_3, self.special_discount_3cd)

    self.stepTic()

    # check the current amount
    self.assertEqual(self.getAmount(order, 'TOTAL_PRICE_WITHOUT_VAT'), 11000)
    self.assertEqual(self.getAmount(order, 'VAT_AMOUNT'), 550)
    self.assertEqual(self.getAmount(order, 'TOTAL_PRICE_WITH_VAT'), 11550)
    # add one more cd, then total is 3. the special discount will be applied.
    order_line_4 = order.newContent(portal_type=self.order_line_portal_type,
                                    resource_value=self.music_album_3,
                                    quantity=1,
                                    price=2400)
    self.appendBaseContributionCategory(order_line_4, self.special_discount_3cd)
    # check again
    self.assertEqual(self.getAmount(order, '3CD_10PERCENT_OFF_FROM_TOTAL'),
                     -1340)
    self.assertEqual(self.getAmount(order, 'TOTAL_PRICE_WITHOUT_VAT'), 12060)
    self.assertEqual(self.getAmount(order, 'VAT_AMOUNT'), 603)
    self.assertEqual(self.getAmount(order, 'TOTAL_PRICE_WITH_VAT'), 12663)

  def test_usecase4(self):
    """
    Use case 4 : Buy 3 CDs or 1 DVD, get 1 poster free.

    2 CD     6000 yen
    1 DVD    3000 yen
    1 Poster    0 yen
    """
    createZODBPythonScript(
      self.portal.portal_skins.custom,
      'TradeModelLine_calculate3CDOr1DVDForPoster',
      'current_aggregated_amount_list, current_movement, aggregated_movement_list',
      '''\
poster_present_3cd = context.portal_categories.base_amount.poster_present_3cd
poster_present_1dvd = context.portal_categories.base_amount.poster_present_1dvd

total_quantity_3cd = sum([movement.getQuantity() for movement in aggregated_movement_list
                          if poster_present_3cd in movement.getBaseContributionValueList()])
total_quantity_1dvd = sum([movement.getQuantity() for movement in aggregated_movement_list
                           if poster_present_1dvd in movement.getBaseContributionValueList()])
if (total_quantity_3cd >= 3 or total_quantity_1dvd >= 1):
  current_movement.setQuantity(1)
  return current_movement
else:
  return None
''')
    order = self.createOrder()
    order.edit(specialise_value=self.trade_condition)
    order.Order_applyTradeCondition(order.getSpecialiseValue())
    order.newContent(portal_type='Trade Model Line',
                     reference='3CD_OR_1DVD_GET_1_POSTER_FREE',
                     resource_value=self.poster,
                     price=0,
                     quantity=None,
                     efficiency=1,
                     target_level=TARGET_LEVEL_DELIVERY,
                     calculation_script_id='TradeModelLine_calculate3CDOr1DVDForPoster',
                     create_line=True,
                     trade_phase=None,
                     base_application_value_list=[self.poster_present_1dvd,
                                                  self.poster_present_3cd])

    order_line_1 = order.newContent(portal_type=self.order_line_portal_type,
                                    resource_value=self.music_album_4,
                                    quantity=2,
                                    price=3000)
    self.appendBaseContributionCategory(order_line_1, self.poster_present_3cd)

    self.stepTic()

    # check the current amount
    self.assertEqual(self.getAmount(order, 'TOTAL_PRICE_WITHOUT_VAT'), 6000)
    self.assertEqual(self.getAmount(order, 'VAT_AMOUNT'), 300)
    self.assertEqual(self.getAmount(order, 'TOTAL_PRICE_WITH_VAT'), 6300)
    self.assertEqual(self.getAmount(order, '3CD_OR_1DVD_GET_1_POSTER_FREE'),
                     None)
    # add 1 dvd, then 1 poster will be given.
    order_line_2 = order.newContent(portal_type=self.order_line_portal_type,
                                    resource_value=self.movie_dvd_1,
                                    quantity=1,
                                    price=3000)
    self.appendBaseContributionCategory(order_line_2, self.poster_present_1dvd)

    self.stepTic()
    
    # check again
543 544 545 546 547 548
    one_free_poster_amount_list = self.getAmount(
      order,
      '3CD_OR_1DVD_GET_1_POSTER_FREE',
      return_object=True)
    self.assertEqual(len(one_free_poster_amount_list), 1)
    one_free_poster_amount = one_free_poster_amount_list[0]
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
    self.assertEqual(one_free_poster_amount.getTotalPrice(), 0)
    self.assertEqual(one_free_poster_amount.getQuantity(), 1)
    self.assertEqual(one_free_poster_amount.getPrice(), 0)
    self.assertEqual(one_free_poster_amount.getResourceValue(), self.poster)
    self.assertEqual(self.getAmount(order, 'TOTAL_PRICE_WITHOUT_VAT'), 9000)
    self.assertEqual(self.getAmount(order, 'VAT_AMOUNT'), 450)
    self.assertEqual(self.getAmount(order, 'TOTAL_PRICE_WITH_VAT'), 9450)

    # even if we buy 3 CDs and 1 DVD, only one poster will be given.
    order_line_3 = order.newContent(portal_type=self.order_line_portal_type,
                                    resource_value=self.music_album_3,
                                    quantity=1,
                                    price=2400)
    self.appendBaseContributionCategory(order_line_3, self.poster_present_3cd)

    self.stepTic()

    # check again
567
    one_free_poster_amount_list = self.getAmount(order,
568 569
                                            '3CD_OR_1DVD_GET_1_POSTER_FREE',
                                            return_object=True)
570 571
    self.assertEqual(len(one_free_poster_amount_list), 1)
    one_free_poster_amount = one_free_poster_amount_list[0]
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
    self.assertEqual(one_free_poster_amount.getTotalPrice(), 0)
    self.assertEqual(one_free_poster_amount.getQuantity(), 1)
    self.assertEqual(one_free_poster_amount.getPrice(), 0)
    self.assertEqual(one_free_poster_amount.getResourceValue(), self.poster)
    self.assertEqual(self.getAmount(order, 'TOTAL_PRICE_WITHOUT_VAT'), 11400)
    self.assertEqual(self.getAmount(order, 'VAT_AMOUNT'), 570)
    self.assertEqual(self.getAmount(order, 'TOTAL_PRICE_WITH_VAT'), 11970)

  def test_usecase5(self):
    """
    Use case 5 : Buy 3 CDs or more, 1 highest priced DVD in ordered 15% off.

    1 DVD    3000 yen
    1 DVD    1000 yen
    2 CD    10000 yen
    1 CD     3000 yen
    discount 3000 * 0.15 = 450 yen
    """
    createZODBPythonScript(
      self.portal.portal_skins.custom,
      'TradeModelLine_calculate3CD15PercentDiscountOf1HighestPricedDVD',
      'current_aggregated_amount_list, current_movement, aggregated_movement_list',
      '''\
total_quantity = sum([movement.getQuantity() for movement in aggregated_movement_list])
if total_quantity >= 3:
  price_dvd_list = []
  product_line_dvd = context.portal_categories.product_line.video.dvd
  for movement in current_aggregated_amount_list:
    resource = movement.getResourceValue()
    if resource.getProductLineValue() == product_line_dvd:
      price_dvd_list.append((movement.getPrice(), movement))
  if price_dvd_list:
    price_dvd_list.sort()
    highest_priced_dvd_movement = price_dvd_list[-1][1]
    total_price = highest_priced_dvd_movement.getTotalPrice()

    from Products.ERP5Type.Document import newTempSimulationMovement
    causality_value_list = list(aggregated_movement_list) + [highest_priced_dvd_movement]
    temporary_movement = newTempSimulationMovement(current_movement.getParentValue(), current_movement.getId())
    temporary_movement.edit(title=current_movement.getProperty('title'),
                            description=current_movement.getProperty('description'),
                            resource=current_movement.getProperty('resource'),
                            reference=current_movement.getProperty('reference'),
                            int_index=current_movement.getProperty('int_index'),
                            base_application_list=current_movement.getProperty('base_application_list'),
                            base_contribution_list=current_movement.getProperty('base_contribution_list'),
                            start_date=highest_priced_dvd_movement.getStartDate(),
                            stop_date=highest_priced_dvd_movement.getStopDate(),
                            create_line=current_movement.getProperty('is_create_line'),
                            trade_phase_list=current_movement.getTradePhaseList(),
                            causality_list=[movement.getRelativeUrl() for movement in causality_value_list])
    temporary_movement.setPrice(current_movement.getProperty('price'))
    temporary_movement.setQuantity(highest_priced_dvd_movement.getPrice())
    return temporary_movement
''')
    order = self.createOrder()
    order.edit(specialise_value=self.trade_condition)
    order.Order_applyTradeCondition(order.getSpecialiseValue())
    order.newContent(portal_type='Trade Model Line',
                     reference='3CD_AND_1HIGHEST_PRICED_DVD_15PERCENT_OFF',
                     resource_value=self.service_discount,
                     price=-0.15,
                     quantity=None,
                     efficiency=1,
                     target_level=TARGET_LEVEL_DELIVERY,
                     calculation_script_id='TradeModelLine_calculate3CD15PercentDiscountOf1HighestPricedDVD',
                     create_line=True,
                     trade_phase=None,
                     base_application_value_list=[self.special_discount_3cd],
                     base_contribution_value_list=[self.discount_amount_of_vat_taxable])

    order_line_1 = order.newContent(portal_type=self.order_line_portal_type,
                                    resource_value=self.movie_dvd_1,
                                    quantity=1,
                                    price=3000)
    order_line_2 = order.newContent(portal_type=self.order_line_portal_type,
                                    resource_value=self.movie_dvd_2,
                                    quantity=1,
                                    price=1000)
    order_line_3 = order.newContent(portal_type=self.order_line_portal_type,
                                    resource_value=self.music_album_1,
                                    quantity=1,
                                    price=5000)
    self.appendBaseContributionCategory(order_line_3, self.special_discount_3cd)
    order_line_4 = order.newContent(portal_type=self.order_line_portal_type,
                                    resource_value=self.music_album_2,
                                    quantity=1,
                                    price=3000)
    self.appendBaseContributionCategory(order_line_4, self.special_discount_3cd)

    self.stepTic()

    # check the current amount
    self.assertEqual(self.getAmount(order, 'TOTAL_PRICE_WITHOUT_VAT'), 12000)
    self.assertEqual(self.getAmount(order, 'VAT_AMOUNT'), 600)
    self.assertEqual(self.getAmount(order, 'TOTAL_PRICE_WITH_VAT'),
                     12600)
    # add one more cd, then total is 3. the special discount will be applied.
    order_line_3.setQuantity(2)

    self.stepTic()

    # check again
    self.assertEqual(self.getAmount(order, '3CD_AND_1HIGHEST_PRICED_DVD_15PERCENT_OFF'),
                     -450)
    self.assertEqual(self.getAmount(order, 'TOTAL_PRICE_WITHOUT_VAT'), 16550)
    self.assertEqual(self.getAmount(order, 'VAT_AMOUNT'), 827.5)
    self.assertEqual(self.getAmount(order, 'TOTAL_PRICE_WITH_VAT'), 17377.5)
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 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746

  def test_usecase6(self):
    """
    Use case 6 : Add a shipping fee by TradeModelLine and VAT is charged to
    this fee.
    """
    order = self.createOrder()
    order.edit(specialise_value=self.trade_condition)
    order.Order_applyTradeCondition(order.getSpecialiseValue())
    order.newContent(portal_type='Trade Model Line',
                     reference='SHIPPING_FEE',
                     resource_value=self.service_shipping_fee,
                     price=1,
                     quantity=500,
                     efficiency=1,
                     target_level=TARGET_LEVEL_DELIVERY,
                     create_line=True,
                     trade_phase=None,
                     base_application_value_list=[],
                     base_contribution_value_list=[self.additional_charge,
                                                   self.vat_taxable])

    order_line_1 = order.newContent(portal_type=self.order_line_portal_type,
                                    resource_value=self.movie_dvd_1,
                                    quantity=1,
                                    price=3000)
    order_line_2 = order.newContent(portal_type=self.order_line_portal_type,
                                    resource_value=self.movie_dvd_2,
                                    quantity=1,
                                    price=1000)

    self.stepTic()

    # check amounts
    self.assertEqual(self.getAmount(order, 'TOTAL_PRICE_WITHOUT_VAT'), 4500)
    self.assertEqual(self.getAmount(order, 'VAT_AMOUNT'), 225)
    self.assertEqual(
      len(self.getAmount(order, 'VAT_AMOUNT', return_object=True)),
      1)
    self.assertEqual(self.getAmount(order, 'TOTAL_PRICE_WITH_VAT'), 4725)

    # change trade model line and calculate vat price per movement
    order.newContent(portal_type='Trade Model Line',
                     title='VAT Amount',
                     reference='VAT_AMOUNT',
                     resource_value=self.service_vat,
                     price=0.05,
                     quantity=None,
                     efficiency=1,
                     target_level=TARGET_LEVEL_MOVEMENT,
                     create_line=True,
                     trade_phase_value=self.portal.portal_categories.trade_phase.default.invoicing,
                     base_application_value_list=[self.discount_amount_of_vat_taxable,
                                                  self.vat_taxable],
                     base_contribution_value_list=[self.vat_amount])

    self.stepTic()

    # check amounts again
    self.assertEqual(self.getAmount(order, 'TOTAL_PRICE_WITHOUT_VAT'), 4500)
    self.assertEqual(self.getAmount(order, 'VAT_AMOUNT'), 225)
    self.assertEqual(
      len(self.getAmount(order, 'VAT_AMOUNT', return_object=True)),
      3)
    self.assertEqual(self.getAmount(order, 'TOTAL_PRICE_WITH_VAT'), 4725)


747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
class TestComplexTradeModelLineUseCaseSale(TestComplexTradeModelLineUseCase):
  order_portal_type = 'Sale Order'
  order_line_portal_type = 'Sale Order Line'
  trade_condition_portal_type = 'Sale Trade Condition'


class TestComplexTradeModelLineUseCasePurchase(TestComplexTradeModelLineUseCase):
  order_portal_type = 'Purchase Order'
  order_line_portal_type = 'Purchase Order Line'
  trade_condition_portal_type = 'Purchase Trade Condition'


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