İçereği Atla
Odoo Menü
  • Portal
  • Ücretsiz deneyin
  • Uygulamalar
    Finans
    • Muhasebe
    • Faturalama
    • Masraf Yönetimi
    • Elektronik Çizelge (BI)
    • Belgeler
    • İmza
    Satış
    • Müşteri İlişkileri Yönetimi (CRM)
    • Satış
    • Satış Noktası Mağaza
    • Satış Noktası Restoran
    • Abonelikler
    • Kiralama
    Web Sitesi
    • Web Sitesi Oluşturucu
    • eTicaret
    • Blog
    • Forum
    • Canlı Sohbet
    • eÖğrenme
    Tedarik Zinciri
    • Envanter
    • Üretim
    • Ürün Yaşam Döngüsü Yönetimi
    • Satın Alma
    • Bakım
    • Kalite
    İnsan Kaynakları
    • Çalışanlar
    • İşe Alım
    • İzin
    • Değerlendirme
    • Referans
    • Filo Yönetimi
    Pazarlama
    • Sosyal Medyada Pazarlama
    • E-posta ile Pazarlama
    • SMS ile Pazarlama
    • Etkinlikler
    • Pazarlama Otomasyonu
    • Anket
    Hizmetler
    • Proje Yönetimi
    • Çalışma Çizelgeleri
    • Saha Hizmeti
    • Yardım Masası
    • Planlama
    • Randevular
    Verimlilik
    • Sohbet
    • Onay
    • Nesnelerin İnterneti
    • VoIP
    • Bilgi Bankası
    • WhatsApp
    Üçüncü taraf uygulamalar Odoo Stüdyo Odoo Bulut Platformu
  • Sektörler
    Perakende satış
    • Kitapçı
    • Giyim Mağazası
    • Mobilya Mağazası
    • Gıda Marketi
    • Hırdavat Dükkanı
    • Oyuncak Dükkanı
    Gıda ve Konaklama
    • Bar ve Pub
    • Restoran
    • Fast Food Restoranı
    • Konuk Evi
    • İçecek Distribütörü
    • Otel
    Gayrimenkul
    • Emlak Acentesi
    • Mimarlık Firması
    • İnşaat
    • Emlak Yönetimi
    • Bahçe Tasarımı
    • Mülk Sahipleri Derneği
    Uzmanlık
    • Muhasebe Firması
    • Odoo Partner
    • Pazarlama Ajansı
    • Hukuk Firması
    • Yetenek Kazanımı
    • Denetim ve Belgelendirme
    Üretim
    • Tekstil
    • Metal
    • Mobilyalar
    • Gıda
    • Bira fabrikası
    • Kurumsal Hediye
    Sağlık ve Spor
    • Spor Kulübü
    • Optik Mağazası
    • Fitness Merkezi
    • Sağlıklı Yaşam Merkezi
    • Eczane
    • Kuaför Salonu
    Ticaret
    • Tamirci
    • BT Donanım & Destek
    • Güneş Enerjisi Sistemleri
    • Ayakkabı İmalatçısı
    • Temizlik Hizmetleri
    • HVAC Hizmetleri
    Diğerleri
    • Kar Amacı Gütmeyen Kuruluş
    • Çevre Ajansı
    • Reklam Panosu Kiralama
    • Fotoğrafçılık
    • Bisiklet Kiralama
    • Yazılım Bayisi
    Tüm Sektörlere Göz Atın
  • Topluluk
    Öğrenim
    • Eğitim Araçları
    • Dokümantasyon
    • Sertifikasyonlar
    • Eğitim Etkinlikleri
    • Blog
    • Podcast
    Eğitim ve Gelişim
    • Eğitim Programı
    • Scale Up! İşletme Oyunu
    • Odoo'yu Ziyaret Edin
    Yazılım
    • İndirin
    • Sürümleri Kıyaslayın
    • Sürümler
    İş Birliği
    • Github
    • Forum
    • Etkinlikler
    • Çeviriler
    • Partner Olun
    • Partnerler için Hizmetler
    • Muhasebe Firmanızı Kaydettirin
    Hizmetler
    • Partner Bulun
    • Muhasebeci Bulun
    • Bir danışmanla görüşün
    • Kurulum Hizmetleri
    • Müşteri Referansları
    • Destek
    • Sürüm Yükseltme
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Demo randevusu alın
  • Fiyatlandırma
  • Yardım

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

  • Müşteri İlişkileri Yönetimi
  • e-Commerce
  • Muhasebe
  • Envanter
  • PoS
  • Proje Yönetimi
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Etiketler (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Etiketler (View all)
odoo accounting v14 pos v15
About this forum
Yardım

Help me with this code.

Abone Ol

Get notified when there's activity on this post

Bu soru işaretlendi
codereview
2 Cevaplar
2202 Görünümler
Avatar
Jenish M

class PurchaseOrderInherit(models.Model):

     _inherit = "purchase.order.line"

     margin = fields.Float("Margin %", compute="_compute_margin",

inverse="_inverse_margin", digits='Product Price', store=True)

     mrp = fields.Float("MRP", help="Load from Product MRP(Latest)",

digits='Product Price')

     @api.onchange('product_id')

     def _compute_mrp(self):

         for rec in self:

             if rec.product_id:

                 rec.mrp = rec.product_id.lst_price

             else:

                 rec.mrp = False

     # Get Margin

     @api.depends("mrp", "product_qty", "price_subtotal", "product_id")

     def _compute_margin(self):

         for rec in self:

             if rec.mrp:

                 price_sub_mrp = self._get_tax_amount(rec.product_id,

rec.mrp)

                 margin = (price_sub_mrp * rec.product_qty) -

rec.price_subtotal

                 rec.margin = price_sub_mrp and margin / price_sub_mrp

             else:

                 rec.margin = False

     @api.onchange('margin')

     def _inverse_margin(self):

         for rec in self:

             if rec.order_id.mrp_updatable:

                 cost = rec.product_qty and rec.price_total /

rec.product_qty

                 rec.mrp = cost / (1 - rec.margin)

             else:

                 cost = rec.mrp * (1 - rec.margin)

                 cost_tax = self._get_tax_amount(rec.product_id, cost)

                 if rec.discount:

                     price_unit = cost_tax + cost_tax * (rec.discount / 100)

                 else:

                     price_unit = cost_tax

                 rec.price_unit = price_unit

     # Get price excluding tax amount

     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

This is a custom code I have developed for purchase

It calculates the margin% for purchase and I have set a inverse method for margin so when ever I change the margin based on a Boolean(mrp_updateable) field it revere mrp or price_unit

But when I check this will results to some decimal point differences

If I change margin as 20 then when I save it shows 19.99 and if I change discount it changes price_unit

Can anyone review the code and help me

Thanks in advance..

0
Avatar
Vazgeç
Avatar
Jainesh Shah(Aktiv Software)
En İyi Yanıt

Hello Mad Max ,

    

    The issue you are facing with the slight decimal point differences when changing the margin and saving 

    could be due to several factors such as precision errors in floating-point arithmetic or how Odoo handles rounding. 

    

    Here are a few steps to ensure your calculations are as precise as possible:


    1) Ensure Precision in Float Fields:

    Make sure the fields involved in the calculations (like margin, mrp, price_unit, etc.) have the appropriate precision.


    2) Use Rounding:

    Explicitly round your values to avoid precision issues when performing arithmetic operations.


    3) Refactor Calculation Logic:

    Ensure your calculation logic is consistent and uses precise arithmetic.


    Let's go through your code and make some adjustments to improve precision and clarity.


   //Code in comment//

   Hope this helps.

    

    If you need any help in customization feel free to contact us.


Thanks & Regards,


Email:  odoo@aktivsoftware.com           


Skype: kalpeshmaheshwari  

0
Avatar
Vazgeç
Jainesh Shah(Aktiv Software)

Code:

from odoo import api, fields, models
import math

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

margin = fields.Float("Margin %", compute="_compute_margin",
inverse="_inverse_margin", digits='Product Price', store=True)
mrp = fields.Float("MRP", help="Load from Product MRP (Latest)",
digits='Product Price')

@api.onchange('product_id')
def _compute_mrp(self):
for rec in self:
rec.mrp = rec.product_id.lst_price if rec.product_id else False

# Get Margin
@api.depends("mrp", "product_qty", "price_subtotal", "product_id")
def _compute_margin(self):
for rec in self:
if rec.mrp and rec.product_qty:
price_sub_mrp = self._get_tax_amount(rec.product_id, rec.mrp)
margin = (price_sub_mrp * rec.product_qty) - rec.price_subtotal
rec.margin = round(price_sub_mrp and margin / price_sub_mrp, 2)
else:
rec.margin = False

@api.onchange('margin')
def _inverse_margin(self):
for rec in self:
if rec.product_qty:
if rec.order_id.mrp_updatable:
cost = rec.price_total / rec.product_qty
rec.mrp = round(cost / (1 - rec.margin), 2) if rec.margin != 1 else cost
else:
cost = rec.mrp * (1 - rec.margin)
cost_tax = self._get_tax_amount(rec.product_id, cost)
price_unit = cost_tax + (cost_tax * (rec.discount / 100) if rec.discount else 0)
rec.price_unit = round(price_unit, 2)

# Get price excluding tax amount
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

Jenish M
Üretici

Thanks for the effort.
but no luck for me.

Avatar
Sayed Mohammed Aqeel Ebrahim
En İyi Yanıt

Add Import for float_round and float_compare:

from odoo.tools.float_utils import float_round, float_compare

Update _compute_margin to use float_round:

rec.margin = float_round(rec.margin, precision_digits=rec._get_digits('Product Price'))

Update _inverse_margin to use float_round:

rec.mrp = float_round(rec.mrp, precision_digits=rec._ge(price_unit, precision_digits=rec._get_digits('Product Price'))

Add Helper Method _get_digits to fetch precision:

def _get_digits(self, precision_name):
    """Get the number of digits for the given precision."""
    return self.env['decimal.precision'].precision_get(precision_name)

0
Avatar
Vazgeç
Jenish M
Üretici

Thanks for the answer, but nothing changes.

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

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

Üye Ol
İlgili Gönderiler Cevaplar Görünümler Aktivite
Help review canceling purchase code
code review
Avatar
Avatar
2
Mar 15
5669
.update() method not updating values
code
Avatar
0
Eyl 23
2619
Why is the hr.attendance date column called "name"?
code
Avatar
0
Ağu 16
4967
How to refresh the form view in coding in openerp 7?
code
Avatar
0
Mar 15
5431
line code.
code
Avatar
0
Tem 24
3437
Topluluk
  • Eğitim Araçları
  • Dokümantasyon
  • Forum
Açık Kaynak
  • İndirin
  • Github
  • Runbot
  • Çeviriler
Hizmetler
  • Odoo.sh Hosting
  • Destek
  • Sürüm Yükseltme
  • Özel Geliştirmeler
  • Eğitim
  • Muhasebeci Bulun
  • Partner Bulun
  • Partner Olun
Hakkında
  • Şirketimiz
  • Pazarlama Gereçleri
  • İletişim
  • Kariyer
  • Etkinlikler
  • Podcast
  • Blog
  • Müşteriler
  • Hukuki • Gizlilik
  • Güvenlik
الْعَرَبيّة 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, müşteri ilişkileri yönetimi, eTicaret, muhasebe, envanter, satış noktası, proje yönetimi gibi şirketinizin tüm ihtiyaçlarını karşılayan bir açık kaynak işletme uygulamaları paketidir.

Odoo’nun eşsiz değer önermesi, aynı anda hem kullanımının çok kolay olup hem de tamamen entegre olmasıdır.

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