Пример #1
0
def daily_transactions(request):
    error = False
    msg = ''
    search = request.GET.get('q')
    # Default payment type is withdrawal i.e. 2
    payment_type = request.GET.get('transaction_type', 2)
    try:
        payment_type = int(payment_type)
    except Exception as e:
        payment_type = 2
    queryset = PaymentHistory.objects.filter(
        transaction_date__range=get_today_range(),
        payment_type=payment_type
    ).values(
        'transaction_date', 'payment_type', 'transaction_amount', 'balance_amount',
        username=F('user__username'), user_id=F('user__id'),
    )

    if search:
        queryset = queryset.filter(
            Q(user__id__icontains=search) |
            Q(user__username__icontains=search)
        )

    context_data = dict()
    context_data['error'] = error
    context_data['message'] = msg
    context_data['result'] = list(queryset)
    return Response(context_data, status=HTTP_200_OK)
def game_report(request):
    error = False
    msg = ''
    market = request.GET.get('market')
    context_data = dict()
    game_result = PaymentHistory.objects.filter(
        player__isnull=False, bet__isnull=False, player__market_id=market,
        transaction_date__range=get_today_range()
    )
    result = {}
    for i in game_result:
        if i.player.game_id in result:
            if i.bet.bet_number in result[i.player.game_id]:
                result[i.player.game_id][i.bet.bet_number] += i.bet.bet_amount
            else:
                result[i.player.game_id][i.bet.bet_number] = i.bet.bet_amount
        else:
            result[i.player.game_id] = {}
            if i.bet.bet_number in result[i.player.game_id]:
                result[i.player.game_id][i.bet.bet_number] += i.bet.bet_amount
            else:
                result[i.player.game_id][i.bet.bet_number] = i.bet.bet_amount
    context_data['error'] = error
    context_data['message'] = msg
    context_data['result'] = result
    return JsonResponse(context_data, status=200)
def daily_result(request):
    try:
        game_result = GameResult.objects.filter(result_date__range=get_today_range(False, True)).values(
            'single', 'panel', 'panel_type', 'result_date', 'id', 'market_id'
        )
        markets_queryset = Market.objects.filter(
            is_active=True
        ).order_by('id').values('id', 'market_name', 'market_type')
        market_df = pd.DataFrame(list(markets_queryset))
        market_df['game_result'] = market_df.apply(lambda x: get_result_from_market(x, game_result), axis=1)
        market_df['market_name'] = market_df['market_name'].apply(lambda x: str(x).lower())
        market_group = market_df.groupby('market_name')
        result = market_group.apply(lambda x: x[:2].to_dict(orient='records')).to_dict()
        market_name_list = []
        for i in markets_queryset.values_list('market_name', flat=True):
            market_name = str(i).lower()
            if market_name not in market_name_list:
                market_name_list.append(market_name)
        error = False
        msg = ''
    except Exception as e:
        error = True
        msg = str(e)
        result = {}
        market_name_list = []
    context_data = dict()
    context_data['error'] = error
    context_data['message'] = msg
    context_data['result'] = result
    context_data['market_name_list'] = market_name_list
    return JsonResponse(context_data, status=200)
def game_result_list(request):
    error = False
    msg = ''
    context_data = dict()
    game_result = GameResult.objects.filter(result_date__range=get_today_range()).order_by('-id').annotate(
        market_name=F('market__market_name'),
        market_type=F('market__market_type')
    ).values(
        'single', 'panel', 'panel_type', 'result_date', 'id', 'market_id', 'market_name', 'market_type'
    )
    context_data['error'] = error
    context_data['message'] = msg
    context_data['result'] = list(game_result)
    return JsonResponse(context_data, status=200)
def update_market_result(request):
    error = False
    msg = ''
    market = request.data.get('market')
    single = request.data.get('single')
    panel = request.data.get('panel')
    panel_type = request.data.get('panel_type')
    if not error and not market:
        error = True
        msg = 'Please select market'

    if not error:
        try:
            single = int(single)
        except Exception as e:
            error = True
            msg = 'Please select single'

    if not error and not panel:
        error = True
        msg = 'Please select panel'

    if not error and not panel_type:
        error = True
        msg = 'Please select panel type'

    if not error:
        if not request.user.is_superuser:
            error = True
            msg = 'You are not the admin user'

    if not error:
        try:
            with transaction.atomic():
                game_result = GameResult.objects.create(
                    market_id=market,
                    single=single,
                    panel=panel,
                    panel_type=panel_type
                )
                date_range = get_today_range()
                if game_result.market_id in [13, 14]:
                    date_range = get_today_range(is_past_date=True)
                payment_history = PaymentHistory.objects.filter(
                    transaction_date__range=date_range,
                    player__isnull=False,
                    bet__isnull=False,
                    payment_type=constants.PAYMENT_TYPE_PLAY
                )
                for obj in payment_history:
                    u = UserProfile.objects.get(user=obj.player.user)
                    if game_result.market.id == obj.player.market.id:  # OPEN
                        if int(obj.player.game.game_type) == constants.GAME_TYPE_SINGLE:
                            #
                            # single
                            if int(obj.bet.bet_number) == int(game_result.single):
                                obj.bet.win_amount = obj.bet.bet_amount * \
                                                     constants.GAME_SINGLE_RATE
                                obj.bet.result_status = \
                                    constants.RESULT_STATUS_WIN
                                u.account_balance += obj.bet.win_amount
                                u.save()
                                obj.bet.save()
                                obj.payment_type = constants.PAYMENT_TYPE_WIN
                                obj.transaction_type = \
                                    constants.TRANSACTION_TYPE_CREDIT
                                obj.balance_amount += u.account_balance
                                obj.save()
                            else:
                                obj.bet.result_status = \
                                    constants.RESULT_STATUS_LOSS
                                obj.bet.save()
                                obj.payment_type = constants.PAYMENT_TYPE_LOSS
                                obj.transaction_type = \
                                    constants.TRANSACTION_TYPE_DEBIT
                                obj.save()
                        if int(game_result.panel_type) == constants.PANEL_TYPE_SINGLE:
                            if int(obj.player.game.game_type) == \
                                    constants.GAME_TYPE_SINGLE_PANEL:
                                if int(obj.bet.bet_number) == int(game_result.panel):
                                    obj.bet.win_amount = obj.bet.bet_amount *\
                                                         constants.GAME_SINGLE_PANEL_RATE
                                    obj.bet.result_status = constants.RESULT_STATUS_WIN
                                    u.account_balance += obj.bet.win_amount
                                    u.save()
                                    obj.bet.save()
                                    obj.payment_type = constants.PAYMENT_TYPE_WIN
                                    obj.transaction_type = constants.TRANSACTION_TYPE_CREDIT
                                    obj.balance_amount += u.account_balance
                                    obj.save()
                                else:
                                    obj.bet.result_status = constants.RESULT_STATUS_LOSS
                                    obj.bet.save()
                                    obj.payment_type = constants.PAYMENT_TYPE_LOSS
                                    obj.transaction_type = constants.TRANSACTION_TYPE_DEBIT
                                    obj.save()
                            elif int(obj.player.game.game_type) == \
                                    constants.GAME_TYPE_DOUBLE_PANEL:
                                obj.bet.result_status = constants.RESULT_STATUS_LOSS
                                obj.bet.save()
                                obj.payment_type = constants.PAYMENT_TYPE_LOSS
                                obj.transaction_type = constants.TRANSACTION_TYPE_DEBIT
                                obj.save()
                        elif int(game_result.panel_type) == constants.PANEL_TYPE_DOUBLE:
                            if int(obj.player.game.game_type) == constants.GAME_TYPE_DOUBLE_PANEL:
                                if int(obj.bet.bet_number) == int(game_result.panel):
                                    obj.bet.win_amount = obj.bet.bet_amount *\
                                                         constants.GAME_DOUBLE_PANEL_RATE
                                    obj.bet.result_status = constants.RESULT_STATUS_WIN
                                    u.account_balance += obj.bet.win_amount
                                    u.save()
                                    obj.bet.save()
                                    obj.payment_type = constants.PAYMENT_TYPE_WIN
                                    obj.transaction_type = constants.TRANSACTION_TYPE_CREDIT
                                    obj.balance_amount += u.account_balance
                                    obj.save()
                                else:
                                    obj.bet.result_status = constants.RESULT_STATUS_LOSS
                                    obj.bet.save()
                                    obj.payment_type = constants.PAYMENT_TYPE_LOSS
                                    obj.transaction_type = constants.TRANSACTION_TYPE_DEBIT
                                    obj.save()
                            elif int(obj.player.game.game_type) == \
                                    constants.GAME_TYPE_SINGLE_PANEL:
                                obj.bet.result_status = constants.RESULT_STATUS_LOSS
                                obj.bet.save()
                                obj.payment_type = constants.PAYMENT_TYPE_LOSS
                                obj.transaction_type = constants.TRANSACTION_TYPE_DEBIT
                                obj.save()

                    if game_result.market.id == obj.player.market.id + 1:
                        if int(game_result.market.market_type) == constants.MARKET_TYPE_CLOSE:
                            calculate_market_id = int(game_result.market_id) - 1
                            g_result = GameResult.objects.filter(
                                market_id=calculate_market_id,
                                result_date__range=date_range
                            ).order_by('-id').first()

                            if g_result:
                                if int(obj.player.game.game_type) == \
                                        constants.GAME_TYPE_JODI:
                                    if obj.bet.result_status == constants.RESULT_STATUS_PENDING:
                                        b = str(obj.bet.bet_number)
                                        if len(b) == 1:
                                            b = '0' + b
                                        if b == str(g_result.single) + str(game_result.single):
                                            obj.bet.win_amount = \
                                                obj.bet.bet_amount * \
                                                constants.GAME_JODI_RATE
                                            obj.bet.result_status = \
                                                constants.RESULT_STATUS_WIN
                                            u.account_balance += obj.bet.win_amount
                                            u.save()
                                            obj.bet.save()
                                            obj.payment_type = constants.PAYMENT_TYPE_WIN
                                            obj.transaction_type = constants.TRANSACTION_TYPE_CREDIT
                                            obj.balance_amount += u.account_balance
                                            obj.save()
                                        else:
                                            obj.bet.result_status = \
                                                constants.RESULT_STATUS_LOSS
                                            obj.bet.save()
                                            obj.payment_type = constants.PAYMENT_TYPE_LOSS
                                            obj.transaction_type = \
                                                constants.TRANSACTION_TYPE_DEBIT
                                            obj.save()
            error = False
            msg = 'Ok'
        except Exception as e:
            error = True
            msg = str(e)
    context_data = dict()
    context_data['error'] = error
    context_data['message'] = msg
    return JsonResponse(context_data, status=200)
def get_payment_history(request):
    context_data = dict()
    error = False
    msg = ''
    user_id = request.GET.get('user_id', None)
    market_id = request.GET.get('market_id', None)
    user = request.user

    if user_id:
        try:
            user = User.objects.get(id=user_id)
        except Exception as e:
            user = request.user

    try:
        queryset = PaymentHistory.objects.filter(
            user_id=user,
            payment_type__in=[
                constants.PAYMENT_TYPE_WIN,
                constants.PAYMENT_TYPE_PLAY,
                constants.PAYMENT_TYPE_LOSS,
                constants.PAYMENT_TYPE_CANCELLED
            ],
        ).order_by('-transaction_date').annotate(
            transaction_id=F('id'),
            player_game=F('player__game__name'),
            player_game_type=F('player__game__game_type'),
            market_name=F('player__market__market_name'),
            market_type=F('player__market__market_type'),
            market_time=F('player__market__market_time'),
            bet_number=F('bet__bet_number'),
            bet_amount=F('bet__bet_amount'),
            win_amount=F('bet__win_amount'),
            result_status=F('bet__result_status')
        ).values(
            'transaction_id', 'transaction_date', 'transaction_amount', 'transaction_type', 'balance_amount',
            'payment_type', 'user_id', 'player_id', 'bet_id', 'player_game', 'player_game_type', 'market_name',
            'market_type', 'bet_number', 'bet_amount', 'win_amount',
            'result_status', 'market_time'
        )
        if market_id:
            queryset = queryset.filter(
                player__market_id=market_id,
                transaction_date__range=get_today_range(False, True)
            )

        data_frame = pd.DataFrame(list(queryset))
        data_frame['can_cancel'] = False
        # data_frame['can_cancel'] = data_frame.apply(
        #     lambda x: can_play_cancel(x), axis=1)

        context_data['result'] = data_frame.to_dict(orient='records')
        # context_data['result'] = list(queryset)
        error = False
        msg = ''
    except Exception as e:
        error = True
        msg = str(e)
    context_data['error'] = error
    context_data['message'] = msg
    return JsonResponse(context_data, status=200)