How can I set it up so that the purchase_price (‘Cost’) field can also be changed in the ‘posted’ status? I use odoo 18.
1. I have added the costs, margin and margin (%) to the table in the ‘Accounting’ app:
from odoo import api, fields, models
class AccountMoveLine(models.Model):
_inherit = "account.move.line"
margin = fields.Float(
"Margin", compute='_compute_margin',
digits='Product Price', store=True, groups="base.group_user")
margin_percent = fields.Float(
"Margin (%)", compute='_compute_margin', store=True)
purchase_price = fields.Float(
string="Cost",
digits='Product Price', store=True, readonly=False, copy=False)
@api.depends('price_subtotal', 'quantity', 'purchase_price')
def _compute_margin(self):
for line in self:
line.margin = line.price_subtotal - (line.purchase_price * line.quantity)
line.margin_percent = line.price_subtotal and line.margin/line.price_subtotal
2. Then I did the following:
class AccountMove(models.Model):
_inherit ='account.move'
invoice_line_ids = fields.One2many( # /!\ invoice_line_ids is just a subset of line_ids.
'account.move.line',
'move_id',
string='Invoice lines',
copy=False,
readonly=False,
domain=[('display_type', 'in', ('product', 'line_section', 'line_note'))],
)
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
<record id="view_customer_move_form" model="ir.ui.view">
<field name="name">account.move.customer.form</field>
<field name="model">account.move</field>
<field name="inherit_id" ref="account.view_move_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='invoice_line_ids']" position="attributes">
<attribute name="readonly">0</attribute>
</xpath>
</field>
</record>
</data>
</odoo>
3. This is the field message I am currently receiving:
Invalid Operation
You cannot modify the following readonly fields on a posted move: invoice_line_ids
All other fields are read-only. How can I work around this?