def preprocess_statement_CBA(data): """ returns list of **kwargs """ # date value line_dump balance def process_line_dump(line_dump): splits = [ 'Card xx', 'Value Date: ', 'BPAY ', ] for split in splits: try: description, additional = line_dump.split(split) return description, "{}{}".format(split, additional) except ValueError: return line_dump, '' raw_lines = data.split('\r\n') processed_lines = [] for line in raw_lines: if line.split('\t')[0] == 'date': continue kwargs = {} date, value, line_dump, balance = line.split('\t') kwargs['date'] = make_date(date).date() kwargs['value'] = Decimal(value) kwargs['line_dump'] = line_dump kwargs['description'], kwargs['additional'] = process_line_dump( line_dump) kwargs['balance'] = Decimal(balance) processed_lines.append(kwargs) return processed_lines
def process_kwargs(self, kwargs): """ Minimum keys assumed included: 'user', 'date', 'lines' 1. Keys added (if not already defined): 'cls', 'source' add `source` using ledgers.utils.get_source(`Model`) based upon `Model` provided in object settings 2. Keys checked: 'relation', dates, decimals, required fields IS_DATE using dateparser.parse IS_DECIMAL using ledgers.utils.make_decimal() IS_RELATION to normalise relation field name 3. Create dicts: (obj_kwargs, trans_kwargs) Check all required fields are represented (or explode) append `row_dict` set to `list_kwargs` """ # Generate list of codes to check against. # Cheaper than checking db for every account. ACCOUNT_CODE_LIST = Account.get_account_code_list() process_kwargs = {k.lower(): v for k, v in kwargs.items()} # If `cls` not manually described in kwargs. process_kwargs['source'] = utils.get_source(self) for key in kwargs: if key in settings.FIELD_IS_DATE: process_kwargs[key] = utils.make_date(kwargs[key]) if key in settings.FIELD_IS_DECIMAL: process_kwargs[key] = utils.make_decimal(kwargs[key]) if key in settings.FIELD_IS_RELATION: # Relation names are not always consistently used. # eg. Creditor, Relation if kwargs[key] is None: # Is likely to have emtpy relation column heading. # Remove empty relation, so doesn't blow up save. process_kwargs.pop(key) else: process_kwargs['relation'] = self.get_relation(kwargs[key]) if key in ACCOUNT_CODE_LIST: process_kwargs.setdefault('accounts', []).append( (key, kwargs[key])) self.check_required(process_kwargs) return process_kwargs
def test_journalentry_save_transaction_account_code_passes(self): new_journalentry = JournalEntry() new_journalentry.save_transaction(self.kwargs) test_kwargs = { 'transaction__date': utils.make_date('5-May-2020'), 'transaction__user': self.user, 'transaction__value': 1.00, 'transaction__source': utils.get_source(JournalEntry) } test_object = JournalEntry.objects.get(**test_kwargs) self.assertEqual(new_journalentry, test_object)
def preprocess_statement_NAB(data): """ returns list of **kwargs """ # date value nil nil additional description balance raw_lines = data.split('\r\n') processed_lines = [] for line in raw_lines: kwargs = {} date, value, nil, nil, additional, description, balance = line.split( '\t') kwargs['date'] = make_date(date).date() kwargs['value'] = Decimal(value) kwargs['line_dump'] = "{} {}".format(description, additional) kwargs['description'] = description kwargs['additional'] = additional kwargs['balance'] = Decimal(balance) processed_lines.append(kwargs) return processed_lines
def test_creditorinvoice_save_transaction_lines_passes(self): self.kwargs = { 'date': '5-May-2020', 'user': self.user, 'lines': [(self.a1, 1), (self.a2, 2), (self.c, -3)], } self.kwargs['invoice_number'] = 'abc123' self.kwargs['relation'] = self.creditor self.kwargs['gst_total'] = 0 new_creditorinvoice = CreditorInvoice() new_creditorinvoice.save_transaction(self.kwargs) test_kwargs = { 'transaction__date': utils.make_date('5-May-2020'), 'transaction__user': self.user, 'transaction__value': 3.00, 'transaction__source': utils.get_source(CreditorInvoice) } test_object = CreditorInvoice.objects.get(**test_kwargs) self.assertEqual(new_creditorinvoice, test_object)
def test_make_date_nearly_normal2_passes(self): test_input = "Thursday, 2nd May 2017" test_result = datetime(2017, 5, 2, 0, 0) self.assertEqual(utils.make_date(test_input), test_result)
def test_make_date_nearly_normal_passes(self): test_input = "2017-May-2" test_result = datetime(2017, 5, 2, 0, 0) self.assertEqual(utils.make_date(test_input), test_result)
def test_make_date_not_date_failure(self): test_input = "asdf" # @@ bit inconsistent .. could be better. self.assertEqual(utils.make_date(test_input), None)