def create(self, request): try: store = request.data['store_name'] phone_number = request.data['phone_number'] email = request.data['email'] flat_number = request.data['flat_number'] building = request.data['building'] street = request.data['street'] area_code = request.data['area_code'] except: content = { 'error': 'Incomplete params', 'description': 'phone_number, store_name, email, flat_number, building, street, area_code' } return Response(content, status=status.HTTP_400_BAD_REQUEST) # CHECK IF THE VENDOR HAS ALREADY REQUESTED FOR AN ACCOUNT existing_vendors = Vendor.objects.filter(phone_number=phone_number) if len(existing_vendors) > 0: content = { 'error': 'Already exists', 'description': 'Vendor with similar details already exists' } return Response(content, status=status.HTTP_400_BAD_REQUEST) area = get_object_or_404(Area, area_code=area_code) new_address = Address.objects.create(flat_number=flat_number, building=building, street=street, area=area) vendor = Vendor.objects.create(store_name=store, email=email, phone_number=phone_number) vendor.addresses.add(new_address) vendor.save() # SEND EMAIL TO SALES approval_link = 'http://vendor-yourguy.herokuapp.com/#/home/vendor/{}'.format( vendor.id) subject = 'YourGuy: New Vendor Account Request' body = constants.VENDOR_ACCOUNT_REQUESTED_MESSAGE_SALES.format( store, phone_number, email, flat_number, building, street, area_code, approval_link) send_email(constants.SALES_EMAIL, subject, body) content = { 'status': 'Thank you! We have received your request. Our sales team will contact you soon.' } return Response(content, status=status.HTTP_201_CREATED)
def request_vendor_account(self, request, pk=None): # INPUT PARAMETER CHECK ---------------------------------------------------------- try: store_name = request.data['store_name'] phone_number = request.data['phone_number'] email = request.data['email'] full_address = request.data['full_address'] pin_code = request.data['pin_code'] landmark = request.data.get('landmark') except: content = { 'error': 'Incomplete parameters', 'description': 'store_name, phone_number, email, full_address, pin_code, landmark[optional]' } return Response(content, status=status.HTTP_400_BAD_REQUEST) # ---------------------------------------------------------------------------------- # CHECK IF THE VENDOR HAS ALREADY REQUESTED FOR AN ACCOUNT ------------------------ existing_vendors = Vendor.objects.filter(phone_number=phone_number) existing_users = User.objects.filter(username=phone_number) if len(existing_vendors) > 0 or len(existing_users) > 0: content = { 'error': 'Already exists', 'description': 'Vendor with similar details already exists' } return Response(content, status=status.HTTP_400_BAD_REQUEST) # ---------------------------------------------------------------------------------- # CREATING NEW VENDOR -------------------------------------------------------------- new_vendor = Vendor.objects.create(store_name=store_name, email=email, phone_number=phone_number) new_address = create_address(full_address, pin_code, landmark) new_vendor.addresses.add(new_address) new_vendor.save() # ---------------------------------------------------------------------------------- # SEND EMAIL TO SALES --------------------------------------------------------------- try: approval_link = 'http://app.yourguy.in/#/home/vendor/{}'.format( new_vendor.id) subject = 'YourGuy: New Vendor Account Request' body = constants.VENDOR_ACCOUNT_REQUESTED_MESSAGE_SALES.format( store_name, phone_number, email, full_address, pin_code, approval_link) send_email(constants.SALES_EMAIL, subject, body) except Exception, e: pass
def website_email(request): try: name = request.data['name'] phone_number = request.data['phone_number'] message = request.data['message'] except Exception as e: content = { 'error': 'Imcomplete parameters', 'description': 'name, phone_number, message are mandatory parameters' } return Response(content, status=status.HTTP_400_BAD_REQUEST) body = 'Hi,' body = body + '\n\nMr.%s has sent a message from website.' % (name) body = body + '\nPhone number: %s' % (phone_number) body = body + '\nMessage: %s' % (message) body = body + '\n\n-YourGuy Bot' send_email(constants.EMAIL_WEBSITE, 'Message from website', body) return Response(status=status.HTTP_200_OK)
def daily_report(request): date = datetime.today() day_start = ist_day_start(date) day_end = ist_day_end(date) # TOTAL ORDERS ---------------------------------------------------------------------- delivery_statuses_today = OrderDeliveryStatus.objects.filter(date__gte = day_start, date__lte = day_end) orders_total_count = len(delivery_statuses_today) # ----------------------------------------------------------------------------------- if orders_total_count == 0: today_string = datetime.now().strftime("%Y %b %d") email_subject = 'Daily Report : %s' % (today_string) email_body = "Good Evening Guys," email_body = email_body + "\n\n No orders on the app." email_body = email_body + "\n\n Chill out!" email_body = email_body + "\n\n- YourGuy BOT" send_email(constants.EMAIL_IDS_EVERYBODY, email_subject, email_body) return Response(status = status.HTTP_200_OK) else: # TOTAL ORDERS ASSIGNED vs UNASSIGNED ORDERS ---------------------------------------- orders_unassigned_count = delivery_statuses_today.filter(delivery_guy = None).count() orders_assigned_count = orders_total_count - orders_unassigned_count orders_unassigned_percentage = "{0:.0f}%".format(float(orders_unassigned_count)/float(orders_total_count) * 100) orders_assigned_percentage = "{0:.0f}%".format(float(orders_assigned_count)/float(orders_total_count) * 100) # ----------------------------------------------------------------------------------- # ORDERS ACC TO ORDER_STATUS -------------------------------------------------------- orders_placed_count = delivery_statuses_today.filter(order_status = constants.ORDER_STATUS_PLACED).count() orders_queued_count = delivery_statuses_today.filter(order_status = constants.ORDER_STATUS_QUEUED).count() orders_queued_percentage = "{0:.0f}%".format(float(orders_queued_count)/float(orders_total_count) * 100) orders_intransit_count = delivery_statuses_today.filter(order_status = constants.ORDER_STATUS_INTRANSIT).count() orders_intransit_percentage = "{0:.0f}%".format(float(orders_intransit_count)/float(orders_total_count) * 100) orders_delivered_count = delivery_statuses_today.filter(order_status = constants.ORDER_STATUS_DELIVERED).count() orders_delivered_percentage = "{0:.0f}%".format(float(orders_delivered_count)/float(orders_total_count) * 100) orders_pickup_attempted_count = delivery_statuses_today.filter(order_status = constants.ORDER_STATUS_PICKUP_ATTEMPTED).count() orders_pickup_attempted_percentage = "{0:.0f}%".format(float(orders_pickup_attempted_count)/float(orders_total_count) * 100) orders_delivery_attempted_count = delivery_statuses_today.filter(order_status = constants.ORDER_STATUS_DELIVERY_ATTEMPTED).count() orders_delivert_attempted_percentage= "{0:.0f}%".format(float(orders_delivery_attempted_count)/float(orders_total_count) * 100) orders_rejected_count = delivery_statuses_today.filter(order_status = constants.ORDER_STATUS_REJECTED).count() orders_rejected_percentage = "{0:.0f}%".format(float(orders_rejected_count)/float(orders_total_count) * 100) orders_canceled_count = delivery_statuses_today.filter(order_status = constants.ORDER_STATUS_CANCELLED).count() orders_canceled_percentage = "{0:.0f}%".format(float(orders_canceled_count)/float(orders_total_count) * 100) pending_orders_count = orders_queued_count + orders_placed_count + orders_intransit_count pending_orders_percentage = "{0:.0f}%".format(float(pending_orders_count)/float(orders_total_count) * 100) completed_orders_count = orders_delivered_count + orders_pickup_attempted_count + orders_delivery_attempted_count + orders_rejected_count + orders_canceled_count completed_orders_percentage = "{0:.0f}%".format(float(completed_orders_count)/float(orders_total_count) * 100) # ----------------------------------------------------------------------------------- # DG ATTENDANCE DETAILS ------------------------------------------------------------- total_dg_count = DeliveryGuy.objects.all().count() total_dg_checked_in_count = DGAttendance.objects.filter(date__year = date.year, date__month = date.month, date__day = date.day).count() dg_checkin_percentage = "{0:.0f}%".format(float(total_dg_checked_in_count)/float(total_dg_count) * 100) # ----------------------------------------------------------------------------------- # TOTAL COD COLLECTED Vs SUPPOSSED TO BE COLLECTED ---------------------------------- total_cod_collected = delivery_statuses_today.aggregate(Sum('cod_collected_amount')) total_cod_collected = total_cod_collected['cod_collected_amount__sum'] executable_deliveries = delivery_statuses_today.filter(Q(order_status = 'QUEUED') | Q(order_status = 'INTRANSIT') | Q(order_status = 'DELIVERED') | Q(order_status = 'DELIVERYATTEMPTED') | Q(order_status = 'PICKUPATTEMPTED')) total_cod_dict = executable_deliveries.aggregate(total_cod = Sum('order__cod_amount')) total_cod_to_be_collected = total_cod_dict['total_cod'] if total_cod_to_be_collected > 0: cod_collected_percentage = "{0:.0f}%".format(float(total_cod_collected)/float(total_cod_to_be_collected) * 100) else: cod_collected_percentage = "100%" # ----------------------------------------------------------------------------------- # DELIVERY BOY WHO HAVE COD -------------------------------------------------------- cod_deliveries = delivery_statuses_today.filter(cod_collected_amount__gt = 0) cod_with_delivery_boys = cod_deliveries.values('delivery_guy__user__first_name').annotate(total=Sum('cod_collected_amount')) cod_with_dg_string = '' for item in cod_with_delivery_boys: delivery_guy = item['delivery_guy__user__first_name'] total = item['total'] cod_with_dg_string = cod_with_dg_string + "\n%s = %s" % (delivery_guy, total) # ----------------------------------------------------------------------------------- # SEND AN EMAIL SAYING CANT FIND APPROPRAITE DELIVERY GUY FOR THIS ORDER. PLEASE ASSIGN MANUALLY today_string = datetime.now().strftime("%Y %b %d") email_subject = 'Daily Report : %s' % (today_string) email_body = "Good Evening Guys, \n\nPlease find the report of the day." email_body = email_body + "\n\nTotal orders = %s" % (orders_total_count) email_body = email_body + "\nPending orders = %s [%s percent]" % (pending_orders_count, pending_orders_percentage) email_body = email_body + "\nExecuted orders = %s [%s percent]" % (completed_orders_count, completed_orders_percentage) email_body = email_body + "\n\nSTATUS WISE BIFURGATION ------------" email_body = email_body + "\nOrders assigned = %s [%s percent]" % (orders_assigned_count, orders_assigned_percentage) email_body = email_body + "\nOrders unassigned = %s [%s percent]" % (orders_unassigned_count, orders_unassigned_percentage) email_body = email_body + "\nQueued = %s [%s percent]" % (orders_queued_count, orders_queued_percentage) email_body = email_body + "\nInTransit = %s [%s percent]" % (orders_intransit_count, orders_intransit_percentage) email_body = email_body + "\ndelivered = %s [%s percent]" % (orders_delivered_count, orders_delivered_percentage) email_body = email_body + "\nPickup Attempted = %s [%s percent]" % (orders_pickup_attempted_count, orders_pickup_attempted_percentage) email_body = email_body + "\nDelivery Attempted = %s [%s percent]" % (orders_delivery_attempted_count, orders_delivered_percentage) email_body = email_body + "\nRejected = %s [%s percent]" % (orders_rejected_count, orders_rejected_percentage) email_body = email_body + "\nCanceled = %s [%s percent]" % (orders_canceled_count, orders_canceled_percentage) email_body = email_body + "\n------------------------------------" email_body = email_body + "\n\nDELIVERY BOY ATTENDANCE -------" email_body = email_body + "\nTotal DGs on app = %s" % total_dg_count email_body = email_body + "\nTotal DGs CheckIn = %s [%s percent]" % (total_dg_checked_in_count, dg_checkin_percentage) email_body = email_body + "\n-----------------------------------" email_body = email_body + "\n\nCOD DETAILS ------------------" email_body = email_body + "\nTotal COD to be collected = %s" % total_cod_to_be_collected email_body = email_body + "\nTotal COD collected = %s [%s percent]" % (total_cod_collected, cod_collected_percentage) email_body = email_body + "\n-----------------------------------" email_body = email_body + "\n\nCOD WITH EACH DG ------------------" email_body = email_body + cod_with_dg_string email_body = email_body + "\n-----------------------------------" email_body = email_body + "\n\n- YourGuy BOT" send_email(constants.EMAIL_DAILY_REPORT, email_subject, email_body) # ------------------------------------------------------------------------------------------------ return Response(status = status.HTTP_200_OK)
if latest_assigned_delivery is not None: delivery_status.delivery_guy = latest_assigned_delivery.delivery_guy delivery_status.save() except Exception, e: unassigned_deliveries = unassigned_deliveries + "\n %s - %s - %s" % (vendor.store_name, delivery_status.id, consumer.full_name) pass # ------------------------------------------------------------------------------------------------ except Exception, e: pass # SEND AN EMAIL SAYING CANT FIND APPROPRAITE DELIVERY GUY FOR THIS ORDER. PLEASE ASSIGN MANUALLY today_string = datetime.now().strftime("%Y %b %d") email_subject = 'Unassigned orders for %s' % (today_string) email_body = "Hello, \nPlease find the unassigned orders for the day. \nUnassigned pickups: %s \n\nUnassigned deliveries: %s \nPlease assign manually. \n\n- Team YourGuy" % (unassigned_pickups, unassigned_deliveries) send_email(constants.EMAIL_UNASSIGNED_ORDERS, email_subject, email_body) # ------------------------------------------------------------------------------------------------ # TODO #inform_dgs_about_orders_assigned() return @api_view(['GET']) def daily_report(request): date = datetime.today() day_start = ist_day_start(date) day_end = ist_day_end(date) # TOTAL ORDERS ---------------------------------------------------------------------- delivery_statuses_today = OrderDeliveryStatus.objects.filter(date__gte = day_start, date__lte = day_end)
# if pricing is None: # pricing = 0.0 # else: # pricing = float(pricing) # new_account = VendorAccount.objects.create(vendor = vendor, pricing = pricing) # if pan is not None: # new_account.pan = pan # new_account.save() # SEND AN EMAIL/SMS TO CUSTOMER AND SALES WITH ACCOUNT CREDENTIALS subject = 'YourGuy: Account created for {}'.format(vendor.store_name) customer_message = constants.VENDOR_ACCOUNT_APPROVED_MESSAGE.format( vendor.phone_number, password) customer_emails = [vendor.email] send_email(customer_emails, subject, customer_message) send_sms(vendor.phone_number, customer_message) sales_message = constants.VENDOR_ACCOUNT_APPROVED_MESSAGE_SALES.format( vendor.store_name) send_email(constants.SALES_EMAIL, subject, sales_message) content = { 'description': 'Your account credentials has been sent via email and SMS.' } return Response(content, status=status.HTTP_200_OK) @detail_route(methods=['post']) def add_address(self, request, pk): try:
from api.views import send_email import constants from rest_framework.decorators import api_view from rest_framework.response import Response from rest_framework import status @api_view(['POST']) def website_email(request): try: name = request.data['name'] phone_number = request.data['phone_number'] message = request.data['message'] except Exception, e: content = { 'error': 'Imcomplete parameters', 'description': 'name, phone_number, message are mandatory parameters' } return Response(content, status=status.HTTP_400_BAD_REQUEST) body = 'Hi,' body = body + '\n\nMr.%s has sent a message from website.' % (name) body = body + '\nPhone number: %s' % (phone_number) body = body + '\nMessage: %s' % (message) body = body + '\n\n-YourGuy Bot' send_email(constants.EMAIL_WEBSITE, 'Message from website', body) return Response(status=status.HTTP_200_OK)