Changeset - 57d1c880665b
[Not reviewed]
default
0 1 0
Dennis Fink - 2 years ago 2022-02-28 15:43:59
dennis.fink@c3l.lu
Update
1 file changed with 44 insertions and 25 deletions:
0 comments (0 inline, 0 general)
stockcli/stock.py
Show inline comments
 
from operator import itemgetter
 
from typing import Any
 

	
 
from rich.panel import Panel
 
from rich.table import Table
 
@@ -10,14 +11,16 @@ from .style import GreenBoldText
 

	
 
def get_info_by_barcode(barcode: str) -> None:
 

	
 
    product = utils.get_request(f"stock/products/by-barcode/{barcode}")
 
    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)
 
@@ -40,7 +43,7 @@ def get_info_by_barcode(barcode: str) ->
 

	
 
    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']}"
 
@@ -62,7 +65,9 @@ def get_info_by_barcode(barcode: str) ->
 

	
 
    if stock_amount > 0:
 
        grid.add_row(GreenBoldText("Locations:"))
 
        locations = utils.get_request(f"stock/products/{product_id}/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(
 
@@ -77,22 +82,26 @@ def get_info_by_barcode(barcode: str) ->
 

	
 
def transfer_by_barcode(barcode: str) -> None:
 

	
 
    product = utils.get_request(f"stock/products/by-barcode/{barcode}")
 
    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")
 
    all_locations = utils.get_request("objects/locations")
 
    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", style="green", no_wrap=True)
 
    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("Product ID:", product_id, "")
 
    grid.add_row("Product Name:", inner_product["name"], "")
 
    grid.add_row("Locations:", "", "")
 
    grid.add_row(GreenBoldText("Product ID:"), product_id)
 
    grid.add_row(GreenBoldText("Product Name:"), inner_product["name"])
 
    grid.add_row(GreenBoldText("Locations:"))
 

	
 
    choices = []
 

	
 
@@ -124,7 +133,7 @@ def transfer_by_barcode(barcode: str) ->
 
        return
 

	
 
    grid = Table.grid(padding=DEFAULT_PADDING)
 
    grid.add_column(justify="right", style="green", no_wrap=True)
 
    grid.add_column(justify="right", style="blue", no_wrap=True)
 
    grid.add_column(justify="left", style="cyan", no_wrap=True)
 

	
 
    choices = []
 
@@ -132,7 +141,7 @@ def transfer_by_barcode(barcode: str) ->
 
        location_id = location["id"]
 
        if int(location_id) != from_id:
 
            choices.append(location_id)
 
            grid.add_row(f"[blue]{location_id}[/blue]", location["name"])
 
            grid.add_row(location_id, location["name"])
 

	
 
    choices.append("0")
 
    console.print(
 
@@ -159,23 +168,27 @@ def transfer_by_barcode(barcode: str) ->
 

	
 
    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)
 
    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}")
 
    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", style="green", no_wrap=True)
 
    grid.add_column(justify="right", no_wrap=True)
 
    grid.add_column(justify="left", style="cyan", no_wrap=True)
 
    grid.add_row("Product ID:", product_id)
 
    grid.add_row("Product Name:", inner_product["name"])
 
    grid.add_row(GreenBoldText("Product ID:"), product_id)
 
    grid.add_row(GreenBoldText("Product Name:"), inner_product["name"])
 
    console.print(
 
        Panel(
 
            grid,
 
@@ -187,25 +200,29 @@ def add_by_barcode(barcode: str) -> None
 
    amount = int_prompt.ask("Amount")
 
    data = {"amount": amount, "transaction_type": "purchase"}
 

	
 
    response = utils.post_request(f"stock/products/by-barcode/{barcode}/add", data)
 
    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}")
 
    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", style="green", no_wrap=True)
 
    grid.add_column(justify="right", no_wrap=True)
 
    grid.add_column(justify="left", style="cyan", no_wrap=True)
 

	
 
    grid.add_row("Product ID:", product_id)
 
    grid.add_row("Product Name:", inner_product["name"])
 
    grid.add_row("Overall Stock Amount:", product["stock_amount"])
 
    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(
 
@@ -220,6 +237,8 @@ def update_by_barcode(barcode: str) -> N
 
        "new_amount": amount,
 
    }
 

	
 
    response = utils.post_request(f"stock/products/{product_id}/inventory", data)
 
    response = utils.post_request(
 
        f"stock/products/{product_id}/inventory", data
 
    )  # type: dict[str, Any]
 
    console.print("Successfully updated!")
 
    return
0 comments (0 inline, 0 general)