testTradeCondition.py 32 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
##############################################################################
#
# Copyright (c) 2008 Nexedi SA and Contributors. All Rights Reserved.
#          Jerome Perrin <jerome@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################

import unittest

31
import transaction
32 33 34 35 36 37 38 39
from DateTime import DateTime

from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase

class TradeConditionTestCase(ERP5TypeTestCase):
  """Tests for Trade Conditions and Tax
  """
  def getBusinessTemplateList(self):
40
    return ('erp5_base', 'erp5_pdm', 'erp5_trade', 'erp5_accounting',
41 42
            'erp5_invoicing', 'erp5_tax_resource', 'erp5_discount_resource',
            'erp5_legacy_tax_system', 'erp5_simplified_invoicing',)
43

44 45
  size_category_list = ['small', 'big']
  def afterSetUp(self):
46
    self.validateRules()
47 48 49
    for category_id in self.size_category_list:
      self.portal.portal_categories.size.newContent(id=category_id,
                                                    title=category_id)
50 51 52 53
    self.base_amount = self.portal.portal_categories.base_amount
    self.tax = self.portal.tax_module.newContent(
                                    portal_type='Tax',
                                    title='Tax')
54 55 56
    self.discount = self.portal.discount_module.newContent(
                                    portal_type='Discount',
                                    title='Discount')
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    self.client = self.portal.organisation_module.newContent(
                                    portal_type='Organisation',
                                    title='Client')
    self.vendor = self.portal.organisation_module.newContent(
                                    portal_type='Organisation',
                                    title='Vendor')
    self.resource = self.portal.product_module.newContent(
                                    portal_type='Product',
                                    title='Resource')
    self.currency = self.portal.currency_module.newContent(
                                    portal_type='Currency',
                                    title='Currency')
    self.trade_condition_module = self.portal.getDefaultModule(
                                      self.trade_condition_type)
    self.trade_condition = self.trade_condition_module.newContent(
                            portal_type=self.trade_condition_type,
                            title='Trade Condition')
    self.order_module = self.portal.getDefaultModule(
                                      self.order_type)
    self.order = self.order_module.newContent(
                            portal_type=self.order_type,
                            created_by_builder=1,
                            title='Order')

81
  def beforeTearDown(self):
82
    transaction.abort()
83 84 85 86 87 88 89 90 91
    for module in (self.portal.tax_module,
                   self.portal.organisation_module,
                   self.portal.currency_module,
                   self.portal.product_module,
                   self.portal.accounting_module,
                   self.portal.account_module,
                   self.portal.portal_simulation,
                   self.trade_condition_module,
                   self.order_module,
92 93 94
                   self.portal.portal_categories.base_amount,
                   self.portal.portal_categories.size,
      ):
95
      module.manage_delObjects(list(module.objectIds()))
96
    transaction.commit()
97 98 99 100 101 102 103
    self.tic()


class AccountingBuildTestCase(TradeConditionTestCase):
  """Same as TradeConditionTestCase, but with a rule to generate
  accounting.
  """
104 105
  def afterSetUp(self):
    TradeConditionTestCase.afterSetUp(self)
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
    self.receivable_account = self.portal.account_module.newContent(
                                    id='receivable',
                                    title='Receivable',
                                    account_type='asset/receivable')
    self.payable_account = self.portal.account_module.newContent(
                                    id='payable',
                                    title='Payable',
                                    account_type='liability/payable')
    self.income_account = self.portal.account_module.newContent(
                                    id='income',
                                    title='Income',
                                    account_type='income')
    self.expense_account = self.portal.account_module.newContent(
                                    id='expense',
                                    title='Expense',
                                    account_type='expense')
    self.collected_tax_account = self.portal.account_module.newContent(
                                    id='collected_tax',
                                    title='Collected Tax',
                                    account_type='liability/payable/collected_vat')
    self.refundable_tax_account = self.portal.account_module.newContent(
                                    id='refundable_tax',
                                    title='Refundable Tax',
                                    account_type='asset/receivable/refundable_vat')

    for account in self.portal.account_module.contentValues():
      self.assertNotEquals(account.getAccountTypeValue(), None)
      account.validate()
    
    itr = self.portal.portal_rules.newContent(
Kazuhiko Shiozaki's avatar
Kazuhiko Shiozaki committed
136
                        portal_type='Invoice Transaction Simulation Rule',
137 138 139
                        reference='default_invoice_transaction_rule',
                        id='test_invoice_transaction_rule',
                        title='Transaction Rule',
Kazuhiko Shiozaki's avatar
Kazuhiko Shiozaki committed
140
                        test_method_id='SimulationMovement_testInvoiceTransactionSimulationRule',
141 142 143 144 145 146
                        version=100)
    predicate = itr.newContent(portal_type='Predicate',)
    predicate.edit(
            string_index='resource_type',
            title='Resource Product',
            int_index=1,
147
            test_method_id='SimulationMovement_isDeliveryMovement' )
148 149 150 151 152
    predicate = itr.newContent(portal_type='Predicate')
    predicate.edit(
            string_index='resource_type',
            title='Resource Tax',
            int_index=2,
153
            test_method_id='SimulationMovement_isTaxMovement' )
154
    transaction.commit()
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
    self.tic()
    accounting_rule_cell_list = itr.contentValues(
                            portal_type='Accounting Rule Cell')
    self.assertEquals(2, len(accounting_rule_cell_list))
    product_rule_cell = itr._getOb("movement_0")
    self.assertEquals(product_rule_cell.getTitle(), 'Resource Product')
    product_rule_cell.newContent(
                         portal_type='Accounting Transaction Line',
                         source_value=self.receivable_account,
                         destination_value=self.payable_account,
                         quantity=-1)
    product_rule_cell.newContent(
                         portal_type='Accounting Transaction Line',
                         source_value=self.income_account,
                         destination_value=self.expense_account,
                         quantity=1)
    
    tax_rule_cell = itr._getOb("movement_1")
    self.assertEquals(tax_rule_cell.getTitle(), 'Resource Tax')
    tax_rule_cell.newContent(
                         portal_type='Accounting Transaction Line',
                         source_value=self.receivable_account,
                         destination_value=self.payable_account,
                         quantity=-1)
    tax_rule_cell.newContent(
                         portal_type='Accounting Transaction Line',
                         source_value=self.collected_tax_account,
                         destination_value=self.refundable_tax_account,
                         quantity=1)
    itr.validate()
185
    transaction.commit()
186 187
    self.tic()

188 189 190
  def beforeTearDown(self):
    TradeConditionTestCase.beforeTearDown(self)
    self.portal.portal_rules.manage_delObjects('test_invoice_transaction_rule')
191
    transaction.commit()
192
    self.tic()
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269

class TestApplyTradeCondition(TradeConditionTestCase):
  """Tests Applying Trade Conditions
  """
  def test_apply_trade_condition_set_categories(self):
    self.trade_condition.setSourceSectionValue(self.vendor)
    self.trade_condition.setDestinationSectionValue(self.client)
    self.trade_condition.setSourceValue(self.vendor)
    self.trade_condition.setDestinationValue(self.client)
    self.trade_condition.setPriceCurrencyValue(self.currency)
    self.order.setSpecialiseValue(self.trade_condition)

    self.order.Order_applyTradeCondition(self.trade_condition, force=1)
    
    self.assertEquals(self.vendor, self.order.getSourceSectionValue())
    self.assertEquals(self.vendor, self.order.getSourceValue())
    self.assertEquals(self.client, self.order.getDestinationSectionValue())
    self.assertEquals(self.client, self.order.getDestinationValue())
    self.assertEquals(self.currency, self.order.getPriceCurrencyValue())

  def test_apply_trade_condition_keep_categories(self):
    # source section & source are set on the order, not on the TC
    self.order.setSourceSectionValue(self.vendor)
    self.order.setSourceValue(self.vendor)

    self.trade_condition.setSourceSectionValue(None)
    self.trade_condition.setSourceValue(None)
    self.trade_condition.setDestinationSectionValue(self.client)
    self.trade_condition.setDestinationValue(self.client)
    self.trade_condition.setPriceCurrencyValue(self.currency)
    self.order.setSpecialiseValue(self.trade_condition)

    self.order.Order_applyTradeCondition(self.trade_condition, force=1)
    
    # Applying the TC keeps values on the order
    self.assertEquals(self.vendor, self.order.getSourceSectionValue())
    self.assertEquals(self.vendor, self.order.getSourceValue())
    self.assertEquals(self.client, self.order.getDestinationSectionValue())
    self.assertEquals(self.client, self.order.getDestinationValue())
    self.assertEquals(self.currency, self.order.getPriceCurrencyValue())

  def test_apply_trade_condition_set_categories_with_hierarchy(self):
    trade_condition_source = self.trade_condition_module.newContent(
                            portal_type=self.trade_condition.getPortalType(),
                            title='Trade Condition Source',
                            source_value=self.vendor,
                            source_section_value=self.vendor)
    trade_condition_dest = self.trade_condition_module.newContent(
                            portal_type=self.trade_condition.getPortalType(),
                            title='Trade Condition Destination',
                            destination_value=self.client,
                            destination_section_value=self.client,
                            price_currency_value=self.currency,
                            # also set a source, it should not be used
                            source_value=self.client)
    self.trade_condition.setSpecialiseValueList(
        (trade_condition_source, trade_condition_dest))

    self.order.Order_applyTradeCondition(self.trade_condition, force=1)
    
    self.assertEquals(self.vendor, self.order.getSourceSectionValue())
    self.assertEquals(self.vendor, self.order.getSourceValue())
    self.assertEquals(self.client, self.order.getDestinationSectionValue())
    self.assertEquals(self.client, self.order.getDestinationValue())
    self.assertEquals(self.currency, self.order.getPriceCurrencyValue())

  def test_apply_trade_condition_copy_subobjects(self):
    self.trade_condition.setPaymentConditionTradeDate('custom')
    self.trade_condition.setPaymentConditionPaymentDate(DateTime(2001, 01, 01))
    self.order.setSpecialiseValue(self.trade_condition)

    self.order.Order_applyTradeCondition(self.trade_condition, force=1)
    
    self.assertEquals('custom', self.order.getPaymentConditionTradeDate())
    self.assertEquals(DateTime(2001, 01, 01),
                      self.order.getPaymentConditionPaymentDate())

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
  def test_apply_twice_trade_condition_copy_subobjects(self):
    self.trade_condition.setPaymentConditionTradeDate('custom')
    self.trade_condition.setPaymentConditionPaymentDate(DateTime(2001, 01, 01))
    self.order.setSpecialiseValue(self.trade_condition)

    self.order.Order_applyTradeCondition(self.trade_condition, force=1)
    self.assertEquals(1, len(self.order.contentValues(
                                portal_type='Payment Condition')))
    self.assertEquals('custom', self.order.getPaymentConditionTradeDate())
    self.assertEquals(DateTime(2001, 01, 01),
                      self.order.getPaymentConditionPaymentDate())
    self.order.Order_applyTradeCondition(self.trade_condition, force=1)
    self.assertEquals(1, len(self.order.contentValues(
                                portal_type='Payment Condition')))

285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
  def test_apply_trade_condition_copy_subobjects_with_hierarchy(self):
    other_trade_condition = self.trade_condition_module.newContent(
                            portal_type=self.trade_condition.getPortalType(),
                            title='Other Trade Condition')
    other_trade_condition.setPaymentConditionTradeDate('custom')
    other_trade_condition.setPaymentConditionPaymentDate(
                                              DateTime(2001, 01, 01))

    self.trade_condition.setSpecialiseValue(other_trade_condition)
    self.order.setSpecialiseValue(self.trade_condition)

    self.order.Order_applyTradeCondition(self.trade_condition, force=1)
    
    self.assertEquals('custom', self.order.getPaymentConditionTradeDate())
    self.assertEquals(DateTime(2001, 01, 01),
                      self.order.getPaymentConditionPaymentDate())

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
  def test_apply_trade_condition_twice_update_order(self):
    self.trade_condition.setSourceSectionValue(self.vendor)
    self.trade_condition.setDestinationSectionValue(self.client)
    self.trade_condition.setSourceValue(self.vendor)
    self.trade_condition.setDestinationValue(self.client)
    self.trade_condition.setPriceCurrencyValue(self.currency)
    self.trade_condition.setPaymentConditionTradeDate('custom')
    self.trade_condition.setPaymentConditionPaymentDate(DateTime(2001, 01, 01))
    self.order.setSpecialiseValue(self.trade_condition)

    self.order.Order_applyTradeCondition(self.trade_condition, force=1)
    
    self.assertEquals(self.vendor, self.order.getSourceSectionValue())
    self.assertEquals(self.vendor, self.order.getSourceValue())
    self.assertEquals(self.client, self.order.getDestinationSectionValue())
    self.assertEquals(self.client, self.order.getDestinationValue())
    self.assertEquals(self.currency, self.order.getPriceCurrencyValue())
    self.assertEquals('custom', self.order.getPaymentConditionTradeDate())
    self.assertEquals(DateTime(2001, 01, 01),
                      self.order.getPaymentConditionPaymentDate())

    new_vendor = self.portal.organisation_module.newContent(
                    portal_type='Organisation',
                    title='New vendor')
    new_trade_condition = self.trade_condition_module.newContent(
                    portal_type=self.trade_condition_type,
                    source_section_value=new_vendor,
                    payment_condition_trade_date='custom',
                    payment_condition_payment_date=DateTime(2002, 2, 2))

    self.order.Order_applyTradeCondition(new_trade_condition, force=1)
    self.assertEquals(new_vendor, self.order.getSourceSectionValue())
    self.assertEquals(self.vendor, self.order.getSourceValue())
    self.assertEquals(self.client, self.order.getDestinationSectionValue())
    self.assertEquals(self.client, self.order.getDestinationValue())
    self.assertEquals(self.currency, self.order.getPriceCurrencyValue())
    self.assertEquals('custom', self.order.getPaymentConditionTradeDate())
    self.assertEquals(DateTime(2002, 02, 02),
                      self.order.getPaymentConditionPaymentDate())


343 344 345 346 347 348 349 350 351 352 353 354 355
  def test_tax_model_line_consistency(self):
    base_1 = self.base_amount.newContent(
                          portal_type='Category',
                          title='Base 1')
    tax_model_line = self.trade_condition.newContent(
                  portal_type='Tax Model Line',
                  base_application_value=base_1,
                  float_index=1,
                  efficiency=0.2,
                  resource_value=self.tax)
    self.assertEquals([], tax_model_line.checkConsistency())
    self.assertEquals([], self.trade_condition.checkConsistency())
  
356 357 358 359 360 361 362 363 364 365 366 367 368
  def test_discount_model_line_consistency(self):
    base_1 = self.base_amount.newContent(
                          portal_type='Category',
                          title='Base 1')
    discount_model_line = self.trade_condition.newContent(
                  portal_type='Discount Model Line',
                  base_application_value=base_1,
                  float_index=1,
                  efficiency=0.2,
                  resource_value=self.discount)
    self.assertEquals([], discount_model_line.checkConsistency())
    self.assertEquals([], self.trade_condition.checkConsistency())

369 370 371 372 373 374 375 376 377 378 379 380 381 382
  def test_view_tax_model_line(self):
    base_1 = self.base_amount.newContent(
                          portal_type='Category',
                          title='Base 1')
    tax_model_line = self.trade_condition.newContent(
                  portal_type='Tax Model Line',
                  base_application_value=base_1,
                  float_index=1,
                  efficiency=0.2,
                  resource_value=self.tax)
    # TODO: fail if a field has an error
    tax_model_line.view()
    self.trade_condition.TradeCondition_viewTax()

383 384 385 386 387 388 389 390 391 392 393 394 395 396
  def test_view_discount_model_line(self):
    base_1 = self.base_amount.newContent(
                          portal_type='Category',
                          title='Base 1')
    discount_model_line = self.trade_condition.newContent(
                  portal_type='Discount Model Line',
                  base_application_value=base_1,
                  float_index=1,
                  efficiency=0.2,
                  resource_value=self.discount)
    # TODO: fail if a field has an error
    discount_model_line.view()
    self.trade_condition.TradeCondition_viewDiscount()

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
  def test_tax_line_consistency(self):
    base_1 = self.base_amount.newContent(
                          portal_type='Category',
                          title='Base 1')
    tax_line = self.order.newContent(
                        portal_type='Tax Line',
                        resource_value=self.tax,
                        base_application_value=base_1,
                        quantity=0,
                        efficiency=5.5)
    self.assertEquals([], tax_line.checkConsistency())

  def test_view_tax_line(self):
    base_1 = self.base_amount.newContent(
                          portal_type='Category',
                          title='Base 1')
    tax_line = self.order.newContent(
                        portal_type='Tax Line',
                        resource_value=self.tax,
                        base_application_value=base_1,
                        quantity=0,
                        efficiency=5.5)
    # TODO: fail if a field has an error
    tax_line.view()
    self.order.Delivery_viewTax()

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
  def test_discount_line_consistency(self):
    base_1 = self.base_amount.newContent(
                          portal_type='Category',
                          title='Base 1')
    discount_line = self.order.newContent(
                        portal_type='Discount Line',
                        resource_value=self.discount,
                        base_application_value=base_1,
                        quantity=0,
                        efficiency=5.5)
    self.assertEquals([], discount_line.checkConsistency())

  def test_view_discount_line(self):
    base_1 = self.base_amount.newContent(
                          portal_type='Category',
                          title='Base 1')
    discount_line = self.order.newContent(
                        portal_type='Discount Line',
                        resource_value=self.discount,
                        base_application_value=base_1,
                        quantity=0,
                        efficiency=5.5)
    # TODO: fail if a field has an error
    discount_line.view()
    self.order.Delivery_viewDiscount()

449

450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
class TestTradeConditionSupplyLine(TradeConditionTestCase):
  """A trade condition can contain supply line and those supply lines are used
  in priority, for example to calculate the price
  """
  def test_category_acquisition(self):
    self.trade_condition.setSourceValue(self.vendor)
    self.trade_condition.setSourceSectionValue(self.vendor)
    self.trade_condition.setDestinationValue(self.client)
    self.trade_condition.setDestinationSectionValue(self.client)
    self.trade_condition.setPriceCurrencyValue(self.currency)

    supply_line = self.trade_condition.newContent(
                                    portal_type=self.supply_line_type)

    self.assertEquals(self.vendor, supply_line.getSourceValue())
    self.assertEquals(self.vendor, supply_line.getSourceSectionValue())
    self.assertEquals(self.client, supply_line.getDestinationValue())
    self.assertEquals(self.client, supply_line.getDestinationSectionValue())
468
    self.assertEquals(self.currency, supply_line.getPriceCurrencyValue())
469 470 471 472 473 474 475 476 477 478

  def test_movement_price_assignment(self):
    # supply line from the trade condition apply to the movements in order
    # where this trade condition is used
    supply_line = self.trade_condition.newContent(
                                    portal_type=self.supply_line_type,
                                    resource_value=self.resource,
                                    base_price=123)

    self.order.setSpecialiseValue(self.trade_condition)
479
    transaction.commit()
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
    self.tic()

    line = self.order.newContent(portal_type=self.order_line_type,
                                 resource_value=self.resource,
                                 quantity=1)
    self.assertEquals(123, line.getPrice())

  def test_supply_line_priority(self):
    # supply lines from related trade condition should have priority over
    # supply lines from supply modules
    other_supply = self.portal.getDefaultModule(self.supply_type
                             ).newContent(portal_type=self.supply_type,
                                          resource_value=self.resource,
                                          source_section_value=self.vendor,
                                          destination_section_value=self.client)
    other_supply_line = other_supply.newContent(
                                    portal_type=self.supply_line_type,
                                    base_price=1)
    supply_line = self.trade_condition.newContent(
                                    portal_type=self.supply_line_type,
                                    resource_value=self.resource,
                                    base_price=2)
    self.order.setSpecialiseValue(self.trade_condition)
    self.order.setSourceSectionValue(self.vendor)
    self.order.setDestinationSectionValue(self.vendor)
505
    transaction.commit()
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
    self.tic()

    line = self.order.newContent(portal_type=self.order_line_type,
                                 resource_value=self.resource,
                                 quantity=1)
    # using the supply line inside trade condition
    self.assertEquals(2, line.getPrice())

  # TODO: move to testSupplyLine ! (which does not exist yet)
  def test_supply_line_section(self):
    # if a supply lines defines a section, it has priority over supply lines
    # not defining sections
    other_entity = self.portal.organisation_module.newContent(
                                      portal_type='Organisation',
                                      title='Other')
    supply = self.portal.getDefaultModule(self.supply_type
                             ).newContent(portal_type=self.supply_type,
                                          resource_value=self.resource,)
    supply_line = supply.newContent(
                                    portal_type=self.supply_line_type,
                                    base_price=1)

    other_supply = self.portal.getDefaultModule(self.supply_type
                             ).newContent(portal_type=self.supply_type,
                                          resource_value=self.resource,
                                          destination_section_value=self.client,
                                          source_section_value=self.vendor)
    other_supply_line = other_supply.newContent(
                                    portal_type=self.supply_line_type,
                                    base_price=2)

    self.order.setSourceSectionValue(self.vendor)
    self.order.setDestinationSectionValue(self.client)
539
    transaction.commit()
540 541 542 543 544 545 546
    self.tic()

    line = self.order.newContent(portal_type=self.order_line_type,
                                 resource_value=self.resource,
                                 quantity=1)
    # using the supply line with section defined
    self.assertEquals(2, line.getPrice())
547 548


549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
class TestEffectiveTradeCondition(TradeConditionTestCase):
  """Tests for getEffectiveModel
  
  XXX open questions:
   - should getEffectiveModel take validation state into account ? if yes, how
     to do it in generic/customizable way ?
   - would getEffectiveModel(at_date) be enough ?
  """
  def test_getEffectiveModel(self):
    # getEffectiveModel returns the model with highest version
    reference = self.id()
    self.trade_condition.setReference(reference)
    self.trade_condition.setVersion('001')
    self.trade_condition.setEffectiveDate('2009/01/01')
    self.trade_condition.setExpirationDate('2009/12/31')
    other_trade_condition = self.trade_condition_module.newContent(
                            portal_type=self.trade_condition.getPortalType(),
                            title='Other Trade Condition',
                            reference=reference,
                            effective_date='2009/01/01',
                            expiration_date='2009/12/31',
                            version='002')
    transaction.commit()
    self.tic()
    
    self.assertEquals(other_trade_condition,
        self.trade_condition.getEffectiveModel(
                    start_date=DateTime('2009/06/01'),
                    stop_date=DateTime('2009/06/01')))

    # outside date range, nothing
    self.assertEquals(None,
        self.trade_condition.getEffectiveModel(
                    start_date=DateTime('2008/06/01'),
                    stop_date=DateTime('2008/06/01')))
    self.assertEquals(None,
        self.trade_condition.getEffectiveModel(
                    start_date=DateTime('2010/06/01'),
                    stop_date=DateTime('2010/06/01')))

  def test_getEffectiveModel_return_self(self):
    # getEffectiveModel returns the trade condition if it's effective
    self.trade_condition.setReference(self.id())
    self.trade_condition.setEffectiveDate('2009/01/01')
    self.trade_condition.setExpirationDate('2009/12/31')
    transaction.commit()
    self.tic()
    self.assertEquals(self.trade_condition,
        self.trade_condition.getEffectiveModel(
                    start_date=DateTime('2009/06/01'),
                    stop_date=DateTime('2009/06/01')))

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
  def test_getEffectiveModel_without_dates(self):
    # a trade condition without effective / expiration date is effective
    self.trade_condition.setReference(self.id())
    self.trade_condition.setEffectiveDate(None)
    self.trade_condition.setExpirationDate(None)
    transaction.commit()
    self.tic()
    self.assertEquals(self.trade_condition,
        self.trade_condition.getEffectiveModel(
                    start_date=DateTime('2009/06/01'),
                    stop_date=DateTime('2009/06/01')))

    self.trade_condition.setEffectiveDate(None)
    self.trade_condition.setExpirationDate('2009/12/31')
    transaction.commit()
    self.tic()
    self.assertEquals(self.trade_condition,
        self.trade_condition.getEffectiveModel(
                    start_date=DateTime('2009/06/01'),
                    stop_date=DateTime('2009/06/01')))

    self.trade_condition.setEffectiveDate('2009/01/01')
    self.trade_condition.setExpirationDate(None)
    transaction.commit()
    self.tic()
    self.assertEquals(self.trade_condition,
        self.trade_condition.getEffectiveModel(
                    start_date=DateTime('2009/06/01'),
                    stop_date=DateTime('2009/06/01')))

    
632 633 634 635 636 637 638 639 640 641 642
  def test_getEffectiveModel_return_self_when_no_reference(self):
    # when no reference defined, getEffectiveModel returns the trade condition.
    self.trade_condition.setReference(None)
    self.assertEquals(self.trade_condition,
        self.trade_condition.getEffectiveModel())
    self.assertEquals(self.trade_condition,
        self.trade_condition.getEffectiveModel(start_date=DateTime(),
                                               stop_date=DateTime()))



643 644 645 646 647
class TestWithSaleOrder:
  order_type = 'Sale Order'
  order_line_type = 'Sale Order Line'
  order_cell_type = 'Sale Order Cell'
  trade_condition_type = 'Sale Trade Condition'
648 649
  supply_type = 'Sale Supply'
  supply_line_type = 'Sale Supply Line'
650 651 652 653 654 655

class TestWithPurchaseOrder:
  order_type = 'Purchase Order'
  order_line_type = 'Purchase Order Line'
  order_cell_type = 'Purchase Order Cell'
  trade_condition_type = 'Purchase Trade Condition'
656 657
  supply_type = 'Purchase Supply'
  supply_line_type = 'Purchase Supply Line'
658 659 660 661 662 663

class TestWithSaleInvoice:
  order_type = 'Sale Invoice Transaction'
  order_line_type = 'Invoice Line'
  order_cell_type = 'Invoice Cell'
  trade_condition_type = 'Sale Trade Condition'
664 665
  supply_type = 'Sale Supply'
  supply_line_type = 'Sale Supply Line'
666 667 668 669 670 671

class TestWithPurchaseInvoice:
  order_type = 'Purchase Invoice Transaction'
  order_line_type = 'Invoice Line'
  order_cell_type = 'Invoice Cell'
  trade_condition_type = 'Purchase Trade Condition'
672 673
  supply_type = 'Purchase Supply'
  supply_line_type = 'Purchase Supply Line'
674 675 676 677 678 679 680 681 682


class TestApplyTradeConditionSaleOrder(
      TestApplyTradeCondition, TestWithSaleOrder):
  pass
class TestApplyTradeConditionPurchaseOrder(
      TestApplyTradeCondition, TestWithPurchaseOrder):
  pass

683 684 685 686 687 688 689 690 691 692 693 694 695
class TestTradeConditionSupplyLineSaleOrder(
      TestTradeConditionSupplyLine, TestWithSaleOrder):
  pass
class TestTradeConditionSupplyLinePurchaseOrder(
      TestTradeConditionSupplyLine, TestWithPurchaseOrder):
  pass
class TestTradeConditionSupplyLineSaleInvoice(
      TestTradeConditionSupplyLine, TestWithSaleInvoice):
  pass
class TestTradeConditionSupplyLinePurchaseInvoice(
      TestTradeConditionSupplyLine, TestWithPurchaseInvoice):
  pass

696 697 698 699 700 701 702 703
class TestEffectiveSaleTradeCondition(
            TestEffectiveTradeCondition,
            TestWithSaleOrder):
  pass
class TestEffectivePurchaseTradeCondition(
            TestEffectiveTradeCondition,
            TestWithPurchaseOrder):
  pass
704

705 706 707 708
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestApplyTradeConditionSaleOrder))
  suite.addTest(unittest.makeSuite(TestApplyTradeConditionPurchaseOrder))
709 710 711 712
  suite.addTest(unittest.makeSuite(TestTradeConditionSupplyLineSaleOrder))
  suite.addTest(unittest.makeSuite(TestTradeConditionSupplyLinePurchaseOrder))
  suite.addTest(unittest.makeSuite(TestTradeConditionSupplyLineSaleInvoice))
  suite.addTest(unittest.makeSuite(TestTradeConditionSupplyLinePurchaseInvoice))
713 714
  suite.addTest(unittest.makeSuite(TestEffectiveSaleTradeCondition))
  suite.addTest(unittest.makeSuite(TestEffectivePurchaseTradeCondition))
715 716
  return suite