Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

openerp server action - python code

Tags:

python

odoo

i dont know if i should be asking this here (im now thinking maybe a moderator would move it to stackoverflow), but im not getting an answer on the openerp or launchpad forums.
In OpenERP 6.0.1, the following function does what its supposed to do when a button is placed in the invoice form 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 is to say that the invoice lines' price unit and discount are updated when this button is clicked in the form.

im trying to create a server action for object "Invoice" of type "python code" that executes 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 it does not work. what am i doing wrong?

EDIT: can anyone help me write a function that updates all invoices' lines' similar to those in /account/wizard/account_invoice_state.py ?

like image 312
3a2roub Avatar asked Sep 16 '26 02:09

3a2roub


1 Answers

If I want to run some code on the selected rows in a screen, I use a wizard with client_action_multi. Here's a wizard I wrote that just sets a "sanity checked" flag on stock pickings:

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 the client_action_multi so it can be executed on the selected records in a list:

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

To actually run the wizard, click the Action button in the menubar.

like image 142
Don Kirkby Avatar answered Sep 17 '26 16:09

Don Kirkby



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!