[IMP] account_reconciliation_widget_due_date: Refactor code

This commit is contained in:
Alexandre D. Díaz
2021-07-13 17:55:29 +02:00
committed by Víctor Martínez
parent 35d0df7a12
commit e4c6ca1840
8 changed files with 139 additions and 191 deletions

View File

@@ -66,6 +66,7 @@ Contributors
* `Tecnativa <https://www.tecnativa.com>`__: * `Tecnativa <https://www.tecnativa.com>`__:
* Víctor Martínez * Víctor Martínez
* Alexandre D. Díaz
Maintainers Maintainers
~~~~~~~~~~~ ~~~~~~~~~~~

View File

@@ -1,4 +1,5 @@
# Copyright 2021 Tecnativa - Víctor Martínez # Copyright 2021 Tecnativa - Víctor Martínez
# Copyright 2021 Tecnativa - Alexandre D. Díaz
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo import api, models from odoo import api, models
@@ -15,16 +16,12 @@ class AccountReconciliation(models.AbstractModel):
return data return data
@api.model @api.model
def process_bank_statement_line(self, st_line_ids, data): def update_bank_statement_line_due_date(self, move_ids, st_line_ids, dates):
res = super().process_bank_statement_line(st_line_ids, data) """'move_ids', 'st_line_ids' and 'dates' must have the same length"""
AccountMove = self.env["account.move"] account_move_obj = self.env["account.move"]
st_line_Move = self.env["account.bank.statement.line"] st_line_move_obj = self.env["account.bank.statement.line"]
key = 0 for index, move_id in enumerate(move_ids):
for move in res["moves"]: move_record = account_move_obj.browse(move_id)
if "date_due" in data[key] and data[key]["date_due"]: st_line = st_line_move_obj.browse(st_line_ids[index])
move_record = AccountMove.browse(move) st_line.date_due = parse_date(self.env, dates[index])
st_line = st_line_Move.browse(st_line_ids[key]) move_record.line_ids.date_maturity = st_line.date_due
st_line.date_due = parse_date(self.env, data[key]["date_due"])
move_record.line_ids.date_maturity = st_line.date_due
key += 1
return res

View File

@@ -1,3 +1,4 @@
* `Tecnativa <https://www.tecnativa.com>`__: * `Tecnativa <https://www.tecnativa.com>`__:
* Víctor Martínez * Víctor Martínez
* Alexandre D. Díaz

View File

@@ -3,7 +3,7 @@
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head> <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils: http://docutils.sourceforge.net/" /> <meta name="generator" content="Docutils 0.15.1: http://docutils.sourceforge.net/" />
<title>Account Reconciliation Widget Due Date</title> <title>Account Reconciliation Widget Due Date</title>
<style type="text/css"> <style type="text/css">
@@ -414,6 +414,7 @@ If you spotted it first, help us smashing it by providing a detailed and welcome
<ul class="simple"> <ul class="simple">
<li><a class="reference external" href="https://www.tecnativa.com">Tecnativa</a>:<ul> <li><a class="reference external" href="https://www.tecnativa.com">Tecnativa</a>:<ul>
<li>Víctor Martínez</li> <li>Víctor Martínez</li>
<li>Alexandre D. Díaz</li>
</ul> </ul>
</li> </li>
</ul> </ul>

View File

@@ -1,13 +1,17 @@
/* Copyright 2021 Tecnativa - Víctor Martínez
* License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */
odoo.define( odoo.define(
"account_reconciliation_widget_due_date.ReconciliationClientAction", "account_reconciliation_widget_due_date.ReconciliationClientAction",
function(require) { function(require) {
"use strict"; "use strict";
var action = require("account.ReconciliationClientAction");
action.StatementAction.include({ const ReconciliationClientAction = require("account.ReconciliationClientAction");
ReconciliationClientAction.StatementAction.include({
custom_events: _.extend( custom_events: _.extend(
{}, {},
action.StatementAction.prototype.custom_events, ReconciliationClientAction.StatementAction.prototype.custom_events,
{ {
change_date_due: "_onAction", change_date_due: "_onAction",
} }

View File

@@ -1,154 +1,86 @@
/* eslint-disable init-declarations */ /* Copyright 2021 Tecnativa - Víctor Martínez
/* eslint-disable-line no-shadow */ * Copyright 2021 Tecnativa - Alexandre D. Díaz
* License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */
odoo.define("account_reconciliation_widget_due_date.ReconciliationModel", function( odoo.define("account_reconciliation_widget_due_date.ReconciliationModel", function(
require require
) { ) {
"use strict"; "use strict";
var Model = require("account.ReconciliationModel"); const ReconciliationModel = require("account.ReconciliationModel");
var utils = require("web.utils");
var session = require("web.session"); ReconciliationModel.StatementModel.include({
var core = require("web.core"); /**
var _t = core._t; * @param {String} handle
Model.StatementModel.include({ * @param {Moment} date_due
* @param {Boolean} preserveMode
* @returns {Promise}
*/
changeDateDue: function(handle, date_due, preserveMode) { changeDateDue: function(handle, date_due, preserveMode) {
var self = this; const line = this.getLine(handle);
var line = this.getLine(handle);
line.st_line.date_due = date_due; line.st_line.date_due = date_due;
return Promise.resolve(date_due)
.then(function() { return this._computeLine(line)
return self._computeLine(line); .then(() =>
}) this.changeMode(handle, preserveMode ? line.mode : "default", true)
.then(function() { )
return self.changeMode( .then(() => date_due);
handle,
preserveMode ? line.mode : "default",
true
);
});
}, },
/**
* @override
*/
_validatePostProcess: function(data) {
if (_.isEmpty(this._validateLineHandles)) {
return this._super.apply(this, arguments);
}
const line_ids = [];
const dates = [];
for (const handle of this._validateLineHandles) {
const line = this.getLine(handle);
line_ids.push(line.id);
dates.push(line.st_line.date_due);
}
const tasks = [];
tasks.push(this._super.apply(this, arguments));
tasks.push(
this._rpc({
model: "account.reconciliation.widget",
method: "update_bank_statement_line_due_date",
args: [data.moves, line_ids, dates],
context: this.context,
})
);
return Promise.all(tasks);
},
/**
* @override
*/
validate: function(handle) { validate: function(handle) {
var self = this; // TODO: Check if need recalculate this is next versions
this.display_context = "validate"; let line_handles = [];
var handles = [];
if (handle) { if (handle) {
handles = [handle]; line_handles = [handle];
} else { } else {
_.each(this.lines, function(line, handle) { _.each(this.lines, (line, line_handle) => {
if ( if (
!line.reconciled && !line.reconciled &&
line.balance && line.balance &&
!line.balance.amount && !line.balance.amount &&
line.reconciliation_proposition.length line.reconciliation_proposition.length
) { ) {
handles.push(handle); line_handles.push(line_handle);
} }
}); });
} }
var ids = [];
var values = [];
var handlesPromises = [];
_.each(handles, function(handle) {
var line = self.getLine(handle);
var props = _.filter(line.reconciliation_proposition, function(prop) {
return !prop.invalid;
});
var computeLinePromise;
if (props.length === 0) {
// Usability: if user has not chosen any lines and click validate, it has the same behavior
// as creating a write-off of the same amount.
props.push(
self._formatQuickCreate(line, {
account_id: [
line.st_line.open_balance_account_id,
self.accounts[line.st_line.open_balance_account_id],
],
})
);
// Update balance of line otherwise it won't be to zero and another line will be added
line.reconciliation_proposition.push(props[0]);
computeLinePromise = self._computeLine(line);
}
ids.push(line.id);
handlesPromises.push(
Promise.resolve(computeLinePromise).then(function() {
var values_dict = {
partner_id: line.st_line.partner_id,
counterpart_aml_dicts: _.map(
_.filter(props, function(prop) {
return !isNaN(prop.id) && !prop.already_paid;
}),
self._formatToProcessReconciliation.bind(self, line)
),
payment_aml_ids: _.pluck(
_.filter(props, function(prop) {
return !isNaN(prop.id) && prop.already_paid;
}),
"id"
),
new_aml_dicts: _.map(
_.filter(props, function(prop) {
return isNaN(prop.id) && prop.display;
}),
self._formatToProcessReconciliation.bind(self, line)
),
to_check: line.to_check,
date_due: line.st_line.date_due,
};
// If the lines are not fully balanced, create an unreconciled amount. // HACK: Store handles to use it in '_validatePostProcess' and frees at promise resolution
// line.st_line.currency_id is never false here because its equivalent to this._validateLineHandles = line_handles;
// statement_line.currency_id or statement_line.journal_id.currency_id or statement_line.journal_id.company_id.currency_id (Python-side). return this._super.apply(this, arguments).then(results => {
// see: get_statement_line_for_reconciliation_widget method in account/models/account_bank_statement.py for more details this._validateLineHandles = null;
var currency = session.get_currency(line.st_line.currency_id); return results;
var balance = line.balance.amount;
if (!utils.float_is_zero(balance, currency.digits[1])) {
var unreconciled_amount_dict = {
account_id: line.st_line.open_balance_account_id,
credit: balance > 0 ? balance : 0,
debit: balance < 0 ? -balance : 0,
name: line.st_line.name + " : " + _t("Open balance"),
};
values_dict.new_aml_dicts.push(unreconciled_amount_dict);
}
values.push(values_dict);
line.reconciled = true;
})
);
_.each(self.lines, function(other_line) {
if (other_line != line) {
var filtered_prop = other_line.reconciliation_proposition.filter(
p =>
!line.reconciliation_proposition
.map(l => l.id)
.includes(p.id)
);
if (
filtered_prop.length !=
other_line.reconciliation_proposition.length
) {
other_line.need_update = true;
other_line.reconciliation_proposition = filtered_prop;
}
self._computeLine(line);
}
});
});
return Promise.all(handlesPromises).then(function() {
return self
._rpc({
model: "account.reconciliation.widget",
method: "process_bank_statement_line",
args: [ids, values],
context: self.context,
})
.then(self._validatePostProcess.bind(self))
.then(function() {
self.valuenow += handles.length;
return {handles: handles};
});
}); });
}, },
}); });

View File

@@ -1,52 +1,70 @@
/* Copyright 2021 Tecnativa - Víctor Martínez
* Copyright 2021 Tecnativa - Alexandre D. Díaz
* License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */
odoo.define("account_reconciliation_widget_due_date.ReconciliationRenderer", function( odoo.define("account_reconciliation_widget_due_date.ReconciliationRenderer", function(
require require
) { ) {
"use strict"; "use strict";
var renderer = require("account.ReconciliationRenderer"); const ReconciliationRenderer = require("account.ReconciliationRenderer");
var basic_fields = require("web.basic_fields"); const basic_fields = require("web.basic_fields");
var core = require("web.core"); const core = require("web.core");
var _t = core._t;
renderer.LineRenderer.include({ const _t = core._t;
ReconciliationRenderer.LineRenderer.include({
/**
* @override
*/
_onFieldChanged: function(event) { _onFieldChanged: function(event) {
var fieldName = event.target.name; const fieldName = event.target.name;
if (fieldName === "date_due") { if (fieldName === "date_due") {
var date_due = event.data.changes.date_due; const date_due = event.data.changes.date_due;
this.trigger_up("change_date_due", {data: date_due}); this.trigger_up("change_date_due", {data: date_due});
} else { } else {
this._super.apply(this, arguments); this._super.apply(this, arguments);
} }
}, },
/**
* @override
*/
start: function() { start: function() {
this._super.apply(this, arguments); return Promise.all([
var self = this; this._super.apply(this, arguments),
this._makeDateDueRecord().then(function(recordID) { this._makeDateDueRecord(),
self.fields.date_due = new basic_fields.FieldDate( ]);
self,
"date_due",
self.model.get(recordID),
{
mode: "edit",
attrs: {placeholder: _t("Select Due date")},
}
);
if (self._initialState.st_line.date_due !== "") {
self.fields.date_due.value = self._initialState.st_line.date_due;
}
self.fields.date_due.insertAfter(
self.$(".accounting_view caption .o_buttons")
);
});
}, },
/**
*
* @returns {Promise}
*/
_makeDateDueRecord: function() { _makeDateDueRecord: function() {
var field = { const field = {
type: "date", type: "date",
name: "date_due", name: "date_due",
}; };
return this.model.makeRecord("account.bank.statement.line", [field], { return this.model
date_due: {}, .makeRecord("account.bank.statement.line", [field], {
}); date_due: {},
})
.then(recordID => {
this.fields.date_due = new basic_fields.FieldDate(
this,
"date_due",
this.model.get(recordID),
{
mode: "edit",
attrs: {placeholder: _t("Select Due date")},
}
);
if (this._initialState.st_line.date_due !== "") {
this.fields.date_due.value = this._initialState.st_line.date_due;
}
this.fields.date_due.appendTo(this.$(".accounting_view caption"));
});
}, },
}); });
}); });

View File

@@ -95,13 +95,10 @@ class TestAccountReconciliationWidgetDueDate(TransactionCase):
] ]
res = reconciliation_widget.process_bank_statement_line( res = reconciliation_widget.process_bank_statement_line(
[line_b.id], [line_b.id],
[ [{"partner_id": line_b.partner_id.id, "new_aml_dicts": new_aml_dicts}],
{ )
"partner_id": line_b.partner_id.id, reconciliation_widget.update_bank_statement_line_due_date(
"new_aml_dicts": new_aml_dicts, res["moves"], [line_b.id], [line_b.date_due],
"date_due": line_b.date_due,
}
],
) )
self.assertEqual(len(res["moves"]), 1) self.assertEqual(len(res["moves"]), 1)
move = account_move_model.browse(res["moves"][0]) move = account_move_model.browse(res["moves"][0])
@@ -123,13 +120,10 @@ class TestAccountReconciliationWidgetDueDate(TransactionCase):
] ]
res = reconciliation_widget.process_bank_statement_line( res = reconciliation_widget.process_bank_statement_line(
[line_c.id], [line_c.id],
[ [{"partner_id": line_c.partner_id.id, "new_aml_dicts": new_aml_dicts}],
{ )
"partner_id": line_c.partner_id.id, reconciliation_widget.update_bank_statement_line_due_date(
"new_aml_dicts": new_aml_dicts, res["moves"], [line_c.id], ["2021-02-05"],
"date_due": "2021-02-05",
}
],
) )
self.assertEqual(line_c.date_due, date(2021, 2, 5)) self.assertEqual(line_c.date_due, date(2021, 2, 5))
self.assertEqual(len(res["moves"]), 1) self.assertEqual(len(res["moves"]), 1)