Skip to Content
Odoo Menu
  • Sign in
  • Try it free
  • Apps
    Finance
    • Accounting
    • Invoicing
    • Expenses
    • Spreadsheet (BI)
    • Documents
    • Sign
    Sales
    • CRM
    • Sales
    • POS Shop
    • POS Restaurant
    • Subscriptions
    • Rental
    Websites
    • Website Builder
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Supply Chain
    • Inventory
    • Manufacturing
    • PLM
    • Purchase
    • Maintenance
    • Quality
    Human Resources
    • Employees
    • Recruitment
    • Time Off
    • Appraisals
    • Referrals
    • Fleet
    Marketing
    • Social Marketing
    • Email Marketing
    • SMS Marketing
    • Events
    • Marketing Automation
    • Surveys
    Services
    • Project
    • Timesheets
    • Field Service
    • Helpdesk
    • Planning
    • Appointments
    Productivity
    • Discuss
    • Approvals
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Industries
    Retail
    • Book Store
    • Clothing Store
    • Furniture Store
    • Grocery Store
    • Hardware Store
    • Toy Store
    Food & Hospitality
    • Bar and Pub
    • Restaurant
    • Fast Food
    • Guest House
    • Beverage Distributor
    • Hotel
    Real Estate
    • Real Estate Agency
    • Architecture Firm
    • Construction
    • Estate Management
    • Gardening
    • Property Owner Association
    Consulting
    • Accounting Firm
    • Odoo Partner
    • Marketing Agency
    • Law firm
    • Talent Acquisition
    • Audit & Certification
    Manufacturing
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Corporate Gifts
    Health & Fitness
    • Sports Club
    • Eyewear Store
    • Fitness Center
    • Wellness Practitioners
    • Pharmacy
    • Hair Salon
    Trades
    • Handyman
    • IT Hardware & Support
    • Solar Energy Systems
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    Others
    • Nonprofit Organization
    • Environmental Agency
    • Billboard Rental
    • Photography
    • Bike Leasing
    • Software Reseller
    Browse all Industries
  • Community
    Learn
    • Tutorials
    • Documentation
    • Certifications
    • Training
    • Blog
    • Podcast
    Empower Education
    • Education Program
    • Scale Up! Business Game
    • Visit Odoo
    Get the Software
    • Download
    • Compare Editions
    • Releases
    Collaborate
    • Github
    • Forum
    • Events
    • Translations
    • Become a Partner
    • Services for Partners
    • Register your Accounting Firm
    Get Services
    • Find a Partner
    • Find an Accountant
    • Meet an advisor
    • Implementation Services
    • Customer References
    • Support
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Get a demo
  • Pricing
  • Help

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

  • CRM
  • e-Commerce
  • Accounting
  • Inventory
  • PoS
  • Project
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
Help

I need to add inverse function for a margin field,

Subscribe

Get notified when there's activity on this post

This question has been flagged
decimalcomputeinversedecimal-accuracyDecimal-precision
1516 Views
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
Discard
Enjoying the discussion? Don't just read, join in!

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

Sign up
Related Posts Replies Views Activity
The decimal precision in the sum tree view does not follow the decimal precision in the field
decimal-accuracy Decimal-precision odoo16features
Avatar
0
Dec 23
1511
Inverse function on compute field Solved
compute inverse odoo10
Avatar
Avatar
1
Nov 24
41395
Inverse function for making the compute field editable Solved
editable compute inverse odoo12
Avatar
Avatar
Avatar
Avatar
3
Aug 24
15048
Removing decimal amount from listed price on website
decimal pricing website_builder decimal-accuracy
Avatar
Avatar
1
May 24
2445
The translation does not change when I change the nameUA field in the view
fields translations compute inverse
Avatar
Avatar
Avatar
2
Feb 24
4524
Community
  • Tutorials
  • Documentation
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Translations
Services
  • Odoo.sh Hosting
  • Support
  • Upgrade
  • Custom Developments
  • Education
  • Find an Accountant
  • Find a Partner
  • Become a Partner
About us
  • Our company
  • Brand Assets
  • Contact us
  • Jobs
  • Events
  • Podcast
  • Blog
  • Customers
  • Legal • Privacy
  • Security
الْعَرَبيّة 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 is a suite of open source business apps that cover all your company needs: CRM, eCommerce, accounting, inventory, point of sale, project management, etc.

Odoo's unique value proposition is to be at the same time very easy to use and fully integrated.

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