Exemple #1
0
def action_process(d: DataDir, config: Configuration, logger: Logger,
                   userid: int):
    if not os.path.exists(d.user_config_path(userid)):
        raise ValueError(f'config file for user {userid} not found')
    user_data = DataDir(config)
    user_data.add_config(d.user_config_path(userid))

    acc: UserAccounts = d.UserAccounts(userid)

    # try to convert xls to xlsx
    for datafile in d.UserXLSDataFiles(userid):
        try:
            import pyexcel as p

            xlsx_file = datafile + 'x'
            logger.info(f'Trying to convert {datafile} to {xlsx_file}')
            if os.path.exists(xlsx_file):
                logger.info(f'IGNORED (Already exists) {xlsx_file}')
            else:
                p.save_book_as(file_name=datafile, dest_file_name=xlsx_file)
                logger.info(f'Converted')
        except ImportError as e:
            logger.error('Error during xls to xlsx conversion',
                         'Error',
                         exc_info=e)

    datafiles: List[str] = d.UserDataFiles(userid)
    if len(datafiles) == 0:
        logger.error('No xlsx files')
    import_tag: str = f'IMP-{datetime.date.today().strftime("%Y-%m-%d")}'
    for datafile in d.UserDataFiles(userid):
        #datafile_parts:str = os.path.splitext(os.path.basename(datafile))
        outputfn = os.path.join(user_data.user_folder_output(userid),
                                os.path.basename(datafile))

        stmt_data = XlsxDriver(filepath=datafile,
                               output_file_path=outputfn,
                               first_row=get_config_int(
                                   user_data.config, 'datafile.first_row'))
        stmt_data.read()

        from execution_context import ExecutionContext

        ec = ExecutionContext(stmt_data, acc, list_files('rules', r'.*\.yaml'),
                              d.UserTaxNumber(userid),
                              d.user_folder_output(userid))
        (total_rows, rows_processed) = ec.execute(import_tag)
        stmt_data.close()
        logger.info(
            f'DONE. Processed {total_rows} rows, prepared {rows_processed} statements with tag {import_tag}'
        )
Exemple #2
0
def action_fetch_statements(d: DataDir, config: Configuration, logger: Logger,
                            userid: int):
    if not os.path.exists(d.user_config_path(userid)):
        raise ValueError(f'config file for user {userid} not found')
    user_data = DataDir(config)
    user_data.add_config(d.user_config_path(userid))
    accounts = user_data.UserAccounts(userid)
    bankApi: PbApi = PbApi(user_data)
    logger.info(
        f'getting statements for period {bankApi.first_date} - {bankApi.last_date}'
    )
    bankApi.fetch_statements()
    num_entries = bankApi.store(
        os.path.join(user_data.user_folder_input(userid),
                     f'{bankApi.period_str}.xlsx'),
        get_config_int(user_data.config, 'datafile.first_row'))
    logger.info(
        f'{num_entries} statements stored in {bankApi.period_str}.xlsx')
Exemple #3
0
def action_balance(d: DataDir, config: Configuration, logger: Logger,
                   userid: int):
    if not os.path.exists(d.user_config_path(userid)):
        raise ValueError(f'config file for user {userid} not found')
    user_data = DataDir(config)
    user_data.add_config(d.user_config_path(userid))
    accounts = user_data.UserAccounts(userid)
    bankApi: PbApi = PbApi(user_data)
    balance = {}
    for b in bankApi.balance():
        amount = Decimal(b['balanceIn'])
        balance[b["acc"]] = {
            'bank_balance': Decimal(b['balanceIn']),
            'currency': b["currency"]
        }

    userAccounts = user_data.GetUserAccounts(userid)
    for a in user_data.GetUserAccounts(userid):
        _d = {'taxer_balance': Decimal(a.balance), 'currency': a.currency}
        num = a.num if a.num else a.title
        if num in balance:
            balance[num].update(_d)
        else:
            balance[num] = _d
    logger.info(
        f'{"Номер счета": <30} {"Банк": >15} {"Taxer": >15} {"Валюта": <3}')
    for acc in balance:
        b = balance[acc]
        bank_balance = b['bank_balance'] if 'bank_balance' in b else Decimal(0)
        taxer_balance = b[
            'taxer_balance'] if 'taxer_balance' in b else Decimal(0)
        currency = b["currency"]
        if bank_balance.is_zero() and taxer_balance.is_zero(): continue
        logger.info(
            f'{acc: <30} {bank_balance: >15.2f} {taxer_balance: >15.2f} {currency: <3}'
        )
Exemple #4
0
            logger.error('No xlsx files')
        import_tag: str = f'IMP-{datetime.date.today().strftime("%Y-%m-%d")}'
        for datafile in d.UserDataFiles(userid):
            #datafile_parts:str = os.path.splitext(os.path.basename(datafile))
            outputfn = os.path.join(user_data.user_folder_output(userid),
                                    os.path.basename(datafile))

            stmt_data = XlsxDriver(filepath=datafile,
                                   output_file_path=outputfn,
                                   first_row=get_config_int(
                                       user_data.config, 'datafile.first_row'))
            stmt_data.read()

            from execution_context import ExecutionContext

            ec = ExecutionContext(stmt_data, d.UserAccounts(userid),
                                  list_files('rules', r'.*\.yaml'),
                                  d.UserTaxNumber(userid),
                                  d.user_folder_output(userid))
            (total_rows, rows_processed) = ec.execute(import_tag)
            stmt_data.close()
            logger.info(
                f'DONE. Processed {total_rows} rows, prepared {rows_processed} statements with tag {import_tag}'
            )

if config['action'].as_str() == 'push':
    for userid in d.Users():
        if not os.path.exists(d.user_config_path(userid)):
            raise ValueError(f'config file for user {userid} not found')
        user_data = DataDir(config)
        user_data.add_config(d.user_config_path(userid))