backbone-accounting 0.17.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
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
use chrono::{DateTime, Utc, NaiveDate};
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use uuid::Uuid;
use rust_decimal::Decimal;

use super::StatementType;
use super::StatementStatus;
use super::AuditMetadata;

/// Strongly-typed ID for FinancialStatement
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct FinancialStatementId(pub Uuid);

impl FinancialStatementId {
    pub fn new(id: Uuid) -> Self { Self(id) }
    pub fn generate() -> Self { Self(Uuid::new_v4()) }
    pub fn into_inner(self) -> Uuid { self.0 }
}

impl std::fmt::Display for FinancialStatementId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl std::str::FromStr for FinancialStatementId {
    type Err = uuid::Error;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Self(Uuid::parse_str(s)?))
    }
}

impl From<Uuid> for FinancialStatementId {
    fn from(id: Uuid) -> Self { Self(id) }
}

impl From<FinancialStatementId> for Uuid {
    fn from(id: FinancialStatementId) -> Self { id.0 }
}

impl AsRef<Uuid> for FinancialStatementId {
    fn as_ref(&self) -> &Uuid { &self.0 }
}

impl std::ops::Deref for FinancialStatementId {
    type Target = Uuid;
    fn deref(&self) -> &Self::Target { &self.0 }
}

#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct FinancialStatement {
    pub id: Uuid,
    pub statement_number: String,
    pub statement_type: StatementType,
    pub name: String,
    pub fiscal_period_id: Option<Uuid>,
    pub fiscal_year: i32,
    pub fiscal_month: Option<i32>,
    pub as_of_date: NaiveDate,
    pub period_start: Option<NaiveDate>,
    pub period_end: Option<NaiveDate>,
    pub is_comparative: bool,
    pub comparative_period_id: Option<Uuid>,
    pub comparative_as_of_date: Option<NaiveDate>,
    pub status: StatementStatus,
    pub total_current_assets: Option<Decimal>,
    pub total_non_current_assets: Option<Decimal>,
    pub total_assets: Option<Decimal>,
    pub total_current_liabilities: Option<Decimal>,
    pub total_non_current_liabilities: Option<Decimal>,
    pub total_liabilities: Option<Decimal>,
    pub total_equity: Option<Decimal>,
    pub balance_check: bool,
    pub balance_difference: Option<Decimal>,
    pub total_revenue: Option<Decimal>,
    pub total_cogs: Option<Decimal>,
    pub gross_profit: Option<Decimal>,
    pub gross_profit_margin: Option<Decimal>,
    pub total_operating_expenses: Option<Decimal>,
    pub operating_income: Option<Decimal>,
    pub operating_margin: Option<Decimal>,
    pub total_other_income: Option<Decimal>,
    pub total_other_expenses: Option<Decimal>,
    pub income_before_tax: Option<Decimal>,
    pub tax_expense: Option<Decimal>,
    pub net_income: Option<Decimal>,
    pub net_profit_margin: Option<Decimal>,
    pub cash_from_operations: Option<Decimal>,
    pub cash_from_investing: Option<Decimal>,
    pub cash_from_financing: Option<Decimal>,
    pub net_cash_change: Option<Decimal>,
    pub beginning_cash: Option<Decimal>,
    pub ending_cash: Option<Decimal>,
    pub total_debits: Option<Decimal>,
    pub total_credits: Option<Decimal>,
    pub trial_balance_check: bool,
    pub line_items: serde_json::Value,
    pub comparative_data: serde_json::Value,
    pub variance_data: serde_json::Value,
    pub notes: serde_json::Value,
    pub management_notes: Option<String>,
    pub generated_at: Option<DateTime<Utc>>,
    pub generated_by: Option<Uuid>,
    pub generation_parameters: serde_json::Value,
    pub reviewed_at: Option<DateTime<Utc>>,
    pub reviewed_by: Option<Uuid>,
    pub approved_at: Option<DateTime<Utc>>,
    pub approved_by: Option<Uuid>,
    pub published_at: Option<DateTime<Utc>>,
    pub pdf_url: Option<String>,
    pub excel_url: Option<String>,
    pub currency: String,
    #[serde(default)]
    #[sqlx(json)]
    pub metadata: AuditMetadata,
}

impl FinancialStatement {
    /// Create a builder for FinancialStatement
    pub fn builder() -> FinancialStatementBuilder {
        <FinancialStatementBuilder as Default>::default()
    }

    /// Create a new FinancialStatement with required fields
    pub fn new(statement_number: String, statement_type: StatementType, name: String, fiscal_year: i32, as_of_date: NaiveDate, is_comparative: bool, status: StatementStatus, balance_check: bool, trial_balance_check: bool, line_items: serde_json::Value, comparative_data: serde_json::Value, variance_data: serde_json::Value, notes: serde_json::Value, generation_parameters: serde_json::Value, currency: String) -> Self {
        Self {
            id: Uuid::new_v4(),
            statement_number,
            statement_type,
            name,
            fiscal_period_id: None,
            fiscal_year,
            fiscal_month: None,
            as_of_date,
            period_start: None,
            period_end: None,
            is_comparative,
            comparative_period_id: None,
            comparative_as_of_date: None,
            status,
            total_current_assets: None,
            total_non_current_assets: None,
            total_assets: None,
            total_current_liabilities: None,
            total_non_current_liabilities: None,
            total_liabilities: None,
            total_equity: None,
            balance_check,
            balance_difference: None,
            total_revenue: None,
            total_cogs: None,
            gross_profit: None,
            gross_profit_margin: None,
            total_operating_expenses: None,
            operating_income: None,
            operating_margin: None,
            total_other_income: None,
            total_other_expenses: None,
            income_before_tax: None,
            tax_expense: None,
            net_income: None,
            net_profit_margin: None,
            cash_from_operations: None,
            cash_from_investing: None,
            cash_from_financing: None,
            net_cash_change: None,
            beginning_cash: None,
            ending_cash: None,
            total_debits: None,
            total_credits: None,
            trial_balance_check,
            line_items,
            comparative_data,
            variance_data,
            notes,
            management_notes: None,
            generated_at: None,
            generated_by: None,
            generation_parameters,
            reviewed_at: None,
            reviewed_by: None,
            approved_at: None,
            approved_by: None,
            published_at: None,
            pdf_url: None,
            excel_url: None,
            currency,
            metadata: AuditMetadata::default(),
        }
    }

    /// Get the entity's unique identifier
    pub fn id(&self) -> &Uuid {
        &self.id
    }

    /// Get a strongly-typed ID for this entity
    pub fn typed_id(&self) -> FinancialStatementId {
        FinancialStatementId(self.id)
    }

    /// Get when this entity was created
    pub fn created_at(&self) -> Option<&DateTime<Utc>> {
        self.metadata.created_at.as_ref()
    }

    /// Get when this entity was last updated
    pub fn updated_at(&self) -> Option<&DateTime<Utc>> {
        self.metadata.updated_at.as_ref()
    }

    /// Check if this entity is soft deleted
    pub fn is_deleted(&self) -> bool {
        self.metadata.deleted_at.is_some()
    }

    /// Check if this entity is active (not deleted)
    pub fn is_active(&self) -> bool {
        self.metadata.deleted_at.is_none()
    }

    /// Get when this entity was deleted
    pub fn deleted_at(&self) -> Option<&DateTime<Utc>> {
        self.metadata.deleted_at.as_ref()
    }

    /// Get who created this entity
    pub fn created_by(&self) -> Option<&Uuid> {
        self.metadata.created_by.as_ref()
    }

    /// Get who last updated this entity
    pub fn updated_by(&self) -> Option<&Uuid> {
        self.metadata.updated_by.as_ref()
    }

    /// Get who deleted this entity
    pub fn deleted_by(&self) -> Option<&Uuid> {
        self.metadata.deleted_by.as_ref()
    }

    /// Get the current status
    pub fn status(&self) -> &StatementStatus {
        &self.status
    }


    // ==========================================================
    // Fluent Setters (with_* for optional fields)
    // ==========================================================

    /// Set the fiscal_period_id field (chainable)
    pub fn with_fiscal_period_id(mut self, value: Uuid) -> Self {
        self.fiscal_period_id = Some(value);
        self
    }

    /// Set the fiscal_month field (chainable)
    pub fn with_fiscal_month(mut self, value: i32) -> Self {
        self.fiscal_month = Some(value);
        self
    }

    /// Set the period_start field (chainable)
    pub fn with_period_start(mut self, value: NaiveDate) -> Self {
        self.period_start = Some(value);
        self
    }

    /// Set the period_end field (chainable)
    pub fn with_period_end(mut self, value: NaiveDate) -> Self {
        self.period_end = Some(value);
        self
    }

    /// Set the comparative_period_id field (chainable)
    pub fn with_comparative_period_id(mut self, value: Uuid) -> Self {
        self.comparative_period_id = Some(value);
        self
    }

    /// Set the comparative_as_of_date field (chainable)
    pub fn with_comparative_as_of_date(mut self, value: NaiveDate) -> Self {
        self.comparative_as_of_date = Some(value);
        self
    }

    /// Set the total_current_assets field (chainable)
    pub fn with_total_current_assets(mut self, value: Decimal) -> Self {
        self.total_current_assets = Some(value);
        self
    }

    /// Set the total_non_current_assets field (chainable)
    pub fn with_total_non_current_assets(mut self, value: Decimal) -> Self {
        self.total_non_current_assets = Some(value);
        self
    }

    /// Set the total_assets field (chainable)
    pub fn with_total_assets(mut self, value: Decimal) -> Self {
        self.total_assets = Some(value);
        self
    }

    /// Set the total_current_liabilities field (chainable)
    pub fn with_total_current_liabilities(mut self, value: Decimal) -> Self {
        self.total_current_liabilities = Some(value);
        self
    }

    /// Set the total_non_current_liabilities field (chainable)
    pub fn with_total_non_current_liabilities(mut self, value: Decimal) -> Self {
        self.total_non_current_liabilities = Some(value);
        self
    }

    /// Set the total_liabilities field (chainable)
    pub fn with_total_liabilities(mut self, value: Decimal) -> Self {
        self.total_liabilities = Some(value);
        self
    }

    /// Set the total_equity field (chainable)
    pub fn with_total_equity(mut self, value: Decimal) -> Self {
        self.total_equity = Some(value);
        self
    }

    /// Set the balance_difference field (chainable)
    pub fn with_balance_difference(mut self, value: Decimal) -> Self {
        self.balance_difference = Some(value);
        self
    }

    /// Set the total_revenue field (chainable)
    pub fn with_total_revenue(mut self, value: Decimal) -> Self {
        self.total_revenue = Some(value);
        self
    }

    /// Set the total_cogs field (chainable)
    pub fn with_total_cogs(mut self, value: Decimal) -> Self {
        self.total_cogs = Some(value);
        self
    }

    /// Set the gross_profit field (chainable)
    pub fn with_gross_profit(mut self, value: Decimal) -> Self {
        self.gross_profit = Some(value);
        self
    }

    /// Set the gross_profit_margin field (chainable)
    pub fn with_gross_profit_margin(mut self, value: Decimal) -> Self {
        self.gross_profit_margin = Some(value);
        self
    }

    /// Set the total_operating_expenses field (chainable)
    pub fn with_total_operating_expenses(mut self, value: Decimal) -> Self {
        self.total_operating_expenses = Some(value);
        self
    }

    /// Set the operating_income field (chainable)
    pub fn with_operating_income(mut self, value: Decimal) -> Self {
        self.operating_income = Some(value);
        self
    }

    /// Set the operating_margin field (chainable)
    pub fn with_operating_margin(mut self, value: Decimal) -> Self {
        self.operating_margin = Some(value);
        self
    }

    /// Set the total_other_income field (chainable)
    pub fn with_total_other_income(mut self, value: Decimal) -> Self {
        self.total_other_income = Some(value);
        self
    }

    /// Set the total_other_expenses field (chainable)
    pub fn with_total_other_expenses(mut self, value: Decimal) -> Self {
        self.total_other_expenses = Some(value);
        self
    }

    /// Set the income_before_tax field (chainable)
    pub fn with_income_before_tax(mut self, value: Decimal) -> Self {
        self.income_before_tax = Some(value);
        self
    }

    /// Set the tax_expense field (chainable)
    pub fn with_tax_expense(mut self, value: Decimal) -> Self {
        self.tax_expense = Some(value);
        self
    }

    /// Set the net_income field (chainable)
    pub fn with_net_income(mut self, value: Decimal) -> Self {
        self.net_income = Some(value);
        self
    }

    /// Set the net_profit_margin field (chainable)
    pub fn with_net_profit_margin(mut self, value: Decimal) -> Self {
        self.net_profit_margin = Some(value);
        self
    }

    /// Set the cash_from_operations field (chainable)
    pub fn with_cash_from_operations(mut self, value: Decimal) -> Self {
        self.cash_from_operations = Some(value);
        self
    }

    /// Set the cash_from_investing field (chainable)
    pub fn with_cash_from_investing(mut self, value: Decimal) -> Self {
        self.cash_from_investing = Some(value);
        self
    }

    /// Set the cash_from_financing field (chainable)
    pub fn with_cash_from_financing(mut self, value: Decimal) -> Self {
        self.cash_from_financing = Some(value);
        self
    }

    /// Set the net_cash_change field (chainable)
    pub fn with_net_cash_change(mut self, value: Decimal) -> Self {
        self.net_cash_change = Some(value);
        self
    }

    /// Set the beginning_cash field (chainable)
    pub fn with_beginning_cash(mut self, value: Decimal) -> Self {
        self.beginning_cash = Some(value);
        self
    }

    /// Set the ending_cash field (chainable)
    pub fn with_ending_cash(mut self, value: Decimal) -> Self {
        self.ending_cash = Some(value);
        self
    }

    /// Set the total_debits field (chainable)
    pub fn with_total_debits(mut self, value: Decimal) -> Self {
        self.total_debits = Some(value);
        self
    }

    /// Set the total_credits field (chainable)
    pub fn with_total_credits(mut self, value: Decimal) -> Self {
        self.total_credits = Some(value);
        self
    }

    /// Set the management_notes field (chainable)
    pub fn with_management_notes(mut self, value: String) -> Self {
        self.management_notes = Some(value);
        self
    }

    /// Set the generated_at field (chainable)
    pub fn with_generated_at(mut self, value: DateTime<Utc>) -> Self {
        self.generated_at = Some(value);
        self
    }

    /// Set the generated_by field (chainable)
    pub fn with_generated_by(mut self, value: Uuid) -> Self {
        self.generated_by = Some(value);
        self
    }

    /// Set the reviewed_at field (chainable)
    pub fn with_reviewed_at(mut self, value: DateTime<Utc>) -> Self {
        self.reviewed_at = Some(value);
        self
    }

    /// Set the reviewed_by field (chainable)
    pub fn with_reviewed_by(mut self, value: Uuid) -> Self {
        self.reviewed_by = Some(value);
        self
    }

    /// Set the approved_at field (chainable)
    pub fn with_approved_at(mut self, value: DateTime<Utc>) -> Self {
        self.approved_at = Some(value);
        self
    }

    /// Set the approved_by field (chainable)
    pub fn with_approved_by(mut self, value: Uuid) -> Self {
        self.approved_by = Some(value);
        self
    }

    /// Set the published_at field (chainable)
    pub fn with_published_at(mut self, value: DateTime<Utc>) -> Self {
        self.published_at = Some(value);
        self
    }

    /// Set the pdf_url field (chainable)
    pub fn with_pdf_url(mut self, value: String) -> Self {
        self.pdf_url = Some(value);
        self
    }

    /// Set the excel_url field (chainable)
    pub fn with_excel_url(mut self, value: String) -> Self {
        self.excel_url = Some(value);
        self
    }

    // ==========================================================
    // Partial Update
    // ==========================================================

    /// Apply partial updates from a map of field name to JSON value
    pub fn apply_patch(&mut self, fields: std::collections::HashMap<String, serde_json::Value>) {
        for (key, value) in fields {
            match key.as_str() {
                "statement_number" => {
                    if let Ok(v) = serde_json::from_value(value) { self.statement_number = v; }
                }
                "statement_type" => {
                    if let Ok(v) = serde_json::from_value(value) { self.statement_type = v; }
                }
                "name" => {
                    if let Ok(v) = serde_json::from_value(value) { self.name = v; }
                }
                "fiscal_period_id" => {
                    if let Ok(v) = serde_json::from_value(value) { self.fiscal_period_id = v; }
                }
                "fiscal_year" => {
                    if let Ok(v) = serde_json::from_value(value) { self.fiscal_year = v; }
                }
                "fiscal_month" => {
                    if let Ok(v) = serde_json::from_value(value) { self.fiscal_month = v; }
                }
                "as_of_date" => {
                    if let Ok(v) = serde_json::from_value(value) { self.as_of_date = v; }
                }
                "period_start" => {
                    if let Ok(v) = serde_json::from_value(value) { self.period_start = v; }
                }
                "period_end" => {
                    if let Ok(v) = serde_json::from_value(value) { self.period_end = v; }
                }
                "is_comparative" => {
                    if let Ok(v) = serde_json::from_value(value) { self.is_comparative = v; }
                }
                "comparative_period_id" => {
                    if let Ok(v) = serde_json::from_value(value) { self.comparative_period_id = v; }
                }
                "comparative_as_of_date" => {
                    if let Ok(v) = serde_json::from_value(value) { self.comparative_as_of_date = v; }
                }
                "status" => {
                    if let Ok(v) = serde_json::from_value(value) { self.status = v; }
                }
                "total_current_assets" => {
                    if let Ok(v) = serde_json::from_value(value) { self.total_current_assets = v; }
                }
                "total_non_current_assets" => {
                    if let Ok(v) = serde_json::from_value(value) { self.total_non_current_assets = v; }
                }
                "total_assets" => {
                    if let Ok(v) = serde_json::from_value(value) { self.total_assets = v; }
                }
                "total_current_liabilities" => {
                    if let Ok(v) = serde_json::from_value(value) { self.total_current_liabilities = v; }
                }
                "total_non_current_liabilities" => {
                    if let Ok(v) = serde_json::from_value(value) { self.total_non_current_liabilities = v; }
                }
                "total_liabilities" => {
                    if let Ok(v) = serde_json::from_value(value) { self.total_liabilities = v; }
                }
                "total_equity" => {
                    if let Ok(v) = serde_json::from_value(value) { self.total_equity = v; }
                }
                "balance_check" => {
                    if let Ok(v) = serde_json::from_value(value) { self.balance_check = v; }
                }
                "balance_difference" => {
                    if let Ok(v) = serde_json::from_value(value) { self.balance_difference = v; }
                }
                "total_revenue" => {
                    if let Ok(v) = serde_json::from_value(value) { self.total_revenue = v; }
                }
                "total_cogs" => {
                    if let Ok(v) = serde_json::from_value(value) { self.total_cogs = v; }
                }
                "gross_profit" => {
                    if let Ok(v) = serde_json::from_value(value) { self.gross_profit = v; }
                }
                "gross_profit_margin" => {
                    if let Ok(v) = serde_json::from_value(value) { self.gross_profit_margin = v; }
                }
                "total_operating_expenses" => {
                    if let Ok(v) = serde_json::from_value(value) { self.total_operating_expenses = v; }
                }
                "operating_income" => {
                    if let Ok(v) = serde_json::from_value(value) { self.operating_income = v; }
                }
                "operating_margin" => {
                    if let Ok(v) = serde_json::from_value(value) { self.operating_margin = v; }
                }
                "total_other_income" => {
                    if let Ok(v) = serde_json::from_value(value) { self.total_other_income = v; }
                }
                "total_other_expenses" => {
                    if let Ok(v) = serde_json::from_value(value) { self.total_other_expenses = v; }
                }
                "income_before_tax" => {
                    if let Ok(v) = serde_json::from_value(value) { self.income_before_tax = v; }
                }
                "tax_expense" => {
                    if let Ok(v) = serde_json::from_value(value) { self.tax_expense = v; }
                }
                "net_income" => {
                    if let Ok(v) = serde_json::from_value(value) { self.net_income = v; }
                }
                "net_profit_margin" => {
                    if let Ok(v) = serde_json::from_value(value) { self.net_profit_margin = v; }
                }
                "cash_from_operations" => {
                    if let Ok(v) = serde_json::from_value(value) { self.cash_from_operations = v; }
                }
                "cash_from_investing" => {
                    if let Ok(v) = serde_json::from_value(value) { self.cash_from_investing = v; }
                }
                "cash_from_financing" => {
                    if let Ok(v) = serde_json::from_value(value) { self.cash_from_financing = v; }
                }
                "net_cash_change" => {
                    if let Ok(v) = serde_json::from_value(value) { self.net_cash_change = v; }
                }
                "beginning_cash" => {
                    if let Ok(v) = serde_json::from_value(value) { self.beginning_cash = v; }
                }
                "ending_cash" => {
                    if let Ok(v) = serde_json::from_value(value) { self.ending_cash = v; }
                }
                "total_debits" => {
                    if let Ok(v) = serde_json::from_value(value) { self.total_debits = v; }
                }
                "total_credits" => {
                    if let Ok(v) = serde_json::from_value(value) { self.total_credits = v; }
                }
                "trial_balance_check" => {
                    if let Ok(v) = serde_json::from_value(value) { self.trial_balance_check = v; }
                }
                "line_items" => {
                    if let Ok(v) = serde_json::from_value(value) { self.line_items = v; }
                }
                "comparative_data" => {
                    if let Ok(v) = serde_json::from_value(value) { self.comparative_data = v; }
                }
                "variance_data" => {
                    if let Ok(v) = serde_json::from_value(value) { self.variance_data = v; }
                }
                "notes" => {
                    if let Ok(v) = serde_json::from_value(value) { self.notes = v; }
                }
                "management_notes" => {
                    if let Ok(v) = serde_json::from_value(value) { self.management_notes = v; }
                }
                "generated_at" => {
                    if let Ok(v) = serde_json::from_value(value) { self.generated_at = v; }
                }
                "generated_by" => {
                    if let Ok(v) = serde_json::from_value(value) { self.generated_by = v; }
                }
                "generation_parameters" => {
                    if let Ok(v) = serde_json::from_value(value) { self.generation_parameters = v; }
                }
                "reviewed_at" => {
                    if let Ok(v) = serde_json::from_value(value) { self.reviewed_at = v; }
                }
                "reviewed_by" => {
                    if let Ok(v) = serde_json::from_value(value) { self.reviewed_by = v; }
                }
                "approved_at" => {
                    if let Ok(v) = serde_json::from_value(value) { self.approved_at = v; }
                }
                "approved_by" => {
                    if let Ok(v) = serde_json::from_value(value) { self.approved_by = v; }
                }
                "published_at" => {
                    if let Ok(v) = serde_json::from_value(value) { self.published_at = v; }
                }
                "pdf_url" => {
                    if let Ok(v) = serde_json::from_value(value) { self.pdf_url = v; }
                }
                "excel_url" => {
                    if let Ok(v) = serde_json::from_value(value) { self.excel_url = v; }
                }
                "currency" => {
                    if let Ok(v) = serde_json::from_value(value) { self.currency = v; }
                }
                _ => {} // ignore unknown fields
            }
        }
    }

}

impl super::Entity for FinancialStatement {
    type Id = Uuid;

    fn entity_id(&self) -> &Self::Id {
        &self.id
    }

    fn entity_type() -> &'static str {
        "FinancialStatement"
    }
}

impl backbone_core::PersistentEntity for FinancialStatement {
    fn entity_id(&self) -> String {
        self.id.to_string()
    }
    fn set_entity_id(&mut self, id: String) {
        if let Ok(uuid) = uuid::Uuid::parse_str(&id) {
            self.id = uuid;
        }
    }
    fn created_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
        self.metadata.created_at
    }
    fn set_created_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
        self.metadata.created_at = Some(ts);
    }
    fn updated_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
        self.metadata.updated_at
    }
    fn set_updated_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
        self.metadata.updated_at = Some(ts);
    }
    fn deleted_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
        self.metadata.deleted_at
    }
    fn set_deleted_at(&mut self, ts: Option<chrono::DateTime<chrono::Utc>>) {
        self.metadata.deleted_at = ts;
    }
}

impl backbone_orm::EntityRepoMeta for FinancialStatement {
    fn column_types() -> std::collections::HashMap<String, String> {
        let mut m = std::collections::HashMap::new();
        m.insert("id".to_string(), "uuid".to_string());
        m.insert("fiscal_period_id".to_string(), "uuid".to_string());
        m.insert("comparative_period_id".to_string(), "uuid".to_string());
        m.insert("statement_type".to_string(), "statement_type".to_string());
        m.insert("status".to_string(), "statement_status".to_string());
        m.insert("as_of_date".to_string(), "date".to_string());
        m.insert("period_start".to_string(), "date".to_string());
        m.insert("period_end".to_string(), "date".to_string());
        m.insert("comparative_as_of_date".to_string(), "date".to_string());
        m.insert("generated_at".to_string(), "timestamptz".to_string());
        m.insert("reviewed_at".to_string(), "timestamptz".to_string());
        m.insert("approved_at".to_string(), "timestamptz".to_string());
        m.insert("published_at".to_string(), "timestamptz".to_string());
        m
    }
    fn search_fields() -> &'static [&'static str] {
        &["statement_number", "name", "currency"]
    }
    fn relations() -> &'static [(&'static str, &'static str, &'static str)] {
        &[("fiscalPeriod", "fiscal_periods", "fiscalPeriodId"), ("comparativePeriod", "fiscal_periods", "comparativePeriodId")]
    }
}

/// Builder for FinancialStatement entity
///
/// Provides a fluent API for constructing FinancialStatement instances.
/// System fields (id, metadata, timestamps) are auto-initialized.
#[derive(Debug, Clone, Default)]
pub struct FinancialStatementBuilder {
    statement_number: Option<String>,
    statement_type: Option<StatementType>,
    name: Option<String>,
    fiscal_period_id: Option<Uuid>,
    fiscal_year: Option<i32>,
    fiscal_month: Option<i32>,
    as_of_date: Option<NaiveDate>,
    period_start: Option<NaiveDate>,
    period_end: Option<NaiveDate>,
    is_comparative: Option<bool>,
    comparative_period_id: Option<Uuid>,
    comparative_as_of_date: Option<NaiveDate>,
    status: Option<StatementStatus>,
    total_current_assets: Option<Decimal>,
    total_non_current_assets: Option<Decimal>,
    total_assets: Option<Decimal>,
    total_current_liabilities: Option<Decimal>,
    total_non_current_liabilities: Option<Decimal>,
    total_liabilities: Option<Decimal>,
    total_equity: Option<Decimal>,
    balance_check: Option<bool>,
    balance_difference: Option<Decimal>,
    total_revenue: Option<Decimal>,
    total_cogs: Option<Decimal>,
    gross_profit: Option<Decimal>,
    gross_profit_margin: Option<Decimal>,
    total_operating_expenses: Option<Decimal>,
    operating_income: Option<Decimal>,
    operating_margin: Option<Decimal>,
    total_other_income: Option<Decimal>,
    total_other_expenses: Option<Decimal>,
    income_before_tax: Option<Decimal>,
    tax_expense: Option<Decimal>,
    net_income: Option<Decimal>,
    net_profit_margin: Option<Decimal>,
    cash_from_operations: Option<Decimal>,
    cash_from_investing: Option<Decimal>,
    cash_from_financing: Option<Decimal>,
    net_cash_change: Option<Decimal>,
    beginning_cash: Option<Decimal>,
    ending_cash: Option<Decimal>,
    total_debits: Option<Decimal>,
    total_credits: Option<Decimal>,
    trial_balance_check: Option<bool>,
    line_items: Option<serde_json::Value>,
    comparative_data: Option<serde_json::Value>,
    variance_data: Option<serde_json::Value>,
    notes: Option<serde_json::Value>,
    management_notes: Option<String>,
    generated_at: Option<DateTime<Utc>>,
    generated_by: Option<Uuid>,
    generation_parameters: Option<serde_json::Value>,
    reviewed_at: Option<DateTime<Utc>>,
    reviewed_by: Option<Uuid>,
    approved_at: Option<DateTime<Utc>>,
    approved_by: Option<Uuid>,
    published_at: Option<DateTime<Utc>>,
    pdf_url: Option<String>,
    excel_url: Option<String>,
    currency: Option<String>,
}

impl FinancialStatementBuilder {
    /// Set the statement_number field (required)
    pub fn statement_number(mut self, value: String) -> Self {
        self.statement_number = Some(value);
        self
    }

    /// Set the statement_type field (required)
    pub fn statement_type(mut self, value: StatementType) -> Self {
        self.statement_type = Some(value);
        self
    }

    /// Set the name field (required)
    pub fn name(mut self, value: String) -> Self {
        self.name = Some(value);
        self
    }

    /// Set the fiscal_period_id field (optional)
    pub fn fiscal_period_id(mut self, value: Uuid) -> Self {
        self.fiscal_period_id = Some(value);
        self
    }

    /// Set the fiscal_year field (required)
    pub fn fiscal_year(mut self, value: i32) -> Self {
        self.fiscal_year = Some(value);
        self
    }

    /// Set the fiscal_month field (optional)
    pub fn fiscal_month(mut self, value: i32) -> Self {
        self.fiscal_month = Some(value);
        self
    }

    /// Set the as_of_date field (required)
    pub fn as_of_date(mut self, value: NaiveDate) -> Self {
        self.as_of_date = Some(value);
        self
    }

    /// Set the period_start field (optional)
    pub fn period_start(mut self, value: NaiveDate) -> Self {
        self.period_start = Some(value);
        self
    }

    /// Set the period_end field (optional)
    pub fn period_end(mut self, value: NaiveDate) -> Self {
        self.period_end = Some(value);
        self
    }

    /// Set the is_comparative field (default: `false`)
    pub fn is_comparative(mut self, value: bool) -> Self {
        self.is_comparative = Some(value);
        self
    }

    /// Set the comparative_period_id field (optional)
    pub fn comparative_period_id(mut self, value: Uuid) -> Self {
        self.comparative_period_id = Some(value);
        self
    }

    /// Set the comparative_as_of_date field (optional)
    pub fn comparative_as_of_date(mut self, value: NaiveDate) -> Self {
        self.comparative_as_of_date = Some(value);
        self
    }

    /// Set the status field (default: `StatementStatus::default()`)
    pub fn status(mut self, value: StatementStatus) -> Self {
        self.status = Some(value);
        self
    }

    /// Set the total_current_assets field (optional)
    pub fn total_current_assets(mut self, value: Decimal) -> Self {
        self.total_current_assets = Some(value);
        self
    }

    /// Set the total_non_current_assets field (optional)
    pub fn total_non_current_assets(mut self, value: Decimal) -> Self {
        self.total_non_current_assets = Some(value);
        self
    }

    /// Set the total_assets field (optional)
    pub fn total_assets(mut self, value: Decimal) -> Self {
        self.total_assets = Some(value);
        self
    }

    /// Set the total_current_liabilities field (optional)
    pub fn total_current_liabilities(mut self, value: Decimal) -> Self {
        self.total_current_liabilities = Some(value);
        self
    }

    /// Set the total_non_current_liabilities field (optional)
    pub fn total_non_current_liabilities(mut self, value: Decimal) -> Self {
        self.total_non_current_liabilities = Some(value);
        self
    }

    /// Set the total_liabilities field (optional)
    pub fn total_liabilities(mut self, value: Decimal) -> Self {
        self.total_liabilities = Some(value);
        self
    }

    /// Set the total_equity field (optional)
    pub fn total_equity(mut self, value: Decimal) -> Self {
        self.total_equity = Some(value);
        self
    }

    /// Set the balance_check field (default: `false`)
    pub fn balance_check(mut self, value: bool) -> Self {
        self.balance_check = Some(value);
        self
    }

    /// Set the balance_difference field (optional)
    pub fn balance_difference(mut self, value: Decimal) -> Self {
        self.balance_difference = Some(value);
        self
    }

    /// Set the total_revenue field (optional)
    pub fn total_revenue(mut self, value: Decimal) -> Self {
        self.total_revenue = Some(value);
        self
    }

    /// Set the total_cogs field (optional)
    pub fn total_cogs(mut self, value: Decimal) -> Self {
        self.total_cogs = Some(value);
        self
    }

    /// Set the gross_profit field (optional)
    pub fn gross_profit(mut self, value: Decimal) -> Self {
        self.gross_profit = Some(value);
        self
    }

    /// Set the gross_profit_margin field (optional)
    pub fn gross_profit_margin(mut self, value: Decimal) -> Self {
        self.gross_profit_margin = Some(value);
        self
    }

    /// Set the total_operating_expenses field (optional)
    pub fn total_operating_expenses(mut self, value: Decimal) -> Self {
        self.total_operating_expenses = Some(value);
        self
    }

    /// Set the operating_income field (optional)
    pub fn operating_income(mut self, value: Decimal) -> Self {
        self.operating_income = Some(value);
        self
    }

    /// Set the operating_margin field (optional)
    pub fn operating_margin(mut self, value: Decimal) -> Self {
        self.operating_margin = Some(value);
        self
    }

    /// Set the total_other_income field (optional)
    pub fn total_other_income(mut self, value: Decimal) -> Self {
        self.total_other_income = Some(value);
        self
    }

    /// Set the total_other_expenses field (optional)
    pub fn total_other_expenses(mut self, value: Decimal) -> Self {
        self.total_other_expenses = Some(value);
        self
    }

    /// Set the income_before_tax field (optional)
    pub fn income_before_tax(mut self, value: Decimal) -> Self {
        self.income_before_tax = Some(value);
        self
    }

    /// Set the tax_expense field (optional)
    pub fn tax_expense(mut self, value: Decimal) -> Self {
        self.tax_expense = Some(value);
        self
    }

    /// Set the net_income field (optional)
    pub fn net_income(mut self, value: Decimal) -> Self {
        self.net_income = Some(value);
        self
    }

    /// Set the net_profit_margin field (optional)
    pub fn net_profit_margin(mut self, value: Decimal) -> Self {
        self.net_profit_margin = Some(value);
        self
    }

    /// Set the cash_from_operations field (optional)
    pub fn cash_from_operations(mut self, value: Decimal) -> Self {
        self.cash_from_operations = Some(value);
        self
    }

    /// Set the cash_from_investing field (optional)
    pub fn cash_from_investing(mut self, value: Decimal) -> Self {
        self.cash_from_investing = Some(value);
        self
    }

    /// Set the cash_from_financing field (optional)
    pub fn cash_from_financing(mut self, value: Decimal) -> Self {
        self.cash_from_financing = Some(value);
        self
    }

    /// Set the net_cash_change field (optional)
    pub fn net_cash_change(mut self, value: Decimal) -> Self {
        self.net_cash_change = Some(value);
        self
    }

    /// Set the beginning_cash field (optional)
    pub fn beginning_cash(mut self, value: Decimal) -> Self {
        self.beginning_cash = Some(value);
        self
    }

    /// Set the ending_cash field (optional)
    pub fn ending_cash(mut self, value: Decimal) -> Self {
        self.ending_cash = Some(value);
        self
    }

    /// Set the total_debits field (optional)
    pub fn total_debits(mut self, value: Decimal) -> Self {
        self.total_debits = Some(value);
        self
    }

    /// Set the total_credits field (optional)
    pub fn total_credits(mut self, value: Decimal) -> Self {
        self.total_credits = Some(value);
        self
    }

    /// Set the trial_balance_check field (default: `false`)
    pub fn trial_balance_check(mut self, value: bool) -> Self {
        self.trial_balance_check = Some(value);
        self
    }

    /// Set the line_items field (default: `serde_json::json!([])`)
    pub fn line_items(mut self, value: serde_json::Value) -> Self {
        self.line_items = Some(value);
        self
    }

    /// Set the comparative_data field (default: `serde_json::json!({})`)
    pub fn comparative_data(mut self, value: serde_json::Value) -> Self {
        self.comparative_data = Some(value);
        self
    }

    /// Set the variance_data field (default: `serde_json::json!({})`)
    pub fn variance_data(mut self, value: serde_json::Value) -> Self {
        self.variance_data = Some(value);
        self
    }

    /// Set the notes field (default: `serde_json::json!([])`)
    pub fn notes(mut self, value: serde_json::Value) -> Self {
        self.notes = Some(value);
        self
    }

    /// Set the management_notes field (optional)
    pub fn management_notes(mut self, value: String) -> Self {
        self.management_notes = Some(value);
        self
    }

    /// Set the generated_at field (optional)
    pub fn generated_at(mut self, value: DateTime<Utc>) -> Self {
        self.generated_at = Some(value);
        self
    }

    /// Set the generated_by field (optional)
    pub fn generated_by(mut self, value: Uuid) -> Self {
        self.generated_by = Some(value);
        self
    }

    /// Set the generation_parameters field (default: `serde_json::json!({})`)
    pub fn generation_parameters(mut self, value: serde_json::Value) -> Self {
        self.generation_parameters = Some(value);
        self
    }

    /// Set the reviewed_at field (optional)
    pub fn reviewed_at(mut self, value: DateTime<Utc>) -> Self {
        self.reviewed_at = Some(value);
        self
    }

    /// Set the reviewed_by field (optional)
    pub fn reviewed_by(mut self, value: Uuid) -> Self {
        self.reviewed_by = Some(value);
        self
    }

    /// Set the approved_at field (optional)
    pub fn approved_at(mut self, value: DateTime<Utc>) -> Self {
        self.approved_at = Some(value);
        self
    }

    /// Set the approved_by field (optional)
    pub fn approved_by(mut self, value: Uuid) -> Self {
        self.approved_by = Some(value);
        self
    }

    /// Set the published_at field (optional)
    pub fn published_at(mut self, value: DateTime<Utc>) -> Self {
        self.published_at = Some(value);
        self
    }

    /// Set the pdf_url field (optional)
    pub fn pdf_url(mut self, value: String) -> Self {
        self.pdf_url = Some(value);
        self
    }

    /// Set the excel_url field (optional)
    pub fn excel_url(mut self, value: String) -> Self {
        self.excel_url = Some(value);
        self
    }

    /// Set the currency field (default: `"IDR".to_string()`)
    pub fn currency(mut self, value: String) -> Self {
        self.currency = Some(value);
        self
    }

    /// Build the FinancialStatement entity
    ///
    /// Returns Err if any required field without a default is missing.
    pub fn build(self) -> Result<FinancialStatement, String> {
        let statement_number = self.statement_number.ok_or_else(|| "statement_number is required".to_string())?;
        let statement_type = self.statement_type.ok_or_else(|| "statement_type is required".to_string())?;
        let name = self.name.ok_or_else(|| "name is required".to_string())?;
        let fiscal_year = self.fiscal_year.ok_or_else(|| "fiscal_year is required".to_string())?;
        let as_of_date = self.as_of_date.ok_or_else(|| "as_of_date is required".to_string())?;

        Ok(FinancialStatement {
            id: Uuid::new_v4(),
            statement_number,
            statement_type,
            name,
            fiscal_period_id: self.fiscal_period_id,
            fiscal_year,
            fiscal_month: self.fiscal_month,
            as_of_date,
            period_start: self.period_start,
            period_end: self.period_end,
            is_comparative: self.is_comparative.unwrap_or(false),
            comparative_period_id: self.comparative_period_id,
            comparative_as_of_date: self.comparative_as_of_date,
            status: self.status.unwrap_or_default(),
            total_current_assets: self.total_current_assets,
            total_non_current_assets: self.total_non_current_assets,
            total_assets: self.total_assets,
            total_current_liabilities: self.total_current_liabilities,
            total_non_current_liabilities: self.total_non_current_liabilities,
            total_liabilities: self.total_liabilities,
            total_equity: self.total_equity,
            balance_check: self.balance_check.unwrap_or(false),
            balance_difference: self.balance_difference,
            total_revenue: self.total_revenue,
            total_cogs: self.total_cogs,
            gross_profit: self.gross_profit,
            gross_profit_margin: self.gross_profit_margin,
            total_operating_expenses: self.total_operating_expenses,
            operating_income: self.operating_income,
            operating_margin: self.operating_margin,
            total_other_income: self.total_other_income,
            total_other_expenses: self.total_other_expenses,
            income_before_tax: self.income_before_tax,
            tax_expense: self.tax_expense,
            net_income: self.net_income,
            net_profit_margin: self.net_profit_margin,
            cash_from_operations: self.cash_from_operations,
            cash_from_investing: self.cash_from_investing,
            cash_from_financing: self.cash_from_financing,
            net_cash_change: self.net_cash_change,
            beginning_cash: self.beginning_cash,
            ending_cash: self.ending_cash,
            total_debits: self.total_debits,
            total_credits: self.total_credits,
            trial_balance_check: self.trial_balance_check.unwrap_or(false),
            line_items: self.line_items.unwrap_or(serde_json::json!([])),
            comparative_data: self.comparative_data.unwrap_or(serde_json::json!({})),
            variance_data: self.variance_data.unwrap_or(serde_json::json!({})),
            notes: self.notes.unwrap_or(serde_json::json!([])),
            management_notes: self.management_notes,
            generated_at: self.generated_at,
            generated_by: self.generated_by,
            generation_parameters: self.generation_parameters.unwrap_or(serde_json::json!({})),
            reviewed_at: self.reviewed_at,
            reviewed_by: self.reviewed_by,
            approved_at: self.approved_at,
            approved_by: self.approved_by,
            published_at: self.published_at,
            pdf_url: self.pdf_url,
            excel_url: self.excel_url,
            currency: self.currency.unwrap_or("IDR".to_string()),
            metadata: AuditMetadata::default(),
        })
    }
}