diff --git a/account_invoice_overdue_reminder/models/res_company.py b/account_invoice_overdue_reminder/models/res_company.py index 45f0c9a1b..f5387b3fe 100644 --- a/account_invoice_overdue_reminder/models/res_company.py +++ b/account_invoice_overdue_reminder/models/res_company.py @@ -27,6 +27,9 @@ class ResCompany(models.Model): default="last_reminder", string="Contact to Remind", ) + overdue_reminder_sort_by_amount = fields.Boolean( + string="Sort Overdue reminder by remaining amount", default=True + ) @api.model def _overdue_reminder_interface_selection(self): diff --git a/account_invoice_overdue_reminder/wizard/overdue_reminder_wizard.py b/account_invoice_overdue_reminder/wizard/overdue_reminder_wizard.py index 21c2c63c5..d9300e94b 100644 --- a/account_invoice_overdue_reminder/wizard/overdue_reminder_wizard.py +++ b/account_invoice_overdue_reminder/wizard/overdue_reminder_wizard.py @@ -117,6 +117,18 @@ def _prepare_remind_trigger_domain(self, base_domain): domain.append(("user_id", "in", self.user_ids.ids)) return domain + def _sort_moves(self, moves): + # Use a config to sort by residual amount desc + # Config is set to true by default + if self.company_id.overdue_reminder_sort_by_amount: + return sorted( + moves, + key=lambda to_sort: to_sort["amount_residual_signed"], + reverse=True, + ) + # Sorted by partner name + return moves + def run(self): self.ensure_one() if self.start_days < 0: @@ -159,10 +171,8 @@ def run(self): ["commercial_partner_id", "amount_residual_signed"], ["commercial_partner_id"], ) - # Sort by residual amount desc - rg_res_sorted = sorted( - rg_res, key=lambda to_sort: to_sort["amount_residual_signed"], reverse=True - ) + rg_res_sorted = self._sort_moves(rg_res) + action_ids = [] for rg_re in rg_res_sorted: commercial_partner_id = rg_re["commercial_partner_id"][0] diff --git a/account_invoice_overdue_reminder/wizard/res_config_settings.py b/account_invoice_overdue_reminder/wizard/res_config_settings.py index 1066ad6ce..b7b32ffe5 100644 --- a/account_invoice_overdue_reminder/wizard/res_config_settings.py +++ b/account_invoice_overdue_reminder/wizard/res_config_settings.py @@ -23,3 +23,7 @@ class ResConfigSettings(models.TransientModel): overdue_reminder_partner_policy = fields.Selection( related="company_id.overdue_reminder_partner_policy", readonly=False ) + + overdue_reminder_sort_by_amount = fields.Boolean( + related="company_id.overdue_reminder_sort_by_amount", readonly=False + ) diff --git a/account_invoice_overdue_reminder/wizard/res_config_settings_view.xml b/account_invoice_overdue_reminder/wizard/res_config_settings_view.xml index dbce79c27..33cc9f340 100644 --- a/account_invoice_overdue_reminder/wizard/res_config_settings_view.xml +++ b/account_invoice_overdue_reminder/wizard/res_config_settings_view.xml @@ -34,6 +34,29 @@ +