from odoo import models, fields, api
|
|
from odoo.exceptions import UserError, ValidationError
|
|
from psycopg2 import sql, DatabaseError
|
|
from datetime import datetime
|
|
|
|
from werkzeug import utils
|
|
import base64
|
|
|
|
|
|
class DonationDonation(models.Model):
|
|
_inherit = 'donation.donation'
|
|
|
|
operation_id = fields.Many2one(
|
|
'opendons.operation',
|
|
string='Operation',
|
|
track_visibility='onchange',
|
|
ondelete='restrict',
|
|
domain=[('state', '=', 'exported')]
|
|
|
|
)
|
|
# operation_state= fields.Char(related='operation_id.state')
|
|
|
|
segment_id = fields.Many2one(
|
|
'opendons.segment',
|
|
string='Segment',
|
|
track_visibility='onchange',
|
|
ondelete='restrict'
|
|
|
|
)
|
|
|
|
end_date = fields.Date(
|
|
string='End Date',
|
|
index=True,
|
|
readonly=True,
|
|
track_visibility='onchange'
|
|
)
|
|
suspended_date = fields.Date(
|
|
string='Suspended Date',
|
|
index=True,
|
|
readonly=True,
|
|
track_visibility='onchange'
|
|
)
|
|
lastexecution_date = fields.Date(
|
|
string='Last execution Date',
|
|
index=True,
|
|
readonly=True,
|
|
track_visibility='onchange'
|
|
)
|
|
frequency =fields.Selection(
|
|
[('monthly','Monthly'), ('bimonthly','Bimonthly'),('quarterly','Quarterly'),('half-yearly','Half-yearly'),('annually','Annually')],
|
|
default='monthly'
|
|
)
|
|
|
|
|
|
|
|
@api.onchange('operation_id')
|
|
def _onchange_operation_id(self):
|
|
res = {}
|
|
res['domain']={'segment_id':[('operation_id', '=', self.operation_id.id)]}
|
|
return res
|
|
|
|
# @api.onchange('start_date')
|
|
# def _onchange_start_date(self):
|
|
# if self.start_date:
|
|
# self.donation_date=fields.Date.context_today(self)
|
|
# return True
|
|
|
|
# payment_batch_id = fields.Many2one(
|
|
# 'opendons_payment_batch',
|
|
# string='Payment Batch',
|
|
# ondelete='set null'
|
|
# )
|
|
|
|
# def _compute_rum(self):
|
|
# for rec in self:
|
|
# rec.rum_direct_debit=''
|
|
#if rec.recurring_template: rec.rum_direct_debit='GHGHH65767688899976'
|
|
#rec.rum_direct_debit='GHGHH65767688899976'
|
|
|
|
class DonationRecurringGenerate(models.TransientModel):
|
|
_inherit = 'donation.recurring.generate'
|
|
|
|
|
|
def generate(self):
|
|
self.ensure_one()
|
|
doo = self.env["donation.donation"]
|
|
donations = doo.search(
|
|
[
|
|
("recurring_template", "=", "active"),
|
|
("company_id", "=", self.company_id.id),
|
|
]
|
|
)
|
|
new_donation_ids = []
|
|
|
|
|
|
|
|
for donation in donations:
|
|
generate=True
|
|
existing_recur_donations = doo.search([("source_recurring_id", "=",int(donation.id))])
|
|
|
|
if donation.donation_date>fields.Date.context_today(self):
|
|
generate=False
|
|
|
|
delta=0
|
|
if donation.frequency=='annually':delta=365
|
|
if donation.frequency=='half-yearly':delta=365/2
|
|
if donation.frequency=='quarterly':delta=365/4
|
|
if donation.frequency=='bimonthly':delta=365/6
|
|
if donation.frequency=='monthly':delta=365/12
|
|
|
|
if existing_recur_donations:
|
|
for d in existing_recur_donations:
|
|
days_diff=(d.donation_date-fields.Date.context_today(self)).days
|
|
if days_diff<=delta:
|
|
generate=False
|
|
break
|
|
|
|
|
|
if generate==True:
|
|
default = {
|
|
"recurring_template":'',
|
|
"donation_date": fields.Date.context_today(self),
|
|
"source_recurring_id": donation.id,
|
|
"payment_ref": '',
|
|
"payment_mode_id":donation.payment_mode_id.id,
|
|
"mandate_required":'True',
|
|
"mandate_id":donation.mandate_id.id,
|
|
}
|
|
|
|
new_donation = donation.copy(default=default)
|
|
new_donation_ids.append(new_donation.id)
|
|
|
|
|
|
action = self.env.ref("donation.donation_action").sudo().read([])[0]
|
|
action.update(
|
|
{
|
|
"domain": [("id", "in", new_donation_ids)],
|
|
"limit": 500,
|
|
}
|
|
)
|
|
|
|
return action
|
|
|