diff --git a/odex25_takaful/odex_takaful/models/donation_extension_history.py b/odex25_takaful/odex_takaful/models/donation_extension_history.py index f6bfb371e..74e583f55 100644 --- a/odex25_takaful/odex_takaful/models/donation_extension_history.py +++ b/odex25_takaful/odex_takaful/models/donation_extension_history.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- from odoo import models, fields, api, _ +from dateutil.relativedelta import relativedelta class DonationExtensionHistory(models.Model): @@ -125,6 +126,74 @@ class DonationExtensionHistory(models.Model): store=False ) + direct_debit_partner_bank_id = fields.Many2one("res.partner.bank") + debit_payment_file_attachment = fields.Binary(attachment=True) + debit_payment_attachment_file_name = fields.Char() + journal_id = fields.Many2one('account.journal') + last_digits = fields.Char() + bank_id = fields.Many2one('res.bank') + + paid_amount = fields.Float() + + + def _apply_extension_on_sponsorship(self): + new_end_date = self.donation_detail_id.end_date + relativedelta(months=self.extension_months) + + donation_line_new_vals = { + 'end_date': new_end_date, + 'payment_month_count': self.donation_detail_id.payment_month_count + self.extension_months, + } + if self.new_direct_debit: + donation_line_new_vals.update({ + 'direct_debit': self.new_direct_debit, + 'direct_debit_partner_bank_id': self.direct_debit_partner_bank_id.id, + 'journal_id': self.journal_id.id, + 'debit_payment_file_attachment': self.debit_payment_file_attachment, + 'debit_payment_attachment_file_name': self.debit_payment_attachment_file_name, + 'last_digits': self.last_digits, + 'bank_id': self.bank_id.id, + }) + + self.donation_detail_id.write(donation_line_new_vals) + + benefit_ids = self.donation_detail_id.benefit_ids | self.donation_detail_id.benefit_id + benefit_ids.write({ + 'sponsorship_end_date': new_end_date, + 'kafala_status': 'have_kafala', + }) + + # Create new scheduling lines for the extension period + if self.new_direct_debit: + self._create_extension_scheduling_lines() + + def _create_extension_scheduling_lines(self): + self.ensure_one() + + donation_line = self.donation_detail_id + start_date = donation_line.end_date + relativedelta(months=1) + + # Distribute total amount across months + base_amount, remainder = divmod(self.extension_amount, self.extension_months) + base_amount = float(base_amount) + + for month in range(self.extension_months): + scheduled_date = start_date + relativedelta(months=month) + month_year = scheduled_date.strftime("%m/%Y") + # Distribute remainder across first months + amount = base_amount + 1 if month < remainder else base_amount + # Format the amount to 2 decimal places for better representation + amount = round(amount, 2) + # Create scheduling line + self.env['sponsorship.scheduling.line'].sudo().create({ + 'sponsorship_id': donation_line.sponsorship_id.id or donation_line.sponsorship_mechanism_id.id, + 'donation_detail_linked_id': donation_line.id, + 'beneficiary_id': donation_line.benefit_id.id if donation_line.benefit_id else False, + 'month_year': month_year, + 'scheduled_date': scheduled_date, + 'amount': amount, + 'status': 'unpaid', + }) + def _sms_get_number_fields(self): """Return fields to use for SMS phone number""" return ['sponsor_phone'] @@ -172,7 +241,7 @@ class DonationExtensionHistory(models.Model): 'context': { 'active_model': 'account.move', 'active_ids': self.invoice_id.ids, - 'default_amount': self.extension_amount, + 'default_amount': self.extension_amount - self.paid_amount, 'sponsorship_payment_skip_compute_amount': True, 'dont_redirect_to_payments': True, 'sponsorship_line_ids': self.donation_detail_id.ids, diff --git a/odex25_takaful/odex_takaful/wizards/account_payment_register.py b/odex25_takaful/odex_takaful/wizards/account_payment_register.py index 800d6680a..5fea90ff0 100644 --- a/odex25_takaful/odex_takaful/wizards/account_payment_register.py +++ b/odex25_takaful/odex_takaful/wizards/account_payment_register.py @@ -377,7 +377,10 @@ class AccountRegisterPayment(models.TransientModel): if self.env.context.get('from_extension_history') and donation_extension_history_id and res: history = self.env['donation.extension.history'].browse(donation_extension_history_id) if history: - history.sudo().write({'state': 'paid'}) + history.paid_amount += self.amount + if history.paid_amount >= history.extension_amount: + history.sudo()._apply_extension_on_sponsorship() + history.sudo().write({'state': 'paid'}) elif self.env.context.get('dont_redirect_to_payments') and self.env.context.get('wiz_id'): if self.amount < self.env.context.get('default_amount'): @@ -405,6 +408,7 @@ class AccountRegisterPayment(models.TransientModel): ('invoice_id', '=', line.extension_invoice_id.id) ], limit=1) if history: + history.sudo()._apply_extension_on_sponsorship() history.sudo().write({'state': 'paid'}) return { diff --git a/odex25_takaful/odex_takaful/wizards/donation_extension_wizard.py b/odex25_takaful/odex_takaful/wizards/donation_extension_wizard.py index 464dd6cf9..21c0c55a5 100644 --- a/odex25_takaful/odex_takaful/wizards/donation_extension_wizard.py +++ b/odex25_takaful/odex_takaful/wizards/donation_extension_wizard.py @@ -353,29 +353,29 @@ class DonationExtensionWizardLine(models.TransientModel): old_end_date = self.current_end_date # Update end date - donation_line_new_vals = { - 'end_date': new_end_date, - 'payment_month_count': self.donation_line_id.payment_month_count + self.months, - } - if self.direct_debit: - donation_line_new_vals.update({ - 'direct_debit': self.direct_debit, - 'direct_debit_partner_bank_id': self.direct_debit_partner_bank_id.id, - 'journal_id': self.journal_id.id, - 'debit_payment_file_attachment': self.debit_payment_file_attachment, - 'debit_payment_attachment_file_name': self.debit_payment_attachment_file_name, - }) - self.donation_line_id.write(donation_line_new_vals) + # donation_line_new_vals = { + # 'end_date': new_end_date, + # 'payment_month_count': self.donation_line_id.payment_month_count + self.months, + # } + # if self.direct_debit: + # donation_line_new_vals.update({ + # 'direct_debit': self.direct_debit, + # 'direct_debit_partner_bank_id': self.direct_debit_partner_bank_id.id, + # 'journal_id': self.journal_id.id, + # 'debit_payment_file_attachment': self.debit_payment_file_attachment, + # 'debit_payment_attachment_file_name': self.debit_payment_attachment_file_name, + # }) + # self.donation_line_id.write(donation_line_new_vals) - benefit_ids = self.donation_line_id.benefit_ids | self.donation_line_id.benefit_id - benefit_ids.write({ - 'sponsorship_end_date': new_end_date, - 'kafala_status': 'have_kafala', - }) + # benefit_ids = self.donation_line_id.benefit_ids | self.donation_line_id.benefit_id + # benefit_ids.write({ + # 'sponsorship_end_date': new_end_date, + # 'kafala_status': 'have_kafala', + # }) - # Create new scheduling lines for the extension period - if self.direct_debit: - self._create_extension_scheduling_lines() + # # Create new scheduling lines for the extension period + # if self.direct_debit: + # self._create_extension_scheduling_lines() # Create invoice for the extension invoice_id = self._create_extension_invoice() @@ -393,6 +393,12 @@ class DonationExtensionWizardLine(models.TransientModel): 'old_direct_debit': self.donation_line_id.direct_debit, 'new_direct_debit': self.direct_debit, + 'direct_debit_partner_bank_id': self.direct_debit_partner_bank_id.id, + 'debit_payment_file_attachment': self.debit_payment_file_attachment, + 'debit_payment_attachment_file_name': self.debit_payment_attachment_file_name, + 'journal_id': self.journal_id.id, + 'last_digits': self.last_digits, + 'bank_id': self.bank_id.id, }) if not self.direct_debit: @@ -402,36 +408,36 @@ class DonationExtensionWizardLine(models.TransientModel): return False - def _create_extension_scheduling_lines(self): - """ - Create scheduling lines for the extension period - """ - self.ensure_one() + # def _create_extension_scheduling_lines(self): + # """ + # Create scheduling lines for the extension period + # """ + # self.ensure_one() - donation_line = self.donation_line_id - start_date = self.current_end_date + relativedelta(months=1) + # donation_line = self.donation_line_id + # start_date = self.current_end_date + relativedelta(months=1) - # Distribute total amount across months - base_amount, remainder = divmod(self.total_donation_amount, self.months) - base_amount = float(base_amount) + # # Distribute total amount across months + # base_amount, remainder = divmod(self.total_donation_amount, self.months) + # base_amount = float(base_amount) - for month in range(self.months): - scheduled_date = start_date + relativedelta(months=month) - month_year = scheduled_date.strftime("%m/%Y") - # Distribute remainder across first months - amount = base_amount + 1 if month < remainder else base_amount - # Format the amount to 2 decimal places for better representation - amount = round(amount, 2) - # Create scheduling line - self.env['sponsorship.scheduling.line'].sudo().create({ - 'sponsorship_id': donation_line.sponsorship_id.id or donation_line.sponsorship_mechanism_id.id, - 'donation_detail_linked_id': donation_line.id, - 'beneficiary_id': donation_line.benefit_id.id if donation_line.benefit_id else False, - 'month_year': month_year, - 'scheduled_date': scheduled_date, - 'amount': amount, - 'status': 'unpaid', - }) + # for month in range(self.months): + # scheduled_date = start_date + relativedelta(months=month) + # month_year = scheduled_date.strftime("%m/%Y") + # # Distribute remainder across first months + # amount = base_amount + 1 if month < remainder else base_amount + # # Format the amount to 2 decimal places for better representation + # amount = round(amount, 2) + # # Create scheduling line + # self.env['sponsorship.scheduling.line'].sudo().create({ + # 'sponsorship_id': donation_line.sponsorship_id.id or donation_line.sponsorship_mechanism_id.id, + # 'donation_detail_linked_id': donation_line.id, + # 'beneficiary_id': donation_line.benefit_id.id if donation_line.benefit_id else False, + # 'month_year': month_year, + # 'scheduled_date': scheduled_date, + # 'amount': amount, + # 'status': 'unpaid', + # }) def _create_extension_invoice(self): """