Commit 566836b0 authored by Bryan Carlo Guanlao's avatar Bryan Carlo Guanlao

Orders App edit

edited order_item model fields and fixed discount bugs
parent d2fb0a10
...@@ -2,14 +2,18 @@ from django.contrib import admin ...@@ -2,14 +2,18 @@ from django.contrib import admin
from .models import Order, OrderItem from .models import Order, OrderItem
# Register your models here. # Register your models here.
class OrderAdmin(admin.ModelAdmin): class OrderAdmin(admin.ModelAdmin):
model = Order model = Order
list_display = ('order_no', 'order_date', 'amount_due', ) list_display = ('order_no', 'order_date', 'amount_due', )
class OrderItemAdmin(admin.ModelAdmin): class OrderItemAdmin(admin.ModelAdmin):
model = OrderItem model = OrderItem
list_display = ('order_no', 'item_id', 'color', 'quantity', 'sub_total', 'is_successful') list_display = ('order_no', 'item_id', 'item_color',
'quantity', 'sub_total', 'is_successful')
admin.site.register(Order, OrderAdmin) admin.site.register(Order, OrderAdmin)
admin.site.register(OrderItem, OrderItemAdmin) admin.site.register(OrderItem, OrderItemAdmin)
\ No newline at end of file
# Generated by Django 3.2.12 on 2022-12-06 05:37
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('orders', '0002_alter_orderitem_discount'),
]
operations = [
migrations.RenameField(
model_name='orderitem',
old_name='color',
new_name='item_color',
),
]
# Generated by Django 3.2.12 on 2022-12-06 06:21
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('orders', '0003_rename_color_orderitem_item_color'),
]
operations = [
migrations.AlterField(
model_name='orderitem',
name='discount',
field=models.IntegerField(default=0, help_text='in percentage', validators=[django.core.validators.MaxValueValidator(100), django.core.validators.MinValueValidator(0)]),
),
]
...@@ -6,48 +6,70 @@ from datetime import date ...@@ -6,48 +6,70 @@ from datetime import date
from decimal import * from decimal import *
# Create your models here. # Create your models here.
class Order(models.Model): class Order(models.Model):
order_no = models.AutoField(primary_key=True) order_no = models.AutoField(primary_key=True)
order_date = models.DateField(auto_now_add=True) order_date = models.DateField(auto_now_add=True)
customer_id = models.ForeignKey(Customer, on_delete=models.CASCADE, db_column='customer_id') customer_id = models.ForeignKey(
items = models.ManyToManyField(Product, through='OrderItem', through_fields=('order_no', 'item_id')) Customer, on_delete=models.CASCADE, db_column='customer_id')
items = models.ManyToManyField(
Product, through='OrderItem', through_fields=('order_no', 'item_id'))
delivery_schedule = models.DateTimeField() delivery_schedule = models.DateTimeField()
delivery_address = models.CharField(max_length=254) delivery_address = models.CharField(max_length=254)
as_gift = models.BooleanField(help_text='Is this order intended as a Gift?') as_gift = models.BooleanField(
recipient = models.CharField(max_length=50, help_text='Type your name if order is not intended as a gift.') help_text='Is this order intended as a Gift?')
amount_due = models.DecimalField(default=0, editable=False, decimal_places=2, max_digits=12) recipient = models.CharField(
max_length=50, help_text='Type your name if order is not intended as a gift.')
amount_due = models.DecimalField(
default=0, editable=False, decimal_places=2, max_digits=12)
def __str__(self): def __str__(self):
return str(self.order_no) return str(self.order_no)
class Meta: class Meta:
db_table = "order" db_table = "order"
class OrderItem(models.Model): class OrderItem(models.Model):
order_no = models.ForeignKey(Order, on_delete=models.CASCADE, db_column='order_no') order_no = models.ForeignKey(
item_id = models.ForeignKey(Product, on_delete=models.CASCADE, db_column='item_id') Order, on_delete=models.CASCADE, db_column='order_no')
color = models.CharField(max_length=20, choices=(("RED","Red"), ("ORANGE","Orange"), ("YELLOW", "Yellow"), item_id = models.ForeignKey(
("GREEN", "Green"), ("BLUE", "Blue"), ("PURPLE", "Purple"), ("PINK", "Pink"), ("BLACK", "Black"))) Product, on_delete=models.CASCADE, db_column='item_id')
quantity = models.IntegerField(default=1, validators=[MaxValueValidator(99), MinValueValidator(0)]) item_color = models.CharField(max_length=20, choices=(("RED", "Red"), ("ORANGE", "Orange"), ("YELLOW", "Yellow"),
("GREEN", "Green"), ("BLUE", "Blue"), ("PURPLE", "Purple"), ("PINK", "Pink"), ("BLACK", "Black")))
quantity = models.IntegerField(
default=1, validators=[MaxValueValidator(99), MinValueValidator(0)])
personalization = models.CharField(max_length=254) personalization = models.CharField(max_length=254)
discount = models.IntegerField(default=0, validators=[MaxValueValidator(100), MinValueValidator(0)]) discount = models.IntegerField(
discount_price = models.DecimalField(default=0, decimal_places=2, max_digits=12) default=0, validators=[MaxValueValidator(100), MinValueValidator(0)], help_text='in percentage')
sub_total = models.DecimalField(default=0, editable=False, decimal_places=2, max_digits=12) discount_price = models.DecimalField(
default=0, decimal_places=2, max_digits=12, editable=False)
sub_total = models.DecimalField(
default=0, editable=False, decimal_places=2, max_digits=12)
is_successful = models.BooleanField(default=True, editable=False) is_successful = models.BooleanField(default=True, editable=False)
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
import products.models as product_model import products.models as product_model
inventory = product_model.Inventory.objects.all() inventory = product_model.Inventory.objects.all()
product = product_model.Product.objects.all()
for prod in inventory: for prod in inventory:
if(prod.item_id == self.item_id and prod.color == self.color): if(prod.item_id == self.item_id and prod.color == self.item_color):
if(prod.quantity < self.quantity): if(prod.stock < self.quantity):
self.is_successful = False self.is_successful = False
else: else:
prod.quantity -= self.quantity prod.stock -= self.quantity
prod.save() prod.save()
if(self.is_successful): if(self.is_successful):
self.sub_total= self.discount_price * self.quantity self.discount_price = self.item_id.price * \
(1 - (self.discount/100))
self.sub_total = self.discount_price * self.quantity
self.order_no.amount_due += Decimal(self.sub_total) self.order_no.amount_due += Decimal(self.sub_total)
self.order_no.save() self.order_no.save()
return super().save(*args, **kwargs) return super().save(*args, **kwargs)
def __str__(self): def __str__(self):
return str(self.quantity) + " pcs. of " + str(self.item_id) + " of variant " + self.color.lower() + " added to order #" + str(self.order_no) return str(self.quantity) + " pcs. of " + str(self.item_id) + " of variant " + self.item_color.lower() + " added to order #" + str(self.order_no)
class Meta: class Meta:
db_table = "order_item" db_table = "order_item"
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment