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

"""Test suite for erp5_accounting_l10n_m9
"""
import os, sys
if __name__ == '__main__':
  execfile(os.path.join(sys.path[0], 'framework.py'))

os.environ.setdefault('EVENT_LOG_FILE', 'zLOG.log')
os.environ.setdefault('EVENT_LOG_SEVERITY', '-300')

from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
39
from Products.ERP5Type.tests.utils import reindex
40 41 42 43 44 45 46
from AccessControl.SecurityManagement import newSecurityManager
from Products.DCWorkflow.DCWorkflow import ValidationFailed

class TestAccounting_l10n_M9(ERP5TypeTestCase):
  """Test Accounting M9 and Invoice Transmission Sheets.
  """
  RUN_ALL_TESTS = 1
47 48
  invoice_transmission_sheet_portal_type = \
        'Invoice Transmission Sheet'
49 50 51 52

  def getTitle(self):
    return "ERP5 Accounting l10n M9"
  
53
  def getInvoiceTransmissionSheetModule(self):
54 55 56
    """Returns the module for purchase invoice transmission sheets.
    """
    return getattr( self.portal,
57
                    'invoice_transmission_sheet_module',
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
                    None )
  
  def getAccountingModule(self):
    """Returns the accounting module."""
    return getattr( self.portal, 'accounting_module', None)

  def getAccountModule(self):
    """Returns the account module."""
    return getattr( self.portal, 'account_module', None)

  def getBusinessTemplateList(self):
    return ( 'erp5_base',
             'erp5_trade', # TODO: remove those dependencies
             'erp5_pdm',
             'erp5_pdf_style',
             'erp5_accounting',
             'erp5_accounting_l10n_fr_m9',
           )

  def login(self):
    """creates a user and login"""
    uf = self.getPortal().acl_users
    uf._doAddUser('zope', 'zope', ['Member', 'Assignee', 'Assignor',
                               'Auditor', 'Author', 'Manager'], [])
    user = uf.getUserById('zope').__of__(uf)
    newSecurityManager(None, user)

  def afterSetUp(self):
    """set up """
    self.login()
    portal = self.getPortal()
    self.portal = portal
    self.category_tool = portal.portal_categories
    self.section = self._createOrganisation()
    self.mirror_section = self._createOrganisation()
93 94
  
  @reindex
95 96 97 98 99 100 101
  def _createOrganisation(self, **kw):
    """Create an organisation and index it.
    """
    org = self.getOrganisationModule().newContent(portal_type='Organisation')
    org.edit(**kw)
    return org

102
  @reindex
103 104 105 106 107 108 109 110 111
  def _getAccount(self, account_id, **kw):
    """Get an account or create it.
    """
    account_module = self.getAccountModule()
    account = getattr(account_module, account_id, None)
    if account is None:
      account = account_module.newContent(id=account_id)
    account.edit(**kw)
    return account
112 113
  
  @reindex
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
  def _createPurchaseInvoice(self, amount=100, **kw):
    """Create a purchase invoice and index it.
    """
    payable_account = self._getAccount('payable_account',
                                       gap='fr/m9/4/40/401/4012',
                                       account_type='liability/payable')
    expense_account = self._getAccount('expense_account',
                                       gap='fr/m9/6/60/602/6022/60225',
                                       account_type='expense')
    invoice = self.getAccountingModule().newContent(
                      portal_type='Purchase Invoice Transaction',
                      created_by_builder=1)
    invoice.newContent(portal_type='Accounting Transaction Line',
                       source_value=expense_account,
                       quantity=amount)
    invoice.newContent(portal_type='Accounting Transaction Line',
                       source_value=payable_account,
                       quantity=-amount)
    invoice.edit(**kw)
    return invoice
  
  def test_TransmissionSheetModule(self):
136 137
    """Checks the invoice transmission sheet module is installed."""
    self.assertNotEquals(None, self.getInvoiceTransmissionSheetModule())
138 139 140 141 142 143 144 145 146 147 148 149 150
  
  def test_AccountingPlanInstallation(self):
    """Tests that the accounting plan is well installed."""
    self.failUnless('m9' in self.category_tool.gap.fr.objectIds())
    self.assertNotEquals(0, len(self.category_tool.gap.fr.m9.objectIds()))
    self.failUnless('gap/fr/m9' in [x[1] for x in
           self.portal.account_module.AccountModule_getAvailableGapList()])

  def test_SimpleTransmissionSheet(self):
    """Simple use case for a transmission sheet."""
    invoice = self._createPurchaseInvoice(
                            destination_section_value=self.section,
                            source_section_value=self.mirror_section, )
151
    transmission_sheet_module = self.getInvoiceTransmissionSheetModule()
152
    transmission_sheet = transmission_sheet_module.newContent(
153
            portal_type=self.invoice_transmission_sheet_portal_type)
154 155
    self.assertEquals(transmission_sheet.getValidationState(), 'draft')
    # add an invoice to the transamission sheet
Jérome Perrin's avatar
Jérome Perrin committed
156 157
    invoice.setAggregateValue(transmission_sheet)
    invoice.recursiveImmediateReindexObject()
158 159
    self.getWorkflowTool().doActionFor(
                            transmission_sheet,
160
                            'emit_action') # XXX is this transition name good?
161
    self.assertEquals(transmission_sheet.getValidationState(),
162
                            'new')
163 164 165 166

  def test_TransmissionSheetEmitRefusedIfNoInvoice(self):
    """Transmission sheet cannot be emitted if it doesn't contain any invoice.
    """
167
    transmission_sheet_module = self.getInvoiceTransmissionSheetModule()
168
    transmission_sheet = transmission_sheet_module.newContent(
169
            portal_type=self.invoice_transmission_sheet_portal_type)
170 171 172 173
    self.assertEquals(transmission_sheet.getValidationState(), 'draft')
    self.assertRaises(ValidationFailed, self.getWorkflowTool().doActionFor,
                      transmission_sheet, 'emit_action')

174 175 176 177 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
  def test_AccountTypeConstaintForExpense(self):
    account = self._getAccount('account',
                               gap='fr/m9/6/60/602/6022/60225',
                               account_type='expense')
    self.assertEquals([], account.checkConsistency())
  
  def test_AccountTypeConstaintFixForExpense(self):
    account = self._getAccount('account',
                               gap='fr/m9/6/60/602/6022/60225',
                               account_type='equity')
    self.assertEquals(1, len(account.checkConsistency(fixit=1)))
    self.assertEquals('expense', account.getAccountType())

  def test_AccountTypeConstaintForPayable(self):
    account = self._getAccount('payable_account',
                               gap='fr/m9/4/40',
                               account_type='liability/payable')
    self.assertEquals([], account.checkConsistency())
  
  def test_AccountTypeConstaintFixForPayable(self):
    account = self._getAccount('payable_account',
                               gap='fr/m9/4/40',
                               account_type='equity')
    self.assertEquals(1, len(account.checkConsistency(fixit=1)))
    self.assertEquals('liability/payable', account.getAccountType())

  def test_AccountTypeConstaintForClass4(self):
    # members of class 4 can be payable or receivable
    account = self._getAccount('class4_account',
                               gap='fr/m9/4/44',
                               account_type='liability/payable')
    self.assertEquals([], account.checkConsistency())
    
    account.edit(account_type='asset/receivable')
    self.assertEquals([], account.checkConsistency())
    
  def test_AccountTypeConstaintFixForClass4(self):
    # members of class 4 can be payable or receivable
    account = self._getAccount('class4_account',
                               gap='fr/m9/4/44',
                               account_type='equity')
    self.assertEquals(1, len(account.checkConsistency(fixit=1)))
    self.failUnless(account.getAccountType() in ('liability/payable',
                                                 'asset/receivable'))

219 220 221 222 223 224
  def test_AccountTypeConstaintFixForPayable(self):
    account = self._getAccount('payable_account',
                               gap='fr/m9/4/40',
                               account_type='equity')
    self.assertEquals(1, len(account.checkConsistency(fixit=1)))
    self.assertEquals('liability/payable', account.getAccountType())
225 226 227 228 229 230 231 232 233 234 235 236 237 238
  
  # Members of gap/fr/m9/4/47 are very specific
  # for now, we do not fix 476 or 477
  def test_AccountTypeConstaintFixFor4718(self):
    account = self._getAccount('4718',
                               gap='fr/m9/4/47/471/4718', )
    self.assertEquals(1, len(account.checkConsistency(fixit=1)))
    self.assertEquals('asset/receivable', account.getAccountType())
  
  def test_AccountTypeConstaintFixFor4721(self):
    account = self._getAccount('4721',
                               gap='fr/m9/4/47/472/4721', )
    self.assertEquals(1, len(account.checkConsistency(fixit=1)))
    self.assertEquals('liability/payable', account.getAccountType())
239

240 241 242 243 244 245 246 247 248 249 250 251
  def test_AccountTypeConstaintFixFor4731(self):
    account = self._getAccount('4731',
                               gap='fr/m9/4/47/473/4731', )
    self.assertEquals(1, len(account.checkConsistency(fixit=1)))
    self.assertEquals('asset/receivable', account.getAccountType())

  def test_AccountTypeConstaintFixFor4735(self):
    account = self._getAccount('4735',
                               gap='fr/m9/4/47/473/4735', )
    self.assertEquals(1, len(account.checkConsistency(fixit=1)))
    self.assertEquals('liability/payable', account.getAccountType())
    
252 253 254 255 256 257 258 259 260
if __name__ == '__main__':
  framework()
else:
  import unittest
  def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(TestAccounting_l10n_M9))
    return suite