Milestone ultima alpha
This commit is contained in:
@@ -254,92 +254,49 @@ async def _execute(db, sql: str, params: Dict[str, Any]) -> int:
|
||||
|
||||
|
||||
@_log_call()
|
||||
async def sp_xExePackingListPallet_async(db, IDOperatore: int, Documento: str) -> SPResult:
|
||||
"""Toggle the reservation state of all cells belonging to a packing list.
|
||||
|
||||
The implementation mirrors the original SQL stored procedure while using
|
||||
the shared async DB client already managed by the application.
|
||||
"""
|
||||
async def sp_xExePackingListPallet_async(db, IDOperatore: int, Documento: str, Azione: str = "P") -> SPResult:
|
||||
"""Execute the original reservation stored procedure used by the C# client."""
|
||||
try:
|
||||
_MODULE_LOGGER.log(_MODULE_LOG_LEVEL, f"Procedura async packing list avviata documento={Documento} id_operatore={IDOperatore}")
|
||||
nominativo = await _query_one_value(
|
||||
db,
|
||||
"SELECT LOGIN FROM Operatori WHERE id = :IDOperatore",
|
||||
{"IDOperatore": IDOperatore},
|
||||
) or ""
|
||||
|
||||
celle = await _query_all(
|
||||
db,
|
||||
"""
|
||||
SELECT DISTINCT Cella
|
||||
FROM dbo.XMag_ViewPackingList
|
||||
WHERE Documento = :Documento
|
||||
""",
|
||||
{"Documento": Documento},
|
||||
azione = str(Azione or "P").strip().upper()
|
||||
if azione not in ("P", "S"):
|
||||
return SPResult(rc=-10, message=f"Azione non valida: {Azione}", id_result=None)
|
||||
_MODULE_LOGGER.log(
|
||||
_MODULE_LOG_LEVEL,
|
||||
f"Procedura packing list via stored procedure documento={Documento} azione={azione} id_operatore={IDOperatore}",
|
||||
)
|
||||
id_celle = [row.get("Cella") for row in celle if "Cella" in row]
|
||||
_MODULE_LOGGER.log(_MODULE_LOG_LEVEL, f"Celle coinvolte per documento={Documento}: {len(id_celle)}")
|
||||
sql = """
|
||||
SET NOCOUNT ON;
|
||||
DECLARE @RC int = 0;
|
||||
|
||||
# Each cell is toggled individually because the original procedure also
|
||||
# updates metadata such as operator and timestamp per row.
|
||||
for id_cella in id_celle:
|
||||
if id_cella is None:
|
||||
continue
|
||||
stato = await _query_one_value(
|
||||
db,
|
||||
"SELECT IDStato FROM Celle WHERE ID = :IDC",
|
||||
{"IDC": id_cella},
|
||||
)
|
||||
_MODULE_LOGGER.debug(f"Toggling cella id={id_cella} stato_corrente={stato}")
|
||||
if stato == 0:
|
||||
await _execute(
|
||||
db,
|
||||
"""
|
||||
UPDATE Celle
|
||||
SET IDStato = 1,
|
||||
ModUtente = :N,
|
||||
ModDataOra = GETDATE()
|
||||
WHERE ID = :IDC
|
||||
""",
|
||||
{"N": nominativo, "IDC": id_cella},
|
||||
)
|
||||
else:
|
||||
await _execute(
|
||||
db,
|
||||
"""
|
||||
UPDATE Celle
|
||||
SET IDStato = 0,
|
||||
ModUtente = :N,
|
||||
ModDataOra = GETDATE()
|
||||
WHERE ID = :IDC
|
||||
""",
|
||||
{"N": nominativo, "IDC": id_cella},
|
||||
)
|
||||
EXEC dbo.sp_xExePackingListPallet
|
||||
@IDOperatore = :IDOperatore,
|
||||
@Documento = :Documento,
|
||||
@Azione = :Azione,
|
||||
@RC = @RC OUTPUT;
|
||||
|
||||
description = await _query_one_value(
|
||||
db,
|
||||
"""
|
||||
SELECT TOP 1 NAZIONE
|
||||
FROM dbo.XMag_ViewPackingList
|
||||
WHERE Documento = :Documento
|
||||
GROUP BY Documento, NAZIONE
|
||||
ORDER BY NAZIONE
|
||||
""",
|
||||
{"Documento": Documento},
|
||||
SELECT CAST(@RC AS int) AS RC;
|
||||
"""
|
||||
_log_sql("sp_xExePackingListPallet", sql, {"IDOperatore": IDOperatore, "Documento": Documento, "Azione": azione})
|
||||
if not hasattr(db, "query_json"):
|
||||
raise RuntimeError("Il client DB non espone query_json necessario per eseguire la stored procedure.")
|
||||
res = await db.query_json(
|
||||
sql,
|
||||
{"IDOperatore": IDOperatore, "Documento": Documento, "Azione": azione},
|
||||
as_dict_rows=True,
|
||||
commit=True,
|
||||
)
|
||||
|
||||
await _execute(
|
||||
db,
|
||||
"""
|
||||
INSERT INTO dbo.LogPackingList (Code, Description, IDInsUser, InsDateTime)
|
||||
VALUES (:Code, :Descr, :IDInsUser, GETDATE());
|
||||
""",
|
||||
{"Code": Documento, "Descr": description, "IDInsUser": IDOperatore},
|
||||
)
|
||||
|
||||
new_id = await _query_one_value(db, "SELECT SCOPE_IDENTITY() AS ID", {})
|
||||
_MODULE_LOGGER.log(_MODULE_LOG_LEVEL, f"Procedura completata documento={Documento} id_result={new_id}")
|
||||
return SPResult(rc=0, message="", id_result=int(new_id) if new_id is not None else None)
|
||||
rows = []
|
||||
if isinstance(res, dict):
|
||||
rows = res.get("rows", []) or []
|
||||
_log_dataset("sp_xExePackingListPallet", rows)
|
||||
rc = 0
|
||||
if rows and isinstance(rows[0], dict):
|
||||
try:
|
||||
rc = int(rows[0].get("RC") or 0)
|
||||
except Exception:
|
||||
rc = 0
|
||||
_MODULE_LOGGER.log(_MODULE_LOG_LEVEL, f"Stored procedure completata documento={Documento} azione={azione} rc={rc}")
|
||||
return SPResult(rc=rc, message="", id_result=None)
|
||||
except Exception as exc:
|
||||
_MODULE_LOGGER.exception(f"Procedura fallita documento={Documento}: {exc}")
|
||||
_MODULE_LOGGER.exception(f"Procedura fallita documento={Documento} azione={Azione}: {exc}")
|
||||
return SPResult(rc=-1, message=str(exc), id_result=None)
|
||||
|
||||
Reference in New Issue
Block a user