from odoo import models, fields, api
|
|
from odoo.exceptions import UserError, ValidationError, Warning
|
|
from psycopg2 import sql, DatabaseError
|
|
|
|
from werkzeug import utils
|
|
import re
|
|
from string import ascii_uppercase
|
|
|
|
class partner(models.Model):
|
|
_inherit = 'res.partner'
|
|
|
|
type = fields.Selection(
|
|
[('contact', 'Contact'),
|
|
('invoice', 'Invoice Address'),
|
|
('delivery', 'Delivery Address'),
|
|
('other', 'Other Address'),
|
|
("private", "Private Address"),
|
|
], string='Address Type',
|
|
default='private',
|
|
help="Invoice & Delivery addresses are used in sales orders. Private addresses are only visible by authorized users.")
|
|
|
|
locality=fields.Char('Locality')
|
|
tax_street=fields.Char('Street')
|
|
tax_street2=fields.Char('Street2')
|
|
tax_locality=fields.Char('Locality')
|
|
tax_city=fields.Char('City')
|
|
tax_zip=fields.Char('Zip ')
|
|
tax_country_id=fields.Many2one('res.country')
|
|
|
|
def _compute_address_count(self):
|
|
# The current user may not have access rights for donations
|
|
for partner in self:
|
|
try:
|
|
partner.address_count = len(partner.address_ids)
|
|
except Exception:
|
|
partner.address_count = 0
|
|
|
|
|
|
|
|
def _compute_relationshipPartner_count(self):
|
|
# The current user may not have access rights for donations
|
|
for partner in self:
|
|
try:
|
|
partner.relationshipPartner_count = len(partner.relationshipPartner_ids)
|
|
except Exception:
|
|
partner.relationshipPartner_count = 0
|
|
|
|
def _compute_recurring_donation_count(self):
|
|
# The current user may not have access rights for donations
|
|
for partner in self:
|
|
try:
|
|
partner.recurring_donation_count=self.env['donation.donation'].search_count(['&',('recurring_template','!=',''),('partner_id','=',int(partner.id))])
|
|
|
|
except Exception:
|
|
partner.recurring_donation_count = 0
|
|
|
|
qualifier_ids = fields.One2many(
|
|
'opendons_qualifier.partnerqualifier',
|
|
'partner_id',
|
|
string='Qualifier',
|
|
readonly=True
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
request_ids = fields.Many2many('opendons.request', 'request_partner_rel', 'partner_id', 'request_id', string='requests',ondelete='cascade')
|
|
operation_ids = fields.Many2many('opendons.operation', 'operation_partner_rel', 'partner_id', 'operation_id', string='operations',ondelete='cascade' )
|
|
operation_excl_ids = fields.Many2many('opendons.operation', 'operation_excl_partner_rel', 'partner_id', 'operation_id', string='excluded operations',ondelete='cascade')
|
|
segment_ids = fields.Many2many('opendons.segment', 'segment_partner_rel', 'partner_id', 'segment_id', string='segments',ondelete='cascade')
|
|
export_batch_ids=fields.Many2many('opendons.operation.batch_export', 'export_batch_partner_rel', 'partner_id', 'export_batch_id', string='export batchs',ondelete='cascade')
|
|
|
|
address_count = fields.Integer(
|
|
compute='_compute_address_count',
|
|
string="# of Addresses",
|
|
readonly=True
|
|
)
|
|
|
|
|
|
relationshipPartner_ids = fields.One2many(
|
|
'opendons_relationship.partner',
|
|
'partner_id',
|
|
string='Relationship',
|
|
readonly=True
|
|
)
|
|
relationshipPartner_count = fields.Integer(
|
|
compute='_compute_relationshipPartner_count',
|
|
string="# of Relationship",
|
|
readonly=True
|
|
)
|
|
|
|
|
|
|
|
recurring_donation_count = fields.Integer(
|
|
compute='_compute_recurring_donation_count',
|
|
string="# of recurring donation",
|
|
readonly=True
|
|
)
|
|
|
|
key=fields.Char(
|
|
string="Key",
|
|
store=True,
|
|
readonly=True
|
|
|
|
)
|
|
firstname=fields.Char(string="First name")
|
|
npai_count=fields.Integer(String="NPAI count", readonly=True)
|
|
source=fields.Char(String="source")
|
|
|
|
donor_id=fields.Char('Donor id',compute="_compute_donor_id",store=True)
|
|
|
|
aggregate_id=fields.Many2one('opendons.aggregate.partner','partner_id')
|
|
|
|
first_donation_product_id=fields.Many2one(related='aggregate_id.first_donation_product_id')
|
|
first_donation_date=fields.Date(related='aggregate_id.first_donation_date')
|
|
max_amount_donation=fields.Float(related='aggregate_id.max_amount_donation')
|
|
annual_average_number_donation=fields.Float(related='aggregate_id.annual_average_number_donation')
|
|
average_amount_donation=fields.Float(related='aggregate_id.average_amount_donation')
|
|
|
|
|
|
def _compute_donor_id(self):
|
|
for rec in self:
|
|
if rec.key:
|
|
rec.donor_id=str(rec.id)+rec.key
|
|
else :
|
|
rec.donor_id=""
|
|
|
|
|
|
|
|
def _alpha(self,id):
|
|
alpha={}
|
|
alpha[0]='A'
|
|
alpha[1]='B'
|
|
alpha[2]='C'
|
|
alpha[3]='D'
|
|
alpha[4]='E'
|
|
alpha[5]='F'
|
|
alpha[6]='G'
|
|
alpha[7]='H'
|
|
alpha[8]='I'
|
|
alpha[9]='J'
|
|
alpha[10]='K'
|
|
alpha[11]='L'
|
|
alpha[12]='M'
|
|
alpha[13]='N'
|
|
alpha[14]='P'
|
|
alpha[15]='Q'
|
|
alpha[16]='R'
|
|
alpha[17]='S'
|
|
alpha[18]='T'
|
|
alpha[19]='U'
|
|
alpha[20]='V'
|
|
alpha[21]='W'
|
|
alpha[22]='X'
|
|
alpha[23]='Y'
|
|
alpha[24]='Z'
|
|
return (alpha[id%25])
|
|
|
|
def _compute_key(self):
|
|
|
|
|
|
self.key==self._alpha(int(self.id))
|
|
|
|
|
|
@api.model
|
|
def create(self, values):
|
|
# Override the original create function for the res.partner model
|
|
record = super(partner, self).create(values)
|
|
|
|
# Change the values of a variable in this super function
|
|
record['key'] = self._alpha(int(record.id))
|
|
|
|
# Return the record so that the changes are applied and everything is stored.
|
|
return record
|
|
|
|
def _compute_all_key(self):
|
|
|
|
for rec in self.env['res.partner'].search([]):
|
|
rec.key=self._alpha(int(rec.id))
|
|
|
|
def _compute_all_donor_id(self):
|
|
|
|
for rec in self.env['res.partner'].search([]):
|
|
rec.donor_id=str(rec.id)+rec.key
|
|
|
|
def _update_source(self):
|
|
|
|
|
|
for rec in self.env['res.partner'].search([]):
|
|
rec.source='joomla'
|
|
|
|
|
|
def _remove_source(self):
|
|
|
|
self.env["res.partner"].search([('source','=','joomla')]).unlink()
|
|
|
|
|
|
|
|
@api.onchange('street')
|
|
def _onchange_street(self):
|
|
for rec in self:
|
|
rec.npai_count=0
|
|
return
|
|
|
|
@api.constrains('email')
|
|
def _onchange_email(self):
|
|
if self.email:
|
|
if not self.image_1920 and self._context.get('gravatar_image') and self.email:
|
|
self.image_1920 = self._get_gravatar_image(self.email)
|
|
regex = re.compile(r'([A-Za-z0-9]+[.-_])*[A-Za-z0-9]+@[A-Za-z0-9-]+(\.[A-Z|a-z]{2,})+')
|
|
if not re.fullmatch(regex, self.email):
|
|
|
|
raise ValidationError('Invalid email')
|
|
|
|
@api.constrains('phone')
|
|
def _onchange_phone(self):
|
|
if self.phone:
|
|
regex = re.compile(r'(^(?:(?:\+|00)33[\s.-]{0,3}(?:\(0\)[\s.-]{0,3})?|0)[1-9](?:(?:[\s.-]?\d{2}){4}|\d{2}(?:[\s.-]?\d{3}){2})$)+')
|
|
|
|
# 0123456789
|
|
# 01 23 45 67 89
|
|
# 01.23.45.67.89
|
|
# 0123 45.67.89
|
|
# 0033 123-456-789
|
|
# +33-1.23.45.67.89
|
|
# +33 - 123 456 789
|
|
# +33(0) 123 456 789
|
|
# +33 (0)123 45 67 89
|
|
# +33 (0)1 2345-6789
|
|
# +33(0) - 123456789
|
|
|
|
|
|
if not re.fullmatch(regex, self.phone):
|
|
|
|
raise ValidationError('Invalid phone')
|
|
|
|
@api.constrains('mobile')
|
|
def _onchange_mobile(self):
|
|
if self.mobile:
|
|
regex = re.compile(r'(^(?:(?:\+|00)33[\s.-]{0,3}(?:\(0\)[\s.-]{0,3})?|0)[1-9](?:(?:[\s.-]?\d{2}){4}|\d{2}(?:[\s.-]?\d{3}){2})$)+')
|
|
|
|
# 0123456789
|
|
# 01 23 45 67 89
|
|
# 01.23.45.67.89
|
|
# 0123 45.67.89
|
|
# 0033 123-456-789
|
|
# +33-1.23.45.67.89
|
|
# +33 - 123 456 789
|
|
# +33(0) 123 456 789
|
|
# +33 (0)123 45 67 89
|
|
# +33 (0)1 2345-6789
|
|
# +33(0) - 123456789
|
|
|
|
|
|
if not re.fullmatch(regex, self.mobile):
|
|
|
|
raise ValidationError('Invalid mobile')
|
|
|
|
@api.onchange('zip')
|
|
def _onchange_zip(self):
|
|
if self.zip:
|
|
if len(self.zip)!=5: raise ValidationError('invalid zip code')
|
|
if not self.zip.isnumeric(): raise ValidationError('invalid zip code')
|
|
return
|