def list_terminals(request): """ API get list Terminal to select \n Parameters for this api : Có thể bỏ trống hoặc không gửi lên - name -- text - merchant_id -- number """ queryset = Terminal.objects.terminal_un_register_vnpayment() if request.user.is_superuser is False: if request.user.is_area_manager or request.user.is_sale_admin: provinces = get_provinces_viewable_queryset(request.user) queryset = queryset.filter(province_code__in=provinces.values('province_code')) else: shops = get_shops_viewable_queryset(request.user) queryset = queryset.filter(shop__in=shops) name = request.GET.get('name', None) merchant_id = request.GET.get('merchant_id', None) if name is not None and name != '': queryset = queryset.filter(Q(terminal_id__icontains=name) | Q(terminal_name__icontains=name)) if merchant_id is not None and merchant_id != '': queryset = queryset.filter(merchant_id=merchant_id) queryset = queryset.order_by('terminal_name')[0:settings.PAGINATE_BY] data = [{'id': terminal.id, 'name': terminal.terminal_id + ' - ' + terminal.terminal_name} for terminal in queryset] return successful_response(data)
def list_merchants(request): """ API get list Merchant to select \n Parameters for this api : Có thể bỏ trống hoặc không cần gửi lên - code -- text """ queryset = Merchant.objects.values('id', 'merchant_code', 'merchant_name', 'merchant_brand') if request.user.is_superuser is False: shops = get_shops_viewable_queryset(request.user) queryset = queryset.filter(pk__in=shops.values('merchant')) code = request.GET.get('code', None) if code is not None and code != '': queryset = queryset.filter( Q(merchant_code__icontains=code) | Q(merchant_brand__icontains=code)) queryset = queryset.order_by('merchant_brand')[0:settings.PAGINATE_BY] data = [{ 'id': merchant['id'], 'code': merchant['merchant_code'] + ' - ' + merchant['merchant_brand'] } for merchant in queryset] return successful_response(data)
def get_queryset_merchant_list(request): queryset = Merchant.objects.all() if request.user.is_superuser is False: shops = get_shops_viewable_queryset(request.user) queryset = queryset.filter(pk__in=shops.values('merchant')) merchant_text = request.query_params.get('merchant_text', None) area_id = request.query_params.get('area_id', None) province_id = request.query_params.get('province_id', None) status = request.query_params.get('status', None) from_date = request.query_params.get('from_date', None) to_date = request.query_params.get('to_date', None) if merchant_text is not None and merchant_text != '': merchant_text = format_string(merchant_text) queryset = queryset.filter( Q(merchant_code__icontains=merchant_text) | Q(merchant_name__icontains=merchant_text) | Q(merchant_brand__icontains=merchant_text)) if area_id is not None and area_id != '': if area_id.isdigit(): province_codes = [] area = Area.objects.get(pk=int(area_id)) provinces = area.get_provinces() for item in provinces: province_codes.append(item.province_code) queryset = queryset.filter(province_code__in=province_codes) if province_id is not None and province_id != '': if province_id.isdigit(): province = QrProvince.objects.get(pk=int(province_id)) queryset = queryset.filter(province_code=province.province_code) if status is not None and status != '': queryset = queryset.filter(status=status) if from_date is not None and from_date != '': queryset = queryset.filter(created_date__gte=datetime.strptime( from_date, '%d/%m/%Y').strftime('%Y-%m-%d %H:%M:%S')) if to_date is not None and to_date != '': queryset = queryset.filter(created_date__lte=( datetime.strptime(to_date, '%d/%m/%Y').strftime('%Y-%m-%d') + ' 23:59:59')) return queryset
def list_shop_for_search(request): """ API để search full text search không dấu các shop dựa trên địa chỉ, shop_code hoặc merchant brand, param là name """ name = request.GET.get('name', None) user_info = request.user queryset = get_shops_viewable_queryset(user_info) if name is not None and name != '': name = format_string(name) querysetABS = queryset.filter( Q(code__icontains=name) | Q(merchant__merchant_brand__icontains=name) | Q(address__icontains=name))[:10] lengQuerysetABS = len(querysetABS) if lengQuerysetABS < 10: name_en = unidecode(name).lower() search_query = SearchQuery(name_en) querysetFTS = queryset.annotate(rank=SearchRank( F('document'), search_query)).order_by('-rank').exclude( pk__in=querysetABS)[:(10 - lengQuerysetABS)] else: querysetFTS = [] else: querysetABS = queryset[:10] querysetFTS = [] data = [] for shop in itertools.chain(querysetABS, querysetFTS): code = shop.code if shop.code is not None else 'N/A' address = shop.address if shop.address is not None else 'N/A' merchant_brand = shop.merchant.merchant_brand if shop.merchant.merchant_brand is not None else 'N/A' data.append({ 'id': shop.id, 'shop_info': code + ' - ' + merchant_brand + ' - ' + address }) return successful_response(data)
def list_recommend_shops(request, pk): ''' API for get shop number_transaction and nearly shops \n :param shop_id ''' current_shop = get_object_or_404(Shop, pk=pk) current_shop_shop_cube = ShopCube.objects.filter(shop_id=pk).first() day = date.today().day if current_shop_shop_cube is not None: if 1 <= day <= 7: current_shop_number_of_tran = current_shop_shop_cube.number_of_tran_w_1_7 \ if current_shop_shop_cube.number_of_tran_w_1_7 is not None \ and current_shop_shop_cube.number_of_tran_w_1_7 != '' \ else '' if 8 <= day <= 14: current_shop_number_of_tran = current_shop_shop_cube.number_of_tran_w_8_14 \ if current_shop_shop_cube.number_of_tran_w_8_14 is not None \ and current_shop_shop_cube.number_of_tran_w_8_14 != '' \ else '' if 15 <= day <= 21: current_shop_number_of_tran = current_shop_shop_cube.number_of_tran_w_15_21 \ if current_shop_shop_cube.number_of_tran_w_15_21 is not None \ and current_shop_shop_cube.number_of_tran_w_15_21 != '' \ else '' if 22 <= day: current_shop_number_of_tran = current_shop_shop_cube.number_of_tran_w_22_end \ if current_shop_shop_cube.number_of_tran_w_22_end is not None \ and current_shop_shop_cube.number_of_tran_w_22_end != '' \ else '' else: current_shop_number_of_tran = '' nearly_shops_by_latlong = [] if current_shop.wards != '' and current_shop.wards is not None: try: shop_list = get_shops_viewable_queryset(request.user) \ .filter(wards=current_shop.wards, activated=1).exclude(pk=pk) for shop in shop_list: code = shop.code if shop.code is not None else '' address = shop.address if shop.address is not None else '' if shop.latitude and current_shop.latitude: distance = findDistance(shop.latitude, shop.longitude, current_shop.latitude, current_shop.longitude) nearly_shops_by_latlong.append({ 'id': shop.id, 'shop_info_html': '<strong>' + code + ' - ' + shop.name + '</strong> - ' + address, 'shop_info': code + ' - ' + shop.name + ' - ' + address, 'address': shop.address, 'latitude': shop.latitude, 'longitude': shop.longitude, 'distance_value': distance.get('value'), 'distance_text': distance.get('text') }) nearly_shops_by_latlong_sorted = sorted( nearly_shops_by_latlong, key=lambda k: k['distance_value']) except Exception as e: print(str(e)) data = { 'address': current_shop.address if current_shop.address != '' and current_shop.address is not None else '', 'street': current_shop.street if current_shop.street != '' and current_shop.street is not None else '', 'number_of_tran': current_shop_number_of_tran, 'latitude': current_shop.latitude, 'longitude': current_shop.longitude, 'nearly_shops': nearly_shops_by_latlong_sorted } return successful_response(data)
def get_queryset_shop_list(request, export_data=False): status = request.query_params.get('status', None) shop_obj = Shop if not export_data: shop_obj = ShopFullData if status is not None and status != '': if status == '0': queryset = shop_obj.objects.shop_active().filter( Q(street__isnull=True) | Q(street='')) elif status == '1': queryset = shop_obj.objects.shop_disable() elif status == '2': shop_caring_lists = StaffCare.objects.filter( type=StaffCareType.STAFF_SHOP).values('shop') queryset = shop_obj.objects.shop_active().filter(~Q( pk__in=shop_caring_lists)) else: if status == '3': shop_lists = ShopCube.objects.number_of_tran_this_week( value=1).values('shop_id') elif status == '4': shop_lists = ShopCube.objects.number_of_tran_this_week( value=2).values('shop_id') elif status == '5': shop_lists = ShopCube.objects.number_of_tran_this_week( value=3).values('shop_id') else: shop_lists = ShopCube.objects.number_of_tran_this_week( value=0).values('shop_id') queryset = shop_obj.objects.shop_active().filter(pk__in=shop_lists) else: queryset = shop_obj.objects.shop_active() if request.user.is_superuser is False: if request.user.is_area_manager or request.user.is_sale_admin: if request.user.is_manager_outside_vnpay: shops = get_shops_viewable_queryset(request.user) queryset = queryset.filter(pk__in=shops) else: provinces_viewable = get_provinces_viewable_queryset( request.user) cross_assign_status = request.query_params.get( 'cross_assign_status', None) if cross_assign_status is not None and cross_assign_status != '': if cross_assign_status == '0': staff_viewable = get_staffs_viewable_queryset( request.user) shop_ids = StaffCare.objects.filter(staff__in=staff_viewable, type=StaffCareType.STAFF_SHOP) \ .values('shop_id') queryset = queryset.filter(pk__in=shop_ids).exclude( province__in=provinces_viewable) if cross_assign_status == '1': staff_viewable = get_staffs_viewable_queryset( request.user) shop_ids = StaffCare.objects.filter(type=StaffCareType.STAFF_SHOP) \ .exclude(staff__in=staff_viewable).values('shop_id') queryset = queryset.filter( pk__in=shop_ids, province__in=provinces_viewable) if cross_assign_status == '2': staff_viewable = get_staffs_viewable_queryset( request.user) shop_id_can_view = StaffCare.objects.filter(staff__in=staff_viewable, type=StaffCareType.STAFF_SHOP) \ .values('shop_id') shop_id_can_not_view = StaffCare.objects.filter(type=StaffCareType.STAFF_SHOP) \ .exclude(staff__in=staff_viewable).values('shop_id') queryset = queryset.filter( (Q(pk__in=shop_id_can_not_view) & Q(province__in=provinces_viewable)) | (Q(pk__in=shop_id_can_view) & ~Q(province__in=provinces_viewable))) else: queryset = queryset.filter( province_id__in=provinces_viewable) else: shops = get_shops_viewable_queryset(request.user) queryset = queryset.filter(pk__in=shops) code = request.query_params.get('code', None) merchant_id = request.query_params.get('merchant_id', None) team_id = request.query_params.get('team_id', None) staff_id = request.query_params.get('staff_id', None) area_id = request.query_params.get('area_id', None) province_id = request.query_params.get('province_id', None) district_id = request.query_params.get('district_id', None) ward_id = request.query_params.get('ward_id', None) from_date = request.query_params.get('from_date', None) to_date = request.query_params.get('to_date', None) if code is not None and code != '': code = format_string(code) queryset = queryset.filter( Q(code__icontains=code) | Q(name__icontains=code)) if merchant_id is not None and merchant_id != '': queryset = queryset.filter(merchant_id=merchant_id) if team_id is not None and team_id != '': staffs = Staff.objects.filter(team=team_id) shop_ids = StaffCare.objects.filter( staff__in=staffs, type=StaffCareType.STAFF_SHOP).values('shop_id') queryset = queryset.filter(pk__in=shop_ids) if staff_id is not None and staff_id != '': shop_ids = StaffCare.objects.filter( staff=staff_id, type=StaffCareType.STAFF_SHOP).values('shop_id') queryset = queryset.filter(pk__in=shop_ids) if area_id is not None and area_id != '': if area_id.isdigit(): area = Area.objects.get(pk=int(area_id)) provinces = area.get_provinces() queryset = queryset.filter(province_id__in=provinces) if province_id is not None and province_id != '': queryset = queryset.filter(province_id=province_id) if district_id is not None and district_id != '': queryset = queryset.filter(district_id=district_id) if ward_id is not None and ward_id != '': queryset = queryset.filter(wards_id=ward_id) if from_date is not None and from_date != '': queryset = queryset.filter(created_date__gte=datetime.strptime( from_date, '%d/%m/%Y').strftime('%Y-%m-%d %H:%M:%S')) if to_date is not None and to_date != '': queryset = queryset.filter(created_date__lte=( datetime.strptime(to_date, '%d/%m/%Y').strftime('%Y-%m-%d') + ' 23:59:59')) return queryset.order_by('-id')
def retrieve(self, request, pk): """ API get detail Shop """ if request.user.is_superuser is False: if request.user.is_area_manager or request.user.is_sale_admin: provinces = get_provinces_viewable_queryset(request.user) shop = Shop.objects.filter(pk=pk, province__in=provinces).first() else: shops = get_shops_viewable_queryset(request.user) shop = Shop.objects.filter(pk=pk, pk__in=shops).first() else: shop = Shop.objects.filter(pk=pk).first() if shop is None: return custom_response(Code.SHOP_NOT_FOUND) first_terminal = shop.terminals.order_by('created_date').first() if shop.shop_cube and shop.shop_cube.voucher_code_list is not None and shop.shop_cube.voucher_code_list != '[]': if '[' not in shop.shop_cube.voucher_code_list: voucher_code_list = shop.shop_cube.voucher_code_list else: voucher_code_list = ast.literal_eval( shop.shop_cube.voucher_code_list) else: voucher_code_list = '' data = { 'name': shop.name, 'code': shop.code, 'address': shop.address, 'street': shop.street, 'phone': first_terminal.qr_terminal_contact.terminal_phone if (first_terminal and first_terminal.qr_terminal_contact) else None, 'created_date': formats.date_format(shop.created_date, "SHORT_DATETIME_FORMAT") if shop.created_date else '', 'first_terminal_created_date': formats.date_format(first_terminal.created_date, "SHORT_DATETIME_FORMAT") if first_terminal and first_terminal.created_date else None, 'merchant': { 'id': shop.merchant.id if shop.merchant else None, 'merchant_code': shop.merchant.merchant_code if shop.merchant else None, 'merchant_name': shop.merchant.merchant_name if shop.merchant else None, 'merchant_brand': shop.merchant.merchant_brand if shop.merchant else None }, 'staff': { 'id': shop.staff.id if shop.staff else None, 'full_name': shop.staff.full_name if shop.staff else None, 'email': shop.staff.email if shop.staff else None, 'phone': shop.staff.mobile if shop.staff else None, }, 'team': { 'id': shop.team.id if shop.team else None, 'name': shop.team.name if shop.team else None, 'code': shop.team.code if shop.team else None }, 'staff_of_chain': { 'id': shop.staff_of_chain.id if shop.staff_of_chain else None, 'full_name': shop.staff_of_chain.full_name if shop.staff_of_chain else None, 'email': shop.staff_of_chain.email if shop.staff_of_chain else None, 'phone': shop.staff_of_chain.mobile if shop.staff_of_chain else None, }, 'team_of_chain': { 'id': shop.team_of_chain.id if shop.team_of_chain else None, 'name': shop.team_of_chain.name if shop.team_of_chain else None, 'code': shop.team_of_chain.code if shop.team_of_chain else None }, 'shop_cube': { 'report_date': shop.shop_cube.report_date, 'number_of_tran_acm': shop.shop_cube.number_of_tran_acm, 'number_of_tran_last_m': shop.shop_cube.number_of_tran_last_m, 'number_of_tran': shop.shop_cube.number_of_tran, 'number_of_tran_w_1_7': shop.shop_cube.number_of_tran_w_1_7, 'number_of_tran_w_8_14': shop.shop_cube.number_of_tran_w_8_14, 'number_of_tran_w_15_21': shop.shop_cube.number_of_tran_w_15_21, 'number_of_tran_w_22_end': shop.shop_cube.number_of_tran_w_22_end, 'voucher_code_list': voucher_code_list, } if shop.shop_cube else None, 'province': { 'id': shop.province.id, 'name': shop.province.province_name, 'code': shop.province.province_code } if shop.province else None, 'district': { 'id': shop.district.id, 'name': shop.district.district_name, 'code': shop.district.district_code } if shop.district else None, 'wards': { 'id': shop.wards.id, 'name': shop.wards.wards_name, 'code': shop.wards.wards_code } if shop.wards else None, } return successful_response(data)
def get_queryset_terminal_list(request): queryset = Terminal.objects.terminal_un_register_vnpayment() if request.user.is_superuser is False: if request.user.is_area_manager or request.user.is_sale_admin: # Neu la SM (or SA) cua tripi, teko thi load ds shop => ds ter if request.user.is_manager_outside_vnpay: shops = get_shops_viewable_queryset(request.user) queryset = queryset.filter(shop__in=shops) # Neu la SM (or SA) cua Vnpay thi load ds provinces => ds ter else: provinces = get_provinces_viewable_queryset(request.user) queryset = queryset.filter(province_code__in=provinces.values('province_code')) else: shops = get_shops_viewable_queryset(request.user) queryset = queryset.filter(shop__in=shops) shop_id = request.query_params.get('shop_id', None) terminal_text = request.query_params.get('terminal_text', None) area_id = request.query_params.get('area_id', None) merchant_id = request.query_params.get('merchant_id', None) staff_id = request.query_params.get('staff_id', None) team_id = request.query_params.get('team_id', None) status = request.query_params.get('status', None) province_code = request.query_params.get('province_code', None) district_code = request.query_params.get('district_code', None) ward_code = request.query_params.get('ward_code', None) from_date = request.query_params.get('from_date', None) to_date = request.query_params.get('to_date', None) assign_shop = request.query_params.get('assign_shop', None) if shop_id is not None and shop_id != '': shop_id = format_string(shop_id) queryset = queryset.filter(shop_id=shop_id) if terminal_text is not None and terminal_text != '': terminal_text = format_string(terminal_text) queryset = queryset.filter( Q(terminal_id__icontains=terminal_text) | Q(terminal_name__icontains=terminal_text)) if area_id is not None and area_id != '': if area_id.isdigit(): province_codes = [] area = Area.objects.get(pk=int(area_id)) provinces = area.get_provinces() for item in provinces: province_codes.append(item.province_code) queryset = queryset.filter(province_code__in=province_codes) if merchant_id is not None and merchant_id != '': queryset = queryset.filter(merchant_id=merchant_id) if staff_id is not None and staff_id != '': shops = StaffCare.objects.filter(staff_id=staff_id, type=StaffCareType.STAFF_SHOP).values('shop') queryset = queryset.filter(shop__in=shops) if team_id is not None and team_id != '': staffs = Staff.objects.filter(team_id=team_id) shops = StaffCare.objects.filter(staff__in=staffs, type=StaffCareType.STAFF_SHOP).values('shop') queryset = queryset.filter(shop__in=shops) if status is not None and status != '': queryset = queryset.filter(status=int(status)) if province_code is not None and province_code != '': queryset = queryset.filter(province_code=province_code) if district_code is not None and district_code != '': queryset = queryset.filter(district_code=district_code) if ward_code is not None and ward_code != '': queryset = queryset.filter(wards_code=ward_code) if assign_shop is not None and assign_shop != '': if assign_shop == '1': queryset = queryset.filter(shop__isnull=False) else: queryset = queryset.filter(shop__isnull=True) if from_date is not None and from_date != '': queryset = queryset.filter( created_date__gte=datetime.strptime(from_date, '%d/%m/%Y').strftime('%Y-%m-%d %H:%M:%S')) if to_date is not None and to_date != '': queryset = queryset.filter( created_date__lte=(datetime.strptime(to_date, '%d/%m/%Y').strftime('%Y-%m-%d') + ' 23:59:59')) return queryset
def detail(request, pk): try: if request.user.is_superuser is False: if request.user.is_area_manager or request.user.is_sale_admin: provinces = get_provinces_viewable_queryset(request.user) terminal = Terminal.objects.filter(pk=pk, province_code__in=provinces.values('province_code')).first() else: shops = get_shops_viewable_queryset(request.user) terminal = Terminal.objects.filter(pk=pk, shop__in=shops).first() else: terminal = Terminal.objects.filter(pk=pk).first() if terminal is None: return custom_response(Code.TERMINAL_NOT_FOUND) merchant = terminal.merchant shop = terminal.shop staff = shop.staff if shop else None team = staff.team if staff else None staff_chain = shop.staff_of_chain if shop else None team_chain = staff_chain.team if staff_chain else None data = { 'id': terminal.id, 'terminal_id': terminal.terminal_id, 'terminal_name': terminal.terminal_name, 'terminal_address': terminal.terminal_address, 'province': {'id': terminal.get_province().id, 'name': terminal.get_province().province_name, 'code': terminal.get_province().province_code} if terminal.get_province() else None, 'district': {'id': terminal.get_district().id, 'name': terminal.get_district().district_name, 'code': terminal.get_district().district_code} if terminal.get_district() else None, 'wards': {'id': terminal.get_wards().id, 'name': terminal.get_wards().wards_name, 'code': terminal.get_wards().wards_code} if terminal.get_wards() else None, 'business_address': terminal.business_address, 'merchant': { 'id': merchant.id if merchant else '', 'name': merchant.merchant_name if merchant else '', 'code': merchant.merchant_code if merchant else '', 'brand': merchant.merchant_brand if merchant else '', }, 'shop': { 'id': shop.id if shop else '', 'name': shop.name if shop else '', 'code': shop.code if shop else '', 'address': shop.address if shop else '', 'street': shop.street if shop else '', 'take_care_status': shop.take_care_status if shop else '', 'activated': shop.activated if shop else '', 'province_name': shop.province.province_name if (shop and shop.province) else '', 'district_name': shop.district.district_name if (shop and shop.district) else '', 'wards_name': shop.wards.wards_name if (shop and shop.wards) else '', 'staff': { 'id': staff.id if staff else None, 'full_name': staff.full_name if staff else '', 'email': staff.email if staff else '' }, 'team': { 'id': team.id if team else None, 'code': team.code if team else '', 'name': team.name if team else '' }, 'staff_chain': { 'id': staff_chain.id if staff_chain else None, 'full_name': staff_chain.full_name if staff_chain else '', 'email': staff_chain.email if staff_chain else '' }, 'team_chain': { 'id': team_chain.id if team_chain else None, 'code': team_chain.code if team_chain else '', 'name': team_chain.name if team_chain else '' } }, 'created_date': formats.date_format(terminal.created_date, "SHORT_DATETIME_FORMAT") if terminal.created_date else '', 'status': int(terminal.get_status()) if terminal.get_status() else None, } return successful_response(data) except Exception as e: logging.error('Get detail terminal exception: %s', e) return custom_response(Code.INTERNAL_SERVER_ERROR)
def detail(request, pk): # API detail try: if request.user.is_superuser is False: shops = get_shops_viewable_queryset(request.user) merchant = Merchant.objects.filter( pk=pk, pk__in=shops.values('merchant')).first() else: merchant = Merchant.objects.filter(pk=pk).first() if merchant is None: return custom_response(Code.MERCHANT_NOT_FOUND) staff = merchant.get_staff() merchant_info = merchant.merchant_info data = { 'merchant_id': merchant.id, 'merchant_code': merchant.merchant_code, 'merchant_brand': merchant.merchant_brand, 'merchant_name': merchant.merchant_name, 'address': merchant.address, 'type': merchant.get_type().full_name if merchant.get_type() else '', 'total_shop': merchant.shops.count(), 'total_terminal': merchant.terminals.count(), 'merchant_info': { 'contact_name': merchant_info.contact_name if merchant_info else 'N/A' }, 'staff': { 'full_name': staff.full_name if staff is not None else '', 'email': staff.email if staff is not None else '' }, 'staff_care': { "full_name": merchant.staff_care.full_name if merchant.staff_care.full_name else 'N/A', "email": merchant.staff_care.email if merchant.staff_care.email else 'N/A', "team": merchant.staff_care.team.name if merchant.staff_care.team else 'N/A', } if merchant.staff_care else { "full_name": 'N/A', "email": 'N/A', "team": 'N/A', }, 'created_date': formats.date_format(merchant.created_date, "SHORT_DATETIME_FORMAT") if merchant.created_date else '', 'status': int(merchant.get_status()) if merchant.get_status() else None, 'merchant_cube': merchant.get_merchant_cube(), } return successful_response(data) except Exception as e: logging.error('Get detail merchant exception: %s', e) return custom_response(Code.INTERNAL_SERVER_ERROR)