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

TODO:
Fabien Morin's avatar
Fabien Morin committed
32
  - review naming of new methods
33 34 35
  - in the test test_04_paySheetCalculation, add sub_object (annotation_line,
  ratio_line and payment conditioni), and verify that before the script
  'PaySheetTransaction_applyModel' is called, subobjects are not in the
36 37
  paysheet, and after that there are copied in.
  - use ratio settings and test it (there is a method getRatioQuantityList, see
Fabien Morin's avatar
Fabien Morin committed
38
  the file Document/PaySheetTransaction.py)
39
  - test with bonus which participate on the base_salary and see if the
40 41 42
  contribution are applied on the real base_salary or on the base_salary + bonus
  (it should).

43
WARNING:
Fabien Morin's avatar
Fabien Morin committed
44 45
  - current API naming may change although model should be stable.

46 47
"""

48
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5ReportTestCase
49 50
from AccessControl.SecurityManagement import newSecurityManager
from Testing import ZopeTestCase
51
from DateTime import DateTime
52

53
class TestPayrollMixin(ERP5ReportTestCase):
54 55 56 57 58 59 60 61 62 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 98 99 100

  paysheet_model_portal_type        = 'Pay Sheet Model'
  paysheet_model_line_portal_type   = 'Pay Sheet Model Line'
  paysheet_transaction_portal_type  = 'Pay Sheet Transaction'
  paysheet_line_portal_type         = 'Pay Sheet Line'
  payroll_service_portal_type       = 'Payroll Service'
  currency_portal_type              = 'Currency'
  person_portal_type                = 'Person'
  organisation_portal_type          = 'Organisation'


  default_region                    = 'europe/west/france'
  france_settings_forfait           = 'france/forfait'
  france_settings_slice_a           = 'france/tranche_a'
  france_settings_slice_b           = 'france/tranche_b'
  france_settings_slice_c           = 'france/tranche_c'
  tax_category_employer_share       = 'employer_share'
  tax_category_employee_share       = 'employee_share'
  base_amount_deductible_tax        = 'deductible_tax'
  base_amount_non_deductible_tax    = 'deductible_tax'
  base_amount_bonus                 = 'bonus'
  base_amount_base_salary           = 'base_salary'
  grade_worker                      = 'worker'
  grade_engineer                    = 'engineer'

  plafond = 2682.0

  model = None
  model_id                          = 'model_one'
  model_title                       = 'Model One'
  person_id                         = 'one'
  person_title                      = 'One'
  person_career_grade               = 'worker'
  organisation_id                   = 'company_one'
  organisation_title                = 'Company One'
  variation_settings_category_list  = ['salary_range/france',]
  price_currency                    = 'currency_module/EUR'

  def getTitle(self):
    return "Payroll"

  def afterSetUp(self):
    """Prepare the test."""
    self.portal = self.getPortal()
    self.organisation_module = self.portal.organisation_module
    self.person_module = self.portal.person_module
    self.payroll_service_module = self.portal.payroll_service_module
101
    self.paysheet_model_module = self.portal.paysheet_model_module
Jérome Perrin's avatar
Jérome Perrin committed
102
    self.validateRules()
103 104 105
    self.createCategories()
    self.createCurrencies()

106 107 108
    self.model = self.createModel(self.model_id, self.model_title,
        self.person_id, self.person_title, self.person_career_grade,
        self.organisation_id, self.organisation_title,
109 110 111 112 113 114 115 116
        self.variation_settings_category_list, self.price_currency)

    self.login()

    # creation of payroll services
    self.urssaf_id = 'sickness_insurance'
    self.labour_id = 'labour'

117 118
    self.urssaf_slice_list = ['salary_range/'+self.france_settings_slice_a,
                              'salary_range/'+self.france_settings_slice_b,
119 120 121 122 123 124 125 126 127
                              'salary_range/'+self.france_settings_slice_c]

    self.urssaf_share_list = ['tax_category/'+self.tax_category_employee_share,
                              'tax_category/'+self.tax_category_employer_share]

    self.salary_slice_list = ['salary_range/'+self.france_settings_forfait,]
    self.salary_share_list = ['tax_category/'+self.tax_category_employee_share,]


128 129 130
    self.payroll_service_organisation = self.createOrganisation(
                                          id='urssaf', title='URSSAF')
    self.urssaf = self.createPayrollService(id=self.urssaf_id,
131 132
        title='State Insurance',
        product_line='state_insurance',
133 134 135 136
        variation_base_category_list=['tax_category', 'salary_range'],
        variation_category_list=self.urssaf_slice_list + \
                                self.urssaf_share_list)

137
    self.labour = self.createPayrollService(id=self.labour_id,
138 139
        title='Labour',
        product_line='labour',
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 173 174
        variation_base_category_list=['tax_category', 'salary_range'],
        variation_category_list=self.salary_slice_list +\
                                self.salary_share_list)

  def _safeTic(self):
    """Like tic, but swallowing errors, usefull for teardown"""
    try:
      get_transaction().commit()
      self.tic()
    except RuntimeError:
      pass

  def beforeTearDown(self):
    """Clear everything for next test."""
    self._safeTic()
    for module in [ 'organisation_module',
                    'person_module',
                    'currency_module',
                    'payroll_service_module',
                    'paysheet_model_module',
                    'accounting_module']:
      folder = getattr(self.getPortal(), module, None)
      if folder:
        [x.unindexObject() for x in folder.objectValues()]
        self._safeTic()
        folder.manage_delObjects([x.getId() for x in folder.objectValues()])
    self._safeTic()
    # cancel remaining messages
    activity_tool = self.getPortal().portal_activities
    for message in activity_tool.getMessageList():
      activity_tool.manageCancel(message.object_path, message.method_id)
      ZopeTestCase._print('\nCancelling active message %s.%s()\n'
                          % (message.object_path, message.method_id) )
    get_transaction().commit()

175
  def login(self):
176
    uf = self.getPortal().acl_users
177
    uf._doAddUser('admin', '', ['Manager', 'Assignee', 'Assignor',
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
                               'Associate', 'Auditor', 'Author'], [])
    user = uf.getUserById('admin').__of__(uf)
    newSecurityManager(None, user)

  def createCategories(self):
    """Create the categories for our test. """
    # create categories
    for cat_string in self.getNeededCategoryList() :
      base_cat = cat_string.split("/")[0]
      # if base_cat not exist, create it
      if getattr(self.getPortal().portal_categories, base_cat, None) == None:
        self.getPortal().portal_categories.newContent(\
                                          portal_type='Base Category',
                                          id=base_cat)
        get_transaction().commit()
        self.tic()
      path = self.getPortal().portal_categories[base_cat]
      for cat in cat_string.split("/")[1:] :
        if not cat in path.objectIds() :
          path = path.newContent(
                    portal_type='Category',
                    id=cat,
                    title=cat.replace('_', ' ').title(),)
        else:
          path = path[cat]
    get_transaction().commit()
    self.tic()
    # check categories have been created
    for cat_string in self.getNeededCategoryList() :
      self.assertNotEquals(None,
                self.getCategoryTool().restrictedTraverse(cat_string),
                cat_string)

  def getNeededCategoryList(self):
    """return a list of categories that should be created."""
    return ('region/%s' % self.default_region,
            'salary_range/%s' % self.france_settings_forfait,
            'salary_range/%s' % self.france_settings_slice_a,
            'salary_range/%s' % self.france_settings_slice_b,
            'salary_range/%s' % self.france_settings_slice_c,
            'tax_category/%s' % self.tax_category_employer_share,
            'tax_category/%s' % self.tax_category_employee_share,
            'base_amount/%s' % self.base_amount_deductible_tax,
            'base_amount/%s' % self.base_amount_non_deductible_tax,
            'base_amount/%s' % self.base_amount_bonus,
            'base_amount/%s' % self.base_amount_base_salary,
224
            'base_amount/net_salary',
225 226
            'grade/%s' % self.grade_worker,
            'grade/%s' % self.grade_engineer,
227
            'quantity_unit/time/month',
228 229 230 231
            'group/demo_group',
            'product_line/base_salary',
            'product_line/payroll_tax_1',
            'product_line/payroll_tax_2',
232 233 234 235
           )

  def createCurrencies(self):
    """Create some currencies.
236
    This script will reuse existing currencies, because we want currency ids
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
    to be stable, as we use them as categories.
    """
    currency_module = self.getCurrencyModule()
    if not hasattr(currency_module, 'EUR'):
      self.EUR = currency_module.newContent(
          portal_type = self.currency_portal_type,
          reference = "EUR", id = "EUR", base_unit_quantity=0.001 )
      self.USD = currency_module.newContent(
          portal_type = self.currency_portal_type,
          reference = "USD", id = "USD" )
      self.YEN = currency_module.newContent(
          portal_type = self.currency_portal_type,
          reference = "YEN", id = "YEN" )
      get_transaction().commit()
      self.tic()
    else:
      self.EUR = currency_module.EUR
      self.USD = currency_module.USD
      self.YEN = currency_module.YEN

  def getBusinessTemplateList(self):
    """ """
259
    return ('erp5_base', 'erp5_pdm', 'erp5_trade', 'erp5_accounting',
260
            'erp5_invoicing', 'erp5_payroll', )
261

262
  def createPerson(self, id='one', title='One',
263 264 265 266 267 268 269 270
      career_subordination_value=None, career_grade=None, **kw):
    """
      Create some Pesons so that we have something to feed.
    """
    person_module = self.portal.getDefaultModule(portal_type=\
                                                 self.person_portal_type)
    if hasattr(person_module, id):
      person_module.manage_delObjects([id])
271
    person = person_module.newContent(portal_type=self.person_portal_type,
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
                                      id=id)
    person.edit(
        title=title,
        career_subordination_value=career_subordination_value,
        career_grade=career_grade,
               )
    get_transaction().commit()
    self.tic()
    return person

  def createOrganisation(self, id='company_one', title='Company One', **kw):
    if hasattr(self.organisation_module, id):
      self.organisation_module.manage_delObjects([id])
    organisation = self.organisation_module.newContent( \
                                   portal_type=self.organisation_portal_type,
                                   id=id,
                                   title=title)
    get_transaction().commit()
    self.tic()
    return organisation

293
  def createPayrollService(self, id='', title='',
294
      variation_base_category_list=None,
295
      variation_category_list=None, product_line=None, **kw):
296

297 298 299 300
    payroll_service_portal_type = 'Payroll Service'
    payroll_service_module = self.portal.getDefaultModule(\
                                    portal_type=payroll_service_portal_type)

301
    if variation_category_list == None:
302
      variation_category_list=[]
303
    if variation_base_category_list == None:
304 305 306 307
      variation_category_list=[]
    if hasattr(payroll_service_module, id):
      payroll_service_module.manage_delObjects([id])

308 309 310 311 312
    payroll_service = payroll_service_module.newContent(
                            title=title,
                            portal_type=self.payroll_service_portal_type,
                            id=id,
                            quantity_unit='time/month',
313
                            product_line=product_line)
314 315 316 317 318 319
    payroll_service.setVariationBaseCategoryList(variation_base_category_list)
    payroll_service.setVariationCategoryList(variation_category_list)
    get_transaction().commit()
    self.tic()
    return payroll_service

320 321
  def createModel(self, id, title='', person_id='',
      person_title='', person_career_grade='',
322 323 324 325 326 327 328 329 330 331
      organisation_id='', organisation_title='',
      variation_settings_category_list=None,
      price_currency=''):
    """
      Create a model
    """
    if variation_settings_category_list == None:
      variation_settings_category_list = []

    organisation = self.createOrganisation(organisation_id, organisation_title)
332
    person = self.createPerson(id=person_id, title=person_title,
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
                               career_subordination_value=organisation,
                               career_grade=person_career_grade)

    if hasattr(self.paysheet_model_module, id):
      self.paysheet_model_module.manage_delObjects([id])
    paysheet_model = self.paysheet_model_module.newContent( \
                                portal_type=self.paysheet_model_portal_type,
                                id=id)
    paysheet_model.edit(\
        title=title,
        variation_settings_category_list=variation_settings_category_list,
        destination_section_value=organisation,
        source_section_value=person,)
    paysheet_model.setPriceCurrency(price_currency)
    get_transaction().commit()
    self.tic()

    return paysheet_model

  def addSlice(self, model, slice, min_value, max_value, base_id='cell'):
    '''
      add a new slice in the model
    '''
356
    slice_value = model.newCell(slice, portal_type='Pay Sheet Model Slice',
357
        base_id=base_id)
358 359
    slice_value.setQuantityRangeMax(max_value)
    slice_value.setQuantityRangeMin(min_value)
360 361
    get_transaction().commit()
    self.tic()
362
    return slice_value
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378

  def addAllSlices(self, model):
    '''
      create all usefull slices with min and max values
    '''
    slice_list = []
    slice_list.append(self.addSlice(model, 'salary_range/%s' % \
        self.france_settings_forfait, 0, 9999999999999))
    slice_list.append(self.addSlice(model, 'salary_range/%s' % \
        self.france_settings_slice_a, 0, self.plafond))
    slice_list.append(self.addSlice(model, 'salary_range/%s' % \
        self.france_settings_slice_b, self.plafond, self.plafond*4))
    slice_list.append(self.addSlice(model, 'salary_range/%s' % \
        self.france_settings_slice_c, self.plafond*4, self.plafond*8))
    return slice_list

379 380 381 382 383 384 385 386 387 388
  def createModelLine(self,
                      model,
                      id,
                      variation_category_list,
                      resource,
                      slice_list,
                      share_list,
                      values,
                      editable=False,
                      source_value=None,
389 390
                      base_application_list=[],
                      base_contribution_list=[]):
391
    '''
392
      test the function addModelLine and test if the model line has been
393 394
      well created.
      explaination for values :
395 396 397 398
      if slice_list is ('slice_a', 'slice_b') and share list is ('employer',
      'employee') and if you want to put 100 % of 1000 for slice_a for the
      employee and employer, and 50% of the base_application for slice_b
      employer and and 2000 for slice_b employee, the value list will look
399 400 401 402
      like this :
      values = [[[1000, 1], [1000, 1]], [[2000, None], [None, 0.5]]]

      next, two representations to well understand :
403

404
       'employee_share', 'employer_share'
405
      [[  1470, None  ], [  2100, None  ]]
406 407 408 409 410 411
       'salary_range/france/forfait'

    'employee_share',  'employer_share'   'employee_share',  'employer_share'
[ [   None, 0.01   ], [   None, 0.02   ],[   None, 0.01  ], [   None, 0.02  ] ]
'salary_range/france/tranche_a''salary_range/france/tranche_b'
    '''
412

413
    # verify if category used in this model line are selected in the resource
414 415 416 417 418 419 420
    resource_list = resource.getVariationCategoryList(base=1)
    msg='%r != %r' % (resource_list, variation_category_list)
    for i in variation_category_list:
      self.failUnless(i in resource_list, msg)

    if hasattr(model, id):
      model.manage_delObjects([id])
421 422 423 424 425 426
    model_line = model.newContent(
                        portal_type=self.paysheet_model_line_portal_type,
                        id=id,
                        resource_value=resource,
                        source_value=source_value,
                        editable=editable,
427 428
                        base_application_list=base_application_list,
                        base_contribution_list=base_contribution_list,
429
                        variation_category_list=variation_category_list,)
430 431 432 433 434 435 436 437
    get_transaction().commit()
    self.tic()

    # put values in Model Line cells
    model_line.updateCellRange(base_id='movement')
    for slice in slice_list:
      for share in share_list:
        cell = model_line.newCell(\
438
            share, slice, portal_type='Pay Sheet Cell', base_id='movement')
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
        cell.setMappedValuePropertyList(['quantity', 'price'])
        amount = values[share_list.index(share)][slice_list.index(slice)][0]
        percent = values[share_list.index(share)][slice_list.index(slice)][1]
        if amount != None:
          cell.setQuantity(amount)
        if percent != None:
          cell.setPrice(percent)
        get_transaction().commit()
        self.tic()

    return model_line

  def createPaySheet(self, model, id='my_paysheet'):
    '''
      create a Pay Sheet with the model specialisation
    '''
    paysheet_module = self.portal.getDefaultModule(\
                            portal_type=self.paysheet_transaction_portal_type)
    if hasattr(paysheet_module, id):
      paysheet_module.manage_delObjects([id])
    paysheet = paysheet_module.newContent(\
        portal_type               = self.paysheet_transaction_portal_type,
        id                        = id,
        title                     = id,
        specialise_value          = model,
        source_section_value      = model.getSourceSectionValue(),
465 466 467
        destination_section_value = model.getDestinationSectionValue(),
        start_date                = DateTime(2008, 1, 1),
        stop_date                 = DateTime(2008, 1, 31),)
468 469 470 471 472 473 474
    paysheet.setPriceCurrency('currency_module/EUR')
    get_transaction().commit()
    self.tic()
    return paysheet

  def calculatePaySheet(self, paysheet):
    '''
475
      Calcul the given paysheet like if you have click on the 'Calculation of
476 477
      the Pay Sheet Transaction' action button.
      XXX Editable line are not yet take into account
478
      XXX this method should not exist ! use the standard method
479 480
    '''
    paysheet_line_list = \
481
        paysheet.createPaySheetLineList()
482 483 484 485 486 487 488 489 490 491 492 493 494 495
    portal_type_list = ['Annotation Line', 'Payment Condition',
                        'Pay Sheet Model Ratio Line']
    paysheet.PaySheetTransaction_copySubObject(portal_type_list)
    get_transaction().commit()
    self.tic()
    return paysheet_line_list

  def assertEqualAmounts(self, pay_sheet_line, correct_value_slice_list,
      base_salary, i):
    slice_list = pay_sheet_line.getVariationCategoryList(\
        base_category_list='base_salary')
    share_list = pay_sheet_line.getVariationCategoryList(\
        base_category_list='tax_category')
    for slice in slice_list:
496
      for share in share_list:
497 498 499 500 501 502 503 504 505 506 507 508 509 510
        cell = pay_sheet_line.getCell(share, slice)
        value = cell.getQuantity()
        min_slice = correct_value_slice_list[i-1]
        max_slice = correct_value_slice_list[i]

        if base_salary <= max_slice:
          correct_value = base_salary - min_slice
        else:
          correct_value = max_slice - min_slice
        self.assertEqual(correct_value, value)
      i += 1


class TestPayroll(TestPayrollMixin):
511
  quiet = 0
512

513
  def test_01_modelCreation(self):
514 515 516 517 518 519
    '''
      test the function createModel and test if the model has been well created
    '''

    if hasattr(self.paysheet_model_module, self.model_id):
      self.paysheet_model_module.manage_delObjects([self.model_id])
520

521 522 523 524
    model_count_before_add = \
        len(self.paysheet_model_module.contentValues(portal_type=\
        self.paysheet_model_portal_type))

525 526 527 528 529 530 531 532
    self.model = self.createModel(self.model_id,
                                  self.model_title,
                                  self.person_id,
                                  self.person_title,
                                  self.person_career_grade,
                                  self.organisation_id,
                                  self.organisation_title,
                                  self.variation_settings_category_list,
533 534 535 536 537 538 539 540 541 542 543 544 545
                                  self.price_currency)

    model_count_after_add = \
        len(self.paysheet_model_module.contentValues(portal_type=\
        self.paysheet_model_portal_type))

    # check that the number of model_lines has been incremented
    self.assertEqual(model_count_before_add+1, model_count_after_add)

    #check model have been well created
    self.model = self.paysheet_model_module._getOb(self.model_id)
    self.assertEqual(self.model_id, self.model.getId())
    self.assertEqual(self.model_title, self.model.getTitle())
546 547
    self.assertEqual(self.organisation_title,
                     self.model.getDestinationSectionTitle())
548
    self.assertEqual(self.person_title, self.model.getSourceSectionTitle())
549 550
    self.assertEqual(self.variation_settings_category_list,
                     self.model.getVariationSettingsCategoryList(base=1))
551

552
  def test_02_addModelLine(self):
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
    '''
      create a Model Line and test if it has been well created
    '''
    #model = self.createModel()
    self.addAllSlices(self.model)

    payroll_service_portal_type = 'Payroll Service'
    payroll_service_module = self.portal.getDefaultModule(\
                                    portal_type=payroll_service_portal_type)

    model_line_id = 'URSSAF'

    variation_category_list = self.urssaf_share_list + self.urssaf_slice_list

    model_line_count_before_add = len(self.model.contentValues(portal_type=\
        self.paysheet_model_line_portal_type))

570 571 572 573 574 575
    returned_model_line = self.createModelLine(
        model=self.model,
        id=model_line_id,
        variation_category_list=variation_category_list,
        resource=self.urssaf,
        share_list=self.urssaf_share_list,
576
        slice_list=self.urssaf_slice_list,
577
        values=[[[None, 0.01], [None, 0.02],[None, 0.03]], [[None, 0.04],
578 579 580
                 [None, 0.05], [None, 0.06]]],
        base_application_list=['base_amount/base_salary',],
        base_contribution_list=['base_amount/deductible_tax',])
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595

    model_line_count_after_add = len(self.model.contentValues(portal_type=\
        self.paysheet_model_line_portal_type))

    # check that the number of model_lines has been incremented
    self.assertEqual(model_line_count_before_add+1, model_line_count_after_add)

    model_line = self.model._getOb(model_line_id)
    self.assertEqual(returned_model_line, model_line)
    self.assertEqual(model_line_id, model_line.getId())
    payroll_service_portal_type = 'Payroll Service'
    payroll_service_module = self.portal.getDefaultModule(\
        portal_type=payroll_service_portal_type)
    resource = payroll_service_module._getOb(self.urssaf_id)
    self.assertEqual(resource, model_line.getResourceValue())
596
    self.assertEqual(variation_category_list,
597 598
        model_line.getVariationCategoryList())

599
  def test_03_createPaySheet(self):
600 601 602 603 604 605 606 607 608 609 610
    '''
      create a Pay Sheet with the model specialisation and verify it was well
      created
    '''
    paysheet_id = 'my_paysheet'
    paysheet_returned = self.createPaySheet(self.model, paysheet_id)
    paysheet_module = self.portal.getDefaultModule(\
                          portal_type=self.paysheet_transaction_portal_type)
    paysheet = paysheet_module._getOb(paysheet_id)
    self.assertEqual(paysheet_returned, paysheet)
    self.assertEqual(paysheet_id, paysheet.getId())
611
    self.assertEqual(paysheet.getDestinationSectionTitle(),
612
        self.model.getDestinationSectionTitle())
613
    self.assertEqual(paysheet.getSourceSectionTitle(),
614
        self.model.getSourceSectionTitle())
615
    self.assertEqual(paysheet.getSpecialiseValue(), self.model)
616

617
  def test_04_paySheetCalculation(self):
618
    '''
619
      test if the scripts called by the 'Calculation of the Pay Sheet
620 621 622 623 624 625 626
      Transaction' action create the paysheet lines
    '''
    self.addAllSlices(self.model)

    model_line_id1 = 'urssaf'
    model_line_id2 = 'salary'

627 628
    urssaf_slice_list = [ 'salary_range/'+self.france_settings_slice_a,
                          'salary_range/'+self.france_settings_slice_b,
629 630
                          'salary_range/'+self.france_settings_slice_c]

631
    urssaf_share_list = [ 'tax_category/'+self.tax_category_employee_share,
632 633 634 635 636 637 638 639
                          'tax_category/'+self.tax_category_employer_share]

    salary_slice_list = ['salary_range/'+self.france_settings_forfait,]
    salary_share_list = ['tax_category/'+self.tax_category_employee_share,]

    variation_category_list_urssaf = urssaf_share_list + urssaf_slice_list
    variation_category_list_salary = salary_share_list + salary_slice_list

640
    model_line1 = self.createModelLine(model=self.model,
641
        id=model_line_id1,
642
        variation_category_list=variation_category_list_urssaf,
643 644
        resource=self.urssaf,
        share_list=self.urssaf_share_list,
645
        slice_list=self.urssaf_slice_list,
646
        values=[[[None, 0.01], [None, 0.02], [None, 0.03]], [[None, 0.04],
647
               [None, 0.05], [None, 0.06]]],
648 649 650
        source_value=self.payroll_service_organisation,
        base_application_list=[ 'base_amount/base_salary'],
        base_contribution_list=['base_amount/deductible_tax',])
651

652
    model_line2 = self.createModelLine(model=self.model,
653
        id=model_line_id2,
654
        variation_category_list=variation_category_list_salary,
655 656 657 658 659 660
        resource=self.labour,
        share_list=self.salary_share_list,
        slice_list=self.salary_slice_list,
        values=[[[10000, None],],],
        base_application_list=[],
        base_contribution_list=['base_amount/base_salary', 'base_amount/gross_salary'])
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678

    pay_sheet_line_count = len(self.model.contentValues(portal_type=\
        self.paysheet_line_portal_type)) + 2 # because in this test, 2 lines
                                             # are added

    paysheet = self.createPaySheet(self.model)

    paysheet_line_count_before_calculation = \
        len(paysheet.contentValues(portal_type= \
        self.paysheet_line_portal_type))

    # calculate the pay sheet
    pay_sheet_line_list = self.calculatePaySheet(paysheet=paysheet)

    paysheet_line_count_after_calculation = \
        len(paysheet.contentValues(portal_type= \
        self.paysheet_line_portal_type))
    self.assertEqual(paysheet_line_count_before_calculation, 0)
679
    self.assertEqual(paysheet_line_count_after_calculation,
680 681 682 683 684 685 686
        pay_sheet_line_count)

    # check the amount in the cells of the created paysheet lines
    for pay_sheet_line in pay_sheet_line_list:
      service = pay_sheet_line.getResourceId()
      if service == self.urssaf_id:
        i = 1
687
        correct_value_slice_list = [0, self.plafond, self.plafond*4,
688 689 690
                                    self.plafond*8]

        self.assertEqualAmounts(pay_sheet_line, correct_value_slice_list,
691
            10000, i)
692 693 694
        self.assertEquals(
            [self.payroll_service_organisation.getRelativeUrl()],
            pay_sheet_line._getCategoryMembershipList('source_section'))
695

696 697 698 699 700
        # check the base_contribution has been copied from the pay sheet model
        # to the pay sheet line
        self.assertEquals(model_line1.getBaseContributionList(),
                          pay_sheet_line.getBaseContributionList())

701 702 703 704 705
      elif service == self.labour_id:
        cell = pay_sheet_line.getCell(\
            'tax_category/'+ self.tax_category_employee_share,
            'salary_range/'+ self.france_settings_forfait)
        value = cell.getTotalPrice()
706
        self.assertEqual(10000, value)
707 708
        self.assertEquals([],
            pay_sheet_line._getCategoryMembershipList('source_section'))
709

710 711 712 713 714
        # check the base_contribution has been copied from the pay sheet model
        # to the pay sheet line
        self.assertEquals(model_line2.getBaseContributionList(),
                          pay_sheet_line.getBaseContributionList())

715 716 717
      else:
        self.fail("Unknown service for line %s" % pay_sheet_line)

718
  def test_05_caculationWithANonNullMinimumValueSlice(self):
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
    '''
      if the is only slice B (without previous slice A), test that
      the amount paid for this tax is correct
    '''
    self.addAllSlices(self.model)

    model_line_id1 = 'urssaf'
    model_line_id2 = 'salary'
    base_salary = 10000

    urssaf_slice_list = ['salary_range/'+self.france_settings_slice_b,]
    variation_category_list_urssaf = self.urssaf_share_list + urssaf_slice_list
    variation_category_list_salary = self.salary_share_list + \
        self.salary_slice_list

734
    model_line1 = self.createModelLine(model=self.model,
735
        id=model_line_id1,
736 737
        variation_category_list=variation_category_list_urssaf,
        resource=self.urssaf, share_list=self.urssaf_share_list,
738
        slice_list=urssaf_slice_list,
739 740 741
        values=[[[None, 0.03]], [[None, 0.04]],],
        base_application_list=[ 'base_amount/base_salary'],
        base_contribution_list=['base_amount/deductible_tax',])
742

743
    model_line2 = self.createModelLine(model=self.model,
744
        id=model_line_id2,
745 746
        variation_category_list=variation_category_list_salary,
        resource=self.labour, share_list=self.salary_share_list,
747 748 749 750
        slice_list=self.salary_slice_list,
        values=[[[base_salary, None]],],
        base_application_list=[],
        base_contribution_list=['base_amount/base_salary', 'base_amount/gross_salary',])
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768

    pay_sheet_line_count = len(self.model.contentValues(portal_type=\
        self.paysheet_line_portal_type)) + 2 # because in this test, 2 lines
                                             # are added

    paysheet = self.createPaySheet(self.model)

    paysheet_line_count_before_calculation = \
        len(paysheet.contentValues(portal_type= \
        self.paysheet_line_portal_type))

    # calculate the pay sheet
    pay_sheet_line_list = self.calculatePaySheet(paysheet=paysheet)

    paysheet_line_count_after_calculation = \
        len(paysheet.contentValues(portal_type= \
        self.paysheet_line_portal_type))
    self.assertEqual(paysheet_line_count_before_calculation, 0)
769
    self.assertEqual(paysheet_line_count_after_calculation,
770
        pay_sheet_line_count)
771

772 773 774 775 776
    # check the amount in the cells of the created paysheet lines
    for pay_sheet_line in pay_sheet_line_list:
      service = pay_sheet_line.getResourceId()
      if service == self.urssaf_id:
        i = 2 # the begining max slice
777
        correct_value_slice_list = [0, self.plafond, self.plafond*4,
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
                                    self.plafond*8]

        self.assertEqualAmounts(pay_sheet_line, correct_value_slice_list,
            base_salary, i)

      elif service == self.labour_id:
        cell = pay_sheet_line.getCell('tax_category/'+\
            self.tax_category_employee_share,
            'salary_range/'+ self.france_settings_forfait)
        value = cell.getTotalPrice()
        self.assertEqual(base_salary, value)

      else:
        self.fail("Unknown service for line %s" % pay_sheet_line)

793
  def test_06_model_inheritance(self):
794 795 796 797 798 799 800 801 802
    '''
      check that a model can inherite some datas from another
      the ineritance rules are the following :
       - a DATA could be a model_line, annotation_line, ratio_line or
         payement_condition (XXX -> this last one haven't yet reference)
       - a model_line, annotation_line and a ratio_line have a REFERENCE
       - a model can have some DATA's
       - a model can inherite from another, that's mean :
         o At the calculation step, each DATA of the parent model will be
803
           checked : the DATA with a REFERENCE that's already in the child
804
           model will not entered in the calcul. The other will.
805 806
         o This will be repeated on each parent model and on each parent of
           the parent model,... until there is no parent model to inherite
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
           (or until a max loop number has been reached).
    '''
    # create 3 models
    model_employee = self.paysheet_model_module.newContent(id='model_employee',
        portal_type='Pay Sheet Model')

    model_company = self.paysheet_model_module.newContent(id='model_company',
        portal_type='Pay Sheet Model')

    model_country = self.paysheet_model_module.newContent(id='model_country',
        portal_type='Pay Sheet Model')

    # add some content in the models
    model_employee.newContent(id='over_time_duration',
                              title='over_time_duration',
822
                              portal_type='Annotation Line',
823 824 825 826
                              reference='over_time_duration',)

    model_company.newContent( id='worked_time_duration',
                              title='worked_time_duration',
827
                              portal_type='Annotation Line',
828 829 830 831
                              reference='worked_time_duration',)

    model_country.newContent( id='social_insurance',
                              title='social_insurance',
832
                              portal_type='Annotation Line',
833 834 835 836 837 838 839 840
                              reference='social_insurance',)

    # inherite from each other
    model_employee.setSpecialiseValue(model_company)
    model_company.setSpecialiseValue(model_country)

    # return a list of data that should contain data from all model
    portal_type_list = ['Annotation Line', ]
841 842
    model_reference_dict = model_employee.getInheritanceModelReferenceDict(\
        portal_type_list=portal_type_list)
843 844 845 846 847 848 849


    # check data's are corrected
    number_of_different_references = []
    for model in model_reference_dict.keys():
      number_of_different_references.extend(model_reference_dict[model])

850
    self.assertEqual(len(number_of_different_references), 3) # here, there is
851 852 853 854
                                                # 3 differents annotation line

    # check the model number
    self.assertEqual(len(model_reference_dict), 3)
855
    self.assertEqual(model_reference_dict[model_employee.getRelativeUrl()],
856
        ['over_time_duration',])
857
    self.assertEqual(model_reference_dict[model_company.getRelativeUrl()],
858
        ['worked_time_duration',])
859
    self.assertEqual(model_reference_dict[model_country.getRelativeUrl()],
860 861 862 863 864
        ['social_insurance',])

    # check with more values on each model
    # employee :
    model_employee.newContent(id='1',
865
                              portal_type='Annotation Line',
866 867 868
                              reference='1',)
    # company :
    model_company.newContent( id='1',
869
                              portal_type='Annotation Line',
870 871
                              reference='1',)
    model_company.newContent( id='2',
872
                              portal_type='Annotation Line',
873 874 875
                              reference='2',)
    # country :
    model_country.newContent( id='1',
876
                              portal_type='Annotation Line',
877 878
                              reference='1',)
    model_country.newContent( id='2',
879
                              portal_type='Annotation Line',
880 881
                              reference='2',)
    model_country.newContent( id='3',
882
                              portal_type='Annotation Line',
883 884
                              reference='3',)
    model_country.newContent( id='4',
885
                              portal_type='Annotation Line',
886 887 888 889 890 891
                              reference='4',)

    # return a list of data that should contain data from all model
    portal_type_list = ['Annotation Line', ]
    model_reference_dict = {}
    model_reference_dict = model_employee.getInheritanceModelReferenceDict(\
892
        portal_type_list=portal_type_list)
893 894 895 896 897 898 899 900

    # check that if a reference is already present in the model_employee,
    # and the model_company contain a data with the same one, the data used at
    # the calculation step is the model_employee data.
    number_of_different_references = []
    for model in model_reference_dict.keys():
      number_of_different_references.extend(model_reference_dict[model])

901
    self.assertEqual(len(number_of_different_references), 7) # here, there is
902 903 904
    # 4 differents annotation lines, and with the 3 ones have been had before
    # that's make 7 !

905 906


907 908
    # check the model number
    self.assertEqual(len(model_reference_dict), 3)
909
    self.assertEqual(set(model_reference_dict[model_employee.getRelativeUrl()]),
910
        set(['1', 'over_time_duration']))
911
    self.assertEqual(set(model_reference_dict[model_company.getRelativeUrl()]),
912
        set(['2', 'worked_time_duration']))
913
    self.assertEqual(set(model_reference_dict[model_country.getRelativeUrl()]),
914
        set(['3','4', 'social_insurance']))
915 916 917


    # same test with a multi model inheritance
918
    model_a = self.paysheet_model_module.newContent(id='model_a',
Fabien Morin's avatar
typo  
Fabien Morin committed
919
        title='model_a', portal_type='Pay Sheet Model')
920
    model_b = self.paysheet_model_module.newContent(id='model_b',
Fabien Morin's avatar
typo  
Fabien Morin committed
921
        title='model_b', portal_type='Pay Sheet Model')
922
    model_c = self.paysheet_model_module.newContent(id='model_c',
Fabien Morin's avatar
typo  
Fabien Morin committed
923
        title='model_c', portal_type='Pay Sheet Model')
924
    model_d = self.paysheet_model_module.newContent(id='model_d',
Fabien Morin's avatar
typo  
Fabien Morin committed
925
        title='model_d', portal_type='Pay Sheet Model')
926 927 928 929 930 931 932 933 934 935 936 937

    # check with more values on each model
    # a :
    model_a.newContent(id='5', portal_type='Annotation Line', reference='5')
    # b :
    model_b.newContent(id='5',portal_type='Annotation Line', reference='5')
    model_b.newContent(id='6',portal_type='Annotation Line', reference='6')
    # c :
    model_c.newContent(id='5', portal_type='Annotation Line', reference='5')
    model_c.newContent(id='6', portal_type='Annotation Line', reference='6')
    model_c.newContent(id='7', portal_type='Annotation Line', reference='7')
    model_c.newContent(id='8', portal_type='Annotation Line', reference='8')
938 939 940 941
    # d :
    model_d.newContent(id='5',portal_type='Annotation Line', reference='5')
    model_d.newContent(id='6',portal_type='Annotation Line', reference='6')

942 943 944

    # inherite from each other
    model_a.setSpecialiseValue(model_c)
945
    model_country.setSpecialiseValue(model_d)
946 947 948
    model_company.setSpecialiseValueList([model_country, model_a, model_b])
    model_employee.setSpecialiseValue(model_company)

Fabien Morin's avatar
typo  
Fabien Morin committed
949 950
    # get a list of data that should contain data from all model inheritance
    # dependances tree
951 952 953
    portal_type_list = ['Annotation Line', ]
    model_reference_dict = {}
    model_reference_dict = model_employee.getInheritanceModelReferenceDict(\
954
        portal_type_list=portal_type_list)
955 956 957 958 959 960 961


    # check data's are corrected
    number_of_different_references = []
    for model in model_reference_dict.keys():
      number_of_different_references.extend(model_reference_dict[model])

962
    self.assertEqual(len(number_of_different_references), 11) # here, there is
963 964
    # 8 differents annotation lines, and with the 3 ones have been had before
    # that's make 11 !
965

966
    # check the model number
967 968 969 970
    self.assertEqual(len(model_reference_dict), 6) # there is 7 model, but the
    # model_d is not take into account because it have no annotation line wich
    # are not already added by other models

971 972 973 974 975 976 977 978 979 980 981 982 983 984

    # the inheritance tree look like this :

#                                model_employee
#                           ('overtime_duration', '1')
#                                      |
#                                      |
#                                      |
#                                model_company
#                      ('worked_time_duration', '1', '2')
#                         /            |            \
#                        /             |             \
#                       /              |              \
#            model_country           model_a          model_b
Fabien Morin's avatar
typo  
Fabien Morin committed
985 986
#         ('social_insurance',       ('5',)          ('5', '6')
#          '1', '2', '3', '4')         |
987 988 989
#                  |                   |
#                  |                   |
#               model_d             model_c
Fabien Morin's avatar
typo  
Fabien Morin committed
990
#            ('5', '6')       ('5', '6', '7', '8')
991 992 993 994




Fabien Morin's avatar
typo  
Fabien Morin committed
995
    self.assertEqual(set(model_reference_dict[model_employee.getRelativeUrl()]),
996
        set(['1', 'over_time_duration']))
997
    self.assertEqual(set(model_reference_dict[model_company.getRelativeUrl()]),
998
        set(['2', 'worked_time_duration']))
999
    self.assertEqual(set(model_reference_dict[model_country.getRelativeUrl()]),
1000
        set(['3','4', 'social_insurance']))
1001 1002
    self.assertEqual(model_reference_dict[model_a.getRelativeUrl()], ['5',])
    self.assertEqual(model_reference_dict[model_b.getRelativeUrl()], ['6',])
1003
    self.assertEqual(set(model_reference_dict[model_c.getRelativeUrl()]),
1004
        set(['7', '8']))
1005

1006

Fabien Morin's avatar
typo  
Fabien Morin committed
1007
    # get all sub objects from a paysheet witch inherite of model_employee
1008

1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
    # create a paysheet
    id = 'inheritance_paysheet'
    paysheet_module = self.portal.getDefaultModule(\
                            portal_type=self.paysheet_transaction_portal_type)
    if hasattr(paysheet_module, id):
      paysheet_module.manage_delObjects([id])
    paysheet = paysheet_module.newContent(\
        portal_type               = self.paysheet_transaction_portal_type,
        id                        = id,
        title                     = id,
        specialise_value          = model_employee)

    # check heneritance works
    self.assertEqual(paysheet.getSpecialiseValue(), model_employee)

1024
    # get a list of all this subObjects:
Fabien Morin's avatar
typo  
Fabien Morin committed
1025
    sub_object_list = paysheet.getInheritedObjectValueList(portal_type_list)
1026
    self.assertEqual(len(sub_object_list), 11)
1027

1028
  def test_07_model_getCell(self):
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
    '''
      Model objects have a overload method called getCell. This method first
      call the XMLMatrix.getCell and if the cell is not found, call
      getCell method in all it's inherited model until the cell is found or
      the cell have been searched on all inherited models.

      TODO : Currently, the method use a Depth-First Search algorithm, it will
      be better to use Breadth-First Search one.
      more about this on :
        - http://en.wikipedia.org/wiki/Breadth-first_search
        - http://en.wikipedia.org/wiki/Depth-first_search
    '''
    # create 3 models
    model_employee = self.paysheet_model_module.newContent(id='model_employee',
        portal_type='Pay Sheet Model')
    model_employee.edit(variation_settings_category_list=
        self.variation_settings_category_list)

    model_company = self.paysheet_model_module.newContent(id='model_company',
        portal_type='Pay Sheet Model')
    model_company.edit(variation_settings_category_list=
        self.variation_settings_category_list)

1052 1053 1054 1055 1056 1057
    model_company_alt = self.paysheet_model_module.newContent(
        id='model_company_alt',
        portal_type='Pay Sheet Model')
    model_company_alt.edit(variation_settings_category_list=
        self.variation_settings_category_list)

1058 1059 1060 1061 1062 1063 1064
    model_country = self.paysheet_model_module.newContent(id='model_country',
        portal_type='Pay Sheet Model')
    model_country.edit(variation_settings_category_list=
        self.variation_settings_category_list)

    # add some cells in the models
    self.addSlice(model_employee, 'salary_range/%s' % \
1065
        self.france_settings_slice_a, 0, 1)
1066 1067

    self.addSlice(model_company, 'salary_range/%s' % \
1068
        self.france_settings_slice_b, 2, 3)
1069 1070

    self.addSlice(model_company_alt, 'salary_range/%s' % \
1071
        self.france_settings_forfait, 20, 30)
1072 1073

    self.addSlice(model_country, 'salary_range/%s' % \
1074
        self.france_settings_slice_c, 4, 5)
1075

1076
    # inherite from each other
1077
    model_employee.setSpecialiseValueList((model_company, model_company_alt))
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
    model_company.setSpecialiseValue(model_country)


    # check getCell results

    # check model_employee could access all cells
    cell_a = model_employee.getCell('salary_range/%s' % \
                        self.france_settings_slice_a)
    self.assertNotEqual(cell_a, None)
    self.assertEqual(cell_a.getQuantityRangeMin(), 0)
1088
    self.assertEqual(cell_a.getQuantityRangeMax(), 1)
1089 1090 1091 1092

    cell_b = model_employee.getCell('salary_range/%s' % \
                        self.france_settings_slice_b)
    self.assertNotEqual(cell_b, None)
1093 1094 1095 1096 1097 1098 1099 1100
    self.assertEqual(cell_b.getQuantityRangeMin(), 2)
    self.assertEqual(cell_b.getQuantityRangeMax(), 3)

    cell_forfait = model_employee.getCell('salary_range/%s' % \
                        self.france_settings_forfait)
    self.assertNotEqual(cell_forfait, None)
    self.assertEqual(cell_forfait.getQuantityRangeMin(), 20)
    self.assertEqual(cell_forfait.getQuantityRangeMax(), 30)
1101 1102 1103 1104

    cell_c = model_employee.getCell('salary_range/%s' % \
                        self.france_settings_slice_c)
    self.assertNotEqual(cell_c, None)
1105 1106
    self.assertEqual(cell_c.getQuantityRangeMin(), 4)
    self.assertEqual(cell_c.getQuantityRangeMax(), 5)
1107

1108 1109
    # check model_company and model_company_alt could access just it's own cell
    # and this of the country model
1110 1111 1112 1113 1114 1115 1116
    cell_a = model_company.getCell('salary_range/%s' % \
                        self.france_settings_slice_a)
    self.assertEqual(cell_a, None)

    cell_b = model_company.getCell('salary_range/%s' % \
                        self.france_settings_slice_b)
    self.assertNotEqual(cell_b, None)
1117 1118 1119
    self.assertEqual(cell_b.getQuantityRangeMin(), 2)
    self.assertEqual(cell_b.getQuantityRangeMax(), 3)

1120
    cell_forfait = model_company_alt.getCell('salary_range/%s' % \
1121 1122 1123 1124
                        self.france_settings_forfait)
    self.assertNotEqual(cell_forfait, None)
    self.assertEqual(cell_forfait.getQuantityRangeMin(), 20)
    self.assertEqual(cell_forfait.getQuantityRangeMax(), 30)
1125 1126 1127 1128

    cell_c = model_company.getCell('salary_range/%s' % \
                        self.france_settings_slice_c)
    self.assertNotEqual(cell_c, None)
1129 1130
    self.assertEqual(cell_c.getQuantityRangeMin(), 4)
    self.assertEqual(cell_c.getQuantityRangeMax(), 5)
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141

    # check model_country could access just it's own cell
    # model
    cell_a = model_country.getCell('salary_range/%s' % \
                        self.france_settings_slice_a)
    self.assertEqual(cell_a, None)

    cell_b = model_country.getCell('salary_range/%s' % \
                        self.france_settings_slice_b)
    self.assertEqual(cell_b, None)

1142 1143 1144 1145
    cell_forfait = model_country.getCell('salary_range/%s' % \
                        self.france_settings_forfait)
    self.assertEqual(cell_forfait, None)

1146 1147 1148
    cell_c = model_country.getCell('salary_range/%s' % \
                        self.france_settings_slice_c)
    self.assertNotEqual(cell_c, None)
1149 1150
    self.assertEqual(cell_c.getQuantityRangeMin(), 4)
    self.assertEqual(cell_c.getQuantityRangeMax(), 5)
1151

1152

1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
  def test_model_slice_cell_range(self):
    base_id = 'cell'
    model_1 = self.paysheet_model_module.newContent(
                            portal_type='Pay Sheet Model',
                            variation_settings_category_list=
                                  ('salary_range/france',))

    model_2 = self.paysheet_model_module.newContent(
                            portal_type='Pay Sheet Model',
                            specialise_value=model_1,)

    cell = model_1.newCell('salary_range/france/tranche_a',
                    portal_type='Pay Sheet Model Slice',
                    base_id='cell')
    cell.setQuantityRangeMin(1)
    cell.setQuantityRangeMax(2)
1169

1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
    # model 2 gets cell values from model 1 (see test_07_model_getCell)
    self.assertEquals(1,
        model_2.getCell('salary_range/france/tranche_a').getQuantityRangeMin())
    self.assertEquals(2,
        model_2.getCell('salary_range/france/tranche_a').getQuantityRangeMax())

    # model 2 can override values
    model_2.edit(variation_settings_category_list=('salary_range/france',))
    cell = model_2.newCell('salary_range/france/tranche_a',
                    portal_type='Pay Sheet Model Slice',
                    base_id='cell')
    cell.setQuantityRangeMin(3)
    cell.setQuantityRangeMax(4)
    self.assertEquals(3,
        model_2.getCell('salary_range/france/tranche_a').getQuantityRangeMin())
    self.assertEquals(4,
        model_2.getCell('salary_range/france/tranche_a').getQuantityRangeMax())

    # when unsetting variation settings category on this model will acquire
    # again values from specialised model
    model_2.edit(variation_settings_category_list=())
    self.assertEquals(1,
        model_2.getCell('salary_range/france/tranche_a').getQuantityRangeMin())
    self.assertEquals(2,
        model_2.getCell('salary_range/france/tranche_a').getQuantityRangeMax())


1197 1198 1199 1200 1201 1202 1203
  def test_PaySheetTransaction_getMovementList(self):
    # Tests PaySheetTransaction_getMovementList script
    pay_sheet = self.createPaySheet(self.model)
    # when pay sheet has no line, the script returns an empty list
    self.assertEquals(pay_sheet.PaySheetTransaction_getMovementList(), [])
    # we add a line, then it is returned in the list
    line = pay_sheet.newContent(portal_type='Pay Sheet Line')
Jérome Perrin's avatar
Jérome Perrin committed
1204
    self.assertEquals(1, len(pay_sheet.PaySheetTransaction_getMovementList()))
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214

    # if the line has cells with different tax categories, new properties are
    # added to this line.
    line.setResourceValue(self.urssaf)
    line.setVariationCategoryList(['tax_category/employee_share',
                                   'tax_category/employer_share'])
    line.updateCellRange(base_id='movement')
    cell0 = line.newCell('tax_category/employee_share',
                         portal_type='Pay Sheet Cell', base_id='movement')
    cell0.setMappedValuePropertyList(['quantity', 'price'])
1215
    cell0.setVariationCategoryList(('tax_category/employee_share',))
1216 1217 1218 1219 1220
    cell0.setPrice(2)
    cell0.setQuantity(3)
    cell1 = line.newCell('tax_category/employer_share',
                         portal_type='Pay Sheet Cell', base_id='movement')
    cell1.setMappedValuePropertyList(['quantity', 'price'])
1221
    cell1.setVariationCategoryList(('tax_category/employer_share',))
1222 1223
    cell1.setPrice(4)
    cell1.setQuantity(5)
1224

1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
    movement_list = pay_sheet.PaySheetTransaction_getMovementList()
    self.assertEquals(1, len(movement_list))
    movement = movement_list[0]
    self.assertEquals(2, movement.employee_share_price)
    self.assertEquals(3, movement.employee_share_quantity)
    self.assertEquals(2*3, movement.employee_share_total_price)
    self.assertEquals(4, movement.employer_share_price)
    self.assertEquals(5, movement.employer_share_quantity)
    self.assertEquals(4*5, movement.employer_share_total_price)

1235 1236 1237 1238 1239 1240 1241 1242
  def test_createEditablePaySheetLine(self):
    # test the creation of lines with editable lines in the model
    line = self.model.newContent(
          id='line',
          portal_type='Pay Sheet Model Line',
          resource_value=self.labour,
          variation_category_list=['tax_category/employee_share'],
          editable=1)
1243 1244 1245
    # Note that it is required that the editable line contains at least one
    # cell, to know which tax_category is used (employee share or employer
    # share).
1246 1247 1248 1249 1250
    line.updateCellRange(base_id='movement')
    cell = line.newCell('tax_category/employee_share',
                        portal_type='Pay Sheet Cell',
                        base_id='movement')
    cell.setMappedValuePropertyList(('quantity', 'price'))
1251
    cell.setVariationCategoryList(('tax_category/employee_share',))
1252 1253 1254
    cell.setPrice(1)

    pay_sheet = self.createPaySheet(self.model)
1255

1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
    # PaySheetTransaction_getEditableObjectLineList is the script used as list
    # method to display editable lines in the dialog listbox
    editable_line_list = pay_sheet\
          .PaySheetTransaction_getEditableObjectLineList()
    self.assertEquals(1, len(editable_line_list))
    editable_line = editable_line_list[0]
    self.assertEquals(1, editable_line.employee_share_price)
    self.assertEquals(0, editable_line.employee_share_quantity)
    self.assertEquals('paysheet_model_module/model_one/line',
                      editable_line.model_line)
    self.assertEquals(None, editable_line.salary_range_relative_url)
1267

1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
    # PaySheetTransaction_createAllPaySheetLineList is the script used to create line and cells in the
    # paysheet using the listbox input
    pay_sheet.PaySheetTransaction_createAllPaySheetLineList(
      listbox=[dict(listbox_key='0',
                    employee_share_price=1,
                    employee_share_quantity=2,
                    model_line='paysheet_model_module/model_one/line',
                    salary_range_relative_url='',)])
    pay_sheet_line_list = pay_sheet.contentValues(portal_type='Pay Sheet Line')
    self.assertEquals(1, len(pay_sheet_line_list))
    pay_sheet_line = pay_sheet_line_list[0]
    self.assertEquals(self.labour, pay_sheet_line.getResourceValue())
    cell = pay_sheet_line.getCell('tax_category/employee_share',
                                  base_id='movement')
    self.assertNotEquals(None, cell)
    self.assertEquals(1, cell.getPrice())
    self.assertEquals(2, cell.getQuantity())
1285

1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
    # if the script is called again, previous content is erased.
    pay_sheet.PaySheetTransaction_createAllPaySheetLineList(
      listbox=[dict(listbox_key='0',
                    employee_share_price=0.5,
                    employee_share_quantity=10,
                    model_line='paysheet_model_module/model_one/line',
                    salary_range_relative_url='',)])
    pay_sheet_line_list = pay_sheet.contentValues(portal_type='Pay Sheet Line')
    self.assertEquals(1, len(pay_sheet_line_list))
    pay_sheet_line = pay_sheet_line_list[0]
    self.assertEquals(self.labour, pay_sheet_line.getResourceValue())
    cell = pay_sheet_line.getCell('tax_category/employee_share',
                                  base_id='movement')
    self.assertNotEquals(None, cell)
    self.assertEquals(0.5, cell.getPrice())
    self.assertEquals(10, cell.getQuantity())
1302

1303 1304 1305 1306 1307 1308 1309 1310 1311
    # If the user enters a null quantity, the line will not be created
    pay_sheet.PaySheetTransaction_createAllPaySheetLineList(
      listbox=[dict(listbox_key='0',
                    employee_share_price=1,
                    employee_share_quantity=0,
                    model_line='paysheet_model_module/model_one/line',
                    salary_range_relative_url='',)])
    pay_sheet_line_list = pay_sheet.contentValues(portal_type='Pay Sheet Line')
    self.assertEquals(0, len(pay_sheet_line_list))
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321

  def test_createEditablePaySheetLineAppliedToBase(self):
    # test the creation of lines with editable lines in the model, when those
    # editable lines applies to a base
    # line1 will contribute to 'base_salary'
    line1 = self.model.newContent(
          id='line1',
          portal_type='Pay Sheet Model Line',
          resource_value=self.labour,
          variation_category_list=['tax_category/employee_share'],
1322 1323 1324
          base_application_list= [],
          base_contribution_list=['base_amount/base_salary',
                                  'base_amount/gross_salary'],
1325 1326 1327 1328 1329 1330 1331
          float_index=1,
          int_index=1)
    line1.updateCellRange(base_id='movement')
    cell = line1.newCell('tax_category/employee_share',
                        portal_type='Pay Sheet Cell',
                        base_id='movement')
    cell.setMappedValuePropertyList(('quantity', 'price'))
1332
    cell.setVariationCategoryList(('tax_category/employee_share',))
1333 1334 1335 1336 1337 1338 1339 1340
    cell.setPrice(1)
    cell.setQuantity(100)
    # line2 will apply to 'base_salary', but we'll set 0 quantity in the dialog
    line2 = self.model.newContent(
          id='line2',
          portal_type='Pay Sheet Model Line',
          resource_value=self.labour,
          variation_category_list=['tax_category/employee_share'],
1341 1342 1343 1344
          base_application_list= [],
          base_contribution_list=['base_amount/base_salary',
                                  'base_amount/gross_salary'],
          #base_amount_list=['base_salary'],
1345 1346 1347 1348 1349 1350 1351 1352
          editable=1,
          float_index=2,
          int_index=2)
    line2.updateCellRange(base_id='movement')
    cell = line2.newCell('tax_category/employee_share',
                        portal_type='Pay Sheet Cell',
                        base_id='movement')
    cell.setMappedValuePropertyList(('quantity', 'price'))
1353
    cell.setVariationCategoryList(('tax_category/employee_share',))
1354 1355 1356
    cell.setPrice(1)

    pay_sheet = self.createPaySheet(self.model)
1357

1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
    # PaySheetTransaction_getEditableObjectLineList is the script used as list
    # method to display editable lines in the dialog listbox
    editable_line_list = pay_sheet\
          .PaySheetTransaction_getEditableObjectLineList()
    self.assertEquals(1, len(editable_line_list))
    editable_line = editable_line_list[0]
    self.assertEquals(1, editable_line.employee_share_price)
    self.assertEquals(0, editable_line.employee_share_quantity)
    self.assertEquals('paysheet_model_module/model_one/line2',
                      editable_line.model_line)
    self.assertEquals(None, editable_line.salary_range_relative_url)
1369

1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
    # PaySheetTransaction_createAllPaySheetLineList is the script used to create line and cells in the
    # paysheet using the listbox input
    pay_sheet.PaySheetTransaction_createAllPaySheetLineList(
      listbox=[dict(listbox_key='0',
                    employee_share_price=.5,
                    employee_share_quantity=4,
                    model_line='paysheet_model_module/model_one/line2',
                    salary_range_relative_url='',)])
    pay_sheet_line_list = pay_sheet.contentValues(portal_type='Pay Sheet Line')
    self.assertEquals(2, len(pay_sheet_line_list))
    pay_sheet_line1 = [l for l in pay_sheet_line_list
                         if l.getIntIndex() == 1][0]
    self.assertEquals(self.labour, pay_sheet_line1.getResourceValue())
    cell = pay_sheet_line1.getCell('tax_category/employee_share',
                                  base_id='movement')
    self.assertNotEquals(None, cell)
    self.assertEquals(1, cell.getPrice())
    self.assertEquals(100, cell.getQuantity())
1388

1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
    pay_sheet_line2 = [l for l in pay_sheet_line_list
                         if l.getIntIndex() == 2][0]
    self.assertEquals(self.labour, pay_sheet_line2.getResourceValue())
    cell = pay_sheet_line2.getCell('tax_category/employee_share',
                                  base_id='movement')
    self.assertNotEquals(None, cell)
    self.assertEquals(.5, cell.getPrice())
    self.assertEquals(4, cell.getQuantity())

    # if the script is called again, previous content is erased.
    pay_sheet.PaySheetTransaction_createAllPaySheetLineList(
      listbox=[dict(listbox_key='0',
                    employee_share_price=0.6,
                    employee_share_quantity=10,
                    model_line='paysheet_model_module/model_one/line2',
                    salary_range_relative_url='',)])
    pay_sheet_line_list = pay_sheet.contentValues(portal_type='Pay Sheet Line')
    self.assertEquals(2, len(pay_sheet_line_list))
    pay_sheet_line1 = [l for l in pay_sheet_line_list
                         if l.getIntIndex() == 1][0]
    self.assertEquals(self.labour, pay_sheet_line1.getResourceValue())
    cell = pay_sheet_line1.getCell('tax_category/employee_share',
                                  base_id='movement')
    self.assertNotEquals(None, cell)
    self.assertEquals(1, cell.getPrice())
    self.assertEquals(100, cell.getQuantity())
1415

1416 1417 1418 1419 1420 1421 1422 1423
    pay_sheet_line2 = [l for l in pay_sheet_line_list
                         if l.getIntIndex() == 2][0]
    self.assertEquals(self.labour, pay_sheet_line2.getResourceValue())
    cell = pay_sheet_line2.getCell('tax_category/employee_share',
                                  base_id='movement')
    self.assertNotEquals(None, cell)
    self.assertEquals(0.6, cell.getPrice())
    self.assertEquals(10, cell.getQuantity())
1424

1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
    # If the user enters a null quantity, the line will not be created
    pay_sheet.PaySheetTransaction_createAllPaySheetLineList(
      listbox=[dict(listbox_key='0',
                    employee_share_price=1,
                    employee_share_quantity=0,
                    model_line='paysheet_model_module/model_one/line2',
                    salary_range_relative_url='',)])
    pay_sheet_line_list = pay_sheet.contentValues(portal_type='Pay Sheet Line')
    self.assertEquals(1, len(pay_sheet_line_list))
    pay_sheet_line1 = [l for l in pay_sheet_line_list
                         if l.getIntIndex() == 1][0]
    self.assertEquals(self.labour, pay_sheet_line1.getResourceValue())
    cell = pay_sheet_line1.getCell('tax_category/employee_share',
                                  base_id='movement')
    self.assertNotEquals(None, cell)
    self.assertEquals(1, cell.getPrice())
    self.assertEquals(100, cell.getQuantity())

1443 1444 1445 1446 1447 1448 1449
  def test_createPaySheetLineNonePrice(self):
    # test the creation of lines when the price is not set, but only the
    # quantity. This means that no ratio is applied on this line.
    line = self.model.newContent(
          id='line',
          portal_type='Pay Sheet Model Line',
          resource_value=self.labour,
1450 1451
          variation_category_list=['tax_category/employee_share'],
          base_contribution_list=['base_amount/base_salary', 'base_amount/gross_salary'])
1452 1453 1454 1455 1456 1457 1458 1459 1460
    line.updateCellRange(base_id='movement')
    cell = line.newCell('tax_category/employee_share',
                        portal_type='Pay Sheet Cell',
                        base_id='movement')
    cell.setMappedValuePropertyList(('quantity', 'price'))
    cell.setVariationCategoryList(('tax_category/employee_share',))
    cell.setQuantity(5)

    pay_sheet = self.createPaySheet(self.model)
1461

1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
    pay_sheet.PaySheetTransaction_createAllPaySheetLineList()
    pay_sheet_line_list = pay_sheet.contentValues(portal_type='Pay Sheet Line')
    self.assertEquals(1, len(pay_sheet_line_list))
    pay_sheet_line = pay_sheet_line_list[0]
    self.assertEquals(self.labour, pay_sheet_line.getResourceValue())
    cell = pay_sheet_line.getCell('tax_category/employee_share',
                                  base_id='movement')
    self.assertNotEquals(None, cell)
    self.assertEquals(1, cell.getPrice())
    self.assertEquals(5, cell.getQuantity())
1472

1473 1474 1475 1476 1477 1478 1479
  def test_createPaySheetLineZeroPrice(self):
    # test the creation of lines when the price is set to zero: the line should
    # not be created.
    line = self.model.newContent(
          id='line',
          portal_type='Pay Sheet Model Line',
          resource_value=self.labour,
1480 1481
          variation_category_list=['tax_category/employee_share'],
          base_contribution_list=['base_amount/base_salary', 'base_amount/gross_salary'])
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
    line.updateCellRange(base_id='movement')
    cell = line.newCell('tax_category/employee_share',
                        portal_type='Pay Sheet Cell',
                        base_id='movement')
    cell.setMappedValuePropertyList(('quantity', 'price'))
    cell.setVariationCategoryList(('tax_category/employee_share',))
    cell.setQuantity(5)
    cell.setPrice(0)

    pay_sheet = self.createPaySheet(self.model)
1492

1493 1494 1495
    pay_sheet.PaySheetTransaction_createAllPaySheetLineList()
    pay_sheet_line_list = pay_sheet.contentValues(portal_type='Pay Sheet Line')
    self.assertEquals(0, len(pay_sheet_line_list))
1496

1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
  def test_paysheet_consistency(self):
    # minimal test for checkConsistency on a Pay Sheet Transaction and its
    # subdocuments (may have to be updated when we'll add more constraints).
    paysheet = self.createPaySheet(self.model)
    paysheet.setResourceValue(self.portal.currency_module.EUR)
    paysheet.newContent(portal_type='Pay Sheet Line')
    paysheet.newContent(portal_type='Pay Sheet Transaction Line')
    paysheet.newContent(portal_type='Annotation Line')
    paysheet.newContent(portal_type='Pay Sheet Model Ratio Line')
    paysheet.newContent(portal_type='Payment Condition')
    self.assertEquals([], paysheet.checkConsistency())
1508

1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526
  def test_paysheet_model_consistency(self):
    # minimal test for checkConsistency on a Pay Sheet Model and its
    # subdocuments (may have to be updated when we'll add more constraints).
    model = self.model
    model.newContent(portal_type='Pay Sheet Model Line') # XXX this one needs a
                                                         # resource
    model.newContent(portal_type='Annotation Line')
    model.newContent(portal_type='Pay Sheet Model Ratio Line')
    model.newContent(portal_type='Payment Condition')
    self.assertEquals([], model.checkConsistency())

  def test_payroll_service_consistency(self):
    # minimal test for checkConsistency on a Payroll Service
    service = self.portal.payroll_service_module.newContent(
                           portal_type='Payroll Service')
    service.setVariationBaseCategoryList(['tax_category'])
    service.setVariationCategoryList(['tax_category/employee_share'])
    self.assertEquals([], service.checkConsistency())
1527

Jérome Perrin's avatar
Jérome Perrin committed
1528 1529 1530 1531 1532
  def test_apply_model(self):
    eur = self.portal.currency_module.EUR
    employee = self.portal.person_module.newContent(
                      portal_type='Person',
                      title='Employee')
1533 1534
    employee_bank_account = employee.newContent(
                      portal_type='Bank Account')
Jérome Perrin's avatar
Jérome Perrin committed
1535 1536 1537
    employer = self.portal.organisation_module.newContent(
                      portal_type='Organisation',
                      title='Employer')
1538 1539
    employer_bank_account = employee.newContent(
                      portal_type='Bank Account')
Jérome Perrin's avatar
Jérome Perrin committed
1540 1541 1542
    model = self.portal.paysheet_model_module.newContent(
                      portal_type='Pay Sheet Model',
                      source_section_value=employee,
1543
                      source_payment_value=employee_bank_account,
Jérome Perrin's avatar
Jérome Perrin committed
1544
                      destination_section_value=employer,
1545
                      destination_payment_value=employer_bank_account,
Jérome Perrin's avatar
Jérome Perrin committed
1546 1547 1548 1549 1550 1551
                      price_currency_value=eur,
                      payment_condition_payment_date=DateTime(2008, 1, 1),
                      work_time_annotation_line_quantity=10)
    paysheet = self.portal.accounting_module.newContent(
                      portal_type='Pay Sheet Transaction',
                      specialise_value=model)
1552

1553
    paysheet.PaySheetTransaction_applyModel()
Jérome Perrin's avatar
Jérome Perrin committed
1554 1555
    self.assertEquals(employee, paysheet.getSourceSectionValue())
    self.assertEquals(employer, paysheet.getDestinationSectionValue())
1556 1557
    self.assertEquals(employee_bank_account, paysheet.getSourcePaymentValue())
    self.assertEquals(employer_bank_account, paysheet.getDestinationPaymentValue())
1558 1559 1560 1561
    self.assertEquals(employee_bank_account,
                      paysheet.getPaymentConditionSourcePaymentValue())
    self.assertEquals(employer_bank_account,
                      paysheet.getPaymentConditionDestinationPaymentValue())
Jérome Perrin's avatar
Jérome Perrin committed
1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
    self.assertEquals(eur, paysheet.getResourceValue())
    self.assertEquals(eur, paysheet.getPriceCurrencyValue())
    self.assertEquals(DateTime(2008, 1, 1),
                      paysheet.getPaymentConditionPaymentDate())
    self.assertEquals(10, paysheet.getWorkTimeAnnotationLineQuantity())

    # if not found on the first model, values are searched recursivly in the
    # model hierarchy
    other_model = self.portal.paysheet_model_module.newContent(
                      portal_type='Pay Sheet Model',
                      specialise_value=model)
    paysheet = self.portal.accounting_module.newContent(
                      portal_type='Pay Sheet Transaction',
                      specialise_value=other_model)
1576

1577
    paysheet.PaySheetTransaction_applyModel()
Jérome Perrin's avatar
Jérome Perrin committed
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
    self.assertEquals(employee, paysheet.getSourceSectionValue())
    self.assertEquals(employer, paysheet.getDestinationSectionValue())
    self.assertEquals(eur, paysheet.getResourceValue())
    self.assertEquals(eur, paysheet.getPriceCurrencyValue())
    self.assertEquals(DateTime(2008, 1, 1),
                      paysheet.getPaymentConditionPaymentDate())
    self.assertEquals(10, paysheet.getWorkTimeAnnotationLineQuantity())

    # applying twice does not copy subdocument twice
    self.assertEquals(2, len(paysheet.contentValues()))
1588
    paysheet.PaySheetTransaction_applyModel()
Jérome Perrin's avatar
Jérome Perrin committed
1589 1590
    self.assertEquals(2, len(paysheet.contentValues()))

1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614
  def test_apply_model_empty_line(self):
    # apply a model with some empty lines
    eur = self.portal.currency_module.EUR
    employee = self.portal.person_module.newContent(
                      portal_type='Person',
                      title='Employee')
    employer = self.portal.organisation_module.newContent(
                      portal_type='Organisation',
                      title='Employer')
    model = self.portal.paysheet_model_module.newContent(
                      portal_type='Pay Sheet Model',
                      source_section_value=employee,
                      destination_section_value=employer,
                      price_currency_value=eur,
                      payment_condition_payment_date=DateTime(2008, 1, 1),
                      work_time_annotation_line_quantity=10)
    employee_model = self.portal.paysheet_model_module.newContent(
                      portal_type='Pay Sheet Model',
                      specialise_value=model,
                      work_time_annotation_line_quantity=20)
    employee_model.setWorkTimeAnnotationLineQuantity(None)
    paysheet = self.portal.accounting_module.newContent(
                      portal_type='Pay Sheet Transaction',
                      specialise_value=employee_model)
1615

1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
    paysheet.PaySheetTransaction_applyModel()
    self.assertEquals(employee, paysheet.getSourceSectionValue())
    self.assertEquals(employer, paysheet.getDestinationSectionValue())
    self.assertEquals(eur, paysheet.getResourceValue())
    self.assertEquals(eur, paysheet.getPriceCurrencyValue())
    self.assertEquals(DateTime(2008, 1, 1),
                      paysheet.getPaymentConditionPaymentDate())
    # WorkTimeAnnotationLine is not taken on employee_model, because the line
    # is "empty", it is taken on model.
    self.assertEquals(10, paysheet.getWorkTimeAnnotationLineQuantity())

    # applying twice does not copy subdocument twice
    self.assertEquals(2, len(paysheet.contentValues()))
    paysheet.PaySheetTransaction_applyModel()
    self.assertEquals(2, len(paysheet.contentValues()))

1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653
  def test_calculate_paysheet_source_annotation_line_reference(self):
    # the payroll service provider can be specified using the reference of an
    # annotation line.
    eur = self.portal.currency_module.EUR
    employee = self.portal.person_module.newContent(
                      portal_type='Person',
                      title='Employee')
    employer = self.portal.organisation_module.newContent(
                      portal_type='Organisation',
                      title='Employer')
    provider = self.portal.organisation_module.newContent(
                      portal_type='Organisation',
                      title='Payroll Service Provider')
    model = self.portal.paysheet_model_module.newContent(
                      portal_type='Pay Sheet Model',
                      source_section_value=employee,
                      destination_section_value=employer,
                      price_currency_value=eur,)
    model_line = model.newContent(
                    portal_type='Pay Sheet Model Line',
                    resource_value=self.urssaf,
                    variation_category_list=['tax_category/employee_share'],
1654 1655 1656
                    source_annotation_line_reference='tax1',
                    base_application_list =  ['base_amount/base_salary',],
                    base_contribution_list = ['base_amount/deductible_tax',],)
1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
    model_line.updateCellRange(base_id='movement')
    cell = model_line.newCell('tax_category/employee_share',
                              portal_type='Pay Sheet Cell',
                              base_id='movement')
    cell.setMappedValuePropertyList(('quantity', 'price'))
    cell.setPrice(10)
    cell.setQuantity(10)

    annotation = model.newContent(
                        portal_type='Annotation Line',
                        reference='tax1',
                        source_value=provider)

    paysheet = self.portal.accounting_module.newContent(
                      portal_type='Pay Sheet Transaction',
                      specialise_value=model)
1673

1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686
    paysheet.PaySheetTransaction_applyModel()
    paysheet.createPaySheetLineList()
    paysheet_line_list = paysheet.contentValues(portal_type='Pay Sheet Line')
    self.assertEquals(1, len(paysheet_line_list))
    paysheet_line = paysheet_line_list[0]

    self.assertEquals([provider.getRelativeUrl()],
                      paysheet_line._getCategoryMembershipList('source_section'))
    self.assertEquals(self.urssaf, paysheet_line.getResourceValue())
    self.assertEquals(100, paysheet_line.getTotalPrice())
    self.assertEquals(['tax_category/employee_share'],
                      paysheet_line.getVariationCategoryList())

1687

1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735
  def test_PayrollTaxesReport(self):
    eur = self.portal.currency_module.EUR
    payroll_service = self.portal.payroll_service_module.newContent(
                      portal_type='Payroll Service',
                      title='PS1',
                      variation_base_category_list=('tax_category',),
                      variation_category_list=('tax_category/employee_share',
                                               'tax_category/employer_share'))
    employer = self.portal.organisation_module.newContent(
                      portal_type='Organisation',
                      title='Employer',
                      price_currency_value=eur,
                      group_value=self.portal.portal_categories.group.demo_group)
    employee1 = self.portal.person_module.newContent(
                      portal_type='Person',
                      title='Employee One',
                      career_reference='E1',
                      career_subordination_value=employer)
    employee2 = self.portal.person_module.newContent(
                      portal_type='Person',
                      title='Employee Two',
                      career_reference='E2',
                      career_subordination_value=employer)
    provider = self.portal.organisation_module.newContent(
                      portal_type='Organisation',
                      title='Payroll Service Provider')
    other_provider = self.portal.organisation_module.newContent(
                      portal_type='Organisation',
                      title='Another Payroll Service Provider')
    ps1 = self.portal.accounting_module.newContent(
                      portal_type='Pay Sheet Transaction',
                      title='Employee 1',
                      destination_section_value=employer,
                      source_section_value=employee1,
                      start_date=DateTime(2006, 1, 1),)
    line = ps1.newContent(portal_type='Pay Sheet Line',
                   resource_value=payroll_service,
                   source_section_value=provider,
                # (destination is set by PaySheetTransaction.createPaySheetLine)
                   destination_value=employee1,
                   variation_category_list=('tax_category/employee_share',
                                            'tax_category/employer_share'))
    line.updateCellRange(base_id='movement')
    cell_employee = line.newCell('tax_category/employee_share',
                                portal_type='Pay Sheet Cell',
                                base_id='movement',
                                mapped_value_property_list=('price',
                                                            'quantity'),)
1736
    cell_employee.edit(price=-.50, quantity=2000, tax_category='employee_share')
1737 1738 1739 1740 1741
    cell_employer = line.newCell('tax_category/employer_share',
                                portal_type='Pay Sheet Cell',
                                base_id='movement',
                                mapped_value_property_list=('price',
                                                            'quantity'),)
1742
    cell_employer.edit(price=-.40, quantity=2000, tax_category='employer_share')
1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762
    ps1.plan()

    ps2 = self.portal.accounting_module.newContent(
                      portal_type='Pay Sheet Transaction',
                      title='Employee 2',
                      destination_section_value=employer,
                      source_section_value=employee2,
                      start_date=DateTime(2006, 1, 1),)
    line = ps2.newContent(portal_type='Pay Sheet Line',
                   resource_value=payroll_service,
                   source_section_value=provider,
                   destination_value=employee2,
                   variation_category_list=('tax_category/employee_share',
                                            'tax_category/employer_share'))
    line.updateCellRange(base_id='movement')
    cell_employee = line.newCell('tax_category/employee_share',
                                portal_type='Pay Sheet Cell',
                                base_id='movement',
                                mapped_value_property_list=('price',
                                                            'quantity'),)
1763
    cell_employee.edit(price=-.50, quantity=3000, tax_category='employee_share')
1764 1765 1766 1767 1768
    cell_employer = line.newCell('tax_category/employer_share',
                                portal_type='Pay Sheet Cell',
                                base_id='movement',
                                mapped_value_property_list=('price',
                                                            'quantity'),)
1769
    cell_employer.edit(price=-.40, quantity=3000, tax_category='employer_share')
1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782

    other_line = ps2.newContent(portal_type='Pay Sheet Line',
                   resource_value=payroll_service,
                   destination_value=employee2,
                   source_section_value=other_provider,
                   variation_category_list=('tax_category/employee_share',
                                            'tax_category/employer_share'))
    other_line.updateCellRange(base_id='movement')
    cell_employee = other_line.newCell('tax_category/employee_share',
                                portal_type='Pay Sheet Cell',
                                base_id='movement',
                                mapped_value_property_list=('price',
                                                            'quantity'),)
1783
    cell_employee.edit(price=-.46, quantity=2998, tax_category='employee_share')
1784 1785 1786 1787 1788
    cell_employer = other_line.newCell('tax_category/employer_share',
                                portal_type='Pay Sheet Cell',
                                base_id='movement',
                                mapped_value_property_list=('price',
                                                            'quantity'),)
1789
    cell_employer.edit(price=-.42, quantity=2998, tax_category='employer_share')
1790 1791 1792 1793

    get_transaction().commit()
    self.tic()

Jérome Perrin's avatar
Jérome Perrin committed
1794 1795 1796
    # AccountingTransactionModule_getPaySheetMovementMirrorSectionItemList is
    # used in the report dialog to display possible organisations.
    self.assertEquals(
1797 1798
        [('', ''),
         (other_provider.getTitle(), other_provider.getRelativeUrl()),
Jérome Perrin's avatar
Jérome Perrin committed
1799 1800 1801 1802
         (provider.getTitle(), provider.getRelativeUrl())],
        self.portal.accounting_module\
    .AccountingTransactionModule_getPaySheetMovementMirrorSectionItemList())

1803
    # set request variables and render
1804 1805 1806 1807 1808 1809
    request_form = self.portal.REQUEST
    request_form['at_date'] = DateTime(2006, 2, 2)
    request_form['section_category'] = 'group/demo_group'
    request_form['simulation_state'] = ['draft', 'planned']
    request_form['resource'] = payroll_service.getRelativeUrl()
    request_form['mirror_section'] = provider.getRelativeUrl()
1810

1811
    report_section_list = self.getReportSectionList(
1812
                             self.portal.accounting_module,
1813 1814
                             'AccountingTransactionModule_viewPaySheetLineReport')
    self.assertEquals(1, len(report_section_list))
1815

1816 1817 1818 1819 1820 1821 1822 1823
    line_list = self.getListBoxLineList(report_section_list[0])
    data_line_list = [l for l in line_list if l.isDataLine()]
    self.assertEquals(2, len(data_line_list))

    # base_unit_quantity for EUR is set to 0.001 in createCurrencies, so the
    # precision is 3
    precision = self.portal.REQUEST.get('precision')
    self.assertEquals(3, precision)
1824

1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840
    self.checkLineProperties(data_line_list[0],
                            id=1,
                            employee_career_reference='E1',
                            employee_title='Employee One',
                            base=2000,
                            employee_share=2000 * .50,
                            employer_share=2000 * .40,
                            total=(2000 * .50 + 2000 * .40))
    self.checkLineProperties(data_line_list[1],
                            id=2,
                            employee_career_reference='E2',
                            employee_title='Employee Two',
                            base=3000,
                            employee_share=3000 * .50,
                            employer_share=3000 * .40,
                            total=(3000 * .50 + 3000 * .40))
1841
    # stat line
1842 1843 1844 1845 1846 1847
    self.checkLineProperties(line_list[-1],
                            base=3000 + 2000,
                            employee_share=(3000 + 2000) * .50,
                            employer_share=(3000 + 2000) * .40,
                            total=((3000 + 2000) * .50 + (3000 + 2000) * .40))

1848

1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902
  def test_PayrollTaxesReportDifferentSalaryRange(self):
    eur = self.portal.currency_module.EUR
    payroll_service = self.portal.payroll_service_module.newContent(
                      portal_type='Payroll Service',
                      title='PS1',
                      variation_base_category_list=('tax_category',
                                                    'salary_range'),
                      variation_category_list=('tax_category/employee_share',
                                               'tax_category/employer_share',
                                               'salary_range/france/tranche_a',
                                               'salary_range/france/tranche_b'))
    employer = self.portal.organisation_module.newContent(
                      portal_type='Organisation',
                      title='Employer',
                      price_currency_value=eur,
                      group_value=self.portal.portal_categories.group.demo_group)
    employee1 = self.portal.person_module.newContent(
                      portal_type='Person',
                      title='Employee One',
                      career_reference='E1',
                      career_subordination_value=employer)
    employee2 = self.portal.person_module.newContent(
                      portal_type='Person',
                      title='Employee Two',
                      career_reference='E2',
                      career_subordination_value=employer)
    provider = self.portal.organisation_module.newContent(
                      portal_type='Organisation',
                      title='Payroll Service Provider')
    other_provider = self.portal.organisation_module.newContent(
                      portal_type='Organisation',
                      title='Another Payroll Service Provider')
    ps1 = self.portal.accounting_module.newContent(
                      portal_type='Pay Sheet Transaction',
                      title='Employee 1',
                      destination_section_value=employer,
                      source_section_value=employee1,
                      start_date=DateTime(2006, 1, 1),)
    line = ps1.newContent(portal_type='Pay Sheet Line',
                   resource_value=payroll_service,
                   source_section_value=provider,
                # (destination is set by PaySheetTransaction.createPaySheetLine)
                   destination_value=employee1,
                   variation_category_list=('tax_category/employee_share',
                                            'tax_category/employer_share',
                                            'salary_range/france/tranche_a',
                                            'salary_range/france/tranche_b'))
    line.updateCellRange(base_id='movement')
    cell_employee_a = line.newCell('tax_category/employee_share',
                                   'salary_range/france/tranche_a',
                                   portal_type='Pay Sheet Cell',
                                   base_id='movement',
                                   mapped_value_property_list=('price',
                                                               'quantity'),)
1903
    cell_employee_a.edit(price=-.50, quantity=1000,
1904 1905 1906 1907 1908 1909 1910 1911
                         tax_category='employee_share',
                         salary_range='france/tranche_a')
    cell_employee_b = line.newCell('tax_category/employee_share',
                                   'salary_range/france/tranche_b',
                                   portal_type='Pay Sheet Cell',
                                   base_id='movement',
                                   mapped_value_property_list=('price',
                                                               'quantity'),)
1912
    cell_employee_b.edit(price=-.20, quantity=500,
1913 1914 1915 1916 1917 1918 1919 1920 1921
                         tax_category='employee_share',
                         salary_range='france/tranche_b')

    cell_employer_a = line.newCell('tax_category/employer_share',
                                   'salary_range/france/tranche_a',
                                   portal_type='Pay Sheet Cell',
                                   base_id='movement',
                                   mapped_value_property_list=('price',
                                                               'quantity'),)
1922
    cell_employer_a.edit(price=-.40, quantity=1000,
1923 1924 1925 1926 1927 1928 1929 1930
                         tax_category='employer_share',
                         salary_range='france/tranche_a')
    cell_employer_b = line.newCell('tax_category/employer_share',
                                   'salary_range/france/tranche_b',
                                   portal_type='Pay Sheet Cell',
                                   base_id='movement',
                                   mapped_value_property_list=('price',
                                                               'quantity'),)
1931
    cell_employer_b.edit(price=-.32, quantity=500,
1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957
                         tax_category='employer_share',
                         salary_range='france/tranche_b')

    ps1.plan()

    ps2 = self.portal.accounting_module.newContent(
                      portal_type='Pay Sheet Transaction',
                      title='Employee 2',
                      destination_section_value=employer,
                      source_section_value=employee2,
                      start_date=DateTime(2006, 1, 1),)
    line = ps2.newContent(portal_type='Pay Sheet Line',
                   resource_value=payroll_service,
                   source_section_value=provider,
                   destination_value=employee2,
                   variation_category_list=('tax_category/employee_share',
                                            'tax_category/employer_share',
                                            'salary_range/france/tranche_a',
                                            'salary_range/france/tranche_b'))
    line.updateCellRange(base_id='movement')
    cell_employee_a = line.newCell('tax_category/employee_share',
                                   'salary_range/france/tranche_a',
                                   portal_type='Pay Sheet Cell',
                                   base_id='movement',
                                   mapped_value_property_list=('price',
                                                               'quantity'),)
1958
    cell_employee_a.edit(price=-.50, quantity=1000,
1959 1960 1961 1962 1963 1964 1965 1966
                         salary_range='france/tranche_a',
                         tax_category='employee_share')
    cell_employee_b = line.newCell('tax_category/employee_share',
                                   'salary_range/france/tranche_b',
                                   portal_type='Pay Sheet Cell',
                                   base_id='movement',
                                   mapped_value_property_list=('price',
                                                               'quantity'),)
1967
    cell_employee_b.edit(price=-.20, quantity=3000,
1968 1969 1970 1971 1972 1973 1974 1975 1976
                         salary_range='france/tranche_b',
                         tax_category='employee_share')

    cell_employer_a = line.newCell('tax_category/employer_share',
                                   'salary_range/france/tranche_a',
                                   portal_type='Pay Sheet Cell',
                                   base_id='movement',
                                   mapped_value_property_list=('price',
                                                               'quantity'),)
1977
    cell_employer_a.edit(price=-.40, quantity=1000,
1978 1979 1980 1981 1982 1983 1984 1985
                         salary_range='france/tranche_a',
                         tax_category='employer_share')
    cell_employer_b = line.newCell('tax_category/employer_share',
                                   'salary_range/france/tranche_b',
                                   portal_type='Pay Sheet Cell',
                                   base_id='movement',
                                   mapped_value_property_list=('price',
                                                               'quantity'),)
1986
    cell_employer_b.edit(price=-.32, quantity=3000,
1987 1988 1989 1990 1991
                         salary_range='france/tranche_b',
                         tax_category='employer_share')
    get_transaction().commit()
    self.tic()

1992
    # set request variables and render
1993 1994 1995 1996 1997 1998
    request_form = self.portal.REQUEST
    request_form['at_date'] = DateTime(2006, 2, 2)
    request_form['section_category'] = 'group/demo_group'
    request_form['simulation_state'] = ['draft', 'planned']
    request_form['resource'] = payroll_service.getRelativeUrl()
    request_form['mirror_section'] = provider.getRelativeUrl()
1999

2000
    report_section_list = self.getReportSectionList(
2001
                             self.portal.accounting_module,
2002 2003
                             'AccountingTransactionModule_viewPaySheetLineReport')
    self.assertEquals(1, len(report_section_list))
2004

2005 2006 2007
    line_list = self.getListBoxLineList(report_section_list[0])
    data_line_list = [l for l in line_list if l.isDataLine()]
    self.assertEquals(6, len(data_line_list))
2008

2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056
    self.checkLineProperties(data_line_list[0],
                            id=1,
                            employee_career_reference='E1',
                            employee_title='Employee One',
                            base=1000,
                            employee_share=1000 * .50,
                            employer_share=1000 * .40,
                            total=(1000 * .50 + 1000 * .40))
    self.checkLineProperties(data_line_list[1],
                            id=2,
                            employee_career_reference='E2',
                            employee_title='Employee Two',
                            base=1000,
                            employee_share=1000 * .50,
                            employer_share=1000 * .40,
                            total=(1000 * .50 + 1000 * .40))
    self.checkLineProperties(data_line_list[2],
                            employee_title='Total Tranche A',
                            base=2000,
                            employee_share=2000 * .50,
                            employer_share=2000 * .40,
                            #total=(2000 * .50 + 2000 * .40)
                            )

    self.checkLineProperties(data_line_list[3],
                            id=3,
                            employee_career_reference='E1',
                            employee_title='Employee One',
                            base=500,
                            employee_share=500 * .20,
                            employer_share=500 * .32,
                            total=(500 * .20 + 500 * .32))
    self.checkLineProperties(data_line_list[4],
                            id=4,
                            employee_career_reference='E2',
                            employee_title='Employee Two',
                            base=3000,
                            employee_share=3000 * .20,
                            employer_share=3000 * .32,
                            total=(3000 * .20 + 3000 * .32))
    self.checkLineProperties(data_line_list[5],
                            employee_title='Total Tranche B',
                            base=3500,
                            employee_share=3500 * .20,
                            employer_share=3500 * .32,
                            #total=(3500 * .20 + 3500 * .32),
                            )

2057
    # stat line
2058 2059 2060 2061 2062 2063 2064
    self.checkLineProperties(line_list[-1],
                            base=2000 + 3500,
                            employee_share=(2000 * .50 + 3500 * .20),
                            employer_share=(2000 * .40 + 3500 * .32),
                            total=((2000 * .50 + 3500 * .20) +
                                   (2000 * .40 + 3500 * .32)))

2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113
  def test_NetSalaryReport(self):
    eur = self.portal.currency_module.EUR
    salary_service = self.portal.payroll_service_module.newContent(
                      portal_type='Payroll Service',
                      title='Gross Salary',
                      variation_base_category_list=('tax_category',),
                      variation_category_list=('tax_category/employee_share',
                                               'tax_category/employer_share'))
    payroll_service = self.portal.payroll_service_module.newContent(
                      portal_type='Payroll Service',
                      title='PS1',
                      variation_base_category_list=('tax_category',),
                      variation_category_list=('tax_category/employee_share',
                                               'tax_category/employer_share'))
    employer = self.portal.organisation_module.newContent(
                      portal_type='Organisation',
                      title='Employer',
                      price_currency_value=eur,
                      group_value=self.portal.portal_categories.group.demo_group)
    employee1 = self.portal.person_module.newContent(
                      portal_type='Person',
                      title='Employee One',
                      career_reference='E1',
                      career_subordination_value=employer)
    employee1_ba = employee1.newContent(portal_type='Bank Account',
                                        title='Bank 1')
    employee2 = self.portal.person_module.newContent(
                      portal_type='Person',
                      title='Employee Two',
                      career_reference='E2',
                      career_subordination_value=employer)
    employee2_ba = employee2.newContent(portal_type='Bank Account',
                                        title='Bank 2')
    provider = self.portal.organisation_module.newContent(
                      portal_type='Organisation',
                      title='Payroll Service Provider')
    other_provider = self.portal.organisation_module.newContent(
                      portal_type='Organisation',
                      title='Another Payroll Service Provider')
    ps1 = self.portal.accounting_module.newContent(
                      portal_type='Pay Sheet Transaction',
                      title='Employee 1',
                      destination_section_value=employer,
                      source_section_value=employee1,
                      payment_condition_source_payment_value=employee1_ba,
                      start_date=DateTime(2006, 1, 1),)
    line = ps1.newContent(portal_type='Pay Sheet Line',
                   resource_value=salary_service,
                   destination_value=employee1,
2114
                   base_contribution_list=['base_amount/net_salary',],
2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127
                   variation_category_list=('tax_category/employee_share',
                                            'tax_category/employer_share'))
    line.updateCellRange(base_id='movement')
    cell_employee = line.newCell('tax_category/employee_share',
                                portal_type='Pay Sheet Cell',
                                base_id='movement',
                                mapped_value_property_list=('price',
                                                            'quantity'),)
    cell_employee.edit(price=1, quantity=2000, tax_category='employee_share')
    line = ps1.newContent(portal_type='Pay Sheet Line',
                   resource_value=payroll_service,
                   source_section_value=provider,
                   destination_value=employee1,
2128
                   base_contribution_list=['base_amount/net_salary',],
2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155
                   variation_category_list=('tax_category/employee_share',
                                            'tax_category/employer_share'))
    line.updateCellRange(base_id='movement')
    cell_employee = line.newCell('tax_category/employee_share',
                                portal_type='Pay Sheet Cell',
                                base_id='movement',
                                mapped_value_property_list=('price',
                                                            'quantity'),)
    cell_employee.edit(price=-.50, quantity=2000, tax_category='employee_share')
    cell_employer = line.newCell('tax_category/employer_share',
                                portal_type='Pay Sheet Cell',
                                base_id='movement',
                                mapped_value_property_list=('price',
                                                            'quantity'),)
    cell_employer.edit(price=-.40, quantity=2000, tax_category='employer_share')
    ps1.plan()

    ps2 = self.portal.accounting_module.newContent(
                      portal_type='Pay Sheet Transaction',
                      title='Employee 2',
                      destination_section_value=employer,
                      source_section_value=employee2,
                      payment_condition_source_payment_value=employee2_ba,
                      start_date=DateTime(2006, 1, 1),)
    line = ps2.newContent(portal_type='Pay Sheet Line',
                   resource_value=salary_service,
                   destination_value=employee2,
2156
                   base_contribution_list=['base_amount/net_salary',],
2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169
                   variation_category_list=('tax_category/employee_share',
                                            'tax_category/employer_share'))
    line.updateCellRange(base_id='movement')
    cell_employee = line.newCell('tax_category/employee_share',
                                portal_type='Pay Sheet Cell',
                                base_id='movement',
                                mapped_value_property_list=('price',
                                                            'quantity'),)
    cell_employee.edit(price=1, quantity=3000, tax_category='employee_share')
    line = ps2.newContent(portal_type='Pay Sheet Line',
                   resource_value=payroll_service,
                   source_section_value=provider,
                   destination_value=employee2,
2170
                   base_contribution_list=['base_amount/net_salary',],
2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189
                   variation_category_list=('tax_category/employee_share',
                                            'tax_category/employer_share'))
    line.updateCellRange(base_id='movement')
    cell_employee = line.newCell('tax_category/employee_share',
                                portal_type='Pay Sheet Cell',
                                base_id='movement',
                                mapped_value_property_list=('price',
                                                            'quantity'),)
    cell_employee.edit(price=-.50, quantity=3000, tax_category='employee_share')
    cell_employer = line.newCell('tax_category/employer_share',
                                portal_type='Pay Sheet Cell',
                                base_id='movement',
                                mapped_value_property_list=('price',
                                                            'quantity'),)
    cell_employer.edit(price=-.40, quantity=3000, tax_category='employer_share')

    get_transaction().commit()
    self.tic()

2190
    # set request variables and render
2191 2192 2193 2194
    request_form = self.portal.REQUEST
    request_form['at_date'] = DateTime(2006, 2, 2)
    request_form['section_category'] = 'group/demo_group'
    request_form['simulation_state'] = ['draft', 'planned']
2195

2196 2197 2198 2199
    report_section_list = self.getReportSectionList(
                             self.portal.accounting_module,
                             'AccountingTransactionModule_viewNetSalaryReport')
    self.assertEquals(1, len(report_section_list))
2200

2201 2202 2203 2204 2205 2206 2207 2208
    line_list = self.getListBoxLineList(report_section_list[0])
    data_line_list = [l for l in line_list if l.isDataLine()]
    self.assertEquals(2, len(data_line_list))

    # base_unit_quantity for EUR is set to 0.001 in createCurrencies, so the
    # precision is 3
    precision = self.portal.REQUEST.get('precision')
    self.assertEquals(3, precision)
2209

2210 2211 2212 2213 2214 2215 2216 2217 2218 2219
    self.checkLineProperties(data_line_list[0],
                            employee_career_reference='E1',
                            employee_title='Employee One',
                            employee_bank_account='Bank 1',
                            total_price=2000 - (2000 * .5),)
    self.checkLineProperties(data_line_list[1],
                            employee_career_reference='E2',
                            employee_title='Employee Two',
                            employee_bank_account='Bank 2',
                            total_price=3000 - (3000 * .5),)
2220
    # stat line
2221 2222 2223 2224
    self.checkLineProperties(
            line_list[-1],
            total_price=3000 + 2000 - (2000 * .5) - (3000 * .5))

2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375
  def test_AccountingLineGeneration(self):
    # create payroll services
    base_salary = self.portal.payroll_service_module.newContent(
                          portal_type='Payroll Service',
                          title='Base Salary',
                          product_line='base_salary',
                          variation_base_category_list=('tax_category',),
                          variation_category_list=('tax_category/employee_share',
                                                   'tax_category/employer_share'))
    bonus = self.portal.payroll_service_module.newContent(
                          portal_type='Payroll Service',
                          title='Bonus',
                          product_line='base_salary',
                          variation_base_category_list=('tax_category',),
                          variation_category_list=('tax_category/employee_share',
                                                   'tax_category/employer_share'))
    deductions = self.portal.payroll_service_module.newContent(
                          portal_type='Payroll Service',
                          title='Deductions',
                          product_line='base_salary',
                          variation_base_category_list=('tax_category',),
                          variation_category_list=('tax_category/employee_share',
                                                   'tax_category/employer_share'))
    tax1 = self.portal.payroll_service_module.newContent(
                          portal_type='Payroll Service',
                          title='Tax1',
                          product_line='payroll_tax_1',
                          variation_base_category_list=('tax_category',),
                          variation_category_list=('tax_category/employee_share',
                                                   'tax_category/employer_share'))

    # create accounts
    account_payroll_wages_expense = self.portal.account_module.newContent(
                          portal_type='Account',
                          title='Payroll Wages (expense)',
                          account_type='expense',)
    account_payroll_taxes_expense = self.portal.account_module.newContent(
                          portal_type='Account',
                          title='Payroll Taxes (expense)',
                          account_type='expense',)
    account_net_wages = self.portal.account_module.newContent(
                          portal_type='Account',
                          title='Net Wages',
                          account_type='liability/payable',)
    account_payroll_taxes = self.portal.account_module.newContent(
                          portal_type='Account',
                          title='Payroll Taxes',
                          account_type='liability/payable',)

    # create an invoice transaction rule for pay sheets.
    rule = self.portal.portal_rules.newContent(
                          portal_type='Invoice Transaction Rule',
                          title='Rule for PaySheet Accounting',
                          reference='paysheet_transaction_rule',
                          test_method_id=
                              'SimulationMovement_testInvoiceTransactionRule')
    rule.newContent(portal_type='Predicate',
                    title='Employee Share',
                    string_index='tax_category',
                    int_index=1,
                    membership_criterion_base_category_list=('tax_category',),
                    membership_criterion_category_list=('tax_category/employee_share',))
    rule.newContent(portal_type='Predicate',
                    title='Employer Share',
                    string_index='tax_category',
                    int_index=2,
                    membership_criterion_base_category_list=('tax_category',),
                    membership_criterion_category_list=('tax_category/employer_share',))

    rule.newContent(portal_type='Predicate',
                    title='Base Salary',
                    string_index='payroll_service',
                    int_index=1,
                    membership_criterion_base_category_list=('product_line',),
                    membership_criterion_category_list=('product_line/base_salary',))
    rule.newContent(portal_type='Predicate',
                    title='Payroll Tax 1',
                    string_index='payroll_service',
                    int_index=2,
                    membership_criterion_base_category_list=('product_line',),
                    membership_criterion_category_list=('product_line/payroll_tax_1',))

    get_transaction().commit()
    self.tic()

    cell_list = rule.contentValues(portal_type='Accounting Rule Cell')
    self.assertEquals(4, len(cell_list))

    employee_base_salary = rule._getOb('movement_0_0')
    self.assertEquals('Employee Share * Base Salary',
                      employee_base_salary.getTitle())
    employee_base_salary.newContent(
                      portal_type='Accounting Rule Cell Line',
                      destination_debit=1,
                      destination_value=account_payroll_wages_expense)
    employee_base_salary.newContent(
                      portal_type='Accounting Rule Cell Line',
                      destination_credit=1,
                      destination_value=account_net_wages)

    employer_tax = rule._getOb('movement_1_1')
    self.assertEquals('Employer Share * Payroll Tax 1',
                      employer_tax.getTitle())
    employer_tax.newContent(
                      portal_type='Accounting Rule Cell Line',
                      destination_debit=1,
                      destination_value=account_payroll_taxes)
    employer_tax.newContent(
                      portal_type='Accounting Rule Cell Line',
                      destination_credit=1,
                      destination_value=account_payroll_taxes_expense)

    employee_tax = rule._getOb('movement_0_1')
    self.assertEquals('Employee Share * Payroll Tax 1',
                      employee_tax.getTitle())
    employee_tax.newContent(
                      portal_type='Accounting Rule Cell Line',
                      destination_debit=1,
                      destination_value=account_payroll_taxes)
    employee_tax.newContent(
                      portal_type='Accounting Rule Cell Line',
                      destination_credit=1,
                      generate_prevision_script_id=\
      'SimulationMovement_generatePrevisionForEmployeeSharePaySheetMovement',
                      destination_value=account_net_wages)
    rule.validate()

    # create a pay sheet
    eur = self.portal.currency_module.EUR
    employer = self.portal.organisation_module.newContent(
                      portal_type='Organisation',
                      title='Employer',
                      price_currency_value=eur,
                      group_value=self.portal.portal_categories.group.demo_group)
    employee = self.portal.person_module.newContent(
                      portal_type='Person',
                      title='Employee',
                      career_reference='E1',
                      career_subordination_value=employer)
    provider = self.portal.organisation_module.newContent(
                      portal_type='Organisation',
                      title='Payroll Service Provider')

    ps = self.portal.accounting_module.newContent(
                      portal_type='Pay Sheet Transaction',
                      price_currency_value=eur,
                      resource_value=eur,
                      title='Employee 1',
                      destination_section_value=employer,
                      source_section_value=employee,
                      start_date=DateTime(2006, 1, 1),)
2376

2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396
    # base salary = 2000
    line = ps.newContent(portal_type='Pay Sheet Line',
                   title='Base salary',
                   resource_value=base_salary,
                   destination_value=employee,
                   variation_category_list=('tax_category/employee_share',
                                            'tax_category/employer_share'))
    line.updateCellRange(base_id='movement')
    cell_employee = line.newCell('tax_category/employee_share',
                                portal_type='Pay Sheet Cell',
                                base_id='movement',
                                mapped_value_property_list=('price',
                                                            'quantity'),)
    cell_employee.edit(price=1, quantity=2000, tax_category='employee_share')
    cell_employer = line.newCell('tax_category/employer_share',
                                portal_type='Pay Sheet Cell',
                                base_id='movement',
                                mapped_value_property_list=('price',
                                                            'quantity'),)
    cell_employer.edit(price=1, quantity=2000, tax_category='employer_share')
2397

2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439
    # base_salary += 100 (bonus)
    line = ps.newContent(portal_type='Pay Sheet Line',
                   title='Bonus',
                   resource_value=bonus,
                   destination_value=employee,
                   variation_category_list=('tax_category/employee_share',
                                            'tax_category/employer_share'))
    line.updateCellRange(base_id='movement')
    cell_employee = line.newCell('tax_category/employee_share',
                                portal_type='Pay Sheet Cell',
                                base_id='movement',
                                mapped_value_property_list=('price',
                                                            'quantity'),)
    cell_employee.edit(price=1, quantity=100, tax_category='employee_share')
    cell_employer = line.newCell('tax_category/employer_share',
                                portal_type='Pay Sheet Cell',
                                base_id='movement',
                                mapped_value_property_list=('price',
                                                            'quantity'),)
    cell_employer.edit(price=1, quantity=100, tax_category='employer_share')

    # base_salary -= 50 (deductions)   => base_salary == 2050
    line = ps.newContent(portal_type='Pay Sheet Line',
                   title='Deduction',
                   resource_value=deductions,
                   destination_value=employee,
                   variation_category_list=('tax_category/employee_share',
                                            'tax_category/employer_share'))
    line.updateCellRange(base_id='movement')
    cell_employee = line.newCell('tax_category/employee_share',
                                portal_type='Pay Sheet Cell',
                                base_id='movement',
                                mapped_value_property_list=('price',
                                                            'quantity'),)
    cell_employee.edit(price=-1, quantity=50, tax_category='employee_share')
    cell_employer = line.newCell('tax_category/employer_share',
                                portal_type='Pay Sheet Cell',
                                base_id='movement',
                                mapped_value_property_list=('price',
                                                            'quantity'),)
    cell_employer.edit(price=-1, quantity=50, tax_category='employer_share')

2440
    # tax1 = 10% for employee ( 205 )
2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466
    #        20% for employer ( 410 )
    line = ps.newContent(portal_type='Pay Sheet Line',
                   title='Tax 1',
                   resource_value=tax1,
                   source_section_value=provider,
                   destination_value=employee,
                   variation_category_list=('tax_category/employee_share',
                                            'tax_category/employer_share'))
    line.updateCellRange(base_id='movement')
    cell_employee = line.newCell('tax_category/employee_share',
                                portal_type='Pay Sheet Cell',
                                base_id='movement',
                                mapped_value_property_list=('price',
                                                            'quantity'),)
    cell_employee.edit(price=-.1, quantity=2050, tax_category='employee_share')
    cell_employer = line.newCell('tax_category/employer_share',
                                portal_type='Pay Sheet Cell',
                                base_id='movement',
                                mapped_value_property_list=('price',
                                                            'quantity'),)
    cell_employer.edit(price=-.2, quantity=2050, tax_category='employer_share')

    ps.plan()

    get_transaction().commit()
    self.tic()
2467

2468 2469 2470
    related_applied_rule = ps.getCausalityRelatedValue(
                                portal_type='Applied Rule')
    self.assertNotEquals(related_applied_rule, None)
2471

2472 2473 2474 2475 2476 2477 2478 2479 2480
    # build accounting lines
    ps.confirm()
    ps.start()
    get_transaction().commit()
    self.tic()

    accounting_line_list = ps.contentValues(
        portal_type='Pay Sheet Transaction Line')
    self.assertEquals(len(accounting_line_list), 4)
2481

2482 2483 2484 2485 2486 2487 2488 2489 2490 2491
    line = [l for l in accounting_line_list
            if l.getDestinationValue() == account_payroll_wages_expense][0]
    self.assertEquals(2050, line.getDestinationDebit())
    self.assertEquals(employer, line.getDestinationSectionValue())

    line = [l for l in accounting_line_list
            if l.getDestinationValue() == account_net_wages][0]
    self.assertEquals(2050 - 205, line.getDestinationCredit())
    self.assertEquals(employer, line.getDestinationSectionValue())
    self.assertEquals(employee, line.getSourceSectionValue())
2492

2493 2494 2495 2496 2497 2498 2499 2500 2501 2502
    line = [l for l in accounting_line_list
            if l.getDestinationValue() == account_payroll_taxes_expense][0]
    self.assertEquals(410, line.getDestinationDebit())
    self.assertEquals(employer, line.getDestinationSectionValue())

    line = [l for l in accounting_line_list
            if l.getDestinationValue() == account_payroll_taxes][0]
    self.assertEquals(410 + 205, line.getDestinationCredit())
    self.assertEquals(employer, line.getDestinationSectionValue())
    self.assertEquals(provider, line.getSourceSectionValue())
2503

2504 2505 2506 2507 2508 2509 2510 2511
  def test_intermediateLinesAreNotCreatedOnPaysheet(self):
    '''
      Intermediate lines are paysheet model lines usefull to calcul, but we
      don't want to have on paysheet. So a checkbox on paysheet model lines
      permit to create it or not (created by default)
    '''
    eur = self.portal.currency_module.EUR
    model = self.paysheet_model_module.newContent( \
2512 2513
                              portal_type='Pay Sheet Model',
                              variation_settings_category_list=self.variation_settings_category_list)
2514 2515
    model.setPriceCurrencyValue(eur)

2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527
    
    self.addSlice(model, 'salary_range/%s' % \
        self.france_settings_slice_a, 0, 1000)
    self.addSlice(model, 'salary_range/%s' % \
        self.france_settings_slice_b, 1000, 2000)
    self.addSlice(model, 'salary_range/%s' % \
        self.france_settings_slice_c, 2000, 10000000)
    self.addSlice(model, 'salary_range/%s' % \
        self.france_settings_forfait, 0, 10000000)

    urssaf_slice_list = [ 'salary_range/'+self.france_settings_slice_a,]
    urssaf_share_list = [ 'tax_category/'+self.tax_category_employee_share,]
2528 2529 2530 2531 2532 2533 2534 2535 2536
    salary_slice_list = ['salary_range/'+self.france_settings_forfait,]
    salary_share_list = ['tax_category/'+self.tax_category_employee_share,]
    variation_category_list_urssaf = urssaf_share_list + urssaf_slice_list
    variation_category_list_salary = salary_share_list + salary_slice_list

    model_line_1 = self.createModelLine(model=model,
        id='model_line_1',
        variation_category_list=variation_category_list_salary,
        resource=self.labour,
2537 2538
        share_list=salary_share_list,
        slice_list=salary_slice_list,
2539 2540 2541
        values=[[[10000, None],],],
        base_application_list=[],
        base_contribution_list=['base_amount/base_salary', 'base_amount/gross_salary'])
2542 2543
    model_line_1.setIntIndex(1)

2544 2545 2546 2547
    model_line_2 = self.createModelLine(model=model,
        id='model_line_2',
        variation_category_list=variation_category_list_urssaf,
        resource=self.urssaf,
2548 2549 2550
        share_list=urssaf_share_list,
        slice_list=urssaf_slice_list,
        values=[[[None, 0.8]],],
2551
        source_value=self.payroll_service_organisation,
2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564
        base_application_list=[ 'base_amount/base_salary',],
        base_contribution_list=['base_amount/net_salary',])
    model_line_2.setIntIndex(2)
    
    model_line_3 = self.createModelLine(model=model,
        id='model_line_3',
        variation_category_list=variation_category_list_urssaf,
        resource=self.urssaf,
        share_list=urssaf_share_list,
        slice_list=urssaf_slice_list,
        values=[[[None, -0.1]],],
        source_value=self.payroll_service_organisation,
        base_application_list=[ 'base_amount/net_salary',],
2565
        base_contribution_list=['base_amount/deductible_tax',])
2566
    model_line_3.setIntIndex(3)
2567 2568 2569 2570 2571 2572 2573 2574 2575

    # create a paysheet with two lines
    paysheet = self.portal.accounting_module.newContent(
                              portal_type='Pay Sheet Transaction',
                              specialise_value=model)
    paysheet.PaySheetTransaction_applyModel()
    self.assertEquals(len(paysheet.contentValues(portal_type='Pay Sheet Line')), 0)
    # calculate the pay sheet
    pay_sheet_line_list = self.calculatePaySheet(paysheet=paysheet)
2576 2577 2578 2579 2580
    self.assertEquals(len(paysheet.contentValues(portal_type='Pay Sheet Line')), 3)
    # check values on the paysheet
    self.assertEquals(paysheet.contentValues()[0].contentValues()[0].getTotalPrice(), 10000)
    self.assertEquals(paysheet.contentValues()[1].contentValues()[0].getTotalPrice(), 8000)
    self.assertEquals(paysheet.contentValues()[2].contentValues()[0].getTotalPrice(), -800)
2581 2582

    # create a paysheet with one normal line and an intermediate line
2583
    model_line_2.setCreatePaysheetLine(False)
2584 2585 2586 2587 2588 2589 2590 2591
    paysheet = self.portal.accounting_module.newContent(
                              portal_type='Pay Sheet Transaction',
                              specialise_value=model)
    paysheet.PaySheetTransaction_applyModel()
    self.assertEquals(len(paysheet.contentValues(portal_type='Pay Sheet Line')), 0)
    # calculate the pay sheet
    pay_sheet_line_list = self.calculatePaySheet(paysheet=paysheet)
    # now only one line should be created 
2592 2593 2594 2595 2596
    self.assertEquals(len(paysheet.contentValues(portal_type='Pay Sheet Line')), 2)

    # check values on the paysheet
    self.assertEquals(paysheet.contentValues()[0].contentValues()[0].getTotalPrice(), 10000)
    self.assertEquals(paysheet.contentValues()[1].contentValues()[0].getTotalPrice(), -800)
2597

2598 2599 2600 2601 2602
import unittest
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestPayroll))
  return suite