Openerp server action - python code

I don't know if I should be asking this here (now I think maybe the moderator will move it to stackoverflow), but I don't get an answer in the openerp or launchpad forums.
In OpenERP 6.0.1, the following function does what it needs to do when the button is in the tab form in order to execute it:

class account_invoice(osv.osv):
    _inherit = "account.invoice"

    """ Function to update all lines on invoice """
    def update_invoice(self, cr, uid, ids, context=None):
        if context is None:
            context = {}
        line_obj = self.pool.get('account.invoice.line')
        invoice_ids = self.browse(cr, uid, ids, context)
        for invoice in invoice_ids:
            for line in invoice.invoice_line:
                if line.product_id:
                    res = line_obj.product_id_change(cr, uid, [line.id], (line.product_id and line.product_id.id or False), uom=(line.uos_id and line.uos_id.id or False), qty=(line.quantity or 0),
                name=(line.name or ''), type=(invoice.type or False), partner_id=invoice.partner_id.id, fposition_id=invoice.fiscal_position.id, price_unit=(line.price_unit or 0),
                address_invoice_id=(invoice.address_invoice_id and invoice.address_invoice_id.id or False), currency_id=(invoice.currency_id and invoice.currency_id.id or False), context=context)
                    price_unit = res['value']['price_unit']
                    discount = res['value']['discount']
                    line_obj.write(cr, uid, [line.id], {'price_unit': price_unit})
                    line_obj.write(cr, uid, [line.id], {'discount': discount})
        return True

    account_invoice()

      

This means that the price block and discount on the invoice line are updated when this button is clicked on the form.

im trying to create a server action for an "python code" type invoice object that does this function on all invoices from a menu item. in the python code box i wrote:

inv = self.pool.get('account.invoice')
line_obj = self.pool.get('account.invoice.line')
for invoice in inv.browse(cr, uid, ids):
    for line in invoice.invoice_line:
        res = line_obj.product_id_change(cr, uid, [line.id], (line.product_id and line.product_id.id or False), uom=(line.uos_id and line.uos_id.id or False), qty=(line.quantity or 0), name=(line.name or ''), type=(invoice.type or False), partner_id=invoice.partner_id.id, fposition_id=invoice.fiscal_position.id, price_unit=(line.price_unit or 0), address_invoice_id=(invoice.address_invoice_id and invoice.address_invoice_id.id or False), currency_id=(invoice.currency_id and invoice.currency_id.id or False), context=context)
    price_unit = res['value']['price_unit']
    discount = res['value']['discount']
    line_obj.write(cr, uid, [line.id], {'price_unit': price_unit})
    line_obj.write(cr, uid, [line.id], {'discount': discount})

      

but that won't work. what am I doing wrong?

EDIT: Can anyone help me write a function that updates all invoice lines similar to the ones in / account / wizard / account_invoice_state.py?

+3


source to share


3 answers


I managed to successfully write what I need, here it is:



class account_invoice_update(osv.osv_memory):

_name = "account.invoice.update"

""" Function to update all lines on selected invoice(s) """
def invoice_update(self, cr, uid, ids, context=None):
    if context is None:
        context = {}

    pool_obj = pooler.get_pool(cr.dbname)
    data_inv = pool_obj.get('account.invoice').read(cr, uid, context['active_ids'], ['state'], context=context)

    for record in data_inv:
        if record['state'] in ('cancel','paid','open'):
            raise osv.except_osv(_('Warning'), _("Selected Invoice(s) cannot be cancelled as they are already in 'Cancelled','Done', or 'Open' state!"))

    inv_obj = self.pool.get('account.invoice')
    inv_line_obj = self.pool.get('account.invoice.line')
    for invoice in inv_obj.browse(cr, uid, context.get('active_ids'), context=context):
        for line in invoice.invoice_line:
            res = inv_line_obj.product_id_change(cr, uid, [line.id], (line.product_id and line.product_id.id or False), uom=(line.uos_id and line.uos_id.id or False), qty=(line.quantity or 0), name=(line.name or ''), type=(invoice.type or False), partner_id=invoice.partner_id.id, fposition_id=invoice.fiscal_position.id, price_unit=(line.price_unit or 0), address_invoice_id=(invoice.address_invoice_id and invoice.address_invoice_id.id or False), currency_id=(invoice.currency_id and invoice.currency_id.id or False), context=context)
            price_unit = res['value']['price_unit']
            discount = res['value']['discount']
            inv_line_obj.write(cr, uid, [line.id], {'price_unit': price_unit})
            inv_line_obj.write(cr, uid, [line.id], {'discount': discount})

    return {'type': 'ir.actions.act_window_close'}

account_invoice_update()

      

+1


source


If I want to run the code on selected lines on the screen, I use the wizard with client_action_multi

. Here's a wizard I wrote that just ticks the "sanity tested" box when sorting stocks:

import wizard
import pooler

def _set_flags(self, cr, uid, data, context):
    stock_picking_obj = pooler.get_pool(cr.dbname).get('stock.picking')

    move_ids = data['ids']
    picking_ids = stock_picking_obj.search(
        cr,
        uid,
        [('move_lines', 'in', move_ids)])
    stock_picking_obj.write(cr, uid, picking_ids, {'sanity_checked': True})
    return {}

class sanity_checked(wizard.interface):
    states = {
        'init': {
            'actions': [_set_flags],
            'result': {'type': 'state', 'state':'end'}
        },
    }
sanity_checked('promise.date.sanity.checked')

      

I configure the wizard with client_action_multi

so that it can be executed on the selected entries in the list:



<wizard
        id="wiz_sanity_checked"
        model="stock.move"
        string="Sanity Checked"
        name="promise.date.sanity.checked"
        keyword="client_action_multi"/>

      

To start the wizard, click the Action button on the menu bar.

+1


source


In server action, you can write simple python codes. You are trying to use self.pool.get (). This is not possible due to server action. And please state your requirement

0


source







All Articles