def add_transaction(request, mid): if request.method == "POST": post_data = request.POST form = AddTransaction(post_data) month = Month.objects.get(pk=mid) if form.is_valid(): data = form.cleaned_data score_source = Score.objects.get( pk=post_data.get('score_source', '')) data['score_source'] = score_source data['month'] = month score_goal_id = post_data.get('score_goal', '') if score_goal_id: score_goal = Score.objects.get(pk=score_goal_id) data['score_goal'] = str(score_goal.id) data['score_goal_name'] = score_goal.account.name transaction = Transaction(**data) transaction.save() score_source.remainder -= data['amount'] score_source.save() score_goal.remainder += data['amount'] score_goal.save() messages.success(request, u"Переказ успішно доданий!") elif post_data.get('planned_expense', ''): planned_expense = PlannedExpense.objects.get( pk=post_data.get('planned_expense', '')) data['planned_expense'] = planned_expense transaction = Transaction(**data) transaction.save() change_accounts(transaction, planned_expense, month, score_source, True) messages.success(request, u"Витрата успішно додана!") return HttpResponseRedirect(reverse("show_balance", kwargs={'mid': mid}))
def import_csv(filename, account): ''' ''' # CSVs come in many formats, find a matching profile for profile in CsvImportProfile.objects.all().order_by('order'): if profile.match(filename): logging.info("Found matching CSV profile: %s", profile) break else: raise ValueError, "No profiles matched the file" with open(filename, 'rb') as csv_file: transactions = parse_csv(csv_file, profile.get_fieldnames(), dayfirst=profile.date_day_first, skip_rows=profile.data_start_row) logging.info("%d CSV transactions found in file %s", len(transactions), filename) for t in transactions: # split payee string into parts try: trans_type, payee = match_transaction_type(t['payee']) except ValueError: logging.warning("Transaction type cannot be determined: %s", t['payee']) trans_type = None payee = t['payee'] # create and save Transactions in DB new_transaction = Transaction( account = account, name = payee, #TODO: populate or remove as we don't need a name?! date = t['date'], transaction_type = trans_type, payee = payee, ##paid_on = paid_on_dt, ##import_string = t['import_string'], # DictReader doesn't give us the original line ) new_transaction.save() t_part = TransactionPart( transaction = new_transaction, amount = '%.2f' % t['amount'], category = match_category(payee), ) t_part.save() logging.debug("Imported %s - %s", new_transaction, t_part) return len(transactions)
def __create_transactions(): user_k = User.objects.get(username="******") user_s = User.objects.get(username="******") transaction1 = Transaction(owner=user_k, amount="300", currency="D") transaction1.save() transaction2 = Transaction(owner=user_k, amount="30", currency="E") transaction2.save() transaction3 = Transaction(owner=user_k, amount="30000", currency="R") transaction3.save() transaction4 = Transaction(owner=user_s, amount="40000", currency="R") transaction4.save()
def create_transaction(self, account, date): num_transactions = random.randint(3, 5) for i in range(num_transactions): dt = self.get_datetime(date) t = Transaction(account=account) t.guid = uuid.uuid4().hex t.uid = uuid.uuid4().hex t.date = date t.created_at = timezone.make_aware(dt) t.updated_at = timezone.make_aware(dt) t.transacted_at = timezone.make_aware(dt) t.posted_at = timezone.make_aware(dt) t.amount = self.generate_amount() t.is_expense = True t.category = self.get_category() t.description = self.get_description() t.save() dt_str = dt.strftime('%Y-%m-%d %H:%M:%S') with freeze_time(dt_str): self.make_transfers(dt)
def __create_transactions(): user = User.objects.get(username="******") transaction1 = Transaction(owner=user, amount="300", currency="D") transaction1.save() transaction2 = Transaction(owner=user, amount="30", currency="E") transaction2.save() transaction3 = Transaction(owner=user, amount="30000", currency="R") transaction3.save()
def saving_transfer(request, sid, action): saving = Saving.objects.get(pk=sid) if request.method == "POST": amount = request.POST.get('amount', '') transaction_data = {} if action == 'add': saving.amount += int(amount) transaction_data['amount'] = int(amount) transaction_data[ 'detail'] = u"Зарахування коштів на збереження {} по рахунку {}".format( saving.saving_total.title, saving.account.name) elif action == 'remove': saving.amount -= int(amount) transaction_data['amount'] = -int(amount) transaction_data[ 'detail'] = u"Зняття коштів зі збереження {} по рахунку {}".format( saving.saving_total.title, saving.account.name) saving.save() transaction_data['model'] = saving.__class__.__name__ transaction_data['model_id'] = saving.id transaction = Transaction(**transaction_data) transaction.save() if action == 'add': messages.success( request, u"Кошти на {} на рахунок {} успішно зараховано".format( saving.saving_total.title, saving.account.name)) if action == 'remove': messages.error( request, u"Кошти зі збереження {} на рахунку {} успішно знято".format( saving.saving_total.title, saving.account.name)) return HttpResponseRedirect(reverse("savings_list")) elif request.method == "GET": return render(request, 'finance/saving_change.html', { 'action': action, 'saving': saving })
def import_qif(filename, account): """ """ with open(filename, 'rb') as qif_file: transactions = parse_qif(qif_file) logging.info("%d QIF transactions found in file %s", len(transactions), filename) for t in transactions: try: trans_type, payee = match_transaction_type(t['payee']) except ValueError: logging.warning("Transaction type cannot be determined: %s", t['payee']) trans_type = None payee = t['payee'] # create and save Transactions in DB new_transaction = Transaction( account = account, name = payee, #TODO: populate or remove as we don't need a name?! date = t['date'], transaction_type = trans_type, payee = payee, ##paid_on = paid_on_dt, import_string = t['import_string'], ) new_transaction.save() if t.get('amount_in_split'): part_list = [] for n, amount in t['amount_in_split']: # create and save Transactions in DB if t.get('category_in_split'): # assumed category_in_split has same number of items as amount split_category = _get_category(t['category_in_split'][n]) else: split_category = match_category(payee) t_part = TransactionPart( transaction = new_transaction, amount = '%.2f' % amount, category = split_category, description = '', #TODO: iterate over t['memo'] field if available ) t_part.save() #TODO: Required again after? part_list.append(t_part) logging.debug("Imported %s - %s", new_transaction, part_list) else: # create a single transactionamount if t.get('category'): # Category from QIF takes priority if available category = _get_category(category_name) else: category = match_category(payee) t_part = TransactionPart( transaction = new_transaction, amount = '%.2f' % t['amount'], description = t.get('memo', ''), category = category, ) t_part.save() #TODO: Required again after? logging.debug("Imported %s - %s", new_transaction, t_part) return len(transactions)
def __create_transactions(): user_k = User.objects.get(username="******") transaction1 = Transaction(owner=user_k, amount="300", currency="D") transaction1.save()