summaryrefslogtreecommitdiff
path: root/addons/iap/static/src/js/crash_manager.js
blob: 11324fb68a2caee41f5e2fe703a3c1804ef939fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
odoo.define('iap.CrashManager', function (require) {
"use strict";

var ajax = require('web.ajax');
var core = require('web.core');
var CrashManager = require('web.CrashManager').CrashManager;
var Dialog = require('web.Dialog');

var _t = core._t;
var QWeb = core.qweb;

CrashManager.include({
    /**
     * Change button string depending if it's Enterprise and trial available
     * (Only change the text message in the button, doesn't change IAP side validation)
     *
     * @param {boolean} isTrial
     * @returns {string}
     * @private
     */
    _getButtonMessage: function (isTrial){
        var isEnterprise = _.last(odoo.session_info.server_version_info) === 'e';
        return isTrial && isEnterprise ? _t('Start a Trial at Odoo') : _t('Buy credits');
    },
    /**
     * @override
     */
    rpc_error: function (error) {
        var self = this;
        if (error.data.name === "odoo.addons.iap.tools.iap_tools.InsufficientCreditError") {
            var error_data = JSON.parse(error.data.message);
            ajax.jsonRpc('/web/dataset/call_kw', 'call', {
                model:  'iap.account',
                method: 'get_credits_url',
                args: [],
                kwargs: {
                    base_url: error_data.base_url,
                    service_name: error_data.service_name,
                    credit: error_data.credit,
                    trial: error_data.trial
                }
            }).then(function (url) {
                var content = $(QWeb.render('iap.redirect_to_odoo_credit', {
                        data: error_data,
                    }));
                if (error_data.body) {
                    content.css('padding', 0);
                }
                new Dialog(this, {
                    size: 'large',
                    title: error_data.title || _t("Insufficient Balance"),
                    $content: content,
                    buttons: [{
                        text: self._getButtonMessage(error_data.trial),
                        classes : "btn-primary",
                        click: function () {
                            window.open(url, '_blank');
                        },
                        close:true,
                    }, {
                        text: _t("Cancel"),
                        close: true,
                    }],
                }).open();
            });
        } else {
            this._super.apply(this, arguments);
        }
    },
});

});