0

This issue comes after I achieved this.

To sum it up, I created a new extra field for discount in Sale Order. It is calculated in subtotal. It is working as expected as the part of Sale Order.

After that, I confirmed it, sent products, and created invoice.

I found out that the subtotal in invoice created from that SO does not have the same value as in the SO because the subtotal in invoice does not calculate by including the extra discount.

I have looked into _create_invoices method in Sale model but I have no clues where the subtotal calculation takes place.

My goal is to just make the invoice subtotal calculation get the same value as in the current SO subtotal. Of course, other calculated fields after subtotal as well such as Untaxed Amount, Taxes, and Total must match the SO too.

================

UPDATE

Okay now that I figured out which ones to use. I override only _get_price_total_and_subtotal_model and _prepare_invoice_line. This makes me able to send extra discount from sale to invoice. However, when I create invoice from sale, the unit price shown in invoice is reduced by the discount, but if I create invoice manually from invoice (I have extra discount field in invoice too) the unit price is not reduced by the extra discount, only the subtotal is discounted, which is the result I am looking for. The only issue is that why creating invoice from sale changes unit price?

holydragon
  • 6,158
  • 6
  • 39
  • 62
  • You have to do the same logic in invoice lines. And you also have to add the `extra_discount` field also. Anywhere in sale.order.line there should be a prepare (literally _prepare in its name) method you could override to add your new field/value for the creation of invoice lines. – CZoellner Mar 12 '21 at 09:08
  • I tried overriding the method you suggested. I also tried to override create method in account.move.line and _get_price_total_and_subtotal_model. However, it looks like my overriding create method is not really overriding the actual create method because the error I get comes from the original one. Do you have any suggestions about that? – holydragon Mar 12 '21 at 10:49
  • As i've mentioned you need the field in `account.move.line` and can fill it in context of invoice creations from sale orders by overriding [`sale.order.line._prepare_invoice_line`](https://github.com/odoo/odoo/blob/852781cf64ad6bb10c552e106e93e2bb6035e34b/addons/sale/models/sale.py#L1595-L1621). And then the mess is getting complicated in Odoo 14, because they changed a lot, which i didn't know until now :/ Now i'm pissed, thank you :-P – CZoellner Mar 12 '21 at 12:18
  • The next thing you have to look into is the computation of [`price_subtotal` in invoice lines](https://github.com/odoo/odoo/blob/852781cf64ad6bb10c552e106e93e2bb6035e34b/addons/account/models/account_move.py#L3242-L3287) – CZoellner Mar 12 '21 at 12:20
  • Right now I override `_get_price_total_and_subtotal` and `_get_price_total_and_subtotal_model`. I also override `_prepare_invoice_line`. I think I might need to override `create` method in `account.move.line` too in order to add the new field to the calculation method so I did that. Now it keeps getting error at `move = self.env['account.move'].browse(vals['move_id'])` in the `create` method whenever I try to create invoice. Any idea? – holydragon Mar 15 '21 at 02:40
  • Okay now that I figured out which ones to use. I override only `_get_price_total_and_subtotal_model` and `_prepare_invoice_line`. This makes me able to send extra discount from sale to invoice. However, when I create invoice from sale, the unit price shown in invoice is reduced by the discount, but if I create invoice manually from invoice (I have extra discount field in invoice too) the unit price is not reduced by the extra discount, only the subtotal is discounted, which is the result I am looking for. The only issue is that why creating invoice from sale changes unit price? – holydragon Mar 15 '21 at 03:33

1 Answers1

0

In the end after spending a whole week figuring out how does it all work, I just need to override _get_price_total_and_subtotal_model and create in account.move.line model. and _prepare_invoice_line in sale.order.line model.

Thank you CZoellner for your advice.

holydragon
  • 6,158
  • 6
  • 39
  • 62