2

Could someone help me how to patch StockForecasted in Odoo16?

class StockForecasted extends Component{
    async _getReportValues(){
        ​   ​this.resModel = this.context.active_model || (this.context.params && this.context.params.active_model);
    ​   ​....
    ​}
}

I tried this below code but did not work

/** @odoo-module **/
import { patch } from '@web/core/utils/patch';
import { StockForecasted } from "@stock/stock_forecasted/stock_forecasted";
console.log(StockForecasted)
patch(StockForecasted.prototype, 'test_my_stock.StockForecasted', {
    async _getReportValues(){
        console.log('_getReportValuesInherit')
        this.resModel = this.context.active_model || (this.context.params && this.context.params.active_model);
        if (!this.resModel) {
            if (this.props.action.res_model) {
                const actionModel = await this.orm.read('ir.model', [Number(this.props.action.res_model)], ['model']);
                if (actionModel.length && actionModel[0].model) {
                    this.resModel = actionModel[0].model
                }
            } else if (this.props.action._originalAction) {
                const originalContextAction = JSON.parse(this.props.action._originalAction).context;
                if (originalContextAction) {
                    this.resModel = originalContextAction.active_model
                }
            }
        }

Browser showed 'undefined'. I think this is because this class is not 'export'. But I cannot export this class. This is Odoo code base.

  • Could you provide some browser full logs with `debug=assets` enabled in the url? where are you seeing the undefined? – aekis.dev Jul 29 '23 at 03:58

1 Answers1

3

This happens because Odoo failed to import it from stock/stock_forecasted/stock_forecasted
You should see the following error in the console:

TypeError: StockForecasted is undefined

If you check the stock/static/src/stock_forecasted/stock_forecasted.js file, you will see that they added the StockForecasted component to actions registry as replenish_report

Example:

/** @odoo-module **/

import { patch } from '@web/core/utils/patch';
import { registry } from "@web/core/registry";
import "@stock/stock_forecasted/stock_forecasted";
const actionRegistry = registry.category("actions");

const StockForecasted = actionRegistry.get('replenish_report');

patch(StockForecasted.prototype, 'test_my_stock.StockForecasted', {
    async _getReportValues(){
        console.log('_getReportValuesInherit')
        await this._super(...arguments);
    }
});
Kenly
  • 24,317
  • 7
  • 44
  • 60
  • This works, brilliant. I have a question regarding 'import "@stock/stock_forecasted/stock_forecasted";'. Without this line, it doesn't work. I thought the moment Odoo load this file, its already add the 'replenish_report' into registry. – Duc Trong Pham Jul 29 '23 at 15:48
  • The line is used to make sure the replenish report is already imported, you can check this [issue](https://github.com/odoo/owl/issues/1070) – Kenly Jul 31 '23 at 09:45