mirror of
https://github.com/Sosokker/Inventory-Management-System.git
synced 2025-12-19 23:44:05 +01:00
Create Signal to handle items transfer / finish up overview
This commit is contained in:
parent
d111057021
commit
44d1064503
@ -7,8 +7,8 @@ class WarehouseAdmin(admin.ModelAdmin):
|
|||||||
|
|
||||||
@admin.register(Inventory)
|
@admin.register(Inventory)
|
||||||
class InventoryAdmin(admin.ModelAdmin):
|
class InventoryAdmin(admin.ModelAdmin):
|
||||||
list_display = ('warehouse', 'max_stock', 'min_stock', 'current_stock')
|
list_display = ('stock_identifier', 'warehouse', 'max_stock', 'min_stock', 'current_stock')
|
||||||
|
|
||||||
@admin.register(Item)
|
@admin.register(Item)
|
||||||
class ItemAdmin(admin.ModelAdmin):
|
class ItemAdmin(admin.ModelAdmin):
|
||||||
list_display = ('inventory', 'name', 'description', 'category', 'weight', 'quantity')
|
list_display = ('name', 'description', 'category', 'weight', 'quantity', 'inventory')
|
||||||
@ -4,3 +4,6 @@ from django.apps import AppConfig
|
|||||||
class InventoryConfig(AppConfig):
|
class InventoryConfig(AppConfig):
|
||||||
default_auto_field = 'django.db.models.BigAutoField'
|
default_auto_field = 'django.db.models.BigAutoField'
|
||||||
name = 'inventory'
|
name = 'inventory'
|
||||||
|
|
||||||
|
def ready(self):
|
||||||
|
import inventory.signals
|
||||||
|
|||||||
@ -37,13 +37,17 @@ class Inventory(models.Model):
|
|||||||
"""
|
"""
|
||||||
warehouse = models.ForeignKey(Warehouse, on_delete=models.CASCADE)
|
warehouse = models.ForeignKey(Warehouse, on_delete=models.CASCADE)
|
||||||
max_stock = models.IntegerField()
|
max_stock = models.IntegerField()
|
||||||
min_stock = models.IntegerField()
|
min_stock = models.IntegerField(default=0)
|
||||||
current_stock = models.IntegerField()
|
current_stock = models.IntegerField(default=0)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def stock_percentage(self) -> float:
|
def stock_percentage(self) -> float:
|
||||||
return self.current_stock / self.max_stock * 100
|
return self.current_stock / self.max_stock * 100
|
||||||
|
|
||||||
|
@property
|
||||||
|
def stock_identifier(self) -> str:
|
||||||
|
return f"#{self.id} {self.warehouse.name}"
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.warehouse.name} - {self.stock_percentage}%"
|
return f"{self.warehouse.name} - {self.stock_percentage}%"
|
||||||
|
|
||||||
|
|||||||
41
inventory/signals.py
Normal file
41
inventory/signals.py
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
from django.db.models.signals import post_save, pre_save
|
||||||
|
from django.dispatch import receiver
|
||||||
|
from inventory.models import Item
|
||||||
|
from transaction.models import Transfer
|
||||||
|
|
||||||
|
|
||||||
|
@receiver(pre_save, sender=Item)
|
||||||
|
def handle_item_move(sender, instance, **kwargs):
|
||||||
|
"""
|
||||||
|
Signal to handle moving an Item to a new Inventory.
|
||||||
|
"""
|
||||||
|
if instance.pk:
|
||||||
|
old_item = Item.objects.get(pk=instance.pk)
|
||||||
|
if old_item.inventory != instance.inventory:
|
||||||
|
old_item.inventory.current_stock -= old_item.quantity
|
||||||
|
old_item.inventory.save()
|
||||||
|
|
||||||
|
@receiver(post_save, sender=Item)
|
||||||
|
def update_inventory_current_stock(sender, instance, **kwargs):
|
||||||
|
"""
|
||||||
|
Signal to update current_stock of related Inventory when an Item is created or assigned to a new Inventory.
|
||||||
|
"""
|
||||||
|
if kwargs.get('created', False) or instance.pk:
|
||||||
|
instance.inventory.current_stock += instance.quantity
|
||||||
|
instance.inventory.save()
|
||||||
|
|
||||||
|
|
||||||
|
@receiver(post_save, sender=Item)
|
||||||
|
def create_transfer_instance(sender, instance, **kwargs):
|
||||||
|
"""
|
||||||
|
Signal to create a Transfer instance when an Item is transferred between warehouses.
|
||||||
|
"""
|
||||||
|
# If the item is being created or updated
|
||||||
|
if kwargs.get('created', False) or instance.pk:
|
||||||
|
if instance.pk and instance.inventory.warehouse != instance.inventory.old_warehouse:
|
||||||
|
Transfer.objects.create(
|
||||||
|
from_warehouse=instance.inventory.old_warehouse,
|
||||||
|
to_warehouse=instance.inventory.warehouse,
|
||||||
|
item=instance,
|
||||||
|
quantity=instance.quantity,
|
||||||
|
)
|
||||||
@ -229,7 +229,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="row no-gutters align-items-center">
|
<div class="row no-gutters align-items-center">
|
||||||
<div class="col-auto">
|
<div class="col-auto">
|
||||||
<div class="h5 mb-0 mr-3 font-weight-bold text-gray-800">{{ stock_percentage }}%</div>
|
<div class="h5 mb-0 mr-3 font-weight-bold text-gray-800">{{ stock_percentage|floatformat:3 }}%</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="col">
|
<div class="col">
|
||||||
<div class="progress progress-sm mr-2">
|
<div class="progress progress-sm mr-2">
|
||||||
@ -280,11 +280,11 @@
|
|||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
{% if warehouse_list|length != 0 %}
|
{% if warehouse_list|length != 0 %}
|
||||||
{% for warehouse in warehouse_list %}
|
{% for warehouse in warehouse_list %}
|
||||||
<h4 class="small font-weight-bold">Account Setup <span class="float-right">
|
<h4 class="small font-weight-bold my-2">{{ warehouse.name }} <span class="float-right">
|
||||||
{% if warehouse.stock_percentage == 100 %}Complete!{% else %}{{ warehouse.stock_percentage }}%{% endif %}
|
{% if warehouse.stock_percentage == 100 %}Complete!{% else %}{{ warehouse.stock_percentage|floatformat:3 }}%{% endif %}
|
||||||
</span></h4>
|
</span></h4>
|
||||||
<div class="progress">
|
<div class="progress">
|
||||||
<div class="progress-bar bg-success" role="progressbar" style="width: 100%" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100"></div>
|
<div class="progress-bar bg-success" role="progressbar" style="width: {{ warehouse.stock_percentage }}%" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100"></div>
|
||||||
</div>
|
</div>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
{% else %}
|
{% else %}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user