-
Notifications
You must be signed in to change notification settings - Fork 3
/
db_migrate.py
executable file
·2119 lines (2046 loc) · 116 KB
/
db_migrate.py
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
#!/usr/bin/python3
"""
This file is part of GNUKhata:A modular,robust and Free Accounting System.
License: AGPLv3
Contributors:
"Sai Karthik" <kskarthik@disroot.org>
"Krishnakant Mane" <kkmane@riseup.net>
"Ishan Masdekar " <imasdekar@dff.org.in>
"Navin Karkera" <navin@dff.org.in>
'Prajkta Patkar'<prajakta@dff.org.in>
'Reshma Bhatwadekar'<reshma_b@riseup.net>
"Sanket Kolnoorkar"<Sanketf123@gmail.com>
'Aditya Shukla' <adityashukla9158.as@gmail.com>
'Pravin Dake' <pravindake24@gmail.com>
"""
# from pyramid.view import view_defaults, view_config
from requests import request
from gkcore import eng
from pyramid.request import Request
from gkcore.models import gkdb
from sqlalchemy.sql import select
from sqlalchemy import func, desc, MetaData, inspect
from sqlalchemy.engine.base import Connection
from sqlalchemy import and_
import jwt
import gkcore
import json
from Crypto.PublicKey import RSA
from gkcore.models.meta import (
does_foreignkey_exist,
does_unique_constraint_exist,
inventoryMigration,
addFields,
columnExists,
columnTypeMatches,
tableExists,
getOnDelete,
uniqueConstraintExists,
)
from datetime import datetime, timedelta
import traceback
from gkcore.views.api_gkuser import getUserRole
con = Connection
class Migrate:
def __init__(self, request):
self.request = Request
self.request = request
self.con = Connection
def db(self):
"""
This function will be called only once while upgrading gnukhata.
The function will be mostly concerned with adding new fields to the databse or altering those which are present.
The columnExists() function will be used to check if a certain column exists.
If the function returns False then the field is created.
For example:
We check if the field stockdate is present.
If it is not present it means that this is an upgrade.
"""
self.con = eng.connect()
try:
organisations = self.con.execute(select([gkdb.organisation.c.orgcode]))
allorg = organisations.fetchall()
if not tableExists("cslastprice"):
self.con.execute(
"create table cslastprice(cslpid serial, lastprice numeric(13,2), inoutflag integer, custid integer NOT NULL,productcode integer NOT NULL,orgcode integer NOT NULL, primary key (cslpid), constraint cslastprice_orgcode_fkey FOREIGN KEY (orgcode) REFERENCES organisation(orgcode), constraint cslastprice_custid_fkey FOREIGN KEY (custid) REFERENCES customerandsupplier(custid),constraint cslastprice_productcode_fkey FOREIGN KEY (productcode) REFERENCES product(productcode), unique(orgcode, custid, productcode, inoutflag))"
)
inoutflags = [9, 15]
for orgid in allorg:
numberOfInvoices = self.con.execute(
select([func.count(gkdb.invoice.c.invid).label("invoices")])
)
invoices = numberOfInvoices.fetchone()
if int(invoices["invoices"]) > 0:
customers = self.con.execute(
select([gkdb.customerandsupplier.c.custid]).where(
gkdb.customerandsupplier.c.orgcode
== int(orgid["orgcode"])
)
)
customerdata = customers.fetchall()
products = self.con.execute(
select([gkdb.product.c.productcode]).where(
gkdb.product.c.orgcode == int(orgid["orgcode"])
)
)
productdata = products.fetchall()
for customer in customerdata:
for product in productdata:
for inoutflag in inoutflags:
try:
lastInvoice = self.con.execute(
"select max(invid) as invid from invoice where orgcode = %d and contents ? '%s' and inoutflag = %d and custid = %d"
% (
int(orgid["orgcode"]),
str(product["productcode"]),
int(inoutflag),
int(customer["custid"]),
)
)
lastInvoiceId = lastInvoice.fetchone()["invid"]
if lastInvoiceId != None:
lastPriceData = self.con.execute(
select([gkdb.invoice.c.contents]).where(
and_(
gkdb.invoice.c.invid
== int(lastInvoiceId),
gkdb.product.c.orgcode
== int(orgid["orgcode"]),
)
)
)
lastPriceDict = lastPriceData.fetchone()[
"contents"
]
productCode = product["productcode"]
if (
str(productCode).decode("utf-8")
in lastPriceDict
):
lastPriceValue = list(
lastPriceDict[
str(productCode).decode("utf-8")
].keys()
)[0]
priceDetails = {
"custid": int(customer["custid"]),
"productcode": int(
product["productcode"]
),
"orgcode": int(orgid["orgcode"]),
"inoutflag": int(inoutflag),
"lastprice": float(lastPriceValue),
}
lastPriceEntry = self.con.execute(
gkdb.cslastprice.insert(),
[priceDetails],
)
except:
pass
if not columnExists("unitofmeasurement", "description"):
self.con.execute("alter table unitofmeasurement add description text")
self.con.execute(
"alter table unitofmeasurement add sysunit integer default 0"
)
""" Following dictionary of uom, first try to insert single uqc if it fail means uqc is exists in table then updated its description and sysunit"""
dictofuqc = {
"BAG": "BAGS",
"BAL": "BALE",
"BDL": "BUNDLES",
"BKL": "BUCKLES",
"BOU": "BILLIONS OF UNITS",
"BOX": "BOX",
"BTL": "BOTTLES",
"BUN": "BUNCHES",
"CAN": "CANS",
"CBM": "CUBIC METER",
"CCM": "CUBIC CENTIMETER",
"CMS": "CENTIMETER",
"CRT": "Carat",
"CTN": "CARTONS",
"DOZ": "DOZEN",
"DRM": "DRUM",
"GGK": "GREAT GROSS",
"GMS": "GRAMS",
"GRS": "GROSS",
"GYD": "GROSS YARDS",
"KGS": "KILOGRAMS",
"KLR": "KILOLITER",
"KME": "KILOMETERS",
"MLT": "MILLILITER",
"MTR": "METER",
"MTS": "METRIC TON",
"NOS": "NUMBER",
"OTH": "OTHERS",
"PAC": "PACKS",
"PCS": "PIECES",
"PRS": "PAIRS",
"QTL": "QUINTAL",
"ROL": "ROLLS",
"SET": "SETS",
"SQF": "SQUARE FEET",
"SQM": "SQUARE METER",
"SQY": "SQUARE YARDS",
"TBS": "TABLETS",
"TGM": "TEN GRAMS",
"THD": "THOUSANDS",
"TON": "GREAT BRITAIN TON",
"TUB": "TUBES",
"UGS": "US GALLONS",
"UNT": "UNITS",
"YDS": "YARDS",
}
for unit, desc in list(dictofuqc.items()):
try:
self.con.execute(
gkdb.unitofmeasurement.insert(),
[
{
"unitname": unit,
"description": desc,
"conversionrate": 0.00,
"sysunit": 1,
}
],
)
except:
self.con.execute(
"update unitofmeasurement set sysunit=1, description='%s' where unitname='%s'"
% (desc, unit)
)
dictofuqc.pop(unit, 0)
if not columnExists("unitofmeasurement", "uqc"):
self.con.execute("alter table unitofmeasurement add uqc integer")
# discount flag is use to check whether discount is in percent or in amount.
# 1 = discount in amount, 16 = discount in percent.
if not columnExists("delchal", "discflag"):
self.con.execute(
"alter table invoicebin add column discflag integer default 1"
)
self.con.execute(
"alter table invoice add column discflag integer default 1"
)
self.con.execute(
"alter table delchal add column discflag integer default 1"
)
# in product following two collumns are added for discount in percent and in amount.
self.con.execute(
"alter table product add column percentdiscount numeric(5,2) default 0.00"
)
self.con.execute(
"alter table product add column amountdiscount numeric(13,2) default 0.00"
)
# Round off is use to detect that total amount of invoice is rounded off or not.
# If the field is not exist then it will create field.
if not columnExists("purchaseorder", "roundoffflag"):
self.con.execute(
"alter table purchaseorder add column roundoffflag integer default 0"
)
self.con.execute(
"alter table delchal add column roundoffflag integer default 0"
)
self.con.execute(
"alter table drcr add column roundoffflag integer default 0"
)
# remove goid if present
if columnExists("purchaseorder", "goid"):
self.con.execute("alter table purchaseorder drop column goid")
self.con.execute("alter table rejectionnote drop column goid")
self.con.execute("alter table drcr drop column goid")
self.con.execute("alter table budget drop column goid")
self.con.execute("alter table vouchers drop column goid")
self.con.execute("alter table invoice drop column goid")
self.con.execute("alter table delchal drop column goid")
# Round off is use to detect that total amount of invoice is rounded off or not.
# If the field is not exist then it will create field.
# Round Off Paid and Round Off Received account will genrate which is use while creating voucher for that invoice.
if not columnExists("invoice", "roundoffflag"):
self.con.execute(
"alter table invoice add column roundoffflag integer default 0"
)
for orgcode in allorg:
result = self.con.execute(
select([gkdb.accounts.c.accountcode]).where(
and_(
gkdb.accounts.c.orgcode == orgcode["orgcode"],
gkdb.accounts.c.accountname == "Round Off Paid",
)
)
)
account = result.fetchone()
if account == None:
grpCodePaid = self.con.execute(
select([gkdb.groupsubgroups.c.groupcode]).where(
and_(
gkdb.groupsubgroups.c.groupname
== "Indirect Expense",
gkdb.groupsubgroups.c.orgcode == orgcode["orgcode"],
)
)
)
grpCodeP = grpCodePaid.fetchone()
ropAdd = self.con.execute(
gkdb.accounts.insert(),
[
{
"accountname": "Round Off Paid",
"groupcode": grpCodeP["groupcode"],
"orgcode": orgcode["orgcode"],
"defaultflag": 180,
}
],
)
grpCodeReceived = self.con.execute(
select([gkdb.groupsubgroups.c.groupcode]).where(
and_(
gkdb.groupsubgroups.c.groupname
== "Indirect Income",
gkdb.groupsubgroups.c.orgcode == orgcode["orgcode"],
)
)
)
grpCodeR = grpCodeReceived.fetchone()
rorAdd = self.con.execute(
gkdb.accounts.insert(),
[
{
"accountname": "Round Off Received",
"groupcode": grpCodeR["groupcode"],
"orgcode": orgcode["orgcode"],
"defaultflag": 181,
}
],
)
# In Below query we are adding field pincode to invoice table
if not columnExists("invoice", "pincode"):
self.con.execute("alter table invoice add pincode text")
# In Below query we are adding field pincode to invoicebin table
if not columnExists("invoicebin", "pincode"):
self.con.execute("alter table invoicebin add pincode text")
# In Below query we are adding field pincode to customersupplier table
if not columnExists("customerandsupplier", "pincode"):
self.con.execute("alter table customerandsupplier add pincode text")
if not columnExists("customerandsupplier", "gst_reg_type"):
self.con.execute(
"alter table customerandsupplier add gst_reg_type integer"
)
if not columnExists("customerandsupplier", "gst_party_type"):
self.con.execute(
"alter table customerandsupplier add gst_party_type integer"
)
# Below query is to remove gbflag if it exists.
if columnExists("godown", "gbflag"):
self.con.execute("alter table godown drop column gbflag")
# In Below query we are adding field pincode to purchaseorder table
if not columnExists("purchaseorder", "pincode"):
self.con.execute("alter table purchaseorder add pincode text")
# In Below query we are adding field invnarration to invoicebin table
if not columnExists("invoicebin", "invnarration"):
self.con.execute("alter table invoicebin add invnarration text")
# In Below query we are adding field dcinfo to invoicebin table
if not columnExists("invoicebin", "dcinfo"):
self.con.execute("alter table invoicebin add dcinfo jsonb")
# In Below query we are adding field dcnarration to delchal table
if not columnExists("delchal", "dcnarration"):
self.con.execute("alter table delchal add dcnarration text")
# In Below query we are adding field dcnarration to delchalbin table
if not columnExists("delchalbin", "dcnarration"):
self.con.execute("alter table delchalbin add dcnarration text")
if not columnExists("organisation", "avnoflag"):
self.con.execute(
"alter table organisation add avnoflag integer default 0"
)
if not columnExists("organisation", "ainvnoflag"):
self.con.execute(
"alter table organisation add ainvnoflag integer default 0"
)
if not columnExists("organisation", "modeflag"):
self.con.execute(
"alter table organisation add modeflag integer default 1"
)
if not columnExists("organisation", "avflag"):
self.con.execute(
"alter table organisation add avflag integer default 1"
)
if not columnExists("organisation", "maflag"):
self.con.execute(
"alter table organisation add maflag integer default 0"
)
if not columnExists("accounts", "sysaccount"):
self.con.execute(
"alter table accounts add sysaccount integer default 0"
)
self.con.execute(
"update accounts set sysaccount=1 where accountname in ('Closing Stock', 'Opening Stock', 'Profit & Loss', 'Stock at the Beginning')"
)
if not columnExists("accounts", "defaultflag"):
self.con.execute(
"alter table accounts add defaultflag integer default 0"
)
for orgcode in allorg:
try:
groupdata = self.con.execute(
select([gkdb.groupsubgroups.c.groupcode]).where(
and_(
gkdb.groupsubgroups.c.orgcode == orgcode["orgcode"],
gkdb.groupsubgroups.c.groupname
== "Current Liabilities",
)
)
)
groupCode = groupdata.fetchone()
subGroup = {
"groupname": "Duties & Taxes",
"subgroupof": groupCode["groupcode"],
"orgcode": orgcode["orgcode"],
}
self.con.execute(gkdb.groupsubgroups.insert(), subGroup)
chartofacc = [
"Cash in hand",
"Krishi Kalyan Cess",
"Swachh Bharat Cess",
"Electricity Expense",
"Professional Fees",
"Bank A/C",
"Sale A/C",
"Purchase A/C",
"Discount Paid",
"Bonus",
"Depreciation Expense",
"Discount Received",
"Salary",
"Bank Charges",
"Rent",
"Travel Expense",
"Accumulated Depreciation",
"Miscellaneous Expense",
"VAT_OUT",
"VAT_IN",
]
for acc in chartofacc:
accname = self.con.execute(
select([gkdb.accounts.c.accountcode]).where(
and_(
gkdb.accounts.c.orgcode == orgcode["orgcode"],
gkdb.accounts.c.accountname == acc,
)
)
)
acname = accname.fetchone()
if acname == None:
if acc == "Cash in hand":
cash = self.con.execute(
select([gkdb.groupsubgroups.c.groupcode]).where(
and_(
gkdb.groupsubgroups.c.groupname
== "Cash",
gkdb.groupsubgroups.c.orgcode
== orgcode["orgcode"],
)
)
)
cashgrp = cash.fetchone()
cashadd = self.con.execute(
gkdb.accounts.insert(),
{
"accountname": "Cash in hand",
"groupcode": cashgrp["groupcode"],
"orgcode": orgcode["orgcode"],
"defaultflag": 3,
},
)
elif acc == "Krishi Kalyan Cess":
cess = self.con.execute(
select([gkdb.groupsubgroups.c.groupcode]).where(
and_(
gkdb.groupsubgroups.c.groupname
== "Duties & Taxes",
gkdb.groupsubgroups.c.orgcode
== orgcode["orgcode"],
)
)
)
cesscode = cess.fetchone()
cessadd = self.con.execute(
gkdb.accounts.insert(),
[
{
"accountname": "Krishi Kalyan Cess",
"groupcode": cesscode["groupcode"],
"orgcode": orgcode["orgcode"],
}
],
)
elif acc == "VAT_OUT":
vout = self.con.execute(
select([gkdb.groupsubgroups.c.groupcode]).where(
and_(
gkdb.groupsubgroups.c.groupname
== "Duties & Taxes",
gkdb.groupsubgroups.c.orgcode
== orgcode["orgcode"],
)
)
)
voutcode = vout.fetchone()
voutadd = self.con.execute(
gkdb.accounts.insert(),
[
{
"accountname": "VAT_OUT",
"groupcode": voutcode["groupcode"],
"orgcode": orgcode["orgcode"],
"sysaccount": 1,
}
],
)
elif acc == "VAT_IN":
vin = self.con.execute(
select([gkdb.groupsubgroups.c.groupcode]).where(
and_(
gkdb.groupsubgroups.c.groupname
== "Duties & Taxes",
gkdb.groupsubgroups.c.orgcode
== orgcode["orgcode"],
)
)
)
vincode = vin.fetchone()
vinadd = self.con.execute(
gkdb.accounts.insert(),
[
{
"accountname": "VAT_IN",
"groupcode": vincode["groupcode"],
"orgcode": orgcode["orgcode"],
"sysaccount": 1,
}
],
)
elif acc == "Swachh Bharat Cess":
bcess = self.con.execute(
select([gkdb.groupsubgroups.c.groupcode]).where(
and_(
gkdb.groupsubgroups.c.groupname
== "Duties & Taxes",
gkdb.groupsubgroups.c.orgcode
== orgcode["orgcode"],
)
)
)
cesscode = bcess.fetchone()
bcessadd = self.con.execute(
gkdb.accounts.insert(),
[
{
"accountname": "Swachh Bharat Cess",
"groupcode": cesscode["groupcode"],
"orgcode": orgcode["orgcode"],
}
],
)
elif acc == "Salary":
sal = self.con.execute(
select([gkdb.groupsubgroups.c.groupcode]).where(
and_(
gkdb.groupsubgroups.c.groupname
== "Direct Expense",
gkdb.groupsubgroups.c.orgcode
== orgcode["orgcode"],
)
)
)
salcode = sal.fetchone()
saladd = self.con.execute(
gkdb.accounts.insert(),
[
{
"accountname": "Salary",
"groupcode": salcode["groupcode"],
"orgcode": orgcode["orgcode"],
}
],
)
elif acc == "Miscellaneous Expense":
miscex = self.con.execute(
select([gkdb.groupsubgroups.c.groupcode]).where(
and_(
gkdb.groupsubgroups.c.groupname
== "Direct Expense",
gkdb.groupsubgroups.c.orgcode
== orgcode["orgcode"],
)
)
)
miscexcode = miscex.fetchone()
miscexadd = self.con.execute(
gkdb.accounts.insert(),
[
{
"accountname": "Miscellaneous Expense",
"groupcode": miscexcode["groupcode"],
"orgcode": orgcode["orgcode"],
}
],
)
elif acc == "Bank Charges":
bnkch = self.con.execute(
select([gkdb.groupsubgroups.c.groupcode]).where(
and_(
gkdb.groupsubgroups.c.groupname
== "Direct Expense",
gkdb.groupsubgroups.c.orgcode
== orgcode["orgcode"],
)
)
)
bnkchcode = bnkch.fetchone()
bnkchadd = self.con.execute(
gkdb.accounts.insert(),
[
{
"accountname": "Bank Charges",
"groupcode": bnkchcode["groupcode"],
"orgcode": orgcode["orgcode"],
}
],
)
elif acc == "Rent":
rent = self.con.execute(
select([gkdb.groupsubgroups.c.groupcode]).where(
and_(
gkdb.groupsubgroups.c.groupname
== "Direct Expense",
gkdb.groupsubgroups.c.orgcode
== orgcode["orgcode"],
)
)
)
rentcode = rent.fetchone()
rentadd = self.con.execute(
gkdb.accounts.insert(),
[
{
"accountname": "Rent",
"groupcode": rentcode["groupcode"],
"orgcode": orgcode["orgcode"],
}
],
)
elif acc == "Travel Expense":
travel = self.con.execute(
select([gkdb.groupsubgroups.c.groupcode]).where(
and_(
gkdb.groupsubgroups.c.groupname
== "Direct Expense",
gkdb.groupsubgroups.c.orgcode
== orgcode["orgcode"],
)
)
)
travelcode = travel.fetchone()
traveladd = self.con.execute(
gkdb.accounts.insert(),
[
{
"accountname": "Travel Expense",
"groupcode": travelcode["groupcode"],
"orgcode": orgcode["orgcode"],
}
],
)
elif acc == "Electricity Expense":
elect = self.con.execute(
select([gkdb.groupsubgroups.c.groupcode]).where(
and_(
gkdb.groupsubgroups.c.groupname
== "Direct Expense",
gkdb.groupsubgroups.c.orgcode
== orgcode["orgcode"],
)
)
)
electcode = elect.fetchone()
electadd = self.con.execute(
gkdb.accounts.insert(),
[
{
"accountname": "Electricity Expense",
"groupcode": electcode["groupcode"],
"orgcode": orgcode["orgcode"],
}
],
)
elif acc == "Professional Fees":
fees = self.con.execute(
select([gkdb.groupsubgroups.c.groupcode]).where(
and_(
gkdb.groupsubgroups.c.groupname
== "Direct Expense",
gkdb.groupsubgroups.c.orgcode
== orgcode["orgcode"],
)
)
)
feescode = fees.fetchone()
feesadd = self.con.execute(
gkdb.accounts.insert(),
[
{
"accountname": "Professional Fees",
"groupcode": feescode["groupcode"],
"orgcode": orgcode["orgcode"],
}
],
)
elif acc == "Bank A/C":
bank = self.con.execute(
select([gkdb.groupsubgroups.c.groupcode]).where(
and_(
gkdb.groupsubgroups.c.groupname
== "Bank",
gkdb.groupsubgroups.c.orgcode
== orgcode["orgcode"],
)
)
)
bankgrp = bank.fetchone()
bankadd = self.con.execute(
gkdb.accounts.insert(),
{
"accountname": "Bank A/C",
"groupcode": bankgrp["groupcode"],
"orgcode": orgcode["orgcode"],
"defaultflag": 2,
},
)
elif acc == "Discount Paid":
disc = self.con.execute(
select([gkdb.groupsubgroups.c.groupcode]).where(
and_(
gkdb.groupsubgroups.c.groupname
== "Indirect Expense",
gkdb.groupsubgroups.c.orgcode
== orgcode["orgcode"],
)
)
)
disccode = disc.fetchone()
discadd = self.con.execute(
gkdb.accounts.insert(),
[
{
"accountname": "Discount Paid",
"groupcode": disccode["groupcode"],
"orgcode": orgcode["orgcode"],
}
],
)
elif acc == "Bonus":
bonus = self.con.execute(
select([gkdb.groupsubgroups.c.groupcode]).where(
and_(
gkdb.groupsubgroups.c.groupname
== "Indirect Expense",
gkdb.groupsubgroups.c.orgcode
== orgcode["orgcode"],
)
)
)
bonuscode = bonus.fetchone()
bonusadd = self.con.execute(
gkdb.accounts.insert(),
[
{
"accountname": "Bonus",
"groupcode": bonuscode["groupcode"],
"orgcode": orgcode["orgcode"],
}
],
)
elif acc == "Depreciation Expense":
depex = self.con.execute(
select([gkdb.groupsubgroups.c.groupcode]).where(
and_(
gkdb.groupsubgroups.c.groupname
== "Indirect Expense",
gkdb.groupsubgroups.c.orgcode
== orgcode["orgcode"],
)
)
)
depexcode = depex.fetchone()
depexadd = self.con.execute(
gkdb.accounts.insert(),
[
{
"accountname": "Depreciation Expense",
"groupcode": depexcode["groupcode"],
"orgcode": orgcode["orgcode"],
}
],
)
elif acc == "Accumulated Depreciation":
accdep = self.con.execute(
select([gkdb.groupsubgroups.c.groupcode]).where(
and_(
gkdb.groupsubgroups.c.groupname
== "Fixed Assets",
gkdb.groupsubgroups.c.orgcode
== orgcode["orgcode"],
)
)
)
accdepcode = accdep.fetchone()
accdepadd = self.con.execute(
gkdb.accounts.insert(),
{
"accountname": "Accumulated Depreciation",
"groupcode": accdepcode["groupcode"],
"orgcode": orgcode["orgcode"],
},
)
elif acc == "Discount Received":
discpur = self.con.execute(
select([gkdb.groupsubgroups.c.groupcode]).where(
and_(
gkdb.groupsubgroups.c.groupname
== "Indirect Income",
gkdb.groupsubgroups.c.orgcode
== orgcode["orgcode"],
)
)
)
discpurcd = discpur.fetchone()
discadd = self.con.execute(
gkdb.accounts.insert(),
{
"accountname": "Discount Received",
"groupcode": discpurcd["groupcode"],
"orgcode": orgcode["orgcode"],
},
)
elif acc == "Sale A/C":
sale = self.con.execute(
select([gkdb.groupsubgroups.c.groupcode]).where(
and_(
gkdb.groupsubgroups.c.groupname
== "Sales",
gkdb.groupsubgroups.c.orgcode
== orgcode["orgcode"],
)
)
)
salecode = sale.fetchone()
if salecode == None:
acsale = self.con.execute(
select(
[gkdb.groupsubgroups.c.groupcode]
).where(
and_(
gkdb.groupsubgroups.c.groupname
== "Direct Income",
gkdb.groupsubgroups.c.orgcode
== orgcode["orgcode"],
)
)
)
saleCode = acsale.fetchone()
saleData = self.con.execute(
gkdb.groupsubgroups.insert(),
{
"groupname": "Sales",
"subgroupof": saleCode["groupcode"],
"orgcode": orgcode["orgcode"],
},
)
saleadd = self.con.execute(
gkdb.accounts.insert(),
{
"accountname": "Sale A/C",
"groupcode": salecode["groupcode"],
"orgcode": orgcode["orgcode"],
"defaultflag": 19,
},
)
else:
saleadd = self.con.execute(
gkdb.accounts.insert(),
{
"accountname": "Sale A/C",
"groupcode": salecode["groupcode"],
"orgcode": orgcode["orgcode"],
"defaultflag": 19,
},
)
elif acc == "Purchase A/C":
purch = self.con.execute(
select([gkdb.groupsubgroups.c.groupcode]).where(
and_(
gkdb.groupsubgroups.c.groupname
== "Purchase",
gkdb.groupsubgroups.c.orgcode
== orgcode["orgcode"],
)
)
)
purchcd = purch.fetchone()
if purchcd == None:
acpurc = self.con.execute(
select(
[gkdb.groupsubgroups.c.groupcode]
).where(
and_(
gkdb.groupsubgroups.c.groupname
== "Direct Expense",
gkdb.groupsubgroups.c.orgcode
== orgcode["orgcode"],
)
)
)
purCode = acpurc.fetchone()
insData = self.con.execute(
gkdb.groupsubgroups.insert(),
[
{
"groupname": "Purchase",
"subgroupof": purCode["groupcode"],
"orgcode": orgcode["orgcode"],
},
{
"groupname": "Consumables",
"subgroupof": purCode["groupcode"],
"orgcode": orgcode["orgcode"],
},
],
)
purchadd = self.con.execute(
gkdb.accounts.insert(),
{
"accountname": "Purchase A/C",
"groupcode": purchcd["groupcode"],
"orgcode": orgcode["orgcode"],
"defaultflag": 16,
},
)
else:
purchadd = self.con.execute(
gkdb.accounts.insert(),
{
"accountname": "Purchase A/C",
"groupcode": purchcd["groupcode"],
"orgcode": orgcode["orgcode"],
"defaultflag": 16,
},
)
elif acc == "Cash in hand":
self.con.execute(
"update accounts set defaultflag = 3 where accountcode =%d"
% int(acname["accountcode"])
)
elif acc == "Bank A/C":
self.con.execute(
"update accounts set defaultflag = 2 where accountcode =%d"
% int(acname["accountcode"])
)
elif acc == "Sale A/C":
self.con.execute(
"update accounts set defaultflag = 19 where accountcode =%d"
% int(acname["accountcode"])
)
elif acc == "Purchase A/C":
self.con.execute(
"update accounts set defaultflag = 16 where accountcode =%d"
% int(acname["accountcode"])
)
elif acc == "Round Off Paid":
self.con.execute(
"update accounts set defaultflag = 180 where accountcode =%d"
% int(acname["accountcode"])
)
elif acc == "Round Off Received":
self.con.execute(
"update accounts set defaultflag = 181 where accountcode =%d"
% int(acname["accountcode"])
)
elif acc == "VAT_IN":
self.con.execute(
"update accounts set defaultflag = 0, sysaccount = 1 where accountcode =%d"
% int(acname["accountcode"])
)
elif acc == "VAT_OUT":
self.con.execute(
"update accounts set defaultflag = 0, sysaccount = 1 where accountcode =%d"
% int(acname["accountcode"])
)
else:
self.con.execute(
"update accounts set defaultflag = 0 where accountcode =%d"
% int(acname["accountcode"])
)