def get_event_ordered_items_and_cart_item_combined_category_wise(main_attendee, event, order_queryset, all_attendees): from carts.utils import get_cart_items_queryset_for_each_category order_data = OrderOptimizedSerializer(order_queryset.latest()).data # here ordered_item_queryset is all items that belong to these orders queryset, this includes # order_items of all attendees and with not null value in `canceled_ordered_item` ordered_items_queryset = get_all_active_ordered_items_of_main_user(order_queryset) registration_ordered_items = OrderedItemSerializer(ordered_items_queryset.filter( transaction_type=SALE, event_item__group__name=dict(DEFAULT_EVENT_ITEM_GROUPS).get(REGISTRATION)).order_by( 'created_at' ), many=True).data accommodation_cart_items = get_cart_items_queryset_for_each_category(main_attendee.user, Q(event_item__group__name=dict( DEFAULT_EVENT_ITEM_GROUPS).get( ACCOMMODATION)), event) accommodation_ordered_items = ordered_items_queryset.filter( transaction_type=SALE, event_item__group__name=dict(DEFAULT_EVENT_ITEM_GROUPS).get(ACCOMMODATION)) accommodation_items = get_combined_final_data_from_ordered_items_and_cart_items(accommodation_ordered_items, accommodation_cart_items, all_attendees, only_sale_carts=True) transportation_cart_items = get_cart_items_queryset_for_each_category(main_attendee.user, Q(event_item__group__name=dict( DEFAULT_EVENT_ITEM_GROUPS).get( TRANSPORTATION)), event) transportation_ordered_items = ordered_items_queryset.filter( transaction_type=SALE, event_item__group__name=dict(DEFAULT_EVENT_ITEM_GROUPS).get(TRANSPORTATION)) transportation_items = get_combined_final_data_from_ordered_items_and_cart_items(transportation_ordered_items, transportation_cart_items, all_attendees, only_sale_carts=True) # coupon_items = OrderedItemSerializer( # ordered_items_queryset.filter(is_coupon_item=True), many=True).data # payment_items = OrderedItemSerializer( # ordered_items_queryset.filter( # transaction_type=RECEIPT), many=True).data # refund_items = OrderedItemSerializer( # ordered_items_queryset.filter(transaction_type=REFUND), many=True).data response_data = { 'order': order_data, 'registration_items': registration_ordered_items, 'accommodation_items': accommodation_items, 'transportation_items': transportation_items, 'group_type': main_attendee.group_type, 'net_balance': get_refundable_balance_from_latest_order(order_queryset.latest()) # 'coupon_items': coupon_items, # 'payment_items': payment_items, # 'refund_items': refund_items, } return response_data
def get_guest_attendee_order_items(ordered_items_queryset, order_queryset, all_guest_attendees, transaction_type): guest_attendee_order_items = [] order_item_queryset = ordered_items_queryset.filter( order__in=order_queryset, transaction_type=transaction_type) for guest in all_guest_attendees: guest_order_items = order_item_queryset.filter( user=guest.user).order_by('id') guest_attendee_order_items.append( OrderedItemSerializer(guest_order_items, many=True).data) return guest_attendee_order_items
def ordered_item_detail(request, order_id, product_id, format=None): """ Retrieve, update or delete a order instance. """ try: order = Order.objects.get(id=order_id) except Order.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) try: product = Product.objects.get(id=product_id) except Product.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) try: ordered_item = OrderedItem.objects.get(order_id=order, product_id=product) except OrderedItem.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = OrderedItemSerializer(ordered_item) return Response(serializer.data) elif request.method == 'PUT': if order.status in (config.ORDER_CANCELED, config.ORDER_PAID, config.ORDER_PLACED): return Response('No modification to ordered items allowed', status=status.HTTP_400_BAD_REQUEST) if 'quantity' in request.DATA: ordered_item.quantity = request.DATA['quantity'] ordered_item.save() serializer = OrderedItemSerializer(ordered_item) return Response(serializer.data) elif request.method == 'DELETE': if order.status in (config.ORDER_CANCELED, config.ORDER_PAID, config.ORDER_PLACED): return Response('No modification to ordered items allowed', status=status.HTTP_400_BAD_REQUEST) ordered_item.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def get_active_items_of_order(order_queryset, main_user, main_attendee=None, event=None): # import is done here to avoid circular dependency error from events.utils import ( get_guest_attendee_order_items, get_all_attendees) ordered_items_queryset = get_all_active_ordered_items_of_main_user(order_queryset) # now we update confirmation codes for each attendees all_attendees = get_all_attendees(main_attendee, event) latest_order = get_latest_order_of_main_user(main_user, event) # refundable_balance value obtained above may be positive or negative , if it is positive , it means # balance is greater than balance_credit so we need to refund him/her back # but if the value is negative , more amount has to be paid since credit coupon amount is larger than balance return { 'order': OrderOptimizedSerializer(order_queryset.latest()).data, 'guest_attendees_order_items': get_guest_attendee_order_items(ordered_items_queryset, order_queryset, # the following excludes main_attendee from all # attendees to get guest attendees all_attendees.exclude(uuid=main_attendee.uuid), dict(ORDER_ITEM_TRANSACTION_TYPE_CHOICES).get( SALE)), 'main_attendee_order_items': OrderedItemSerializer( ordered_items_queryset.filter(user=main_user).filter(transaction_type=SALE).order_by('id'), many=True).data, 'coupon_items': OrderedItemCouponSerializer( get_all_coupon_items_from_ordered_items(ordered_items_queryset), many=True).data, 'payment_items': OrderedItemSerializer( ordered_items_queryset.filter( transaction_type=RECEIPT).order_by('id'), many=True).data, 'service_charge_and_tax_items': OrderedItemSerializer( ordered_items_queryset.filter( transaction_type=dict(ORDER_ITEM_TRANSACTION_TYPE_CHOICES).get(TAX_OR_SERVICE_CHARGE)).order_by('id'), many=True).data, 'refund_items': OrderedItemSerializer( ordered_items_queryset.filter(transaction_type=REFUND).order_by('id'), many=True).data, 'net_balance': get_refundable_balance_from_latest_order(latest_order), }
class OrderSerializer(ModelSerializer): from orders.serializers import OrderedItemSerializer user = UserSerializer() event_attendee = EventAttendeeSerializer() order_items = OrderedItemSerializer(many=True) class Meta: model = Order exclude = ( 'deleted_at', 'uuid', 'id', 'order_number', 'order_cfy', 'balance', 'balance_credit', 'operator') def to_representation(self, instance): response = super().to_representation(instance) data = { 'uuid': instance.uuid, 'order_number': instance.order_number, 'order_cfy': instance.order_cfy, 'balance': instance.balance, 'balance_credit': instance.balance_credit, 'operator': instance.operator, } response.update(data) return response
def get_active_items_of_order_and_changed_items_of_cart( event, main_attendee, order_queryset): # import done here to avoid circular dependency import error from events.utils import get_guest_attendees from carts.utils import ( perform_operations_on_cart_items_for_cart_summary, get_combined_final_data_from_ordered_items_and_cart_items) latest_order = order_queryset.latest() ordered_items_base_queryset = get_all_active_ordered_items_of_main_user( order_queryset) result = perform_operations_on_cart_items_for_cart_summary( main_attendee.user, latest_order, main_attendee, event) all_cart_items = result['all_cart_items'] main_attendee_ordered_items_queryset = ordered_items_base_queryset.filter( user=main_attendee.user) main_attendee_order_items = main_attendee_ordered_items_queryset.filter( transaction_type=dict(ORDER_ITEM_TRANSACTION_TYPE_CHOICES).get(SALE)) # now we update confirmation codes for each attendees all_guest_attendees = get_guest_attendees(main_attendee, event) guest_attendees_order_items = ordered_items_base_queryset.filter( event_attendee__in=all_guest_attendees) guest_attendee_items = [] for guest_attendee in all_guest_attendees: guest_attendee_items.append( get_combined_final_data_from_ordered_items_and_cart_items( guest_attendees_order_items, all_cart_items, # we pass it as list to make it # iterable [guest_attendee], only_sale_carts=False)) coupon_ordered_items = get_all_coupon_items_from_ordered_items( ordered_items_base_queryset) coupon_cart_items = get_all_coupon_items_from_cart_items(all_cart_items) return { 'order': OrderOptimizedSerializer(latest_order).data, 'guest_attendees_order_items': guest_attendee_items, 'main_attendee_order_items': get_combined_final_data_from_ordered_items_and_cart_items( main_attendee_order_items, all_cart_items, # we pass it as list to make it # iterable [main_attendee], only_sale_carts=False), 'coupon_items': get_combined_coupon_items_serialized_data(coupon_ordered_items, coupon_cart_items), 'payment_items': OrderedItemSerializer(main_attendee_ordered_items_queryset.filter( transaction_type=RECEIPT), many=True).data, 'refund_items': OrderedItemSerializer(main_attendee_ordered_items_queryset.filter( transaction_type=REFUND), many=True).data, 'service_charge_and_tax_items': ItemCartSerializer( (data for data in result['service_charge_and_tax_items']), many=True).data, 'cancellation_charge_cart_items': ItemCartSerializer( (data for data in result['cancellation_charge_cart_items']), many=True).data, 'amount_net_for_changed_cart_items': result['amount_net_for_changed_cart_items'], 'net_service_charge_and_tax_amount': result['net_service_charge_and_tax_amount'], 'total_cancellation_charge_amount': result['total_cancellation_charge_amount'], 'net_balance_from_latest_order': result['net_balance_from_latest_order'], 'current_total_credit_amount_used': result['current_total_credit_amount_used'], 'current_total_discount_coupon_used': result['current_total_discount_coupon_used'], 'final_amount_to_pay': result['final_amount_to_pay'], 'balance_credit': result['balance_credit'], 'refundable_balance': result['refundable_balance'], }
def order_detail(request, id, format=None): """ Retrieve, update or delete a order instance. """ try: order = Order.objects.get(id=id) except Order.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': order_serializer = OrderSerializer(order) try: ordered_item = OrderedItem.objects.filter(order_id=order) except OrderedItem.DoesNotExist: return Response(order_serializer.data) ordered_item_serializer = OrderedItemSerializer(ordered_item, many=True) return Response([order_serializer.data] + ordered_item_serializer.data) elif request.method == 'POST': if order.status in (config.ORDER_CANCELED, config.ORDER_PAID, config.ORDER_PLACED): return Response('No modification to ordered items allowed', status=status.HTTP_400_BAD_REQUEST) try: if 'product_id' in request.DATA: product = Product.objects.get(id=request.DATA['product_id']) elif 'name' in request.DATA: product = Product.objects.get(name=request.DATA['name']) except Product.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if 'quantity' in request.DATA: ordered_item = OrderedItem(order_id=order, product_id=product, quantity=request.DATA['quantity']) else: ordered_item = OrderedItem(order_id=order, product_id=product) ordered_item.save() product.ordered = True product.save() serializer = OrderedItemSerializer(ordered_item) return Response(serializer.data) elif request.method == 'PUT': if 'date' in request.DATA: order.date = request.DATA['date'] if 'status' in request.DATA: current_status = ordered_item.status new_status = request.DATA['status'] if current_status != new_status: status = (current_status, new_status) if status == (config.ORDER_DRAFT, config.ORDER_PLACED): if not order_item: return Response(status=status.HTTP_400_BAD_REQUEST) elif status == (config.ORDER_DRAFT, config.ORDER_CANCELED): if 'reason' not in request.DATA or not request.DATA[ 'reason']: return Response(status=status.HTTP_400_BAD_REQUEST) elif status == (config.ORDER_PLACED, config.ORDER_PAID): pass elif status == (config.ORDER_PLACED, config.ORDER_CANCELED): if 'reason' not in request.DATA or not request.DATA[ 'reason']: return Response(status=status.HTTP_400_BAD_REQUEST) else: return Response(status=status.HTTP_400_BAD_REQUEST) order.status = new_status order.save() order_serializer = OrderSerializer(order) try: ordered_item = OrderedItem.objects.filter(order_id=order) except OrderedItem.DoesNotExist: return Response(status=status.HTTP_204_NO_CONTENT) ordered_item_serializer = OrderedItemSerializer(ordered_item, many=True) return Response([order_serializer.data] + ordered_item_serializer.data)
def viewOrderedItem(request): print(request.GET['id']) items = OrderedItem.objects.filter(ordId = request.GET['id']) serializer = OrderedItemSerializer(items, many = True) return JsonResponse(serializer.data, safe = False)