-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidp_roll_conversion.py
More file actions
268 lines (220 loc) · 8.44 KB
/
Copy pathidp_roll_conversion.py
File metadata and controls
268 lines (220 loc) · 8.44 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
"""Convert Fowler roll-priced invoice lines to feet qty + per-foot unit cost."""
from __future__ import annotations
import re
# Fallback when UoM is RL but roll length could not be parsed from the description.
ROLL_ITEM_FALLBACK_FEET: dict[str, int] = {
"Wire - 18 GA 13 Strand": 500,
"Wire - 14 GA Red/Blue, Golf (for 2-wire systems)": 1000,
# Legacy export typo — keep so older catalog CSV rows still resolve.
"Wire - 14 GA Red/Blue, Gold (for 2-wire systems)": 1000,
}
# Fowler mislabels UoM as EA; treat matched catalog ItemName as roll (feet per roll).
ROLL_ITEMS_EA_MEANS_ROLL: dict[str, int] = {
"Wire - 14 GA Red/Blue, Golf (for 2-wire systems)": 1000,
"Wire - 14 GA Red/Blue, Gold (for 2-wire systems)": 1000,
'Micro Drip Line - 1/4" 6 in spacing, Black (Hunter)': 100,
'Micro Drip Line - Blank - 1/4" (Rain Bird XQ or A250)': 100,
}
# Fowler Hunter 2-wire decoder cable — roll in description, UoM often EA.
_INVOICE_HUNTER_2WIRE_ROLL_RE = re.compile(
r"14/2\s+jacketed.*hunter\s+2-?wire",
re.IGNORECASE | re.DOTALL,
)
# Rain Bird SPX-FLEX swing pipe — 100' rolls, UoM often EA.
_INVOICE_SPX_FLEX_SWING_PIPE_RE = re.compile(
r"spx-?\s*flex.*swing\s+pipe|swing\s+pipe.*spx-?\s*flex",
re.IGNORECASE | re.DOTALL,
)
# Rain Bird XFDE emitter tubing — 500' rolls, Fowler UoM often EA; catalog XFDE612500.
_INVOICE_XFDE_EMITTER_ROLL_RE = re.compile(
r"xfde\s+17mm.*500\s*'?\s*roll.*emitter\s+tubing",
re.IGNORECASE | re.DOTALL,
)
# Hunter 1/4" black emitter tubing — 100' rolls, Fowler UoM often EA.
_INVOICE_HUNTER_BLACK_EMITTER_TUBING_ROLL_RE = re.compile(
r"black\s+emitter\s+tubing.*\bhunter\b"
r"|\bhunter\b.*black\s+emitter\s+tubing"
r"|\bemitter\s+tubing\s+hunter\b",
re.IGNORECASE | re.DOTALL,
)
# Rain Bird XQ blank drip tube — 100' rolls, Fowler UoM often EA; catalog XQ.
_INVOICE_XQ_DRIP_TUBE_ROLL_RE = re.compile(
r"\bxq\b.*drip\s+tube|drip\s+tube.*\bxq\b|"
r"\bxq\b.*1/4.*rain\s+bird",
re.IGNORECASE | re.DOTALL,
)
ROLL_ITEMS_BY_CODE: dict[str, int] = {
"XFDE612500": 500,
"XQ": 100,
}
# Pro Turf / Fowler SIDR-15 poly — Fowler qty/UoM is feet; do not treat 300' ROLL as roll count.
_SIDR15_POLY_NO_ROLL_CONVERSION_RE = re.compile(
r"pro\s+turf\s+green.*sidr-?\s*15|sidr-?\s*15.*pro\s+turf\s+green|"
r"sidr-?\s*15.*poly\s+pipe.*(?:3608|black)|"
r"(?:3608|black).*sidr-?\s*15.*poly\s+pipe",
re.IGNORECASE | re.DOTALL,
)
_EXPLICIT_FEET_RE = re.compile(
r"""
\b(\d{2,4})\s*(?:FT|FEET)\b # 500 FT
|
\b(\d{2,4})' # 500'
""",
re.IGNORECASE | re.VERBOSE,
)
_ROLL_LENGTH_WHITELIST_RE = re.compile(r"\b(250|500|1000|2000)\b")
def _normalize_item_code(item_code: str | None) -> str:
if not item_code:
return ""
return str(item_code).strip().lstrip("#").upper()
def _item_code_roll_feet(description_raw: str, item_code: str | None) -> int | None:
"""One-off roll lines keyed by catalog item code + invoice description."""
code = _normalize_item_code(item_code)
feet_per_roll = ROLL_ITEMS_BY_CODE.get(code)
if feet_per_roll is None:
return None
desc = description_raw or ""
if code == "XFDE612500" and not _INVOICE_XFDE_EMITTER_ROLL_RE.search(desc):
return None
if code == "XQ" and not (
_INVOICE_XQ_DRIP_TUBE_ROLL_RE.search(desc) or "drip tube" in desc.lower()
):
return None
return feet_per_roll_from_description(description_raw) or feet_per_roll
def normalize_uom(uom: str | None) -> str:
if not uom:
return ""
s = str(uom).strip().upper()
if s in {"RL", "ROLL", "ROL", "ROLLS"}:
return "RL"
return s
def is_roll_uom(uom: str | None) -> bool:
return normalize_uom(uom) == "RL"
def _skip_roll_conversion(description_raw: str) -> bool:
"""Invoice lines where qty is already feet (or each), not roll count."""
return bool(_SIDR15_POLY_NO_ROLL_CONVERSION_RE.search(description_raw or ""))
def _should_convert_as_roll(
uom_raw: str | None,
item_name: str | None,
description_raw: str = "",
*,
item_code: str | None = None,
) -> bool:
if is_roll_uom(uom_raw):
return True
if item_name and item_name in ROLL_ITEMS_EA_MEANS_ROLL:
return True
if _item_code_roll_feet(description_raw, item_code) is not None:
return True
return _invoice_description_means_roll(description_raw) is not None
def _invoice_description_means_roll(description_raw: str) -> int | None:
"""
One-off Fowler invoice lines sold as rolls but UoM is often EA.
Returns feet per roll when description matches, else None.
"""
desc = description_raw or ""
if _INVOICE_HUNTER_2WIRE_ROLL_RE.search(desc):
return (
feet_per_roll_from_description(desc)
or feet_per_roll_whitelist(desc)
or 1000
)
if _INVOICE_SPX_FLEX_SWING_PIPE_RE.search(desc):
return feet_per_roll_from_description(desc) or 100
if _INVOICE_HUNTER_BLACK_EMITTER_TUBING_ROLL_RE.search(desc):
return feet_per_roll_from_description(desc) or 100
if _INVOICE_XQ_DRIP_TUBE_ROLL_RE.search(desc):
return feet_per_roll_from_description(desc) or 100
return None
def _catalog_fallback_feet(item_name: str | None) -> int | None:
if not item_name:
return None
if item_name in ROLL_ITEM_FALLBACK_FEET:
return ROLL_ITEM_FALLBACK_FEET[item_name]
if item_name in ROLL_ITEMS_EA_MEANS_ROLL:
return ROLL_ITEMS_EA_MEANS_ROLL[item_name]
return None
def feet_per_roll_from_description(description: str) -> int | None:
"""Parse explicit roll length from description (500 FT, 500', etc.)."""
for match in _EXPLICIT_FEET_RE.finditer(description or ""):
raw = match.group(1) or match.group(2)
if raw:
return int(raw)
return None
def feet_per_roll_whitelist(description: str) -> int | None:
"""Common Fowler roll lengths when UoM is already RL."""
match = _ROLL_LENGTH_WHITELIST_RE.search(description or "")
return int(match.group(1)) if match else None
def resolve_feet_per_roll(
description_raw: str,
uom_raw: str | None,
*,
item_name: str | None = None,
item_code: str | None = None,
) -> tuple[int | None, str | None]:
"""
Return (feet_per_roll, source) where source is 'description' or 'catalog'.
"""
if not _should_convert_as_roll(
uom_raw, item_name, description_raw, item_code=item_code
):
return None, None
feet = feet_per_roll_from_description(description_raw)
if feet:
return feet, "description"
feet = feet_per_roll_whitelist(description_raw)
if feet:
return feet, "description"
feet = _item_code_roll_feet(description_raw, item_code)
if feet:
return feet, "item_code"
feet = _catalog_fallback_feet(item_name)
if feet:
return feet, "catalog"
feet = _invoice_description_means_roll(description_raw)
if feet:
return feet, "description"
return None, None
def roll_line_missing_feet_per_roll(
description_raw: str,
uom_raw: str | None,
item_name: str | None = None,
*,
item_code: str | None = None,
) -> bool:
"""True when line should convert as roll but feet per roll cannot be determined."""
if not _should_convert_as_roll(
uom_raw, item_name, description_raw, item_code=item_code
):
return False
feet, _ = resolve_feet_per_roll(
description_raw, uom_raw, item_name=item_name, item_code=item_code
)
return feet is None
def maybe_convert_roll_line(
quantity: float,
unit_price: float,
*,
description_raw: str,
uom_raw: str | None,
item_name: str | None = None,
item_code: str | None = None,
) -> tuple[float, float, str | None]:
"""
When UoM is RL, convert roll qty/price to feet qty and per-foot pre-tax price.
Preserves line extended total. Returns (qty, unit_price, note_or_none).
"""
if quantity <= 0:
return quantity, unit_price, None
if _skip_roll_conversion(description_raw):
return quantity, unit_price, None
feet, source = resolve_feet_per_roll(
description_raw, uom_raw, item_name=item_name, item_code=item_code
)
if not feet:
return quantity, unit_price, None
new_qty = quantity * feet
new_price = unit_price / feet
uom_label = "RL" if is_roll_uom(uom_raw) else "EA→roll"
note = f"roll→ft: {quantity:g} {uom_label} × {feet} = {new_qty:g} ({source})"
return new_qty, new_price, note