Skip to content

Commit

Permalink
visited in crm
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaniog committed Jun 16, 2024
1 parent 0c5cc0b commit 6203596
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 5 deletions.
28 changes: 28 additions & 0 deletions backend/apps/amo/dto.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class DealDTO:
name: str
price: int
bought_tickets: str
visited_tickets: str
created_at: datetime
contact: ContactDTO
status_id: int
Expand Down Expand Up @@ -60,6 +61,7 @@ def deal_to_json(deal: DealDTO) -> {}:
"created_at": int(deal.created_at.astimezone().timestamp()),
"custom_fields_values": [
{"field_id": d_f_id[DEAL_BOUGHT_TICKETS], "values": [{"value": deal.bought_tickets}]},
{"field_id": d_f_id["Посещения"], "values": [{"value": deal.visited_tickets}]},
{"field_id": d_f_id["ID заказа"], "values": [{"value": deal.payment_id}]},
],
"_embedded": {
Expand All @@ -80,14 +82,19 @@ def deal_to_json(deal: DealDTO) -> {}:
def order_to_deal(order) -> DealDTO:
print(order)
bookings = group_bookings_by_place_event_time(order)
visited_bookings = group_visited_by_place_event_time(order)
p_f_id = PIPELINE_FIELD_TO_ID
status = p_f_id["оплачен"] if (order.payment_status == "succeeded") else p_f_id["не оплачен"]

if len(visited_bookings) != 0:
status = p_f_id["посетил"]

return DealDTO(
order.payment_id,
order.cart.buyer.first_name + " " + order.cart.buyer.last_name + " " + datetime.now().strftime("%d.%m"),
int(order.total),
"\n\n".join(list(map(booking_to_string, bookings))),
"\n\n".join(list(map(booking_to_string, visited_bookings))),
datetime.now(),
ContactDTO(
first_name=order.cart.buyer.first_name,
Expand Down Expand Up @@ -118,3 +125,24 @@ def group_bookings_by_place_event_time(order):
)
for key, value in grouped_data.items()
]


def group_visited_by_place_event_time(order):
bookings = Booking.objects.filter(cart=order.cart, visited=True).select_related('event', 'ticket', 'event__place')
grouped_data = defaultdict(lambda: {'types': defaultdict(int), 'amount': 0, 'price': 0.0})
print(bookings)
for booking in bookings:
key = (booking.event.place.name, booking.event.name, booking.time)
grouped_data[key]['types'][booking.ticket.get_type_display()] += booking.quantity
grouped_data[key]['price'] += int(booking.ticket.price)

return [
BookingDTO(
place_name=key[0],
event_name=key[1],
types=dict(value['types']),
price=value['price'],
time=key[2]
)
for key, value in grouped_data.items()
]
21 changes: 21 additions & 0 deletions backend/apps/booking/migrations/0012_booking_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 5.0.6 on 2024-06-16 09:14

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


class Migration(migrations.Migration):

dependencies = [
('booking', '0011_booking_quantity'),
('payments', '0005_order_qr_code'),
]

operations = [
migrations.AddField(
model_name='booking',
name='order',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, related_name='bookings', to='payments.order'),
preserve_default=False,
),
]
9 changes: 5 additions & 4 deletions backend/apps/booking/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ def total_places(self):
@property
def total(self):
return (
self.tickets.aggregate(total=Sum(F("ticket__price") * F("quantity")))[
"total"
]
or 0
self.tickets.aggregate(total=Sum(F("ticket__price") * F("quantity")))[
"total"
]
or 0
)


Expand All @@ -44,3 +44,4 @@ class Booking(models.Model):
cart = models.ForeignKey(Cart, on_delete=models.CASCADE)
visited = models.BooleanField(default=False)
quantity = models.IntegerField(validators=[MinValueValidator(0)])
order = models.ForeignKey("payments.Order", on_delete=models.CASCADE, related_name="bookings")
4 changes: 3 additions & 1 deletion backend/apps/booking/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from .dto import EventWithDate, EventWithDateSerializer, TicketWithDate
from .models import Buyer, Cart
from .serializers import BookingSerializer, BuyerSerializer, CartSerializer
from apps.amo.views import post_orders


class CartListCreateAPIView(generics.ListCreateAPIView):
Expand Down Expand Up @@ -172,7 +173,8 @@ def get(self, request, pk):

class BookingVisitAPIView(APIView):
def get(self, request, pk):
b = Booking.objects.filter(pk=pk).first()
b: Booking = Booking.objects.filter(pk=pk).first()
b.visited = not b.visited
b.save()
post_orders([b.order])
return Response(BookingSerializer(b).data, status.HTTP_200_OK)
1 change: 1 addition & 0 deletions backend/apps/payments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ def create_bookings(sender, instance, **kwargs):
time=t.time,
cart=t.cart,
quantity=t.ticket.personas,
order=instance
)
post_orders([instance])

0 comments on commit 6203596

Please sign in to comment.