add - IR to all sheba in devices
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
from django.db import transaction
|
||||
from django.db.models import Sum
|
||||
from rest_framework.exceptions import APIException
|
||||
|
||||
from apps.product import models as product_models
|
||||
|
||||
@@ -15,14 +17,57 @@ class QuotaDistributionService:
|
||||
self.distribution = product_models.QuotaDistribution.objects.filter(
|
||||
quota=quota,
|
||||
assigned_organization=organization,
|
||||
quota__is_closed=False,
|
||||
warehouse_entry__gt=0
|
||||
).first()
|
||||
|
||||
@property
|
||||
def total_weight(self):
|
||||
def total_weight():
|
||||
return self.distribution.aggregate(
|
||||
total=Sum('weight')
|
||||
)['total'] or 0
|
||||
|
||||
pass
|
||||
@property
|
||||
def remaining_weight():
|
||||
return self.distribution.aggregate(
|
||||
total=Sum('remaining_weight')
|
||||
)['total'] or 0
|
||||
|
||||
def to_representation(self): # noqa
|
||||
return {
|
||||
'quota_id': self.quota.id,
|
||||
'quota_identity': self.quota.quota_id,
|
||||
'product': self.quota.product.name,
|
||||
'total_weight': self.total_weight,
|
||||
'remaining_weight': self.remaining_weight,
|
||||
'distribution_ids': self.distribution_ids,
|
||||
}
|
||||
|
||||
@transaction.atomic
|
||||
def consume(self, weight): # noqa
|
||||
"""
|
||||
Consume 'amount' of weight across all distributions in order.
|
||||
Automatically splits usage if needed.
|
||||
"""
|
||||
|
||||
if weight > self.remaining_weight:
|
||||
raise APIException('Not enough weight to consume')
|
||||
|
||||
remaining_to_consume = weight
|
||||
|
||||
for dist in self.distribution.select_for_update():
|
||||
if remaining_to_consume <= 0:
|
||||
break
|
||||
|
||||
available = dist.remaining_weight
|
||||
if available <= 0:
|
||||
continue
|
||||
|
||||
consume_amount = min(available, remaining_to_consume)
|
||||
dist.remaining_weight -= consume_amount
|
||||
dist.save(update_fields=["remaining_weight"])
|
||||
remaining_to_consume -= consume_amount
|
||||
|
||||
if remaining_to_consume > 0:
|
||||
raise APIException('Not enough weight to consume')
|
||||
|
||||
return True
|
||||
|
||||
Reference in New Issue
Block a user