-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_db.py
More file actions
242 lines (221 loc) · 5.23 KB
/
Copy pathupdate_db.py
File metadata and controls
242 lines (221 loc) · 5.23 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
import sqlite3
con = sqlite3.connect("database/trading.db")
cur = con.cursor()
# Ensure acc_mast columns exist
columns = ["mobile","pan","gstin","address","city","state"]
for col in columns:
try:
cur.execute(f"ALTER TABLE acc_mast ADD COLUMN {col} TEXT")
print(f"Added column: {col}")
except:
print(f"Column already exists: {col}")
for stmt in [
"ALTER TABLE users_mast ADD COLUMN mfa_required INTEGER DEFAULT 0",
"ALTER TABLE users_mast ADD COLUMN default_theme TEXT",
"ALTER TABLE users_mast ADD COLUMN default_density TEXT",
]:
try:
cur.execute(stmt)
print("Applied:", stmt)
except:
print("Skipped:", stmt)
cur.execute("""
CREATE TABLE IF NOT EXISTS acc_groups(
group_id INTEGER PRIMARY KEY AUTOINCREMENT,
group_name TEXT UNIQUE,
acc_type TEXT
)
""")
print("Ensured acc_groups table exists")
cur.execute("""
INSERT OR IGNORE INTO acc_groups(group_name,acc_type) VALUES
('Current Assets','ASSET'),
('Fixed Assets','ASSET'),
('Current Liabilities','LIABILITY'),
('Direct Income','INCOME'),
('Indirect Income','INCOME'),
('Direct Expense','EXPENSE'),
('Indirect Expense','EXPENSE'),
('Capital','EQUITY')
""")
print("Seeded default acc_groups (INSERT OR IGNORE)")
# Accounting vouchers tables
cur.execute("""
CREATE TABLE IF NOT EXISTS vouchers_head(
v_id INTEGER PRIMARY KEY AUTOINCREMENT,
v_date TEXT,
v_type TEXT,
narration TEXT,
user_code TEXT
)
""")
print("Ensured vouchers_head exists")
cur.execute("""
CREATE TABLE IF NOT EXISTS vouchers_lines(
id INTEGER PRIMARY KEY AUTOINCREMENT,
v_id INTEGER,
ledger_id INTEGER,
acc_id INTEGER,
dr REAL DEFAULT 0,
cr REAL DEFAULT 0
)
""")
print("Ensured vouchers_lines exists")
cur.execute("""
CREATE TABLE IF NOT EXISTS credit_notes(
id INTEGER PRIMARY KEY AUTOINCREMENT,
so_id INTEGER,
cn_date TEXT,
customer TEXT,
amount REAL,
reason TEXT
)
""")
print("Ensured credit_notes exists")
cur.execute("""
CREATE TABLE IF NOT EXISTS debit_notes(
id INTEGER PRIMARY KEY AUTOINCREMENT,
po_id INTEGER,
dn_date TEXT,
supplier TEXT,
amount REAL,
reason TEXT
)
""")
print("Ensured debit_notes exists")
cur.execute("""
CREATE TABLE IF NOT EXISTS item_ledger(
id INTEGER PRIMARY KEY AUTOINCREMENT,
la_id INTEGER,
la_date TEXT,
item_id INTEGER,
so_id INTEGER,
po_id INTEGER,
customer_id INTEGER,
supplier_id INTEGER,
qty REAL
)
""")
print("Ensured item_ledger exists")
cur.execute("""
CREATE TABLE IF NOT EXISTS audit_log(
id INTEGER PRIMARY KEY AUTOINCREMENT,
log_time TEXT,
user_code TEXT,
table_name TEXT,
action TEXT,
pk TEXT,
details TEXT
)
""")
print("Ensured audit_log exists")
# WhatsApp / notification tracking
cur.execute("""
CREATE TABLE IF NOT EXISTS notifications(
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_at TEXT,
channel TEXT,
direction TEXT,
kind TEXT,
ref_id INTEGER,
party_name TEXT,
mobile TEXT,
message TEXT,
status TEXT,
reply_text TEXT,
reply_time TEXT,
user_code TEXT
)
""")
print("Ensured notifications table exists")
# WhatsApp sent flags and approval fields
for stmt in [
"ALTER TABLE sale_orders ADD COLUMN whatsapp_sent INTEGER DEFAULT 0",
"ALTER TABLE purchase_orders ADD COLUMN whatsapp_sent INTEGER DEFAULT 0",
"ALTER TABLE loading_advice_head ADD COLUMN whatsapp_sent INTEGER DEFAULT 0",
"ALTER TABLE loading_advice_head ADD COLUMN approval_status TEXT",
"ALTER TABLE loading_advice_head ADD COLUMN approval_note TEXT",
]:
try:
cur.execute(stmt)
print("Applied:", stmt)
except:
print("Skipped:", stmt)
cur.execute("""
CREATE TABLE IF NOT EXISTS sale_schemes(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
start_date TEXT,
end_date TEXT,
min_qty REAL,
reward_text TEXT
)
""")
print("Ensured sale_schemes exists")
cur.execute("""
CREATE TABLE IF NOT EXISTS daily_rates(
id INTEGER PRIMARY KEY AUTOINCREMENT,
rate_date TEXT,
item_id INTEGER,
sale_rate REAL,
purchase_rate REAL
)
""")
print("Ensured daily_rates exists")
cur.execute("""
CREATE UNIQUE INDEX IF NOT EXISTS idx_daily_rates_date_item
ON daily_rates(rate_date,item_id)
""")
print("Ensured idx_daily_rates_date_item exists")
cur.execute("""
CREATE TABLE IF NOT EXISTS grn_head(
grn_id INTEGER PRIMARY KEY AUTOINCREMENT,
la_id INTEGER,
grn_date TEXT,
supplier TEXT,
total REAL
)
""")
print("Ensured grn_head exists")
cur.execute("""
CREATE TABLE IF NOT EXISTS grn_body(
id INTEGER PRIMARY KEY AUTOINCREMENT,
grn_id INTEGER,
item_name TEXT,
section TEXT,
qty REAL,
rate REAL,
amount REAL
)
""")
print("Ensured grn_body exists")
# Create ledgers table if not exists
cur.execute("""
CREATE TABLE IF NOT EXISTS ledgers(
ledger_id INTEGER PRIMARY KEY AUTOINCREMENT,
ledger_name TEXT UNIQUE,
group_id INTEGER
)
""")
print("Ensured ledgers table exists")
# Seed default ledgers
cur.execute("""
INSERT OR IGNORE INTO ledgers(ledger_name,group_id) VALUES
('Cash',1),
('Bank',1),
('Sundry Debtors',1),
('Sundry Creditors',3),
('Sales',4),
('Purchase',6),
('Output CGST',3),
('Output SGST',3),
('Output IGST',3),
('Input CGST',1),
('Input SGST',1),
('Input IGST',1),
('Capital',8)
""")
print("Seeded default ledgers (INSERT OR IGNORE)")
con.commit()
con.close()
print("Update complete")