Odoo 条码扫码功能 采购订单、销售订单通过扫码增加明细

可以再次下载 :Odoo 销售扫码

https://apps.odoo.com/apps/modules/11.0/sale_barcodes/

很多人都说从9.0 之后,很多社区版功能被阉割了,比如大家常说的仓库条码扫码模块就没有了。 但是却为我们留下了bcarcode模块,方便我们进行扩展。
由于有需求,需要为采购模块增加条码扫码功能,代码如下:
1.需要在purchase.order.line 增加product_barcode字段,关联自产品资料的bcarcode:

1
2
3
4
class PurchaseOrderLine(models.Model):
    _inherit = 'purchase.order.line'
  
    product_barcode = fields.Char(related='product_id.barcode')

  

2.在purchase.order.line 增加一个方法on_barcode_scanned,获取扫描枪获取的条码,同时继承扩展barcodes.barcode_events_mixin,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class PurchaseOrder(models.Model):
    _name = 'purchase.order'
    _inherit = ['purchase.order', 'barcodes.barcode_events_mixin']
  
    def _add_product(self, product, qty=1.0):
        order_line = self.order_line.filtered(lambda r: r.product_id.id == product.id)
        if order_line:
            order_line.product_qty = qty
        else:
            product_lang = product.with_context({
                'lang': self.partner_id.lang,
                'partner_id': self.partner_id.id,
            })
            name = product_lang.display_name
            if product_lang.description_purchase:
                name += '\n' + product_lang.description_purchase
  
            vals = {
                'product_id': product.id,
                'name': name,
                'date_planned': fields.Datetime.now(),
                'product_uom': product.uom_po_id.id,
                'product_qty': 1,
                'price_unit': product.standard_price,
                'state': 'draft',
            }
  
            self.order_line += self.order_line.new(vals)
  
    def on_barcode_scanned(self, barcode):
        product = self.env['product.product'].search([('barcode', '=', barcode)])
        if product:
            self._add_product(product)

  

3.增加js代码,使得前端获取条码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
odoo.define('purchase_barcode.PurchaseOrderBarcodeHandler', function (require) {
    "use strict";
  
    var field_registry = require('web.field_registry');
    var AbstractField = require('web.AbstractField');
    var FormController = require('web.FormController');
  
    FormController.include({
        _barcodePurchaseAddRecordId: function (barcode, activeBarcode) {
            if (!activeBarcode.handle) {
                return $.Deferred().reject();
            }
            var record = this.model.get(activeBarcode.handle);
            if (record.data.state != 'draft') {
                this.do_warn("采购订单", '只能对草稿状态的单据增加明细');
                return $.Deferred().reject();
            }
            return this._barcodeAddX2MQuantity(barcode, activeBarcode);
        }
    })
  
    var PurchaseOrderBarcodeHandler = AbstractField.extend({
        init: function () {
            this._super.apply(this, arguments);
  
            this.trigger_up('activeBarcode', {
                name: this.name,
                fieldName: 'order_line',
                quantity: 'product_qty',
                setQuantityWithKeypress: true,
                commands: {
                    // 'O-CMD.MAIN-MENU': _.bind(this.do_action, this, 'stock_barcode.stock_barcode_action_main_menu', {clear_breadcrumbs: true}),
                    barcode: '_barcodePurchaseAddRecordId',
                }
            });
        },
    });
  
    field_registry.add('purchaseorder_barcode_handler', PurchaseOrderBarcodeHandler);
  
});

  

4.扩展purchase order view 部分,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<record id="view_purchase_order_barcode_inherit_form" model="ir.ui.view">
    <field name="name">purchase.order.form</field>
    <field name="model">purchase.order</field>
    <field name="inherit_id" ref="purchase.purchase_order_form"/>
    <field name="priority" eval="8"/>
    <field name="arch" type="xml">
        <form position="inside">
            <field name="_barcode_scanned" widget="purchaseorder_barcode_handler"/>
        </form>
  
        <xpath expr="//field[@name='order_line']/tree" position="inside">
            <field name="product_barcode" invisible="1"/>
        </xpath>
        <xpath expr="//field[@name='order_line']/kanban" position="inside">
            <field name="product_barcode" invisible="1"/>
        </xpath>
        <xpath expr="//field[@name='order_line']//field[@name='product_qty']" position="attributes">
            <attribute name="options">{'barcode_events': True}</attribute>
            <attribute name="widget">field_float_scannable</attribute>
        </xpath>
        <xpath expr="//button[@name='button_confirm']" position="attributes">
            <attribute name="barcode_trigger">validate</attribute>
        </xpath>
    </field>
</record>

  

这样,就可以在界面上直接用扫描枪扫码增加订单明细了。

 
posted @   CrossPython  阅读(358)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示