Add records to orders

This commit is contained in:
fnux 2020-03-02 09:25:03 +01:00
parent 8e41b894c0
commit 81bd54116a
4 changed files with 34 additions and 16 deletions

View file

@ -65,16 +65,6 @@ class BillEntry():
# confusing: the order is a 'contract' with the customer, were both parts # confusing: the order is a 'contract' with the customer, were both parts
# agree on deal => That's what we want to keep archived. # agree on deal => That's what we want to keep archived.
# #
# SOON:
#
# We'll need to add some kind of OrderEntry table (each order might have
# multiple entries) storing: recurring_price, recurring_period, setup_fee, description
#
# FOR NOW:
#
# We dynamically get pricing from linked product, as they are not updated in
# this stage of development.
#
# /!\ BIG FAT WARNING /!\ # # /!\ BIG FAT WARNING /!\ #
class Order(models.Model): class Order(models.Model):
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
@ -95,11 +85,23 @@ class Order(models.Model):
choices = RecurringPeriod.choices, choices = RecurringPeriod.choices,
default = RecurringPeriod.PER_MONTH) default = RecurringPeriod.PER_MONTH)
@property
def records(self):
return OrderRecord.objects.filter(order=self)
@property @property
def amount(self): def setup_fee(self):
records = OrderRecord.objects.filter(order=self) return reduce(lambda acc, record: acc + record.setup_fee, self.records, 0)
return 1
@property
def recurring_price(self):
return reduce(lambda acc, record: acc + record.recurring_price, self.records, 0)
def add_record(self, setup_fee, recurring_price, description):
OrderRecord.objects.create(order=self,
setup_fee=setup_fee,
recurring_price=recurring_price,
description=description)
class OrderRecord(models.Model): class OrderRecord(models.Model):
order = models.ForeignKey(Order, on_delete=models.CASCADE) order = models.ForeignKey(Order, on_delete=models.CASCADE)

View file

@ -26,10 +26,17 @@ class PaymentMethodSerializer(serializers.ModelSerializer):
class ProductSerializer(serializers.Serializer): class ProductSerializer(serializers.Serializer):
vms = VMProductSerializer(many=True, read_only=True) vms = VMProductSerializer(many=True, read_only=True)
class OrderRecordSerializer(serializers.ModelSerializer):
class Meta:
model = OrderRecord
fields = ['setup_fee', 'recurring_price', 'description']
class OrderSerializer(serializers.ModelSerializer): class OrderSerializer(serializers.ModelSerializer):
records = OrderRecordSerializer(many=True, read_only=True)
class Meta: class Meta:
model = Order model = Order
fields = '__all__' fields = ['uuid', 'creation_date', 'starting_date', 'ending_date',
'bill', 'recurring_period', 'records', 'recurring_price', 'setup_fee']
class UserSerializer(serializers.ModelSerializer): class UserSerializer(serializers.ModelSerializer):
class Meta: class Meta:

View file

@ -40,8 +40,6 @@ class VMProduct(Product):
blank=True, blank=True,
null=True) null=True)
description = "Virtual Machine"
# VM-specific. The name is only intended for customers: it's a pain te # VM-specific. The name is only intended for customers: it's a pain te
# remember IDs (speaking from experience as ungleich customer)! # remember IDs (speaking from experience as ungleich customer)!
name = models.CharField(max_length=32) name = models.CharField(max_length=32)
@ -55,6 +53,10 @@ class VMProduct(Product):
else: else:
raise Exception('Invalid recurring period for VM Product pricing.') raise Exception('Invalid recurring period for VM Product pricing.')
@property
def description(self):
return "Virtual machine '{}': {} core(s), {}GB memory".format(
self.name, self.cores, self.ram_in_gb)
class VMWithOSProduct(VMProduct): class VMWithOSProduct(VMProduct):
pass pass

View file

@ -28,6 +28,9 @@ class VMProductViewSet(ProductViewSet):
return VMProduct.objects.filter(owner=self.request.user) return VMProduct.objects.filter(owner=self.request.user)
def create(self, request): def create(self, request):
# TODO: what if something blows-up midway?
# => need a transaction
# Create base order. # Create base order.
order = Order.objects.create( order = Order.objects.create(
recurring_period=RecurringPeriod.PER_MONTH, recurring_period=RecurringPeriod.PER_MONTH,
@ -40,6 +43,10 @@ class VMProductViewSet(ProductViewSet):
serializer.is_valid(raise_exception=True) serializer.is_valid(raise_exception=True)
vm = serializer.save(owner=request.user, order=order) vm = serializer.save(owner=request.user, order=order)
# Add Product record to order (VM is mutable, allows to keep history in order).
order.add_record(vm.setup_fee,
vm.recurring_price(order.recurring_period), vm.description)
return Response(serializer.data) return Response(serializer.data)