Skip to content

Commit

Permalink
More tweaks to profit and loss report
Browse files Browse the repository at this point in the history
  • Loading branch information
eseidel committed Mar 2, 2024
1 parent 51440cd commit dbca680
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
53 changes: 29 additions & 24 deletions packages/cli/bin/report_profit_loss.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,30 @@ import 'package:cli/printing.dart';

class ProfitLoss {
ProfitLoss({
required this.start,
required this.end,
required this.sales,
required this.contracts,
required this.cogs,
required this.goods,
required this.fuel,
required this.constructionMaterials,
required this.subsidies,
required this.categorizationPending,
});

final DateTime start;
final DateTime end;

final int sales;
final int contracts;
final int cogs;
final int goods;
final int fuel;
final int constructionMaterials;
final int subsidies;
final int categorizationPending;

int get totalIncome => sales + contracts;
int get totalCostOfGoodsSold => cogs + fuel;
int get totalCostOfGoodsSold => goods + fuel;
int get grossProfit => totalIncome - totalCostOfGoodsSold;
int get totalExpenses =>
constructionMaterials + subsidies + categorizationPending;
Expand Down Expand Up @@ -65,8 +70,9 @@ class ReportBuilder {
goods += transaction.creditsChange;
case MarketTransactionTypeEnum.SELL:
sales += transaction.creditsChange;
case null:
_fail('Unknown market transaction type: null');
}
_fail('Unknown market transaction type: ${transaction.tradeType}');
case AccountingType.fuel:
_expect(transaction.isPurchase, 'Fuel is not a purchase');
fuel += transaction.creditsChange;
Expand Down Expand Up @@ -142,10 +148,12 @@ class ReportBuilder {
}

return ProfitLoss(
start: transactions.first.timestamp,
end: transactions.last.timestamp,
sales: sales,
contracts: contracts,
cogs: goods,
fuel: fuel,
goods: -goods,
fuel: -fuel,
constructionMaterials: constructionMaterials,
subsidies: subsidies,
categorizationPending: categorizationPending,
Expand All @@ -155,31 +163,28 @@ class ReportBuilder {

Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
final rb = ReportBuilder(fs, db);
final profitLoss = await rb.buildProfitLoss();
final report = await rb.buildProfitLoss();

String c(int credits) => creditsString(credits);

logger.info('Transactions: ${rb.transactionCount}');

// For time x-y
logger
..info('PROFIT & LOSS')
..info('')
..info('Transactions: ${rb.transactionCount}')
..info('Between ${report.start} and ${report.end}')
..info('Income')
..info(' Sales: ${c(profitLoss.sales)}')
..info(' Contracts: ${c(profitLoss.contracts)}')
..info('Total Income: ${c(profitLoss.totalIncome)}')
..info(' Sales: ${c(report.sales)}')
..info(' Contracts: ${c(report.contracts)}')
..info('Total Income: ${c(report.totalIncome)}')
..info('Cost of Goods Sold')
..info(' COGS: ${c(profitLoss.cogs)}')
..info(' Fuel: ${c(profitLoss.fuel)}')
..info('Total Cost of Goods Sold: ${c(profitLoss.totalCostOfGoodsSold)}')
..info('Gross Profit: ${c(profitLoss.grossProfit)}')
..info(' Goods: ${c(report.goods)}')
..info(' Fuel: ${c(report.fuel)}')
..info('Total Cost of Goods Sold: ${c(report.totalCostOfGoodsSold)}')
..info('Gross Profit: ${c(report.grossProfit)}')
..info('Expenses')
..info(' Construction Materials: ${c(profitLoss.constructionMaterials)}')
..info(' Subsidies: ${c(profitLoss.subsidies)}')
..info(' Categorization Pending: ${c(profitLoss.categorizationPending)}')
..info('Total Expenses: ${c(profitLoss.totalExpenses)}')
..info('Net Income: ${c(profitLoss.netIncome)}');
// ..info(' Construction Materials: ${c(profitLoss.constructionMaterials)}')
// ..info(' Subsidies: ${c(profitLoss.subsidies)}')
// ..info(' Categorization Pending: ${c(profitLoss.categorizationPending)}')
..info('Total Expenses: ${c(report.totalExpenses)}')
..info('Net Income: ${c(report.netIncome)}');
}

void main(List<String> args) async {
Expand Down
4 changes: 3 additions & 1 deletion packages/db/lib/db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,10 @@ class Database {
}

/// Get all transactions from the database.
/// Currently returns in timestamp order, but that may not always be the case.
Future<Iterable<Transaction>> allTransactions() async {
final result = await connection.execute('SELECT * FROM transaction_');
final result = await connection
.execute('SELECT * FROM transaction_ ORDER BY timestamp');
return result.map((r) => r.toColumnMap()).map(transactionFromColumnMap);
}

Expand Down

0 comments on commit dbca680

Please sign in to comment.