@ -4,17 +4,59 @@ from datetime import datetime,date
import os , secrets
import os , secrets
import decimal
import decimal
import base64
import base64
import logging
import json
_logger = logging . getLogger ( __name__ )
code_journal = ' IN '
code_journal = ' IN '
date_compta = fields . datetime . now ( )
date_compta = fields . datetime . now ( )
total_debit = total_credit = 0
no_mvt = 1
class AccountingFileWizard ( models . TransientModel ) :
class AccountingFileWizard ( models . TransientModel ) :
_name = ' accounting.file.wizard '
_name = ' accounting.file.wizard '
_description = ' accounting file wizard '
_description = ' accounting file wizard '
start_date = fields . Datetime ( ' start date ' , required = True , default = lambda self : self . _compute_start_date ( ) )
def _compute_start_date ( self ) :
date_max = [ ]
#recherche de la date d'export d'écriture la plus récente
query = " select max(date_compta) as date_compta from donation_donation "
self . _cr . execute ( query )
query_res = self . _cr . dictfetchall ( )
for r in query_res :
d_date_compta = r [ ' date_compta ' ]
if d_date_compta : date_max . append ( d_date_compta )
query = " select max(date_compta) as date_compta from kalachakra_membership "
self . _cr . execute ( query )
query_res = self . _cr . dictfetchall ( )
for r in query_res :
m_date_compta = r [ ' date_compta ' ]
if m_date_compta : date_max . append ( m_date_compta )
query = " select max(date_compta) as date_compta from event_registration "
self . _cr . execute ( query )
query_res = self . _cr . dictfetchall ( )
for r in query_res :
e_date_compta = r [ ' date_compta ' ]
if e_date_compta : date_max . append ( e_date_compta )
if date_max : return max ( date_max )
else : return fields . Date . context_today ( self )
#accounting_date=fields.Date(string='Accounting date',required=True)
#accounting_date=fields.Date(string='Accounting date',required=True)
def action_create_accounting_file ( self ) :
def action_create_accounting_file ( self ) :
global total_debit
global total_credit
total_debit = 0
total_credit = 0
#debug
#debug
donation = self . env [ ' donation.donation ' ] . search ( [ ] )
donation = self . env [ ' donation.donation ' ] . search ( [ ] )
for d in donation :
for d in donation :
@ -26,7 +68,7 @@ class AccountingFileWizard(models.TransientModel):
for r in reg :
for r in reg :
r . date_compta = False
r . date_compta = False
r . date_compta_down_payment = False
r . date_compta_down_payment = False
tmstp = secrets . token_hex ( 16 )
tmstp = secrets . token_hex ( 16 )
filename = ' /tmp/accounting_file_ ' + tmstp + ' .txt '
filename = ' /tmp/accounting_file_ ' + tmstp + ' .txt '
@ -41,19 +83,18 @@ class AccountingFileWizard(models.TransientModel):
#Débit ex: 200,00
#Débit ex: 200,00
#Crédit ex : 200,00
#Crédit ex : 200,00
#N° de pièce: ref transaction , ex: txD-457
#N° de pièce: ref transaction , ex: txD-457
no_mvt = 1
fic_line = " N° mouvement;Code du journal;Date;N° de compte;Libellé;Débit;Crédit;N° de pièce " + " \n "
fic_line = " N° mouvement;Code du journal;Date;N° de compte;Libellé;Débit;Crédit;N° de pièce " + " \n "
f . write ( fic_line )
f . write ( fic_line )
#self.donation_lines(f,no_mvt)
#self.membership_lines(f,no_mvt)
#self.event_lines(f,no_mvt)
self . booking_event_lines ( f , no_mvt )
self . donation_lines ( f )
self . membership_lines ( f )
self . event_lines ( f )
self . booking_event_lines ( f )
fic_line = " TOTAL_DEBIT= " + str ( total_debit ) + " TOTAL CREDIT= " + str ( total_credit )
f . write ( fic_line )
f . close
f . close
f = open ( filename , " r " )
f = open ( filename , " r " )
@ -111,10 +152,15 @@ class AccountingFileWizard(models.TransientModel):
}
}
return True
return True
def donation_lines ( self , f , no_mvt ) :
def donation_lines ( self , f ) :
global total_debit
global total_credit
global no_mvt
#écritures pour les dons validés (donc payés), non déjà exportés
#écritures pour les dons validés (donc payés), non déjà exportés
donation = self . env [ ' donation.donation ' ] . search ( [ ( ' date_compta ' , ' = ' , False ) , ( ' state ' , ' = ' , ' done ' ) ] )
donation = self . env [ ' donation.donation ' ] . search ( [ ( ' date_compta ' , ' = ' , False ) , ( ' state ' , ' = ' , ' done ' ) ] )
#raise UserError(donation)
for d in donation :
for d in donation :
if datetime ( d . donation_date . year , d . donation_date . month , d . donation_date . day ) < self . start_date : continue
#on ne prend pas en compte les dons liés à une retraite
#on ne prend pas en compte les dons liés à une retraite
#car traité avec les retraites
#car traité avec les retraites
if d . invoice_id : continue
if d . invoice_id : continue
@ -122,6 +168,10 @@ class AccountingFileWizard(models.TransientModel):
#if not d.payment_mode_id: raise UserError(_("no payment mode found for donation %r ",donation.number))
#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))
#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 )
account_debit_number = self . _file_format ( str ( d . payment_mode_id . fixed_journal_id . default_account_id . code ) , 6 )
if d . transaction_id . 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 )
firstname = d . partner_id . firstname
firstname = d . partner_id . firstname
name = d . partner_id . name
name = d . partner_id . name
if not name : name = ' N/A '
if not name : name = ' N/A '
@ -131,15 +181,15 @@ class AccountingFileWizard(models.TransientModel):
lib1 = self . _file_format ( lib1 , 12 )
lib1 = self . _file_format ( lib1 , 12 )
#lib1=lib1[11]
#lib1=lib1[11]
date_line = self . _date_format ( str ( d . donation_date ) )
date_line = self . _date_format ( d . donation_date )
#recherche de la transaction
#recherche de la transaction
lib_piece = self . _file_format ( d . number , 20 )
lib_piece = self . _file_format ( d . number , 20 )
pt = self . env [ ' payment.transaction ' ] . search ( [ ( ' donation_ids ' , ' in ' , d . id ) ] )
pt = self . env [ ' payment.transaction ' ] . search ( [ ( ' donation_ids ' , ' in ' , d . id ) ] , limit = 1 )
if pt : lib_piece = pt . reference
if pt : lib_piece = pt . reference
for line in d . line_ids :
for line in d . line_ids :
affectation = line . product_id . name
affectation = line . product_id . name
amount = str ( line . amount )
amount = line . amount
#if not line.product_id.property_account_income_id: raise UserError(_("no credit account found for product %r",line.product_id.name))
#if not line.product_id.property_account_income_id: raise UserError(_("no credit account found for product %r",line.product_id.name))
account_credit_number = self . _file_format ( line . product_id . property_account_income_id . code , 6 )
account_credit_number = self . _file_format ( line . product_id . property_account_income_id . code , 6 )
@ -149,23 +199,28 @@ class AccountingFileWizard(models.TransientModel):
lib = lib1 + ' ' + lib2
lib = lib1 + ' ' + lib2
debit = ' '
debit = ' '
credit = str ( amount )
credit = str ( amount )
total_credit + = amount
fic_line = str ( no_mvt ) + ' ; ' + ' IN ' + ' ; ' + date_line + ' ; ' + account_credit_number + ' ; ' + lib + ' ; ' + debit + ' ; ' + credit + ' ; ' + lib_piece + ' \n '
fic_line = str ( no_mvt ) + ' ; ' + ' IN ' + ' ; ' + date_line + ' ; ' + account_credit_number + ' ; ' + lib + ' ; ' + debit + ' ; ' + credit + ' ; ' + lib_piece + ' \n '
f . write ( fic_line )
f . write ( fic_line )
no_mvt = no_mvt + 1
no_mvt + = 1
#debit line
#debit line
debit = str ( amount )
debit = str ( amount )
credit = ' '
credit = ' '
fic_line = str ( no_mvt ) + ' ; ' + ' IN ' + ' ; ' + date_line + ' ; ' + account_debit_number + ' ; ' + lib + ' ; ' + debit + ' ; ' + credit + ' ; ' + lib_piece + ' \n '
fic_line = str ( no_mvt ) + ' ; ' + ' IN ' + ' ; ' + date_line + ' ; ' + account_debit_number + ' ; ' + lib + ' ; ' + debit + ' ; ' + credit + ' ; ' + lib_piece + ' \n '
f . write ( fic_line )
f . write ( fic_line )
no_mvt = no_mvt + 1
total_debit + = amount
no_mvt + = 1
return no_mvt
return no_mvt
def membership_lines ( self , f , no_mvt ) :
def membership_lines ( self , f ) :
global total_debit
global total_credit
global no_mvt
#écritures pour les ADHESIONS réglées non déjà exportés
#écritures pour les ADHESIONS réglées non déjà exportés
membership = self . env [ ' kalachakra.membership ' ] . search ( [ ( ' date_compta ' , ' = ' , False ) , ( ' state ' , ' = ' , ' done ' ) ] )
membership = self . env [ ' kalachakra.membership ' ] . search ( [ ( ' date_compta ' , ' = ' , False ) , ( ' state ' , ' = ' , ' done ' ) ] )
for m in membership :
for m in membership :
m . date_compta = date_compta
m . date_compta = date_compta
if datetime ( m . start_date . year , m . start_date . month , m . start_date . day ) < self . start_date : continue
#on ne prend pas en compte les adhésions liés à une retraite
#on ne prend pas en compte les adhésions liés à une retraite
#car traité avec les retraites
#car traité avec les retraites
if m . balance_invoice_id : continue
if m . balance_invoice_id : continue
@ -176,12 +231,13 @@ class AccountingFileWizard(models.TransientModel):
#si adhésion pas liée à une facture ou à une transaction, on ne prend pas
#si adhésion pas liée à une facture ou à une transaction, on ne prend pas
if not m . invoice_id and not m . transaction_id : continue
if not m . invoice_id and not m . transaction_id : continue
if m . transaction_id :
CB_pm = self . env [ ' account.payment.mode ' ] . search ( [ ( ' name ' , ' = ' , ' CB ' ) ] , limit = 1 )
if CB_pm :
account_debit_number = self . _file_format ( str ( CB_pm . fixed_journal_id . default_account_id . code ) , 6 )
else :
raise UserError ( _ ( ' please configure CB payement mode ' ) )
if m . transaction_id . 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 )
else :
account_debit_number = self . _file_format ( str ( self . env [ ' account.payment.mode ' ] . search ( [ ( ' name ' , ' = ' , ' CB ' ) ] ) . fixed_journal_id . default_account_id . code ) , 6 )
if m . invoice_id :
if m . invoice_id :
#if not m.invoice_id.payment_mode_id.fixed_journal_id: raise UserError(_("no account found for payment mode %r",d.payment_mode_id.name))
#if not m.invoice_id.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 ( m . invoice_id . payment_mode_id . fixed_journal_id . default_account_id . code ) , 6 )
account_debit_number = self . _file_format ( str ( m . invoice_id . payment_mode_id . fixed_journal_id . default_account_id . code ) , 6 )
@ -192,10 +248,10 @@ class AccountingFileWizard(models.TransientModel):
if not firstname : firstname = ' '
if not firstname : firstname = ' '
lib1 = firstname [ 0 ] + ' ' + name
lib1 = firstname [ 0 ] + ' ' + name
lib1 = self . _file_format ( lib1 , 12 )
lib1 = self . _file_format ( lib1 , 12 )
date_line = self . _date_format ( str ( m . start_date ) )
amount = str ( m . amount )
date_line = self . _date_format ( m . start_date )
amount = m . amount
#if not m.product_id.property_account_income_id: raise UserError(_("no credit account found for product %r",m.product_id.name))
#if not m.product_id.property_account_income_id: raise UserError(_("no credit account found for product %r",m.product_id.name))
account_credit_number = self . _file_format ( m . product_id . property_account_income_id . code , 6 )
account_credit_number = self . _file_format ( ' 756100 ' , 6 )
affectation = m . product_id . name
affectation = m . product_id . name
#lib pièce
#lib pièce
@ -214,15 +270,20 @@ class AccountingFileWizard(models.TransientModel):
fic_line = str ( no_mvt ) + ' ; ' + ' IN ' + ' ; ' + date_line + ' ; ' + account_credit_number + ' ; ' + lib + ' ; ' + debit + ' ; ' + credit + ' ; ' + lib_piece + ' \n '
fic_line = str ( no_mvt ) + ' ; ' + ' IN ' + ' ; ' + date_line + ' ; ' + account_credit_number + ' ; ' + lib + ' ; ' + debit + ' ; ' + credit + ' ; ' + lib_piece + ' \n '
f . write ( fic_line )
f . write ( fic_line )
no_mvt = no_mvt + 1
no_mvt + = 1
total_credit + = amount
#debit line
#debit line
debit = str ( amount )
debit = str ( amount )
credit = ' '
credit = ' '
fic_line = str ( no_mvt ) + ' ; ' + ' IN ' + ' ; ' + date_line + ' ; ' + account_debit_number + ' ; ' + lib + ' ; ' + debit + ' ; ' + credit + ' ; ' + lib_piece + ' \n '
fic_line = str ( no_mvt ) + ' ; ' + ' IN ' + ' ; ' + date_line + ' ; ' + account_debit_number + ' ; ' + lib + ' ; ' + debit + ' ; ' + credit + ' ; ' + lib_piece + ' \n '
f . write ( fic_line )
f . write ( fic_line )
no_mvt = no_mvt + 1
no_mvt + = 1
total_debit + = amount
return no_mvt
return no_mvt
def event_lines ( self , f , no_mvt ) :
def event_lines ( self , f ) :
global total_debit
global total_credit
global no_mvt
#écritures pour les inscriptions évts hors RETRAITES avec facture payée non déjà exportés
#écritures pour les inscriptions évts hors RETRAITES avec facture payée non déjà exportés
@ -232,6 +293,14 @@ class AccountingFileWizard(models.TransientModel):
reg . date_compta = date_compta
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))
#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 )
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 :
account_debit_number = self . _file_format ( str ( self . env [ ' account.payment.mode ' ] . search ( [ ( ' name ' , ' = ' , ' Espèces ' ) ] ) . fixed_journal_id . default_account_id . code ) , 6 )
trans = self . env [ ' payment.transaction ' ] . search ( [ ( ' invoice_ids ' , ' in ' , reg . invoice_id . id ) , ( ' state ' , ' = ' , ' done ' ) ] , limit = 1 )
if trans :
if 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 )
firstname = reg . partner_id . firstname
firstname = reg . partner_id . firstname
name = reg . partner_id . name
name = reg . partner_id . name
if not name : name = ' N/A '
if not name : name = ' N/A '
@ -239,17 +308,21 @@ class AccountingFileWizard(models.TransientModel):
lib1 = firstname [ 0 ] + ' ' + name
lib1 = firstname [ 0 ] + ' ' + name
lib1 = self . _file_format ( lib1 , 12 )
lib1 = self . _file_format ( lib1 , 12 )
#date du paiement
#date du paiement
date_line = str ( reg . write_date . year ) + ' - ' + str ( reg . write_date . month ) + ' - ' + str ( reg . write_date . day )
date_line = self . _date_format ( date_line )
if self . _payment_date_date ( reg . invoice_id ) : continue
date_line = self . _payment_date ( reg . invoice_id )
if reg . invoice_id . amount_total == 0 : continue
if reg . invoice_id . amount_total == 0 : continue
if reg . invoice_id . payment_state != ' paid ' : continue
if reg . invoice_id . payment_state != ' paid ' : continue
amount = str ( reg . invoice_id . amount_total )
amount = reg . invoice_id . amount_total
#if not reg.event_id.participation_product_id: raise UserError(_("no credit account found for product %r",reg.event_id.participation_product_id.name))
#if not reg.event_id.participation_product_id: raise UserError(_("no credit account found for product %r",reg.event_id.participation_product_id.name))
account_credit_number = self . _file_format ( reg . event_id . participation_product_id . property_account_income_id . code , 6 )
account_credit_number = self . _file_format ( reg . event_id . participation_product_id . property_account_income_id . code , 6 )
if not reg . event_id . participation_product_id . property_account_income_id . code :
account_credit_number = self . _file_format ( ' 754700 ' , 6 )
affectation = reg . event_id . participation_product_id . name
affectation = reg . event_id . participation_product_id . name
#si facture liée à une transaction:
#si facture liée à une transaction:
trans = self . env [ ' payment.transaction ' ] . search ( [ ( ' invoice_ids ' , ' in ' , reg . invoice_id . id ) , ( ' state ' , ' = ' , ' done ' ) ] )
trans = self . env [ ' payment.transaction ' ] . search ( [ ( ' invoice_ids ' , ' in ' , reg . invoice_id . id ) , ( ' state ' , ' = ' , ' done ' ) ] , limit = 1 )
if trans :
if trans :
lib_piece = self . _file_format ( trans . reference , 20 )
lib_piece = self . _file_format ( trans . reference , 20 )
else :
else :
@ -262,64 +335,82 @@ class AccountingFileWizard(models.TransientModel):
fic_line = str ( no_mvt ) + ' ; ' + ' IN ' + ' ; ' + date_line + ' ; ' + account_credit_number + ' ; ' + lib + ' ; ' + debit + ' ; ' + credit + ' ; ' + lib_piece + ' \n '
fic_line = str ( no_mvt ) + ' ; ' + ' IN ' + ' ; ' + date_line + ' ; ' + account_credit_number + ' ; ' + lib + ' ; ' + debit + ' ; ' + credit + ' ; ' + lib_piece + ' \n '
f . write ( fic_line )
f . write ( fic_line )
no_mvt = no_mvt + 1
no_mvt + = 1
total_credit + = amount
#debit line
#debit line
debit = str ( amount )
debit = str ( amount )
credit = ' '
credit = ' '
fic_line = str ( no_mvt ) + ' ; ' + ' IN ' + ' ; ' + date_line + ' ; ' + account_debit_number + ' ; ' + lib + ' ; ' + debit + ' ; ' + credit + ' ; ' + lib_piece + ' \n '
fic_line = str ( no_mvt ) + ' ; ' + ' IN ' + ' ; ' + date_line + ' ; ' + account_debit_number + ' ; ' + lib + ' ; ' + debit + ' ; ' + credit + ' ; ' + lib_piece + ' \n '
f . write ( fic_line )
f . write ( fic_line )
no_mvt = no_mvt + 1
no_mvt + = 1
total_debit + = amount
return no_mvt
return no_mvt
def booking_event_lines ( self , f , no_mvt ) :
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é
#é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 ( [ ' | ' , ( ' date_compta ' , ' = ' , False ) , ( ' date_compta_down_payment ' , ' != ' , False ) ] )
registration = self . env [ ' event.registration ' ] . search ( [ ] )
for reg in registration :
for reg in registration :
if reg :
if reg . event_id . booking_event :
#_logger.error('acompte:'+str(reg.down_payment_invoice_id.payment_state)+' solde :'+str(reg.balance_invoice_id.payment_state)+' date_compta'+str(reg.date_compta)+' date_compta_down_payment:'+str(reg.date_compta_down_payment))
membership_amount = 0
membership_amount = 0
donation_amount = 0
donation_amount = 0
membership_credit_line = False
membership_credit_line = False
donation_credit_line = False
donation_credit_line = False
reg . date_compta = date_compta
#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))
#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 ( reg . event_id . participation_product_id . property_account_income_id . code , 6 )
account_credit_number = self . _file_format ( ' 170000 ' , 6 )
firstname = reg . firstname
name = reg . name
firstname = reg . partner_id . firstname
name = reg . partner_id . name
if not name : name = ' N/A '
if not name : name = ' N/A '
if not firstname : firstname = ' '
if not firstname : firstname = ' '
lib1 = firstname [ 0 ] + ' ' + name
lib1 = firstname [ 0 ] + ' ' + name
lib1 = self . _file_format ( lib1 , 12 )
lib1 = self . _file_format ( lib1 , 12 )
lib2 = self . _file_format ( reg . event_id . participation _product_id. name , 13 )
lib2 = self . _file_format ( reg . event_id . booking _product_id. name , 13 )
lib = lib1 + ' ' + lib2
lib = lib1 + ' ' + lib2
#si pas de facture d'acompte et facture payée:
#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 not reg . date_compta :
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
case_line = 1
down_payment = False
down_payment = False
balance_payment = False
balance_payment = False
#if not reg.invoice_id.payment_mode_id.fixed_journal_id: raise UserError(_("no account found payment mode %r",reg.invoice_id.payment_mode_id.name))
#if not reg.invoice_id.payment_mode_id.fixed_journal_id: raise UserError(_("no account found payment mode %r",reg.invoice_id.payment_mode_id.name))
account_debit_number = self . _file_format ( str ( reg . invoice_id . payment_mode_id . fixed_journal_id . default_account_id . code ) , 6 )
account_debit_number = self . _file_format ( str ( reg . invoice_id . payment_mode_id . fixed_journal_id . default_account_id . code ) , 6 )
amount = reg . invoice_id . amount_total
trans = self . env [ ' payment.transaction ' ] . search ( [ ( ' invoice_ids ' , ' in ' , reg . 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 )
#recherche de l'adhésion
#recherche de l'adhésion
membership_credit_line = False
membership_credit_line = False
membership = self . env [ ' kalachakra.membership ' ] . search ( [ ( ' invoice_id ' , ' = ' , int ( reg . invoice_id ) ) ] )
membership = self . env [ ' kalachakra.membership ' ] . search ( [ ( ' invoice_id ' , ' = ' , int ( reg . invoice_id ) ) ] )
if membership : membership_credit_line = True
if membership :
membership_credit_line = True
membership_amount = membership . amount
#recherche de dons
#recherche de dons
donation_credit_line = False
donation_credit_line = False
donations = donation_booking = self . env [ ' donation.donation ' ] . search ( [ ( ' invoice_id ' , ' = ' , int ( reg . invoice_id ) ) ] )
donations = donation_booking = self . env [ ' donation.donation ' ] . search ( [ ( ' invoice_id ' , ' = ' , int ( reg . invoice_id ) ) ] )
if donations : donation_credit_line = True
date_line = self . _date_format ( str ( reg . invoice_id . write_date ) )
trans = self . env [ ' payment.transaction ' ] . search ( [ ( ' invoice_ids ' , ' in ' , reg . invoice_id ) ] )
if donations :
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 )
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 :
#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 :
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 ) )
case_line = 2
reg . date_compta = date_compta
reg . date_compta = date_compta
down_payment = True
down_payment = True
balance_payment = True
balance_payment = True
membership_amount = 0
membership_amount = 0
donation_amount = 0
donation_amount = 0
@ -327,8 +418,15 @@ class AccountingFileWizard(models.TransientModel):
donation_credit_line = False
donation_credit_line = False
#if not reg.down_payment_invoice_id.payment_mode_id.fixed_journal_id: raise UserError(_("no account found payment mode %r",reg.down_payment_invoice_id.payment_mode_id.name))
#if not reg.down_payment_invoice_id.payment_mode_id.fixed_journal_id: raise UserError(_("no account found payment mode %r",reg.down_payment_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 )
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 )
amount = reg . down_payment_invoice_id . amount_total + reg . balance_invoice_id . amount_total
amount = reg . down_payment_invoice_id . amount_total + reg . balance_invoice_id . amount_total
date_line = self . _date_format ( str ( reg . balance_invoice_id . write_date ) )
if self . _payment_date_date ( reg . balance_invoice_id ) : continue
date_line = self . _payment_date ( reg . balance_invoice_id )
#recherche de l'adhésion
#recherche de l'adhésion
membership = self . env [ ' kalachakra.membership ' ] . search ( [ ( ' balance_invoice_id ' , ' = ' , int ( reg . balance_invoice_id ) ) ] )
membership = self . env [ ' kalachakra.membership ' ] . search ( [ ( ' balance_invoice_id ' , ' = ' , int ( reg . balance_invoice_id ) ) ] )
@ -345,10 +443,12 @@ class AccountingFileWizard(models.TransientModel):
for d in donations :
for d in donations :
donation_amount = donation_amount + d . amount_total
donation_amount = donation_amount + d . amount_total
trans = self . env [ ' payment.transaction ' ] . search ( [ ( ' invoice_ids ' , ' in ' , reg . invoice_id . id ) ] )
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 pas encore exporté et solde non payée :
#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 :
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 ' )
case_line = 3
reg . date_compta_down_payment = date_compta
reg . date_compta_down_payment = date_compta
down_payment = True
down_payment = True
balance_payment = False
balance_payment = False
@ -356,17 +456,30 @@ class AccountingFileWizard(models.TransientModel):
membership_credit_line = False
membership_credit_line = False
#if not reg.down_payment_invoice_id.payment_mode_id.fixed_journal_id: raise UserError(_("no account found payment mode %r",reg.down_payment_invoice_id.payment_mode_id.name))
#if not reg.down_payment_invoice_id.payment_mode_id.fixed_journal_id: raise UserError(_("no account found payment mode %r",reg.down_payment_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 )
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 )
amount = reg . down_payment_invoice_id . amount_total
amount = reg . down_payment_invoice_id . amount_total
date_line = self . _date_format ( str ( reg . down_payment_invoice_id . write_date ) )
trans = self . env [ ' payment.transaction ' ] . search ( [ ( ' invoice_ids ' , ' in ' , reg . down_payment_invoice_id . id ) ] )
if self . _payment_date_date ( reg . down_payment_invoice_id ) : continue
date_line = self . _payment_date ( reg . down_payment_invoice_id )
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é :
#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 :
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 ' )
case_line = 4
reg . date_compta = date_compta
reg . date_compta = date_compta
down_payment = False
down_payment = False
balance_payment = True
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))
#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 )
account_debit_number = self . _file_format ( str ( reg . down_payment_invoice_id . payment_mode_id . fixed_journal_id . default_account_id . code ) , 6 )
date_line = self . _date_format ( str ( reg . balance_invoice_id . write_date ) )
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
date_line = self . _payment_date ( reg . balance_invoice_id )
#recherche de l'adhésion
#recherche de l'adhésion
membership_credit_line = False
membership_credit_line = False
membership = self . env [ ' kalachakra.membership ' ] . search ( [ ( ' balance_invoice_id ' , ' = ' , int ( reg . balance_invoice_id ) ) ] )
membership = self . env [ ' kalachakra.membership ' ] . search ( [ ( ' balance_invoice_id ' , ' = ' , int ( reg . balance_invoice_id ) ) ] )
@ -381,33 +494,80 @@ class AccountingFileWizard(models.TransientModel):
donation_credit_line = True
donation_credit_line = True
for d in donations :
for d in donations :
amount_donation = amount_donation + donations . amount_total
amount_donation = amount_donation + donations . amount_total
trans = self . env [ ' payment.transaction ' ] . search ( [ ( ' invoice_ids ' , ' in ' , reg . balance_invoice_id . id ) ] )
#écritures pour les inscription aux RETRAITES avec facture complémentaire
trans = self . env [ ' payment.transaction ' ] . search ( [ ( ' state ' , ' = ' , ' done ' ) , ( ' invoice_ids ' , ' in ' , reg . balance_invoice_id . id ) ] , limit = 1 )
else : continue
else : continue
if trans :
if trans :
lib_piece = self . _file_format ( trans . reference , 20 )
lib_piece = self . _file_format ( trans . reference , 20 )
else :
else :
lib_piece = self . _file_format ( lib2 , 20 )
if case_line == 1 : lib_piece = self . _file_format ( reg . invoice_id . name , 20 )
if case_line == 2 : lib_piece = self . _file_format ( reg . balance_invoice_id . name , 20 )
if case_line == 3 : lib_piece = self . _file_format ( reg . down_payment_invoice_id . name , 20 )
if case_line == 4 : lib_piece = self . _file_format ( reg . balance_invoice_id . name , 20 )
#debit line
#debit line
debit = str ( amount )
if case_line == 1 : amount = reg . invoice_id . amount_total
if case_line == 2 : amount = reg . down_payment_invoice_id . amount_total + reg . balance_invoice_id . amount_total
if case_line == 3 : amount = reg . down_payment_invoice_id . amount_total
if case_line == 4 : amount = reg . balance_invoice_id . amount_total
if amount == 0 : continue
credit = ' '
credit = ' '
debit = str ( amount )
fic_line = str ( no_mvt ) + ' ; ' + ' IN ' + ' ; ' + date_line + ' ; ' + account_debit_number + ' ; ' + lib + ' ; ' + debit + ' ; ' + credit + ' ; ' + lib_piece + ' \n '
fic_line = str ( no_mvt ) + ' ; ' + ' IN ' + ' ; ' + date_line + ' ; ' + account_debit_number + ' ; ' + lib + ' ; ' + debit + ' ; ' + credit + ' ; ' + lib_piece + ' \n '
f . write ( fic_line )
f . write ( fic_line )
no_mvt = no_mvt + 1
no_mvt = no_mvt + 1
total_debit + = amount
#credit(s) line(s)
#credit(s) line(s)
#participation credit line
#participation credit line
if not amount : amount = 0
if not amount : amount = 0
amount = float ( amount ) - float ( membership_amount ) - float ( donation_amount )
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 = no_mvt + 1
if case_line == 1 :
amount = amount - membership_amount - donation_amount
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 = no_mvt + 1
total_credit + = amount
if case_line == 2 :
amount = reg . down_payment_invoice_id . amount_total
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 = no_mvt + 1
total_credit + = amount
amount = float ( reg . balance_invoice_id . amount_total ) - float ( membership_amount ) - float ( donation_amount )
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 = no_mvt + 1
total_credit + = amount
if case_line == 3 :
amount = reg . down_payment_invoice_id . amount_total
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 = no_mvt + 1
total_credit + = amount
if case_line == 4 :
amount = reg . balance_invoice_id . amount_total - membership_amount - donation_amount
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 = no_mvt + 1
total_credit + = amount
if membership_credit_line :
if membership_credit_line :
debit = ' '
debit = ' '
@ -415,15 +575,44 @@ class AccountingFileWizard(models.TransientModel):
fic_line = str ( no_mvt ) + ' ; ' + ' IN ' + ' ; ' + date_line + ' ; ' + account_credit_number + ' ; ' + lib + ' ; ' + debit + ' ; ' + credit + ' ; ' + lib_piece + ' \n '
fic_line = str ( no_mvt ) + ' ; ' + ' IN ' + ' ; ' + date_line + ' ; ' + account_credit_number + ' ; ' + lib + ' ; ' + debit + ' ; ' + credit + ' ; ' + lib_piece + ' \n '
f . write ( fic_line )
f . write ( fic_line )
no_mvt = no_mvt + 1
no_mvt = no_mvt + 1
total_credit + = membership_amount
if donation_credit_line :
if donation_credit_line :
for d in donations :
for d in donations :
debit = ' '
debit = ' '
credit = str ( d . amount_total )
amount = d . amount_total
credit = str ( amount )
fic_line = str ( no_mvt ) + ' ; ' + ' IN ' + ' ; ' + date_line + ' ; ' + account_credit_number + ' ; ' + lib + ' ; ' + debit + ' ; ' + credit + ' ; ' + lib_piece + ' \n '
fic_line = str ( no_mvt ) + ' ; ' + ' IN ' + ' ; ' + date_line + ' ; ' + account_credit_number + ' ; ' + lib + ' ; ' + debit + ' ; ' + credit + ' ; ' + lib_piece + ' \n '
f . write ( fic_line )
f . write ( fic_line )
no_mvt = no_mvt + 1
no_mvt = no_mvt + 1
return no_mvt
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 self . _payment_date_date ( reg . end_of_stay_invoice_id ) : continue
date_line = self . _payment_date ( reg . end_of_stay_invoice_id )
#écriture de débit
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 _file_format ( self , data , nbcars ) :
def _file_format ( self , data , nbcars ) :
if data :
if data :
if len ( data ) > nbcars :
if len ( data ) > nbcars :
@ -433,7 +622,44 @@ class AccountingFileWizard(models.TransientModel):
else :
else :
return ' '
return ' '
def _payment_ ( self , move_id ) :
if move_id . invoice_payments_widget :
payment_info = json . loads ( move_id . invoice_payments_widget )
if payment_info :
if datetime . strptime ( payment_info [ ' content ' ] [ 0 ] [ ' date ' ] , ' % Y- % m- %d ' ) > self . start_date :
return False
else :
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 )
if payment_info :
if datetime . strptime ( payment_info [ ' content ' ] [ 0 ] [ ' date ' ] , ' % Y- % m- %d ' ) > self . start_date :
return False
else :
return True
else : return True
def _payment_date ( self , move_id ) :
if move_id . invoice_payments_widget :
payment_info = json . loads ( move_id . invoice_payments_widget )
if payment_info :
return self . _date_format ( payment_info [ ' content ' ] [ 0 ] [ ' date ' ] )
#
else :
return ' False '
else : return ' False '
def _date_format ( self , data ) :
def _date_format ( self , data ) :
r = data . split ( ' - ' )
return r [ 2 ] + ' / ' + r [ 1 ] + ' / ' + r [ 0 ]
#_logger.error('date='+str(data))
if data :
r = str ( data ) . split ( ' - ' )
return r [ 2 ] + ' / ' + r [ 1 ] + ' / ' + r [ 0 ]
else :
return ' False '