224 lines
10 KiB
Python
224 lines
10 KiB
Python
# -*- coding: utf-8 -*-
|
|
from odoo import models, fields, api, _
|
|
from odoo.exceptions import UserError, ValidationError, Warning
|
|
import logging
|
|
from odoo import SUPERUSER_ID
|
|
from datetime import datetime, date, timedelta
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
class EsterdadWizard(models.Model):
|
|
_name = "esterdad.wizard"
|
|
|
|
amount = fields.Monetary(currency_field='currency_id', compute='_get_total_sponsorship_amount')
|
|
currency_id = fields.Many2one(
|
|
'res.currency',
|
|
string="Currency",
|
|
required=True,
|
|
default=lambda self: self.env.company.currency_id
|
|
)
|
|
cancel_date = fields.Date()
|
|
pay_date = fields.Datetime(related='sponsor_id.pay_date')
|
|
cancel_reason = fields.Many2one('sponsorship.reason.stop')
|
|
another_reason = fields.Boolean()
|
|
reason = fields.Text()
|
|
sponsor_id = fields.Many2one('takaful.sponsorship')
|
|
sponsor_name = fields.Char()
|
|
mobile = fields.Char()
|
|
id_num = fields.Char()
|
|
payment_ids = fields.Many2many(
|
|
'account.payment',
|
|
'esterdad_wizard_payment_rel',
|
|
'wizard_id',
|
|
'payment_id',
|
|
string="Payments",
|
|
domain="[('id', 'in', allowed_payment_ids)]",
|
|
)
|
|
allowed_payment_ids = fields.Many2many(
|
|
'account.payment',
|
|
'esterdad_wizard_allowed_payment_rel',
|
|
'wizard_id',
|
|
'payment_id',
|
|
string="Allowed Payments",
|
|
)
|
|
confirmed = fields.Boolean()
|
|
|
|
@api.onchange('sponsor_id')
|
|
def _onchange_sponsor_id_set_payment_domain(self):
|
|
"""Limit available payments to same partner within restriction_period days."""
|
|
for rec in self:
|
|
domain = []
|
|
partner = False
|
|
invoice_names = []
|
|
|
|
if rec.sponsor_id:
|
|
# Assuming sponsor_id.sponsor_id is the related partner on the sponsorship
|
|
partner = getattr(rec.sponsor_id, 'sponsor_id', False) or getattr(rec.sponsor_id, 'partner_id', False)
|
|
|
|
# Get all invoices from journal_entry_ids where move_type is 'out_invoice'
|
|
invoices = rec.sponsor_id.journal_entry_ids.filtered(lambda inv: inv.move_type == 'out_invoice')
|
|
# Get all invoice names (numbers) in a list
|
|
invoice_names = invoices.mapped('name')
|
|
|
|
if partner:
|
|
# Get restriction period (in days) from config parameters
|
|
sudo_conf = self.env['ir.config_parameter'].sudo()
|
|
restriction_period = sudo_conf.get_param('odex_takaful.restriction_period')
|
|
|
|
try:
|
|
restriction_period = int(restriction_period or 0)
|
|
except (TypeError, ValueError):
|
|
restriction_period = 0
|
|
|
|
if restriction_period and restriction_period > 0:
|
|
limit_date = date.today() - timedelta(days=restriction_period)
|
|
domain = [
|
|
('partner_id', '=', partner.id),
|
|
('date', '>=', limit_date),
|
|
]
|
|
else:
|
|
# If no restriction configured, just filter by partner
|
|
domain = [('partner_id', '=', partner.id)]
|
|
|
|
# Add filter for ref field to match invoice names if we have any
|
|
if invoice_names:
|
|
domain.append(('ref', 'in', invoice_names))
|
|
|
|
# If we have a domain, fetch the matching payments and store them
|
|
# in the helper field, then use their IDs as the domain.
|
|
if domain:
|
|
payments = self.env['account.payment'].search(domain)
|
|
rec.allowed_payment_ids = payments
|
|
return {'domain': {'payment_ids': [('id', 'in', payments.ids)]}}
|
|
else:
|
|
rec.allowed_payment_ids = False
|
|
return {'domain': {'payment_ids': []}}
|
|
def action_confirm_refund(self):
|
|
for rec in self:
|
|
user = rec.sponsor_id.sponsor_id.kafel_id
|
|
if user:
|
|
user.generate_otp()
|
|
context = dict(self.env.context or {})
|
|
context['default_user_id'] = user.id
|
|
context['default_otp'] = user.otp_code
|
|
context['default_esterdad_id'] = rec.id
|
|
rec.sponsor_id.write({
|
|
'cancel_record_id': rec.id
|
|
})
|
|
context['default_payment_ids'] = [(6, 0, rec.payment_ids.ids)]
|
|
view = self.env.ref('odex_takaful.view_otp_wizard_form')
|
|
return {
|
|
'name': _('OTP Confirmation'),
|
|
'view_mode': 'form',
|
|
'view_type': 'form',
|
|
'type': 'ir.actions.act_window',
|
|
'res_model': 'otp.confirmation.wizard',
|
|
'view_id': view.id,
|
|
'target': 'new',
|
|
'context': context,
|
|
}
|
|
else:
|
|
raise UserError(_("No user assigned to this sponsor"))
|
|
|
|
@api.depends('sponsor_id.donations_details_lines', 'sponsor_id.donations_details_lines_mechanism_ids')
|
|
def _get_total_sponsorship_amount(self):
|
|
|
|
for rec in self:
|
|
if rec.sponsor_id:
|
|
rec.amount = (
|
|
sum(line.total_donation_amount for line in rec.sponsor_id.donations_details_lines) +
|
|
sum(line.total_donation_amount for line in rec.sponsor_id.donations_details_lines_mechanism_ids)
|
|
)
|
|
|
|
|
|
class OTPWizard(models.TransientModel):
|
|
_name = "otp.confirmation.wizard"
|
|
|
|
otp = fields.Integer()
|
|
otp_code = fields.Integer()
|
|
esterdad_id = fields.Many2one('esterdad.wizard')
|
|
user_id = fields.Many2one('res.users')
|
|
payment_ids = fields.Many2many(
|
|
'account.payment',
|
|
string="Payments"
|
|
)
|
|
|
|
def action_confirm_otp(self):
|
|
for rec in self:
|
|
if rec.otp == rec.otp_code:
|
|
sponsor_ship = rec.esterdad_id.sponsor_id
|
|
rec.esterdad_id.cancel_date = date.today()
|
|
if sponsor_ship:
|
|
# Get ref values from selected payments
|
|
payment_refs = rec.payment_ids.mapped('ref')
|
|
# Filter out empty/False refs
|
|
payment_refs = [ref for ref in payment_refs if ref]
|
|
|
|
if not payment_refs:
|
|
raise UserError(_("No references found in selected payments"))
|
|
|
|
# Search for invoices in account.move matching payment refs
|
|
invoices = self.env['account.move'].search([
|
|
('name', 'in', payment_refs),
|
|
])
|
|
if not invoices:
|
|
raise UserError(_("No invoices matching the specified references"))
|
|
for invoice in invoices:
|
|
credit = self.env['account.move.reversal'].with_company(self.env.user.company_id.id).create({
|
|
'refund_method': 'refund',
|
|
'date': date.today(),
|
|
'date_mode': 'custom',
|
|
'move_ids': [invoice.id],
|
|
})
|
|
x = credit.reverse_moves()
|
|
sponsor_ship.state = 'canceled'
|
|
domain_ids = x.get('res_id')
|
|
if domain_ids:
|
|
last_id = domain_ids
|
|
sponsor_ship.write({
|
|
'journal_entry_ids': [(4, last_id)]
|
|
})
|
|
credit_note = self.env['account.move'].search([('id', '=', last_id)])
|
|
credit_note.action_post()
|
|
credit_note.esterdad_id = rec.esterdad_id
|
|
rec.esterdad_id.confirmed = True
|
|
rec.esterdad_id.sponsor_id.is_canceled_refund = True
|
|
|
|
# Get all donation lines from donations_details_lines and donations_details_lines_mechanism_ids
|
|
all_donation_lines = sponsor_ship.donations_details_lines | sponsor_ship.donations_details_lines_mechanism_ids
|
|
|
|
# Set state to 'cancel' for all donation lines
|
|
if all_donation_lines:
|
|
all_donation_lines.write({'state': 'cancel'})
|
|
all_donation_lines.mapped('benefit_id').write({'kafala_status': 'have_not_kafala',
|
|
'sponsor_related_id': False,
|
|
'sponsorship_end_date': fields.Date.today(),
|
|
})
|
|
# Process extension history for each donation line
|
|
for donation_line in all_donation_lines:
|
|
# Get extension_history_ids for this donation line
|
|
extension_histories = donation_line.extension_history_ids
|
|
|
|
if extension_histories:
|
|
# Set state to 'cancel' for all extension histories
|
|
extension_histories.write({'state': 'cancel'})
|
|
total_extension_months = 0
|
|
current_payment_month_count = 0
|
|
new_payment_month_count = 0
|
|
# Sum all extension_months
|
|
total_extension_months = sum(extension_histories.mapped('extension_months'))
|
|
|
|
# Subtract the sum from payment_month_count
|
|
if total_extension_months > 0:
|
|
current_payment_month_count = donation_line.payment_month_count or 0
|
|
new_payment_month_count = current_payment_month_count - total_extension_months
|
|
# Ensure it doesn't go below 0
|
|
donation_line.write({
|
|
'payment_month_count': max(0, new_payment_month_count)
|
|
})
|
|
|
|
|
|
else:
|
|
raise UserError(_("The entered number is incorrect"))
|