Skip to content

Commit

Permalink
برمجة أسعار الصرف بطريقة الاستعلامات المباشرة في نموذج قواعد البيانات
Browse files Browse the repository at this point in the history
  • Loading branch information
vzool committed Nov 13, 2024
1 parent 86fb7ce commit 504bda4
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 21 deletions.
Binary file modified analysis/database/zakat-pony.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified analysis/database/zakat.pdf
Binary file not shown.
Binary file modified analysis/database/zakat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion analysis/database/zakat.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
72 changes: 52 additions & 20 deletions zakat/zakat_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2445,6 +2445,7 @@ class Exchange(db.Entity):
record_date = pony.Required(datetime.datetime, unique=True)
rate = pony.Required(Decimal)
desc = pony.Optional(pony.LongStr)
created_at = pony.Required(datetime.datetime, default=lambda: datetime.datetime.now())


class Report(db.Entity):
Expand Down Expand Up @@ -2832,12 +2833,24 @@ def _set_exchange(self, account: int, created: str = None, rate: float = None, d
return False
if not self._account_exists(account):
self._track(account=account, debug=debug)
Exchange(
account=account,
rate=rate,
desc=description if description else '',
record_date=Helper.time_to_datetime(created),
)
if self.raw_sql:
db.execute(f'''
INSERT INTO exchange (account_id, record_date, rate, desc, created_at)
VALUES(
{account},
"{created}",
{rate},
"{description if description else ''}",
"{str(datetime.datetime.now())}"
);
''')
else:
Exchange(
account=account,
rate=rate,
desc=description if description else '',
record_date=Helper.time_to_datetime(created),
)
if debug:
print("exchange-created-1",
f'account: {account}, created: {created}, rate:{rate}, description:{description}')
Expand All @@ -2854,19 +2867,38 @@ def _exchange(self, account: int, created: str = None, debug: bool = False) -> d
raise ValueError(f'The account must be an integer, {type(account)} was provided.')
if created is None:
created = Helper.time()
exchange = Exchange.select(
lambda e: e.account.id == account and e.time <= created
).order_by(pony.desc(Exchange.time)).first()
if debug:
print('valid_rates', exchange, type(exchange), exchange)
if exchange:
if self.raw_sql:
x = db.execute(f'''
SELECT record_date, rate, desc
FROM exchange
WHERE account_id = {account} AND
datetime(record_date) <= datetime("{created}")
ORDER BY record_date DESC
LIMIT 1;
''')
exchange = x.fetchone()
if debug:
print("exchange-read-1", f'account={account}, created={created}, latest_rate={exchange}')
return {
"time": exchange.record_date,
"rate": exchange.rate,
"description": exchange.desc if exchange.desc else None,
}
print('valid_rates', exchange, type(exchange), exchange)
if exchange:
return {
"time": exchange[0],
"rate": exchange[1],
"description": exchange[2] if exchange[2] else None,
}
else:
exchange = Exchange.select(
lambda e: e.account.id == account and e.record_date <= created
).order_by(pony.desc(Exchange.record_date)).first()
if debug:
print('valid_rates', exchange, type(exchange), exchange)
if exchange:
if debug:
print("exchange-read-1", f'account={account}, created={created}, latest_rate={exchange}')
return {
"time": exchange.record_date,
"rate": exchange.rate,
"description": exchange.desc if exchange.desc else None,
}
if debug:
print("exchange-read-0", f'account: {account}, created: {created}')
return {"time": created, "rate": 1, "description": None} # إرجاع القيمة الافتراضية مع وصف فارغ
Expand All @@ -2879,7 +2911,7 @@ def _exchanges(self, account: int) -> dict | None:
if self._account_exists(account):
result = {}
for exchange in Exchange.select(lambda e: e.account.id == account)[:]:
result[exchange.time] = exchange.to_dict()
result[exchange.record_date] = exchange.to_dict()
if result:
return result
return None
Expand Down Expand Up @@ -2946,7 +2978,7 @@ def _transfer(self, unscaled_amount: float | int | Decimal, from_account: int, t
if debug:
print('target_amount', target_amount)
# Perform the transfer
box = Box.get(account=to_account, time=age)
box = Box.get(account=to_account, record_date=age)
if debug:
print('box_exists', age)
if box:
Expand Down

0 comments on commit 504bda4

Please sign in to comment.