File size: 4,500 Bytes
4a10a29
 
eba303d
 
 
4a10a29
 
 
 
 
eba303d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from abc import ABC, abstractmethod

from loguru import logger


class ImageProcessingInterface(ABC):

    @abstractmethod
    def process_image(self, image_base64: str, model_name: str, prompt: str, system: str, temperature: float):
        pass

    def _validate_receipt_data(self, json_content):
        """
        Validates the receipt data to ensure required fields are correctly populated.

        :param json_content: The JSON content to validate
        :return: True if the required fields are valid, False otherwise
        """
        if "total_price" not in json_content or json_content.get("total_price") == "unknown":
            return False

        if "items" not in json_content or not json_content.get("items"):
            return False

        return True

    def _add_total_price(self, json_content):
        """
        Duplicates the value of 'total_price_without_discount' into 'total_price' for each item in the 'items' array.
        If 'total_price_without_discount' is missing or equals 'unknown', 'total_price' is set to "0.00".

        :param json_content: Dictionary containing the JSON data
        :return: Modified JSON content
        """
        if "items" in json_content:
            for item in json_content["items"]:
                total_price_without_discount = item.get("total_price_without_discount")
                if total_price_without_discount and total_price_without_discount != "unknown":
                    item["total_price"] = total_price_without_discount
                else:
                    logger.warning(
                        f"Item {item.get('name', 'unknown')} has missing or 'unknown' 'total_price_without_discount'. Setting 'total_price' to '0.00'.")
                    item["total_price"] = "0.00"
        return json_content

    def _extract_float_value(self, json_content, key, default=0.0):
        try:
            return float(json_content.get(key, default))
        except (ValueError, TypeError):
            logger.error(f"Invalid {key} value: {json_content.get(key)}")
            return default

    def _add_discount_item(self, json_content):
        """
        Adds a new "Discount" item to the 'items' array if 'total_discount' is greater than 0.

        :param json_content: Dictionary containing the JSON data
        :return: Modified JSON content
        """
        total_discount = self._extract_float_value(json_content, "total_discount", 0.0)
        all_items_price_with_tax = json_content.get("all_items_price_with_tax", "unknown")

        if total_discount > 0:
            discount_item = {
                "name": "Discount",
                "quantity": 1.0,
                "measurement_unit": "ks",
                "total_price_without_discount": -total_discount,
                "unit_price": -total_discount,
                "total_price_with_discount": -total_discount,
                "discount": 0.0,
                "category": "Discount",
                "item_price_with_tax": all_items_price_with_tax,
                "total_price": -total_discount
            }
            if "items" not in json_content:
                json_content["items"] = []
            json_content["items"].append(discount_item)
            logger.info("Added 'Discount' item to the receipt.")
        return json_content

    def _add_rounding_item(self, json_content):
        """
        Adds a new "Rounding" item to the 'items' array if 'rounding' is not zero.

        :param json_content: Dictionary containing the JSON data
        :return: Modified JSON content
        """
        rounding = self._extract_float_value(json_content, "rounding", 0.0)

        if rounding != 0:
            all_items_price_with_tax = json_content.get("all_items_price_with_tax", "unknown")
            rounding_item = {
                "name": "Rounding",
                "quantity": 1.0,
                "measurement_unit": "ks",
                "total_price_without_discount": rounding,
                "unit_price": rounding,
                "total_price_with_discount": rounding,
                "discount": 0.0,
                "category": "Rounding",
                "item_price_with_tax": all_items_price_with_tax,
                "total_price": rounding
            }
            if "items" not in json_content:
                json_content["items"] = []
            json_content["items"].append(rounding_item)
            logger.info("Added 'Rounding' item to the receipt.")

        return json_content