Ir al contenido
Menú
Se marcó esta pregunta
3 Respuestas
13331 Vistas
I am trying to achieve a functionality where when a user clicks on an expired product show an error modal. i keep getting the error:

expired_product.models: Cannot read properties of undefined (reading 'extend') TypeError: Cannot read properties of undefined (reading 'extend')


odoo.define('expired_product.ProductScreen', function (require) {    "use strict";   
const ProductScreen = require('point_of_sale.ProductScreen'); const Registries = require('point_of_sale.Registries'); const { _t } = require('web.core'); const { DateTime } = luxon;
    const ExpiredProductScreen = ProductScreen =>        class extends ProductScreen {            async _clickProduct(event) {                const product = event.detail.product;                console.log('Product clicked:', product);
                // Display expiration_time and expiry_date                console.log('Expiration Time (Days):', product.expiration_time);                console.log('Expiry Date:', product.expiry_date);
                // Check if product has expired                if (this._isProductExpired(product)) {                    await this._showExpiredProductModal(product);                    return;                }
                // If not expired, proceed with normal product selection                super._clickProduct(event);            }
            _isProductExpired(product) {                // Check product expiration                if (product.expiry_date) {                    const expirationDate = DateTime.fromISO(product.expiry_date);                    const today = DateTime.local();                    return expirationDate < today;                }                return false;            }
            async _showExpiredProductModal(product) {                const { confirmed } = await this.showPopup('ConfirmPopup', {                    title: _t('Product Expired'),                    body: _.str.sprintf(                        _t('The product "%s" has expired on %s. Do you want to continue?'),                        product.display_name,                        DateTime.fromISO(product.expiry_date).toLocaleString(DateTime.DATE_FULL)                    ),                    confirmText: _t('Continue'),                    cancelText: _t('Cancel')                });
                // If user confirms, allow product selection                if (confirmed) {                    this.env.pos.get_order().add_product(product);                }            }        };
    Registries.Component.extend(ProductScreen, ExpiredProductScreen);
    return ExpiredProductScreen;});


Avatar
Descartar
Autor

Thank you. This helps a lot.

Mejor respuesta

You’re getting the “Cannot read properties of undefined (reading 'extend')” error because the ProductScreen component isn’t fully defined when you try to extend it. Make sure your custom module waits until ProductScreen is initialized before applying Registries.Component.extend(ProductScreen, ExpiredProductScreen). Also check that your product has the expiry fields (expiry_date, etc.) defined and passed through to the POS so the check logic has data to work with.

Avatar
Descartar
Mejor respuesta

The error "Cannot read properties of undefined (reading 'extend')" in your Odoo POS expiry check code usually arises from incorrect inheritance or missing dependencies.



  1. Verify Inheritance: Ensure your ExpiredProductScreen class correctly extends the ProductScreen class. Double-check the Registries.Component.extend line.

  2. Check Dependencies: Confirm that all necessary modules (point_of_sale, web.core, luxon) are correctly declared and installed. A missing dependency can cause this error.

  3. Debug Product Data: Add more detailed logging before the _isProductExpired check to inspect the product object's structure. This helps pinpoint if product.expiry_date is actually defined and contains the expected data. Verify that your product model has the expiry_date field correctly defined and populated.

  4. Review Date Handling: Ensure product.expiry_date is stored in a format compatible with luxon.DateTime.fromISO. Incorrect date formatting can lead to parsing errors.

  5. Odoo Version Compatibility: Check if your code is compatible with your Odoo version. Incompatibilities between versions can cause unexpected behavior.


Resources: While no exact matches were found for this specific error on our website, we have many resources on Odoo POS customization. For more in-depth assistance, please contact us.


For personalized assistance:
https://www.pragtech.co.in/contact.html

Avatar
Descartar
Mejor respuesta

Hi,

The error you're encountering, Cannot read properties of undefined (reading 'extend'), occurs because Registries.Component.extend is being applied improperly before the component (ProductScreen) is fully defined. To fix this, ensure that the ProductScreen component is extended only after it is properly initialized. In the code, the component should be extended after defining the ExpiredProductScreen class.


odoo.define('expired_product.ProductScreen', function (require) {

    "use strict";


    const ProductScreen = require('point_of_sale.ProductScreen');

    const Registries = require('point_of_sale.Registries');

    const { _t } = require('web.core');

    const { DateTime } = luxon;


    const ExpiredProductScreen = ProductScreen =>

        class extends ProductScreen {

            async _clickProduct(event) {

                const product = event.detail.product || event.detail;

                console.log('Product clicked:', product);


                // Display expiration_time and expiry_date

                console.log('Expiration Time (Days):', product.expiration_time);

                console.log('Expiry Date:', product.expiry_date);


                // Check if product has expired

                if (this._isProductExpired(product)) {

                    await this._showExpiredProductModal(product);

                    return;

                }


                // If not expired, proceed with normal product selection

                super._clickProduct(event);

            }


            _isProductExpired(product) {

                // Check product expiration

                if (product.expiry_date) {

                    const expirationDate = DateTime.fromISO(product.expiry_date);

                    const today = DateTime.local();

                    return expirationDate < today;

                }

                return false;

            }


            async _showExpiredProductModal(product) {

                const { confirmed } = await this.showPopup('ConfirmPopup', {

                    title: _t('Product Expired'),

                    body: _t(

                        The product "${product.display_name}" has expired on ${DateTime.fromISO(product.expiry_date).toLocaleString(DateTime.DATE_FULL)}. Do you want to continue?

                    ),

                    confirmText: _t('Continue'),

                    cancelText: _t('Cancel'),

                });


                // If user confirms, allow product selection

                if (confirmed) {

                    this.env.pos.get_order().add_product(product);

                }

            }

        };


    // Correctly extend the ProductScreen using Registries

    Registries.Component.extend(ProductScreen, ExpiredProductScreen);


    return ExpiredProductScreen;

});


Additionally, you need to check whether the product has a lot or serial number tracking (via product.tracking) before performing the expiration check, as expiration is only relevant for such products. Also, ensure that product.expiry_date exists before checking for expiration to avoid errors when this field is missing.


Hope it helps

Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
2
sept 25
1480
1
mar 25
793
0
feb 19
3433
4
ago 25
1263
2
mar 16
4815