@ -12,6 +12,7 @@ code_journal='IN'
date_compta = fields . datetime . now ( )
total_debit = total_credit = 0
no_mvt = 1
debug = True
class AccountingFileWizard ( models . TransientModel ) :
_name = ' accounting.file.wizard '
@ -58,24 +59,25 @@ class AccountingFileWizard(models.TransientModel):
total_credit = 0
#debug
donation = self . env [ ' donation.donation ' ] . search ( [ ] )
for d in donation :
d . date_compta = False
membership = self . env [ ' kalachakra.membership ' ] . search ( [ ] )
for m in membership :
m . date_compta = False
reg = self . env [ ' event.registration ' ] . search ( [ ] )
for r in reg :
r . date_compta = False
r . date_compta_down_payment = False
invoices = self . env [ ' account.move ' ] . search ( [ ] )
for i in invoices :
i . date_compta_out_refund = False
i . date_compta = False
# if debug:
# donation=self.env['donation.donation'].search([])
# for d in donation:
# d.date_compta=False
# membership=self.env['kalachakra.membership'].search([])
# for m in membership:
# m.date_compta=False
# reg=self.env['event.registration'].search([])
# for r in reg:
# r.date_compta=False
# r.date_compta_down_payment=False
# invoices=self.env['account.move'].search([])
# for i in invoices:
# i.date_compta_out_refund=False
# i.date_compta=False
# return True
tmstp = secrets . token_hex ( 16 )
filename = ' /tmp/accounting_file_ ' + tmstp + ' .txt '
filename = ' /tmp/accounting_file_ ' + tmstp + ' .csv '
f = open ( filename , " a " )
@ -97,7 +99,7 @@ class AccountingFileWizard(models.TransientModel):
self . membership_lines ( f )
self . event_lines ( f )
self . booking_event_lines ( f )
#self.out_refund_lines(f )
self . out_refund_lines ( f )
fic_line = " TOTAL_DEBIT= " + str ( total_debit ) + " TOTAL CREDIT= " + str ( total_credit )
f . write ( fic_line )
@ -107,8 +109,9 @@ class AccountingFileWizard(models.TransientModel):
#raise UserError('gg'+f.read())
vals = { }
vals [ ' accounting_file ' ] = base64 . encodebytes ( data )
vals [ ' document_fname ' ] = ' export_comptable.txt '
vals [ ' document_fname ' ] = ' export_comptable.csv '
vals [ ' exported_date ' ] = fields . Date . context_today ( self )
vals [ ' start_date ' ] = self . start_date
res = self . env [ ' accounting.file ' ] . create ( vals )
f . close
os . unlink ( filename )
@ -165,11 +168,18 @@ class AccountingFileWizard(models.TransientModel):
donation = self . env [ ' donation.donation ' ] . search ( [ ( ' date_compta ' , ' = ' , False ) , ( ' state ' , ' = ' , ' done ' ) ] )
#raise UserError(donation)
for d in donation :
if datetime ( d . donation_date . year , d . donation_date . month , d . donation_date . day ) < self . start_date : continue
date_payment = d . donation_date
#recherche de la date de paiement du don
trans = self . env [ ' payment.transaction ' ] . search ( [ ( ' donation_ids ' , ' in ' , d . id ) , ( ' state ' , ' = ' , ' done ' ) ] , limit = 1 )
if trans : date_payment = trans . date . date ( )
#if datetime(d.donation_date.year, d.donation_date.month, d.donation_date.day)<self.start_date:continue
if date_payment < self . start_date . date ( ) : continue
date_payment = self . _date_format ( date_payment )
#on ne prend pas en compte les dons liés à une retraite
#car traité avec les retraites
if d . invoice_id : continue
d . date_compta = date_compta
if not debug : d . date_compta = date_compta
#if not d.payment_mode_id: raise UserError(_("no payment mode found for donation %r ",donation.number))
#if not d.payment_mode_id.fixed_journal_id: raise UserError(_("no account found for payment mode %r",d.payment_mode_id.name))
account_debit_number = self . _file_format ( str ( d . payment_mode_id . fixed_journal_id . default_account_id . code ) , 6 )
@ -187,6 +197,7 @@ class AccountingFileWizard(models.TransientModel):
#lib1=lib1[11]
date_line = self . _date_format ( d . donation_date )
if trans : date_line = self . _date_format ( str ( trans . date . year ) + ' - ' + str ( trans . date . month ) + ' - ' + str ( trans . date . day ) )
#recherche de la transaction
lib_piece = self . _file_format ( d . number , 20 )
pt = self . env [ ' payment.transaction ' ] . search ( [ ( ' donation_ids ' , ' in ' , d . id ) ] , limit = 1 )
@ -224,8 +235,9 @@ class AccountingFileWizard(models.TransientModel):
#écritures pour les ADHESIONS réglées non déjà exportés
membership = self . env [ ' kalachakra.membership ' ] . search ( [ ( ' date_compta ' , ' = ' , False ) , ( ' state ' , ' = ' , ' done ' ) ] )
for m in membership :
m . date_compta = date_compta
if datetime ( m . start_date . year , m . start_date . month , m . start_date . day ) < self . start_date : continue
if not debug : m . date_compta = date_compta
#on ne prend pas en compte les adhésions liés à une retraite
#car traité avec les retraites
if m . balance_invoice_id : continue
@ -294,11 +306,15 @@ class AccountingFileWizard(models.TransientModel):
registration = self . env [ ' event.registration ' ] . search ( [ ( ' date_compta ' , ' = ' , False ) , ( ' payment_status ' , ' = ' , ' paid ' ) , ( ' event_id.booking_event ' , ' = ' , False ) , ( ' event_id.individual_booking_event ' , ' = ' , False ) ] )
if registration :
for reg in registration :
if reg . date_payment and reg . date_payment < self . start_date . date ( ) : continue
membership_credit_line = False
membership_amount = 0
reg . date_compta = date_compta
if not debug : reg . date_compta = date_compta
#if not reg.invoice_id.payment_mode_id.fixed_journal_id: raise UserError(_("no account found for payment mode %r",reg.invoice_id.payment_mode_id))
account_debit_number = self . _file_format ( str ( reg . invoice_id . payment_mode_id . fixed_journal_id . default_account_id . code ) , 6 )
if not reg . invoice_id . payment_mode_id :
@ -317,8 +333,8 @@ class AccountingFileWizard(models.TransientModel):
lib1 = self . _file_format ( lib1 , 12 )
#date du paiement
if self . _payment_date_date ( reg . invoice_id ) : continue
date_line = self . _payment_date ( reg . invoice_id )
#if self._payment_date_date(reg.invoice_id): continue
date_line = self . _date_format ( reg . date_payment )
if reg . invoice_id . amount_total == 0 : continue
if reg . invoice_id . payment_state != ' paid ' : continue
@ -389,8 +405,10 @@ class AccountingFileWizard(models.TransientModel):
#si pas de facture d'acompte et facture payée:
if not reg . down_payment_invoice_id and reg . invoice_id . payment_state == ' paid ' and reg . date_compta == False :
_logger . error ( ' CC1 ' )
reg . date_compta = date_compta
if reg . date_payment and reg . date_payment < self . start_date . date ( ) : continue
if not debug : reg . date_compta = date_compta
case_line = 1
down_payment = False
balance_payment = False
@ -419,17 +437,19 @@ class AccountingFileWizard(models.TransientModel):
donation_credit_line = True
for d in donations :
donation_amount + = d . amount_total
if self . _payment_date_date ( reg . invoice_id ) : continue
date_line = self . _payment_date ( reg . invoice_id )
date_line = self . _date_format ( reg . date_payment )
trans = self . env [ ' payment.transaction ' ] . search ( [ ( ' state ' , ' = ' , ' done ' ) , ( ' invoice_ids ' , ' in ' , reg . invoice_id . id ) ] , limit = 1 )
#si facture d'acompte payée et solde payée et pas encore exportée :
elif reg . down_payment_invoice_id . payment_state == ' paid ' and reg . balance_invoice_id . payment_state == ' paid ' and not reg . date_compta_down_payment and not reg . date_compta :
_logger . error ( ' CC2 ' + ' date_compta= ' + str ( reg . date_compta ) + ' date_compta_down_payment= ' + str ( reg . date_compta_down_payment ) )
if reg . date_payment_balance and reg . date_payment_balance < self . start_date . date ( ) : continue
case_line = 2
reg . date_compta = date_compta
if not debug : reg . date_compta = date_compta
down_payment = True
balance_payment = True
membership_amount = 0
@ -441,11 +461,12 @@ class AccountingFileWizard(models.TransientModel):
trans = self . env [ ' payment.transaction ' ] . search ( [ ( ' invoice_ids ' , ' in ' , reg . down_payment_invoice_id . id ) , ( ' state ' , ' = ' , ' done ' ) ] , limit = 1 )
if trans and trans . acquirer_id . name == ' Paypal ' : account_debit_number = self . _file_format ( str ( self . env [ ' account.payment.mode ' ] . search ( [ ( ' name ' , ' = ' , ' Paypal ' ) ] ) . fixed_journal_id . default_account_id . code ) , 6 )
if trans and trans . acquirer_id . name == ' Paiement par carte bancaire ' : account_debit_number = self . _file_format ( str ( self . env [ ' account.payment.mode ' ] . search ( [ ( ' name ' , ' = ' , ' CB ' ) ] ) . fixed_journal_id . default_account_id . code ) , 6 )
amount = reg . down_payment_invoice_id . amount_total + reg . balance_invoice_id . amount_total
if self . _payment_date_date ( reg . balance_invoice_id ) : continue
date_line = self . _payment_date ( reg . balance_invoice_id )
date_line = self . _date_format ( reg . date_payment_balance )
#recherche de l'adhésion
@ -468,8 +489,9 @@ class AccountingFileWizard(models.TransientModel):
#si facture d'acompte payée pas encore exporté et solde non payée :
elif reg . down_payment_invoice_id . payment_state == ' paid ' and not reg . date_compta_down_payment and reg . balance_invoice_id . payment_state != ' paid ' :
_logger . error ( ' CC3 ' )
if reg . date_payment_down_payment and reg . date_payment_down_payment < self . start_date . date ( ) : continue
case_line = 3
reg . date_compta_down_payment = date_compta
if not debug : reg . date_compta_down_payment = date_compta
down_payment = True
balance_payment = False
donation_credit_line = False
@ -478,25 +500,26 @@ class AccountingFileWizard(models.TransientModel):
account_debit_number = self . _file_format ( str ( reg . down_payment_invoice_id . payment_mode_id . fixed_journal_id . default_account_id . code ) , 6 )
trans = self . env [ ' payment.transaction ' ] . search ( [ ( ' invoice_ids ' , ' in ' , reg . down_payment_invoice_id . id ) , ( ' state ' , ' = ' , ' done ' ) ] , limit = 1 )
if trans and trans . acquirer_id . name == ' Paypal ' : account_debit_number = self . _file_format ( str ( self . env [ ' account.payment.mode ' ] . search ( [ ( ' name ' , ' = ' , ' Paypal ' ) ] ) . fixed_journal_id . default_account_id . code ) , 6 )
if trans and trans . acquirer_id . name == ' Paiement par carte bancaire ' : account_debit_number = self . _file_format ( str ( self . env [ ' account.payment.mode ' ] . search ( [ ( ' name ' , ' = ' , ' CB ' ) ] ) . fixed_journal_id . default_account_id . code ) , 6 )
amount = reg . down_payment_invoice_id . amount_total
if self . _payment_date_date ( reg . down_payment_invoice_id ) : continue
date_line = self . _payment_date ( reg . down_payment_invoice_id )
date_line = self . _date_format ( reg . date_payment_down_payment )
trans = self . env [ ' payment.transaction ' ] . search ( [ ( ' state ' , ' = ' , ' done ' ) , ( ' invoice_ids ' , ' in ' , reg . down_payment_invoice_id . id ) ] , limit = 1 )
#si facture d'acompte payée et exporté et solde payée pas exporté :
elif reg . down_payment_invoice_id . payment_state == ' paid ' and reg . balance_invoice_id . payment_state == ' paid ' and reg . date_compta_down_payment and not reg . date_compta :
_logger . error ( ' CC4 ' )
if reg . date_payment_balance and reg . date_payment_balance < self . start_date . date ( ) : continue
case_line = 4
reg . date_compta = date_compta
if not debug : reg . date_compta = date_compta
down_payment = False
balance_payment = True
#if not reg.balance_invoice_id.payment_mode_id.fixed_journal_id: raise UserError(_("no account found payment mode %r",reg.balance_invoice_id.payment_mode_id.name))
account_debit_number = self . _file_format ( str ( reg . down_payment_invoice_id . payment_mode_id . fixed_journal_id . default_account_id . code ) , 6 )
trans = self . env [ ' payment.transaction ' ] . search ( [ ( ' invoice_ids ' , ' in ' , reg . down_payment_invoice_id . id ) , ( ' state ' , ' = ' , ' done ' ) ] , limit = 1 )
if trans and trans . acquirer_id . name == ' Paypal ' : account_debit_number = self . _file_format ( str ( self . env [ ' account.payment.mode ' ] . search ( [ ( ' name ' , ' = ' , ' Paypal ' ) ] ) . fixed_journal_id . default_account_id . code ) , 6 )
if self . _payment_date_date ( reg . balance_invoice_id ) : continue
if trans and trans . acquirer_id . name == ' Paiement par carte bancaire ' : account_debit_number = self . _file_format ( str ( self . env [ ' account.payment.mode ' ] . search ( [ ( ' name ' , ' = ' , ' CB ' ) ] ) . fixed_journal_id . default_account_id . code ) , 6 )
date_line = self . _payment_date ( reg . balance_invoice_id )
@ -610,9 +633,10 @@ class AccountingFileWizard(models.TransientModel):
#écritures pour la facture complémentaire
if reg . end_of_stay_invoice_id and reg . end_of_stay_invoice_id . payment_state == ' paid ' :
if self . _payment_date_date ( reg . end_of_stay_invoice_id ) : continue
if reg . date_payment_end_of_stay and reg . date_payment_end_of_stay < self . start_date . date ( ) : continue
date_line = self . _payment_date ( reg . end_of_stay_invoice_id )
#écriture de débit
if not debug : reg . date_compta = date_compta
amount = reg . end_of_stay_invoice_id . amount_total
if amount == 0 : continue
debit = str ( amount )
@ -640,9 +664,9 @@ class AccountingFileWizard(models.TransientModel):
global total_credit
global no_mvt
#écritures pour les avoirs pas encore utilisés ou payés mais jamais passé en compta
out_refund_invoices = self . env [ ' account.move ' ] . search ( [ ( ' move_type ' , ' = ' , ' out_refund ' ) , ( ' date_compta ' , ' = ' , False ) , ( ' date_compta_out_refund ' , ' = ' , False ) ] )
out_refund_invoices = self . env [ ' account.move ' ] . search ( [ ( ' move_type ' , ' = ' , ' out_refund ' ) , ( ' state ' , ' = ' , ' posted ' ) , ( ' payment_state ' , ' = ' , ' not_paid ' ) , ( ' date_compta' , ' = ' , False ) , ( ' date_compta_out_refund ' , ' = ' , False ) ] )
for invoice in out_refund_invoices :
invoice . date_compta_out_refund = date_compta
if not debug : invoice . date_compta_out_refund = date_compta
firstname = invoice . partner_id . firstname
name = invoice . partner_id . name
if not name : name = ' N/A '
@ -684,10 +708,13 @@ class AccountingFileWizard(models.TransientModel):
#écritures pour les avoirs qui ont été utilisés
#écriture Débit 411000 – Crédit 170000
out_refund_invoices = self . env [ ' account.move ' ] . search ( [ ( ' move_type ' , ' = ' , ' out_refund ' ) , ( ' payment_state ' , ' = ' , ' paid ' ) , ( ' date_compta ' , ' = ' , False ) ] )
for invoice in out_refund_invoices :
invoice . date_compta = date_compta
_logger . error ( invoice . id )
if not debug : invoice . date_compta = date_compta
firstname = invoice . partner_id . firstname
name = invoice . partner_id . name
_logger . error ( name )
if not name : name = ' N/A '
if not firstname : firstname = ' '
lib1 = firstname [ 0 ] + ' ' + name
@ -706,9 +733,11 @@ class AccountingFileWizard(models.TransientModel):
#écriture de débit
amount = invoice . amount_total
account_debit_number = ' 411000 '
#date du paiement
if self . _payment_date_date ( invoice . out_invoice_id ) : continue
date_line = self . _payment_date ( invoice . out_invoice_id )
_logger . error ( self . _payment_date_ok ( invoice . out_invoice_id ) )
if not self . _payment_date_ok ( invoice ) : continue
date_line = self . _payment_date ( invoice )
if amount == 0 : continue
@ -751,16 +780,19 @@ class AccountingFileWizard(models.TransientModel):
return True
else : return True
def _payment_date_date ( self , move_id ) :
if move_id . invoice_payments_widget :
payment_info = json . loads ( move_id . invoice_payments_widget )
def _payment_date_ok ( self , move ) :
if move . invoice_payments_widget :
payment_info = json . loads ( move . invoice_payments_widget )
if payment_info :
if datetime . strptime ( payment_info [ ' content ' ] [ 0 ] [ ' date ' ] , ' % Y- % m- %d ' ) > self . start_date :
return False
_logger . error ( payment_info [ ' content ' ] [ 0 ] [ ' date ' ] )
if datetime . strptime ( payment_info [ ' content ' ] [ 0 ] [ ' date ' ] , ' % Y- % m- %d ' ) > = self . start_date :
return True
else :
return True
else : return True
return False
else :
return False
def _payment_date ( self , move_id ) :
if move_id . invoice_payments_widget :
@ -776,6 +808,8 @@ class AccountingFileWizard(models.TransientModel):
#_logger.error('date='+str(data))
if data :
r = str ( data ) . split ( ' - ' )
if len ( r [ 2 ] ) == 1 : r [ 2 ] = ' 0 ' + r [ 2 ]
if len ( r [ 1 ] ) == 1 : r [ 1 ] = ' 0 ' + r [ 1 ]
return r [ 2 ] + ' / ' + r [ 1 ] + ' / ' + r [ 0 ]
else :