-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvelavan_migration.sql
More file actions
3418 lines (3247 loc) · 146 KB
/
Copy pathvelavan_migration.sql
File metadata and controls
3418 lines (3247 loc) · 146 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
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
-- REMOVE RECORDS RELATED TO UNWANTED TABLE
with a as (select voucher_id from sales_emi_reconciliation_voucher),
b as ( delete from ac_txn using a where ac_txn.voucher_id = a.voucher_id),
c as ( delete from bill_allocation using a where bill_allocation.voucher_id = a.voucher_id),
d as ( delete from bank_txn using a where bank_txn.voucher_id = a.voucher_id),
e as ( delete from inv_txn using a where inv_txn.voucher_id = a.voucher_id)
delete
from voucher using a
where voucher.id = a.voucher_id;
with a as (select voucher_id from customer_advance),
b as ( delete from ac_txn using a where ac_txn.voucher_id = a.voucher_id),
c as ( delete from bill_allocation using a where bill_allocation.voucher_id = a.voucher_id),
d as ( delete from bank_txn using a where bank_txn.voucher_id = a.voucher_id),
e as ( delete from inv_txn using a where inv_txn.voucher_id = a.voucher_id)
delete
from voucher using a
where voucher.id = a.voucher_id;
with a as (select voucher_id from gift_voucher),
b as ( delete from ac_txn using a where ac_txn.voucher_id = a.voucher_id),
c as ( delete from bill_allocation using a where bill_allocation.voucher_id = a.voucher_id),
d as ( delete from bank_txn using a where bank_txn.voucher_id = a.voucher_id),
e as ( delete from inv_txn using a where inv_txn.voucher_id = a.voucher_id)
delete
from voucher using a
where voucher.id = a.voucher_id;
with a as (select voucher_id from shipment),
b as ( delete from ac_txn using a where ac_txn.voucher_id = a.voucher_id),
c as ( delete from bill_allocation using a where bill_allocation.voucher_id = a.voucher_id),
d as ( delete from bank_txn using a where bank_txn.voucher_id = a.voucher_id),
e as ( delete from inv_txn using a where inv_txn.voucher_id = a.voucher_id)
delete
from voucher using a
where voucher.id = a.voucher_id;
--REMOVE FOREIGN KEY--
DO $$
DECLARE r RECORD;
BEGIN
FOR r IN
SELECT n.nspname, c.relname, con.conname
FROM pg_constraint con
JOIN pg_class c ON c.oid = con.conrelid
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE con.contype = 'f'
AND n.nspname = 'public'
LOOP
EXECUTE format(
'ALTER TABLE %I.%I DROP CONSTRAINT %I;',
r.nspname, r.relname, r.conname
);
END LOOP;
END $$;
--REMOVE DEFAULT--
DO $$
DECLARE r RECORD;
BEGIN
FOR r IN
SELECT table_schema, table_name, column_name
FROM information_schema.columns
WHERE column_default IS NOT NULL
AND column_name <> 'id'
AND is_identity = 'NO'
AND table_schema = 'public'
LOOP
EXECUTE format(
'ALTER TABLE %I.%I ALTER COLUMN %I DROP DEFAULT;',
r.table_schema,
r.table_name,
r.column_name
);
END LOOP;
END $$;
-- --ADD DEFAULT--
--
-- alter table batch
-- alter column barcode
-- set default (currval('batch_id_seq')::text);
--REMOVE TRIGGER--
DO $$
DECLARE r RECORD;
BEGIN
FOR r IN
SELECT event_object_schema,
event_object_table,
trigger_name
FROM information_schema.triggers
WHERE trigger_schema = 'public'
LOOP
EXECUTE format(
'DROP TRIGGER IF EXISTS %I ON %I.%I;',
r.trigger_name,
r.event_object_schema,
r.event_object_table
);
END LOOP;
END $$;
--REMOVE TRIGGER FUNCTION--
DO $$
DECLARE r RECORD;
BEGIN
FOR r IN
SELECT
n.nspname AS schema_name,
p.proname AS function_name,
pg_get_function_identity_arguments(p.oid) AS args
FROM pg_proc p
JOIN pg_namespace n ON n.oid = p.pronamespace
WHERE n.nspname = 'public'
AND p.prorettype = 'pg_catalog.trigger'::regtype
LOOP
EXECUTE format(
'DROP FUNCTION IF EXISTS %I.%I(%s);',
r.schema_name,
r.function_name,
r.args
);
END LOOP;
END $$;
--REMOVE VIEWS--
DO $$
DECLARE
r RECORD;
BEGIN
FOR r IN
SELECT viewname
FROM pg_views
WHERE schemaname = 'public'
LOOP
EXECUTE 'DROP VIEW IF EXISTS public.' || quote_ident(r.viewname) || ' CASCADE';
END LOOP;
END $$;
--ADD OR MODIFY COLUMN--
-- AC_TXN --
--##
alter table ac_txn
add if not exists qty float,
add if not exists hsn_code text,
add if not exists uqc text,
add if not exists gst_tax text,
add if not exists gst_ratio float,
add if not exists taxable_amount float,
add if not exists cgst_amount float,
add if not exists sgst_amount float,
add if not exists igst_amount float,
add if not exists cess_amount float;
--##
UPDATE ac_txn a
SET qty = g.qty,
gst_tax = g.gst_tax,
gst_ratio = g.tax_ratio,
hsn_code = g.hsn_code,
uqc = g.uqc,
taxable_amount = g.taxable_amount,
cgst_amount = g.cgst_amount,
sgst_amount = g.sgst_amount,
igst_amount = g.igst_amount,
cess_amount = g.cess_amount
FROM gst_txn g
WHERE a.id = g.ac_txn_id;
--##
alter table ac_txn
alter column is_memo set not null,
alter column metadata type jsonb using metadata::jsonb,
alter column sno type integer using sno::integer;
-- AC_TXN --
-- BATCH --
update batch
set batch_no = coalesce(nullif(upper(regexp_replace(batch_no, '\s+', '', 'g')), ''), '1');
--##
alter table batch
alter batch_no set not null;
--## duplicate batch_no fixed, with concat batch_no, - , id
with a as
(select array_agg(id) as ids
from batch
group by branch_id, warehouse_id, inventory_id, vendor_id, batch_no
having count(1) > 1)
update batch b
set batch_no = b.batch_no || '-' || b.id::text
from a
where b.id = any (a.ids);
-- BATCH --
-- INV_TXN --
--##
alter table inv_txn
add if not exists sno int,
add if not exists qty float,
add if not exists unit_id int,
add if not exists unit_conv float,
add if not exists rate float,
add if not exists batch_no text,
add if not exists metadata jsonb,
add if not exists rate_tax_inclusive bool,
add if not exists gst_tax text,
add if not exists mrp float,
add if not exists p_rate float,
add if not exists s_rate float,
add if not exists barcode text,
add if not exists expiry date,
add if not exists nlc float,
add if not exists cost float,
add if not exists landing_cost float,
add if not exists disc_mode1 text,
add if not exists discount1 float,
add if not exists disc_mode2 text,
add if not exists discount2 float,
add if not exists disc_mode3 text,
add if not exists discount3 float,
add if not exists hsn_code text,
add if not exists cess_on_qty float,
add if not exists cess_on_val float,
add if not exists customer_id int,
add if not exists sales_person_id int,
add if not exists dummy bool,
add if not exists udf_drug_classifications int[];
--##
update inv_txn t
set sno = i.sno,
qty = i.qty,
rate = i.rate,
unit_id = i.unit_id,
unit_conv = i.unit_conv,
rate_tax_inclusive = true,
gst_tax = i.gst_tax,
disc_mode1 = i.disc_mode,
discount1 = i.discount,
hsn_code = i.hsn_code,
cess_on_qty = i.cess_on_qty,
cess_on_val = i.cess_on_val
from credit_note_inv_item i
where t.id = i.id;
--##
update inv_txn t
set customer_id = b.customer_id
from credit_note b
where b.customer_id is not null
and t.voucher_id = b.voucher_id;
--##
update inv_txn t
set sno = i.sno,
qty = i.qty,
rate = i.rate,
unit_id = i.unit_id,
unit_conv = i.unit_conv,
gst_tax = i.gst_tax,
disc_mode1 = i.disc1_mode,
discount1 = i.discount1,
disc_mode2 = i.disc2_mode,
discount2 = i.discount2,
rate_tax_inclusive = false,
hsn_code = i.hsn_code,
cess_on_qty = i.cess_on_qty,
cess_on_val = i.cess_on_val
from debit_note_inv_item i
where t.id = i.id;
--##
update inv_txn t
set vendor_id = b.vendor_id,
vendor_name = b.vendor_name
from debit_note b
where b.vendor_id is not null
and t.voucher_id = b.voucher_id;
--##
update inv_txn t
set sno = i.sno,
qty = i.qty,
rate = i.cost,
unit_id = i.unit_id,
unit_conv = i.unit_conv,
gst_tax = i.gst_tax,
hsn_code = i.hsn_code,
cess_on_qty = i.cess_on_qty,
cess_on_val = i.cess_on_val
from personal_use_purchase_inv_item i
where t.id = i.id;
--##
update inv_txn t
set sno = i.sno,
qty = i.qty,
rate = i.rate,
p_rate = i.rate,
unit_id = i.unit_id,
unit_conv = i.unit_conv,
gst_tax = i.gst_tax,
disc_mode1 = i.disc1_mode,
discount1 = i.discount1,
disc_mode2 = i.disc2_mode,
discount2 = i.discount2,
hsn_code = i.hsn_code,
cess_on_qty = i.cess_on_qty,
cess_on_val = i.cess_on_val
from purchase_bill_inv_item i
where t.id = i.id;
--##
update inv_txn t
set vendor_id = b.vendor_id,
vendor_name = b.vendor_name,
customer_id = b.customer_id
from purchase_bill b
where t.voucher_id = b.voucher_id;
--##
--##
update inv_txn t
set sno = i.sno,
qty = i.qty,
rate = i.rate,
unit_id = i.unit_id,
unit_conv = i.unit_conv,
gst_tax = i.gst_tax,
disc_mode1 = i.disc_mode,
discount1 = i.discount,
hsn_code = i.hsn_code,
cess_on_qty = i.cess_on_qty,
cess_on_val = i.cess_on_val,
sales_person_id = i.s_inc_id,
udf_drug_classifications = i.drug_classifications
from sale_bill_inv_item i
where t.id = i.id;
--##
update inv_txn t
set customer_id = b.customer_id
from sale_bill b
where b.customer_id is not null
and t.voucher_id = b.voucher_id;
--##
update inv_txn t
set sno = i.sno,
qty = i.qty,
rate = i.rate,
unit_id = i.unit_id,
unit_conv = i.unit_conv,
barcode = i.barcode,
asset_amount = i.asset_amount
from stock_journal_inv_item i
where t.id = i.id;
--##
update inv_txn t
set sno = i.sno,
unit_id = i.unit_id,
unit_conv = i.unit_conv,
qty = i.qty,
rate = i.rate,
asset_amount = i.asset_amount
from inventory_opening i
where t.id = i.id;
--##
alter table inv_txn
alter column sno set not null,
alter column qty set not null,
alter column unit_id set not null,
alter column unit_conv set not null,
alter column rate set not null,
alter column inward set not null,
alter column outward set not null;
-- INV_TXN --
-- VOUCHER --
--##
alter table voucher
add if not exists metadata jsonb,
add if not exists branch_gst_reg_type text,
add if not exists branch_gst_location_id text,
add if not exists branch_gst_no text,
add if not exists party_gst_reg_type text,
add if not exists party_gst_location_id text,
add if not exists party_gst_no text,
add if not exists gst_location_type text,
add if not exists vendor_id int,
add if not exists vendor_name text,
add if not exists customer_id int,
add if not exists customer_name text,
add if not exists warehouse_id int,
add if not exists warehouse_name text,
add if not exists rounded_off float,
add if not exists disc_mode text,
add if not exists discount float,
add if not exists sales_person_id int,
add if not exists sale_value float,
add if not exists profit_value float,
add if not exists nlc_value float,
add if not exists valid_provisional_profit bool,
add if not exists udf_alt_branch_id int,
add if not exists udf_alt_warehouse_id int,
add if not exists udf_transfer_voucher_id int,
add if not exists udf_approved bool,
add if not exists udf_doctor_id int;
--##
update voucher v
set branch_gst_reg_type = b.branch_gst ->> 'reg_type',
branch_gst_location_id = b.branch_gst ->> 'location_id',
branch_gst_no = b.branch_gst ->> 'gst_no',
party_gst_reg_type = b.party_gst ->> 'reg_type',
party_gst_location_id = b.party_gst ->> 'location_id',
party_gst_no = b.party_gst ->> 'gst_no',
warehouse_id = b.warehouse_id,
warehouse_name = b.warehouse_name,
vendor_id = b.vendor_id,
vendor_name = b.vendor_name,
disc_mode = b.discount_mode,
discount = b.discount_amount,
rounded_off = b.rounded_off
from debit_note b
where v.id = b.voucher_id;
--##
update voucher v
set branch_gst_reg_type = b.branch_gst ->> 'reg_type',
branch_gst_location_id = b.branch_gst ->> 'location_id',
branch_gst_no = b.branch_gst ->> 'gst_no',
party_gst_reg_type = b.party_gst ->> 'reg_type',
party_gst_location_id = b.party_gst ->> 'location_id',
party_gst_no = b.party_gst ->> 'gst_no',
warehouse_id = b.warehouse_id,
warehouse_name = b.warehouse_name,
customer_id = b.customer_id,
customer_name = b.customer_name,
disc_mode = b.discount_mode,
discount = b.discount_amount,
rounded_off = b.rounded_off,
sales_person_id = b.s_inc_id
from credit_note b
where v.id = b.voucher_id;
--##
update voucher v
set warehouse_id = b.warehouse_id,
warehouse_name = b.warehouse_name,
udf_alt_branch_id = b.alt_branch_id,
udf_alt_warehouse_id = b.alt_warehouse_id,
udf_transfer_voucher_id = b.transfer_voucher_id,
udf_approved = b.approved
from stock_journal b
where v.id = b.voucher_id;
--##
update voucher v
set warehouse_id = b.warehouse_id,
warehouse_name = b.warehouse_name,
vendor_id = b.vendor_id,
vendor_name = b.vendor_name
from goods_inward_note b
where v.id = b.voucher_id;
--##
update voucher v
set branch_gst_reg_type = b.branch_gst ->> 'reg_type',
branch_gst_location_id = b.branch_gst ->> 'location_id',
branch_gst_no = b.branch_gst ->> 'gst_no',
warehouse_id = b.warehouse_id,
warehouse_name = b.warehouse_name
from personal_use_purchase b
where v.id = b.voucher_id;
--##
update voucher v
set branch_gst_reg_type = b.branch_gst ->> 'reg_type',
branch_gst_location_id = b.branch_gst ->> 'location_id',
branch_gst_no = b.branch_gst ->> 'gst_no',
party_gst_reg_type = b.party_gst ->> 'reg_type',
party_gst_location_id = b.party_gst ->> 'location_id',
party_gst_no = b.party_gst ->> 'gst_no',
warehouse_id = b.warehouse_id,
warehouse_name = b.warehouse_name,
vendor_id = b.vendor_id,
vendor_name = b.vendor_name,
customer_id = b.customer_id,
customer_name = b.customer_name,
rounded_off = b.rounded_off,
disc_mode = b.discount_mode,
discount = b.discount_amount,
sale_value = b.sale_value,
profit_value = b.profit_value,
nlc_value = b.nlc_value,
valid_provisional_profit = b.valid_provisional_profit
from purchase_bill b
where v.id = b.voucher_id;
--##
update voucher v
set branch_gst_reg_type = b.branch_gst ->> 'reg_type',
branch_gst_location_id = b.branch_gst ->> 'location_id',
branch_gst_no = b.branch_gst ->> 'gst_no',
party_gst_reg_type = b.party_gst ->> 'reg_type',
party_gst_location_id = b.party_gst ->> 'location_id',
party_gst_no = b.party_gst ->> 'gst_no',
warehouse_id = b.warehouse_id,
warehouse_name = b.warehouse_name,
customer_id = b.customer_id,
customer_name = b.customer_name,
disc_mode = b.discount_mode,
discount = b.discount_amount,
rounded_off = b.rounded_off,
sales_person_id = b.s_inc_id,
udf_doctor_id = b.doctor_id
from sale_bill b
where v.id = b.voucher_id;
--##
--##
update voucher v
set branch_gst_reg_type = b.branch_gst ->> 'reg_type',
branch_gst_location_id = b.branch_gst ->> 'location_id',
branch_gst_no = b.branch_gst ->> 'gst_no',
party_gst_reg_type = b.party_gst ->> 'reg_type',
party_gst_location_id = b.party_gst ->> 'location_id',
party_gst_no = b.party_gst ->> 'gst_no',
warehouse_id = b.warehouse_id,
warehouse_name = b.warehouse_name,
customer_id = b.customer_id,
customer_name = b.customer_name,
disc_mode = b.discount_mode,
discount = b.discount_amount,
rounded_off = b.rounded_off,
sales_person_id = b.s_inc_id
from sale_quotation b
where v.id = b.voucher_id;
--##
alter table voucher
alter column mode set not null,
alter column e_invoice_details type jsonb using e_invoice_details::jsonb,
alter column eway_bill_details type jsonb using eway_bill_details::jsonb;
-- VOUCHER --
-- BANK_BENEFICIARY --
alter table bank_beneficiary rename beneficiary_code to code;
alter table bank_beneficiary alter column bank_account_type set not null;
-- BANK_BENEFICIARY --
-- BILL_ALLOCATION --
--##
alter table bill_allocation rename meta_data to metadata;
alter table bill_allocation alter column sno type int using sno::int;
--##
-- BILL_ALLOCATION --
-- GSTR_2B --
--##
alter table gstr_2b drop column IF EXISTS id;
alter table gstr_2b rename column gst_no to ctin;
alter table gstr_2b rename column supplier_name to trdnm;
alter table gstr_2b rename column invoice_no to inum;
alter table gstr_2b rename column invoice_date to dt;
alter table gstr_2b rename column total_invoice_value to val;
alter table gstr_2b rename column total_taxable_amount to txval;
alter table gstr_2b rename column integrated_tax_amount to igst;
alter table gstr_2b rename column central_tax_amount to cgst;
alter table gstr_2b rename column state_tax_amount to sgst;
alter table gstr_2b rename column cess_amount to cess;
-- GSTR_2B --
-- INVENTORY --
--##
alter table inventory
add if not exists cess_qty float,
add if not exists cess_value float;
alter table inventory
rename column category1_id to section_id;
alter table inventory
rename column compositions to udf_compositions;
alter table inventory
drop column if exists incentive_applicable;
alter table inventory
drop column if exists incentive_range_id;
alter table inventory
drop column if exists incentive_type;
--##
update inventory
set
cess_qty = (cess ->> 'on_quantity')::double precision,
cess_value = (cess ->> 'on_value')::double precision;
--##
alter table inventory
drop column if exists cess;
-- INVENTORY --
-- INVENTORY_BRANCH_DETAIL --
--##
alter table inventory_branch_detail
add if not exists s_disc_percentage float;
--##
alter table inventory_branch_detail alter column mrp_price_list type jsonb using mrp_price_list::jsonb;
alter table inventory_branch_detail alter column s_rate_price_list type jsonb using mrp_price_list::jsonb;
alter table inventory_branch_detail alter column nlc_price_list type jsonb using mrp_price_list::jsonb;
--##
update inventory_branch_detail x
set s_disc_percentage = y.value
from price_list_condition y
where x.inventory_id = y.inventory_id
and (x.branch_id = any (y.branches) or y.branches is null or array_length(y.branches, 1) = 0);
--##
alter table inventory_branch_detail
alter column reorder_mode drop not null;
--##
alter table inventory_branch_detail
alter column reorder_level drop not null;
--##
-- INVENTORY_BRANCH_DETAIL --
-- MEMBER --
--##
alter table member
add if not exists perms text[],
add if not exists ui_perms jsonb;
--##
update member m
set
perms = coalesce(mr.perms, '{}'::text[]),
ui_perms = coalesce(to_jsonb(mr.perms), '[]'::jsonb)
from member_role mr
where mr.name = m.role_id;
--##
alter table member alter column settings type jsonb using settings::jsonb;
-- MEMBER --
-- ORGANIZATION --
--##
alter table organization
add if not exists created_by int,
add if not exists updated_by int;
--##
alter table organization
alter column configuration type jsonb using configuration::jsonb,
alter column license_claims type jsonb using license_claims::jsonb;
--##
UPDATE organization o
SET
created_by = m.id,
updated_by = m.id,
id = '0194244f-d800-7001-8000-000000000000'
FROM member m
WHERE m.id = 1;
-- ORGANIZATION --
-- TDS_NATURE_OF_PAYMENT --
--##
alter table tds_nature_of_payment
add if not exists tds_account_id int;
--##
WITH a AS (SELECT id, tds_nature_of_payment_id, tds_deductee_type
FROM account
WHERE tds_nature_of_payment_id IS NOT NULL)
UPDATE tds_nature_of_payment t
SET tds_account_id = a.id
FROM a
WHERE a.tds_nature_of_payment_id = t.id
and tds_account_id is null;
--##
--##
update tds_nature_of_payment
set tds_account_id = coalesce((select id
from account
where tds_nature_of_payment_id is not null
limit 1), 1)
where tds_account_id is null;
--##
alter table tds_nature_of_payment
alter tds_account_id set not null;
--##
--##
-- TDS_NATURE_OF_PAYMENT --
--DROP OR MODIFY COLUMN--
-- AC_TXN --
-- ====== generated ======
alter table ac_txn alter column amount drop expression;
-- ====== generated ======
-- ====== columns ======
alter table ac_txn drop if exists is_opening;
alter table ac_txn drop if exists voucher_prefix;
alter table ac_txn drop if exists voucher_fy;
alter table ac_txn drop if exists voucher_seq;
alter table ac_txn drop if exists eft_reconciliation_voucher_id;
alter table ac_txn drop if exists account_type_name;
alter table ac_txn drop if exists alt_account_name;
alter table ac_txn drop if exists inst_no;
alter table ac_txn drop if exists base_account_types;
-- ====== columns ======
-- AC_TXN --
-- INV_TXN --
alter table inv_txn drop if exists division_id;
alter table inv_txn drop if exists division_name;
alter table inv_txn drop if exists party_id;
alter table inv_txn drop if exists party_name;
alter table inv_txn drop if exists vendor_name;
alter table inv_txn drop if exists reorder_inventory_id;
alter table inv_txn drop if exists inventory_hsn;
alter table inv_txn drop if exists manufacturer_name;
alter table inv_txn drop if exists is_opening;
alter table inv_txn drop if exists inventory_voucher_id;
alter table inv_txn rename column category1_id to section_id;
alter table inv_txn drop if exists category1_name;
alter table inv_txn drop if exists category2_id;
alter table inv_txn drop if exists category2_name;
alter table inv_txn drop if exists category3_id;
alter table inv_txn drop if exists category3_name;
alter table inv_txn drop if exists doctor_id;
alter table inv_txn drop if exists doctor_name;
alter table inv_txn drop if exists sale_taxable_amount;
alter table inv_txn drop if exists sale_tax_amount;
alter table inv_txn drop if exists pos_id;
-- INV_TXN --
-- VOUCHER --
alter table voucher drop if exists branch_gst;
alter table voucher drop if exists party_gst;
alter table voucher drop if exists party_id;
alter table voucher drop if exists party_name;
alter table voucher drop if exists pos_counter_code;
alter table voucher drop if exists approval_state;
alter table voucher drop if exists require_no_of_approval;
alter table voucher drop if exists pos_counter_session_id;
alter table voucher drop if exists pos_counter_settlement_id;
-- VOUCHER --
-- ACCOUNT_DAILY_SUMMARY --
alter table account_daily_summary alter column amount drop expression;
-- ACCOUNT_DAILY_SUMMARY --
-- ACCOUNT --
--##
-- ====== generated ======
alter table account alter column val_name drop expression;
-- ====== generated ======
-- ====== columns ======
alter table account drop if exists contact_type;
alter table account drop if exists hide;
alter table account drop if exists short_name;
alter table account drop if exists gst_type;
alter table account drop if exists cheque_in_favour_of;
alter table account drop if exists description;
alter table account drop if exists is_commission_discounted;
alter table account drop if exists commission;
alter table account drop if exists aadhar_no;
alter table account drop if exists alternate_mobile;
alter table account drop if exists telephone;
alter table account drop if exists contact_person;
alter table account drop if exists category1;
alter table account drop if exists category2;
alter table account drop if exists category3;
alter table account drop if exists category4;
alter table account drop if exists category5;
alter table account drop if exists agent_id;
alter table account drop if exists commission_account_id;
alter table account drop if exists delivery_address;
alter table account drop if exists enable_loyalty_point;
alter table account drop if exists loyalty_point;
alter table account drop if exists tags;
alter table account drop if exists e_banking_enabled;
alter table account drop if exists transport_detail;
alter table account drop if exists service_charge_gst_account_id;
alter table account drop if exists service_charge_non_gst_account_id;
alter table account drop if exists itc_ineligible;
alter table account drop if exists secondary_emails;
alter table account alter column transaction_enabled set not null;
alter table account ADD column code int;
update account SET code = id;
alter table account alter column code SET NOT NULL;
alter table account alter column e_banking_info type jsonb using e_banking_info::jsonb;
-- ====== columns ======
--##
-- ACCOUNT --
-- BANK --
alter table bank drop if exists created_at;
alter table bank drop if exists updated_at;
-- BANK --
-- BANK_BENEFICIARY --
alter table bank_beneficiary drop if exists bank_name;
alter table bank_beneficiary drop if exists bank_code;
-- BANK_BENEFICIARY --
-- BANK_TXN --
--##
-- ====== generated ======
alter table bank_txn alter column credit drop expression;
alter table bank_txn alter column debit drop expression;
-- ====== generated ======
-- ====== columns ======
alter table bank_txn drop if exists in_favour_of;
alter table bank_txn drop if exists base_account_types;
alter table bank_txn drop if exists alt_account_name;
alter table bank_txn drop if exists bank_beneficiary_id;
alter table bank_txn drop if exists epayment_tran_ref;
alter table bank_txn drop if exists epayment_req_ref;
alter table bank_txn drop if exists epayment_status;
alter table bank_txn drop if exists bank_ref_no;
alter table bank_txn drop if exists bank_particulars;
alter table bank_txn drop if exists emailed;
alter table bank_txn alter column sno type integer using sno::integer;
alter table bank_txn alter column is_memo set not null;
-- ====== columns ======
--##
-- BANK_TXN --
-- BATCH --
--##
-- ====== generated ======
alter table batch alter column p_rate drop expression;
alter table batch alter column closing drop expression;
-- ====== generated ======
-- ====== columns ======
alter table batch drop if exists sno;
alter table batch drop if exists reorder_inventory_id;
alter table batch drop if exists inventory_hsn;
alter table batch drop if exists branch_name;
alter table batch drop if exists warehouse_name;
alter table batch drop if exists division_id;
alter table batch drop if exists division_name;
alter table batch drop if exists txn_id;
alter table batch drop if exists inventory_voucher_id;
alter table batch drop if exists opening_p_rate;
alter table batch drop if exists label_qty;
alter table batch drop if exists retail_qty;
alter table batch drop if exists is_retail_qty;
alter table batch drop if exists unit_name;
alter table batch drop if exists source_batch_id;
alter table batch drop if exists manufacturer_name;
alter table batch drop if exists vendor_name;
alter table batch rename column category1_id to section_id;
alter table batch drop if exists category1_name;
alter table batch drop if exists category2_id;
alter table batch drop if exists category2_name;
alter table batch drop if exists category3_id;
alter table batch drop if exists category3_name;
alter table batch alter column unit_conv set not null;
alter table batch alter column batch_no set not null;
-- ====== columns ======
--##
-- BATCH --
-- BILL ALLOCATION IMP --
alter table bill_allocation
add if not exists new_ref_no text;
--##
create index bill_allocation_pending
on bill_allocation (pending);
--##
with a as (select coalesce(nullif(upper(regexp_replace(ref_no, '\s+', '', 'g')), ''), voucher_no, '1') as no,
array_agg(pending) as pids
from bill_allocation
where ref_type = 'NEW'
group by account_id, branch_id,
coalesce(nullif(upper(regexp_replace(ref_no, '\s+', '', 'g')), ''), voucher_no, '1'))
update bill_allocation b
set new_ref_no = a.no
from a
where b.pending = any (a.pids);
--##
alter table bill_allocation
rename ref_no to old_ref_no;
--##
alter table bill_allocation
rename new_ref_no to ref_no;
--##
-- BILL ALLOCATION IMP --
-- BILL_ALLOCATION --
--##
-- ====== columns ======
alter table bill_allocation drop if exists base_account_types;
alter table bill_allocation drop if exists pending;
alter table bill_allocation drop if exists is_approved;
alter table bill_allocation drop if exists due_date;
alter table bill_allocation drop if exists account_type_name;
alter table bill_allocation drop if exists bill_date;
alter table bill_allocation alter column metadata type jsonb using metadata::jsonb;
-- ====== columns ======
--##
-- BILL_ALLOCATION --
-- INVENTORY --
--##
-- ====== generated ======
alter table inventory alter column val_name drop expression;
-- ====== generated ======
-- ====== columns ======
alter table inventory drop if exists division_id;
alter table inventory drop if exists inventory_type;
alter table inventory drop if exists retail_qty;
alter table inventory drop if exists reorder_inventory_id;
alter table inventory drop if exists bulk_inventory_id;
alter table inventory drop if exists distribution_qty;
alter table inventory drop if exists purchase_config;
alter table inventory drop if exists sale_config;
alter table inventory drop if exists tags;
alter table inventory drop if exists description;
alter table inventory drop if exists manufacturer_name;
alter table inventory drop if exists vendor_id;
alter table inventory drop if exists vendor_name;
alter table inventory drop if exists vendors;
alter table inventory drop if exists set_rate_values_via_purchase;
alter table inventory drop if exists apply_s_rate_from_master_for_sale;
alter table inventory drop if exists fitting_charge;
alter table inventory drop if exists itc_ineligible;
alter table inventory drop if exists category2_id;
alter table inventory drop if exists category3_id;
alter table inventory drop if exists category1_name;
alter table inventory drop if exists category2_name;
alter table inventory drop if exists category3_name;
alter table inventory add column code integer;
update inventory set code = id;
alter table inventory alter column code set not null;
-- ====== columns ======
-- INVENTORY --
-- INVENTORY_BRANCH_DETAIL --
--##
-- ====== columns ======
alter table inventory_branch_detail drop if exists inventory_name;
alter table inventory_branch_detail drop if exists branch_name;
alter table inventory_branch_detail drop if exists inventory_barcodes;
alter table inventory_branch_detail drop if exists s_disc;
alter table inventory_branch_detail drop if exists discount_1;
alter table inventory_branch_detail drop if exists discount_2;
alter table inventory_branch_detail drop if exists preferred_vendor_name;
alter table inventory_branch_detail drop if exists last_vendor_name;
alter table inventory_branch_detail drop if exists s_customer_disc;
alter table inventory_branch_detail drop if exists p_rate_tax_inc;
alter table inventory_branch_detail drop if exists reorder_inventory_id;
alter table inventory_branch_detail drop if exists val_name;
alter table inventory_branch_detail drop if exists division_id;
alter table inventory_branch_detail rename reorder_mode to udf_reorder_mode;
alter table inventory_branch_detail rename reorder_level to udf_reorder_level;
alter table inventory_branch_detail rename min_order to udf_min_order;
alter table inventory_branch_detail rename max_order to udf_max_order;
-- ====== columns ======
--##
-- INVENTORY_BRANCH_DETAIL --
-- MEMBER --
--##
-- ====== columns ======
-- remote_access
alter table member drop if exists remote_access;
-- alter table member rename remote_access to udf_remote_access;
-- user_id
alter table member drop if exists user_id;
-- alter table member rename user_id to udf_user_id;
-- role_id
alter table member drop if exists role_id;
-- alter table member rename role_id to udf_role_id;
-- ====== columns ======
--##
-- MEMBER --
-- VOUCHER_TYPE --
--##
-- ====== columns ======
alter table voucher_type drop column IF EXISTS approve1_id;
alter table voucher_type drop column IF EXISTS approve2_id;
alter table voucher_type drop column IF EXISTS approve3_id;
alter table voucher_type drop column IF EXISTS approve4_id;
alter table voucher_type drop column IF EXISTS approve5_id;
-- ====== columns ======
--##
-- VOUCHER_TYPE --
-- TDS_ON_VOUCHER --
--##
-- ====== generated ======
-- amount_after_tds_deduction
alter table tds_on_voucher alter column amount_after_tds_deduction drop expression;
-- ====== generated ======
-- ====== columns ======
alter table tds_on_voucher drop column IF EXISTS branch_name;
alter table tds_on_voucher drop column IF EXISTS pending_id;
-- ====== columns ======
--##
-- TDS_ON_VOUCHER -
-- UNIT --
--##
-- ====== columns ======
alter table unit drop column IF EXISTS conversions;
alter table unit alter column precision type int using precision::int;
-- ====== columns ======
--##
-- UNIT --
-- changed_at removal --
alter table account drop if exists changed_at;
alter table account_type drop if exists changed_at;
alter table batch drop if exists changed_at;
alter table branch drop if exists changed_at;
alter table country drop if exists changed_at;