1

[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.

holydragon
  • 6,158
  • 6
  • 39
  • 62

3 Answers3

1

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 = $$$$
Bhavesh Odedra
  • 10,990
  • 12
  • 33
  • 58
0

have a look at apply_discount function in an inherited class of sale.order

def apply_discount(self, cr, uid, ids, discount_rate):
        cur_obj = self.pool.get('res.currency')
        res = {}
        line_obj = self.pool.get('sale.order.line')
        for order in self.browse(cr, uid, ids, context=None):
            for line in order.order_line:
                line_obj.write(cr, uid, [line.id], {'discount': discount_rate}, context=None)
        return res

A new column was added to the new inherited subclass of sale order

'discount_rate' : fields.float('Discount rate'),

Then in the sale order view (an inherited one) placed the new field discount on the sale.order.view and fired an event on the on_change of the value passing the self value of the field to the on_change event

In this way you can apply discount sequentially to the rows of the order without altering the normal process.
user16930239
  • 6,319
  • 2
  • 9
  • 33
0

Firstful, please pardon my English. Then let's get into the core, I guess that this is exactly the task that I was assigned. Luckily that I was not alone, I was guided by my so-called senior which showed me the way in order to achieve the Global Discount for Invoice and Bill.

By looking at your methods listing, you were already on the right path! So now, let me help you further...as much as I can

As in my case, I didn't put any new field regarding the Global Discount in Account Move Line model, even though in Sale Order Line and Purchase Order Line Global Discount fields do exist.

All and all, here are the methods that need to be customized:

  1. _onchange_invoice_line_ids
  2. _compute_amount
  3. _recompute_payment_terms_lines
  4. _recompute_tax_lines

I heavily modified the 3rd and 4th methods. However, I think that I still have some bugs, don't hesitate to tell me.

Dharman
  • 30,962
  • 25
  • 85
  • 135
farisfath25
  • 71
  • 1
  • 8