[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>`__:
* Víctor Martínez
* Alexandre D. Díaz
Maintainers
~~~~~~~~~~~

View File

@@ -1,4 +1,5 @@
# 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).
from odoo import api, models
@@ -15,16 +16,12 @@ class AccountReconciliation(models.AbstractModel):
return data
@api.model
def process_bank_statement_line(self, st_line_ids, data):
res = super().process_bank_statement_line(st_line_ids, data)
AccountMove = self.env["account.move"]
st_line_Move = self.env["account.bank.statement.line"]
key = 0
for move in res["moves"]:
if "date_due" in data[key] and data[key]["date_due"]:
move_record = AccountMove.browse(move)
st_line = st_line_Move.browse(st_line_ids[key])
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
def update_bank_statement_line_due_date(self, move_ids, st_line_ids, dates):
"""'move_ids', 'st_line_ids' and 'dates' must have the same length"""
account_move_obj = self.env["account.move"]
st_line_move_obj = self.env["account.bank.statement.line"]
for index, move_id in enumerate(move_ids):
move_record = account_move_obj.browse(move_id)
st_line = st_line_move_obj.browse(st_line_ids[index])
st_line.date_due = parse_date(self.env, dates[index])
move_record.line_ids.date_maturity = st_line.date_due

View File

@@ -1,3 +1,4 @@
* `Tecnativa <https://www.tecnativa.com>`__:
* 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">
<head>
<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>
<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">
<li><a class="reference external" href="https://www.tecnativa.com">Tecnativa</a>:<ul>
<li>Víctor Martínez</li>
<li>Alexandre D. Díaz</li>
</ul>
</li>
</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(
"account_reconciliation_widget_due_date.ReconciliationClientAction",
function(require) {
"use strict";
var action = require("account.ReconciliationClientAction");
action.StatementAction.include({
const ReconciliationClientAction = require("account.ReconciliationClientAction");
ReconciliationClientAction.StatementAction.include({
custom_events: _.extend(
{},
action.StatementAction.prototype.custom_events,
ReconciliationClientAction.StatementAction.prototype.custom_events,
{
change_date_due: "_onAction",
}

View File

@@ -1,154 +1,86 @@
/* eslint-disable init-declarations */
/* eslint-disable-line no-shadow */
/* 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.ReconciliationModel", function(
require
) {
"use strict";
var Model = require("account.ReconciliationModel");
var utils = require("web.utils");
var session = require("web.session");
var core = require("web.core");
var _t = core._t;
Model.StatementModel.include({
const ReconciliationModel = require("account.ReconciliationModel");
ReconciliationModel.StatementModel.include({
/**
* @param {String} handle
* @param {Moment} date_due
* @param {Boolean} preserveMode
* @returns {Promise}
*/
changeDateDue: function(handle, date_due, preserveMode) {
var self = this;
var line = this.getLine(handle);
const line = this.getLine(handle);
line.st_line.date_due = date_due;
return Promise.resolve(date_due)
.then(function() {
return self._computeLine(line);
})
.then(function() {
return self.changeMode(
handle,
preserveMode ? line.mode : "default",
true
);
});
return this._computeLine(line)
.then(() =>
this.changeMode(handle, preserveMode ? line.mode : "default", true)
)
.then(() => date_due);
},
/**
* @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) {
var self = this;
this.display_context = "validate";
var handles = [];
// TODO: Check if need recalculate this is next versions
let line_handles = [];
if (handle) {
handles = [handle];
line_handles = [handle];
} else {
_.each(this.lines, function(line, handle) {
_.each(this.lines, (line, line_handle) => {
if (
!line.reconciled &&
line.balance &&
!line.balance.amount &&
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.
// line.st_line.currency_id is never false here because its equivalent to
// statement_line.currency_id or statement_line.journal_id.currency_id or statement_line.journal_id.company_id.currency_id (Python-side).
// see: get_statement_line_for_reconciliation_widget method in account/models/account_bank_statement.py for more details
var currency = session.get_currency(line.st_line.currency_id);
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};
});
// HACK: Store handles to use it in '_validatePostProcess' and frees at promise resolution
this._validateLineHandles = line_handles;
return this._super.apply(this, arguments).then(results => {
this._validateLineHandles = null;
return results;
});
},
});

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(
require
) {
"use strict";
var renderer = require("account.ReconciliationRenderer");
var basic_fields = require("web.basic_fields");
var core = require("web.core");
var _t = core._t;
const ReconciliationRenderer = require("account.ReconciliationRenderer");
const basic_fields = require("web.basic_fields");
const core = require("web.core");
renderer.LineRenderer.include({
const _t = core._t;
ReconciliationRenderer.LineRenderer.include({
/**
* @override
*/
_onFieldChanged: function(event) {
var fieldName = event.target.name;
const fieldName = event.target.name;
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});
} else {
this._super.apply(this, arguments);
}
},
/**
* @override
*/
start: function() {
this._super.apply(this, arguments);
var self = this;
this._makeDateDueRecord().then(function(recordID) {
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")
);
});
return Promise.all([
this._super.apply(this, arguments),
this._makeDateDueRecord(),
]);
},
/**
*
* @returns {Promise}
*/
_makeDateDueRecord: function() {
var field = {
const field = {
type: "date",
name: "date_due",
};
return this.model.makeRecord("account.bank.statement.line", [field], {
date_due: {},
});
return this.model
.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(
[line_b.id],
[
{
"partner_id": line_b.partner_id.id,
"new_aml_dicts": new_aml_dicts,
"date_due": line_b.date_due,
}
],
[{"partner_id": line_b.partner_id.id, "new_aml_dicts": new_aml_dicts}],
)
reconciliation_widget.update_bank_statement_line_due_date(
res["moves"], [line_b.id], [line_b.date_due],
)
self.assertEqual(len(res["moves"]), 1)
move = account_move_model.browse(res["moves"][0])
@@ -123,13 +120,10 @@ class TestAccountReconciliationWidgetDueDate(TransactionCase):
]
res = reconciliation_widget.process_bank_statement_line(
[line_c.id],
[
{
"partner_id": line_c.partner_id.id,
"new_aml_dicts": new_aml_dicts,
"date_due": "2021-02-05",
}
],
[{"partner_id": line_c.partner_id.id, "new_aml_dicts": new_aml_dicts}],
)
reconciliation_widget.update_bank_statement_line_due_date(
res["moves"], [line_c.id], ["2021-02-05"],
)
self.assertEqual(line_c.date_due, date(2021, 2, 5))
self.assertEqual(len(res["moves"]), 1)