-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_orders.py
More file actions
116 lines (94 loc) · 3.98 KB
/
sync_orders.py
File metadata and controls
116 lines (94 loc) · 3.98 KB
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
111
112
113
114
115
116
from api_valerdat import ApiValerdat
from datetime import date, datetime
from uuid import UUID
import json
import logging
class SyncOrders:
def __init__(self, customer: str, username: str, password: str):
self.__api = ApiValerdat(
customer=customer, username=username, password=password
)
self.__system_uuid = self.__get_orders_ids_in_local_system()
self.__system_last_update = self.__get_system_last_update()
def __get_orders_ids_in_local_system(self):
with open("./uuid.json", "r") as file:
system_uuids = [UUID(i) for i in json.load(file)["uuid"]]
return system_uuids
def __get_system_last_update(self):
with open("./uuid.json", "r") as file:
last_update = json.load(file)["last_update"]
return date.fromisoformat(last_update) if last_update else datetime.now().date()
def system_uuids(self):
return self.__system_uuid
def system_last_update(self):
return self.__system_last_update
def __fetch_combined_orders(self, uuid: UUID):
return self.__api.get_order(order_uuid=uuid, include_up_to_date=True)
def __fetch_single_order(self, uuid: UUID):
return self.__api.get_order(order_uuid=uuid, include_up_to_date=False)
def fetch_new_orders(self):
orders = [
i
for i in self.__api.get_orders_ids(self.__system_last_update)
if i.uuid not in self.__system_uuid
]
if len(orders) == 0:
return [], False, []
if len(orders) == 1:
return self.__fetch_single_order(orders[-1].uuid), False, orders
return self.__fetch_combined_orders(orders[0].uuid), True, orders
def replace_changes_in_local_system(self, system, api) -> list:
result = []
replaced_data = []
for sys in system["orders"]:
exists_in_api = False
for ap in api:
if (
sys["proveedor_ref"] == ap["proveedor_ref"]
and sys["producto_ref"] == ap["producto_ref"]
and sys["almacen_ref"] == ap["almacen_ref"]
and sys["fecha_emision"] == ap["fecha_emision"]
):
result.append(ap)
replaced_data.append(
f'{ap["proveedor_ref"]}-{ap["producto_ref"]}-{ap["almacen_ref"]}-{ap["fecha_emision"]}'
)
exists_in_api = True
logging.info(f"[*] Replacing order in local system - order {ap} ")
break
if exists_in_api == False:
result.append(sys)
# agragar los que no se reemplazaron
for ap in api:
if (
f'{ap["proveedor_ref"]}-{ap["producto_ref"]}-{ap["almacen_ref"]}-{ap["fecha_emision"]}'
not in replaced_data
):
logging.info(f"[+] Adding new order to local system - order {ap}")
result.append(ap)
return result
def add_order_in_local_system(self):
orders, combined, uuid_orders = self.fetch_new_orders()
if len(orders) == 0:
logging.debug("No new orders to add in local system")
return
existing_data_in_local_system = json.loads(open("./orders.json", "r").read())
orders = [i.asdict() for i in orders]
data_write_in_local_system = self.replace_changes_in_local_system(
existing_data_in_local_system, orders
)
open("./orders.json", "w").write(
json.dumps({"orders": data_write_in_local_system})
)
self.__system_uuid = [
*[i for i in self.__system_uuid],
*[i.uuid for i in uuid_orders],
]
open("./uuid.json", "w").write(
json.dumps(
{
"uuid": [str(i) for i in self.__system_uuid],
"last_update": datetime.now().date().strftime("%Y-%m-%d"),
}
)
)