I've been trying to solve a problem we have in my company. We use a prestashop web page that's connected to odoo. Sometimes, each platform get a different price in the orders (just cents but you can't pass the order if the difference is bigger than 0.03, we can change that number but dont want). I did a python program that gave me the new values to adjust the prices in odoo, and now I'm trying to implement it in odoo, it turns out that it's easier said than done. In order to call the function, I need to search an order in the odoo interface, click edit and change a value (if the difference between both prices are bigger than 0.03 the function that gets called), then I can see in the terminal that prices are recalculated perfectly (even though the function is called too many times, twice per order.order_line, don't know why). At that moment, I can see the ddbb directly and check that the value has been actually changed.
Problem: when clicking the save button, the function is called yet again, why?, when odoo rereads each line in the order, it takes the previous value (just the first line) and keeps modifying that value only.
So, does someone know why that happens?, and more importantly how could I make it so that odoo reads the last value?
we are using odoo 9. There are no errors, just wrong results.
I attach only the pieces of code that might be causing the problem. The initial function is amount_all, this one calls self._price_readjuster_when_different_odooPR(), within that function updatingLinePrices is called.
Hope everything is clear to understand
class SaleOrder(models.Model): _inherit = 'sale.order'
def updatingLinePrices(self, order, price_adjusted):
## Updates the new price for each product
for line, newPrice in zip(order.order_line, price_adjusted):
#import pdb; pdb.set_trace()
try:
if order.order_line.search([['id', '=', line.id], ['name', '=', '[DISCOUNT] Cupon de Descuento']]).id == False:
#56 => 10%
tax = 1.1 if str(line.tax_id.id) == 56 else 1.21 #
if not self.env.context.get('line'):
line.with_context(line=True).write({
"price_subtotal": newPrice * line.product_uom_qty,
"price_unit": newPrice,
"price_tax": (newPrice*tax)-newPrice,
"price_total": newPrice*tax,
"price_reduce": newPrice,
})
except Exception as e:
print("Error updating to new prices: {}".format(e))
return line
@api.depends('order_line.price_total')
def _amount_all(self):
"""
Overrides method to calculate amount_tax over price_tax ignoring tax calculation method.
"""
#print("_amount_all function has been called")
for order in self:
amount_total = amount_untaxed = amount_tax = 0.0
for line in order.order_line:
amount_untaxed += line.price_subtotal
amount_tax += line.price_tax
amount_total = amount_untaxed + amount_tax
if self.prestashop_bind_ids.total_amount_tax and self.prestashop_bind_ids.total_amount:
variation_in_tax = abs(self.prestashop_bind_ids.total_amount_tax - amount_tax)
variation_in_total = abs(self.prestashop_bind_ids.total_amount - amount_total)
variation = variation_in_tax + variation_in_total
print('\n\nvariation_tax: {}\nvariation_total: {}\nVariation: {}\n\n'.format( variation_in_tax, variation_in_total, variation))
if variation >= 0.03 and variation_in_tax >= 0.03 or variation_in_total > 0.03:
self._price_readjuster_when_different_odooPR()
order.update({
'amount_untaxed': order.pricelist_id.currency_id.round(amount_untaxed),
'amount_tax': order.pricelist_id.currency_id.round(amount_tax),
'amount_total': amount_total,
})