Skip to Content
Menu
This question has been flagged
2 Replies
4288 Views

I'm a Odoo user (not developer). I have a custom module that have a bug, I'm trying to understand how fix bug, but I don't find solution. I think code interested is in model file. Module, by barcode scanning in a custom field, add product line in Order with Product, Description, Qty, Unit price, but missing taxes. If same product barcode is scanned more time, is increase quantity in same product line. Bug issue is Not add product Taxes, so I have a product line without taxes. I've seen inside code, and there is not any command that invokes Taxes.

Please, anyone can help me to fix this, and say me correct code to add? I'm not a developer, I don't know nothing about code and programming.

------------First code part:  

# added the price history map

priceHistory = {}

class SaleOrder(models.Model):
"""Inherit Sale Order."""

_inherit = "sale.order"
barcode = fields.Char(string='Barcode', size=50)


def _add_product(self, product, qty, price):
"""Add line or update qty and price based on barcode."""
corresponding_line = self.order_line.filtered(lambda r: r.product_id.id == product.id)
if corresponding_line:
corresponding_line[0].product_uom_qty += float(qty)
corresponding_line[0].price_unit = float(price) or product.list_price
else:
self.order_line += self.order_line.new({
'product_id': product.id,
'product_uom_qty': qty,
'name': product.name,
'product_uom': product.uom_id.id,
'price_unit': float(price) or product.list_price,
})
return True

------------ Last code part 

            if product_id:                 # get the history price
                if price_position == -1:
                    #if priceHistory.has_key(product_id.id):
                    if product_id.id in priceHistory.keys():
                        price = priceHistory[product_id.id]

                self._add_product(product_id, qty, price)
                self.barcode = barcode = None

                #save the product price
                priceHistory[product_id.id] = price
                return

​Here I've tried to add:

  'tax_id' : account.tax

belowe line

'price_unit': float(price) or product.list_price,
but is not correct, I have error

Thank you very much


Avatar
Discard
Author Best Answer

Hi  Zbik, thank you very much for your help, your code in sale order work perfect!!

I've tryed to replicate it in purchase order also (because module work in Purchase order, Account move and Stock Picking also) but in Purchase I have error.  Can you see if I need adapt it in purchase? Please can you help me again like in sale order.

This is code I've used, what I mistake?  Thanks very much

class PurchaseOrder(models.Model):
"""Inherit Purchase Order."""

_inherit = "purchase.order"
barcode = fields.Char(string='Barcode', size=50)


def _add_product(self, product, qty, price):
"""Add line or update qty and price based on barcode."""
corresponding_line = self.order_line.filtered(lambda r: r.product_id.id == product.id)
taxes = product.taxes_id.filtered(lambda t: t.company_id.id == self.company_id.id)
taxes_ids = taxes.ids

if corresponding_line:
corresponding_line[0].product_qty += float(qty)
corresponding_line[0].price_unit = float(price) or product.list_price
if self.partner_id and self.fiscal_position_id:
taxes_ids = self.fiscal_position_id.map_tax(taxes, product, self.partner_id).ids

else:
self.order_line += self.order_line.new({
'product_id': product.id,
'product_qty': qty,
'date_planned': fields.Datetime.to_string(datetime.datetime.now() - dateutil.relativedelta.relativedelta(months=1)),
'name': product.name,
'product_uom': product.uom_id.id,
'price_unit': float(price) or product.list_price,
'tax_id': [(6, 0, taxes_ids)],
})
return True
                
            
Avatar
Discard

You calculate taxes_ids before ... if corresponding_line

Author

Hi Zbik thanks, I've solved yesterday. If you can, can yoy give me a suggestion for this https://www.odoo.com/it_IT/forum/help-1/question/custom-module-how-fix-bug-price-order-line-163337 It's a new bug I've found. Thank you very much

Best Answer

You must first calculate the taxes that should be applied based on your partner, company and fiscal position.
For example, int this way:

taxes = product.taxes_id.filtered(lambda t: t.company_id.id == self.company_id.id)
taxes_ids = taxes.ids
if self.partner_id and self.fiscal_position_id:
taxes_ids = self.fiscal_position_id.map_tax(taxes, product, self.partner_id).ids


Finally apply:

'tax_id': [(6, 0, taxes_ids)],

Avatar
Discard
Related Posts Replies Views Activity
4
Jan 16
9408
0
Mar 15
2545
1
May 18
5358
1
Mar 15
10477
0
Apr 24
320