testTradeCondition.py 86.5 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 41
    return ('erp5_base', 'erp5_pdm', 'erp5_trade', 'erp5_accounting',
            'erp5_invoicing', 'erp5_simplified_invoicing')
42

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

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


class AccountingBuildTestCase(TradeConditionTestCase):
  """Same as TradeConditionTestCase, but with a rule to generate
  accounting.
  """
103 104
  def afterSetUp(self):
    TradeConditionTestCase.afterSetUp(self)
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
    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(
                        portal_type='Invoice Transaction Rule',
                        reference='default_invoice_transaction_rule',
                        id='test_invoice_transaction_rule',
                        title='Transaction Rule',
                        test_method_id='SimulationMovement_testInvoiceTransactionRule',
                        version=100)
    predicate = itr.newContent(portal_type='Predicate',)
    predicate.edit(
            string_index='resource_type',
            title='Resource Product',
            int_index=1,
146
            test_method_id='SimulationMovement_isDeliveryMovement' )
147 148 149 150 151
    predicate = itr.newContent(portal_type='Predicate')
    predicate.edit(
            string_index='resource_type',
            title='Resource Tax',
            int_index=2,
152
            test_method_id='SimulationMovement_isTaxMovement' )
153
    transaction.commit()
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
    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()
184
    transaction.commit()
185 186
    self.tic()

187 188 189
  def beforeTearDown(self):
    TradeConditionTestCase.beforeTearDown(self)
    self.portal.portal_rules.manage_delObjects('test_invoice_transaction_rule')
190
    transaction.commit()
191
    self.tic()
192 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

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

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
  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')))

284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
  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())

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


342 343 344 345 346 347 348 349 350 351 352 353 354
  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())
  
355 356 357 358 359 360 361 362 363 364 365 366 367
  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())

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

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

396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
  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()

422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
  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()

448 449 450 451

class TestTaxLineCalculation(TradeConditionTestCase):
  """Test calculating Tax Lines.
  """
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
  def test_apply_trade_condition_twice_and_tax_lines(self):
    base_1 = self.base_amount.newContent(
                          portal_type='Category',
                          title='Base 1')
    self.resource.setBaseContributionValue(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.order.Order_applyTradeCondition(self.trade_condition, force=1)

    # this creates a tax line, with quantity 0, and it will be updated when
    # needed
    tax_line_list = self.order.contentValues(portal_type='Tax Line')
    self.assertEquals(1, len(tax_line_list))
    tax_line = tax_line_list[0]
    self.assertEquals(0, tax_line.getQuantity())
    self.assertEquals(self.tax, tax_line.getResourceValue())
    self.assertEquals(0.2, tax_line.getPrice())
    
    # if we apply twice, we don't have the tax lines twice
    self.order.Order_applyTradeCondition(self.trade_condition, force=1)
    tax_line_list = self.order.contentValues(portal_type='Tax Line')
    self.assertEquals(1, len(tax_line_list))

480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
  def test_apply_trade_condition_after_line_creation(self):
    base_1 = self.base_amount.newContent(
                          portal_type='Category',
                          title='Base 1')
    self.resource.setBaseContributionValue(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)
    
    order_line = self.order.newContent(
                          portal_type=self.order_line_type,
                          resource_value=self.resource,
                          quantity=10,
                          price=10,)

    self.order.Order_applyTradeCondition(self.trade_condition, force=1)

500
    transaction.commit()
501 502 503 504 505 506
    tax_line_list = self.order.contentValues(portal_type='Tax Line')
    self.assertEquals(1, len(tax_line_list))
    tax_line = tax_line_list[0]
    self.assertEquals(100, tax_line.getQuantity())
    self.assertEquals(self.tax, tax_line.getResourceValue())
    self.assertEquals(0.2, tax_line.getPrice())
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
  def test_simple_tax_model_line_calculation(self):
    base_1 = self.base_amount.newContent(
                          portal_type='Category',
                          title='Base 1')
    self.resource.setBaseContributionValue(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.order.Order_applyTradeCondition(self.trade_condition, force=1)

    # this creates a tax line, with quantity 0, and it will be updated when
    # needed
    tax_line_list = self.order.contentValues(portal_type='Tax Line')
    self.assertEquals(1, len(tax_line_list))
    tax_line = tax_line_list[0]
    self.assertEquals(0, tax_line.getQuantity())
    self.assertEquals(self.tax, tax_line.getResourceValue())
    self.assertEquals(0.2, tax_line.getPrice())

    order_line = self.order.newContent(
                          portal_type=self.order_line_type,
                          resource_value=self.resource,
                          quantity=10,
                          price=10,)

537 538
    transaction.commit()
    # at the end of transaction, tax lines are updated
539 540 541 542 543 544 545 546 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
    tax_line_list = self.order.contentValues(portal_type='Tax Line')
    self.assertEquals(1, len(tax_line_list))
    tax_line = tax_line_list[0]
    self.assertEquals(100, tax_line.getQuantity())
    self.assertEquals(0.2, tax_line.getPrice())
    self.assertEquals(20, tax_line.getTotalPrice())
    
  def test_tax_model_line_calculation_with_two_lines(self):
    base_1 = self.base_amount.newContent(
                          portal_type='Category',
                          title='Base 1')
    self.resource.setBaseContributionValue(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.order.Order_applyTradeCondition(self.trade_condition, force=1)

    # this creates a tax line, with quantity 0, and it will be updated when
    # needed
    tax_line_list = self.order.contentValues(portal_type='Tax Line')
    self.assertEquals(1, len(tax_line_list))
    tax_line = tax_line_list[0]
    self.assertEquals(0, tax_line.getQuantity())
    self.assertEquals(self.tax, tax_line.getResourceValue())
    self.assertEquals(0.2, tax_line.getPrice())

    order_line_1 = self.order.newContent(
                          portal_type=self.order_line_type,
                          resource_value=self.resource,
                          quantity=3,
                          price=10,)
    order_line_2 = self.order.newContent(
                          portal_type=self.order_line_type,
                          resource_value=self.resource,
                          quantity=7,
                          price=10,)
    
580 581
    transaction.commit()
    # at the end of transaction, tax lines are updated
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
    tax_line_list = self.order.contentValues(portal_type='Tax Line')
    self.assertEquals(1, len(tax_line_list))
    tax_line = tax_line_list[0]
    self.assertEquals(100, tax_line.getQuantity())
    self.assertEquals(0.2, tax_line.getPrice())
    self.assertEquals(20, tax_line.getTotalPrice())
    
    order_line_1_tax_line_list = \
      order_line_1.DeliveryMovement_getCorrespondingTaxLineList()
    self.assertEquals(1, len(order_line_1_tax_line_list))
    tax_line = order_line_1_tax_line_list[0]
    self.assertEquals(30, tax_line.getQuantity())
    self.assertEquals(0.2, tax_line.getPrice())
    self.assertEquals(6, tax_line.getTotalPrice())

    order_line_2_tax_line_list = \
      order_line_2.DeliveryMovement_getCorrespondingTaxLineList()
    self.assertEquals(1, len(order_line_2_tax_line_list))
    tax_line = order_line_2_tax_line_list[0]
    self.assertEquals(70, tax_line.getQuantity())
    self.assertEquals(0.2, tax_line.getPrice())
    self.assertEquals(14, tax_line.getTotalPrice())

  def test_tax_on_tax(self):
    base_1 = self.base_amount.newContent(
                          portal_type='Category',
                          title='Base 1')
    base_2 = self.base_amount.newContent(
                          portal_type='Category',
                          title='Base 2')
    tax2 = self.portal.tax_module.newContent(
                          portal_type='Tax',
                          title='Tax 2')
    self.resource.setBaseContributionValue(base_1)
    tax_model_line = self.trade_condition.newContent(
                  portal_type='Tax Model Line',
                  base_application_value=base_1,
                  base_contribution_value=base_2,
                  float_index=1,
                  efficiency=0.2,
                  resource_value=self.tax)
    tax_model_line = self.trade_condition.newContent(
                  portal_type='Tax Model Line',
                  base_application_value=base_2,
                  float_index=2,
                  efficiency=0.5,
                  resource_value=tax2)
    
    self.order.Order_applyTradeCondition(self.trade_condition, force=1)

632
    transaction.commit()
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
    tax_line_list = self.order.contentValues(portal_type='Tax Line')
    self.assertEquals(2, len(tax_line_list))
    tax_line1 = [tl for tl in tax_line_list if
                   tl.getResourceValue() == self.tax][0]
    self.assertEquals(0, tax_line1.getQuantity())
    self.assertEquals(0.2, tax_line1.getPrice())
    self.assertEquals(1, tax_line1.getFloatIndex())
    self.assertEquals([base_1], tax_line1.getBaseApplicationValueList())
    self.assertEquals([base_2], tax_line1.getBaseContributionValueList())

    tax_line2 = [tl for tl in tax_line_list if
                   tl.getResourceValue() == tax2][0]
    self.assertEquals(0, tax_line2.getQuantity())
    self.assertEquals(0.5, tax_line2.getPrice())
    self.assertEquals(2, tax_line2.getFloatIndex())
    self.assertEquals([base_2], tax_line2.getBaseApplicationValueList())

    order_line = self.order.newContent(
                          portal_type=self.order_line_type,
                          resource_value=self.resource,
                          quantity=3,
                          price=10,)
655
    transaction.commit()
656 657 658 659
    self.assertEquals(30, tax_line1.getQuantity())
    self.assertEquals((30*0.2), tax_line2.getQuantity())
    
    order_line.setQuantity(5)
660
    transaction.commit()
661 662 663
    self.assertEquals(50, tax_line1.getQuantity())
    self.assertEquals((50*0.2), tax_line2.getQuantity())
    
664 665 666 667 668 669 670
    tax_movement_list = order_line.DeliveryMovement_getCorrespondingTaxLineList()
    self.assertEquals(2, len(tax_movement_list))
    tax_1_movement = [m for m in tax_movement_list if m.getPrice() == 0.2][0]
    self.assertEquals(tax_1_movement.getQuantity(), 50)
    tax_2_movement = [m for m in tax_movement_list if m.getPrice() == 0.5][0]
    self.assertEquals(tax_2_movement.getQuantity(), 50*0.2)

671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700

  def test_update_order_line_quantity_update_tax_line(self):
    base_1 = self.base_amount.newContent(
                          portal_type='Category',
                          title='Base 1')
    self.resource.setBaseContributionValue(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.order.Order_applyTradeCondition(self.trade_condition, force=1)

    # this creates a tax line, with quantity 0, and it will be updated when
    # needed
    tax_line_list = self.order.contentValues(portal_type='Tax Line')
    self.assertEquals(1, len(tax_line_list))
    tax_line = tax_line_list[0]
    self.assertEquals(0, tax_line.getQuantity())
    self.assertEquals(self.tax, tax_line.getResourceValue())
    self.assertEquals(0.2, tax_line.getPrice())

    order_line = self.order.newContent(
                          portal_type=self.order_line_type,
                          resource_value=self.resource,
                          quantity=10,
                          price=10,)

701
    transaction.commit()
702 703 704 705 706 707 708 709 710 711
    # tax lines are updated
    tax_line_list = self.order.contentValues(portal_type='Tax Line')
    self.assertEquals(1, len(tax_line_list))
    tax_line = tax_line_list[0]
    self.assertEquals(100, tax_line.getQuantity())
    self.assertEquals(0.2, tax_line.getPrice())
    self.assertEquals(20, tax_line.getTotalPrice())
    
    # change the quantity on order_line,
    order_line.setQuantity(20)
712 713 714
    transaction.commit()
    # the tax line is updated (by an interraction workflow at the end of
    # transaction)
715 716 717
    self.assertEquals(200, tax_line.getQuantity())
    self.assertEquals(40, tax_line.getTotalPrice())

718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
  def test_delete_order_line_quantity_update_tax_line(self):
    base_1 = self.base_amount.newContent(
                          portal_type='Category',
                          title='Base 1')
    self.resource.setBaseContributionValue(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.order.Order_applyTradeCondition(self.trade_condition, force=1)

    # this creates a tax line, with quantity 0, and it will be updated when
    # needed
    tax_line_list = self.order.contentValues(portal_type='Tax Line')
    self.assertEquals(1, len(tax_line_list))
    tax_line = tax_line_list[0]
    self.assertEquals(0, tax_line.getQuantity())
    self.assertEquals(self.tax, tax_line.getResourceValue())
    self.assertEquals(0.2, tax_line.getPrice())

    order_line = self.order.newContent(
                          portal_type=self.order_line_type,
                          resource_value=self.resource,
                          quantity=10,
                          price=10,)

747
    transaction.commit()
748 749 750 751 752 753 754 755 756
    # tax lines are updated
    tax_line_list = self.order.contentValues(portal_type='Tax Line')
    self.assertEquals(1, len(tax_line_list))
    tax_line = tax_line_list[0]
    self.assertEquals(100, tax_line.getQuantity())
    self.assertEquals(0.2, tax_line.getPrice())
    self.assertEquals(20, tax_line.getTotalPrice())
    
    # delete the order line
Kazuhiko Shiozaki's avatar
Kazuhiko Shiozaki committed
757
    self.order.manage_delObjects([order_line.getId()])
758 759 760 761
    # the tax line is updated
    self.assertEquals(0, tax_line.getQuantity())
    self.assertEquals(0, tax_line.getTotalPrice())

762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
  def test_clone_order_line_quantity_update_tax_line(self):
    base_1 = self.base_amount.newContent(
                          portal_type='Category',
                          title='Base 1')
    self.resource.setBaseContributionValue(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.order.Order_applyTradeCondition(self.trade_condition, force=1)

    # this creates a tax line, with quantity 0, and it will be updated when
    # needed
    tax_line_list = self.order.contentValues(portal_type='Tax Line')
    self.assertEquals(1, len(tax_line_list))
    tax_line = tax_line_list[0]
    self.assertEquals(0, tax_line.getQuantity())
    self.assertEquals(self.tax, tax_line.getResourceValue())
    self.assertEquals(0.2, tax_line.getPrice())

    order_line = self.order.newContent(
                          portal_type=self.order_line_type,
                          resource_value=self.resource,
                          quantity=10,
                          price=10,)

    transaction.commit()
    # tax lines are updated
    tax_line_list = self.order.contentValues(portal_type='Tax Line')
    self.assertEquals(1, len(tax_line_list))
    tax_line = tax_line_list[0]
    self.assertEquals(100, tax_line.getQuantity())
    self.assertEquals(0.2, tax_line.getPrice())
    self.assertEquals(20, tax_line.getTotalPrice())
    
    # clone the order line
    cloned_order_line = order_line.Base_createCloneDocument(batch_mode=1)
    # the tax line is updated
    self.assertEquals(200, tax_line.getQuantity())
    self.assertEquals(40, tax_line.getTotalPrice())

806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
  def test_order_cell_and_tax_line(self):
    base_1 = self.base_amount.newContent(
                          portal_type='Category',
                          title='Base 1')
    self.resource.setBaseContributionValue(base_1)
    # make a resource with size variation
    self.resource.setVariationBaseCategoryList(('size',))
    self.resource.setVariationCategoryList(('size/big', 'size/small'))

    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.order.Order_applyTradeCondition(self.trade_condition, force=1)

    # this creates a tax line, with quantity 0, and it will be updated when
    # needed
    tax_line_list = self.order.contentValues(portal_type='Tax Line')
    self.assertEquals(1, len(tax_line_list))
    tax_line = tax_line_list[0]
    self.assertEquals(0, tax_line.getQuantity())
    self.assertEquals(self.tax, tax_line.getResourceValue())
    self.assertEquals(0.2, tax_line.getPrice())

    order_line = self.order.newContent(
                          portal_type=self.order_line_type,
                          resource_value=self.resource,)
    order_line.setVariationCategoryList(('size/big', 'size/small'))
    order_line.updateCellRange(base_id='movement')
    cell_red = order_line.newCell('size/big',
                                  portal_type=self.order_cell_type,
                                  base_id='movement')
    cell_red.setMappedValuePropertyList(['quantity', 'price'])
    cell_red.setPrice(5)
    cell_red.setQuantity(10)
    cell_blue = order_line.newCell('size/small',
                             portal_type=self.order_cell_type,
                             base_id='movement')
    cell_blue.setMappedValuePropertyList(['quantity', 'price'])
    cell_blue.setPrice(2)
    cell_blue.setQuantity(25)
    self.assertEquals(100, order_line.getTotalPrice(fast=0))
    
852
    transaction.commit()
853 854 855 856 857 858 859
    tax_line_list = self.order.contentValues(portal_type='Tax Line')
    self.assertEquals(1, len(tax_line_list))
    tax_line = tax_line_list[0]
    self.assertEquals(100, tax_line.getQuantity())
    self.assertEquals(self.tax, tax_line.getResourceValue())
    self.assertEquals(0.2, tax_line.getPrice())
    
860 861
    self.assertEquals(100, self.order.getTotalPrice(fast=0))
    self.assertEquals(120, self.order.getTotalNetPrice(fast=0))
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899


  def test_hierarchical_order_line_and_tax_line(self):
    base_1 = self.base_amount.newContent(
                          portal_type='Category',
                          title='Base 1')
    self.resource.setBaseContributionValue(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.order.Order_applyTradeCondition(self.trade_condition, force=1)

    # this creates a tax line, with quantity 0, and it will be updated when
    # needed
    tax_line_list = self.order.contentValues(portal_type='Tax Line')
    self.assertEquals(1, len(tax_line_list))
    tax_line = tax_line_list[0]
    self.assertEquals(0, tax_line.getQuantity())
    self.assertEquals(self.tax, tax_line.getResourceValue())
    self.assertEquals(0.2, tax_line.getPrice())

    order_line = self.order.newContent(
                          portal_type=self.order_line_type,
                          resource_value=self.resource,)
    suborder_line1 = order_line.newContent(
                          portal_type=self.order_line_type,
                          quantity=4,
                          price=5)
    suborder_line2 = order_line.newContent(
                          portal_type=self.order_line_type,
                          quantity=2,
                          price=40)

900
    transaction.commit()
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
    tax_line_list = self.order.contentValues(portal_type='Tax Line')
    self.assertEquals(1, len(tax_line_list))
    tax_line = tax_line_list[0]
    self.assertEquals(100, tax_line.getQuantity())
    self.assertEquals(self.tax, tax_line.getResourceValue())
    self.assertEquals(0.2, tax_line.getPrice())
  
  def test_base_contribution_pseudo_acquisition(self):
    base_1 = self.base_amount.newContent(portal_type='Category',
                                         title='Base 1')
    self.resource.setBaseContributionValueList((base_1,))
    line = self.order.newContent(portal_type=self.order_line_type)
    self.assertEquals([], line.getBaseContributionValueList())
    line.setResourceValue(self.resource)
    self.assertEquals([base_1], line.getBaseContributionValueList())
    line.setBaseContributionValueList([])
    self.assertEquals([], line.getBaseContributionValueList())

  def test_multiple_order_line_multiple_tax_line(self):
    base_1 = self.base_amount.newContent(
                          portal_type='Category',
                          title='Base 1')
    base_2 = self.base_amount.newContent(
                          portal_type='Category',
                          title='Base 2')
    self.resource.setBaseContributionValueList((base_1, base_2))
    tax_model_line_1 = self.trade_condition.newContent(
                  portal_type='Tax Model Line',
                  base_application_value=base_1,
                  float_index=1,
                  efficiency=0.1,
                  resource_value=self.tax)
    tax_model_line_2 = self.trade_condition.newContent(
                  portal_type='Tax Model Line',
                  base_application_value=base_2,
                  float_index=2,
                  efficiency=0.2,
                  resource_value=self.tax)
    tax_model_line_1_2 = self.trade_condition.newContent(
                  portal_type='Tax Model Line',
                  base_application_value_list=(base_1, base_2),
                  float_index=3,
                  efficiency=0.3,
                  resource_value=self.tax)
    
    self.order.Order_applyTradeCondition(self.trade_condition, force=1)
    line_1 = self.order.newContent(
                  portal_type=self.order_line_type,
                  quantity=1, price=1,
                  resource_value=self.resource,
                  base_contribution_value_list=(base_1,))
    # -> tax_model_line_1 and tax_model_line_1_2 are applicable
    line_2 = self.order.newContent(
                  portal_type=self.order_line_type,
                  quantity=2, price=2,
                  resource_value=self.resource,
                  base_contribution_value_list=(base_2,))
    # -> tax_model_line_2 and tax_model_line_1_2 are applicable
    line_3 = self.order.newContent(
                  portal_type=self.order_line_type,
                  quantity=3, price=3,
                  resource_value=self.resource,
                  base_contribution_value_list=(base_1, base_2))
    # -> tax_model_line_1, tax_model_line_2 and tax_model_line_1_2 are applicable
    #  (but they are not applied twice)

967
    transaction.commit()
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
    tax_line_list = self.order.contentValues(portal_type='Tax Line')
    self.assertEquals(3, len(tax_line_list))
    tax_line_1 = [x for x in tax_line_list if x.getPrice() == 0.1][0]
    tax_line_2 = [x for x in tax_line_list if x.getPrice() == 0.2][0]
    tax_line_3 = [x for x in tax_line_list if x.getPrice() == 0.3][0]

    self.assertEquals(sum([line_1.getTotalPrice(),
                           line_3.getTotalPrice()]), tax_line_1.getQuantity())
    self.assertEquals(sum([line_2.getTotalPrice(),
                           line_3.getTotalPrice()]), tax_line_2.getQuantity())
    self.assertEquals(sum([line_1.getTotalPrice(),
                           line_2.getTotalPrice(),
                           line_3.getTotalPrice()]), tax_line_3.getQuantity())

    tax_movement_list = line_1.DeliveryMovement_getCorrespondingTaxLineList()
    self.assertEquals(2, len(tax_movement_list))
    tax_1_movement = [m for m in tax_movement_list if m.getPrice() == 0.1][0]
985 986 987 988 989 990 991 992 993 994 995
    self.assertEquals(tax_1_movement.getQuantity(), 1)
    tax_3_movement = [m for m in tax_movement_list if m.getPrice() == 0.3][0]
    self.assertEquals(tax_3_movement.getQuantity(), 1)
    
    tax_movement_list = line_2.DeliveryMovement_getCorrespondingTaxLineList()
    self.assertEquals(2, len(tax_movement_list))
    tax_2_movement = [m for m in tax_movement_list if m.getPrice() == 0.2][0]
    self.assertEquals(tax_2_movement.getQuantity(), 4)
    tax_3_movement = [m for m in tax_movement_list if m.getPrice() == 0.3][0]
    self.assertEquals(tax_3_movement.getQuantity(), 4)
    
996
    tax_movement_list = line_3.DeliveryMovement_getCorrespondingTaxLineList()
997 998 999 1000 1001 1002 1003
    self.assertEquals(3, len(tax_movement_list))
    tax_1_movement = [m for m in tax_movement_list if m.getPrice() == 0.1][0]
    self.assertEquals(tax_1_movement.getQuantity(), 9)
    tax_2_movement = [m for m in tax_movement_list if m.getPrice() == 0.2][0]
    self.assertEquals(tax_2_movement.getQuantity(), 9)
    tax_3_movement = [m for m in tax_movement_list if m.getPrice() == 0.3][0]
    self.assertEquals(tax_3_movement.getQuantity(), 9)
1004
    
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
  def test_temp_order(self):
    base_1 = self.base_amount.newContent(
                          portal_type='Category',
                          title='Base 1')
    self.resource.setBaseContributionValue(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)

    order = self.portal.getDefaultModule(self.order_type).newContent(
                          portal_type=self.order_type,
                          temp_object=1)
    order.Order_applyTradeCondition(self.trade_condition, force=1)

    tax_line_list = order.contentValues(portal_type='Tax Line')
    self.assertEquals(1, len(tax_line_list))
    tax_line = tax_line_list[0]
    self.assertEquals(0, tax_line.getQuantity())
    self.assertEquals(self.tax, tax_line.getResourceValue())
    self.assertEquals(0.2, tax_line.getPrice())

    order_line = order.newContent(
                          portal_type=self.order_line_type,
                          resource_value=self.resource,
                          quantity=10,
                          price=40)
1035
    transaction.commit()
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080

    tax_line_list = order.contentValues(portal_type='Tax Line')
    self.assertEquals(1, len(tax_line_list))
    tax_line = tax_line_list[0]
    self.assertEquals(400, tax_line.getQuantity())
    self.assertEquals(self.tax, tax_line.getResourceValue())
    self.assertEquals(0.2, tax_line.getPrice())
  
  def test_temp_order_hierarchical(self):
    base_1 = self.base_amount.newContent(
                          portal_type='Category',
                          title='Base 1')
    self.resource.setBaseContributionValue(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)

    order = self.portal.getDefaultModule(self.order_type).newContent(
                          portal_type=self.order_type,
                          temp_object=1)
    order.Order_applyTradeCondition(self.trade_condition, force=1)

    tax_line_list = order.contentValues(portal_type='Tax Line')
    self.assertEquals(1, len(tax_line_list))
    tax_line = tax_line_list[0]
    self.assertEquals(0, tax_line.getQuantity())
    self.assertEquals(self.tax, tax_line.getResourceValue())
    self.assertEquals(0.2, tax_line.getPrice())

    order_line = order.newContent(
                          portal_type=self.order_line_type,
                          resource_value=self.resource,)
    suborder_line1 = order_line.newContent(
                          portal_type=self.order_line_type,
                          quantity=4,
                          price=5)
    suborder_line2 = order_line.newContent(
                          portal_type=self.order_line_type,
                          quantity=2,
                          price=40)

1081
    transaction.commit()
1082 1083 1084 1085 1086 1087 1088
    tax_line_list = order.contentValues(portal_type='Tax Line')
    self.assertEquals(1, len(tax_line_list))
    tax_line = tax_line_list[0]
    self.assertEquals(100, tax_line.getQuantity())
    self.assertEquals(self.tax, tax_line.getResourceValue())
    self.assertEquals(0.2, tax_line.getPrice())
  
1089

1090
class TestTaxLineOrderSimulation(AccountingBuildTestCase):
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
  """Test Simulation of Tax Lines on Orders
  """
  def test_tax_line_simulation(self):
    base_1 = self.base_amount.newContent(
                          portal_type='Category',
                          title='Base 1')
    self.resource.setBaseContributionValue(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)
    
    order = self.order
    order.Order_applyTradeCondition(self.trade_condition, force=1)
    order.setSourceSectionValue(self.vendor)
    order.setSourceValue(self.vendor)
    order.setDestinationSectionValue(self.client)
    order.setDestinationValue(self.client)
    order.setStartDate(DateTime(2001, 1, 1))
    order_line = order.newContent(
                          portal_type=self.order_line_type,
                          resource_value=self.resource,
                          quantity=10,
                          price=10,)
    order.plan()
    order.confirm()
    self.assertEquals('confirmed', order.getSimulationState())
1120
    transaction.commit()
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
    self.tic()
    related_applied_rule_list = order.getCausalityRelatedValueList(
                                      portal_type='Applied Rule')
    self.assertEquals(1, len(related_applied_rule_list))
    root_applied_rule = related_applied_rule_list[0]
    simulation_movement_list = root_applied_rule.contentValues(
                                   portal_type='Simulation Movement')
    self.assertEquals(1, len(simulation_movement_list))
    level2_applied_rule_list = simulation_movement_list[0].contentValues()
    self.assertEquals(2, len(level2_applied_rule_list))
    # first test the invoice movement, they should have base_contribution set
    # correctly
    invoice_rule_list = [ar for ar in level2_applied_rule_list if
             ar.getSpecialiseValue().getPortalType() == 'Invoicing Rule']
    self.assertEquals(1, len(invoice_rule_list))
    invoice_simulation_movement_list = invoice_rule_list[0].contentValues()
    self.assertEquals(1, len(invoice_simulation_movement_list))
    invoice_simulation_movement = invoice_simulation_movement_list[0]
    self.assertEquals(self.resource,
        invoice_simulation_movement.getResourceValue())
    self.assertEquals([base_1],
        invoice_simulation_movement.getBaseContributionValueList())

    # now test the tax movement
    applied_tax_rule_list = [ar for ar in level2_applied_rule_list if
             ar.getSpecialiseValue().getPortalType() == 'Tax Rule']
    self.assertEquals(1, len(applied_tax_rule_list))
    tax_simulation_movement_list = applied_tax_rule_list[0].contentValues()
    self.assertEquals(1, len(tax_simulation_movement_list))
    tax_simulation_movement = tax_simulation_movement_list[0]

    self.assertEquals(self.tax, tax_simulation_movement.getResourceValue())
    self.assertEquals([base_1],
                      tax_simulation_movement.getBaseApplicationValueList())
    self.assertEquals(100, tax_simulation_movement.getQuantity())
    self.assertEquals(0.2, tax_simulation_movement.getPrice())
    
    # reexpand and check nothing changed
    root_applied_rule.expand()
    applied_tax_rule_list = [ar for ar in level2_applied_rule_list if
             ar.getSpecialiseValue().getPortalType() == 'Tax Rule']
    self.assertEquals(1, len(applied_tax_rule_list))
    tax_simulation_movement_list = applied_tax_rule_list[0].contentValues()
    self.assertEquals(1, len(tax_simulation_movement_list))
    tax_simulation_movement = tax_simulation_movement_list[0]

    self.assertEquals(self.tax, tax_simulation_movement.getResourceValue())
    self.assertEquals([base_1],
                      tax_simulation_movement.getBaseApplicationValueList())
    self.assertEquals(100, tax_simulation_movement.getQuantity())
    self.assertEquals(0.2, tax_simulation_movement.getPrice())

  def test_2_tax_lines_simulation(self):
    base_1 = self.base_amount.newContent(
                          portal_type='Category',
                          title='Base 1')
    self.resource.setBaseContributionValue(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)
    
    order = self.order
    order.Order_applyTradeCondition(self.trade_condition, force=1)
    order.setSourceSectionValue(self.vendor)
    order.setSourceValue(self.vendor)
    order.setDestinationSectionValue(self.client)
    order.setDestinationValue(self.client)
    order.setStartDate(DateTime(2001, 1, 1))
    order_line1 = order.newContent(
                          portal_type=self.order_line_type,
                          resource_value=self.resource,
                          quantity=2,
                          price=15,)
    order_line2 = order.newContent(
                          portal_type=self.order_line_type,
                          resource_value=self.resource,
                          quantity=7,
                          price=10,)
    order.plan()
    order.confirm()
    self.assertEquals('confirmed', order.getSimulationState())
1205
    transaction.commit()
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
    self.tic()
    related_applied_rule_list = order.getCausalityRelatedValueList(
                                      portal_type='Applied Rule')
    self.assertEquals(1, len(related_applied_rule_list))
    root_applied_rule = related_applied_rule_list[0]
    simulation_movement_list = root_applied_rule.contentValues(
                                   portal_type='Simulation Movement')
    self.assertEquals(2, len(simulation_movement_list))
    # line 1
    line1_simulation_movement_list = [sm for sm in simulation_movement_list
          if sm.getOrderValue() == order_line1]
    self.assertEquals(1, len(line1_simulation_movement_list))
    simulation_movement = line1_simulation_movement_list[0]
    self.assertEquals(2.0, simulation_movement.getQuantity())
    applied_tax_rule_list = [ar for ar in simulation_movement.objectValues()
        if ar.getSpecialiseValue().getPortalType() == 'Tax Rule']
    self.assertEquals(1, len(applied_tax_rule_list))
    tax_simulation_movement_list = applied_tax_rule_list[0].contentValues()
    self.assertEquals(1, len(tax_simulation_movement_list))
    tax_simulation_movement = tax_simulation_movement_list[0]
    self.assertEquals(self.tax, tax_simulation_movement.getResourceValue())
    self.assertEquals([base_1],
                      tax_simulation_movement.getBaseApplicationValueList())
    self.assertEquals(30, tax_simulation_movement.getQuantity())
    self.assertEquals(0.2, tax_simulation_movement.getPrice())
    
    # line 2
    line2_simulation_movement_list = [sm for sm in simulation_movement_list
          if sm.getOrderValue() == order_line2]
    self.assertEquals(1, len(line2_simulation_movement_list))
    simulation_movement = line2_simulation_movement_list[0]
    self.assertEquals(7., simulation_movement.getQuantity())
    applied_tax_rule_list = [ar for ar in simulation_movement.objectValues()
        if ar.getSpecialiseValue().getPortalType() == 'Tax Rule']
    self.assertEquals(1, len(applied_tax_rule_list))
    tax_simulation_movement_list = applied_tax_rule_list[0].contentValues()
    self.assertEquals(1, len(tax_simulation_movement_list))
    tax_simulation_movement = tax_simulation_movement_list[0]
    self.assertEquals(self.tax, tax_simulation_movement.getResourceValue())
    self.assertEquals([base_1],
                      tax_simulation_movement.getBaseApplicationValueList())
    self.assertEquals(70, tax_simulation_movement.getQuantity())
    self.assertEquals(0.2, tax_simulation_movement.getPrice())


  def test_tax_line_build(self):
    base_1 = self.base_amount.newContent(
                          portal_type='Category',
                          title='Base 1')
    self.resource.setBaseContributionValue(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)
    
    order = self.order
    order.Order_applyTradeCondition(self.trade_condition, force=1)
    order.setSourceSectionValue(self.vendor)
    order.setSourceValue(self.vendor)
    order.setDestinationSectionValue(self.client)
    order.setDestinationValue(self.client)
    order.setPriceCurrencyValue(self.currency)
    order.setStartDate(DateTime(2001, 1, 1))
    order_line = order.newContent(
                          portal_type=self.order_line_type,
                          resource_value=self.resource,
                          quantity=2,
                          price=15,)
    order.plan()
    order.confirm()
    self.assertEquals('confirmed', order.getSimulationState())
1279
    transaction.commit()
1280 1281 1282 1283 1284 1285 1286 1287 1288
    self.tic()
    related_delivery = order.getCausalityRelatedValue(
                  portal_type=('Purchase Packing List', 'Sale Packing List'))
    self.assertNotEquals(related_delivery, None)
    related_delivery.setReady()
    related_delivery.start()
    related_delivery.stop()
    related_delivery.deliver()
    self.assertEquals('delivered', related_delivery.getSimulationState())
1289
    transaction.commit()
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
    self.tic()
    
    related_invoice = related_delivery.getCausalityRelatedValue(
                  portal_type=('Purchase Invoice Transaction',
                               'Sale Invoice Transaction'))
    self.assertNotEquals(related_invoice, None)
    invoice_line_list = related_invoice.contentValues(
                  portal_type='Invoice Line')
    tax_line_list = related_invoice.contentValues(
                  portal_type='Tax Line')

    self.assertEquals(1, len(invoice_line_list))
    invoice_line = invoice_line_list[0]
    self.assertEquals(2, invoice_line.getQuantity())
    self.assertEquals(15, invoice_line.getPrice())
    self.assertEquals(self.resource, invoice_line.getResourceValue())
    self.assertEquals([base_1], invoice_line.getBaseContributionValueList())

    self.assertEquals(1, len(tax_line_list))
    tax_line = tax_line_list[0]
    self.assertEquals(30, tax_line.getQuantity())
    self.assertEquals(0.2, tax_line.getPrice())
    self.assertEquals(self.tax, tax_line.getResourceValue())
    self.assertEquals([base_1], tax_line.getBaseApplicationValueList())
    self.assertEquals([], tax_line.getBaseContributionValueList())

    self.assertEquals('solved', related_invoice.getCausalityState())

1318 1319 1320 1321 1322
    # Of course, this invoice does not generate simulation again. An applied
    # rule is created, but it is empty.
    related_applied_rule = related_invoice.getCausalityRelatedValue(
                                portal_type='Applied Rule')
    self.assertEquals(0, len(related_applied_rule.objectValues()))
1323
    
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
  def test_tax_line_build_accounting(self):
    base_1 = self.base_amount.newContent(
                          portal_type='Category',
                          title='Base 1')
    self.resource.setBaseContributionValue(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)
    
    order = self.order
    order.Order_applyTradeCondition(self.trade_condition, force=1)
    order.setSourceSectionValue(self.vendor)
    order.setSourceValue(self.vendor)
    order.setDestinationSectionValue(self.client)
    order.setDestinationValue(self.client)
    order.setPriceCurrencyValue(self.currency)
    order.setStartDate(DateTime(2001, 1, 1))
    order_line = order.newContent(
                          portal_type=self.order_line_type,
                          resource_value=self.resource,
                          quantity=2,
                          price=15,)
    order.plan()
    order.confirm()
    self.assertEquals('confirmed', order.getSimulationState())
1352
    transaction.commit()
1353 1354 1355 1356 1357 1358 1359 1360 1361
    self.tic()
    related_delivery = order.getCausalityRelatedValue(
                  portal_type=('Purchase Packing List', 'Sale Packing List'))
    self.assertNotEquals(related_delivery, None)
    related_delivery.setReady()
    related_delivery.start()
    related_delivery.stop()
    related_delivery.deliver()
    self.assertEquals('delivered', related_delivery.getSimulationState())
1362
    transaction.commit()
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
    self.tic()
    
    related_invoice = related_delivery.getCausalityRelatedValue(
                  portal_type=('Purchase Invoice Transaction',
                               'Sale Invoice Transaction'))
    self.assertNotEquals(related_invoice, None)
    self.assertEquals('confirmed', related_invoice.getSimulationState())
    self.assertEquals('solved', related_invoice.getCausalityState())
    accounting_line_list = related_invoice.getMovementList(
                    portal_type=self.portal.getPortalAccountingMovementTypeList())
    self.assertEquals(0, len(accounting_line_list))

    related_invoice.start()
1376
    transaction.commit()
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
    self.tic()
    self.assertEquals('started', related_invoice.getSimulationState())
    self.assertEquals('solved', related_invoice.getCausalityState())

    accounting_line_list = related_invoice.getMovementList(
                    portal_type=self.portal.getPortalAccountingMovementTypeList())
    self.assertEquals(3, len(accounting_line_list))
    receivable_line = [l for l in accounting_line_list if
                        l.getSourceValue() == self.receivable_account][0]
    self.assertEquals(self.payable_account,
                      receivable_line.getDestinationValue())
    self.assertEquals(36, receivable_line.getSourceDebit())
    
    tax_line = [l for l in accounting_line_list if
                        l.getSourceValue() == self.collected_tax_account][0]
    self.assertEquals(self.refundable_tax_account,
                      tax_line.getDestinationValue())
    self.assertEquals(6, tax_line.getSourceCredit())

    income_line = [l for l in accounting_line_list if
                        l.getSourceValue() == self.income_account][0]
    self.assertEquals(self.expense_account,
                      income_line.getDestinationValue())
    self.assertEquals(30, income_line.getSourceCredit())

1402 1403 1404 1405 1406 1407
    # Of course, this invoice does not generate simulation again. An applied
    # rule is created, but it is empty.
    related_applied_rule = related_invoice.getCausalityRelatedValue(
                                portal_type='Applied Rule')
    self.assertEquals(0, len(related_applied_rule.objectValues()))

1408 1409 1410
    # and there's no other invoices
    self.assertEquals(1, len(self.portal.accounting_module.contentValues()))

1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447

  def test_tax_line_merged_build(self):
    # an order with 2 lines and 1 tax line will later be built in an invoice
    # with 2 lines and 1 tax line
    base_1 = self.base_amount.newContent(
                          portal_type='Category',
                          title='Base 1')
    self.resource.setBaseContributionValue(base_1)
    resource2 = self.portal.product_module.newContent(
                            portal_type='Product',
                            title='Resource 2',
                            base_contribution_value_list=[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)
    
    order = self.order
    order.Order_applyTradeCondition(self.trade_condition, force=1)
    order.setSourceSectionValue(self.vendor)
    order.setSourceValue(self.vendor)
    order.setDestinationSectionValue(self.client)
    order.setDestinationValue(self.client)
    order.setPriceCurrencyValue(self.currency)
    order.setStartDate(DateTime(2001, 1, 1))
    order_line1 = order.newContent(
                          portal_type=self.order_line_type,
                          resource_value=self.resource,
                          quantity=2,
                          price=15,)
    order_line2 = order.newContent(
                          portal_type=self.order_line_type,
                          resource_value=resource2,
                          quantity=7,
                          price=10,)
1448
    transaction.commit()
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
    # check existing tax line
    tax_line_list = order.contentValues(portal_type='Tax Line')
    self.assertEquals(1, len(tax_line_list))
    tax_line = tax_line_list[0]
    self.assertEquals(self.tax, tax_line.getResourceValue())
    self.assertEquals(2*15 + 7*10, tax_line.getQuantity())
    self.assertEquals(0.2, tax_line.getPrice())

    order.plan()
    order.confirm()
    self.assertEquals('confirmed', order.getSimulationState())
1460
    transaction.commit()
1461 1462 1463 1464 1465 1466 1467 1468 1469
    self.tic()
    related_delivery = order.getCausalityRelatedValue(
                  portal_type=('Purchase Packing List', 'Sale Packing List'))
    self.assertNotEquals(related_delivery, None)
    related_delivery.setReady()
    related_delivery.start()
    related_delivery.stop()
    related_delivery.deliver()
    self.assertEquals('delivered', related_delivery.getSimulationState())
1470
    transaction.commit()
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
    self.tic()
    
    related_invoice = related_delivery.getCausalityRelatedValue(
                  portal_type=('Purchase Invoice Transaction',
                               'Sale Invoice Transaction'))
    self.assertNotEquals(related_invoice, None)
    invoice_line_list = related_invoice.contentValues(
                  portal_type='Invoice Line')
    tax_line_list = related_invoice.contentValues(
                  portal_type='Tax Line')

    self.assertEquals(2, len(invoice_line_list))

    self.assertEquals(1, len(tax_line_list))
    tax_line = tax_line_list[0]
    self.assertEquals(100, tax_line.getQuantity())
    self.assertEquals(0.2, tax_line.getPrice())
    self.assertEquals(self.tax, tax_line.getResourceValue())
    self.assertEquals([base_1], tax_line.getBaseApplicationValueList())
    self.assertEquals([], tax_line.getBaseContributionValueList())

    self.assertEquals('solved', related_invoice.getCausalityState())

  def test_tax_line_updated_on_invoice_line_change(self):
    base_1 = self.base_amount.newContent(
                          portal_type='Category',
                          title='Base 1')
    self.resource.setBaseContributionValue(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)
    
    order = self.order
    order.Order_applyTradeCondition(self.trade_condition, force=1)
    order.setSourceSectionValue(self.vendor)
    order.setSourceValue(self.vendor)
    order.setDestinationSectionValue(self.client)
    order.setDestinationValue(self.client)
    order.setPriceCurrencyValue(self.currency)
    order.setStartDate(DateTime(2001, 1, 1))
    order_line = order.newContent(
                          portal_type=self.order_line_type,
                          resource_value=self.resource,
                          quantity=2,
                          price=15,)
    order.plan()
    order.confirm()
    self.assertEquals('confirmed', order.getSimulationState())
1522
    transaction.commit()
1523 1524 1525 1526 1527 1528 1529 1530 1531
    self.tic()
    related_delivery = order.getCausalityRelatedValue(
                  portal_type=('Purchase Packing List', 'Sale Packing List'))
    self.assertNotEquals(related_delivery, None)
    related_delivery.setReady()
    related_delivery.start()
    related_delivery.stop()
    related_delivery.deliver()
    self.assertEquals('delivered', related_delivery.getSimulationState())
1532
    transaction.commit()
1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556
    self.tic()
    
    related_invoice = related_delivery.getCausalityRelatedValue(
                  portal_type=('Purchase Invoice Transaction',
                               'Sale Invoice Transaction'))
    self.assertNotEquals(related_invoice, None)
    self.assertEquals('solved', related_invoice.getCausalityState())
    invoice_line_list = related_invoice.contentValues(
                  portal_type='Invoice Line')
    tax_line_list = related_invoice.contentValues(
                  portal_type='Tax Line')

    self.assertEquals(1, len(tax_line_list))
    tax_line = tax_line_list[0]
    self.assertEquals(30, tax_line.getQuantity())
    self.assertEquals(0.2, tax_line.getPrice())
    self.assertEquals(self.tax, tax_line.getResourceValue())
    self.assertEquals([base_1], tax_line.getBaseApplicationValueList())
    self.assertEquals([], tax_line.getBaseContributionValueList())

    self.assertEquals(1, len(invoice_line_list))
    invoice_line = invoice_line_list[0]
    # change a total price on the invoice_line,
    invoice_line.setQuantity(3)
1557
    transaction.commit()
1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599
    self.tic()
    # it will be reflected on the tax line
    self.assertEquals(45, tax_line.getQuantity())
    self.assertTrue(tax_line.isDivergent())
    # and the invoice is diverged
    self.assertEquals('diverged', related_invoice.getCausalityState())
    

class TestTaxLineInvoiceSimulation(AccountingBuildTestCase):
  """Test Simulation of Tax Lines on Invoices
  """
  def test_tax_line_simulation(self):
    base_1 = self.base_amount.newContent(
                          portal_type='Category',
                          title='Base 1')
    self.resource.setBaseContributionValue(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)
    
    invoice = self.order
    invoice.Order_applyTradeCondition(self.trade_condition, force=1)
    invoice.setSourceSectionValue(self.vendor)
    invoice.setSourceValue(self.vendor)
    invoice.setDestinationSectionValue(self.client)
    invoice.setDestinationValue(self.client)
    invoice.setStartDate(DateTime(2001, 1, 1))
    invoice.setPriceCurrencyValue(self.currency)
    invoice_line = invoice.newContent(
                          portal_type=self.order_line_type,
                          resource_value=self.resource,
                          quantity=10,
                          price=10,)
    tax_line_list = invoice.contentValues(portal_type='Tax Line')
    self.assertEquals(1, len(tax_line_list))
    tax_line = tax_line_list[0]

    invoice.plan()
    invoice.confirm()
1600 1601
    invoice.start()
    self.assertEquals('started', invoice.getSimulationState())
1602
    transaction.commit()
1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633
    self.tic()
    related_applied_rule_list = invoice.getCausalityRelatedValueList(
                                      portal_type='Applied Rule')
    self.assertEquals(1, len(related_applied_rule_list))
    root_applied_rule = related_applied_rule_list[0]
    simulation_movement_list = root_applied_rule.contentValues(
                                   portal_type='Simulation Movement')
    self.assertEquals(2, len(simulation_movement_list))
    tax_simulation_movement_list = [m for m in simulation_movement_list
                                    if m.getOrderValue() == tax_line]
    self.assertEquals(1, len(tax_simulation_movement_list))
    tax_simulation_movement = tax_simulation_movement_list[0]
    self.assertEquals([base_1],
        tax_simulation_movement.getBaseApplicationValueList())
    self.assertEquals(100, tax_simulation_movement.getQuantity())
    self.assertEquals(0.2, tax_simulation_movement.getPrice())
    self.assertEquals(self.currency,
                      tax_simulation_movement.getPriceCurrencyValue())

    invoice_simulation_movement_list = [m for m in simulation_movement_list
                                    if m.getOrderValue() == invoice_line]
    self.assertEquals(1, len(invoice_simulation_movement_list))
    invoice_simulation_movement = invoice_simulation_movement_list[0]
    self.assertEquals([base_1],
        invoice_simulation_movement.getBaseContributionValueList())
    self.assertEquals(10, invoice_simulation_movement.getQuantity())
    self.assertEquals(10, invoice_simulation_movement.getPrice())
    self.assertEquals(self.currency,
                      invoice_simulation_movement.getPriceCurrencyValue())
    self.assertEquals(self.resource,
                      invoice_simulation_movement.getResourceValue())
1634

1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650
    accounting_line_list = invoice.getMovementList(
                            portal_type=('Sale Invoice Transaction Line',
                                         'Purchase Invoice Transaction Line'))
    self.assertEquals(3, len(accounting_line_list))
    receivable_line = [l for l in accounting_line_list if
                        l.getSourceValue() == self.receivable_account][0]
    self.assertEquals(self.payable_account,
                      receivable_line.getDestinationValue())
    self.assertEquals(120, receivable_line.getSourceDebit())
    
    tax_line = [l for l in accounting_line_list if
                        l.getSourceValue() == self.collected_tax_account][0]
    self.assertEquals(self.refundable_tax_account,
                      tax_line.getDestinationValue())
    self.assertEquals(20, tax_line.getSourceCredit())

1651 1652 1653 1654 1655 1656
    income_line = [l for l in accounting_line_list if
                        l.getSourceValue() == self.income_account][0]
    self.assertEquals(self.expense_account,
                      income_line.getDestinationValue())
    self.assertEquals(100, income_line.getSourceCredit())

1657 1658 1659 1660
    self.assertEquals('solved', invoice.getCausalityState())


class DiscountCalculation:
1661
  """Test Discount Calculations
1662 1663
  """
  def test_simple_discount_model_line_calculation(self):
1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
    base_1 = self.base_amount.newContent(
                          portal_type='Category',
                          title='Base 1')
    self.resource.setBaseContributionValue(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)
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693
  

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())
1694
    self.assertEquals(self.currency, supply_line.getPriceCurrencyValue())
1695 1696 1697 1698 1699 1700 1701 1702 1703 1704

  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)
1705
    transaction.commit()
1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730
    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)
1731
    transaction.commit()
1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
    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)
1765
    transaction.commit()
1766 1767 1768 1769 1770 1771 1772
    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())
1773 1774


1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826
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')))

1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857
  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')))

    
1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868
  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()))



1869 1870 1871 1872 1873
class TestWithSaleOrder:
  order_type = 'Sale Order'
  order_line_type = 'Sale Order Line'
  order_cell_type = 'Sale Order Cell'
  trade_condition_type = 'Sale Trade Condition'
1874 1875
  supply_type = 'Sale Supply'
  supply_line_type = 'Sale Supply Line'
1876 1877 1878 1879 1880 1881

class TestWithPurchaseOrder:
  order_type = 'Purchase Order'
  order_line_type = 'Purchase Order Line'
  order_cell_type = 'Purchase Order Cell'
  trade_condition_type = 'Purchase Trade Condition'
1882 1883
  supply_type = 'Purchase Supply'
  supply_line_type = 'Purchase Supply Line'
1884 1885 1886 1887 1888 1889

class TestWithSaleInvoice:
  order_type = 'Sale Invoice Transaction'
  order_line_type = 'Invoice Line'
  order_cell_type = 'Invoice Cell'
  trade_condition_type = 'Sale Trade Condition'
1890 1891
  supply_type = 'Sale Supply'
  supply_line_type = 'Sale Supply Line'
1892 1893 1894 1895 1896 1897

class TestWithPurchaseInvoice:
  order_type = 'Purchase Invoice Transaction'
  order_line_type = 'Invoice Line'
  order_cell_type = 'Invoice Cell'
  trade_condition_type = 'Purchase Trade Condition'
1898 1899
  supply_type = 'Purchase Supply'
  supply_line_type = 'Purchase Supply Line'
1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919


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

class TestTaxLineCalculationSaleOrder(
    TestTaxLineCalculation, TestWithSaleOrder):
  pass
class TestTaxLineCalculationPurchaseOrder(
    TestTaxLineCalculation, TestWithPurchaseOrder):
  pass
class TestTaxLineCalculationSaleInvoice(
    TestTaxLineCalculation, TestWithSaleInvoice):
  def not_available(self):
    pass
  test_hierarchical_order_line_and_tax_line = not_available
1920
  test_temp_order_hierarchical = not_available
1921 1922 1923 1924 1925
class TestTaxLineCalculationPurchaseInvoice(
    TestTaxLineCalculation, TestWithPurchaseInvoice):
  def not_available(self):
    pass
  test_hierarchical_order_line_and_tax_line = not_available
1926
  test_temp_order_hierarchical = not_available
1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941

class TestTaxLineOrderSimulationSaleOrder(
      TestTaxLineOrderSimulation, TestWithSaleOrder):
  pass
class TestTaxLineOrderSimulationPurchaseOrder(
      TestTaxLineOrderSimulation, TestWithPurchaseOrder):
  pass

class TestTaxLineInvoiceSimulationPurchaseInvoice(
      TestTaxLineInvoiceSimulation, TestWithPurchaseInvoice):
  pass
class TestTaxLineInvoiceSimulationSaleInvoice(
      TestTaxLineInvoiceSimulation, TestWithSaleInvoice):
  pass

1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954
class TestTradeConditionSupplyLineSaleOrder(
      TestTradeConditionSupplyLine, TestWithSaleOrder):
  pass
class TestTradeConditionSupplyLinePurchaseOrder(
      TestTradeConditionSupplyLine, TestWithPurchaseOrder):
  pass
class TestTradeConditionSupplyLineSaleInvoice(
      TestTradeConditionSupplyLine, TestWithSaleInvoice):
  pass
class TestTradeConditionSupplyLinePurchaseInvoice(
      TestTradeConditionSupplyLine, TestWithPurchaseInvoice):
  pass

1955 1956 1957 1958 1959 1960 1961 1962
class TestEffectiveSaleTradeCondition(
            TestEffectiveTradeCondition,
            TestWithSaleOrder):
  pass
class TestEffectivePurchaseTradeCondition(
            TestEffectiveTradeCondition,
            TestWithPurchaseOrder):
  pass
1963

1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestApplyTradeConditionSaleOrder))
  suite.addTest(unittest.makeSuite(TestApplyTradeConditionPurchaseOrder))
  suite.addTest(unittest.makeSuite(TestTaxLineCalculationSaleOrder))
  suite.addTest(unittest.makeSuite(TestTaxLineCalculationPurchaseOrder))
  suite.addTest(unittest.makeSuite(TestTaxLineCalculationSaleInvoice))
  suite.addTest(unittest.makeSuite(TestTaxLineCalculationPurchaseInvoice))
  suite.addTest(unittest.makeSuite(TestTaxLineOrderSimulationSaleOrder))
  suite.addTest(unittest.makeSuite(TestTaxLineOrderSimulationPurchaseOrder))
  suite.addTest(unittest.makeSuite(TestTaxLineInvoiceSimulationPurchaseInvoice))
  suite.addTest(unittest.makeSuite(TestTaxLineInvoiceSimulationSaleInvoice))
1976 1977 1978 1979
  suite.addTest(unittest.makeSuite(TestTradeConditionSupplyLineSaleOrder))
  suite.addTest(unittest.makeSuite(TestTradeConditionSupplyLinePurchaseOrder))
  suite.addTest(unittest.makeSuite(TestTradeConditionSupplyLineSaleInvoice))
  suite.addTest(unittest.makeSuite(TestTradeConditionSupplyLinePurchaseInvoice))
1980 1981
  suite.addTest(unittest.makeSuite(TestEffectiveSaleTradeCondition))
  suite.addTest(unittest.makeSuite(TestEffectivePurchaseTradeCondition))
1982 1983
  return suite