增加平板扫码功能
This commit is contained in:
4
sf_machine_connect/static/src/css/barcode_button.css
Normal file
4
sf_machine_connect/static/src/css/barcode_button.css
Normal file
@@ -0,0 +1,4 @@
|
||||
|
||||
.barcode_button {
|
||||
outline: none;
|
||||
}
|
||||
126
sf_machine_connect/static/src/js/barcode_button.js
Normal file
126
sf_machine_connect/static/src/js/barcode_button.js
Normal file
@@ -0,0 +1,126 @@
|
||||
/** @odoo-module **/
|
||||
|
||||
import { browser } from "@web/core/browser/browser";
|
||||
import { Dialog } from "@web/core/dialog/dialog";
|
||||
import { _lt } from "@web/core/l10n/translation";
|
||||
import { useChildRef, useOwnedDialogs, useService } from "@web/core/utils/hooks";
|
||||
import { sprintf } from "@web/core/utils/strings";
|
||||
import { isMobileOS } from "@web/core/browser/feature_detection";
|
||||
import * as BarcodeScanner from "@web/webclient/barcode/barcode_scanner";
|
||||
const { useRef } = owl;
|
||||
const {xml, Component} = owl;
|
||||
import { standardFieldProps } from "@web/views/fields/standard_field_props";
|
||||
import {registry} from "@web/core/registry";
|
||||
import {useInputField} from "@web/views/fields/input_field_hook";
|
||||
|
||||
export class CodeField extends Component {
|
||||
setup() {
|
||||
console.log('CodeField created')
|
||||
console.log('this',this)
|
||||
console.log('this.props',this.props)
|
||||
console.log('this.props.record',this.props.value)
|
||||
useInputField({
|
||||
getValue: () => this.props.value,
|
||||
refName: "scan_code",
|
||||
});
|
||||
console.log('我是setup1')
|
||||
super.setup();
|
||||
this.orm = this.env.services.orm;
|
||||
this.record = this.props.record;
|
||||
}
|
||||
|
||||
async onBarcodeBtnClick() {
|
||||
const barcode = await BarcodeScanner.scanBarcode();
|
||||
if (barcode) {
|
||||
await this.onBarcodeScanned(barcode);
|
||||
if ("vibrate" in browser.navigator) {
|
||||
browser.navigator.vibrate(100);
|
||||
}
|
||||
} else {
|
||||
this.notification.add(this.env._t("Please, scan again !"), {
|
||||
type: "warning",
|
||||
});
|
||||
}
|
||||
}
|
||||
async search(barcode) {
|
||||
// alert('我是search')
|
||||
const domain = [["code", "=", barcode]];
|
||||
console.log(domain)
|
||||
const fields = ["id", "code", "name", "state"];
|
||||
console.log(fields)
|
||||
const results = await this.orm.call("sf.tray", "search_read", [domain, fields]);
|
||||
const values = await this.orm.call("sf.tray", "search_read", [domain]);
|
||||
console.log(results)
|
||||
return results.map((result) => {
|
||||
return {
|
||||
id: result.id,
|
||||
code: result.code,
|
||||
name: result.name,
|
||||
state: result.state,
|
||||
values: values,
|
||||
};
|
||||
});
|
||||
}
|
||||
async onBarcodeScanned(barcode) {
|
||||
const results = await this.search(barcode);
|
||||
console.log(results)
|
||||
const records = results.filter((r) => !!r.id);
|
||||
console.log(records)
|
||||
if (records.length === 1) {
|
||||
if (records[0].state === '空闲') {
|
||||
console.log('currentModel',this)
|
||||
console.log('this.record.data',this.record.data)
|
||||
console.log('this.record.data.id', this.record.data.id)
|
||||
const workorder = await this.orm.call('mrp.workorder', 'read', [this.record.data.id]);
|
||||
console.log('workorder', workorder[0])
|
||||
const updatedRecord = await this.orm.call("sf.tray", "write", [
|
||||
[records[0].id],
|
||||
{
|
||||
state: "占用",
|
||||
workorder_id: workorder[0].id,
|
||||
production_id: workorder[0].product_id[0],
|
||||
// workorder_id: workorder.id,
|
||||
}]);
|
||||
console.log(workorder[0].routing_type);
|
||||
console.log(workorder[0].production_id);
|
||||
// const productionIDS = await this.orm.call('mrp.production', 'search', [[['id', '=', workorder[0].production_id[0]]]]);
|
||||
const productionIDS = await this.orm.call('mrp.workorder', 'search', [[["production_id", "=", workorder[0].production_id[0]]]]);
|
||||
console.log('prooooooo', productionIDS);
|
||||
console.log('values', records[0].values[0]);
|
||||
productionIDS.forEach(async (data) => {
|
||||
// 处理每一个数据
|
||||
console.log(data);
|
||||
const updatetrayRecord = await this.orm.call("mrp.workorder", "write", [
|
||||
[data],
|
||||
{
|
||||
tray_id: records[0].values[0].id,
|
||||
// tray_id: false,
|
||||
}]);
|
||||
console.log(updatetrayRecord)
|
||||
});
|
||||
this.props.update(records[0].code, records[0].tray_id);
|
||||
location.replace(location.href)
|
||||
// location.reload()
|
||||
} else {
|
||||
if (records[0].state === '占用') {
|
||||
console.log('此托盘已占用,请检查')
|
||||
alert('此托盘已占用,请检查')
|
||||
} else {
|
||||
console.log('此托盘已损坏,请登记')
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
const searchInput = this.autocompleteContainerRef.el.querySelector("input");
|
||||
searchInput.value = barcode;
|
||||
searchInput.dispatchEvent(new Event("input"));
|
||||
if (this.env.isSmall) {
|
||||
searchInput.click();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
CodeField.template = 'sf_machine_connect.CodeField'
|
||||
// Register the field in the registry
|
||||
CodeField.props = standardFieldProps;
|
||||
registry.category("fields").add("code", CodeField);
|
||||
51
sf_machine_connect/static/src/js/barcode_form.js
Normal file
51
sf_machine_connect/static/src/js/barcode_form.js
Normal file
@@ -0,0 +1,51 @@
|
||||
odoo.define('my_module.barcode_handler', function (require) {
|
||||
"use strict";
|
||||
|
||||
var core = require('web.core');
|
||||
var registry = require('web.field_registry');
|
||||
var session = require('web.session');
|
||||
var FieldChar = require('web.basic_fields').FieldChar;
|
||||
|
||||
var _t = core._t;
|
||||
|
||||
var BarcodeHandlerField = FieldChar.extend({
|
||||
init: function () {
|
||||
this._super.apply(this, arguments);
|
||||
this.scanInProgress = false;
|
||||
},
|
||||
willStart: function () {
|
||||
return this._super.apply(this, arguments).then(() => {
|
||||
var barcode = this.call('barcode', 'get_barcode');
|
||||
if (barcode) {
|
||||
this._onBarcodeScanned(barcode);
|
||||
}
|
||||
this.call('barcode', 'start_listening');
|
||||
});
|
||||
},
|
||||
destroy: function () {
|
||||
this.call('barcode', 'stop_listening');
|
||||
this._super.apply(this, arguments);
|
||||
},
|
||||
_onBarcodeScanned: function (barcode) {
|
||||
if (this.scanInProgress) {
|
||||
return;
|
||||
}
|
||||
this.scanInProgress = true;
|
||||
var self = this;
|
||||
session.rpc('/mrp_barcode/scan_to_open_report_from_form', { barcode: barcode }).then(function (result) {
|
||||
self.scanInProgress = false;
|
||||
self.$el.val(result);
|
||||
self.trigger_up('field_changed', {
|
||||
dataPointID: self.dataPointID,
|
||||
changes: { value: result },
|
||||
});
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
registry.add('barcode_handlerr', BarcodeHandlerField);
|
||||
|
||||
return {
|
||||
BarcodeHandlerField: BarcodeHandlerField,
|
||||
};
|
||||
});
|
||||
77
sf_machine_connect/static/src/js/barcode_handler_field.js
Normal file
77
sf_machine_connect/static/src/js/barcode_handler_field.js
Normal file
@@ -0,0 +1,77 @@
|
||||
/** @odoo-module **/
|
||||
|
||||
import { registry } from "@web/core/registry";
|
||||
import { standardFieldProps } from "@web/views/fields/standard_field_props";
|
||||
import { useBus, useService } from "@web/core/utils/hooks";
|
||||
const { Component, xml } = owl;
|
||||
|
||||
|
||||
export class BarcodeHandlerField extends Component {
|
||||
setup() {
|
||||
console.log('99999999111');
|
||||
this.actionService = useService("action")
|
||||
console.log(this.actionService)
|
||||
|
||||
const barcode = useService("barcode");
|
||||
// this.rpc = useService("rpc");
|
||||
// useBus(barcode.bus, "barcode_scanned", this.onBarcodeScanned.bind(this));
|
||||
useBus(barcode.bus, "barcode_scanned", this.onBarcodeScanned.bind(this));
|
||||
}
|
||||
async _rpc(params) {
|
||||
// const { data } = await this.env.services.rpc('/web/dataset/call_kw', params);
|
||||
const response = await this.env.services.rpc('/web/dataset/call_kw', params);
|
||||
console.log('response', response);
|
||||
console.log('response.result', response.result);
|
||||
|
||||
// return response
|
||||
const responseObject = JSON.parse(response)
|
||||
return responseObject;
|
||||
}
|
||||
|
||||
async onBarcodeScanned(event) {
|
||||
const { barcode } = event.detail;
|
||||
this.props.update(barcode);
|
||||
// const actionService = useService("action");
|
||||
// const productId = 12345
|
||||
console.log('111222222222211111');
|
||||
|
||||
// 根据条形码获取相关数据,例如产品ID
|
||||
const response = await this._rpc({
|
||||
model: 'mrp.workorder',
|
||||
method: 'get_product_id_by_barcode',
|
||||
args: [barcode],
|
||||
kwargs:{},
|
||||
});
|
||||
// console.log(productId.result)
|
||||
if (response.result) {
|
||||
const action = response.result;
|
||||
console.log(action)
|
||||
console.log('11111111111111111111111111111111111');
|
||||
// 通过产品ID执行操作并跳转到表单视图
|
||||
// await this.actionService.doAction({
|
||||
// // type: 'ir.actions.act_window',
|
||||
// // res_model: 'product.product',
|
||||
// // res_id: productId,
|
||||
// // views: [[false, 'form']],
|
||||
// // target: 'current',
|
||||
// 'name': '工单',
|
||||
// 'type': 'ir.actions.act_window',
|
||||
// 'views': [[false, 'form']],
|
||||
// 'view_mode': 'form',
|
||||
// 'res_model': 'mrp.workorder',
|
||||
// 'view_id': 918,
|
||||
// 'res_id': 1023,
|
||||
// 'target': 'current',
|
||||
// });
|
||||
await this.actionService.doAction(response.result);
|
||||
} else {
|
||||
console.error("Barcode not found or RPC call failed.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
BarcodeHandlerField.template = xml``;
|
||||
BarcodeHandlerField.props = { ...standardFieldProps };
|
||||
|
||||
registry.category("fields").add("barcode_handlerrr", BarcodeHandlerField);
|
||||
@@ -1,254 +0,0 @@
|
||||
// /** @odoo-module **/
|
||||
//
|
||||
// import { browser } from "@web/core/browser/browser";
|
||||
// import { Dialog } from "@web/core/dialog/dialog";
|
||||
// import { _lt } from "@web/core/l10n/translation";
|
||||
// import { useChildRef, useOwnedDialogs, useService } from "@web/core/utils/hooks";
|
||||
// import { sprintf } from "@web/core/utils/strings";
|
||||
// import { isMobileOS } from "@web/core/browser/feature_detection";
|
||||
// import * as BarcodeScanner from "@web/webclient/barcode/barcode_scanner";
|
||||
//
|
||||
// const {xml, Component} = owl;
|
||||
// import { standardFieldProps } from "@web/views/fields/standard_field_props";
|
||||
// // Import the registry
|
||||
// import {registry} from "@web/core/registry";
|
||||
//
|
||||
//
|
||||
// export class CodeField extends Component {
|
||||
// setup() {
|
||||
// super.setup();
|
||||
// }
|
||||
// async onBarcodeBtnClick() {
|
||||
// const barcode = await BarcodeScanner.scanBarcode();
|
||||
// if (barcode) {
|
||||
// await this.onBarcodeScanned(barcode);
|
||||
// if ("vibrate" in browser.navigator) {
|
||||
// browser.navigator.vibrate(100);
|
||||
// }
|
||||
// } else {
|
||||
// this.notification.add(this.env._t("Please, scan again !"), {
|
||||
// type: "warning",
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
// async search(barcode) {
|
||||
// const results = await this.orm.call("sf.tray", "name_search", [code], {
|
||||
// name: barcode,
|
||||
// args: this.getDomain(),
|
||||
// operator: "ilike",
|
||||
// limit: 2, // If one result we set directly and if more than one we use normal flow so no need to search more
|
||||
// context: this.context,
|
||||
// });
|
||||
// return results.map((result) => {
|
||||
// const [id, displayName] = result;
|
||||
// return {
|
||||
// id,
|
||||
// name: displayName,
|
||||
// };
|
||||
// });
|
||||
// }
|
||||
// async onBarcodeScanned(barcode) {
|
||||
// const results = await this.search(barcode);
|
||||
// const records = results.filter((r) => !!r.id);
|
||||
// if (records.length === 1) {
|
||||
// this.update([{ id: records[0].id, name: records[0].name }]);
|
||||
// } else {
|
||||
// const searchInput = this.autocompleteContainerRef.el.querySelector("input");
|
||||
// searchInput.value = barcode;
|
||||
// searchInput.dispatchEvent(new Event("input"));
|
||||
// if (this.env.isSmall) {
|
||||
// searchInput.click();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// CodeField.template = xml`
|
||||
// <button
|
||||
// t-on-click="onBarcodeBtnClick"
|
||||
// type="button"
|
||||
// class="btn ms-3 o_barcode"
|
||||
// tabindex="-1"
|
||||
// draggable="false"
|
||||
// aria-label="Scan barcode"
|
||||
// title="Scan barcode"
|
||||
// data-tooltip="Scan barcode"
|
||||
// />
|
||||
// `;
|
||||
// // CodeField.template = 'sf_machine_connect.CodeField';
|
||||
// CodeField.props = standardFieldProps;
|
||||
//
|
||||
// // Add the field to the correct category
|
||||
// registry.category("fields").add("code", CodeField);
|
||||
/** @odoo-module **/
|
||||
|
||||
import { browser } from "@web/core/browser/browser";
|
||||
import { Dialog } from "@web/core/dialog/dialog";
|
||||
import { _lt } from "@web/core/l10n/translation";
|
||||
import { useChildRef, useOwnedDialogs, useService } from "@web/core/utils/hooks";
|
||||
import { sprintf } from "@web/core/utils/strings";
|
||||
import { isMobileOS } from "@web/core/browser/feature_detection";
|
||||
import * as BarcodeScanner from "@web/webclient/barcode/barcode_scanner";
|
||||
|
||||
const {xml, Component} = owl;
|
||||
import { standardFieldProps } from "@web/views/fields/standard_field_props";
|
||||
// Import the registry
|
||||
import {registry} from "@web/core/registry";
|
||||
|
||||
|
||||
export class CodeField extends Component {
|
||||
setup() {
|
||||
super.setup();
|
||||
this.orm = this.env.services.orm;
|
||||
this.record = this.props.record;
|
||||
}
|
||||
async onBarcodeBtnClick() {
|
||||
// console.log(BarcodeScanner)
|
||||
const barcode = await BarcodeScanner.scanBarcode();
|
||||
// console.log(typeof barcode)
|
||||
// alert(barcode)
|
||||
|
||||
if (barcode) {
|
||||
// console.log("存在")
|
||||
// alert('存在')
|
||||
|
||||
await this.onBarcodeScanned(barcode);
|
||||
if ("vibrate" in browser.navigator) {
|
||||
browser.navigator.vibrate(100);
|
||||
}
|
||||
} else {
|
||||
// console.log("不存在")
|
||||
// alert('不存在')
|
||||
this.notification.add(this.env._t("Please, scan again !"), {
|
||||
type: "warning",
|
||||
});
|
||||
}
|
||||
}
|
||||
async search(barcode) {
|
||||
alert('我是search')
|
||||
const domain = [["code", "=", barcode]];
|
||||
// const domain = [];
|
||||
console.log(domain)
|
||||
alert('走到这里了')
|
||||
const fields = ["id", "code", "name", "state"];
|
||||
console.log(fields)
|
||||
alert('走到这里了1')
|
||||
const results = await this.orm.call("sf.tray", "search_read", [domain, fields]);
|
||||
const values = await this.orm.call("sf.tray", "search_read", [domain]);
|
||||
console.log(results)
|
||||
alert('走到这里了2')
|
||||
return results.map((result) => {
|
||||
return {
|
||||
id: result.id,
|
||||
code: result.code,
|
||||
name: result.name,
|
||||
state: result.state,
|
||||
values: values,
|
||||
};
|
||||
});
|
||||
}
|
||||
async onBarcodeScanned(barcode) {
|
||||
// alert('我是ONbarcodeScanned')
|
||||
const results = await this.search(barcode);
|
||||
console.log(results)
|
||||
const records = results.filter((r) => !!r.id);
|
||||
console.log(records)
|
||||
if (records.length === 1) {
|
||||
if (records[0].state === '空闲') {
|
||||
// const currentModel = this.env.models['mrp.workorder'];
|
||||
console.log('currentModel',this)
|
||||
console.log('this.record.data',this.record.data)
|
||||
// const recordID = this.controllerParams.recordID;
|
||||
// console.log('当前页面记录 ID:', recordID);
|
||||
// console.log('this.record',this.record.id)
|
||||
// console.log('this.record',this.controllerParams.recordID)
|
||||
// const workorder = await this.orm.call('mrp.workorder', 'read', [this.record.data.id, ['id']]);
|
||||
console.log('this.record.data.id', this.record.data.id)
|
||||
const workorder = await this.orm.call('mrp.workorder', 'read', [this.record.data.id]);
|
||||
console.log('workorder', workorder[0])
|
||||
const updatedRecord = await this.orm.call("sf.tray", "write", [
|
||||
[records[0].id],
|
||||
{
|
||||
// state: "占用",
|
||||
workorder_id: workorder[0].id,
|
||||
production_id: workorder[0].product_id[0],
|
||||
// workorder_id: workorder.id,
|
||||
}]);
|
||||
console.log(workorder[0].routing_type);
|
||||
console.log(workorder[0].production_id);
|
||||
// const productionIDS = await this.orm.call('mrp.production', 'search', [[['id', '=', workorder[0].production_id[0]]]]);
|
||||
const productionIDS = await this.orm.call('mrp.workorder', 'search', [[["production_id", "=", workorder[0].production_id[0]]]]);
|
||||
console.log('prooooooo', productionIDS);
|
||||
console.log('values', records[0].values[0]);
|
||||
productionIDS.forEach(async (data) => {
|
||||
// 处理每一个数据
|
||||
console.log(data);
|
||||
const updatetrayRecord = await this.orm.call("mrp.workorder", "write", [
|
||||
[data],
|
||||
{
|
||||
tray_id: records[0].values[0].id,
|
||||
// tray_id: false,
|
||||
}]);
|
||||
console.log(updatetrayRecord)
|
||||
});
|
||||
// this.trigger_up('reload')
|
||||
// this.props.update(records[0].code, records[0].values);
|
||||
this.props.update(records[0].code, records[0].tray_id);
|
||||
// this.trigger_up('button_clicked', {
|
||||
// attrs: {
|
||||
// name: 'save',
|
||||
// },
|
||||
// });
|
||||
// this.do_action({
|
||||
// 'type': 'ir.actions.act_window',
|
||||
// 'res_model': 'mrp.workorder',
|
||||
// 'view_mode': 'form',
|
||||
// 'view_type': 'form',
|
||||
// 'res_id': workorder[0].id,
|
||||
// 'target': 'current',
|
||||
// })
|
||||
} else {
|
||||
if (records[0].state === '占用') {
|
||||
console.log('此托盘已占用,请检查')
|
||||
alert('此托盘已占用,请检查')
|
||||
} else {
|
||||
console.log('此托盘已损坏,请登记')
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
const searchInput = this.autocompleteContainerRef.el.querySelector("input");
|
||||
searchInput.value = barcode;
|
||||
searchInput.dispatchEvent(new Event("input"));
|
||||
if (this.env.isSmall) {
|
||||
searchInput.click();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CodeField.template = xml`
|
||||
<div class="o_field_widget o_input o_barcode_field">
|
||||
<!-- <input class="o_input" type="text" t-att-value="value"/>-->
|
||||
<div class="o_input_container">
|
||||
<t t-raw="widget"/>
|
||||
</div>
|
||||
<button
|
||||
t-on-click="onBarcodeBtnClick"
|
||||
type="button"
|
||||
class="sf_button o_barcode btn ms-3 o_barcode"
|
||||
tabindex="-1"
|
||||
draggable="false"
|
||||
aria-label="Scan barcode"
|
||||
title="Scan barcode"
|
||||
data-tooltip="Scan barcode"
|
||||
/>
|
||||
</div>
|
||||
`;
|
||||
// CodeField.template = 'sf_machine_connect.CodeField';
|
||||
CodeField.props = standardFieldProps;
|
||||
|
||||
// Add the field to the correct category
|
||||
registry.category("fields").add("code", CodeField);
|
||||
|
||||
// style="position: relative; left: 200px;"
|
||||
19
sf_machine_connect/static/src/xml/barcode_button.xml
Normal file
19
sf_machine_connect/static/src/xml/barcode_button.xml
Normal file
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<templates xml:space="preserve">
|
||||
<t t-name="sf_machine_connect.CodeField" owl="1">
|
||||
<div>
|
||||
<input t-ref="scan_code" t-att-id="props.id" t-att-type="props.inputType" t-att-placeholder="props.placeholder" class="barcode_button o_input"/>
|
||||
<button
|
||||
t-on-click="onBarcodeBtnClick"
|
||||
type="button"
|
||||
class="btn ms-3 o_barcode"
|
||||
tabindex="-1"
|
||||
draggable="false"
|
||||
aria-label="Scan barcode"
|
||||
title="Scan barcode"
|
||||
data-tooltip="Scan barcode"
|
||||
style="position: absolute !important;"
|
||||
/>
|
||||
</div>
|
||||
|
||||
</templates>
|
||||
Reference in New Issue
Block a user