BaobabConduit.py 42.2 KB
Newer Older
Kevin Deldycke's avatar
Kevin Deldycke committed
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
##############################################################################
#
# Copyright (c) 2005 Nexedi SARL and Contributors. All Rights Reserved.
#                    Kevin Deldycke <kevin@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.
#
##############################################################################

from Products.ERP5SyncML.Conduit.ERP5Conduit import ERP5Conduit
from AccessControl import ClassSecurityInfo
from Products.ERP5Type import Permissions
from Products.ERP5Type.Utils import convertToUpperCase
from Products.CMFCore.utils import getToolByName
from Acquisition import aq_base, aq_inner, aq_chain, aq_acquire
Yoshinori Okuji's avatar
Yoshinori Okuji committed
35
from ZODB.POSException import ConflictError
Kevin Deldycke's avatar
Kevin Deldycke committed
36 37 38 39 40 41 42 43

import datetime

from zLOG import LOG



class BaobabConduit(ERP5Conduit):
44 45 46 47 48 49 50 51 52 53
  """
  A conduit is in charge to read data from a particular structure,
  and then to save this data in another structure.

  In baobab, the data is read from some sql tables and it will stored
  with ERP5 objects. The difficult parts are :
  - for one sql table we have several kind of objects in ERP5
  - for each properties that comes from sql, we have one ore more
    properties in ERP5

Sebastien Robin's avatar
Sebastien Robin committed
54
  Most importants method defined here are :
55 56 57 58 59 60 61 62 63 64
  - constructContent : it is used when a new set of data comes from
                       the sql table, then constructContent must decide
                       wich kind of object must be created in ERP5
  - editDocument : after constructContent, editDocument is called with
                   all properties that comes from the set of data from
                   sql. Each property must be converted to ERP5 property.

  If you need to handle a new property, the most important thing to know
  is what will be the property used in ERP5. Then you have to enter a
  new dictionary in the property_map variable.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

  # New changes on october 2006:
  * on Compte
    - numero become numero_interne
    - add code_pays (ex K)
    - add code etab (code etablissement), ex 0101
    - add code_guichet
    - add numero_compte : international account number
    - add rib : ex 52
    - add code_bic : ex SGBSSND0
    - add overdraft_facility O/N (Yes/No)
    - add swift_registered  O/N (Yes/No)
  * on Client
    - remove code_bic
    - zone_residence is now well completed, we should find country
         information. We should be a corresponding dictionary in order
         to set the right region
82
  """
Kevin Deldycke's avatar
Kevin Deldycke committed
83 84 85 86 87 88 89

  global property_map

  # Declarative security
  security = ClassSecurityInfo()


90
  ### This data structure associate a xml property to an ERP5 object property in certain conditions
91
  property_map = {
Sebastien Robin's avatar
Sebastien Robin committed
92
    # For example, in the sql export, we use for the first name of a person the
93 94
    # property 'nom', in ERP5 we use the property first_name
    'nom':[{
Sebastien Robin's avatar
Sebastien Robin committed
95
          'erp5_property': 'first_name'
96 97
        , 'conditions'   : {'erp5_portal_type':'Person'}
        }
Sebastien Robin's avatar
Sebastien Robin committed
98
      , {
Sebastien Robin's avatar
Sebastien Robin committed
99
         'erp5_property': 'title'
100 101
        , 'conditions'   : {'erp5_portal_type':'Organisation'}
        }],
Sebastien Robin's avatar
Sebastien Robin committed
102
    # For example, in the sql export, we use for the name of an organisation the
103 104
    # property 'nom', in ERP5 we use the property title
    'adresse': [{
Sebastien Robin's avatar
Sebastien Robin committed
105
        'erp5_property': 'default_address_street_address'
Kevin Deldycke's avatar
Kevin Deldycke committed
106 107
      , 'conditions'   : [{'erp5_portal_type':'Organisation'}
                         ,{'erp5_portal_type':'Person'}]
108 109
      }],
    'zone_residence': [{
Sebastien Robin's avatar
Sebastien Robin committed
110
        'erp5_property': 'default_address_region'
Kevin Deldycke's avatar
Kevin Deldycke committed
111 112
      , 'conditions'   : [{'erp5_portal_type':'Organisation'}
                         ,{'erp5_portal_type':'Person'}]
113 114
      }],
    'titre': [{
Sebastien Robin's avatar
Sebastien Robin committed
115
        'erp5_property': 'prefix'
Kevin Deldycke's avatar
Kevin Deldycke committed
116
      , 'conditions'   : {'erp5_portal_type':'Person'}
117 118
      }],
    'telephone': [{
Sebastien Robin's avatar
Sebastien Robin committed
119
        'erp5_property': 'default_telephone_number'
Kevin Deldycke's avatar
Kevin Deldycke committed
120 121
      , 'conditions'   : [{'erp5_portal_type':'Organisation'}
                         ,{'erp5_portal_type':'Person'}]
122 123
      }],
    'telex': [{
Sebastien Robin's avatar
Sebastien Robin committed
124
        'erp5_property': 'default_fax_number'
Kevin Deldycke's avatar
Kevin Deldycke committed
125 126
      , 'conditions'   : [{'erp5_portal_type':'Organisation'}
                         ,{'erp5_portal_type':'Person'}]
127 128
      }],
    'prenom': [{
Sebastien Robin's avatar
Sebastien Robin committed
129
        'erp5_property': 'last_name'
Kevin Deldycke's avatar
Kevin Deldycke committed
130
      , 'conditions'   : {'erp5_portal_type':'Person'}
131 132
      }],
    'date_naissance': [{
Sebastien Robin's avatar
Sebastien Robin committed
133
        'erp5_property': 'birthday'
Kevin Deldycke's avatar
Kevin Deldycke committed
134
      , 'conditions'   : {'erp5_portal_type':'Person'}
135 136
      }],
    'code_bic': [{
Sebastien Robin's avatar
Sebastien Robin committed
137
        'erp5_property': 'bic_code'
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
      , 'conditions'   : {'erp5_portal_type':'Bank Account'}
      }],
    'code_pays': [{
        'erp5_property': 'bank_country_code'
      , 'conditions'   : {'erp5_portal_type':'Bank Account'}
      }],
    'code_etab': [{
        'erp5_property': 'bank_code'
      , 'conditions'   : {'erp5_portal_type':'Bank Account'}
      }],
    'code_guichet': [{
        'erp5_property': 'branch'
      , 'conditions'   : {'erp5_portal_type':'Bank Account'}
      }],
    'numero_compte': [{
        'erp5_property': 'bank_account_number'
      , 'conditions'   : {'erp5_portal_type':'Bank Account'}
      }],
    'rib': [{
        'erp5_property': 'bank_account_key'
      , 'conditions'   : {'erp5_portal_type':'Bank Account'}
      }],
    'numero_interne': [{
        'erp5_property': 'internal_bank_account_number'
      , 'conditions'   : {'erp5_portal_type':'Bank Account'}
163 164
      }],
    'intitule': [{
Sebastien Robin's avatar
Sebastien Robin committed
165
        'erp5_property': 'title'
Kevin Deldycke's avatar
Kevin Deldycke committed
166
      , 'conditions'   : {'erp5_portal_type':'Bank Account'}
167 168
      }],
    'montant_maxi': [{
Sebastien Robin's avatar
Sebastien Robin committed
169
        'erp5_property': 'operation_upper_limit'
Kevin Deldycke's avatar
Kevin Deldycke committed
170
      , 'conditions'   : {'erp5_portal_type':'Agent Privilege'}
171 172
      }],
    'description': [{
Sebastien Robin's avatar
Sebastien Robin committed
173
        'erp5_property': 'description'
Kevin Deldycke's avatar
Kevin Deldycke committed
174
      , 'conditions'   : {'erp5_portal_type':'Agent Privilege'}
175 176
      }],
    'inventory_title': [{
Sebastien Robin's avatar
Sebastien Robin committed
177
        'erp5_property': 'title'
178
      , 'conditions'   : {'erp5_portal_type':'Cash Inventory Group'}
179 180
      }],
    'title': [{
Sebastien Robin's avatar
Sebastien Robin committed
181
        'erp5_property': 'title'
182
      , 'conditions'   : {'erp5_portal_type':'Bank Account Inventory'}
183 184
      }],
    'amount': [{
185
        'erp5_property': 'quantity'
186
      , 'conditions'   : {'erp5_portal_type':'Bank Account Inventory Line'}
187
      }],
Sebastien Robin's avatar
Sebastien Robin committed
188
    'cle': [{
Sebastien Robin's avatar
Sebastien Robin committed
189
        'erp5_property': 'bank_account_key'
Sebastien Robin's avatar
Sebastien Robin committed
190 191
      , 'conditions'   : {'erp5_portal_type':'Bank Account'}
      }],
192
    }
Kevin Deldycke's avatar
Kevin Deldycke committed
193

194 195


Kevin Deldycke's avatar
Kevin Deldycke committed
196 197 198 199 200 201 202
  """
    Methods below are tools to use the property_map.
  """

  security.declarePrivate('buildConditions')
  def buildConditions(self, object):
    """
203 204 205
      Build a condition dictionnary based on the portal type.
      For example it will returns :
      {'erp5_portal_type':'Agent Privilege'}
Kevin Deldycke's avatar
Kevin Deldycke committed
206 207 208 209 210 211 212 213
    """
    dict = {}
    dict['erp5_portal_type'] = object.getPortalType()
    return dict

  security.declarePrivate('findPropertyMapItem')
  def findPropertyMapItem(self, xml_property_name, conditions):
    """
214
      Find the property_map item that match conditions
215 216 217 218
      It will returns for example :
     { 'xml_property' : 'nom'
      , 'erp5_property': 'first_name'
      , 'conditions'   : {'erp5_portal_type':'Person'} }
Kevin Deldycke's avatar
Kevin Deldycke committed
219
    """
Sebastien Robin's avatar
Sebastien Robin committed
220 221 222 223 224 225 226 227 228
    if property_map.has_key(xml_property_name):
      for item in property_map[xml_property_name]:
        c = item['conditions']
        if type(c) == type([]):
          if conditions in c:
            return item
        else:
          if conditions == c:
            return item
Kevin Deldycke's avatar
Kevin Deldycke committed
229 230 231 232 233 234 235
    return None



  security.declareProtected(Permissions.ModifyPortalContent, 'constructContent')
  def constructContent(self, object, object_id, docid, portal_type):
    """
236 237
      This is a redefinition of the original ERP5Conduit.constructContent function to
      create Baobab objects.
238 239

      This method is in charge to create a new object.
Kevin Deldycke's avatar
Kevin Deldycke committed
240
    """
241
    # Register some path in some variables
242 243 244
    erp5_site_path             = object.absolute_url(relative=1)
    person_module_object       = object.person_module
    organisation_module_object = object.organisation_module
245 246 247

    # Modules below are not always required
    #   (it depends of the nature of objects you want to synchronize)
248
    # So if a module to not exist, we set the value to None
249
    try:    cash_inventory_module = object.cash_inventory_module
250
    except AttributeError: cash_inventory_module = None
251
    try:    bank_account_inventory_module = object.bank_account_inventory_module
252
    except AttributeError: bank_account_inventory_module = None
253
    try:    currency_cash_module = object.currency_cash_module
254
    except AttributeError: currency_cash_module  = None
Kevin Deldycke's avatar
Kevin Deldycke committed
255 256 257

    subobject = None

258 259 260
    # Function to search the parent object where the new content must be construct.
    # Given parameter is the special encoded portal type that represent the path to
    #   the wanted destination.
Kevin Deldycke's avatar
Kevin Deldycke committed
261
    def findObjectFromSpecialPortalType(special_portal_type):
262
      # The first part or portal type, for example "Mandataire"
Kevin Deldycke's avatar
Kevin Deldycke committed
263
      source_portal_type = special_portal_type.split('_')[0]
Sebastien Robin's avatar
Sebastien Robin committed
264
      # The place where we should build,
265 266 267 268
      # [1:] is used to takes the full list except the first element
      # [::-1] is used in order to reverse the order
      # construction_location will be for example 40/Z000900001
      # (person with id 40 and account with id Z000900001
Kevin Deldycke's avatar
Kevin Deldycke committed
269 270
      construction_location = '/'.join(special_portal_type.split('_')[1:][::-1])
      parent_object = None
271
      for search_folder in ('person_module', 'organisation_module'):
272
        # full path : /person_module/40/Z000900001
Kevin Deldycke's avatar
Kevin Deldycke committed
273
        path = '/' + search_folder + '/' + construction_location
274
        parent_object_path = erp5_site_path + path
Kevin Deldycke's avatar
Kevin Deldycke committed
275
        try:
276
          # Get the object with the path
277
          parent_object = object.restrictedTraverse(parent_object_path)
Sebastien Robin's avatar
Sebastien Robin committed
278 279
	  if parent_object is not None:
	    break
Yoshinori Okuji's avatar
Yoshinori Okuji committed
280 281
        except ConflictError:
          raise
Kevin Deldycke's avatar
Kevin Deldycke committed
282
        except:
283
          LOG( 'BaobabConduit:'
284
             , 0
Kevin Deldycke's avatar
Kevin Deldycke committed
285 286 287 288
             , "expected %s parent object (%s) not found in %s" % ( source_portal_type
                                                                  , construction_location
                                                                  , search_folder
                                                                  )
289
             )
Kevin Deldycke's avatar
Kevin Deldycke committed
290
      if parent_object == None:
291 292
        LOG( 'BaobabConduit:'
           , 100
Kevin Deldycke's avatar
Kevin Deldycke committed
293
           , "expected %s parent object (%s) not found !" % (source_portal_type, construction_location)
294
           )
Kevin Deldycke's avatar
Kevin Deldycke committed
295
      else:
296 297
        LOG( 'BaobabConduit:'
           , 0
Kevin Deldycke's avatar
Kevin Deldycke committed
298
           , "%s parent object found at %s" % (source_portal_type, parent_object_path)
299
           )
Kevin Deldycke's avatar
Kevin Deldycke committed
300 301
      return parent_object

302
    ### handle client objects
Kevin Deldycke's avatar
Kevin Deldycke committed
303
    if portal_type.startswith('Client'):
304
      # This is a person object
Kevin Deldycke's avatar
Kevin Deldycke committed
305
      if portal_type[-3:] == 'PER':
306 307 308
        subobject = person_module_object.newContent( portal_type = 'Person'
                                                   , id          = object_id
                                                   )
Kevin Deldycke's avatar
Kevin Deldycke committed
309
        subobject.setCareerRole('client')
310
      else: # This is an organisation object
311 312 313
        subobject = organisation_module_object.newContent( portal_type = 'Organisation'
                                                         , id          = object_id
                                                         )
Kevin Deldycke's avatar
Kevin Deldycke committed
314 315
        subobject.setRole('client')

316
    ### handle bank account objects
Kevin Deldycke's avatar
Kevin Deldycke committed
317 318 319 320
    elif portal_type.startswith('Compte'):
      owner = findObjectFromSpecialPortalType(portal_type)
      if owner == None: return None
      subobject = owner.newContent( portal_type = 'Bank Account'
321 322
                                  , id          = object_id
                                  )
Kevin Deldycke's avatar
Kevin Deldycke committed
323 324 325
      # set the bank account owner as agent with no-limit privileges (only for persons)
      if owner.getPortalType() == 'Person':
        new_agent = subobject.newContent( portal_type = 'Agent'
326 327
                                        , id          = 'owner'
                                        )
Kevin Deldycke's avatar
Kevin Deldycke committed
328 329 330 331 332 333 334 335 336 337 338 339
        new_agent.setAgent(owner.getRelativeUrl())
        privileges = ( 'circularization'
                     , 'cash_out'
                     , 'withdrawal_and_payment'
                     , 'account_document_view'
                     , 'signature'
                     , 'treasury'
                     )
        for privilege in privileges:
          new_priv = new_agent.newContent(portal_type = 'Agent Privilege')
          new_priv.setAgentPrivilege(privilege)

340
    ### handle agent objects
Kevin Deldycke's avatar
Kevin Deldycke committed
341
    elif portal_type.startswith('Mandataire'):
342
      # Get the person or organisation thanks to the portal_type
Kevin Deldycke's avatar
Kevin Deldycke committed
343 344 345
      dest = findObjectFromSpecialPortalType(portal_type)
      if dest == None: return None
      subobject = dest.newContent( portal_type = 'Agent'
346 347
                                 , id          = object_id
                                 )
Kevin Deldycke's avatar
Kevin Deldycke committed
348 349 350
      # try to get the agent in the person module
      person = findObjectFromSpecialPortalType('Person_' + object_id)
      if person == None:
351 352 353
        person = person_module_object.newContent( portal_type = 'Person'
                                                , id          = object_id + 'a'
                                                )
Kevin Deldycke's avatar
Kevin Deldycke committed
354 355
      subobject.setAgent(person.getRelativeUrl())

356
    ### handle privilege objects
Kevin Deldycke's avatar
Kevin Deldycke committed
357
    elif portal_type.startswith('Pouvoir'):
358
      # Get the person or organisation thanks to the portal_type
Kevin Deldycke's avatar
Kevin Deldycke committed
359 360 361
      dest = findObjectFromSpecialPortalType(portal_type)
      if dest == None: return None
      subobject = dest.newContent( portal_type = 'Agent Privilege'
362 363 364
                                 , id          = object_id
                                 )

365
    ### handle inventory objects
366 367
    elif portal_type == 'Cash Inventory':
      if cash_inventory_module == None: return None
368
      subobject = cash_inventory_module.newContent( portal_type = 'Cash Inventory Group'
369 370 371
                                                  , id          = object_id
                                                  )

372
    ### handle inventory details objects
373
    elif portal_type == 'Cash Inventory Detail':
374
      if currency_cash_module == None: return None
375
      # get currency and vault informations by analizing the id
376
      id_items = object_id.split('_')
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
      if len(id_items) != 5:
        LOG( 'BaobabConduit:'
           , 100
           , "Cash Inventory Detail object has a wrong id (%s) !" % (object_id)
           )
        return None
      cell_id        = id_items[0]
      agency_code    = id_items[1]
      inventory_code = id_items[2]
      vault_code     = id_items[3]
      currency_id    = id_items[4]
      # get the path to the vault_code
      vault_path = self.getVaultPathFromCodification( object         = object
                                                    , agency_code    = agency_code
                                                    , inventory_code = inventory_code
                                                    , vault_code     = vault_code
393
                                                    , currency_id    = currency_id
394 395 396 397 398 399
                                                    )
      if vault_path in (None, ''):
        LOG( 'BaobabConduit:'
           , 100
           , "can't find a path to the vault '%s/%s/%s' !" % (agency_code, inventory_code, vault_code)
           )
400
        return None
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
      # try to find an existing inventory with the same price currency and vault
      inventory_list = object.contentValues(filter={'portal_type': 'Cash Inventory'})
      new_inventory = None
      for inventory in inventory_list:
        inventory_currency = inventory.getPriceCurrencyId()
        inventory_vault    = inventory.getDestination()
        if inventory_currency not in (None, '') and \
           inventory_vault    not in (None, '') and \
           inventory_currency == currency_id    and \
           inventory_vault    == vault_path     :
          new_inventory = inventory
          LOG( 'BaobabConduit:'
             , 0
             , "previous Cash Inventory found (%s) !" % (repr(new_inventory))
             )
          break
      # no previous inventory found, create one
      if new_inventory == None:
        new_inventory = object.newContent(portal_type = 'Cash Inventory')
Kevin Deldycke's avatar
Kevin Deldycke committed
420
        new_inventory.setPriceCurrency('currency_module/' + currency_id)
421 422 423
        new_inventory.setDestination(vault_path)
      subobject = new_inventory

424 425 426 427 428 429 430 431 432 433 434 435 436
    ### handle bank account inventory objects
    elif portal_type == 'Bank Account Inventory':
      if bank_account_inventory_module == None: return None
      subobject = bank_account_inventory_module.newContent( portal_type = 'Bank Account Inventory'
                                                          , id          = object_id
                                                          )

    ### handle bank account inventory line objects
    elif portal_type == 'Bank Account Inventory Line':
      subobject = object.newContent( portal_type = 'Bank Account Inventory Line'
                                   , id          = object_id
                                   )

437 438 439
    return subobject


440

441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
  security.declareProtected(Permissions.ModifyPortalContent, 'editDocument')
  def editDocument(self, object=None, **kw):
    """
      This function transfer datas from the dictionary to the baobab document
      object given in parameters.
    """

    if object == None: return

    ### Cash Inventory objects needs two properties to generate the vault path
    if object.getPortalType() == 'Cash Inventory Group':
      vault_path = self.getVaultPathFromCodification( object         = object
                                                    , agency_code    = kw['agency_code']
                                                    , inventory_code = kw['inventory_code']
                                                    )
      object.setDestination(vault_path)

    ### Cash Inventory Detail objects needs all properties to create and update the cell matrix
459
    # This part is only usefull for cash inventory, it is not used for most portal types
460
    if object.getPortalType() == 'Cash Inventory':
461
      # Make sure all variables will be defined
462 463 464 465 466 467 468 469 470 471 472 473
      quantity      = None
      cell_id       = None
      resource_type = None
      base_price    = None
      currency_name = None
      for k,v in kw.items():
        if k == 'quantity'     : quantity      = float(v)
        if k == 'cell_id'      : cell_id       = v
        if k == 'currency_type': resource_type = v
        if k == 'price'        : base_price    = float(v)
        if k == 'currency'     : currency_name = v
      # try to find an existing line with the same resource as the current cell
474 475 476 477 478
      if resource_type in ['BIL']:
        currency_portal_type = 'Banknote'
      elif resource_type in ['MON']:
        currency_portal_type = 'Coin'
      else:
479 480 481 482
        LOG( 'BaobabConduit:'
           , 100
           , "Cash Inventory Detail resource type can't be guess (%s) !" % (resource_type)
           )
483 484 485
        return None
      # get the list of existing currency to find the currency of the line
      line_currency_cash = None
486
      currency_cash_list = object.currency_cash_module.contentValues(filter={'portal_type': currency_portal_type})
487
      for currency_cash in currency_cash_list:
488 489
        # Check the price_currency_id and the base price to make sure
        # we have the right currency
490 491 492 493 494 495
        if base_price    not in (None, '')                    and \
           currency_name not in (None, '')                    and \
           currency_cash.getBasePrice()       == base_price   and \
           currency_cash.getPriceCurrencyId() == currency_name:
          line_currency_cash = currency_cash
          break
496 497
      # no currency found
      if line_currency_cash == None:
498 499 500 501
        LOG( 'BaobabConduit:'
           , 100
           , "Currency '%s %s' not found for the Cash Inventory Detail !" % (base_price, currency_name)
           )
502
        return None
503
      # We are looking for an existing line
504 505 506
      inventory_lines = object.contentValues(filter={'portal_type': 'Cash Inventory Line'})
      new_line = None
      for line in inventory_lines:
507 508 509
        # getResourceValue returns the currency_cash, so if it is
        # equivalent to the currency_cash we have found, then we can
        # update the line
510 511 512 513 514
        if line.getResourceValue() == line_currency_cash:
          new_line = line
          break
      # no previous line found, create one
      if new_line == None:
515
        new_line = object.newContent(portal_type = 'Cash Inventory Line')
516 517
        new_line.setResourceValue(line_currency_cash)
        new_line.setPrice(line_currency_cash.getBasePrice())
518
      # get matrix variation values
519
      category_list = []
520
      # This is the 3 variation axes of the matrix
521 522 523 524 525 526 527 528 529 530 531 532 533 534
      base_cat_map = { 'variation'  : 'variation'
                     , 'letter_code': 'emission_letter'
                     , 'status_code': 'cash_status'
                     }
      for base_key in base_cat_map.keys():
        if base_key in kw.keys() and kw[base_key] not in ('', None):
          if base_key == 'status_code':
            status_table = { 'TVA' : 'valid'
                           , 'NEE' : 'new_emitted'
                           , 'NEU' : 'new_not_emitted'
                           , 'RTC' : 'retired'
                           , 'ATR' : 'to_sort'
                           , 'MUT' : 'mutilated'
                           , 'EAV' : 'to_ventilate'
535
                           , 'ANN' : 'cancelled'
536 537 538 539 540 541
                           }
            category = status_table[kw[base_key]]
          else:
            category = kw[base_key]
        else:
          category = 'not_defined'
542
        # We must have at least a category for each axis
543
        category_list.append(base_cat_map[base_key] + '/' + category)
544 545
      # update the matrix with this cell
      self.updateCashInventoryMatrix( line               = new_line
546 547
                                    , cell_category_list = category_list
                                    , quantity           = quantity
548
                                    , cell_uid           = cell_id
549 550
                                    )

551
    ### Bank Account Inventory Line objects needs two properties to get the right bank account object
552
    # This part is only usefull for bank account inventory line, it is not used for most portal types
553
    if object.getPortalType() == 'Bank Account Inventory Line':
554
      # Make sure variables will be defined
555 556 557 558 559 560
      currency_id         = None
      bank_account_number = None
      for k,v in kw.items():
        if k == 'currency'      : currency_id         = v
        if k == 'account_number': bank_account_number = v
      # try to find the bank account
Sebastien Robin's avatar
Sebastien Robin committed
561 562 563 564
      LOG( 'bank_account_number:'
                 , 200
                 , bank_account_number
                 )
565 566
      if bank_account_number != None:
        bank_account_object = None
567 568 569 570 571
        # We use here the catalog in order to find very quickly
        # all bank with a particular reference, so most of the time
        # we should get only 1 bank account
        bank_account_list = [x.getObject() for x in object.portal_catalog(
                               portal_type=('Bank Account'),
572
                               )]
Sebastien Robin's avatar
Sebastien Robin committed
573 574 575 576
        LOG( 'bank_account_list:'
                 , 200
                 , bank_account_list
                 )
577 578
        # Make sure we have found the right bank account
        for bank_account in bank_account_list:
579
           if bank_account.getInternalBankAccountNumber() == bank_account_number:
580 581
             bank_account_object = bank_account
             break
582
        if bank_account_object != None:
583
          # Se the right account on the inventory line
584
          object.setDestinationValue(bank_account_object)
585
          object.setDestinationPaymentValue(bank_account_object)
586 587 588
          if currency_id != None:
            # verify or add the currency
            current_currency_id = bank_account_object.getPriceCurrencyId()
589
            # Make sure that the bank account will have a currency defined
590
            if current_currency_id in (None, ''):
Kevin Deldycke's avatar
Kevin Deldycke committed
591
              bank_account_object.setPriceCurrency('currency_module/' + currency_id)
592 593 594 595 596
            elif current_currency_id != currency_id:
              LOG( 'BaobabConduit inconsistency:'
                 , 200
                 , 'found bank account has not the same currency as expected'
                 )
597 598 599 600 601
        else:
            LOG( 'BaobabConduit inconsistency:'
               , 200
               , 'no bank account found'
               )
602 603
            import pdb;pdb.set_trace()
            raise KeyError, 'No bank account Found'
604

605 606 607 608 609 610 611 612 613

    """
      Here we use 2 generic way to update object properties :
        1. We try to use the property_map mapping to migrate a value from a property
             to another;
        2. If the latter fail, we try to find a method with a pre-defined name in
             this script to handle the value.
    """

Kevin Deldycke's avatar
Kevin Deldycke committed
614 615 616 617 618 619
    # Set properties of the destination baobab object
    for k,v in kw.items():
      # Try to find a translation rule in the property_map
      cond = self.buildConditions(object)
      map_item = self.findPropertyMapItem(k, cond)

620 621
      ### There is a translation rule, so call the right setProperty() method
      if map_item != None:
622
        # The method id can be for example 'setTitle'
Kevin Deldycke's avatar
Kevin Deldycke committed
623
        method_id = "set" + convertToUpperCase(map_item['erp5_property'])
624 625 626 627
        LOG( 'BaobabConduit:'
           , 0
           , "try to call object method %s on %s" % (repr(method_id), repr(object))
           )
Kevin Deldycke's avatar
Kevin Deldycke committed
628
        if v not in ('', None):
629
           # We look if the method exist
Kevin Deldycke's avatar
Kevin Deldycke committed
630
          if hasattr(object, method_id):
631
            # get the method itself
Kevin Deldycke's avatar
Kevin Deldycke committed
632
            method = getattr(object, method_id)
633
            # This call the method, this exactly the same thing
Sebastien Robin's avatar
Sebastien Robin committed
634
            # as calling directly : object.setTitle(v)
Kevin Deldycke's avatar
Kevin Deldycke committed
635 636
            method(v)
          else:
637 638 639 640 641 642 643
            LOG( 'BaobabConduit:'
               , 100
               , 'property map item don\'t match object properties'
               )

      ### No translation rule found, try to find a hard-coded translation method in the conduit
      else:
644 645 646 647 648 649 650
        # The method is generated with the type of the document and with the
        # name of the property. If the type of the document is 'Client' and the
        # property is nature_economique, then it will try to find a method
        # defined in this conduit 'editClientNatureEconomique'. This is very
        # usefull if we must do some particular conversion or some calculation
        # before editing an object. This is used when there is no simple
        # equivalent between sql table and ERP5.
651
        method_id = "edit%s%s" % (kw['type'].replace(' ', ''), convertToUpperCase(k))
652 653 654 655 656 657
        LOG( 'BaobabConduit:'
           , 0
           , "try to call conduit method %s on %s" % (repr(method_id), repr(object))
           )
        if v not in ('', None):
          if hasattr(self, method_id):
658
            # get the method itself
659
            method = getattr(self, method_id)
660
            # This call the method, this exactly the same thing
Sebastien Robin's avatar
Sebastien Robin committed
661
            # as calling directly : self.editClientNatureEconomique(object,v)
662 663 664 665 666 667 668
            method(object, v)
          else:
            LOG( 'BaobabConduit:'
               , 100
               , "there is no method to handle <%s>%s</%s> data" % (k,repr(v),k)
               )

Kevin Deldycke's avatar
Kevin Deldycke committed
669 670 671 672



  """
673 674 675 676
    All functions below are defined to set a document's property to a value
    given in parameters.
    The name of those functions are chosen to help the transfert of datas
    from a given XML format to standard Baobab objects.
Kevin Deldycke's avatar
Kevin Deldycke committed
677 678
  """

679 680
  ### Client-related-properties functions

Kevin Deldycke's avatar
Kevin Deldycke committed
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
  def editClientCategorie(self, document, value):
    if document.getPortalType() == 'Organisation':
      id_table = { 'BIF': 'institution/world/bank'
                 , 'PFR': 'institution/world/institution'
                 , 'ICU': 'institution/local/common'
                 , 'BET': 'institution/local/institution'
                 , 'ETF': 'institution/local/bank'
                 , 'BTR': 'treasury/national'
                 , 'ORP': 'treasury/other'
                 , 'ORI': 'organism/international'
                 , 'ORR': 'organism/local'
                 , 'COR': 'intermediaries'
                 , 'DIV': 'depositories/various'
                 , 'DER': 'depositories/savings'
                 , 'DAU': 'depositories/other'
                 }
697 698
      #document.setActivity('banking_finance/' + id_table[value])
      document.setActivity('banking_finance/' + value)
Kevin Deldycke's avatar
Kevin Deldycke committed
699
    else:
700
      LOG('BaobabConduit:', 0, 'Person\'s category ignored')
Kevin Deldycke's avatar
Kevin Deldycke committed
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716

  def editClientNatureEconomique(self, document, value):
    if document.getPortalType() == 'Organisation':
      # build the economical class category path
      c = ''
      path = ''
      for i in value[1:]:
        c += i
        if c == '13':
          path += '/S13'
          if value != 'S13':
            path += '/' + value
          break
        path += '/S' + c
      document.setEconomicalClass(path)
    else:
717 718 719 720
      LOG( 'BaobabConduit inconsistency:'
         , 200
         , 'a non-Organisation client can\'t have an economical class'
         )
Kevin Deldycke's avatar
Kevin Deldycke committed
721 722

  def editClientSituationMatrimoniale(self, document, value):
723 724 725 726
    """
    Here we can convert data from sql to data in ERP5 thanks
    to a simple dictionnary: the id_table.
    """
Kevin Deldycke's avatar
Kevin Deldycke committed
727 728 729 730 731 732 733 734
    if document.getPortalType() == 'Person':
      id_table = { 'VEU' : 'widowed'
                 , 'DIV' : 'divorced'
                 , 'MAR' : 'married'
                 , 'CEL' : 'never_married'
                 }
      document.setMaritalStatus(id_table[value])
    else:
735 736 737 738
      LOG( 'BaobabConduit inconsistency:'
         , 200
         , 'a non-Person client can\'t have a marital status'
         )
Kevin Deldycke's avatar
Kevin Deldycke committed
739

Sebastien Robin's avatar
Sebastien Robin committed
740 741
  def editClientCode(self, document, value):
    pass
Sebastien Robin's avatar
Sebastien Robin committed
742 743


Kevin Deldycke's avatar
Kevin Deldycke committed
744 745


746 747
  ### BankAccount-related-properties functions

Kevin Deldycke's avatar
Kevin Deldycke committed
748
  def editCompteDevise(self, document, value):
749
    # Convert compte_devise to price_currency
Kevin Deldycke's avatar
Kevin Deldycke committed
750
    document.setPriceCurrency('currency_module/' + value)
Kevin Deldycke's avatar
Kevin Deldycke committed
751

752 753 754 755 756 757 758 759 760 761 762 763
  def editCompteOverdraftFacility(self, document, value):
    new_value = False
    if value=='O':
      new_value = True
    document.setOverdraftFacility(new_value)

  def editCompteSwiftRegistered(self, document, value):
    new_value = False
    if value=='O':
      new_value = True
    document.setSwiftRegistered(new_value)

Kevin Deldycke's avatar
Kevin Deldycke committed
764
  def editCompteDateOuverture(self, document, value):
765
    # Convert date_ouverture to start_date and stop_date
Kevin Deldycke's avatar
Kevin Deldycke committed
766 767 768 769 770
    if document.getStopDate() in ('', None):
      document.setStopDate(str(datetime.datetime.max))
    document.setStartDate(value)

  def editCompteDateFermeture(self, document, value):
771
    # Convert date_firemeture to start_date and stop_date
Kevin Deldycke's avatar
Kevin Deldycke committed
772 773 774 775
    if document.getStartDate() in ('', None):
      document.setStartDate(str(datetime.datetime.min))
    document.setStopDate(value)

776 777
  ### Agent-related-properties functions

Kevin Deldycke's avatar
Kevin Deldycke committed
778
  def editMandataireNom(self, document, value):
779
    # Convert mandataire_nom to first_name
Kevin Deldycke's avatar
Kevin Deldycke committed
780 781 782
    old_value = document.getAgentValue().getFirstName()
    new_value = value
    if old_value != new_value:
783 784 785 786
      LOG( 'BaobabConduit:'
         , 200
         , 'old value of agent first name (%s) was replaced by a new one (%s)' % (old_value, new_value)
         )
Kevin Deldycke's avatar
Kevin Deldycke committed
787 788 789
      document.getAgentValue().setFirstName(new_value)

  def editMandatairePrenom(self, document, value):
790
    # Convert mandataire_prenom to last_name
Kevin Deldycke's avatar
Kevin Deldycke committed
791 792 793
    old_value = document.getAgentValue().getLastName()
    new_value = value
    if old_value != new_value:
794 795 796 797
      LOG( 'BaobabConduit:'
         , 200
         , 'old value of agent last name (%s) was replaced by a new one (%s)' % (old_value, new_value)
         )
Kevin Deldycke's avatar
Kevin Deldycke committed
798 799 800
      document.getAgentValue().setLastName(new_value)

  def editMandataireService(self, document, value):
801
    # Convert mandataire_service to an assignment
Kevin Deldycke's avatar
Kevin Deldycke committed
802
    assignment = document.getAgentValue().newContent( portal_type = 'Assignment'
803 804
                                                    , id          = 'service'
                                                    )
Kevin Deldycke's avatar
Kevin Deldycke committed
805 806 807 808
    assignment.setGroup(value)
    return

  def editMandataireFonction(self, document, value):
809
    # Convert mandataire_function to a career grade
810
    document.getAgentValue().setCareerGrade(value)
Kevin Deldycke's avatar
Kevin Deldycke committed
811 812 813
    return

  def editMandataireTelephone(self, document, value):
814
    # Convert mandataire_telephone to default_telephone_number
Kevin Deldycke's avatar
Kevin Deldycke committed
815 816 817
    old_value = document.getAgentValue().getDefaultTelephoneNumber()
    new_value = value
    if old_value != new_value:
818 819 820 821
      LOG( 'BaobabConduit:'
         , 200
         , "old value of agent's telephone (%s) was replaced by a new one (%s)" % (old_value, new_value)
         )
Kevin Deldycke's avatar
Kevin Deldycke committed
822 823 824
      document.getAgentValue().setDefaultTelephoneNumber(new_value)

  def editMandataireDateCreation(self, document, value):
825
    # Convert mandataire_date_creation to stop_date and start_date
Kevin Deldycke's avatar
Kevin Deldycke committed
826 827 828 829 830 831
    if document.getStopDate() in ('', None):
      document.setStopDate(str(datetime.datetime.max))
    document.setStartDate(value)



832 833
  ### AgentPrivilege-related-properties functions

Kevin Deldycke's avatar
Kevin Deldycke committed
834
  def editPouvoirCategorie(self, document, value):
835
    # Convert pouvoir_categorie to agent_privilege property
Kevin Deldycke's avatar
Kevin Deldycke committed
836 837 838 839 840 841 842 843 844 845 846
    id_table = { 'COM' : 'clearing'
               , 'CIR' : 'circularization'
               , 'REM' : 'cash_out'
               , 'RET' : 'withdrawal_and_payment'
               , 'RTE' : 'account_document_view'
               , 'SIG' : 'signature'
               , 'TRE' : 'treasury'
               }
    document.setAgentPrivilege(id_table[value])

  def editPouvoirDateDebut(self, document, value):
847
    # Convert pouvoir_date_debut to start_date and stop_date properties
Kevin Deldycke's avatar
Kevin Deldycke committed
848 849 850 851 852
    if document.getStopDate() in ('', None):
      document.setStopDate(str(datetime.datetime.max))
    document.setStartDate(value)

  def editPouvoirDateFin(self, document, value):
853
    # Convert pouvoir_date_fin to start_date and stop_date properties
Kevin Deldycke's avatar
Kevin Deldycke committed
854 855
    if document.getStartDate() in ('', None):
      document.setStartDate(str(datetime.datetime.min))
856 857 858 859
    document.setStopDate(value)



860 861
  ### CashInventory-related-properties functions

862
  def editCashInventoryInventoryDate(self, document, value):
863
    # Convert cash_inventory_inventory_date to stop_date property
864 865 866 867 868 869 870 871 872
    if value in ('', None):
      date = str(datetime.datetime.max)
    else:
      # Convert french date to strandard date
      date_items = value.split('/')
      day   = date_items[0]
      month = date_items[1]
      year  = date_items[2]
      date  = '/'.join([year, month, day])
873 874
    document.setStopDate(date)

875
  def getVaultPathFromCodification( self, object, agency_code=None, inventory_code=None, vault_code=None, currency_id=None):
876 877 878 879 880 881 882
    """
    This method get many parameters and try to find a category
    corresponding with parameters.

    For example if agency_code=A00, this function will returns
    site/aaa/bbb/ccc
    """
883 884 885 886 887 888
    if agency_code in (None, ''):
      return None
    category_tool = object.portal_categories
    # Get the site path to agency
    agency_path = None
    site_base_object = category_tool.resolveCategory('site')
889 890 891 892 893 894
    # XXX Warning, we should use the catalog in order to retrieve this
    # first level. It will go faster. But we need the codification in
    # the catalog table

    # Parse the category tree in order to find the category corresponding
    # to the agency
895 896 897 898
    for site_item in site_base_object.Delivery_getVaultItemList(
                 vault_type=('site',),
                 strict_membership=1,leaf_node=0,
                 user_site=0,with_base=1)[1:]:
899 900 901 902 903 904 905
      site_path = site_item[1]
      site_object = category_tool.resolveCategory(site_path)
      if site_object.getPortalType() == 'Category':
        site_code = site_object.getCodification()
        if site_code not in (None, '') and site_code.upper() == agency_code.upper():
          agency_path = site_path
          break
906
    #import pdb;pdb.set_trace()
907 908 909 910 911
    if inventory_code in (None, ''):
      return agency_path
    # Get the site path corresponding to the inventory type
    inventory_path = None
    agency_site_object = site_object
Sebastien Robin's avatar
Sebastien Robin committed
912
    # Parse the category tree (from the level of the agency) in order to
913 914
    # find the category corresponding to the inventory
    for agency_sub_item in agency_site_object.getCategoryChildItemList(base=1)[1:]:
915 916
      agency_sub_item_path   = agency_sub_item[1]
      agency_sub_item_object = category_tool.resolveCategory(agency_sub_item_path)
917 918 919 920 921 922 923 924
      agency_sub_item_vault  = agency_sub_item_object.getVaultType()
      if agency_sub_item_vault not in (None, ''):
        vault_type_path        = 'vault_type/' + agency_sub_item_vault
        vault_type_object      = category_tool.resolveCategory(vault_type_path)
        vault_type_code        = vault_type_object.getCodification()
        if vault_type_code not in (None, '') and vault_type_code.upper() == inventory_code.upper():
          inventory_path = agency_sub_item_path
          break
925 926 927 928 929
    if vault_code in (None, ''):
      return inventory_path
    # Get the site path corresponding to the vault code
    vault_path = None
    vault_site_object = agency_sub_item_object
Sebastien Robin's avatar
Sebastien Robin committed
930
    # Parse the category tree (from the level of the inventory) in order to
931
    # find the category corresponding to the vault
Sebastien Robin's avatar
Sebastien Robin committed
932
    for vault_sub_item in vault_site_object.getCategoryChildItemList(base=1)[1:]:
933 934 935 936 937 938
      vault_sub_item_path   = vault_sub_item[1]
      vault_sub_item_object = category_tool.resolveCategory(vault_sub_item_path)
      vault_sub_item_code   = vault_sub_item_object.getCodification()
      if vault_sub_item_code not in (None, '') and vault_sub_item_code.upper() == vault_code.upper():
        vault_path = vault_sub_item_path
        break
939 940 941
    if currency_id in (None, ''):
      return vault_path
    # Get the site path corresponding to the currency-related-subvault
Kevin Deldycke's avatar
Kevin Deldycke committed
942
    currency_object = category_tool.currency_module[currency_id]
943 944 945
    currency_title  = currency_object.getTitle()
    currency_vault_path = None
    vault_object = vault_sub_item_object
Sebastien Robin's avatar
Sebastien Robin committed
946
    # Parse the category tree (from the level of the vault) in order to
947 948
    # find the category corresponding to the currency
    for currency_vault_item in vault_object.getCategoryChildItemList(base=1)[1:]:
949 950 951 952 953 954 955 956 957
      currency_vault_item_path   = currency_vault_item[1]
      currency_vault_item_object = category_tool.resolveCategory(currency_vault_item_path)
      currency_vault_item_title  = currency_vault_item_object.getTitle()
      if currency_vault_item_title not in (None, '') and currency_vault_item_title.upper() == currency_title.upper():
        currency_vault_path = currency_vault_item_path
        break
    if currency_vault_path == None:
      return vault_path
    return currency_vault_path
958 959 960 961



  ### CashInventoryDetail-related-properties functions
962

963
  def updateCashInventoryMatrix(self, line, cell_category_list, quantity, cell_uid):
964
    base_id = 'movement'
965
    base_category_list = [ 'emission_letter'
966
                         , 'variation'
967
                         , 'cash_status'
968
                         ]
969 970 971 972 973 974 975 976 977 978

    old_line_category_list   = line.getVariationCategoryList()
    messy_line_category_list = cell_category_list + old_line_category_list

    sorted_line_base_category_list = []
    sorted_line_category_list = []
    sorted_cell_category_list = []
    sorted_cell_range = []

    # cell_category_list must have the same base category order of cell_range base category
979
    for base_category in base_category_list:
980 981 982 983 984 985 986

      # generate the sorted line categories
      for category in messy_line_category_list:
        if category.startswith(base_category + '/') and category not in sorted_line_category_list:
          sorted_line_category_list.append(category)

      # generate the sorted cell range
987
      base_group = []
988
      for category in messy_line_category_list:
989 990
        if category.startswith(base_category + '/') and category not in base_group:
          base_group.append(category)
991 992
      sorted_cell_range.append(base_group)
      # generate the sorted base category
993
      if len(base_group) > 0:
994 995 996 997 998 999 1000 1001 1002 1003 1004
        sorted_line_base_category_list.append(base_category)

      # generate the sorted cell variation categories
      for category in cell_category_list:
        if category.startswith(base_category + '/') and category not in sorted_cell_category_list:
          sorted_cell_category_list.append(category)

    # update line variation categories
    line.setVariationBaseCategoryList(sorted_line_base_category_list)
    line.setVariationCategoryList(sorted_line_category_list)
    line.setCellRange(base_id = base_id, *sorted_cell_range)
1005 1006 1007 1008
    # create the cell
    kwd = { 'base_id'    : base_id
          , 'portal_type': 'Cash Inventory Cell'
          }
1009
    new_cell = line.newCell(*sorted_cell_category_list, **kwd)
1010 1011 1012
    new_cell.edit( mapped_value_property_list         = ('price', 'inventory')
                 , force_update                       = 1
                 , inventory                          = quantity
1013 1014
                 , membership_criterion_category_list = sorted_cell_category_list
                 , category_list                      = sorted_cell_category_list
1015
                 , title                              = cell_uid
1016 1017 1018 1019 1020 1021 1022
                 )



  ### BankAccountInventory-related-properties functions

  def editBankAccountInventoryAgencyCode(self, document, value):
1023
    # Convert bank_account_inventory_agency_code to a destination
1024 1025 1026 1027 1028 1029
    agency_path = self.getVaultPathFromCodification( object      = document
                                                   , agency_code = value
                                                   )
    document.setDestination(agency_path)

  def editBankAccountInventoryDate(self, document, value):
1030
    # Convert bank_account_inventory_date to stop_date property
1031 1032 1033 1034 1035 1036 1037 1038 1039
    if value in ('', None):
      date = str(datetime.datetime.max)
    else:
      # Convert french date to strandard date
      date_items = value.split('/')
      day   = date_items[0]
      month = date_items[1]
      year  = date_items[2]
      date  = '/'.join([year, month, day])
Yoshinori Okuji's avatar
Yoshinori Okuji committed
1040
    document.setStopDate(date)
Sebastien Robin's avatar
Sebastien Robin committed
1041

1042 1043 1044 1045 1046
  def editBankAccountInventoryLineCurrency(self, document, value):
    # Convert bank_account_inventory_date to stop_date property
    resource_url = 'currency_module/%s' % value
    resource_value = document.getPortalObject().restrictedTraverse(resource_url)
    document.setResourceValue(resource_value)