File size: 2,800 Bytes
317211f
 
 
 
 
 
 
 
 
 
 
 
 
 
53901bc
317211f
 
 
 
 
 
53901bc
317211f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2ac459
317211f
53901bc
 
 
 
317211f
 
 
fa465f3
 
317211f
 
 
 
 
 
 
 
 
 
d2ac459
317211f
 
 
 
 
 
 
 
 
 
 
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
# generated by datamodel-codegen:
#   filename:  schema.json
#   timestamp: 2023-07-28T11:36:16+00:00

from __future__ import annotations

from datetime import date
from typing import Dict, Optional, Union

import iso4217
from pydantic import BaseModel, Field, constr, validator, ValidationError


class TaxItem(BaseModel):
    gst: str = Field(
        ...,
        title="The total GST tax amount (IGST + CGST + SGST + etc) as a single number",
    )


class TaxItem1(BaseModel):
    vat: str = Field(..., title="The total VAT present in the invoice")


class TaxNumberItem(BaseModel):
    gst_number: constr(min_length=15) = Field(
        ..., title="The alphanumeric GSTIN/GST number code"
    )


class TaxNumberItem1(BaseModel):
    vat_number: str = Field(..., title="The VAT/TIN number present in older invoices")


class TaxNumberItem2(BaseModel):
    ui_number: str = Field(..., title="The tax UIN issued to foreign entities")


class SellerDetails(BaseModel):
    name: Optional[str] = None
    address: Optional[str] = None
    contact: Optional[str] = None
    tax_number: Union[TaxNumberItem, TaxNumberItem1, TaxNumberItem2] = Field(
        ..., title="Tax information"
    )
    pan_number: constr(min_length=10, max_length=10) = Field(
        ..., title="The 10-character alphanumeric PAN code"
    )


class UIDDict(BaseModel):
    invoice_number: str = Field(..., title="The invoice number")
    # other_uids: Dict[str, str] = Field(
    #     ...,
    #     title="Key-value pairs of uniquely identifying numbers (UIDs) like order number, bill number, payment ID, etc but not the invoice number",
    # )


class InformationExtractedFromABillReceipt(BaseModel):
    uids: str = Field(..., title="The bill number/invoice number")
    #uids: UIDDict = Field(..., title="Invoice number and other UIDs")
    total: float = Field(..., title="Total amount or price")
    tax: Union[TaxItem, TaxItem1] = Field(..., title="The total tax amount")
    name: str = Field(
        ...,
        title="Name of the person/entity that the invoice item was charged or delivered to",
    )
    currency: str = Field(
        default="INR",
        title="The ISO 4217 code for the currency in which the prices in the invoice are (inferred from symbols, names, addresses, etc)",
    )
    issue_date: date = Field(
        ..., title="The date the invoice was issued"
    )
    seller_details: SellerDetails = Field(..., title="Information about the seller")
    summary: str = Field(..., title="5-6 words short summary of purchased good(s)")

    @validator("currency")
    @classmethod
    def check_currency(cls, v: str) -> str:
        if not iso4217.Currency.__members__.get(v.lower()):
            raise ValidationError(f"{v} is not a valid ISO 4217 currency code")
        return v.upper()