testTradeCondition.py 37.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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
from DateTime import DateTime

from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
35 36
from Products.ZSQLCatalog.SQLCatalog import Catalog
from Products.ERP5Type.tests.utils import SubcontentReindexingWrapper
37

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

46 47
  size_category_list = ['small', 'big']
  def afterSetUp(self):
48
    self.validateRules()
49 50 51
    for category_id in self.size_category_list:
      self.portal.portal_categories.size.newContent(id=category_id,
                                                    title=category_id)
52 53 54 55
    self.base_amount = self.portal.portal_categories.base_amount
    self.tax = self.portal.tax_module.newContent(
                                    portal_type='Tax',
                                    title='Tax')
56 57 58
    self.discount = self.portal.discount_module.newContent(
                                    portal_type='Discount',
                                    title='Discount')
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    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')

83
  def beforeTearDown(self):
84
    transaction.abort()
85 86 87 88 89 90 91 92 93
    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,
94 95 96
                   self.portal.portal_categories.base_amount,
                   self.portal.portal_categories.size,
      ):
97
      module.manage_delObjects(list(module.objectIds()))
98
    transaction.commit()
99 100
    self.tic()

101
  def test_subcontent_supply_line_reindexing(self):
102 103 104 105 106
    trade_condition = self.trade_condition_module.newContent(
                            portal_type=self.trade_condition_type)
    supply_line = trade_condition.newContent(portal_type=self.supply_line_type)
    self._testSubContentReindexing(trade_condition, [supply_line])
    return
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 147 148 149 150 151 152 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
    # If trade condition is modified, then supply lines must be reindexed
    another_resource = self.portal.product_module.newContent(portal_type='Product',
                                                             title='Another Resource')

    # Supply lines are made without source_section and destination_section
    supply_line_1 = self.trade_condition.newContent(portal_type=self.supply_line_type,
                                                    resource_value=self.resource)
    supply_line_2 = self.trade_condition.newContent(portal_type=self.supply_line_type,
                                                    resource_value=another_resource)

    transaction.commit()
    self.tic()

    # Set source_section and destination_section to trade condition
    # Then recursive reindex should be called
    self.trade_condition.edit(source_section_value=self.client,
                              destination_section_value=self.vendor)

    transaction.commit()
    self.tic()

    # New indexes have source_section, destination_section
    self.assertEqual(len(self.portal.portal_catalog(portal_type=self.trade_condition_type,
                                                    source_section_relative_url=self.client.getRelativeUrl(),
                                                    destination_section_relative_url=self.vendor.getRelativeUrl())),
                     1)

    self.assertEqual(len(self.portal.portal_catalog(portal_type=self.supply_line_type,
                                                    resource_relative_url=another_resource.getRelativeUrl(),
                                                    source_section_relative_url=self.client.getRelativeUrl(),
                                                    destination_section_relative_url=self.vendor.getRelativeUrl())),
                     1)

    self.assertEqual(len(self.portal.portal_catalog(portal_type=self.supply_line_type,
                                                    resource_relative_url=self.resource.getRelativeUrl(),
                                                    source_section_relative_url=self.client.getRelativeUrl(),
                                                    destination_section_relative_url=self.vendor.getRelativeUrl())),
                     1)

    # Change source_section and destination_section
    company_a = self.portal.organisation_module.newContent(portal_type='Organisation',
                                                           title='Company A')
    company_b = self.portal.organisation_module.newContent(portal_type='Organisation',
                                                           title='Company B')

    transaction.commit()
    self.tic()

    # When edit, recursive reindex should be called
    self.trade_condition.edit(source_section_value=None,
                              destination_section_value=None,
                              source_value=company_a,
                              destination_value=company_b)

    transaction.commit()
    self.tic()

    # No old indexes
    self.assertEqual(len(self.portal.portal_catalog(portal_type=self.trade_condition_type,
                                                    source_section_relative_url=self.client.getRelativeUrl(),
                                                    destination_section_relative_url=self.vendor.getRelativeUrl())),
                     0)
    self.assertEqual(len(self.portal.portal_catalog(portal_type=self.supply_line_type,
                                                    resource_relative_url=self.resource.getRelativeUrl(),
                                                    source_section_relative_url=self.client.getRelativeUrl(),
                                                    destination_section_relative_url=self.vendor.getRelativeUrl())),
                     0)
    self.assertEqual(len(self.portal.portal_catalog(portal_type=self.supply_line_type,
                                                    resource_relative_url=another_resource.getRelativeUrl(),
                                                    source_section_relative_url=self.client.getRelativeUrl(),
                                                    destination_section_relative_url=self.vendor.getRelativeUrl())),
                     0)
    # New indexes
    self.assertEqual(len(self.portal.portal_catalog(portal_type=self.trade_condition_type,
                                                    source_relative_url=company_a.getRelativeUrl(),
                                                    destination_relative_url=company_b.getRelativeUrl())),
                     1)
    self.assertEqual(len(self.portal.portal_catalog(portal_type=self.supply_line_type,
                                                    resource_relative_url=self.resource.getRelativeUrl(),
                                                    source_relative_url=company_a.getRelativeUrl(),
                                                    destination_relative_url=company_b.getRelativeUrl())),
                     1)
    self.assertEqual(len(self.portal.portal_catalog(portal_type=self.supply_line_type,
                                                    resource_relative_url=another_resource.getRelativeUrl(),
                                                    source_relative_url=company_a.getRelativeUrl(),
                                                    destination_relative_url=company_b.getRelativeUrl())),
                     1)

195 196 197 198 199

class AccountingBuildTestCase(TradeConditionTestCase):
  """Same as TradeConditionTestCase, but with a rule to generate
  accounting.
  """
200 201
  def afterSetUp(self):
    TradeConditionTestCase.afterSetUp(self)
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
    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
232
                        portal_type='Invoice Transaction Simulation Rule',
233 234 235
                        reference='default_invoice_transaction_rule',
                        id='test_invoice_transaction_rule',
                        title='Transaction Rule',
Kazuhiko Shiozaki's avatar
Kazuhiko Shiozaki committed
236
                        test_method_id='SimulationMovement_testInvoiceTransactionSimulationRule',
237 238 239 240 241 242
                        version=100)
    predicate = itr.newContent(portal_type='Predicate',)
    predicate.edit(
            string_index='resource_type',
            title='Resource Product',
            int_index=1,
243
            test_method_id='SimulationMovement_isDeliveryMovement' )
244 245 246 247 248
    predicate = itr.newContent(portal_type='Predicate')
    predicate.edit(
            string_index='resource_type',
            title='Resource Tax',
            int_index=2,
249
            test_method_id='SimulationMovement_isTaxMovement' )
250
    transaction.commit()
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
    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()
281
    transaction.commit()
282 283
    self.tic()

284 285 286
  def beforeTearDown(self):
    TradeConditionTestCase.beforeTearDown(self)
    self.portal.portal_rules.manage_delObjects('test_invoice_transaction_rule')
287
    transaction.commit()
288
    self.tic()
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

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())

366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
  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')))

381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
  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())

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
  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())


439 440 441 442 443 444 445 446 447 448 449 450 451
  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())
  
452 453 454 455 456 457 458 459 460 461 462 463 464
  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())

465 466 467 468 469 470 471 472 473 474 475 476 477 478
  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()

479 480 481 482 483 484 485 486 487 488 489 490 491 492
  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()

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
  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()

519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
  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()

545

546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
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())
564
    self.assertEquals(self.currency, supply_line.getPriceCurrencyValue())
565 566 567 568 569 570 571 572 573 574

  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)
575
    transaction.commit()
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
    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)
601
    transaction.commit()
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
    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)
635
    transaction.commit()
636 637 638 639 640 641 642
    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())
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
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')))

675 676 677
    # outside date range: should it raise or return nothing ?
    self.assertRaises(Exception,
        self.trade_condition.getEffectiveModel,
678
                    start_date=DateTime('2008/06/01'),
679 680 681
                    stop_date=DateTime('2008/06/01'))
    self.assertRaises(Exception,
        self.trade_condition.getEffectiveModel,
682
                    start_date=DateTime('2010/06/01'),
683
                    stop_date=DateTime('2010/06/01'))
684 685 686 687 688 689 690 691 692 693 694 695 696

  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')))

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
  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')))

    
728 729 730 731 732 733 734 735 736 737 738
  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()))



739 740 741 742 743
class TestWithSaleOrder:
  order_type = 'Sale Order'
  order_line_type = 'Sale Order Line'
  order_cell_type = 'Sale Order Cell'
  trade_condition_type = 'Sale Trade Condition'
744 745
  supply_type = 'Sale Supply'
  supply_line_type = 'Sale Supply Line'
746 747 748 749 750 751

class TestWithPurchaseOrder:
  order_type = 'Purchase Order'
  order_line_type = 'Purchase Order Line'
  order_cell_type = 'Purchase Order Cell'
  trade_condition_type = 'Purchase Trade Condition'
752 753
  supply_type = 'Purchase Supply'
  supply_line_type = 'Purchase Supply Line'
754 755 756 757 758 759

class TestWithSaleInvoice:
  order_type = 'Sale Invoice Transaction'
  order_line_type = 'Invoice Line'
  order_cell_type = 'Invoice Cell'
  trade_condition_type = 'Sale Trade Condition'
760 761
  supply_type = 'Sale Supply'
  supply_line_type = 'Sale Supply Line'
762 763 764 765 766 767

class TestWithPurchaseInvoice:
  order_type = 'Purchase Invoice Transaction'
  order_line_type = 'Invoice Line'
  order_cell_type = 'Invoice Cell'
  trade_condition_type = 'Purchase Trade Condition'
768 769
  supply_type = 'Purchase Supply'
  supply_line_type = 'Purchase Supply Line'
770 771 772 773 774 775 776 777 778


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

779 780 781 782 783 784 785 786 787 788 789 790 791
class TestTradeConditionSupplyLineSaleOrder(
      TestTradeConditionSupplyLine, TestWithSaleOrder):
  pass
class TestTradeConditionSupplyLinePurchaseOrder(
      TestTradeConditionSupplyLine, TestWithPurchaseOrder):
  pass
class TestTradeConditionSupplyLineSaleInvoice(
      TestTradeConditionSupplyLine, TestWithSaleInvoice):
  pass
class TestTradeConditionSupplyLinePurchaseInvoice(
      TestTradeConditionSupplyLine, TestWithPurchaseInvoice):
  pass

792 793 794 795 796 797 798 799
class TestEffectiveSaleTradeCondition(
            TestEffectiveTradeCondition,
            TestWithSaleOrder):
  pass
class TestEffectivePurchaseTradeCondition(
            TestEffectiveTradeCondition,
            TestWithPurchaseOrder):
  pass
800

801 802 803 804
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestApplyTradeConditionSaleOrder))
  suite.addTest(unittest.makeSuite(TestApplyTradeConditionPurchaseOrder))
805 806 807 808
  suite.addTest(unittest.makeSuite(TestTradeConditionSupplyLineSaleOrder))
  suite.addTest(unittest.makeSuite(TestTradeConditionSupplyLinePurchaseOrder))
  suite.addTest(unittest.makeSuite(TestTradeConditionSupplyLineSaleInvoice))
  suite.addTest(unittest.makeSuite(TestTradeConditionSupplyLinePurchaseInvoice))
809 810
  suite.addTest(unittest.makeSuite(TestEffectiveSaleTradeCondition))
  suite.addTest(unittest.makeSuite(TestEffectivePurchaseTradeCondition))
811 812
  return suite