|
| 1 | +def taxes_match_total_incl(doc) -> bool: |
| 2 | + """ |
| 3 | + Check invoice matching rule between taxes and total_incl. |
| 4 | +
|
| 5 | + :return: True if rule matches, False otherwise |
| 6 | + """ |
| 7 | + # Ensure taxes and total_incl exist |
| 8 | + if not doc.taxes or not doc.total_amount.value: |
| 9 | + return False |
| 10 | + |
| 11 | + # Reconstruct total_incl from taxes |
| 12 | + total_vat = 0.0 |
| 13 | + reconstructed_total = 0.0 |
| 14 | + for tax in doc.taxes: |
| 15 | + if tax.value is None or tax.rate is None or tax.rate == 0: |
| 16 | + return False |
| 17 | + total_vat += tax.value |
| 18 | + reconstructed_total += tax.value + 100 * tax.value / tax.rate |
| 19 | + |
| 20 | + # Sanity check |
| 21 | + if total_vat <= 0: |
| 22 | + return False |
| 23 | + |
| 24 | + # Crate epsilon |
| 25 | + eps = 1 / (100 * total_vat) |
| 26 | + if ( |
| 27 | + doc.total_amount.value * (1 - eps) - 0.02 |
| 28 | + <= reconstructed_total |
| 29 | + <= doc.total_amount.value * (1 + eps) + 0.02 |
| 30 | + ): |
| 31 | + for tax in doc.taxes: |
| 32 | + tax.confidence = 1 |
| 33 | + doc.total_tax.confidence = 1.0 |
| 34 | + doc.total_amount.confidence = 1.0 |
| 35 | + return True |
| 36 | + return False |
| 37 | + |
| 38 | + |
| 39 | +def taxes_match_total_excl(doc) -> bool: |
| 40 | + """ |
| 41 | + Check invoice matching rule between taxes and total_excl. |
| 42 | +
|
| 43 | + :return: True if rule matches, False otherwise |
| 44 | + """ |
| 45 | + # Check taxes and total excl exist |
| 46 | + if len(doc.taxes) == 0 or doc.total_net.value is None: |
| 47 | + return False |
| 48 | + |
| 49 | + # Reconstruct total excl from taxes |
| 50 | + total_vat = 0.0 |
| 51 | + reconstructed_total = 0.0 |
| 52 | + for tax in doc.taxes: |
| 53 | + if tax.value is None or tax.rate is None or tax.rate == 0: |
| 54 | + return False |
| 55 | + total_vat += tax.value |
| 56 | + reconstructed_total += 100 * tax.value / tax.rate |
| 57 | + |
| 58 | + # Sanity check |
| 59 | + if total_vat <= 0: |
| 60 | + return False |
| 61 | + |
| 62 | + # Crate epsilon |
| 63 | + eps = 1 / (100 * total_vat) |
| 64 | + # Check that reconstructed total excl matches total excl |
| 65 | + if ( |
| 66 | + doc.total_net.value * (1 - eps) - 0.02 |
| 67 | + <= reconstructed_total |
| 68 | + <= doc.total_net.value * (1 + eps) + 0.02 |
| 69 | + ): |
| 70 | + for tax in doc.taxes: |
| 71 | + tax.confidence = 1 |
| 72 | + doc.total_tax.confidence = 1.0 |
| 73 | + doc.total_net.confidence = 1.0 |
| 74 | + return True |
| 75 | + return False |
| 76 | + |
| 77 | + |
| 78 | +def taxes_plus_total_excl_match_total_incl(doc) -> bool: |
| 79 | + """ |
| 80 | + Check invoice matching rule. |
| 81 | +
|
| 82 | + Rule is: sum(taxes) + total_excluding_taxes = total_including_taxes |
| 83 | + :return: True if rule matches, False otherwise |
| 84 | + """ |
| 85 | + # Check total_tax, total excl and total incl exist |
| 86 | + if ( |
| 87 | + doc.total_net.value is None |
| 88 | + or len(doc.taxes) == 0 |
| 89 | + or doc.total_amount.value is None |
| 90 | + ): |
| 91 | + return False |
| 92 | + |
| 93 | + # Reconstruct total_incl |
| 94 | + total_vat = 0.0 |
| 95 | + for tax in doc.taxes: |
| 96 | + if tax.value is not None: |
| 97 | + total_vat += tax.value |
| 98 | + reconstructed_total = total_vat + doc.total_net.value |
| 99 | + |
| 100 | + # Sanity check |
| 101 | + if total_vat <= 0: |
| 102 | + return False |
| 103 | + |
| 104 | + # Check that reconstructed total incl matches total excl + taxes sum |
| 105 | + if ( |
| 106 | + doc.total_amount.value - 0.01 |
| 107 | + <= reconstructed_total |
| 108 | + <= doc.total_amount.value + 0.01 |
| 109 | + ): |
| 110 | + for tax in doc.taxes: |
| 111 | + tax.confidence = 1 |
| 112 | + doc.total_tax.confidence = 1.0 |
| 113 | + doc.total_net.confidence = 1.0 |
| 114 | + doc.total_amount.confidence = 1.0 |
| 115 | + return True |
| 116 | + return False |
0 commit comments