import re import os import logging from time import sleep from datetime import datetime from datetime import timedelta import yagmail from winmentor import Invoice from winmentor import headers from winmentor import WinMentor tarif = {'COM ELECTRO SRL': {'mail': ['comelectrocj@yahoo.com', 'petrisor.sarca@comelectro-cluj.ro'], 'price': 1500}, 'CHRYSOPEEA BRANDING & DESIGN SRL': {'mail': ['cristina.stan@chrysopeea.com', 'sfeliciacristina@yahoo.com', 'sfeliciacristina0@gmail.com'], 'price': 1400}, 'GAL BANCSI ZOLTAN PFA': {'mail': 'zoltang@gmail.com', 'price': 50}, 'K&K INVESTMENT SRL': {'mail': ['office@kkinv.ro', 'katalin.betegh@kkinv.ro'], 'price': 1300}, 'MATHESITRAD SRL': {'mail': 'tudor.aciu@gmail.com', 'price': 100}, 'WEBS RO SRL': {'mail': 'webs_srl@yahoo.com', 'price': 500}, 'IROD M SRL': {'mail': 'irodm.cluj@gmail.com', 'price': 1700}, 'VMI LOGISTIC CAR SRL': {'mail': 'irimias_laura@yahoo.com', 'price': 250}, 'RENOBI CONSULTING SRL': {'mail': 'office@robeni.ro', 'price': 200}, 'ROBENI TRANS SRL': {'mail': 'office@robeni.ro', 'price': 200}, 'PANGEA TOURIMPEX SRL': {'mail': 'office@robeni.ro', 'price': 200}, 'TAURUS IMOBILIARE SRL': {'mail': 'cotinghiu_dan@yahoo.com', 'price': 300}} # tarif = {'COM ELECTRO SRL': {'mail': 'deeejas@yandex.com', 'price': 1000}} exclude = ['RENOBI CONSULTING SRL', 'ROBENI TRANS SRL', 'PANGEA TOURIMPEX SRL'] tarif2 = tarif.copy() for key in exclude: if key in tarif2: del tarif2[key] logging.basicConfig(filename='winmentor/generated_files/inv_number', format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', level=logging.INFO) mentor = WinMentor() office = mentor.firmlist(headers=headers) fact = Invoice('RO6110398', office) fact.add_bank_account('RO90BTRL01301202926963XX') fact.add_net('www.taxwizard.ro, claudia.conta.ab@gmail.com') # month_1 = (datetime.now() - timedelta(weeks=12)).month month_2 = (datetime.now() - timedelta(weeks=4)).month if datetime.now().month == 1: year_ = datetime.now().year - 1 else: year_ = datetime.now().year service = 'servicii de contabilitate perioada {:02d}-{}' letter = yagmail.SMTP(user='claudia.conta.ab@gmail.com') for entity in sorted(office, key=lambda company: company.name): if entity.name in tarif2: with open('winmentor/generated_files/inv_number') as logged: item = logged.readlines() sep = re.search('-', item[-1]) invNum = int(item[-1][sep.end():-1]) invNum += 1 logging.info(f'inv:{entity.name}_{entity.vat_code}-{invNum}') sleep(0.5) fact.gen_file(entity, 'my-logo.png', 'http://www.taxwizard.ro', tarif2, invNum, service.format(month_2, year_)) for file in os.listdir(fact.PATH_PDF): # print(i[0] + '_' + str(datetime.now().month) + '_' + str(datetime.now().year) + '_' + str(invNum)) # if re.search(i.name + '_' + str(datetime.now().month) + '_' + str(datetime.now().year) + '_' + str(invNum), f): # str(i[0] + '_' + datetime.now().month + '_' + datetime.now().year + '_' + invNum) if re.search(entity.name.title() + ' ' + str(datetime.now().date()), file): print('match found', file) sleep(0.5) subj = f'Factura {entity.name.title()} {datetime.now().date()}' body = f'Regasiti in atasament factura de contabilitate pentru {entity.name.title()} nr. {invNum} din {datetime.now().date()}' letter.send(to=entity.mail, subject=subj, contents=body, attachments=fact.PATH_PDF + '/' + file) sleep(0.5) root, archives = os.walk('winmentor/generated_files') # print(root) for file in filter(lambda f: f.endswith('.pdf'), os.listdir('winmentor/generated_files')): os.rename(os.path.join(root[0], file), os.path.join(archives[0], file)) print('files archived')