Search code examples
odooodoo-14

Global / Line Discount for Sale/Purchase/Account


[Odoo 14 Community Edition]

I need to customize Global and Line Discounts (amount & percentage) into Sale / Purchase / Account.

I have done the Sale and Purchase parts. It is just adding fields and a few logics here and there and send the data to Account (account.move) by prepare_invoice methods.

Now here's the issue I am facing -- The Account. I am having a tremendous confusion of where I should modify. After I tried to understand and tracked the flow of the standard invoicing/billing, I am at lost. There are too many functions/variables for me, who do not understand accounting, to try to get the whole idea out of it.

I did add the discount fields that I need. However, the standard calculation of price / taxes / credit / debit are messed up when I try to inherit some methods that I think I should modify. I ended up having incorrect taxes, unbalanced credit/debit, and incorrect total amount. I tried editing here and there (by inheriting of course. I can still rollback everything I did).

The point is that I need precise suggestions and/or directions of what functions/methods I should inherit just enough to make discount possible. I have 2 Line Discount fields (amount and percent) and 2 Global Discount (also amount and percent). The calculation between them is not the point of interest here. The only problem at this point is to integrate these fields (from SO, PO, and manually created) into the calculation of everything in Invoicing/Billing.

Here are the fields:

account.move

global_discount_amount = fields.Float(string='Global Discount Amount', compute=compute_global_discount_amount, store=True)
global_discount_percent = fields.Float(string='Global Discount Percent', compute=compute_global_discount_percent, store=True)

account.move.line

discount_line_amount = fields.Float(string='Disc. Line Amount', compute=compute_discount_line_amount, store=True)
discount_line_percent = fields.Float(string='Disc. Line %', compute=compute_discount_line_percent, store=True)

Right now, I am messing with some methods such as: (a few examples)

account.move

  • _recompute_tax_lines

account.move.line

  • create
  • _get_fields_onchange_balance_model
  • _get_price_total_and_subtotal_model
  • _onchange_price_subtotal

Most of the modifications are written by copying the whole method from standard into my new model (inherit that standard model) and edit some codes here -- Override the standard code from my understanding.


Solution

  • Function computation/execution either depends on other fields value change or compute every time form/listview load.

    Check in your case what is depends on the function compute_global_discount_amount and compute_global_discount_percentage

    For better developing/troubleshooting, remove any @depends() fields declaration on the functions. Additionally, remove the store=True attribute temporarily. It will help you to narrow down the issue. And make sure you get the correct numbers.

    Once you get it, add back fields depending.

    Here is a sample example of a method (Odoo 14 CE) override which will be executed during compute amount.

    @api.depends(
        'line_ids.matched_debit_ids.debit_move_id.move_id.payment_id.is_matched',
        'line_ids.matched_debit_ids.debit_move_id.move_id.line_ids.amount_residual',
        'line_ids.matched_debit_ids.debit_move_id.move_id.line_ids.amount_residual_currency',
        'line_ids.matched_credit_ids.credit_move_id.move_id.payment_id.is_matched',
        'line_ids.matched_credit_ids.credit_move_id.move_id.line_ids.amount_residual',
        'line_ids.matched_credit_ids.credit_move_id.move_id.line_ids.amount_residual_currency',
        'line_ids.debit',
        'line_ids.credit',
        'line_ids.currency_id',
        'line_ids.amount_currency',
        'line_ids.amount_residual',
        'line_ids.amount_residual_currency',
        'line_ids.payment_id.state',
        'line_ids.full_reconcile_id')
    def _compute_amount(self):
        super()._compute_amount()
        for record in self:
            record.compute_global_discount_amount()
            record.compute_global_discount_percent()
    
    def compute_global_discount_amount(self):
        for record in self:
            # Execute your logic for compute global_discount_amount
            record.global_discount_amount = $$$$