Ejemplo n.º 1
0
    def put(self, request, order_id):
        request_data = request.data
        logger_info.info(request_data)

        result = ResultResponse(code.ARIES_200_SUCCESS, 'success')

        try:
            access_token = str(
                request.META['HTTP_AUTHORIZATION']).split(' ')[1]
            api_request_util.get_admin_token_validate_v2(access_token)
        except Exception as e:
            logger_error.error(str(e))
            result = ResultResponse(code.ARIES_401_UNAUTHORIZED,
                                    'Authentication error')
            return Response(result.get_response(), result.get_code())

        # Change order data
        try:
            order = Order.objects.get(order_id=order_id)
            """
            order.order_status = 11
            order_history = json.loads(order.order_status_history)
            order_history.append(self.get_order_status_history(11))
            order.order_status_history = json.dumps(order_history)

            order.operation_status = 10
            order_operation_history = json.loads(order.operation_status_history)
            order_operation_history.append(self.get_order_status_history(10))
            order.operation_status_history = json.dumps(order_operation_history)
            """

            # Do business logic to each status
            change_status = request_data['operation_status']
            if change_status == 1:
                logger_info.info('start packaging')
                if order.order_status == 0:
                    order_history = json.loads(order.order_status_history)
                    order_history.append(
                        dateformatter.get_order_status_history(1))
                    request_data['order_status'] = 1
                    request_data['order_status_history'] = json.dumps(
                        order_history)

                if order.operation_status == 0:
                    order_operation_history = json.loads(
                        order.operation_status_history)
                    order_operation_history.append(
                        dateformatter.get_order_status_history(1))
                    request_data['operation_status'] = 1
                    request_data['order_operation_history'] = json.dumps(
                        order_operation_history)
            elif change_status == 2:
                logger_info.info('Packaging done')
                if order.operation_status == 1:
                    order_operation_history = json.loads(
                        order.operation_status_history)
                    order_operation_history.append(
                        dateformatter.get_order_status_history(2))
                    request_data['operation_status'] = 2
                    request_data['order_operation_history'] = json.dumps(
                        order_operation_history)
            elif change_status == 5:
                logger_info.info('Delivery started')
                if order.order_status == 1:
                    order_history = json.loads(order.order_status_history)
                    order_history.append(
                        dateformatter.get_order_status_history(2))
                    request_data['order_status'] = 2
                    request_data['order_status_history'] = json.dumps(
                        order_history)

                if order.operation_status == 2:
                    order_operation_history = json.loads(
                        order.operation_status_history)
                    order_operation_history.append(
                        dateformatter.get_order_status_history(3))
                    request_data['operation_status'] = 5
                    request_data['order_operation_history'] = json.dumps(
                        order_operation_history)
            elif change_status == 6:
                logger_info.info('Delivery completed')
                if order.order_status == 2:
                    order_history = json.loads(order.order_status_history)
                    order_history.append(
                        dateformatter.get_order_status_history(10))
                    request_data['order_status'] = 10
                    request_data['order_status_history'] = json.dumps(
                        order_history)

                if order.operation_status == 5:
                    order_operation_history = json.loads(
                        order.operation_status_history)
                    order_operation_history.append(
                        dateformatter.get_order_status_history(6))
                    request_data['operation_status'] = 6
                    request_data['order_operation_history'] = json.dumps(
                        order_operation_history)

            serializer = OrderSerializer(order,
                                         data=request_data,
                                         partial=True)

            if serializer.is_valid():
                serializer.save()
                order_data = serializer.data
                result.set('result', order_data)

                purchase_order = order.purchase_order

                order_data['order_status_history'] = json.loads(
                    order_data['order_status_history'])
                order_data['operation_status_history'] = json.loads(
                    order_data['operation_status_history'])
                order_data['order_details'] = json.loads(
                    purchase_order.order_details)
                order_data[
                    'special_instruction'] = purchase_order.special_instruction
                order_data['extra_telephone'] = purchase_order.extra_telephone

                # price information
                order_data['price_sub_total'] = purchase_order.price_sub_total
                order_data[
                    'price_delivery_fee'] = purchase_order.price_delivery_fee
                order_data['price_discount'] = purchase_order.price_discount
                order_data['price_total'] = purchase_order.price_total

                # Address information (Please check after purchase order model update)
                order_data['address_id'] = purchase_order.delivery_address_id

                del order_data['purchase_order']
                result.set('order', order_data)
            else:
                logger_info.info(serializer.errors)
                result = ResultResponse(code.ARIES_400_BAD_REQUEST,
                                        'Request data invalid')

        except Exception as e:
            logger_info.info(str(e))
            result = ResultResponse(code.ARIES_400_BAD_REQUEST,
                                    'Request_data invalid')

        return Response(result.get_response(), result.get_code())
    def post(self, request):
        result = ResultResponse(code.ARIES_200_SUCCESS, 'success')
        request_data = request.data

        logger_info.info(request_data)

        language_info = header_parser.parse_language(request.META)
        cn_header = language_info[0]

        order_data = dict()
        coupon_instance_list = list()

        # for getting daily order index
        order_manager_v2 = OrderManagerV2(logger_info, logger_error)

        # Step 1. Request data parsing
        try:
            open_id = request.META['HTTP_OPEN_ID']
            access_token = str(request.META['HTTP_AUTHORIZATION']).split(' ')[1]
            order_id = request_data['order_id']
            payment_data = request_data['payment_data']
            notification_order = request_data.get('alipay_notification_order', False)
        except Exception as e:
            logger_info.info(str(e))
            result = ResultResponse(code.ARIES_400_BAD_REQUEST, 'Request data invalid(Token or Open id)')
            return Response(result.get_response(), result.get_code())

        # Step 1.5
        order_count = Order.objects.filter(order_id=order_id, open_id=open_id).count()

        if order_count == 1:
            order = Order.objects.get(order_id=order_id, open_id=open_id)
            purchase_order = PurchaseOrder.objects.get(order_id=order_id, open_id=open_id)
            purchase_order_serializer = UpcomingPurchaseOrderSerializer(purchase_order)
            purchase_order_data = purchase_order_serializer.data
            purchase_order_data['order_details'] = json.loads(purchase_order_data['order_details'])
            purchase_order_data['shipping_detail'] = json.loads(purchase_order_data['shipping_detail'])
            del purchase_order_data['product_list']
            del purchase_order_data['coupon_list']
            del purchase_order_data['order_hash']

            order_data = UpcomingOrderSerializer(order).data
            order_data['order_start_date'] = dateformatter.get_ordering_complete_date_fmt(order.order_start_date)
            order_data['order_status_history'] = json.loads(order_data['order_status_history'])
            order_data['purchase_order'] = purchase_order_data
            order_data['order_cancel_date'] = ''

            # Get Timetable
            order_data['timetable'] = json.loads('[]')

            result.set('upcoming_order', order_data)
            return Response(result.get_response(), result.get_code())

        # Step 2. Check payment data validation and coupon check
        try:
            purchase_order = PurchaseOrder.objects.get(order_id=order_id, open_id=open_id,
                                                       status=self.PURCHASE_ORDER_STATUS_CREATED)

            order_data['purchase_order'] = purchase_order.id

            payment_type = purchase_order.payment_type

            # Check payment data validation
            payload = {'open_id': open_id, 'order_id': order_id, 'payment_type': payment_type,
                       'payment_raw_data': payment_data, 'price_unit': purchase_order.price_unit,
                       'price_total': purchase_order.price_total}

            if notification_order:
                payload['notification_order'] = True
            else:
                payload['notification_order'] = False

            payment_url = payment_util.get_payment_app_url(purchase_order.payment_type)
            response = requests.post(payment_url, json=payload)
            logger_info.info(response.text)
            payment_result = response.json()

            if response.status_code != code.ARIES_200_SUCCESS:
                if response.status_code == code.ARIES_400_BAD_REQUEST:
                    result = ResultResponse(code.ARIES_400_BAD_REQUEST,
                                            get_msg(code.ERROR_9000_INTERNAL_API_CALL_FAILED, cn_header))
                elif payment_result['error_code'] == code.ERROR_4001_PAYMENT_VALIDATION_FAILED:
                    result = ResultResponse(code.ARIES_500_INTERNAL_SERVER_ERROR,
                                            get_msg(code.ERROR_4001_PAYMENT_VALIDATION_FAILED, cn_header))
                    result.set_error(code.ERROR_4001_PAYMENT_VALIDATION_FAILED)
                elif payment_result['error_code'] == code.ERROR_4005_PAYMENT_SYNC_ERROR:
                    result = ResultResponse(code.ARIES_500_INTERNAL_SERVER_ERROR,
                                            get_msg(code.ERROR_4005_PAYMENT_SYNC_ERROR, cn_header))
                purchase_order.status = 3
                purchase_order.save()
                return Response(result.get_response(), result.get_code())

            logger_info.info('Payment finished')

            # Order saved and coupon using section
            coupon_list = json.loads(purchase_order.coupon_list)
            logger_info.info(coupon_list)

            for coupon in coupon_list:
                coupon_id = coupon['coupon_id']
                coupon_count = CustomerCoupon.objects.filter(id=coupon_id).count()

                if coupon_count == 1:
                    coupon_instance = CustomerCoupon.objects.get(id=coupon_id)
                    coupon_instance.status = 1
                    coupon_instance.used_date = datetime.now()
                    coupon_instance_list.append(coupon_instance)
                else:
                    logger_error.error(get_msg(code.ERROR_4001_PAYMENT_VALIDATION_FAILED))
                    result = ResultResponse(code.ARIES_500_INTERNAL_SERVER_ERROR,
                                            get_msg(code.ERROR_4001_PAYMENT_VALIDATION_FAILED))
                    result.set_error(code.ERROR_4001_PAYMENT_VALIDATION_FAILED)
                    purchase_order.status = 3
                    purchase_order.save()
                    return Response(result.get_response(), result.get_code())

        except Exception as e:
            logger_error.error(str(e))
            error_code = code.ERROR_3002_PURCHASE_ORDER_NOT_FOUND
            result = ResultResponse(code.ARIES_500_INTERNAL_SERVER_ERROR, get_msg(error_code), cn_header)
            result.set_error(error_code)
            return Response(result.get_response(), result.get_code())

        # Step 3 Order data section
        logger_info.info('Order data section')

        # Order data
        order_data['open_id'] = open_id
        order_data['hub_id'] = purchase_order.hub_id
        order_data['order_id'] = order_id
        order_data['order_status'] = self.ORDER_STATUS_CREATED
        history_date = dateformatter.get_yyyymmdd_now()
        history_payload = [{'status': 0, 'datetime': history_date}]
        order_data['order_status_history'] = json.dumps(history_payload)
        order_data['order_daily_index'] = order_manager_v2.get_daily_order_index()

        # Delivery data
        is_on_site_pickup = purchase_order.delivery_on_site
        address_id = purchase_order.delivery_address_id

        # Check if it was on-site pickup or not
        if is_on_site_pickup:
            logger_info.info('On site pickup')
            hub_name = hub_data.get_hub_name(cn_header, purchase_order.hub_id)
            complete_address = hub_name + '\n - ' + hub_data.get_hub_address(cn_header, purchase_order.hub_id)
            order_data['delivery_on_site'] = purchase_order.delivery_on_site
            order_data['delivery_address'] = complete_address
        else:
            logger_info.info('NOT on site pickup')
            try:
                headers = {'open-id': str(open_id), 'authorization': 'bearer ' + access_token,
                           'Content-Type': 'application/json'}
                response = requests.get(urlmapper.get_url('PRODUCT_ADDRESS_DETAIL') + '/' + str(address_id),
                                        headers=headers)
            except Exception as e:
                logger_info.info(str(e))

            if response.status_code == code.ARIES_200_SUCCESS:
                response_json = response.json()['user_address']
                logger_info.info(response_json)

                # Set delivery address
                recipient_name = response_json['recipient_name']
                recipient_mdn = response_json['recipient_mdn']

                if len(recipient_mdn) <= 0 and len(recipient_name) <= 0:
                    delivery_address = response_json['name']
                else:
                    delivery_address = response_json['name'] + ' - ' + response_json['detail']

                if len(delivery_address) > 300:
                    delivery_address = delivery_address[:300]
                order_data['delivery_address'] = delivery_address
                order_data['delivery_address_lat'] = response_json['latitude']
                order_data['delivery_address_lng'] = response_json['longitude']
                order_data['delivery_recipient_name'] = recipient_name
                order_data['delivery_recipient_mdn'] = recipient_mdn
            else:
                # New address check
                logger_info.info('Address not found error')
                result = ResultResponse(code.ARIES_500_INTERNAL_SERVER_ERROR,
                                        'Address not found')
                result.set_error(code.ERROR_9000_INTERNAL_API_CALL_FAILED)
                return Response(result.get_response(), result.get_code())

        logger_info.info('After on site pickup')
        order_data['user_name'] = purchase_order.user_name
        order_data['user_telephone'] = purchase_order.user_telephone
        order_data['user_receipt'] = purchase_order.user_receipt
        order_data['delivery_date'] = purchase_order.delivery_date
        order_data['delivery_schedule'] = purchase_order.delivery_schedule
        order_data['delivery_as_fast'] = purchase_order.delivery_as_fast
        order_data['delivery_customer_name'] = purchase_order.user_name
        order_data['delivery_telephone_state'] = 0
        order_data['delivery_telephone'] = 'Delivery telephone'
        pre_time = purchase_order.delivery_start_time
        post_time = purchase_order.delivery_end_time
        delivery_date = str(purchase_order.delivery_date).replace('-', '.')
        order_data['delivery_time'] = delivery_date + ', ' + pre_time + '~' + post_time

        # Shipping data
        order_data['shipping_method'] = purchase_order.shipping_method
        order_data['shipping_status'] = 0

        # Step 4
        logger_info.info('STEP 4')
        order_count = Order.objects.filter(order_id=order_id, open_id=open_id).count()

        if order_count == 1:
            logger_info.info('Order exists')

            order = Order.objects.get(order_id=order_id, open_id=open_id)
            purchase_order = PurchaseOrder.objects.get(order_id=order_id, open_id=open_id)
            purchase_order_serializer = UpcomingPurchaseOrderSerializer(purchase_order)
            purchase_order_data = purchase_order_serializer.data
            purchase_order_data['order_details'] = json.loads(purchase_order_data['order_details'])
            purchase_order_data['shipping_detail'] = json.loads(purchase_order_data['shipping_detail'])
            del purchase_order_data['product_list']
            del purchase_order_data['coupon_list']
            del purchase_order_data['order_hash']

            order_data = UpcomingOrderSerializer(order).data
            order_data['order_start_date'] = dateformatter.get_ordering_complete_date_fmt(order.order_start_date)
            order_data['order_status_history'] = json.loads(order_data['order_status_history'])
            order_data['purchase_order'] = purchase_order_data
            order_data['order_cancel_date'] = ''

            # Get Timetable
            order_data['timetable'] = json.loads('[]')

            result.set('upcoming_order', order_data)
            return Response(result.get_response(), result.get_code())

        # Step 5
        # Create order object and send order data to client
        logger_info.info('Step 5')

        try:
            order_serializer = OrderSerializer(data=order_data)

            if order_serializer.is_valid():
                order_serializer.save()
                logger_info.info('Order valid and save success')

                # Order save success and coupon save
                for coupon_ins in coupon_instance_list:
                    coupon_ins.save()

                # User notification url call
                url = urlmapper.get_url('USER_NOTIFICATION') + '/coupon/1/' + str(len(coupon_instance_list))
                headers = {'open-id': open_id, 'authorization': 'bearer ' + access_token}
                requests.get(url, headers=headers)

                # Get special instruction and include cutlery information
                include_cutlery = purchase_order.include_cutlery
                special_inst = purchase_order.special_instruction

                # Special instruction and include cutlery save call
                url = urlmapper.get_url('USER_CART_INFO_SAVE')
                headers = {'open-id': open_id, 'authorization': 'bearer ' + access_token}
                json_data = {'include_cutlery': include_cutlery, 'special_instruction': special_inst}
                requests.put(url, headers=headers, json=json_data)

                # After user notification
                logger_info.info('After user notification request')
                purchase_order_serializer = UpcomingPurchaseOrderSerializer(purchase_order)
                purchase_order_data = purchase_order_serializer.data
                purchase_order_data['order_details'] = json.loads(purchase_order_data['order_details'])
                purchase_order_data['shipping_detail'] = json.loads(purchase_order_data['shipping_detail'])
                del purchase_order_data['product_list']
                del purchase_order_data['coupon_list']
                del purchase_order_data['order_hash']

                order_data = order_serializer.data
                order = Order.objects.get(id=order_data['id'])
                order_data = UpcomingOrderSerializer(order).data
                order_data['order_start_date'] = dateformatter.get_ordering_complete_date_fmt(order.order_start_date)
                order_data['order_status_history'] = json.loads(order_data['order_status_history'])
                order_data['purchase_order'] = purchase_order_data
                order_data['order_cancel_date'] = ''

                # Get Timetable
                logger_info.info('Get time table')
                order_data['timetable'] = json.loads('[]')

                result.set('upcoming_order', order_data)

                # Check on first purchase
                logger_info.info('Check first purchase')
                first_order_count = Order.objects.filter(open_id=open_id, order_status__lte=10).count()
                if first_order_count == 1:
                    hub_id = purchase_order.hub_id
                    event_order_service = EventOrderService(logger_info, logger_error)
                    event_order_service.create_event_order(0, hub_id, open_id, order_id)
            else:
                logger_error.error(order_serializer.errors)
                result = ResultResponse(code.ARIES_500_INTERNAL_SERVER_ERROR, 'Internal server error')
                return Response(result.get_response(), result.get_code())

            purchase_order.status = 1
            purchase_order.save()
            logger_info.info('Purchase order save')

            # Product stock update
            product_list = json.loads(purchase_order.product_list)
            payload = {'trade_type': 0, 'product_list': product_list, 'validate_str': 'GoDbAcKeNdS'}
            url = urlmapper.get_url('PRODUCT_STOCK')
            response = requests.post(url, json=payload)
            logger_info.info(response.text)

            # Order complete
            headers = {'open-id': open_id, 'authorization': 'bearer ' + access_token}
            payload = {'order_id': order_id}
            url = urlmapper.get_url('OPERATION_ORDER_COMPLETE')
            response = requests.post(url, headers=headers, json=payload)
            logger_info.info(response.text)

        except Exception as e:
            logger_error.error(str(e))
            result = ResultResponse(code.ARIES_500_INTERNAL_SERVER_ERROR, 'Internal server error')
            return Response(result.get_response(), result.get_code())

        # Step 6
        # Save payment method and notify to admin server
        # Upcoming order notification added
        logger_info.info('Step 6')

        headers = {'AUTHORIZATION': request.META['HTTP_AUTHORIZATION']}
        payload = {'latest_payment_method': purchase_order.payment_type}
        url = urlmapper.get_url('USER_INFO') + '/' + open_id + '/info'
        response = requests.put(url, headers=headers, json=payload)
        logger_info.info(response.json())

        try:
            # Hub message
            logger_info.info('Hub message')

            order_manager = OrderManager(logger_info, logger_error)
            order_list_detail = order_manager.get_order_list(order_id)
            payload = {
                'code': 200, 'message': 'success', 'order': order_list_detail, 'type': 1,
                'order_id': order_id, 'title': 'Here comes a new order'
            }
            url = urlmapper.get_url('HUB_MESSAGE_ANDROID')
            response = requests.post(url, json=payload)
            logger_info.info(response.text)
        except Exception as e:
            logger_info.info(str(e))

        return Response(result.get_response(), result.get_code())