-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit-package.py
More file actions
401 lines (329 loc) · 13.4 KB
/
split-package.py
File metadata and controls
401 lines (329 loc) · 13.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#!/usr/bin/env python3
# /// script
# requires-python = ">=3.8"
# dependencies = [
# "t3api_utils",
# "questionary"
# ]
# ///
# Usage: uv run split-package.py
#
# This script walks you through splitting an existing package into a new one.
#
# "Splitting" a package means taking some quantity from a source package and
# creating a new package from it. For example, you might split 5 grams out of
# a 100-gram source package into a new package with its own tag.
#
# The script will:
# 1. Authenticate you with the T3 API
# 2. Let you pick a license to work under
# 3. Load all available source packages, items, and tags
# 4. Prompt you through each decision (which package to split, how much, etc.)
# 5. Submit the new package creation request to the API
from datetime import date
from typing import NotRequired, TypedDict
import questionary
from t3api_utils.api.operations import send_api_request
from t3api_utils.api.parallel import load_all_data_sync
from t3api_utils.main.utils import (get_authenticated_client_or_error,
match_collection_from_csv, pick_license)
# ---------------------------------------------------------------------------
# Type definitions
#
# These TypedDict classes describe the shape of JSON objects returned by
# (or sent to) the T3 API. They help your editor provide autocomplete and
# catch typos when accessing dictionary keys.
# ---------------------------------------------------------------------------
class PackageItem(TypedDict):
"""The item (product type) associated with a package.
Every package is linked to an "item" which defines what kind of product
it contains (e.g. "Biomass", "Concentrate", etc.).
"""
id: int
"""Unique item ID. e.g. 6645."""
class SourceItem(TypedDict):
"""An available item that can be assigned to a new package.
Returned by GET /v2/packages/create/source-items. When creating a new
package, you can either reuse the source package's item or pick a
different one from this list.
"""
id: int
"""Unique item ID. e.g. 6645."""
name: str
"""Display name. e.g. "SBX Biomass SBX Strain 1 Item"."""
class SourcePackage(TypedDict):
"""An existing package that can be used as a source for splitting.
Returned by GET /v2/packages/create/source-packages. Each source package
has a unique tag label (like a barcode) and a quantity you can pull from.
"""
id: int
"""Unique package ID. e.g. 15481."""
item: PackageItem
"""The item associated with this package."""
label: str
"""Package tag label. e.g. "AAAFF03000001F9000001001"."""
unitOfMeasureId: int
"""Unit of measure ID for this package's quantity. e.g. 4 (Pounds)."""
class SourceTag(TypedDict):
"""A tag that can be assigned to a new package.
Tags are unique identifiers (like barcodes) that get physically attached
to packages. Each new package needs an unused tag. Returned by
GET /v2/packages/create/source-tags.
"""
id: int
"""Unique tag ID. e.g. 1527333."""
label: str
"""Tag label. e.g. "AAAFF03000001F9000001001"."""
class Location(TypedDict):
"""A physical location within a facility where packages can be stored."""
id: int
"""Unique location ID. e.g. 581."""
name: str
"""Display name of the location. e.g. "SBX Default Location 1"."""
class UnitOfMeasure(TypedDict):
"""A unit of measure for quantities (weight, volume, or count)."""
abbreviation: str
"""Short abbreviation. e.g. "g", "oz", "lb", "ml", "ea"."""
id: int
"""Unique unit of measure ID. e.g. 2."""
name: str
"""Display name. e.g. "Grams", "Ounces", "Pounds"."""
class PackageInputs(TypedDict):
"""Lookup data needed to create a package.
Returned by GET /v2/packages/create/inputs. Contains the lists of
available locations and units of measure for the selected license.
"""
locations: list[Location]
"""Available facility locations."""
unitsOfMeasure: list[UnitOfMeasure]
"""Available units of measure."""
class Ingredient(TypedDict):
"""Describes how much to pull from a source package.
When creating a new package, you specify one or more "ingredients" --
each ingredient references a source package and says how much quantity
to take from it.
Example::
{
"packageId": 5077333,
"quantity": 56.3,
"unitOfMeasureId": 4
}
"""
finishDate: NotRequired[str]
"""Optional date to mark the source package as finished (YYYY-MM-DD).
If set, the source package will be closed out on this date."""
packageId: int
"""ID of the source package to pull from."""
quantity: float
"""Amount to pull from the source package. e.g. 56.3."""
unitOfMeasureId: int
"""Unit of measure for the quantity. e.g. 4 (Pounds)."""
class CreatePackageBody(TypedDict):
"""The request body sent to POST /v2/packages/create.
This is the final payload that tells the API everything it needs to
create the new package: what to pull from (ingredients), how much to
produce (quantity), where to store it (locationId), and what tag to
assign (tagId).
Example::
{
"actualDate": "2024-08-08",
"ingredients": [{"packageId": 5077333, "quantity": 56.3, "unitOfMeasureId": 4}],
"itemId": 1160223,
"locationId": 50901,
"quantity": 123.45,
"unitOfMeasureId": 1
}
"""
actualDate: str
"""Date the package was created (YYYY-MM-DD)."""
ingredients: list[Ingredient]
"""List of source packages and quantities to pull from."""
itemId: int
"""Item (product type) ID for the new package."""
locationId: int
"""Location ID where the new package will be stored."""
quantity: float
"""Output quantity for the new package."""
tagId: int
"""Tag ID to assign to the new package."""
unitOfMeasureId: int
"""Unit of measure for the output quantity."""
useSameItem: NotRequired[bool]
"""If True, the new package keeps the same item as the source package."""
def main():
# -----------------------------------------------------------------------
# Step 1: Authenticate
#
# This prompts you to log in. You can authenticate via username/password,
# a JWT token (from the T3 Chrome extension), or an API key.
# -----------------------------------------------------------------------
api_client = get_authenticated_client_or_error()
# -----------------------------------------------------------------------
# Step 2: Pick a license
#
# Each facility operates under a license. This lets you choose which
# license (and therefore which facility's data) you want to work with.
# -----------------------------------------------------------------------
license = pick_license(api_client=api_client)
# -----------------------------------------------------------------------
# Step 3: Load reference data from the API
#
# We need to fetch all available source packages, items, tags, locations,
# and units of measure before we can present choices to the user.
# -----------------------------------------------------------------------
# All active packages that can be split from
packages: list[SourcePackage] = load_all_data_sync(
client=api_client,
path="/v2/packages/create/source-packages",
license_number=license["licenseNumber"],
)
# All items (product types) that can be assigned to a new package
items: list[SourceItem] = load_all_data_sync(
client=api_client,
path="/v2/packages/create/source-items",
license_number=license["licenseNumber"],
)
# All unused tags available for assignment to a new package
tags: list[SourceTag] = load_all_data_sync(
client=api_client,
path="/v2/packages/create/source-tags",
license_number=license["licenseNumber"],
)
# Locations and units of measure for the selected license
package_inputs: PackageInputs = send_api_request(
client=api_client,
path="/v2/packages/create/inputs",
method="GET",
params={
"licenseNumber": license["licenseNumber"],
},
)
locations = package_inputs["locations"]
units_of_measure = package_inputs["unitsOfMeasure"]
# -----------------------------------------------------------------------
# Step 4: Collect user inputs
#
# Walk the user through each decision needed to create the new package.
# -----------------------------------------------------------------------
# Pick which existing package to split from
source_package = questionary.select(
"Select source package:",
choices=[
questionary.Choice(title=p["label"], value=p)
for p in packages
],
).ask()
source_package_id = source_package["id"]
# The new package can keep the same item (product type) as the source,
# or you can assign a different item.
use_same_item = questionary.confirm(
"Use same item as source package?",
default=True,
).ask()
if use_same_item:
# Reuse the source package's item
item_id = source_package["item"]["id"]
else:
# Let the user pick a different item from all available items
item_id = questionary.select(
"Select item:",
choices=[
questionary.Choice(title=i["name"], value=i["id"])
for i in items
],
).ask()
# When was this package actually created? Defaults to today.
today = date.today().isoformat()
actual_date = input(f"Actual date (YYYY-MM-DD) [{today}]: ") or today
# Optional: if the source package should be marked as "finished" (fully
# used up) on a specific date, enter it here. Leave blank to skip.
finish_date = input("Finish date (YYYY-MM-DD) [skip]: ")
# How much to pull from the source package
ingredient_quantity = float(input("Ingredient quantity: "))
# What unit is that quantity in (grams, ounces, etc.)
# When using the same item, default to the source package's unit of measure.
uom_choices = [
questionary.Choice(title=f"{u['name']} ({u['abbreviation']})", value=u["id"])
for u in units_of_measure
]
default_uom_id = source_package["unitOfMeasureId"] if use_same_item else None
ingredient_uom_id = questionary.select(
"Select ingredient unit of measure:",
choices=uom_choices,
default=default_uom_id,
).ask()
# Where to store the new package
location_id = questionary.select(
"Select location:",
choices=[
questionary.Choice(title=u["name"], value=u["id"])
for u in locations
],
).ask()
# How much the new package will contain (may differ from ingredient
# quantity due to processing loss, unit conversion, etc.)
# If using the same item and same UoM, default to the ingredient quantity.
if use_same_item and ingredient_uom_id == default_uom_id:
output_quantity_str = input(f"Output quantity [{ingredient_quantity}]: ")
output_quantity = float(output_quantity_str) if output_quantity_str else ingredient_quantity
else:
output_quantity = float(input("Output quantity: "))
# What unit is the output quantity in
# Also defaults to the source package's unit of measure when using the same item.
output_uom_id = questionary.select(
"Select output unit of measure:",
choices=uom_choices,
default=default_uom_id,
).ask()
# Pick an unused tag to assign to the new package
tag_id = questionary.select(
"Select tag:",
choices=[
questionary.Choice(title=t["label"], value=t["id"])
for t in tags
],
).ask()
# -----------------------------------------------------------------------
# Step 5: Build and submit the API request
#
# Assemble the ingredient (what to pull from) and the package body
# (what to create), then POST it to the API.
# -----------------------------------------------------------------------
# The ingredient describes what we're pulling from the source package
ingredient: Ingredient = {
"packageId": source_package_id,
"quantity": ingredient_quantity,
"unitOfMeasureId": ingredient_uom_id,
}
if finish_date:
ingredient["finishDate"] = finish_date
# The body describes the new package we're creating
body: CreatePackageBody = {
"actualDate": actual_date,
"ingredients": [ingredient],
"itemId": item_id,
"locationId": location_id,
"quantity": output_quantity,
"tagId": tag_id,
"unitOfMeasureId": output_uom_id,
}
if use_same_item:
body["useSameItem"] = True
# Ask whether to submit to Metrc for real, or just save as a draft.
# submit=True sends the package to Metrc immediately and creates it.
# submit=False saves it as a draft in T3 that can be reviewed first.
submit = questionary.confirm(
"Submit to Metrc and create the package now?",
default=False,
).ask()
# The API expects a list of package bodies (to support batch creation),
# so we wrap our single body in a list.
send_api_request(
client=api_client,
path="/v2/packages/create",
method="POST",
params={"licenseNumber": license["licenseNumber"], "submit": submit},
json_body=[body],
)
if __name__ == "__main__":
main()