Files
@ 2ea1af3149c2
Branch filter:
Location: ChaosStuff/stockcli/stockcli/stock.py
2ea1af3149c2
7.6 KiB
text/x-python
Add more debug logging
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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | import logging
from operator import itemgetter
from typing import Any
from rich.panel import Panel
from rich.table import Table
from . import utils
from .console import DEFAULT_PADDING, console, int_prompt, prompt
from .style import GreenBoldText
def get_info_by_barcode(barcode: str) -> None:
product = utils.get_request(
f"stock/products/by-barcode/{barcode}"
) # type: dict[str, Any]
inner_product = product["product"]
product_id = inner_product["id"]
product_group = utils.get_request(
f"objects/product_groups/{inner_product['product_group_id']}"
) # type: dict[str, Any]
grid = Table.grid(padding=DEFAULT_PADDING)
grid.add_column(justify="left", no_wrap=True)
grid.add_column(justify="right", style="cyan", no_wrap=True)
grid.add_column(justify="left", no_wrap=True)
grid.add_row(GreenBoldText("Product ID:"), product_id)
grid.add_row(GreenBoldText("Product Name:"), inner_product["name"])
grid.add_row(GreenBoldText("Product Group:"), product_group["name"])
for i, barcode_data in enumerate(product["product_barcodes"], start=1):
grid.add_row(
GreenBoldText(f"Barcode N°{i}:"),
barcode_data["barcode"],
barcode_data["note"],
)
purchase_quantity_unit = product["default_quantity_unit_purchase"]
stock_quantity_unit = product["quantity_unit_stock"]
purchase_to_stock_conversion = utils.get_request(
f"objects/quantity_unit_conversions?query[]=from_qu_id={purchase_quantity_unit['id']}&query[]=to_qu_id={stock_quantity_unit['id']}"
) # type: list[dict[str, Any]]
if len(purchase_to_stock_conversion) != 0:
conversion = f"{purchase_to_stock_conversion[0]['factor']} {stock_quantity_unit['name_plural']}"
else:
conversion = ""
grid.add_row(
GreenBoldText("Purchase Quantity Unit:"),
purchase_quantity_unit["name"],
conversion,
)
grid.add_row(
GreenBoldText("Stock Quantity Unit:"),
stock_quantity_unit["name"],
)
stock_amount = int(product["stock_amount"])
grid.add_row(GreenBoldText("Overall Stock Amount:"), str(stock_amount))
if stock_amount > 0:
grid.add_row(GreenBoldText("Locations:"))
locations = utils.get_request(
f"stock/products/{product_id}/locations"
) # type: list[dict[str, Any]]
for location in sorted(locations, key=itemgetter("location_name")):
grid.add_row(
f"[green bold]-[/green bold] [magenta]{location['location_name']}[/magenta]",
location["amount"],
)
console.print(Panel(grid, title="[green bold]Product Info[/green bold]"))
return
def transfer_by_barcode(barcode: str) -> None:
product = utils.get_request(
f"stock/products/by-barcode/{barcode}"
) # type: dict[str, Any]
inner_product = product["product"]
product_id = inner_product["id"]
locations = utils.get_request(
f"stock/products/{product_id}/locations"
) # type: list[dict[str, Any]]
all_locations = utils.get_request("objects/locations") # type: list[dict[str, Any]]
grid = Table.grid(padding=DEFAULT_PADDING)
grid.add_column(justify="right", no_wrap=True)
grid.add_column(justify="left", style="cyan", no_wrap=True)
grid.add_column(justify="right", style="magenta", no_wrap=True)
grid.add_row(GreenBoldText("Product ID:"), product_id)
grid.add_row(GreenBoldText("Product Name:"), inner_product["name"])
grid.add_row(GreenBoldText("Locations:"))
choices = []
for location in sorted(locations, key=itemgetter("location_id")):
location_id = location["location_id"]
choices.append(location_id)
grid.add_row(
f"[blue]{location_id}[/blue]",
location["location_name"],
location["amount"],
)
choices.append("0")
console.print(
Panel(
grid,
title="[green bold]Product Info[/green bold]",
subtitle="[blue]Transfer stock[/blue]",
)
)
from_id = int_prompt.ask(
"From (Enter 0 to abort)",
choices=choices,
default=int(choices[0]),
)
if not from_id:
logging.debug("User aborted task!")
return
grid = Table.grid(padding=DEFAULT_PADDING)
grid.add_column(justify="right", style="blue", no_wrap=True)
grid.add_column(justify="left", style="cyan", no_wrap=True)
choices = []
for location in sorted(all_locations, key=itemgetter("id")):
location_id = location["id"]
if int(location_id) != from_id:
choices.append(location_id)
grid.add_row(location_id, location["name"])
choices.append("0")
console.print(
Panel(
grid,
title="[green bold]Locations[/green bold]",
subtitle="[blue]Transfer Stock[/blue]",
)
)
to_id = int_prompt.ask(
"To (Enter 0 to abort)",
choices=choices,
default=int(choices[0]),
)
if not to_id:
logging.debug("User aborted task!")
return
amount = int_prompt.ask("Amount (Enter 0 to abort)")
if not amount:
logging.debug("User aborted task!")
return
data = {"amount": amount, "location_id_from": from_id, "location_id_to": to_id}
response = utils.post_request(
f"stock/products/by-barcode/{barcode}/transfer", data
) # type: dict[str, Any]
console.print("Successfully transfered!")
return
def add_by_barcode(barcode: str) -> None:
product = utils.get_request(
f"stock/products/by-barcode/{barcode}"
) # type: dict[str, Any]
inner_product = product["product"]
product_id = inner_product["id"]
grid = Table.grid(padding=DEFAULT_PADDING)
grid.add_column(justify="right", no_wrap=True)
grid.add_column(justify="left", style="cyan", no_wrap=True)
grid.add_row(GreenBoldText("Product ID:"), product_id)
grid.add_row(GreenBoldText("Product Name:"), inner_product["name"])
console.print(
Panel(
grid,
title="[green bold]Product Info[/green bold]",
subtitle="[blue]Add Stock[/blue]",
)
)
amount = int_prompt.ask("Amount (Enter 0 to abort)")
if not amount:
logging.debug("User aborted task!")
return
data = {"amount": amount, "transaction_type": "purchase"}
response = utils.post_request(
f"stock/products/by-barcode/{barcode}/add", data
) # type: dict[str, Any]
console.print("Successfully added!")
return
def update_by_barcode(barcode: str) -> None:
product = utils.get_request(
f"stock/products/by-barcode/{barcode}"
) # type: dict[str, Any]
inner_product = product["product"]
product_id = inner_product["id"]
grid = Table.grid(padding=DEFAULT_PADDING)
grid.add_column(justify="right", no_wrap=True)
grid.add_column(justify="left", style="cyan", no_wrap=True)
grid.add_row(GreenBoldText("Product ID:"), product_id)
grid.add_row(GreenBoldText("Product Name:"), inner_product["name"])
grid.add_row(GreenBoldText("Overall Stock Amount:"), product["stock_amount"])
console.print(
Panel(
grid,
title="[green bold]Product Info[/green bold]",
subtitle="[blue]Update stock[/blue]",
)
)
amount = int_prompt.ask("Amount (Enter 0 to abort)")
if not amount:
logging.debug("User aborted task!")
return
data = {
"new_amount": amount,
}
response = utils.post_request(
f"stock/products/{product_id}/inventory", data
) # type: dict[str, Any]
console.print("Successfully updated!")
return
|