diff --git a/sale_commission/models/account_move.py b/sale_commission/models/account_move.py index cfc6185c9..00fc6429b 100644 --- a/sale_commission/models/account_move.py +++ b/sale_commission/models/account_move.py @@ -221,3 +221,16 @@ def _skip_settlement(self): self.commission_id.invoice_state == "paid" and self.invoice_id.payment_state not in ["in_payment", "paid"] ) or self.invoice_id.state != "posted" + + def _skip_future_payments(self, date_payment_to): + if self.commission_id.invoice_state == "paid": + payments_dates = [] + for ( + _partial, + _amount, + counterpart_line, + ) in self.invoice_id._get_reconciled_invoices_partials(): + payments_dates.append(counterpart_line.date) + if any(date_payment_to < date for date in payments_dates): + return True + return False diff --git a/sale_commission/wizard/wizard_settle.py b/sale_commission/wizard/wizard_settle.py index eebd54b77..7f3282331 100644 --- a/sale_commission/wizard/wizard_settle.py +++ b/sale_commission/wizard/wizard_settle.py @@ -1,7 +1,6 @@ # Copyright 2014-2020 Tecnativa - Pedro M. Baeza # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -import json -from datetime import date, datetime +from datetime import date from dateutil.relativedelta import relativedelta @@ -102,6 +101,10 @@ def action_settle(self): pos += 1 if line._skip_settlement(): continue + if self.date_payment_to and line._skip_future_payments( + self.date_payment_to + ): + continue if line.invoice_date > sett_to: sett_from = self._get_period_start(agent, line.invoice_date) sett_to = self._get_next_period_date( @@ -143,23 +146,4 @@ def _get_agent_lines(self, agent, date_to_agent): ], order="invoice_date", ) - if self.date_payment_to: - aila = aila.filtered( - lambda line: not ( - line.commission_id.invoice_state == "paid" - and line.invoice_id.invoice_payments_widget != "false" - and not self.check_payment_date(line) - ) - ) return aila - - def check_payment_date(self, agent_line): - move_dict_content = json.loads(agent_line.invoice_id.invoice_payments_widget)[ - "content" - ] - return all( - [ - self.date_payment_to > datetime.strptime(x["date"], "%Y-%m-%d").date() - for x in move_dict_content - ] - )