Skip ke Konten
Odoo Menu
  • Login
  • Uji coba gratis
  • Aplikasi
    Keuangan
    • Akuntansi
    • Faktur
    • Pengeluaran
    • Spreadsheet (BI)
    • Dokumen
    • Tanda Tangan
    Sales
    • CRM
    • Sales
    • POS Toko
    • POS Restoran
    • Langganan
    • Rental
    Website
    • Website Builder
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Rantai Pasokan
    • Inventaris
    • Manufaktur
    • PLM
    • Purchase
    • Maintenance
    • Kualitas
    Sumber Daya Manusia
    • Karyawan
    • Rekrutmen
    • Cuti
    • Appraisal
    • Referensi
    • Armada
    Marketing
    • Social Marketing
    • Email Marketing
    • SMS Marketing
    • Acara
    • Otomatisasi Marketing
    • Survei
    Layanan
    • Project
    • Timesheet
    • Layanan Lapangan
    • Meja Bantuan
    • Planning
    • Appointment
    Produktivitas
    • Diskusi
    • Approval
    • IoT
    • VoIP
    • Pengetahuan
    • WhatsApp
    Aplikasi pihak ketiga Odoo Studio Platform Odoo Cloud
  • Industri-Industri
    Retail
    • Toko Buku
    • Toko Baju
    • Toko Furnitur
    • Toko Kelontong
    • Toko Hardware
    • Toko Mainan
    Makanan & Hospitality
    • Bar dan Pub
    • Restoran
    • Fast Food
    • Rumah Tamu
    • Distributor Minuman
    • Hotel
    Real Estate
    • Agensi Real Estate
    • Firma Arsitektur
    • Konstruksi
    • Estate Management
    • Perkebunan
    • Asosiasi Pemilik Properti
    Konsultansi
    • Firma Akuntansi
    • Mitra Odoo
    • Agensi Marketing
    • Firma huku
    • Talent Acquisition
    • Audit & Sertifikasi
    Manufaktur
    • Tekstil
    • Logam
    • Perabotan
    • Makanan
    • Brewery
    • Corporate Gift
    Kesehatan & Fitness
    • Sports Club
    • Toko Kacamata
    • Fitness Center
    • Wellness Practitioners
    • Farmasi
    • Salon Rambut
    Perdagangan
    • Handyman
    • IT Hardware & Support
    • Sistem-Sistem Energi Surya
    • Pembuat Sepatu
    • Cleaning Service
    • Layanan HVAC
    Lainnya
    • Organisasi Nirlaba
    • Agen Lingkungan
    • Rental Billboard
    • Fotografi
    • Penyewaan Sepeda
    • Reseller Software
    Browse semua Industri
  • Komunitas
    Belajar
    • Tutorial-tutorial
    • Dokumentasi
    • Sertifikasi
    • Pelatihan
    • Blog
    • Podcast
    Empower Education
    • Program Edukasi
    • Game Bisnis 'Scale Up!'
    • Kunjungi Odoo
    Dapatkan Softwarenya
    • Download
    • Bandingkan Edisi
    • Daftar Rilis
    Kolaborasi
    • Github
    • Forum
    • Acara
    • Terjemahan
    • Menjadi Partner
    • Layanan untuk Partner
    • Daftarkan perusahaan Akuntansi Anda.
    Dapatkan Layanan
    • Temukan Mitra
    • Temukan Akuntan
    • Temui penasihat
    • Layanan Implementasi
    • Referensi Pelanggan
    • Bantuan
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Dapatkan demo
  • Harga
  • Bantuan

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

  • CRM
  • e-Commerce
  • Akuntansi
  • Inventaris
  • PoS
  • Project
  • MRP
All apps
Anda harus terdaftar untuk dapat berinteraksi di komunitas.
Semua Post Orang Lencana-Lencana
Label (Lihat semua)
odoo accounting v14 pos v15
Mengenai forum ini
Anda harus terdaftar untuk dapat berinteraksi di komunitas.
Semua Post Orang Lencana-Lencana
Label (Lihat semua)
odoo accounting v14 pos v15
Mengenai forum ini
Help

Grouped Sums for computed fields

Langganan

Dapatkan notifikasi saat terdapat aktivitas pada post ini

Pertanyaan ini telah diberikan tanda
computed-fieldsgroup-byv18CommunityEdition
1 Balas
1628 Tampilan
Avatar
Shreya Doodipala

I have 2 computed (not stored) monetary fields on my crm.lead model. I want the grouped sums to appear, when I group the leads (based on some other fields on the model).

My XML code has:

<field name="my_deal_share" sum="My Deal Share" widget="monetary" options="{'currency_field': 'company_currency'}" />

<field name="my_profit_share" sum="My Profit Share" widget="monetary" options="{'currency_field': 'company_currency'}" />

I have used the following Python code in my model:

    @api.model

    def read_group(self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True):

        _logger.info("Entering read_group method for CrmLead")

        _logger.info(f"Domain: {domain}")

        _logger.info(f"Fields requested: {fields}") # Check if ':sum' is present here

        _logger.info(f"Groupby: {groupby}")


        # Call the original read_group first to get the grouped data

        res = super(CrmLead, self).read_group(domain, fields, groupby, offset=offset, limit=limit, orderby=orderby, lazy=lazy)


        _logger.info(f"Original read_group result (first 2 groups): {res[:2]}")


        # Determine which computed fields need summing

        needs_deal_share_sum = 'my_deal_share:sum' in fields

        needs_profit_share_sum = 'my_profit_share:sum' in fields


        _logger.info(f"Needs Deal Share Sum: {needs_deal_share_sum}")

        _logger.info(f"Needs Profit Share Sum: {needs_profit_share_sum}")


        # Only proceed if at least one of the computed fields' sums is requested

        if needs_deal_share_sum or needs_profit_share_sum:

            for group in res:

                # Get the records for this specific group

                group_domain = domain + (group.get(groupby[0] + '_domain') if groupby else [])

                records_in_group = self.search(group_domain)


                _logger.info(f"Processing group: {group.get(groupby[0])}")

                _logger.info(f"Records in group count: {len(records_in_group)}")


                # Calculate and set the sum for my_deal_share if requested

                if needs_deal_share_sum:

                    computed_my_deal_share_sum = sum(rec.my_deal_share for rec in records_in_group)

                    group['my_deal_share'] = computed_my_deal_share_sum

                    _logger.info(f"Computed my_deal_share_sum: {computed_my_deal_share_sum}")


                # Calculate and set the sum for my_profit_share if requested

                if needs_profit_share_sum:

                    computed_my_profit_share_sum = sum(rec.my_profit_share for rec in records_in_group)

                    group['my_profit_share'] = computed_my_profit_share_sum

                    _logger.info(f"Computed my_profit_share_sum: {computed_my_profit_share_sum}")

            _logger.info(f"Final read_group result (first 2 groups) after custom sums: {res[:2]}")


        return res

When I check logs, I see:

2025-06-20 08:01:42,196 7188 INFO db3 odoo.addons.custom_crm.models.crm_lead: Needs Deal Share Sum: False

2025-06-20 08:01:42,196 7188 INFO db3 odoo.addons.custom_crm.models.crm_lead: Needs Profit Share Sum: False

Thus, these grouped sums are not calculated.

How can I resolve this?


Odoo v18 Community Edition

0
Avatar
Buang
Avatar
Christoph Farnleitner
Jawaban Terbai

Seems like you're looking for something like this, where My Deal Share and My Profit Share are computed, non-stored fields:


Based on the sample data in my database, result (of the core read_group) for a simple group by (grouped by stage_id only) is

[
{
'stage_id': (1, 'New'),
'stage_id_count': 4,
'expected_revenue': 26664.0,
'probability': 0.0,
'__fold': False,
'__domain': ['&', '&', ('type', '=', 'opportunity'), ('user_id', '=', 2), ('stage_id', '=', 1)]
}, {
'stage_id': (2, 'Qualified'),
'stage_id_count': 2,
'expected_revenue': 13332.0,
'probability': 47.725,
'__fold': False,
'__domain': ['&', '&', ('type', '=', 'opportunity'), ('user_id', '=', 2), ('stage_id', '=', 2)]
}, {
'stage_id': (3, 'Proposition'),
'stage_id_count': 1,
'expected_revenue': 4444.0,
'probability': 0.0,
'__fold': False,
'__domain': ['&', '&', ('type', '=', 'opportunity'), ('user_id', '=', 2), ('stage_id', '=', 3)]
}, {
'stage_id': (4, 'Won'),
'stage_id_count': 2,
'expected_revenue': 5555.0,
'probability': 100.0,
'__fold': False,
'__domain': ['&', '&', ('type', '=', 'opportunity'), ('user_id', '=', 2), ('stage_id', '=', 4)]
}
]


Note 1: This is not the group by result of the screenshot, as it would just be to excessive. The principle however is exactly the same.



Therefore, you should need to put your focus on the __domain key of each dictionary in the list only, since each grouping-result provides you with the domain relevant to its records already. Using this domain, you now can simply issue your sum'ming function on the resulting record set.

class CrmLead(models.Model):
_inherit = 'crm.lead'

my_deal_share = fields.Monetary(compute='_compute_my_deal_share')
my_profit_share = fields.Monetary(compute='_compute_my_profit_share')
currency_id = fields.Many2one(related='company_id.currency_id')

def _compute_my_deal_share(self):
for rec in self:
rec.my_deal_share = rec.expected_revenue * 0.5 # or, i.e. a rate based on the currently logged in user, it's team, etc...

def _compute_my_profit_share(self):
for rec in self:
rec.my_profit_share = rec.expected_revenue * 0.1

@api.model
def read_group(self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True):
result = super(CrmLead, self).read_group(domain, fields, groupby, offset=offset, limit=limit, orderby=orderby, lazy=lazy)
for group in result:
lead_ids = self.search(group.get('__domain'))
group['my_deal_share'] = sum(lead_ids.mapped('my_deal_share'))
group['my_profit_share'] = sum(lead_ids.mapped('my_profit_share'))
return result


Note 2: Please keep in mind that excessive grouping and or large result sets will affect your database's performance. You may want to consider to the get rid of the search in the loop and try to create a map of all relevant leads that then can be filtered instead.


Note 3: It's easier if you provide an installable example in future...

0
Avatar
Buang
Menikmati diskusi? Jangan hanya membaca, ikuti!

Buat akun sekarang untuk menikmati fitur eksklufi dan agar terlibat dengan komunitas kami!

Daftar
Post Terkait Replies Tampilan Aktivitas
Modify Pivot View
Pivot v18 CommunityEdition
Avatar
Avatar
1
Jun 25
1695
Unable to send email notifications through write
notifications v18 CommunityEdition
Avatar
Avatar
1
Jun 25
1741
Upgrading from 17 to 18 : Computed fields
computed-fields Studio v18
Avatar
1
Feb 25
2275
Hide Records based on user group only in a particular view
views record_rule v18 CommunityEdition
Avatar
Avatar
Avatar
2
Sep 25
1438
Unable to pass context to an action
context server_action v18 CommunityEdition
Avatar
Avatar
1
Jul 25
1569
Komunitas
  • Tutorial-tutorial
  • Dokumentasi
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Terjemahan
Layanan
  • Odoo.sh Hosting
  • Bantuan
  • Peningkatan
  • Custom Development
  • Pendidikan
  • Temukan Akuntan
  • Temukan Mitra
  • Menjadi Partner
Tentang Kami
  • Perusahaan kami
  • Aset Merek
  • Hubungi kami
  • Tugas
  • Acara
  • Podcast
  • Blog
  • Pelanggan
  • Hukum • Privasi
  • Keamanan
الْعَرَبيّة 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 adalah rangkaian aplikasi bisnis open source yang mencakup semua kebutuhan perusahaan Anda: CRM, eCommerce, akuntansi, inventaris, point of sale, manajemen project, dan seterusnya.

Mudah digunakan dan terintegrasi penuh pada saat yang sama adalah value proposition unik Odoo.

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