OscommerceERP5TransactionConduit.py 15.3 KB
Newer Older
1
# -*- coding: utf-8 -*-
Aurel's avatar
Aurel committed
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
##############################################################################
#
# Copyright (c) 2009 Nexedi SA and Contributors. All Rights Reserved.
#               Hervé Poulain <herve@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.ERP5TioSafe.Conduit.TioSafeBaseConduit import TioSafeBaseConduit
from Products.ERP5SyncML.SyncMLConstant import XUPDATE_INSERT_OR_ADD_LIST
from base64 import b16encode
from zLOG import LOG, WARNING
from copy import deepcopy

class OscommerceERP5TransactionConduit(TioSafeBaseConduit):
  """
    This is the conduit use to synchonize tiosafe sale orders and ERP5
  """
  def __init__(self):
    # Define the object_tag element to add object
    self.xml_object_tag = 'transaction'


  def updateNode(self, xml=None, object=None, previous_xml=None, force=0,
      simulate=0,  **kw):
    raise Exception('updateNode: Impossible to update transaction')


  def deleteNode(self, xml=None, object=None, previous_xml=None, force=0,
      simulate=0,  **kw):
    raise Exception('deleteNode: Impossible to delete transaction')


  def _createContent(self, xml=None, object=None, object_id=None, sub_object=None,
      reset_local_roles=0, reset_workflow=0, simulate=0, **kw):
    """
      This is the method calling to create an object
    """
    if object_id is None:
      object_id = self.getAttribute(xml, 'id')
    if object_id is not None:
      if sub_object is None:
        try:
          sub_object = object._getOb(object_id)
        except (AttributeError, KeyError, TypeError):
          sub_object = None
      if sub_object is None: # If so, it doesn't exist
        portal_type = ''
        if xml.xpath('local-name()') == self.xml_object_tag:
          portal_type = self.getObjectType(xml)
        elif xml.xpath('name()') in XUPDATE_INSERT_OR_ADD_LIST: # Deprecated ???
          portal_type = self.getXupdateContentType(xml) # Deprecated ???
        sub_object, reset_local_roles, reset_workflow = self.constructContent(
            object,
            object_id,
            portal_type,
        )

        # Define the trade condition by using the one defined on IS
        integration_site = self.getIntegrationSite(kw.get('domain'))
        if portal_type == "Sale Order":
          sub_object.setSpecialise(integration_site.getSourceTrade())
          sub_object.SaleOrder_applySaleTradeCondition()
        # Mapping between tag and element
        node_dict = {
            'arrow': self.visitArrow,
            'movement': self.visitMovement,
        }
        # if exist namespace retrieve only the tag
        index = 0
        if xml.nsmap not in [None, {}]:
          index = -1

        # Browse the list of arrows and movements
        for node in xml.getchildren():
          # Only works on right tags, and no on the comments, ...
          if type(node.tag) is not str:
            continue
          # Build the slipt list of a tag to test
          split_tag = node.tag.split('}')
          # Build the tag (without is Namespace)
          tag = node.tag.split('}')[index]
          # Treat sub-element
          if len(node.getchildren()):
            if tag in node_dict:
              node_dict[tag](document=sub_object, xml=node, **kw)
            else:
              raise ValueError, "This is an unknown sub-element %s on %s" %(tag, sub_object.getPath())
          if tag == 'currency':
            link_object = object.portal_catalog.getResultValue(
                portal_type='Currency',
                reference=node.text.encode('utf-8'),
            )
            sub_object.setPriceCurrencyValue(link_object)
          elif tag in ['start_date', 'stop_date']:
            if not node.text:
              node.text = None
          elif tag  == "payment_mode":
            sub_object.setPaymentConditionPaymentMode(node.text)

      # Build the content of new Sale Order
      self.newObject(
          object=sub_object,
          xml=xml,
          simulate=simulate,
          reset_local_roles=reset_local_roles,
          reset_workflow=reset_workflow,
      )
    return sub_object


  def afterNewObject(self, object):
    """ Confirm the sale order and, add the grants on this one. """
    if object.getPortalType() in ['Sale Order',]:
      object.confirm()
    object.updateLocalRolesOnSecurityGroups()


  def visitArrow(self, document=None, xml=None, **kw):
    """ Manage the addition of sources and destination in the Sale Orders. """
    # if exist namespace retrieve only the tag
    index = 0
    if xml.nsmap not in [None, {}]:
      index = -1

    # retrieve the integration site
    domain = kw.get('domain')
    integration_site = self.getIntegrationSite(domain
)
    # use the setters of source and destination in terms of the category
    sync_object_list = self.getSynchronizationObjectListForType(kw.get('domain'), 'Person', 'publication') + \
                       self.getSynchronizationObjectListForType(kw.get('domain'), 'Organisation', 'publication')
    category = xml.get('type')
    if category != "":
      if category == 'Ownership':
        arrow_dict = {
        'source': {
          'synchronization': sync_object_list,
          'setter': document.setSourceSectionValue,
          'free_text_setter': document.setSourceSectionFreeText},
        'destination': {
          'synchronization': sync_object_list,
          'setter': document.setDestinationSectionValue,
          'free_text_setter': document.setDestinationSectionFreeText},
          }
      elif category == 'Payment':
        arrow_dict = {
        'source': {
          'synchronization': sync_object_list,
          'setter': document.setSourcePaymentValue,
          'free_text_setter': document.setSourcePaymentFreeText},
        'destination': {
          'synchronization': sync_object_list,
          'setter': document.setDestinationPaymentValue,
          'free_text_setter': document.setDestinationPaymentFreeText},
        }
      elif category == 'Administration':
        arrow_dict = {
        'source': {
          'synchronization': sync_object_list,
          'setter': document.setSourceAdministrationValue,
          'free_text_setter': document.setSourceAdministrationFreeText},
        'destination': {
          'synchronization': sync_object_list,
          'setter': document.setDestinationAdministrationValue,
          'free_text_setter': document.setDestinationAdministrationFreeText},
        }
      elif category == 'Decision':
        arrow_dict = {
        'source': {
          'synchronization': sync_object_list,
          'setter': document.setSourceDecisionValue,
          'free_text_setter': document.setSourceDecisionFreeText},
        'destination': {
          'synchronization': sync_object_list,
          'setter': document.setDestinationDecisionValue,
          'free_text_setter': document.setDestinationDecisionFreeText},
        }
      else:
        raise Exception('visitArrow: Unexpected Category %s' %(category))
    else:
      arrow_dict = {
      'source': {
        'synchronization': sync_object_list,
        'setter': document.setSourceValue,
        'free_text_setter': document.setSourceFreeText},
      'destination': {
        'synchronization': sync_object_list,
        'setter': document.setDestinationValue,
        'free_text_setter': document.setDestinationFreeText},
      }

    # browse the xml and save the source and destination in the sale order
    integration_site = self.getIntegrationSite(kw.get('domain'))
    default_source = integration_site.getSourceAdministrationValue()
    default_org_gid = integration_site.organisation_module.getSourceSectionValue().getGidFromObject(default_source, encoded=False)
    default_node = integration_site.getDestinationValue()
    default_node_gid = integration_site.person_module.getSourceSectionValue().getGidFromObject(default_node, encoded=False)

    for subnode in xml.getchildren():
      # only works on tags, no on the comments or other kind of tag
      if type(subnode.tag) is not str:
        continue
      tag = subnode.tag.split('}')[index]

      if tag in arrow_dict:
        # retrieve the corresponding
        synchronization_list = arrow_dict[tag]['synchronization']
        link_object = None
        link_gid = subnode.text.encode('utf-8')
        if link_gid == default_org_gid:
          link_object = default_source
        else:
          for synchronization in synchronization_list:
            # encode to the output type
            if getattr(synchronization, 'getObjectFromGid', None) is not None:
              link_object = synchronization.getObjectFromGid(b16encode(link_gid))
              #LOG("trying to get %s from %s, got %s" %(link_gid, synchronization.getPath(), link_object), 300, "This is for category type %s" %(category))
              if link_object is not None:
                break

        if link_object is None:
          arrow_dict[tag]['free_text_setter'](link_gid)
          link_object = default_node

        # set person to the sale order
        LOG("AUREL", 300, "will set %s on %s to value %s" %(tag, document, link_object))
        arrow_dict[tag]['setter'](link_object)


  def visitMovement(self, document=None, xml=None, **kw):
    """ Manage the addition of the Sale Order Line. """

    # dictionary of the value of a movement
    movement_dict_value = {'category': []}

    # marker for checking property existency
    MARKER = object()

    # if exist namespace retrieve only the tag
    index = 0
    if xml.nsmap not in [None, {}]:
      index = -1

    integration_site = self.getIntegrationSite(kw.get('domain'))
    default_resource = integration_site.getResourceValue()
    default_resource_gid = integration_site.product_module.getSourceSectionValue().getGidFromObject(default_resource, encoded=False)

    # browse the xml and save the sale order line values
    for subnode in xml.getchildren():
      # only works on tags, no on the comments or other kind of tag
      if type(subnode.tag) is not str:
        continue
      tag = subnode.tag.split('}')[index]
      if tag == 'resource':
        # encode to the output type
        link_gid = subnode.text #.encode('utf-8')
        if link_gid == ' Service Discount':
          link_object = integration_site.getSourceCarrierValue()
        elif link_gid == ' Service Delivery':
          link_object = integration_site.getDestinationCarrierValue()
        elif link_gid == default_resource_gid:
          link_object = default_resource
        else:
          synchronization_list = self.getSynchronizationObjectListForType(kw.get('domain'), 'Product', 'publication')
          for synchronization in synchronization_list:
            link_object = synchronization.getObjectFromGid(b16encode(link_gid))
            if link_object is not None:
              break
        # in the worse case save the line with the unknown product
        if link_object is None:
          raise ValueError, "Impossible to find related resource for gid %s" %(link_gid)

        # set the resource in the dict
        movement_dict_value[tag] = link_object
      elif tag == "VAT":
        vat_category = document.portal_categories.base_amount.trade.base.taxable['vat']
        vat = vat_category[subnode.text]
        movement_dict_value['vat'] = vat
      elif tag == 'category':
        # set categories in the list
        if subnode.text is not None:
          movement_dict_value[tag].append(subnode.text)
      else:
        # set line values in the dict
        if subnode.text is not None:
          movement_dict_value[tag] = subnode.text#.encode('utf-8')
    LOG("visitMovement", 300, "movement_dict_value = %s" %(movement_dict_value))
    if 'quantity' not in movement_dict_value:
      return

    # no variations will be use for the unknown product
    if movement_dict_value['resource'] == default_resource:
      movement_dict_value['category'] = []

    # Create the new Sale Order Line
    sale_order_line = document.newContent(portal_type='Sale Order Line')

    # define the setters of the sale order line
    mapping_of_setter = {
        'title': sale_order_line.setTitle,
        'reference': sale_order_line.setReference,
        'resource': sale_order_line.setResourceValue,
        'description': sale_order_line.setDescription,
        'vat': sale_order_line.setBaseContributionValue,
        'quantity': sale_order_line.setQuantity,
        'price': sale_order_line.setPrice,
    }

    # second, specific work on sale order line or on cell
    if len(movement_dict_value['category']):
      # set the resource on the sale order line
      mapping_of_setter['resource'](movement_dict_value['resource'])
      # work on variations
      variation_category_list = list(set(
          deepcopy(movement_dict_value['category'])
      ))
      #variation_category_list.sort() # XXX: The set remove the order
      LOG("AUREL", 300, "setting the variation on %s to value %s" %(sale_order_line.getPath(), variation_category_list))
      sale_order_line.setVariationCategoryList(variation_category_list)
      variation_category_list = sale_order_line.getVariationCategoryList()
      # create the cell or raise if it's not possible
      cell = sale_order_line.newCell(
        base_id='movement',
        portal_type='Sale Order Cell',
        *variation_category_list
        )

      # set values on the cell
      cell.setCategoryList(variation_category_list)
      cell.setMembershipCriterionCategoryList(variation_category_list)
      cell.setMembershipCriterionBaseCategoryList(
          sale_order_line.getVariationBaseCategoryList(),
      )
      cell.setMappedValuePropertyList(['price', 'quantity'])
      mapping_of_setter['quantity'] = cell.setQuantity
      mapping_of_setter['price'] = cell.setPrice
    else:
      sale_order_line.setVariationCategoryList([])
      
    # set on the sale order line or on cell the values
    for key in movement_dict_value:
      if key in mapping_of_setter:
        mapping_of_setter[key](movement_dict_value[key])


  def editDocument(self, object=None, **kw):
    """
      This is the default editDocument method. This method
      can easily be overwritten.
    """
    # Mapping of the PropertySheet
    mapping = {
        'title': 'title',
        'start_date': 'start_date',
        'stop_date': 'stop_date',
        'reference': 'reference',
        'causality': 'comment',
    }
    property = {}
    # Translate kw with the good PropertySheet
    for k, v in kw.items():
      k = mapping.get(k, k)
      property[k] = v
    object._edit(**property)