diff --git a/addons/account/static/src/components/tax_totals/tax_totals.js b/addons/account/static/src/components/tax_totals/tax_totals.js index 27413903ccc..ab55f6cf36a 100644 --- a/addons/account/static/src/components/tax_totals/tax_totals.js +++ b/addons/account/static/src/components/tax_totals/tax_totals.js @@ -79,7 +79,7 @@ class TaxGroupComponent extends Component { return; } this.props.taxGroup.tax_group_amount = newValue; - this.props.taxGroup.formatted_tax_group_amount = this.props.taxGroup.formatted_tax_group_amount.replace(oldValue.toString(), newValue.toString()); + this.props.onChangeTaxGroup({ oldValue, newValue: newValue, @@ -106,17 +106,17 @@ TaxGroupComponent.template = "account.TaxGroupComponent"; **/ export class TaxTotalsComponent extends Component { setup() { - super.setup(); - this.totals = this.props.record.data[this.props.name]; - this.readonly = this.props.readonly; + this.totals = {}; + this.formatData(this.props); onWillUpdateProps((nextProps) => { - // We only reformat tax groups if there are changed - this.totals = nextProps.record.data[nextProps.name]; - this.readonly = nextProps.readonly; - this._computeTotalsFormat(); + this.formatData(nextProps); }); } + get readonly() { + return this.props.readonly; + } + get currencyId() { const recordCurrency = this.props.record.data.currency_id; return recordCurrency && recordCurrency[0]; @@ -132,52 +132,46 @@ export class TaxTotalsComponent extends Component { /** * This method is the main function of the tax group widget. - * It is called by the TaxGroupComponent and receives the - * newer tax value. + * It is called by the TaxGroupComponent and receives the newer tax value. * - * It is responsible for calculating taxes based on tax groups and triggering - * an event to notify the ORM of a change. + * It is responsible for triggering an event to notify the ORM of a change. */ - _onChangeTaxValueByTaxGroup({ oldValue, newValue, taxGroupId }) { + _onChangeTaxValueByTaxGroup({ oldValue, newValue }) { if (oldValue === newValue) return; - this.totals.amount_total = this.totals.amount_untaxed + newValue; this.props.record.update({ [this.props.name]: this.totals }); } - _format(amount) { - return formatMonetary(amount, { currencyId: this.currencyId }); - } + formatData(props) { + let totals = props.record.data[this.props.name]; + const currencyFmtOpts = { currencyId: props.record.data.currency_id && props.record.data.currency_id[0] }; - _computeTotalsFormat() { - if (!this.totals) { - return; - } - let amount_untaxed = this.totals.amount_untaxed; + let amount_untaxed = totals.amount_untaxed; let amount_tax = 0; let subtotals = []; - for (let subtotal_title of this.totals.subtotals_order) { + for (let subtotal_title of totals.subtotals_order) { let amount_total = amount_untaxed + amount_tax; subtotals.push({ 'name': subtotal_title, 'amount': amount_total, - 'formatted_amount': this._format(amount_total), + 'formatted_amount': formatMonetary(amount_total, currencyFmtOpts), }); - let group = this.totals.groups_by_subtotal[subtotal_title]; + let group = totals.groups_by_subtotal[subtotal_title]; for (let i in group) { amount_tax = amount_tax + group[i].tax_group_amount; } } - this.totals.subtotals = subtotals; + totals.subtotals = subtotals; let amount_total = amount_untaxed + amount_tax; - this.totals.amount_total = amount_total; - this.totals.formatted_amount_total = this._format(amount_total); - for (let group_name of Object.keys(this.totals.groups_by_subtotal)) { - let group = this.totals.groups_by_subtotal[group_name]; - for (let i in group) { - group[i].formatted_tax_group_amount = this._format(group[i].tax_group_amount); - group[i].formatted_tax_group_base_amount = this._format(group[i].tax_group_base_amount); + totals.amount_total = amount_total; + totals.formatted_amount_total = formatMonetary(amount_total, currencyFmtOpts); + for (let group_name of Object.keys(totals.groups_by_subtotal)) { + let group = totals.groups_by_subtotal[group_name]; + for (let key in group) { + group[key].formatted_tax_group_amount = formatMonetary(group[key].tax_group_amount, currencyFmtOpts); + group[key].formatted_tax_group_base_amount = formatMonetary(group[key].tax_group_base_amount, currencyFmtOpts); } } + this.totals = totals; } } TaxTotalsComponent.template = "account.TaxTotalsField";