from odoo import fields, models,api,_ from odoo.exceptions import UserError, ValidationError, Warning from datetime import datetime,date import os,secrets import decimal import base64 import logging import json _logger = logging.getLogger(__name__) code_journal='IN' date_compta=fields.datetime.now() total_debit=total_credit=0 no_mvt=1 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()) 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) def action_create_accounting_file(self): global total_debit global total_credit total_debit=0 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 tmstp=secrets.token_hex(16) filename='/tmp/accounting_file_'+tmstp+'.txt' f = open(filename, "a") #N° mouvement 1-5 #Code du journal "IN" 6-7 #Date 8-17 (10 cars) ex:01/04/2022 #N° de compte 18-23 (6 cars) #Libellé 24- (12+ 13 cars) 12:Initial prénom+nom 13:lib evt exemple :E Meurinne Besoins cent #Débit ex: 200,00 #Crédit ex : 200,00 #N° de pièce: ref transaction , ex: txD-457 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) 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 = open(filename, "r") data = str.encode(f.read(), 'utf-8') #raise UserError('gg'+f.read()) vals={} vals['accounting_file']=base64.encodebytes(data) vals['document_fname']='export_comptable.txt' vals['exported_date']=fields.Date.context_today(self) res=self.env['accounting.file'].create(vals) f.close os.unlink(filename) #display account deposit return { 'name': 'Account deposit form', 'view_type': 'form', 'view_mode': 'form', 'res_model': 'accounting.file', 'res_id': int(res.id), 'type': 'ir.actions.act_window', 'target': 'current' } #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 global no_mvt #é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')]) #raise UserError(donation) for d in donation: if datetime(d.donation_date.year, d.donation_date.month, d.donation_date.day)nbcars: return data[0:nbcars] else: return data else: 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): #_logger.error('date='+str(data)) if data: r=str(data).split('-') return r[2]+'/'+r[1]+'/'+r[0] else: return 'False'