Nacho R
Nacho R

Reputation: 81

Odoo 11 insert one2many from calculated field

I'm developing a custom module to allow users to create invoices with the information of stock moves.

In my .py I'm inhereting two models: stock.move and account.invoice, the target is to add new invoice_line_ids to the current invoice but I don't quite understand how calculated fields work. The code above is what I'm using

# -*- coding: utf-8 -*-
from odoo import fields, models

class Move(models.Model):
    _inherit = 'stock.move'


    x_invoice_id = fields.Many2one('account.invoice',
        string="Factura de referencia", ondelete='set null')


class Invoice(models.Model):
    _inherit = 'account.invoice'

    x_stock_move = fields.One2many('stock.move',
        string="Movimiento asociado",'x_invoice_id')


    invoice_line_ids = fields.One2many('account.invoice.line',
        string="Líneas de la factura",'invoice_id', compute='_add_lines')

## Add product_id and quantity from the selected stock moves
    @api.depends('x_stock_move')
    def _add_lines(self):
        for move in x_stock_move:
            self.env['account.invoice.line'].create({
                    'invoice_id': values['self.invoice_line_ids']
                    'product_id': values['move.product_id'],
                    'quantity': values['move.product_uom_qty']
            })

But the method _add_lines() is not working properly, any ideas?

here is my custom view

<odoo>
    <record id="invoice_stock_moves" model="ir.ui.view">
        <field name="name">account.invoice.x_stock_move</field>
        <field name="model">account.invoice</field>
        <field name="inherit_id" ref="account.invoice_supplier_form"/>
        <field name="arch" type="xml">

            <xpath expr="//page" position="after">

                <page string="Movimientos asociados" attrs="{'invisible': [('partner_id', '=', False)]}">
                    <field name="x_stock_move" widget="many2many" options="{'no_create': True}" domain="['&amp;', ('state','=','done'), ('picking_partner_id','=',context.get('partner_id')), '&amp;', ('x_invoice_id','=',False)]" attrs="{'readonly':[('state','not in',('draft',))]}">
                      <tree>
                        <field name="state" invisible="1"/>
                        <field name="date" />
                        <field name="picking_partner_id" invisible="1"/>
                        <field name="reference" />
                        <field name="product_id" />
                        <field name="product_uom_qty" string="Cantidad" />
                        <field name="product_uom" />
                      </tree>
                    </field>
                </page>

            </xpath>
        </field>
    </record>
</odoo>

Upvotes: 0

Views: 303

Answers (1)

Amr Abd-Alkrim
Amr Abd-Alkrim

Reputation: 404

You can do this by a wizard that takes the active_id of the invoice when clicking on an action to open the wizard. See my below example (Not Tested but you will get the concept)

stock_move_invoice_wizard.xml

<record model="ir.ui.view" id="stock_move_invoice_wizard">
    <field name="name">Stock Move Invoice</field>
    <field name="model">stock.move.invoice.wizard</field>
    <field name="type">form</field>
    <field name="arch" type="xml">
        <form string="Stock Move Invoice">
            <group>
                <field name="invocie_id"/>
            </group>
            <group>
                <field name="moves_ids"/>
            </group>
            <footer>
                <button name="action_update_moves_date" string="Link moves to invocie" type="object" class="oe_highlight"/>
                <button string="Cancel" special="cancel"/>
            </footer>
        </form>
    </field>
</record>

and in the same file add the action to call the wizard

<act_window id="action_stock_move_invoice_wizard"
            name="Stock Move Invoice"
            res_model="stock.move.invoice.wizard"
            context="{'default_invoice_id': active_id}"
            view_mode="form"
            target="new"/>

stock_move_invoice_wizard.py

class StockMoveInvoiceWizard(models.TransientModel):
    _name = 'stock.move.invoice.wizard'

    invoice_id = fields.Many2one('account.invoice', readonly=True)
    moves_ids = fields.Many2many('stock.move')

    def action_update_moves_invoice(self):
        for rec in self:
            for move in rec.moves_ids:
                move.x_invoice_id = rec.invoice_id.id

account_invoice.xml

<record id="account_invoice_form_inherit" model="ir.ui.view">
    <field name="name">account.invoice.form.inherit</field>
    <field name="model">account.invoice</field>
    <field name="inherit_id" ref="put_here_the_move_view_external_id"/>
    <field name="arch" type="xml">
        <xpath expr="//button[@name='action_cancel']" position="after">
            <button name="%(your_module_external_id.action_stock_move_invoice_wizard)d" string="Link Stock Moves" type="action"/>
        </xpath>
    </field>
</record>

Upvotes: 1

Related Questions