簡體   English   中英

odoo 中銷售訂單行中具有庫存數量的產品過濾器

[英]Product filter with stock quantity in sale order line in odoo

在此處輸入圖像描述

 class sale_product_filter(models.Model): _inherit= "sale.order.line" def _get_custom_domain(self): return [('categ_id.branch_id','=',self.env.user.branch_id.id)] product_id = fields.Many2one(domain= lambda self: self._get_custom_domain()) <record id="sale_order_form_inherit" model="ir.ui.view"> <field name="name">sale.order.form.inherit</field> <field name="model">sale.order</field> <field name="inherit_id" ref="sale.view_order_form"/> <field name="priority" eval="8"/> <field name="arch" type="xml"> <xpath expr="//field[@name='order_line']/tree/field[@name='product_id']" position="attributes"> <attribute name="domain"/> </xpath> </field> </record> <record id="rent_order_form_inherit" model="ir.ui.view"> <field name="name">sale.order.form.inherit</field> <field name="model">sale.order</field> <field name="inherit_id" ref="sale_renting.rental_order_primary_form_view"/> <field name="priority" eval="8"/> <field name="arch" type="xml"> <xpath expr="//field[@name='order_line']/tree/field[@name='product_id']" position="attributes"> <attribute name="domain"/> </xpath> </field> </record>

我想過濾銷售訂單行中的產品。 當前登錄用戶有自己的倉庫,里面有很多商品。 我有很多用戶的倉庫。所以,我想在銷售訂單行中只顯示登錄用戶的倉庫產品。

我該怎么做?每個用戶都有分支字段,倉庫也有分支字段。當我在銷售訂單行中添加產品時,我只想顯示倉庫中的產品,該分支與當前用戶的分支相同,倉庫中的產品數量為大於 0。

將默認的product_id域替換為計算域以包含當前用戶分支。

下面的示例定義了一個計算域來過濾產品並僅顯示與當前用戶在同一分支中的產品。

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

    def _get_custom_domain(self):
        return [('categ_id.branch_id', '=', self.env.user.branch_id.id)]

    product_id = fields.Many2one(domain=lambda self: self._get_custom_domain())

繼承表單視圖並替換product_id域:

<record id="view_order_form" model="ir.ui.view">
        <field name="name">sale.order.form</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
        <field name="arch" type="xml">
          <xpath expr="//tree/field[@name='product_id']" position="attributes">
              <attribute name="domain"/>
          </xpath>
        </field>
    </record>  

您可以通過在上下文中指定倉庫來計算特定倉庫中的產品可用數量:

product_obj.with_context(warehouse=warehouse_id.id).browse(product_id).qty_available

搜索quantity > 0的產品,然后使用域中的id字段:

[('id', 'in', filtered_product_ids)]

例子:

def _get_custom_domain(self):
    warehouse_id = self.env['stock.warehouse'].search([('branch_id', '=', self.env.user.branch_id.id)])
    product_ids = self.env['product.product'].with_context(warehouse=warehouse_id.ids).search([]).filtered(lambda p:p.qty_available > 0)
    return [('id', 'in', product_ids.ids)]

編輯:從操作中自定義域

嘗試向操作上下文添加一個額外的域參數:

'additional_domain': [('sale_ok', '=', True)

如果它在操作上下文中可用,則將其添加到計算域:

return [('id', 'in', product_ids.ids)] + self.env.context.get('additional_domain', [])

編輯:繼承引用操作以更改上下文

<record id="sale.action_quotations_with_onboarding" model="ir.actions.act_window">
    <field name="context">{'search_default_my_quotation': 1, 'additional_domain': [('sale_ok', '=', True)]}</field>
</record>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM