Odoo python,产品价格仅历史记录

时间:2020-02-13 09:05:29

标签: python odoo

我是一个没有开发技能的odoo用户。 我有一个有问题的自定义模块。 我将说明模块的工作原理:通过在“新扫描”字段中扫描条形码,可以添加具有自定义价格和数量的采购订单产品行。如果我扫描更多时间相同的条形码,则在同一条生产线上增加数量。在新扫描条形码字段中,在扫描条形码之前,我可以输入新产品价格,在扫描条形码之后。将以新价格添加产品线。 如果我扫描条形码1次,将添加数量为1的产品,并且默认供应商价格。如果我在新的扫描字段中输入新价格并再次扫描条形码,将以增加1数量和新价格来更新产品线。没有priceHistory功能,如果我再次扫描条形码(不输入新价格),将增加1数量,并且先前更新了新价格,它将替换为默认供应商价格。使用priceHistory,我可以保持(固定)新价格的更新,并且如果我再次扫描条形码,则不能替代新价格。 问题是这样的: 新价格也适用于其他供应商的其他采购订单,不仅在当前采购订单中。我仅需要当前购买订单中的priceHistory固定价格历史记录。每个订单都有其priceHistory数据。 当前代码正常工作,如果产品鞋子我将新价格1 $应用于与供应商A的订单PO1,那么我将与其他供应商B,供应商C,D等创建新订单(PO2,PO3等)。何时添加product鞋,priceHistory始终适用1 $(供应商A的价格PO1)。这是问题,我只需要在供应商A的PO1中应用1 $,而不是在所有顺序中(其他供应商的PO2,PO3)应用1 $ 我不是开发人员,请帮助任何人纠正代码?并写信给我修改做。 非常感谢

在所有完整代码下方

# -*- 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                

1 个答案:

答案 0 :(得分:1)

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