# -- coding: utf-8 -- """ Fundo Bíblico RPA - Automação de Lançamentos no SIGA Lê vendas do Google Sheets, agrupa por dia+meio de pagamento, e lança como SAÍDAs no sistema SIGA via Playwright. """ import asyncio import logging import os from collections import defaultdict from dataclasses import dataclass, field from datetime import datetime, date, timedelta from typing import List, Set, Optional from dotenv import load_dotenv import gspread from google.oauth2.service_account import Credentials from playwright.async_api import async_playwright, Page, TimeoutError as PlaywrightTimeout # Logging logging.basicConfig( level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s", ) logger = logging.getLogger(__name__) # ============================================================================== # CONFIGURAÇÃO # ============================================================================== class Config: """Centraliza configurações e variáveis de ambiente.""" SIGA_URL = "https://siga.congregacao.org.br/index.aspx" SPREADSHEET_ID = "1tuOfcMhiUa-VKT1COWtUV4QB6DgUOxzyOx-LJGIHH1A" VENDAS_TAB = "Vendas" LANCAMENTOS_TAB = "SIGA" GOOGLE_CREDENTIALS_FILE: str = "" SIGA_ORIGIN_CODE: str = "0231" SIGA_DEST_CODE: str = "0231" @classmethod def load(cls): load_dotenv() cls.GOOGLE_CREDENTIALS_FILE = os.getenv("GOOGLE_CREDENTIALS_FILE", "credentials.json") cls.SIGA_ORIGIN_CODE = os.getenv("SIGA_ORIGIN_CODE", "0231") cls.SIGA_DEST_CODE = os.getenv("SIGA_DEST_CODE", "0231") if not os.path.exists(cls.GOOGLE_CREDENTIALS_FILE): raise FileNotFoundError( f"Arquivo de credenciais não encontrado: {cls.GOOGLE_CREDENTIALS_FILE}\n" "Configure GOOGLE_CREDENTIALS_FILE no .env" ) # ============================================================================== # DTOs # ============================================================================== @dataclass class SaleItem: """Uma venda individual da planilha.""" id: int data_venda: date item_code: str quantidade: int valor_final: float meio_pag: str @dataclass class AggregatedProduct: """Produto agregado (mesmo código) dentro de um grupo.""" code: str total_qty: int total_valor: float sale_ids: List[int] @dataclass class SaleGroup: """Grupo de vendas agrupado por data + meio de pagamento.""" data_venda: date meio_pag: str items: List[SaleItem] = field(default_factory=list) @property def total_valor(self) -> float: return sum(i.valor_final for i in self.items) @property def all_ids(self) -> List[int]: return [i.id for i in self.items] def get_aggregated_products(self) -> List[AggregatedProduct]: agg: dict[str, AggregatedProduct] = {} for item in self.items: if item.item_code not in agg: agg[item.item_code] = AggregatedProduct( code=item.item_code, total_qty=0, total_valor=0.0, sale_ids=[] ) agg[item.item_code].total_qty += item.quantidade agg[item.item_code].total_valor += item.valor_final agg[item.item_code].sale_ids.append(item.id) return list(agg.values()) # ============================================================================== # UTILITÁRIOS # ============================================================================== def parse_date(date_val) -> Optional[date]: """Converte diversos formatos de data do Google Sheets para date.""" if date_val is None: return None if isinstance(date_val, (int, float)): return (datetime(1899, 12, 30) + timedelta(days=int(date_val))).date() date_str = str(date_val).strip() if not date_str: return None for fmt in ( "%Y-%m-%dT%H:%M:%S", "%Y-%m-%d", "%d/%m/%Y", "%m/%d/%Y", "%d/%m/%Y %H:%M:%S", "%m/%d/%Y %H:%M:%S", ): try: return datetime.strptime(date_str.split(".")[0], fmt).date() except ValueError: continue return None # ============================================================================== # GOOGLE SHEETS SERVICE # ============================================================================== class GoogleSheetsService: """Leitura/escrita no Google Sheets via Service Account.""" def __init__(self, credentials_file: str, spreadsheet_id: str): scopes = [ "https://www.googleapis.com/auth/spreadsheets", "https://www.googleapis.com/auth/drive.readonly", ] creds = Credentials.from_service_account_file(credentials_file, scopes=scopes) gc = gspread.authorize(creds) self.spreadsheet = gc.open_by_key(spreadsheet_id) self.vendas_ws = self.spreadsheet.worksheet(Config.VENDAS_TAB) self.lancamentos_ws = self.spreadsheet.worksheet(Config.LANCAMENTOS_TAB) logger.info("✅ Conectado ao Google Sheets.") def read_vendas(self, target_month: int, target_year: int) -> List[SaleItem]: records = self.vendas_ws.get_all_records() sales: List[SaleItem] = [] for rec in records: try: dt = parse_date(rec.get("DataVenda")) if dt is None or dt.month != target_month or dt.year != target_year: continue sales.append( SaleItem( id=int(rec["ID"]), data_venda=dt, item_code=str(rec["Item"]).strip(), quantidade=int(rec["Qtd"]), valor_final=float(rec["ValorFinal"]), meio_pag=str(rec["MeioPag"]).strip().upper(), ) ) except Exception as e: logger.warning(f"Erro ao parsear registro: {rec} → {e}") logger.info(f"📊 {len(sales)} vendas do mês {target_month:02d}/{target_year}.") return sales def read_lancamentos(self) -> Set[int]: records = self.lancamentos_ws.get_all_records() launched = { int(r["ID"]) for r in records if str(r.get("LancadoSiga", "")).upper() == "SIM" } logger.info(f"📋 {len(launched)} IDs já lançados.") return launched def mark_as_launched(self, ids: List[int]): timestamp = datetime.now().strftime("%d/%m/%Y %H:%M:%S") rows = [[id_val, "SIM", timestamp] for id_val in ids] self.lancamentos_ws.append_rows(rows, value_input_option="USER_ENTERED") logger.info(f"✅ Marcados como lançados: {ids}") # ============================================================================== # AGRUPAMENTO # ============================================================================== def group_sales(sales: List[SaleItem], launched_ids: Set[int]) -> List[SaleGroup]: pending = [s for s in sales if s.id not in launched_ids] if not pending: logger.info("✅ Todas as vendas já foram lançadas!") return [] logger.info(f"📦 {len(pending)} vendas pendentes.") groups_dict: dict[tuple, list] = defaultdict(list) for sale in pending: groups_dict[(sale.data_venda, sale.meio_pag)].append(sale) groups = [ SaleGroup(data_venda=k[0], meio_pag=k[1], items=v) for k, v in sorted(groups_dict.items()) ] for g in groups: prods = ", ".join(f"{p.code}x{p.total_qty}" for p in g.get_aggregated_products()) logger.info(f" 📅 {g.data_venda} | {g.meio_pag} | {prods} | R${g.total_valor:.2f}") return groups # ============================================================================== # AUTOMAÇÃO SIGA # ============================================================================== class SigaAutomation: """Automação do sistema SIGA via Playwright.""" TIMEOUT = 30_000 AUTH_TIMEOUT = 3_600_000 # 1 hora def __init__(self): self.playwright = None self.browser = None self.context = None self.page: Optional[Page] = None async def launch(self): self.playwright = await async_playwright().start() self.browser = await self.playwright.chromium.launch(headless=False) self.context = await self.browser.new_context() self.page = await self.context.new_page() self.page.set_default_timeout(self.TIMEOUT) logger.info("🌐 Navegador iniciado.") async def close(self): if self.context: await self.context.close() if self.browser: await self.browser.close() if self.playwright: await self.playwright.stop() logger.info("🌐 Navegador fechado.") # ---------- AUTENTICAÇÃO ---------- async def navigate_and_wait_auth(self): page = self.page await page.goto(Config.SIGA_URL) logger.info("⏳ Aguardando login manual + reCAPTCHA...") logger.info(" → Faça login no navegador e resolva o captcha.") await page.locator( "a:has-text('PRIMAVERA DO LESTE DL -'), " "a:has-text('BR 09-0231 - PRIMAVERA DO')" ).first.wait_for(state="visible", timeout=self.AUTH_TIMEOUT) logger.info("✅ Login detectado!") async def check_session(self): """Se sessão expirou, aguarda re-login e re-navega.""" try: login_field = self.page.get_by_role("textbox", name="Email/Usuário:") if await login_field.is_visible(timeout=2000): logger.warning("⚠️ Sessão expirada! Aguardando re-login...") await self.navigate_and_wait_auth() await self.navigate_to_fundo_biblico() await self.validate_and_enter_month() except PlaywrightTimeout: pass # ---------- NAVEGAÇÃO ---------- async def navigate_to_fundo_biblico(self): page = self.page link1 = page.get_by_role("link", name=" PRIMAVERA DO LESTE DL -") if await link1.is_visible(timeout=5000): await link1.click() await page.wait_for_timeout(1500) link2 = page.get_by_role("link", name="BR 09-0231 - PRIMAVERA DO") if await link2.is_visible(timeout=5000): await link2.click() await page.wait_for_timeout(1500) await page.get_by_role("link", name=" Fundo Bíblico").click() await page.wait_for_timeout(2000) logger.info("📂 Navegou para Fundo Bíblico.") async def validate_and_enter_month(self): page = self.page now = datetime.now() month_text = f"Mês de Trabalho {now.month:02d}/{now.year}" month_link = page.get_by_role("link", name=month_text) try: await month_link.wait_for(state="visible", timeout=10000) await month_link.click() await page.wait_for_timeout(2000) logger.info(f"📅 Entrou no {month_text}") except PlaywrightTimeout: raise RuntimeError( f"❌ '{month_text}' não encontrado! " "Certifique-se de que o mês atual está criado no SIGA." ) # ---------- LANÇAMENTO DE SAÍDA ---------- async def process_group(self, group: SaleGroup) -> List[int]: """Cria uma SAÍDA para o grupo. Retorna IDs lançados com sucesso.""" page = self.page products = group.get_aggregated_products() logger.info(f"\n{'='*60}") logger.info(f"📦 SAÍDA: {group.data_venda} | {group.meio_pag} | {len(products)} produto(s)") logger.info(f"{'='*60}") await self.check_session() # 1. Novo → Saída await page.get_by_role("button", name=" Novo ").click() await page.wait_for_timeout(1000) await page.get_by_role("link", name="Saída").click() await page.wait_for_timeout(1500) # 2. Datepicker await self._select_date(group.data_venda.day) await page.wait_for_timeout(1500) # 3. Origem await self._select_origin() await page.wait_for_timeout(1000) # 4. Destinatário await self._fill_destination() await page.wait_for_timeout(1000) # 5. Inserir produtos successful_ids: List[int] = [] failed_products: List[AggregatedProduct] = [] for product in products: ok = await self._add_product(product.code, product.total_qty) if ok: successful_ids.extend(product.sale_ids) else: failed_products.append(product) # 6. Se todos falharam → cancelar if not successful_ids: logger.error("❌ Todos produtos falharam! Cancelando SAÍDA...") await self._cancel_saida() return [] if failed_products: codes = [p.code for p in failed_products] logger.warning(f"⚠️ Produtos não encontrados: {codes}") # 7. Ler valor calculado e preencher campo de dinheiro await self._fill_cash_value() # 8. Confirmar await self._confirm() logger.info(f"✅ SAÍDA confirmada! IDs: {successful_ids}") return successful_ids # ---------- AÇÕES INTERNAS ---------- async def _select_date(self, day: int): page = self.page try: dp = page.locator(".datepicker-days") await dp.wait_for(state="visible", timeout=5000) cells = dp.locator("td.day:not(.old):not(.new)") count = await cells.count() for i in range(count): text = (await cells.nth(i).inner_text()).strip() if text == str(day): await cells.nth(i).click() logger.info(f"📅 Dia {day} selecionado.") return raise RuntimeError(f"Dia {day} não encontrado no datepicker!") except PlaywrightTimeout: logger.warning("⚠️ Datepicker não apareceu, continuando...") async def _select_origin(self): page = self.page await page.get_by_role("cell", name="1", exact=True).first.click() await page.wait_for_timeout(500) await page.get_by_role( "link", name="BR 09-0231 - PRIMAVERA DO LESTE - CENTRO" ).click() await page.wait_for_timeout(1000) logger.info("📍 Origem selecionada.") async def _fill_destination(self): page = self.page dest = page.get_by_role("combobox", name="Destinatário *") await dest.fill(Config.SIGA_DEST_CODE) await dest.press("Enter") await page.wait_for_timeout(1500) await page.get_by_text("AVENIDA MINAS GERAIS, 594 -").click() await page.wait_for_timeout(500) logger.info(f"📍 Destinatário: {Config.SIGA_DEST_CODE}") async def _add_product(self, code: str, qty: int) -> bool: """Insere um produto na SAÍDA. Retorna True se OK.""" page = self.page try: # Abrir dropdown await page.get_by_role("link", name="* Selecione * ").click() await page.wait_for_timeout(500) # Buscar produto search = page.get_by_label("", exact=True) await search.fill(code) await search.press("Enter") await page.wait_for_timeout(2000) # Selecionar produto exato selected = await self._select_exact_product(code) if not selected: logger.warning(f" ❌ Produto '{code}' não encontrado no catálogo.") await page.keyboard.press("Escape") await page.wait_for_timeout(500) return False await page.wait_for_timeout(500) # Preencher quantidade qty_field = page.locator("#f_quantidade") await qty_field.click() await qty_field.fill(str(qty)) # Inserir await page.locator("#btn-inserir-produto").click() await page.wait_for_timeout(1500) logger.info(f" ✅ {code} x{qty} inserido.") return True except Exception as e: logger.error(f" ❌ Erro ao inserir '{code}': {e}") try: await page.keyboard.press("Escape") except Exception: pass return False async def _select_exact_product(self, code: str) -> bool: """Tenta selecionar o produto exato no dropdown select2.""" page = self.page code_upper = code.strip().upper() # Estratégia 1: texto exato try: exact = page.locator("#select2-drop").get_by_text(code, exact=True) if await exact.count() > 0 and await exact.first.is_visible(timeout=2000): await exact.first.click() return True except Exception: pass # Estratégia 2: opção que começa com o código try: results = page.locator("#select2-drop .select2-results li") count = await results.count() for i in range(count): text = (await results.nth(i).inner_text()).strip().upper() if text.startswith(code_upper + " ") or text == code_upper: await results.nth(i).click() return True except Exception: pass # Estratégia 3: role option try: options = page.get_by_role("option") count = await options.count() for i in range(count): text = (await options.nth(i).inner_text()).strip().upper() if text.startswith(code_upper): await options.nth(i).click() return True except Exception: pass return False async def _cancel_saida(self): page = self.page cancel = page.locator("button.btn.btn-link.no-border", has_text="Cancelar") await cancel.click() await page.wait_for_timeout(2000) logger.info("🚫 SAÍDA cancelada.") async def _fill_cash_value(self): page = self.page el = page.locator("#f_valoratual") # Tenta ler como input primeiro, fallback para inner_text try: valor = await el.input_value() except Exception: valor = (await el.inner_text()).strip() if not valor: logger.warning("⚠️ #f_valoratual vazio!") valor = "0" cash_field = page.get_by_role("textbox", name="Valor em Dinheiro no Caixa *") await cash_field.click() await cash_field.fill(valor) logger.info(f"💰 Valor em Dinheiro no Caixa: {valor}") async def _confirm(self): page = self.page await page.get_by_role("button", name=" Confirmar").click() await page.wait_for_timeout(2000) await page.get_by_role("button", name="Ok").click() await page.wait_for_timeout(2000) # ============================================================================== # APLICAÇÃO # ============================================================================== class Application: """Orquestrador principal.""" def __init__(self): Config.load() async def run(self): now = datetime.now() month, year = now.month, now.year logger.info(f"\n{'='*60}") logger.info(f"🚀 FUNDO BÍBLICO RPA") logger.info(f"📅 Mês: {month:02d}/{year}") logger.info(f"{'='*60}\n") # 1. Ler planilhas sheets = GoogleSheetsService(Config.GOOGLE_CREDENTIALS_FILE, Config.SPREADSHEET_ID) vendas = sheets.read_vendas(month, year) launched_ids = sheets.read_lancamentos() # 2. Agrupar vendas pendentes groups = group_sales(vendas, launched_ids) if not groups: return # 3. Automação SIGA siga = SigaAutomation() try: await siga.launch() await siga.navigate_and_wait_auth() await siga.navigate_to_fundo_biblico() await siga.validate_and_enter_month() total_ok: List[int] = [] total_fail: List[int] = [] for i, group in enumerate(groups, 1): logger.info(f"\n🔄 Grupo {i}/{len(groups)}") ids_ok = await siga.process_group(group) if ids_ok: total_ok.extend(ids_ok) sheets.mark_as_launched(ids_ok) else: total_fail.extend(group.all_ids) # Resumo logger.info(f"\n{'='*60}") logger.info(f"📊 RESUMO FINAL") logger.info(f" ✅ Lançados: {len(total_ok)} vendas") logger.info(f" ❌ Falhas: {len(total_fail)} vendas") if total_fail: logger.info(f" IDs falhos: {total_fail}") logger.info(f"{'='*60}") except Exception as e: logger.critical(f"💥 Erro crítico: {e}") raise finally: await siga.close() # ============================================================================== # MAIN # ============================================================================== if __name__ == "__main__": asyncio.run(Application().run())