-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidp_sod.py
More file actions
572 lines (490 loc) · 17.7 KB
/
Copy pathidp_sod.py
File metadata and controls
572 lines (490 loc) · 17.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
"""Idaho Sod, Cedron Sod, and shared sod-invoice transforms (invoice total / sq ft)."""
from __future__ import annotations
import re
from dataclasses import dataclass
from typing import TYPE_CHECKING
from idp_costs import LineOutput, line_total_cost, reconcile_line_costs
from idp_reference import InventoryRecord, ReferenceData
from idp_vendor_prefs import is_cedron_sod_vendor, is_idaho_sod_vendor
if TYPE_CHECKING:
from idp_openai import ExtractionResult, LineMatch
# Invoice grass wording → substring that must appear in catalog ItemName (lower).
SOD_CATALOG_HINTS: tuple[tuple[re.Pattern[str], str], ...] = (
(re.compile(r"\brtf\b|rhizomatous", re.I), "rtf"),
(re.compile(r"kentucky|bluegrass", re.I), "bluegrass"),
(re.compile(r"meadow", re.I), "meadow"),
(re.compile(r"\bfescue\b", re.I), "fescue"),
(re.compile(r"native", re.I), "native sod"),
)
_CHARGE_LINE_RE = re.compile(
r"delivery|fuel\s*surcharge|surcharge|pallet|deposit|\btax\b",
re.I,
)
_SOD_MATERIAL_RE = re.compile(
r"kentucky|bluegrass|fescue|meadow|rtf|rhizomatous|\bsod\b",
re.I,
)
@dataclass(frozen=True)
class SodSplitResult:
single_line_ok: bool
line_a: tuple[float, float] # qty, unit_cost
line_b: tuple[float, float] | None
note: str = ""
def sod_catalog_items(refs: ReferenceData) -> list[InventoryRecord]:
"""Material catalog rows whose name contains 'sod'."""
return [
rec
for rec in refs.inventory
if rec.item_type == "Material" and "sod" in (rec.item_name or "").lower()
]
def match_sod_catalog(
description_raw: str,
refs: ReferenceData,
) -> tuple[InventoryRecord | None, float, str]:
"""Map invoice grass description to one of the sod Material SKUs."""
desc = description_raw or ""
if not _SOD_MATERIAL_RE.search(desc):
return None, 0.0, "No sod grass type in description"
hint: str | None = None
for pattern, catalog_hint in SOD_CATALOG_HINTS:
if pattern.search(desc):
hint = catalog_hint
break
if hint is None:
return None, 0.0, "Unrecognized sod type"
candidates = sod_catalog_items(refs)
if hint == "native sod":
matches = [c for c in candidates if "native" in (c.item_name or "").lower()]
elif hint == "rtf":
matches = [c for c in candidates if "rtf" in (c.item_name or "").lower()]
elif hint == "bluegrass":
matches = [
c
for c in candidates
if "bluegrass" in (c.item_name or "").lower()
and "rtf" not in (c.item_name or "").lower()
]
elif hint == "meadow":
matches = [c for c in candidates if "meadow" in (c.item_name or "").lower()]
elif hint == "fescue":
matches = [
c
for c in candidates
if "fescue" in (c.item_name or "").lower()
and "rtf" not in (c.item_name or "").lower()
and "rhizomatous" not in (c.item_name or "").lower()
]
else:
matches = []
if len(matches) == 1:
rec = matches[0]
return rec, 0.95, f"Sod alias → {rec.item_name!r}"
if len(matches) > 1:
rec = matches[0]
return rec, 0.80, f"Ambiguous sod match; picked {rec.item_name!r}"
return None, 0.0, f"No catalog sod for hint {hint!r}"
def is_sod_vendor(vendor_name: str | None, vendor_raw: str | None = None) -> bool:
return (
is_idaho_sod_vendor(vendor_name)
or is_idaho_sod_vendor(vendor_raw)
or is_cedron_sod_vendor(vendor_name)
or is_cedron_sod_vendor(vendor_raw)
)
def is_sod_charge_line(description_raw: str) -> bool:
return bool(_CHARGE_LINE_RE.search(description_raw or ""))
def is_sod_delivery_line(description_raw: str) -> bool:
return bool(re.search(r"delivery", description_raw or "", re.I))
def is_sod_material_line(description_raw: str) -> bool:
desc = description_raw or ""
return bool(_SOD_MATERIAL_RE.search(desc)) and not is_sod_charge_line(desc)
def resolve_sod_line_catalog_match(
description_raw: str,
refs: ReferenceData,
*,
vendor_name: str | None,
vendor_raw: str | None,
) -> tuple[InventoryRecord | None, float, str] | None:
"""
Sod-vendor catalog match. Returns None to fall back to generic match_line.
Fee lines return (None, 0, note) so they never fuzzy-match (e.g. Serviceberry).
"""
if not is_sod_vendor(vendor_name, vendor_raw):
return None
if is_sod_charge_line(description_raw):
return None, 0.0, "Sod fee line (excluded from catalog)"
if is_sod_material_line(description_raw):
rec, conf, note = match_sod_catalog(description_raw, refs)
return rec, conf, note
return None, 0.0, "Not sod grass line"
def _sod_material_blocks(
lines: list[LineMatch],
) -> list[tuple[LineMatch, LineMatch | None]]:
"""Pair each grass line with its per-sf Delivery/Service row when qty matches."""
blocks: list[tuple[LineMatch, LineMatch | None]] = []
i = 0
while i < len(lines):
ln = lines[i]
if not is_sod_material_line(ln.description_raw):
i += 1
continue
delivery: LineMatch | None = None
if i + 1 < len(lines):
nxt = lines[i + 1]
if (
is_sod_delivery_line(nxt.description_raw)
and abs(float(nxt.quantity) - float(ln.quantity)) < 0.01
):
delivery = nxt
i += 2
blocks.append((ln, delivery))
continue
i += 1
blocks.append((ln, delivery))
return blocks
def _sod_catalog_group_key(rec: InventoryRecord) -> str:
code = (rec.item_code or "").strip()
if code:
return code
return f"name:{(rec.item_name or '').strip().lower()}"
def _blended_sod_unit(material: LineMatch, delivery: LineMatch | None) -> float:
unit = float(material.unit_price)
if delivery is not None:
unit += float(delivery.unit_price)
return unit
def compute_sod_price_split(total: float, qty: float) -> SodSplitResult:
"""
Compute a two-line manual split (3-decimal unit costs) that sums to Total Due.
Used for B6 receipt note when a single import line cannot match exactly.
"""
target = round(float(total), 2)
sqft = float(qty)
if sqft <= 0 or target <= 0:
return SodSplitResult(
False,
(sqft, 0.0),
None,
note="Invalid total or quantity",
)
single = [LineOutput("", "", sqft, round(target / sqft, 3))]
if reconcile_line_costs(single, target):
return SodSplitResult(
True,
(sqft, single[0].unit_cost),
None,
note="Single line reconciled to Total Due",
)
qty_int = int(round(sqft))
if qty_int <= 1:
return SodSplitResult(
False,
(sqft, single[0].unit_cost),
None,
note="Quantity too small to split",
)
base_unit = round(target / sqft, 3)
half = qty_int // 2
for qty1 in range(max(1, half - 500), min(qty_int, half + 500) + 1):
qty2 = qty_int - qty1
if qty2 <= 0:
continue
for d1 in range(-5, 6):
for d2 in range(-5, 6):
u1 = round(base_unit + d1 * 0.001, 3)
u2 = round(base_unit + d2 * 0.001, 3)
if u1 < 0 or u2 < 0:
continue
ext = round(qty1 * u1 + qty2 * u2, 2)
if abs(ext - target) < 0.005:
return SodSplitResult(
False,
(float(qty1), u1),
(float(qty2), u2),
note=(
f"Split {qty_int} sq ft into {qty1}+{qty2} "
f"at {u1}/{u2} per sq ft"
),
)
qty1 = float(half)
qty2 = sqft - qty1
u1 = base_unit
ext1 = line_total_cost(qty1, u1)
u2 = round((target - ext1) / qty2, 3) if qty2 > 0 else 0.0
ext = round(line_total_cost(qty1, u1) + line_total_cost(qty2, u2), 2)
if abs(ext - target) < 0.02:
return SodSplitResult(
False,
(qty1, u1),
(qty2, u2),
note="Approximate split (verify Total Due)",
)
return SodSplitResult(
False,
(sqft, single[0].unit_cost),
None,
note="Could not find exact 3-decimal split",
)
def format_sod_receipt_note(
invoice_total: float,
sqft: float,
import_unit: float,
split: SodSplitResult,
) -> str | None:
"""B6 / ReceiptNote text when 3-decimal import column F differs from Total Due."""
return format_sod_receipt_note_from_column_f(
invoice_total,
line_total_cost(sqft, import_unit),
)
def format_sod_receipt_note_from_column_f(
invoice_total: float,
column_f_total: float,
) -> str | None:
"""Variance note from invoice total vs sum of line extended costs (column F)."""
target = round(float(invoice_total), 2)
import_ext = round(float(column_f_total), 2)
variance = round(target - import_ext, 2)
if abs(variance) < 0.005:
return None
return (
f"Actual Invoice Total: ${target:,.2f}, "
f"Variance {variance:+,.2f} (3-decimal limit)."
)
def build_sod_receipt_note(
invoice_total: float,
lines: list[LineOutput],
split: SodSplitResult,
) -> str | None:
if not lines:
return None
column_f = round(
sum(line_total_cost(ln.quantity, ln.unit_cost) for ln in lines),
2,
)
return format_sod_receipt_note_from_column_f(invoice_total, column_f)
def transform_idaho_sod_extraction(
result: ExtractionResult,
refs: ReferenceData,
) -> ExtractionResult:
"""
Collapse Idaho Sod invoice to sod catalog line(s).
Single grass: Total Due ÷ total sq ft. Multiple grass types: one line per SKU with
material+delivery blended unit; pallet / fuel / tax dropped (in Total Due).
"""
if not (
is_idaho_sod_vendor(result.vendor_name)
or is_idaho_sod_vendor(result.vendor_raw)
):
return result
invoice_total = result.invoice_total
if invoice_total is None or invoice_total <= 0:
return result
material_lines = [
ln for ln in result.lines if is_sod_material_line(ln.description_raw)
]
if not material_lines:
return result
catalog_matches: list[tuple[LineMatch, InventoryRecord, float, str]] = []
for ln in material_lines:
rec, match_conf, match_note = match_sod_catalog(ln.description_raw, refs)
if rec:
catalog_matches.append((ln, rec, match_conf, match_note))
if not catalog_matches:
return result
catalog_codes = {_sod_catalog_group_key(rec) for _, rec, _, _ in catalog_matches}
if len(catalog_codes) != 1:
return _transform_multi_grass_sod(
result,
refs,
invoice_total=invoice_total,
vendor_label="Idaho Sod",
total_label="Total Due",
)
total_qty = sum(ln.quantity for ln, _, _, _ in catalog_matches)
if total_qty <= 0:
return result
primary = max(catalog_matches, key=lambda row: row[0].quantity)
ln, rec, match_conf, match_note = primary
line_conf = min(
(row[0].confidence for row in catalog_matches if row[0].confidence),
default=0.95,
)
return _collapse_to_single_sod_line(
result,
vendor_label="Idaho Sod",
invoice_total=invoice_total,
total_qty=total_qty,
rec=rec,
match_conf=match_conf,
match_note=match_note,
description_raw=ln.description_raw,
uom_raw=ln.uom_raw or "SF",
line_confidence=line_conf,
total_label="Total Due",
)
def _collapse_to_single_sod_line(
result: ExtractionResult,
*,
vendor_label: str,
invoice_total: float,
total_qty: float,
rec: InventoryRecord,
match_conf: float,
match_note: str,
description_raw: str,
uom_raw: str,
line_confidence: float,
total_label: str = "invoice total",
) -> ExtractionResult:
from idp_openai import LineMatch
split = compute_sod_price_split(invoice_total, total_qty)
import_unit = round(invoice_total / total_qty, 3)
result.lines = [
LineMatch(
description_raw=description_raw,
quantity=total_qty,
unit_price=import_unit,
uom_raw=uom_raw or "SF",
item_code=rec.item_code or None,
item_name=rec.item_name,
confidence=max(
0.95,
min(line_confidence, match_conf) if line_confidence else match_conf,
),
rationale=(
f"{vendor_label}: {total_label} {invoice_total:.2f} / "
f"{total_qty:.0f} sq ft; {match_note}; single import line"
),
)
]
result.sod_split = split
return result
def _transform_multi_grass_sod(
result: ExtractionResult,
refs: ReferenceData,
*,
invoice_total: float,
vendor_label: str,
total_label: str = "invoice total",
) -> ExtractionResult:
"""
Multiple grass types: one import line per sod SKU with blended material+delivery
unit cost; pallet / fuel / tax lines dropped (included in invoice total).
"""
from idp_openai import LineMatch
blocks = _sod_material_blocks(result.lines)
if not blocks:
return result
grouped: dict[
str,
list[tuple[float, float, LineMatch, InventoryRecord, float, str]],
] = {}
for material, delivery in blocks:
rec, match_conf, match_note = match_sod_catalog(material.description_raw, refs)
if not rec:
continue
group_key = _sod_catalog_group_key(rec)
blended = _blended_sod_unit(material, delivery)
grouped.setdefault(group_key, []).append(
(float(material.quantity), blended, material, rec, match_conf, match_note)
)
if not grouped:
return result
out_lines: list[LineMatch] = []
for group_key, rows in grouped.items():
total_qty = sum(qty for qty, _, _, _, _, _ in rows)
if total_qty <= 0:
continue
total_ext = sum(qty * unit for qty, unit, _, _, _, _ in rows)
import_unit = round(total_ext / total_qty, 3)
_, _, primary, rec, match_conf, match_note = max(rows, key=lambda r: r[0])
line_conf = min(
(primary.confidence for _, _, primary, _, _, _ in rows if primary.confidence),
default=0.95,
)
item_code = (rec.item_code or "").strip() or None
out_lines.append(
LineMatch(
description_raw=primary.description_raw,
quantity=total_qty,
unit_price=import_unit,
uom_raw=primary.uom_raw or "SF",
item_code=item_code,
item_name=rec.item_name,
confidence=max(
0.95,
min(line_conf, match_conf) if line_conf else match_conf,
),
rationale=(
f"{vendor_label}: multi-grass {total_label} {invoice_total:.2f}; "
f"{match_note}; material+delivery blended unit"
),
)
)
if not out_lines:
return result
result.lines = out_lines
total_sqft = sum(ln.quantity for ln in out_lines)
result.sod_split = compute_sod_price_split(invoice_total, total_sqft)
return result
def transform_cedron_sod_extraction(
result: ExtractionResult,
refs: ReferenceData,
) -> ExtractionResult:
"""
Collapse Cedron Sod invoice to one sod catalog line when a single grass type.
invoice_total (bottom left) ÷ total sod quantity. Pallet credit/charge and tax
lines are dropped (already in invoice total). Multiple grass types are left
unchanged for review.
"""
if not (
is_cedron_sod_vendor(result.vendor_name)
or is_cedron_sod_vendor(result.vendor_raw)
):
return result
invoice_total = result.invoice_total
if invoice_total is None or invoice_total <= 0:
return result
material_lines = [
ln for ln in result.lines if is_sod_material_line(ln.description_raw)
]
if not material_lines:
return result
catalog_matches: list[tuple[LineMatch, InventoryRecord, float, str]] = []
for ln in material_lines:
rec, match_conf, match_note = match_sod_catalog(ln.description_raw, refs)
if rec:
catalog_matches.append((ln, rec, match_conf, match_note))
if not catalog_matches:
return result
catalog_codes = {_sod_catalog_group_key(rec) for _, rec, _, _ in catalog_matches}
if len(catalog_codes) != 1:
return _transform_multi_grass_sod(
result,
refs,
invoice_total=invoice_total,
vendor_label="Cedron Sod",
)
total_qty = sum(ln.quantity for ln, _, _, _ in catalog_matches)
if total_qty <= 0:
return result
primary = max(catalog_matches, key=lambda row: row[0].quantity)
ln, rec, match_conf, match_note = primary
line_conf = min((row[0].confidence for row in catalog_matches if row[0].confidence), default=0.95)
return _collapse_to_single_sod_line(
result,
vendor_label="Cedron Sod",
invoice_total=invoice_total,
total_qty=total_qty,
rec=rec,
match_conf=match_conf,
match_note=match_note,
description_raw=ln.description_raw,
uom_raw=ln.uom_raw or "SF",
line_confidence=line_conf,
)
def apply_sod_vendor_transform(
result: ExtractionResult,
refs: ReferenceData,
) -> ExtractionResult:
"""Apply Idaho or Cedron sod collapse when vendor matches."""
result = transform_idaho_sod_extraction(result, refs)
return transform_cedron_sod_extraction(result, refs)