some apis of pos
This commit is contained in:
@@ -143,14 +143,9 @@ class RancherViewSet(viewsets.ModelViewSet, DynamicSearchMixin):
|
||||
""" check national code & existence of rancher """
|
||||
|
||||
rancher = self.queryset.filter(national_code=request.data['national_code'])
|
||||
inventory = InventoryEntry.objects.get(id=43)
|
||||
|
||||
# get rancher live stocks information ant total quota for rancher
|
||||
rancher_quota_by_live_stock = rancher_quota_weight(rancher.first(), inventory)
|
||||
|
||||
if rancher.exists():
|
||||
serializer = self.serializer_class(rancher.first())
|
||||
|
||||
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||
else:
|
||||
return Response({
|
||||
|
||||
@@ -64,7 +64,6 @@ def rancher_quota_weight(rancher, inventory_entry: InventoryEntry):
|
||||
count = livestock_counts.get(live_stock_meta.get(animal_type), 0)
|
||||
|
||||
weight = per_head * Decimal(count)
|
||||
print(weight)
|
||||
details[animal_type] = weight
|
||||
total_weight += weight
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from apps.warehouse.pos.api.v1 import serializers as warehouse_serializers
|
||||
from apps.warehouse.services.services import can_buy_from_inventory
|
||||
from apps.pos_device.mixins.pos_device_mixin import POSDeviceMixin
|
||||
from apps.core.mixins.search_mixin import DynamicSearchMixin
|
||||
from apps.warehouse import models as warehouse_models
|
||||
@@ -7,6 +8,7 @@ from rest_framework.permissions import AllowAny
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import viewsets, filters
|
||||
from apps.herd.models import Rancher
|
||||
from django.db import transaction
|
||||
from rest_framework import status
|
||||
import typing
|
||||
@@ -47,6 +49,28 @@ class InventoryEntryViewSet(viewsets.ModelViewSet, DynamicSearchMixin, POSDevice
|
||||
serializer = self.get_serializer(page, many=True)
|
||||
return self.get_paginated_response(serializer.data)
|
||||
|
||||
@action(
|
||||
methods=['post'],
|
||||
detail=False,
|
||||
url_path='rancher_inventory_entries',
|
||||
url_name='rancher_inventory_entries',
|
||||
name='rancher_inventory_entries'
|
||||
)
|
||||
@transaction.atomic
|
||||
def rancher_inventory_entries(self, request):
|
||||
""" """
|
||||
organization = self.get_device_organization()
|
||||
rancher = Rancher.objects.filter(national_code=request.data['national_code']).first()
|
||||
entries = self.queryset.filter(organization=organization)
|
||||
|
||||
available_entries = [entry for entry in entries if can_buy_from_inventory(rancher, entry)]
|
||||
|
||||
# paginate & response
|
||||
page = self.paginate_queryset(available_entries)
|
||||
if page is not None:
|
||||
serializer = self.get_serializer(page, many=True)
|
||||
return self.get_paginated_response(serializer.data)
|
||||
|
||||
|
||||
class InventoryQuotaSaleTransactionViewSet(viewsets.ModelViewSet):
|
||||
queryset = warehouse_models.InventoryQuotaSaleTransaction.objects.all()
|
||||
|
||||
25
apps/warehouse/services/services.py
Normal file
25
apps/warehouse/services/services.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from apps.warehouse.models import InventoryEntry, InventoryQuotaSaleTransaction
|
||||
from apps.herd.services.services import rancher_quota_weight
|
||||
from django.db.models import Sum
|
||||
|
||||
|
||||
def get_total_sold(inventory_entry, rancher):
|
||||
"""
|
||||
"""
|
||||
return (
|
||||
InventoryQuotaSaleTransaction.objects.filter(
|
||||
inventory_entry=inventory_entry,
|
||||
rancher=rancher
|
||||
).aggregate(total=Sum('weight'))['total'] or 0
|
||||
)
|
||||
|
||||
|
||||
def can_buy_from_inventory(rancher, inventory_entry: InventoryEntry):
|
||||
"""
|
||||
"""
|
||||
quota_weight = rancher_quota_weight(rancher, inventory_entry) # {total, by_type}
|
||||
total_allowed = quota_weight['total']
|
||||
|
||||
total_sold = get_total_sold(inventory_entry, rancher)
|
||||
|
||||
return total_sold < total_allowed
|
||||
Reference in New Issue
Block a user