backbone-accounting 0.18.0

GL, journals, ledger, reconciliation (extracted from bersihir)
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
//! Accounting Module
//!
//! Generated by metaphor-schema. Enhanced with runtime implementations.
//!
//! This module provides:
//! - Domain entities and repositories
//! - Application services
//! - HTTP and gRPC handlers
//! - Route configuration
//! - State machine enforcement
//! - Validation rules runtime
//! - RBAC middleware
//! - Trigger execution system
//! - Computed fields
//! - Workflow orchestrator

#![recursion_limit = "1024"]
#![allow(unused_imports)]

// Generated modules
pub mod domain;
pub mod infrastructure;
pub mod application;
pub mod presentation;
pub mod seeders;
pub mod exports;

// The module's extension (hand-written; ADR-0031).
#[path = "lib.ext.rs"]
mod lib_ext;
pub use lib_ext::*;
/// This crate's module and builder under fixed names, for the extension's `impl` blocks.
#[allow(dead_code)]
pub(crate) type ThisModule = AccountingModule;
#[allow(dead_code)]
pub(crate) type ThisModuleBuilder = AccountingModuleBuilder;

// Re-exports for convenience - Domain entities
pub use domain::entity::*;

// Re-exports - Infrastructure
pub use infrastructure::persistence::*;

// Re-exports - Application services
pub use application::service::AccountService;
pub use application::service::AccountingPostService;
pub use application::service::CostCenterService;
pub use application::service::EmvQrConfigService;
pub use application::service::BankCheckSequenceService;
pub use application::service::PrintedCheckService;
pub use application::service::TaxTagRepairRunService;
pub use application::service::FinancialStatementService;
pub use application::service::FiscalPeriodService;
pub use application::service::JournalService;
pub use application::service::JournalLineService;
pub use application::service::LedgerService;
pub use application::service::ReconciliationService;
pub use application::service::ReconciliationItemService;
pub use application::service::FullReconcileService;
pub use application::service::PartialReconcileService;

use std::sync::Arc;
use axum::Router;
use sqlx::PgPool;

/// Accounting module configuration
///
/// Use the builder pattern to configure and register this module:
///
/// ```text
/// let accounting = AccountingModule::builder()
///     .with_database(pool.clone())
///     .build()?;
///
/// // Unguarded full CRUD (trusted/admin); compose a guarded router for production.
/// let router = accounting.all_crud_routes();
/// ```
pub struct AccountingModule {
    pub(crate) account_service: Arc<AccountService>,
    pub(crate) accounting_post_service: Arc<AccountingPostService>,
    pub(crate) cost_center_service: Arc<CostCenterService>,
    pub(crate) emv_qr_config_service: Arc<EmvQrConfigService>,
    pub(crate) bank_check_sequence_service: Arc<BankCheckSequenceService>,
    pub(crate) printed_check_service: Arc<PrintedCheckService>,
    pub(crate) tax_tag_repair_run_service: Arc<TaxTagRepairRunService>,
    pub(crate) financial_statement_service: Arc<FinancialStatementService>,
    pub(crate) fiscal_period_service: Arc<FiscalPeriodService>,
    pub(crate) journal_service: Arc<JournalService>,
    pub(crate) journal_line_service: Arc<JournalLineService>,
    pub(crate) ledger_service: Arc<LedgerService>,
    pub(crate) reconciliation_service: Arc<ReconciliationService>,
    pub(crate) reconciliation_item_service: Arc<ReconciliationItemService>,
    pub(crate) full_reconcile_service: Arc<FullReconcileService>,
    pub(crate) partial_reconcile_service: Arc<PartialReconcileService>,
    /// The module's extension state (`lib.ext.rs`); its fields read through `Deref`.
    pub(crate) ext: ModuleExt,
}

impl std::ops::Deref for AccountingModule {
    type Target = ModuleExt;
    fn deref(&self) -> &Self::Target {
        &self.ext
    }
}

impl std::ops::DerefMut for AccountingModule {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.ext
    }
}

/// What the extension's `build` hook receives from the generated build (ADR-0031).
#[allow(dead_code)]
pub(crate) struct ModuleParts<'a> {
    pub(crate) db_pool: &'a PgPool,
    pub(crate) account_service: &'a Arc<AccountService>,
    pub(crate) account_repository: &'a Arc<AccountRepository>,
    pub(crate) accounting_post_service: &'a Arc<AccountingPostService>,
    pub(crate) accounting_post_repository: &'a Arc<AccountingPostRepository>,
    pub(crate) cost_center_service: &'a Arc<CostCenterService>,
    pub(crate) cost_center_repository: &'a Arc<CostCenterRepository>,
    pub(crate) emv_qr_config_service: &'a Arc<EmvQrConfigService>,
    pub(crate) emv_qr_config_repository: &'a Arc<EmvQrConfigRepository>,
    pub(crate) bank_check_sequence_service: &'a Arc<BankCheckSequenceService>,
    pub(crate) bank_check_sequence_repository: &'a Arc<BankCheckSequenceRepository>,
    pub(crate) printed_check_service: &'a Arc<PrintedCheckService>,
    pub(crate) printed_check_repository: &'a Arc<PrintedCheckRepository>,
    pub(crate) tax_tag_repair_run_service: &'a Arc<TaxTagRepairRunService>,
    pub(crate) tax_tag_repair_run_repository: &'a Arc<TaxTagRepairRunRepository>,
    pub(crate) financial_statement_service: &'a Arc<FinancialStatementService>,
    pub(crate) financial_statement_repository: &'a Arc<FinancialStatementRepository>,
    pub(crate) fiscal_period_service: &'a Arc<FiscalPeriodService>,
    pub(crate) fiscal_period_repository: &'a Arc<FiscalPeriodRepository>,
    pub(crate) journal_service: &'a Arc<JournalService>,
    pub(crate) journal_repository: &'a Arc<JournalRepository>,
    pub(crate) journal_line_service: &'a Arc<JournalLineService>,
    pub(crate) journal_line_repository: &'a Arc<JournalLineRepository>,
    pub(crate) ledger_service: &'a Arc<LedgerService>,
    pub(crate) ledger_repository: &'a Arc<LedgerRepository>,
    pub(crate) reconciliation_service: &'a Arc<ReconciliationService>,
    pub(crate) reconciliation_repository: &'a Arc<ReconciliationRepository>,
    pub(crate) reconciliation_item_service: &'a Arc<ReconciliationItemService>,
    pub(crate) reconciliation_item_repository: &'a Arc<ReconciliationItemRepository>,
    pub(crate) full_reconcile_service: &'a Arc<FullReconcileService>,
    pub(crate) full_reconcile_repository: &'a Arc<FullReconcileRepository>,
    pub(crate) partial_reconcile_service: &'a Arc<PartialReconcileService>,
    pub(crate) partial_reconcile_repository: &'a Arc<PartialReconcileRepository>,
}

impl AccountingModule {
    /// Create a new module builder
    pub fn builder() -> AccountingModuleBuilder {
        AccountingModuleBuilder::new()
    }

    /// Mount ALL generated CRUD endpoints (12 per entity) with NO domain
    /// validation — the fully **unguarded** surface. A well-formed request can
    /// create invalid rows or soft-delete a referenced master out from under its
    /// dependents. Prefer a guarded composition (read + validated writes) for any
    /// real deployment; use this only in trusted/admin/seeding contexts.
    pub fn all_crud_routes(&self) -> Router {
        use presentation::http::{
            create_account_routes,
            create_accounting_post_routes,
            create_cost_center_routes,
            create_emv_qr_config_routes,
            create_bank_check_sequence_routes,
            create_printed_check_routes,
            create_tax_tag_repair_run_routes,
            create_financial_statement_routes,
            create_fiscal_period_read_routes,
            create_journal_routes,
            create_journal_line_routes,
            create_ledger_routes,
            create_reconciliation_routes,
            create_reconciliation_item_routes,
            create_full_reconcile_routes,
            create_partial_reconcile_routes,
        };

        Router::new()
            .merge(create_account_routes(self.account_service.clone()))
            .merge(create_accounting_post_routes(self.accounting_post_service.clone()))
            .merge(create_cost_center_routes(self.cost_center_service.clone()))
            .merge(create_emv_qr_config_routes(self.emv_qr_config_service.clone()))
            .merge(create_bank_check_sequence_routes(self.bank_check_sequence_service.clone()))
            .merge(create_printed_check_routes(self.printed_check_service.clone()))
            .merge(create_tax_tag_repair_run_routes(self.tax_tag_repair_run_service.clone()))
            .merge(create_financial_statement_routes(self.financial_statement_service.clone()))
            // FiscalPeriod: hand_set lifecycle — the state field moves only through the
            // module's validated verbs; generic writes cannot reach it, so only the
            // read surface mounts here.
            .merge(create_fiscal_period_read_routes(self.fiscal_period_service.clone()))
            .merge(create_journal_routes(self.journal_service.clone()))
            .merge(create_journal_line_routes(self.journal_line_service.clone()))
            .merge(create_ledger_routes(self.ledger_service.clone()))
            .merge(create_reconciliation_routes(self.reconciliation_service.clone()))
            .merge(create_reconciliation_item_routes(self.reconciliation_item_service.clone()))
            .merge(create_full_reconcile_routes(self.full_reconcile_service.clone()))
            .merge(create_partial_reconcile_routes(self.partial_reconcile_service.clone()))
    }

    /// Deprecated alias for [`Self::all_crud_routes`]. `routes()` reads like
    /// "the routes" but mounts UNVALIDATED generic CRUD on every entity — a naive
    /// mount exposes unguarded writes. Compose a guarded router (read + validated
    /// writes) for production, or call `all_crud_routes()` to opt into the full
    /// unguarded surface explicitly.
    #[deprecated(note = "mounts unvalidated generic CRUD; prefer readonly_routes() + validated writes, or all_crud_routes() for the full/unguarded surface")]
    pub fn routes(&self) -> Router {
        self.all_crud_routes()
    }

    /// Read-only routes for every entity (GET endpoints only) — the safe base.
    ///
    /// Generic mutation can't reach here, so this surface cannot bypass a
    /// validated write service's invariants. Use this as the production base and
    /// merge validated write routes (or a write service's HTTP layer) onto it.
    pub fn readonly_routes(&self) -> Router {
        use presentation::http::{
            create_account_read_routes,
            create_accounting_post_read_routes,
            create_cost_center_read_routes,
            create_emv_qr_config_read_routes,
            create_bank_check_sequence_read_routes,
            create_printed_check_read_routes,
            create_tax_tag_repair_run_read_routes,
            create_financial_statement_read_routes,
            create_fiscal_period_read_routes,
            create_journal_read_routes,
            create_journal_line_read_routes,
            create_ledger_read_routes,
            create_reconciliation_read_routes,
            create_reconciliation_item_read_routes,
            create_full_reconcile_read_routes,
            create_partial_reconcile_read_routes,
        };

        Router::new()
            .merge(create_account_read_routes(self.account_service.clone()))
            .merge(create_accounting_post_read_routes(self.accounting_post_service.clone()))
            .merge(create_cost_center_read_routes(self.cost_center_service.clone()))
            .merge(create_emv_qr_config_read_routes(self.emv_qr_config_service.clone()))
            .merge(create_bank_check_sequence_read_routes(self.bank_check_sequence_service.clone()))
            .merge(create_printed_check_read_routes(self.printed_check_service.clone()))
            .merge(create_tax_tag_repair_run_read_routes(self.tax_tag_repair_run_service.clone()))
            .merge(create_financial_statement_read_routes(self.financial_statement_service.clone()))
            .merge(create_fiscal_period_read_routes(self.fiscal_period_service.clone()))
            .merge(create_journal_read_routes(self.journal_service.clone()))
            .merge(create_journal_line_read_routes(self.journal_line_service.clone()))
            .merge(create_ledger_read_routes(self.ledger_service.clone()))
            .merge(create_reconciliation_read_routes(self.reconciliation_service.clone()))
            .merge(create_reconciliation_item_read_routes(self.reconciliation_item_service.clone()))
            .merge(create_full_reconcile_read_routes(self.full_reconcile_service.clone()))
            .merge(create_partial_reconcile_read_routes(self.partial_reconcile_service.clone()))
    }

}

/// Builder for AccountingModule
pub struct AccountingModuleBuilder {
    db_pool: Option<PgPool>,
    /// The builder's extension state (`lib.ext.rs`); its fields read through `Deref`.
    ext: ModuleBuilderExt,
}

impl std::ops::Deref for AccountingModuleBuilder {
    type Target = ModuleBuilderExt;
    fn deref(&self) -> &Self::Target {
        &self.ext
    }
}

impl std::ops::DerefMut for AccountingModuleBuilder {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.ext
    }
}

impl AccountingModuleBuilder {
    /// Create a new builder
    pub fn new() -> Self {
        Self {
            db_pool: None,
            ext: Default::default(),
        }
    }

    /// Set the database connection pool
    pub fn with_database(mut self, pool: PgPool) -> Self {
        self.db_pool = Some(pool);
        self
    }


    /// Build the module with configured dependencies
    pub fn build(self) -> anyhow::Result<AccountingModule> {
        let db_pool = self.db_pool
            .ok_or_else(|| anyhow::anyhow!("Database pool not configured"))?;

        // Account service
        let account_repository = Arc::new(AccountRepository::new(db_pool.clone()));
        let account_service = Arc::new(crate::application::service::build_account_service(account_repository.clone()));

        // AccountingPost service
        let accounting_post_repository = Arc::new(AccountingPostRepository::new(db_pool.clone()));
        let accounting_post_service = Arc::new(crate::application::service::build_accounting_post_service(accounting_post_repository.clone()));

        // CostCenter service
        let cost_center_repository = Arc::new(CostCenterRepository::new(db_pool.clone()));
        let cost_center_service = Arc::new(crate::application::service::build_cost_center_service(cost_center_repository.clone()));

        // EmvQrConfig service
        let emv_qr_config_repository = Arc::new(EmvQrConfigRepository::new(db_pool.clone()));
        let emv_qr_config_service = Arc::new(crate::application::service::build_emv_qr_config_service(emv_qr_config_repository.clone()));

        // BankCheckSequence service
        let bank_check_sequence_repository = Arc::new(BankCheckSequenceRepository::new(db_pool.clone()));
        let bank_check_sequence_service = Arc::new(crate::application::service::build_bank_check_sequence_service(bank_check_sequence_repository.clone()));

        // PrintedCheck service
        let printed_check_repository = Arc::new(PrintedCheckRepository::new(db_pool.clone()));
        let printed_check_service = Arc::new(crate::application::service::build_printed_check_service(printed_check_repository.clone()));

        // TaxTagRepairRun service
        let tax_tag_repair_run_repository = Arc::new(TaxTagRepairRunRepository::new(db_pool.clone()));
        let tax_tag_repair_run_service = Arc::new(crate::application::service::build_tax_tag_repair_run_service(tax_tag_repair_run_repository.clone()));

        // FinancialStatement service
        let financial_statement_repository = Arc::new(FinancialStatementRepository::new(db_pool.clone()));
        let financial_statement_service = Arc::new(crate::application::service::build_financial_statement_service(financial_statement_repository.clone()));

        // FiscalPeriod service
        let fiscal_period_repository = Arc::new(FiscalPeriodRepository::new(db_pool.clone()));
        let fiscal_period_service = Arc::new(crate::application::service::build_fiscal_period_service(fiscal_period_repository.clone()));

        // Journal service
        let journal_repository = Arc::new(JournalRepository::new(db_pool.clone()));
        let journal_service = Arc::new(crate::application::service::build_journal_service(journal_repository.clone()));

        // JournalLine service
        let journal_line_repository = Arc::new(JournalLineRepository::new(db_pool.clone()));
        let journal_line_service = Arc::new(crate::application::service::build_journal_line_service(journal_line_repository.clone()));

        // Ledger service
        let ledger_repository = Arc::new(LedgerRepository::new(db_pool.clone()));
        let ledger_service = Arc::new(crate::application::service::build_ledger_service(ledger_repository.clone()));

        // Reconciliation service
        let reconciliation_repository = Arc::new(ReconciliationRepository::new(db_pool.clone()));
        let reconciliation_service = Arc::new(crate::application::service::build_reconciliation_service(reconciliation_repository.clone()));

        // ReconciliationItem service
        let reconciliation_item_repository = Arc::new(ReconciliationItemRepository::new(db_pool.clone()));
        let reconciliation_item_service = Arc::new(crate::application::service::build_reconciliation_item_service(reconciliation_item_repository.clone()));

        // FullReconcile service
        let full_reconcile_repository = Arc::new(FullReconcileRepository::new(db_pool.clone()));
        let full_reconcile_service = Arc::new(crate::application::service::build_full_reconcile_service(full_reconcile_repository.clone()));

        // PartialReconcile service
        let partial_reconcile_repository = Arc::new(PartialReconcileRepository::new(db_pool.clone()));
        let partial_reconcile_service = Arc::new(crate::application::service::build_partial_reconcile_service(partial_reconcile_repository.clone()));


        // The extension builds its own state from what the generated build made.
        let ext = self.ext.build(&ModuleParts {
            db_pool: &db_pool,
            account_service: &account_service,
            account_repository: &account_repository,
            accounting_post_service: &accounting_post_service,
            accounting_post_repository: &accounting_post_repository,
            cost_center_service: &cost_center_service,
            cost_center_repository: &cost_center_repository,
            emv_qr_config_service: &emv_qr_config_service,
            emv_qr_config_repository: &emv_qr_config_repository,
            bank_check_sequence_service: &bank_check_sequence_service,
            bank_check_sequence_repository: &bank_check_sequence_repository,
            printed_check_service: &printed_check_service,
            printed_check_repository: &printed_check_repository,
            tax_tag_repair_run_service: &tax_tag_repair_run_service,
            tax_tag_repair_run_repository: &tax_tag_repair_run_repository,
            financial_statement_service: &financial_statement_service,
            financial_statement_repository: &financial_statement_repository,
            fiscal_period_service: &fiscal_period_service,
            fiscal_period_repository: &fiscal_period_repository,
            journal_service: &journal_service,
            journal_repository: &journal_repository,
            journal_line_service: &journal_line_service,
            journal_line_repository: &journal_line_repository,
            ledger_service: &ledger_service,
            ledger_repository: &ledger_repository,
            reconciliation_service: &reconciliation_service,
            reconciliation_repository: &reconciliation_repository,
            reconciliation_item_service: &reconciliation_item_service,
            reconciliation_item_repository: &reconciliation_item_repository,
            full_reconcile_service: &full_reconcile_service,
            full_reconcile_repository: &full_reconcile_repository,
            partial_reconcile_service: &partial_reconcile_service,
            partial_reconcile_repository: &partial_reconcile_repository,
        })?;

        Ok(AccountingModule {
            account_service,
            accounting_post_service,
            cost_center_service,
            emv_qr_config_service,
            bank_check_sequence_service,
            printed_check_service,
            tax_tag_repair_run_service,
            financial_statement_service,
            fiscal_period_service,
            journal_service,
            journal_line_service,
            ledger_service,
            reconciliation_service,
            reconciliation_item_service,
            full_reconcile_service,
            partial_reconcile_service,
            ext,
        })
    }
}

impl Default for AccountingModuleBuilder {
    fn default() -> Self {
        Self::new()
    }
}