This commit is contained in:
2026-01-11 16:42:30 +01:00
parent 81ede1c7f4
commit def61f18a2

77
app.py
View File

@@ -58,47 +58,66 @@ file_handler.setFormatter(logging.Formatter(
class AHKParser: class AHKParser:
lab = "AHK" lab = "AHK"
def g(self, pat, txt): def grab(self, text, labels):
m = re.search(pat, txt, re.I | re.S) lines = [l.strip() for l in text.splitlines() if l.strip()]
return m.group(1).strip() if m else None idx = [i for i,l in enumerate(lines) if l in labels]
if not idx:
return {}
values = []
start = idx[-1] + 1
for l in lines[start:]:
if l.startswith(":"):
values.append(l[1:].strip())
if len(values) == len(labels):
break
return dict(zip(labels, values))
def parse(self, text): def parse(self, text):
r = empty_weight_report("AHK") r = empty_weight_report("AHK")
# ---------- report ---------- # report
r["report"]["reference"] = self.g(r"(AHK\s*/[A-Z0-9/]+)", text) r["report"]["reference"] = safe_search(r"(AHK\s*/[A-Z0-9/]+)", text)
r["report"]["date"] = self.g(r"Produced On\s*([0-9]{1,2}\s+[A-Za-z]+\s+20\d{2})", text) r["report"]["date"] = safe_search(r"Produced On\s*([0-9A-Za-z ]+)", text)
# ---------- contract ---------- # contract
r["contract"]["invoice_no"] = self.g(r"Client Reference:\s*([A-Z0-9\- /]+)", text) r["contract"]["invoice_no"] = safe_search(r"Client Reference:\s*([A-Z0-9\- /]+)", text)
r["contract"]["commodity"] = "Raw Cotton" r["contract"]["commodity"] = "Raw Cotton"
r["contract"]["origin"] = self.g(r"Growth\s*:\s*([A-Z ].+?)(?:Arrival Date|First)", text)
# ---------- parties ---------- # parties
r["parties"]["buyer"] = self.g(r"Buyer\s*:\s*([A-Z0-9 ().,-]+)", text) r["parties"]["buyer"] = safe_search(r"Buyer:\s*([A-Z0-9 ().,-]+)", text)
# ---------- shipment ---------- # shipment block
r["shipment"]["bales"] = to_float(self.g(r"Total Bales\s*:\s*(\d+)", text)) ship = self.grab(text, [
r["shipment"]["vessel"] = self.g(r"Vessel\s*:\s*([A-Z0-9 ]+)", text) "Total Bales","Vessel","Voy. No.","B/L No.","B/L Date","Destination"
r["shipment"]["bl_no"] = self.g(r"B/L No\.\s*:\s*([A-Z0-9]+)", text) ])
r["shipment"]["port_destination"] = self.g(r"Destination\s*:\s*([A-Z ,]+)", text) ship2 = self.grab(text, [
r["shipment"]["arrival_date"] = self.g(r"Arrival Date\s*:\s*([0-9A-Za-z-]+)", text) "Growth","Arrival Date","First date of weighing",
r["shipment"]["weighing_method"] = self.g(r"Weighing method\s*:\s*([A-Za-z ]+)", text) "Last Date of Weighing","Weighing method","Tare"
])
# ---------- invoice weights ---------- r["shipment"]["bales"] = to_float(ship.get("Total Bales"))
inv = self.g(r"INVOICE WEIGHTS.*?Net\s*:\s*([\d.]+)\s*kg", text) r["shipment"]["vessel"] = ship.get("Vessel")
r["weights"]["invoice_net_kg"] = to_float(inv) r["shipment"]["bl_no"] = ship.get("B/L No.")
r["shipment"]["port_destination"] = ship.get("Destination")
r["shipment"]["arrival_date"] = ship2.get("Arrival Date")
r["shipment"]["weighing_method"] = ship2.get("Weighing method")
r["contract"]["origin"] = ship2.get("Growth")
# ---------- landed weights ---------- # weights
land = self.g(r"Bales Weighed.*?Net\s*:\s*([\d.]+)\s*kg", text) inv = self.grab(text, ["Bales","Gross","Tare","Net"])
r["weights"]["net_landed_kg"] = to_float(land) land = self.grab(section(text,"Bales Weighed","Outturn"),["Bales","Gross","Tare","Net"])
r["weights"]["gross_landed_kg"] = to_float(self.g(r"Bales Weighed.*?Gross\s*:\s*([\d.]+)\s*kg", text)) r["weights"]["invoice_net_kg"] = to_float(inv.get("Net"))
r["weights"]["tare_kg"] = to_float(self.g(r"Bales Weighed.*?Tare\s*:\s*([\d.]+)\s*kg", text)) r["weights"]["gross_landed_kg"] = to_float(land.get("Gross"))
r["weights"]["tare_kg"] = to_float(land.get("Tare"))
r["weights"]["net_landed_kg"] = to_float(land.get("Net"))
# ---------- loss ---------- # loss
r["weights"]["gain_loss_kg"] = to_float(self.g(r"LOSS.*?(-?\d+\.?\d*)\s*kg", text)) loss = section(text,"LOSS","Invoice average")
r["weights"]["gain_loss_percent"] = to_float(self.g(r"Percentage\s*:\s*(-?\d+\.?\d*)", text)) r["weights"]["gain_loss_kg"] = to_float(safe_search(r"(-?\d+\.?\d*)\s*kg", loss))
r["weights"]["gain_loss_percent"] = to_float(safe_search(r"Percentage\s*:\s*(-?\d+\.?\d*)", loss))
return r return r