odoo12-创建报价单显示当前客户购买同一个产品的上一次的价格

最终实现的样子

clipboard.png

代码

sale_views.xml(不做解释,可以一起学习探讨)

<?xml version="1.0" encoding="UTF-8" ?>

<odoo>  

       <!-- Extended the sale order line view for add last price fields -->
       <record id="view_sale_order_line_inherit_sale_history" model="ir.ui.view">
            <field name="name">view.sale.order.line.inherit.sale.history</field>
            <field name="model">sale.order</field>
            <field name="inherit_id" ref="sale.view_order_form"/>
            <field name="arch" type="xml"> 
                <xpath expr="//field[@name='order_line']/tree//field[@name='price_unit']" position="before"> 
                    <field name="last_price1"/>
                </xpath>
                <xpath expr="//field[@name='order_line']/form//field[@name='price_unit']" position="before"> 
                    <field name="last_price1"/>
                </xpath>
            </field>
       </record>

</odoo>

sale.model

# -*- coding: utf-8 -*-

from odoo import api, models, fields

class SaleOrderLine(models.Model):
    _inherit = 'sale.order.line'

    last_price1 = fields.Float('Last Sale Price 1')

    @api.onchange('product_id')
    def product_id_change(self):
        super(SaleOrderLine, self).product_id_change()
        result = {}
        last_price1 = 0.0

        for record in self:
            line_ids = []
            if record.product_id:
                order_lines = self.env['sale.order.line'].sudo().search([('order_partner_id', '=', record.order_partner_id.id),('product_id', '=', record.product_id.id),('order_id.state','in',('sale','done'))])
                #self.env['sale.order.line'].sudo()    sale.order.line()
                #order_lines sale.order.line(56,)
                if order_lines:
                    for lines in order_lines:
                        line_ids.append(lines.id)
            final_list = sorted(line_ids, key=int, reverse=True)
            # final_list   [56]
            if len(final_list)>=1:
                last_price1 = self.env['sale.order.line'].sudo().browse(final_list[0])
                #self.env['sale.order.line'].sudo().browse()   sale.order.line()

                record.last_price1 = last_price1.price_unit

实现的逻辑(可能不对,一起学习探讨)

  1. 报价单必须“确认”后成为“销售订单后”才能实现
  2. 判断的是同一个客户购买过同一款产品,满足以下三个条件

    - 客户的id是不是在以购买的表中?
    - 产品的id的是不是在已购买的表中?
    - 订单的状态
    

代码的一些简单的讲解

clipboard.png

 @api.onchange('product_id')
#就是当字段发生改变时,触发绑定的函数
super(SaleOrderLine, self).product_id_change()
#Python2的class继承方法
#python3可以这么写super().product_id_change()
order_lines = self.env['sale.order.line'].sudo().search([('order_partner_id', '=', record.order_partner_id.id),('product_id', '=', record.product_id.id),('order_id.state','in',('sale','done'))])

获取到的是一个model的实例化对象,sudo()获取超级权限
self.env['sale.order.line'].sudo() ----> sale.order.line()

根据对象查找domain
.search([domain])

print(order_lines)  #ale.order.line(56,)

我们要进行正序的排序,因为我们只取最近的一次记录
final_list = sorted(line_ids, key=int, reverse=True) --->[56]

brows是浏览对象及其关联对象。从数据库中读取指定的记录,并生成对象返回。
self.env['sale.order.line'].sudo().browse() ---->sale.order.line()

评论

暂无评论