-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathservices_inventory.py
More file actions
117 lines (100 loc) · 4.4 KB
/
Copy pathservices_inventory.py
File metadata and controls
117 lines (100 loc) · 4.4 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
117
from typing import Any
import httpx
from lnbits.core.crud import get_wallet
from lnbits.core.models import User
from lnbits.settings import settings
from .helpers import create_internal_user_access_token, from_csv, inventory_tags_to_list
async def deduct_inventory_stock(wallet_id: str, inventory_payload: dict) -> None:
wallet = await get_wallet(wallet_id)
if not wallet:
return
inventory_id = inventory_payload.get("inventory_id")
items = inventory_payload.get("items") or []
if not inventory_id or not items:
return
items_to_update = []
for item in items:
item_id = item.get("id")
qty = item.get("quantity") or 0
if not item_id or qty <= 0:
continue
items_to_update.append({"id": item_id, "quantity": int(qty)})
if not items_to_update:
return
ids = [item["id"] for item in items_to_update]
quantities = [item["quantity"] for item in items_to_update]
# Needed to accomodate admin users, as using user ID is not possible
access = create_internal_user_access_token(wallet.user)
async with httpx.AsyncClient() as client:
await client.patch(
url=f"http://{settings.host}:{settings.port}/inventory/api/v1/items/{inventory_id}/quantities",
headers={"Authorization": f"Bearer {access}"},
params={"source": "tpos", "ids": ids, "quantities": quantities},
)
return
async def get_default_inventory(user_id: str) -> dict[str, Any] | None:
access = create_internal_user_access_token(user_id)
async with httpx.AsyncClient() as client:
resp = await client.get(
url=f"http://{settings.host}:{settings.port}/inventory/api/v1",
headers={"Authorization": f"Bearer {access}"},
)
inventory = resp.json()
if not inventory:
return None
if isinstance(inventory, list):
inventory = inventory[0] if inventory else None
if not isinstance(inventory, dict):
return None
inventory["tags"] = inventory_tags_to_list(inventory.get("tags"))
inventory["omit_tags"] = inventory_tags_to_list(inventory.get("omit_tags"))
return inventory
async def get_inventory_items_for_tpos(
user_id: str,
inventory_id: str,
tags: str | list[str] | None,
omit_tags: str | list[str] | None,
) -> list[Any]:
tag_list = inventory_tags_to_list(tags)
omit_list = [tag.lower() for tag in inventory_tags_to_list(omit_tags)]
allowed_tags = [tag.lower() for tag in tag_list]
access = create_internal_user_access_token(user_id)
async with httpx.AsyncClient() as client:
resp = await client.get(
url=f"http://{settings.host}:{settings.port}/inventory/api/v1/items/{inventory_id}/paginated",
headers={"Authorization": f"Bearer {access}"},
params={"limit": 500, "offset": 0, "is_active": True},
)
payload = resp.json()
items = payload.get("data", []) if isinstance(payload, dict) else payload
# item images are a comma separated string; make a list
for item in items:
images = item.get("images")
item["images"] = from_csv(images)
def has_allowed_tag(item_tags: str | list[str] | None) -> bool:
# When no tags are configured for this TPoS, show no items
if not tag_list:
return False
item_tag_list = [tag.lower() for tag in inventory_tags_to_list(item_tags)]
return any(tag in item_tag_list for tag in allowed_tags)
def has_omit_tag(item_omit_tags: str | list[str] | None) -> bool:
if not omit_list:
return False
item_tag_list = [tag.lower() for tag in inventory_tags_to_list(item_omit_tags)]
return any(tag in item_tag_list for tag in omit_list)
filtered = [
item
for item in items
if has_allowed_tag(item.get("tags")) and not has_omit_tag(item.get("omit_tags"))
]
# If no items matched the provided tags, fall back to all items minus omitted ones.
if tag_list and not filtered:
filtered = [item for item in items if not has_omit_tag(item.get("omit_tags"))]
# hide items with no stock when stock tracking is enabled
return [
item
for item in filtered
if item.get("quantity_in_stock") is None or item.get("quantity_in_stock") > 0
]
def inventory_available_for_user(user: User | None) -> bool:
return bool(user and "inventory" in (user.extensions or []))