From e6023eb2d84b03aa8c15cac4f56b596d246ab4d3 Mon Sep 17 00:00:00 2001 From: Suherdy Yacob Date: Wed, 4 Feb 2026 17:11:55 +0700 Subject: [PATCH] refactor: Reimplements sequence field management in SignItemCustomPopover using `Many2XAutocomplete` and direct state updates for improved data handling. --- .../src/js/sign_item_custom_popover_patch.js | 80 +++++++++---------- .../xml/sign_item_custom_popover_patch.xml | 68 +++++++--------- 2 files changed, 66 insertions(+), 82 deletions(-) diff --git a/static/src/js/sign_item_custom_popover_patch.js b/static/src/js/sign_item_custom_popover_patch.js index 620e4af..29694bc 100755 --- a/static/src/js/sign_item_custom_popover_patch.js +++ b/static/src/js/sign_item_custom_popover_patch.js @@ -1,61 +1,57 @@ /** @odoo-module **/ import { SignItemCustomPopover } from "@sign/backend_components/sign_template/sign_item_custom_popover"; +import { Many2XAutocomplete } from "@web/views/fields/relational_utils"; import { patch } from "@web/core/utils/patch"; patch(SignItemCustomPopover.prototype, { setup() { super.setup(); - // Add sequence fields to the loaded fields - this.signItemFieldsGet.sequence_id = { type: "many2one", relation: "ir.sequence", string: "Sequence" }; - this.signItemFieldsGet.sequence_prefix = { type: "char", string: "Prefix" }; - this.signItemFieldsGet.sequence_padding = { type: "integer", string: "Padding" }; - this.signItemFieldsGet.sequence_number_next = { type: "integer", string: "Next Number" }; + this.state.sequence_id = this.props.sequence_id || false; + this.state.sequence_prefix = this.props.sequence_prefix || ""; + this.state.sequence_padding = this.props.sequence_padding || 0; + this.state.sequence_number_next = this.props.sequence_number_next || 1; - // Initialize state from props - this.state.sequence_id = this.props.sequence_id; - this.state.sequence_prefix = this.props.sequence_prefix; - this.state.sequence_padding = this.props.sequence_padding; - this.state.sequence_number_next = this.props.sequence_number_next; + // Many2XAutocomplete 'value' prop expects the string to display + this.state.sequence_value = ""; + + if (this.props.sequence_id) { + this.state.sequence_value = "Loading..."; + this.orm.read("ir.sequence", [this.props.sequence_id], ["display_name"]).then((res) => { + if (res && res.length) { + this.state.sequence_value = res[0].display_name; + } else { + this.state.sequence_value = ""; + } + }); + } }, - get recordProps() { - // We need to intercept onRecordChanged to sync changes to this.state - const originalRecordProps = super.recordProps; - const originalOnRecordChanged = originalRecordProps.onRecordChanged; + async onChangeSequence(val) { + // val comes from Many2XAutocomplete's update prop, which usually sends [{id, display_name}] or []/false + const record = Array.isArray(val) && val.length ? val[0] : null; + const sequenceId = record ? record.id : false; - return { - ...originalRecordProps, - onRecordChanged: async (record, changes) => { - if (originalOnRecordChanged) { - await originalOnRecordChanged(record, changes); - } - // Sync sequence fields to state if changed - if ("sequence_id" in changes) { - this.state.sequence_id = changes.sequence_id; - // When sequence changes, we might want to reload prefix/padding/etc if they are computed? - // They are related fields, so 'record' should have them updated. - // But 'changes' might only contain the changed field. - // We should check the record data for the others. + this.state.sequence_value = record ? record.display_name : ""; + this.state.sequence_id = sequenceId; - // Actually, if they are related fields, the `Record` model updates them. - // But `changes` object passed here contains changed values. - } - if ("sequence_prefix" in changes) this.state.sequence_prefix = changes.sequence_prefix; - if ("sequence_padding" in changes) this.state.sequence_padding = changes.sequence_padding; - if ("sequence_number_next" in changes) this.state.sequence_number_next = changes.sequence_number_next; + if (sequenceId) { + const result = await this.orm.read("ir.sequence", [sequenceId], ["prefix", "padding", "number_next"]); + if (result && result.length) { + this.state.sequence_prefix = result[0].prefix || ""; + this.state.sequence_padding = result[0].padding || 0; + this.state.sequence_number_next = result[0].number_next || 1; } - }; + } else { + this.state.sequence_prefix = ""; + this.state.sequence_padding = 0; + this.state.sequence_number_next = 1; + } } }); -import { CharField } from "@web/views/fields/char/char_field"; -import { IntegerField } from "@web/views/fields/integer/integer_field"; - -// Patch static components -const originalComponents = SignItemCustomPopover.components; +// Patch static components to include Many2XAutocomplete SignItemCustomPopover.components = { - ...originalComponents, - CharField, - IntegerField, + ...SignItemCustomPopover.components, + Many2XAutocomplete, }; diff --git a/static/src/xml/sign_item_custom_popover_patch.xml b/static/src/xml/sign_item_custom_popover_patch.xml index 52ef259..796875f 100755 --- a/static/src/xml/sign_item_custom_popover_patch.xml +++ b/static/src/xml/sign_item_custom_popover_patch.xml @@ -1,48 +1,36 @@ - - -
- - - - - - -
-
- - - -
- -
- + + +
+ +
+ +
+ +
+ +
+ + +
+
+ + +
+
+ + +
+
- - - -
- -
- -
-
-
- -
- -
-
-
- -
- -
-
-