Add custom labels printed before box and canister slips
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
24
slips/api.py
24
slips/api.py
@@ -4,7 +4,7 @@ from django.shortcuts import get_object_or_404
|
||||
from django.utils import timezone
|
||||
from ninja import NinjaAPI, Query
|
||||
|
||||
from .models import Product, Order
|
||||
from .models import Product, Order, clean_custom_labels
|
||||
from .schemas import (
|
||||
ProductOut, ProductIn, OrderOut, OrderIn, OrderUpdateIn,
|
||||
CalibrationIn, CalibrationOut,
|
||||
@@ -93,6 +93,10 @@ def create_order(request, payload: OrderIn):
|
||||
else:
|
||||
production_date = None
|
||||
next_control_date = None
|
||||
try:
|
||||
custom_labels = clean_custom_labels(payload.custom_labels)
|
||||
except ValueError as exc:
|
||||
return 400, {'detail': str(exc)}
|
||||
order = Order(
|
||||
product=product,
|
||||
batch_number=payload.batch_number,
|
||||
@@ -101,6 +105,7 @@ def create_order(request, payload: OrderIn):
|
||||
total_weight=payload.total_weight,
|
||||
canister_weight=payload.canister_weight,
|
||||
canisters_per_box=payload.canisters_per_box,
|
||||
custom_labels=custom_labels,
|
||||
)
|
||||
order.apply_weight()
|
||||
order.save()
|
||||
@@ -111,16 +116,25 @@ def create_order(request, payload: OrderIn):
|
||||
def update_order(request, order_id: int, payload: OrderUpdateIn):
|
||||
order = get_object_or_404(Order, id=order_id)
|
||||
|
||||
if payload.canister_count is not None or payload.box_count is not None:
|
||||
counts_changed = payload.canister_count is not None or payload.box_count is not None
|
||||
custom_changed = payload.custom_labels is not None
|
||||
if counts_changed or custom_changed:
|
||||
if order.status != 'pending':
|
||||
return 400, {'detail': 'Only pending orders can change label counts'}
|
||||
cc = payload.canister_count if payload.canister_count is not None else order.canister_count
|
||||
bc = payload.box_count if payload.box_count is not None else order.box_count
|
||||
if cc < 0 or bc < 0:
|
||||
return 400, {'detail': 'counts must be >= 0'}
|
||||
if cc == 0 and bc == 0:
|
||||
return 400, {'detail': 'canister_count and box_count cannot both be 0'}
|
||||
order.set_counts(payload.canister_count, payload.box_count)
|
||||
try:
|
||||
custom = clean_custom_labels(payload.custom_labels) if custom_changed else (order.custom_labels or [])
|
||||
except ValueError as exc:
|
||||
return 400, {'detail': str(exc)}
|
||||
if cc == 0 and bc == 0 and not custom:
|
||||
return 400, {'detail': 'at least one label is required'}
|
||||
if counts_changed:
|
||||
order.set_counts(payload.canister_count, payload.box_count)
|
||||
if custom_changed:
|
||||
order.custom_labels = custom
|
||||
|
||||
if payload.status is not None:
|
||||
order.status = payload.status
|
||||
|
||||
Reference in New Issue
Block a user