@ -6,6 +6,11 @@ from datetime import date,datetime
from werkzeug import utils
from werkzeug import utils
import base64
import base64
import logging
import logging
import csv
import base64
import io
import json
import re
_logger = logging . getLogger ( __name__ )
_logger = logging . getLogger ( __name__ )
# class PaymentTransaction(models.Model):
# class PaymentTransaction(models.Model):
@ -40,6 +45,8 @@ class DonationDonation(models.Model):
transaction_id = fields . Many2one ( ' payment.transaction ' , ' payment transaction ' )
transaction_id = fields . Many2one ( ' payment.transaction ' , ' payment transaction ' )
state_done = fields . Boolean ( compute = ' _compute_donation_state ' , store = True )
state_done = fields . Boolean ( compute = ' _compute_donation_state ' , store = True )
affectation = fields . Char ( compute = ' _compute_affectation ' )
affectation = fields . Char ( compute = ' _compute_affectation ' )
source_import = fields . Char ( ' source import ' )
date_import = fields . Date ( ' date import ' , default = False )
# @api.depends('transaction_id.state')
# @api.depends('transaction_id.state')
# def transaction_state(self):
# def transaction_state(self):
# _logger.error("onchange_transaction_state")
# _logger.error("onchange_transaction_state")
@ -201,4 +208,105 @@ class DonationDonation(models.Model):
return transaction
return transaction
# def _generate_invoice_donation(self, vals):
def _import_donation ( self ) :
files = self . env [ " opendons.partnerdraftfile " ] . search ( [ ( ' source_name ' , ' = ' , ' donation ' ) ] )
for f in files :
decrypted = base64 . b64decode ( f . file ) . decode ( ' utf-8-sig ' )
source_name = f . source_name
with io . StringIO ( decrypted ) as fp :
reader = csv . DictReader ( fp , delimiter = " , " , quotechar = ' " ' )
for row in reader :
vals = { }
lig_vals = { }
vals_p = { }
vals_part = { }
for key , value in row . items ( ) :
#on cherche le type de don
if key == ' Destination don ' :
#dons dans la base:
dbdonpdt = [ ' Activités en ligne ' , ' Aide à la sangha ordonnée ' , ' Autre ' , ' Don selon les besoins du centre ' , ' Rituels et prières ' , ' Soutien aux lamas ' , ' Travaux au centre de retraite ' ]
#mapping avec les valeur du fichier
if value == ' Soutien Lamas ' :
value = ' Soutien aux lamas '
if value == ' Soutien Lamas ' :
value = ' Soutien aux lamas '
donation_product = self . env [ ' product.product ' ] . sudo ( ) . search ( [ ( ' name ' , ' = ' , value ) ] , limit = 1 )
if not donation_product :
vals_p = { }
vals_p [ ' name ' ] = value
vals_p [ ' donation ' ] = True
vals_p [ ' tax_receipt_ok ' ] = True
vals_p [ ' type ' ] = ' service '
vals_p [ ' taxes_id ' ] = False
res = self . env [ ' product.product ' ] . create ( vals_p )
lig_vals [ ' product_id ' ] = res . id
lig_vals [ ' display_name ' ] = res . display_name
lig_vals [ ' tax_receipt_ok ' ] = res . tax_receipt_ok
else :
lig_vals [ ' product_id ' ] = donation_product . id
lig_vals [ ' display_name ' ] = donation_product . display_name
lig_vals [ ' tax_receipt_ok ' ] = donation_product . tax_receipt_ok
if key == ' Email ' :
vals_part [ ' email ' ] = value . strip ( )
if key == ' Nom ' : vals_part [ ' name ' ] = value . strip ( )
if key == ' Prénom ' : vals_part [ ' firstname ' ] = value . strip ( )
if key == ' Date ' :
year_m = value [ 0 : 4 ]
month_m = value [ 5 : 7 ]
day_m = value [ 8 : 10 ]
vals [ ' donation_date ' ] = datetime . strptime ( year_m + ' - ' + month_m + ' - ' + day_m , ' % Y- % m- %d ' )
if key == ' Montant ' :
lig_vals [ ' unit_price ' ] = value
#création du contact si pas trouvé dans la base
partner = self . env [ ' res.partner ' ] . sudo ( ) . search ( [ ( ' email ' , ' = ' , vals_part [ ' email ' ] ) ] , limit = 1 )
if not partner :
vals_part [ ' origine ' ] = ' import_don2022 '
vals_part [ ' tax_receipt_option ' ] = ' annual '
_logger . error ( vals_part )
new_partner = self . env [ ' res.partner ' ] . create ( vals_part )
vals [ ' partner_id ' ] = new_partner . id
else :
vals [ ' partner_id ' ] = partner . id
#creation du don
vals [ ' state ' ] = ' draft '
vals [ ' payment_ref ' ] = ' import '
vals [ ' tax_receipt_option ' ] = ' annual '
vals [ ' source_import ' ] = ' import dons 2022 '
vals [ ' date_import ' ] = fields . datetime . now ( )
donation_draft = self . env [ ' donation.donation ' ] . sudo ( ) . create ( vals )
#create line donation
lig_vals [ ' donation_id ' ] = donation_draft . id
lig_vals [ ' quantity ' ] = 1
donation_line = self . env [ ' donation.line ' ] . sudo ( ) . create ( lig_vals )
return True
def _cancel_donation ( self ) :
#annulation des dons créés aujourd'hui
today = date . today ( )
donation = self . env [ ' donation.donation ' ] . search ( [ ( ' create_date ' , ' >= ' , today ) ] )
for d in donation :
d . state = ' cancel '