-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
591 lines (450 loc) · 20.4 KB
/
models.py
File metadata and controls
591 lines (450 loc) · 20.4 KB
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
from datetime import datetime, date
from werkzeug.security import generate_password_hash, check_password_hash
from decimal import Decimal
from sqlalchemy import (
Integer,
String,
Enum,
ForeignKey,
Date,
Numeric,
Float,
Text,
DateTime,
func,
UniqueConstraint,
Index,
select,
and_,
CheckConstraint,
Boolean
)
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship, Session
import enum
from typing import Optional
import re
class Base(DeclarativeBase):
pass
# --- Facility levels (hospitals / health centres) ---
class FacilityLevelEnum(str, enum.Enum):
NATIONAL_REFERRAL = "National Referral Hospital"
PROVINCIAL_REFERRAL = "Province Referral Hospital"
DISTRICT_HOSPITAL = "District Hospital"
HEALTH_CENTRE = "Health Centre"
# --- Geo ---
class Country(Base):
__tablename__ = "country"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
name: Mapped[str] = mapped_column(String(120), unique=True, nullable=False)
code: Mapped[str] = mapped_column(String(10), unique=True, nullable=False)
provinces: Mapped[list["Province"]] = relationship(
"Province", back_populates="country", cascade="all,delete-orphan"
)
class Province(Base):
__tablename__ = "province"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
name: Mapped[str] = mapped_column(String(120), nullable=False)
code: Mapped[str] = mapped_column(String(10), nullable=False)
country_id: Mapped[int] = mapped_column(ForeignKey("country.id"), nullable=False)
country: Mapped[Country] = relationship("Country", back_populates="provinces")
districts: Mapped[list["District"]] = relationship(
"District", back_populates="province", cascade="all,delete-orphan"
)
class District(Base):
__tablename__ = "district"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
name: Mapped[str] = mapped_column(String(120), nullable=False)
code: Mapped[str] = mapped_column(String(10), nullable=False)
province_id: Mapped[int] = mapped_column(ForeignKey("province.id"), nullable=False)
province: Mapped[Province] = relationship("Province", back_populates="districts")
class Hospital(Base):
__tablename__ = "hospital"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
name: Mapped[str] = mapped_column(String(160), nullable=False)
code: Mapped[str] = mapped_column(String(20), unique=True, nullable=False)
level: Mapped[FacilityLevelEnum] = mapped_column(Enum(FacilityLevelEnum), nullable=False)
province_id: Mapped[int] = mapped_column(ForeignKey("province.id"), nullable=False)
district_id: Mapped[int] = mapped_column(ForeignKey("district.id"), nullable=False)
province: Mapped[Province] = relationship()
district: Mapped[District] = relationship()
class Facility(Base):
__tablename__ = "facility"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
name: Mapped[str] = mapped_column(String(160), nullable=False)
code: Mapped[str] = mapped_column(String(20), unique=True, nullable=False)
level: Mapped[FacilityLevelEnum] = mapped_column(Enum(FacilityLevelEnum), nullable=False)
country_id: Mapped[int] = mapped_column(ForeignKey("country.id"), nullable=False)
province_id: Mapped[int] = mapped_column(ForeignKey("province.id"), nullable=False)
district_id: Mapped[int] = mapped_column(ForeignKey("district.id"), nullable=False)
referral_hospital_id: Mapped[int | None] = mapped_column(ForeignKey("hospital.id"), nullable=True)
country: Mapped[Country] = relationship()
province: Mapped[Province] = relationship()
district: Mapped[District] = relationship()
referral_hospital: Mapped[Hospital] = relationship()
# --- Budget catalogue ---
class BudgetLine(Base):
__tablename__ = "budget_lines"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
code: Mapped[str] = mapped_column(String(64), unique=True, nullable=False, index=True)
name: Mapped[str] = mapped_column(String(255), nullable=False)
description: Mapped[str | None] = mapped_column(Text)
activities: Mapped[list["Activity"]] = relationship(
"Activity",
back_populates="budget_line",
cascade="all, delete-orphan",
)
def __repr__(self):
return f"<BudgetLine {self.code} - {self.name}>"
class Activity(Base):
__tablename__ = "activities"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
budget_line_id: Mapped[int] = mapped_column(
Integer,
ForeignKey("budget_lines.id", ondelete="CASCADE"),
nullable=False,
index=True,
)
code: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
name: Mapped[str] = mapped_column(String(255), nullable=False)
description: Mapped[str | None] = mapped_column(Text)
budget_line: Mapped["BudgetLine"] = relationship("BudgetLine", back_populates="activities")
__table_args__ = (
Index("ix_activity_unique_per_line", "budget_line_id", "code", unique=True),
)
def __repr__(self):
return f"<Activity {self.code} - {self.name}>"
# --- Budget records ---
class Budget(Base):
__tablename__ = "budgets"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
hospital_id: Mapped[int | None] = mapped_column(Integer, ForeignKey("hospital.id"), index=True)
facility_id: Mapped[int | None] = mapped_column(Integer, ForeignKey("facility.id"), index=True)
budget_line_id: Mapped[int] = mapped_column(Integer, ForeignKey("budget_lines.id"), nullable=False, index=True)
activity_id: Mapped[int] = mapped_column(Integer, ForeignKey("activities.id"), nullable=False, index=True)
activity_description: Mapped[str | None] = mapped_column(Text)
level: Mapped[str | None] = mapped_column(String(64), index=True)
estimated_number_quantity: Mapped[float | None] = mapped_column(Float)
estimated_frequency_occurrence: Mapped[float | None] = mapped_column(Float)
unit_price_usd: Mapped[Numeric | None] = mapped_column(Numeric(14, 2))
cost_per_unit_rwf: Mapped[Numeric | None] = mapped_column(Numeric(14, 2))
percent_effort_share: Mapped[float | None] = mapped_column(Float)
component_1: Mapped[Numeric | None] = mapped_column(Numeric(14, 2))
component_2: Mapped[Numeric | None] = mapped_column(Numeric(14, 2))
component_3: Mapped[Numeric | None] = mapped_column(Numeric(14, 2))
component_4: Mapped[Numeric | None] = mapped_column(Numeric(14, 2))
start_date: Mapped[date] = mapped_column(Date, nullable=False, index=True)
end_date: Mapped[date] = mapped_column(Date, nullable=False, index=True)
budget_year: Mapped[str] = mapped_column(String(20), nullable=False, index=True)
is_validated: Mapped[bool] = mapped_column(
Boolean,
nullable=False,
default=False,
server_default="false",
index=True,
)
validated_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
validated_by_id: Mapped[int | None] = mapped_column(
ForeignKey("auth_user.id")
)
validated_by: Mapped["User | None"] = relationship()
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
budget_line: Mapped["BudgetLine"] = relationship("BudgetLine")
activity: Mapped["Activity"] = relationship("Activity")
def __repr__(self):
return f"<Budget id={self.id} BL={self.budget_line_id} ACT={self.activity_id}>"
@classmethod
def prepare_for_insert(cls, budget: "Budget"):
if not budget.start_date or not budget.end_date:
raise ValueError("start_date and end_date are required")
budget.budget_year = compute_budget_year(
budget.start_date,
budget.end_date
)
@classmethod
def validate_budget(cls, sess, budget_id: int, user_id: int):
budget = sess.get(cls, budget_id)
if not budget:
raise ValueError("Budget not found")
if budget.is_validated:
raise ValueError("Budget already validated")
budget.is_validated = True
budget.validated_at = datetime.utcnow()
budget.validated_by_id = user_id
sess.flush()
# --- Quarter, cashbook entry, obligations etc (old model) ---
class Quarter(Base):
__tablename__ = "quarter"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
facility_id: Mapped[int] = mapped_column(ForeignKey("facility.id"), nullable=False)
year: Mapped[int] = mapped_column(Integer, nullable=False)
quarter: Mapped[int] = mapped_column(Integer, nullable=False) # 1..4
reporting_period: Mapped[str | None] = mapped_column(String(120))
status: Mapped[str | None] = mapped_column(String(50))
facility = relationship("Facility")
class QuarterLine(Base):
__tablename__ = "quarter_line"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
quarter_id: Mapped[int] = mapped_column(ForeignKey("quarter.id"), nullable=False)
planned: Mapped[float | None] = mapped_column(Numeric(16, 2))
actual: Mapped[float | None] = mapped_column(Numeric(16, 2))
variance: Mapped[float | None] = mapped_column(Numeric(16, 2))
comments: Mapped[str | None] = mapped_column(Text)
quarter = relationship("Quarter")
class CashbookEntry(Base):
__tablename__ = "cashbook_entry"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
facility_id: Mapped[int] = mapped_column(ForeignKey("facility.id"), nullable=False)
year: Mapped[int] = mapped_column(Integer, nullable=False)
quarter: Mapped[int] = mapped_column(Integer, nullable=False)
txn_date: Mapped[Date] = mapped_column(Date)
reference: Mapped[str | None] = mapped_column(String(120))
description: Mapped[str | None] = mapped_column(String(400))
inflow: Mapped[float | None] = mapped_column(Numeric(16, 2))
outflow: Mapped[float | None] = mapped_column(Numeric(16, 2))
balance: Mapped[float | None] = mapped_column(Numeric(16, 2))
facility = relationship("Facility")
class Obligation(Base):
__tablename__ = "obligation"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
facility_id: Mapped[int] = mapped_column(ForeignKey("facility.id"), nullable=False)
year: Mapped[int] = mapped_column(Integer, nullable=False)
quarter: Mapped[int] = mapped_column(Integer, nullable=False)
vendor: Mapped[str | None] = mapped_column(String(200))
invoice_no: Mapped[str | None] = mapped_column(String(120))
description: Mapped[str | None] = mapped_column(String(400))
amount: Mapped[float | None] = mapped_column(Numeric(16, 2))
status: Mapped[str | None] = mapped_column(String(50))
facility = relationship("Facility")
class Reallocation(Base):
__tablename__ = "reallocation"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
facility_id: Mapped[int] = mapped_column(ForeignKey("facility.id"), nullable=False)
date: Mapped[Date | None] = mapped_column(Date)
from_budget_line_id: Mapped[int | None] = mapped_column(ForeignKey("budget_lines.id"))
to_budget_line_id: Mapped[int | None] = mapped_column(ForeignKey("budget_lines.id"))
amount: Mapped[float | None] = mapped_column(Numeric(16, 2))
reason: Mapped[str | None] = mapped_column(Text)
class Redirection(Base):
__tablename__ = "redirection"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
facility_id: Mapped[int] = mapped_column(ForeignKey("facility.id"), nullable=False)
date: Mapped[Date | None] = mapped_column(Date)
from_component: Mapped[str | None] = mapped_column(String(120))
to_component: Mapped[str | None] = mapped_column(String(120))
amount: Mapped[float | None] = mapped_column(Numeric(16, 2))
reason: Mapped[str | None] = mapped_column(Text)
# ---- Enums ----
class VATRequirementEnum(enum.Enum):
REQUIRED = "VAT_REQUIRED"
NOT_REQUIRED = "VAT_NOT_REQUIRED"
class QuarterEnum(enum.Enum):
Q1 = "Q1" # Oct-Dec
Q2 = "Q2" # Jan-Mar
Q3 = "Q3" # Apr-Jun
Q4 = "Q4" # Jul-Sep
class AccountTypeEnum(enum.Enum):
BANK = "BANK"
MOBILE_MONEY = "MOBILE_MONEY"
CASH = "CASH"
class AccessLevelEnum(str, enum.Enum):
COUNTRY = "COUNTRY" # can see all data
PROVINCE = "PROVINCE" # all data in that province
DISTRICT = "DISTRICT"
HOSPITAL = "HOSPITAL" # (reserved for future finer-grain)
FACILITY = "FACILITY" # restricted to one facility
# ---- Helpers ----
def quarter_from_date(d: date) -> QuarterEnum:
# Fiscal year starts in October
if d.month in (10, 11, 12):
return QuarterEnum.Q1
if d.month in (1, 2, 3):
return QuarterEnum.Q2
if d.month in (4, 5, 6):
return QuarterEnum.Q3
return QuarterEnum.Q4 # (7, 8, 9)
def initials_from_name(name: str) -> str:
cleaned = re.sub(r"[^A-Za-z]", "", name or "").upper()
if not cleaned:
return "XXX"
parts = re.split(r"[AEIOU]+", cleaned)
letters = "".join(p[0] for p in parts if p)[:3]
if len(letters) < 3:
letters = (cleaned[:3]).ljust(3, "X")
return letters
# ---- Account ----
class Account(Base):
__tablename__ = "account"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
name: Mapped[str] = mapped_column(String(160), nullable=False)
type: Mapped[AccountTypeEnum] = mapped_column(Enum(AccountTypeEnum), nullable=False)
bank_name: Mapped[Optional[str]] = mapped_column(String(160))
account_number: Mapped[Optional[str]] = mapped_column(String(64), index=True)
mobile_provider: Mapped[Optional[str]] = mapped_column(String(64))
facility_id: Mapped[int | None] = mapped_column(ForeignKey("facility.id"), nullable=True)
hospital_id: Mapped[int | None] = mapped_column(ForeignKey("hospital.id"), nullable=True)
current_balance: Mapped[Optional[Numeric]] = mapped_column(Numeric(14, 2), default=0)
facility: Mapped[Optional[Facility]] = relationship()
hospital: Mapped[Optional[Hospital]] = relationship()
# ---- Cashbook ----
class Cashbook(Base):
__tablename__ = "cashbook"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
transaction_date: Mapped[date] = mapped_column(Date, nullable=False, index=True)
quarter: Mapped[QuarterEnum] = mapped_column(
Enum(QuarterEnum), nullable=False, index=True
)
hospital_id: Mapped[int | None] = mapped_column(
ForeignKey("hospital.id"), nullable=True, index=True
)
facility_id: Mapped[int | None] = mapped_column(
ForeignKey("facility.id"), nullable=True, index=True
)
account_id: Mapped[int] = mapped_column(
ForeignKey("account.id"), nullable=False, index=True
)
reference: Mapped[str] = mapped_column(
String(40), unique=True, nullable=False, index=True
)
vat_requirement: Mapped[VATRequirementEnum] = mapped_column(
Enum(VATRequirementEnum), nullable=False
)
description: Mapped[str | None] = mapped_column(Text)
budget_line_id: Mapped[int] = mapped_column(
ForeignKey("budget_lines.id"), nullable=False, index=True
)
activity_id: Mapped[int] = mapped_column(
ForeignKey("activities.id"), nullable=False, index=True
)
cash_in: Mapped[Decimal | None] = mapped_column(Numeric(14, 2), nullable=True)
cash_out: Mapped[Decimal | None] = mapped_column(Numeric(14, 2), nullable=True)
balance: Mapped[Decimal] = mapped_column(Numeric(14, 2), nullable=False)
created_at: Mapped[date] = mapped_column(
Date, server_default=func.current_date(), nullable=False
)
updated_at: Mapped[date] = mapped_column(
Date,
server_default=func.current_date(),
onupdate=func.current_date(),
nullable=False,
)
hospital: Mapped[Hospital | None] = relationship()
facility: Mapped[Facility | None] = relationship()
account: Mapped[Account] = relationship()
budget_line: Mapped[BudgetLine] = relationship()
activity: Mapped[Activity] = relationship()
__table_args__ = (
CheckConstraint(
"(cash_in IS NULL) != (cash_out IS NULL)",
name="ck_cash_in_xor_cash_out",
),
CheckConstraint(
"(cash_in IS NULL OR cash_in >= 0) "
"AND (cash_out IS NULL OR cash_out >= 0)",
name="ck_cash_amounts_positive",
),
CheckConstraint(
"(hospital_id IS NOT NULL) OR (facility_id IS NOT NULL)",
name="ck_org_one_present",
),
)
@classmethod
def _quarter_from_date(cls, dt: date) -> str:
if dt is None:
raise ValueError("transaction_date is required to determine quarter")
m = dt.month
if 1 <= m <= 3:
return "Q2"
if 4 <= m <= 6:
return "Q3"
if 7 <= m <= 9:
return "Q4"
return "Q1"
@classmethod
def set_quarter(cls, cb: "Cashbook") -> None:
if cb.transaction_date is None:
raise ValueError("transaction_date is required to determine quarter")
q_code = cls._quarter_from_date(cb.transaction_date)
try:
cb.quarter = QuarterEnum[q_code]
except KeyError:
cb.quarter = QuarterEnum(q_code)
@classmethod
def prepare_for_insert(cls, sess: Session, cb: "Cashbook") -> None:
if cb.quarter is None:
cls.set_quarter(cb)
elif isinstance(cb.quarter, str):
try:
cb.quarter = QuarterEnum[cb.quarter]
except KeyError:
cb.quarter = QuarterEnum(cb.quarter)
if not cb.reference:
cb.reference = cls._generate_reference(sess, cb)
if cb.balance is None:
last = (
sess.query(cls)
.filter(cls.account_id == cb.account_id)
.order_by(cls.transaction_date.desc(), cls.id.desc())
.first()
)
prev = Decimal(last.balance) if last else Decimal("0")
ci = Decimal(cb.cash_in or 0)
co = Decimal(cb.cash_out or 0)
cb.balance = prev + ci - co
@classmethod
def recalc_account_balances(cls, sess: Session, account_id: int) -> None:
rows: list[Cashbook] = (
sess.query(cls)
.filter(cls.account_id == account_id)
.order_by(cls.transaction_date.asc(), cls.id.asc())
.all()
)
running = Decimal("0")
for r in rows:
ci = Decimal(r.cash_in or 0)
co = Decimal(r.cash_out or 0)
running += ci - co
r.balance = running
@classmethod
def _generate_reference(cls, sess: Session, cb: "Cashbook") -> str:
prefix = "CBK"
d = cb.transaction_date.strftime("%Y%m%d")
count = (
sess.query(cls)
.filter(
cls.account_id == cb.account_id,
cls.transaction_date == cb.transaction_date,
)
.count()
)
return f"{prefix}-{d}-{count + 1:04d}"
# ---- Users & auth ----
class User(Base):
__tablename__ = "auth_user"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
username: Mapped[str] = mapped_column(String(120), unique=True, nullable=False)
password_hash: Mapped[str] = mapped_column(String(255), nullable=False)
access_level: Mapped[AccessLevelEnum] = mapped_column(
Enum(AccessLevelEnum),
nullable=False,
default=AccessLevelEnum.FACILITY,
)
country_id: Mapped[int | None] = mapped_column(ForeignKey("country.id"), nullable=True)
hospital_id: Mapped[int | None] = mapped_column(ForeignKey("hospital.id"), nullable=True)
facility_id: Mapped[int | None] = mapped_column(ForeignKey("facility.id"), nullable=True)
country: Mapped[Country | None] = relationship("Country")
hospital: Mapped[Hospital | None] = relationship("Hospital")
facility: Mapped[Facility | None] = relationship("Facility")
def set_password(self, raw: str):
self.password_hash = generate_password_hash(raw)
def check_password(self, raw: str) -> bool:
return check_password_hash(self.password_hash, raw)
class TokenBlocklist(Base):
__tablename__ = "token_blocklist"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
jti: Mapped[str] = mapped_column(String(36), nullable=False, unique=True) # JWT ID
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
__table_args__ = (UniqueConstraint("jti", name="uq_tokenblocklist_jti"),)
def compute_budget_year(start: date, end: date) -> str:
if start.year == end.year:
return str(start.year)
return f"{start.year}-{end.year}"