From 99015a4d4d69c7c789c3250c0df45f4643bbe36b Mon Sep 17 00:00:00 2001 From: AjmalCybro Date: Wed, 29 Jun 2022 14:40:20 +0530 Subject: [PATCH] [FIX] Bug fixed 'account_payment_approval' --- account_payment_approval/__manifest__.py | 2 +- account_payment_approval/doc/RELEASE_NOTES.md | 4 + .../models/account_payment.py | 115 ++++++++++++++++-- 3 files changed, 113 insertions(+), 8 deletions(-) diff --git a/account_payment_approval/__manifest__.py b/account_payment_approval/__manifest__.py index cb7c787b4..3e3af7d31 100644 --- a/account_payment_approval/__manifest__.py +++ b/account_payment_approval/__manifest__.py @@ -22,7 +22,7 @@ { 'name': 'Payment Approvals', - 'version': '14.0.1.0.0', + 'version': '14.0.1.0.1', 'category': 'Accounting', 'summary': """ This modules enables approval feature in the payment.""", 'description': """This modules enables approval feature in the payment. """, diff --git a/account_payment_approval/doc/RELEASE_NOTES.md b/account_payment_approval/doc/RELEASE_NOTES.md index e588144c2..82670d97c 100644 --- a/account_payment_approval/doc/RELEASE_NOTES.md +++ b/account_payment_approval/doc/RELEASE_NOTES.md @@ -5,3 +5,7 @@ ##### ADD - Initial commit for Payment Approval +#### 29.06.2022 +#### Version 14.0.1.0.1 +##### ADD +- Solved Payment Issue \ No newline at end of file diff --git a/account_payment_approval/models/account_payment.py b/account_payment_approval/models/account_payment.py index d7df7d289..b622b36da 100644 --- a/account_payment_approval/models/account_payment.py +++ b/account_payment_approval/models/account_payment.py @@ -37,6 +37,9 @@ class AccountPayment(models.Model): _inherit = "account.payment" _inherits = {'account.move': 'move_id'} + origin_invoice_ids = fields.Many2many('account.move', string='Invoice Origin') + to_reconcile_inv = fields.Many2one('account.move.line', string='Reconcile Move Line') + def _check_is_approver(self): approval = self.env['ir.config_parameter'].sudo().get_param( 'account_payment_approval.payment_approval') @@ -50,16 +53,62 @@ class AccountPayment(models.Model): """Overwrites the _post() to validate the payment in the 'approved' stage too. Currently Odoo allows payment posting only in draft stage. """ - validation = self._check_payment_approval() - if validation: - if self.state not in ('draft', 'approved'): - raise UserError(_("Only a draft or approved payment can be posted.")) - if any(inv.state != 'posted' for inv in self.reconciled_invoice_ids): - raise ValidationError(_("The payment cannot be processed because the invoice is not open!")) - self.move_id._post(soft=False) + if self._context.get('active_model') == 'account.move': + if self._context.get('active_ids'): + if len(self._context.get('active_ids')) == 1: + self.write({ + 'origin_invoice_ids': [(4, self._context.get('active_ids')[0])] + }) + validation = self._check_payment_approval() + if not validation: + if self.state in ('waiting_approval'): + return True + if self.state not in ('draft', 'approved'): + raise UserError(_("Only a draft or approved payment can be posted.")) + + if any(inv.state != 'posted' for inv in self.reconciled_invoice_ids): + raise ValidationError(_("The payment cannot be processed because the invoice is not open!")) + self.move_id._post(soft=False) + else: + self.move_id._post(soft=False) + + if len(self._context.get('active_ids')) > 1: + + for rec in self: + validation = rec._check_payment_approval() + + + else: + validation = self._check_payment_approval() + if not validation: + if self.state in ('waiting_approval'): + return True + if self.state not in ('draft', 'approved'): + raise UserError(_("Only a draft or approved payment can be posted.")) + + if any(inv.state != 'posted' for inv in self.reconciled_invoice_ids): + raise ValidationError(_("The payment cannot be processed because the invoice is not open!")) + self.move_id._post(soft=False) + self.reconcile_move_payment() + else: + self.move_id._post(soft=False) + self.reconcile_move_payment() + + def reconcile_move_payment(self): + if self.to_reconcile_inv: + domain = [('account_internal_type', 'in', ('receivable', 'payable')), ('reconciled', '=', False)] + payment_lines = self.line_ids.filtered_domain(domain) + lines = self.to_reconcile_inv + + for account in payment_lines.account_id: + (payment_lines + lines) \ + .filtered_domain([('account_id', '=', account.id), ('reconciled', '=', False)]) \ + .reconcile() def _check_payment_approval(self): + if not self.state: + return True if self.state == "draft": first_approval = self.env['ir.config_parameter'].sudo().get_param( 'account_payment_approval.payment_approval') @@ -80,6 +129,7 @@ class AccountPayment(models.Model): 'state': 'waiting_approval' }) return False + return True return True def approve_transfer(self): @@ -92,3 +142,54 @@ class AccountPayment(models.Model): self.write({ 'state': 'rejected' }) + + +class AccountPaymentRegister(models.TransientModel): + _inherit = 'account.payment.register' + + def _create_payments(self): + self.ensure_one() + batches = self._get_batches() + edit_mode = self.can_edit_wizard and (len(batches[0]['lines']) == 1 or self.group_payment) + to_process = [] + + if edit_mode: + payment_vals = self._create_payment_vals_from_wizard() + to_process.append({ + 'create_vals': payment_vals, + 'to_reconcile': batches[0]['lines'], + 'batch': batches[0], + }) + else: + # Don't group payments: Create one batch per move. + if not self.group_payment: + new_batches = [] + for batch_result in batches: + for line in batch_result['lines']: + new_batches.append({ + **batch_result, + 'lines': line, + }) + batches = new_batches + + for batch_result in batches: + to_reconcile_inv = batch_result['lines'] + to_process.append({ + 'create_vals': self._create_payment_vals_from_batch(batch_result), + 'to_reconcile': batch_result['lines'], + 'batch': batch_result, + }) + + payments = self._init_payments(to_process, edit_mode=edit_mode) + self._post_payments(to_process, edit_mode=edit_mode) + + for item_rec in to_process: + item_rec.get('payment').write({ + 'to_reconcile_inv': item_rec.get('to_reconcile'), + + }) + + for rec in payments: + if rec.state == 'posted': + self._reconcile_payments(to_process, edit_mode=edit_mode) + return payments