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="['&', ('state','=','done'), ('picking_partner_id','=',context.get('partner_id')), '&', ('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>