Example #1
0
    def add_report_overview(
        self,
        report_id: int,
        last_processed_timestamp: Timestamp,
        processed_actions: int,
        total_actions: int,
        pnls: PnlTotals,
    ) -> None:
        """Inserts the report overview data

        May raise:
        - InputError if the given report id does not exist
        """
        cursor = self.db.conn_transient.cursor()
        cursor.execute(
            'UPDATE pnl_reports SET last_processed_timestamp=?,'
            ' processed_actions=?, total_actions=? WHERE identifier=?',
            (last_processed_timestamp, processed_actions, total_actions,
             report_id),
        )
        if cursor.rowcount != 1:
            raise InputError(
                f'Could not insert overview for {report_id}. '
                f'Report id could not be found in the DB', )

        tuples = []
        for event_type, entry in pnls.items():
            tuples.append(
                (report_id, event_type.serialize(), str(entry.taxable),
                 str(entry.free)))  # noqa: E501
        cursor.executemany(
            'INSERT OR IGNORE INTO pnl_report_totals(report_id, name, taxable_value, free_value) VALUES(?, ?, ?, ?)',  # noqa: E501
            tuples,
        )
        self.db.conn_transient.commit()
Example #2
0
def assert_pnl_totals_close(expected: PnlTotals, got: PnlTotals) -> None:
    # ignore prefork acquisitions for these tests
    got.pop(AccountingEventType.PREFORK_ACQUISITION)

    assert len(expected) == len(got)
    for event_type, expected_pnl in expected.items():
        assert expected_pnl.free.is_close(got[event_type].free)
        assert expected_pnl.taxable.is_close(got[event_type].taxable)
Example #3
0
    def _maybe_add_summary(self, events: List[Dict[str, Any]],
                           pnls: PnlTotals) -> None:
        """Depending on given settings, adds a few summary lines at the end of
        the all events PnL report"""
        if self.settings.pnl_csv_have_summary is False:
            return

        length = len(events) + 1
        template: Dict[str, Any] = {
            'type': '',
            'notes': '',
            'location': '',
            'timestamp': '',
            'asset': '',
            'free_amount': '',
            'taxable_amount': '',
            'price': '',
            'pnl_taxable': '',
            'cost_basis_taxable': '',
            'pnl_free': '',
            'cost_basis_free': '',
        }
        events.append(template)  # separate with 2 new lines
        events.append(template)

        entry = template.copy()
        entry['taxable_amount'] = 'TAXABLE'
        entry['price'] = 'FREE'
        events.append(entry)

        start_sums_index = length + 4
        sums = 0
        for name, value in pnls.items():
            if value.taxable == ZERO and value.free == ZERO:
                continue
            sums += 1
            entry = template.copy()
            entry['free_amount'] = f'{str(name)} total'
            entry['taxable_amount'] = self._add_sumif_formula(
                check_range=f'A2:A{length}',
                condition=f'"{str(name)}"',
                sum_range=f'I2:I{length}',
                actual_value=value.taxable,
            )
            entry['price'] = self._add_sumif_formula(
                check_range=f'A2:A{length}',
                condition=f'"{str(name)}"',
                sum_range=f'J2:J{length}',
                actual_value=value.free,
            )
            events.append(entry)

        entry = template.copy()
        entry['free_amount'] = 'TOTAL'
        if sums != 0:
            entry[
                'taxable_amount'] = f'=SUM(G{start_sums_index}:G{start_sums_index+sums-1})'
            entry[
                'price'] = f'=SUM(H{start_sums_index}:H{start_sums_index+sums-1})'
        else:
            entry['taxable_amount'] = entry['price'] = 0
        events.append(entry)

        events.append(template)  # separate with 2 new lines
        events.append(template)

        version_result = get_current_version(check_for_updates=False)
        entry = template.copy()
        entry['free_amount'] = 'rotki version'
        entry['taxable_amount'] = version_result.our_version
        events.append(entry)

        for setting in ACCOUNTING_SETTINGS:
            entry = template.copy()
            entry['free_amount'] = setting
            entry['taxable_amount'] = str(getattr(self.settings, setting))
            events.append(entry)