Odoo 条码扫码功能
采购订单、销售订单通过扫码增加明细
很多人都说从9.0 之后,很多社区版功能被阉割了,比如大家常说的仓库条码扫码模块就没有了。 但是却为我们留下了bcarcode模块,方便我们进行扩展。
由于有需求,需要为采购模块增加条码扫码功能,代码如下:
1.需要在purchase.order.line 增加product_barcode字段,关联自产品资料的bcarcode:
欢迎大家讨论,参考。转载,请注明出处!
由于有需求,需要为采购模块增加条码扫码功能,代码如下:
1.需要在purchase.order.line 增加product_barcode字段,关联自产品资料的bcarcode:
2.在purchase.order.line 增加一个方法on_barcode_scanned,获取扫描枪获取的条码,同时继承扩展barcodes.barcode_events_mixin,代码如下:class PurchaseOrderLine(models.Model):
_inherit = 'purchase.order.line'
product_barcode = fields.Char(related='product_id.barcode')
3.增加js代码,使得前端获取条码: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)
4.扩展purchase order view 部分,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);
});
这样,就可以在界面上直接用扫描枪扫码增加订单明细了。<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>
欢迎大家讨论,参考。转载,请注明出处!