testOpenOrder.py 17.8 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
##############################################################################
#
# Copyright (c) 2009 Nexedi KK, Nexedi SA and Contributors. All Rights Reserved.
#
# 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
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from DateTime import DateTime
30
import transaction
31 32 33 34 35 36 37 38 39 40 41 42 43 44


class TestOpenOrder(ERP5TypeTestCase):
  """
  Test Open Order
  """

  def getTitle(self):
    return 'Test Open Order'

  def getBusinessTemplateList(self):

    return ('erp5_base',
            'erp5_pdm',
45
            'erp5_simulation',
46
            'erp5_trade',
47
            'erp5_open_trade',
48
            'erp5_simulation_test')
49 50 51 52 53

  def afterSetUp(self):
    if getattr(self.portal, '_run_after_setup', None) is not None:
      return

54 55
    self.getRule(reference='default_open_order_rule').validate()
    self.getRule(reference='default_order_rule').validate()
56 57 58 59 60

    self.portal.portal_categories.base_amount.newContent(
      id='taxable',
      portal_type='Category',
      title='Taxable')
61 62
    tax = self.portal.service_module.newContent(
      portal_type='Service',
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
      title='VAT',
      base_contribution='base_amount/taxable')
    client = self.portal.organisation_module.newContent(
      id='client',
      portal_type='Organisation',
      title='Client')
    vendor = self.portal.organisation_module.newContent(
      id='vendor',
      portal_type='Organisation',
      title='Vendor')
    internet_connection = self.portal.service_module.newContent(
      id='internet_connection',
      title='Internet Connection',
      base_contribution='base_amount/taxable')
    training = self.portal.service_module.newContent(
      id='training',
      title='Training',
      base_contribution='base_amount/taxable')
    bread = self.portal.product_module.newContent(
      id='bread',
      title='Bread',
      base_contribution='base_amount/taxable')
    water = self.portal.product_module.newContent(
      id='water',
      title='Water',
      base_contribution='base_amount/taxable')
    main_trade_condition = self.portal.sale_trade_condition_module.newContent(
      id='main_trade_condition',
      portal_type='Sale Trade Condition',
      title='Vendor ---> Client',
      source=vendor.getRelativeUrl(),
      source_section=vendor.getRelativeUrl(),
      destination=client.getRelativeUrl(),
      destination_section=client.getRelativeUrl(),
      )
98
    main_trade_condition.newContent(portal_type='Trade Model Line',
99 100
                               title='VAT',
                               base_application='base_amount/taxable',
101
                               price=0.05)
102 103 104 105
    main_trade_condition.newContent(portal_type='Sale Supply Line',
                               resource=internet_connection.getRelativeUrl(),
                               priced_quantity=1,
                               base_price=200)
106 107 108
    # Use periodicity_minute=1 in order to see more details of DateTime
    # instance. Because if the time is 00:00:00, then it omits to display
    # some information like timezone for example.
109 110 111 112 113
    main_trade_condition.newContent(
      id='internet_connection_periodicity_line',
      portal_type='Periodicity Line',
      resource=internet_connection.getRelativeUrl(),
      periodicity_term_scope_type='until_the_end_of_month',
114
      periodicity_minute=1,
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
      periodicity_hour=0,
      periodicity_month_day=1)
    main_trade_condition.newContent(portal_type='Sale Supply Line',
                                    resource=training.getRelativeUrl(),
                                    priced_quantity=4,
                                    base_price=400)
    main_trade_condition.newContent(
      id='training_periodicity_line',
      portal_type='Periodicity Line',
      resource=training.getRelativeUrl(),
      periodicity_term_time_scale='day',
      periodicity_term_length_number=1,
      periodicity_hour=10,
      periodicity_week_day='Monday')
    main_trade_condition.newContent(portal_type='Sale Supply Line',
                               resource=bread.getRelativeUrl(),
                               priced_quantity=1,
                               base_price=10)
    main_trade_condition.newContent(
      id='bread_periodicity_line',
      portal_type='Periodicity Line',
      resource=bread.getRelativeUrl(),
      periodicity_term_time_scale='day',
      periodicity_term_length_number=0,
      periodicity_minute=0,
      periodicity_hour_list=(6, 12),
      periodicity_week_day_list=('Monday',
                                 'Tuesday',
                                 'Wednesday',
                                 'Thursday',
                                 'Friday',
                                 'Saturday'))
    main_trade_condition.newContent(portal_type='Sale Supply Line',
                               resource=water.getRelativeUrl(),
                               priced_quantity=1,
                               base_price=5)
    main_trade_condition.newContent(
      id='water_periodicity_line',
      portal_type='Periodicity Line',
      resource=water.getRelativeUrl(),
      periodicity_term_scope_type='until_the_next_period',
      periodicity_minute=0,
      periodicity_hour_list=10,
      periodicity_week_frequency=2,
      periodicity_week_day = 'Monday')

    # Inherit trade conditions to make sure that it works.
    useless_trade_condition = self.portal.sale_trade_condition_module.newContent(
      portal_type='Sale Trade Condition')

    self.portal.sale_trade_condition_module.newContent(
      id='trade_condition',
      portal_type='Sale Trade Condition',
      specialise_list=(useless_trade_condition.getRelativeUrl(),
                       main_trade_condition.getRelativeUrl())
      )
     
    self.portal._run_after_setup = True
173
    transaction.commit()
174 175
    self.tic()

176
  def testPeriodicityDateList(self, timezone=None):
177 178 179
    """
    Make sure that periodicity line can generate correct schedule.
    """
180 181 182
    #self.fail('Test disabled because it freezes')
    def D(yr, mo, dy, hr=0, mn=0, sc=0):
      return DateTime(yr, mo, dy, hr, mn, sc, timezone)
183
    # This across Summer time period, if server's timezone uses it.
184
    self.assertEqual(self.portal.sale_trade_condition_module.main_trade_condition.internet_connection_periodicity_line.getDatePeriodList(
185 186 187 188 189 190 191 192 193 194 195
      D(2008,1,15), D(2008,12,1)),
                    [(D(2008,2,1,0,1), DateTime(2008,2,29)),
                      (D(2008,3,1,0,1), DateTime(2008,3,31)),
                      (D(2008,4,1,0,1), DateTime(2008,4,30)),
                      (D(2008,5,1,0,1), DateTime(2008,5,31)),
                      (D(2008,6,1,0,1), DateTime(2008,6,30)),
                      (D(2008,7,1,0,1), DateTime(2008,7,31)),
                      (D(2008,8,1,0,1), DateTime(2008,8,31)),
                      (D(2008,9,1,0,1), DateTime(2008,9,30)),
                      (D(2008,10,1,0,1), DateTime(2008,10,31)),
                      (D(2008,11,1,0,1), DateTime(2008,11,30)),
196
                      ])
197

198
    self.assertEqual(self.portal.sale_trade_condition_module.main_trade_condition.bread_periodicity_line.getDatePeriodList(
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
      D(2008,2,26), D(2008,3,5)),
                    [(D(2008,2,26,6,0), D(2008,2,26,6,0)),
                      (D(2008,2,26,12,0), D(2008,2,26,12,0)),
                      (D(2008,2,27,6,0), D(2008,2,27,6,0)),
                      (D(2008,2,27,12,0), D(2008,2,27,12,0)),
                      (D(2008,2,28,6,0), D(2008,2,28,6,0)),
                      (D(2008,2,28,12,0), D(2008,2,28,12,0)),
                      (D(2008,2,29,6,0), D(2008,2,29,6,0)),
                      (D(2008,2,29,12,0), D(2008,2,29,12,0)),
                      (D(2008,3,1,6,0), D(2008,3,1,6,0)),
                      (D(2008,3,1,12,0), D(2008,3,1,12,0)),
                      (D(2008,3,3,6,0), D(2008,3,3,6,0)),
                      (D(2008,3,3,12,0), D(2008,3,3,12,0)),
                      (D(2008,3,4,6,0), D(2008,3,4,6,0)),
                      (D(2008,3,4,12,0), D(2008,3,4,12,0)),
214 215 216
                      ])

    self.assertEqual(self.portal.sale_trade_condition_module.main_trade_condition.water_periodicity_line.getDatePeriodList(
217 218 219 220 221 222
      D(2008,2,16), D(2008,4,15)),
                    [(D(2008,2,18,10,0), D(2008,3,3,10,0)),
                      (D(2008,3,3,10,0), D(2008,3,17,10,0)),
                      (D(2008,3,17,10,0), D(2008,3,31,10,0)),
                      (D(2008,3,31,10,0), D(2008,4,14,10,0)),
                      (D(2008,4,14,10,0), D(2008,4,28,10,0)),
223 224
                      ])
    self.assertEqual(self.portal.sale_trade_condition_module.main_trade_condition.training_periodicity_line.getDatePeriodList(
225 226 227 228
      D(2008,2,16), D(2008,3,6)),
                    [(D(2008,2,18,10,0), D(2008,2,19,10,0)),
                      (D(2008,2,25,10,0), D(2008,2,26,10,0)),
                      (D(2008,3,3,10,0), D(2008,3,4,10,0)),
229 230
                      ])

231 232 233
  def testPeriodicityDateListUniversal(self):
    self.testPeriodicityDateList('Universal')

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
  def testOpenOrderRule(self):
    """
    Make sure that Open Order Rule can generate simulation movements by
    following trade conditon's periodicity setting and order's forecasting term.
    """
    open_sale_order = self.portal.sale_order_module.newContent(
      portal_type='Open Sale Order',
      specialise=self.portal.sale_trade_condition_module.trade_condition.getRelativeUrl(),
      start_date=DateTime(3000,2,9),
      stop_date=DateTime(3000,8,1),
      )

    open_sale_order_line = open_sale_order.newContent(
      portal_type='Open Sale Order Line',
      resource=self.portal.service_module.training.getRelativeUrl(),
      quantity=1)

    open_sale_order.Order_applyTradeCondition(open_sale_order.getSpecialiseValue())

253
    transaction.commit()
254 255 256 257
    self.tic()

    self.assertEqual(open_sale_order_line.getPrice(), 100)
    self.assertEqual(open_sale_order.getTotalPrice(), 100)
258 259
    # TODO: test equivalent feature with trade model line
    # self.assertEqual(open_sale_order.getTotalNetPrice(), 105)
260

Yusei Tahara's avatar
Yusei Tahara committed
261
    open_sale_order.setForecastingTermDayCount(5)
262 263 264
    open_sale_order.order()
    open_sale_order.start()

265
    transaction.commit()
266 267 268 269 270
    self.tic()

    applied_rule = open_sale_order.getCausalityRelatedValue(portal_type='Applied Rule')
    self.assertEqual(len(applied_rule.objectIds()), 0)

271 272
    open_order_rule = self.getRule(reference='default_open_order_rule')
    open_order_rule.expand(
273 274 275
      applied_rule,
      calculation_base_date=DateTime(3000,2,9))

276
    transaction.commit()
277 278 279 280 281 282
    self.tic()

    self.assertEqual(len(applied_rule.objectIds()), 1)
    self.assertEqual(applied_rule['1'].getStartDate(), DateTime(3000,2,10,10,0))
    self.assertEqual(applied_rule['1'].getStopDate(), DateTime(3000,2,11,10,0))

Yusei Tahara's avatar
Yusei Tahara committed
283
    open_sale_order.setForecastingTermDayCount(10)
284
    open_order_rule.expand(
285 286 287
      applied_rule,
      calculation_base_date=DateTime(3000,2,9))

288
    transaction.commit()
289 290 291 292 293 294
    self.tic()

    self.assertEqual(len(applied_rule.objectIds()), 2)
    self.assertEqual(applied_rule['2'].getStartDate(), DateTime(3000,2,17,10,0))
    self.assertEqual(applied_rule['2'].getStopDate(), DateTime(3000,2,18,10,0))

295
    open_order_rule.expand(
296 297 298
      applied_rule,
      calculation_base_date=DateTime(3000,3,1))

299
    transaction.commit()
300 301 302 303 304 305 306 307 308 309 310 311
    self.tic()

    self.assertEqual(len(applied_rule.objectIds()), 5)
    self.assertEqual([(movement.getStartDate(), movement.getStopDate())
                      for movement in applied_rule.objectValues(sort_on='start_date')],
                     [(DateTime(3000,2,10,10,0), DateTime(3000,2,11,10,0)),
                      (DateTime(3000,2,17,10,0), DateTime(3000,2,18,10,0)),
                      (DateTime(3000,2,24,10,0), DateTime(3000,2,25,10,0)),
                      (DateTime(3000,3,3,10,0), DateTime(3000,3,4,10,0)),
                      (DateTime(3000,3,10,10,0), DateTime(3000,3,11,10,0))
                      ])

312
    open_order_rule.expand(
313 314 315
      applied_rule,
      calculation_base_date=DateTime(3000,3,1))

316
    transaction.commit()
317 318 319 320 321
    self.tic()

    self.assertEqual(len(applied_rule.objectIds()), 5)

    self.portal.sale_trade_condition_module.main_trade_condition.setExpirationDate(DateTime(3000,3,22))
322
    open_order_rule.expand(
323 324 325
      applied_rule,
      calculation_base_date=DateTime(3000,3,30))

326
    transaction.commit()
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
    self.tic()

    self.assertEqual(len(applied_rule.objectIds()), 6)
    self.assertEqual([(movement.getStartDate(), movement.getStopDate())
                      for movement in applied_rule.objectValues(sort_on='start_date')],
                     [(DateTime(3000,2,10,10,0), DateTime(3000,2,11,10,0)),
                      (DateTime(3000,2,17,10,0), DateTime(3000,2,18,10,0)),
                      (DateTime(3000,2,24,10,0), DateTime(3000,2,25,10,0)),
                      (DateTime(3000,3,3,10,0), DateTime(3000,3,4,10,0)),
                      (DateTime(3000,3,10,10,0), DateTime(3000,3,11,10,0)),
                      (DateTime(3000,3,17,10,0), DateTime(3000,3,18,10,0)),
                      ])

  def testBuildingSaleOrder(self):
    """
    Make sure that open sale order can create sale orders repeatedly
    """
    open_sale_order = self.portal.sale_order_module.newContent(
      portal_type='Open Sale Order',
      specialise=self.portal.sale_trade_condition_module.trade_condition.getRelativeUrl(),
      start_date=DateTime(3000,2,9),
      stop_date=DateTime(3000,8,1),
Yusei Tahara's avatar
Yusei Tahara committed
349
      forecasting_term_day_count=5
350 351 352 353 354
      )

    # Remove other test's side effect.
    self.portal.sale_trade_condition_module.main_trade_condition.setExpirationDate(None)

355
    transaction.commit()
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
    self.tic()

    open_sale_order.newContent(
      title='Piano Lesson',
      portal_type='Open Sale Order Line',
      resource=self.portal.service_module.training.getRelativeUrl(),
      quantity=1)

    open_sale_order.newContent(
      title='Internet Connection',
      portal_type='Open Sale Order Line',
      resource=self.portal.service_module.internet_connection.getRelativeUrl(),
      quantity=1)

    open_sale_order.newContent(
      title='Bread Delivery Serivce',
      portal_type='Open Sale Order Line',
      resource=self.portal.product_module.bread.getRelativeUrl(),
      quantity=1)

    open_sale_order.newContent(
      title='Mineral Water Delivery Service',
      portal_type='Open Sale Order Line',
      resource=self.portal.product_module.water.getRelativeUrl(),
      quantity=1)

    open_sale_order.Order_applyTradeCondition(open_sale_order.getSpecialiseValue())

384
    transaction.commit()
385 386 387 388 389
    self.tic()

    open_sale_order.order()
    open_sale_order.start()

390
    transaction.commit()
391 392 393 394 395 396 397
    self.tic()

    applied_rule = open_sale_order.getCausalityRelatedValue(portal_type='Applied Rule')
    self.assertEqual(len(applied_rule.objectIds()), 0)

    open_sale_order.autoOrderPeriodically(comment='Test', calculation_base_date=DateTime(3000,2,9))

398
    transaction.commit()
399 400 401 402 403 404 405 406
    self.tic()

    self.assertEqual(len(applied_rule.objectIds()), 9)
    self.assertEqual(len(open_sale_order.getCausalityRelatedValueList(portal_type='Sale Order')), 9)

    # Do the same thing and nothing happens.
    open_sale_order.autoOrderPeriodically(comment='Test', calculation_base_date=DateTime(3000,2,9))

407
    transaction.commit()
408 409 410 411 412 413 414 415
    self.tic()

    self.assertEqual(len(applied_rule.objectIds()), 9)
    self.assertEqual(len(open_sale_order.getCausalityRelatedValueList(portal_type='Sale Order')), 9)

    # Next
    open_sale_order.autoOrderPeriodically(comment='Test', calculation_base_date=DateTime(3000,2,14))

416
    transaction.commit()
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
    self.tic()

    self.assertEqual(len(applied_rule.objectIds()), 19)
    self.assertEqual(len(open_sale_order.getCausalityRelatedValueList(portal_type='Sale Order')), 19)

    # Check sale orders
    sale_order_list = [
      brain.getObject()
      for brain in self.portal.portal_catalog(portal_type='Sale Order',
                                              causality_uid=open_sale_order.getUid(),
                                              sort_on='delivery.start_date')]

    # The first order is bread.
    self.assertEqual(
      len(sale_order_list[0].objectValues(portal_type='Sale Order Line')),
      1)
    self.assertEqual(
      sale_order_list[0].objectValues(portal_type='Sale Order Line')[0].getTitle(),
      'Bread Delivery Serivce')
    self.assertEqual(sale_order_list[0].getTotalPrice(), 10)
437 438
    # TODO: test equivalent feature with trade model line
    # self.assertEqual(sale_order_list[0].getTotalNetPrice(), 10.5)
439 440 441 442 443 444 445 446 447

    # The second order is piano lesson.
    self.assertEqual(
      len(sale_order_list[1].objectValues(portal_type='Sale Order Line')),
      1)
    self.assertEqual(
      sale_order_list[1].objectValues(portal_type='Sale Order Line')[0].getTitle(),
      'Piano Lesson')
    self.assertEqual(sale_order_list[1].getTotalPrice(), 100)
448 449
    # TODO: test equivalent feature with trade model line
    #self.assertEqual(sale_order_list[1].getTotalNetPrice(), 105)
450 451 452 453 454 455


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