Skip to Content
Odoo Menú
  • Registra entrada
  • Prova-ho gratis
  • Aplicacions
    Finances
    • Comptabilitat
    • Facturació
    • Despeses
    • Full de càlcul (IA)
    • Documents
    • Signatura
    Vendes
    • CRM
    • Vendes
    • Punt de venda per a botigues
    • Punt de venda per a restaurants
    • Subscripcions
    • Lloguer
    Imatges de llocs web
    • Creació de llocs web
    • Comerç electrònic
    • Blog
    • Fòrum
    • Xat en directe
    • Aprenentatge en línia
    Cadena de subministrament
    • Inventari
    • Fabricació
    • PLM
    • Compres
    • Manteniment
    • Qualitat
    Recursos humans
    • Empleats
    • Reclutament
    • Absències
    • Avaluacions
    • Recomanacions
    • Flota
    Màrqueting
    • Màrqueting Social
    • Màrqueting per correu electrònic
    • Màrqueting per SMS
    • Esdeveniments
    • Automatització del màrqueting
    • Enquestes
    Serveis
    • Projectes
    • Fulls d'hores
    • Servei de camp
    • Suport
    • Planificació
    • Cites
    Productivitat
    • Converses
    • Validacions
    • IoT
    • VoIP
    • Coneixements
    • WhatsApp
    Aplicacions de tercers Odoo Studio Plataforma d'Odoo al núvol
  • Sectors
    Comerç al detall
    • Llibreria
    • Botiga de roba
    • Botiga de mobles
    • Botiga d'ultramarins
    • Ferreteria
    • Botiga de joguines
    Food & Hospitality
    • Bar i pub
    • Restaurant
    • Menjar ràpid
    • Guest House
    • Distribuïdor de begudes
    • Hotel
    Immobiliari
    • Agència immobiliària
    • Estudi d'arquitectura
    • Construcció
    • Gestió immobiliària
    • Jardineria
    • Associació de propietaris de béns immobles
    Consultoria
    • Empresa comptable
    • Partner d'Odoo
    • Agència de màrqueting
    • Bufet d'advocats
    • Captació de talent
    • Auditoria i certificació
    Fabricació
    • Textile
    • Metal
    • Mobles
    • Menjar
    • Brewery
    • Regals corporatius
    Salut i fitness
    • Club d'esport
    • Òptica
    • Centre de fitness
    • Especialistes en benestar
    • Farmàcia
    • Perruqueria
    Trades
    • Servei de manteniment
    • Hardware i suport informàtic
    • Sistemes d'energia solar
    • Shoe Maker
    • Serveis de neteja
    • Instal·lacions HVAC
    Altres
    • Nonprofit Organization
    • Agència del medi ambient
    • Lloguer de panells publicitaris
    • Fotografia
    • Lloguer de bicicletes
    • Distribuïdors de programari
    Browse all Industries
  • Comunitat
    Aprèn
    • Tutorials
    • Documentació
    • Certificacions
    • Formació
    • Blog
    • Pòdcast
    Potenciar l'educació
    • Programa educatiu
    • Scale-Up! El joc empresarial
    • Visita Odoo
    Obtindre el programari
    • Descarregar
    • Comparar edicions
    • Novetats de les versions
    Col·laborar
    • GitHub
    • Fòrum
    • Esdeveniments
    • Traduccions
    • Converteix-te en partner
    • Services for Partners
    • Registra la teva empresa comptable
    Obtindre els serveis
    • Troba un partner
    • Troba un comptable
    • Contacta amb un expert
    • Serveis d'implementació
    • Referències del client
    • Suport
    • Actualitzacions
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Programar una demo
  • Preus
  • Ajuda

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Comptabilitat
  • Inventari
  • PoS
  • Projectes
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Etiquetes (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Etiquetes (View all)
odoo accounting v14 pos v15
About this forum
Ajuda

I need to add inverse function for a margin field,

Subscriure's

Get notified when there's activity on this post

This question has been flagged
decimalcomputeinversedecimal-accuracyDecimal-precision
1537 Vistes
Avatar
Jenish M

In purchase.order.line i have added some fields and their respective inverse method to change the corresponding fields 

class PurchaseOrderInherit(models.Model):
_inherit = "purchase.order.line"

mrp = fields.Float("MRP")
​margin = fields.Float("Mark Down%", compute="_compute_margin", inverse="_inverse_margin", digits='Product Price', store=True,
help="(MRP Excl. - Cost Excl.) / MRP Excl.")
margin_up = fields.Float("Mark Up %", compute="_compute_margin", inverse="_inverse_margin_up", digits='Product Price', store=True,
help="(MRP Excl. - Cost Excl.) / Cost Excl.")
margin_status = fields.Char("Change in Margin%", compute="_compute_margin_status", store=True)
margin_prev = fields.Float("Margin Prev %", store=True)
disc_amount = fields.Float("Disc. Amount", compute="_compute_disc_amount", inverse="_inverse_disc_amount",
help="Discount Amount for Total Quantity", store="True", digits='Product Price')

@api.depends("mrp", "product_qty", "price_subtotal", "product_id")
def _compute_margin(self):
for rec in self:
if rec.mrp and rec.price_subtotal:
base_amount = self._get_tax_amount(rec.product_id, rec.mrp) * rec.product_qty
rec.margin = (base_amount - rec.price_subtotal) / base_amount * 100 if base_amount else 0
rec.margin_up = (base_amount - rec.price_subtotal) / rec.price_subtotal * 100 if rec.price_subtotal else 0
else:
rec.margin = False
rec.margin_up = False

@api.onchange('margin_up')
def _inverse_margin_up(self):
for rec in self:
if rec.product_id and rec.margin_up not in (False, 0):
if rec.price_subtotal:
base_amount = rec.price_subtotal * (1 + rec.margin_up / 100)
rec.mrp = round(rec.product_qty and self._get_mrp_from_base_amount(rec.product_id, base_amount) / rec.product_qty)

@api.onchange('margin')
def _inverse_margin(self):
for rec in self:
base_amount = self._get_tax_amount(rec.product_id, rec.mrp)
if base_amount != 0 and rec.discount != 100:
rec.price_unit = (base_amount * (1 - rec.margin / 100)) / (1 - rec.discount / 100)

def _get_mrp_from_base_amount(self, product, base_amount):
taxes = product.taxes_id.filtered(
lambda x: x.company_id.id == self.env.company.id
or x.company_id.id == self.env.company.parent_id.id
)
tax_amount = sum(taxes.mapped('amount'))
mrp = base_amount * (1 + tax_amount / 100)
return mrp

def _get_tax_amount(self, product, price):
taxes_id = product.taxes_id.filtered(
lambda x: x.company_id.id == self.env.company.id
or x.company_id.id == self.env.company.parent_id.id
)
if taxes_id:
amount = taxes_id.compute_all(
price, product=product, partner=self.env["res.partner"]
)
amount = amount["total_excluded"]
else:
amount = price
return amount

# update margin status
@api.depends("mrp", "price_unit")
def _compute_margin_status(self):
for rec in self:
margin_prev = False
if rec.product_id.id:
margin_prev = (
self.env["purchase.order.line"]
.search(
[
("product_id", "=", rec.product_id.id),
("partner_id", "=", rec.partner_id.id),
("order_id", "!=", rec.order_id._origin.id),
("company_id", '=', self.env.company.id),
],
order="create_date desc",
limit=1,
)
.margin
)
rec.margin_prev = margin_prev
if margin_prev == False:
rec.margin_status = "NA"
elif rec.margin > margin_prev:
rec.margin_status = str(round(rec.margin - margin_prev, 1)) + " ⇑"
elif rec.margin == margin_prev:
rec.margin_status = str(round(rec.margin - margin_prev, 1)) + " ⇔"
elif rec.margin rec.margin_status = str(round(rec.margin - margin_prev, 1)) + " ⇓"

@api.depends('discount')
def _compute_disc_amount(self):
for rec in self:
if rec.discount:
rec.disc_amount = rec.price_unit * rec.product_qty * (rec.discount / 100)
else:
rec.disc_amount = 0.0

@api.onchange('disc_amount')
def _inverse_disc_amount(self):
for rec in self:
if rec.price_unit and rec.product_qty:
rec.discount = (rec.disc_amount / (rec.price_unit * rec.product_qty)) * 100

I have added all the inverse function as api.onchange() but it triggers whenever the field updates it results some unwanted values is there anything i can do
if anyone knows help me

0
Avatar
Descartar
Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Registrar-se
Related Posts Respostes Vistes Activitat
The decimal precision in the sum tree view does not follow the decimal precision in the field
decimal-accuracy Decimal-precision odoo16features
Avatar
0
de des. 23
1529
Inverse function on compute field Solved
compute inverse odoo10
Avatar
Avatar
1
de nov. 24
41411
Inverse function for making the compute field editable Solved
editable compute inverse odoo12
Avatar
Avatar
Avatar
Avatar
3
d’ag. 24
15079
Removing decimal amount from listed price on website
decimal pricing website_builder decimal-accuracy
Avatar
Avatar
1
de maig 24
2464
The translation does not change when I change the nameUA field in the view
fields translations compute inverse
Avatar
Avatar
Avatar
2
de febr. 24
4539
Community
  • Tutorials
  • Documentació
  • Fòrum
Codi obert
  • Descarregar
  • GitHub
  • Runbot
  • Traduccions
Serveis
  • Allotjament a Odoo.sh
  • Suport
  • Actualització
  • Desenvolupaments personalitzats
  • Educació
  • Troba un comptable
  • Troba un partner
  • Converteix-te en partner
Sobre nosaltres
  • La nostra empresa
  • Actius de marca
  • Contacta amb nosaltres
  • Llocs de treball
  • Esdeveniments
  • Pòdcast
  • Blog
  • Clients
  • Informació legal • Privacitat
  • Seguretat
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo és un conjunt d'aplicacions empresarials de codi obert que cobreix totes les necessitats de la teva empresa: CRM, comerç electrònic, comptabilitat, inventari, punt de venda, gestió de projectes, etc.

La proposta única de valor d'Odoo és ser molt fàcil d'utilitzar i estar totalment integrat, ambdues alhora.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now