import csv import http.client import time from io import StringIO import requests import xml.etree.ElementTree as ET from datetime import date import pandas as pd from datetime import date import datetime import os from datetime import datetime dimensao = pd.read_csv('Dimensao.csv') def busca_pdv(codigo_pesquisar): dimensao = pd.read_csv('Dimensao.csv',encoding='utf-8') df = dimensao codigos_correspondentes = df[(df['cpf_gr'] == codigo_pesquisar) | (df['cpf_sup'] == codigo_pesquisar)]['codigo'].tolist() unidades_dict = {'3546': 'aef41e8b-2a07-49eb-90ff-13b4a5ce6b65', '4494': '804cb2f5-be0e-46ea-81cf-5faa1bfb59f2', '4560': '2fb956b7-14ca-4320-b748-d7f3a9ea8e08', '5699': 'b215bb37-3d30-4684-8d63-006a543bf9b4', '11111': '01c5e9d5-fda9-4a7b-a57a-cf4ace4a0571', '12522': '64ea8dd9-ffde-495b-9f86-3108fab1a203', '12817': '56a47735-92e7-4317-9a86-0fcc35676a80', '12818': '32af600c-4823-487b-b254-981666086329', '12819': 'f20e7ffe-9d87-4fbc-b0ec-c49da6f43f33', '12820': '54ba626b-eee9-4e4b-8a99-cbc15c9e3490', '12823': '0c59fa2c-54d4-4f00-8952-38c9e9cda3d0', '12824': 'ff5b50cb-3268-4653-968e-293f5155b938', '12826': 'a15b0b35-bf09-4afa-8243-0469447d0ecd', '12828': '0ec5b446-bbd8-438b-ba7c-d89eb1867c9d', '12829': 'f3635215-5641-425f-80fa-44bc3041815e', '12830': '00c76705-008e-41ab-b731-f7db638b81cc', '12838': '35de9486-db2c-4449-8640-9e6e6e717c15', '13427': 'decb5cb6-1e33-4c28-a2a9-945ff520b733', '14617': '8b264cc0-cf93-4d8c-96d4-4dfb43fa5a4e', '14668': '9439d248-6ca7-49b0-8bec-365741bd1518', '19103': 'ea75000b-2d75-4eaa-af5f-37592b31c510', '20005': 'ba3c5825-bd75-471d-81be-e59dc9bdb765', '20006': 'f0169aed-0a07-4050-8952-114b1adafb50', '20007': '1a28d239-8a39-4205-ae71-305dd47a3f67', '20009': '4cd5cf69-82fd-472c-ade9-8cd924682dca', '20056': '9188a0fe-5672-4e94-abb1-7cd5564566ff', '20057': '8b2dae02-0d67-48d3-8a39-552769786b41', '20441': 'ac31866e-1106-4c41-85dd-61ab3f782a1f', '20858': '2189b870-51ab-4b5f-bf8d-2ed20324bfa2', '20968': '6462b005-be0a-4065-968c-8308ff97389d', '20969': '4c6eb12f-f16d-452f-808a-daa3d44c3cd0', '20970': '550d82e1-ab5c-496e-88dd-0a4d4174b039', '20986': 'd4c1f35b-0458-415c-ba02-e5a4a0e545e1', '20988': '72921848-e9a3-4837-9941-9fccf9ca1beb', '20989': '5b13795e-3062-4208-97de-27ed4642d097', '20991': '9939bb4a-3d8b-43c6-857d-7c2efd6541cc', '20992': '12148415-fb86-431e-89cd-7b17b27a345b', '20993': '4aeac823-6b09-4582-acb4-22f0e72735d8', '20994': '7860f680-bcc9-4d21-bf11-9dde76e2b1f1', '20995': 'a3e122bc-2060-4f80-94f5-f43be99fb7a2', '20996': '8676f6f8-a5b0-44cf-8220-06d41289fc34', '20997': '680f71f3-1d4a-4263-a91f-6fb2a08b2dda', '20998': '03cf8f3a-950c-43da-8b57-c498395fae5c', '20999': '58691507-093a-4197-a696-81367eff33ff', '21000': 'abcd6221-a26d-47bb-95e1-488602f76d51', '21001': 'eadb3fa2-36ec-4936-b840-fb9597cadbac', '21068': 'bdb2958d-7895-4c08-8dc6-7c724c1ae9c6', '21277': '6a448c7d-fe7c-41e4-b8eb-5433ac52693b', '21278': 'aedf2a6d-d982-4111-b3a4-117e46f5e9d5', '21279': 'af5f4861-581a-4d45-aeb9-c90e44f30a50', '21296': 'bbc66aa3-5d74-4405-8f58-d1b739c6a1ba', '21375': '57ff2d4a-3945-48ca-a227-533c402e3060', '21381': '2b030fe9-9285-45ef-97fb-b896337c739f', '21382': 'f0f4884b-c190-4868-85c3-8cb64872ca84', '21383': '0d9f481a-de26-47e6-8171-14f09643df57', '21495': 'f7b321d8-b043-48f2-8555-3496630e9e83', '21624': '22607a28-33a2-4cb4-a456-71054bda3755', '21647': 'd4bb9aba-30e8-44f3-a83e-d44e6f87ce37', '22448': 'e2778279-5fc4-4257-add4-3f196535e989', '22541': 'e6a71600-0e5e-4806-8d6f-8dbe013da6e5', '23156': "ace15a69-2af4-4f8d-8101-92c9e5201c84", '23156': 'ace15a69-2af4-4f8d-8101-92c9e5201c84', '23475': '4b149b33-33c0-4b95-9a7f-716dbd557b4a', '23665': '5495e224-9b18-4b28-9d92-2f335e238dee', '23701': '4e501dc2-c417-4bb4-bc97-3ba167360578', '23702': 'ca93d092-b461-4446-bc78-7b9c717bb553', '23703': '4d132cdc-0763-4e1f-8b4b-6a38a1280f81', '23704': '9c4ec32d-38e2-46c7-9450-624f8f083ded', '23705': '71badcb2-1043-431a-99b1-0bd11a9754a5', '23706': 'c06de6a6-7b03-47ea-80a7-68d4a1f3dd04', '23707': '9db686c8-5119-4eb1-9dd5-363d91db2513', '23708': '3887a770-2b44-45d0-ab08-6cf03ec0e3d8', '23709': 'a0e9e003-144e-4543-ac50-e4bf6894e6a2', '23710': '3e4fc9f9-2716-4c03-b814-4fea06392757', '23711': 'ab746a88-dbf2-4fb1-80f0-4e20a9c945f6', '23712': '5b995c5e-563e-4dee-b408-78243fff8ec0', '23713': 'fe02eab3-620c-402d-ae1d-ad564876b9af' } concatenated_string = "" json_data = unidades_dict for codigo in codigos_correspondentes: if str(codigo) in json_data: # Verificar se o código existe no JSON concatenated_string += json_data[str(codigo)] # Concatenar o valor com um espaço print(codigo) # Remover o espaço extra no final (opcional) concatenated_string = concatenated_string.strip() return concatenated_string def log_pdv(codigo_pesquisar): dimensao = pd.read_csv('Dimensao.csv',encoding='utf-8') df = dimensao codigos_correspondentes = df[(df['cpf_gr'] == codigo_pesquisar) | (df['cpf_sup'] == codigo_pesquisar)]['codigo'].tolist() unidades_dict = {'3546': 'aef41e8b-2a07-49eb-90ff-13b4a5ce6b65', '4494': '804cb2f5-be0e-46ea-81cf-5faa1bfb59f2', '4560': '2fb956b7-14ca-4320-b748-d7f3a9ea8e08', '5699': 'b215bb37-3d30-4684-8d63-006a543bf9b4', '11111': '01c5e9d5-fda9-4a7b-a57a-cf4ace4a0571', '12522': '64ea8dd9-ffde-495b-9f86-3108fab1a203', '12817': '56a47735-92e7-4317-9a86-0fcc35676a80', '12818': '32af600c-4823-487b-b254-981666086329', '12819': 'f20e7ffe-9d87-4fbc-b0ec-c49da6f43f33', '12820': '54ba626b-eee9-4e4b-8a99-cbc15c9e3490', '12823': '0c59fa2c-54d4-4f00-8952-38c9e9cda3d0', '12824': 'ff5b50cb-3268-4653-968e-293f5155b938', '12826': 'a15b0b35-bf09-4afa-8243-0469447d0ecd', '12828': '0ec5b446-bbd8-438b-ba7c-d89eb1867c9d', '12829': 'f3635215-5641-425f-80fa-44bc3041815e', '12830': '00c76705-008e-41ab-b731-f7db638b81cc', '12838': '35de9486-db2c-4449-8640-9e6e6e717c15', '13427': 'decb5cb6-1e33-4c28-a2a9-945ff520b733', '14617': '8b264cc0-cf93-4d8c-96d4-4dfb43fa5a4e', '14668': '9439d248-6ca7-49b0-8bec-365741bd1518', '19103': 'ea75000b-2d75-4eaa-af5f-37592b31c510', '20005': 'ba3c5825-bd75-471d-81be-e59dc9bdb765', '20006': 'f0169aed-0a07-4050-8952-114b1adafb50', '20007': '1a28d239-8a39-4205-ae71-305dd47a3f67', '20009': '4cd5cf69-82fd-472c-ade9-8cd924682dca', '20056': '9188a0fe-5672-4e94-abb1-7cd5564566ff', '20057': '8b2dae02-0d67-48d3-8a39-552769786b41', '20441': 'ac31866e-1106-4c41-85dd-61ab3f782a1f', '20858': '2189b870-51ab-4b5f-bf8d-2ed20324bfa2', '20968': '6462b005-be0a-4065-968c-8308ff97389d', '20969': '4c6eb12f-f16d-452f-808a-daa3d44c3cd0', '20970': '550d82e1-ab5c-496e-88dd-0a4d4174b039', '20986': 'd4c1f35b-0458-415c-ba02-e5a4a0e545e1', '20988': '72921848-e9a3-4837-9941-9fccf9ca1beb', '20989': '5b13795e-3062-4208-97de-27ed4642d097', '20991': '9939bb4a-3d8b-43c6-857d-7c2efd6541cc', '20992': '12148415-fb86-431e-89cd-7b17b27a345b', '20993': '4aeac823-6b09-4582-acb4-22f0e72735d8', '20994': '7860f680-bcc9-4d21-bf11-9dde76e2b1f1', '20995': 'a3e122bc-2060-4f80-94f5-f43be99fb7a2', '20996': '8676f6f8-a5b0-44cf-8220-06d41289fc34', '20997': '680f71f3-1d4a-4263-a91f-6fb2a08b2dda', '20998': '03cf8f3a-950c-43da-8b57-c498395fae5c', '20999': '58691507-093a-4197-a696-81367eff33ff', '21000': 'abcd6221-a26d-47bb-95e1-488602f76d51', '21001': 'eadb3fa2-36ec-4936-b840-fb9597cadbac', '21068': 'bdb2958d-7895-4c08-8dc6-7c724c1ae9c6', '21277': '6a448c7d-fe7c-41e4-b8eb-5433ac52693b', '21278': 'aedf2a6d-d982-4111-b3a4-117e46f5e9d5', '21279': 'af5f4861-581a-4d45-aeb9-c90e44f30a50', '21296': 'bbc66aa3-5d74-4405-8f58-d1b739c6a1ba', '21375': '57ff2d4a-3945-48ca-a227-533c402e3060', '21381': '2b030fe9-9285-45ef-97fb-b896337c739f', '21382': 'f0f4884b-c190-4868-85c3-8cb64872ca84', '21383': '0d9f481a-de26-47e6-8171-14f09643df57', '21495': 'f7b321d8-b043-48f2-8555-3496630e9e83', '21624': '22607a28-33a2-4cb4-a456-71054bda3755', '21647': 'd4bb9aba-30e8-44f3-a83e-d44e6f87ce37', '22448': 'e2778279-5fc4-4257-add4-3f196535e989', '22541': 'e6a71600-0e5e-4806-8d6f-8dbe013da6e5', '23156': "ace15a69-2af4-4f8d-8101-92c9e5201c84", '23156': 'ace15a69-2af4-4f8d-8101-92c9e5201c84', '23475': '4b149b33-33c0-4b95-9a7f-716dbd557b4a', '23665': '5495e224-9b18-4b28-9d92-2f335e238dee', '23701': '4e501dc2-c417-4bb4-bc97-3ba167360578', '23702': 'ca93d092-b461-4446-bc78-7b9c717bb553', '23703': '4d132cdc-0763-4e1f-8b4b-6a38a1280f81', '23704': '9c4ec32d-38e2-46c7-9450-624f8f083ded', '23705': '71badcb2-1043-431a-99b1-0bd11a9754a5', '23706': 'c06de6a6-7b03-47ea-80a7-68d4a1f3dd04', '23707': '9db686c8-5119-4eb1-9dd5-363d91db2513', '23708': '3887a770-2b44-45d0-ab08-6cf03ec0e3d8', '23709': 'a0e9e003-144e-4543-ac50-e4bf6894e6a2', '23710': '3e4fc9f9-2716-4c03-b814-4fea06392757', '23711': 'ab746a88-dbf2-4fb1-80f0-4e20a9c945f6', '23712': '5b995c5e-563e-4dee-b408-78243fff8ec0', '23713': 'fe02eab3-620c-402d-ae1d-ad564876b9af' } concatenated_string = "" json_data = unidades_dict for codigo in codigos_correspondentes: if str(codigo) in json_data: # Verificar se o código existe no JSON concatenated_string += json_data[str(codigo)] # Concatenar o valor com um espaço print(codigo) def encode_url(texto): return ''.join('%{:02X}'.format(ord(char)) if char not in 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.-' else char for char in texto) def gerar_session_id(marca, relatorio): # URL do endpoint url = '', '' if marca == 'BOT': url = 'https://livers.e-boticario.com.br/ReportServer/ReportExecution2005.asmx' payload = f''' {relatorio} ''' headers = { "Content-Type": "text/xml; charset=utf-8", "SOAPAction": "http://schemas.microsoft.com/sqlserver/2005/06/30/reporting/reportingservices/LoadReport", "Authorization": "Basic ZG16XExpdmVSZXBvcnQ6MUVrd1BPRC9VMURrL0YxS2YrVVM=" } response = requests.request("POST", url, data=payload, headers=headers) if response.status_code == 200: response_xml = response.text # Analisar o XML root = ET.fromstring(response_xml) # Encontrar o elemento ExecutionID execution_id_element = root.find( './/{http://schemas.microsoft.com/sqlserver/2005/06/30/reporting/reportingservices}ExecutionID') # Obter o valor do elemento ExecutionID execution_id = execution_id_element.text # print("ExecutionID:", execution_id) print(f'gerar sessão: {response.status_code} - {execution_id}') # print(response.text) return execution_id else: return def carregar_relatorio(marca, execution_id, cpf, data_inicial, data_final): pdvs = busca_pdv(cpf) url, host = '', '' if marca == 'QDB': # url = 'http://rs.quemdisseberenice.com.br/ReportServer/ReportExecution2005.asmx' url = 'rs.quemdisseberenice.com.br' owner_id = 'ea16f59c-ddec-4e6e-aea1-a220cbf0eaba' elif marca == 'BOT': # url = 'http://livers.e-boticario.com.br/ReportServer/ReportExecution2005.asmx' url = 'livers.e-boticario.com.br' owner_id = '3861c9c1-600b-4f7a-86b1-9bf685a6d041' endpoint = '/ReportServer/ReportExecution2005.asmx' payload = f'''
{execution_id}
tipo 3 situacao -1 interesse 2 titulorelatorio Resumo de Vendas filtrorelatorio de 15/01/2025 até 15/01/2025; Lojas: 4494-COMERCIO-PONTA VERDE-MACEIO, 4560-COMERCIO-MACEIO SHOP TERREO-AL, 5699-COMERCIO -MOREIRA LIMA; Produtos: Todos; Vendedor: Todos; Tipo Relatório: Por Vendedor; Situação: Todos; Interesse: Todos; Classificadores: Todos; lojarelatorio CENTRAL 9004494 unidadesnegocio {pdvs} datainicial {data_inicial} 00:00:00 datafinal {data_final} 23:59:59 ownerid 3861c9c1-600b-4f7a-86b1-9bf685a6d041 pt-BR
''' headers = { "Content-Type": "text/xml; charset=utf-8", "SOAPAction": 'http://schemas.microsoft.com/sqlserver/2005/06/30/reporting/reportingservices/SetExecutionParameters', "Authorization": "Basic ZG16XExpdmVSZXBvcnQ6MUVrd1BPRC9VMURrL0YxS2YrVVM=", } # Enable debugging for http.client # http.client.HTTPSConnection.debuglevel = 1 # connection = http.client.HTTPSConnection(url) connection = http.client.HTTPSConnection(url) connection.request("POST", endpoint, body=payload, headers=headers) response = connection.getresponse() # Enable debugging for http.client # http.client.HTTPSConnection.debuglevel = 0 print(f'setar parametros: {response.status}') def gerar_csv(marca, execution_id, relatorio, cpf): url, host = '', '' if marca == 'QDB': url = 'http://rs.quemdisseberenice.com.br/ReportServer?' host = 'rs.quemdisseberenice.com.br' elif marca == 'BOT': url = 'https://livers.e-boticario.com.br/ReportServer?' host = 'livers.e-boticario.com.br' querystring = f'{encode_url(relatorio)}&rs:SessionID={execution_id}&rs:command=Render&rs:Format=CSV&rc:Toolbar=false&rs:ErrorResponseAsXml=true&rs:AllowNewSessions=false%20HTTP/1.1' headers = { 'Accept-Language': 'pt-BR', 'Authorization': 'Basic ZG16XExpdmVSZXBvcnQ6MUVrd1BPRC9VMURrL0YxS2YrVVM=', 'Host': f'{host}' } response = requests.request("GET", f'{url}{querystring}', headers=headers) # print(url, querystring, sep='') # print(f'gerar csv: {response.status_code}') response_data = response.text # print(response_data[:100]) # Separar mês e ano da string de data # inicio = data_inicial.find('-') + 1 # fim = data_inicial.find('-', inicio) #ano = datetime.today().date().strftime('%Y') #numero_mes = datetime.today().date().strftime('%m') csv_file = StringIO(response_data) reader = csv.reader(csv_file) # output_file = fr'../csv_base/brindes/{numero_mes}.{numero_ano}_{cod_unidade}_brindes_{marca}.csv' # output_file = fr'../csv_base/posicao_de_estoque_na_data/posicao estoque_{data}_{cod_unidade}.csv' output_file = fr'temp\{cpf}.csv' # output_file = fr'../csv_base/brindes/{numero_mes}.csv' # Escrever os dados no arquivo CSV with open(output_file, mode='w', newline='', encoding='utf-8') as file: writer = csv.writer(file) # Escrever o cabeçalho writer.writerow(next(reader)) # Escrever as linhas de dados for row in reader: writer.writerow(row) print(f'Dados foram gravados em "{output_file}"') def exec (cpf, data_inicial, data_final): relatorio = '/Vendas/vendas14ResumoVendas' exec_id = gerar_session_id('BOT', relatorio) MAX_RETRIES = 10 retry_count = 0 while True: # Loop para tentar novamente em caso de falha try: carregar_relatorio('BOT', exec_id, cpf, data_inicial, data_final) gerar_csv('BOT', exec_id, relatorio, cpf) # print(cod_unidade, unidade, sep='\t') break # Se chegou até aqui sem lançar exceções, pode sair do loop except requests.exceptions.ConnectionError as e: if retry_count == MAX_RETRIES: raise e else: print(f"Erro de conexão: {e}") time.sleep(5) retry_count += 1 except ConnectionResetError as e: if retry_count == MAX_RETRIES: raise e else: print(f"Erro de conexão: {e}") time.sleep(5) retry_count += 1 consulta = pd.read_csv(fr'temp\{cpf}.csv', encoding='ISO-8859-1') realizado_formatado = consulta['textbox56'] boleto_medio = consulta['textbox86'] realizado = (float(realizado_formatado.iloc[0].replace('.', '').replace(',', '.'))) boleto_formato = (float(boleto_medio.iloc[0].replace('.', '').replace(',', '.'))) mensagem = f"Aqui está o seu realizado deste mês: R$ {realizado} e o boleto médio foi R$ {boleto_formato}" return mensagem ''' hoje = datetime.today() primeiro_dia_mes = hoje.replace(day=1) hoje_formatado = hoje.strftime('%Y-%m-%d') primeiro_dia_formatado = primeiro_dia_mes.strftime('%Y-%m-%d') realizado = exec(736514511, primeiro_dia_formatado, hoje_formatado) print(hoje) '''