diff --git a/stove_match.py b/stove_match.py index 37a597b..73978fd 100644 --- a/stove_match.py +++ b/stove_match.py @@ -68,6 +68,10 @@ def _reserve(key_id: int, budget: float, profile) -> int: def _candidates(name: str, unit: str, region: str, zone: str | None) -> list[dict]: found = fgis_ref.find_resource_price(query=name, region=region, zone=zone, limit=12) + return _candidate_rows(found, unit) + + +def _candidate_rows(found: dict, unit: str) -> list[dict]: if found.get("status") == "db_unavailable": raise MatchingUnavailable(30) if found.get("status") != "ok" or found.get("multiple_zones"): @@ -184,8 +188,12 @@ async def fill(prices: dict, *, key_id: int, budget: float, deadline: float, req await asyncio.to_thread(stove_cache.match_delete, ck) else: continue - candidates = await asyncio.to_thread(_candidates, line["name"], line["unit"], - prices["region"], prices.get("zone")) + found = prices.get("search_results", {}).get(index) + if found is not None: + candidates = _candidate_rows(found, line["unit"]) + else: + candidates = await asyncio.to_thread(_candidates, line["name"], line["unit"], + prices["region"], prices.get("zone")) if candidates: todo.append({"index": index, "line": line, "candidates": candidates, "cache_key": ck}) else: diff --git a/stove_pricing.py b/stove_pricing.py index 60d0501..bf8e2d6 100644 --- a/stove_pricing.py +++ b/stove_pricing.py @@ -18,7 +18,6 @@ RATES = { "bread": ("08-08-003-09", "8-31-9"), } BRICK_QUERY = {"ceramic": "кирпич керамический", "fireclay": "кирпич шамотный"} -_BRICK_WORDS = ("кирпич", "шамот", "огнеупор") _ROMAN = ("I", "II", "III", "IV") @@ -120,7 +119,8 @@ def load(stove: c.StoveRequest) -> dict: zone = zones[0]["zone_name"] if zones else s.zone periods = [r.get("period") for r in probe.get("results", []) if r.get("period")] out = {"valid_at": _period(periods[0]) if periods else "период не указан", "region": region, - "zone": s.zone and zone, "notes": notes, "lines": [], "brick_lines_skipped": []} + "zone": s.zone and zone, "notes": notes, "lines": [], "brick_lines_skipped": [], + "search_results": {}} if s.kind in RATES: code = RATES[s.kind][0] rate = rim_calc.calc_rim_rate(code=code, region=region, zone=zone) @@ -149,9 +149,10 @@ def load(stove: c.StoveRequest) -> dict: out["lines"].append(_line(query, qty, "шт", _choose(got, "шт", query), bricks=True)) for item in s.materials or []: name = item.name - if any(word in name.lower() for word in _BRICK_WORDS): + if re.search(r"\bкирпич(?:и|а|ей|ом|у)?\b", name.lower()): out["brick_lines_skipped"].append(name) continue got = fgis_ref.find_resource_price(query=name, region=region, zone=zone, limit=12) + out["search_results"][len(out["lines"])] = got out["lines"].append(_line(name, item.qty, item.unit, _choose(got, item.unit, name))) return out diff --git a/tests/test_stove_match.py b/tests/test_stove_match.py index 38b124b..05cf442 100644 --- a/tests/test_stove_match.py +++ b/tests/test_stove_match.py @@ -9,6 +9,8 @@ if ROOT not in sys.path: sys.path.insert(0, ROOT) import stove_match +import stove_pricing +import stove_contract as c def _prices(): @@ -57,3 +59,49 @@ def test_out_of_list_code_is_missing_and_not_cached(monkeypatch): result = asyncio.run(stove_match.fill(_prices(), key_id=1, budget=300, deadline=__import__('time').monotonic()+10, request_id="rid")) assert result["lines"][0]["code"] is None assert saved == [] + + +def test_site_fireclay_mortar_is_not_discarded_as_brick(monkeypatch): + monkeypatch.setattr(stove_pricing.fgis_ref, "find_resource_price", lambda **kw: { + "status": "ok", "region": "Москва", "results": []}) + request = c.StoveRequest.model_validate({"stove": { + "kind": "bath", "bricks": {"ceramic": 244, "fireclay": 76}, "region": "Москва", + "materials": [ + {"name": "Мертель / шамотная глина", "qty": 23, "unit": "кг"}, + {"name": "Кирпич красный полнотелый", "qty": 257, "unit": "шт"}, + {"name": "Кирпичный бой для засыпки", "qty": 10, "unit": "кг"}, + ]}}) + priced = stove_pricing.load(request) + assert [line["name"] for line in priced["lines"]] == [ + "кирпич керамический", "кирпич шамотный", "Мертель / шамотная глина", + "Кирпичный бой для засыпки"] + assert priced["brick_lines_skipped"] == ["Кирпич красный полнотелый"] + + +def test_match_reuses_price_search_for_site_material(monkeypatch): + calls = [] + candidate = {"code": "08.1.02.12-0061", "name": "Дверка печная 250×205", "unit": "шт", + "price_current": 1234} + + def search(**kw): + calls.append(kw["query"]) + return {"status": "ok", "region": "Москва", "results": + [candidate] if kw["query"] == "Дверка топочная 250×205" else []} + + monkeypatch.setattr(stove_pricing.fgis_ref, "find_resource_price", search) + monkeypatch.setattr(stove_match.stove_cache, "match_get", lambda key: None) + monkeypatch.setattr(stove_match.stove_cache, "match_put", lambda *args, **kw: None) + + async def choose(batch, *args): + assert len(batch) == 1 and batch[0]["line"]["name"] == "Дверка топочная 250×205" + return [candidate["code"]] + + monkeypatch.setattr(stove_match, "_ask", choose) + request = c.StoveRequest.model_validate({"stove": { + "kind": "bath", "bricks": {"ceramic": 244, "fireclay": 76}, "region": "Москва", + "materials": [{"name": "Дверка топочная 250×205", "qty": 1, "unit": "шт"}]}}) + priced = stove_pricing.load(request) + result = asyncio.run(stove_match.fill( + priced, key_id=1, budget=300, deadline=__import__('time').monotonic()+10, request_id="rid")) + assert calls.count("Дверка топочная 250×205") == 1 + assert result["lines"][2]["code"] == candidate["code"]