Ejemplo n.º 1
0
def create_debt(month, year, type, value, penalty):
    Debt.create(month=month,
                year=year,
                type=type,
                value=value,
                penalty=penalty)
Ejemplo n.º 2
0
def debts(*args, **kwargs):
    if request.method == 'GET':
        debts = Debt. \
            where('complete', False). \
            join(table=Currency.__table__,
                 one='{}.id'.format(Currency.__table__),
                 operator='=',
                 two='{}.currency'.format(Debt.__table__),
                 type='left outer'). \
            group_by(
                '{}.id'.format(Debt.__table__),
                '{}.name'.format(Currency.__table__),
            ). \
            order_by_raw('created_at DESC NULLS LAST'). \
            get([
                '{}.*'.format(Debt.__table__),
                '{}.name as currency_name'.format(Currency.__table__),
            ]). \
            serialize()
        debts_sum = 0.0
        for debt in debts:
            value = debt['value']
            if debt['currency_name'] != 'rub':
                value = replace_value_with_rate(
                    value=value,
                    current_currency=debt['currency_name'],
                    target_currency='rub')
            debts_sum += value
        return app_response(data={'items': debts, 'sum': debts_sum})
    elif request.method == 'POST':
        body = request.json
        name = body.get('name', '')
        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
        end_date = body.get('end_date', None)
        if end_date is not None and end_date == '':
            end_date = None
        description = body.get('description', None)
        user = kwargs.get('user_info')
        debt = Debt.create(creator=user.id,
                           value=value,
                           currency=currency,
                           name=name,
                           end_date=end_date)
        purchase = ComingIns.create(name=name,
                                    value=value,
                                    currency=currency,
                                    creator=user.id,
                                    description=description,
                                    debt=debt.id)
        purchase = purchase.serialize()
        purchase['currency_name'] = Currency.where('id', currency).first().name
        purchase['debt_complete'] = False
        purchase['end_date'] = debt.end_date
        replace_balance(value=float(value), 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
        debt_id = body.get('id', None)
        name = body.get('name', None)
        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
        complete = body.get('complete', False)
        end_date = body.get('end_date', None)
        if end_date is not None and end_date == '':
            end_date = None
        description = body.get('description', None)
        user = kwargs.get('user_info')
        debt = Debt.where('id', debt_id).first()
        if debt and not debt.complete:
            debt_currency = Currency.where('id', debt.currency).first()
            if currency != debt_currency.id:
                body_currency = Currency.where('id', currency).first()
                value = replace_value_with_rate(
                    value=float(value),
                    current_currency=body_currency.name,
                    target_currency=debt_currency.name)
            debt_pay_value = debt.value - value
            if value > 0.0:
                DebtPayment.create(user_id=user.id, value=debt_pay_value)
                debt.value = value
            if end_date is not None:
                if end_date == '':
                    debt.end_date = None
                else:
                    debt.end_date = end_date
            debt.name = name if name is not None else debt.name
            debt.complete = complete
            debt.save()
            if debt_pay_value > 0.0:
                purchase = Purchase.create(name=debt.name,
                                           value=abs(debt_pay_value),
                                           currency=debt.currency,
                                           complete=True,
                                           creator=user.id,
                                           description=description,
                                           debt=debt.id)
            elif debt_pay_value < 0.0:
                purchase = ComingIns.create(name=debt.name,
                                            value=abs(debt_pay_value),
                                            currency=debt.currency,
                                            creator=user.id,
                                            description=description,
                                            debt=debt.id)
            else:
                purchase = Debt.where('id', debt_id).first()
            purchase = purchase.serialize()
            purchase['currency_name'] = Currency.where('id',
                                                       currency).first().name
            purchase['debt_complete'] = complete
            purchase['debt_end_date'] = debt.end_date
            if debt_pay_value > 0.0:
                replace_balance(value=float(debt_pay_value) * (-1),
                                currency=currency)
            elif debt_pay_value < 0.0:
                replace_balance(value=abs(float(debt_pay_value)),
                                currency=currency)
            if purchase['currency_name'] != 'rub':
                debt_pay_value = replace_value_with_rate(
                    value=float(debt_pay_value),
                    current_currency=purchase['currency_name'],
                    target_currency='rub')
            return app_response(data={
                'item': purchase,
                'rub_value': debt_pay_value
            })
        return {}