Skip to content

Commit

Permalink
refactor qty fields
Browse files Browse the repository at this point in the history
  • Loading branch information
erikvw committed Nov 2, 2024
1 parent 548ef2d commit cfefee3
Show file tree
Hide file tree
Showing 25 changed files with 599 additions and 256 deletions.
11 changes: 6 additions & 5 deletions edc_pharmacy/admin/stock/container_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,21 @@ class ContainerAdmin(ModelAdminMixin, admin.ModelAdmin):
fieldsets = (
(
None,
{"fields": (["name", "container_type", "container_units", "container_qty"])},
{"fields": (["name", "container_type", "units", "qty", "may_receive_as"])},
),
audit_fieldset_tuple,
)

list_display = (
"name",
"container_type",
"container_units",
"container_qty",
"units",
"qty",
"may_receive_as",
"created",
"modified",
)
list_filter = ("container_type", "container_units", "created")
radio_fields = {"container_type": admin.VERTICAL, "container_units": admin.VERTICAL}
list_filter = ("container_type", "units", "may_receive_as", "created")
radio_fields = {"container_type": admin.VERTICAL, "units": admin.VERTICAL}
search_fields = ("name",)
ordering = ("name",)
16 changes: 4 additions & 12 deletions edc_pharmacy/admin/stock/order_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class OrderItemInlineAdmin(TabularInlineMixin, admin.TabularInline):
[
"product",
"container",
"unit_qty",
"qty",
]
)
},
Expand Down Expand Up @@ -53,14 +53,8 @@ class OrderAdmin(ModelAdminMixin, admin.ModelAdmin):
)
},
),
(
"Quantity",
{"fields": (["item_count", "unit_qty", "container_qty"])},
),
(
"Status",
{"fields": (["sent", "status"])},
),
("Quantity", {"fields": (["item_count"])}),
("Status", {"fields": (["sent", "status"])}),
audit_fieldset_tuple,
)

Expand All @@ -72,15 +66,13 @@ class OrderAdmin(ModelAdminMixin, admin.ModelAdmin):
"add_order_item",
"items",
"status",
"unit_qty",
"container_qty",
"created",
"modified",
)
list_filter = ("sent", "status", "order_datetime")
radio_fields = {"status": admin.VERTICAL}
search_fields = ("id", "order_identifier")
readonly_fields = ("order_identifier", "unit_qty", "container_qty", "sent")
readonly_fields = ("order_identifier", "sent")

@admin.display(description="Order #", ordering="-order_identifier")
def identifier(self, obj):
Expand Down
23 changes: 10 additions & 13 deletions edc_pharmacy/admin/stock/order_item_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,9 @@ class OrderItemAdmin(ModelAdminMixin, admin.ModelAdmin):
),
(
"Quantity",
{"fields": (["unit_qty", "container_qty", "container_qty_received"])},
),
(
"Status",
{"fields": (["status"])},
{"fields": (["qty", "unit_qty", "unit_qty_received"])},
),
("Status", {"fields": (["status"])}),
audit_fieldset_tuple,
)

Expand All @@ -47,7 +44,7 @@ class OrderItemAdmin(ModelAdminMixin, admin.ModelAdmin):
"change_order_url",
"product_name",
"container_name",
"unit_qty",
"qty",
"received_items_url",
"status",
"receive_url",
Expand All @@ -62,13 +59,13 @@ class OrderItemAdmin(ModelAdminMixin, admin.ModelAdmin):
"order__order_identifier",
)
readonly_fields = (
"container_qty",
"container_qty_received",
"unit_qty",
"unit_qty_received",
)

@admin.display(description="Product", ordering="product__name")
def product_name(self, obj):
return obj.product.name
return obj.product.formulation.description_with_assignment(obj.product.assignment)

@admin.display(description="Container", ordering="container__name")
def container_name(self, obj):
Expand All @@ -95,7 +92,7 @@ def received_items_url(self, obj):
if obj.container_qty_received > 0:
url = reverse("edc_pharmacy_admin:edc_pharmacy_receiveitem_changelist")
url = f"{url}?q={str(obj.id)}"
context = dict(url=url, label=obj.container_qty_received)
context = dict(url=url, label=obj.unit_qty_received)
return render_to_string("edc_pharmacy/stock/items.html", context=context)
return None

Expand All @@ -107,10 +104,10 @@ def receive_url(self, obj):
rcv_obj = None
if not rcv_obj:
url = reverse("edc_pharmacy_admin:edc_pharmacy_receive_add")
next_url = "edc_pharmacy_admin:edc_pharmacy_orderitem_add"
next_url = "edc_pharmacy_admin:edc_pharmacy_orderitem_changelist"
url = f"{url}?next={next_url}&q={str(obj.order.id)}&order={str(obj.order.id)}"
context = dict(
url=url, label="Receive item", title="Receive against this order item"
url=url, label="Start Receiving", title="Receive against this order item"
)
elif obj.status != COMPLETE:
url = reverse("edc_pharmacy_admin:edc_pharmacy_receiveitem_add")
Expand All @@ -125,7 +122,7 @@ def receive_url(self, obj):
else:
url = reverse("edc_pharmacy_admin:edc_pharmacy_receiveitem_changelist")
url = f"{url}?q={str(obj.id)}"
context = dict(url=url, label="Receiving", title="Receiving")
context = dict(url=url, label="Received", title="Received")
return render_to_string("edc_pharmacy/stock/items.html", context=context)

@admin.display(description="ID", ordering="-order_item_identifier")
Expand Down
2 changes: 1 addition & 1 deletion edc_pharmacy/admin/stock/product_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ProductAdmin(ModelAdminMixin, admin.ModelAdmin):
"lot__lot_no",
)
ordering = ("product_identifier",)
readonly_fields = ("product_identifier",)
readonly_fields = ("product_identifier", "name")

@admin.display(description="Product identifier", ordering="product_identifier")
def identifier(self, obj):
Expand Down
5 changes: 3 additions & 2 deletions edc_pharmacy/admin/stock/receive_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ class ReceiveAdmin(ModelAdminMixin, admin.ModelAdmin):
show_object_tools = True

form = ReceiveForm
inlines = [ReceiveItemInlineAdmin]
insert_before_fieldset = "Audit"
# inlines = [ReceiveItemInlineAdmin]
# insert_before_fieldset = "Audit"
ordering = ("-receive_identifier",)
autocomplete_fields = ["order"]

fieldsets = (
(
Expand Down
7 changes: 4 additions & 3 deletions edc_pharmacy/admin/stock/receive_item_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
@admin.register(ReceiveItem, site=edc_pharmacy_admin)
class ReceiveItemAdmin(ModelAdminMixin, admin.ModelAdmin):
show_object_tools = True
show_cancel = True

form = ReceiveItemForm
include_audit_fields_in_list_display = False
Expand All @@ -32,8 +33,8 @@ class ReceiveItemAdmin(ModelAdminMixin, admin.ModelAdmin):
"Quantity",
{
"fields": (
"qty",
"unit_qty",
"container_qty",
"added_to_stock",
)
},
Expand All @@ -43,8 +44,8 @@ class ReceiveItemAdmin(ModelAdminMixin, admin.ModelAdmin):

list_display = (
"identifier",
"qty",
"unit_qty",
"container_qty",
"added_to_stock",
"change_order_url",
"change_order_items_url",
Expand All @@ -67,7 +68,7 @@ class ReceiveItemAdmin(ModelAdminMixin, admin.ModelAdmin):
"container__name",
)

readonly_fields = ("container_qty",)
readonly_fields = ("unit_qty",)

@admin.display(description="Receive", ordering="-receive__receive_datetime")
def change_receive_url(self, obj):
Expand Down
26 changes: 7 additions & 19 deletions edc_pharmacy/admin/stock/stock_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,7 @@ class StockAdmin(ModelAdminMixin, admin.ModelAdmin):
),
(
"Quantity",
{
"fields": (
"unit_qty_in",
"unit_qty_out",
"container_qty",
"container_qty_in",
"container_qty_out",
)
},
{"fields": ("qty_in", "qty_out")},
),
audit_fieldset_tuple,
)
Expand All @@ -47,11 +39,8 @@ class StockAdmin(ModelAdminMixin, admin.ModelAdmin):
"identifier",
"product",
"container",
"unit_qty_in",
"unit_qty_out",
"container_qty",
"container_qty_in",
"container_qty_out",
"qty_in",
"qty_out",
"created",
"modified",
)
Expand All @@ -66,11 +55,10 @@ class StockAdmin(ModelAdminMixin, admin.ModelAdmin):
readonly_fields = (
"stock_identifier",
"product",
"unit_qty_in",
"unit_qty_out",
"container_qty",
"container_qty_in",
"container_qty_out",
"receive_item",
"container",
"qty_in",
"qty_out",
)

@admin.display(description="Identifier", ordering="-stock_datetime")
Expand Down
3 changes: 2 additions & 1 deletion edc_pharmacy/forms/stock/receive_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class ReceiveForm(forms.ModelForm):
class Meta:
model = Receive
fields = "__all__"
help_text = {"receive_identifier": "(read-only)"}
help_text = {"receive_identifier": "(read-only)", "order": "(read-only)"}
widgets = {
"receive_identifier": forms.TextInput(attrs={"readonly": "readonly"}),
"order": forms.TextInput(attrs={"readonly": "readonly"}),
}
25 changes: 18 additions & 7 deletions edc_pharmacy/forms/stock/receive_item_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,24 @@ class ReceiveItemForm(forms.ModelForm):

def clean(self):
cleaned_data = super().clean()
container_qty = self._meta.model.objects.filter(
order_item=cleaned_data.get("order_item")
).aggregate(qty=Sum("container_qty"))["qty"]
if (
cleaned_data.get("unit_qty") * cleaned_data.get("container").container_qty
) + container_qty > cleaned_data.get("order_item").container_qty:
raise forms.ValidationError({"unit_qty": "Exceeds ordered amount"})
unit_qty = (
self._meta.model.objects.filter(
order_item=cleaned_data.get("order_item")
).aggregate(unit_qty=Sum("unit_qty"))["unit_qty"]
or 0.0
)
if (float(cleaned_data.get("qty")) * float(cleaned_data.get("container").qty)) + float(
unit_qty
) > float(cleaned_data.get("order_item").unit_qty):
raise forms.ValidationError(
{
"qty": (
f"Exceeds `unit qty` ordered of "
f'{cleaned_data.get("order_item").unit_qty}. '
"Note: `Unit qty` is the `QTY` * `CONTAINER.QTY`"
)
}
)

return cleaned_data

Expand Down
8 changes: 7 additions & 1 deletion edc_pharmacy/forms/stock/stock_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ class StockForm(forms.ModelForm):
class Meta:
model = Stock
fields = "__all__"
help_text = {"stock_identifier": "(read-only)"}
help_text = {
"stock_identifier": "(read-only)",
"receive_item": "(read-only)",
"container": "(read-only)",
}
widgets = {
"stock_identifier": forms.TextInput(attrs={"readonly": "readonly"}),
"receive_item": forms.TextInput(attrs={"readonly": "readonly"}),
"container": forms.TextInput(attrs={"readonly": "readonly"}),
}
10 changes: 10 additions & 0 deletions edc_pharmacy/list_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,14 @@
(OTHER, "Other ..."),
(NOT_APPLICABLE, "Not applicable"),
],
"edc_pharmacy.containertype": [
("tablet", "Tablet"),
("bottle", "Bottle"),
],
"edc_pharmacy.containerunits": [
("tablet", "Tablet"),
],
"edc_pharmacy.location": [
("central", "Central"),
],
}
Loading

0 comments on commit cfefee3

Please sign in to comment.