envelope-cli 0.2.6

Terminal-based zero-based budgeting application
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//! Transaction model
//!
//! Represents financial transactions with support for splits, transfers,
//! and various statuses (pending, cleared, reconciled).

use chrono::{DateTime, NaiveDate, Utc};
use serde::{Deserialize, Serialize};
use std::fmt;

use super::ids::{AccountId, CategoryId, PayeeId, TransactionId};
use super::money::Money;

/// Status of a transaction
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum TransactionStatus {
    /// Transaction has not yet cleared the bank
    #[default]
    Pending,
    /// Transaction has cleared the bank
    Cleared,
    /// Transaction has been reconciled and is locked
    Reconciled,
}

impl TransactionStatus {
    /// Check if this transaction is locked (cannot be edited without unlocking)
    pub fn is_locked(&self) -> bool {
        matches!(self, Self::Reconciled)
    }
}

impl fmt::Display for TransactionStatus {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Pending => write!(f, "Pending"),
            Self::Cleared => write!(f, "Cleared"),
            Self::Reconciled => write!(f, "Reconciled"),
        }
    }
}

/// A split portion of a transaction assigned to a specific category
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Split {
    /// The category for this split portion
    pub category_id: CategoryId,

    /// The amount for this split (same sign as parent transaction)
    pub amount: Money,

    /// Optional memo for this split
    #[serde(default)]
    pub memo: String,
}

impl Split {
    /// Create a new split
    pub fn new(category_id: CategoryId, amount: Money) -> Self {
        Self {
            category_id,
            amount,
            memo: String::new(),
        }
    }

    /// Create a new split with a memo
    pub fn with_memo(category_id: CategoryId, amount: Money, memo: impl Into<String>) -> Self {
        Self {
            category_id,
            amount,
            memo: memo.into(),
        }
    }
}

/// A financial transaction
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Transaction {
    /// Unique identifier
    pub id: TransactionId,

    /// The account this transaction belongs to
    pub account_id: AccountId,

    /// Transaction date
    pub date: NaiveDate,

    /// Amount (positive for inflow, negative for outflow)
    pub amount: Money,

    /// Payee ID (optional)
    pub payee_id: Option<PayeeId>,

    /// Payee name (stored for display, even if payee_id is set)
    #[serde(default)]
    pub payee_name: String,

    /// Category ID (None if this is a split transaction or transfer)
    pub category_id: Option<CategoryId>,

    /// Split transactions - if non-empty, category_id should be None
    #[serde(default)]
    pub splits: Vec<Split>,

    /// Memo/notes
    #[serde(default)]
    pub memo: String,

    /// Transaction status
    #[serde(default)]
    pub status: TransactionStatus,

    /// If this is a transfer, the ID of the linked transaction in the other account
    pub transfer_transaction_id: Option<TransactionId>,

    /// Import ID for duplicate detection during CSV import
    pub import_id: Option<String>,

    /// When the transaction was created
    pub created_at: DateTime<Utc>,

    /// When the transaction was last modified
    pub updated_at: DateTime<Utc>,
}

impl Transaction {
    /// Create a new transaction
    pub fn new(account_id: AccountId, date: NaiveDate, amount: Money) -> Self {
        let now = Utc::now();
        Self {
            id: TransactionId::new(),
            account_id,
            date,
            amount,
            payee_id: None,
            payee_name: String::new(),
            category_id: None,
            splits: Vec::new(),
            memo: String::new(),
            status: TransactionStatus::Pending,
            transfer_transaction_id: None,
            import_id: None,
            created_at: now,
            updated_at: now,
        }
    }

    /// Create a transaction with all common fields
    pub fn with_details(
        account_id: AccountId,
        date: NaiveDate,
        amount: Money,
        payee_name: impl Into<String>,
        category_id: Option<CategoryId>,
        memo: impl Into<String>,
    ) -> Self {
        let mut txn = Self::new(account_id, date, amount);
        txn.payee_name = payee_name.into();
        txn.category_id = category_id;
        txn.memo = memo.into();
        txn
    }

    /// Check if this is a split transaction
    pub fn is_split(&self) -> bool {
        !self.splits.is_empty()
    }

    /// Check if this is a transfer
    pub fn is_transfer(&self) -> bool {
        self.transfer_transaction_id.is_some()
    }

    /// Check if this is an inflow (positive amount)
    pub fn is_inflow(&self) -> bool {
        self.amount.is_positive()
    }

    /// Check if this is an outflow (negative amount)
    pub fn is_outflow(&self) -> bool {
        self.amount.is_negative()
    }

    /// Check if this transaction is locked
    pub fn is_locked(&self) -> bool {
        self.status.is_locked()
    }

    /// Set the status
    pub fn set_status(&mut self, status: TransactionStatus) {
        self.status = status;
        self.updated_at = Utc::now();
    }

    /// Clear the transaction (mark as cleared)
    pub fn clear(&mut self) {
        self.set_status(TransactionStatus::Cleared);
    }

    /// Mark as reconciled
    pub fn reconcile(&mut self) {
        self.set_status(TransactionStatus::Reconciled);
    }

    /// Add a split
    pub fn add_split(&mut self, split: Split) {
        self.splits.push(split);
        // When splits are added, category_id should be cleared
        self.category_id = None;
        self.updated_at = Utc::now();
    }

    /// Clear all splits and set a single category
    pub fn set_category(&mut self, category_id: CategoryId) {
        self.splits.clear();
        self.category_id = Some(category_id);
        self.updated_at = Utc::now();
    }

    /// Get the total of all splits (should equal transaction amount)
    pub fn splits_total(&self) -> Money {
        self.splits.iter().map(|s| s.amount).sum()
    }

    /// Validate the transaction
    pub fn validate(&self) -> Result<(), TransactionValidationError> {
        // If split, splits total must equal transaction amount
        if self.is_split() {
            let splits_total = self.splits_total();
            if splits_total != self.amount {
                return Err(TransactionValidationError::SplitsMismatch {
                    transaction_amount: self.amount,
                    splits_total,
                });
            }
        }

        // Can't have both category_id and splits
        if self.category_id.is_some() && !self.splits.is_empty() {
            return Err(TransactionValidationError::CategoryAndSplits);
        }

        // Transfers shouldn't have categories
        if self.is_transfer() && (self.category_id.is_some() || !self.splits.is_empty()) {
            return Err(TransactionValidationError::TransferWithCategory);
        }

        Ok(())
    }

    /// Generate an import ID for duplicate detection
    pub fn generate_import_id(&self) -> String {
        use std::hash::{Hash, Hasher};
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        self.date.hash(&mut hasher);
        self.amount.cents().hash(&mut hasher);
        self.payee_name.hash(&mut hasher);
        format!("imp-{:016x}", hasher.finish())
    }
}

impl fmt::Display for Transaction {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{} {} {}",
            self.date.format("%Y-%m-%d"),
            self.payee_name,
            self.amount
        )
    }
}

/// Validation errors for transactions
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TransactionValidationError {
    SplitsMismatch {
        transaction_amount: Money,
        splits_total: Money,
    },
    CategoryAndSplits,
    TransferWithCategory,
}

impl fmt::Display for TransactionValidationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::SplitsMismatch {
                transaction_amount,
                splits_total,
            } => write!(
                f,
                "Split totals ({}) do not match transaction amount ({})",
                splits_total, transaction_amount
            ),
            Self::CategoryAndSplits => {
                write!(f, "Transaction cannot have both a category and splits")
            }
            Self::TransferWithCategory => {
                write!(f, "Transfer transactions should not have a category")
            }
        }
    }
}

impl std::error::Error for TransactionValidationError {}

#[cfg(test)]
mod tests {
    use super::*;

    fn test_account_id() -> AccountId {
        AccountId::new()
    }

    fn test_category_id() -> CategoryId {
        CategoryId::new()
    }

    #[test]
    fn test_new_transaction() {
        let account_id = test_account_id();
        let date = NaiveDate::from_ymd_opt(2025, 1, 15).unwrap();
        let amount = Money::from_cents(-5000);

        let txn = Transaction::new(account_id, date, amount);
        assert_eq!(txn.account_id, account_id);
        assert_eq!(txn.date, date);
        assert_eq!(txn.amount, amount);
        assert_eq!(txn.status, TransactionStatus::Pending);
    }

    #[test]
    fn test_inflow_outflow() {
        let account_id = test_account_id();
        let date = NaiveDate::from_ymd_opt(2025, 1, 15).unwrap();

        let inflow = Transaction::new(account_id, date, Money::from_cents(1000));
        assert!(inflow.is_inflow());
        assert!(!inflow.is_outflow());

        let outflow = Transaction::new(account_id, date, Money::from_cents(-1000));
        assert!(!outflow.is_inflow());
        assert!(outflow.is_outflow());
    }

    #[test]
    fn test_status_transitions() {
        let account_id = test_account_id();
        let date = NaiveDate::from_ymd_opt(2025, 1, 15).unwrap();
        let mut txn = Transaction::new(account_id, date, Money::from_cents(-1000));

        assert!(!txn.is_locked());

        txn.clear();
        assert_eq!(txn.status, TransactionStatus::Cleared);
        assert!(!txn.is_locked());

        txn.reconcile();
        assert_eq!(txn.status, TransactionStatus::Reconciled);
        assert!(txn.is_locked());
    }

    #[test]
    fn test_split_transaction() {
        let account_id = test_account_id();
        let date = NaiveDate::from_ymd_opt(2025, 1, 15).unwrap();
        let mut txn = Transaction::new(account_id, date, Money::from_cents(-10000));

        let cat1 = test_category_id();
        let cat2 = test_category_id();

        txn.add_split(Split::new(cat1, Money::from_cents(-6000)));
        txn.add_split(Split::new(cat2, Money::from_cents(-4000)));

        assert!(txn.is_split());
        assert_eq!(txn.splits_total(), Money::from_cents(-10000));
        assert!(txn.validate().is_ok());
    }

    #[test]
    fn test_split_validation_mismatch() {
        let account_id = test_account_id();
        let date = NaiveDate::from_ymd_opt(2025, 1, 15).unwrap();
        let mut txn = Transaction::new(account_id, date, Money::from_cents(-10000));

        let cat1 = test_category_id();
        txn.add_split(Split::new(cat1, Money::from_cents(-5000)));

        assert!(matches!(
            txn.validate(),
            Err(TransactionValidationError::SplitsMismatch { .. })
        ));
    }

    #[test]
    fn test_category_and_splits_validation() {
        let account_id = test_account_id();
        let date = NaiveDate::from_ymd_opt(2025, 1, 15).unwrap();
        let mut txn = Transaction::new(account_id, date, Money::from_cents(-10000));

        let cat1 = test_category_id();
        let cat2 = test_category_id();

        txn.category_id = Some(cat1);
        txn.splits.push(Split::new(cat2, Money::from_cents(-10000)));

        assert_eq!(
            txn.validate(),
            Err(TransactionValidationError::CategoryAndSplits)
        );
    }

    #[test]
    fn test_import_id_generation() {
        let account_id = test_account_id();
        let date = NaiveDate::from_ymd_opt(2025, 1, 15).unwrap();
        let mut txn = Transaction::new(account_id, date, Money::from_cents(-5000));
        txn.payee_name = "Test Store".to_string();

        let import_id = txn.generate_import_id();
        assert!(import_id.starts_with("imp-"));

        // Same transaction should generate same import ID
        let import_id2 = txn.generate_import_id();
        assert_eq!(import_id, import_id2);
    }

    #[test]
    fn test_serialization() {
        let account_id = test_account_id();
        let date = NaiveDate::from_ymd_opt(2025, 1, 15).unwrap();
        let txn = Transaction::with_details(
            account_id,
            date,
            Money::from_cents(-5000),
            "Test Store",
            Some(test_category_id()),
            "Test memo",
        );

        let json = serde_json::to_string(&txn).unwrap();
        let deserialized: Transaction = serde_json::from_str(&json).unwrap();
        assert_eq!(txn.id, deserialized.id);
        assert_eq!(txn.amount, deserialized.amount);
        assert_eq!(txn.payee_name, deserialized.payee_name);
    }

    #[test]
    fn test_display() {
        let account_id = test_account_id();
        let date = NaiveDate::from_ymd_opt(2025, 1, 15).unwrap();
        let mut txn = Transaction::new(account_id, date, Money::from_cents(-5000));
        txn.payee_name = "Test Store".to_string();

        assert_eq!(format!("{}", txn), "2025-01-15 Test Store -$50.00");
    }
}