Пример #1
0
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