testPackingList.py 79.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
##############################################################################
#
# Copyright (c) 2004-2008 Nexedi SA and Contributors. All Rights Reserved.
#          Sebastien Robin <seb@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################

import unittest

from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.ERP5Type.UnrestrictedMethod import UnrestrictedMethod
33
from Products.ERP5.Document.BusinessTemplate import getChainByType
34 35
from zLOG import LOG
from Products.ERP5Type.tests.Sequence import SequenceList
36
from Products.ERP5.tests.testOrder import TestOrderMixin
37 38 39 40 41 42 43 44 45
from DateTime import DateTime

class TestPackingListMixin(TestOrderMixin):
  """
    Test business template erp5_trade
  """
  container_portal_type = 'Container'
  container_line_portal_type = 'Container Line'
  container_cell_portal_type = 'Container Cell'
46 47 48 49 50 51 52 53 54 55 56
  default_order_sequence = """
        CreateOrganisation1
        CreateOrganisation2
        CreateOrganisation3
        CreateProject1
        CreateProject2
        CreateOrder
        CreateCurrency
        SetOrderPriceCurrency
        SetOrderProfile
        """
57
  # Simple order without cell
58 59 60 61 62 63 64 65 66 67
  default_sequence = default_order_sequence + """
        CreateNotVariatedResource
        Tic
        CreateOrderLine
        SetOrderLineResource
        SetOrderLineDefaultValues
        OrderOrder
        Tic
        ConfirmOrder
        Tic
68 69
        PackingListBuilderAlarm
        Tic
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
        CheckOrderSimulation
        CheckDeliveryBuilding
        CheckPackingListIsNotDivergent
        CheckOrderPackingList
        """
  confirmed_order_without_packing_list = default_order_sequence + """
        CreateNotVariatedResource
        Tic
        CreateOrderLine
        SetOrderLineResource
        SetOrderLineDefaultValues
        OrderOrder
        Tic
        ConfirmOrder
        Tic
85 86
        PackingListBuilderAlarm
        Tic
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
        """
  default_sequence_with_duplicated_lines = default_order_sequence + """
        CreateNotVariatedResource
        Tic
        CreateOrderLine
        SetOrderLineResource
        SetOrderLineDefaultValues
        Tic
        CreateOrderLine
        SetOrderLineResource
        SetOrderLineDefaultValues
        OrderOrder
        Tic
        ConfirmOrder
        Tic
102 103
        PackingListBuilderAlarm
        Tic
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
        CheckOrderSimulation
        CheckDeliveryBuilding
        CheckPackingListIsNotDivergent
        CheckOrderPackingList
        """
  default_sequence_with_two_lines = default_order_sequence + """
        CreateNotVariatedResource
        Tic
        CreateOrderLine
        SetOrderLineResource
        SetOrderLineDefaultValues
        CreateNotVariatedResource
        Tic
        CreateOrderLine
        SetOrderLineResource
        SetOrderLineDefaultValues
        OrderOrder
        Tic
        ConfirmOrder
        Tic
124 125
        PackingListBuilderAlarm
        Tic
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
        CheckOrderSimulation
        CheckDeliveryBuilding
        CheckPackingListIsNotDivergent
        CheckOrderPackingList
        """
  variated_default_sequence = default_order_sequence + """
        CreateVariatedResource
        Tic
        CreateOrderLine
        SetOrderLineResource
        SetOrderLineDefaultValues
        SetOrderLineFullVCL
        CompleteOrderLineMatrix
        OrderOrder
        Tic
        ConfirmOrder
        Tic
143 144
        PackingListBuilderAlarm
        Tic
145 146 147 148 149
        CheckOrderSimulation
        CheckDeliveryBuilding
        CheckPackingListIsNotDivergent
        CheckOrderPackingList
        """
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

  def getTitle(self):
    return "Packing List"

  def enableLightInstall(self):
    """
    You can override this.
    Return if we should do a light install (1) or not (0)
    """
    return 1

  def enableActivityTool(self):
    """
    You can override this.
    Return if we should create (1) or not (0) an activity tool.
    """
    return 1

  def stepCheckOrderPackingList(self, sequence=None, sequence_list=None, **kw):
    """
      Test if packing list is matching order
    """
    packing_list = sequence.get('packing_list')
    order = sequence.get('order')
174 175 176 177
    self.assertEqual(packing_list.getCausalityValue(), order)
    self.assertEqual(packing_list.getSource(), order.getSource())
    self.assertEqual(packing_list.getDestination(), order.getDestination())
    self.assertEqual(packing_list.getDestinationSection(),
178
                                       order.getDestinationSection())
179
    self.assertEqual(packing_list.getSourceSection(),
180
                                       order.getSourceSection())
181
    self.assertEqual(packing_list.getSourceDecision(),
182
                                       order.getSourceDecision())
183
    self.assertEqual(packing_list.getDestinationAdministration(),
184
                                       order.getDestinationAdministration())
185
    self.assertEqual(packing_list.getSourceAdministration(),
186
                                       order.getSourceAdministration())
187
    self.assertEqual(packing_list.getPriceCurrency(),
188
                                       order.getPriceCurrency())
189
    self.assertEqual(packing_list.getDestinationProject(),
190
                                       order.getDestinationProject())
191
    self.assertEqual(packing_list.getSourceProject(),
192 193 194 195 196 197 198 199 200
                                       order.getSourceProject())

  def stepCheckPackingListIsDivergent(self, sequence=None, sequence_list=None,
                                      packing_list=None,**kw):
    """
      Test if packing list is divergent
    """
    if packing_list is None:
      packing_list = sequence.get('packing_list')
201
    self.assertFalse('Site Error' in packing_list.view())
202 203 204 205 206 207 208 209 210 211 212 213 214 215
    self.assertTrue(packing_list.isDivergent())

  def stepCheckNewPackingListIsDivergent(self, sequence=None, sequence_list=None, **kw):
    """
      Test if packing list is divergent
    """
    packing_list = sequence.get('new_packing_list')
    self.stepCheckPackingListIsDivergent(packing_list=packing_list,sequence=sequence)

  def stepCheckPackingListIsCalculating(self, sequence=None, sequence_list=None, **kw):
    """
      Test if packing list is calculating
    """
    packing_list = sequence.get('packing_list')
216
    self.assertEqual('calculating',packing_list.getCausalityState())
217 218 219 220 221 222

  def stepCheckPackingListIsSolved(self, sequence=None, sequence_list=None, **kw):
    """
      Test if packing list is solved
    """
    packing_list = sequence.get('packing_list')
223
    self.assertEqual('solved',packing_list.getCausalityState())
224

225 226
  def stepCheckNewPackingListIsSolved(self, sequence=None, sequence_list=None, **kw):
    packing_list = sequence.get('new_packing_list')
227
    self.assertEqual('solved', packing_list.getCausalityState())
228

229 230 231 232 233
  def stepCheckPackingListIsDiverged(self, sequence=None, sequence_list=None, **kw):
    """
      Test if packing list is divergent
    """
    packing_list = sequence.get('packing_list')
234
    self.assertEqual('diverged', packing_list.getCausalityState())
235 236 237 238 239 240 241 242

  def stepCheckPackingListIsNotDivergent(self, sequence=None, sequence_list=None, **kw):
    """
      Test if packing list is not divergent
    """
    packing_list = sequence.get('packing_list')
    self.assertFalse(packing_list.isDivergent())

243
  @UnrestrictedMethod
244 245 246 247 248 249 250 251 252 253 254
  def stepChangeOrderLineResource(self, sequence=None,
                                        sequence_list=None, **kw):
    """
    Change the resource of the order.
    """
    order = sequence.get('order')
    resource = sequence.get('resource')
    for order_line in order.objectValues(
                             portal_type=self.order_line_portal_type):
      order_line.edit(resource_value=resource)

255 256 257 258 259 260 261 262 263 264 265
  def stepChangePackingListLineResource(self, sequence=None,
                                        sequence_list=None, **kw):
    """
    Change the resource of the packing list.
    """
    packing_list = sequence.get('packing_list')
    resource = sequence.get('resource')
    for packing_list_line in packing_list.objectValues(
                             portal_type=self.packing_list_line_portal_type):
      packing_list_line.edit(resource_value=resource)

266
  @UnrestrictedMethod
267 268 269 270 271 272 273 274 275 276 277 278
  def stepDecreaseOrderLineQuantity(self, sequence=None, sequence_list=None,
                                    **kw):
    """
    Set a decreased quantity on order lines
    """
    order = sequence.get('order')
    quantity = sequence.get('line_quantity', default=self.default_quantity - 1)
    sequence.edit(line_quantity=quantity)
    for order_line in order.objectValues(
        portal_type=self.order_line_portal_type):
      order_line.edit(quantity=quantity)

279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
  def stepDecreasePackingListLineQuantity(self, sequence=None,
      sequence_list=None, **kw):
    """
    Set a decreased quantity on packing list lines
    """
    packing_list = sequence.get('packing_list')
    quantity = sequence.get('line_quantity',default=self.default_quantity)
    quantity = quantity - 1
    sequence.edit(line_quantity=quantity)
    for packing_list_line in packing_list.objectValues(
        portal_type=self.packing_list_line_portal_type):
      packing_list_line.edit(quantity=quantity)
    sequence.edit(last_delta = sequence.get('last_delta', 0.0) - 1.0)

  def stepIncreasePackingListLineQuantity(self, sequence=None,
      sequence_list=None, **kw):
    """
    Set a increased quantity on packing list lines
    """
    packing_list = sequence.get('packing_list')
    quantity = sequence.get('line_quantity',default=self.default_quantity)
    quantity = quantity + 1
    sequence.edit(line_quantity=quantity)
    for packing_list_line in packing_list.objectValues(
        portal_type=self.packing_list_line_portal_type):
      packing_list_line.edit(quantity=quantity)
    sequence.edit(last_delta = sequence.get('last_delta', 0.0) + 1.0)

  def stepSplitAndDeferPackingList(self, sequence=None, sequence_list=None, **kw):
    """
      Do the split and defer action
    """
    packing_list = sequence.get('packing_list')
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
    solver_process_tool = self.portal.portal_solver_processes
    solver_process = solver_process_tool.newSolverProcess(packing_list)
    quantity_solver_decision, = [x for x in solver_process.contentValues()
      if x.getCausalityValue().getTestedProperty() == 'quantity']
    # use Quantity Split Solver.
    quantity_solver_decision.setSolverValue(self.portal.portal_solvers['Quantity Split Solver'])
    # configure for Quantity Split Solver.
    kw = {'delivery_solver':'FIFO Delivery Solver',
          'start_date':self.datetime + 15,
          'stop_date':self.datetime + 25}
    quantity_solver_decision.updateConfiguration(**kw)
    solver_process.buildTargetSolverList()
    solver_process.solve()
    # build split deliveries manually. XXX ad-hoc
    previous_tag = None
    for delivery_builder in packing_list.getBuilderList():
      after_tag = []
      if previous_tag:
        after_tag.append(previous_tag)
      delivery_builder.activate(
        after_method_id=('solve',
                         'immediateReindexObject',
                         'recursiveImmediateReindexObject',), # XXX too brutal.
        after_tag=after_tag,
        ).build(explanation_uid=packing_list.getCausalityValue().getUid())
337 338 339 340 341 342 343 344

  def stepSplitAndDeferDoNothingPackingList(self, sequence=None, sequence_list=None, **kw):
    """
      Do the split and defer action, but choose "do nothing" for divergences
    """
    packing_list = sequence.get('packing_list')
    kw = {'listbox':[
      {'listbox_key':line.getRelativeUrl(),
345
       'choice':'ignore'} for line in packing_list.getMovementList()
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
      if line.isDivergent()]}
    self.portal.portal_workflow.doActionFor(
      packing_list,
      'split_and_defer_action',
      start_date=self.datetime + 15,
      stop_date=self.datetime + 25,
      **kw)

  def stepCheckPackingListSplitted(self, sequence=None, sequence_list=None, **kw):
    """
      Test if packing list was splitted
    """
    order = sequence.get('order')
    packing_list_list = order.getCausalityRelatedValueList(
                               portal_type=self.packing_list_portal_type)
361
    self.assertEqual(2,len(packing_list_list))
362 363 364 365 366 367 368 369 370 371
    packing_list1 = None
    packing_list2 = None
    for packing_list in packing_list_list:
      if packing_list.getUid() == sequence.get('packing_list').getUid():
        packing_list1 = packing_list
      else:
        packing_list2 = packing_list
    sequence.edit(new_packing_list=packing_list2)
    for line in packing_list1.objectValues(
          portal_type= self.packing_list_line_portal_type):
372
      self.assertEqual(self.default_quantity-1,line.getQuantity())
373 374
    for line in packing_list2.objectValues(
          portal_type= self.packing_list_line_portal_type):
375
      self.assertEqual(1,line.getQuantity())
376 377 378 379 380 381 382 383

  def stepCheckPackingListSplittedTwoTimes(self, sequence=None, sequence_list=None, **kw):
    """
      Test if packing list is divergent
    """
    order = sequence.get('order')
    packing_list_list = order.getCausalityRelatedValueList(
                               portal_type=self.packing_list_portal_type)
384
    self.assertEqual(2,len(packing_list_list))
385 386 387 388 389 390 391 392 393
    packing_list1 = None
    packing_list2 = None
    for packing_list in packing_list_list:
      if packing_list.getUid() == sequence.get('packing_list').getUid():
        packing_list1 = packing_list
      else:
        packing_list2 = packing_list
    for line in packing_list1.objectValues(
          portal_type= self.packing_list_line_portal_type):
394
      self.assertEqual(self.default_quantity-2,line.getQuantity())
395 396
    for line in packing_list2.objectValues(
          portal_type= self.packing_list_line_portal_type):
397
      self.assertEqual(2,line.getQuantity())
398 399 400 401 402 403 404 405

  def stepCheckPackingListNotSplitted(self, sequence=None, sequence_list=None, **kw):
    """
      Test if packing list is divergent
    """
    order = sequence.get('order')
    packing_list_list = order.getCausalityRelatedValueList(
                               portal_type=self.packing_list_portal_type)
406
    self.assertEqual(1,len(packing_list_list))
407 408 409 410
    packing_list1 = sequence.get('packing_list')
    last_delta = sequence.get('last_delta', 0.0)
    for line in packing_list1.objectValues(
          portal_type= self.packing_list_line_portal_type):
411
      self.assertEqual(self.default_quantity + last_delta,
412 413 414
          line.getQuantity())
      simulation_list = line.getDeliveryRelatedValueList(
                            portal_type='Simulation Movement')
415
      self.assertEqual(len(simulation_list),1)
416
      simulation_movement = simulation_list[0]
417
      self.assertEqual(self.default_quantity + last_delta,
418 419 420 421 422 423 424 425 426 427
          simulation_movement.getCorrectedQuantity())

  def stepCheckPackingListNotSolved(self, sequence=None, sequence_list=None, **kw):
    """
      This step is specific to test_10 : the incorrectly used solver didn't
      solve anything.
    """
    order = sequence.get('order')
    packing_list_list = order.getCausalityRelatedValueList(
                               portal_type=self.packing_list_portal_type)
428
    self.assertEqual(1,len(packing_list_list))
429 430 431 432
    packing_list1 = sequence.get('packing_list')
    last_delta = sequence.get('last_delta', 0.0)
    for line in packing_list1.objectValues(
          portal_type= self.packing_list_line_portal_type):
433
      self.assertEqual(self.default_quantity + last_delta,
434 435 436
          line.getQuantity())
      simulation_list = line.getDeliveryRelatedValueList(
                            portal_type='Simulation Movement')
437
      self.assertEqual(len(simulation_list),1)
438 439 440
      simulation_movement = simulation_list[0]

      # Here we don't add last_delta, as the solver didn't do its work.
441
      self.assertEqual(self.default_quantity,
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
          simulation_movement.getCorrectedQuantity())

  def stepChangePackingListDestination(self, sequence=None,
                                       sequence_list=None, **kw):
    """
      Test if packing list is divergent
    """
    organisation3 = sequence.get('organisation3')
    packing_list = sequence.get('packing_list')
    packing_list.edit(destination_value=organisation3)

  def stepCreateOrganisation3(self,sequence=None, sequence_list=None, **kw):
    """
      Create a empty organisation
    """
    self.stepCreateOrganisation(sequence=sequence, sequence_list=sequence_list,
                                **kw)
    organisation = sequence.get('organisation')
    sequence.edit(organisation3=organisation)

  def stepCheckSimulationDestinationUpdated(self,sequence=None, sequence_list=None, **kw):
    """
      Test if the destination of the simulation movement was changed
    """
    applied_rule = sequence.get('applied_rule')
    simulation_movement_list = applied_rule.objectValues()
468
    self.assertEqual(len(simulation_movement_list),1)
469 470 471
    org3 = sequence.get('organisation3')
    for simulation_movement in simulation_movement_list:
      simulation_movement = simulation_movement.objectValues()[0].objectValues()[0]
472
      self.assertEqual(simulation_movement.getDestinationValue(),org3)
473 474 475 476 477 478 479 480 481 482 483 484 485 486

  def stepChangePackingListStartDate(self, sequence=None, sequence_list=None, **kw):
    """
      Change the start_date of the packing_list.
    """
    packing_list = sequence.get('packing_list')
    packing_list.edit(start_date=self.datetime + 15)

  def stepCheckSimulationStartDateUpdated(self,sequence=None, sequence_list=None, **kw):
    """
      Test if the start_date of the simulation movement was changed
    """
    applied_rule = sequence.get('applied_rule')
    simulation_movement_list = applied_rule.objectValues()
487
    self.assertEqual(len(simulation_movement_list),1)
488 489
    delivery_applied_rule = simulation_movement_list[0].objectValues()[0]
    simulation_movement_list = delivery_applied_rule.objectValues()
490
    self.assertEqual(len(simulation_movement_list),1)
491
    for simulation_movement in simulation_movement_list:
492
      self.assertEqual(simulation_movement.getStartDate(),self.datetime + 15)
493 494 495 496 497 498 499

  def stepCheckSimulationQuantityUpdated(self,sequence=None, sequence_list=None, **kw):
    """
      Test if the quantity of the simulation movement was changed
    """
    applied_rule = sequence.get('applied_rule')
    simulation_movement_list = applied_rule.objectValues()
500
    self.assertEqual(len(simulation_movement_list),1)
501 502
    for simulation_movement in simulation_movement_list:
      simulation_movement = simulation_movement.objectValues()[0].objectValues()[0]
503
      self.assertEqual(simulation_movement.getQuantity() +
504 505 506 507 508 509 510 511 512
                        simulation_movement.getDeliveryError(),
                        self.default_quantity)

  def stepCheckSimulationQuantityUpdatedForMergedLine(self,sequence=None, sequence_list=None, **kw):
    """
      Test if the quantity of the simulation movement was changed
    """
    applied_rule = sequence.get('applied_rule')
    simulation_movement_list = applied_rule.objectValues()
513
    self.assertEqual(len(simulation_movement_list),2)
514 515
    for simulation_movement in simulation_movement_list:
      simulation_movement = simulation_movement.objectValues()[0].objectValues()[0]
516
      self.assertEqual(simulation_movement.getQuantity() +
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
                        simulation_movement.getDeliveryError(),
                        self.default_quantity)

  def stepEditPackingListLine(self,sequence=None, sequence_list=None, **kw):
    """
      Edits a Packing List Line
    """
    packing_list_line = sequence.get('packing_list_line')
    packing_list_line.edit(description='This line was edited!')

  def stepDeletePackingListLine(self,sequence=None, sequence_list=None, **kw):
    """
      Deletes a Packing List Line
    """
    packing_list = sequence.get('packing_list')
    packing_list_line_id = sequence.get('packing_list_line').getId()
    packing_list.manage_delObjects([packing_list_line_id])

  def stepAddPackingListLine(self,sequence=None, sequence_list=None, **kw):
    """
      Adds a Packing List Line
    """
    packing_list = sequence.get('packing_list')
    packing_list_line = packing_list.newContent(
        portal_type=self.packing_list_line_portal_type)
    self.stepCreateNotVariatedResource(sequence=sequence,
        sequence_list=sequence_list, **kw)
    resource = sequence.get('resource')
    packing_list_line.setResourceValue(resource)
    packing_list_line.edit(price=100, quantity=200)

  def stepCheckSimulationConnected(self,sequence=None, sequence_list=None, **kw):
    """
      Check if simulation movement are connected
    """
    applied_rule = sequence.get('applied_rule')
    simulation_movement_list = applied_rule.objectValues()
554
    self.assertEqual(len(simulation_movement_list),1)
555 556 557 558
    order_line = sequence.get('order_line')
    packing_list = sequence.get('packing_list')
    packing_list_line = sequence.get('packing_list_line')
    for simulation_movement in simulation_movement_list:
559 560
      self.assertEqual(simulation_movement.getDeliveryValue(), order_line)
      self.assertEqual(packing_list_line.getCausalityValue(),
561 562
                        order_line)
      rule_list = simulation_movement.objectValues()
563
      self.assertTrue(len(rule_list), 1)
564
      delivering_rule = rule_list[0]
565
      self.assertTrue(delivering_rule.getSpecialiseValue().getPortalType(),
566 567
                      'Delivering Rule')
      child_simulation_movement_list = delivering_rule.objectValues()
568
      self.assertTrue(len(child_simulation_movement_list), 1)
569
      child_simulation_movement = child_simulation_movement_list[0]
570
      self.assertEqual(child_simulation_movement.getDeliveryValue(),
571 572 573 574 575 576 577 578
                        packing_list_line)

  def stepCheckSimulationDisconnected(self,sequence=None, sequence_list=None, **kw):
    """
      Check if simulation movement are disconnected
    """
    applied_rule = sequence.get('applied_rule')
    simulation_movement_list = applied_rule.objectValues()
579
    self.assertEqual(len(simulation_movement_list),1)
580 581
    for simulation_movement in simulation_movement_list:
      child_simulation_movement = simulation_movement.objectValues()[0].objectValues()[0]
582
      self.assertEqual(child_simulation_movement.getDeliveryValue(),None)
583

584
  def stepCheckTwoSimulationLines(self, sequence):
585
    """
586 587
    Check there are exactly two simulation lines related to the packing list
    line(s)
588
    """
589
    simulation_movement_list = self._getSPLSimulationMovementList(sequence)
590
    self.assertEqual(len(simulation_movement_list),2)
591 592 593 594 595 596 597 598 599 600 601

  def _getSPLSimulationMovementList(self, sequence):
    """ Get the simulation movement lines from sales packing list movements """
    packing_list = sequence['packing_list']
    movement_list = packing_list.getMovementList()
    simulation_movement_list = []
    for movement in movement_list:
      simulation_movement_list.extend(
        movement.getDeliveryRelatedValueList()
      )
    return simulation_movement_list
602

603
  def stepModifySimulationLineQuantity(self,sequence=None, sequence_list=None, **kw):
604
    """
605
      Modify quantity on simulation lines related to SPL lines
606
    """
607
    simulation_movement_list = self._getSPLSimulationMovementList(sequence)
608
    for simulation_movement in simulation_movement_list:
609 610 611
      # we record the property so it doesn't get changed by expand with
      # the value from a higher simulation level
      simulation_movement.recordProperty('quantity')
612
      simulation_movement.edit(quantity=self.default_quantity-1)
613
      #simulation_movement.getDeliveryValue().edit(quantity=self.default_quantity-1)
614 615
      simulation_movement.expand()

616
  def stepModifySimulationLineStartDate(self,sequence=None, sequence_list=None, **kw):
617
    """
618
    Modify start_date on simulation lines related to SPL lines
619
    """
620
    simulation_movement_list = self._getSPLSimulationMovementList(sequence)
621
    for simulation_movement in simulation_movement_list:
622 623
      # we record the property so it doesn't get changed by expand with
      # the value from a higher simulation level
624 625
      simulation_movement.recordProperty('start_date')
      simulation_movement.edit(start_date=self.datetime+15)
626 627 628 629
      simulation_movement.expand()

  def stepModifyOneSimulationLineStartDate(self,sequence=None, sequence_list=None, **kw):
    """
630
      Modify start_date on only one simulation line related to SPL lines
631
    """
632
    simulation_movement_list = self._getSPLSimulationMovementList(sequence)
633
    self.assertEqual(len(simulation_movement_list),
634
                      len(sequence['resource_list']))
635 636 637 638 639 640
    simulation_movement_list[-1].recordProperty('start_date')
    simulation_movement_list[-1].edit(start_date=self.datetime+15)
    simulation_movement_list[-1].expand()

  def stepModifySimulationLineResource(self,sequence=None, sequence_list=None, **kw):
    """
641
      Modify the resource on simulation lines related to SPL lines
642
    """
643
    simulation_movement_list = self._getSPLSimulationMovementList(sequence)
644 645 646 647 648 649 650 651
    resource_list = sequence.get('resource_list')
    for simulation_movement in simulation_movement_list:
      simulation_movement.recordProperty('resource')
      simulation_movement.edit(resource_value=resource_list[-1])
      simulation_movement.expand()

  def stepModifyOneSimulationLineResource(self,sequence=None, sequence_list=None, **kw):
    """
652
      Modify the resource on only one simulation line related to SPL lines
653
    """
654
    simulation_movement_list = self._getSPLSimulationMovementList(sequence)
655 656 657 658 659 660 661
    resource_list = sequence.get('resource_list')
    simulation_movement_list[-1].recordProperty('resource')
    simulation_movement_list[-1].edit(resource_value=resource_list[-1])
    simulation_movement_list[-1].expand()

  def stepNewPackingListAdoptPrevisionQuantity(self,sequence=None, sequence_list=None, **kw):
    """
662
      Solve quantity divergence on new_packing_list with 'Adopt Solver'
663 664
    """
    packing_list = sequence.get('new_packing_list')
665
    self._solveDivergence(packing_list, 'quantity', 'Adopt Solver')
666

667
  def stepAcceptDecisionDestination(self,sequence=None, sequence_list=None, **kw):
668
    """
669
      Solve destination divergence on packing_list with 'Adopt Solver'
670 671
    """
    packing_list = sequence.get('packing_list')
672
    self._solveDivergence(packing_list, 'destination', 'Accept Solver')
673 674 675

  def stepUnifyStartDateWithDecision(self,sequence=None, sequence_list=None, **kw):
    """
676 677
      Solve start_date divergence on packing_list with the 'Unify Solver',
      using the start_date of the packing_list.
678 679
    """
    packing_list = sequence.get('packing_list')
680 681
    self._solveDivergence(packing_list, 'start_date', 'Unify Solver',
                          value=packing_list.getStartDate())
682 683 684

  def stepUnifyStartDateWithPrevision(self,sequence=None, sequence_list=None, **kw):
    """
685 686
      Solve start_date divergence on packing_list with the 'Unify Solver',
      using the start_date of one of the simulation movements.
687 688
    """
    packing_list = sequence.get('packing_list')
689
    simulation_movement_list = self._getSPLSimulationMovementList(sequence)
690
    self._solveDivergence(packing_list, 'start_date',
691
      'Unify Solver', value=simulation_movement_list[-1].getStartDate())
692 693 694

  def stepAcceptDecisionResource(self,sequence=None, sequence_list=None, **kw):
    packing_list = sequence.get('packing_list')
695
    self._solveDivergence(packing_list, 'resource', 'Accept Solver')
696 697 698

  def stepAcceptDecisionQuantity(self,sequence=None, sequence_list=None, **kw):
    packing_list = sequence.get('packing_list')
699
    self._solveDivergence(packing_list, 'quantity', 'Accept Solver')
700 701 702

  def stepAdoptPrevisionResource(self,sequence=None, sequence_list=None, **kw):
    packing_list = sequence.get('packing_list')
703
    self._solveDivergence(packing_list, 'resource', 'Adopt Solver')
704 705 706

  def stepAdoptPrevisionQuantity(self,sequence=None, sequence_list=None, **kw):
    packing_list = sequence.get('packing_list')
707 708
    self._solveDivergence(packing_list, 'quantity', 'Adopt Solver')

709
  def _solveDivergence(self, document, property, solver, **kw):
710 711 712 713 714 715 716 717
    """Solve divergence by using solver tool"""
    solver_process_tool = self.portal.portal_solver_processes
    solver_process = solver_process_tool.newSolverProcess(document)
    solver_decision, = [x for x in solver_process.contentValues()
      if x.getCausalityValue().getTestedProperty() == property]
    # use Quantity Accept Solver.
    solver_decision.setSolverValue(self.portal.portal_solvers[solver])
    # configure for Accept Solver.
718
    solver_decision.updateConfiguration(tested_property_list=[property], **kw)
719 720
    solver_process.buildTargetSolverList()
    solver_process.solve()
721 722 723 724 725 726

  def stepCheckPackingListLineWithNewQuantityPrevision(self,sequence=None, sequence_list=None, **kw):
    """
      Look if the packing list has new previsions
    """
    packing_list_line = sequence.get('packing_list_line')
727
    self.assertEqual(packing_list_line.getQuantity(),self.default_quantity-1)
728 729 730 731 732 733

  def stepCheckPackingListLineWithNewQuantityPrevisionForMergedLine(self,sequence=None, sequence_list=None, **kw):
    """
      Look if the packing list has new previsions
    """
    packing_list_line = sequence.get('packing_list_line')
734
    self.assertEqual(packing_list_line.getQuantity(),(self.default_quantity-1)*2)
735

736
  def stepCheckPackingListLineWithNewResource(self,sequence=None, sequence_list=None, **kw):
737
    """
738
      Look if the packing list has new resource
739 740 741
    """
    packing_list_line = sequence.get('packing_list_line')
    new_resource = sequence.get('resource')
742 743
    self.assertEqual(packing_list_line.getQuantity(), self.default_quantity*2)
    self.assertEqual(packing_list_line.getResourceValue(), new_resource)
744 745 746
    simulation_line_list = packing_list_line.getDeliveryRelatedValueList()
    order_line_list = sum([x.getParentValue().getParentValue().getDeliveryList()
                           for x in simulation_line_list], [])
747
    self.assertEqual(sorted(packing_list_line.getCausalityList()),
748 749
                      sorted(order_line_list))

750 751 752
  def stepCheckPackingListLineWithPreviousResource(self, sequence=None):
    packing_list_line = sequence.get('packing_list_line')
    old_resource = sequence['resource_list'][-2]
753
    self.assertEqual(packing_list_line.getResourceValue(), old_resource)
754

755 756 757 758
  def stepCheckPackingListLineWithSameResource(self,sequence=None, sequence_list=None, **kw):
    """
      Look if the packing list has new previsions
    """
759 760
    old_packing_list_line = sequence.get('packing_list_line')
    packing_list_line = old_packing_list_line.aq_parent[str(int(old_packing_list_line.getId())-1)]
761
    resource = sequence.get('resource')
762
    for line in sequence.get('packing_list').getMovementList():
763 764 765
      self.assertEqual(line.getResourceValue(), resource)
      self.assertEqual(line.getQuantity(), self.default_quantity)
      self.assertEqual(line.getCausalityList(),
766 767
                        [x.getParentValue().getParentValue().getDelivery()
                         for x in line.getDeliveryRelatedValueList()])
768 769 770 771 772 773 774 775 776 777

  def stepCheckNewPackingListAfterStartDateAdopt(self,sequence=None, sequence_list=None, **kw):
    """
      Check if simulation movement are disconnected
    """
    applied_rule = sequence.get('applied_rule')
    packing_list_line = sequence.get('packing_list_line')
    packing_list = sequence.get('packing_list')
    LOG('CheckNewPackingList, self.datetime+15',0,self.datetime+15)
    LOG('CheckNewPackingList, packing_list.getStartDate',0,packing_list.getStartDate())
778 779
    self.assertEqual(packing_list_line.getQuantity(),self.default_quantity)
    self.assertEqual(packing_list.getStartDate(),self.datetime+15)
780 781
    simulation_movement_list = applied_rule.objectValues()
    resource_list = sequence.get('resource_list')
782
    self.assertEqual(len(simulation_movement_list),len(resource_list))
783 784 785 786 787 788 789 790
    delivery_value_list = []
    for simulation_movement in simulation_movement_list:
#      self.assertNotEquals(simulation_movement.getDeliveryValue(),None)
      delivery_value = simulation_movement.getDeliveryValue()
      if delivery_value not in delivery_value_list:
        delivery_value_list.append(delivery_value_list)
#      new_packing_list = delivery_value.getParent()
#      self.assertNotEquals(new_packing_list.getUid(),packing_list.getUid())
791
    self.assertEqual(len(delivery_value_list),len(resource_list))
792 793 794 795 796 797 798

  def stepCheckNewSplitPackingListAfterStartDateAdopt(self,sequence=None, sequence_list=None, **kw):
    """
      Check if simulation movement are disconnected
    """
    applied_rule = sequence.get('applied_rule')
    packing_list = sequence.get('packing_list')
799
    packing_list_line = [x for x in packing_list.getMovementList()
800 801
                         if x.getQuantity()][0]
    new_packing_list = self.portal.sale_packing_list_module[str(int(packing_list.getId())-1)]
802
    new_packing_list_line = [x for x in new_packing_list.getMovementList()
803
                             if x.getQuantity()][0]
804 805 806 807
    self.assertEqual(packing_list_line.getQuantity(),self.default_quantity)
    self.assertEqual(packing_list.getStartDate(),self.datetime+10)
    self.assertEqual(new_packing_list_line.getQuantity(),self.default_quantity)
    self.assertEqual(new_packing_list.getStartDate(),self.datetime+15)
808 809
    simulation_movement_list = applied_rule.objectValues()
    resource_list = sequence.get('resource_list')
810
    self.assertEqual(len(simulation_movement_list),len(resource_list))
811 812 813 814 815 816 817 818
    delivery_value_list = []
    for simulation_movement in simulation_movement_list:
#      self.assertNotEquals(simulation_movement.getDeliveryValue(),None)
      delivery_value = simulation_movement.getDeliveryValue()
      if delivery_value not in delivery_value_list:
        delivery_value_list.append(delivery_value_list)
#      new_packing_list = delivery_value.getParent()
#      self.assertNotEquals(new_packing_list.getUid(),packing_list.getUid())
819
    self.assertEqual(len(delivery_value_list),len(resource_list))
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863

  def stepAddPackingListContainer(self,sequence=None,
                                  packing_list=None,sequence_list=None, **kw):
    """
      Check if simulation movement are disconnected
    """
    if packing_list is None:
      packing_list = sequence.get('packing_list')
    container = packing_list.newContent(portal_type=self.container_portal_type)
    sequence.edit(container=container)

  def stepDefineNewPackingListContainer(self,sequence=None, sequence_list=None, **kw):
    """
      Check if simulation movement are disconnected
    """
    packing_list = sequence.get('new_packing_list')
    self.stepAddPackingListContainer(sequence=sequence,packing_list=packing_list)
    self.stepAddPackingListContainerLine(sequence=sequence)
    self.stepSetContainerLineFullQuantity(quantity=1,sequence=sequence)

  def stepAddPackingListContainerLine(self,sequence=None, sequence_list=None, **kw):
    """
      Add a container line in the packing list
    """
    container = sequence.get('container')
    container_line = container.newContent(portal_type=self.container_line_portal_type)
    sequence.edit(container_line=container_line)
    resource = sequence.get('resource')
    container_line.edit(resource_value=resource)

  def stepSetContainerLineSmallQuantity(self,sequence=None, sequence_list=None, **kw):
    """
      Set a small quantity on the container line, it should not be enough for
      the packing list to be packed.
    """
    container_line = sequence.get('container_line')
    container_line.edit(quantity=self.default_quantity-1)

  def stepCheckContainerLineSmallQuantity(self, sequence=None,
      sequence_list=None, **kw):
    """
      Checks that quantity is set correctly on the container_line.
    """
    container_line = sequence.get('container_line')
864 865
    self.assertEqual(self.default_quantity - 1, container_line.getQuantity())
    self.assertEqual(self.default_quantity - 1,
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
                      container_line.getTotalQuantity())

  def stepSetContainerLineFullQuantity(self,sequence=None, sequence_list=None,
                                       quantity=None,**kw):
    """
      Set the full quantity
    """
    container_line = sequence.get('container_line')
    if quantity is None:
      quantity = sequence.get('line_quantity',self.default_quantity)
    container_line.edit(quantity=quantity)
    container_line.immediateReindexObject()

  def stepSetContainerFullQuantity(self,sequence=None, sequence_list=None,
                                       quantity=None,**kw):
    """
      Really fills the container
    """
    packing_list = sequence.get('packing_list')
    container = sequence.get('container')
    #empty container
    container.deleteContent(container.contentIds())
    for line in packing_list.objectValues(
        portal_type=self.packing_list_line_portal_type):
      resource = line.getResourceValue()
      container_line = \
          container.newContent(portal_type=self.container_line_portal_type)
      container_line.setResourceValue(resource)
      # without variation
      if not line.hasCellContent():
        quantity = line.getQuantity()
        container_line.edit(quantity=quantity)
        container_line.immediateReindexObject()
899 900
        self.assertEqual(quantity, container_line.getQuantity())
        self.assertEqual(quantity, container_line.getTotalQuantity())
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
      # with variation
      elif line.hasCellContent():
        vcl = line.getVariationCategoryList()
        vcl.sort()
        base_id = 'movement'
        container_line.setVariationCategoryList(vcl)
        cell_key_list = list(line.getCellKeyList(base_id=base_id))
        cell_key_list.sort()
        for cell_key in cell_key_list:
          if line.hasCell(base_id=base_id, *cell_key):
            old_cell = line.getCell(base_id=base_id, *cell_key)
            cell = container_line.newCell(base_id=base_id,
                portal_type=self.container_cell_portal_type, *cell_key)
            cell.edit(mapped_value_property_list=['price', 'quantity'],
                price=old_cell.getPrice(),
                quantity=old_cell.getQuantity(),
                predicate_category_list=cell_key,
                variation_category_list=cell_key)
            cell.immediateReindexObject()
920 921
          self.assertEqual(old_cell.getQuantity(), cell.getQuantity())
          self.assertEqual(old_cell.getTotalQuantity(), cell.getTotalQuantity())
922

923 924
        self.assertEqual(line.getQuantity(), container_line.getQuantity())
        self.assertEqual(line.getTotalQuantity(), container_line.getTotalQuantity())
925 926

    # quantity is 1 on the container itself
927 928
    self.assertEqual(1, container.getQuantity())
    self.assertEqual(1, container.getTotalQuantity())
929 930 931 932 933 934 935

  def stepCheckPackingListIsNotPacked(self,sequence=None, sequence_list=None, **kw):
    """
      Check that the number of objects in containers are
      not equals to the quantity of the packing list
    """
    packing_list = sequence.get('packing_list')
936
    self.assertFalse(packing_list.isPacked())
937
    self.assertEqual('missing', packing_list.getContainerState())
938 939 940 941 942 943 944 945 946

  def stepCheckPackingListIsPacked(self,sequence=None, sequence_list=None,
                                   packing_list=None,**kw):
    """
      Check that the number of objects in containers are
      equals to the quantity of the packing list
    """
    if packing_list is None:
      packing_list = sequence.get('packing_list')
947
    self.commit()
948
    self.assertTrue(packing_list.isPacked())
949
    self.assertEqual('packed', packing_list.getContainerState())
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983

  def stepCheckNewPackingListIsPacked(self,sequence=None, sequence_list=None, **kw):
    """
      Check that the number of objects in containers are
      equals to the quantity of the packing list
    """
    packing_list = sequence.get('new_packing_list')
    self.stepCheckPackingListIsPacked(sequence=sequence,
                                      packing_list=packing_list)

  def stepCreateCurrency(self, sequence, **kw) :
    """Create a default currency. """
    currency_module = self.getCurrencyModule()
    if currency_module._getOb('EUR', None) is None:
      currency = self.getCurrencyModule().newContent(
          portal_type='Currency',
          id="EUR",
          base_unit_quantity=0.01,
          )
    else:
      currency = currency_module._getOb('EUR')
    sequence.edit(currency=currency)

  def stepSetOrderPriceCurrency(self, sequence, **kw) :
    """Set the price currency of the order.

    This step is not necessary.
    TODO : - include a test without this step.
           - include a test with this step late.
    """
    currency = sequence.get('currency')
    order = sequence.get('order')
    order.setPriceCurrency(currency.getRelativeUrl())

984 985 986 987 988 989 990 991 992 993
  def _checkRecordedProperty(self, movement_list, property_id, assertion):
    for movement in movement_list:
      for simulation_movement in movement.getDeliveryRelatedValueList():
        if assertion:
          self.assertTrue(simulation_movement.isPropertyRecorded(property_id))
        else:
          self.assertFalse(simulation_movement.isPropertyRecorded(property_id))

  def stepCheckSimulationMovementHasRecordedQuantity(self, sequence=None,
                                                     sequence_list=None):
994 995
    movement_list = sequence.get('packing_list').objectValues(
      portal_type=self.packing_list_line_portal_type)
996 997 998 999
    self._checkRecordedProperty(movement_list, 'quantity', True)

  def stepCheckSimulationMovementHasNoRecordedQuantity(self, sequence=None,
                                                       sequence_list=None):
1000 1001
    movement_list = sequence.get('packing_list').objectValues(
      portal_type=self.packing_list_line_portal_type)
1002 1003 1004 1005
    self._checkRecordedProperty(movement_list, 'quantity', False)

  def stepCheckSimulationMovementHasRecordedResource(self, sequence=None,
                                                     sequence_list=None):
1006 1007
    movement_list = sequence.get('packing_list').objectValues(
      portal_type=self.packing_list_line_portal_type)
1008 1009 1010 1011
    self._checkRecordedProperty(movement_list, 'resource', True)

  def stepCheckSimulationMovementHasNoRecordedResource(self, sequence=None,
                                                       sequence_list=None):
1012 1013
    movement_list = sequence.get('packing_list').objectValues(
      portal_type=self.packing_list_line_portal_type)
1014
    self._checkRecordedProperty(movement_list, 'resource', False)
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029

class TestPackingList(TestPackingListMixin, ERP5TypeTestCase) :

  run_all_test = 1
  quiet = 0

  def test_01_PackingListDecreaseQuantity(self, quiet=quiet, run=run_all_test):
    """
      Change the quantity on an delivery line, then
      see if the packing list is divergent and then
      split and defer the packing list
    """
    if not run: return
    sequence_list = SequenceList()

1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
    sequence_string = self.default_sequence + """
        DecreasePackingListLineQuantity
        CheckPackingListIsCalculating
        Tic
        CheckPackingListIsDiverged
        SplitAndDeferPackingList
        Tic
        CheckPackingListIsSolved
        CheckPackingListSplitted
        """
1040 1041 1042 1043 1044 1045 1046 1047
    sequence_list.addSequenceString(sequence_string)

    sequence_list.play(self, quiet=quiet)

  def test_02_PackingListChangeDestination(self, quiet=quiet, run=run_all_test):
    if not run: return
    sequence_list = SequenceList()

1048 1049 1050 1051 1052
    sequence_string = self.default_sequence + """
        ChangePackingListDestination
        CheckPackingListIsCalculating
        Tic
        CheckPackingListIsDiverged
1053
        AcceptDecisionDestination
1054 1055 1056 1057 1058
        Tic
        CheckPackingListIsSolved
        CheckPackingListIsNotDivergent
        CheckSimulationDestinationUpdated
        """
1059 1060 1061 1062 1063 1064 1065 1066
    sequence_list.addSequenceString(sequence_string)

    sequence_list.play(self, quiet=quiet)

  def test_03_PackingListChangeStartDate(self, quiet=quiet, run=run_all_test):
    if not run: return
    sequence_list = SequenceList()

1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
    sequence_string = self.default_sequence + """
        ChangePackingListStartDate
        CheckPackingListIsCalculating
        Tic
        CheckPackingListIsDiverged
        UnifyStartDateWithDecision
        Tic
        CheckPackingListIsSolved
        CheckPackingListIsNotDivergent
        CheckSimulationStartDateUpdated
        """
1078 1079 1080 1081 1082 1083 1084 1085
    sequence_list.addSequenceString(sequence_string)

    sequence_list.play(self, quiet=quiet)

  def test_04_PackingListDeleteLine(self, quiet=quiet, run=run_all_test):
    if not run: return
    sequence_list = SequenceList()

1086 1087 1088 1089 1090 1091 1092
    sequence_string = self.default_sequence + """
        CheckSimulationConnected
        DeletePackingListLine
        CheckPackingListIsNotDivergent
        Tic
        CheckSimulationDisconnected
        """
1093 1094 1095 1096 1097 1098 1099 1100
    sequence_list.addSequenceString(sequence_string)

    sequence_list.play(self, quiet=quiet)

  def test_05_SimulationChangeQuantity(self, quiet=quiet, run=run_all_test):
    if not run: return
    sequence_list = SequenceList()

1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
    sequence_string = self.default_sequence + """
        ModifySimulationLineQuantity
        Tic
        CheckPackingListIsDiverged
        AdoptPrevisionQuantity
        Tic
        CheckPackingListIsNotDivergent
        CheckPackingListIsSolved
        CheckPackingListLineWithNewQuantityPrevision
        """
1111 1112 1113 1114 1115 1116 1117 1118
    sequence_list.addSequenceString(sequence_string)

    sequence_list.play(self, quiet=quiet)

  def test_05a_SimulationChangeQuantityAndAcceptDecision(self, quiet=quiet, run=run_all_test):
    if not run: return
    sequence_list = SequenceList()

1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
    sequence_string = self.default_sequence + """
        ModifySimulationLineQuantity
        Tic
        CheckPackingListIsDiverged
        AcceptDecisionQuantity
        Tic
        CheckPackingListIsNotDivergent
        CheckPackingListIsSolved
        CheckSimulationQuantityUpdated
        """
1129 1130 1131 1132 1133 1134 1135 1136
    sequence_list.addSequenceString(sequence_string)

    sequence_list.play(self, quiet=quiet)

  def test_05b_SimulationChangeQuantityForMergedLine(self, quiet=quiet, run=run_all_test):
    if not run: return
    sequence_list = SequenceList()

1137
    sequence_string = self.default_sequence_with_duplicated_lines + """
1138 1139
        CheckTwoSimulationLines
        ModifySimulationLineQuantity
1140 1141 1142 1143 1144 1145 1146 1147
        Tic
        CheckPackingListIsDiverged
        AdoptPrevisionQuantity
        Tic
        CheckPackingListIsNotDivergent
        CheckPackingListIsSolved
        CheckPackingListLineWithNewQuantityPrevisionForMergedLine
        """
1148 1149 1150 1151 1152 1153 1154 1155
    sequence_list.addSequenceString(sequence_string)

    sequence_list.play(self, quiet=quiet)

  def test_05c_SimulationChangeQuantityAndAcceptDecisionForMergedLine(self, quiet=quiet, run=run_all_test):
    if not run: return
    sequence_list = SequenceList()

1156
    sequence_string = self.default_sequence_with_duplicated_lines + """
1157 1158
        CheckTwoSimulationLines
        ModifySimulationLineQuantity
1159 1160 1161 1162 1163 1164 1165 1166
        Tic
        CheckPackingListIsDiverged
        AcceptDecisionQuantity
        Tic
        CheckPackingListIsNotDivergent
        CheckPackingListIsSolved
        CheckSimulationQuantityUpdatedForMergedLine
        """
1167 1168 1169 1170 1171 1172 1173 1174
    sequence_list.addSequenceString(sequence_string)

    sequence_list.play(self, quiet=quiet)

  def test_05d_SimulationChangeResourceOnOneSimulationMovementForMergedLine(self, quiet=quiet, run=run_all_test):
    if not run: return
    sequence_list = SequenceList()

1175 1176 1177 1178 1179 1180 1181
    sequence_string = self.default_sequence_with_duplicated_lines + """
        CreateNotVariatedResource
        ModifyOneSimulationLineResource
        Tic
        CheckPackingListIsDiverged
        AdoptPrevisionResource
        Tic
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
        # Trying to Solve the divergence above with one simulation
        # movement changes the resource
        CheckPackingListLineWithNewResource
        # but doesn't solve the divergence as it is now divergent with the
        # other simulation movement
        CheckPackingListIsDiverged
        # solving again reverts the value.
        AdoptPrevisionResource
        Tic
        CheckPackingListLineWithPreviousResource
        # but now the packing list is divergent with the previous
        # simulation movement
        CheckPackingListIsDiverged
        # We have to chose one of them and accept the decision
        AcceptDecisionResource
        Tic
1198 1199 1200
        CheckPackingListIsNotDivergent
        CheckPackingListIsSolved
        """
1201 1202 1203 1204 1205 1206 1207 1208
    sequence_list.addSequenceString(sequence_string)

    sequence_list.play(self, quiet=quiet)

  def test_05e_SimulationUnifyResourceOnSimulationMovementsForNonMergedLines(self, quiet=quiet, run=run_all_test):
    if not run: return
    sequence_list = SequenceList()

1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
    sequence_string = self.default_sequence_with_two_lines + """
        ModifySimulationLineResource
        Tic
        CheckPackingListIsDiverged
        AdoptPrevisionResource
        Tic
        CheckPackingListIsNotDivergent
        CheckPackingListIsSolved
        CheckPackingListLineWithSameResource
        """
1219 1220 1221 1222 1223 1224 1225 1226
    sequence_list.addSequenceString(sequence_string)

    sequence_list.play(self, quiet=quiet)

  def test_05f_SimulationChangeAndPartialAcceptDecision(self, quiet=quiet, run=run_all_test):
    if not run: return
    sequence_list = SequenceList()

1227 1228
    sequence_string = self.default_sequence_with_duplicated_lines + """
        CreateNotVariatedResource
1229 1230
        CheckTwoSimulationLines
        ModifySimulationLineQuantity
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
        ModifyOneSimulationLineResource
        ModifySimulationLineStartDate
        Tic
        CheckPackingListIsDiverged
        AcceptDecisionQuantity
        Tic
        CheckPackingListIsDiverged
        AcceptDecisionResource
        Tic
        CheckPackingListIsDiverged
        UnifyStartDateWithDecision
        Tic
        CheckPackingListIsNotDivergent
        CheckPackingListIsSolved
        CheckSimulationQuantityUpdatedForMergedLine
        """
1247 1248 1249 1250
    sequence_list.addSequenceString(sequence_string)

    sequence_list.play(self, quiet=quiet)

1251 1252 1253 1254
  # The 3 following tests currently fail because they are making assertions on
  # an applied rule which with the new simulation structure is not the same as
  # in the original test packing list.

1255 1256 1257 1258
  def test_06_SimulationChangeStartDate(self, quiet=quiet, run=run_all_test):
    if not run: return
    sequence_list = SequenceList()

1259 1260 1261 1262 1263 1264 1265 1266 1267
    sequence_string = self.default_sequence + """
        ModifySimulationLineStartDate
        Tic
        CheckPackingListIsDiverged
        UnifyStartDateWithPrevision
        Tic
        CheckPackingListIsSolved
        CheckNewPackingListAfterStartDateAdopt
        """
1268 1269 1270 1271 1272 1273 1274 1275 1276
    # XXX Check if there is a new packing list created
    sequence_list.addSequenceString(sequence_string)

    sequence_list.play(self, quiet=quiet)

  def test_07_SimulationChangeStartDateWithTwoOrderLine(self, quiet=quiet, run=run_all_test):
    if not run: return
    sequence_list = SequenceList()

1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
    sequence_string = self.default_sequence_with_two_lines + """
        ModifySimulationLineStartDate
        Tic
        CheckPackingListIsDiverged
        CheckPackingListIsDivergent
        UnifyStartDateWithPrevision
        Tic
        CheckPackingListIsNotDivergent
        CheckPackingListIsSolved
        CheckNewPackingListAfterStartDateAdopt
        """
1288 1289 1290 1291 1292 1293 1294 1295 1296
    # XXX Check if there is a new packing list created
    sequence_list.addSequenceString(sequence_string)

    sequence_list.play(self, quiet=quiet)

  def test_07a_SimulationChangeStartDateWithTwoOrderLine(self, quiet=quiet, run=run_all_test):
    if not run: return
    sequence_list = SequenceList()

1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
    sequence_string = self.default_sequence_with_two_lines + """
        ModifyOneSimulationLineStartDate
        Tic
        CheckPackingListIsDiverged
        CheckPackingListIsDivergent
        UnifyStartDateWithPrevision
        Tic
        CheckPackingListIsNotDivergent
        CheckPackingListIsSolved
        CheckNewPackingListAfterStartDateAdopt
        """
1308 1309 1310 1311 1312 1313 1314 1315 1316
    # XXX Check if there is a new packing list created
    sequence_list.addSequenceString(sequence_string)

    sequence_list.play(self, quiet=quiet)

  def test_08_AddContainers(self, quiet=quiet, run=run_all_test):
    if not run: return
    sequence_list = SequenceList()

1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
    sequence_string = self.default_sequence + """
        AddPackingListContainer
        AddPackingListContainerLine
        SetContainerLineSmallQuantity
        CheckContainerLineSmallQuantity
        CheckPackingListIsNotPacked
        SetContainerFullQuantity
        Tic
        CheckPackingListIsPacked
        """
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
    # XXX Check if there is a new packing list created
    sequence_list.addSequenceString(sequence_string)

    sequence_list.play(self, quiet=quiet)

  def test_09_AddContainersWithVariatedResources(self, quiet=quiet, run=run_all_test):
    if not run: return
    sequence_list = SequenceList()

    # Test with a order with cells
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
    sequence_string = self.variated_default_sequence + """
        AddPackingListContainer
        AddPackingListContainerLine
        SetContainerLineSmallQuantity
        CheckContainerLineSmallQuantity
        CheckPackingListIsNotPacked
        SetContainerFullQuantity
        Tic
        CheckPackingListIsPacked
        ModifySimulationLineStartDate
        Tic
        CheckPackingListIsDiverged
        CheckPackingListIsDivergent
        UnifyStartDateWithPrevision
        Tic
        """
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
    # XXX Check if there is a new packing list created
    sequence_list.addSequenceString(sequence_string)

    sequence_list.play(self, quiet=quiet)

  def test_10_PackingListIncreaseQuantity(self, quiet=quiet, run=run_all_test):
    """
    - Increase the quantity on an delivery line
    - check if the packing list is divergent
    - Apply the "split and defer" solver to the packing list
    - check that nothing was splitted and the packing list is still divergent
      (reset the delta before, as we don't expect a modification)

    Basically, when we apply "split and defer" to a packing list, we don't
    want it to modify lines which have been increased.
    """
    if not run: return
    sequence_list = SequenceList()

1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
    sequence_string = self.default_sequence + """
        IncreasePackingListLineQuantity
        CheckPackingListIsCalculating
        Tic
        CheckPackingListIsDiverged
        SplitAndDeferPackingList
        Tic
        CheckPackingListIsDiverged
        CheckPackingListIsDivergent
        CheckPackingListNotSolved
        """
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
    sequence_list.addSequenceString(sequence_string)

    sequence_list.play(self, quiet=quiet)

  def test_11_PackingListDecreaseTwoTimesQuantityAndUpdateDelivery(self,
                                               quiet=quiet, run=run_all_test):
    """
      Change the quantity on an delivery line, then
      see if the packing list is divergent and then
      split and defer the packing list
    """
    if not run: return
    sequence_list = SequenceList()

1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
    sequence_string = self.default_sequence + """
        DecreasePackingListLineQuantity
        CheckPackingListIsCalculating
        Tic
        CheckPackingListIsDiverged
        SplitAndDeferPackingList
        Tic
        CheckPackingListIsSolved
        CheckPackingListSplitted
        DecreasePackingListLineQuantity
        CheckPackingListIsCalculating
        Tic
        CheckPackingListIsDiverged
        SplitAndDeferPackingList
        Tic
        CheckNewPackingListIsDivergent
        NewPackingListAdoptPrevisionQuantity
        Tic
        CheckPackingListIsSolved
1416
        CheckNewPackingListIsSolved
1417 1418
        CheckPackingListSplittedTwoTimes
        """
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
    sequence_list.addSequenceString(sequence_string)

    sequence_list.play(self, quiet=quiet)

  def test_SplitAndDeferDoNothing(self, quiet=quiet, run=run_all_test):
    """
    Use split & defer to solve a divergence, but choose do nothing for all
    lines.
    """
    if not run: return
    sequence_list = SequenceList()

1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
    sequence_string = self.default_sequence + """
        IncreasePackingListLineQuantity
        CheckPackingListIsCalculating
        Tic
        CheckPackingListIsDiverged
        SplitAndDeferDoNothingPackingList
        Tic
        CheckPackingListIsDiverged
        CheckPackingListIsDivergent
        CheckPackingListNotSolved
        """
1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
    sequence_list.addSequenceString(sequence_string)

    sequence_list.play(self, quiet=quiet)


  def test_12_PackingListLineChangeResource(self, quiet=quiet, run=run_all_test):
    """
    Test if delivery diverged when we change the resource.
    """
    if not run: return
    sequence_list = SequenceList()

1454 1455 1456 1457 1458 1459 1460
    sequence_string = self.default_sequence + """
        CreateNotVariatedResource
        ChangePackingListLineResource
        CheckPackingListIsCalculating
        Tic
        CheckPackingListIsDivergent
        """
1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
    sequence_list.addSequenceString(sequence_string)

    sequence_list.play(self, quiet=quiet)

  def test_14_PackingListHavePriceCurrencyCategory(self, quiet=quiet,
                                                   run=run_all_test):
    """Deliveries must have a price currency category. #252
    """
    if not run:
      return
    pl = self.getPortal().getDefaultModule(self.packing_list_portal_type
               ).newContent(portal_type=self.packing_list_portal_type)
1473
    self.assertTrue(hasattr(pl, 'getPriceCurrency'))
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487

  def test_PackingList_viewAsODT(self):
    # tests packing list printout
    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')
    packing_list = self.portal.getDefaultModule(self.packing_list_portal_type).newContent(
                              portal_type=self.packing_list_portal_type,
                              title='Packing List',
1488
                              specialise=self.business_process,
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
                              source_value=vendor,
                              source_section_value=vendor,
                              destination_value=client,
                              destination_section_value=client)
    line = packing_list.newContent(
                            portal_type=self.packing_list_line_portal_type,
                            resource_value=resource,
                            quantity=10,
                            price=3)
    packing_list.confirm()
    self.tic()

    odt = packing_list.PackingList_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))

  def test_15_CheckBuilderCanBeCalledTwiceSafely(self):
    """
1510 1511
    Builder design should allows to call the build method as many times as we
    want. Make sure that we will not have duplicated packing list if build is
1512 1513 1514 1515
    called several times.
    """
    delivery_builder = getattr(self.getPortalObject().portal_deliveries,
                               self.delivery_builder_id)
1516

1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
    def doNothing(self, *args, **kw):
      pass
    original_delivery_builder_build =  delivery_builder.__class__.build

    try:
      # We patch the delivery builder to make sure that it will not be
      # called by activities
      delivery_builder.__class__.build = doNothing
      sequence_list = SequenceList()

      sequence_string = self.confirmed_order_without_packing_list
      sequence_list.addSequenceString(sequence_string)
      sequence_list.play(self)

      # Now restore the build method and make sure first call returns document
      delivery_builder.__class__.build = original_delivery_builder_build
      self.assertTrue(len(delivery_builder.build()) > 0)
      # The second call should returns empty result even if tic not called
      self.assertTrue(len(delivery_builder.build()) ==  0)
    finally:
      delivery_builder.build = original_delivery_builder_build

  def test_16_simulation_reindexation_on_cancel(self):
    self.organisation_portal_type = 'Organisation'
    self.resource_portal_type = 'Product'

    packing_list_module = self.portal.getDefaultModule(
        portal_type=self.packing_list_portal_type)
    organisation_module = self.portal.getDefaultModule(
        portal_type=self.organisation_portal_type)
    resource_module = self.portal.getDefaultModule(
        portal_type=self.resource_portal_type)
    source = organisation_module.newContent(
        portal_type=self.organisation_portal_type)
    destination = organisation_module.newContent(
        portal_type=self.organisation_portal_type)
    resource = resource_module.newContent(
        portal_type=self.resource_portal_type)

    packing_list = packing_list_module.newContent(
        portal_type=self.packing_list_portal_type,
        source_value=source,
        destination_value=destination,
1560
        specialise=self.business_process,
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618
        start_date=DateTime())
    packing_list_line = packing_list.newContent(
        portal_type=self.packing_list_line_portal_type,
        resource_value=resource,
        quantity=1)
    packing_list.confirm()
    self.tic()
    self.assertEqual('confirmed', packing_list.getSimulationState())
    simulation_movement = packing_list_line.getDeliveryRelatedValue(
        portal_type='Simulation Movement')
    self.assertEqual('confirmed', simulation_movement.getSimulationState())
    packing_list.cancel()
    self.tic()
    self.assertEqual('cancelled', packing_list.getSimulationState())
    self.assertEqual('cancelled', simulation_movement.getSimulationState())

  def stepCreateSourceAccount(self, sequence=None, **kw):
    organisation = self.stepCreateOrganisation(sequence, None, 'dummy_source')
    sequence.edit(source_account = sequence.get('dummy_source')['bank'])

  def stepCreateDestinationAccount(self, sequence=None, **kw):
    organisation = self.stepCreateOrganisation(sequence, None,
        'dummy_destination')
    sequence.edit(destination_account = sequence.get('dummy_destination')
        ['bank'])

  def stepSetOrderLineSourceAccount(self, sequence=None, **kw):
    order_line = sequence.get('order_line')
    account = sequence.get('source_account')
    self.assertNotEqual(None, account)
    order_line.setSourceAccountValue(account)

  def stepSetOrderLineDestinationAccount(self, sequence=None, **kw):
    order_line = sequence.get('order_line')
    account = sequence.get('destination_account')
    self.assertNotEqual(None, account)
    order_line.setDestinationAccountValue(account)

  def stepCheckPackingListLineSourceAccount(self, sequence=None, **kw):
    packing_list = sequence.get('packing_list')
    account = sequence.get('source_account')
    for line in packing_list.getMovementList():
      self.assertEqual(line.getSourceAccountValue(), account)

  def stepCheckPackingListLineDestinationAccount(self, sequence=None, **kw):
    packing_list = sequence.get('packing_list')
    account = sequence.get('destination_account')
    for line in packing_list.getMovementList():
      self.assertEqual(line.getDestinationAccountValue(), account)

  def test_17_PackingListOrderLineWithAccount(self, quiet=quiet):
    """
      Check how packing list behaves if comes from order line which has
      source/destination account set.
    """
    sequence_list = SequenceList()

    sequence_string = self.default_order_sequence + """
1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
        CreateNotVariatedResource
        CreateSourceAccount
        CreateDestinationAccount
        Tic
        CreateOrderLine
        SetOrderLineResource
        SetOrderLineDefaultValues
        SetOrderLineSourceAccount
        SetOrderLineDestinationAccount
        OrderOrder
        Tic
        ConfirmOrder
        Tic
1632 1633
        PackingListBuilderAlarm
        Tic
1634 1635 1636 1637 1638 1639 1640
        CheckOrderSimulation
        CheckDeliveryBuilding
        CheckPackingListIsSolved
        CheckOrderPackingList
        CheckPackingListLineSourceAccount
        CheckPackingListLineDestinationAccount
        """
1641 1642 1643 1644
    sequence_list.addSequenceString(sequence_string)

    sequence_list.play(self, quiet=quiet)

1645 1646 1647 1648 1649 1650 1651 1652 1653
  def test_18_ChangeQuantityOnPackingListAndOrder(self, quiet=quiet):
    """
      Change the quantity on a packing list line, and accept the
      divergence, then change the quantity on an order line to the same
      value and check if it does not cause divergence on a packing list
      line and recorded properties are reset after re-expand.
    """
    sequence_list = SequenceList()

1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666
    sequence_string = self.default_sequence + """
        DecreasePackingListLineQuantity
        Tic
        CheckPackingListIsDiverged
        AcceptDecisionQuantity
        Tic
        CheckPackingListIsSolved
        CheckSimulationMovementHasRecordedQuantity
        DecreaseOrderLineQuantity
        Tic
        CheckPackingListIsSolved
        CheckSimulationMovementHasNoRecordedQuantity
        """
1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679
    sequence_list.addSequenceString(sequence_string)

    sequence_list.play(self, quiet=quiet)

  def test_19_ChangeResourceOnPackingListAndOrder(self, quiet=quiet):
    """
      Change the resource on a packing list line, and accept the
      divergence, then change the resource on an order line to the same
      value and check if it does not cause divergence on a packing list
      line and recorded properties are reset after re-expand.
    """
    sequence_list = SequenceList()

1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693
    sequence_string = self.default_sequence + """
        CreateNotVariatedResource
        ChangePackingListLineResource
        Tic
        CheckPackingListIsDiverged
        AcceptDecisionResource
        Tic
        CheckPackingListIsSolved
        CheckSimulationMovementHasRecordedResource
        ChangeOrderLineResource
        Tic
        CheckPackingListIsSolved
        CheckSimulationMovementHasNoRecordedResource
        """
1694 1695 1696 1697
    sequence_list.addSequenceString(sequence_string)

    sequence_list.play(self, quiet=quiet)

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
  def test_subcontent_reindexing_packing_list_line_cell(self):
    """Tests, that indexation of Packing List are propagated to subobjects
    during reindxation"""
    packing_list = self.portal.getDefaultModule(
        self.packing_list_portal_type).newContent(
            portal_type=self.packing_list_portal_type)
    packing_list_line = packing_list.newContent(
        portal_type=self.packing_list_line_portal_type)
    packing_list_cell = packing_list_line.newContent(
        portal_type=self.packing_list_cell_portal_type)
    self._testSubContentReindexing(packing_list, [packing_list_line,
      packing_list_cell])

  def test_subcontent_reindexing_packing_list_container_line_cell(self):
    """Tests, that indexation of Packing List are propagated to subobjects
    during reindxation, for Container, Container Line and Container Cell"""
    packing_list = self.portal.getDefaultModule(
        self.packing_list_portal_type).newContent(
            portal_type=self.packing_list_portal_type)
    container = packing_list.newContent(
        portal_type=self.container_portal_type)
    container_line = container.newContent(
        portal_type=self.container_line_portal_type)
    container_cell = container_line.newContent(
        portal_type=self.container_cell_portal_type)
    self._testSubContentReindexing(packing_list, [container, container_line,
      container_cell])

1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737
  def test_PackingList_getMovementListSorting(self):
    '''Test that is possible to sort getMovementList result passing it sort_on
    parameter
    '''
    packing_list = self.portal.getDefaultModule(self.packing_list_portal_type).newContent(
                              portal_type=self.packing_list_portal_type,
                              title='Packing List')
    # create some packing list lines
    line_bbb = packing_list.newContent(
                            portal_type=self.packing_list_line_portal_type,
                            reference='bbb',
                            int_index=1)
1738 1739 1740 1741 1742 1743 1744 1745
    line_bbb_cell_bbb = line_bbb.newContent(
                            portal_type=self.packing_list_cell_portal_type,
                            reference='bbb',
                            int_index=2)
    line_bbb_cell_aaa = line_bbb.newContent(
                            portal_type=self.packing_list_cell_portal_type,
                            reference='aaa',
                            int_index=1)
1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759
    line_aaa = packing_list.newContent(
                            portal_type=self.packing_list_line_portal_type,
                            reference='aaa',
                            int_index=2)
    line_ccc = packing_list.newContent(
                            portal_type=self.packing_list_line_portal_type,
                            reference='ccc',
                            int_index=4)
    line_ddd = packing_list.newContent(
                            portal_type=self.packing_list_line_portal_type,
                            reference='ddd',
                            int_index=3)
    self.tic()
    # check it's possible to sort by reference
1760
    reference_result = packing_list.getMovementList(sort_on=
1761
        [('reference', 'descending')])
1762
    self.assertEqual(reference_result, [line_ddd, line_ccc,
1763
      line_bbb_cell_bbb, line_bbb_cell_aaa, line_aaa])
1764 1765

    # check it's possible to sort by int_index
1766
    int_index_result = packing_list.getMovementList(sort_on=
1767
        [('int_index', 'ascending')])
1768
    self.assertEqual(int_index_result, [line_bbb_cell_aaa, line_bbb_cell_bbb,
1769
      line_aaa, line_ddd, line_ccc])
1770

1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785
  def test_subcontent_reindexing_container_line_cell(self):
    """Tests, that indexation of Packing List are propagated to subobjects
    during reindxation, for Container, Container Line and Container Cell"""
    packing_list = self.portal.getDefaultModule(
        self.packing_list_portal_type).newContent(
            portal_type=self.packing_list_portal_type)
    container = packing_list.newContent(
        portal_type=self.container_portal_type)
    container_line = container.newContent(
        portal_type=self.container_line_portal_type)
    container_cell = container_line.newContent(
        portal_type=self.container_cell_portal_type)
    self._testSubContentReindexing(container, [container_line,
      container_cell])

1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805
  def stepSetAssignmentOrderProfile(self,sequence=None, sequence_list=None, **kw):
    """
      Configure an Assingnment Order.
      This order represents that it transfers an ownership to somebody.
      In this case, source and destination are not moving.
    """
    organisation1 = sequence.get('organisation1')
    organisation2 = sequence.get('organisation2')
    project1 = sequence.get('project1')
    project2 = sequence.get('project2')
    order = sequence.get('order')
    order.edit(source_value = organisation1,
               source_section_value = organisation1,
               source_payment_value = organisation1['bank'],
               destination_value = organisation1,  # set same organisation
               destination_section_value = organisation2,
               destination_payment_value = organisation2['bank'],
               source_project_value = project1,
               destination_project_value = project2 )
    order.setPaymentConditionEfficiency(1.0)
1806
    self.assertTrue('Site Error' not in order.view())
1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835


  def testTransferOfOwnership(self, quiet=quiet):
    """
     Test that Packing List has a capability to represent transfer of an
     ownership(rights).
    """
    sequence_list = SequenceList()
    assignment_order_sequence = """
        CreateOrganisation1
        CreateOrganisation2
        CreateOrganisation3
        CreateProject1
        CreateProject2
        CreateOrder
        CreateCurrency
        SetOrderPriceCurrency
        SetAssignmentOrderProfile
    """
    sequence_string = assignment_order_sequence + """
        CreateNotVariatedResource
        Tic
        CreateOrderLine
        SetOrderLineResource
        SetOrderLineDefaultValues
        OrderOrder
        Tic
        ConfirmOrder
        Tic
1836 1837
        PackingListBuilderAlarm
        Tic
1838 1839 1840 1841 1842 1843 1844 1845
        CheckOrderSimulation
        CheckDeliveryBuilding
        CheckPackingListIsNotDivergent
        CheckOrderPackingList
    """
    sequence_list.addSequenceString(sequence_string)
    sequence_list.play(self, quiet=quiet)

1846 1847 1848 1849 1850
class TestSolvingPackingList(TestPackingListMixin, ERP5TypeTestCase):
  quiet = 0

  def afterSetUp(self, quiet=1, run=1):
    TestPackingListMixin.afterSetUp(self)
1851 1852 1853
    solver_process_type_info = self.portal.portal_types['Solver Process']
    self.original_allowed_content_types = \
      solver_process_type_info.getTypeAllowedContentTypeList()
1854 1855
    self.added_target_solver_list = []

1856
  @UnrestrictedMethod
1857
  def beforeTearDown(self, quiet=1, run=1):
1858
    super(TestSolvingPackingList, self).beforeTearDown()
1859
    self.portal.portal_rules.new_delivery_simulation_rule.quantity_tester.edit(
1860
      solver=())
1861 1862
    self.portal.portal_types['Solver Process'].setTypeAllowedContentTypeList(
      self.original_allowed_content_types)
1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873
    self.portal.portal_solvers.manage_delObjects(self.added_target_solver_list)
    self.tic()

  @UnrestrictedMethod
  def _setUpTargetSolver(self, solver_id, solver_class, tested_property_list):
    solver_tool = self.portal.portal_solvers
    solver = solver_tool.newContent(
      portal_type='Solver Type',
      id=solver_id,
      tested_property_list=tested_property_list,
      automatic_solver=1,
1874
      type_class=solver_class,
1875 1876 1877 1878 1879 1880 1881
      type_group_list=('target_solver',),
    )
    solver.setCriterion(property='portal_type',
                        identity=['Simulation Movement',])
    solver.setCriterionProperty('portal_type')
    solver_process_type_info = self.portal.portal_types['Solver Process']
    solver_process_type_info.setTypeAllowedContentTypeList(
1882
      solver_process_type_info.getTypeAllowedContentTypeList() +
1883 1884 1885 1886 1887 1888 1889 1890 1891
      [solver_id]
    )
    (default_chain, chain_dict) = getChainByType(self.portal)
    chain_dict['chain_%s' % solver_id] = 'solver_workflow'
    self.portal.portal_workflow.manage_changeWorkflows(default_chain,
                                                       props=chain_dict)
    self.portal.portal_caches.clearAllCache()
    self.added_target_solver_list.append(solver_id)

1892
  @UnrestrictedMethod
1893 1894 1895
  def stepSetUpAutomaticQuantityAcceptSolver(self, sequence=None, sequence_list=None):
    self._setUpTargetSolver('Automatic Quantity Accept Solver',
                            'AcceptSolver', ('quantity',))
1896
    self.portal.portal_rules.new_delivery_simulation_rule.quantity_tester.edit(
1897 1898
      solver=('portal_solvers/Automatic Quantity Accept Solver',))

1899
  @UnrestrictedMethod
1900 1901 1902
  def stepSetUpAutomaticQuantityAdoptSolver(self, sequence=None, sequence_list=None):
    self._setUpTargetSolver('Automatic Quantity Adopt Solver',
                            'AdoptSolver', ('quantity',))
1903
    self.portal.portal_rules.new_delivery_simulation_rule.quantity_tester.edit(
1904 1905
      solver=('portal_solvers/Automatic Quantity Adopt Solver',))

1906
  @UnrestrictedMethod
1907 1908 1909 1910 1911 1912 1913 1914
  def stepSetUpMovementSplitSolver(self, sequence=None, sequence_list=None):
    self._setUpTargetSolver('Movement Split Solver',
                            'MovementSplitSolver', ())

  def stepSplitMovementWithVariatedResources(self, sequence=None,
                                             sequence_list=None):
    packing_list = sequence.get('packing_list')
    simulation_movement_list = sum(
1915
      [x.getDeliveryRelatedValueList() for x in
1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930
       packing_list.getMovementList()[:10]], [])
    solver_process = self.portal.portal_solver_processes.newContent(
      portal_type='Solver Process')
    target_solver = solver_process.newContent(
      portal_type='Movement Split Solver',
      delivery_value_list=simulation_movement_list)
    target_solver.solve()

  def stepCheckSplitMovementWithVariatedResources(self, sequence=None,
                                                  sequence_list=None):
    packing_list = sequence.get('packing_list')
    order = packing_list.getCausalityValue()
    new_packing_list = filter(lambda x:x != packing_list,
                              order.getCausalityRelatedValueList(
      portal_type=packing_list.getPortalType()))[0]
1931
    self.assertEqual(len(packing_list.getMovementList()),
1932
                      len(order.getMovementList()) - 10)
1933
    self.assertEqual(len(new_packing_list.getMovementList()), 10)
1934 1935 1936 1937 1938 1939 1940 1941 1942

  def test_01_PackingListDecreaseQuantity(self, quiet=quiet):
    """
      Change the quantity on an delivery line, then
      see if the packing list is solved automatically
      with accept solver.
    """
    sequence_list = SequenceList()

1943 1944 1945 1946 1947 1948 1949 1950
    sequence_string = """
        SetUpAutomaticQuantityAcceptSolver
        """ + self.default_sequence + """
        DecreasePackingListLineQuantity
        CheckPackingListIsCalculating
        Tic
        CheckPackingListIsSolved
        """
1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962
    sequence_list.addSequenceString(sequence_string)

    sequence_list.play(self, quiet=quiet)

  def test_02_PackingListDecreaseQuantity(self, quiet=quiet):
    """
      Change the quantity on an delivery line, then
      see if the packing list is solved automatically
      with adopt solver.
    """
    sequence_list = SequenceList()

1963 1964 1965 1966 1967 1968 1969 1970
    sequence_string = """
        SetUpAutomaticQuantityAdoptSolver
        """ + self.default_sequence + """
        DecreasePackingListLineQuantity
        CheckPackingListIsCalculating
        Tic
        CheckPackingListIsSolved
        """
1971 1972 1973 1974 1975 1976 1977 1978
    sequence_list.addSequenceString(sequence_string)

    sequence_list.play(self, quiet=quiet)

  def test_09_AddContainersWithVariatedResources(self, quiet=quiet):
    sequence_list = SequenceList()

    # Test with a order with cells
1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993
    sequence_string = """
        SetUpMovementSplitSolver
        """ + self.variated_default_sequence + """
        AddPackingListContainer
        AddPackingListContainerLine
        SetContainerLineSmallQuantity
        CheckContainerLineSmallQuantity
        CheckPackingListIsNotPacked
        SetContainerFullQuantity
        Tic
        CheckPackingListIsPacked
        SplitMovementWithVariatedResources
        Tic
        CheckSplitMovementWithVariatedResources
        """
1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025
    # XXX Check if there is a new packing list created
    sequence_list.addSequenceString(sequence_string)

    sequence_list.play(self, quiet=quiet)

class TestPurchasePackingListMixin(TestPackingListMixin):
  """Mixing class with steps to test purchase packing lists.
  """
  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'
  delivery_builder_id = 'purchase_packing_list_builder'
  container_portal_type = None
  container_line_portal_type = None
  container_cell_portal_type = None

  # all steps related to packing and container does not apply on purchase
  def ignored_step(self, **kw):
    return
  stepAddPackingListContainer = ignored_step
  stepDefineNewPackingListContainer = ignored_step
  stepAddPackingListContainerLine = ignored_step
  stepSetContainerLineSmallQuantity = ignored_step
  stepCheckContainerLineSmallQuantity = ignored_step
  stepSetContainerLineFullQuantity = ignored_step
  stepSetContainerFullQuantity = ignored_step
  stepCheckPackingListIsNotPacked = ignored_step
  stepCheckPackingListIsPacked = ignored_step
  stepCheckNewPackingListIsPacked = ignored_step
2026

2027 2028 2029
  def test_subcontent_reindexing_packing_list_container_line_cell(self):
    """No need to check Containers in Purchase Packing List"""

2030 2031 2032
  def test_subcontent_reindexing_container_line_cell(self):
    """No need to check Containers in Purchase Packing List"""

2033 2034 2035
class TestPurchasePackingList(TestPurchasePackingListMixin, TestPackingList):
  """Tests for purchase packing list.
  """
2036 2037


2038 2039 2040 2041 2042 2043
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestPackingList))
  suite.addTest(unittest.makeSuite(TestSolvingPackingList))
  suite.addTest(unittest.makeSuite(TestPurchasePackingList))
  return suite