贴代码
odoo.define('Product.barcode', function (require) {
"use strict";
var FormController = require('web.FormController');
FormController.include({
_barcodeScanned: async function (barcode, target) {
this._super.apply(this, arguments);
if (this.modelName === "stock.picking" && this.initialState.data.state === '新建'){
var date = await this._rpc({
"model": "stock.picking",
"method": "on_barcode_scanned",
"args": [this.initialState.res_id, barcode],
});}
this.reload();
},
});
});
#调用 models 模型函数方法
def _add_product(self, product, qty=1.0):
order_line = self.move_lines.filtered(lambda r: r.product_id.id == product.id)
if order_line:
order_line.product_uom_qty += qty
self.env.user.notify_success(message='添加物资:%s 成功' % product.name)
else:
product_lang = product.with_context({
'lang': self.partner_id.lang,
'partner_id': self.partner_id.id,
})
name = product_lang.display_name
vals = {
'product_id': product.id,
'name': name,
'product_uom': product.uom_id.id,
'product_uom_qty': 1,
'price_subtotal': product.purchase_price,
'warehouse_id': product.warehouse_id.id,
}
self.move_lines = [(0, 0, vals)]
self.env.user.notify_success(message='添加物资:%s 成功' % product.name)
def on_barcode_scanned(self, barcode):
product = self.env['product.product'].search([('default_code', '=', barcode)])
if product:
self._add_product(product)
else:
self.env.user.notify_success(message='没有编码为:%s 的物资 ' % barcode)