How to compute debit, credit amount in Vendor Bill in Odoo? - 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.
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.

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.

Related

FIX Calculation error on currency - Invoice | Odoo

I uploaded some invoices in bulk to odoo, and I have a problem with the currency. Currently the currency is in USD but the values are being calculated with MXN. The lines of the invoice are in USD but the total appears with MXN. To correct it I can edit the invoice and change it to MXN, then to USD again which triggers a recalculation of the invoice and the problem is corrected. The problem is that there are more than 2000 invoices. I am trying to do a server action with this code:
for invoice in records:
# invoice.write({'currency_id': 33})
invoice._onchange_currency()
invoice._compute_amount()
# invoice.update({'currency_id': 2})
#for line in invoice['invoice_line_ids']:
# precio = line['price_unit']
# line['product_uom_id'] = 1
# line['price_unit'] = precio
But this does not trigger the calculation of the invoice. Is there any other method that does this or that allows me to simulate the aforementioned manual correction to correct these values?

Profit maximization using non linear optimization in pyomo problem framing

Background
We have 5 items in our portfolio along with the quantity sold, margin and prices of every item from last month. Predicted quantity sold of every item is dependent on its own price (through own price elasticity) and price of every other item (cross price elasticity). I am trying to write an pyomo script to increase prices of every item optimally and maximize the profit.
Example for 5 items after calculating a 5*5 matrix for elasticities.
Predicted quantity sold for every item is following function: Pred(Q1) = exp(p1 * e11 + p2 * e12 + ...... + p5 * e15)
Margin for every item is available.
Current Net profit : P1 * Q1 * Margin1 + ......... + P5 * Q5 * Margin5 = 1000
Objective : Max(P1 * (1+change p1) * Pred(Q1) * Margin 1 + ......... + P5 * (1+change p5) * Pred(Q5) * Margin 5
Constraints:
We can change the price of every item by [-5% , +10%]
Predicted quantity sold of every item with new prices should not change by
more than [-5% , +10%] of current quantities
Maximum profit achieved should be greater than 1000
All prices and quantities should be positive
Need help with programming it in pyomo with n variables and constarints, ipopt is what I think of using but open for recommendation from experts.
#Parameters
model.N = pyo.Param(model.a, initialize = qty_dict)
#Function that returns bounds on variable x
def bounds_for_y(model,l):
return (round(model.N[l]*0.95,0),round(model.N[l]*1.1,0))
#Here l is just a placeholder for set a
#Variable Declaration
model.qty = pyo.Var(model.a, domain = pyo.Integers, bounds = bounds_for_y )
Similarly for price:
#Variable declaration
model.prices = pyo.Var(model.a, domain = pyo.Reals, bounds = bounds_for_x, initialize = price_dict)
enter code here

xero sdk: line item unit price rounding to 2 decimal place

I have posted invoice in xero & passing data in 4 decimal points of unit price but xero is converting it into 2 decimal points, so i am getting total amount issue due to rounding...
Through API I know unitdp=4 have to pass but how to do same thing with sdks?
LineItem qboln = new LineItem();
qboln.Description = "Desc"
qboln.Quantity = 3385;
qboln.UnitAmount = 0.7550;
qboln.AccountCode = code;
qboln.TaxType = "INPUT";
qboln.TaxAmount = 0.678;
inv.LineItems.Add(qboln);
so here xero is rounding unit price into 0.76 so I am getting total amount issue that is different with my original invoice.
Please tell me how to solve it ?

How to add quantity based product prices in Prestashop with addon price?

How do we enter prices for below
1 Banner = Base price $20 + Artwork $5 = $25
2 Banners = Base price $20 x 2 ($40) + Artwork $5 = $45
3 Banners = Base price $20 x 3 ($60) + Artwork $5 = $65
Currently Prestashop does not allow to have a fixed addon price for artwork variable.
It should not get multiplied when the quantity increases. (Artwork is a fixed cost)
How can i do that?
You should do creating specifics prices for each quantity. In specific price only modify the quantity value. Also, a list with prices and saves will appear in product page.

Advice and feedback on dividing cash amounts into actual counts of various bills and coinage

So I need an idea of how to divide out an amount of money into actual counts of various bills and coinage. I know this is confusing, so let me give an example:
$16.32 - Sixteen dollars and thirty-two cents
One $10 bill
One $5 bill
One $1 bill
One Quarter ($0.25)
One Nickel ($0.05)
Two Pennies ($0.01)
So as you can see, we're just getting the number of bills and coinage that goes into a value, which will change according to user input.
Here's my current setup (Visual Basic):
If 100 Mod amount < 0 Then
If 50 Mod amount < 0 Then
' Continue this pattern until you get all the way down to the end ($0.01)
Else
While amount > 50
fiftiesAmount += 1
amount -= 50
End If
Else
While amount > 100
hundredsAmount += 1
amount -= 100
End If
Basically, each If statement determines whether or not your total amount needs an extra billing amount of that type, and then either adds to the amount of bills/coinage already created or moves on to the next amount.
Is this an efficient way of doing things, or am I missing out on an easier/faster algorithm/pattern that would make my life, and whoever is reading my code's life easier?
If you need extra details, I'll be happy to edit the question as needed.
Convert your amount to cents (it's easier). Divide by the currency value being tested, and then deduct that amount from the balance (pseudo-code)
Value = 16.32 * 100 ' Convert to cents
If Value > 10000 ' Hundreds
Hundreds = Value / 10000 ' How many?
Value = Value - (Hundreds * 10000) ' Reduce amount accordingly
End If
If Value > 5000 ' Fifties
Fifties = Value / 5000
Value = Value - (Fifties * 5000)
End If
If Value > 2000 ' Twenties
Twenties = Value / 2000
Value = Value - (Twenties * 2000)
End If
Repeat until you have less than 100, at which point you start with coins (50, 25, 10, 5)
Once you've got > 10, you've reached pennies; save them, reduce Value by that amount, and
Value is zero, so you're finished.