@ -12,14 +12,14 @@ 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 '
_description = ' accounting file wizard '
start_date = fields . Datetime ( ' start date ' , required = True , default = lambda self : self . _compute_start_date ( ) )
debug_mode = fields . Boolean ( ' debug mode ' , default = True )
def _compute_start_date ( self ) :
date_max = [ ]
#recherche de la date d'export d'écriture la plus récente
@ -100,6 +100,7 @@ class AccountingFileWizard(models.TransientModel):
self . event_lines ( f )
self . booking_event_lines ( f )
self . out_refund_lines ( f )
self . end_of_stay_lines ( f )
fic_line = " TOTAL_DEBIT= " + str ( total_debit ) + " TOTAL CREDIT= " + str ( total_credit )
f . write ( fic_line )
@ -128,38 +129,7 @@ class AccountingFileWizard(models.TransientModel):
}
#rajouter en fin de fichier un total DEBIT et un total CREDIT
f . close
#création de l'export en base
vals = { }
f = open ( filename , " r " )
data = str . encode ( f . read ( ) , ' utf-8 ' )
vals [ ' accounting_file ' ] = base64 . encodebytes ( data )
vals [ ' document_fname ' ] = ' export_comptable.csv '
vals [ ' exported_date ' ] = fields . Date . context_today ( self )
res = self . env [ ' accounting.file ' ] . create ( vals )
f . close
os . unlink ( filename )
#affichage de l'export
return {
' name ' : ' Account file form ' ,
' view_type ' : ' form ' ,
' view_mode ' : ' form ' ,
' res_model ' : ' accounting.file ' ,
' res_id ' : int ( res . id ) ,
' type ' : ' ir.actions.act_window ' ,
' target ' : ' current '
}
return True
def donation_lines ( self , f ) :
global total_debit
global total_credit
@ -179,7 +149,7 @@ class AccountingFileWizard(models.TransientModel):
#on ne prend pas en compte les dons liés à une retraite
#car traité avec les retraites
if d . invoice_id : continue
if not debug : d . date_compta = date_compta
if not self . debug_mode : 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 )
@ -237,7 +207,7 @@ class AccountingFileWizard(models.TransientModel):
for m in membership :
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
if not self . debug_mode : 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
@ -314,7 +284,7 @@ class AccountingFileWizard(models.TransientModel):
if reg . date_payment and reg . date_payment < self . start_date . date ( ) : continue
membership_credit_line = False
membership_amount = 0
if not debug : reg . date_compta = date_compta
if not self . debug_mode : 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 :
@ -376,12 +346,110 @@ class AccountingFileWizard(models.TransientModel):
no_mvt + = 1
total_debit + = amount
return no_mvt
def end_of_stay_lines ( self , f ) :
global total_debit
global total_credit
global no_mvt
registration = self . env [ ' event.registration ' ] . search ( [ ] )
for reg in registration :
if not reg . end_of_stay_invoice_id : continue
if reg . end_of_stay_invoice_id . date_compta_end_of_stay : continue
if not reg . date_payment_end_of_stay : continue
if reg . date_payment_end_of_stay and reg . date_payment_end_of_stay < self . start_date . date ( ) : continue
account_debit_number = self . _file_format ( str ( reg . end_of_stay_invoice_id . payment_mode_id . fixed_journal_id . default_account_id . code ) , 6 )
trans = self . env [ ' payment.transaction ' ] . search ( [ ( ' invoice_ids ' , ' in ' , reg . end_of_stay_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 )
account_credit_number = self . _file_format ( ' 170000 ' , 6 )
firstname = reg . partner_id . firstname
name = reg . partner_id . name
if not name : name = ' N/A '
if not firstname : firstname = ' '
lib1 = firstname [ 0 ] + ' ' + name
lib1 = self . _file_format ( lib1 , 12 )
lib2 = self . _file_format ( reg . event_id . booking_product_id . name , 13 )
lib = lib1 + ' ' + lib2
#recherche de l'adhésion
membership_credit_line = False
membership = self . env [ ' kalachakra.membership ' ] . search ( [ ( ' invoice_id ' , ' = ' , int ( reg . end_of_stay_invoice_id . id ) ) ] )
membership_amount = 0
if membership :
membership_credit_line = True
membership_amount = membership . amount
#recherche de dons
donation_credit_line = False
donation_amount = 0
donations = donation_booking = self . env [ ' donation.donation ' ] . search ( [ ( ' invoice_id ' , ' = ' , int ( reg . end_of_stay_invoice_id . id ) ) ] )
if donations :
donation_credit_line = True
for d in donations :
donation_amount + = d . amount_total
date_line = self . _payment_date ( reg . end_of_stay_invoice_id )
#écriture de débit
if not self . debug_mode : reg . date_compta_end_of_stay = date_compta
amount = reg . end_of_stay_invoice_id . amount_total
if amount == 0 : continue
debit = str ( amount )
credit = ' '
lib_piece = self . _file_format ( reg . end_of_stay_invoice_id . name , 20 )
fic_line = str ( no_mvt ) + ' ; ' + ' IN ' + ' ; ' + date_line + ' ; ' + account_debit_number + ' ; ' + lib + ' ; ' + debit + ' ; ' + credit + ' ; ' + lib_piece + ' \n '
f . write ( fic_line )
no_mvt + = 1
total_debit + = amount
#écriture de crédit
debit = ' '
credit = str ( amount - membership_amount - donation_amount )
fic_line = str ( no_mvt ) + ' ; ' + ' IN ' + ' ; ' + date_line + ' ; ' + account_credit_number + ' ; ' + lib + ' ; ' + debit + ' ; ' + credit + ' ; ' + lib_piece + ' \n '
f . write ( fic_line )
no_mvt + = 1
total_credit + = amount
if membership_credit_line :
debit = ' '
credit = str ( membership_amount )
fic_line = str ( no_mvt ) + ' ; ' + ' IN ' + ' ; ' + date_line + ' ; ' + self . _file_format ( ' 756100 ' , 6 ) + ' ; ' + lib + ' ; ' + debit + ' ; ' + credit + ' ; ' + lib_piece + ' \n '
f . write ( fic_line )
no_mvt = no_mvt + 1
total_credit + = membership_amount
if donation_credit_line :
for d in donations :
debit = ' '
amount = d . amount_total
credit = str ( amount )
fic_line = str ( no_mvt ) + ' ; ' + ' IN ' + ' ; ' + date_line + ' ; ' + account_credit_number + ' ; ' + lib + ' ; ' + debit + ' ; ' + credit + ' ; ' + lib_piece + ' \n '
f . write ( fic_line )
no_mvt = no_mvt + 1
total_credit + = amount
def booking_event_lines ( self , f ) :
global total_debit
global total_credit
global no_mvt
#écritures pour les inscription aux RETRAITES avec facture acomptes payés + factures soldes payés non déjà exporté
registration = self . env [ ' event.registration ' ] . search ( [ ] )
membership_amount = 0
donation_amount = 0
membership_credit_line = False
donation_credit_line = False
#if not reg.event_id.participation_product_id.property_account_income_id: raise UserError(_("no credit account found for product %r",reg.event_id.participation_product_id.name))
account_credit_number = self . _file_format ( ' 170000 ' , 6 )
for reg in registration :
if reg . event_id . booking_event :
@ -408,7 +476,7 @@ class AccountingFileWizard(models.TransientModel):
_logger . error ( ' CC1 ' )
if reg . date_payment and reg . date_payment < self . start_date . date ( ) : continue
if not debug : reg . date_compta = date_compta
if not self . debug_mode : reg . date_compta = date_compta
case_line = 1
down_payment = False
balance_payment = False
@ -442,14 +510,14 @@ class AccountingFileWizard(models.TransientModel):
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
if not debug : reg . date_compta = date_compta
if not self . debug_mode : reg . date_compta = date_compta
down_payment = True
balance_payment = True
membership_amount = 0
@ -491,7 +559,7 @@ class AccountingFileWizard(models.TransientModel):
_logger . error ( ' CC3 ' )
if reg . date_payment_down_payment and reg . date_payment_down_payment < self . start_date . date ( ) : continue
case_line = 3
if not debug : reg . date_compta_down_payment = date_compta
if not self . debug_mode : reg . date_compta_down_payment = date_compta
down_payment = True
balance_payment = False
donation_credit_line = False
@ -511,7 +579,7 @@ class AccountingFileWizard(models.TransientModel):
_logger . error ( ' CC4 ' )
if reg . date_payment_balance and reg . date_payment_balance < self . start_date . date ( ) : continue
case_line = 4
if not debug : reg . date_compta = date_compta
if not self . debug_mode : 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))
@ -540,6 +608,7 @@ class AccountingFileWizard(models.TransientModel):
trans = self . env [ ' payment.transaction ' ] . search ( [ ( ' state ' , ' = ' , ' done ' ) , ( ' invoice_ids ' , ' in ' , reg . balance_invoice_id . id ) ] , limit = 1 )
else : continue
@ -631,34 +700,16 @@ class AccountingFileWizard(models.TransientModel):
no_mvt = no_mvt + 1
total_credit + = amount
#écritures pour la facture complémentaire
if reg . end_of_stay_invoice_id and reg . end_of_stay_invoice_id . payment_state == ' paid ' :
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 )
credit = ' '
lib_piece = self . _file_format ( reg . end_of_stay_invoice_id . name , 20 )
fic_line = str ( no_mvt ) + ' ; ' + ' IN ' + ' ; ' + date_line + ' ; ' + account_debit_number + ' ; ' + lib + ' ; ' + debit + ' ; ' + credit + ' ; ' + lib_piece + ' \n '
f . write ( fic_line )
no_mvt + = 1
total_debit + = amount
#écriture de crédit
debit = ' '
credit = str ( amount )
fic_line = str ( no_mvt ) + ' ; ' + ' IN ' + ' ; ' + date_line + ' ; ' + account_credit_number + ' ; ' + lib + ' ; ' + debit + ' ; ' + credit + ' ; ' + lib_piece + ' \n '
f . write ( fic_line )
no_mvt + = 1
total_credit + = amount
_logger . error ( ' TOTAL DEBIT ' + str ( total_debit ) + ' TOTAL CREDIT ' + str ( total_credit ) )
return True
def out_refund_lines ( self , f ) :
global total_debit
global total_credit
@ -666,7 +717,7 @@ class AccountingFileWizard(models.TransientModel):
#é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 ' ) , ( ' state ' , ' = ' , ' posted ' ) , ( ' payment_state ' , ' = ' , ' not_paid ' ) , ( ' date_compta ' , ' = ' , False ) , ( ' date_compta_out_refund ' , ' = ' , False ) ] )
for invoice in out_refund_invoices :
if not debug : invoice . date_compta_out_refund = date_compta
if not self . debug_mode : invoice . date_compta_out_refund = date_compta
firstname = invoice . partner_id . firstname
name = invoice . partner_id . name
if not name : name = ' N/A '
@ -710,11 +761,11 @@ class AccountingFileWizard(models.TransientModel):
out_refund_invoices = self . env [ ' account.move ' ] . search ( [ ( ' move_type ' , ' = ' , ' out_refund ' ) , ( ' payment_state ' , ' = ' , ' paid ' ) , ( ' date_compta ' , ' = ' , False ) ] )
for invoice in out_refund_invoices :
_logger . error ( invoice . id )
if not debug : invoice . date_compta = date_compta
#_logger.error(invoice.id )
if not self . debug_mode : invoice . date_compta = date_compta
firstname = invoice . partner_id . firstname
name = invoice . partner_id . name
_logger . error ( name )
#_logger.error(name )
if not name : name = ' N/A '
if not firstname : firstname = ' '
lib1 = firstname [ 0 ] + ' ' + name
@ -735,7 +786,7 @@ class AccountingFileWizard(models.TransientModel):
account_debit_number = ' 411000 '
#date du paiement
_logger . error ( self . _payment_date_ok ( 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 )
@ -785,7 +836,7 @@ class AccountingFileWizard(models.TransientModel):
payment_info = json . loads ( move . invoice_payments_widget )
if payment_info :
_logger . error ( payment_info [ ' content ' ] [ 0 ] [ ' date ' ] )
#_logger.error(payment_info['content'][0]['date'] )
if datetime . strptime ( payment_info [ ' content ' ] [ 0 ] [ ' date ' ] , ' % Y- % m- %d ' ) > = self . start_date :
return True