refactor: update datepicker interaction, improve destination selection via select2, and add dynamic payment method handling, apos primeiro teste preliminar

This commit is contained in:
2026-03-29 02:12:51 -04:00
parent ca340bbcdf
commit a670bfdca4

View File

@@ -335,19 +335,15 @@ class SigaAutomation:
await page.get_by_role("link", name="Saída").click()
await page.wait_for_timeout(1500)
# 2. Datepicker
# 2. Datepicker — clicar no input #f_dataemissao para abrir calendário
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
# 3. Destinatário (origem já implícita no contexto da localidade)
await self._fill_destination()
await page.wait_for_timeout(1000)
# 5. Inserir produtos
# 4. Inserir produtos
successful_ids: List[int] = []
failed_products: List[AggregatedProduct] = []
@@ -358,7 +354,7 @@ class SigaAutomation:
else:
failed_products.append(product)
# 6. Se todos falharam → cancelar
# 5. Se todos falharam → cancelar
if not successful_ids:
logger.error("❌ Todos produtos falharam! Cancelando SAÍDA...")
await self._cancel_saida()
@@ -368,10 +364,10 @@ class SigaAutomation:
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()
# 6. Ler valor calculado e preencher campo de pagamento
await self._fill_payment_value(group.meio_pag)
# 8. Confirmar
# 7. Confirmar
await self._confirm()
logger.info(f"✅ SAÍDA confirmada! IDs: {successful_ids}")
return successful_ids
@@ -379,7 +375,14 @@ class SigaAutomation:
# ---------- AÇÕES INTERNAS ----------
async def _select_date(self, day: int):
"""Clica no input de data para abrir o datepicker Bootstrap e seleciona o dia."""
page = self.page
# Clicar no input de data para abrir o calendário
date_input = page.locator("#f_dataemissao")
await date_input.click()
await page.wait_for_timeout(500)
try:
dp = page.locator(".datepicker-days")
await dp.wait_for(state="visible", timeout=5000)
@@ -397,23 +400,23 @@ class SigaAutomation:
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):
"""Preenche o destinatário via select2 (#s2id_f_pessoa)."""
page = self.page
dest = page.get_by_role("combobox", name="Destinatário *")
await dest.fill(Config.SIGA_DEST_CODE)
await dest.press("Enter")
# Clicar no select2 para abrir o dropdown
await page.locator("#s2id_f_pessoa .select2-choice").click()
await page.wait_for_timeout(500)
# Digitar o código no campo de busca
search = page.locator("#select2-drop input.select2-input")
await search.fill(Config.SIGA_DEST_CODE)
await page.wait_for_timeout(1500)
await page.get_by_text("AVENIDA MINAS GERAIS, 594 -").click()
# Selecionar o resultado que contém o código
result = page.locator("#select2-drop .select2-results li").first
await result.wait_for(state="visible", timeout=5000)
await result.click()
await page.wait_for_timeout(500)
logger.info(f"📍 Destinatário: {Config.SIGA_DEST_CODE}")
@@ -508,24 +511,34 @@ class SigaAutomation:
await page.wait_for_timeout(2000)
logger.info("🚫 SAÍDA cancelada.")
async def _fill_cash_value(self):
async def _fill_payment_value(self, meio_pag: str):
"""Lê o valor total dos produtos e preenche o campo correto (Dinheiro ou PIX)."""
page = self.page
el = page.locator("#f_valoratual")
# Tenta ler como input primeiro, fallback para inner_text
# Ler o valor total dos produtos calculado pelo sistema
try:
valor = await el.input_value()
valor_produtos = await page.locator("#f_valorproduto").input_value()
except Exception:
valor = (await el.inner_text()).strip()
valor_produtos = "0,00"
if not valor:
logger.warning("⚠️ #f_valoratual vazio!")
valor = "0"
if not valor_produtos or valor_produtos.strip() == "":
valor_produtos = "0,00"
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}")
if meio_pag == "DINHEIRO":
field = page.locator("#f_valorcaixa")
await field.click()
await field.fill(valor_produtos)
logger.info(f"💰 Valor em Dinheiro no Caixa: {valor_produtos}")
elif meio_pag == "PIX":
field = page.locator("#f_valorQrcodePix")
await field.click()
await field.fill(valor_produtos)
logger.info(f"💰 Pagto QRCode/PIX: {valor_produtos}")
else:
field = page.locator("#f_valorcaixa")
await field.click()
await field.fill(valor_produtos)
logger.info(f"💰 Valor (fallback dinheiro): {valor_produtos}")
async def _confirm(self):
page = self.page