from app.billing.backend.supplier_invoices import ( _build_template_builder_result, _hybrid_validate_invoice, _smart_extract_lines, ) DCS_TEXT = """ Nr. Varenr Tekst Model Antal Pris Beløb- 1 1003550858 Apple iPhone 15 6,1 128GB - Blå - Grade A NP/IPH/15/128/BLUE/A 1 3.149,00 3.149,00 Omvendt betalingspligt* KN8: 84713000 EAN: 5711802732536 S/N: MQJYVVF7TJ 2 1001562408 PanzerGlass SAFE. by TPU Case Transparent SAFE95538 1 49,00 49,00 3 1002365988 SAFE. by PanzerGlass Apple iPhone 16, 15 | Ul SAFE95875 4 49,00 196,00 4 1002484222 Apple iPhone 16 6,1 128GB - Sort MYE73QN/A 1 5.099,00 5.099,00 Omvendt betalingspligt* 5 1002365899 PanzerGlass CARE by Feature Case Transparent 1325 1 59,00 59,00 6 1000775307 Huawei E3372-325 Trådløs mobilmodem Trådløs 51071UXG 1 189,00 189,00 7 1003143468 Apple 11-inch iPad Air M3 Wi-Fi 11 128GB 8GB MC9W4KN/A 2 4.299,00 8.598,00 Omvendt betalingspligt* 8 68678 PostNord - Erhverv - Fast fragt 40 - DK - 1-5 1 40,00 40,00 Varebeløb Momsgrundlag Moms Momssats Totalbeløb DKK 17.379,00 533,00 133,25 25,00% 17.512,25 """ def test_dcs_layout_extracts_all_lines_and_metadata(): lines = _smart_extract_lines(DCS_TEXT) assert len(lines) == 8 assert lines[0]["unit_price"] == 3149.0 assert lines[0]["ean"] == "5711802732536" assert lines[0]["serial_numbers"] == ["MQJYVVF7TJ"] assert lines[0]["reverse_charge"] is True assert lines[6]["line_total"] == 8598.0 def test_hybrid_validation_understands_reverse_charge_summary(): result = _hybrid_validate_invoice({"lines": [], "total_amount": "17.512,25"}, DCS_TEXT) validation = result["_hybrid_validation"] assert validation["status"] == "validated" assert validation["line_count"] == 8 assert validation["line_sum"] == 17379.0 assert validation["document_totals"]["vat_basis"] == 533.0 assert result["total_amount"] == 17512.25 def test_generic_layout_accepts_alphanumeric_supplier_sku_and_english_amounts(): text = "1 ABC-123 Managed router service 2 1,250.00 2,500.00" lines = _smart_extract_lines(text) assert len(lines) == 1 assert lines[0]["sku"] == "ABC-123" assert lines[0]["quantity"] == 2.0 assert lines[0]["unit_price"] == 1250.0 assert lines[0]["line_total"] == 2500.0 def test_template_builder_overrides_wrong_ai_values_and_excludes_buyer_identity(): text = """ Faktura Nummer 5055616 Dato 2/10-25 Momsnr. DK29522790 BMC Denmark ApS Lejrvej 39 Nr. Varenr Tekst Model Antal Pris Beløb- 1 1000822111 Apple TV 4K MN893MP/A 1 1.099,00 1.099,00 2 1000809070 Xiaomi Air Purifier BHR5860EU 2 499,00 998,00 3 1003114298 Lenovo ThinkPad L-T14G2-SCA-B102 1 1.999,00 1.999,00 4 1003283728 HPE SmartMemory RFB-815101-B21 2 569,00 1.138,00 5 68678 PostNord Erhverv Fast fragt 1 40,00 40,00 Varebeløb Momsgrundlag Moms Momssats Totalbeløb DKK 5.274,00 3.275,00 818,75 25,00% 6.092,75 DCS ApS CVR: DK26686091 www.dcs.dk """ bad_ai_result = { "total_amount": "2097", "cvr": "29522790", "detection_patterns": ["BMC Denmark ApS", "Lejrvej 39", "3500 Værløse"], } result = _build_template_builder_result( text, bad_ai_result, {"name": "DCS ApS", "cvr_number": "26686091"}, ) assert result["invoice_number"]["value"] == "5055616" assert result["invoice_date"]["value"] == "2/10-25" assert result["total_amount"]["value"] == "6092.75" assert result["cvr"]["value"] == "26686091" assert "BMC Denmark ApS" not in result["detection_patterns"] assert "Lejrvej 39" not in result["detection_patterns"] assert "DCS ApS" in result["detection_patterns"] assert result["line_count"] == 5 for field in ("invoice_number", "invoice_date", "total_amount", "cvr"): match = __import__("re").search(result[field]["pattern"], text, __import__("re").IGNORECASE | __import__("re").MULTILINE) assert match, field