vladkhard
vladkhard

Reputation: 97

how do I add action properly in odoo js framework

I am trying to add barcode scanner on top of Inventory module (on top of the pickings, to be more specific). Anytime barcode is scanned when inside picking view it must add one product done if can find its barcode. I've written extension for 'web.AbstractAction' (I've taken Attendance module as example), but it cannot be reached from inside of picking view. I am clearly missing some point where I need to specify some setting, but I don't know where and official documentation cannot ask my question.

Here is all my code on this task:

static/js/barcode_scanner.js

odoo.define('stock.barcode_scanner', function(require) {
    'use sctrict';

    var AbstractAction = require('web.AbstractAction');
    var core = require('web.core');

    var BarcodeAction = AbstractAction.extend({

        start: function() {
            var self = this;
            core.bus.on('barcode_scanned', this, this._onBarcodeScanned);
            return this._super();
            // .then(function() {
            //     if (self.message_demo_barcodes) {
            //         self.setup_message_demo_barcodes();
            //     }
            // });
        },

        destroy: function () {
            core.bus.off('barcode_scanned', this, this._onBarcodeScanned);
            this._super();
        },

        _onBarcodeScanned: function(barcode) {
            var self = this;
            core.bus.off('barcode_scanned', this, this._onBarcodeScanned);
            this._rpc({
                    model: 'stock.picking',
                    method: 'product_scan',
                    args: [barcode, ],
                })
                .then(function (result) {
                    if (result.action) {
                        self.do_action(result.action);
                    } else if (result.warning) {
                        self.do_warn(result.warning);
                        core.bus.on('barcode_scanned', self, self._onBarcodeScanned);
                    }
                }, function () {
                    core.bus.on('barcode_scanned', self, self._onBarcodeScanned);
                });
        },

    });

    core.action_registry.add('stock_barcode_scanner', BarcodeAction);

    return {
        BarcodeAction: BarcodeAction,
    };
});

models/stock_picking.py

import logging
from odoo import models, api, _


_logger = logging.getLogger(__name__)


class Picking(models.Model):
    _inherit = "stock.picking"
    _description = "Extended Picking"

    @api.model
    def product_scan(self, barcode):
        _logger.info("i'm trying {}".format(barcode))
        return {'warning': _('%(barcode)s scanned') % {'barcode': barcode}}

views/stock_picking_views.xml

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>
        <record id="stock_barcode_scanner_action" model="ir.actions.client">
            <field name="name">Pickings</field>
            <field name="tag">stock_barcode_scanner</field>
            <field name="target">fullscreen</field>
        </record>
    </data>
</odoo>

views/web_asset_backend_template.xml

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <template id="assets_backend" name="nickel_inventory assets" inherit_id="web.assets_backend">
        <xpath expr="." position="inside">
            <script type="text/javascript" src="/nickel_inventory/static/js/barcode_scanner.js"></script>
        </xpath>
    </template>

</odoo>

__manifest__.py

# -*- coding: utf-8 -*-
{
    'name': "nickel_inventory",

    'summary': """
    """,

    'description': """
    """,

    'author': "",
    'website': "",

    # Categories can be used to filter modules in modules listing
    # Check https://github.com/odoo/odoo/blob/13.0/odoo/addons/base/data/ir_module_category_data.xml
    # for the full list
    'category': 'Sales/Sales',
    'version': '0.1',

    # any module necessary for this one to work correctly
    'depends': ['base', 'product', 'stock'],

    # always loaded
    'data': [
        # 'security/ir.model.access.csv',
        'views/product_views.xml',
        'views/stock_picking_views.xml',
        'views/web_asset_backend_template.xml',
    ],
    # only loaded in demonstration mode
    'demo': [],
    'installable': True,
}

What am I missing? Can it really work on top of odoo views, or I only can use it inside js framework views?

Upvotes: 1

Views: 1500

Answers (1)

vladkhard
vladkhard

Reputation: 97

If someone is curious how I solved that - I've added binding to menu item. In my case, odoo opens clear white page and listens to the keyboard input.

views/stock_picking_views.xml:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>
        <record id="stock_barcode_scanner_action" model="ir.actions.client">
            <field name="name">Pickings</field>
            <field name="tag">stock_barcode_scanner</field>
            <field name="target">fullscreen</field>
        </record>

        <menuitem id="menu_stock_barcode_scanner" name="Barcode scanner" sequence="20" action="stock_barcode_scanner_action"/>
    </data>
</odoo>

The next step is to fill this empty page with useful forms and logic.

Upvotes: 1

Related Questions