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

import unittest

33

34 35 36 37
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from zLOG import LOG
from Products.ERP5Type.tests.Sequence import SequenceList
from testOrder import TestOrderMixin
38
from Products.ERP5.tests.utils import newSimulationExpectedFailure
39 40 41 42 43 44 45

class TestProductionOrderMixin(TestOrderMixin):

  run_all_test = 1
  resource_portal_type = 'Product'
  order_portal_type = 'Production Order'
  order_line_portal_type = 'Production Order Line'
46
  order_cell_portal_type = 'Production Order Cell'
47 48 49 50 51 52 53 54 55
  supply_chain_portal_type = 'Supply Chain'
  supply_node_portal_type = 'Supply Node'
  supply_link_portal_type = 'Supply Link'
  component_portal_type = 'Component'
  transformation_portal_type = 'Transformation'
  transformed_resource_portal_type = \
                        'Transformation Transformed Resource'
  operation_line_portal_type = 'Transformation Operation'

56
  operation_category_list = ['operation1', 'operation2']
57 58
  colour_variation_portal_type = 'Product Individual Variation'
  morphology_variation_portal_type = 'Product Individual Variation'
59 60 61 62 63

  colour_list = ['green','blue']
  mrp_size_list = ['Man','Woman']


64 65 66
  def getBusinessTemplateList(self):
    """
    """
67 68
    return ('erp5_base','erp5_pdm', 'erp5_simulation', 'erp5_trade', 'erp5_mrp',
            'erp5_simulation_test')
69

70
  def setUpPreferences(self):
71
    system_preference = self.portal.portal_preferences.newContent(
72 73 74 75 76
      portal_type = 'System Preference'
    )
    system_preference.edit(
      preferred_product_individual_variation_base_category = ('variation',),
      preferred_component_individual_variation_base_category = ('variation',),
77
      preferred_product_variation_base_category = ('industrial_phase', 'colour', 'size'),
78
      preferred_component_variation_base_category = ('colour', 'size'),
79 80 81 82 83 84 85 86 87
      priority = 1,
    )
    system_preference.enable()
    self.tic()

  def afterSetUp(self):
    TestOrderMixin.afterSetUp(self)
    self.setUpPreferences()

88
  def createCategories(self):
89 90
    """
      Light install create only base categories, so we create
91 92 93 94
      some categories for testing them
    """
    TestOrderMixin.createCategories(self)
    if len(self.category_tool.operation.contentValues()) == 0:
95
      for category_id in self.operation_category_list:
96 97 98 99
        o = self.category_tool.operation.newContent(
                                               portal_type='Category',
                                               id=category_id)

100
  def stepCreateColourSizeVariatedComponent1(self,sequence=None, sequence_list=None, \
101 102
                                    **kw):
    """
103
      Create a resource with colour and size variation
104 105 106 107 108 109 110
    """
    portal = self.getPortal()
    resource_module = portal.getDefaultModule(self.component_portal_type)
    resource = resource_module.newContent(
                                  portal_type=self.component_portal_type)
    resource.edit(
      title = "ColourSizeVariatedComponent1",
111
      variation_base_category_list = ['colour','size'],
112
    )
113
    resource.setVariationCategoryList(['size/'+q for q in self.mrp_size_list] + ['colour/'+q for q in self.colour_list])
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    sequence.edit(component1=resource)

  def stepCreateColourSizeVariatedResource(self, sequence=None, sequence_list=None, \
                                 **kw):
    """
      Create a resource with colour and size variation
    """
    portal = self.getPortal()
    resource_module = portal.getDefaultModule(self.resource_portal_type)
    resource = resource_module.newContent(portal_type=self.resource_portal_type)
    resource.edit(
      title = "ColourSizeVariatedResource",
      industrial_phase_list=["phase1", "phase2"],
      variation_base_category_list = ['size','colour'],
    )
    resource.setVariationCategoryList(['size/'+q for q in self.mrp_size_list] + ['colour/'+q for q in self.colour_list])

    sequence.edit( resource = resource )
    resource_list = sequence.get('resource_list',default=[])
    resource_list.append(resource)
    sequence.edit( resource_list = resource_list )

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
  def stepCreateVariatedResource(self, sequence=None, sequence_list=None, \
                                 **kw):
    """
      Create a resource with variation
    """
    portal = self.getPortal()
    resource_module = portal.getDefaultModule(self.resource_portal_type)
    resource = resource_module.newContent(portal_type=self.resource_portal_type)
    resource.edit(
      title = "VariatedResource",
      industrial_phase_list=["phase1", "phase2"],
      product_line = 'apparel'
    )
#    resource.setSizeList(self.size_list)
    # Add colour variation
151

152 153
    colour_variation_count = 3
    for i in range(colour_variation_count):
154
      variation = resource.newContent(portal_type = self.colour_variation_portal_type)
155 156 157 158 159 160 161
      variation.edit(
        title = 'ColourVariation%s' % str(i),
        variation_base_category_list = ('variation',)
      )
    # Add morphology variation
    morphology_variation_count = 2
    for i in range(morphology_variation_count) :
162
      variation = resource.newContent(portal_type=self.morphology_variation_portal_type)
163 164 165 166 167 168 169 170 171 172
      variation.edit(
        title = 'MorphologyVariation%s' % str(i),
        variation_base_category_list = ('variation',)
      )

    sequence.edit( resource = resource )
    resource_list = sequence.get('resource_list',default=[])
    resource_list.append(resource)
    sequence.edit( resource_list = resource_list )

173
  def stepCreateProductionOrganisation1(self, sequence=None, sequence_list=None,
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
                                        **kw):
    """
      Create a organisation for supply
    """
    self.stepCreateOrganisation(sequence=sequence, sequence_list=sequence_list,
                                title='production_organisation1', **kw)

  # Note: SC means Supply Chain
  def stepCreateGenericSC(self, sequence=None, sequence_list=None, **kw):
    """
      Create a empty Supply Chain
    """
    portal = self.getPortal()
    supply_chain_module = portal.getDefaultModule( \
                                   portal_type=self.supply_chain_portal_type)
    supply_chain = supply_chain_module.newContent( \
                                   portal_type=self.supply_chain_portal_type)
    supply_chain.edit(
192
      title = "Supply Chain Generic",
193 194 195
    )
    sequence.edit(supply_chain=supply_chain)

196
  def stepCreateProductionSC(self, sequence=None, sequence_list=None,
197 198 199 200 201 202 203 204
                             **kw):
    """
      Create a empty organisation
    """
    # Create supply chain
    self.stepCreateGenericSC(sequence=sequence, sequence_list=sequence_list,
                             **kw)
    supply_chain = sequence.get('supply_chain')
205 206 207
    supply_chain.edit(
      title = 'Supply Chain Production'
    )
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
    # Create production node
    production_organisation  = sequence.get('production_organisation1')
    production_node = supply_chain.newContent(
                         portal_type=self.supply_node_portal_type)
    production_node.edit(
      destination_value=production_organisation
    )
    sequence.edit(production_node1=production_node)
    # Create production link
    production_link = supply_chain.newContent(
                         portal_type=self.supply_link_portal_type)
    production_link.edit(
      destination_value=production_node,
      destination_section_value=production_organisation,
      min_delay=5,
      max_delay=6,
      deliverable=1,
      industrial_phase_list=["phase1"]
    )

  def stepCreateComponent1(self,sequence=None, sequence_list=None, \
                                    **kw):
    """
      Create a resource with no variation
    """
    portal = self.getPortal()
    resource_module = portal.getDefaultModule(self.component_portal_type)
    resource = resource_module.newContent(
                                  portal_type=self.component_portal_type)
    resource.edit(
      title = "Component1"
    )
    sequence.edit(component1=resource)

  def stepCreateComponent2(self,sequence=None, sequence_list=None, \
                                    **kw):
    """
      Create a resource with no variation
    """
    portal = self.getPortal()
    resource_module = portal.getDefaultModule(self.component_portal_type)
    resource = resource_module.newContent(
                                  portal_type=self.component_portal_type)
    resource.edit(
      title = "Component2"
    )
    sequence.edit(component2=resource)

  def stepInvalidateTransformation(self, sequence=None, sequence_list=None,
                               **kw):
    transformation = sequence.get('transformation')

    workflow_tool = self.getPortal().portal_workflow

    workflow_tool.doActionFor(
        transformation,
        'invalidate_action'
    )

267
    self.assertEqual('invalidated',transformation.getValidationState())
268 269 270 271 272 273 274 275 276 277 278 279

  def stepValidateTransformation(self, sequence=None, sequence_list=None,
                               **kw):
    transformation = sequence.get('transformation')

    workflow_tool = self.getPortal().portal_workflow

    workflow_tool.doActionFor(
        transformation,
        'validate_action'
    )

280
    self.assertEqual('validated',transformation.getValidationState())
281

282 283 284 285 286 287 288 289 290 291
  def stepCreateEmptyTransformation(self, sequence=None, sequence_list=None,
                               **kw):
    portal = self.getPortal()
    transformation_module = portal.getDefaultModule(
                                     self.transformation_portal_type)
    transformation = transformation_module.newContent(
                                   portal_type=self.transformation_portal_type)
    sequence.edit(transformation=transformation)


292 293
  def stepSetTransformationTransformedResourceQuantityMatrix(self, sequence=None, sequence_list=None,
                               **kw):
294 295 296
    """
      Fills variation based quantity matrix
    """
297 298 299 300

    transformation_transformed_resource = sequence.get('transformation_transformed_resource')

    for colour in self.colour_list:
301
      colour_path = 'colour/%s' % colour
302
      for size in self.mrp_size_list:
303
        size_path = 'size/%s' % size
Nicolas Dumazet's avatar
Nicolas Dumazet committed
304
        transformation_transformed_resource.newCell(
305 306 307
          colour_path,
          size_path,
          mapped_value_property_list = ('quantity',),
308
          quantity = self.colour_size_quantity_dict[colour][size],
309 310
          membership_criterion_base_category= ('size', 'colour', ),
          membership_criterion_category= (size_path, colour_path,),
Nicolas Dumazet's avatar
Nicolas Dumazet committed
311
          base_id="quantity",
312 313 314 315
        )

  def stepSetTransformationTransformedResourceVariationMatrix(self, sequence=None, sequence_list=None,
                               **kw):
316 317 318
    """
      Fills variation matrix
    """
319 320 321 322

    transformation_transformed_resource = sequence.get('transformation_transformed_resource')

    for colour in self.colour_list:
323
      colour_path = 'colour/%s' % colour
324
      for size in self.mrp_size_list:
325
        size_path = 'size/%s' % size
Nicolas Dumazet's avatar
Nicolas Dumazet committed
326
        transformation_transformed_resource.newCell(
327 328
          colour_path,
          size_path,
329
          categories = self.colour_size_variation_dict[colour][size],
330 331
          membership_criterion_base_category= ('size', 'colour', ),
          membership_criterion_category= (size_path, colour_path,),
Nicolas Dumazet's avatar
Nicolas Dumazet committed
332
          base_id="variation",
333 334
        )

335 336
  def stepSetOrderLineQuantityMatrix(self, sequence=None, sequence_list=None,
                               **kw):
337 338 339
    """
      Fills variation based quantity matrix
    """
340 341 342
    order_line = sequence.get('order_line')

    for colour in self.colour_list:
343
      colour_path = 'colour/%s' % colour
344
      for size in self.mrp_size_list:
345
        size_path = 'size/%s' % size
Nicolas Dumazet's avatar
Nicolas Dumazet committed
346
        order_line.newCell(
347 348 349
          size_path,
          colour_path,
          mapped_value_property_list = ('quantity',),
Nicolas Dumazet's avatar
Nicolas Dumazet committed
350
          quantity = self.order_line_colour_size_quantity_dict[colour][size],
351 352 353
          categories = [size_path, colour_path],
          membership_criterion_base_category= ('size', 'colour', ),
          membership_criterion_category= (size_path, colour_path,),
Nicolas Dumazet's avatar
Nicolas Dumazet committed
354
          base_id='movement',
355 356 357 358 359 360 361 362 363 364
        )

  def stepSetOrderLineVariationCategories(self, sequence=None, sequence_list=None,
                               **kw):
    order_line = sequence.get('order_line')

    order_line.setVariationCategoryList(
      value = self.order_line_variation_category_list
    )

365 366
  def stepSetTransformationTransformedResourceVariation(self, sequence=None, sequence_list=None,
                               **kw):
367 368 369
    """
      Fills categories of variation
    """
370 371 372 373 374 375 376 377 378 379

    transformation_transformed_resource = sequence.get('transformation_transformed_resource')

    transformation_transformed_resource.edit(
        v_variation_base_category_list = self.variation_category_list,
        q_variation_base_category_list = self.variation_category_list,
    )

  def stepSetTransformationVariation(self, sequence=None, sequence_list=None,
                               **kw):
380 381 382
    """
      Fills categories of variation
    """
383
    transformation = sequence.get('transformation')
384
    transformation.setVariationBaseCategoryList(self.variation_category_list)
385

386 387 388 389 390 391
  def stepFillTransformationWithResource(self, sequence=None, sequence_list=None,
                               **kw):

    transformation = sequence.get('transformation')
    resource = sequence.get('resource')
    self.assertNotEquals(None, resource)
392
    transformation.setResourceValue(resource)
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442

  def stepSetOrderLineQuantity(self, sequence=None, sequence_list=None,
                               **kw):
    order_line = sequence.get('order_line')
    order_line.edit(
        quantity = self.production_order_line_quantity
    )

  def stepSetTransformationTransformedResourceQuantity(self, sequence=None, sequence_list=None,
                               **kw):
    transformation_transformed_resource = sequence.get('transformation_transformed_resource')
    transformation_transformed_resource.edit(
      quantity = self.transformation_transformed_resource_quantity
    )

  def stepSetTransformationTransformedResourceEfficiency(self, sequence=None, sequence_list=None,
                               **kw):
    transformation_transformed_resource = sequence.get('transformation_transformed_resource')
    transformation_transformed_resource.edit(
      efficiency = self.transformation_transformed_resource_efficiency
    )

  def stepSetTransformationTransformedResourceIndustrialPhaseList(self, sequence=None, sequence_list=None,
                               **kw):
    transformation_transformed_resource = sequence.get('transformation_transformed_resource')
    transformation_transformed_resource.edit(
      industrial_phase_list = self.transformation_transformed_resource_industrial_phase_list
    )

  def stepFillTransformationTransformedResourceWithComponent1(self, sequence=None, sequence_list=None,
                               **kw):
    transformation_transformed_resource = sequence.get('transformation_transformed_resource')
    component1 = sequence.get('component1')

    self.assertNotEquals(None, component1)

    transformation_transformed_resource.edit(
      resource_value = component1,
    )

  def stepCreateTransformationTransformedResource(self, sequence=None, sequence_list=None,
                               **kw):

    transformation = sequence.get('transformation')
    transformation_transformed_resource = transformation.newContent(
      portal_type = self.transformed_resource_portal_type,
        )

    sequence.edit(transformation_transformed_resource = transformation_transformed_resource)

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 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
  def stepCreateTransformation(self, sequence=None, sequence_list=None,
                               **kw):
    """
      Create a transformation
    """
    # Create transformation
    portal = self.getPortal()
    transformation_module = portal.getDefaultModule(
                                     self.transformation_portal_type)
    transformation = transformation_module.newContent(
                                   portal_type=self.transformation_portal_type)
    sequence.edit(transformation=transformation)
    # Set resource
    resource = sequence.get('resource')
    transformation.setResourceValue(resource)
    # Create operation line 1
    operation_line = transformation.newContent(
        portal_type=self.operation_line_portal_type)
    operation_line.edit(
        # FIXME hardcoded
        quantity=2,
        resource_value=portal.portal_categories.resolveCategory(
                                     'operation/operation1'),
        industrial_phase_list=['phase1']
    )

    # Create operation line 2
    operation_line = transformation.newContent(
        portal_type=self.operation_line_portal_type)
    operation_line.edit(
        # FIXME hardcoded
        quantity=3,
        resource_value=portal.portal_categories.resolveCategory(
                                     'operation/operation2'),
        industrial_phase_list=['phase2']
    )
    # Create transformed resource line 1
    line = transformation.newContent(
        portal_type=self.transformed_resource_portal_type)
    line.edit(
        # FIXME hardcoded
        quantity=6,
        resource_value=sequence.get('component1'),
        industrial_phase_list=['supply_phase1']
    )
    # Create transformed resource line 2
    line = transformation.newContent(
        portal_type=self.transformed_resource_portal_type)
    line.edit(
        # FIXME hardcoded
        quantity=7,
        resource_value=sequence.get('component2'),
        industrial_phase_list=['supply_phase2']
    )

  def stepCreateOrder(self, sequence=None, sequence_list=None, **kw):
    """
500
      Create a filled order
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
    """
    portal = self.getPortal()
    order_module = portal.getDefaultModule(portal_type=self.order_portal_type)
    order = order_module.newContent(portal_type=self.order_portal_type)
    organisation = sequence.get('organisation')
    supply_chain = sequence.get('supply_chain')
    order.edit(
      start_date = self.datetime + 10,
      stop_date = self.datetime + 20,
      destination_value=organisation,
      destination_section_value=organisation,
      specialise_value=supply_chain
    )
    sequence.edit(order=order)

  def stepCreateOrderLine(self, sequence=None, sequence_list=None, **kw):
    """
518
      Create a filled order line
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
    """
    order = sequence.get('order')
    order_line = order.newContent(portal_type=self.order_line_portal_type)
    resource = sequence.get('resource')
    transformation = sequence.get('transformation')
    order_line.edit(
      resource_value=resource,
      specialise_value=transformation,
      quantity=5
    )
    sequence.edit(order_line=order_line)

  def stepCreateOrderLineWithoutTransformation(self, sequence=None, sequence_list=None, **kw):
    """
      Create a empty order line
    """
    order = sequence.get('order')
    order_line = order.newContent(portal_type=self.order_line_portal_type)
    resource = sequence.get('resource')
    order_line.edit(
      resource_value=resource,
      quantity=5
    )
    sequence.edit(order_line=order_line)

544 545 546 547 548
  def stepCheckVariationSimulation(self, sequence=None, sequence_list=None, **kw):
    # XXX: This check is not testing too much, beside for variation system
    #      used in production simulations.
    order = sequence.get('order')

Nicolas Dumazet's avatar
Nicolas Dumazet committed
549 550 551 552
    movement_list = order.getMovementList()
    self.assertNotEqual(len(movement_list), 0)

    for order_movement in movement_list:
553 554 555
      size = order_movement.getSize()
      colour = order_movement.getColour()

556 557 558
      self.assertNotEqual(size, None)
      self.assertNotEqual(colour, None)

559 560 561
      want_produced_quantity = self.order_line_colour_size_quantity_dict[colour][size]
      want_consume_quantity = self.colour_size_quantity_dict[colour][size]
      want_consume_for_production = want_produced_quantity * want_consume_quantity
562

563
      order_simulation_movement = order_movement.getDeliveryRelatedValue(\
564
          portal_type='Simulation Movement')
565
      produced_movement = order_simulation_movement.contentValues()[0].contentValues()[0]
566

567
      self.assertEqual(
568 569 570 571 572 573 574 575 576 577 578
        want_produced_quantity,
        produced_movement.getQuantity()
      )

      transformation_rule = produced_movement.contentValues()[0]

      consumption_movement = [q for q in transformation_rule.contentValues() \
          if q.getId().startswith('cr')][0]
      production_delivery_movement = [q for q in transformation_rule.contentValues() \
          if q.getId().startswith('pr')][0]

579
      self.assertEqual(
580 581 582 583
          want_consume_for_production,
          consumption_movement.getQuantity()
      )

584
      self.assertEqual(
585 586 587 588 589 590 591 592
        want_produced_quantity,
        production_delivery_movement.getQuantity()
      )

      transformation_sourcing_rule = consumption_movement.contentValues()[0]

      consume_delivery_movement = transformation_sourcing_rule.contentValues()[0]

593
      self.assertEqual(
594 595 596 597
        want_consume_for_production,
        consume_delivery_movement.getQuantity()
      )

598 599 600
  def stepCheckEfficiencySimulation(self, sequence=None, sequence_list=None, **kw):
    """Check that efficiency is applied where is it needed"""

601
    # XXX: This check is not testing too much, beside for efficiency related quantity
602 603 604 605 606
    #      in just two places.
    order = sequence.get('order')

    applied_rule = order.getCausalityRelatedValue(portal_type = self.applied_rule_portal_type)

607
    order_simulation_movement_list = applied_rule.contentValues()
608 609

    # XXX: hardcode
610
    self.assertEqual(
611
        1,
612
        len(order_simulation_movement_list)
613 614
    )

615 616 617
    order_simulation_movement = order_simulation_movement_list[0]
    production_applied_rule = order_simulation_movement.contentValues()[0]
    production_movement_list = production_applied_rule.contentValues()
618 619 620 621 622 623 624
    production_movement = production_movement_list[0]

    transformation_applied_rule = production_movement.contentValues()[0]

    consumed_movement = [q for q in transformation_applied_rule.contentValues() \
        if q.getId().startswith('cr')][0]

625
    self.assertEqual(
626 627 628 629 630 631 632 633
        consumed_movement.getQuantity(),
        self.quantity_after_efficiency_calculation
    )

    transformation_sourcing_rule = consumed_movement.contentValues()[0]

    consumption_delivery_movement = [q for q in transformation_sourcing_rule.contentValues() \
        if q.getId().startswith('ts')][0]
634

635
    self.assertEqual(
636 637 638 639
        consumption_delivery_movement.getQuantity(),
        self.quantity_after_efficiency_calculation
    )

640 641 642 643 644 645
  def stepCheckOrderLineTransformationIsSet(self, sequence=None, sequence_list=None, **kw):
    order_line = sequence.get('order_line')
    transformation = sequence.get('transformation')

    self.assertNotEquals(None, transformation)

646
    self.assertEqual(order_line.getSpecialiseValue(), transformation)
647 648 649 650

  def stepCheckOrderLineTransformationIsNotSet(self, sequence=None, sequence_list=None, **kw):
    order_line = sequence.get('order_line')

651
    self.assertEqual(order_line.getSpecialiseValue(), None)
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669

  def stepRemoveResourceFromOrderLine(self, sequence=None, sequence_list=None, **kw):
    order_line = sequence.get('order_line')

    order_line.edit(
        resource = None
    )

  def stepCheckOrderSimulation(self, sequence=None, sequence_list=None, **kw):
    """
      Test if simulation is matching order
    """
    order = sequence.get('order')
    related_applied_rule_list = order.getCausalityRelatedValueList( \
                                   portal_type=self.applied_rule_portal_type)
    no_applied_rule_state = ('draft', 'auto_planned')
    order_state = order.getSimulationState()
    if order_state in no_applied_rule_state:
670
      self.assertEqual(0, len(related_applied_rule_list))
671
    else:
672
      self.assertEqual(1, len(related_applied_rule_list))
673 674
      applied_rule = related_applied_rule_list[0].getObject()
      sequence.edit(applied_rule=applied_rule)
675
      self.assertTrue(applied_rule is not None)
676 677
      # Test if applied rule has a specialise value with default_order_rule
      # XXX hardcoded value
678
      self.assertEqual('default_production_order_rule', \
679
                        applied_rule.getSpecialiseReference())
680

681 682 683 684 685 686 687
      simulation_movement_list = applied_rule.objectValues()
      sequence.edit(simulation_movement_list=simulation_movement_list)

  def checkObjectAttributes(self, object, attribute_list):
    LOG('checkObjectAttributes object.getPath',0,object.getPath())
    for value, attribute in attribute_list:
      try:
688
        self.assertEqual(value,
689 690 691 692 693
                          getattr(object, attribute)())
      except AssertionError:
        LOG('Raise Assertion error',0,'')
        LOG('object.getQuantity()',0,object.getQuantity())
        LOG('object.__dict__',0,object.__dict__)
694 695 696 697
        delivery_value = object.getDeliveryValue()
        LOG('object.getDeliveryValue()', 0, delivery_value)
        if delivery_value is not None:
          LOG('object.getDeliveryValue().getQuantity()',0,delivery_value.getQuantity())
698 699 700 701 702 703 704 705 706 707 708 709
        raise AssertionError, "Attribute: %s, Value: %s, Result: %s" %\
                    (attribute, value, getattr(object, attribute)())

  def stepCheckProductionSimulation(self, sequence=None, sequence_list=None,
                                    **kw):
    """
      Hardcoded check
    """
    self.stepCheckOrderSimulation(sequence=sequence,
                                  sequence_list=sequence_list, **kw)
    # Test simulation movement generated related to order line
    simulation_movement_list = sequence.get('simulation_movement_list')
710
    self.assertEqual(1, len(simulation_movement_list))
711
    order_line = sequence.get('order_line')
712
    related_simulation_movement_list = order_line.getDeliveryRelatedValueList()
713
    self.assertEqual(1, len(related_simulation_movement_list))
714
    related_simulation_movement = related_simulation_movement_list[0]
715
    self.assertEqual(related_simulation_movement,
716 717 718 719 720 721 722
                      simulation_movement_list[0])
    production_organisation1 = sequence.get('production_organisation1')
    # XXX FIXME test date
    self.checkObjectAttributes(
           related_simulation_movement, (
             (order_line.getQuantity(), 'getQuantity'),
             (order_line.getResourceValue(), 'getResourceValue'),
723
             (order_line.getVariationCategoryList(),
724 725
              'getVariationCategoryList'),
             (order_line.getDestinationValue(), 'getDestinationValue'),
726
             (order_line.getDestinationSectionValue(),
727 728 729 730 731
              'getDestinationSectionValue'),
             (production_organisation1, 'getSourceValue'),
             (production_organisation1, 'getSourceSectionValue')))
    # Test next applied rule
    applied_rule_list = related_simulation_movement.objectValues()
732
    self.assertEqual(1, len(applied_rule_list))
733
    applied_rule = applied_rule_list[0]
734 735
    self.assertEqual("Applied Rule", applied_rule.getPortalType())
    self.assertEqual('default_delivering_rule', \
736 737 738
                      applied_rule.getSpecialiseReference())
    # Test next applied rule
    simulation_movement_list = applied_rule.objectValues()
739
    self.assertEqual(1, len(simulation_movement_list))
740 741
    simulation_movement = simulation_movement_list[0]
    applied_rule_list = simulation_movement.objectValues()
742
    self.assertEqual(1, len(applied_rule_list))
743
    applied_rule = applied_rule_list[0]
744 745
    self.assertEqual("Applied Rule", applied_rule.getPortalType())
    self.assertEqual('default_transformation_rule', \
746
                      applied_rule.getSpecialiseReference())
747
    # Test deeper simulation
748
    simulation_movement_list = applied_rule.objectValues()
749
    self.assertEqual(2, len(simulation_movement_list))
750 751
    # Test consumed movement
    transformation = sequence.get('transformation')
752 753
    consumed_movement_id = 'cr_%s_1_%s' % (transformation.getId(),
                                           applied_rule.getParentId())
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
    consumed_movement = applied_rule[consumed_movement_id]
    operation_resource = consumed_movement.portal_categories.resolveCategory(
                                              'operation/operation1')
    # FIXME
    self.checkObjectAttributes(
           consumed_movement, (
             (10, 'getQuantity'),
             (operation_resource, 'getResourceValue'),
             ([], 'getVariationCategoryList'),
             (None, 'getDestinationValue'),
             (None, 'getDestinationSectionValue'),
             (production_organisation1, 'getSourceValue'),
             (production_organisation1, 'getSourceSectionValue')))
    # Test produced resource
    produced_movement = applied_rule.pr
    resource = sequence.get('resource')
    production_organisation1 = sequence.get('production_organisation1')
    self.checkObjectAttributes(
           produced_movement, (
             (5, 'getQuantity'),
             (resource, 'getResourceValue'),
             ([], 'getVariationCategoryList'),
             (production_organisation1, 'getDestinationValue'),
             (production_organisation1, 'getDestinationSectionValue'),
             (None, 'getSourceValue'),
             (None, 'getSourceSectionValue')))

781
  def stepCreateSupplyOrganisation1(self, sequence=None, sequence_list=None,
782 783 784 785 786 787 788
                                        **kw):
    """
      Create a organisation for supply
    """
    self.stepCreateOrganisation(sequence=sequence, sequence_list=sequence_list,
                                title='supply_organisation1', **kw)

789
  def stepCreateSourcingSC(self, sequence=None, sequence_list=None,
790 791 792 793 794 795 796 797
                             **kw):
    """
      Create a empty organisation
    """
    # Create supply chain
    self.stepCreateProductionSC(sequence=sequence, sequence_list=sequence_list,
                                **kw)
    supply_chain = sequence.get('supply_chain')
798 799 800
    supply_chain.edit(
      title = 'Supply Chain Sourcing',
    )
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
    # Create supply node
    supply_organisation  = sequence.get('supply_organisation1')
    supply_node = supply_chain.newContent(
                         portal_type=self.supply_node_portal_type)
    supply_node.edit(
      destination_value=supply_organisation
    )
    # Create sourcing link
    supply_link = supply_chain.newContent(
                         portal_type=self.supply_link_portal_type)
    production_node1 = sequence.get('production_node1')
    production_organisation1 = sequence.get('production_organisation1')
    supply_link.edit(
      source_value=supply_node,
      source_section_value=supply_organisation,
      destination_value=production_node1,
      destination_section_value=production_organisation1,
      min_delay=5,
      max_delay=6,
      deliverable=0,
      industrial_phase_list=["supply_phase1"]
    )

  def stepCheckSourcingSimulation(self, sequence=None, sequence_list=None,
                                  **kw):
    """
      Hardcoded check
    """
    self.stepCheckOrderSimulation(sequence=sequence,
                                  sequence_list=sequence_list, **kw)
    # Test simulation movement generated related to order line
    simulation_movement_list = sequence.get('simulation_movement_list')
833
    self.assertEqual(1, len(simulation_movement_list))
834
    order_line = sequence.get('order_line')
835
    related_simulation_movement_list = order_line.getDeliveryRelatedValueList()
836
    self.assertEqual(1, len(related_simulation_movement_list))
837
    related_simulation_movement = related_simulation_movement_list[0]
838
    self.assertEqual(related_simulation_movement,
839 840 841 842 843 844 845
                      simulation_movement_list[0])
    production_organisation1 = sequence.get('production_organisation1')
    # XXX FIXME test date
    self.checkObjectAttributes(
           related_simulation_movement, (
             (order_line.getQuantity(), 'getQuantity'),
             (order_line.getResourceValue(), 'getResourceValue'),
846
             (order_line.getVariationCategoryList(),
847 848
              'getVariationCategoryList'),
             (order_line.getDestinationValue(), 'getDestinationValue'),
849
             (order_line.getDestinationSectionValue(),
850 851 852 853
              'getDestinationSectionValue'),
             (production_organisation1, 'getSourceValue'),
             (production_organisation1, 'getSourceSectionValue')))
    # Test next applied rule
854
    applied_rule_list = related_simulation_movement.objectValues()[0].objectValues()[0].objectValues()
855
    self.assertEqual(1, len(applied_rule_list))
856
    applied_rule = applied_rule_list[0]
857 858
    self.assertEqual("Applied Rule", applied_rule.getPortalType())
    self.assertEqual('default_transformation_rule', \
859
                      applied_rule.getSpecialiseReference())
860
    # Test deeper simulation
861 862
    simulation_movement_list = list(applied_rule.objectValues())
    # FIXME
863
    self.assertEqual(3, len(simulation_movement_list))
864 865 866 867 868 869 870 871 872 873 874 875 876
    # Test produced resource
    produced_movement = applied_rule.pr
    resource = sequence.get('resource')
    production_organisation1 = sequence.get('production_organisation1')
    self.checkObjectAttributes(
           produced_movement, (
             (5, 'getQuantity'),
             (resource, 'getResourceValue'),
             ([], 'getVariationCategoryList'),
             (production_organisation1, 'getDestinationValue'),
             (production_organisation1, 'getDestinationSectionValue'),
             (None, 'getSourceValue'),
             (None, 'getSourceSectionValue')))
877
    self.assertEqual(0, len(produced_movement.objectValues()))
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900

    simulation_movement_list.remove(produced_movement)
    # All code before is a stupid copy (except movement count)
    # Test consumed movement
    operation_resource = resource.portal_categories.resolveCategory(
                                              'operation/operation1')
    component_resource = sequence.get('component1')
#     for consumed_movement in (applied_rule.cr_1, applied_rule.cr_2):
    for consumed_movement in simulation_movement_list:
      if consumed_movement.getResourceValue() == operation_resource:
        operation_movement = consumed_movement
      else:
        component_movement = consumed_movement
    # Check operation movement
    self.checkObjectAttributes(
           operation_movement, (
             (10, 'getQuantity'),
             (operation_resource, 'getResourceValue'),
             ([], 'getVariationCategoryList'),
             (None, 'getDestinationValue'),
             (None, 'getDestinationSectionValue'),
             (production_organisation1, 'getSourceValue'),
             (production_organisation1, 'getSourceSectionValue')))
901
    self.assertEqual(0, len(operation_movement.objectValues()))
902 903 904 905 906 907 908 909 910 911
    # Check component movement
    self.checkObjectAttributes(
           component_movement, (
             (30, 'getQuantity'),
             (component_resource, 'getResourceValue'),
             ([], 'getVariationCategoryList'),
             (None, 'getDestinationValue'),
             (None, 'getDestinationSectionValue'),
             (production_organisation1, 'getSourceValue'),
             (production_organisation1, 'getSourceSectionValue')))
912
    self.assertEqual(1, len(component_movement.objectValues()))
913 914
    # Test supply applied rule
    applied_rule = component_movement.objectValues()[0]
915 916
    self.assertEqual("Applied Rule", applied_rule.getPortalType())
    self.assertEqual('default_transformation_sourcing_rule', \
917
                      applied_rule.getSpecialiseReference())
918 919 920
    # Test supply movement
    simulation_movement_list = applied_rule.objectValues()
    # FIXME
921
    self.assertEqual(1, len(simulation_movement_list))
922 923 924 925 926 927 928 929 930 931 932 933
    # Test supply resource
    supply_movement = applied_rule.ts
    supply_organisation1 = sequence.get('supply_organisation1')
    self.checkObjectAttributes(
           supply_movement, (
             (30, 'getQuantity'),
             (component_resource, 'getResourceValue'),
             ([], 'getVariationCategoryList'),
             (production_organisation1, 'getDestinationValue'),
             (production_organisation1, 'getDestinationSectionValue'),
             (supply_organisation1, 'getSourceValue'),
             (supply_organisation1, 'getSourceSectionValue')))
934
    self.assertEqual(0, len(supply_movement.objectValues()))
935

936 937 938 939 940
    sequence.edit(
      produced_movement = produced_movement,
      operation_movement = operation_movement,
      component_movement = component_movement,
      supply_movement = supply_movement,
941
      produced_delivery_movement = related_simulation_movement.objectValues()[0].objectValues()[0],
942 943
    )

944
  def stepCreateProductionOrganisation2(self, sequence=None,
945 946 947 948 949 950 951
                                        sequence_list=None, **kw):
    """
      Create a organisation for supply
    """
    self.stepCreateOrganisation(sequence=sequence, sequence_list=sequence_list,
                                title='production_organisation2', **kw)

952
  def stepCreateSupplyOrganisation2(self, sequence=None, sequence_list=None,
953 954 955 956 957 958 959
                                        **kw):
    """
      Create a organisation for supply
    """
    self.stepCreateOrganisation(sequence=sequence, sequence_list=sequence_list,
                                title='supply_organisation2', **kw)

960
  def stepCreateTwoPhasesSC(self, sequence=None, sequence_list=None,
961 962 963 964 965 966 967 968
                             **kw):
    """
      Create a empty organisation
    """
    # Create supply chain
    self.stepCreateSourcingSC(sequence=sequence, sequence_list=sequence_list,
                              **kw)
    supply_chain = sequence.get('supply_chain')
969 970 971
    supply_chain.edit(
      title = 'Supply Chain Two Phases',
    )
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
    # Create production node
    production_organisation2  = sequence.get('production_organisation2')
    production_node2 = supply_chain.newContent(
                         portal_type=self.supply_node_portal_type)
    production_node2.edit(
      destination_value=production_organisation2
    )
    sequence.edit(production_node2=production_node2)
    # Create production link
    production_link2 = supply_chain.newContent(
                         portal_type=self.supply_link_portal_type)
    production_link2.edit(
      destination_value=production_node2,
      min_delay=5,
      max_delay=6,
      deliverable=0,
      industrial_phase_list=["phase2"]
    )
    # Link production_node2 and production_node1
    supply_link = supply_chain.newContent(
                         portal_type=self.supply_link_portal_type)
    production_node1 = sequence.get('production_node1')
    supply_link.edit(
      source_value=production_node2,
      destination_value=production_node1,
      min_delay=5,
      max_delay=6,
      deliverable=0,
      industrial_phase_list=[]
    )
    # Create supply node
    supply_organisation2  = sequence.get('supply_organisation2')
    supply_node2 = supply_chain.newContent(
                         portal_type=self.supply_node_portal_type)
    supply_node2.edit(
      destination_value=supply_organisation2
    )
    # Create sourcing link
    supply_link2 = supply_chain.newContent(
                         portal_type=self.supply_link_portal_type)
    supply_link2.edit(
      source_value=supply_node2,
      destination_value=production_node2,
      min_delay=5,
      max_delay=6,
      deliverable=0,
      industrial_phase_list=["supply_phase2"]
    )

  def stepCheckTwoPhasesSimulation(self, sequence=None, sequence_list=None,
                                   **kw):
    """
      Hardcoded check
    """
#     self.stepCheckSourcingSimulation(sequence=sequence,
#                                      sequence_list=sequence_list, **kw)
    self.stepCheckOrderSimulation(sequence=sequence,
                                  sequence_list=sequence_list, **kw)
    # Test simulation movement generated related to order line
    simulation_movement_list = sequence.get('simulation_movement_list')
1032
    self.assertEqual(1, len(simulation_movement_list))
1033
    order_line = sequence.get('order_line')
1034
    related_simulation_movement_list = order_line.getDeliveryRelatedValueList()
1035
    self.assertEqual(1, len(related_simulation_movement_list))
1036
    related_simulation_movement = related_simulation_movement_list[0]
1037
    self.assertEqual(related_simulation_movement,
1038 1039 1040 1041 1042 1043 1044
                      simulation_movement_list[0])
    production_organisation1 = sequence.get('production_organisation1')
    # XXX FIXME test date
    self.checkObjectAttributes(
           related_simulation_movement, (
             (order_line.getQuantity(), 'getQuantity'),
             (order_line.getResourceValue(), 'getResourceValue'),
1045
             (order_line.getVariationCategoryList(),
1046 1047
              'getVariationCategoryList'),
             (order_line.getDestinationValue(), 'getDestinationValue'),
1048
             (order_line.getDestinationSectionValue(),
1049 1050 1051 1052 1053
              'getDestinationSectionValue'),
             (production_organisation1, 'getSourceValue'),
             (production_organisation1, 'getSourceSectionValue')))
    # Test next applied rule
    applied_rule_list = related_simulation_movement.objectValues()
1054
    self.assertEqual(1, len(applied_rule_list))
1055
    applied_rule = applied_rule_list[0]
1056 1057
    self.assertEqual("Applied Rule", applied_rule.getPortalType())
    self.assertEqual('default_delivering_rule', \
1058 1059 1060
                      applied_rule.getSpecialiseReference())
    # Test next applied rule
    simulation_movement_list = applied_rule.objectValues()
1061
    self.assertEqual(1, len(simulation_movement_list))
1062 1063
    simulation_movement = simulation_movement_list[0]
    applied_rule_list = simulation_movement.objectValues()
1064
    self.assertEqual(1, len(applied_rule_list))
1065
    applied_rule = applied_rule_list[0]
1066 1067
    self.assertEqual("Applied Rule", applied_rule.getPortalType())
    self.assertEqual('default_transformation_rule', \
1068
                      applied_rule.getSpecialiseReference())
1069
    # Test deeper simulation
1070 1071
    simulation_movement_list = list(applied_rule.objectValues())
    # FIXME
1072
    self.assertEqual(4, len(simulation_movement_list))
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
    # Test produced resource
    produced_movement = applied_rule.pr
    resource = sequence.get('resource')
    production_organisation1 = sequence.get('production_organisation1')
    self.checkObjectAttributes(
           produced_movement, (
             (5, 'getQuantity'),
             (resource, 'getResourceValue'),
             ([], 'getVariationCategoryList'),
             (production_organisation1, 'getDestinationValue'),
             (production_organisation1, 'getDestinationSectionValue'),
             (None, 'getSourceValue'),
             (None, 'getSourceSectionValue')))
1086
    self.assertEqual(0, len(produced_movement.objectValues()))
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112

    # Get modified resource (test later)
    modified_movement = applied_rule.mr_1
    simulation_movement_list.remove(produced_movement)
    simulation_movement_list.remove(modified_movement)
    # All code before is a stupid copy (except movement count)
    # Test consumed movement
    operation_resource = resource.portal_categories.resolveCategory(
                                              'operation/operation1')
    component_resource = sequence.get('component1')
#     for consumed_movement in (applied_rule.cr_1, applied_rule.cr_2):
    for consumed_movement in simulation_movement_list:
      if consumed_movement.getResourceValue() == operation_resource:
        operation_movement = consumed_movement
      else:
        component_movement = consumed_movement
    # Check operation movement
    self.checkObjectAttributes(
           operation_movement, (
             (10, 'getQuantity'),
             (operation_resource, 'getResourceValue'),
             ([], 'getVariationCategoryList'),
             (None, 'getDestinationValue'),
             (None, 'getDestinationSectionValue'),
             (production_organisation1, 'getSourceValue'),
             (production_organisation1, 'getSourceSectionValue')))
1113
    self.assertEqual(0, len(operation_movement.objectValues()))
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
    # Check component movement
    self.checkObjectAttributes(
           component_movement, (
             (30, 'getQuantity'),
             (component_resource, 'getResourceValue'),
             ([], 'getVariationCategoryList'),
             (None, 'getDestinationValue'),
             (None, 'getDestinationSectionValue'),
             (production_organisation1, 'getSourceValue'),
             (production_organisation1, 'getSourceSectionValue')))
1124
    self.assertEqual(1, len(component_movement.objectValues()))
1125 1126
    # Test supply applied rule
    applied_rule = component_movement.objectValues()[0]
1127 1128
    self.assertEqual("Applied Rule", applied_rule.getPortalType())
    self.assertEqual('default_transformation_sourcing_rule', \
1129
                      applied_rule.getSpecialiseReference())
1130 1131 1132
    # Test supply movement
    simulation_movement_list = applied_rule.objectValues()
    # FIXME
1133
    self.assertEqual(1, len(simulation_movement_list))
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
    # Test supply resource
    supply_movement = applied_rule.ts
    supply_organisation1 = sequence.get('supply_organisation1')
    self.checkObjectAttributes(
           supply_movement, (
             (30, 'getQuantity'),
             (component_resource, 'getResourceValue'),
             ([], 'getVariationCategoryList'),
             (production_organisation1, 'getDestinationValue'),
             (production_organisation1, 'getDestinationSectionValue'),
             (supply_organisation1, 'getSourceValue'),
             (supply_organisation1, 'getSourceSectionValue')))
1146
    self.assertEqual(0, len(supply_movement.objectValues()))
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159

    # Test modified movement
    resource = sequence.get('resource')
    production_organisation1 = sequence.get('production_organisation1')
    self.checkObjectAttributes(
           modified_movement, (
             (5, 'getQuantity'),
             (resource, 'getResourceValue'),
             (['industrial_phase/phase2'], 'getVariationCategoryList'),
             (production_organisation1, 'getSourceValue'),
             (production_organisation1, 'getSourceSectionValue'),
             (None, 'getDestinationValue'),
             (None, 'getDestinationSectionValue')))
1160
    self.assertEqual(1, len(modified_movement.objectValues()))
1161 1162 1163
    # Test next applied rule
    applied_rule_list = modified_movement.objectValues()
    applied_rule = applied_rule_list[0]
1164 1165
    self.assertEqual("Applied Rule", applied_rule.getPortalType())
    self.assertEqual('default_transformation_sourcing_rule', \
1166
                      applied_rule.getSpecialiseReference())
1167
    # Test deeper simulation
1168
    simulation_movement_list = list(applied_rule.objectValues())
1169
    self.assertEqual(1, len(simulation_movement_list))
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
    # Test produced resource
    sourcing_movement = simulation_movement_list[0]
    resource = sequence.get('resource')
    production_organisation1 = sequence.get('production_organisation1')
    production_organisation2 = sequence.get('production_organisation2')
    self.checkObjectAttributes(
           sourcing_movement, (
             (5, 'getQuantity'),
             (resource, 'getResourceValue'),
             (['industrial_phase/phase2'], 'getVariationCategoryList'),
             (production_organisation1, 'getDestinationValue'),
# XXX             (production_organisation1, 'getDestinationSectionValue'),
             (production_organisation2, 'getSourceValue'),
# XXX             (production_organisation2, 'getSourceSectionValue')))
           ))
1185
    self.assertEqual(1, len(sourcing_movement.objectValues()))
1186 1187
    # Test next applied rule
    applied_rule_list = sourcing_movement.objectValues()
1188
    self.assertEqual(1, len(applied_rule_list))
1189
    applied_rule = applied_rule_list[0]
1190 1191
    self.assertEqual("Applied Rule", applied_rule.getPortalType())
    self.assertEqual('default_transformation_rule', \
1192
                      applied_rule.getSpecialiseReference())
1193
    # Test deeper simulation
1194 1195
    simulation_movement_list = list(applied_rule.objectValues())
    # FIXME
1196
    self.assertEqual(3, len(simulation_movement_list))
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
    # Test produced resource
    produced_movement = applied_rule.pr
    resource = sequence.get('resource')
    production_organisation2 = sequence.get('production_organisation2')
    self.checkObjectAttributes(
           produced_movement, (
             (5, 'getQuantity'),
             (resource, 'getResourceValue'),
             (['industrial_phase/phase2'], 'getVariationCategoryList'),
             (production_organisation2, 'getDestinationValue'),
# XXX             (production_organisation2, 'getDestinationSectionValue'),
             (None, 'getSourceValue'),
             (None, 'getSourceSectionValue')))
1210
    self.assertEqual(0, len(produced_movement.objectValues()))
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233

    simulation_movement_list.remove(produced_movement)
    # All code before is a stupid copy (except movement count)
    # Test consumed movement
    operation_resource = resource.portal_categories.resolveCategory(
                                              'operation/operation2')
    component_resource = sequence.get('component2')
    for consumed_movement in simulation_movement_list:
      if consumed_movement.getResourceValue() == operation_resource:
        operation_movement = consumed_movement
      else:
        component_movement = consumed_movement
    # Check operation movement
    self.checkObjectAttributes(
           operation_movement, (
             (15, 'getQuantity'),
             (operation_resource, 'getResourceValue'),
             ([], 'getVariationCategoryList'),
             (None, 'getDestinationValue'),
             (None, 'getDestinationSectionValue'),
             (production_organisation2, 'getSourceValue'),
# XXX              (production_organisation2, 'getSourceSectionValue')))
           ))
1234
    self.assertEqual(0, len(operation_movement.objectValues()))
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
    # Check component movement
    self.checkObjectAttributes(
           component_movement, (
             (35, 'getQuantity'),
             (component_resource, 'getResourceValue'),
             ([], 'getVariationCategoryList'),
             (None, 'getDestinationValue'),
             (None, 'getDestinationSectionValue'),
             (production_organisation2, 'getSourceValue'),
# XXX              (production_organisation2, 'getSourceSectionValue')))
           ))
1246
    self.assertEqual(1, len(component_movement.objectValues()))
1247 1248
    # Test supply applied rule
    applied_rule = component_movement.objectValues()[0]
1249 1250
    self.assertEqual("Applied Rule", applied_rule.getPortalType())
    self.assertEqual('default_transformation_sourcing_rule', \
1251
                      applied_rule.getSpecialiseReference())
1252 1253 1254
    # Test supply movement
    simulation_movement_list = applied_rule.objectValues()
    # FIXME
1255
    self.assertEqual(1, len(simulation_movement_list))
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
    # Test supply resource
    supply_movement = applied_rule.ts
    supply_organisation2  = sequence.get('supply_organisation2')
    self.checkObjectAttributes(
           supply_movement, (
             (35, 'getQuantity'),
             (component_resource, 'getResourceValue'),
             ([], 'getVariationCategoryList'),
             (production_organisation2, 'getDestinationValue'),
# XXX              (production_organisation2, 'getDestinationSectionValue'),
             (supply_organisation2, 'getSourceValue'),
# XXX              (supply_organisation2, 'getSourceSectionValue')))
           ))
1269
    self.assertEqual(0, len(supply_movement.objectValues()))
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288

  SOURCING_ORDER_SEQUENCE = '\
                      CreateProductionOrganisation1 \
                      CreateSupplyOrganisation1 \
                      CreateSourcingSC \
                      CreateNotVariatedResource \
                      CreateComponent1 \
                      CreateTransformation \
                      CreateOrganisation \
                      CreateOrder \
                      CreateOrderLine \
                      Tic \
                      OrderOrder \
                      Tic \
                      CheckSourcingSimulation \
                      ConfirmOrder \
                      Tic \
                      CheckSourcingSimulation \
                      '
1289

1290 1291 1292 1293 1294 1295 1296 1297 1298
class TestProductionOrder(TestProductionOrderMixin, ERP5TypeTestCase):
  """
    Test business template erp5_mrp
  """
  run_all_test = 1

  def getTitle(self):
    return "Production Order"

1299
  @newSimulationExpectedFailure
1300 1301 1302 1303 1304 1305
  def test_01_testProductionSimulationExpand(self, quiet=0, run=run_all_test):
    """
      Test generation and update of order applied rule.
    """
    if not run: return
    sequence_list = SequenceList()
1306
    # Test when order is
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
    sequence_string = '\
                      CreateProductionOrganisation1 \
                      CreateProductionSC \
                      CreateNotVariatedResource \
                      CreateComponent1 \
                      CreateTransformation \
                      CreateOrganisation \
                      CreateOrder \
                      CreateOrderLine \
                      Tic \
                      OrderOrder \
                      Tic \
                      CheckProductionSimulation \
                      '
    sequence_list.addSequenceString(sequence_string)
    sequence_list.play(self)

1324
  @newSimulationExpectedFailure
1325
  def test_02_testSourcingSimulationExpand(self, quiet=0,
1326 1327 1328 1329 1330 1331
                                                     run=run_all_test):
    """
      Test generation and update of order applied rule.
    """
    if not run: return
    sequence_list = SequenceList()
1332
    # Test when order is
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
    sequence_string = '\
                      CreateProductionOrganisation1 \
                      CreateSupplyOrganisation1 \
                      CreateSourcingSC \
                      CreateNotVariatedResource \
                      CreateComponent1 \
                      CreateTransformation \
                      CreateOrganisation \
                      CreateOrder \
                      CreateOrderLine \
                      Tic \
                      OrderOrder \
                      Tic \
                      CheckSourcingSimulation \
                      '
    sequence_list.addSequenceString(sequence_string)
    sequence_list.play(self)

1351
  @newSimulationExpectedFailure
1352 1353 1354 1355 1356
  def test_03_testIndustrialPhase(self, quiet=0, run=run_all_test):
    """
    """
    if not run: return
    sequence_list = SequenceList()
1357
    # Test when order is
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
    sequence_string = '\
                      CreateProductionOrganisation1 \
                      CreateProductionOrganisation2 \
                      CreateSupplyOrganisation1 \
                      CreateSupplyOrganisation2 \
                      CreateTwoPhasesSC \
                      CreateNotVariatedResource \
                      CreateComponent1 \
                      CreateComponent2 \
                      CreateTransformation \
                      CreateOrganisation \
                      CreateOrder \
                      CreateOrderLine \
                      Tic \
                      OrderOrder \
                      Tic \
                      CheckTwoPhasesSimulation \
                      '
    sequence_list.addSequenceString(sequence_string)
    sequence_list.play(self)

1379
  @newSimulationExpectedFailure
1380 1381 1382 1383 1384 1385 1386
  def test_04_testProductionSimulationBuild(self, quiet=0, run=run_all_test):
    """
    Test delivery building.
    XXX Test method still required
    """
    if not run: return
    sequence_list = SequenceList()
1387
    # Test when order is
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
    sequence_string = '\
                      CreateProductionOrganisation1 \
                      CreateProductionSC \
                      CreateNotVariatedResource \
                      CreateComponent1 \
                      CreateTransformation \
                      CreateOrganisation \
                      CreateOrder \
                      CreateOrderLine \
                      Tic \
                      OrderOrder \
                      Tic \
                      ConfirmOrder \
                      Tic \
                      '
    sequence_list.addSequenceString(sequence_string)
    sequence_list.play(self)

1406
  @newSimulationExpectedFailure
1407
  def test_05_testSourcingSimulationBuild(self, quiet=0,
1408 1409 1410 1411 1412 1413 1414
                                          run=run_all_test):
    """
    Test delivery building.
    XXX Test method still required
    """
    if not run: return
    sequence_list = SequenceList()
1415
    # Test when order is
1416 1417 1418
    sequence_list.addSequenceString(self.SOURCING_ORDER_SEQUENCE)
    sequence_list.play(self)

1419
  @newSimulationExpectedFailure
1420 1421 1422 1423 1424 1425 1426
  def test_06_testIndustrialPhase(self, quiet=0, run=run_all_test):
    """
    Test delivery building.
    XXX Test method still required
    """
    if not run: return
    sequence_list = SequenceList()
1427
    # Test when order is
1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
    sequence_string = '\
                      CreateProductionOrganisation1 \
                      CreateProductionOrganisation2 \
                      CreateSupplyOrganisation1 \
                      CreateSupplyOrganisation2 \
                      CreateTwoPhasesSC \
                      CreateNotVariatedResource \
                      CreateComponent1 \
                      CreateComponent2 \
                      CreateTransformation \
                      CreateOrganisation \
                      CreateOrder \
                      CreateOrderLine \
                      Tic \
                      OrderOrder \
                      Tic \
                      ConfirmOrder \
                      Tic \
                      '
    sequence_list.addSequenceString(sequence_string)
    sequence_list.play(self)

1450
  def stepCopyPasteSupplyChain(self, sequence=None, sequence_list=None,
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
                               **kw):
    """
    Copy/Paste the supply chain
    """
    portal = self.getPortal()
    supply_chain_module = portal.getDefaultModule( \
                                   portal_type=self.supply_chain_portal_type)
    supply_chain = sequence.get('supply_chain')

    cb_data = supply_chain_module.manage_copyObjects([supply_chain.getId()])
    copied, = supply_chain_module.manage_pasteObjects(cb_data)
    pasted_sc = supply_chain_module[copied['new_id']]
    sequence.edit(pasted_sc=pasted_sc)

1465
  def stepCheckPastedSupplyChain(self, sequence=None, sequence_list=None,
1466 1467 1468 1469 1470 1471 1472
                                 **kw):
    """
    Check pasted supply chain
    """
    pasted_sc = sequence.get('pasted_sc')
    pasted_supply_node = pasted_sc.contentValues(portal_type='Supply Node')[0]
    pasted_supply_link = pasted_sc.contentValues(portal_type='Supply Link')[0]
1473
    self.assertEqual(pasted_supply_node.getRelativeUrl(),
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505
                      pasted_supply_link.getDestination())

  def test_07_testTransformationInteractionProductionOrderLine(self, quiet=0, run=run_all_test):
    """
    Test for setting/resetting Transformation on Production Order Line
    """
    if not run: return

    bootstrap_sequence_string = '\
                      CreateNotVariatedResource \
                      CreateOrder \
                      '
    sequence_list = SequenceList()

    # normal case
    sequence_string = bootstrap_sequence_string + '\
                      CreateTransformation \
                      Tic \
                      CreateOrderLineWithoutTransformation \
                      Tic \
                      CheckOrderLineTransformationIsSet \
                      '
    sequence_list.addSequenceString(sequence_string)

    # no transformation
    sequence_string = bootstrap_sequence_string + '\
                      CreateOrderLineWithoutTransformation \
                      Tic \
                      CheckOrderLineTransformationIsNotSet \
                      '
    sequence_list.addSequenceString(sequence_string)

1506
    # transformation set, then not modified
1507 1508 1509 1510 1511 1512 1513 1514
    sequence_string = bootstrap_sequence_string + '\
                      CreateTransformation \
                      Tic \
                      CreateOrderLineWithoutTransformation \
                      Tic \
                      CheckOrderLineTransformationIsSet \
                      RemoveResourceFromOrderLine \
                      Tic \
1515
                      CheckOrderLineTransformationIsSet \
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
                      '
    sequence_list.addSequenceString(sequence_string)

    # more than one transformation
    sequence_string = bootstrap_sequence_string + '\
                      CreateTransformation \
                      CreateTransformation \
                      Tic \
                      CreateOrderLineWithoutTransformation \
                      Tic \
                      CheckOrderLineTransformationIsNotSet \
                      '
    sequence_list.addSequenceString(sequence_string)

    # case of invalidated Transformation
    sequence_string = bootstrap_sequence_string + '\
                      CreateTransformation \
                      Tic \
                      ValidateTransformation \
                      InvalidateTransformation \
                      Tic \
                      CreateOrderLineWithoutTransformation \
                      Tic \
                      CheckOrderLineTransformationIsNotSet \
                      '
    sequence_list.addSequenceString(sequence_string)

    # case of invalidated Transformation and other
    sequence_string = bootstrap_sequence_string + '\
                      CreateTransformation \
                      Tic \
                      ValidateTransformation \
                      InvalidateTransformation \
                      Tic \
                      CreateTransformation \
                      Tic \
                      CreateOrderLineWithoutTransformation \
                      Tic \
                      CheckOrderLineTransformationIsSet \
                      '
    sequence_list.addSequenceString(sequence_string)

    sequence_list.play(self)
1559 1560

  @newSimulationExpectedFailure
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 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644
  def test_08_testTransformationWithEfficiency(self, quiet=0, run=run_all_test):
    """
    Test, that efficiency from transformation applies correctly
    """
    if not run: return

    sequence_string = '\
                      CreateProductionOrganisation1 \
                      CreateSupplyOrganisation1 \
                      CreateSourcingSC \
                      Tic \
                      CreateNotVariatedResource \
                      CreateComponent1 \
                      CreateEmptyTransformation \
                      FillTransformationWithResource \
                      Tic \
                      CreateTransformationTransformedResource \
                      FillTransformationTransformedResourceWithComponent1 \
                      SetTransformationTransformedResourceQuantity \
                      SetTransformationTransformedResourceEfficiency \
                      SetTransformationTransformedResourceIndustrialPhaseList \
                      Tic \
                      CreateOrganisation \
                      CreateOrder \
                      CreateOrderLine \
                      SetOrderLineQuantity \
                      Tic \
                      OrderOrder \
                      Tic \
                      CheckEfficiencySimulation \
                      '
    sequence_list = SequenceList()
    sequence_list.addSequenceString(sequence_string)

    # case - we need Q:10.0, efficiency is 80% (we need more), producing 1.0
    self.transformation_transformed_resource_quantity = 10.0
    self.transformation_transformed_resource_efficiency = 0.8 # 80%
    self.transformation_transformed_resource_industrial_phase_list = ['supply_phase1',]

    self.production_order_line_quantity = 1.0
    self.quantity_after_efficiency_calculation = 12.5 # (1.0 * 10.0) / 0.8

    sequence_list.play(self)

    # case - we need Q:10.0, efficiency is None (normal, nothing set), producing 1.0
    self.transformation_transformed_resource_quantity = 10.0
    self.transformation_transformed_resource_efficiency = None
    self.transformation_transformed_resource_industrial_phase_list = ['supply_phase1',]

    self.production_order_line_quantity = 1.0
    self.quantity_after_efficiency_calculation = 10.0 # (1.0 * 10.0) / 1.0

    sequence_list.play(self)

    # case - we need Q:10.0, efficiency is 100% (normal), producing 1.0
    self.transformation_transformed_resource_quantity = 10.0
    self.transformation_transformed_resource_efficiency = 1.0
    self.transformation_transformed_resource_industrial_phase_list = ['supply_phase1',]

    self.production_order_line_quantity = 1.0
    self.quantity_after_efficiency_calculation = 10.0 # (1.0 * 10.0) / 1.0

    sequence_list.play(self)

    # case - we need Q:10.0, efficiency is 125% (miracle?), producing 1.0
    self.transformation_transformed_resource_quantity = 10.0
    self.transformation_transformed_resource_efficiency = 1.25
    self.transformation_transformed_resource_industrial_phase_list = ['supply_phase1',]

    self.production_order_line_quantity = 1.0
    self.quantity_after_efficiency_calculation = 8.0 # (1.0 * 10.0) / 1.25

    sequence_list.play(self)

    # case - we need Q:10.0, efficiency is -80% (nonsense?), producing 1.0
    self.transformation_transformed_resource_quantity = 10.0
    self.transformation_transformed_resource_efficiency = -0.8
    self.transformation_transformed_resource_industrial_phase_list = ['supply_phase1',]

    self.production_order_line_quantity = 1.0
    self.quantity_after_efficiency_calculation = -12.5 # (1.0 * 10.0) / -0.8

    sequence_list.play(self)

1645
  @newSimulationExpectedFailure
1646 1647 1648 1649
  def test_09_testTransformationWithVariation(self, quiet=0, run=run_all_test):
    """
    Test, that variation from transformation works correctly on order

1650
    Note: Read below variables to know, what and how was defined
1651 1652 1653 1654 1655 1656 1657 1658
    """
    if not run: return

    self.transformation_transformed_resource_quantity = 0.0
    self.transformation_transformed_resource_industrial_phase_list = ['supply_phase1',]
    self.production_order_line_quantity = 0.0
    self.variation_category_list = ['colour','size']

1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680
    self.colour_size_quantity_dict = {
      'green' : {
        'Man' : 1.0,
        'Woman' : 2.0
      },
      'blue' : {
        'Man' : 3.0,
        'Woman' : 4.0
      },
    }

    self.colour_size_variation_dict = {
      'green' : {
        'Man' : ('colour/green','size/Man'),
        'Woman' : ('colour/green','size/Woman')
      },
      'blue' : {
        'Man' : ('colour/blue','size/Man'),
        'Woman' : ('colour/blue','size/Woman')
      },
    }

1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698
    self.order_line_variation_category_list = [
      'size/Man',
      'size/Woman',
      'colour/green',
      'colour/blue',
    ]

    self.order_line_colour_size_quantity_dict = {
      'green' : {
        'Man' : 9.0,
        'Woman' : 8.0
      },
      'blue' : {
        'Man' : 7.0,
        'Woman' : 6.0
      },
    }

1699 1700 1701 1702 1703 1704
    sequence_string = '\
                      CreateProductionOrganisation1 \
                      CreateSupplyOrganisation1 \
                      CreateSourcingSC \
                      Tic \
                      CreateColourSizeVariatedResource \
1705
                      CreateColourSizeVariatedComponent1 \
1706 1707 1708 1709 1710 1711 1712 1713 1714 1715
                      Tic \
                      CreateEmptyTransformation \
                      FillTransformationWithResource \
                      SetTransformationVariation \
                      Tic \
                      CreateTransformationTransformedResource \
                      FillTransformationTransformedResourceWithComponent1 \
                      SetTransformationTransformedResourceVariation \
                      SetTransformationTransformedResourceIndustrialPhaseList \
                      Tic \
1716 1717 1718
                      SetTransformationTransformedResourceQuantityMatrix \
                      SetTransformationTransformedResourceVariationMatrix \
                      Tic \
1719 1720 1721
                      CreateOrganisation \
                      CreateOrder \
                      CreateOrderLine \
1722 1723
                      SetOrderLineVariationCategories \
                      SetOrderLineQuantityMatrix \
1724 1725 1726 1727
                      Tic \
                      OrderOrder \
                      Tic \
                      CheckVariationSimulation \
1728 1729
                      ConfirmOrder \
                      Tic \
1730 1731 1732 1733 1734 1735
                      '
    sequence_list = SequenceList()
    sequence_list.addSequenceString(sequence_string)

    sequence_list.play(self)

1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749
  def stepCreateEmptySC(self, sequence=None, sequence_list=None, **kw):
    """
      Create a empty Supply Chain
    """
    portal = self.getPortal()
    supply_chain_module = portal.getDefaultModule( \
                                   portal_type=self.supply_chain_portal_type)
    supply_chain = supply_chain_module.newContent( \
                                   portal_type=self.supply_chain_portal_type)
    supply_chain.edit(
      title = "Supply Chain Empty",
    )
    sequence.edit(empty_supply_chain=supply_chain)

1750
  def stepCutPasteSupplyNodeInAnotherContainer(self, sequence=None,
1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
                                               sequence_list=None, **kw):
    """
    Cut/Paste a supply node in another container
    """
    supply_chain = sequence.get('supply_chain')
    empty_supply_chain = sequence.get('empty_supply_chain')

    supply_node = supply_chain.contentValues(portal_type='Supply Node')[0]
    cb_data = supply_chain.manage_cutObjects([supply_node.getId()])
    copied, = empty_supply_chain.manage_pasteObjects(cb_data)

1762
  def stepCheckPastedSupplyNode(self, sequence=None, sequence_list=None,
1763 1764 1765 1766 1767 1768 1769 1770 1771
                                 **kw):
    """
    Check pasted supply node
    """
    supply_chain = sequence.get('supply_chain')
    empty_supply_chain = sequence.get('empty_supply_chain')

    supply_node = empty_supply_chain.contentValues(portal_type='Supply Node')[0]
    supply_link = supply_chain.contentValues(portal_type='Supply Link')[0]
1772
    self.assertEqual(supply_node.getRelativeUrl(),
1773 1774 1775 1776 1777 1778
                      supply_link.getDestination())

def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestProductionOrder))
  return suite