コード例 #1
0
ファイル: listeners.py プロジェクト: jeame/satchmo-openerp
def _delete_for_mapper(mapper, mapping_table=None, parent=None):
    ''' Delete the OE object(s) corresponding to the given mapper '''

    #get model corresponding to specified mapper
    obj_model = mapper.content_type.model_class()

    if mapping_table is None:
        #get root mapping if no mapping is specified
        mapping_table = ModelMapper.get_for_model(obj_model)[mapper.oerp_model]

    res = True

    #copy mapper for future reference
    mapper_del = DeletedObjMapper.objects.get_or_create(
        content_type = mapper.content_type,
        oerp_model = mapper.oerp_model,
        oerp_id = mapper.oerp_id,
        parent = parent)[0]

    #find child mappers
    child_mappers = mapper.__class__.objects.filter(parent = mapper)

    for child in child_mappers:
        #recursively delete child objects
        child.sync_now = mapper.sync_now
        res = res and _delete_for_mapper(
            mapper = child,
            mapping_table = mapping_table[child.oerp_model],
            parent = mapper_del)

    #check whether objects corresponding to this oe model can be deleted
    mapper.auto_del = ModelMapper.check_auto_del(mapping_table)


    try:
        if mapper.sync_now:
            #Automatic synchronization is turned on
            oerp_object = Oerp(mapper.oerp_model, mapper.oerp_id)

            #only try to delete if the object exists and is not
            #deleted automatically with its parent object
            if oerp_object.exists and not mapper.auto_del:
                oerp_object.delete()
            #deleting non-existent objects is OK and is not logged
            mapper_del.is_dirty = False
        else:
            #this object will be deleted later
            mapper_del.is_dirty = True
    except Exception as errmsg:
        #the sync failed
        log.error('Sync failed -- %s' % errmsg)
        mapper_del.is_dirty = True
        res = False
    finally:
        #store mapper in deleted mappers and delete original mapper
        if mapper != mapper_del:
            mapper.delete()
        mapper_del.save()

    return res
コード例 #2
0
ファイル: listeners.py プロジェクト: jeame/satchmo-openerp
def _validate_order_for_mapper(order_mapper):

    #reload mapper
    order_mapper = order_mapper.__class__.objects.get(id=order_mapper.id)

    #prepare
    order = order_mapper.object
    order_model = Oerp('sale.order', order_mapper.oerp_id)

    #try to confirm the order
    try:
        order_model.confirm_order()
        #get id of the invoice for this order
        #it should be OK to only consider the last invoice since it has
        #just been created
        invoice_id = order_model.read(['invoice_ids'])['invoice_ids'][-1]

        order_mapper.save_state('clean')

    except OerpSyncFailed as errmsg:
        #Something went wrong
        order_mapper.save_state('dirty')
        log.error('Sync failed -- %s' % errmsg)
        return False

    except KeyError:
        #The invoice doesn't seem to exist
        order_mapper.save_state('dirty')
        log.error('Sync failed -- Invoice doesn\'t exist (order id %s)' % order.id)
        return False

    #sync payments
    try:
        #confirm the invoice
        confirm_invoice_model = Oerp('account.invoice.confirm')
        confirm_invoice_model.validate_invoice(invoice_id)
    except:
        #the invoice could not be confirmed
        log.error('Sync failed -- Invoice couldn\'t be confirmed (%s)' % invoice_id)
        return False

    res = True
    voucher_model = Oerp('account.voucher')

    #try adding payments to invoice
    for payment in order.payments_completed():
        try:
            #create payment object mapper
            payment_mapper = ObjMapper(object=payment, oerp_model='account.voucher')
            #load partner object mapper
            partner_mapper = ObjMapper.objects.get_for_object(
                                order.contact.billing_address, 'res.partner')
            #add payment
            voucher_model.add_payment(
                partner_id = partner_mapper.oerp_id,
                account_id = settings.OPENERP_SETTINGS['ACCOUNT_ID'],
                journal_id = settings.OPENERP_SETTINGS['JOURNAL_ID'],
                period_id = date.today().month, #FIXME: find a better solution here
                amount = float(payment.amount),
                company_id = settings.OPENERP_SETTINGS['COMPANY_ID'],
                currency_id = settings.OPENERP_SETTINGS['CURRENCY_ID'],
            )
            payment_mapper.oerp_id = voucher_model.id
            payment_mapper.save_state('clean')

        except Exception as e:
            #The creation failed
            payment_mapper.save_state('dirty')
            log.error('Sync failed -- %s' % e)
            res = False

    return res
コード例 #3
0
ファイル: listeners.py プロジェクト: jeame/satchmo-openerp
def _save_for_mapper(mapper, mapping_table=None):

    #get model corresponding to specified mapper
    obj_model = mapper.content_type.model_class()

    if mapping_table is None:
        #get root mapping if no mapping is specified
        mapping_table = ModelMapper.get_for_model(obj_model)[mapper.oerp_model]

    res = True

    try:
        if mapper.sync_now:
            #The object is synced now

            oerp_object = Oerp(mapper.oerp_model, mapper.oerp_id)

            if oerp_object.exists:
                #object has to be updated
                data_dict = ModelMapper.parse_data(
                    mapper.object,
                    mapping_table,
                    mapper.oerp_model,
                    'update')[0]
                oerp_object.update(data_dict)
            else:
                #create object if it doesn't exist
                data_dict = ModelMapper.parse_data(
                    mapper.object,
                    mapping_table,
                    mapper.oerp_model,
                    'create')[0]
                oerp_object.create(data_dict)

                #update mapper
                mapper.oerp_id = oerp_object.id

            #consider sync successful if we get to this point
            mapper.save_state('clean')

        else:
            #This object is synced later. Only change mapper status...
            mapper.save_state('dirty')

    except MappingError as errmsg:
        log.error('Sync failed -- %s' % errmsg)
        mapper.save_state('dirty')
        res = False

    except OerpSyncFailed as errmsg:
        log.error('Sync failed -- %s' % errmsg)
        mapper.save_state('dirty')
        res = False

    else:
        #the sync was successful
        #run this recursively if child mappings are found
        children = ModelMapper.get_children(mapping_table)
        for child_model, child_mapping in children.items():
            #create child mapper
            log.debug('Mapping child model (%s)' % child_model)
            child_mapper = ObjMapper.objects.get_or_create(
                parent = mapper,
                content_type = mapper.content_type,
                object_id = mapper.object_id,
                oerp_model = child_model,)[0]
            child_mapper.sync_now = mapper.sync_now

            res = res and _save_for_mapper(child_mapper, child_mapping)

    return res