diff --git a/addons/account_asset/static/src/js/account_asset.js b/addons/account_asset/static/src/js/account_asset.js index 5fe995a98a8..4c9ed37145f 100644 --- a/addons/account_asset/static/src/js/account_asset.js +++ b/addons/account_asset/static/src/js/account_asset.js @@ -1,36 +1,76 @@ -/* -Purpose : show toggle button on depreciation/installment lines for posted/unposted line. -Details : called in list view with "", - this will call the method create_move on the object account.asset.depreciation.line -*/ - odoo.define('account_asset.widget', function(require) { "use strict"; +/** + * The purpose of this widget is to shows a toggle button on depreciation and + * installment lines for posted/unposted line. When clicked, it calls the method + * create_move on the object account.asset.depreciation.line. + * Note that this widget can only work on the account.asset.depreciation.line + * model as some of its fields are harcoded. + */ + +var AbstractField = require('web.AbstractField'); var core = require('web.core'); -var QWeb = core.qweb; -var session = require('web.session'); +var registry = require('web.field_registry'); var _t = core._t; -var WidgetOnButton = core.list_widget_registry.get('field').extend({ - format: function(row_data, options) { - this._super.apply(this, arguments); - this.is_posted = !!row_data.move_posted_check.value; - this.parent_state = row_data.parent_state.value; - var class_color = ''; - if (this.is_posted){class_color = 'o_is_posted';} - else{ - if (row_data.move_check.value){class_color = 'o_unposted';} +var AccountAssetWidget = AbstractField.extend({ + events: _.extend({}, AbstractField.prototype.events, { + 'click': '_onClick', + }), + description: "", + + //-------------------------------------------------------------------------- + // Private + //-------------------------------------------------------------------------- + + /** + * @override + * @private + */ + _render: function () { + var className = ''; + var disabled = true; + var title; + if (this.recordData.move_posted_check) { + className = 'o_is_posted'; + title = _t('Posted'); + } else if (this.recordData.move_check) { + className = 'o_unposted'; + title = _t('Accounting entries waiting for manual verification'); + } else { + disabled = false; + title = _t('Unposted'); } - return $('
').append((this.parent_state === 'open')? $('', { + var $button = $('', { type: 'button', - title: (this.has_value)? _t('Posted') : _t('Unposted'), - disabled: !!this.has_value, - 'class': 'btn btn-sm btn-link fa fa-circle o_widgetonbutton ' + class_color, - }) : '').html(); + title: title, + disabled: disabled, + }).addClass('btn btn-sm btn-link fa fa-circle o_deprec_lines_toggler ' + className); + this.$el.html($button); + }, + + //-------------------------------------------------------------------------- + // Handlers + //-------------------------------------------------------------------------- + + /** + * @private + * @param {MouseEvent} event + */ + _onClick: function (event) { + event.stopPropagation(); + this.trigger_up('button_clicked', { + attrs: { + name: 'create_move', + type: 'object', + }, + record: this.record, + }); }, }); -core.list_widget_registry.add("button.widgetonbutton", WidgetOnButton); +registry.add("deprec_lines_toggler", AccountAssetWidget); + }); diff --git a/addons/account_asset/static/src/less/account_asset.less b/addons/account_asset/static/src/less/account_asset.less index c540f2d65cd..b2e2fb43315 100644 --- a/addons/account_asset/static/src/less/account_asset.less +++ b/addons/account_asset/static/src/less/account_asset.less @@ -1,9 +1,9 @@ -.o_web_client .o_widgetonbutton { +.o_web_client .o_deprec_lines_toggler { color: @brand-danger; &.o_is_posted { color: @brand-success; } - &.o_unposted{ + &.o_unposted { color: @brand-warning; } } diff --git a/addons/account_asset/static/tests/account_asset_tests.js b/addons/account_asset/static/tests/account_asset_tests.js new file mode 100644 index 00000000000..eab7f5f5d99 --- /dev/null +++ b/addons/account_asset/static/tests/account_asset_tests.js @@ -0,0 +1,165 @@ +odoo.define('account_asset.widget_tests', function (require) { +"use strict"; + +var FormView = require('web.FormView'); +var testUtils = require('web.test_utils'); + +var createView = testUtils.createView; + +QUnit.module('fields', {}, function () { + +QUnit.module('account_asset', { + beforeEach: function () { + this.data = { + asset: { + fields: { + display_name: { string: "Displayed name", type: "char" }, + line_ids: { + string: "Lines", + type: "one2many", + relation: 'line', + relation_field: 'asset_id', + }, + }, + records: [{ + id: 1, + display_name: "asset name", + line_ids: [1, 2, 3, 4], + }], + }, + line: { + fields: { + move_check: {string: "Move Check", type: 'boolean'}, + move_posted_check: {string: "Move Posted Check", type: 'boolean'}, + asset_id: {string: "Asset", type: 'many2one', relation: 'asset'}, + }, + records: [{ + id: 1, + move_check: true, + move_posted_check: true, + }, { + id: 2, + move_check: false, + move_posted_check: true, + }, { + id: 3, + move_check: true, + move_posted_check: false, + }, { + id: 4, + move_check: false, + move_posted_check: false, + }], + }, + }; + } +}); + +QUnit.test('basic rendering', function (assert) { + assert.expect(17); + + var form = createView({ + View: FormView, + model: 'asset', + data: this.data, + arch: '', + res_id: 1, + }); + + // check the header + assert.strictEqual(form.$('thead th').text(), "", "toggler column should have no title"); + + // check the classnames + assert.ok(form.$('.o_deprec_lines_toggler_cell:nth(0) button').hasClass('o_is_posted'), + "first line toggler should have classname 'o_is_posted'"); + assert.ok(!form.$('.o_deprec_lines_toggler_cell:nth(0) button').hasClass('o_unposted'), + "first line toggler should not have classname 'o_unposted'"); + + assert.ok(form.$('.o_deprec_lines_toggler_cell:nth(1) button').hasClass('o_is_posted'), + "second line toggler should have classname 'o_is_posted'"); + assert.ok(!form.$('.o_deprec_lines_toggler_cell:nth(1) button').hasClass('o_unposted'), + "second line toggler should not have classname 'o_unposted'"); + + assert.ok(!form.$('.o_deprec_lines_toggler_cell:nth(2) button').hasClass('o_is_posted'), + "third line toggler should not have classname 'o_is_posted'"); + assert.ok(form.$('.o_deprec_lines_toggler_cell:nth(2) button').hasClass('o_unposted'), + "third line toggler should have classname 'o_unposted'"); + + assert.ok(!form.$('.o_deprec_lines_toggler_cell:nth(3) button').hasClass('o_is_posted'), + "fourth line toggler should not have classname 'o_is_posted'"); + assert.ok(!form.$('.o_deprec_lines_toggler_cell:nth(3) button').hasClass('o_unposted'), + "fourth line toggler should not have classname 'o_unposted'"); + + // check the titles + assert.strictEqual(form.$('.o_deprec_lines_toggler_cell:nth(0) button').attr('title'), + 'Posted', "first line toggler should have correct title"); + assert.strictEqual(form.$('.o_deprec_lines_toggler_cell:nth(1) button').attr('title'), + 'Posted', "second line toggler should have correct title"); + assert.strictEqual(form.$('.o_deprec_lines_toggler_cell:nth(2) button').attr('title'), + 'Accounting entries waiting for manual verification', + "third line toggler should have correct title"); + assert.strictEqual(form.$('.o_deprec_lines_toggler_cell:nth(3) button').attr('title'), + 'Unposted', "fourth line toggler should have correct title"); + + // check disabled property + assert.ok(form.$('.o_deprec_lines_toggler_cell:nth(0) button').attr('disabled'), + "first line toggle should be disabled"); + assert.ok(form.$('.o_deprec_lines_toggler_cell:nth(1) button').attr('disabled'), + "second line toggle should be disabled"); + assert.ok(form.$('.o_deprec_lines_toggler_cell:nth(2) button').attr('disabled'), + "third line toggle should be disabled"); + assert.ok(!form.$('.o_deprec_lines_toggler_cell:nth(3) button').attr('disabled'), + "fourth line toggle should not be disabled"); + + form.destroy(); +}); + +QUnit.test('click events are correctly triggered', function (assert) { + assert.expect(3); + + var form = createView({ + View: FormView, + model: 'asset', + data: this.data, + arch: '', + res_id: 1, + intercepts: { + execute_action: function (event) { + var data = event.data; + assert.strictEqual(data.model, 'line', "should have correct model"); + assert.strictEqual(data.record_id, 4, "should have correct res_id"); + assert.strictEqual(data.action_data.name, 'create_move', + "should call correct method"); + }, + } + }); + + // click on last row toggler + form.$('.o_deprec_lines_toggler_cell:nth(3) button').click(); + + form.destroy(); +}); + +}); + +}); diff --git a/addons/account_asset/views/account_asset_templates.xml b/addons/account_asset/views/account_asset_templates.xml index 53b12fe1daf..9cff8bbb378 100644 --- a/addons/account_asset/views/account_asset_templates.xml +++ b/addons/account_asset/views/account_asset_templates.xml @@ -6,6 +6,10 @@ + + +