POS 产品订单行

POS product order lines

在产品表单视图中有按钮销售。当您激活时,它会显示包含该产品所有销售订单的树状视图。我的目标是制作相同的按钮,但它必须显示使用该产品制作的所有 pos 订单。

我试过类似的东西,但我知道这完全是垃圾。如果有人能向我解释它是如何工作的,我会更加感激

<record id="act_product_pos_sale" model="ir.actions.act_window">
            <field name="name">POS Product Sale1</field>
            <field name="res_model">product.product</field>
            <field name="view_id" ref="product.product_product_tree_view"/>
        </record>

        <record model="ir.ui.view" id="product_form_pos_sale_button">
            <field name="name">product.product.sale.pos.order</field>
            <field name="model">product.product</field>
            <field name="inherit_id" ref="product.product_normal_form_view"/>
            <field name="arch" type="xml">
                <div name="button_box" position="inside">
                    <button class="oe_stat_button" name="action_view_pos_product"
                        type="object" icon="fa-usd">
                        <field string="POS" name="pos_product_order_total" widget="statinfo" />
                    </button>
                </div>
            </field>
        </record>


class ProductProduct(models.Model):
    _inherit = 'product.product'

 @api.multi
    def action_view_pos_product(self):
        OrderLine = self.env['pos.order.line']
        action = self.env.ref('sale.act_product_pos_sale')
        # action['domain'] = [('product_id', 'in', products.ids)]
        # action['context'] = {'': ,}
        return action

您添加操作并使用按钮调用该操作: 第 1 步:您必须使用计算方法计算总销售数量:

class ProductProduct(models.Model):
    _inherit = 'product.product'

    @api.multi
    def _pos_sales_count(self):
        r = {}
        domain = [
            ('state', 'in', ['sale', 'done']),
            ('product_id', 'in', self.ids),
        ]
        for group in self.env['report.pos.order'].read_group(domain, ['product_id', 'product_qty'], ['product_id']):
            r[group['product_id'][0]] = group['product_qty']
        for product in self:
            product.sales_count = r.get(product.id, 0)
        return r

    pos_sales_count = fields.Integer(compute='_pos_sales_count', string='#Pos Sales')


 class ProductTemplate(models.Model):
    _inherit = 'product.template'

    @api.multi
    @api.depends('product_variant_ids.pos_sales_count')
    def _pos_sales_count(self):
        for product in self:
            product.pos_sales_count = sum([p.sales_count for p in product.product_variant_ids])

    pos_sales_count = fields.Integer(compute='_pos_sales_count', string='#POS Sales')

第 2 步:定义对 link 与产品相关的 pos 订单行的操作:

<record id="action_product_pos_sale_list" model="ir.actions.act_window">
    <field name="name">Sale Order Lines</field>
    <field name="res_model">pos.order.line</field>
    <field name="context">{'search_default_product_id': [active_id], 'default_product_id': active_id}</field>
</record>

<record model="ir.ui.view" id="product_form_view_pos_sale_order_button">
    <field name="name">product.product.pos.sale.order</field>
    <field name="model">product.product</field>
    <field name="inherit_id" ref="product.product_normal_form_view"/>
    <field name="groups_id" eval="[(4, ref('sales_team.group_sale_salesman'))]"/>
    <field name="arch" type="xml">
        <div name="button_box" position="inside">
            <button class="oe_stat_button" name="%(action_product_pos_sale_list)d"
                type="action" icon="fa-usd">
                <field string="Sales" name="pos_sales_count" widget="statinfo" />
            </button>
        </div>
    </field>
</record>