testInvoice.py 139 KB
Newer Older
1 2
##############################################################################
#
3
# Copyright (c) 2004-2008 Nexedi SA and Contributors. All Rights Reserved.
4
#          Sebastien Robin <seb@nexedi.com>
5
#          Jerome Perrin <jerome@nexedi.com>
6 7
#
# WARNING: This program as such is intended to be used by professional
8
# programmers who take the whole responsibility of assessing all potential
9 10
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
11
# guarantees and support are strongly adviced to contract a Free Software
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
# 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.
#
##############################################################################
29 30
"""
  Tests invoice creation from simulation.
31

32
"""
33

34
import transaction
35
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
36
from Products.ERP5Type.tests.utils import FileUpload, DummyMailHost
37
from Products.ERP5Type.UnrestrictedMethod import UnrestrictedMethod
38
from Products.ERP5OOo.OOoUtils import OOoParser
39
from AccessControl.SecurityManagement import newSecurityManager
40
from DateTime import DateTime
41
from Acquisition import aq_parent
42
from zLOG import LOG
43
from Products.ERP5Type.tests.Sequence import SequenceList
44
from testPackingList import TestPackingListMixin
45
from testAccountingRules import TestAccountingRulesMixin
46

47 48
class TestInvoiceMixin(TestPackingListMixin,
                       TestAccountingRulesMixin,):
49 50
  """Test methods for invoices
  """
51 52 53 54 55
  default_region = "europe/west/france"
  vat_gap = 'fr/pcg/4/44/445/4457/44571'
  vat_rate = 0.196
  sale_gap = 'fr/pcg/7/70/707/7071/70712'
  customer_gap = 'fr/pcg/4/41/411'
56
  bank_gap = 'fr/pcg/5/51/512'
57 58
  mail_delivery_mode = 'by_mail'
  cpt_incoterm = 'cpt'
59 60
  unit_piece_quantity_unit = 'unit/piece'
  mass_quantity_unit = 'mass/kg'
61
  oldMailhost = None
62

63 64 65 66 67 68 69 70
  # (account_id, account_gap, account_type)
  account_definition_list = (
      ('receivable_vat', vat_gap, 'liability/payable/collected_vat',),
      ('sale', sale_gap, 'income'),
      ('customer', customer_gap, 'asset/receivable'),
      ('refundable_vat', vat_gap, 'asset/receivable/refundable_vat'),
      ('purchase', sale_gap, 'expense'),
      ('supplier', customer_gap, 'liability/payable'),
71
      ('bank', bank_gap, 'asset/cash/bank'),
72 73 74 75 76 77 78 79
      )
  # (line_id, source_account_id, destination_account_id, line_quantity)
  transaction_line_definition_list = (
      ('income', 'sale', 'purchase', 1.0),
      ('receivable', 'customer', 'supplier', -1.0 - vat_rate),
      ('collected_vat', 'receivable_vat', 'refundable_vat', vat_rate),
      )

80 81 82
  def getTitle(self):
    return "Invoices"

83 84
  def getBusinessTemplateList(self):
    return ('erp5_base', 'erp5_pdm', 'erp5_trade', 'erp5_accounting',
85
            'erp5_invoicing', 'erp5_simplified_invoicing', 'erp5_apparel',
86
            'erp5_project', 'erp5_administration')
87

88
  @UnrestrictedMethod
89
  def createCategories(self):
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    """Create the categories for our test. """
    for cat_string in self.getNeededCategoryList() :
      base_cat = cat_string.split("/")[0]
      path = self.getPortal().portal_categories[base_cat]
      for cat in cat_string.split("/")[1:] :
        if not cat in path.objectIds() :
          path = path.newContent(
                    portal_type='Category',
                    id=cat,)
        else:
          path = path[cat]
    # check categories have been created
    for cat_string in self.getNeededCategoryList() :
      self.assertNotEquals(None,
                self.getCategoryTool().restrictedTraverse(cat_string),
                cat_string)

  def getNeededCategoryList(self):
    """return a list of categories that should be created."""
    return ('region/%s' % self.default_region,
            'gap/%s' % self.vat_gap,
            'gap/%s' % self.sale_gap,
            'gap/%s' % self.customer_gap,
113
            'gap/%s' % self.bank_gap,
Jérome Perrin's avatar
Jérome Perrin committed
114 115
            'delivery_mode/%s' % self.mail_delivery_mode,
            'incoterm/%s' % self.cpt_incoterm,
116 117
            'quantity_unit/%s' % self.unit_piece_quantity_unit,
            'quantity_unit/%s' % self.mass_quantity_unit,
118 119
        )

120

121 122 123 124
  def afterSetUp(self):
    self.createCategories()
    self.validateRules()
    self.login()
125 126 127 128
    self.oldMailHost = getattr(self.portal, 'MailHost', None)
    if self.oldMailHost is not None:
      self.portal.manage_delObjects(['MailHost'])
      self.portal._setObject('MailHost', DummyMailHost('MailHost'))
129 130
    transaction.commit()
    self.tic()
131

132
  def beforeTearDown(self):
133
    transaction.abort()
134
    self.tic()
135 136 137 138
    # restore the original MailHost
    if self.oldMailHost is not None:
      self.portal.manage_delObjects(['MailHost'])
      self.portal._setObject('MailHost', DummyMailHost('MailHost'))
139 140 141 142 143 144 145
    for folder in (self.portal.accounting_module,
                   self.portal.organisation_module,
                   self.portal.sale_order_module,
                   self.portal.purchase_order_module,
                   self.portal.sale_packing_list_module,
                   self.portal.purchase_packing_list_module,
                   self.portal.portal_simulation,):
146
      
147
      folder.manage_delObjects([x for x in folder.objectIds() if x not in ('organisation_1','organisation_2','ppl_1','ppl_2')])
148
     
149
    transaction.commit()
150
    self.tic()
151

152 153
  def login(self):
    """login, without manager role"""
154
    uf = self.getPortal().acl_users
155
    uf._doAddUser('test_invoice_user', '', ['Assignee', 'Assignor', 'Member',
156
                               'Associate', 'Auditor', 'Author'], [])
157
    user = uf.getUserById('test_invoice_user').__of__(uf)
158
    newSecurityManager(None, user)
159

160 161 162 163
  def stepCreateSaleInvoiceTransactionRule(self, sequence, **kw) :
    """Create the rule for accounting. """
    self.createInvoiceTransactionRule(resource=sequence.get('resource'))

164
  @UnrestrictedMethod
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  def createInvoiceTransactionRule(self, resource=None):
    """Create a sale invoice transaction rule with only one cell for
    product_line/apparel and default_region
    The accounting rule cell will have the provided resource, but this his more
    or less optional (as long as price currency is set correctly on order)
    """
    portal = self.portal
    account_module = portal.account_module
    for account_id, account_gap, account_type \
               in self.account_definition_list:
      if not account_id in account_module.objectIds():
        account = account_module.newContent(id=account_id)
        account.setGap(account_gap)
        account.setAccountType(account_type)
        portal.portal_workflow.doActionFor(account, 'validate_action')

181
    invoice_rule = portal.portal_rules.default_invoice_transaction_simulation_rule
182 183 184
    if invoice_rule.getValidationState() == 'validated':
      invoice_rule.invalidate()
    invoice_rule.deleteContent(list(invoice_rule.objectIds()))
185
    transaction.commit()
186 187 188 189 190 191 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
    self.tic()
    region_predicate = invoice_rule.newContent(portal_type = 'Predicate')
    product_line_predicate = invoice_rule.newContent(portal_type = 'Predicate')
    region_predicate.edit(
      membership_criterion_base_category_list = ['destination_region'],
      membership_criterion_category_list =
                   ['destination_region/region/%s' % self.default_region ],
      int_index = 1,
      string_index = 'region'
    )
    product_line_predicate.edit(
      membership_criterion_base_category_list = ['product_line'],
      membership_criterion_category_list =
                            ['product_line/apparel'],
      int_index = 1,
      string_index = 'product'
    )
    product_line_predicate.immediateReindexObject()
    region_predicate.immediateReindexObject()

    invoice_rule.updateMatrix()
    cell_list = invoice_rule.getCellValueList(base_id='movement')
    self.assertEquals(len(cell_list),1)
    cell = cell_list[0]

    for line_id, line_source_id, line_destination_id, line_ratio in \
        self.transaction_line_definition_list:
      line = cell.newContent(id=line_id,
          portal_type='Accounting Transaction Line', quantity=line_ratio,
          resource_value=resource,
          source_value=account_module[line_source_id],
          destination_value=account_module[line_destination_id])

    invoice_rule.validate()
220
    transaction.commit()
221 222
    self.tic()

223 224 225 226 227
  def stepCreateEntities(self, sequence, **kw) :
    """Create a vendor and two clients. """
    self.stepCreateOrganisation1(sequence, **kw)
    self.stepCreateOrganisation2(sequence, **kw)
    self.stepCreateOrganisation3(sequence, **kw)
228 229
    self.stepCreateProject1(sequence, **kw)
    self.stepCreateProject2(sequence, **kw)
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
    vendor = sequence.get('organisation1')
    vendor.setRegion(self.default_region)
    vendor.validate()
    sequence.edit(vendor=vendor)
    client1 = sequence.get('organisation2')
    client1.setRegion(self.default_region)
    self.assertNotEquals(client1.getRegionValue(), None)
    client1.validate()
    sequence.edit(client1=client1)
    client2 = sequence.get('organisation3')
    self.assertEquals(client2.getRegionValue(), None)
    client2.validate()
    sequence.edit(client2=client2)

  def stepCheckOrderRule(self, sequence=None, sequence_list=None, **kw):
    """Check we have a related Order Rule"""
    order = sequence.get('order')
    simulation_tool = self.getSimulationTool()
    # Check that there is an applied rule for our packing list
    rule_list = [x for x in simulation_tool.objectValues()
                            if x.getCausalityValue()==order]
    self.assertNotEquals(len(rule_list), 0)
    sequence.edit(order_rule_list = rule_list)

    self.assertEquals(len(order.getMovementList()),
                  sum([len(rule.objectIds()) for rule in rule_list]))

  def stepCheckInvoicingRule(self, sequence=None, sequence_list=None, **kw):
    """
    Checks that the invoicing rule is applied and its values are correct.
    """
    order_rule_list = sequence.get('order_rule_list')
    invoicing_rule_list = []
    invoice_transaction_rule_list = []
    for order_rule in order_rule_list :
      for order_simulation_movement in order_rule.objectValues() :
266
        temp_invoicing_rule_list = [ar for ar in order_simulation_movement.objectValues()[0].objectValues()[0].objectValues()
267
          if ar.getSpecialiseValue().getPortalType() == 'Invoice Simulation Rule']
268 269 270 271 272 273
        self.assertEquals(len(temp_invoicing_rule_list), 1)
        invoicing_rule_list.extend(temp_invoicing_rule_list)
    sequence.edit(invoicing_rule_list=invoicing_rule_list)
    invoicing_rule = invoicing_rule_list[0]
    sequence.edit(invoicing_rule = invoicing_rule)
    for invoicing_rule in invoicing_rule_list:
274
      self.assertEquals(invoicing_rule.getSpecialiseReference(),
275 276 277 278 279 280
          'default_invoicing_rule')
      self.assertEquals(invoicing_rule.getPortalType(),
          'Applied Rule')
      simulation_movement_list = invoicing_rule.objectValues()
      self.assertNotEquals(len(simulation_movement_list), 0)
      for simulation_movement in simulation_movement_list :
281 282 283
        invoice_transaction_rule_list.extend([applied_rule for applied_rule
          in simulation_movement.objectValues() if applied_rule \
              .getSpecialiseValue().getPortalType()
284
              == 'Invoice Transaction Simulation Rule'])
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
        resource_list = sequence.get('resource_list')
        self.assertEquals(simulation_movement.getPortalType(),
                          'Simulation Movement')
        self.assertTrue(simulation_movement.getResourceValue() in
            resource_list)
        self.assertTrue(simulation_movement.isConvergent())
        # TODO: What is the invoice dates supposed to be ?
        # is this done through profiles ?
        #self.assertEquals(simulation_movement.getStartDate(),
        #           sequence.get('order').getStartDate())
        #self.assertEquals(simulation_movement.getStopDate(),
        #            sequence.get('order').getStopDate())
    sequence.edit(invoice_transaction_rule_list=invoice_transaction_rule_list)

  def stepCheckInvoiceTransactionRule(self, sequence=None, sequence_list=None,
      **kw):
    """
302
    Checks that the applied invoice_transaction_rule is expanded and its movements are
303 304 305 306
    consistent with its parent movement
    """
    invoice_transaction_rule_list = \
        sequence.get('invoice_transaction_rule_list')
307 308 309 310 311
    for applied_invoice_transaction_rule in invoice_transaction_rule_list:
      parent_movement = aq_parent(applied_invoice_transaction_rule)
      invoice_transaction_rule = \
        applied_invoice_transaction_rule.getSpecialiseValue()
      self.assertEquals(3, len(applied_invoice_transaction_rule.objectValues()))
312
      for line_id, line_source_id, line_destination_id, line_ratio in \
313 314 315 316 317 318 319 320 321
                                            self.transaction_line_definition_list:
        movement = None
        for simulation_movement in \
                applied_invoice_transaction_rule.objectValues():
          if simulation_movement.getSourceId() == line_source_id and\
              simulation_movement.getDestinationId() == line_destination_id:
            movement = simulation_movement
            break

322 323 324 325 326 327 328 329
        self.assertTrue(movement is not None)
        self.assertEquals(movement.getCorrectedQuantity(), parent_movement.getPrice() *
            parent_movement.getCorrectedQuantity() * line_ratio)
        self.assertEquals(movement.getStartDate(),
            parent_movement.getStartDate())
        self.assertEquals(movement.getStopDate(),
            parent_movement.getStopDate())

330 331 332 333 334 335
  def modifyPackingListState(self, transition_name,
                             sequence,packing_list=None):
    """ calls the workflow for the packing list """
    if packing_list is None:
      packing_list = sequence.get('packing_list')
    packing_list.portal_workflow.doActionFor(packing_list, transition_name)
336

337 338 339 340 341
  def stepSetReadyPackingList(self, sequence=None, sequence_list=None, **kw):
    """ set the Packing List as Ready. This must build the invoice. """
    self.modifyPackingListState('set_ready_action', sequence=sequence)
    packing_list = sequence.get('packing_list')
    self.assertEquals(packing_list.getSimulationState(), 'ready')
342

343 344 345 346 347 348 349
  def stepSetReadyNewPackingList(self, sequence=None,
                                 sequence_list=None, **kw):
    """ set the Packing List as Ready. This must build the invoice. """
    packing_list = sequence.get('new_packing_list')
    self.modifyPackingListState('set_ready_action', sequence=sequence,
                                packing_list=packing_list)
    self.assertEquals(packing_list.getSimulationState(), 'ready')
350

351 352 353 354
  def stepStartPackingList(self, sequence=None, sequence_list=None, **kw):
    self.modifyPackingListState('start_action', sequence=sequence)
    packing_list = sequence.get('packing_list')
    self.assertEquals(packing_list.getSimulationState(), 'started')
355

356 357 358 359 360
  def stepStartNewPackingList(self, sequence=None, sequence_list=None, **kw):
    packing_list = sequence.get('new_packing_list')
    self.modifyPackingListState('start_action', sequence=sequence,
                                packing_list=packing_list)
    self.assertEquals(packing_list.getSimulationState(), 'started')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
361

362 363 364 365
  def stepStopPackingList(self, sequence=None, sequence_list=None, **kw):
    self.modifyPackingListState('stop_action', sequence=sequence)
    packing_list = sequence.get('packing_list')
    self.assertEquals(packing_list.getSimulationState(), 'stopped')
366

367 368 369 370
  def stepDeliverPackingList(self, sequence=None, sequence_list=None, **kw):
    self.modifyPackingListState('deliver_action', sequence=sequence)
    packing_list = sequence.get('packing_list')
    self.assertEquals(packing_list.getSimulationState(), 'delivered')
371

372 373 374 375
  def stepCancelPackingList(self, sequence=None, sequence_list=None, **kw):
    self.modifyPackingListState('cancel_action', sequence=sequence)
    packing_list = sequence.get('packing_list')
    self.assertEquals(packing_list.getSimulationState(), 'cancelled')
376

377 378 379 380 381 382
  def modifyInvoiceState(self, transition_name,
                             sequence,invoice=None):
    """ calls the workflow for the invoice """
    if invoice is None:
      invoice = sequence.get('invoice')
    invoice.portal_workflow.doActionFor(invoice, transition_name)
383

384 385 386 387
  def stepStartInvoice(self, sequence=None, sequence_list=None, **kw):
    self.modifyInvoiceState('start_action', sequence=sequence)
    invoice = sequence.get('invoice')
    self.assertEquals(invoice.getSimulationState(), 'started')
388

389 390 391 392 393
  def stepStartNewInvoice(self, sequence=None, sequence_list=None, **kw):
    invoice = sequence.get('new_invoice')
    self.modifyInvoiceState('start_action', sequence=sequence,
                                invoice=invoice)
    self.assertEquals(invoice.getSimulationState(), 'started')
394

395 396 397 398
  def stepStopInvoice(self, sequence=None, sequence_list=None, **kw):
    self.modifyInvoiceState('stop_action', sequence=sequence)
    invoice = sequence.get('invoice')
    self.assertEquals(invoice.getSimulationState(), 'stopped')
399

400 401 402 403
  def stepDeliverInvoice(self, sequence=None, sequence_list=None, **kw):
    self.modifyInvoiceState('deliver_action', sequence=sequence)
    invoice = sequence.get('invoice')
    self.assertEquals(invoice.getSimulationState(), 'delivered')
404

405 406 407 408
  def stepCancelInvoice(self, sequence=None, sequence_list=None, **kw):
    self.modifyInvoiceState('cancel_action', sequence=sequence)
    invoice = sequence.get('invoice')
    self.assertEquals(invoice.getSimulationState(), 'cancelled')
409

410

411 412 413 414 415 416 417
  def stepSwitchPackingLists(self, sequence=None, sequence_list=None, **kw):
    packing_list = sequence.get('packing_list')
    new_packing_list = sequence.get('new_packing_list')
    #invoice = new_packing_list.getDefaultCausalityRelatedValue(
        #portal_type=self.invoice_portal_type)
    sequence.edit(packing_list=new_packing_list,
        new_packing_list=packing_list)#, invoice=invoice)
418

419 420 421 422
  def stepSwitchInvoices(self, sequence=None, sequence_list=None, **kw):
    invoice = sequence.get('invoice')
    new_invoice = sequence.get('new_invoice')
    sequence.edit(invoice=new_invoice, new_invoice=invoice)
423

424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
  def stepCheckPackingListSimulation(self, sequence=None, sequence_list=None, **kw):
    """ checks that simulation movements related to the packing list are OK """
    packing_list = sequence.get('packing_list')
    order = sequence.get('order')
    order_root_applied_rule = order.getCausalityRelatedValueList(
                                  portal_type = 'Applied Rule')[0]
    # check simulation movements from this packing list
    for movement in packing_list.getMovementList() :
      simulation_movement_list = movement.getOrderRelatedValueList()
      self.assertNotEquals(len(simulation_movement_list), 0)
      total_quantity = 0
      for simulation_movement in simulation_movement_list :
        total_quantity += simulation_movement.getQuantity()
        # check that those movements come from the same root applied
        # rule than the order.
        self.assertEquals( simulation_movement.getRootAppliedRule(),
                           order_root_applied_rule)
      self.assertEquals(total_quantity, movement.getQuantity())
442

443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
  def checkMirrorAcquisition(self, object, acquired_object):
    """
      Check if properties are well acquired for mirrored case
    """
    # packing_list_movement, simulation_movement

    self.assertEquals(acquired_object.getStartDate(), object.getStopDate())
    self.assertEquals(acquired_object.getStopDate(), object.getStartDate())
    self.assertEquals(acquired_object.getSourceValue(), \
                      object.getDestinationValue())
    self.assertEquals(acquired_object.getDestinationValue(), \
                      object.getSourceValue())

    self.assertEquals(acquired_object.getSourceSectionValue(), \
                      object.getDestinationSectionValue())
    self.assertEquals(acquired_object.getDestinationSectionValue(), \
                      object.getSourceSectionValue())

461 462 463 464 465 466 467
  def stepCheckInvoiceBuilding(self, sequence=None, sequence_list=None, **kw):
    """
    checks that the invoice is built with the default_invoice_builder
    """
    packing_list = sequence.get('packing_list')
    related_invoice_list = packing_list.getCausalityRelatedValueList(
                     portal_type=self.invoice_portal_type)
468

469 470 471 472
    if packing_list.getPortalType() == 'Purchase Packing List':
      packing_list_building_state = 'stopped'
    else:
      packing_list_building_state = 'started'
473 474 475 476 477
    packing_list_state = packing_list.getSimulationState()
    if packing_list_state != packing_list_building_state :
      self.assertEquals(0, len(related_invoice_list))
    else:
      self.assertEquals(1, len(related_invoice_list))
478

479 480 481 482
      invoice = related_invoice_list[0].getObject()
      self.failUnless(invoice is not None)
      # Invoices created by Delivery Builder are in confirmed state
      self.assertEquals(invoice.getSimulationState(), 'confirmed')
483

484 485 486 487 488 489 490 491 492 493
      # Get the list of simulation movements of packing list ...
      packing_list_simulation_movement_list = []
      for packing_list_movement in packing_list.getMovementList():
           packing_list_simulation_movement_list.extend(
                packing_list_movement.getDeliveryRelatedValueList())
      # ... invoice simulation movement are their childrens.
      simulation_movement_list = []
      for p_l_simulation_movement in packing_list_simulation_movement_list :
        for applied_rule in p_l_simulation_movement.objectValues() :
          simulation_movement_list.extend(applied_rule.objectValues())
494

495 496 497 498 499 500 501 502 503 504
      # First, test if each Simulation Movement is related to an
      # Invoice Movement
      invoice_relative_url = invoice.getRelativeUrl()
      for simulation_movement in simulation_movement_list:
        invoice_movement_list = simulation_movement.getDeliveryValueList()
        self.assertEquals(len(invoice_movement_list), 1)
        invoice_movement = invoice_movement_list[0]
        self.failUnless(invoice_movement is not None)
        self.assert_(invoice_movement.getRelativeUrl().\
                              startswith(invoice_relative_url))
505

506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
      # Then, test if each Invoice movement is equals to the sum of somes
      # Simulation Movements
      for invoice_movement in invoice.getMovementList(portal_type = [
                          self.invoice_cell_portal_type,
                          self.invoice_line_portal_type]) :
        related_simulation_movement_list = invoice_movement.\
                 getDeliveryRelatedValueList(portal_type='Simulation Movement')
        quantity = 0
        total_price = 0
        invoice_movement_quantity = invoice_movement.getQuantity()
        for related_simulation_movement in related_simulation_movement_list:
          quantity += related_simulation_movement.getQuantity()
          total_price += related_simulation_movement.getPrice() *\
                         related_simulation_movement.getQuantity()
          # Test resource
          self.assertEquals(invoice_movement.getResource(), \
                            related_simulation_movement.getResource())
          # Test resource variation
          self.assertEquals(invoice_movement.getVariationText(), \
                            related_simulation_movement.getVariationText())
          self.assertEquals(invoice_movement.getVariationCategoryList(), \
                        related_simulation_movement.getVariationCategoryList())
          # Test acquisition
          self.checkAcquisition(invoice_movement,
                                related_simulation_movement)
          # Test delivery ratio
          self.assertEquals(related_simulation_movement.getQuantity() /\
                            invoice_movement_quantity, \
                            related_simulation_movement.getDeliveryRatio())
535

536 537 538
        self.assertEquals(quantity, invoice_movement.getQuantity())
        # Test price
        self.assertEquals(total_price / quantity, invoice_movement.getPrice())
539

540
      sequence.edit(invoice = invoice)
541

542 543 544 545
      # Test causality
      self.assertEquals(len(invoice.getCausalityValueList(
                      portal_type = self.packing_list_portal_type)), 1)
      self.assertEquals(invoice.getCausalityValue(), packing_list)
546

547 548 549 550 551
      # Finally, test getTotalQuantity and getTotalPrice on Invoice
      self.assertEquals(packing_list.getTotalQuantity(),
                        invoice.getTotalQuantity())
      self.assertEquals(packing_list.getTotalPrice(),
                        invoice.getTotalPrice())
552

553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
  def stepCheckInvoicesConsistency(self, sequence=None, sequence_list=None,
      **kw):
    """
    Checks that all invoices are consistent:
    - transaction lines match invoice lines
    - no movement is divergent
    """
    invoice_list = self.getPortal()['accounting_module'].objectValues()
    for invoice in invoice_list:
      accounting_state_list = \
          list(self.getPortal().getPortalCurrentInventoryStateList())
      accounting_state_list.append('cancelled')
      if invoice.getSimulationState() in accounting_state_list:
        invoice_line_list = invoice.contentValues(
            portal_type=self.invoice_line_portal_type)
        invoice_transaction_line_list = invoice.contentValues(
            portal_type=self.invoice_transaction_line_portal_type)
        self.assertEquals(3, len(invoice_transaction_line_list))
        expected_price = 0.0
        for line in invoice_line_list:
          expected_price += line.getTotalPrice()
        for line_id, line_source, line_dest, line_ratio in \
            self.transaction_line_definition_list:
          for line in invoice.contentValues(
              portal_type=self.invoice_transaction_line_portal_type):
            if line.getSource() == 'account_module/%s' % line_source and \
                line.getDestination() == 'account_module/%s' % line_dest:
              break
          else:
            self.fail('No line found that matches %s' % line_id)
          resource_precision = line.getResourceValue().getQuantityPrecision()
          self.assertEquals(round(line.getQuantity(), resource_precision),
              round(expected_price * line_ratio, resource_precision))
586

587 588 589 590 591 592 593 594 595 596 597
  def stepCheckInvoiceLineHasReferenceAndIntIndex(self, sequence=None, **kw):
    """Check that the unique invoice line in the invoice has reference and int
    index.
    """
    invoice = sequence.get('invoice')
    invoice_line_list = invoice.contentValues(
                            portal_type=self.invoice_line_portal_type)
    self.assertEquals(1, len(invoice_line_list))
    invoice_line = invoice_line_list[0]
    self.assertEquals(1, invoice_line.getIntIndex())
    self.assertEquals('1', invoice_line.getReference())
598

599 600 601 602 603 604 605 606 607
  def stepCheckPackingListInvoice(
                      self, sequence=None, sequence_list=None, **kw):
    """ Checks if the delivery builder is working as expected,
        coping the atributes from packing list to invoice."""
    packing_list = sequence.get('packing_list')
    related_invoice_list = packing_list.getCausalityRelatedValueList(
                     portal_type=self.invoice_portal_type)
    self.assertEquals(len(related_invoice_list), 1)
    invoice = related_invoice_list[0]
608 609
    self.assertEquals(packing_list.getSource(), invoice.getSource())
    self.assertEquals(packing_list.getDestination(), invoice.getDestination())
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
    self.assertEquals(packing_list.getDestinationSection(), \
                                       invoice.getDestinationSection())
    self.assertEquals(packing_list.getSourceSection(), \
                                       invoice.getSourceSection())
    self.assertEquals(packing_list.getDestinationDecision(), \
                                       invoice.getDestinationDecision())
    self.assertEquals(packing_list.getSourceDecision(), \
                                       invoice.getSourceDecision())
    self.assertEquals(packing_list.getDestinationAdministration(), \
                                       invoice.getDestinationAdministration())
    self.assertEquals(packing_list.getSourceAdministration(), \
                                       invoice.getSourceAdministration())
    self.assertEquals(packing_list.getDestinationProject(), \
                                       invoice.getDestinationProject())
    self.assertEquals(packing_list.getSourceProject(), \
                                       invoice.getSourceProject())
    self.assertEquals(packing_list.getPriceCurrency(), \
                                       invoice.getPriceCurrency())
628

629 630


631 632 633 634 635
  def stepCheckDeliveryRuleForDeferred(
                      self, sequence=None, sequence_list=None, **kw):
    """ Checks that a delivery rule has been created when we took 'split
        and defer' decision on the divergeant Packing List. """
    # TODO
636

637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
  def stepCheckDeliveryRuleIsEmpty(
                      self, sequence=None, sequence_list=None, **kw):
    """ Checks that an empty delivery rule is created for the
        convergeant Packing List"""
    packing_list = sequence.get('packing_list')
    self.failUnless(packing_list is not None)
    simulation_tool = self.getSimulationTool()
    # Check that there is an applied rule for our packing list
    rule_list = [x for x in simulation_tool.objectValues()
                          if x.getCausalityValue()==packing_list]
    self.assertEquals(len(rule_list),1)
    packing_list_rule = rule_list[0]
    sequence.edit(packing_list_rule=packing_list_rule)
    rule_line_list = packing_list_rule.objectValues()
    packing_list_line_list = packing_list.objectValues()
    self.assertEquals(len(packing_list_line_list),
                      len(rule_line_list))
    self.assertEquals(1, len(rule_line_list))
    rule_line = rule_line_list[0]
    packing_list_line = packing_list_line_list[0]
    self.assertEquals(rule_line.getQuantity(), 10)
    self.assertEquals(rule_line.getPrice(), 100)
    self.assertEquals(rule_line.getDeliveryValue(),
                      packing_list_line)
    self.assertEquals(rule_line.getStartDate(),
                      packing_list_line.getStartDate())
    self.assertEquals(rule_line.getStopDate(),
                      packing_list_line.getStopDate())
    self.assertEquals(rule_line.getPortalType(),
                      'Simulation Movement')
667

668

669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
  def stepCheckPackingList(self,sequence=None, sequence_list=None,**kw):
    """  """
    packing_list_module = self.getSalePackingListModule()
    order_rule = sequence.get('order_rule')
    order = sequence.get('order')
    sale_packing_list_list = []
    for o in packing_list_module.objectValues():
      if o.getCausalityValue() == order:
        sale_packing_list_list.append(o)
    self.assertEquals(len(sale_packing_list_list), 1)
    sale_packing_list = sale_packing_list_list[0]
    sale_packing_list_line_list = sale_packing_list.objectValues()
    self.assertEquals(len(sale_packing_list_line_list),1)
    sale_packing_list_line = sale_packing_list_line_list[0]
    product = sequence.get('resource')
    self.assertEquals(sale_packing_list_line.getResourceValue(),
                      product)
    self.assertEquals(sale_packing_list_line.getPrice(),
                      self.price1)
    LOG('sale_packing_list_line.showDict()',0,
          sale_packing_list_line.showDict())
    self.assertEquals(sale_packing_list_line.getQuantity(),
                      self.quantity1)
    self.assertEquals(sale_packing_list_line.getTotalPrice(),
                      self.total_price1)
    sequence.edit(packing_list = sale_packing_list)
695

696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
  def stepCheckTwoInvoices(self,sequence=None, sequence_list=None, **kw):
    """ checks invoice properties are well set. """
    # Now we will check that we have two invoices created
    packing_list = sequence.get('packing_list')
    invoice_list = packing_list.getCausalityRelatedValueList(
         portal_type=self.invoice_portal_type)
    self.assertEquals(len(invoice_list),1)
    invoice = invoice_list[0]
    self.assertEquals(invoice.getSimulationState(), 'confirmed')
    sequence.edit(invoice=invoice)
    new_packing_list = sequence.get('new_packing_list')
    new_invoice_list = new_packing_list.getCausalityRelatedValueList(
        portal_type=self.invoice_portal_type)
    self.assertEquals(len(new_invoice_list),1)
    new_invoice = new_invoice_list[0]
    self.assertEquals(new_invoice.getSimulationState(), 'confirmed')
    sequence.edit(new_invoice=new_invoice)
713

714 715 716 717 718 719 720
  def stepStartTwoInvoices(self,sequence=None, sequence_list=None, **kw):
    """ start both invoices. """
    portal = self.getPortal()
    invoice = sequence.get('invoice')
    new_invoice = sequence.get('new_invoice')
    portal.portal_workflow.doActionFor(invoice, 'start_action')
    portal.portal_workflow.doActionFor(new_invoice, 'start_action')
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 747 748 749 750 751 752 753 754 755 756 757 758 759
  def stepCheckTwoInvoicesTransactionLines(self,sequence=None,
                                           sequence_list=None, **kw):
    """ checks invoice properties are well set. """
    invoice = sequence.get('invoice')
    new_invoice = sequence.get('new_invoice')
    self.assertEquals(3,len(invoice.objectValues(
        portal_type=self.invoice_transaction_line_portal_type)))
    self.assertEquals(3,len(new_invoice.objectValues(
        portal_type=self.invoice_transaction_line_portal_type)))
    account_module = self.getAccountModule()
    found_dict = {}
    for line in invoice.objectValues(
        portal_type=self.invoice_transaction_line_portal_type):
      source_id = line.getSourceId()
      found_dict[source_id] = line.getQuantity()
    total_price = (self.default_quantity-1) * self.default_price
    expected_dict = {
      'sale' : total_price,
      'receivable_vat' : total_price * self.vat_rate,
      'customer' : - (total_price + total_price * self.vat_rate)
      }
    self.failIfDifferentSet(expected_dict.keys(),found_dict.keys())
    for key in found_dict.keys():
      self.assertAlmostEquals(expected_dict[key],found_dict[key],places=2)
    found_dict = {}
    for line in new_invoice.objectValues(
        portal_type=self.invoice_transaction_line_portal_type):
      source_id = line.getSourceId()
      found_dict[source_id] = line.getQuantity()
    total_price = 1 * self.default_price
    expected_dict = {
      'sale' : total_price,
      'receivable_vat' : total_price * self.vat_rate,
      'customer' : - (total_price + total_price * self.vat_rate)
      }
    self.failIfDifferentSet(expected_dict.keys(), found_dict.keys())
    for key in found_dict.keys():
      self.assertAlmostEquals(expected_dict[key], found_dict[key], places=2)
760

761 762 763 764 765 766 767 768 769 770 771 772 773
  def stepRebuildAndCheckNothingIsCreated(self, sequence=None,
                                           sequence_list=None, **kw):
    """Rebuilds with sale_invoice_builder and checks nothing more is
    created. """
    accounting_module = self.getAccountingModule()
    portal_type_list = ('Sale Invoice Transaction', 'Purchase Invoice Transaction')
    sale_invoice_transaction_count = len(accounting_module.objectValues(
      portal_type=portal_type_list))
    for builder in self.getPortal().portal_deliveries.objectValues():
      builder.build()
    self.assertEquals(sale_invoice_transaction_count,
                      len(accounting_module.objectValues(
      portal_type=portal_type_list)))
774

775 776 777 778 779 780 781 782 783
  def stepModifyInvoicesDate(self, sequence=None,
                                           sequence_list=None, **kw):
    """Change invoice date"""
    invoice = sequence.get('invoice')
    new_invoice = sequence.get('new_invoice')
    invoice.edit(start_date=self.datetime,
                 stop_date=self.datetime+1)
    new_invoice.edit(start_date=self.datetime,
                 stop_date=self.datetime+1)
784

785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
  def stepRemoveDateMovementGroupForTransactionBuilder(self, sequence=None,
            sequence_list=None, **kw):
    """
    Remove DateMovementGroup
    """
    portal = self.getPortal()
    builder = portal.portal_deliveries.sale_invoice_transaction_builder
    delivery_movement_group_list = builder.getDeliveryMovementGroupList()
    uf = self.getPortal().acl_users
    uf._doAddUser('admin', '', ['Manager'], [])
    user = uf.getUserById('admin').__of__(uf)
    newSecurityManager(None, user)
    for movement_group in delivery_movement_group_list:
      if movement_group.getPortalType() == 'Property Movement Group':
        # it contains 'start_date' and 'stop_date' only, so we remove
        # movement group itself.
        builder.deleteContent(movement_group.getId())
    builder.newContent(
      portal_type = 'Parent Explanation Movement Group',
      collect_order_group='delivery',
      int_index=len(delivery_movement_group_list)+1
      )
    user = uf.getUserById('test_invoice_user').__of__(uf)
    newSecurityManager(None, user)
809

810 811 812 813
  def stepEditInvoice(self, sequence=None, sequence_list=None, **kw):
    """Edit the current invoice, to trigger updateAppliedRule."""
    invoice = sequence.get('invoice')
    invoice.edit()
814

815 816 817 818 819
    # call updateAppliedRule directly, don't rely on edit interactions
    rule_reference = 'default_invoice_rule'
    self.assertNotEquals(0,
        len(self.portal.portal_rules.searchFolder(reference=rule_reference)))
    invoice.updateAppliedRule(rule_reference=rule_reference)
820

821 822 823 824 825 826 827 828 829 830 831
  def stepCheckInvoiceRuleNotAppliedOnInvoiceEdit(self,
                    sequence=None, sequence_list=None, **kw):
    """If we call edit on the invoice, invoice rule should not be
    applied on lines created by delivery builder."""
    invoice = sequence.get('invoice')
    # FIXME: empty applied rule should not be created
    #self.assertEquals(len(invoice.getCausalityRelatedValueList(
    #         portal_type=self.applied_rule_portal_type)), 0)
    for invoice_mvt in invoice.getMovementList():
      self.assertEquals(len(invoice_mvt.getOrderRelatedValueList(
            portal_type=self.simulation_movement_portal_type)), 0)
832

833 834 835 836
  def stepEditPackingList(self, sequence=None, sequence_list=None, **kw):
    """Edit the current packing list, to trigger updateAppliedRule."""
    packing_list = sequence.get('packing_list')
    packing_list.edit()
837

838 839 840 841 842
    # call updateAppliedRule directly, don't rely on edit interactions
    rule_reference = 'default_delivery_rule'
    self.assertNotEquals(0,
        len(self.portal.portal_rules.searchFolder(reference=rule_reference)))
    packing_list.updateAppliedRule(rule_reference=rule_reference)
843

844 845 846 847 848 849 850 851 852 853 854
  def stepCheckDeliveryRuleNotAppliedOnPackingListEdit(self,
                    sequence=None, sequence_list=None, **kw):
    """If we call edit on the packing list, delivery rule should not be
    applied on lines created by delivery builder."""
    packing_list = sequence.get('packing_list')
    # FIXME: empty applied rule should not be created
    #self.assertEquals(len(packing_list.getCausalityRelatedValueList(
    #         portal_type=self.applied_rule_portal_type)), 0)
    for delivery_mvt in packing_list.getMovementList():
      self.assertEquals(len(delivery_mvt.getOrderRelatedValueList(
            portal_type=self.simulation_movement_portal_type)), 0)
855

856 857 858 859 860 861 862 863 864 865 866 867 868
  def stepDecreaseInvoiceLineQuantity(self, sequence=None, sequence_list=None,
      **kw):
    """
    Set a decreased quantity on invoice lines
    """
    invoice = sequence.get('invoice')
    quantity = sequence.get('line_quantity',default=self.default_quantity)
    quantity = quantity - 1
    sequence.edit(line_quantity=quantity)
    for invoice_line in invoice.objectValues(
        portal_type=self.invoice_line_portal_type):
      invoice_line.edit(quantity=quantity)
    sequence.edit(last_delta = sequence.get('last_delta', 0.0) - 1.0)
869

870 871 872 873 874 875 876 877 878 879 880 881 882
  def stepIncreaseInvoiceLineQuantity(self, sequence=None, sequence_list=None,
      **kw):
    """
    Set a Increased quantity on invoice lines
    """
    invoice = sequence.get('invoice')
    quantity = sequence.get('line_quantity',default=self.default_quantity)
    quantity = quantity + 1
    sequence.edit(line_quantity=quantity)
    for invoice_line in invoice.objectValues(
        portal_type=self.invoice_line_portal_type):
      invoice_line.edit(quantity=quantity)
    sequence.edit(last_delta = sequence.get('last_delta', 0.0) + 1.0)
883

884 885 886 887 888 889 890 891 892 893 894 895 896
  def stepSetInvoiceLineQuantityToZero(self, sequence=None, sequence_list=None,
      **kw):
    """
    Set the quantity on invoice lines to zero
    """
    invoice = sequence.get('invoice')
    #default_quantity = sequence.get('line_quantity',default_quantity)
    quantity = 0.0
    sequence.edit(line_quantity=quantity)
    for invoice_line in invoice.objectValues(
        portal_type=self.invoice_line_portal_type):
      invoice_line.edit(quantity=quantity)
    sequence.edit(last_delta = - self.default_quantity)
897

898 899 900 901 902 903
  def stepChangeInvoiceStartDate(self, sequence=None, sequence_list=None, **kw):
    """
      Change the start_date of the invoice.
    """
    invoice = sequence.get('invoice')
    invoice.edit(start_date=self.datetime + 15)
904

905 906 907 908 909 910 911
  def stepCheckInvoiceIsCalculating(self, sequence=None, sequence_list=None,
      **kw):
    """
    Test if invoice is calculating
    """
    invoice = sequence.get('invoice')
    self.assertEquals('calculating',invoice.getCausalityState())
912

913 914 915 916 917 918 919
  def stepCheckInvoiceIsDiverged(self, sequence=None, sequence_list=None,
      **kw):
    """
    Test if invoice is diverged
    """
    invoice = sequence.get('invoice')
    self.assertEquals('diverged',invoice.getCausalityState())
920

921 922 923 924 925 926 927 928
  def stepCheckInvoiceIsSolved(self, sequence=None, sequence_list=None,
      **kw):
    """
    Test if invoice is solved
    """
    invoice = sequence.get('invoice')
    self.assertEquals('solved', invoice.getCausalityState(),
                      invoice.getDivergenceList())
929

930 931 932 933 934 935 936
  def stepCheckInvoiceIsDivergent(self, sequence=None, sequence_list=None,
      **kw):
    """
    Test if invoice is divergent
    """
    invoice = sequence.get('invoice')
    self.assertTrue(invoice.isDivergent())
937

938 939 940 941 942 943 944 945
  def stepCheckInvoiceIsNotDivergent(self, sequence=None, sequence_list=None,
      **kw):
    """
    Test if invoice is not divergent
    """
    invoice = sequence.get('invoice')
    if invoice.isDivergent():
      self.fail(invoice.getDivergenceList())
946

947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
  def stepSplitAndDeferInvoice(self, sequence=None, sequence_list=None,
      **kw):
    """
    split and defer at the invoice level
    """
    invoice = sequence.get('invoice')
    kw = {'listbox':[
      {'listbox_key':line.getRelativeUrl(),
       'choice':'SplitAndDefer'} for line in invoice.getMovementList()]}
    self.portal.portal_workflow.doActionFor(
      invoice,
      'split_and_defer_action',
      start_date=self.datetime + 15,
      stop_date=self.datetime + 25,
      **kw)
    pass
963

964 965 966 967 968
  def stepUnifyStartDateWithDecisionInvoice(self, sequence=None,
                                            sequence_list=None):
    invoice = sequence.get('invoice')
    self._solveDeliveryGroupDivergence(invoice, 'start_date',
                                       invoice.getRelativeUrl())
969

970 971 972
  def stepAcceptDecisionQuantityInvoice(self,sequence=None, sequence_list=None):
    invoice = sequence.get('invoice')
    self._solveDivergence(invoice, 'quantity', 'accept')
973

974 975 976 977 978 979 980
  def stepAcceptDecisionInvoice(self, sequence=None, sequence_list=None,
      **kw):
    """
    accept decision at the invoice level
    """
    invoice = sequence.get('invoice')
    invoice.portal_workflow.doActionFor(invoice,'accept_decision_action')
981

982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
  def stepCheckInvoiceSplitted(self, sequence=None, sequence_list=None, **kw):
    """
    Test if invoice was splitted
    """
    packing_list = sequence.get('packing_list')
    invoice_list = packing_list.getCausalityRelatedValueList(
        portal_type=self.invoice_portal_type)
    self.assertEquals(2,len(invoice_list))
    invoice1 = None
    invoice2 = None
    for invoice in invoice_list:
      if invoice.getUid() == sequence.get('invoice').getUid():
        invoice1 = invoice
      else:
        invoice2 = invoice
    sequence.edit(new_invoice=invoice2)
    for line in invoice1.objectValues(
          portal_type=self.invoice_line_portal_type):
      self.assertEquals(self.default_quantity-1,line.getQuantity())
    for line in invoice2.objectValues(
          portal_type=self.invoice_line_portal_type):
      self.assertEquals(1,line.getQuantity())
1004

1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
  def stepCheckInvoiceNotSplitted(self, sequence=None, sequence_list=None, **kw):
    """
    Test if invoice was not splitted
    """
    packing_list = sequence.get('packing_list')
    invoice_list = packing_list.getCausalityRelatedValueList(
        portal_type=self.invoice_portal_type)
    self.assertEquals(1,len(invoice_list))
    invoice1 = None
    for invoice in invoice_list:
      if invoice.getUid() == sequence.get('invoice').getUid():
        invoice1 = invoice
    last_delta = sequence.get('last_delta', 0.0)
    for line in invoice1.objectValues(
        portal_type=self.invoice_line_portal_type):
      self.assertEquals(self.default_quantity + last_delta,
          line.getQuantity())
1022

1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
  def stepAddInvoiceLines(self, sequence=None, sequence_list=[]):
    """
    add some invoice and accounting lines to the invoice
    """
    invoice = sequence.get('invoice')
    invoice.newContent(portal_type='Invoice Line',
        resource_value=sequence.get('resource'), quantity=3, price=555)
    invoice.newContent(portal_type='Sale Invoice Transaction Line',
        id ='receivable', source='account_module/customer',
        destination='account_module/supplier', quantity=-1665)
    invoice.newContent(portal_type='Sale Invoice Transaction Line',
        id='income', source='account_module/sale',
        destination='account_module/purchase', quantity=1665)
1036

1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
  def stepAddWrongInvoiceLines(self, sequence=None, sequence_list=[]):
    """
    add some wrong invoice and accounting lines to the invoice
    """
    invoice = sequence.get('invoice')
    invoice.newContent(portal_type='Sale Invoice Transaction Line',
        id='bad_movement', source='account_module/sale',
        destination='account_module/purchase', quantity=2, price=4)
    invoice.newContent(portal_type='Sale Invoice Transaction Line',
        id='counter_bad_movement', source='account_module/sale',
        destination='account_module/purchase', quantity=-2, price=4)
    for movement in invoice.getMovementList():
      movement.edit(resource_value=sequence.get('resource'))
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
  def stepCheckStartInvoiceFail(self, sequence=None, sequence_list=[]):
    """
    checks that it's not possible to start an invoice with really wrong
    lines
    """
    try:
      self.tic()
    except RuntimeError, exc:
      invoice = sequence.get('invoice')
      it_builder = self.portal.portal_deliveries.sale_invoice_transaction_builder
      # check which activities are failing
      self.assertTrue(str(exc).startswith('tic is looping forever.'),
          '%s does not start with "tic is looping forever."' % str(exc))
      msg_list = ['/'.join(x.object_path) for x in
          self.getActivityTool().getMessageList()]
      self.assertTrue(it_builder.getPath() in msg_list, '%s in %s' %
          (it_builder.getPath(), msg_list))
      # flush failing activities
      activity_tool = self.getActivityTool()
      activity_tool.manageClearActivities(keep=0)
    else:
      self.fail("Error: stepStartInvoice didn't fail, the builder script"
          + " InvoiceTransaction_postTransactionLineGeneration should have"
          + " complained that accounting movements use multiple resources")
1075

1076 1077 1078 1079 1080 1081 1082 1083 1084
  def stepCheckSimulationTrees(self, sequence=None, sequence_list=[]):
    """
    check that rules are created in the order we expect them
    """
    applied_rule_set = set()
    invoice = sequence.get('invoice')
    for movement in invoice.getMovementList():
      for sm in movement.getDeliveryRelatedValueList():
        applied_rule_set.add(sm.getRootAppliedRule())
1085

1086
    rule_dict = {
1087
        'Order Root Simulation Rule': {
1088
          'movement_type_list': ['Sale Order Line', 'Sale Order Cell'],
1089
          'next_rule_list': ['Delivery Simulation Rule', ],
1090
          },
1091
        'Delivery Simulation Rule': {
1092
          'movement_type_list': ['Sale Packing List Line', 'Sale Packing List Cell'],
1093
          'next_rule_list': ['Invoice Simulation Rule', ],
1094
          },
1095
        'Invoice Simulation Rule': {
1096
          'movement_type_list': invoice.getPortalInvoiceMovementTypeList(),
1097
          'next_rule_list': ['Invoice Transaction Simulation Rule', 'Trade Model Simulation Rule'],
1098
          },
1099 1100
        'Trade Model Simulation Rule': {
          'next_rule_list': ['Invoice Transaction Simulation Rule'],
1101
          },
1102
        'Invoice Simulation Rule': {
1103 1104
          'movement_type_list': invoice.getPortalInvoiceMovementTypeList() \
              + invoice.getPortalAccountingMovementTypeList(),
1105 1106
          'next_rule_list': ['Invoice Transaction Simulation Rule', 'Payment Simulation Rule',
            'Trade Model Simulation Rule'],
1107
          },
1108
        'Invoice Transaction Simulation Rule': {
1109 1110
          'parent_movement_type_list': invoice.getPortalInvoiceMovementTypeList(),
          'movement_type_list': invoice.getPortalAccountingMovementTypeList(),
1111
          'next_rule_list': ['Payment Simulation Rule'],
1112
          },
1113
        'Payment Simulation Rule': {
1114 1115 1116 1117
          'parent_movement_type_list': invoice.getPortalAccountingMovementTypeList(),
          'next_rule_list': [],
          },
        }
1118

1119 1120 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
    def checkTree(rule):
      """
      checks the tree recursively
      """
      rule_type = rule.getSpecialiseValue().getPortalType()
      rule_def = rule_dict.get(rule_type, {})
      for k, v in rule_def.iteritems():
        if k == 'movement_type_list':
          for movement in rule.objectValues():
            if movement.getDeliveryValue() is not None:
              self.assertTrue(movement.getDeliveryValue().getPortalType() in v,
                  'looking for %s in %s on %s' % (
                  movement.getDeliveryValue().getPortalType(), v,
                  movement.getPath()))
        elif k == 'next_rule_list':
          for movement in rule.objectValues():
            found_rule_dict = {}
            for next_rule in movement.objectValues():
              next_rule_type = next_rule.getSpecialiseValue().getPortalType()
              self.assertTrue(next_rule_type in v,
                  'looking for %s in %s on %s' % (
                  next_rule_type, v, next_rule.getPath()))
              n = found_rule_dict.get(next_rule_type, 0)
              found_rule_dict[next_rule_type] = n + 1
            # for each movement, we want to make sure that each rule is not
            # instanciated more than once
            if len(found_rule_dict):
              self.assertEquals(set(found_rule_dict.itervalues()), set([1]))
        elif k == 'parent_movement_type_list':
          if rule.getParentValue().getDeliveryValue() is not None:
            parent_type = rule.getParentValue().getDeliveryValue().getPortalType()
            self.assertTrue(parent_type in v, 'looking for %s in %s on %s' % (
                parent_type, v, rule.getParentValue().getPath()))
        elif k == 'parent_id_list':
          self.assertTrue(rule.getParentId() in v, 'looking for %s in %s on %s'
              % (rule.getParentId(), v, rule.getPath()))
      for movement in rule.objectValues():
        for next_rule in movement.objectValues():
          checkTree(next_rule)
1158

1159 1160
    for applied_rule in applied_rule_set:
      checkTree(applied_rule)
1161

1162
  def stepAddInvoiceLinesManyTransactions(self, sequence=None, sequence_list=[]):
1163
    """
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
    add some invoice and accounting lines to the invoice
    """
    invoice = sequence.get('invoice')
    invoice_line = invoice.newContent(portal_type='Invoice Line')
    transaction_line_1 = invoice.newContent(portal_type='Sale Invoice Transaction Line')
    transaction_line_2 = invoice.newContent(portal_type='Sale Invoice Transaction Line')
    transaction.commit()
    self.tic()
    invoice_line.edit(resource_value=sequence.get('resource'), quantity=3,
        price=555)
    transaction_line_1.edit(id ='receivable', source='account_module/customer',
        destination='account_module/supplier', quantity=-1665)
    transaction_line_2.edit(
        id='income', source='account_module/sale',
        destination='account_module/purchase', quantity=1665)
1179

1180 1181 1182 1183 1184 1185
class TestInvoice(TestInvoiceMixin):
  """Test methods for sale and purchase invoice.
  Subclasses must defines portal types to use.
  """
  quiet = 1
  def test_invoice_transaction_line_resource(self):
1186
    """
1187 1188 1189 1190 1191 1192
    tests that simulation movements corresponding to accounting line have a
    good resource in the simulation
    """
    resource = self.portal.getDefaultModule(
        self.resource_portal_type).newContent(
                    portal_type=self.resource_portal_type,
1193
                    title='Resource',
Jérome Perrin's avatar
Jérome Perrin committed
1194
                    product_line='apparel')
1195 1196
    currency = self.portal.currency_module.newContent(
                                portal_type='Currency',
1197 1198
                                title='Currency',
                                base_unit_quantity=0.01)
1199
    self.createInvoiceTransactionRule(currency)
1200

1201 1202 1203
    client = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
                            title='Client',
1204
                            price_currency= currency.getRelativeUrl(),
1205 1206 1207 1208
                            default_address_region=self.default_region)
    vendor = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
                            title='Vendor',
1209
                            price_currency= currency.getRelativeUrl(),
1210 1211 1212 1213 1214 1215 1216
                            default_address_region=self.default_region)
    order = self.portal.getDefaultModule(self.order_portal_type).newContent(
                              portal_type=self.order_portal_type,
                              source_value=vendor,
                              source_section_value=vendor,
                              destination_value=client,
                              destination_section_value=client,
1217 1218 1219 1220 1221 1222 1223 1224 1225
                              start_date=DateTime(2008, 1, 1),
                              price_currency_value=currency,
                              title='Order')
    order_line = order.newContent(portal_type=self.order_line_portal_type,
                                  resource_value=resource,
                                  quantity=1,
                                  price=2)

    order.confirm()
1226
    transaction.commit()
1227
    self.tic()
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245

    related_applied_rule = order.getCausalityRelatedValue(
                             portal_type='Applied Rule')
    order_movement = related_applied_rule.contentValues()[0]
    delivery_applied_rule = order_movement.contentValues()[0]
    delivery_movement = delivery_applied_rule.contentValues()[0]
    invoice_applied_rule = delivery_movement.contentValues()[0]
    invoice_movement = invoice_applied_rule.contentValues()[0]
    invoice_transaction_applied_rule = invoice_movement.contentValues()[0]
    invoice_transaction_movement =\
         invoice_transaction_applied_rule.contentValues()[0]
    self.assertEquals(currency,
          invoice_transaction_movement.getResourceValue())
    self.assertEquals(currency,
          delivery_movement.getPriceCurrencyValue())

    
  def test_modify_planned_order_invoicing_rule(self):
1246
    """
1247 1248 1249 1250 1251 1252
    tests that modifying a planned order affects movements from invoicing
    rule
    """
    resource = self.portal.getDefaultModule(
        self.resource_portal_type).newContent(
                    portal_type=self.resource_portal_type,
1253 1254 1255 1256
                    title='Resource',
                    product_line='apparel')
    currency = self.portal.currency_module.newContent(
                                portal_type='Currency',
1257 1258 1259
                                title='Currency',
                                base_unit_quantity=0.01)

1260 1261 1262
    client = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
                            title='Client',
1263
                            price_currency= currency.getRelativeUrl())
1264 1265 1266
    vendor = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
                            title='Vendor',
1267
                            price_currency= currency.getRelativeUrl())
1268 1269 1270 1271 1272 1273
    order = self.portal.getDefaultModule(self.order_portal_type).newContent(
                              portal_type=self.order_portal_type,
                              source_value=vendor,
                              source_section_value=vendor,
                              destination_value=client,
                              destination_section_value=client,
1274
                              start_date=DateTime(2008, 1, 1),
1275 1276
                              price_currency_value=currency,
                              title='Order')
1277
    order_line = order.newContent(portal_type=self.order_line_portal_type,
1278
                                  resource_value=resource,
1279
                                  quantity=1,
1280
                                  price=2)
1281

1282 1283 1284 1285 1286 1287 1288 1289
    other_entity = self.portal.organisation_module.newContent(
                                    portal_type='Organisation',
                                    title='Other Entity',
                                    price_currency=currency.getRelativeUrl())
    other_project = self.portal.project_module.newContent(
                                    portal_type='Project',
                                    title='Other Project')
    order.plan()
1290
    transaction.commit()
1291
    self.tic()
1292
    self.assertEquals('planned', order.getSimulationState())
1293

1294 1295 1296 1297 1298 1299 1300
    related_applied_rule = order.getCausalityRelatedValue(
                             portal_type='Applied Rule')
    delivery_movement = related_applied_rule.contentValues()[0]
    invoice_applied_rule = delivery_movement.contentValues()[0]
    invoice_movement = invoice_applied_rule.contentValues()[0]

    order_line.setSourceValue(other_entity)
1301 1302
    transaction.commit()
    self.tic()
1303 1304 1305
    invoice_movement = invoice_applied_rule.contentValues()[0]
    self.assertEquals(other_entity,
                      invoice_movement.getSourceValue())
1306

1307 1308 1309 1310 1311 1312
    order_line.setDestinationValue(other_entity)
    transaction.commit()
    self.tic()
    invoice_movement = invoice_applied_rule.contentValues()[0]
    self.assertEquals(other_entity,
                      invoice_movement.getDestinationValue())
Jérome Perrin's avatar
Jérome Perrin committed
1313

1314 1315 1316 1317 1318 1319
    order_line.setSourceSectionValue(other_entity)
    transaction.commit()
    self.tic()
    invoice_movement = invoice_applied_rule.contentValues()[0]
    self.assertEquals(other_entity,
                      invoice_movement.getSourceSectionValue())
1320

1321 1322 1323
    # make sure destination_section != source_section, this might be needed by
    # some rules
    order_line.setSourceSectionValue(order_line.getDestinationSectionValue())
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 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 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 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
    order_line.setDestinationSectionValue(other_entity)
    transaction.commit()
    self.tic()
    invoice_movement = invoice_applied_rule.contentValues()[0]
    self.assertEquals(other_entity,
                 invoice_movement.getDestinationSectionValue())

    order_line.setSourceAdministrationValue(other_entity)
    transaction.commit()
    self.tic()
    invoice_movement = invoice_applied_rule.contentValues()[0]
    self.assertEquals(other_entity,
                 invoice_movement.getSourceAdministrationValue())

    order_line.setDestinationAdministrationValue(other_entity)
    transaction.commit()
    self.tic()
    invoice_movement = invoice_applied_rule.contentValues()[0]
    self.assertEquals(other_entity,
            invoice_movement.getDestinationAdministrationValue())

    order_line.setSourceDecisionValue(other_entity)
    transaction.commit()
    self.tic()
    invoice_movement = invoice_applied_rule.contentValues()[0]
    self.assertEquals(other_entity,
                 invoice_movement.getSourceDecisionValue())

    order_line.setDestinationDecisionValue(other_entity)
    transaction.commit()
    self.tic()
    invoice_movement = invoice_applied_rule.contentValues()[0]
    self.assertEquals(other_entity,
            invoice_movement.getDestinationDecisionValue())

    order_line.setSourceProjectValue(other_project)
    transaction.commit()
    self.tic()
    invoice_movement = invoice_applied_rule.contentValues()[0]
    self.assertEquals(other_project,
                 invoice_movement.getSourceProjectValue())

    order_line.setDestinationProjectValue(other_project)
    transaction.commit()
    self.tic()
    invoice_movement = invoice_applied_rule.contentValues()[0]
    self.assertEquals(other_project,
            invoice_movement.getDestinationProjectValue())

    order_line.setSourcePaymentValue(other_entity)
    transaction.commit()
    self.tic()
    invoice_movement = invoice_applied_rule.contentValues()[0]
    self.assertEquals(other_entity,
                 invoice_movement.getSourcePaymentValue())

    order_line.setDestinationPaymentValue(other_entity)
    transaction.commit()
    self.tic()
    invoice_movement = invoice_applied_rule.contentValues()[0]
    self.assertEquals(other_entity,
            invoice_movement.getDestinationPaymentValue())

    order_line.setSourceFunctionValue(other_entity)
    transaction.commit()
    self.tic()
    invoice_movement = invoice_applied_rule.contentValues()[0]
    self.assertEquals(other_entity,
                 invoice_movement.getSourceFunctionValue())

    order_line.setDestinationFunctionValue(other_entity)
    transaction.commit()
    self.tic()
    invoice_movement = invoice_applied_rule.contentValues()[0]
    self.assertEquals(other_entity,
            invoice_movement.getDestinationFunctionValue())

    self.assertNotEquals(123, order_line.getPrice())
    order_line.setPrice(123)
    transaction.commit()
    self.tic()
    invoice_movement = invoice_applied_rule.contentValues()[0]
    self.assertEquals(123,
            invoice_movement.getPrice())

    self.assertNotEquals(456, order_line.getQuantity())
    order_line.setQuantity(456)
    transaction.commit()
    self.tic()
    invoice_movement = invoice_applied_rule.contentValues()[0]
    self.assertEquals(456,
            invoice_movement.getQuantity())

    other_resource = self.portal.product_module.newContent(
                                        portal_type='Product',
                                        title='Other Resource')
    order_line.setResourceValue(other_resource)
1422
    transaction.commit()
1423
    self.tic()
1424 1425 1426 1427 1428 1429 1430 1431
    # after changing 'resource', related simulation movement will be
    # replaced with another id, and we need to find the appropriate one
    # here.
    delivery_movement = related_applied_rule.contentValues()[0]
    invoice_applied_rule = delivery_movement.contentValues()[0]
    invoice_movement = invoice_applied_rule.contentValues()[0]
    self.assertEquals(other_resource,
            invoice_movement.getResourceValue())
1432

1433
    order_line.setStartDate(DateTime(2001, 02, 03))
1434
    transaction.commit()
1435
    self.tic()
1436 1437 1438
    invoice_movement = invoice_applied_rule.contentValues()[0]
    self.assertEquals(DateTime(2001, 02, 03),
                 invoice_movement.getStartDate())
1439

1440 1441 1442 1443 1444 1445
    order_line.setStopDate(DateTime(2002, 03, 04))
    transaction.commit()
    self.tic()
    invoice_movement = invoice_applied_rule.contentValues()[0]
    self.assertEquals(DateTime(2002, 03, 04),
                 invoice_movement.getStopDate())
1446

1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
  def test_modify_planned_order_invoice_transaction_rule(self):
    """
    tests that modifying a planned order affects movements from invoice
    transaction rule
    """
    resource = self.portal.getDefaultModule(
        self.resource_portal_type).newContent(
                    portal_type=self.resource_portal_type,
                    title='Resource',
                    product_line='apparel')
    currency = self.portal.currency_module.newContent(
                                portal_type='Currency',
                                title='Currency',
                                base_unit_quantity=0.01)
    self.createInvoiceTransactionRule(currency)
1462

1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
    client = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
                            title='Client',
                            default_address_region=self.default_region)
    vendor = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
                            title='Vendor',
                            default_address_region=self.default_region)
    order = self.portal.getDefaultModule(self.order_portal_type).newContent(
                              portal_type=self.order_portal_type,
                              source_value=vendor,
                              source_section_value=vendor,
                              destination_value=client,
                              destination_section_value=client,
                              start_date=DateTime(2008, 1, 1),
                              price_currency_value=currency,
                              title='Order')
    order_line = order.newContent(portal_type=self.order_line_portal_type,
                                  resource_value=resource,
                                  quantity=1,
                                  price=2)
    other_entity = self.portal.organisation_module.newContent(
                                      portal_type='Organisation',
                                      title='Other Entity',
                                      default_address_region=self.default_region)
    other_project = self.portal.project_module.newContent(
                                      portal_type='Project',
                                      title='Other Project')
    order.plan()
1492
    transaction.commit()
1493
    self.tic()
1494
    self.assertEquals('planned', order.getSimulationState())
1495

1496 1497 1498 1499 1500 1501 1502 1503
    related_applied_rule = order.getCausalityRelatedValue(
                             portal_type='Applied Rule')
    order_movement = related_applied_rule.contentValues()[0]
    delivery_applied_rule = order_movement.contentValues()[0]
    delivery_movement = delivery_applied_rule.contentValues()[0]
    invoice_applied_rule = delivery_movement.contentValues()[0]
    invoice_movement = invoice_applied_rule.contentValues()[0]
    invoice_transaction_applied_rule = invoice_movement.contentValues()[0]
1504

1505 1506 1507 1508 1509 1510 1511 1512
    # utility function to return the simulation movement that should be used
    # for "income" line
    def getIncomeSimulationMovement(applied_rule):
      for movement in applied_rule.contentValues():
        if movement.getDestination() == 'account_module/purchase'\
            and movement.getSource() == 'account_module/sale':
          return movement
      self.fail('Income movement not found')
1513

1514 1515 1516
    self.assertEquals(3, len(invoice_transaction_applied_rule))
    invoice_transaction_movement = getIncomeSimulationMovement(
                                        invoice_transaction_applied_rule)
1517

1518
    order_line.setSourceSectionValue(other_entity)
1519
    transaction.commit()
1520
    self.tic()
1521 1522
    self.assertEquals(other_entity,
                      invoice_transaction_movement.getSourceSectionValue())
1523

1524 1525 1526
    # make sure destination_section != source_section, this might be needed by
    # some rules
    order_line.setSourceSectionValue(order_line.getDestinationSectionValue())
1527

1528
    order_line.setDestinationSectionValue(other_entity)
1529
    transaction.commit()
1530
    self.tic()
1531 1532 1533 1534 1535
    self.assertEquals(3, len(invoice_transaction_applied_rule))
    invoice_transaction_movement = getIncomeSimulationMovement(
                                        invoice_transaction_applied_rule)
    self.assertEquals(other_entity,
                 invoice_transaction_movement.getDestinationSectionValue())
1536

1537 1538 1539 1540 1541 1542 1543 1544
    order_line.setSourceAdministrationValue(other_entity)
    transaction.commit()
    self.tic()
    self.assertEquals(3, len(invoice_transaction_applied_rule))
    invoice_transaction_movement = getIncomeSimulationMovement(
                                        invoice_transaction_applied_rule)
    self.assertEquals(other_entity,
                 invoice_transaction_movement.getSourceAdministrationValue())
1545

1546 1547 1548 1549 1550 1551 1552 1553
    order_line.setDestinationAdministrationValue(other_entity)
    transaction.commit()
    self.tic()
    self.assertEquals(3, len(invoice_transaction_applied_rule))
    invoice_transaction_movement = getIncomeSimulationMovement(
                                        invoice_transaction_applied_rule)
    self.assertEquals(other_entity,
            invoice_transaction_movement.getDestinationAdministrationValue())
1554

1555
    order_line.setSourceDecisionValue(other_entity)
1556
    transaction.commit()
1557
    self.tic()
1558 1559 1560 1561 1562
    self.assertEquals(3, len(invoice_transaction_applied_rule))
    invoice_transaction_movement = getIncomeSimulationMovement(
                                        invoice_transaction_applied_rule)
    self.assertEquals(other_entity,
                 invoice_transaction_movement.getSourceDecisionValue())
1563

1564 1565 1566 1567 1568 1569 1570 1571
    order_line.setDestinationDecisionValue(other_entity)
    transaction.commit()
    self.tic()
    self.assertEquals(3, len(invoice_transaction_applied_rule))
    invoice_transaction_movement = getIncomeSimulationMovement(
                                        invoice_transaction_applied_rule)
    self.assertEquals(other_entity,
            invoice_transaction_movement.getDestinationDecisionValue())
1572

1573 1574 1575 1576 1577 1578 1579 1580
    order_line.setSourceProjectValue(other_project)
    transaction.commit()
    self.tic()
    self.assertEquals(3, len(invoice_transaction_applied_rule))
    invoice_transaction_movement = getIncomeSimulationMovement(
                                        invoice_transaction_applied_rule)
    self.assertEquals(other_project,
                 invoice_transaction_movement.getSourceProjectValue())
1581

1582 1583 1584 1585 1586 1587 1588 1589
    order_line.setDestinationProjectValue(other_project)
    transaction.commit()
    self.tic()
    self.assertEquals(3, len(invoice_transaction_applied_rule))
    invoice_transaction_movement = getIncomeSimulationMovement(
                                        invoice_transaction_applied_rule)
    self.assertEquals(other_project,
            invoice_transaction_movement.getDestinationProjectValue())
1590

1591 1592 1593 1594 1595 1596 1597 1598
    order_line.setSourceFunctionValue(other_entity)
    transaction.commit()
    self.tic()
    self.assertEquals(3, len(invoice_transaction_applied_rule))
    invoice_transaction_movement = getIncomeSimulationMovement(
                                        invoice_transaction_applied_rule)
    self.assertEquals(other_entity,
                 invoice_transaction_movement.getSourceFunctionValue())
1599

1600 1601 1602 1603 1604 1605 1606 1607
    order_line.setDestinationFunctionValue(other_entity)
    transaction.commit()
    self.tic()
    self.assertEquals(3, len(invoice_transaction_applied_rule))
    invoice_transaction_movement = getIncomeSimulationMovement(
                                        invoice_transaction_applied_rule)
    self.assertEquals(other_entity,
            invoice_transaction_movement.getDestinationFunctionValue())
1608

1609 1610 1611 1612 1613 1614 1615 1616
    order_line.setSourcePaymentValue(other_entity)
    transaction.commit()
    self.tic()
    self.assertEquals(3, len(invoice_transaction_applied_rule))
    invoice_transaction_movement = getIncomeSimulationMovement(
                                        invoice_transaction_applied_rule)
    self.assertEquals(other_entity,
                 invoice_transaction_movement.getSourcePaymentValue())
1617

1618 1619 1620 1621 1622 1623 1624 1625
    order_line.setDestinationPaymentValue(other_entity)
    transaction.commit()
    self.tic()
    self.assertEquals(3, len(invoice_transaction_applied_rule))
    invoice_transaction_movement = getIncomeSimulationMovement(
                                        invoice_transaction_applied_rule)
    self.assertEquals(other_entity,
            invoice_transaction_movement.getDestinationPaymentValue())
1626

1627 1628 1629 1630 1631 1632 1633 1634 1635
    order_line.setQuantity(1)
    order_line.setPrice(123)
    transaction.commit()
    self.tic()
    self.assertEquals(3, len(invoice_transaction_applied_rule))
    invoice_transaction_movement = getIncomeSimulationMovement(
                                        invoice_transaction_applied_rule)
    self.assertEquals(123,
            invoice_transaction_movement.getQuantity())
1636

1637 1638 1639 1640 1641 1642 1643 1644 1645
    order_line.setQuantity(456)
    order_line.setPrice(1)
    transaction.commit()
    self.tic()
    self.assertEquals(3, len(invoice_transaction_applied_rule))
    invoice_transaction_movement = getIncomeSimulationMovement(
                                        invoice_transaction_applied_rule)
    self.assertEquals(456,
            invoice_transaction_movement.getQuantity())
1646

1647 1648 1649 1650 1651 1652 1653 1654
    order_line.setStartDate(DateTime(2001, 02, 03))
    transaction.commit()
    self.tic()
    self.assertEquals(3, len(invoice_transaction_applied_rule))
    invoice_transaction_movement = getIncomeSimulationMovement(
                                        invoice_transaction_applied_rule)
    self.assertEquals(DateTime(2001, 02, 03),
                 invoice_transaction_movement.getStartDate())
1655

1656 1657 1658 1659 1660 1661 1662 1663
    order_line.setStopDate(DateTime(2002, 03, 04))
    transaction.commit()
    self.tic()
    self.assertEquals(3, len(invoice_transaction_applied_rule))
    invoice_transaction_movement = getIncomeSimulationMovement(
                                        invoice_transaction_applied_rule)
    self.assertEquals(DateTime(2002, 03, 04),
                 invoice_transaction_movement.getStopDate())
1664 1665


1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689
  def test_Invoice_viewAsODT(self):
    resource = self.portal.getDefaultModule(
        self.resource_portal_type).newContent(
                    portal_type=self.resource_portal_type,
                    title='Resource',)
    client = self.portal.organisation_module.newContent(
                              portal_type='Organisation', title='Client')
    vendor = self.portal.organisation_module.newContent(
                              portal_type='Organisation', title='Vendor')
    invoice = self.portal.getDefaultModule(self.invoice_portal_type).newContent(
                              portal_type=self.invoice_portal_type,
                              start_date=DateTime(2008, 12, 31),
                              title='Invoice',
                              source_value=vendor,
                              source_section_value=vendor,
                              destination_value=client,
                              destination_section_value=client)
    line = invoice.newContent(portal_type=self.invoice_line_portal_type,
                            resource_value=resource,
                            quantity=10,
                            price=3)
    invoice.confirm()
    transaction.commit()
    self.tic()
1690

1691 1692 1693 1694 1695 1696
    odt = invoice.Invoice_viewAsODT()
    from Products.ERP5OOo.tests.utils import Validator
    odf_validator = Validator()
    err_list = odf_validator.validate(odt)
    if err_list:
      self.fail(''.join(err_list))
1697

1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728
  def test_Invoice_viewAsODT_empty_image(self):
    resource = self.portal.getDefaultModule(
        self.resource_portal_type).newContent(
                    portal_type=self.resource_portal_type,
                    title='Resource',)
    client = self.portal.organisation_module.newContent(
                              portal_type='Organisation', title='Client')
    client_logo = client.newContent(portal_type='Image',
                                    id='default_image')
    vendor = self.portal.organisation_module.newContent(
                              portal_type='Organisation', title='Vendor')
    vendor_logo = vendor.newContent(portal_type='Image',
                                    id='default_image')
    self.assertEquals(0, vendor_logo.getSize())
    self.assertEquals(0, vendor.getDefaultImageWidth())
    self.assertEquals(0, vendor.getDefaultImageHeight())
    invoice = self.portal.getDefaultModule(self.invoice_portal_type).newContent(
                              portal_type=self.invoice_portal_type,
                              start_date=DateTime(2008, 12, 31),
                              title='Invoice',
                              source_value=vendor,
                              source_section_value=vendor,
                              destination_value=client,
                              destination_section_value=client)
    line = invoice.newContent(portal_type=self.invoice_line_portal_type,
                            resource_value=resource,
                            quantity=10,
                            price=3)
    invoice.confirm()
    transaction.commit()
    self.tic()
1729

1730 1731 1732 1733 1734 1735
    odt = invoice.Invoice_viewAsODT()
    from Products.ERP5OOo.tests.utils import Validator
    odf_validator = Validator()
    err_list = odf_validator.validate(odt)
    if err_list:
      self.fail(''.join(err_list))
1736

1737 1738 1739 1740 1741
    # the <draw:image> should not be present, because there's no logo
    parser = OOoParser()
    parser.openFromString(odt)
    style_xml = parser.oo_files['styles.xml']
    self.assert_('<draw:image' not in style_xml)
1742

1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758
  def test_Invoice_viewAsODT_invalid_image(self):
    resource = self.portal.getDefaultModule(
        self.resource_portal_type).newContent(
                    portal_type=self.resource_portal_type,
                    title='Resource',)
    file_data = FileUpload(__file__, 'rb')
    client = self.portal.organisation_module.newContent(
                              portal_type='Organisation', title='Client')
    client_logo = client.newContent(portal_type='Image',
                                    id='default_image',
                                    file=file_data)
    vendor = self.portal.organisation_module.newContent(
                              portal_type='Organisation', title='Vendor')
    vendor_logo = vendor.newContent(portal_type='Image',
                                    id='default_image',
                                    file=file_data)
1759

1760 1761 1762 1763
    # width and height of an invalid image are -1 according to
    # OFS.Image.getImageInfo maybe this is not what we want here ?
    self.assertEquals(-1, vendor.getDefaultImageWidth())
    self.assertEquals(-1, vendor.getDefaultImageHeight())
1764

1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779
    invoice = self.portal.getDefaultModule(self.invoice_portal_type).newContent(
                              portal_type=self.invoice_portal_type,
                              start_date=DateTime(2008, 12, 31),
                              title='Invoice',
                              source_value=vendor,
                              source_section_value=vendor,
                              destination_value=client,
                              destination_section_value=client)
    line = invoice.newContent(portal_type=self.invoice_line_portal_type,
                            resource_value=resource,
                            quantity=10,
                            price=3)
    invoice.confirm()
    transaction.commit()
    self.tic()
1780

1781 1782 1783 1784 1785 1786
    odt = invoice.Invoice_viewAsODT()
    from Products.ERP5OOo.tests.utils import Validator
    odf_validator = Validator()
    err_list = odf_validator.validate(odt)
    if err_list:
      self.fail(''.join(err_list))
1787

1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798
  def test_invoice_building_with_cells(self):
    # if the order has cells, the invoice built from that order must have
    # cells too
    resource = self.portal.getDefaultModule(
        self.resource_portal_type).newContent(
                    portal_type=self.resource_portal_type,
                    title='Resource',
                    variation_base_category_list=['size'])
    currency = self.portal.currency_module.newContent(
                                portal_type='Currency',
                                title='Currency')
1799

1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814
    client = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
                            title='Client')
    vendor = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
                            title='Vendor')
    order = self.portal.getDefaultModule(self.order_portal_type).newContent(
                              portal_type=self.order_portal_type,
                              source_value=vendor,
                              source_section_value=vendor,
                              destination_value=client,
                              destination_section_value=client,
                              start_date=DateTime(2008, 1, 1),
                              price_currency_value=currency,
                              title='Order')
1815

1816 1817 1818 1819 1820
    order_line = order.newContent(portal_type=self.order_line_portal_type,
                                  resource_value=resource,)
    order_line.setVariationBaseCategoryList(('size', ))
    order_line.setVariationCategoryList(['size/Baby', 'size/Child/32'])
    order_line.updateCellRange()
1821

1822 1823 1824 1825 1826 1827
    cell_baby = order_line.newCell('size/Baby', base_id='movement',
                             portal_type=self.order_cell_portal_type)
    cell_baby.edit(quantity=10,
                   price=4,
                   variation_category_list=['size/Baby'],
                   mapped_value_property_list=['quantity', 'price'],)
1828

1829 1830 1831 1832 1833 1834 1835 1836 1837
    cell_child_32 = order_line.newCell('size/Child/32', base_id='movement',
                                 portal_type=self.order_cell_portal_type)
    cell_child_32.edit(quantity=20,
                       price=5,
                       variation_category_list=['size/Child/32'],
                       mapped_value_property_list=['quantity', 'price'],)
    order.confirm()
    transaction.commit()
    self.tic()
1838

1839 1840 1841
    related_packing_list = order.getCausalityRelatedValue(
                                  portal_type=self.packing_list_portal_type)
    self.assertNotEquals(related_packing_list, None)
1842

1843 1844 1845 1846
    related_packing_list.start()
    related_packing_list.stop()
    transaction.commit()
    self.tic()
1847

1848 1849 1850
    related_invoice = related_packing_list.getCausalityRelatedValue(
                                  portal_type=self.invoice_portal_type)
    self.assertNotEquals(related_invoice, None)
1851

1852 1853 1854 1855
    line_list = related_invoice.contentValues(
                     portal_type=self.invoice_line_portal_type)
    self.assertEquals(1, len(line_list))
    invoice_line = line_list[0]
1856

1857 1858 1859 1860
    self.assertEquals(resource, invoice_line.getResourceValue())
    self.assertEquals(['size'], invoice_line.getVariationBaseCategoryList())
    self.assertEquals(2,
          len(invoice_line.getCellValueList(base_id='movement')))
1861

1862 1863 1864 1865 1866 1867 1868 1869
    cell_baby = invoice_line.getCell('size/Baby', base_id='movement')
    self.assertNotEquals(cell_baby, None)
    self.assertEquals(resource, cell_baby.getResourceValue())
    self.assertEquals(10, cell_baby.getQuantity())
    self.assertEquals(4, cell_baby.getPrice())
    self.assertTrue('size/Baby' in
                    cell_baby.getVariationCategoryList())
    self.assertTrue(cell_baby.isMemberOf('size/Baby'))
1870

1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893
    cell_child_32 = invoice_line.getCell('size/Child/32', base_id='movement')
    self.assertNotEquals(cell_child_32, None)
    self.assertEquals(resource, cell_child_32.getResourceValue())
    self.assertEquals(20, cell_child_32.getQuantity())
    self.assertEquals(5, cell_child_32.getPrice())
    self.assertTrue('size/Child/32' in
                    cell_child_32.getVariationCategoryList())
    self.assertTrue(cell_child_32.isMemberOf('size/Child/32'))
  
  
  
  def test_invoice_created_from_packing_list_with_no_order(self):
    # if the order has cells and an aggregate, the invoice built
    #from that order must have
    # cells too
    resource = self.portal.getDefaultModule(
        self.resource_portal_type).newContent(
                    portal_type=self.resource_portal_type,
                    title='Resource',
                    variation_base_category_list=['size'])
    currency = self.portal.currency_module.newContent(
                                portal_type='Currency',
                                title='Currency')
1894

1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910
    client = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
                            title='Client')
    vendor = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
                            title='Vendor')
    no_order_packing_list = \
self.portal.getDefaultModule(self.packing_list_portal_type).newContent(
                              portal_type=self.packing_list_portal_type,
                              source_value=vendor,
                              source_section_value=vendor,
                              destination_value=client,
                              destination_section_value=client,
                              start_date=DateTime(2008, 1, 1),
                              price_currency_value=currency,
                              title='Order')
1911

1912 1913 1914 1915 1916 1917
    packing_list_line = no_order_packing_list.newContent(
                        portal_type=self.packing_list_line_portal_type,
                                  resource_value=resource,)
    packing_list_line.setVariationBaseCategoryList(('size', ))
    packing_list_line.setVariationCategoryList(['size/Baby', 'size/Child/32'])
    packing_list_line.updateCellRange()
1918

1919 1920 1921 1922 1923 1924
    cell_baby = packing_list_line.newCell('size/Baby', base_id='movement',
                             portal_type=self.packing_list_cell_portal_type)
    cell_baby.edit(quantity=10,
                   price=4,
                   variation_category_list=['size/Baby'],
                   mapped_value_property_list=['quantity', 'price'],)
1925

1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936
    cell_child_32 = packing_list_line.newCell(
                                'size/Child/32',base_id='movement',
                                 portal_type=self.packing_list_cell_portal_type)
    cell_child_32.edit(quantity=20,
                       price=5,
                       variation_category_list=['size/Child/32'],
                       mapped_value_property_list=['quantity', 'price'],)
    no_order_packing_list.confirm()
    transaction.commit()
    self.tic()
    self.assertNotEquals(no_order_packing_list, None)
1937

1938 1939 1940 1941
    no_order_packing_list.start()
    no_order_packing_list.stop()
    transaction.commit()
    self.tic()
1942

1943 1944 1945
    related_invoice = no_order_packing_list.getCausalityRelatedValue(
                                  portal_type=self.invoice_portal_type)
    self.assertNotEquals(related_invoice, None)
1946

1947 1948 1949 1950
    line_list = related_invoice.contentValues(
                     portal_type=self.invoice_line_portal_type)
    self.assertEquals(1, len(line_list))
    invoice_line = line_list[0]
1951

1952 1953 1954 1955
    self.assertEquals(resource, invoice_line.getResourceValue())
    self.assertEquals(['size'], invoice_line.getVariationBaseCategoryList())
    self.assertEquals(2,
          len(invoice_line.getCellValueList(base_id='movement')))
1956

1957 1958 1959 1960 1961 1962 1963 1964
    cell_baby = invoice_line.getCell('size/Baby', base_id='movement')
    self.assertNotEquals(cell_baby, None)
    self.assertEquals(resource, cell_baby.getResourceValue())
    self.assertEquals(10, cell_baby.getQuantity())
    self.assertEquals(4, cell_baby.getPrice())
    self.assertTrue('size/Baby' in
                    cell_baby.getVariationCategoryList())
    self.assertTrue(cell_baby.isMemberOf('size/Baby'))
1965

1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986
    cell_child_32 = invoice_line.getCell('size/Child/32', base_id='movement')
    self.assertNotEquals(cell_child_32, None)
    self.assertEquals(resource, cell_child_32.getResourceValue())
    self.assertEquals(20, cell_child_32.getQuantity())
    self.assertEquals(5, cell_child_32.getPrice())
    self.assertTrue('size/Child/32' in
                    cell_child_32.getVariationCategoryList())
    self.assertTrue(cell_child_32.isMemberOf('size/Child/32'))
    
  def test_invoice_building_with_cells_and_aggregate(self):
    # if the order has cells and an aggregate, the invoice built
    #from that order must have
    # cells too
    resource = self.portal.getDefaultModule(
        self.resource_portal_type).newContent(
                    portal_type=self.resource_portal_type,
                    title='Resource',
                    variation_base_category_list=['size'])
    currency = self.portal.currency_module.newContent(
                                portal_type='Currency',
                                title='Currency')
1987

1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002
    client = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
                            title='Client')
    vendor = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
                            title='Vendor')
    order = self.portal.getDefaultModule(self.order_portal_type).newContent(
                              portal_type=self.order_portal_type,
                              source_value=vendor,
                              source_section_value=vendor,
                              destination_value=client,
                              destination_section_value=client,
                              start_date=DateTime(2008, 1, 1),
                              price_currency_value=currency,
                              title='Order')
2003

2004 2005 2006 2007 2008
    order_line = order.newContent(portal_type=self.order_line_portal_type,
                                  resource_value=resource,)
    order_line.setVariationBaseCategoryList(('size', ))
    order_line.setVariationCategoryList(['size/Baby', 'size/Child/32'])
    order_line.updateCellRange()
2009

2010 2011 2012 2013 2014 2015
    cell_baby = order_line.newCell('size/Baby', base_id='movement',
                             portal_type=self.order_cell_portal_type)
    cell_baby.edit(quantity=10,
                   price=4,
                   variation_category_list=['size/Baby'],
                   mapped_value_property_list=['quantity', 'price'],)
2016

2017 2018 2019 2020 2021 2022 2023 2024 2025
    cell_child_32 = order_line.newCell('size/Child/32', base_id='movement',
                                 portal_type=self.order_cell_portal_type)
    cell_child_32.edit(quantity=20,
                       price=5,
                       variation_category_list=['size/Child/32'],
                       mapped_value_property_list=['quantity', 'price'],)
    order.confirm()
    transaction.commit()
    self.tic()
2026

2027 2028 2029
    related_packing_list = order.getCausalityRelatedValue(
                                  portal_type=self.packing_list_portal_type)
    self.assertNotEquals(related_packing_list, None)
2030

2031 2032 2033 2034
    related_packing_list.start()
    related_packing_list.stop()
    transaction.commit()
    self.tic()
2035

2036 2037 2038
    related_invoice = related_packing_list.getCausalityRelatedValue(
                                  portal_type=self.invoice_portal_type)
    self.assertNotEquals(related_invoice, None)
2039

2040 2041 2042 2043
    line_list = related_invoice.contentValues(
                     portal_type=self.invoice_line_portal_type)
    self.assertEquals(1, len(line_list))
    invoice_line = line_list[0]
2044

2045 2046 2047 2048
    self.assertEquals(resource, invoice_line.getResourceValue())
    self.assertEquals(['size'], invoice_line.getVariationBaseCategoryList())
    self.assertEquals(2,
          len(invoice_line.getCellValueList(base_id='movement')))
2049

2050 2051 2052 2053 2054 2055 2056 2057
    cell_baby = invoice_line.getCell('size/Baby', base_id='movement')
    self.assertNotEquals(cell_baby, None)
    self.assertEquals(resource, cell_baby.getResourceValue())
    self.assertEquals(10, cell_baby.getQuantity())
    self.assertEquals(4, cell_baby.getPrice())
    self.assertTrue('size/Baby' in
                    cell_baby.getVariationCategoryList())
    self.assertTrue(cell_baby.isMemberOf('size/Baby'))
2058

2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082
    cell_child_32 = invoice_line.getCell('size/Child/32', base_id='movement')
    self.assertNotEquals(cell_child_32, None)
    self.assertEquals(resource, cell_child_32.getResourceValue())
    self.assertEquals(20, cell_child_32.getQuantity())
    self.assertEquals(5, cell_child_32.getPrice())
    self.assertTrue('size/Child/32' in
                    cell_child_32.getVariationCategoryList())
    self.assertTrue(cell_child_32.isMemberOf('size/Child/32'))
    
   
  def test_description_copied_on_lines(self):
    # if the order lines have different descriptions, description must be
    # copied in the simulation and on created movements
    resource = self.portal.getDefaultModule(
        self.resource_portal_type).newContent(
                    portal_type=self.resource_portal_type,
                    title='Resource',)
    resource2 = self.portal.getDefaultModule(
        self.resource_portal_type).newContent(
                    portal_type=self.resource_portal_type,
                    title='Resource2',)
    currency = self.portal.currency_module.newContent(
                                portal_type='Currency',
                                title='Currency')
2083

2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098
    client = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
                            title='Client')
    vendor = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
                            title='Vendor')
    order = self.portal.getDefaultModule(self.order_portal_type).newContent(
                              portal_type=self.order_portal_type,
                              source_value=vendor,
                              source_section_value=vendor,
                              destination_value=client,
                              destination_section_value=client,
                              start_date=DateTime(2008, 1, 1),
                              price_currency_value=currency,
                              title='Order')
2099

2100 2101 2102 2103 2104 2105 2106 2107 2108 2109
    order.newContent(portal_type=self.order_line_portal_type,
                                  quantity=3,
                                  price=10,
                                  description='The first line',
                                  resource_value=resource,)
    order.newContent(portal_type=self.order_line_portal_type,
                                  quantity=5,
                                  price=10,
                                  description='The second line',
                                  resource_value=resource2,)
2110

2111 2112 2113
    order.confirm()
    transaction.commit()
    self.tic()
2114

2115 2116 2117 2118 2119 2120 2121 2122 2123 2124
    related_packing_list = order.getCausalityRelatedValue(
                                  portal_type=self.packing_list_portal_type)
    self.assertNotEquals(related_packing_list, None)
    
    movement_list = related_packing_list.getMovementList()
    self.assertEquals(2, len(movement_list))
    self.assertEquals(['The first line'],
        [m.getDescription() for m in movement_list if m.getQuantity() == 3])
    self.assertEquals(['The second line'],
        [m.getDescription() for m in movement_list if m.getQuantity() == 5])
2125

2126 2127 2128 2129
    related_packing_list.start()
    related_packing_list.stop()
    transaction.commit()
    self.tic()
2130

2131 2132 2133
    related_invoice = related_packing_list.getCausalityRelatedValue(
                                  portal_type=self.invoice_portal_type)
    self.assertNotEquals(related_invoice, None)
2134

2135 2136 2137 2138 2139 2140 2141
    movement_list = related_invoice.getMovementList(
                              portal_type=self.invoice_line_portal_type)
    self.assertEquals(2, len(movement_list))
    self.assertEquals(['The first line'],
        [m.getDescription() for m in movement_list if m.getQuantity() == 3])
    self.assertEquals(['The second line'],
        [m.getDescription() for m in movement_list if m.getQuantity() == 5])
2142

2143 2144 2145 2146

  def test_CopyAndPaste(self):
    """Test copy on paste on Invoice.
    When an invoice is copy/pasted, references should be resetted.
2147
    """
2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164
    accounting_module = self.portal.accounting_module
    invoice = accounting_module.newContent(
                    portal_type=self.invoice_portal_type)
    invoice.edit(reference='reference',
                 source_reference='source_reference',
                 destination_reference='destination_reference',)
    cb_data = accounting_module.manage_copyObjects([invoice.getId()])
    copied, = accounting_module.manage_pasteObjects(cb_data)
    new_invoice = accounting_module[copied['new_id']]
    self.assertNotEquals(invoice.getReference(),
                         new_invoice.getReference())
    self.assertNotEquals(invoice.getSourceReference(),
                         new_invoice.getSourceReference())
    self.assertNotEquals(invoice.getDestinationReference(),
                         new_invoice.getDestinationReference())

  def test_delivery_mode_and_incoterm_on_invoice(self):
2165
    """
2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278
    test that categories delivery_mode and incoterm are copied on 
    the invoice by the delivery builder
    """ 
    resource = self.portal.product_module.newContent(
                    portal_type='Product',
                    title='Resource',
                    product_line='apparel')
    currency = self.portal.currency_module.newContent(
                                portal_type='Currency',
                                title='euro')
    currency.setBaseUnitQuantity(0.01)
    self.createInvoiceTransactionRule(currency)
    transaction.commit()
    self.tic()#execute transaction
    client = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
                            title='Client',
                            default_address_region=self.default_region)
    vendor = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
                            title='Vendor',
                            default_address_region=self.default_region)
    order = self.portal.getDefaultModule(self.order_portal_type).newContent(
                              portal_type=self.order_portal_type,
                              source_value=vendor,
                              source_section_value=vendor,
                              destination_value=client,
                              destination_section_value=client,
                              start_date=DateTime(2008,10, 21),
                              price_currency_value=currency,
                              delivery_mode=self.mail_delivery_mode,
                              incoterm=self.cpt_incoterm,
                              title='Order')
    order_line = order.newContent(portal_type=self.order_line_portal_type,
                                  resource_value=resource,
                                  quantity=5,
                                  price=2)
    order.confirm()
    transaction.commit()
    self.tic()
    related_packing_list = order.getCausalityRelatedValue(
                                portal_type=self.packing_list_portal_type)
    self.assertNotEquals(related_packing_list, None)
    self.assertEquals(related_packing_list.getDeliveryMode(),
                         order.getDeliveryMode())
    self.assertEquals(related_packing_list.getIncoterm(),
                         order.getIncoterm())
    related_packing_list.start()
    related_packing_list.stop()
    transaction.commit()
    self.tic()
    related_invoice = related_packing_list.getCausalityRelatedValue(
                                  portal_type=self.invoice_portal_type)
    self.assertNotEquals(related_invoice, None)
    self.assertEquals(related_invoice.getDeliveryMode(),
                         order.getDeliveryMode())
    self.assertEquals(related_invoice.getIncoterm(),
                         order.getIncoterm())
                         
                         
  def test_01_quantity_unit_copied(self):
    """
    tests that when a resource uses different quantity unit that the
    quantity units are copied on the packing list line and then the invoice
    line using the delivery builers
    """           
    resource = self.portal.product_module.newContent(
                    portal_type='Product',
                    title='Resource',
                    product_line='apparel')
    resource.setQuantityUnitList([self.unit_piece_quantity_unit,
                                 self.mass_quantity_unit])
    currency = self.portal.currency_module.newContent(
                                portal_type='Currency',
                                title='euro')
    currency.setBaseUnitQuantity(0.01)
    transaction.commit()
    self.tic()#execute transaction
    client = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
                            title='Client',
                            default_address_region=self.default_region)
    vendor = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
                            title='Vendor',
                            default_address_region=self.default_region)
    order = self.portal.getDefaultModule(self.order_portal_type).newContent(
                              portal_type=self.order_portal_type,
                              source_value=vendor,
                              source_section_value=vendor,
                              destination_value=client,
                              destination_section_value=client,
                              start_date=DateTime(2008,10, 21),
                              price_currency_value=currency,
                              delivery_mode=self.mail_delivery_mode,
                              incoterm=self.cpt_incoterm,
                              title='Order')
    first_order_line = order.newContent(
                          portal_type=self.order_line_portal_type,
                                  resource_value=resource,
                             quantity_unit = self.unit_piece_quantity_unit,
                                  quantity=5,
                                  price=3)
    second_order_line = order.newContent( 
                          portal_type=self.order_line_portal_type,
                                  resource_value=resource,
                             quantity_unit=self.mass_quantity_unit,
                                  quantity=1.5,
                                  price=2)
    self.assertEquals(first_order_line.getQuantityUnit(),
                      self.unit_piece_quantity_unit)
    self.assertEquals(second_order_line.getQuantityUnit(),
                      self.mass_quantity_unit)
2279

2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294
    order.confirm()
    transaction.commit()
    self.tic()
    related_packing_list = order.getCausalityRelatedValue(
                                portal_type=self.packing_list_portal_type)
    self.assertNotEquals(related_packing_list, None)
    movement_list = related_packing_list.getMovementList()
    self.assertEquals(len(movement_list),2)
    movement_list = sorted(movement_list, key=lambda x: x.getQuantity())
    self.assertEquals(movement_list[0].getQuantityUnit(),
                      self.mass_quantity_unit)
    self.assertEquals(movement_list[0].getQuantity(), 1.5)
    self.assertEquals(movement_list[1].getQuantityUnit(),
                      self.unit_piece_quantity_unit)
    self.assertEquals(movement_list[1].getQuantity(), 5)
2295

2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312
    related_packing_list.start()
    related_packing_list.stop()
    related_packing_list.deliver()
    transaction.commit()
    self.tic()
    related_invoice = related_packing_list.getCausalityRelatedValue(
                                portal_type=self.invoice_portal_type)
    self.assertNotEquals(related_invoice, None)
    movement_list = related_invoice.getMovementList()
    self.assertEquals(len(movement_list),2)
    movement_list = sorted(movement_list, key=lambda x: x.getQuantity())
    self.assertEquals(movement_list[0].getQuantityUnit(),
                      self.mass_quantity_unit)
    self.assertEquals(movement_list[0].getQuantity(), 1.5)
    self.assertEquals(movement_list[1].getQuantityUnit(),
                      self.unit_piece_quantity_unit)
    self.assertEquals(movement_list[1].getQuantity(), 5)
2313

2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327
 

  def _acceptDivergenceOnInvoice(self, invoice, divergence_list):
    builder_list = invoice.getBuilderList()
    self.assertEquals(2, len(builder_list))
    for builder in builder_list:
      builder.solveDivergence(invoice.getRelativeUrl(),
                              divergence_to_accept_list=divergence_list)

  def test_accept_quantity_divergence_on_invoice_with_stopped_packing_list(
                self, quiet=quiet):
    sequence_list = SequenceList()
    sequence = sequence_list.addSequenceString(self.PACKING_LIST_DEFAULT_SEQUENCE)
    sequence_list.play(self, quiet=quiet)
2328

2329
    packing_list = sequence.get('packing_list')
2330 2331 2332 2333 2334 2335 2336 2337 2338
    packing_list_line = packing_list.getMovementList()[0]
    previous_quantity = packing_list_line.getQuantity()
    
    packing_list.setReady()
    packing_list.start()
    packing_list.stop()
    self.assertEquals('stopped', packing_list.getSimulationState())
    transaction.commit()
    self.tic()
2339

2340 2341 2342 2343 2344 2345
    invoice = packing_list.getCausalityRelatedValue(
                                  portal_type=self.invoice_portal_type)
    self.assertNotEquals(invoice, None)
    invoice_line_list = invoice.getMovementList()
    self.assertEquals(1, len(invoice_line_list))
    invoice_line = invoice_line_list[0]
2346

2347 2348 2349 2350 2351
    new_quantity = invoice_line.getQuantity() * 2
    invoice_line.setQuantity(new_quantity)
    
    transaction.commit()
    self.tic()
2352

2353 2354 2355
    self.assertTrue(invoice.isDivergent())
    divergence_list = invoice.getDivergenceList()
    self.assertEquals(1, len(divergence_list))
2356

2357 2358
    divergence = divergence_list[0]
    self.assertEquals('quantity', divergence.tested_property)
2359

2360 2361
    # accept decision
    self._acceptDivergenceOnInvoice(invoice, divergence_list)
2362

2363 2364 2365
    transaction.commit()
    self.tic()
    self.assertEquals('solved', invoice.getCausalityState())
2366

2367 2368 2369 2370 2371
    self.assertEquals([], invoice.getDivergenceList())
    self.assertEquals(new_quantity, invoice_line.getQuantity())
    self.assertEquals(new_quantity,
          invoice_line.getDeliveryRelatedValue(portal_type='Simulation Movement'
              ).getQuantity())
2372

2373 2374 2375 2376 2377 2378 2379 2380 2381
    self.assertEquals([], packing_list.getDivergenceList())
    self.assertEquals('solved', packing_list.getCausalityState())
 
  def _adoptDivergenceOnInvoice(self, invoice, divergence_list):
    builder_list = invoice.getBuilderList()
    self.assertEquals(2, len(builder_list))
    for builder in builder_list:
      builder.solveDivergence(invoice.getRelativeUrl(),
                              divergence_to_adopt_list=divergence_list)
2382

2383 2384 2385 2386 2387 2388
  def test_adopt_quantity_divergence_on_invoice_line_with_stopped_packing_list(
                self, quiet=quiet):
    # #1053
    sequence_list = SequenceList()
    sequence = sequence_list.addSequenceString(self.PACKING_LIST_DEFAULT_SEQUENCE)
    sequence_list.play(self, quiet=quiet)
2389

2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401
    packing_list = sequence.get('packing_list')
    packing_list_line = packing_list.getMovementList()[0]
    previous_quantity = packing_list_line.getQuantity()
    previous_resource = packing_list_line.getResource()
    previous_price = packing_list_line.getPrice()
    
    packing_list.setReady()
    packing_list.start()
    packing_list.stop()
    self.assertEquals('stopped', packing_list.getSimulationState())
    transaction.commit()
    self.tic()
2402

2403 2404 2405 2406 2407 2408
    invoice = packing_list.getCausalityRelatedValue(
                                  portal_type=self.invoice_portal_type)
    self.assertNotEquals(invoice, None)
    invoice_line_list = invoice.getMovementList()
    self.assertEquals(1, len(invoice_line_list))
    invoice_line = invoice_line_list[0]
2409

2410 2411 2412 2413 2414
    new_quantity = invoice_line.getQuantity() * 2
    invoice_line.setQuantity(new_quantity)
    
    transaction.commit()
    self.tic()
2415

2416 2417 2418
    self.assertTrue(invoice.isDivergent())
    divergence_list = invoice.getDivergenceList()
    self.assertEquals(1, len(divergence_list))
2419

2420 2421
    divergence = divergence_list[0]
    self.assertEquals('quantity', divergence.tested_property)
2422

2423 2424
    # adopt prevision
    self._adoptDivergenceOnInvoice(invoice, divergence_list)
2425

2426 2427 2428 2429
    transaction.commit()
    self.tic()
    self.assertEquals([], invoice.getDivergenceList())
    self.assertEquals('solved', invoice.getCausalityState())
2430

2431 2432 2433 2434
    self.assertEquals(1,
        len(invoice.getMovementList(portal_type=self.invoice_line_portal_type)))
    self.assertEquals(0,
        len(invoice.getMovementList(portal_type=self.invoice_transaction_line_portal_type)))
2435

2436 2437 2438 2439 2440 2441
    self.assertEquals(previous_resource, invoice_line.getResource())
    self.assertEquals(previous_quantity, invoice_line.getQuantity())
    self.assertEquals(previous_price, invoice_line.getPrice())
    self.assertEquals(previous_quantity,
          invoice_line.getDeliveryRelatedValue(portal_type='Simulation Movement'
              ).getQuantity())
2442

2443 2444 2445
    self.assertEquals([], packing_list.getDivergenceList())
    self.assertEquals('solved', packing_list.getCausalityState())
 
2446

2447

2448 2449 2450
class TestSaleInvoiceMixin(TestInvoiceMixin,
                           ERP5TypeTestCase):
  """Test sale invoice are created from orders then packing lists.
2451

2452 2453 2454 2455 2456 2457 2458 2459 2460
    Those tests methods only work for sale, because sale and purchase invoice
    are not built at the same time on packing list workflow.
  """
  quiet = 0
  invoice_portal_type = 'Sale Invoice Transaction'
  invoice_line_portal_type = 'Invoice Line'
  invoice_cell_portal_type = 'Invoice Cell'
  invoice_transaction_line_portal_type = 'Sale Invoice Transaction Line'
  payment_portal_type = 'Payment Transaction'
2461

2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488
  # default sequence for one line of not varianted resource.
  PACKING_LIST_DEFAULT_SEQUENCE = """
      stepCreateEntities
      stepCreateCurrency
      stepCreateSaleInvoiceTransactionRule
      stepCreateOrder
      stepSetOrderProfile
      stepSetOrderPriceCurrency
      stepCreateNotVariatedResource
      stepTic
      stepCreateOrderLine
      stepSetOrderLineResource
      stepSetOrderLineDefaultValues
      stepOrderOrder
      stepTic
      stepCheckDeliveryBuilding
      stepConfirmOrder
      stepTic
      stepCheckOrderRule
      stepCheckOrderSimulation
      stepCheckDeliveryBuilding
      stepAddPackingListContainer
      stepAddPackingListContainerLine
      stepSetContainerLineFullQuantity
      stepTic
      stepCheckPackingListIsPacked
    """
2489

2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522
  # default sequence for two lines of not varianted resource.
  PACKING_LIST_TWO_LINES_DEFAULT_SEQUENCE = """
      stepCreateEntities
      stepCreateCurrency
      stepCreateSaleInvoiceTransactionRule
      stepCreateOrder
      stepSetOrderProfile
      stepSetOrderPriceCurrency
      stepCreateNotVariatedResource
      stepTic
      stepCreateOrderLine
      stepSetOrderLineResource
      stepSetOrderLineDefaultValues
      stepCreateNotVariatedResource
      stepTic
      stepCreateOrderLine
      stepSetOrderLineResource
      stepSetOrderLineDefaultValues
      stepOrderOrder
      stepTic
      stepCheckDeliveryBuilding
      stepConfirmOrder
      stepTic
      stepCheckOrderRule
      stepCheckOrderSimulation
      stepCheckDeliveryBuilding
      stepAddPackingListContainer
      stepAddPackingListContainerLine
      stepTic
      stepSetContainerFullQuantity
      stepTic
      stepCheckPackingListIsPacked
    """
2523

2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561
  # default sequence for one line of not varianted resource.
  TWO_PACKING_LIST_DEFAULT_SEQUENCE = """
      stepCreateEntities
      stepCreateCurrency
      stepCreateSaleInvoiceTransactionRule
      stepCreateOrder
      stepSetOrderProfile
      stepSetOrderPriceCurrency
      stepCreateNotVariatedResource
      stepTic
      stepCreateOrderLine
      stepSetOrderLineResource
      stepSetOrderLineDefaultValues
      stepOrderOrder
      stepTic
      stepCheckDeliveryBuilding
      stepConfirmOrder
      stepTic
      stepCheckOrderRule
      stepCheckOrderSimulation
      stepCheckDeliveryBuilding
      stepDecreasePackingListLineQuantity
      stepCheckPackingListIsCalculating
      stepTic
      stepCheckPackingListIsDiverged
      stepSplitAndDeferPackingList
      stepTic
      stepCheckPackingListIsSolved
      stepCheckPackingListSplitted
      stepAddPackingListContainer
      stepAddPackingListContainerLine
      stepSetContainerLineFullQuantity
      stepTic
      stepCheckPackingListIsPacked
      stepDefineNewPackingListContainer
      stepTic
      stepCheckNewPackingListIsPacked
    """
2562

2563
class TestSaleInvoice(TestSaleInvoiceMixin, TestInvoice, ERP5TypeTestCase):
2564 2565
  """Tests for sale invoice.
  """
2566
  quiet = 0
2567 2568 2569

  # fix inheritance
  login = TestInvoiceMixin.login
2570

2571 2572
  @UnrestrictedMethod
  def createCategories(self):
2573
    TestPackingListMixin.createCategories(self)
2574
    TestInvoiceMixin.createCategories(self)
2575 2576 2577

  getNeededCategoryList = TestInvoiceMixin.getNeededCategoryList

2578
  def test_01_SimpleInvoice(self, quiet=quiet):
2579
    """
2580
    Checks that a Simple Invoice is created from a Packing List
2581 2582
    """
    if not quiet:
2583
      self.logMessage('Simple Invoice')
2584 2585 2586
    sequence_list = SequenceList()
    for base_sequence in (self.PACKING_LIST_DEFAULT_SEQUENCE, ) :
      sequence_list.addSequenceString(
2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598
        base_sequence +
      """
        stepSetReadyPackingList
        stepTic
        stepStartPackingList
        stepCheckInvoicingRule
        stepTic
        stepCheckInvoiceBuilding
        stepRebuildAndCheckNothingIsCreated
        stepCheckInvoicesConsistency
        stepCheckInvoiceLineHasReferenceAndIntIndex
      """)
2599 2600
    sequence_list.play(self, quiet=quiet)

2601
  def test_02_TwoInvoicesFromTwoPackingList(self, quiet=quiet):
2602
    """
2603 2604 2605 2606 2607 2608 2609 2610
    This test was created for the following bug:
        - an order is created and confirmed
        - the packing list is split
        - the 2 packing list are delivered (at different date)
        - 2 invoices are built, then we set the same date on both of them
        - the accounting rules are generated and put in only one invoice !!,
          so we have an invoice with twice the number of accounting rules
          and an invoice with no accounting rules. both invoices are wrong
2611
    """
2612
    if not quiet: self.logMessage('Two Invoices from Two Packing List')
2613
    sequence_list = SequenceList()
2614
    for base_sequence in (self.TWO_PACKING_LIST_DEFAULT_SEQUENCE, ) :
2615
      sequence_list.addSequenceString(
2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630
        base_sequence +
      """
        stepSetReadyPackingList
        stepSetReadyNewPackingList
        stepTic
        stepStartPackingList
        stepStartNewPackingList
        stepTic
        stepCheckTwoInvoices
        stepRemoveDateMovementGroupForTransactionBuilder
        stepStartTwoInvoices
        stepTic
        stepCheckTwoInvoicesTransactionLines
        stepCheckInvoicesConsistency
      """)
2631 2632
    sequence_list.play(self, quiet=quiet)

2633
  def test_03_InvoiceEditAndInvoiceRule(self, quiet=quiet):
2634
    """
2635 2636 2637 2638 2639 2640 2641 2642 2643
    Invoice Rule should not be applied on invoice lines created from\
    Packing List.

    We want to prevent this from happening:
      - Create a packing list
      - An invoice is created from packing list
      - Invoice is edited, updateAppliedRule is called
      - A new Invoice Rule is created for this invoice, and accounting
        movements for this invoice are present twice in the simulation.
2644 2645
    """
    if not quiet:
2646
      self.logMessage('Invoice Edit')
2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657
    sequence_list = SequenceList()
    for base_sequence in (self.PACKING_LIST_DEFAULT_SEQUENCE, ) :
      sequence_list.addSequenceString(
        base_sequence +
      """
        stepSetReadyPackingList
        stepTic
        stepStartPackingList
        stepCheckInvoicingRule
        stepTic
        stepCheckInvoiceBuilding
2658 2659
        stepEditInvoice
        stepCheckInvoiceRuleNotAppliedOnInvoiceEdit
2660 2661 2662 2663
        stepCheckInvoicesConsistency
      """)
    sequence_list.play(self, quiet=quiet)

2664
  def test_04_PackingListEditAndInvoiceRule(self, quiet=quiet):
2665
    """
2666 2667
    Delivery Rule should not be applied on packing list lines created\
    from Order.
2668
    """
2669 2670
    if not quiet:
      self.logMessage('Packing List Edit')
2671
    sequence_list = SequenceList()
2672 2673 2674 2675 2676 2677 2678 2679 2680 2681
    for base_sequence in (self.PACKING_LIST_DEFAULT_SEQUENCE, ) :
      sequence_list.addSequenceString(
        base_sequence +
      """
        stepEditPackingList
        stepCheckDeliveryRuleNotAppliedOnPackingListEdit
        stepCheckInvoicesConsistency
      """)
    sequence_list.play(self, quiet=quiet)

2682
  def test_05_InvoiceEditPackingListLine(self, quiet=quiet):
2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695
    """
    Checks that editing a Packing List Line still creates a correct
    Invoice
    """
    if not quiet:
      self.logMessage('Packing List Line Edit')
    sequence_list = SequenceList()
    for base_sequence in (self.PACKING_LIST_DEFAULT_SEQUENCE, ) :
      sequence_list.addSequenceString(
        base_sequence +
    """
      stepEditPackingListLine
      stepSetReadyPackingList
2696
      stepTic
2697 2698 2699 2700 2701 2702 2703 2704 2705
      stepStartPackingList
      stepCheckInvoicingRule
      stepTic
      stepCheckInvoiceBuilding
      stepRebuildAndCheckNothingIsCreated
      stepCheckInvoicesConsistency
    """)
    sequence_list.play(self, quiet=quiet)

2706
  def test_06_InvoiceDeletePackingListLine(self, quiet=quiet):
2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722
    """
    Checks that deleting a Packing List Line still creates a correct
    Invoice
    """
    if not quiet:
      self.logMessage('Packing List Line Delete')
    sequence_list = SequenceList()
    for base_sequence in (self.PACKING_LIST_TWO_LINES_DEFAULT_SEQUENCE, ) :
      sequence_list.addSequenceString(
        base_sequence +
    """
      stepDeletePackingListLine
      stepSetReadyPackingList
      stepTic
      stepStartPackingList
      stepCheckInvoicingRule
2723
      stepTic
2724 2725 2726 2727
      stepCheckInvoiceBuilding
      stepRebuildAndCheckNothingIsCreated
      stepCheckInvoicesConsistency
    """)
2728
    sequence_list.play(self, quiet=quiet)
2729

2730
  def test_07_InvoiceAddPackingListLine(self, quiet=quiet):
2731
    """
2732 2733
    Checks that adding a Packing List Line still creates a correct
    Invoice
2734
    """
2735 2736
    if not quiet:
      self.logMessage('Packing List Line Add')
2737
    sequence_list = SequenceList()
2738 2739 2740 2741 2742 2743 2744
    for base_sequence in (self.PACKING_LIST_DEFAULT_SEQUENCE,
        self.PACKING_LIST_TWO_LINES_DEFAULT_SEQUENCE) :
      sequence_list.addSequenceString(
        base_sequence +
    """
      stepAddPackingListLine
      stepSetContainerFullQuantity
2745
      stepTic
2746 2747 2748 2749
      stepSetReadyPackingList
      stepTic
      stepStartPackingList
      stepCheckInvoicingRule
2750
      stepTic
2751 2752 2753 2754
      stepCheckInvoiceBuilding
      stepRebuildAndCheckNothingIsCreated
      stepCheckInvoicesConsistency
    """)
2755
    sequence_list.play(self, quiet=quiet)
2756

2757
  def test_08_InvoiceDecreaseQuantity(self, quiet=quiet):
2758 2759 2760 2761 2762 2763
    """
    Change the quantity of a Invoice Line,
    check that the invoice is divergent,
    then split and defer, and check everything is solved
    """
    if not quiet:
Łukasz Nowak's avatar
Łukasz Nowak committed
2764
      self.logMessage('Invoice Decrease Quantity')
2765 2766 2767 2768 2769 2770 2771 2772 2773
    sequence = self.PACKING_LIST_DEFAULT_SEQUENCE + \
    """
    stepSetReadyPackingList
    stepTic
    stepStartPackingList
    stepCheckInvoicingRule
    stepCheckInvoiceTransactionRule
    stepTic
    stepCheckInvoiceBuilding
2774

2775 2776 2777
    stepDecreaseInvoiceLineQuantity
    stepCheckInvoiceIsDivergent
    stepCheckInvoiceIsCalculating
2778 2779
    stepTic
    stepCheckInvoiceIsDiverged
2780 2781
    stepSplitAndDeferInvoice
    stepTic
2782

2783 2784 2785
    stepCheckInvoiceIsNotDivergent
    stepCheckInvoiceIsSolved
    stepCheckInvoiceSplitted
2786

2787 2788 2789
    stepCheckPackingListIsNotDivergent
    stepCheckPackingListIsSolved
    stepCheckInvoiceTransactionRule
2790

2791 2792 2793 2794
    stepRebuildAndCheckNothingIsCreated
    stepCheckInvoicesConsistency
    """
    self.playSequence(sequence, quiet=quiet)
2795

2796
  def test_09_InvoiceChangeStartDateFail(self, quiet=quiet):
2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812
    """
    Change the start_date of a Invoice Line,
    check that the invoice is divergent,
    then accept decision, and check Packing list is divergent
    """
    if not quiet:
      self.logMessage('Invoice Change Sart Date')
    sequence = self.PACKING_LIST_DEFAULT_SEQUENCE + \
    """
    stepSetReadyPackingList
    stepTic
    stepStartPackingList
    stepCheckInvoicingRule
    stepCheckInvoiceTransactionRule
    stepTic
    stepCheckInvoiceBuilding
2813

2814 2815 2816
    stepChangeInvoiceStartDate
    stepCheckInvoiceIsDivergent
    stepCheckInvoiceIsCalculating
2817 2818
    stepTic
    stepCheckInvoiceIsDiverged
2819
    stepUnifyStartDateWithDecisionInvoice
2820
    stepTic
2821

2822 2823 2824
    stepCheckInvoiceNotSplitted
    stepCheckInvoiceIsNotDivergent
    stepCheckInvoiceIsSolved
2825

2826 2827 2828
    stepCheckPackingListIsDivergent
    """
    self.playSequence(sequence, quiet=quiet)
2829

2830
  def test_09b_InvoiceChangeStartDateSucceed(self, quiet=quiet):
2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851
    """
    Change the start_date of a Invoice Line,
    check that the invoice is divergent,
    deliver the Packing List to make sure it's frozen,
    then accept decision, and check everything is solved
    """
    if not quiet:
      self.logMessage('Invoice Change Sart Date')
    sequence = self.PACKING_LIST_DEFAULT_SEQUENCE + \
    """
    stepSetReadyPackingList
    stepTic
    stepStartPackingList
    stepCheckInvoicingRule
    stepCheckInvoiceTransactionRule
    stepTic
    stepCheckInvoiceBuilding
    stepStopPackingList
    stepTic
    stepDeliverPackingList
    stepTic
2852

2853 2854 2855
    stepChangeInvoiceStartDate
    stepCheckInvoiceIsDivergent
    stepCheckInvoiceIsCalculating
2856 2857
    stepTic
    stepCheckInvoiceIsDiverged
2858
    stepUnifyStartDateWithDecisionInvoice
2859
    stepTic
2860

2861 2862 2863
    stepCheckInvoiceNotSplitted
    stepCheckInvoiceIsNotDivergent
    stepCheckInvoiceIsSolved
2864

2865 2866 2867 2868 2869 2870 2871 2872 2873
    stepCheckPackingListIsNotDivergent
    stepCheckPackingListIsSolved
    stepCheckInvoiceTransactionRule

    stepRebuildAndCheckNothingIsCreated
    stepCheckInvoicesConsistency
    """
    self.playSequence(sequence, quiet=quiet)

2874
  def test_10_AcceptDecisionOnPackingList(self, quiet=quiet):
2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886
    """
    - Increase or Decrease the quantity of a Packing List line
    - Accept Decision on Packing List
    - Packing List must not be divergent and use new quantity
    - Invoice must not be divergent and use new quantity
    """
    if not quiet:
      self.logMessage('InvoiceAcceptDecisionOnPackingList')
    end_sequence = \
    """
    stepSetContainerFullQuantity
    stepCheckPackingListIsCalculating
2887 2888
    stepTic
    stepCheckPackingListIsDiverged
2889
    stepAcceptDecisionQuantity
2890 2891 2892
    stepTic
    stepCheckPackingListIsSolved
    stepCheckPackingListNotSplitted
2893

2894 2895 2896 2897 2898 2899 2900
    stepSetReadyPackingList
    stepTic
    stepStartPackingList
    stepCheckInvoicingRule
    stepCheckInvoiceTransactionRule
    stepTic
    stepCheckInvoiceBuilding
2901

2902 2903 2904 2905 2906 2907 2908
    stepStopPackingList
    stepTic
    stepDeliverPackingList
    stepTic
    stepCheckPackingListIsNotDivergent
    stepCheckPackingListIsSolved
    stepCheckInvoiceTransactionRule
2909

2910 2911 2912 2913 2914 2915 2916 2917 2918
    stepStartInvoice
    stepTic
    stepStopInvoice
    stepTic
    stepDeliverInvoice
    stepTic
    stepCheckInvoiceNotSplitted
    stepCheckInvoiceIsNotDivergent
    stepCheckInvoiceIsSolved
2919

2920 2921 2922
    stepRebuildAndCheckNothingIsCreated
    stepCheckInvoicesConsistency
    """
2923

2924 2925 2926 2927 2928 2929 2930
    mid_sequence_list = ["""
    stepCheckInvoicingRule
    stepDecreasePackingListLineQuantity
    """, """
    stepCheckInvoicingRule
    stepIncreasePackingListLineQuantity
    """]
2931

2932 2933 2934 2935 2936 2937
    sequence_list = SequenceList()
    for seq in mid_sequence_list:
      sequence = self.PACKING_LIST_DEFAULT_SEQUENCE + \
          seq + end_sequence
      sequence_list.addSequenceString(sequence)
    sequence_list.play(self, quiet=quiet)
2938

2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971
  def test_16a_ManuallyAddedMovementsManyTransactions(self, quiet=quiet):
    """
    Checks that adding invoice lines and accounting lines to one invoice
    generates correct simulation

    In this case checks what is happening, where movements are added in
    one transaction and edited in another
    """
    if not quiet:
      self.logMessage('Invoice with Manually Added Movements in separate transactions')
    sequence_list = SequenceList()
    for base_sequence in (self.PACKING_LIST_DEFAULT_SEQUENCE, ) :
      sequence_list.addSequenceString(
          base_sequence +
          """
          stepSetReadyPackingList
          stepTic
          stepStartPackingList
          stepCheckInvoicingRule
          stepTic
          stepCheckInvoiceBuilding
          stepRebuildAndCheckNothingIsCreated
          stepCheckInvoicesConsistency
          stepAddInvoiceLinesManyTransactions
          stepTic
          stepCheckInvoiceIsSolved
          stepStartInvoice
          stepTic
          stepCheckSimulationTrees
          """)
    sequence_list.play(self, quiet=quiet)


2972
  def test_11_AcceptDecisionOnPackingListAndInvoice(self, quiet=quiet):
2973
    """
2974 2975 2976 2977 2978 2979 2980
    - Increase or Decrease the quantity of a Packing List line
    - Accept Decision on Packing List
    - Packing List must not be divergent and use new quantity
    - Put old quantity on Invoice
    - Accept Decision on Invoice
    - Packing List must not be divergent and use new quantity
    - Invoice must not be divergent and use old quantity
2981
    """
2982 2983 2984 2985 2986 2987
    if not quiet:
      self.logMessage('InvoiceAcceptDecisionOnPackingListAndInvoice')
    mid_sequence = \
    """
    stepSetContainerFullQuantity
    stepCheckPackingListIsCalculating
2988 2989
    stepTic
    stepCheckPackingListIsDiverged
2990
    stepAcceptDecisionQuantity
2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013
    stepTic
    stepCheckPackingListIsSolved
    stepCheckPackingListNotSplitted

    stepSetReadyPackingList
    stepTic
    stepStartPackingList
    stepCheckInvoicingRule
    stepCheckInvoiceTransactionRule
    stepTic
    stepCheckInvoiceBuilding

    stepStopPackingList
    stepTic
    stepDeliverPackingList
    stepTic
    stepCheckPackingListIsNotDivergent
    stepCheckPackingListIsSolved
    stepCheckInvoiceTransactionRule
    """
    end_sequence = \
    """
    stepCheckInvoiceIsDiverged
3014
    stepAcceptDecisionQuantityInvoice
3015
    stepTic
3016 3017
    stepCheckInvoiceIsNotDivergent
    stepCheckInvoiceIsSolved
3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048
    stepStartInvoice
    stepTic
    stepStopInvoice
    stepTic
    stepDeliverInvoice
    stepTic
    stepCheckPackingListIsNotDivergent
    stepCheckPackingListIsSolved
    stepCheckInvoiceTransactionRule
    stepCheckInvoiceNotSplitted
    stepCheckInvoiceIsNotDivergent
    stepCheckInvoiceIsSolved

    stepRebuildAndCheckNothingIsCreated
    stepCheckInvoicesConsistency
    """

    mid_sequence_list = [("""
    stepCheckInvoicingRule
    stepDecreasePackingListLineQuantity
    """, """
    stepIncreaseInvoiceLineQuantity
    stepTic
    """), ("""
    stepCheckInvoicingRule
    stepIncreasePackingListLineQuantity
    """, """
    stepDecreaseInvoiceLineQuantity
    stepTic
    """)]

3049
    sequence_list = SequenceList()
3050 3051 3052 3053
    for seq1, seq2 in mid_sequence_list:
      sequence = self.PACKING_LIST_DEFAULT_SEQUENCE + \
          seq1 + mid_sequence + seq2 + end_sequence
      sequence_list.addSequenceString(sequence)
3054
    sequence_list.play(self, quiet=quiet)
3055

3056
  def test_12_SplitPackingListAndAcceptInvoice(self, quiet=quiet):
3057 3058 3059 3060 3061
    """
    - Decrease the quantity of a Packing List line
    - Split and Defer on Packing List
    - Packing List must not be divergent and use new quantity
    - splitted Packing List must not be divergent and use old - new quantity
3062

3063 3064 3065 3066 3067
    - Put old quantity on Invoice1
    - Accept Decision on Invoice1
    - Packing List must not be divergent and use new quantity
    - splitted Packing List must not be divergent and use old - new quantity
    - Invoice1 must not be divergent and use old quantity
3068

3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083
    - set Invoice2 quantity to 0
    - Accept Decision on Invoice2
    - Packing List must not be divergent and use new quantity
    - splitted Packing List must not be divergent and use old - new quantity
    - Invoice1 must not be divergent and use old quantity
    - Invoice2 must not be divergent and use 0 as quantity
    """
    if not quiet:
      self.logMessage('InvoiceSplitPackingListAndAcceptInvoice')
    sequence = self.PACKING_LIST_DEFAULT_SEQUENCE + \
    """
    stepCheckInvoicingRule
    stepDecreasePackingListLineQuantity
    stepSetContainerFullQuantity
    stepCheckPackingListIsCalculating
3084 3085
    stepTic
    stepCheckPackingListIsDiverged
3086 3087 3088 3089
    stepSplitAndDeferPackingList
    stepTic
    stepCheckPackingListIsSolved
    stepCheckPackingListSplitted
3090

3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103
    stepSetReadyPackingList
    stepTic
    stepStartPackingList
    stepCheckInvoicingRule
    stepCheckInvoiceTransactionRule
    stepTic
    stepCheckInvoiceBuilding
    stepStopPackingList
    stepTic
    stepDeliverPackingList
    stepTic
    stepCheckPackingListIsSolved
    stepCheckPackingListSplitted
3104

3105 3106
    stepIncreaseInvoiceLineQuantity
    stepCheckInvoiceIsCalculating
3107 3108
    stepTic
    stepCheckInvoiceIsDiverged
3109
    stepAcceptDecisionQuantityInvoice
3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121
    stepTic
    stepStartInvoice
    stepTic
    stepStopInvoice
    stepTic
    stepDeliverInvoice
    stepTic
    stepCheckInvoiceIsSolved
    stepCheckInvoiceNotSplitted
    stepCheckPackingListIsNotDivergent
    stepCheckPackingListIsSolved
    stepCheckInvoiceTransactionRule
3122

3123 3124
    stepRebuildAndCheckNothingIsCreated
    stepCheckInvoicesConsistency
3125

3126
    stepSwitchPackingLists
3127

3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143
    stepAddPackingListContainer
    stepSetContainerFullQuantity
    stepTic
    stepCheckPackingListIsSolved
    stepSetReadyPackingList
    stepTic
    stepStartPackingList
    stepCheckInvoicingRule
    stepCheckInvoiceTransactionRule
    stepTic
    stepCheckInvoiceBuilding
    stepStopPackingList
    stepTic
    stepDeliverPackingList
    stepTic
    stepCheckPackingListIsSolved
3144

3145 3146
    stepSetInvoiceLineQuantityToZero
    stepCheckInvoiceIsCalculating
3147 3148
    stepTic
    stepCheckInvoiceIsDiverged
3149
    stepAcceptDecisionQuantityInvoice
3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161
    stepTic
    stepStartInvoice
    stepTic
    stepStopInvoice
    stepTic
    stepDeliverInvoice
    stepTic
    stepCheckInvoiceIsSolved
    stepCheckInvoiceNotSplitted
    stepCheckPackingListIsNotDivergent
    stepCheckPackingListIsSolved
    stepCheckInvoiceTransactionRule
3162

3163 3164 3165 3166
    stepRebuildAndCheckNothingIsCreated
    stepCheckInvoicesConsistency
    """
    self.playSequence(sequence, quiet=quiet)
3167

3168
  def test_13_SplitAndDeferInvoice(self, quiet=quiet):
3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195
    """
    - Accept Order, Accept Packing List
    - Decrease quantity on Invoice
    - Split and defer Invoice
    - Accept Invoice
    - Accept splitted Invoice
    - Packing List must not be divergent and use old quantity
    - Invoice must not be divergent and use new quantity
    - splitted Invoice must not be divergent and use old - new quantity
    """
    if not quiet:
      self.logMessage('InvoiceSplitAndDeferInvoice')
    sequence = self.PACKING_LIST_DEFAULT_SEQUENCE + \
    """
    stepSetReadyPackingList
    stepTic
    stepStartPackingList
    stepCheckInvoicingRule
    stepCheckInvoiceTransactionRule
    stepTic
    stepCheckInvoiceBuilding
    stepStopPackingList
    stepTic
    stepDeliverPackingList
    stepTic
    stepCheckPackingListIsSolved
    stepCheckPackingListNotSplitted
3196

3197 3198 3199
    stepDecreaseInvoiceLineQuantity
    stepCheckInvoiceIsDivergent
    stepCheckInvoiceIsCalculating
3200 3201
    stepTic
    stepCheckInvoiceIsDiverged
3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212
    stepSplitAndDeferInvoice
    stepTic
    stepStartInvoice
    stepTic
    stepStopInvoice
    stepTic
    stepDeliverInvoice
    stepTic
    stepCheckInvoiceIsNotDivergent
    stepCheckInvoiceIsSolved
    stepCheckInvoiceSplitted
3213

3214 3215
    stepRebuildAndCheckNothingIsCreated
    stepCheckInvoicesConsistency
3216

3217 3218 3219
    stepCheckPackingListIsNotDivergent
    stepCheckPackingListIsSolved
    stepCheckInvoiceTransactionRule
3220

3221
    stepSwitchInvoices
3222

3223 3224 3225 3226 3227 3228 3229 3230
    stepStartInvoice
    stepTic
    stepStopInvoice
    stepTic
    stepDeliverInvoice
    stepTic
    stepCheckInvoiceIsNotDivergent
    stepCheckInvoiceIsSolved
3231

3232 3233 3234 3235
    stepRebuildAndCheckNothingIsCreated
    stepCheckInvoicesConsistency
    """
    self.playSequence(sequence, quiet=quiet)
3236

3237
  def test_14_AcceptDecisionOnInvoice(self, quiet=quiet):
3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267
    """
    - Accept Order, Accept Packing List
    - Increase or Decrease quantity on Invoice
    - Accept Decision on Invoice
    - Accept Invoice
    - Packing List must not be divergent and use old quantity
    - Invoice must not be divergent and use new quantity
    """
    if not quiet:
      self.logMessage('InvoiceAcceptDecisionOnInvoice')
    mid_sequence = \
    """
    stepSetReadyPackingList
    stepTic
    stepStartPackingList
    stepCheckInvoicingRule
    stepCheckInvoiceTransactionRule
    stepTic
    stepCheckInvoiceBuilding
    stepStopPackingList
    stepTic
    stepDeliverPackingList
    stepTic
    stepCheckPackingListIsSolved
    stepCheckPackingListNotSplitted
    """
    end_sequence = \
    """
    stepCheckInvoiceIsDivergent
    stepCheckInvoiceIsCalculating
3268 3269
    stepTic
    stepCheckInvoiceIsDiverged
3270
    stepAcceptDecisionQuantityInvoice
3271 3272 3273 3274 3275 3276 3277
    stepTic
    stepStartInvoice
    stepTic
    stepStopInvoice
    stepTic
    stepDeliverInvoice
    stepTic
3278

3279 3280 3281
    stepCheckPackingListIsNotDivergent
    stepCheckPackingListIsSolved
    stepCheckInvoiceTransactionRule
3282

3283 3284 3285
    stepCheckInvoiceNotSplitted
    stepCheckInvoiceIsNotDivergent
    stepCheckInvoiceIsSolved
3286

3287 3288 3289
    stepRebuildAndCheckNothingIsCreated
    stepCheckInvoicesConsistency
    """
3290

3291 3292 3293 3294 3295
    mid_sequence_list = ["""
    stepDecreaseInvoiceLineQuantity
    """, """
    stepIncreaseInvoiceLineQuantity
    """]
3296

3297 3298 3299 3300 3301 3302
    sequence_list = SequenceList()
    for seq in mid_sequence_list:
      sequence = self.PACKING_LIST_DEFAULT_SEQUENCE + \
          mid_sequence + seq + end_sequence
      sequence_list.addSequenceString(sequence)
    sequence_list.play(self, quiet=quiet)
3303 3304


3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322
  def test_Reference(self):
    # A reference is set automatically on Sale Invoice Transaction
    supplier = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
                            title='Supplier')
    client = self.portal.organisation_module.newContent(
                            portal_type='Organisation',
                            title='Client')
    currency = self.portal.currency_module.newContent(
                            portal_type='Currency')
    invoice = self.portal.accounting_module.newContent(
                    portal_type='Sale Invoice Transaction',
                    start_date=DateTime(),
                    price_currency_value=currency,
                    resource_value=currency,
                    source_section_value=supplier,
                    destination_section_value=client)
    self.portal.portal_workflow.doActionFor(invoice, 'confirm_action')
3323

3324
    self.assertEquals('1', invoice.getReference())
3325

3326
  def test_16_ManuallyAddedMovements(self, quiet=quiet):
3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352
    """
    Checks that adding invoice lines and accounting lines to one invoice
    generates correct simulation
    """
    if not quiet:
      self.logMessage('Invoice with Manually Added Movements')
    sequence_list = SequenceList()
    for base_sequence in (self.PACKING_LIST_DEFAULT_SEQUENCE, ) :
      sequence_list.addSequenceString(
          base_sequence +
          """
          stepSetReadyPackingList
          stepTic
          stepStartPackingList
          stepCheckInvoicingRule
          stepTic
          stepCheckInvoiceBuilding
          stepRebuildAndCheckNothingIsCreated
          stepCheckInvoicesConsistency
          stepAddInvoiceLines
          stepTic
          stepStartInvoice
          stepTic
          stepCheckSimulationTrees
          """)
    sequence_list.play(self, quiet=quiet)
3353

3354
  def test_17_ManuallyAddedWrongMovements(self, quiet=quiet):
3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379
    """
    Checks that adding invoice lines and accounting lines to one invoice
    generates correct simulation, even when adding very wrong movements
    """
    if not quiet:
      self.logMessage('Invoice with Manually Added Movements')
    sequence_list = SequenceList()
    for base_sequence in (self.PACKING_LIST_DEFAULT_SEQUENCE, ) :
      sequence_list.addSequenceString(
          base_sequence +
          """
          stepSetReadyPackingList
          stepTic
          stepStartPackingList
          stepCheckInvoicingRule
          stepTic
          stepCheckInvoiceBuilding
          stepAddWrongInvoiceLines
          stepTic
          stepStartInvoice
          stepCheckStartInvoiceFail
          stepCheckSimulationTrees
          """)
    sequence_list.play(self, quiet=quiet)

3380
  def test_18_compareInvoiceAndPackingList(self, quiet=quiet):
3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400
    """
    Checks that a Simple Invoice is created from a Packing List
    """
    if not quiet:
      self.logMessage('Simple Invoice')
    sequence_list = SequenceList()
    for base_sequence in (self.PACKING_LIST_DEFAULT_SEQUENCE, ) :
      sequence_list.addSequenceString(
        base_sequence +
      """
        stepSetReadyPackingList
        stepTic
        stepStartPackingList
        stepCheckInvoicingRule
        stepTic
        stepCheckInvoiceBuilding
        stepCheckInvoicesConsistency
        stepCheckPackingListInvoice
      """)
    sequence_list.play(self, quiet=quiet)
3401

3402 3403 3404 3405 3406 3407
  def _adoptDivergenceOnPackingList(self, packing_list, divergence_list):
    builder_list = packing_list.getBuilderList()
    for builder in builder_list:
      builder.solveDivergence(packing_list.getRelativeUrl(),
                              divergence_to_adopt_list=divergence_list)

3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422
  def test_accept_quantity_divergence_on_invoice_with_started_packing_list(
                        self, quiet=quiet):
    # only applies to sale invoice, because purchase invoices are not built yet
    # when the packing list is in started state
    sequence_list = SequenceList()
    sequence = sequence_list.addSequenceString(self.PACKING_LIST_DEFAULT_SEQUENCE)
    sequence_list.play(self, quiet=quiet)

    packing_list = sequence.get('packing_list')
    packing_list_line = packing_list.getMovementList()[0]
    previous_quantity = packing_list_line.getQuantity()
    
    packing_list.setReady()
    packing_list.start()
    self.assertEquals('started', packing_list.getSimulationState())
3423
    transaction.commit()
3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435
    self.tic()

    invoice = packing_list.getCausalityRelatedValue(
                                  portal_type=self.invoice_portal_type)
    self.assertNotEquals(invoice, None)
    invoice_line_list = invoice.getMovementList()
    self.assertEquals(1, len(invoice_line_list))
    invoice_line = invoice_line_list[0]

    new_quantity = invoice_line.getQuantity() * 2
    invoice_line.setQuantity(new_quantity)
    
3436
    transaction.commit()
3437 3438 3439 3440 3441 3442 3443 3444 3445 3446
    self.tic()

    self.assertTrue(invoice.isDivergent())
    divergence_list = invoice.getDivergenceList()
    self.assertEquals(1, len(divergence_list))

    divergence = divergence_list[0]
    self.assertEquals('quantity', divergence.tested_property)

    # accept decision
3447
    self._acceptDivergenceOnInvoice(invoice, divergence_list)
3448

3449
    transaction.commit()
3450 3451 3452 3453 3454 3455 3456 3457 3458
    self.tic()
    self.assertEquals('solved', invoice.getCausalityState())

    self.assertEquals([], invoice.getDivergenceList())
    self.assertEquals(new_quantity, invoice_line.getQuantity())
    self.assertEquals(new_quantity,
          invoice_line.getDeliveryRelatedValue(portal_type='Simulation Movement'
              ).getQuantity())

3459 3460
    if invoice_line.getDeliveryRelatedValue().getParentValue().getSpecialiseId() == \
        'new_invoice_simulation_rule':
3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477
      # With new simulation solvers, changes on simulation movements will
      # not backtrack.
      pass
    else:
      # With legacy simulation solvers, changes on simulation movements
      # will backtrack if simulation movements are not frozen.
      # the packing list is divergent, because it is not frozen
      self.assertEquals('diverged', packing_list.getCausalityState())
      divergence_list = packing_list.getDivergenceList()
      self.assertEquals(1, len(divergence_list))
      divergence = divergence_list[0]
      self.assertEquals('quantity', divergence.tested_property)
      # if we adopt prevision on this packing list, both invoice and
      # packing list will be solved
      self._adoptDivergenceOnPackingList(packing_list, divergence_list)
      transaction.commit()
      self.tic()
3478 3479
    self.assertEquals('solved', packing_list.getCausalityState())
    self.assertEquals('solved', invoice.getCausalityState())
3480
  
3481

3482
class TestPurchaseInvoice(TestInvoice, ERP5TypeTestCase):
3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495
  """Tests for purchase invoice.
  """
  resource_portal_type = 'Product'
  order_portal_type = 'Purchase Order'
  order_line_portal_type = 'Purchase Order Line'
  order_cell_portal_type = 'Purchase Order Cell'
  packing_list_portal_type = 'Purchase Packing List'
  packing_list_line_portal_type = 'Purchase Packing List Line'
  packing_list_cell_portal_type = 'Purchase Packing List Cell'
  invoice_portal_type = 'Purchase Invoice Transaction'
  invoice_transaction_line_portal_type = 'Purchase Invoice Transaction Line'
  invoice_line_portal_type = 'Invoice Line'
  invoice_cell_portal_type = 'Invoice Cell'
Jérome Perrin's avatar
Jérome Perrin committed
3496

3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519
  # default sequence for one line of not varianted resource.
  PACKING_LIST_DEFAULT_SEQUENCE = """
      stepCreateEntities
      stepCreateCurrency
      stepCreateSaleInvoiceTransactionRule
      stepCreateOrder
      stepSetOrderProfile
      stepSetOrderPriceCurrency
      stepCreateNotVariatedResource
      stepTic
      stepCreateOrderLine
      stepSetOrderLineResource
      stepSetOrderLineDefaultValues
      stepOrderOrder
      stepTic
      stepCheckDeliveryBuilding
      stepConfirmOrder
      stepTic
      stepCheckOrderRule
      stepCheckOrderSimulation
      stepCheckDeliveryBuilding
      stepTic
    """
3520

3521 3522 3523
import unittest
def test_suite():
  suite = unittest.TestSuite()
3524 3525
  suite.addTest(unittest.makeSuite(TestSaleInvoice))
  suite.addTest(unittest.makeSuite(TestPurchaseInvoice))
3526
  return suite