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

import unittest

31 32
import transaction
from DateTime import DateTime
33
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
34
from Products.ERP5Type.tests.utils import reindex
35
from AccessControl.SecurityManagement import newSecurityManager
36
from Products.ERP5Type.tests.Sequence import SequenceList
37
from testInvoice import TestSaleInvoiceMixin
38

39
class TestItemMixin(TestSaleInvoiceMixin):
40 41 42 43 44
  item_portal_type = 'Item'

  def getBusinessTemplateList(self):
    """
    """
45
    return TestSaleInvoiceMixin.getBusinessTemplateList(self) + ('erp5_item',)
46
  
Jérome Perrin's avatar
Jérome Perrin committed
47
  def login(self):
48 49 50 51 52
    uf = self.getPortal().acl_users
    uf._doAddUser('rc', '', ['Manager', 'Member', 'Assignee'], [])
    user = uf.getUserById('rc').__of__(uf)
    newSecurityManager(None, user)
    
Jérome Perrin's avatar
Jérome Perrin committed
53
  def createOrganisation(self, title=None):
54
    organisation_portal_type = 'Organisation'
Jérome Perrin's avatar
Jérome Perrin committed
55
    organisation_module = self.portal.getDefaultModule(
56
                                   portal_type=organisation_portal_type)
Jérome Perrin's avatar
Jérome Perrin committed
57
    organisation = organisation_module.newContent(
58 59 60 61
                                   portal_type=organisation_portal_type)
    if title is None:
      organisation.edit(title='organisation%s' % organisation.getId())
    else:
Jérome Perrin's avatar
Jérome Perrin committed
62 63 64
      organisation.edit(title=title)
    return organisation

65 66 67 68 69
  def createNotVariatedResource(self,title=None):
    """
      Create a resource with no variation
    """
    resource_portal_type = 'Product'
Jérome Perrin's avatar
Jérome Perrin committed
70
    resource_module = self.portal.getDefaultModule(resource_portal_type)
71 72 73 74 75 76 77 78
    resource = resource_module.newContent(portal_type=resource_portal_type)
    resource.edit(
      title = "NotVariatedResource%s" % resource.getId(),
      quantity_unit='unit/piece',
      aggregated_portal_type_list=['Item'],
      required_aggregated_portal_type_list=['Item']
    )
    return resource
Jérome Perrin's avatar
Jérome Perrin committed
79
  
80 81 82 83 84 85
  def createVariatedResource(self,title=None):
    preference = self.portal.portal_preferences
    portal_workflow = self.portal.portal_workflow
    pref = preference.newContent(portal_type='System Preference')
    pref.setPreferredProductIndividualVariationBaseCategoryList(['size'])
    portal_workflow.doActionFor(pref, 'enable_action')
Jérome Perrin's avatar
Jérome Perrin committed
86
    transaction.commit()
87 88 89
    self.tic()
    
    resource_portal_type = 'Product'
Jérome Perrin's avatar
Jérome Perrin committed
90
    resource_module = self.portal.getDefaultModule(resource_portal_type)
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    resource = resource_module.newContent(portal_type=resource_portal_type)
    resource.edit(
      title = "VariatedResource%s" % resource.getId(),
    )
    resource.setQuantityUnit('unit/piece')
    resource.setAggregatedPortalTypeList('Item')
    resource.setRequiredAggregatedPortalTypeList('Item')

    # Add size variation
    size_variation_count = 3
    for i in range(size_variation_count):
      variation_portal_type = 'Product Individual Variation'
      variation = resource.newContent(portal_type = variation_portal_type)
      variation.edit(
        title = 'SizeVariation%s' % str(i)
      )

    return resource
Jérome Perrin's avatar
Jérome Perrin committed
109

110 111 112 113 114 115 116
  def createPackingList(self,
                        resource=None,
                        organisation=None,
                        portal_type='Purchase Packing List'):
    portal = self.portal
    packing_list_module = portal.getDefaultModule(portal_type=portal_type)
    pac_list = packing_list_module.newContent(portal_type=portal_type)
117 118
    pac_list.edit(
      title = "PPL%s" % pac_list.getId(),
119 120
      start_date = self.datetime + 20,
      stop_date = self.datetime + 10,
121
    )
122

123 124 125 126 127 128 129 130 131 132 133
    if organisation is not None:
      pac_list.edit(source_value=organisation,
                 source_section_value=organisation,
                 destination_value=organisation,
                 destination_section_value=organisation,
                 source_decision_value=organisation,
                 destination_decision_value=organisation,
                 source_administration_value=organisation,
                 destination_administration_value=organisation,
                 )
    return pac_list
Jérome Perrin's avatar
Jérome Perrin committed
134

135 136 137 138 139
  def createPackingListLine(self,
                            packing_list=None,
                            resource=None,
                            portal_type='Purchase Packing List Line'):
    packing_list_line = packing_list.newContent(portal_type=portal_type)
140 141 142 143
    packing_list_line.edit(
      title = "Packing List Line"
    )
    packing_list_line.setResourceValue(resource)
Jérome Perrin's avatar
Jérome Perrin committed
144 145
    return packing_list_line

146 147 148 149
  def stepCreateItemList(self, sequence=None, sequence_list=None, **kw):
    """ Create some items """
    item_module = self.getPortal().item_module
    resource = sequence.get('resource')
150
    item = item_module.newContent(portal_type=self.item_portal_type)
151

Jérome Perrin's avatar
Jérome Perrin committed
152
    sequence.edit(item_list=[item])
153 154 155

  def stepOrderLineSetAggregationList(self, sequence=None,
                                          sequence_list=None, **kw):
156
    """  Aggregate Items """
157 158
    order_line = sequence.get('order_line')
    item_list = sequence.get('item_list')
Jérome Perrin's avatar
Jérome Perrin committed
159
    order_line.setAggregateValueList(item_list)
160

Jérome Perrin's avatar
Jérome Perrin committed
161
  def stepCheckOrderLineAggregate(self, sequence=None,
162 163 164
                                          sequence_list=None, **kw):
    """ Check items """
    order_line = sequence.get('order_line')
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    self.checkAggregate(line=order_line, sequence=sequence)

  def stepCheckSimulationAggregate(self, sequence=None,
                                          sequence_list=None, **kw):
    """ Check items """
    order_line = sequence.get('order_line')
    simulation_movement = order_line.getOrderRelatedValue()
    self.checkAggregate(line=simulation_movement, sequence=sequence)

  def stepCheckPackingListLineAggregate(self, sequence=None,
                                          sequence_list=None, **kw):
    """ Check items """
    packing_list_line = sequence.get('packing_list_line')
    self.checkAggregate(line=packing_list_line, sequence=sequence)

  def stepCheckInvoiceLineAggregate(self, sequence=None,
                                          sequence_list=None, **kw):
    """ Check items """
    invoice = sequence.get('invoice')
    invoice_line_list = invoice.contentValues(
                         portal_type=self.invoice_line_portal_type)
    self.checkAggregate(line=invoice_line_list[0], sequence=sequence)
187

188 189 190 191
  def checkAggregate(self, line=None, sequence=None):
    """ Check items """
    item_list = sequence.get('item_list')
    self.assertEquals(len(line.getAggregateList()),1)
Jérome Perrin's avatar
Jérome Perrin committed
192
    self.failUnless(item_list[0] in line.getAggregateValueList())
193
    
194

Jérome Perrin's avatar
Jérome Perrin committed
195
class TestItem(TestItemMixin, ERP5TypeTestCase):
196 197 198

  quiet = 0

Jérome Perrin's avatar
Jérome Perrin committed
199
  def test_01_ItemSimpleTest(self, quiet=quiet):
200 201 202
    sequence_list = SequenceList()

    # Test with a simply order without cell
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
    sequence_string = 'stepCreateOrganisation1 \
                       stepCreateOrganisation2 \
                       stepCreateOrganisation3 \
                       stepCreateItemList \
                       stepCreateOrder \
                       stepSetOrderProfile \
                       stepCreateNotVariatedResource \
                       stepTic \
                       stepCreateOrderLine \
                       stepSetOrderLineResource \
                       stepSetOrderLineDefaultValues \
                       stepOrderLineSetAggregationList \
                       stepConfirmOrder \
                       stepTic \
                       stepCheckOrderLineAggregate \
                       stepCheckOrderSimulation \
                       stepCheckSimulationAggregate \
                       stepCheckDeliveryBuilding \
                       stepCheckPackingListLineAggregate \
                       stepCheckPackingListIsNotDivergent '
223 224

    sequence_list.addSequenceString(sequence_string)
225 226 227
    sequence_list.play(self, quiet=quiet)


Jérome Perrin's avatar
Jérome Perrin committed
228
  def test_02_ItemWithInvoice(self, quiet=quiet):
229
    sequence_list = SequenceList()
230

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    sequence_string = 'stepCreateEntities \
                       stepCreateCurrency \
                       stepCreateItemList \
                       stepCreateSaleInvoiceTransactionRule \
                       stepCreateOrder \
                       stepSetOrderProfile \
                       stepSetOrderPriceCurrency \
                       stepCreateNotVariatedResource \
                       stepTic \
                       stepCreateOrderLine \
                       stepSetOrderLineResource \
                       stepSetOrderLineDefaultValues \
                       stepOrderLineSetAggregationList \
                       stepConfirmOrder \
                       stepTic \
                       stepCheckOrderRule \
                       stepCheckOrderLineAggregate \
                       stepCheckOrderSimulation \
                       stepCheckSimulationAggregate \
                       stepCheckDeliveryBuilding \
                       stepCheckPackingListLineAggregate \
                       stepAddPackingListContainer \
                       stepAddPackingListContainerLine \
                       stepSetContainerLineFullQuantity \
                       stepTic \
                       stepCheckPackingListIsPacked \
                       stepSetReadyPackingList \
                       stepTic \
                       stepStartPackingList \
                       stepCheckInvoicingRule \
                       stepTic \
                       stepCheckInvoiceBuilding \
                       stepRebuildAndCheckNothingIsCreated \
                       stepCheckInvoicesConsistency \
                       stepCheckInvoiceLineAggregate \
                      ' 

    sequence_list.addSequenceString(sequence_string)
269
    sequence_list.play(self, quiet=quiet)
270 271
    
  
Jérome Perrin's avatar
Jérome Perrin committed
272
  def test_03_CreateItemsFromPackingListLine(self):
273
    organisation = self.createOrganisation(title='Organisation I')
Jérome Perrin's avatar
Jérome Perrin committed
274
    transaction.commit()
275 276
    self.tic()
    resource = self.createVariatedResource()
277 278 279
    # XXX this tests depends on the relative url of the resource
    self.assertEquals('product_module/1', resource.getRelativeUrl())

Jérome Perrin's avatar
Jérome Perrin committed
280
    transaction.commit()
281 282
    self.tic()
    packing_list = self.createPackingList(resource=resource,organisation=organisation)
Jérome Perrin's avatar
Jérome Perrin committed
283 284
    packing_list_line= self.createPackingListLine(packing_list=packing_list,
                                                  resource=resource)
Jérome Perrin's avatar
Jérome Perrin committed
285
    transaction.commit()
286
    self.tic()
287 288 289 290
    
    # make sure we can render the dialog
    packing_list_line.DeliveryLine_viewItemCreationDialog()

291 292 293 294 295
    # create a listbox 
    listbox = ({ 'listbox_key': '000',
              'title': 'Lot A',
              'reference': '20_05_09_LA',
              'quantity': 20.0,
296
              'line_variation_category_list': 'size/product_module/1/3',
297 298 299 300 301
              },
              { 'listbox_key': '001',
              'title': 'Lot B',
              'reference': '20_05_09_LB',
              'quantity': 10.0,
302
              'line_variation_category_list': 'size/product_module/1/2',
303
              },
Jérome Perrin's avatar
Jérome Perrin committed
304
              { 'listbox_key': '002',
305 306 307
              'title': 'Lot C',
              'reference': '20_05_09_LC',
              'quantity': 15.0,
308
              'line_variation_category_list': 'size/product_module/1/1',
309 310
              },
              )
311 312

    self.portal.REQUEST.set('type', 'Item')
313
    packing_list_line.DeliveryLine_createItemList(listbox=listbox)
Jérome Perrin's avatar
Jérome Perrin committed
314
    transaction.commit()
315 316
    self.tic()
    self.assertEquals(
Jérome Perrin's avatar
Jérome Perrin committed
317 318 319
           len([x.getObject() for x in self.portal.portal_catalog(
                                          portal_type='Item',
                                          title='Lot A')]) ,1)
320
    self.assertEquals(
Jérome Perrin's avatar
Jérome Perrin committed
321 322 323
           len([x.getObject() for x in self.portal.portal_catalog(
                                          portal_type='Item',
                                          title='Lot B')]), 1)
324
    self.assertEquals(
Jérome Perrin's avatar
Jérome Perrin committed
325 326 327
           len([x.getObject() for x in self.portal.portal_catalog(
                                          portal_type='Item',
                                          title='Lot C')]), 1)
328 329 330 331 332 333 334 335

    self.assertEquals(packing_list_line.getTotalQuantity(), 45.0)
    self.assertEquals(sorted(packing_list_line.getVariationCategoryList()),
                      sorted(['size/product_module/1/3',
                              'size/product_module/1/2',
                              'size/product_module/1/1']))
    self.assertEquals(packing_list_line.getAggregateTitleList(), [])

336 337
    movement_cell_list = packing_list_line.contentValues(
                                    portal_type='Purchase Packing List Cell')
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
    self.assertEquals(3, len(movement_cell_list))

    cell = packing_list_line.getCell(base_id='movement',
                                     *('size/product_module/1/3', ))
    self.assertEquals(cell.getQuantity(), 20)
    self.assertEquals(['Lot A'], cell.getAggregateTitleList())

    cell = packing_list_line.getCell(base_id='movement',
                                     *('size/product_module/1/2', ))
    self.assertEquals(cell.getQuantity(), 10)
    self.assertEquals(['Lot B'], cell.getAggregateTitleList())

    cell = packing_list_line.getCell(base_id='movement',
                                     *('size/product_module/1/1', ))
    self.assertEquals(cell.getQuantity(), 15)
    self.assertEquals(['Lot C'], cell.getAggregateTitleList())
354
    
Jérome Perrin's avatar
Jérome Perrin committed
355
     
Jérome Perrin's avatar
Jérome Perrin committed
356
  def test_04_CreateItemsFromPackingListLineWithVariationDefined(self):
357
    organisation = self.createOrganisation(title='Organisation II')
Jérome Perrin's avatar
Jérome Perrin committed
358
    transaction.commit()
359 360
    self.tic()
    resource = self.createVariatedResource()
361 362 363
    # XXX this tests depends on the relative url of the resource
    self.assertEquals('product_module/2', resource.getRelativeUrl())

Jérome Perrin's avatar
Jérome Perrin committed
364
    transaction.commit()
365 366 367
    self.tic()
    packing_list = self.createPackingList(resource=resource,organisation=organisation)
   
Jérome Perrin's avatar
Jérome Perrin committed
368 369
    packing_list_line= self.createPackingListLine(packing_list=packing_list,
                                                  resource=resource)
Jérome Perrin's avatar
Jérome Perrin committed
370
    transaction.commit()
371 372 373 374 375 376
    self.tic()
    # create a listbox 
    listbox = ({ 'listbox_key': '000',
              'title': 'Lot A2',
              'reference': '25_05_09_LA2',
              'quantity': 20.0,
377
              'line_variation_category_list': 'size/product_module/2/3',
378 379
              },
              )
380
    self.portal.REQUEST.set('type', 'Item')
381
    packing_list_line.DeliveryLine_createItemList(listbox=listbox)
382 383 384 385
    self.assertEquals(packing_list_line.getVariationCategoryList(),
                      ['size/product_module/2/3'])
    self.assertEquals(packing_list_line.getTotalQuantity(), 20)

386 387 388 389 390
    # create listbox a second time
    listbox = ({ 'listbox_key': '000',
              'title': 'Lot B2',
              'reference': '25_05_09_LB2',
              'quantity': 20.0,
391
              'line_variation_category_list': 'size/product_module/2/1',
392
              },
Jérome Perrin's avatar
Jérome Perrin committed
393
              { 'listbox_key': '001',
394 395 396
              'title': 'Lot C2',
              'reference': '25_05_09_LC2',
              'quantity': 15.0,
397
              'line_variation_category_list': 'size/product_module/2/2',
398 399
              },
              )
400
    self.portal.REQUEST.set('type', 'Item')
401
    packing_list_line.DeliveryLine_createItemList(listbox=listbox)
Jérome Perrin's avatar
Jérome Perrin committed
402
    transaction.commit()
403
    self.tic()
404 405 406 407 408 409 410

    self.assertEquals(packing_list_line.getTotalQuantity(), 55.0)
    self.assertEquals(sorted(packing_list_line.getVariationCategoryList()),
                      sorted(['size/product_module/2/1',
                              'size/product_module/2/2',
                              'size/product_module/2/3']))

411 412
    movement_cell_list = packing_list_line.contentValues(
                                    portal_type='Purchase Packing List Cell')
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
    self.assertEquals(3, len(movement_cell_list))

    cell = packing_list_line.getCell(base_id='movement',
                                     *('size/product_module/2/3', ))
    self.assertEquals(cell.getQuantity(), 20)
    self.assertEquals(['Lot A2'], cell.getAggregateTitleList())

    cell = packing_list_line.getCell(base_id='movement',
                                     *('size/product_module/2/1', ))
    self.assertEquals(cell.getQuantity(), 20)
    self.assertEquals(['Lot B2'], cell.getAggregateTitleList())

    cell = packing_list_line.getCell(base_id='movement',
                                     *('size/product_module/2/2', ))
    self.assertEquals(cell.getQuantity(), 15)
    self.assertEquals(['Lot C2'], cell.getAggregateTitleList())
429
 
430

Jérome Perrin's avatar
Jérome Perrin committed
431
  def test_05_CreateItemsFromPackingListLineWithNotVariatedResource(self):
432
    organisation = self.createOrganisation(title='Organisation III')
Jérome Perrin's avatar
Jérome Perrin committed
433
    transaction.commit()
434 435
    self.tic()
    resource = self.createNotVariatedResource()
Jérome Perrin's avatar
Jérome Perrin committed
436
    transaction.commit()
437
    self.tic()
Jérome Perrin's avatar
Jérome Perrin committed
438 439
    packing_list = self.createPackingList(resource=resource,
                                          organisation=organisation)
440
   
Jérome Perrin's avatar
Jérome Perrin committed
441 442
    packing_list_line= self.createPackingListLine(packing_list=packing_list,
                                                  resource=resource)
443
    packing_list_line.setQuantity(32)
Jérome Perrin's avatar
Jérome Perrin committed
444
    transaction.commit()
445 446 447 448 449 450 451 452 453 454 455 456
    self.tic()
    # create a listbox 
    listbox = ({ 'listbox_key': '000',
              'title': 'Lot A3',
              'reference': '25_05_09_LA3',
              'quantity': 10.0,
              },
              { 'listbox_key': '001',
              'title': 'Lot B3',
              'reference': '25_05_09_LB3',
              'quantity': 5.0,
              },
Jérome Perrin's avatar
Jérome Perrin committed
457
              { 'listbox_key': '002',
458 459 460 461 462
              'title': 'Lot C3',
              'reference': '25_05_09_LC3',
              'quantity': 15.0,
              },
              )
463
    self.portal.REQUEST.set('type', 'Item')
464
    packing_list_line.DeliveryLine_createItemList(listbox=listbox)
Jérome Perrin's avatar
Jérome Perrin committed
465
    transaction.commit()
466 467
    self.tic()
    self.assertEquals(
Jérome Perrin's avatar
Jérome Perrin committed
468 469 470
           len([x.getObject() for x in self.portal.portal_catalog(
                                          portal_type='Item',
                                          title='Lot A3')]), 1)
471
    self.assertEquals(
Jérome Perrin's avatar
Jérome Perrin committed
472 473 474
           len([x.getObject() for x in self.portal.portal_catalog(
                                          portal_type='Item',
                                          title='Lot B3')]), 1)
475
    self.assertEquals(
Jérome Perrin's avatar
Jérome Perrin committed
476 477 478
           len([x.getObject() for x in self.portal.portal_catalog(
                                          portal_type='Item',
                                          title='Lot C3')]), 1)
479 480
    self.assertEquals(packing_list_line.getQuantity(),30.0)
    
Jérome Perrin's avatar
Jérome Perrin committed
481 482 483
    self.assertEquals(packing_list_line.getVariationCategoryList(), [])
    self.assertEquals(packing_list_line.getAggregateTitleList(),
                      ['Lot A3', 'Lot B3', 'Lot C3'])
484 485 486 487
    movement_cell_list = packing_list_line.contentValues(
                                    portal_type='Purchase Packing List Cell')
    self.assertEquals(movement_cell_list,[])
    
Jérome Perrin's avatar
Jérome Perrin committed
488

489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 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 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
  def test_select_item_dialog_no_variation(self):
    organisation = self.createOrganisation(title='Organisation III')
    resource = self.createNotVariatedResource()
    
    # create one item that is in organisation
    packing_list = self.createPackingList(resource=resource,
                                          organisation=organisation)
    packing_list.edit(source_section=None, source=None)
    packing_list_line = self.createPackingListLine(packing_list=packing_list,
                                                   resource=resource)
    item = self.portal.item_module.newContent(
                                    portal_type='Item',
                                    title='Test Item',
                                    reference='TI',
                                    quantity=12)
    packing_list_line.setAggregateValue(item)
    packing_list.confirm()
    packing_list.stop()
    self.assertEquals('stopped', packing_list.getSimulationState())
    transaction.commit()
    self.tic()
    

    packing_list = self.createPackingList(resource=resource,
                                          organisation=organisation,
                                          portal_type='Internal Packing List')
    packing_list_line = self.createPackingListLine(
                              packing_list=packing_list,
                              resource=resource,
                              portal_type='Internal Packing List Line')
    packing_list_line.setQuantity(32)
    
    # we can view the dialog
    packing_list_line.DeliveryLine_viewSelectItemListDialog()

    # the listbox contains the items physically in the source of the packing
    # list
    self.assertEquals([item],
                      packing_list_line.DeliveryLine_getSelectableItemList())
    
    packing_list_line.DeliveryLine_selectItemList(
              list_selection_name='select_item_fast_input_selection',
              listbox_uid=(item.getUid(),),
              uids=(item.getUid(),))

    self.assertEquals([item], packing_list_line.getAggregateValueList())
    self.assertEquals(12, packing_list_line.getQuantity())


  def test_select_item_dialog_variation(self):
    organisation = self.createOrganisation(title='Organisation IV')
    resource = self.createVariatedResource()
    variation_category_list = [ 'size/%s' % variation.getRelativeUrl()
                                 for variation in resource.contentValues() ]
    
    # create one item that is in organisation
    packing_list = self.createPackingList(resource=resource,
                                          organisation=organisation)
    packing_list.edit(source_section=None, source=None)
    packing_list_line = self.createPackingListLine(packing_list=packing_list,
                                                   resource=resource)
    packing_list_line.setVariationCategoryList(variation_category_list)

    variation = variation_category_list[0]
    cell = packing_list_line.newCell(base_id='movement',
                                     *(variation,))
    cell.edit(mapped_value_property_list=('quantity,'),
              quantity=1,
              variation_category_list=[variation])

    item = self.portal.item_module.newContent(
                                      portal_type='Item',
                                      title='Test Item %s' % variation,
                                      reference='TI%s' % variation,
                                      quantity=12)
    cell.setAggregateValue(item)

    packing_list.confirm()
    packing_list.stop()
    self.assertEquals('stopped', packing_list.getSimulationState())
    transaction.commit()
    self.tic()
    

    packing_list = self.createPackingList(resource=resource,
                                          organisation=organisation,
                                          portal_type='Internal Packing List')
    packing_list_line = self.createPackingListLine(
                              packing_list=packing_list,
                              resource=resource,
                              portal_type='Internal Packing List Line')
    packing_list_line.setQuantity(32)
    
    # we can view the dialog
    packing_list_line.DeliveryLine_viewSelectItemListDialog()

    # the listbox contains the items physically in the source of the packing
    # list, and have matching variations
    self.assertEquals([item],
                      packing_list_line.DeliveryLine_getSelectableItemList())
    
    packing_list_line.DeliveryLine_selectItemList(
              list_selection_name='select_item_fast_input_selection',
              listbox_uid=(item.getUid(),),
              uids=(item.getUid(),))

    self.assertEquals([variation],
                      packing_list_line.getVariationCategoryList())
    self.assertEquals(12, packing_list_line.getTotalQuantity())
    self.assertEquals([], packing_list_line.getAggregateValueList())

    self.assertEquals(1,
        len(packing_list_line.getCellValueList(base_id='movement')))
    
    cell = packing_list_line.getCell(base_id='movement', *(variation, ))
    self.assertEquals(12, cell.getQuantity())
    self.assertEquals([item], cell.getAggregateValueList())


608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
class TestItemScripts(ERP5TypeTestCase):
  """Test scripts from erp5_item.
  """
  def getBusinessTemplateList(self):
    return ('erp5_base', 'erp5_pdm', 'erp5_trade', 'erp5_item',)

  def afterSetUp(self):
    self.validateRules()
    size_category = self.portal.portal_categories.size
    if 'big' not in size_category.objectIds():
      size_category.newContent(portal_type='Category',
                               id='big',
                               title='Big')
    if 'small' not in size_category.objectIds():
      size_category.newContent(portal_type='Category',
                               id='small',
                               title='Small')

    self.node = self.portal.organisation_module.newContent(
                              portal_type='Organisation',
                              title='Node')
    self.section = self.portal.organisation_module.newContent(
                              portal_type='Organisation',
                              title='Section')
    self.mirror_node = self.portal.organisation_module.newContent(
                              portal_type='Organisation',
                              title='Mirror Node')
    self.mirror_section = self.portal.organisation_module.newContent(
                              portal_type='Organisation',
                              title='Mirror Section')
    self.product = self.portal.product_module.newContent(
                              portal_type='Product',
                              title='Product')
    self.variated_product = self.portal.product_module.newContent(
                              portal_type='Product',
                              title='Variated Product',
                              variation_base_category_list=('size',),
                              variation_category_list=('size/big',
                                                       'size/small'))
    self.item = self.portal.item_module.newContent(
                              portal_type='Item',
                              title='Item')
    transaction.commit()
    self.tic()
  
  def beforeTearDown(self):
    transaction.abort()
    for module in (self.portal.organisation_module,
                   self.portal.item_module,
                   self.portal.sale_packing_list_module,
                   self.portal.purchase_packing_list_module,
                   self.portal.product_module,
                   self.portal.portal_simulation,):
      module.manage_delObjects(list(module.objectIds()))
    transaction.commit()
    self.tic()

  @reindex
  def _makeSalePackingListLine(self):
    packing_list = self.portal.sale_packing_list_module.newContent(
                  portal_type='Sale Packing List',
                  source_value=self.mirror_node,
                  source_section_value=self.mirror_section,
                  destination_value=self.node,
                  destination_section_value=self.section,
                  start_date=DateTime() - 1,)
    line = packing_list.newContent(
                  portal_type='Sale Packing List Line',
                  quantity=1,
                  resource_value=self.product,
                  aggregate_value=self.item,)
    packing_list.confirm()
    packing_list.start()
    packing_list.deliver()
    return line

  # with line
  def test_Item_getResourceValue(self):
    self.assertEquals(None, self.item.Item_getResourceValue())
    line = self._makeSalePackingListLine()
    self.assertEquals(self.product, self.item.Item_getResourceValue())
689 690
    self.assertEquals(None, self.item.Item_getResourceValue(
                                at_date=DateTime() - 2))
691 692 693 694 695

  def test_Item_getResourceTitle(self):
    self.assertEquals(None, self.item.Item_getResourceTitle())
    line = self._makeSalePackingListLine()
    self.assertEquals('Product', self.item.Item_getResourceTitle())
696 697
    self.assertEquals(None, self.item.Item_getResourceTitle(
                                at_date=DateTime() - 2))
698 699 700 701 702

  def test_Item_getCurrentOwnerValue(self):
    self.assertEquals(None, self.item.Item_getCurrentOwnerValue())
    line = self._makeSalePackingListLine()
    self.assertEquals(self.section, self.item.Item_getCurrentOwnerValue())
703 704
    self.assertEquals(None,
        self.item.Item_getCurrentOwnerValue(at_date=DateTime() - 2))
705 706 707 708 709

  def test_Item_getCurrentOwnerTitle(self):
    self.assertEquals(None, self.item.Item_getCurrentOwnerTitle())
    line = self._makeSalePackingListLine()
    self.assertEquals('Section', self.item.Item_getCurrentOwnerTitle())
710 711
    self.assertEquals(None,
        self.item.Item_getCurrentOwnerTitle(at_date=DateTime() - 2))
712 713 714 715 716

  def test_Item_getCurrentSiteValue(self):
    self.assertEquals(None, self.item.Item_getCurrentSiteValue())
    line = self._makeSalePackingListLine()
    self.assertEquals(self.node, self.item.Item_getCurrentSiteValue())
717 718
    self.assertEquals(None, self.item.Item_getCurrentSiteValue(
                                            at_date=DateTime() - 2))
719 720 721 722 723

  def test_Item_getCurrentSiteTitle(self):
    self.assertEquals(None, self.item.Item_getCurrentSiteTitle())
    line = self._makeSalePackingListLine()
    self.assertEquals('Node', self.item.Item_getCurrentSiteTitle())
724 725
    self.assertEquals(None,
          self.item.Item_getCurrentSiteTitle(at_date=DateTime() - 2))
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755

  # with cells
  @reindex
  def _makeSalePackingListCellWithVariation(self):
    packing_list = self.portal.sale_packing_list_module.newContent(
                  portal_type='Sale Packing List',
                  source_value=self.mirror_node,
                  source_section_value=self.mirror_section,
                  destination_value=self.node,
                  destination_section_value=self.section,
                  start_date=DateTime() - 1,)
    line = packing_list.newContent(
                  portal_type='Sale Packing List Line',
                  resource_value=self.variated_product,)
    line.setVariationCategoryList(['size/small'])
    cell = line.newCell(portal_type='Sale Packing List Cell',
                 base_id='movement', *('size/small',))
    cell.edit(mapped_value_property_list=['price','quantity'],
              quantity=1,
              variation_category_list=['size/small'],
              aggregate_value=self.item)
    packing_list.confirm()
    packing_list.start()
    packing_list.deliver()
    return cell

  def test_Item_getVariationCategoryList(self):
    self.assertEquals([], self.item.Item_getVariationCategoryList())
    self._makeSalePackingListCellWithVariation()
    self.assertEquals(['size/small'], self.item.Item_getVariationCategoryList())
756 757
    self.assertEquals([],
        self.item.Item_getVariationCategoryList(at_date=DateTime() - 2))
758 759 760 761 762 763 764

  def test_Item_getVariationRangeCategoryItemList(self):
    self.assertEquals([], self.item.Item_getVariationRangeCategoryItemList())
    self._makeSalePackingListCellWithVariation()
    self.assertEquals([['Big', 'size/big'],
                       ['Small', 'size/small']],
        self.item.Item_getVariationRangeCategoryItemList())
765 766 767
    self.assertEquals([],
        self.item.Item_getVariationRangeCategoryItemList(
                          at_date=DateTime() - 2))
768

Jérome Perrin's avatar
Jérome Perrin committed
769
    
770 771 772
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestItem))
773
  suite.addTest(unittest.makeSuite(TestItemScripts))
774 775
  return suite