Merge pull request #6333 from expsa/samir_aladawi_refund_sponsorship

[UPD] odex_takaful
This commit is contained in:
SamirLADOUI-sa 2026-01-24 02:19:01 +01:00 committed by GitHub
commit 6d813a11fb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 150 additions and 108 deletions

View File

@ -2982,6 +2982,7 @@ msgstr "مفوتر"
#: model:ir.model.fields,field_description:odex_takaful.field_sponsorship_payment__invoice_ids
#: model:ir.model.fields,field_description:odex_takaful.field_takaful_sponsorship__invoice_count
#: model_terms:ir.ui.view,arch_db:odex_takaful.takaful_sponsorship_form
#: model_terms:ir.ui.view,arch_db:odex_takaful.view_esterdad_wizard_form
#, python-format
msgid "Invoices"
msgstr "الفواتير"
@ -7581,3 +7582,8 @@ msgstr "التمديد الشامل"
#: model:ir.actions.server,name:odex_takaful.server_action_print_mother_and_orphan_reports
msgid "Print Mother/Orphan Report"
msgstr "طباعة تقرير أم اليتيم/اليتيم"
#. module: odex_takaful
#: model_terms:ir.ui.view,arch_db:odex_takaful.takaful_account_move_inherit_form
msgid "Previous Payments"
msgstr "المدفوعات السابقة"

View File

@ -15,6 +15,9 @@ class AccountMove(models.Model):
payment_id = fields.Many2one('account.payment', string='Payment', copy=False)
esterdad_id = fields.Many2one('esterdad.wizard')
previous_payment_ids = fields.Many2many(related='esterdad_id.payment_ids')
sponsorship_cancel_reason = fields.Text(readonly=True)
def action_view_esterdad_id(self):
self.ensure_one()

View File

@ -330,9 +330,9 @@ class TakafulSponsorship(models.Model):
context = dict(self.env.context or {})
context['default_sponsor_id'] = rec.id
context['default_pay_date'] = rec.pay_date
context['default_amount'] = rec.total_sponsorship_amount
# context['default_amount'] = rec.total_sponsorship_amount
context['default_sponsor_name'] = rec.sponsor_id.first_name
context['default_mobile'] = rec.sponsor_id.mobile
context['default_mobile'] = rec.sponsor_phone
context['default_id_num'] = rec.sponsor_id.id_number
# context['default_payment_ids'] = rec.payment_ids.ids
view = self.env.ref('odex_takaful.view_esterdad_wizard_form')
@ -349,13 +349,11 @@ class TakafulSponsorship(models.Model):
else:
raise UserError(_("You cannot do this. The refund period has expired."))
@api.depends('sponsor_id','sponsor_id.mobile')
@api.depends('sponsor_id', 'sponsor_id.mobile')
def _compute_sponsor_phone(self):
for rec in self:
if rec.sponsor_id.id != rec.env.company.faal_kheer_partner_id.id:
rec.sponsor_phone = rec.sponsor_id.mobile if rec.sponsor_id else False
for rec in self.filtered(lambda r: r.state in ['draft']):
if rec.sponsor_or_donor_type == 'registered':
rec.sponsor_phone = rec.sponsor_id.mobile
def compute_days_after_payment(self):
"""Check if the number of hours passed after payment is within the configured limit"""
@ -1057,7 +1055,7 @@ class TakafulSponsorship(models.Model):
rec.sponsor_donor_type = 'registered'
# Update related fields from selected sponsor
rec.sponsor_phone = rec.sponsor_id.mobile
# rec.sponsor_phone = rec.sponsor_id.mobile
rec.preferred_communication = rec.sponsor_id.preferred_communication
@api.model

View File

@ -5,6 +5,13 @@
<field name="model">account.move</field>
<field name="inherit_id" ref="account.view_move_form"/>
<field name="arch" type="xml">
<xpath expr="//header" position="after">
<div class="alert alert-danger" role="alert"
style="margin-bottom:0px;text-align:center;"
attrs="{'invisible':[('esterdad_id', '=', False)]}">
<field name="sponsorship_cancel_reason"/>
</div>
</xpath>
<xpath expr="//field[@name='invoice_origin']" position="attributes">
<attribute name="attrs">{'invisible': False}</attribute>
@ -14,7 +21,11 @@
</xpath>
<xpath expr="//notebook" position="inside">
<page string="Previous Payments" attrs="{'invisible': [('esterdad_id', '=', False)]}">
<field name="previous_payment_ids" widget="many2many" />
</page>
</xpath>
</field>

View File

@ -193,7 +193,7 @@
]
}"/>
<div class="o_row">
<field name="sponsor_id" nolabel="1"
<field name="sponsor_id" nolabel="1"
context="{'form_view_ref': 'odex_takaful.view_takaful_sponsor_form' ,'tree_view_ref':'odex_takaful.takaful_sponsor_tree'}"
attrs="{
'invisible': ['|',
@ -228,16 +228,11 @@
],
'readonly': [('state','!=','draft')]
}"/>
<field name="sponsor_phone" string="Mobile Number" widget="phone"
<field name="sponsor_phone" string="Mobile Number" widget="phone" placeholder="05xxxxxxxx" force_save="1"
attrs="{
'invisible': ['|',
'&amp;', ('record_type','=','donation'), ('sponsor_or_donor_type','!=','unknown'),
'&amp;', ('record_type','=','sponsorship'), ('sponsor_donor_type','!=','unknown')
],
'readonly': [('state','!=','draft')]
}"
placeholder="05xxxxxxxx"/>
'required': [('sponsor_or_donor_type', '!=', 'unknown')],
'readonly': [('state', '!=', 'draft')]
}"/>
<field name="preferred_communication" string="Preferred Communication"
attrs="{

View File

@ -24,76 +24,89 @@ class EsterdadWizard(models.Model):
another_reason = fields.Boolean()
reason = fields.Text()
sponsor_id = fields.Many2one('takaful.sponsorship')
journal_entry_ids = fields.Many2many('account.move', domain="[('id', 'in', allowed_journal_entry_ids)]")
allowed_journal_entry_ids = fields.Many2many('account.move', compute='_compute_allowed_journal_entry_ids')
sponsor_name = fields.Char()
mobile = fields.Char()
mobile = fields.Char(required=True)
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",
)
payment_ids = fields.Many2many('account.payment', compute='_compute_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."""
@api.depends('sponsor_id')
def _compute_allowed_journal_entry_ids(self):
for rec in self:
domain = []
partner = False
invoice_names = []
refund_hour_limit = self.env['ir.config_parameter'].sudo().get_param('odex_takaful.cancel_refund', default=0)
limit_refund_date_time = fields.Datetime.now() - timedelta(hours=int(refund_hour_limit))
# rec.allowed_journal_entry_ids = rec.sponsor_id.journal_entry_ids.ids
rec.allowed_journal_entry_ids = rec.sponsor_id.journal_entry_ids.filtered(lambda r:
r.move_type == 'out_invoice' and\
r.payment_state == 'paid' and\
r.create_date >= limit_refund_date_time).ids
@api.depends('journal_entry_ids')
def _compute_payment_ids(self):
for rec in self:
rec.payment_ids = [(6, 0, rec.journal_entry_ids.payment_id.ids)]
# @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)
# 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')
# # 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')
# 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
# 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)]
# 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))
# # 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': []}}
# 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
@ -102,11 +115,15 @@ class EsterdadWizard(models.Model):
context = dict(self.env.context or {})
context['default_user_id'] = user.id
context['default_otp'] = user.otp_code
context['default_esterdad_id'] = rec.id
context['default_esterdad_id'] = rec.id
context['default_invoice_ids'] = rec.journal_entry_ids.ids
context['default_cancel_reason'] = rec.cancel_reason.name if not rec.another_reason else rec.reason
rec.sponsor_id.write({
'cancel_record_id': rec.id
})
context['default_payment_ids'] = [(6, 0, rec.payment_ids.ids)]
if rec.sponsor_id.sponsor_or_donor_type == 'unknown' and not rec.sponsor_id.sponsor_phone:
rec.sponsor_id.sponsor_phone = rec.mobile
# context['default_payment_ids'] = [(6, 0, rec.payment_ids.ids)]
view = self.env.ref('odex_takaful.view_otp_wizard_form')
return {
'name': _('OTP Confirmation'),
@ -121,15 +138,10 @@ class EsterdadWizard(models.Model):
else:
raise UserError(_("No user assigned to this sponsor"))
@api.depends('sponsor_id.donations_details_lines', 'sponsor_id.donations_details_lines_mechanism_ids')
@api.depends('payment_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)
)
rec.amount = sum(rec.payment_ids.mapped('amount'))
class OTPWizard(models.TransientModel):
@ -139,10 +151,12 @@ class OTPWizard(models.TransientModel):
otp_code = fields.Integer()
esterdad_id = fields.Many2one('esterdad.wizard')
user_id = fields.Many2one('res.users')
payment_ids = fields.Many2many(
'account.payment',
string="Payments"
)
invoice_ids = fields.Many2many('account.move')
# payment_ids = fields.Many2many(
# 'account.payment',
# string="Payments"
# )
cancel_reason = fields.Text()
def action_confirm_otp(self):
for rec in self:
@ -150,21 +164,21 @@ class OTPWizard(models.TransientModel):
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]
# # 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"))
# 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:
# # Search for invoices in account.move matching payment refs
# invoices = self.env['account.move'].search([
# ('name', 'in', payment_refs),
# ])
if not rec.invoice_ids:
raise UserError(_("No invoices matching the specified references"))
for invoice in invoices:
for invoice in rec.invoice_ids:
credit = self.env['account.move.reversal'].with_company(self.env.user.company_id.id).create({
'refund_method': 'refund',
'date': date.today(),
@ -181,7 +195,11 @@ class OTPWizard(models.TransientModel):
})
credit_note = self.env['account.move'].search([('id', '=', last_id)])
credit_note.action_post()
credit_note.esterdad_id = rec.esterdad_id
credit_note.write({
'esterdad_id': rec.esterdad_id.id,
'sponsorship_cancel_reason': rec.cancel_reason
})
rec.esterdad_id.confirmed = True
rec.esterdad_id.sponsor_id.is_canceled_refund = True

View File

@ -21,6 +21,17 @@
<field name="arch" type="xml">
<form string="Esterdad Wizard" create="0" edit="0">
<sheet>
<field name="allowed_journal_entry_ids" invisible="1" />
<h2>Invoices</h2>
<field name="journal_entry_ids" nolabel="1" widget="many2many" colspan="2" options="{'no_open': True}">
<tree create="0" edit="0">
<field name="state" invisible="1" />
<field name="invoice_date"/>
<field name="name"/>
<field name="payment_reference"/>
<field name="amount_total"/>
</tree>
</field>
<group>
<field name="amount"/>
<field name="confirmed" invisible="1"/>
@ -33,7 +44,7 @@
<field name="reason" attrs="{'invisible': [('another_reason', '=', False)],
'required': [('another_reason', '=', True)]}"/>
<field name="sponsor_id" invisible="1"/>
<field name="allowed_payment_ids" widget="many2many_tags" invisible="1"/>
<!-- <field name="allowed_payment_ids" widget="many2many_tags" invisible="1"/> -->
</group>
<notebook>
<page string="بيانات المتبرع">
@ -45,7 +56,7 @@
</page>
<page string="Payment Details" groups="odex_takaful.group_refund_approval">
<field name="payment_ids">
<tree editable="1" >
<tree create="0" delete="0" editable="bottom">
<field name="date" readonly="1"/>
<field name="name" readonly="1"/>
@ -55,13 +66,13 @@
readonly="1"/>
<field name="state" readonly="1"/>
<field name="currency_id" groups="base.group_multi_currency"/>
<field name="confirm_payment_refund"
<field name="confirm_payment_refund" widget="binary"
attrs="{'readonly': [('payment_method_line_id_type', '=', 'cash')]}"/>
<field name="IBN_number"
attrs="{'readonly': [('payment_method_line_id_type', '=', 'cash')]}"/>
<field name="confirm_payment_refund_image"
<field name="confirm_payment_refund_image" widget="binary"
attrs="{'readonly': [('payment_method_line_id_type', '=', 'cash')]}"/>
<field name="payment_method_line_id_type"/>