Skip to content

Commit

Permalink
add stock requests, etc WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
erikvw committed Nov 3, 2024
1 parent cfefee3 commit 25ff9e8
Show file tree
Hide file tree
Showing 39 changed files with 2,028 additions and 225 deletions.
6 changes: 3 additions & 3 deletions edc_pharmacy/admin/medication/assignment_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ class AssignmentAdmin(ModelAdminMixin, admin.ModelAdmin):
fieldsets = (
(
None,
{"fields": ["assignment", "display_label"]},
{"fields": ["name", "display_label"]},
),
audit_fieldset_tuple,
)

list_display: Tuple[str, ...] = (
"assignment",
"name",
"display_label",
"created",
"modified",
)

search_fields: Tuple[str, ...] = ("assignment", "display_label")
search_fields: Tuple[str, ...] = ("name", "display_label")
40 changes: 37 additions & 3 deletions edc_pharmacy/admin/stock/container_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,27 @@

@admin.register(Container, site=edc_pharmacy_admin)
class ContainerAdmin(ModelAdminMixin, admin.ModelAdmin):
change_list_title = "Pharmacy: Containers"
show_object_tools = True

form = ContainerForm

fieldsets = (
(
None,
{"fields": (["name", "container_type", "units", "qty", "may_receive_as"])},
{
"fields": (
[
"name",
"container_type",
"units",
"qty",
"may_order_as",
"may_receive_as",
"may_request_as",
]
)
},
),
audit_fieldset_tuple,
)
Expand All @@ -26,11 +39,32 @@ class ContainerAdmin(ModelAdminMixin, admin.ModelAdmin):
"container_type",
"units",
"qty",
"may_receive_as",
"may_order",
"may_receive",
"may_request",
"created",
"modified",
)
list_filter = ("container_type", "units", "may_receive_as", "created")
list_filter = (
"container_type",
"units",
"may_order_as",
"may_receive_as",
"may_request_as",
"created",
)
radio_fields = {"container_type": admin.VERTICAL, "units": admin.VERTICAL}
search_fields = ("name",)
ordering = ("name",)

@admin.display(description="order", ordering="may_order_as")
def may_order(self, obj):
return obj.may_order_as

@admin.display(description="receive", ordering="may_receive_as")
def may_receive(self, obj):
return obj.may_receive_as

@admin.display(description="request", ordering="may_request_as")
def may_request(self, obj):
return obj.may_request_as
25 changes: 18 additions & 7 deletions edc_pharmacy/admin/stock/order_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from ...admin_site import edc_pharmacy_admin
from ...forms import OrderForm, OrderItemForm
from ...models import Order, OrderItem
from ...models import Order, OrderItem, ReceiveItem
from ..model_admin_mixin import ModelAdminMixin


Expand All @@ -33,6 +33,7 @@ class OrderItemInlineAdmin(TabularInlineMixin, admin.TabularInline):

@admin.register(Order, site=edc_pharmacy_admin)
class OrderAdmin(ModelAdminMixin, admin.ModelAdmin):
change_list_title = "Pharmacy: Orders"
show_object_tools = True
show_cancel = True

Expand Down Expand Up @@ -64,8 +65,9 @@ class OrderAdmin(ModelAdminMixin, admin.ModelAdmin):
"sent",
"item_count",
"add_order_item",
"items",
"status",
"items",
"receive_changelist",
"created",
"modified",
)
Expand All @@ -78,19 +80,28 @@ class OrderAdmin(ModelAdminMixin, admin.ModelAdmin):
def identifier(self, obj):
return obj.order_identifier

@admin.display(description="Order items", ordering="-order_identifier")
@admin.display(description="Orders", ordering="-order_identifier")
def items(self, obj):
url = reverse("edc_pharmacy_admin:edc_pharmacy_orderitem_changelist")
url = f"{url}?q={obj.order_identifier}"
context = dict(url=url, label="Order items", title="Go to items on this order")
return render_to_string("edc_pharmacy/stock/items.html", context=context)
context = dict(url=url, label="Ordered items", title="Go to items on this order")
return render_to_string("edc_pharmacy/stock/items_as_link.html", context=context)

@admin.display(description="Add order item")
@admin.display(description="Add item")
def add_order_item(self, obj):
if obj.item_count > OrderItem.objects.filter(order=obj).count():
url = reverse("edc_pharmacy_admin:edc_pharmacy_orderitem_add")
next_url = "edc_pharmacy_admin:edc_pharmacy_order_changelist"
url = f"{url}?next={next_url}&order={str(obj.id)}&q={str(obj.order_identifier)}"
context = dict(url=url, label="Add order item")
return render_to_string("edc_pharmacy/stock/items.html", context=context)
return render_to_string("edc_pharmacy/stock/items_as_button.html", context=context)
return None

@admin.display(description="Receiving")
def receive_changelist(self, obj):
if ReceiveItem.objects.filter(receive__order=obj).exists():
url = reverse("edc_pharmacy_admin:edc_pharmacy_receiveitem_changelist")
url = f"{url}?q={str(obj.order_identifier)}"
context = dict(url=url, label="Received items", title="Received items")
return render_to_string("edc_pharmacy/stock/items_as_link.html", context=context)
return None
46 changes: 31 additions & 15 deletions edc_pharmacy/admin/stock/order_item_admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from decimal import Decimal

from django.contrib import admin
from django.template.loader import render_to_string
from django.urls import reverse
Expand All @@ -12,7 +14,8 @@

@admin.register(OrderItem, site=edc_pharmacy_admin)
class OrderItemAdmin(ModelAdminMixin, admin.ModelAdmin):
show_object_tools = True
change_list_title = "Pharmacy: Ordered items"
show_object_tools = False
show_cancel = True
form = OrderItemForm
ordering = ("-order_item_identifier",)
Expand Down Expand Up @@ -41,13 +44,14 @@ class OrderItemAdmin(ModelAdminMixin, admin.ModelAdmin):

list_display = (
"identifier",
"change_order_url",
"product_name",
"container_name",
"qty",
"received_items_url",
"received_items_changelist",
"status",
"order_changelist",
"receive_url",
"stock_changelist",
"created",
"modified",
)
Expand All @@ -72,30 +76,41 @@ def container_name(self, obj):
return obj.container.name

@admin.display(description="Order #", ordering="-order__order_datetime")
def change_order_url(self, obj):
def order_changelist(self, obj):
url = reverse("edc_pharmacy_admin:edc_pharmacy_order_changelist")
url = f"{url}?q={obj.order.order_identifier}"
context = dict(url=url, label=obj.order.order_identifier, title="Back to order")
return render_to_string("edc_pharmacy/stock/items.html", context=context)
return render_to_string("edc_pharmacy/stock/items_as_link.html", context=context)

@admin.display(description="Receive item")
def changelist_receive_item_url(self, obj):
if obj.container_qty == obj.container_qty_received:
if obj.container.qty == obj.unit_qty_received:
url = reverse("edc_pharmacy_admin:edc_pharmacy_receiveitem_changelist")
url = f"{url}?q={obj.order.order_identifier}"
context = dict(url=url, label="Receive more", title="Receive more")
return render_to_string("edc_pharmacy/stock/items.html", context=context)
return render_to_string("edc_pharmacy/stock/items_as_link.html", context=context)
return None

@admin.display(description="qty received", ordering="container_qty_received")
def received_items_url(self, obj):
if obj.container_qty_received > 0:
@admin.display(description="not received", ordering="unit_qty_received")
def received_items_changelist(self, obj):
if (obj.unit_qty_received or Decimal(0)) > Decimal(0):
url = reverse("edc_pharmacy_admin:edc_pharmacy_receiveitem_changelist")
url = f"{url}?q={str(obj.id)}"
context = dict(url=url, label=obj.unit_qty_received)
return render_to_string("edc_pharmacy/stock/items.html", context=context)
context = dict(
url=url,
label=obj.unit_qty - obj.unit_qty_received,
title="Go to received item(s) for this ordered item",
)
return render_to_string("edc_pharmacy/stock/items_as_link.html", context=context)
return None

@admin.display(description="Stock")
def stock_changelist(self, obj):
url = reverse("edc_pharmacy_admin:edc_pharmacy_stock_changelist")
url = f"{url}?q={obj.order.order_identifier}"
context = dict(url=url, label="Stock", title="Go to stock")
return render_to_string("edc_pharmacy/stock/items_as_link.html", context=context)

@admin.display(description="Receiving")
def receive_url(self, obj):
try:
Expand All @@ -121,9 +136,10 @@ 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="Received", title="Received")
return render_to_string("edc_pharmacy/stock/items.html", context=context)
url = f"{url}?q={str(obj.order.order_identifier)}"
context = dict(url=url, label="Received items", title="Received items")
return render_to_string("edc_pharmacy/stock/items_as_link.html", context=context)
return render_to_string("edc_pharmacy/stock/items_as_button.html", context=context)

@admin.display(description="ID", ordering="-order_item_identifier")
def identifier(self, obj):
Expand Down
7 changes: 5 additions & 2 deletions edc_pharmacy/admin/stock/product_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

@admin.register(Product, site=edc_pharmacy_admin)
class ProductAdmin(ModelAdminMixin, admin.ModelAdmin):
change_list_title = "Pharmacy: Products"
show_object_tools = True
show_cancel = True

form = ProductForm

Expand All @@ -31,10 +33,10 @@ class ProductAdmin(ModelAdminMixin, admin.ModelAdmin):
)

list_display = (
"identifier",
"name",
"formulation",
"assignment",
"identifier",
"created",
"modified",
)
Expand All @@ -48,7 +50,8 @@ class ProductAdmin(ModelAdminMixin, admin.ModelAdmin):
)
ordering = ("product_identifier",)
readonly_fields = ("product_identifier", "name")
radio_fields = {"assignment": admin.VERTICAL}

@admin.display(description="Product identifier", ordering="product_identifier")
@admin.display(description="ID", ordering="product_identifier")
def identifier(self, obj):
return obj.product_identifier.split("-")[0]
26 changes: 18 additions & 8 deletions edc_pharmacy/admin/stock/receive_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ class ReceiveItemInlineAdmin(TabularInlineMixin, admin.TabularInline):

@admin.register(Receive, site=edc_pharmacy_admin)
class ReceiveAdmin(ModelAdminMixin, admin.ModelAdmin):
show_object_tools = True
change_list_title = "Pharmacy: Receiving"
show_object_tools = False
show_cancel = True

form = ReceiveForm
# inlines = [ReceiveItemInlineAdmin]
Expand All @@ -60,9 +62,10 @@ class ReceiveAdmin(ModelAdminMixin, admin.ModelAdmin):
list_display = (
"identifier",
"receive_datetime",
"items",
"location",
"order",
"item_count",
"order_changelist",
"items",
"created",
"modified",
)
Expand All @@ -73,7 +76,7 @@ class ReceiveAdmin(ModelAdminMixin, admin.ModelAdmin):
"modified",
)
search_fields = (
"receive_identifier",
"id",
"order__order_identifier",
"location__name",
)
Expand All @@ -82,9 +85,16 @@ class ReceiveAdmin(ModelAdminMixin, admin.ModelAdmin):
def identifier(self, obj):
return obj.receive_identifier

@admin.display(description="Receive items", ordering="receive_identifier")
@admin.display(description="Received items", ordering="receive_identifier")
def items(self, obj):
url = reverse("edc_pharmacy_admin:edc_pharmacy_receiveitem_changelist")
url = f"{url}?q={obj.receive_identifier}"
context = dict(url=url, label="Receive items", title="Go to received items")
return render_to_string("edc_pharmacy/stock/items.html", context=context)
url = f"{url}?q={obj.id}"
context = dict(url=url, label="Received items", title="Go to received items")
return render_to_string("edc_pharmacy/stock/items_as_link.html", context=context)

@admin.display(description="Order #")
def order_changelist(self, obj):
url = reverse("edc_pharmacy_admin:edc_pharmacy_order_changelist")
url = f"{url}?q={str(obj.order.order_identifier)}"
context = dict(url=url, label=obj.order.order_identifier, title="Back to order")
return render_to_string("edc_pharmacy/stock/items_as_link.html", context=context)
Loading

0 comments on commit 25ff9e8

Please sign in to comment.