Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[17.0][UPD] advance_check_void: correct check numbers when multiple c… #604

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions advance_check_void/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
from . import account_payment
from . import payment_check_void
from . import payment_check_history
from . import print_prenumbered_checks
48 changes: 45 additions & 3 deletions advance_check_void/models/account_payment.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Copyright (C) 2019 - TODAY, Open Source Integrators
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import math
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
from odoo.exceptions import ValidationError, UserError


class AccountPayment(models.Model):
Expand Down Expand Up @@ -108,9 +109,50 @@ def print_checks(self):
)
)
rec.message_post(body=message)
result = super().print_checks()
return result

# Since this method can be called via a client_action_multi, we need to make sure the received records are what we expect
self = self.filtered(lambda r: r.payment_method_line_id.code == 'check_printing' and r.state != 'reconciled')

if len(self) == 0:
raise UserError(_("Payments to print as a checks must have 'Check' selected as payment method and "
"not have already been reconciled"))
if any(payment.journal_id != self[0].journal_id for payment in self):
raise UserError(_("In order to print multiple checks at once, they must belong to the same bank journal."))

if not self[0].journal_id.check_manual_sequencing:
# The wizard asks for the number printed on the first pre-printed check
# so payments are attributed the number of the check the'll be printed on.
self.env.cr.execute("""
SELECT payment.id
FROM account_payment payment
JOIN account_move move ON movE.id = payment.move_id
WHERE journal_id = %(journal_id)s
AND payment.check_number IS NOT NULL
ORDER BY payment.check_number::BIGINT DESC
LIMIT 1
""", {
'journal_id': self.journal_id.id,
})
last_printed_check = self.browse(self.env.cr.fetchone())
bills_per_check = self.env['ir.config_parameter'].sudo().get_param('advance_check_void.bills_per_check') or 9
number_len = len(last_printed_check.check_number or "")
next_check_number = '%0{}d'.format(number_len) % (int(last_printed_check.check_number) + (math.ceil(len(last_printed_check.reconciled_bill_ids) / bills_per_check) or 1))

return {
'name': _('Print Pre-numbered Checks'),
'type': 'ir.actions.act_window',
'res_model': 'print.prenumbered.checks',
'view_mode': 'form',
'target': 'new',
'context': {
'payment_ids': self.ids,
'default_next_check_number': next_check_number,
}
}
else:
self.filtered(lambda r: r.state == 'draft').action_post()
return self.do_print_checks()

def void_check_button(self):
return {
"name": _("Void Check"),
Expand Down
21 changes: 21 additions & 0 deletions advance_check_void/models/print_prenumbered_checks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

import math
from odoo import models, _


class PrintPreNumberedChecks(models.TransientModel):
_inherit = 'print.prenumbered.checks'

def print_checks(self):
check_number = int(self.next_check_number)
number_len = len(self.next_check_number or "")
payments = self.env['account.payment'].browse(self.env.context['payment_ids'])
payments.filtered(lambda r: r.state == 'draft').action_post()
payments.filtered(lambda r: r.state == 'posted' and not r.is_move_sent).write({'is_move_sent': True})
for payment in payments:
bills_per_check = self.env['ir.config_parameter'].sudo().get_param('advance_check_void.bills_per_check') or 9
payment.check_number = '%0{}d'.format(number_len) % check_number
check_number += (math.ceil(len(payment.reconciled_bill_ids) / bills_per_check) or 1)
return payments.do_print_checks()
Loading