Enable Dark Mode!
how-to-add-a-field-in-the-partner-ledger-in-odoo-19.jpg
By: Saneen K

How to Add a Field in the Partner Ledger in Odoo 19

Technical Odoo 19 Fields and widgets

The Partner Ledger report is one of the most important financial reports in an ERP system. This report provides complete details about each transaction made by any customer or vendor. This keeps the accounting users in touch with customers/vendors on payments for invoices, bills, etc. It helps to track the outstanding entries of each partner.

Although the standard Partner Ledger report includes the essential accounting information like invoice number, journal, invoice date, matching number, debit, credit, balance, etc. Some businesses often require additional information to meet their specific needs. For example, some companies need to show any type of custom field or any existing fields, or to make the report more user-friendly and more informative.

In this blog, we try to add a custom field from the invoice/bill to the standard partner ledger report in Odoo, while keeping the standard partner ledger report as such.  In this first step, we add a custom field named Partner Reference on the invoice/bill form. And then we will load this field into the standard partner ledger report.

Let's go through the implementation step by step to understand how this customization can be achieved.

In Odoo, the data for financial reports is fetched from the Journal Items (account.move.line). So first we need to add the field in the โ€˜account.move.lineโ€™ model. For more convenience, we add the Partner Reference field in the Invoice/Bill (account.move), and then we propagate this field into the Journal Items. This approach keeps the field easily accessible on the invoice or bill while making it available for reporting.

The first step is to inherit the โ€˜account.moveโ€™ model for adding the custom field.

class AccountMove(models.Model):
   _inherit = 'account.move'
   partner_reference = fields.Char(string='Partner Reference',
                                   help='Partner Reference')

Next, add the same field to the account.move.line model as a related field. By defining it as a stored related field, the value entered on the invoice or bill is automatically synchronized to the corresponding journal items and can be used efficiently in reports and search operations.

class AccountMoveLine(models.Model):
   _inherit = 'account.move.line'
   partner_reference = fields.Char(string='Partner Reference',
                                   related='move_id.partner_reference',
                                   store=True,readonly=True,
                                   help='Partner Reference')

The next step is to add this custom field to the Invoice/Bill form view. For this, inherit the โ€˜account.moveโ€™ form view and add the custom field in the desired position.

<record id="view_move_form" model="ir.ui.view">
   <field name="name">account.move.form.account.extend</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_vendor_bill_id']" position="after">
           <field name="partner_reference"/>
       </xpath>
       <xpath expr="//field[@name='tax_ids']" position="after">
           <field name="partner_reference" optional="hide"/>
       </xpath>
   </field>
</record>

This will result in the following.

How to Add a Field in the Partner Ledger in Odoo 19-cybrosys

After adding the custom field to the Invoice/Bill form, the next step is to display it in the Partner Ledger report. To do this, we first need to add a new column to the Partner Ledger report by creating a data file and defining the report column, as shown below.

<data>
        <record id="account_reports.partner_ledger_report" model="account.report">
            <field name="column_ids">
                <record id="aged_receivable_report_partner_reference" model="account.report.column">
                    <field name="name">Partner Reference</field>
                    <field name="expression_label">partner_reference</field>
                    <field name="figure_type">string</field>
                </record>
            </field>
        </record>
    </data>

The above data file creates a new column named โ€˜Partner Referenceโ€™ in the standard Partner Ledger report. The expression_label value, partner_reference, acts as a unique identifier that links this report column to the value returned by the report handler. Since the field contains text, the figure_type is set to string, ensuring that the report displays the value correctly.

The final step is to extend the account.partner.ledger.report.handler model to pass the custom field value to the newly created Partner Reference column in the Partner Ledger report. This is achieved by inheriting the _get_additional_column_aml_values() method and including the partner_reference field from the account.move.line model in the SQL query. The alias partner_reference must match the expression_label defined for the report column, allowing Odoo to populate the corresponding column with the correct value.

class AccountPartnerLedgerReportHandler(models.AbstractModel):
   _inherit = 'account.partner.ledger.report.handler'
   def _get_additional_column_aml_values(self):
       additional_columns = super()._get_additional_column_aml_values()
       return SQL(""" %s
           account_move_line.partner_reference AS partner_reference,
           """,additional_columns,)

This will add the value from the Partner Reference field into the partner ledger report.

How to Add a Field in the Partner Ledger in Odoo 19-cybrosys

The Partner Ledger is one of the most important accounting reports in Odoo, and adding business-specific information to it can make financial analysis more meaningful and efficient. In this blog, we added a custom Partner Reference field to invoices and vendor bills, propagated it to the corresponding journal items, and displayed it in the standard Partner Ledger report by extending the report configuration and handler.

To read more about Overview of Partner Ledger Report in Odoo 19 Accounting, refer to our blog Overview of Partner Ledger Report in Odoo 19 Accounting.


Frequently Asked Questions

Why do we need to add the field to both account.move and account.move.line?

The account.move model stores the header-level information of an invoice or bill, while account.move.line stores the individual journal items. The partner ledger report fetches data from the journal items, so the field must exist there to appear in the report. However, we add it to account.move first because it's more convenient for users to enter the value once at the invoice level. By using a related field on account.move.line with store=True, the value automatically propagates to all journal items without requiring manual entry on each line, giving us the best of both worlds - easy data entry and efficient reporting.

What is the purpose of the expression_label, and why must it match the alias in SQL?

The expression_label is a unique identifier that acts as a link between the report column definition and the actual data coming from the report handler. When you define a column in the account.report model, Odoo looks for a value with the same name in the data returned by the handler.

Can I add multiple custom fields to the partner ledger using this approach?

Absolutely! You can add as many fields as you need by following the same pattern. Simply add each field to the account.move and account.move.line models, define separate columns in the report XML with unique expression_label values, and extend the _get_additional_column_aml_values() method to include all your fields in the SQL query.

Will this customization break when I upgrade Odoo to a newer version?

This is an important consideration. Since we're using standard Odoo inheritance mechanisms (_inherit, inherit_id, and extending methods with super()), the customization follows Odoo's best practices for module development. However, during major version upgrades, Odoo may change the structure of the partner ledger report or the underlying models. To minimize issues, always store your custom code in a separate module (not modifying core files), test thoroughly after each upgrade, and check Odoo's official migration guides.

If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



WhatsApp