FIX Calculation error on currency - Invoice | Odoo - 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?

Related

Shopify Scripts - Tiered product discount by quantity only when same variant

This is a good example https://help.shopify.com/en/manual/checkout-settings/script-editor/examples/line-item-scripts#tiered-product-discount-by-quantity
But I need it to work only if you enter code herechoose the same variant. You cant mix and match. The product has multiple variants but I only want the discount to apply if the product is in the same variant group.
discount +10 same products
Example 1 correct
Phone / variant Black x10 = discount
phone / variant Red x5 = NO discount
Example 2 incorrect
Phone / variant Black x5 = discount
phone / variant Red x5 = discount
Discount applied because total is 10 but its incorrect because the variant is not the same
Thanks for the help.
Not sure if I understand the question but this should be a good start.
def apply_discount(cart, min_quantity, perc_discount)
cart.line_items.each do |line_item|
if line_item.quantity > min_quantity
new_line_price = line_item.line_price * perc_discount
line_item.change_line_price(new_line_price, message: "More than 10 same product!")
end
end
end
apply_discount(Input.cart, 10, 0.90)
We just check line by line if it's more than min_quantity we apply the discount.
If you want to apply the discount only once you can just put a return inside the if
This will not work if you have discounts already applied to a subset of items of the same variant... line items are separated by sku and price, so for that you would need something more complex. But I was not sure you were interested in that case.

calculate new price_unit in sale order line

I have created a new custom module to include width and lenght of a product as attributes with custom values, and I need to update the price_unit of the sales order line with the current unit price multiplied by the (lenght*witdh) value, so I can have the price per surface unit in the product card, and the price per total surface in the sales order line.
I use the product configurator and this is my code:
from odoo import models, fields, api
# import pymsgbox
class aurea_calculated_field_line(models.Model):
_inherit = 'sale.order.line'
field_superficie = fields.Float('Superficie')
field_alto = fields.Integer('Alto')
field_ancho = fields.Integer('Ancho')
#api.onchange('field_alto', 'field_ancho', 'product_uom_qty', 'quantity')
def _value_pc5(self):
for record in self:
record.field_superficie = record.field_ancho * record.field_alto
self.price_unit = float(self.field_superficie) * self.product_id.lst_price
#api.onchange('product_id')
def _value_pc4(self):
if not self.product_custom_attribute_value_ids and not self.product_no_variant_attribute_value_ids:
return ""
for pacv in self.product_custom_attribute_value_ids:
if pacv.custom_product_template_attribute_value_id.display_name == 'Largo: Largo':
self.field_alto = pacv.custom_value
if pacv.custom_product_template_attribute_value_id.display_name == 'Ancho: Ancho':
self.field_ancho = pacv.custom_value
It works fine, but the problem is when I change the product quantity the unit price is reset to the product pricelist price.
Any suggestions?
Thanks in advance
I looked at this a bit. I didn't reproduce the problem but here is something you probably can work with.
In Odoo13 source code in file addons/sale/models/sale.py on line 1614 is a method which causes behavior you have problem with:
#api.onchange('product_uom', 'product_uom_qty')
def product_uom_change(self):
if not self.product_uom or not self.product_id:
self.price_unit = 0.0
return
if self.order_id.pricelist_id and self.order_id.partner_id:
product = self.product_id.with_context(
lang=self.order_id.partner_id.lang,
partner=self.order_id.partner_id,
quantity=self.product_uom_qty,
date=self.order_id.date_order,
pricelist=self.order_id.pricelist_id.id,
uom=self.product_uom.id,
fiscal_position=self.env.context.get('fiscal_position')
)
self.price_unit = self.env['account.tax']._fix_tax_included_price_company(self._get_display_price(product), product.taxes_id, self.tax_id, self.company_id)
This re-calculates unit price when product quantity is changed. You'd have to overwrite this method in your code. Easiest way would be add this to your aurea_calculated_field_line-class.
def product_uom_change(self):
return
But this needs a lot of testing to make sure that some other feature does not break because of this.

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

Implement a vectorized stop loss when price goes below a threshold using pandas DataFrame

Given this example DataFrame:
date;close;signal;positions
2017-01-02;27.90;0.0;0.0
2017-01-03;27.76;0.0;0.0
2017-01-04;28.65;1.0;1.0
2017-01-05;28.72;1.0;0.0
2017-01-06;28.00;1.0;0.0
2017-01-09;27.03;1.0;0.0 # <<<--- Note the price is -5% when compared to 28.65 (in 2017-01-04)
2017-01-10;28.26;1.0;0.0
2017-01-11;28.35;0.0;-1.0 # <<-- Sell
2017-01-12;29.12;0.0;0.0
2017-01-13;28.99;0.0;0.0
2017-01-16;28.50;1.0;1.0
2017-01-17;28.45;1.0;0.0
2017-01-18;29.06;1.0;0.0
2017-01-19;28.74;0.0;-1.0
2017-01-20;28.76;0.0;0.0
2017-01-23;29.50;0.0;0.0
2017-01-24;29.12;1.0;1.0
2017-01-25;29.87;1.0;0.0
2017-01-26;27.22;1.0;0.0 # <<<--- Note the price is -5% when compared to 29.12 (in 2017-01-24)
2017-01-27;29.76;1.0;0.0 # <<-- still holding the position...
I want to implement a "stop loss" when the prices go bellow, let's say, 5%. In this case, the DataFrame should look like as:
date;close;signal;positions
2017-01-02;27.90;0.0;0.0
2017-01-03;27.76;0.0;0.0
2017-01-04;28.65;1.0;1.0 # <<-- Buy
2017-01-05;28.72;1.0;0.0
2017-01-06;28.00;1.0;0.0
2017-01-09;27.03;0.0;-1.0 # <<-- Sell with stop-loss
2017-01-10;28.26;0.0;0.0
2017-01-11;28.35;0.0;0.0
2017-01-12;29.12;0.0;0.0
2017-01-13;28.99;0.0;0.0
2017-01-16;28.50;1.0;1.0 # <<-- Buy
2017-01-17;28.45;1.0;0.0
2017-01-18;29.06;1.0;0.0
2017-01-19;28.74;0.0;-1.0 # <<-- Sell with profit
2017-01-20;28.76;0.0;0.0
2017-01-23;29.50;0.0;0.0
2017-01-24;29.12;1.0;1.0 # <<-- Buy
2017-01-25;29.87;1.0;0.0
2017-01-26;27.22;0.0;-1.0 # <<-- Sell with stop-loss
2017-01-27;29.76;0.0;0.0
(Please note the changes in 2017-01-09 and 2017-01-26).
Just to make it clear, the "signal" column represents holding positions when it is 1.0. The "positions" column was computed using df['signal'].diff() and it represents a buy when equal to 1.0 and a sell when it is equal to -1.0.
Thank you in advance!
IIUC now...
data['cor_price'] = data['close'].where((data['signal'] == 1) & (data['positions'] == 1), pd.np.nan)
data['cor_price'] = data['cor_price'].ffill().astype(data['close'].dtype)
data['diff_perc'] = (data['close'] - data['cor_price']) / data['cor_price']
data['positions2'] = np.where(data['diff_perc'] <= -0.05, 1, 0)
data
This might be open for improvement, but I took it step by step. First create a column with the corresponding buying price and forward fill. Then create a column with the price differences and lastly create positions2 which shows the result you need.

How to increase shipping cost according to quantity

I am currently setting up the shipping settings for my site www.lordswoods.co.uk
I have set up two carriers
Yodel- maximum package weight 30kg (carrier range 1kg to 75kg) price £7.75
Pallet track - for all total orders above 75kg
When I order a 1 x 20kg item such as pro master 5 - economy green. The shipping price in the cart is £7.75 as it should be. However when I increase the quantity to two, the shipping price stays the same at £7.75, whereas I want it to to be £7.75 per item until 75 kg is reached.
e.g., 3 x promaster 5 should equal £23.25 (3 x 7.75)
How do I set this up?
You can try adding the shipping cost to all the products separately instead of adding it in the carrier. That way your shipping charges will increase for each item added to the cart.