Tahersaeedi commited on
Commit
0a7a9c8
·
verified ·
1 Parent(s): 781397a

Update app.py from anycoder

Browse files
Files changed (1) hide show
  1. app.py +170 -0
app.py ADDED
@@ -0,0 +1,170 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sqlite3
2
+ import datetime
3
+ import os
4
+
5
+ DB_NAME = "guarantee_manager.db"
6
+
7
+ def get_db_connection():
8
+ conn = sqlite3.connect(DB_NAME)
9
+ conn.row_factory = sqlite3.Row
10
+ return conn
11
+
12
+ def init_db():
13
+ conn = get_db_connection()
14
+ c = conn.cursor()
15
+
16
+ # جدول کاربران (برای لاگین ساده)
17
+ c.execute('''
18
+ CREATE TABLE IF NOT EXISTS users (
19
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
20
+ username TEXT UNIQUE NOT NULL,
21
+ password TEXT NOT NULL
22
+ )
23
+ ''')
24
+
25
+ # جدول پیمانکاران
26
+ c.execute('''
27
+ CREATE TABLE IF NOT EXISTS contractors (
28
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
29
+ company_name TEXT NOT NULL,
30
+ national_id TEXT,
31
+ phone TEXT,
32
+ address TEXT
33
+ )
34
+ ''')
35
+
36
+ # جدول ضمانت‌نامه‌ها
37
+ c.execute('''
38
+ CREATE TABLE IF NOT EXISTS guarantees (
39
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
40
+ guarantee_number TEXT UNIQUE NOT NULL,
41
+ type TEXT NOT NULL,
42
+ bank_name TEXT NOT NULL,
43
+ branch_name TEXT,
44
+ amount REAL NOT NULL,
45
+ issue_date TEXT NOT NULL,
46
+ due_date TEXT NOT NULL,
47
+ status TEXT NOT NULL,
48
+ contractor_id INTEGER,
49
+ contract_info TEXT,
50
+ description TEXT,
51
+ file_path TEXT,
52
+ FOREIGN KEY (contractor_id) REFERENCES contractors (id)
53
+ )
54
+ ''')
55
+
56
+ # ایجاد کاربر پیش‌فرض اگر وجود ندارد (admin/admin)
57
+ # در نسخه واقعی باید پسورد هش شود
58
+ c.execute("SELECT * FROM users WHERE username = 'admin'")
59
+ if not c.fetchone():
60
+ c.execute("INSERT INTO users (username, password) VALUES ('admin', 'admin')")
61
+
62
+ conn.commit()
63
+ conn.close()
64
+
65
+ # --- توابع پیمانکاران ---
66
+ def add_contractor(name, nid, phone, address):
67
+ conn = get_db_connection()
68
+ try:
69
+ conn.execute("INSERT INTO contractors (company_name, national_id, phone, address) VALUES (?, ?, ?, ?)",
70
+ (name, nid, phone, address))
71
+ conn.commit()
72
+ return True, "پیمانکار با موفقیت افزوده شد."
73
+ except Exception as e:
74
+ return False, str(e)
75
+ finally:
76
+ conn.close()
77
+
78
+ def get_contractors_list():
79
+ conn = get_db_connection()
80
+ rows = conn.execute("SELECT id, company_name FROM contractors").fetchall()
81
+ conn.close()
82
+ return [(r["company_name"], r["id"]) for r in rows]
83
+
84
+ def get_all_contractors_df():
85
+ conn = get_db_connection()
86
+ rows = conn.execute("SELECT id, company_name, national_id, phone, address FROM contractors").fetchall()
87
+ conn.close()
88
+ return [list(r) for r in rows]
89
+
90
+ # --- توابع ضمانت‌نامه‌ها ---
91
+ def add_guarantee(g_num, g_type, bank, branch, amount, i_date, d_date, status, c_id, c_info, desc, f_path):
92
+ conn = get_db_connection()
93
+ try:
94
+ conn.execute('''
95
+ INSERT INTO guarantees
96
+ (guarantee_number, type, bank_name, branch_name, amount, issue_date, due_date, status, contractor_id, contract_info, description, file_path)
97
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
98
+ ''', (g_num, g_type, bank, branch, amount, i_date, d_date, status, c_id, c_info, desc, f_path))
99
+ conn.commit()
100
+ return True, "ضمانت‌نامه ذخیره شد."
101
+ except sqlite3.IntegrityError:
102
+ return False, "شماره ضمانت‌نامه تکراری است."
103
+ except Exception as e:
104
+ return False, str(e)
105
+ finally:
106
+ conn.close()
107
+
108
+ def search_guarantees(search_query="", status_filter=None):
109
+ conn = get_db_connection()
110
+ query = '''
111
+ SELECT g.id, g.guarantee_number, g.type, g.bank_name, g.amount, g.due_date, g.status, c.company_name, g.contract_info
112
+ FROM guarantees g
113
+ LEFT JOIN contractors c ON g.contractor_id = c.id
114
+ WHERE 1=1
115
+ '''
116
+ params = []
117
+
118
+ if status_filter and status_filter != "همه":
119
+ query += " AND g.status = ?"
120
+ params.append(status_filter)
121
+
122
+ if search_query:
123
+ query += " AND (g.guarantee_number LIKE ? OR c.company_name LIKE ? OR g.bank_name LIKE ?)"
124
+ params.append(f"%{search_query}%")
125
+ params.append(f"%{search_query}%")
126
+ params.append(f"%{search_query}%")
127
+
128
+ rows = conn.execute(query, params).fetchall()
129
+ conn.close()
130
+ return [list(r) for r in rows]
131
+
132
+ def get_alerts(days_threshold=30):
133
+ conn = get_db_connection()
134
+ today = datetime.date.today()
135
+ target_date = today + datetime.timedelta(days=days_threshold)
136
+
137
+ # تبدیل تاریخ‌ها برای مقایسه رشته‌ای (فرض بر فرمت YYYY-MM-DD)
138
+ today_str = today.strftime("%Y-%m-%d")
139
+ target_str = target_date.strftime("%Y-%m-%d")
140
+
141
+ query = '''
142
+ SELECT g.guarantee_number, g.bank_name, g.due_date, c.company_name, g.amount
143
+ FROM guarantees g
144
+ LEFT JOIN contractors c ON g.contractor_id = c.id
145
+ WHERE g.status = 'فعال' AND g.due_date <= ? AND g.due_date >= ?
146
+ ORDER BY g.due_date ASC
147
+ '''
148
+ rows = conn.execute(query, (target_str, today_str)).fetchall()
149
+ conn.close()
150
+ return [list(r) for r in rows]
151
+
152
+ def get_stats():
153
+ conn = get_db_connection()
154
+ total_active = conn.execute("SELECT COUNT(*), SUM(amount) FROM guarantees WHERE status='فعال'").fetchone()
155
+ total_expired = conn.execute("SELECT COUNT(*) FROM guarantees WHERE status='فعال' AND due_date < date('now')").fetchone()
156
+ total_released = conn.execute("SELECT COUNT(*) FROM guarantees WHERE status='آزاد شده'").fetchone()
157
+ conn.close()
158
+
159
+ count = total_active[0] if total_active[0] else 0
160
+ amount = total_active[1] if total_active[1] else 0
161
+ expired = total_expired[0] if total_expired[0] else 0
162
+ released = total_released[0] if total_released[0] else 0
163
+
164
+ return count, amount, expired, released
165
+
166
+ def check_auth(username, password):
167
+ conn = get_db_connection()
168
+ user = conn.execute("SELECT * FROM users WHERE username = ? AND password = ?", (username, password)).fetchone()
169
+ conn.close()
170
+ return user is not None