コード例 #1
0
def backlogs_put(**kwargs):
    """
    actually insert transaction
    """
    obj = json.loads(request.data.decode('utf-8', 'strict'))
    origin_time = datetime.datetime.strptime(
            obj['origin_time'], '%Y-%m-%d').date()
    operation_time = datetime.datetime.strptime(obj['time'], '%Y-%m-%d').date()

    transaction = Transaction(
        time=operation_time,
        account_id=int(obj['account.id']),
        sum=strip_numbers(obj['sum']),
        income_id=obj['income.id'],
        comment=obj['comment'])
    DB.add(transaction)
    DB.flush()
    if origin_time != operation_time:
        # payment before
        DB.add(
            Payforward(
                income_id=int(obj['income.id']),
                income_date=origin_time,
                payment_date=operation_time,
                transaction_id=transaction.record_id
            )
        )
    DB.commit()
    return transaction
コード例 #2
0
def transaction_put(**kwargs):  # pylint: disable=C0111,W0613
    obj = json.loads(request.data.decode('utf-8', 'strict'))
    print(obj)
    # try:
    i = Transaction(
        time=datetime.datetime.strptime(obj['time'], '%Y-%m-%d').date(),
        account_id=int(obj['account.id']),
        user_id=session['user'][0],
        summ=decimal.Decimal(strip_numbers(obj['summ'])),
        transfer=int(obj['transfer']) if
        (obj['transfer'] != '' and int(obj['transfer']) > 0) else None,
        income_id=int(obj['income.id']) if
        (obj['income.id'] != '' and int(obj['income.id']) > 0) else None,
        comments=obj['comments'])
    DB.add(i)
    DB.flush()
    if 'new_account.id' in obj:
        transfer = Transaction(
            time=datetime.datetime.strptime(obj['time'], '%Y-%m-%d').date(),
            account_id=int(obj['new_account.id']),
            user_id=session['user'][0],
            summ=decimal.Decimal(strip_numbers(obj['new_sum'])),
            transfer=int(obj['transfer'])
            if int(obj['transfer']) > 0 else None,
            income_id=int(obj['income.id'])
            if int(obj['income.id']) > 0 else None,
            comments=obj['comments'])
        DB.add(transfer)
        DB.flush()
        i.transfer = transfer.record_id
        transfer.transfer = i.record_id
    DB.commit()
    # except:
    #    abort(400)
    return i
コード例 #3
0
def transactions_post(**kwargs):
    obj = json.loads(request.data.decode('utf-8', 'strict'))
    # try:
    i = Transaction(
        time=datetime.datetime.strptime(obj['time'], '%Y-%m-%d').date(),
        account_id=int(obj['account.id']),
        sum=decimal.Decimal(strip_numbers(obj['sum'])),
        transfer=int(obj['transfer']) if int(obj['transfer']) > 0 else None,
        income_id=int(obj['income.id']) if int(obj['income.id']) > 0 else None,
        comment=obj['comment']
    )
    DB.add(i)
    DB.flush()
    if 'new_account.id' in obj:
        transfer = Transaction(
          time=datetime.datetime.strptime(obj['time'], '%Y-%m-%d').date(),
          account_id=int(obj['new_account.id']),
          sum=decimal.Decimal(strip_numbers(obj['new_sum'])),
          transfer=int(obj['transfer']) if int(obj['transfer']) > 0 else None,
          comment=obj['comment'],
          income_id=int(obj['income.id']) if int(obj['income.id']) > 0 else None
        )
        DB.add(transfer)
        DB.flush()
        i.transfer = transfer.record_id
        transfer.transfer = i.record_id
    DB.commit()
    # except:
    #    abort(400)
    return i
コード例 #4
0
def backlogs_delete(**kwargs):
    # just create transaction with sum zero
    obj = json.loads(request.data.decode('utf-8', 'strict'))
    t = Transaction(
        time=datetime.datetime.strptime(obj['origin_time'], '%Y-%m-%d').date(),
        account_id=0,
        sum=0,
        income_id=obj['income.id'],
        comment='cancelled')
    DB.add(t)
    DB.flush()
    DB.commit()
    return t
コード例 #5
0
def accounts_put(**kwargs):
    a = DB.query(Account).get(kwargs['id'])
    obj = json.loads(request.data.decode('utf-8', 'strict'))
    a.title = obj['title']
    a.show = obj['show']
    a.currency_id = obj['currency.id']
    delta_sum = decimal.Decimal(strip_numbers(obj['sum'])) - a.sum()
    if delta_sum != 0:
        t = Transaction(time=datetime.date.today(), sum=delta_sum,
                        account_id=kwargs['id'], comment='fix account summ')
        DB.add(t)
    DB.commit()
    return {'updated': DB.query(Account).get(kwargs['id']), "previous": a}
コード例 #6
0
def accounts_post(**kwargs):
    """ add new account and set first transaction with rests of money """
    obj = json.loads(request.data.decode('utf-8', 'strict'))
    new_account = Account(
            title=obj['title'],
            currency_id=int(obj['currency.id']))
    DB.add(new_account)
    DB.flush()
    if float(strip_numbers(obj['sum'])) > 0:
        DB.add(Transaction(account_id=new_account.record_id,
                           show=obj['show'],
                           comment='initial summ',
                           time=datetime.date.today(),
                           sum=strip_numbers(obj['sum'])))
    DB.commit()
    return new_account
コード例 #7
0
 def fix_balance(self,
                 n_balance,
                 n_date=datetime.now().replace(hour=23,
                                               minute=59,
                                               second=59,
                                               microsecond=999999)):  # pylint: disable=C0111
     from .transactions import Transaction  # pylint: disable=cyclic-import
     delta = n_balance - self.balance(n_date)
     if delta == 0:
         return 0
     transaction = Transaction(time=n_date,
                               summ=delta,
                               account_id=self.record_id,
                               user_id=self.user_id,
                               comment='fix account summ')
     DB.add(transaction)
     return transaction.record_id
コード例 #8
0
def usercurrency_put(**kwargs):
    """ update user currencies

    Arguments:
        **kwargs id -- currency id

    Returns:
        json -- result code
    """

    obj = json.loads(request.data.decode('utf-8', 'strict'))
    updated = 0

    if 'default' in obj and obj['default']:
        # clean previous default
        updated = DB.query(UserCurrencies).filter(
            and_(
                UserCurrencies.user_id == session['user'][0],
                UserCurrencies.currency_id != kwargs['id'])
            ).update(
                {UserCurrencies.default: False},
                synchronize_session='evaluate')
        updated = DB.query(UserCurrencies).filter(
            and_(
                UserCurrencies.user_id == session['user'][0],
                UserCurrencies.currency_id == kwargs['id']
                )
            ).update(
                {UserCurrencies.default: True},
                synchronize_session='evaluate')
        if updated == 0:
            # if not updated nothing - inser new record
            DB.add(UserCurrencies(
                user_id=session['user'][0],
                currency_id=kwargs['id'],
                default=True
                ))
    else:
        DB.add(UserCurrencies(
            user_id=session['user'][0],
            currency_id=kwargs['id']
            ))
    DB.commit()
    return {'result': 'Ok'}
コード例 #9
0
def incomes_post(**kwargs):
    obj = json.loads(request.data.decode('utf-8', 'strict'))
    # try:
    i = Income(
      title=obj['title'],
      currency_id=int(obj['currency.id']),
      sum=decimal.Decimal(strip_numbers(obj['sum'])),
      start_date=datetime.datetime.strptime(
          obj['start_date'], '%Y-%m-%d').date(),
      end_date=(None if obj['end_date'] == '' else datetime.datetime.strptime(
          obj['end_date'], '%Y-%m-%d').date()),
      period_id=int(obj['period.id'])
    )
    DB.add(i)
    DB.flush()
    DB.commit()
    # except:
    #    abort(400)
    return i