Beispiel #1
0
def purchase(*args, **kwargs):
    if request.method == 'GET':
        all_items = request.args.get('all', None)
        completed = request.args.get('completed', None)
        if completed is not None:
            completed = True if completed == 1 or completed == '1' else False
        prescheduled = request.args.get('prescheduled', None)
        if not prescheduled:
            first_day, last_day = calendar.monthrange(
                datetime.datetime.now().date().year,
                datetime.datetime.now().date().month)
            first_day = datetime.datetime.now().date().replace(day=1)
            last_day = datetime.datetime.now().date().replace(day=last_day)
            if all_items and completed is None:
                return app_response(data={'items': Purchase. \
                                    where('{}.updated_at'.format(Purchase.__table__), '>=', first_day). \
                                    where('{}.updated_at'.format(Purchase.__table__), '<=', last_day). \
                                    or_where('{}.complete'.format(Purchase.__table__), False). \
                                    join(table=Loan.__table__,
                                         one='{}.id'.format(Loan.__table__),
                                         operator='=',
                                         two='{}.loan'.format(Purchase.__table__),
                                         type='left outer'). \
                                    join(table=Debt.__table__,
                                         one='{}.id'.format(Debt.__table__),
                                         operator='=',
                                         two='{}.debt'.format(Purchase.__table__),
                                         type='left outer'). \
                                    join(table=User.__table__,
                                         one='{}.id'.format(User.__table__),
                                         operator='=',
                                         two='{}.creator'.format(Purchase.__table__),
                                         type='left outer'). \
                                    join(table=Currency.__table__,
                                         one='{}.id'.format(Currency.__table__),
                                         operator='=',
                                         two='{}.currency'.format(Purchase.__table__),
                                         type='left outer'). \
                                    group_by(
                                        '{}.id'.format(Purchase.__table__),
                                        '{}.closed'.format(Loan.__table__),
                                        '{}.complete'.format(Debt.__table__),
                                        '{}.end_date'.format(Debt.__table__),
                                        '{}.name'.format(Currency.__table__),
                                        '{}.first_name'.format(User.__table__),
                                    ). \
                                    order_by_raw('{}.updated_at DESC NULLS LAST'.format(Purchase.__table__)). \
                                    get([
                                        '{}.*'.format(Purchase.__table__),
                                        '{}.closed as loan_closed'.format(Loan.__table__),
                                        '{}.complete as debt_complete'.format(Debt.__table__),
                                        '{}.end_date as debt_end_date'.format(Debt.__table__),
                                        '{}.name as currency_name'.format(Currency.__table__),
                                        '{}.first_name as creator_name'.format(User.__table__),
                                    ]). \
                                    serialize()})
            elif all_items and completed is not None:
                return app_response(data={'items': Purchase. \
                                    where('{}.updated_at'.format(Purchase.__table__), '>=', first_day). \
                                    where('{}.updated_at'.format(Purchase.__table__), '<=', last_day). \
                                    where('{}.complete'.format(Purchase.__table__), completed). \
                                    join(table=Loan.__table__,
                                         one='{}.id'.format(Loan.__table__),
                                         operator='=',
                                         two='{}.loan'.format(Purchase.__table__),
                                         type='left outer'). \
                                    join(table=Debt.__table__,
                                         one='{}.id'.format(Debt.__table__),
                                         operator='=',
                                         two='{}.debt'.format(Purchase.__table__),
                                         type='left outer'). \
                                    join(table=User.__table__,
                                         one='{}.id'.format(User.__table__),
                                         operator='=',
                                         two='{}.creator'.format(Purchase.__table__),
                                         type='left outer'). \
                                    join(table=Currency.__table__,
                                         one='{}.id'.format(Currency.__table__),
                                         operator='=',
                                         two='{}.currency'.format(Purchase.__table__),
                                         type='left outer'). \
                                    group_by(
                                        '{}.id'.format(Purchase.__table__),
                                        '{}.closed'.format(Loan.__table__),
                                        '{}.complete'.format(Debt.__table__),
                                        '{}.end_date'.format(Debt.__table__),
                                        '{}.name'.format(Currency.__table__),
                                        '{}.first_name'.format(User.__table__),
                                    ). \
                                    order_by_raw('{}.updated_at DESC NULLS LAST'.format(Purchase.__table__)). \
                                    get([
                                        '{}.*'.format(Purchase.__table__),
                                        '{}.closed as loan_closed'.format(Loan.__table__),
                                        '{}.complete as debt_complete'.format(Debt.__table__),
                                        '{}.end_date as debt_end_date'.format(Debt.__table__),
                                        '{}.name as currency_name'.format(Currency.__table__),
                                        '{}.first_name as creator_name'.format(User.__table__),
                                    ]). \
                                    serialize()})
        else:
            purchases = Purchase. \
                where('{}.complete'.format(Purchase.__table__), False). \
                join(table=Loan.__table__,
                     one='{}.id'.format(Loan.__table__),
                     operator='=',
                     two='{}.loan'.format(Purchase.__table__),
                     type='left outer'). \
                join(table=Debt.__table__,
                     one='{}.id'.format(Debt.__table__),
                     operator='=',
                     two='{}.debt'.format(Purchase.__table__),
                     type='left outer'). \
                join(table=User.__table__,
                     one='{}.id'.format(User.__table__),
                     operator='=',
                     two='{}.creator'.format(Purchase.__table__),
                     type='left outer'). \
                join(table=Currency.__table__,
                     one='{}.id'.format(Currency.__table__),
                     operator='=',
                     two='{}.currency'.format(Purchase.__table__),
                     type='left outer'). \
                group_by(
                    '{}.id'.format(Purchase.__table__),
                    '{}.closed'.format(Loan.__table__),
                    '{}.complete'.format(Debt.__table__),
                    '{}.end_date'.format(Debt.__table__),
                    '{}.name'.format(Currency.__table__),
                    '{}.first_name'.format(User.__table__),
                ). \
                order_by_raw('{}.updated_at DESC NULLS LAST'.format(Purchase.__table__)). \
                get([
                    '{}.*'.format(Purchase.__table__),
                    '{}.closed as loan_closed'.format(Loan.__table__),
                    '{}.complete as debt_complete'.format(Debt.__table__),
                    '{}.end_date as debt_end_date'.format(Debt.__table__),
                    '{}.name as currency_name'.format(Currency.__table__),
                    '{}.first_name as creator_name'.format(User.__table__),
                ]). \
                serialize()
            purchases_sum = 0.0
            for p in purchases:
                value = p['value']
                if p['currency_name'] != 'rub':
                    value = replace_value_with_rate(
                        value=value,
                        current_currency=p['currency_name'],
                        target_currency='rub')
                purchases_sum += value
            return app_response(data={
                'items': purchases,
                'sum': purchases_sum
            })
    elif request.method == 'POST':
        body = request.json
        value = body.get('value', 0.0)
        value = float(value)
        currency = body.get('currency', None)
        if currency is None:
            currency = Currency.where('name', 'rub').first().id
        name = body.get('name', '')
        description = body.get('description', None)
        complete = body.get('complete', False)
        user = kwargs.get('user_info')
        purchase = Purchase.create(name=name,
                                   value=value,
                                   currency=currency,
                                   complete=complete,
                                   creator=user.id,
                                   description=description)
        purchase = purchase.serialize()
        purchase['currency_name'] = Currency.where('id', currency).first().name
        purchase['loan_closed'] = None
        purchase['debt_complete'] = None
        purchase['debt_end_date'] = None
        if complete:
            replace_balance(value=float(value) * (-1), currency=currency)
        if purchase['currency_name'] != 'rub':
            value = replace_value_with_rate(
                value=float(value),
                current_currency=purchase['currency_name'],
                target_currency='rub')
        return app_response(data={'item': purchase, 'rub_value': value})
    elif request.method == 'PUT':
        body = request.json
        purchase_id = body.get('id', None)
        value = body.get('value', None)
        if value is not None:
            value = float(value)
        currency = body.get('currency', None)
        name = body.get('name', None)
        description = body.get('description', None)
        complete = body.get('complete', False)
        user = kwargs.get('user_info')
        if purchase_id:
            purchase = Purchase.where('id', purchase_id).first()
            if purchase and not purchase.complete:
                balance_value = None
                if value is not None and not complete:
                    purchase.value = value
                elif not value and complete:
                    balance_value = purchase.value
                purchase.currency = currency if currency is not None else purchase.currency
                purchase.name = name if name is not None else purchase.name
                purchase.description = description if description is not None else purchase.description
                purchase.complete = complete
                purchase.doer = user.id if complete else 0
                purchase.save()
                purchase = Purchase. \
                    where('{}.id'.format(Purchase.__table__), purchase_id). \
                    join(table=Loan.__table__,
                         one='{}.id'.format(Loan.__table__),
                         operator='=',
                         two='{}.loan'.format(Purchase.__table__),
                         type='left outer'). \
                    join(table=Debt.__table__,
                         one='{}.id'.format(Debt.__table__),
                         operator='=',
                         two='{}.debt'.format(Purchase.__table__),
                         type='left outer'). \
                    join(table=Currency.__table__,
                         one='{}.id'.format(Currency.__table__),
                         operator='=',
                         two='{}.currency'.format(Purchase.__table__),
                         type='left outer'). \
                    group_by(
                        '{}.id'.format(Purchase.__table__),
                        '{}.closed'.format(Loan.__table__),
                        '{}.complete'.format(Debt.__table__),
                        '{}.end_date'.format(Debt.__table__),
                        '{}.name'.format(Currency.__table__),
                    ). \
                    first([
                        '{}.*'.format(Purchase.__table__),
                        '{}.closed as loan_closed'.format(Loan.__table__),
                        '{}.complete as debt_complete'.format(Debt.__table__),
                        '{}.end_date as debt_end_date'.format(Debt.__table__),
                        '{}.name as currency_name'.format(Currency.__table__),
                    ]). \
                    serialize()
                if balance_value and complete:
                    replace_balance(value=float(value) * (-1),
                                    currency=currency)
            value = purchase['value']
            if purchase['currency_name'] != 'rub':
                value = replace_value_with_rate(
                    value=float(value),
                    current_currency=purchase['currency_name'],
                    target_currency='rub')
            purchases_sum = 0.0
            for p in Purchase. \
                    where('complete', False). \
                    join(table=Currency.__table__,
                         one='{}.id'.format(Currency.__table__),
                         operator='=',
                         two='{}.currency'.format(Purchase.__table__),
                         type='left outer'). \
                    group_by(
                        '{}.id'.format(Purchase.__table__),
                        '{}.name'.format(Currency.__table__),
                    ). \
                    get([
                        '{}.*'.format(Purchase.__table__),
                        '{}.name as currency_name'.format(Currency.__table__),
                    ]):
                value = p.value
                if p.currency_name != 'rub':
                    value = replace_value_with_rate(
                        value=value,
                        current_currency=p.currency_name,
                        target_currency='rub')
                purchases_sum += value
            return app_response(data={
                'item': purchase,
                'rub_value': value,
                'sum': purchases_sum
            })
        return app_response(data={})