Jeremy Gillbert
Jeremy Gillbert

Reputation: 133

How to compute debit, credit amount in Vendor Bill in Odoo?

I am new to Odoo Accounting. I have been encountering a problem in calculating debit and credit amount of Vendor Bill. I have added two new fields in purchase order line, discount_type and discount_amt. The subtotal value must be (price_unit * quantity) - discount. I could compute the subtotal amount. But when I check the journal items, the debit and credit amount are not changed. I mean the discount amount are not subtracted. But when I saved the form, I got the error which said debit and credit were not balanced. How can I do that?

    def compute_price_subtotal(self):

    for line in self:

        line.discount_type = line.purchase_line_id.discount_type
        line.discount_amt = line.purchase_line_id.discount_amt

        qty = line.quantity or 0
        price_unit = line.price_unit or 0

        subtotal = price_unit * qty

        discount_type = line.discount_type
        discount_amount = line.discount_amt

        if discount_type == 'fixed':
            discount = discount_amount * qty
            line.price_subtotal = subtotal - discount

        elif discount_type == 'percentage':
            discount = subtotal * (discount_amount / 100)
            line.price_subtotal = subtotal - discount

        else:
            line.price_subtotal = subtotal

        if line.move_id.type in line.move_id.get_outbound_types():
            sign = 1
        elif line.move_id.type in line.move_id.get_inbound_types():
            sign = -1
        else:
            sign = 1

        price_subtotal = sign * line.price_subtotal

        line.update({
            'debit': price_subtotal > 0.0 and price_subtotal or 0.0,
            'credit': price_subtotal < 0.0 and -price_subtotal or 0.0,
        })

The above method is to calculate the price_subtotal, debit and credit.

enter image description here

In the picture, the untaxed amount is 13800 and tax is 690. So the total amount will be 13800 + 690 = 14490. But in the Journal items, it shows 15000 and the subtotal values are different.

enter image description here

Upvotes: 1

Views: 1031

Answers (1)

Levizar
Levizar

Reputation: 85

This is because you only modify the line you are interested in. This tries to modify the debit/credit but it is leaving the account move unbalanced. As this violate a constrains, the modification of the accounting is prevented.

You need to balance the move before updating anything in it. This involves updating the tax line and the payable/receivable line too. Once you have all of those lines, you can update the whole account move.

This means that you have to make something like this: (assuming the calculation is already done)


lines_to_write = [
    (1, line_A_id, line_A_values),
    (1, tax_line_id, tax_line_values),
    (1, receivable_line_id, receivable_line_values)
]
move.write({'line_ids': lines_to_write})

You can get the list of command here

Btw, to recompute the debit and credit when a business field is changed, you can (should) call the method _get_fields_onchange_subtotal_model to get the new values and then to update the account move line with those new values.

One of the reason is the fact that the accounting and the invoice could be in different currency.

Disclaimer: you should modify taxes only if you are sure of what you are doing. This can have an impact on the user's tax report.

Upvotes: 2

Related Questions