def save_sa_registration(request, groups): data = request.POST existing_sa = False data_source = [] phone_number = utils.mobile_format(str(data['phone-number'])) if data['sa-id']: service_advisor_id = data['sa-id'] existing_sa = True else: service_advisor_id = TEMP_SA_ID_PREFIX + str(random.randint(10**5, 10**6)) data_source.append(utils.create_sa_feed_data(data, request.user.username, service_advisor_id)) logger.info('[Temporary_sa_registration]:: Initiating dealer-sa feed for ID' + service_advisor_id) if Roles.ASCS in groups: feed_type='asc_sa' else: feed_type='dealer' feed_remark = FeedLogWithRemark(len(data_source), feed_type='Dealer Feed', action='Received', status=True) sap_obj = SAPFeed() feed_response = sap_obj.import_to_db(feed_type=feed_type, data_source=data_source, feed_remark=feed_remark) if feed_response.failed_feeds > 0: failure_msg = list(feed_response.remarks.elements())[0] logger.info('[Temporary_sa_registration]:: dealer-sa feed fialed ' + failure_msg) return json.dumps({"message": failure_msg}) logger.info('[Temporary_sa_registration]:: dealer-sa feed completed') if existing_sa: return json.dumps({'message': SA_UPDATE_SUCCESS}) return json.dumps({'message': SA_REGISTER_SUCCESS})
def register_customer(request, group=None): post_data = request.POST data_source = [] existing_customer = False product_obj = models.ProductData.objects.filter(product_id=post_data['customer-vin']) if not post_data['customer-id']: temp_customer_id = utils.generate_temp_id(TEMP_ID_PREFIX) else: temp_customer_id = post_data['customer-id'] existing_customer = True data_source.append(utils.create_purchase_feed_data(post_data, product_obj[0], temp_customer_id)) check_with_invoice_date = utils.subtract_dates(data_source[0]['product_purchase_date'], product_obj[0].invoice_date) check_with_today_date = utils.subtract_dates(data_source[0]['product_purchase_date'], datetime.datetime.now()) if not existing_customer and check_with_invoice_date.days < 0 or check_with_today_date.days > 0: message = "Product purchase date should be between {0} and {1}".\ format((product_obj[0].invoice_date).strftime("%d-%m-%Y"),(datetime.datetime.now()).strftime("%d-%m-%Y")) logger.info('[Temporary_cust_registration]:: {0} Entered date is: {1}'.format(message, str(data_source[0]['product_purchase_date']))) return json.dumps({"message": message}) try: with transaction.atomic(): customer_obj = models.CustomerTempRegistration.objects.filter(temp_customer_id = temp_customer_id) if customer_obj: customer_obj = customer_obj[0] if customer_obj.new_number != data_source[0]['customer_phone_number']: update_count = models.Constant.objects.get(constant_name='mobile_number_update_count').constant_value if customer_obj.mobile_number_update_count >= int(update_count) and group[0] != Roles.SDMANAGERS: customer_update = models.CustomerUpdateFailure(product_id = product_obj[0], customer_name = data_source[0]['customer_name'], customer_id = customer_obj.temp_customer_id, updated_by = "dealer-"+ str(request.user), old_number = customer_obj.new_number, new_number = data_source[0]['customer_phone_number']) customer_update.save() message = get_template('PHONE_NUMBER_UPDATE_COUNT_EXCEEDED') return json.dumps({'message' : message}) if models.UserProfile.objects.filter(phone_number=data_source[0]['customer_phone_number']): message = get_template('FAILED_UPDATE_PHONE_NUMBER').format(phone_number=data_source[0]['customer_phone_number']) return json.dumps({'message': message}) old_number = customer_obj.new_number customer_obj.new_number = data_source[0]['customer_phone_number'] customer_obj.product_data = product_obj[0] customer_obj.sent_to_sap = False customer_obj.dealer_asc_id = str(request.user) customer_obj.mobile_number_update_count+=1 update_history = models.CustomerUpdateHistory(temp_customer=customer_obj, updated_field='Phone Number', old_value=old_number, new_value=customer_obj.new_number, email_flag=False) update_history.save() message = get_template('CUSTOMER_MOBILE_NUMBER_UPDATE').format(customer_name=customer_obj.new_customer_name, new_number=customer_obj.new_number) for phone_number in [customer_obj.new_number, old_number]: phone_number = utils.get_phone_number_format(phone_number) sms_log(settings.BRAND, receiver=phone_number, action=AUDIT_ACTION, message=message) send_job_to_queue(send_customer_phone_number_update_message, {"phone_number":phone_number, "message":message, "sms_client":settings.SMS_CLIENT}) if models.UserProfile.objects.filter(user__groups__name=Roles.BRANDMANAGERS).exists(): groups = utils.stringify_groups(request.user) if Roles.ASCS in groups: dealer_asc_id = "asc : " + customer_obj.dealer_asc_id elif Roles.DEALERS in groups: dealer_asc_id = "dealer : " + customer_obj.dealer_asc_id else : dealer_asc_id = "manager : " + customer_obj.dealer_asc_id message = get_template('CUSTOMER_PHONE_NUMBER_UPDATE').format(customer_id=customer_obj.temp_customer_id, old_number=old_number, new_number=customer_obj.new_number, dealer_asc_id=dealer_asc_id) managers = models.UserProfile.objects.filter(user__groups__name=Roles.BRANDMANAGERS) for manager in managers: phone_number = utils.get_phone_number_format(manager.phone_number) sms_log(settings.BRAND, receiver=phone_number, action=AUDIT_ACTION, message=message) send_job_to_queue(send_customer_phone_number_update_message, {"phone_number":phone_number, "message":message, "sms_client":settings.SMS_CLIENT}) else: if models.UserProfile.objects.filter(phone_number=data_source[0]['customer_phone_number']): message = get_template('FAILED_UPDATE_PHONE_NUMBER').format(phone_number=data_source[0]['customer_phone_number']) return json.dumps({'message': message}) customer_obj = models.CustomerTempRegistration(product_data=product_obj[0], new_customer_name = data_source[0]['customer_name'], new_number = data_source[0]['customer_phone_number'], product_purchase_date = data_source[0]['product_purchase_date'], temp_customer_id = temp_customer_id, dealer_asc_id = str(request.user)) customer_obj.save() logger.info('[Temporary_cust_registration]:: Initiating purchase feed') feed_remark = FeedLogWithRemark(len(data_source), feed_type='Purchase Feed', action='Received', status=True) sap_obj = SAPFeed() feed_response = sap_obj.import_to_db(feed_type='purchase', data_source=data_source, feed_remark=feed_remark) if feed_response.failed_feeds > 0: logger.info('[Temporary_cust_registration]:: ' + json.dumps(feed_response.remarks)) raise ValueError('purchase feed failed!') logger.info('[Temporary_cust_registration]:: purchase feed completed') except Exception as ex: logger.info(ex) return HttpResponseBadRequest() if existing_customer: return json.dumps({'message': CUST_UPDATE_SUCCESS}) return json.dumps({'message': CUST_REGISTER_SUCCESS + temp_customer_id})
def save_to_db(feed_type=None, data_source=[], feed_remark=None): sap_obj = SAPFeed() return sap_obj.import_to_db(feed_type=feed_type, data_source=data_source, feed_remark=feed_remark)