stateset-db 1.22.0

Database implementations for StateSet iCommerce
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
//! PostgreSQL revenue recognition (ASC 606) repository implementation

use super::map_db_error;
use chrono::{DateTime, NaiveDate, Utc};
use rust_decimal::Decimal;
use sqlx::FromRow;
use sqlx::postgres::PgPool;
use stateset_core::{
    CommerceError, CreateRevenueContract, CurrencyCode, PerformanceObligation, RecognitionMethod,
    Result, RevenueContract, RevenueContractFilter, RevenueContractStatus, RevenueEntryStatus,
    RevenueRecognitionRepository, RevenueSchedule, RevenueScheduleEntry, UpdateRevenueContract,
    Validate, generate_revenue_contract_number, generate_revenue_schedule,
};
use uuid::Uuid;

/// PostgreSQL implementation of `RevenueRecognitionRepository`
#[derive(Debug, Clone)]
pub struct PgRevenueRecognitionRepository {
    pool: PgPool,
}

#[derive(FromRow)]
struct RevenueContractRow {
    id: Uuid,
    contract_number: String,
    customer_id: Uuid,
    order_id: Option<Uuid>,
    invoice_id: Option<Uuid>,
    transaction_price: Decimal,
    currency: String,
    status: String,
    effective_date: NaiveDate,
    created_at: DateTime<Utc>,
    updated_at: DateTime<Utc>,
}

#[derive(FromRow)]
struct PerformanceObligationRow {
    id: Uuid,
    contract_id: Uuid,
    description: String,
    standalone_selling_price: Option<Decimal>,
    allocated_amount: Decimal,
    recognition_method: String,
    recognized_amount: Decimal,
    created_at: DateTime<Utc>,
    updated_at: DateTime<Utc>,
}

#[derive(FromRow)]
struct RevenueScheduleEntryRow {
    period: i32,
    period_start: NaiveDate,
    amount: Decimal,
    status: String,
}

impl PgRevenueRecognitionRepository {
    pub const fn new(pool: PgPool) -> Self {
        Self { pool }
    }

    fn parse_field<T: std::str::FromStr>(raw: &str, field: &str) -> Result<T>
    where
        T::Err: std::fmt::Display,
    {
        raw.parse().map_err(|e| {
            CommerceError::DatabaseError(format!("Invalid revenue_contract.{field} '{raw}': {e}"))
        })
    }

    fn row_to_contract(row: RevenueContractRow) -> Result<RevenueContract> {
        Ok(RevenueContract {
            id: row.id,
            contract_number: row.contract_number,
            customer_id: row.customer_id,
            order_id: row.order_id,
            invoice_id: row.invoice_id,
            transaction_price: row.transaction_price,
            currency: Self::parse_field::<CurrencyCode>(&row.currency, "currency")?,
            status: Self::parse_field::<RevenueContractStatus>(&row.status, "status")?,
            effective_date: row.effective_date,
            obligations: Vec::new(),
            created_at: row.created_at,
            updated_at: row.updated_at,
        })
    }

    fn row_to_obligation(row: PerformanceObligationRow) -> Result<PerformanceObligation> {
        Ok(PerformanceObligation {
            id: row.id,
            contract_id: row.contract_id,
            description: row.description,
            standalone_selling_price: row.standalone_selling_price,
            allocated_amount: row.allocated_amount,
            recognition_method: serde_json::from_str::<RecognitionMethod>(&row.recognition_method)
                .map_err(|e| {
                    CommerceError::DatabaseError(format!(
                        "Invalid performance_obligation.recognition_method: {e}"
                    ))
                })?,
            recognized_amount: row.recognized_amount,
            created_at: row.created_at,
            updated_at: row.updated_at,
        })
    }

    fn row_to_entry(row: RevenueScheduleEntryRow) -> Result<RevenueScheduleEntry> {
        Ok(RevenueScheduleEntry {
            period: u32::try_from(row.period).unwrap_or(0),
            period_start: row.period_start,
            amount: row.amount,
            status: Self::parse_field::<RevenueEntryStatus>(&row.status, "entry.status")?,
        })
    }

    async fn load_obligations_async(
        &self,
        contract_id: Uuid,
    ) -> Result<Vec<PerformanceObligation>> {
        let rows = sqlx::query_as::<_, PerformanceObligationRow>(
            "SELECT * FROM performance_obligations WHERE contract_id = $1 ORDER BY created_at, id",
        )
        .bind(contract_id)
        .fetch_all(&self.pool)
        .await
        .map_err(map_db_error)?;
        rows.into_iter().map(Self::row_to_obligation).collect()
    }

    async fn load_full_async(&self, id: Uuid) -> Result<Option<RevenueContract>> {
        let row = sqlx::query_as::<_, RevenueContractRow>(
            "SELECT * FROM revenue_contracts WHERE id = $1",
        )
        .bind(id)
        .fetch_optional(&self.pool)
        .await
        .map_err(map_db_error)?;
        match row {
            Some(row) => {
                let mut head = Self::row_to_contract(row)?;
                head.obligations = self.load_obligations_async(id).await?;
                Ok(Some(head))
            }
            None => Ok(None),
        }
    }

    async fn load_entries_async(&self, obligation_id: Uuid) -> Result<Vec<RevenueScheduleEntry>> {
        let rows = sqlx::query_as::<_, RevenueScheduleEntryRow>(
            "SELECT * FROM revenue_schedule_entries WHERE obligation_id = $1 ORDER BY period",
        )
        .bind(obligation_id)
        .fetch_all(&self.pool)
        .await
        .map_err(map_db_error)?;
        rows.into_iter().map(Self::row_to_entry).collect()
    }

    async fn require_obligation_locked(
        tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
        id: Uuid,
    ) -> Result<PerformanceObligation> {
        let row = sqlx::query_as::<_, PerformanceObligationRow>(
            "SELECT * FROM performance_obligations WHERE id = $1 FOR UPDATE",
        )
        .bind(id)
        .fetch_optional(tx.as_mut())
        .await
        .map_err(map_db_error)?;
        row.map(Self::row_to_obligation).transpose()?.ok_or(CommerceError::NotFound)
    }

    /// Create a revenue contract with obligations (async)
    pub async fn create_contract_async(
        &self,
        input: CreateRevenueContract,
    ) -> Result<RevenueContract> {
        input.validate()?;
        let id = Uuid::new_v4();
        let now = Utc::now();
        let contract_number =
            input.contract_number.clone().unwrap_or_else(generate_revenue_contract_number);
        let currency = input.currency.unwrap_or_default();

        let mut tx = self.pool.begin().await.map_err(map_db_error)?;
        sqlx::query(
            "INSERT INTO revenue_contracts (id, contract_number, customer_id, order_id, invoice_id, transaction_price, currency, status, effective_date, created_at, updated_at)
             VALUES ($1, $2, $3, $4, $5, $6, $7, 'draft', $8, $9, $9)",
        )
        .bind(id)
        .bind(&contract_number)
        .bind(input.customer_id)
        .bind(input.order_id)
        .bind(input.invoice_id)
        .bind(input.transaction_price)
        .bind(currency.to_string())
        .bind(input.effective_date)
        .bind(now)
        .execute(tx.as_mut())
        .await
        .map_err(map_db_error)?;

        for ob in &input.obligations {
            let method_json = serde_json::to_string(&ob.recognition_method)
                .map_err(|e| CommerceError::DatabaseError(e.to_string()))?;
            sqlx::query(
                "INSERT INTO performance_obligations (id, contract_id, description, standalone_selling_price, allocated_amount, recognition_method, recognized_amount, created_at, updated_at)
                 VALUES ($1, $2, $3, $4, $5, $6, 0, $7, $7)",
            )
            .bind(Uuid::new_v4())
            .bind(id)
            .bind(&ob.description)
            .bind(ob.standalone_selling_price)
            .bind(ob.allocated_amount)
            .bind(&method_json)
            .bind(now)
            .execute(tx.as_mut())
            .await
            .map_err(map_db_error)?;
        }
        tx.commit().await.map_err(map_db_error)?;
        self.load_full_async(id).await?.ok_or(CommerceError::NotFound)
    }

    /// Get a revenue contract with obligations (async)
    pub async fn get_contract_async(&self, id: Uuid) -> Result<Option<RevenueContract>> {
        self.load_full_async(id).await
    }

    /// List revenue contracts (async)
    pub async fn list_contracts_async(
        &self,
        filter: RevenueContractFilter,
    ) -> Result<Vec<RevenueContract>> {
        let after_cursor = super::parse_after_cursor(filter.after_cursor.as_ref())?;
        let limit = super::effective_limit(filter.limit);
        // Offset pagination applies only in non-cursor mode.
        let offset = if after_cursor.is_none() { i64::from(filter.offset.unwrap_or(0)) } else { 0 };
        let mut query = String::from("SELECT * FROM revenue_contracts WHERE 1=1");
        let mut idx = 1;
        if filter.customer_id.is_some() {
            query.push_str(&format!(" AND customer_id = ${idx}"));
            idx += 1;
        }
        if filter.order_id.is_some() {
            query.push_str(&format!(" AND order_id = ${idx}"));
            idx += 1;
        }
        if filter.invoice_id.is_some() {
            query.push_str(&format!(" AND invoice_id = ${idx}"));
            idx += 1;
        }
        if filter.status.is_some() {
            query.push_str(&format!(" AND status = ${idx}"));
            idx += 1;
        }
        if filter.effective_from.is_some() {
            query.push_str(&format!(" AND effective_date >= ${idx}"));
            idx += 1;
        }
        if filter.effective_to.is_some() {
            query.push_str(&format!(" AND effective_date <= ${idx}"));
            idx += 1;
        }
        if filter.search.is_some() {
            query.push_str(&format!(" AND contract_number ILIKE ${idx}"));
            idx += 1;
        }
        if after_cursor.is_some() {
            // Keyset cursor: (created_at, id) for stable DESC ordering
            query.push_str(&format!(
                " AND (created_at < ${idx} OR (created_at = ${idx} AND id < ${}))",
                idx + 1
            ));
            idx += 2;
        }
        query.push_str(&format!(
            " ORDER BY created_at DESC, id DESC LIMIT ${} OFFSET ${}",
            idx,
            idx + 1
        ));

        let mut q = sqlx::query_as::<_, RevenueContractRow>(&query);
        if let Some(customer) = filter.customer_id {
            q = q.bind(customer);
        }
        if let Some(order) = filter.order_id {
            q = q.bind(order);
        }
        if let Some(invoice) = filter.invoice_id {
            q = q.bind(invoice);
        }
        if let Some(status) = filter.status {
            q = q.bind(status.to_string());
        }
        if let Some(from) = filter.effective_from {
            q = q.bind(from);
        }
        if let Some(to) = filter.effective_to {
            q = q.bind(to);
        }
        if let Some(search) = filter.search {
            q = q.bind(format!("%{search}%"));
        }
        if let Some((cursor_created, cursor_id)) = after_cursor {
            q = q.bind(cursor_created).bind(cursor_id);
        }
        let rows = q.bind(limit).bind(offset).fetch_all(&self.pool).await.map_err(map_db_error)?;
        let mut out = Vec::with_capacity(rows.len());
        for row in rows {
            let id = row.id;
            let mut head = Self::row_to_contract(row)?;
            head.obligations = self.load_obligations_async(id).await?;
            out.push(head);
        }
        Ok(out)
    }

    /// Update a revenue contract (async)
    pub async fn update_contract_async(
        &self,
        id: Uuid,
        input: UpdateRevenueContract,
    ) -> Result<RevenueContract> {
        let mut tx = self.pool.begin().await.map_err(map_db_error)?;
        let row = sqlx::query_as::<_, RevenueContractRow>(
            "SELECT * FROM revenue_contracts WHERE id = $1 FOR UPDATE",
        )
        .bind(id)
        .fetch_optional(tx.as_mut())
        .await
        .map_err(map_db_error)?;
        let contract =
            row.map(Self::row_to_contract).transpose()?.ok_or(CommerceError::NotFound)?;
        let status = match input.status {
            Some(next) if next != contract.status => {
                if !contract.status.can_transition_to(next) {
                    return Err(CommerceError::Conflict(format!(
                        "revenue contract cannot transition from {} to {next}",
                        contract.status
                    )));
                }
                next
            }
            _ => contract.status,
        };
        sqlx::query(
            "UPDATE revenue_contracts SET order_id = $1, invoice_id = $2, status = $3, effective_date = $4, updated_at = $5 WHERE id = $6",
        )
        .bind(input.order_id.or(contract.order_id))
        .bind(input.invoice_id.or(contract.invoice_id))
        .bind(status.to_string())
        .bind(input.effective_date.unwrap_or(contract.effective_date))
        .bind(Utc::now())
        .bind(id)
        .execute(tx.as_mut())
        .await
        .map_err(map_db_error)?;
        tx.commit().await.map_err(map_db_error)?;
        self.load_full_async(id).await?.ok_or(CommerceError::NotFound)
    }

    /// Generate and persist the recognition schedule (async)
    pub async fn generate_schedule_async(&self, obligation_id: Uuid) -> Result<RevenueSchedule> {
        let mut tx = self.pool.begin().await.map_err(map_db_error)?;
        let obligation = Self::require_obligation_locked(&mut tx, obligation_id).await?;
        let recognized: i64 = sqlx::query_scalar(
            "SELECT COUNT(*) FROM revenue_schedule_entries WHERE obligation_id = $1 AND status = 'recognized'",
        )
        .bind(obligation_id)
        .fetch_one(tx.as_mut())
        .await
        .map_err(map_db_error)?;
        if recognized > 0 {
            return Err(CommerceError::Conflict(
                "cannot regenerate a schedule with recognized revenue entries".into(),
            ));
        }
        let recognition_date: NaiveDate =
            sqlx::query_scalar("SELECT effective_date FROM revenue_contracts WHERE id = $1")
                .bind(obligation.contract_id)
                .fetch_one(tx.as_mut())
                .await
                .map_err(map_db_error)?;
        let entries = generate_revenue_schedule(
            obligation.recognition_method,
            obligation.allocated_amount,
            recognition_date,
        );
        if entries.is_empty() {
            return Err(CommerceError::ValidationError(
                "cannot generate a time-based revenue schedule for this obligation".into(),
            ));
        }
        sqlx::query("DELETE FROM revenue_schedule_entries WHERE obligation_id = $1")
            .bind(obligation_id)
            .execute(tx.as_mut())
            .await
            .map_err(map_db_error)?;
        for e in &entries {
            sqlx::query(
                "INSERT INTO revenue_schedule_entries (obligation_id, period, period_start, amount, status)
                 VALUES ($1, $2, $3, $4, $5)",
            )
            .bind(obligation_id)
            .bind(i32::try_from(e.period).unwrap_or(i32::MAX))
            .bind(e.period_start)
            .bind(e.amount)
            .bind(e.status.to_string())
            .execute(tx.as_mut())
            .await
            .map_err(map_db_error)?;
        }
        tx.commit().await.map_err(map_db_error)?;
        Ok(RevenueSchedule {
            obligation_id,
            method: obligation.recognition_method,
            total_amount: obligation.allocated_amount,
            entries,
        })
    }

    /// Get the persisted recognition schedule (async)
    pub async fn get_schedule_async(&self, obligation_id: Uuid) -> Result<Option<RevenueSchedule>> {
        let row = sqlx::query_as::<_, PerformanceObligationRow>(
            "SELECT * FROM performance_obligations WHERE id = $1",
        )
        .bind(obligation_id)
        .fetch_optional(&self.pool)
        .await
        .map_err(map_db_error)?;
        let Some(obligation) = row.map(Self::row_to_obligation).transpose()? else {
            return Ok(None);
        };
        let entries = self.load_entries_async(obligation_id).await?;
        if entries.is_empty() {
            return Ok(None);
        }
        Ok(Some(RevenueSchedule {
            obligation_id,
            method: obligation.recognition_method,
            total_amount: entries.iter().map(|e| e.amount).sum(),
            entries,
        }))
    }

    /// Recognize deferred entries through a date (async)
    pub async fn recognize_period_async(
        &self,
        obligation_id: Uuid,
        through: NaiveDate,
    ) -> Result<RevenueSchedule> {
        let mut tx = self.pool.begin().await.map_err(map_db_error)?;
        let obligation = Self::require_obligation_locked(&mut tx, obligation_id).await?;
        let total_entries: i64 = sqlx::query_scalar(
            "SELECT COUNT(*) FROM revenue_schedule_entries WHERE obligation_id = $1",
        )
        .bind(obligation_id)
        .fetch_one(tx.as_mut())
        .await
        .map_err(map_db_error)?;
        if total_entries == 0 {
            return Err(CommerceError::Conflict(
                "no revenue schedule has been generated for this obligation".into(),
            ));
        }
        let newly_recognized: Option<Decimal> = sqlx::query_scalar(
            "SELECT SUM(amount) FROM revenue_schedule_entries WHERE obligation_id = $1 AND status = 'deferred' AND period_start <= $2",
        )
        .bind(obligation_id)
        .bind(through)
        .fetch_one(tx.as_mut())
        .await
        .map_err(map_db_error)?;
        let newly_recognized = newly_recognized.unwrap_or(Decimal::ZERO);
        sqlx::query(
            "UPDATE revenue_schedule_entries SET status = 'recognized' WHERE obligation_id = $1 AND status = 'deferred' AND period_start <= $2",
        )
        .bind(obligation_id)
        .bind(through)
        .execute(tx.as_mut())
        .await
        .map_err(map_db_error)?;
        let now = Utc::now();
        sqlx::query(
            "UPDATE performance_obligations SET recognized_amount = recognized_amount + $1, updated_at = $2 WHERE id = $3",
        )
        .bind(newly_recognized)
        .bind(now)
        .bind(obligation_id)
        .execute(tx.as_mut())
        .await
        .map_err(map_db_error)?;
        // Complete the contract when every obligation is fully recognized.
        sqlx::query(
            "UPDATE revenue_contracts SET status = 'completed', updated_at = $1
             WHERE id = $2 AND status = 'active' AND NOT EXISTS (
                 SELECT 1 FROM performance_obligations
                 WHERE contract_id = $2 AND recognized_amount < allocated_amount
             )",
        )
        .bind(now)
        .bind(obligation.contract_id)
        .execute(tx.as_mut())
        .await
        .map_err(map_db_error)?;
        tx.commit().await.map_err(map_db_error)?;
        self.auto_post_recognition_entry(obligation_id, newly_recognized, through).await?;
        let entries = self.load_entries_async(obligation_id).await?;
        Ok(RevenueSchedule {
            obligation_id,
            method: obligation.recognition_method,
            total_amount: entries.iter().map(|e| e.amount).sum(),
            entries,
        })
    }

    /// Create and post a balanced revenue-recognition journal entry
    /// (debit deferred/unearned revenue, credit sales revenue) when the active
    /// GL auto-posting config has `auto_post_revenue_recognition` enabled.
    ///
    /// Mirrors the invoice auto-posting pattern: config-gated, posted via the
    /// existing general-ledger repository. The deferred-revenue account is the
    /// config's `unearned_revenue_account_id`, falling back to the first active
    /// posting account with the `unearned_revenue` sub-type; revenue is credited
    /// to the config's `sales_revenue_account_id`.
    async fn auto_post_recognition_entry(
        &self,
        obligation_id: Uuid,
        amount: Decimal,
        through: NaiveDate,
    ) -> Result<()> {
        let gl = super::general_ledger::PgGeneralLedgerRepository::new(self.pool.clone());
        let Some(config) = gl.get_auto_posting_config_async().await? else { return Ok(()) };
        if !config.auto_post_revenue_recognition || amount <= Decimal::ZERO {
            return Ok(());
        }
        let deferred_account_id = match config.unearned_revenue_account_id {
            Some(id) => id,
            None => gl
                .list_accounts_async(stateset_core::GlAccountFilter {
                    account_sub_type: Some(stateset_core::AccountSubType::UnearnedRevenue),
                    status: Some(stateset_core::AccountStatus::Active),
                    is_posting: Some(true),
                    limit: Some(1),
                    ..Default::default()
                })
                .await?
                .first()
                .map(|a| a.id)
                .ok_or_else(|| {
                    CommerceError::ValidationError(
                        "auto-post revenue recognition requires an unearned_revenue account in the auto-posting config or chart of accounts"
                            .to_string(),
                    )
                })?,
        };
        gl.create_journal_entry_async(stateset_core::CreateJournalEntry {
            entry_date: through,
            entry_type: Some(stateset_core::JournalEntryType::Standard),
            description: format!("Revenue recognition for obligation {obligation_id}"),
            lines: vec![
                stateset_core::CreateJournalEntryLine::debit(
                    deferred_account_id,
                    amount,
                    Some("Deferred Revenue".to_string()),
                ),
                stateset_core::CreateJournalEntryLine::credit(
                    config.sales_revenue_account_id,
                    amount,
                    Some("Sales Revenue".to_string()),
                ),
            ],
            source_document_type: Some("revenue_recognition".to_string()),
            source_document_id: Some(obligation_id),
            auto_post: Some(true),
        })
        .await?;
        Ok(())
    }

    /// List the obligations under a contract (async)
    pub async fn list_obligations_async(
        &self,
        contract_id: Uuid,
    ) -> Result<Vec<PerformanceObligation>> {
        self.load_obligations_async(contract_id).await
    }
}

impl RevenueRecognitionRepository for PgRevenueRecognitionRepository {
    fn create_contract(&self, input: CreateRevenueContract) -> Result<RevenueContract> {
        super::block_on(self.create_contract_async(input))
    }

    fn get_contract(&self, id: Uuid) -> Result<Option<RevenueContract>> {
        super::block_on(self.get_contract_async(id))
    }

    fn list_contracts(&self, filter: RevenueContractFilter) -> Result<Vec<RevenueContract>> {
        super::block_on(self.list_contracts_async(filter))
    }

    fn update_contract(&self, id: Uuid, input: UpdateRevenueContract) -> Result<RevenueContract> {
        super::block_on(self.update_contract_async(id, input))
    }

    fn list_obligations(&self, contract_id: Uuid) -> Result<Vec<PerformanceObligation>> {
        super::block_on(self.list_obligations_async(contract_id))
    }

    fn generate_schedule(&self, obligation_id: Uuid) -> Result<RevenueSchedule> {
        super::block_on(self.generate_schedule_async(obligation_id))
    }

    fn get_schedule(&self, obligation_id: Uuid) -> Result<Option<RevenueSchedule>> {
        super::block_on(self.get_schedule_async(obligation_id))
    }

    fn recognize_period(&self, obligation_id: Uuid, through: NaiveDate) -> Result<RevenueSchedule> {
        super::block_on(self.recognize_period_async(obligation_id, through))
    }
}