Skip to Content
Menu
This question has been flagged
1 Reply
64 Views

I am working with Odoo v17. I had extended the account.move model to include the 'serie' field

class AccountMove(models.Model):
    _inherit = "account.move"

    serie = fields.Selection([
        ('a', 'A'),
        ('e', 'E'),
        ('f', 'F'),
        ('g', 'G'),  
        ('i', 'I'),      
        ('cp', 'CP'),
        ], string='Serie', required=True, default='a')

The code was on production and several invoices where created. Now it is necesary to delete some options of the field. Options, 'e', 'f', 'g' are not going to be available anymore. But just remove those options from the field would cause that an Error would raise when accessing a previously created invoice with a serie 'e', 'f', or 'g'. So I desiided to keep the fields definition as it is, but modify the widget to show only options 'a', 'i', 'cp'. with the following code i was able to achieve that only the options 'a', 'i', 'cp' where shown for the series field. But when i try to open an invoice previously created with series= 'e', 'f', 'g', it shows an error. This is the code:

/** @odoo-module **/

import {registry} from "@web/core/registry";

import {
  SelectionField,
  selectionField,
} from "@web/views/fields/selection/selection_field";

export class FilteredSerieSelection extends SelectionField {
  get availableOptions() {
    return ['a','i','cp'];
  }

  /** Override **/
  get options() {
    const availableOptions = this.availableOptions;
    return super.options.filter((x) => availableOptions.includes(x[0]));
  }
}

registry.category("fields").add("custom_serie_select", {
  ...selectionField,
  component: FilteredSerieSelection,
});

In the view:

<?xml version="1.0" encoding="utf-8" ?>
<odoo>
    <record id="account_move_viixoo_form_inherited" model="ir.ui.view">
        <field name="name">account.move.viixoo.form.inherit</field>
        <field name="model">account.move</field>
        <field name="inherit_id" ref="account.view_move_form" />
        <field name="arch" type="xml">
            <field name="l10n_mx_edi_usage" position="after">
                <field name="serie"  widget="custom_serie_select" required="move_type == 'out_invoice'" invisible="move_type != 'out_invoice'" readonly="id != False and name != '/'"/>
            </field>
        </field>
    </record>
</odoo>

This is the error shown:

OwlError: An error occured in the owl lifecycle (see this Error's "cause" property)
    OwlError@http://127.0.0.1:8069/web/assets/a3292ec/web.assets_web.min.js:684:1
    handleError@http://127.0.0.1:8069/web/assets/a3292ec/web.assets_web.min.js:916:101
    handleError@http://127.0.0.1:8069/web/assets/a3292ec/web.assets_web.min.js:1542:29
    _render@http://127.0.0.1:8069/web/assets/a3292ec/web.assets_web.min.js:941:19
    render@http://127.0.0.1:8069/web/assets/a3292ec/web.assets_web.min.js:939:6
    initiateRender@http://127.0.0.1:8069/web/assets/a3292ec/web.assets_web.min.js:1007:47

Caused by: TypeError: this.options.find(...) is undefined
    get string@http://127.0.0.1:8069/web/assets/a3292ec/web.assets_web.min.js:8386:224
    template@http://127.0.0.1:8069/web/assets/a3292ec/web.assets_web.min.js line 1500 > Function:15:18
    _render@http://127.0.0.1:8069/web/assets/a3292ec/web.assets_web.min.js:940:96
    render@http://127.0.0.1:8069/web/assets/a3292ec/web.assets_web.min.js:939:6
    initiateRender@http://127.0.0.1:8069/web/assets/a3292ec/web.assets_web.min.js:1007:47

When creating a new invoice, the desired options are shown correctly without error.
I need to be able to shown only those options, but for the previously created invoices it should show the series 'e', 'f', 'g' without error.

Avatar
Discard
Best Answer

Hi,

Please update the code like below:


import { SelectionField, selectionField } from "@web/views/fields/selection/selection_field";

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


patch(SelectionField.prototype, {

      async setup() {

        super.setup();

       },

       get options() {

           switch (this.type) {

           case "many2one":

            return [...this.specialData.data];

            case "selection":

               if(this.props.name=="serie" && this.props.record.resModel=="account.move"){

                   return this.props.record.fields[this.props.name].selection.filter(

                   (option) => option[0] !== false && option[1] !== ""&& option[1] in ['a', 'i', 'cp']

                 );

               }

              return this.props.record.fields[this.props.name].selection.filter(

                   (option) => option[0] !== false && option[1] !== ""

              );

              default:

               return [];

         }

         }

});


Hope it helps

Avatar
Discard
Related Posts Replies Views Activity
1
Mar 24
455
0
Oct 22
1255
2
Oct 24
716
3
Oct 23
3462
2
Dec 24
2832