testComplexTradeModelLineUseCase.py 21.1 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
# -*- 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
from Products.ERP5.tests.testTradeModelLine import TestTradeModelLineMixin
32

33 34 35 36 37 38

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.
  """

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
  def appendBaseContributionCategory(self, document, *new_category):
    document.setBaseContributionList(
      document.getBaseContributionList() + list(new_category))

  def createCategories(self):
    super(TestComplexTradeModelLineUseCase, self).createCategories()
    category_tool = self.portal.portal_categories
    self.createCategoriesInCategory(category_tool.base_amount, (
      'additional_charge',
      'discount_amount',
      'discount_amount_of_non_vat_taxable',
      'discount_amount_of_vat_taxable',
      'total_price_of_ordered_items',
      'total_price_of_vat_taxable',
      'total_price_without_vat',
      'total_price_with_vat',
      'vat_amount',
      'vat_taxable',
      ))
    self.createCategoriesInCategory(category_tool.product_line, (
      'audio', 'video', 'other'))
    self.createCategoriesInCategory(category_tool.product_line.audio, ('cd',))
    self.createCategoriesInCategory(category_tool.product_line.video, ('dvd',))
    self.createCategoriesInCategory(category_tool.quantity_unit, ('unit',))
63 64

  def afterSetUp(self):
65
    super(TestComplexTradeModelLineUseCase, self).afterSetUp()
66 67 68
    portal = self.portal

    # add currency
69 70
    jpy = portal.currency_module.newContent(title='Yen', reference='JPY',
                                            base_unit_quantity='1')
71 72 73 74 75 76
    # add organisations
    my_company = portal.organisation_module.newContent(title='My Company')
    client_1 = portal.organisation_module.newContent(title='Client 1')

    # create services
    self.service_vat = portal.service_module.newContent(title='VAT')
77 78
    self.service_discount = portal.service_module.newContent(title='DISCOUNT')
    self.service_shipping_fee = portal.service_module.newContent(title='SHIPPING FEE')
79 80

    # create products
81
    def addProductDocument(title, product_line):
82 83
      return portal.product_module.newContent(
        title=title,
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
        product_line=product_line,
        quantity_unit='unit',
        base_contribution_list=('base_amount/vat_taxable',
                                'base_amount/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')
    self.poster = addProductDocument('Poster', 'other')
    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')

    # create a trade condition with several common trade model lines
    self.trade_condition = self.createTradeCondition(
      self.createBusinessProcess(), (
      dict(title='Total Price Without VAT',
           reference='TOTAL_PRICE_WITHOUT_VAT',
           price=1,
           int_index=10,
           target_delivery=True,
           base_application_list=('base_amount/discount_amount_of_non_vat_taxable',
                                  'base_amount/discount_amount_of_vat_taxable',
                                  'base_amount/total_price_of_ordered_items',
                                  'base_amount/additional_charge'),
           base_contribution='base_amount/total_price_without_vat'),
      dict(title='Total Price Of VAT Taxable',
           reference='TOTAL_PRICE_OF_VAT_TAXABLE',
           price=1,
           int_index=10,
           target_delivery=True,
           base_application_list=('base_amount/discount_amount_of_vat_taxable',
                                  'base_amount/vat_taxable'),
           base_contribution='base_amount/total_price_of_vat_taxable'),
      dict(title='Discount Amount',
           reference='DISCOUNT_AMOUNT',
           resource_value=self.service_discount,
           price=1,
           trade_phase='default/invoicing',
           int_index=10,
           target_delivery=True,
           base_application_list=('base_amount/discount_amount_of_vat_taxable',
                                  'base_amount/discount_amount_of_non_vat_taxable'),
           base_contribution='base_amount/discount_amount'),
      dict(title='VAT Amount',
           reference='VAT_AMOUNT',
           resource_value=self.service_vat,
           price=0.05,
           trade_phase='default/invoicing',
           int_index=10,
           target_delivery=True,
           base_application_list=('base_amount/discount_amount_of_vat_taxable',
                                  'base_amount/vat_taxable'),
           base_contribution='base_amount/vat_amount'),
      dict(title='Total Price With VAT',
           reference='TOTAL_PRICE_WITH_VAT',
           price=1,
           int_index=20,
           target_delivery=True,
           base_application_list=('base_amount/vat_amount',
                                  'base_amount/total_price_without_vat'),
           base_contribution='base_amount/total_price_with_vat')
      ),
147 148 149 150 151 152 153 154
      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)

155
    self.tic()
156 157 158 159 160 161 162 163 164 165 166

  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
    """
167 168 169 170 171
    special_discount = self.setBaseAmountQuantityMethod(
      'special_discount', """\
def getBaseAmountQuantity(delivery_amount, base_application, **kw):
  if delivery_amount.isDelivery():
    total_quantity = sum([movement.getQuantity()
172
      for movement in delivery_amount.getBaseAmountList()
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
      if base_application in movement.getBaseContributionList()])
    if total_quantity < 3:
      return 0
  return context.getBaseAmountQuantity(
    delivery_amount, base_application, **kw)
return getBaseAmountQuantity""")

    trade_condition = self.createTradeCondition(
      self.trade_condition, (
      dict(reference='SPECIAL_DISCOUNT_3CD_LINEAR',
           resource_value=self.service_discount,
           price=-0.1,
           int_index=0,
           target_delivery=True,
           base_application=special_discount,
           base_contribution='base_amount/discount_amount_of_vat_taxable'),
      ))

    order = self.createOrder(trade_condition, (
      dict(id='1', price=5000, quantity=1, resource_value=self.music_album_1),
      dict(id='2', price=3000, quantity=1, resource_value=self.music_album_2),
      dict(id='3', price=100, quantity=1, resource_value=self.candy),
      ))
    self.appendBaseContributionCategory(order['1'], special_discount)
    self.appendBaseContributionCategory(order['2'], special_discount)
198
    self.commit()
199
    self.getAggregatedAmountDict(order, partial_check=True,
200
      SPECIAL_DISCOUNT_3CD_LINEAR=dict(total_price=0),
201 202 203
      TOTAL_PRICE_WITHOUT_VAT=dict(total_price=8100),
      TOTAL_PRICE_WITH_VAT=dict(total_price=8505),
      VAT_AMOUNT=dict(total_price=405))
204 205

    # add one more cd, then total is 3. the special discount will be applied.
206 207 208 209 210
    line = order.newContent(portal_type=self.order_line_portal_type,
                            resource_value=self.music_album_3,
                            quantity=1,
                            price=2400)
    self.appendBaseContributionCategory(line, special_discount)
211
    self.commit()
212 213 214 215 216
    self.getAggregatedAmountDict(order, partial_check=True,
      SPECIAL_DISCOUNT_3CD_LINEAR=dict(total_price=-1040),
      TOTAL_PRICE_WITHOUT_VAT=dict(total_price=9460),
      TOTAL_PRICE_WITH_VAT=dict(total_price=9933),
      VAT_AMOUNT=dict(total_price=473))
217 218 219 220 221 222 223 224 225 226 227

  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
    """
228 229 230 231
    special_discount = self.setBaseAmountQuantityMethod(
      'special_discount', """\
return lambda delivery_amount, base_application, **kw: \\
  3 <= sum([movement.getQuantity()
232
            for movement in delivery_amount.getBaseAmountList()
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
            if base_application in movement.getBaseContributionList()])""")

    trade_condition = self.createTradeCondition(
      self.trade_condition, (
      dict(reference='SPECIAL_DISCOUNT_3CD_FIXED',
           resource_value=self.service_discount,
           price=-1,
           quantity=500,
           int_index=0,
           target_delivery=True,
           base_application=special_discount,
           base_contribution='base_amount/discount_amount_of_vat_taxable'),
      ))

    order = self.createOrder(trade_condition, (
      dict(id='1', price=5000, quantity=1, resource_value=self.music_album_1),
      dict(id='2', price=3000, quantity=1, resource_value=self.music_album_2),
      dict(id='3', price=3000, quantity=1, resource_value=self.movie_dvd_1),
      ))
    self.appendBaseContributionCategory(order['1'], special_discount)
    self.appendBaseContributionCategory(order['2'], special_discount)
254
    self.commit()
255
    self.getAggregatedAmountDict(order, partial_check=True,
256
      SPECIAL_DISCOUNT_3CD_FIXED=dict(total_price=0),
257 258 259
      TOTAL_PRICE_WITHOUT_VAT=dict(total_price=11000),
      TOTAL_PRICE_WITH_VAT=dict(total_price=11550),
      VAT_AMOUNT=dict(total_price=550))
260 261

    # add one more cd, then total is 3. the special discount will be applied.
262 263 264 265 266
    line = order.newContent(portal_type=self.order_line_portal_type,
                            resource_value=self.music_album_3,
                            quantity=1,
                            price=2400)
    self.appendBaseContributionCategory(line, special_discount)
267
    self.commit()
268 269 270 271 272
    self.getAggregatedAmountDict(order, partial_check=True,
      SPECIAL_DISCOUNT_3CD_FIXED=dict(total_price=-500),
      TOTAL_PRICE_WITHOUT_VAT=dict(total_price=12900),
      TOTAL_PRICE_WITH_VAT=dict(total_price=13545),
      VAT_AMOUNT=dict(total_price=645))
273 274 275 276 277 278 279 280 281 282 283

  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
    """
284 285 286 287
    special_discount = self.setBaseAmountQuantityMethod(
      'special_discount', """\
def getBaseAmountQuantity(delivery_amount, base_application, **kw):
  total_quantity = sum([movement.getQuantity()
288
    for movement in delivery_amount.getBaseAmountList()
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
    if base_application in movement.getBaseContributionList()])
  if total_quantity < 3:
    return 0
  return delivery_amount.getGeneratedAmountQuantity(
    'base_amount/total_price_of_ordered_items')
return getBaseAmountQuantity""")

    trade_condition = self.createTradeCondition(
      self.trade_condition, (
      dict(reference='SPECIAL_DISCOUNT_3CD_LINEAR',
           resource_value=self.service_discount,
           price=-0.1,
           int_index=0,
           target_delivery=True,
           base_application=special_discount,
           base_contribution='base_amount/discount_amount_of_vat_taxable'),
      ))

    order = self.createOrder(trade_condition, (
      dict(id='1', price=5000, quantity=1, resource_value=self.music_album_1),
      dict(id='2', price=3000, quantity=1, resource_value=self.movie_dvd_1),
      dict(id='3', price=3000, quantity=1, resource_value=self.music_album_2),
      ))
    self.appendBaseContributionCategory(order['1'], special_discount)
    self.appendBaseContributionCategory(order['3'], special_discount)
314
    self.commit()
315
    self.getAggregatedAmountDict(order, partial_check=True,
316
      SPECIAL_DISCOUNT_3CD_LINEAR=dict(total_price=0),
317 318 319
      TOTAL_PRICE_WITHOUT_VAT=dict(total_price=11000),
      TOTAL_PRICE_WITH_VAT=dict(total_price=11550),
      VAT_AMOUNT=dict(total_price=550))
320 321

    # add one more cd, then total is 3. the special discount will be applied.
322 323 324 325 326
    line = order.newContent(portal_type=self.order_line_portal_type,
                            resource_value=self.music_album_3,
                            quantity=1,
                            price=2400)
    self.appendBaseContributionCategory(line, special_discount)
327
    self.commit()
328 329 330 331 332
    self.getAggregatedAmountDict(order, partial_check=True,
      SPECIAL_DISCOUNT_3CD_LINEAR=dict(total_price=-1340),
      TOTAL_PRICE_WITHOUT_VAT=dict(total_price=12060),
      TOTAL_PRICE_WITH_VAT=dict(total_price=12663),
      VAT_AMOUNT=dict(total_price=603))
333 334 335 336 337 338 339 340 341

  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
    """
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
    total_quantity = """\
def getBaseAmountQuantity(delivery_amount, base_application, **kw):
  value = delivery_amount.getGeneratedAmountQuantity(base_application)
  if base_application in delivery_amount.getBaseContributionList():
    value += delivery_amount.getQuantity()
  return value
return getBaseAmountQuantity"""
    poster_present_1dvd = self.setBaseAmountQuantityMethod(
      'poster_present_1dvd', total_quantity)
    poster_present_3cd = self.setBaseAmountQuantityMethod(
      'poster_present_3cd', total_quantity)
    special_discount = self.setBaseAmountQuantityMethod(
      'special_discount', """\
return lambda delivery_amount, base_application, **kw: \\
  3 <= delivery_amount.getGeneratedAmountQuantity(%r) or \\
  1 <= delivery_amount.getGeneratedAmountQuantity(%r)"""
      % (poster_present_3cd, poster_present_1dvd))

    trade_condition = self.createTradeCondition(
      self.trade_condition, (
      dict(reference='SPECIAL_DISCOUNT_3CD_OR_1DVD_FIXED',
           resource_value=self.poster,
           price=0,
           int_index=0,
           target_delivery=True,
           base_application=special_discount),
      ))

    order = self.createOrder(trade_condition, (
      dict(id='1', price=3000, quantity=2, resource_value=self.music_album_4),
      ))
    self.appendBaseContributionCategory(order['1'], poster_present_3cd)
374
    self.commit()
375
    self.getAggregatedAmountDict(order, partial_check=True,
376
      SPECIAL_DISCOUNT_3CD_OR_1DVD_FIXED=dict(total_price=0),
377 378 379
      TOTAL_PRICE_WITHOUT_VAT=dict(total_price=6000),
      TOTAL_PRICE_WITH_VAT=dict(total_price=6300),
      VAT_AMOUNT=dict(total_price=300))
380 381

    # add 1 dvd, then 1 poster will be given.
382 383 384 385 386
    line = order.newContent(portal_type=self.order_line_portal_type,
                            resource_value=self.movie_dvd_1,
                            quantity=1,
                            price=3000)
    self.appendBaseContributionCategory(line, poster_present_1dvd)
387
    self.commit()
388
    self.getAggregatedAmountDict(order, partial_check=True,
389
      SPECIAL_DISCOUNT_3CD_OR_1DVD_FIXED=dict(total_price=0, quantity=1,
390 391 392 393
                                              resource_value=self.poster),
      TOTAL_PRICE_WITHOUT_VAT=dict(total_price=9000),
      TOTAL_PRICE_WITH_VAT=dict(total_price=9450),
      VAT_AMOUNT=dict(total_price=450))
394 395

    # even if we buy 3 CDs and 1 DVD, only one poster will be given.
396 397 398 399 400
    line = order.newContent(portal_type=self.order_line_portal_type,
                            resource_value=self.music_album_3,
                            quantity=1,
                            price=2400)
    self.appendBaseContributionCategory(line, poster_present_3cd)
401
    self.commit()
402
    self.getAggregatedAmountDict(order, partial_check=True,
403
      SPECIAL_DISCOUNT_3CD_OR_1DVD_FIXED=dict(total_price=0, quantity=1,
404 405 406 407
                                              resource_value=self.poster),
      TOTAL_PRICE_WITHOUT_VAT=dict(total_price=11400),
      TOTAL_PRICE_WITH_VAT=dict(total_price=11970),
      VAT_AMOUNT=dict(total_price=570))
408 409 410 411 412 413 414 415 416 417 418

  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
    """
419 420 421 422
    special_discount = self.setBaseAmountQuantityMethod(
      'special_discount', """\
def getBaseAmountQuantity(delivery_amount, base_application, **kw):
  highest_price = quantity = 0
423
  for movement in delivery_amount.getBaseAmountList():
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
    if base_application in movement.getBaseContributionList():
      quantity += movement.getQuantity()
    if movement.getResourceValue().getProductLine() == 'video/dvd':
      highest_price = max(highest_price, movement.getPrice())
  return quantity >= 3 and highest_price
return getBaseAmountQuantity""")

    trade_condition = self.createTradeCondition(
      self.trade_condition, (
      dict(reference='SPECIAL_DISCOUNT_3CD',
           resource_value=self.service_discount,
           price=-0.15,
           int_index=0,
           target_delivery=True,
           base_application=special_discount,
           base_contribution='base_amount/discount_amount_of_vat_taxable'),
      ))

    order = self.createOrder(trade_condition, (
      dict(id='1', price=3000, quantity=1, resource_value=self.movie_dvd_1),
      dict(id='2', price=1000, quantity=1, resource_value=self.movie_dvd_2),
      dict(id='3', price=5000, quantity=1, resource_value=self.music_album_1),
      dict(id='4', price=3000, quantity=1, resource_value=self.music_album_2),
      ))
    self.appendBaseContributionCategory(order['3'], special_discount)
    self.appendBaseContributionCategory(order['4'], special_discount)
450
    self.commit()
451
    self.getAggregatedAmountDict(order, partial_check=True,
452
      SPECIAL_DISCOUNT_3CD=dict(total_price=0),
453 454 455
      TOTAL_PRICE_WITHOUT_VAT=dict(total_price=12000),
      TOTAL_PRICE_WITH_VAT=dict(total_price=12600),
      VAT_AMOUNT=dict(total_price=600))
456 457

    # add one more cd, then total is 3. the special discount will be applied.
458
    order['3'].setQuantity(2)
459
    self.commit()
460 461 462 463 464
    self.getAggregatedAmountDict(order, partial_check=True,
      SPECIAL_DISCOUNT_3CD=dict(total_price=-450),
      TOTAL_PRICE_WITHOUT_VAT=dict(total_price=16550),
      TOTAL_PRICE_WITH_VAT=dict(total_price=17377.5),
      VAT_AMOUNT=dict(total_price=827.5))
465 466 467 468 469 470

  def test_usecase6(self):
    """
    Use case 6 : Add a shipping fee by TradeModelLine and VAT is charged to
    this fee.
    """
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
    fixed_quantity = self.setBaseAmountQuantityMethod('fixed_quantity', """\
return lambda *args, **kw: 1""")

    trade_condition = self.createTradeCondition(
      self.trade_condition, (
      dict(reference='SHIPPING_FEE',
           resource_value=self.service_discount,
           quantity=500,
           int_index=0,
           target_delivery=True,
           base_application=fixed_quantity,
           base_contribution_list=('base_amount/additional_charge',
                                   'base_amount/vat_taxable')),
      ))

    order = self.createOrder(trade_condition, (
      dict(id='1', price=3000, quantity=1, resource_value=self.movie_dvd_1),
      dict(id='2', price=1000, quantity=1, resource_value=self.movie_dvd_2),
      ))
490
    self.commit()
491 492 493 494
    self.getAggregatedAmountDict(order, partial_check=True,
      TOTAL_PRICE_WITHOUT_VAT=dict(total_price=4500),
      TOTAL_PRICE_WITH_VAT=dict(total_price=4725),
      VAT_AMOUNT=dict(total_price=225))
495 496


497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
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