from odoo import models, fields, api
|
|
from odoo.exceptions import UserError, ValidationError,Warning
|
|
from psycopg2 import sql, DatabaseError
|
|
|
|
from werkzeug import utils
|
|
|
|
class opendons_payment_batch(models.Model):
|
|
_name = 'opendons_payment_batch'
|
|
_description = 'manage payment batch'
|
|
|
|
company_id = fields.Many2one(
|
|
"res.company",
|
|
string="Company",
|
|
ondelete="cascade",
|
|
default=lambda self: self.env.company,
|
|
)
|
|
payment_mode_id = fields.Many2one(
|
|
"account.payment.mode",
|
|
string="Payment Mode",
|
|
domain="[('company_id', '=', company_id), ('donation', '=', True)]",
|
|
copy=False,
|
|
tracking=True
|
|
|
|
)
|
|
product_id = fields.Many2one(
|
|
'product.product',
|
|
'Product',
|
|
|
|
domain=[('donation', '=', True)],
|
|
ondelete='restrict'
|
|
|
|
)
|
|
|
|
|
|
|
|
partner_id=fields.Many2one(
|
|
'res.partner',
|
|
string='Partner',
|
|
index=True,
|
|
track_visibility='onchange',
|
|
ondelete='restrict'
|
|
)
|
|
|
|
operation_id=fields.Many2one(
|
|
'opendons.operation',
|
|
string='Operation',
|
|
index=True,
|
|
domain=[('state', '=', 'exported')],
|
|
track_visibility='onchange',
|
|
ondelete='restrict'
|
|
)
|
|
|
|
|
|
segment_id=fields.Many2one(
|
|
'opendons.segment',
|
|
string='Segment',
|
|
|
|
index=True,
|
|
track_visibility='onchange',
|
|
ondelete='restrict'
|
|
)
|
|
|
|
donation_ids = fields.One2many(
|
|
'donation.donation',
|
|
'payment_batch_id',
|
|
string='donation',
|
|
track_visibility='onchange')
|
|
|
|
@api.model
|
|
def _default_currency(self):
|
|
company = self.env['res.company']._company_default_get(
|
|
'donation.donation')
|
|
return company.currency_id
|
|
|
|
currency_id = fields.Many2one(
|
|
'res.currency',
|
|
string='Currency',
|
|
required=True,
|
|
states={'done': [('readonly', True)]},
|
|
track_visibility='onchange',
|
|
ondelete='restrict',
|
|
default=_default_currency
|
|
)
|
|
|
|
payment_count=fields.Integer(string='payments count', readonly=True)
|
|
payment_input=fields.Char('Input field')
|
|
input_mode=fields.Selection([('manual','manual'),('loyalty','loyalty QRCODE'),('prospect','prospect QRCODE')])
|
|
assignment=fields.Selection([('single','single'),('multiple','multiple')])
|
|
donation_amount = fields.Monetary(
|
|
'Donation amount',
|
|
currency_field='currency_id',
|
|
|
|
)
|
|
|
|
company_id = fields.Many2one(
|
|
"res.company",
|
|
string="Company",
|
|
required=True,
|
|
default=lambda self: self.env.company
|
|
|
|
)
|
|
@api.onchange('operation_id')
|
|
def _onchange_operation_id(self):
|
|
res = {}
|
|
res['domain']={'segment_id':[('operation_id', '=', self.operation_id.id)]}
|
|
return res
|
|
|
|
|
|
@api.onchange("payment_input")
|
|
def _onchange_payment_input(self):
|
|
|
|
if self.payment_input:
|
|
|
|
#si le QRCODE a déjà été utilisé, message d'avertissement
|
|
qrcode_exist=self.env['opendons.payment_batch_qrcode'].search([('qrcode','=',self.payment_input)])
|
|
if qrcode_exist : raise Warning('qrcode already used')
|
|
|
|
if self.input_mode=='loyalty':
|
|
|
|
inpt=self.payment_input.split(';')
|
|
if len(inpt)!=4:raise ValidationError('input string not valid')
|
|
partner_id=int(inpt[0])
|
|
key=inpt[1]
|
|
operation_id=int(inpt[2])
|
|
segment_id=int(inpt[3])
|
|
|
|
partner = self.env['res.partner'].sudo().search(['&',('id','=',int(partner_id)),('key','=',key)])
|
|
operation = self.env['res.partner'].sudo().search([('id','=',int(operation_id))])
|
|
segment = self.env['res.partner'].sudo().search([('id','=',int(segment_id))])
|
|
|
|
if not partner : ValidationError('partner not found')
|
|
if not operation : ValidationError('operation not found')
|
|
if not segment : ValidationError('segment not found')
|
|
|
|
|
|
self.partner_id=partner_id
|
|
self.operation_id=operation_id
|
|
self.segment_id=segment_id
|
|
res=self.action_add_payment()
|
|
|
|
if self.input_mode=='prospect':
|
|
inpt=self.payment_input.split(';')
|
|
if len(inpt)!=4:raise ValidationError('input string not valid')
|
|
partner_string=int(inpt[0])
|
|
key=inpt[1]
|
|
operation_id=int(inpt[2])
|
|
segment_id=int(inpt[3])
|
|
|
|
#partner = self.env['res.partner'].sudo().search(['&',('id','=',int(partner_id)),('key','=',key)])
|
|
operation = self.env['res.partner'].sudo().search([('id','=',int(operation_id))])
|
|
segment = self.env['res.partner'].sudo().search([('id','=',int(segment_id))])
|
|
|
|
#if not partner : ValidationError('partner not found')
|
|
if not operation : ValidationError('operation not found')
|
|
if not segment : ValidationError('segment not found')
|
|
|
|
#self.partner_id=partner_id
|
|
self.operation_id=operation_id
|
|
self.segment_id=segment_id
|
|
#res=self.action_ajouter_payment()
|
|
|
|
|
|
|
|
|
|
def action_add_payment(self):
|
|
|
|
|
|
|
|
vals={}
|
|
vals['payment_batch_id']=self.id
|
|
vals['payment_mode_id']=int(self.payment_mode_id)
|
|
vals['tax_receipt_option']='annual'
|
|
vals['partner_id']=self.partner_id.id
|
|
vals['operation_id']=self.operation_id.id
|
|
vals['segment_id']=self.segment_id.id
|
|
vals['donation_date']=fields.Date.context_today(self)
|
|
vals['check_total']=self.donation_amount
|
|
donation=self.env['donation.donation'].create(vals)
|
|
|
|
#creation de l'affectation du don si affectation simple
|
|
if self.assignment=='single':
|
|
vals={}
|
|
vals['donation_id']=donation.id
|
|
vals['currency_id']=self.currency_id
|
|
#vals['company_currency_id']=self.company_currency_id
|
|
vals['product_id']=self.product_id.id
|
|
vals['quantity']=1
|
|
vals['unit_price']=self.donation_amount
|
|
donation_line=self.env['donation.line'].create(vals)
|
|
|
|
donation.validate()
|
|
vals={}
|
|
|
|
vals['qrcode']=self.payment_input
|
|
self.env['opendons.payment_batch_qrcode'].create(vals)
|
|
# else:
|
|
# view_id = self.env.ref('donation.donation_form').id
|
|
# context = self._context.copy()
|
|
# return {
|
|
# 'name':'donation.form',
|
|
# 'view_type':'form',
|
|
# 'view_mode':'tree',
|
|
# 'views' : [(view_id,'form')],
|
|
# 'res_model':'donation.donation',
|
|
# 'view_id':view_id,
|
|
# 'type':'ir.actions.act_window',
|
|
# 'res_id':self.id,
|
|
# 'target':'new',
|
|
# 'context':context,
|
|
# }
|
|
|
|
|
|
|
|
return True
|
|
|
|
class opendons_payment_batch_qrcode(models.Model):
|
|
_name = 'opendons.payment_batch_qrcode'
|
|
_description = 'store qrcode to avoid duplicate entries'
|
|
|
|
qrcode=fields.Char('qrcode')
|