I'm a odoo user without development skills. I have a custom module with a issue. I explain how module work: I can add in purchase order products lines with custom price and quantity by scanning barcode in a New scanning field. If I scan more time same barcode, in same product line is increase quantity. In new scan barcode field, before scan barcode, I can type new product price and after scan barcode. Will be add line product with new price. If I scan barcode 1 time, will add product with qty 1, and default supplier price. If I type in new scanning field new price and scan again barcode, will update product line with 1 qty increase and new price. Without priceHistory feature, if I scan again barcode (without type new price), will add 1 qty increase and new price previous updated, it will be substitute with default supplier price. With priceHistory, I can hold (fixed) new price updated, and if I will scan again barcode, new price will not be substitute. Issue is this: new price is applied to othes PO of others suppliers also, not only in this current PO. I need priceHistory fix price history only in current purchase order. Each order own priceHistory data. Currently code work, If product Shoes I apply new price 1 $ to order PO1 with supplier A, when I will create new orders (PO2,PO3,etc) with others supplier B, supplier C, D, etc.. When I will add product Shoes, priceHistory apply always 1$ (price PO1 of supplier A). This is the Issue, I need to apply 1$ only in PO1 of supplier A, and not in all order (PO2,PO3, of others suppliers) I'm not a developer, please anyone can be help to correct code? And write me modify to do. Thanks very much
BELOW ALL COMPLETE CODE
# -*- coding: utf-8 -*-
from odoo import api, fields, models
from odoo.exceptions import Warning
import math
import dateutil.relativedelta
import datetime
import locale
# added the price history map
priceHistory = {}
class PurchaseOrder(models.Model):
"""Inherit Purchase Order."""
_inherit = "purchase.order"
barcode = fields.Char(string='Barcode', size=50)
def _add_product(self, product, qty, price):
corresponding_line = self.order_line.filtered(lambda r: r.product_id.id == product.id)
supplierinfo = product.mapped('seller_ids')[:1]
if corresponding_line:
corresponding_line[0].product_qty += float(qty)
corresponding_line[0].price_unit = float(price) or supplierinfo.price
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 supplierinfo.price,
'taxes_id': product.supplier_taxes_id,
})
return True
@api.onchange('barcode')
def barcode_scanning(self):
"""Barcode decode."""
if self.barcode:
scan_barcode = self.barcode
barcode = scan_barcode
qty_position = scan_barcode.find('*')
price_position = scan_barcode.find('/')
if price_position > 0:
price = scan_barcode[:price_position].replace(',','.')
barcode = scan_barcode[price_position + 1:]
else:
price = 0
if qty_position > 0:
qty = scan_barcode[price_position + 1:qty_position].replace(',','.')
barcode = scan_barcode[qty_position + 1:]
else:
if float(price) > 0:
qty = 0
else:
qty = 1
#added, to search internal reference
product_id = self.env['product.product'].search([('default_code', '=ilike', barcode)], limit=1)
# product_id = self.env['product.product'].search([('default_code', '=', barcode)])
if barcode and not product_id:
product_id = self.env['product.product'].search([('barcode', '=', barcode)])
if barcode and not product_id:
self.barcode = barcode = None
raise Warning('Barcode sconosciuto')
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
def _add_product(self, product, qty, price):
corresponding_line = self.order_line.filtered(lambda r: r.product_id.id == product.id)
# Get all Vendor List
supplierinfo = product.mapped('seller_ids')
for supp_info in supplierinfo:
# Match with current PO Vendor with assign product Vendor
if self.partner_id.id == supp_info.id:
# Update the Pice
price = supp_info.price
if corresponding_line:
corresponding_line[0].product_qty += float(qty)
corresponding_line[0].price_unit = float(price)
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),
'taxes_id': product.supplier_taxes_id,
})
return True