Skip to content

Commit

Permalink
Enhance restaurant and order management features in admin interface
Browse files Browse the repository at this point in the history
- Added opening and closing time fields for each day of the week in the Restaurant model and admin.
- Updated OrderDetail to include a restaurant foreign key and adjusted related methods for price calculations.
- Modified Order model to include a note field and changed the date field to DateTimeField for better tracking.
- Enhanced ProductSerializer to calculate final prices including thermopack prices and delivery times.
- Improved admin dashboard template by cleaning up unnecessary custom CSS and JS references.

These changes improve the management of restaurant operations and enhance the user experience in the admin interface.
  • Loading branch information
UnCubanoDev committed Dec 11, 2024
1 parent 4f328f3 commit d75849e
Show file tree
Hide file tree
Showing 10 changed files with 286 additions and 52 deletions.
50 changes: 34 additions & 16 deletions api/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,21 @@ class RestaurantAdmin(admin.ModelAdmin):
'phone',
'is_active',
'funds',
'tax'
'tax',
'monday_opening_time',
'monday_closing_time',
'tuesday_opening_time',
'tuesday_closing_time',
'wednesday_opening_time',
'wednesday_closing_time',
'thursday_opening_time',
'thursday_closing_time',
'friday_opening_time',
'friday_closing_time',
'saturday_opening_time',
'saturday_closing_time',
'sunday_opening_time',
'sunday_closing_time',
]
list_filter = ['is_active']
search_fields = ['name', 'user__username', 'phone']
Expand Down Expand Up @@ -65,30 +79,26 @@ class DistributorRatingAdmin(admin.ModelAdmin):
class OrderDetailInline(admin.TabularInline):
model = OrderDetail
extra = 0
readonly_fields = ['get_final_price']
fields = ['product', 'quantity', 'get_final_price']
readonly_fields = ['product', 'quantity', 'get_final_price', 'restaurant']
fields = ['product', 'quantity', 'get_final_price', 'restaurant']

def get_final_price(self, obj):
if obj.id:
return obj.get_final_price()
return 0
get_final_price.short_description = _("Precio Final")
return obj.get_final_price() # Asegúrate de que este método exista en OrderDetail

def save_related(self, request, form, change):
super().save_related(request, form, change)
for order_detail in form.instance.products.all():
if not order_detail.unit_price: # Solo si el precio no está establecido
order_detail.unit_price = order_detail.product.price
order_detail.save()


@admin.register(Order)
class OrderAdmin(admin.ModelAdmin):
list_display = [
'user',
'date',
'time',
'distributor',
'delivery_address',
'status',
'pay_type',
'get_sub_total',
'delivery_price',
'total_price',
'was_paid_by_distributor',
'note',
]
inlines = [OrderDetailInline]

Expand Down Expand Up @@ -155,6 +165,14 @@ class ProductAdmin(admin.ModelAdmin):
]
list_filter = ['is_active']

def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "category":
if request.GET.get('restaurant'):
kwargs["queryset"] = ProductCategory.objects.filter(business_id=request.GET.get('restaurant'))
else:
kwargs["queryset"] = ProductCategory.objects.none()
return super().formfield_for_foreignkey(db_field, request, **kwargs)


@admin.register(RestaurantRating)
class RestaurantRatingAdmin(admin.ModelAdmin):
Expand Down
92 changes: 92 additions & 0 deletions api/migrations/0004_remove_restaurant_bussiness_type_and_more.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Generated by Django 4.1.1 on 2024-12-11 03:52

import datetime
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('api', '0003_configuration_business_closing_time_and_more'),
]

operations = [
migrations.RemoveField(
model_name='restaurant',
name='bussiness_type',
),
migrations.RemoveField(
model_name='restaurant',
name='time',
),
migrations.AddField(
model_name='restaurant',
name='friday_closing_time',
field=models.TimeField(default=datetime.time(23, 59), verbose_name='Friday Closing Time'),
),
migrations.AddField(
model_name='restaurant',
name='friday_opening_time',
field=models.TimeField(default=datetime.time(8, 0), verbose_name='Friday Opening Time'),
),
migrations.AddField(
model_name='restaurant',
name='monday_closing_time',
field=models.TimeField(default=datetime.time(23, 59), verbose_name='Monday Closing Time'),
),
migrations.AddField(
model_name='restaurant',
name='monday_opening_time',
field=models.TimeField(default=datetime.time(8, 0), verbose_name='Monday Opening Time'),
),
migrations.AddField(
model_name='restaurant',
name='saturday_closing_time',
field=models.TimeField(default=datetime.time(23, 59), verbose_name='Saturday Closing Time'),
),
migrations.AddField(
model_name='restaurant',
name='saturday_opening_time',
field=models.TimeField(default=datetime.time(8, 0), verbose_name='Saturday Opening Time'),
),
migrations.AddField(
model_name='restaurant',
name='sunday_closing_time',
field=models.TimeField(default=datetime.time(23, 59), verbose_name='Sunday Closing Time'),
),
migrations.AddField(
model_name='restaurant',
name='sunday_opening_time',
field=models.TimeField(default=datetime.time(8, 0), verbose_name='Sunday Opening Time'),
),
migrations.AddField(
model_name='restaurant',
name='thursday_closing_time',
field=models.TimeField(default=datetime.time(23, 59), verbose_name='Thursday Closing Time'),
),
migrations.AddField(
model_name='restaurant',
name='thursday_opening_time',
field=models.TimeField(default=datetime.time(8, 0), verbose_name='Thursday Opening Time'),
),
migrations.AddField(
model_name='restaurant',
name='tuesday_closing_time',
field=models.TimeField(default=datetime.time(23, 59), verbose_name='Tuesday Closing Time'),
),
migrations.AddField(
model_name='restaurant',
name='tuesday_opening_time',
field=models.TimeField(default=datetime.time(8, 0), verbose_name='Tuesday Opening Time'),
),
migrations.AddField(
model_name='restaurant',
name='wednesday_closing_time',
field=models.TimeField(default=datetime.time(23, 59), verbose_name='Wednesday Closing Time'),
),
migrations.AddField(
model_name='restaurant',
name='wednesday_opening_time',
field=models.TimeField(default=datetime.time(8, 0), verbose_name='Wednesday Opening Time'),
),
]
18 changes: 18 additions & 0 deletions api/migrations/0005_order_note.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.1.1 on 2024-12-11 03:59

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('api', '0004_remove_restaurant_bussiness_type_and_more'),
]

operations = [
migrations.AddField(
model_name='order',
name='note',
field=models.TextField(blank=True, null=True, verbose_name='note'),
),
]
23 changes: 23 additions & 0 deletions api/migrations/0006_configuration_delivery_time_restaurant_time.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.1.1 on 2024-12-11 04:19

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('api', '0005_order_note'),
]

operations = [
migrations.AddField(
model_name='configuration',
name='delivery_time',
field=models.IntegerField(default=30, verbose_name='delivery time (minutes)'),
),
migrations.AddField(
model_name='restaurant',
name='time',
field=models.IntegerField(default=0, verbose_name='time'),
),
]
18 changes: 18 additions & 0 deletions api/migrations/0007_product_thermopack_price.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.1.1 on 2024-12-11 04:25

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('api', '0006_configuration_delivery_time_restaurant_time'),
]

operations = [
migrations.AddField(
model_name='product',
name='thermopack_price',
field=models.DecimalField(decimal_places=2, default=0, max_digits=10, verbose_name='thermopack price'),
),
]
24 changes: 24 additions & 0 deletions api/migrations/0008_orderdetail_restaurant_alter_order_date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 4.1.1 on 2024-12-11 04:53

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('api', '0007_product_thermopack_price'),
]

operations = [
migrations.AddField(
model_name='orderdetail',
name='restaurant',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.PROTECT, related_name='order_details', to='api.restaurant'),
),
migrations.AlterField(
model_name='order',
name='date',
field=models.DateTimeField(auto_now_add=True),
),
]
51 changes: 34 additions & 17 deletions api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Configuration(SingletonModel):
_("business opening time"),
default=time(8, 0)
)
delivery_time = models.IntegerField(_("delivery time (minutes)"), default=30)

class Meta:
verbose_name = _("configuration")
Expand Down Expand Up @@ -113,15 +114,27 @@ class Restaurant(models.Model):
_("image"), upload_to='restaurants', null=True, blank=True)
user = models.ForeignKey(
settings.AUTH_USER_MODEL, verbose_name=_("user"), on_delete=models.CASCADE)
time = models.CharField(_("time"), max_length=10)
bussiness_type = models.ManyToManyField(
"api.Category", verbose_name=_("Business type"))
is_active = models.BooleanField(_("is active"), default=True)
tax = models.FloatField(_("tax"), default=10)
latitude = models.CharField(_("latitude"), max_length=25, default='')
longitude = models.CharField(_("longitude"), max_length=25, default='')
recommended = models.BooleanField(_("recommended"), default=False)
funds = models.DecimalField(_("funds"), max_digits=10, decimal_places=2, default=0)
monday_opening_time = models.TimeField(_("Monday Opening Time"), default=time(8, 0))
monday_closing_time = models.TimeField(_("Monday Closing Time"), default=time(23, 59))
tuesday_opening_time = models.TimeField(_("Tuesday Opening Time"), default=time(8, 0))
tuesday_closing_time = models.TimeField(_("Tuesday Closing Time"), default=time(23, 59))
wednesday_opening_time = models.TimeField(_("Wednesday Opening Time"), default=time(8, 0))
wednesday_closing_time = models.TimeField(_("Wednesday Closing Time"), default=time(23, 59))
thursday_opening_time = models.TimeField(_("Thursday Opening Time"), default=time(8, 0))
thursday_closing_time = models.TimeField(_("Thursday Closing Time"), default=time(23, 59))
friday_opening_time = models.TimeField(_("Friday Opening Time"), default=time(8, 0))
friday_closing_time = models.TimeField(_("Friday Closing Time"), default=time(23, 59))
saturday_opening_time = models.TimeField(_("Saturday Opening Time"), default=time(8, 0))
saturday_closing_time = models.TimeField(_("Saturday Closing Time"), default=time(23, 59))
sunday_opening_time = models.TimeField(_("Sunday Opening Time"), default=time(8, 0))
sunday_closing_time = models.TimeField(_("Sunday Closing Time"), default=time(23, 59))
time = models.IntegerField(_("time"), default=0)

@property
def categories_product(self):
Expand Down Expand Up @@ -284,6 +297,7 @@ class Product(models.Model):
um = models.CharField(_("unit of measurement"), max_length=10)
amount = models.IntegerField(_("amount"))
is_active = models.BooleanField(_("is active"), default=True)
thermopack_price = models.DecimalField(_("thermopack price"), max_digits=10, decimal_places=2, default=0)

class Meta:
verbose_name = _("product")
Expand Down Expand Up @@ -333,7 +347,7 @@ class Order(models.Model):
)
distributor = models.ForeignKey("api.Distributor", verbose_name=_(
"distributor"), on_delete=models.DO_NOTHING, null=True, blank=True)
date = models.DateField(_("date"), auto_now=True)
date = models.DateTimeField(auto_now_add=True)
time = models.TimeField(_("time"), auto_now=True)
delivery_address = models.ForeignKey(
"directorio.Address",
Expand All @@ -347,6 +361,7 @@ class Order(models.Model):
was_paid_by_distributor = models.BooleanField(
_("was paid by distributor"), default=False)
delivery_total_distance = models.IntegerField(_("delivery total distance"))
note = models.TextField(_("note"), blank=True, null=True)

@property
def total_price(self):
Expand Down Expand Up @@ -445,47 +460,48 @@ class OrderDetail(models.Model):
)
quantity = models.PositiveIntegerField(
_("quantity"),
default=1 # Valor por defecto para registros existentes
default=1
)
# Campos para mantener precios históricos
unit_price = models.DecimalField(
_("unit price"),
max_digits=10,
decimal_places=2,
null=True,
default=0 # Valor por defecto
default=0
)
tax_rate = models.DecimalField(
_("tax rate"),
max_digits=5,
decimal_places=2,
null=True,
default=0 # Valor por defecto
default=0
)
exchange_rate = models.DecimalField(
_("exchange rate"),
max_digits=10,
decimal_places=2,
null=True,
default=1 # Valor por defecto
default=1
)
restaurant = models.ForeignKey(
'Restaurant',
on_delete=models.PROTECT,
related_name='order_details'
)

def save(self, *args, **kwargs):
if not self.pk: # Solo si es nuevo
config = Configuration.objects.first()
self.unit_price = self.product.price
self.unit_price = self.product.price # Establecer el unit_price al precio del producto
self.tax_rate = self.product.restaurant.tax
config = Configuration.objects.first()
self.exchange_rate = config.exchange_rate if config else 1
super().save(*args, **kwargs)

def get_final_price(self):
if self.unit_price is None:
return 0

base_price = float(self.unit_price) * (1 + (float(self.tax_rate or 0) / 100))
base_price = float(self.product.price) * (1 + (float(self.tax_rate or 0) / 100))
if self.order.user.phone.startswith('+53'):
return round(base_price * self.quantity, 2)
return round((base_price / float(self.exchange_rate or 1)) * self.quantity, 2)
return math.ceil(base_price * self.quantity)
return math.ceil((base_price / float(self.exchange_rate or 1)) * self.quantity)

class Meta:
verbose_name = _("order detail")
Expand All @@ -496,3 +512,4 @@ def __str__(self):

def get_absolute_url(self):
return reverse("orderdetail_detail", kwargs={"pk": self.pk})

Loading

0 comments on commit d75849e

Please sign in to comment.