Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
277 Zobrazení

I’m customizing purchase requisition lines in Odoo, using a Many2XAutocomplete widget for selecting product templates. After selecting a template, a product configurator dialog opens where I pick a variant. Once selected, I want the product field in the exact line the user is editing to autocomplete with the chosen product variant.

The problem:

Many2XAutocomplete only gives me a list of line record IDs or a general record context, but not the exact current line ID being edited. Without knowing this current line ID, I cannot properly update that line with the selected product variant.

I’ve tried accessing this.env.model and this.env.model.root, but neither reliably provides the precise line ID for the current autocomplete widget instance.

Does anyone know:

  • How to get the exact line ID from within Many2XAutocomplete when a selection is made?
  • Or, an alternative approach to reliably identify and update the line that triggered the product template selection and configurator?

This is crucial because without the line ID, I can’t correctly set the product variant on the intended line, breaking the expected user flow.

Thanks in advance for any guidance or examples!

Here you can see my code :

import { patch } from "@web/core/utils/patch";

import { ProductConfiguratorDialog } from "@sale/js/product_configurator_dialog/product_configurator_dialog";

import { Many2XAutocomplete } from "@web/views/fields/relational_utils";

import { rpc } from "@web/core/network/rpc";

import { useInputField } from "@web/views/fields/input_field_hook";



const originalOnSelect = Many2XAutocomplete.prototype.onSelect;


patch(Many2XAutocomplete.prototype, {

    async onSelect(option, params = {}) {

        if (this.props.resModel === "product.template" && option?.value) {

            const dialog = this.env.services.dialog;

            const env = this.env;


            try {

                const result = await rpc("/my_product_configurator/get_values", {

                    product_template_id: option.value,

                    currency_id: 1,

                    so_date: new Date().toISOString(),

                });


                await dialog.add(ProductConfiguratorDialog, {

                    productTemplateId: result.product_template_id,

                    quantity: 1.0,

                    ptavIds: [],

                    customPtavs: [],

                    soDate: result.so_date,

                    productUOMId: 1,

                    companyId: 1,

                    pricelistId: 1,

                    currencyId: result.currency_id,

                    options: result.options,


                    discard: () => console.log("Discard clicked"),


                    save: async (mainProduct) => {

                        const record = this.env.model.root;


                        if (!record.resId) {

                            await record.save();

                        }


                        const requisitionId = record.resId;

                        if (!requisitionId) {

                            console.error("Missing requisition ID even after save.");

                            return;

                        }


                        const ptavIds = (mainProduct.attribute_lines || []).flatMap(

                            line => line.selected_attribute_value_ids || []

                        );


                        const templateIds = [mainProduct.product_tmpl_id];

                        const lineIds = env.model.root._values.line_ids.currentIds


                        try {

                            const response = await rpc("/my_product_configurator/set_variants_for_lines", {

                                ptav_ids: ptavIds,

                                template_ids: templateIds,

                                requisition_id: requisitionId,

                                line_ids: lineIds

                            });


                            if (response?.status === "ok") {

                                console.log("Created requisition lines:", response.created_line_ids);

                            } else {

                                console.error("Error from controller:", response?.error || "Unknown error");

                            }

                        } catch (rpcError) {

                            console.error("RPC Error:", rpcError);

                        }


                       await this.env.model.root.load();


                        if (this.env.model.root.model.root) {

                            await this.env.model.root.model.root.load();  

                        }



                    },  


                    close: () => console.log("Dialog closed"),

                });


            } catch (dialogError) {

                console.error("Dialog setup failed:", dialogError);

            }

        }


        return await originalOnSelect.call(this, option, params);

    },

});






Avatar
Zrušit
Related Posts Odpovědi Zobrazení Aktivita
4
zář 20
9081
0
dub 25
754
2
kvě 21
10644
0
dub 21
6
0
úno 19
3713