-
Notifications
You must be signed in to change notification settings - Fork 2
/
script_creacion_inicial.sql
2240 lines (1683 loc) · 85 KB
/
script_creacion_inicial.sql
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
/* ---------------------------------------------------------------------- */
/* Tables */
/* ---------------------------------------------------------------------- */
/* ---------------------------------------------------------------------- */
/* Add table "Roles" */
/* ---------------------------------------------------------------------- */
USE [GD2C2012]
GO
CREATE SCHEMA [MR_ANDERSON] AUTHORIZATION [gd]
CREATE TABLE [Roles] (
[Rol] NVARCHAR(255) NOT NULL,
[Habilitado] BIT NOT NULL,
CONSTRAINT [PK_Roles] PRIMARY KEY ([Rol])
)
CREATE TABLE [Tipo_Usuario](
[Tipo] NVARCHAR(100) NOT NULL,
CONSTRAINT [PK_Tipo] PRIMARY KEY ([Tipo])
)
CREATE TABLE [Rol_Tipo](
[Tipo] NVARCHAR(100) NOT NULL,
[Rol] NVARCHAR(255) NOT NULL,
CONSTRAINT [PK_Tipo_Rol] PRIMARY KEY ([Rol],[Tipo])
)
/* ---------------------------------------------------------------------- */
/* Add table "Funcionalidades_Roles" */
/* ---------------------------------------------------------------------- */
CREATE TABLE [Funcionalidades_Roles] (
[Funcionalidad] VARCHAR(40) NOT NULL,
[Rol] NVARCHAR(255) NOT NULL
)
CREATE TABLE [Ciudades_Cupon](
[ciudad] NVARCHAR(255) NOT null,
[codigo] NVARCHAR(50) NOT null,
CONSTRAINT [PK_Ciudades_Cupon] PRIMARY KEY([ciudad], [codigo])
)
/* ---------------------------------------------------------------------- */
/* Add table "Login" */
/* ---------------------------------------------------------------------- */
CREATE TABLE [Login] (
[username] NVARCHAR(100) NOT NULL,
[user_password] NVARCHAR(255) ,
[last_login] DATETIME ,
[intentos_fallidos] NUMERIC(3) NOT NULL,
[Habilitado] BIT NOT NULL,
[Tipo] NVARCHAR(100) NOT NULL,
[Rol] NVARCHAR(255) NOT NULL,
CONSTRAINT [PK_Login] PRIMARY KEY ([username])
)
/* ---------------------------------------------------------------------- */
/* Add table "Direccion" */
/* ---------------------------------------------------------------------- */
CREATE TABLE [Direccion] (
[calle] NVARCHAR(100) NOT NULL,
[nro_piso] NUMERIC(3),
[depto] NVARCHAR(40),
[localidad] NVARCHAR(100) NOT NULL,
[username] NVARCHAR(100) NOT NULL,
[codigo_postal] NUMERIC(5),
CONSTRAINT [PK_Direccion] PRIMARY KEY ([calle], [localidad],[username])
)
/* ---------------------------------------------------------------------- */
/* Add table "Datos_Clientes" */
/* ---------------------------------------------------------------------- */
CREATE TABLE [Datos_Clientes] (
[nombre] NVARCHAR(255) NOT NULL,
[dni] NUMERIC(18) NOT NULL,
[apellido] NVARCHAR(255) NOT NULL,
[telefono] NUMERIC(18) unique NOT NULL,
[mail] NVARCHAR(255) NOT NULL,
[fecha_nac] DATETIME NOT NULL,
[username] NVARCHAR(100) NOT NULL,
[saldo] NUMERIC(10,2) NOT NULL,
CONSTRAINT [dni] PRIMARY KEY ([dni]),
UNIQUE ([telefono])
)
/* ---------------------------------------------------------------------- */
/* Add table "Cliente_Origen" */
/* ---------------------------------------------------------------------- */
CREATE TABLE [Cliente_Origen] (
[dni] NUMERIC(18) NOT NULL unique,
[id_origen] NUMERIC IDENTITY(0,1) NOT NULL,
CONSTRAINT [PK_Cliente_Origen] PRIMARY KEY ([id_origen])
)
/* ---------------------------------------------------------------------- */
/* Add table "Cliente_Destino" */
/* ---------------------------------------------------------------------- */
CREATE TABLE [Cliente_Destino] (
[dni] NUMERIC(18) NOT NULL unique,
[id_destino] NUMERIC IDENTITY(0,1) NOT NULL,
CONSTRAINT [PK_Cliente_Destino] PRIMARY KEY ([id_destino])
)
/* ---------------------------------------------------------------------- */
/* Add table "Cargas" */
/* ---------------------------------------------------------------------- */
CREATE TABLE [Cargas] (
[monto] int NOT NULL,
[fecha] DATETIME NOT NULL,
[dni] NUMERIC(18) NOT NULL,
[tipo_pago] VARCHAR(40) NOT NULL -- EFECTIVO CREDITO DEBITO
)
/* ---------------------------------------------------------------------- */
/* Add table "Datos_Proveedores" */
/* ---------------------------------------------------------------------- */
CREATE TABLE [Datos_Proveedores] (
[provee_cuit] NVARCHAR(20) NOT NULL,
[provee_rs] NVARCHAR(100) NOT NULL unique,
[provee_telefono] NUMERIC(18) NOT NULL,
[provee_rubro] NVARCHAR(100) NOT NULL,
[username] NVARCHAR(100) NOT NULL,
[nombre_contacto] VARCHAR(40),
[provee_email] NVARCHAR(255),
CONSTRAINT [PK_Datos_Proveedores] PRIMARY KEY ([provee_cuit])
)
/* ---------------------------------------------------------------------- */
/* Add table "Factura" */
/* ---------------------------------------------------------------------- */
CREATE TABLE [Factura] (
[factura_nro] NUMERIC(18) NOT NULL,
[factura_fecha] DATETIME NOT NULL,
[provee_cuit] NVARCHAR(20),
CONSTRAINT [PK_Factura] PRIMARY KEY ([factura_nro])
)
/* ---------------------------------------------------------------------- */
/* Add table "Datos_Tarjeta" */
/* ---------------------------------------------------------------------- */
CREATE TABLE [Datos_Tarjeta] (
[nro_tarjeta] NVARCHAR(50) NOT NULL,
[fecha_emision] DATETIME NOT NULL,
[fecha_vencimiento] DATETIME NOT NULL,
[dni] NUMERIC(18) NOT NULL,
[tipo] NVARCHAR(10) NOT NULL, --CREDITO DEBITO
PRIMARY KEY ([nro_tarjeta], [tipo])
)
/* ---------------------------------------------------------------------- */
/* Add table "Cupones" */
/* ---------------------------------------------------------------------- */
CREATE TABLE [Cupones] (
[codigo] NVARCHAR(50) NOT NULL,
[precio] NUMERIC(18,2) NOT NULL,
[precio_fict] NUMERIC(18,2) NOT NULL,
[cantidad_x_usuario] NUMERIC(18) NOT NULL,
[descripcion] NVARCHAR(255) NOT NULL,
[stock_disponible] NUMERIC(10) NOT NULL,
[provee_cuit] NVARCHAR(20) NOT NULL,
[vencimiento_oferta] DATETIME NOT NULL,
[vencimiento_canje] DATETIME,
[fecha_publicacion] DATETIME NOT NULL,
[publicado] BIT NOT NULL,
CONSTRAINT [codigo] PRIMARY KEY ([codigo])
)
/* ---------------------------------------------------------------------- */
/* Add table "Giftcard" */
/* ---------------------------------------------------------------------- */
CREATE TABLE [Giftcard] (
[fecha] DATETIME NOT NULL,
[monto] NUMERIC(18,2) NOT NULL,
[id_origen] NUMERIC NOT NULL,
[id_destino] NUMERIC NOT NULL
)
/* ---------------------------------------------------------------------- */
/* Add table "Factura_Renglon" */
/* ---------------------------------------------------------------------- */
CREATE TABLE [Factura_Renglon] (
[cantidad] NUMERIC(18),
[factura_nro] NUMERIC(18) NOT NULL,
[codigo] NVARCHAR(50) NOT NULL,
CONSTRAINT [PK_Factura_Renglon] PRIMARY KEY ([factura_nro], [codigo])
)
/* ---------------------------------------------------------------------- */
/* Add table "Ciudades" */
/* ---------------------------------------------------------------------- */
CREATE TABLE [Ciudades] (
[ciudad] NVARCHAR(255) not null,
[dni] NUMERIC(18) not null
)
/* ---------------------------------------------------------------------- */
/* Add table "Compras" */
/* ---------------------------------------------------------------------- */
CREATE TABLE [Compras] (
[dni] NUMERIC NOT NULL,
[fecha] DATETIME NOT NULL,
[id_compra] NUMERIC IDENTITY(0,1) NOT NULL,
[codigo] NVARCHAR(50),
CONSTRAINT [PK_Compras] PRIMARY KEY ([id_compra])
)
/* ---------------------------------------------------------------------- */
/* Add table "Devoluciones" */
/* ---------------------------------------------------------------------- */
CREATE TABLE [Devoluciones] (
[fecha_devolucion] DATETIME NOT NULL,
[motivo] NVARCHAR(255),
[id_compra] NUMERIC NOT NULL
)
/* ---------------------------------------------------------------------- */
/* Add table "Consumos" */
/* ---------------------------------------------------------------------- */
CREATE TABLE [Consumos] (
[fecha_consumo] DATETIME NOT NULL,
[id_compra] NUMERIC NOT NULL,
[facturado] bit
)
GO
/* ---------------------------------------------------------------------- */
/* Foreign key constraints */
/* ---------------------------------------------------------------------- */
ALTER TABLE [MR_ANDERSON].[Ciudades_Cupon] ADD CONSTRAINT [Codigo_Ciudad]
FOREIGN KEY ([codigo]) REFERENCES [MR_ANDERSON].[Cupones] ([codigo])
GO
ALTER TABLE [MR_ANDERSON].[Datos_Clientes] ADD CONSTRAINT [Login_Datos_Clientes]
FOREIGN KEY ([username]) REFERENCES [MR_ANDERSON].[Login] ([username])
GO
ALTER TABLE [MR_ANDERSON].[Rol_Tipo] ADD CONSTRAINT [FK_ROL]
FOREIGN KEY ([Rol]) REFERENCES [MR_ANDERSON].[Roles] ([Rol])
GO
ALTER TABLE [MR_ANDERSON].[Rol_Tipo] ADD CONSTRAINT [FK_TIPO]
FOREIGN KEY ([Tipo]) REFERENCES [MR_ANDERSON].[Tipo_Usuario] ([Tipo])
GO
ALTER TABLE [MR_ANDERSON].[Login] ADD CONSTRAINT [Tipo_Login]
FOREIGN KEY ([Tipo]) REFERENCES [MR_ANDERSON].[Tipo_Usuario] ([Tipo])
GO
ALTER TABLE [MR_ANDERSON].[Cupones] ADD CONSTRAINT [Datos_Proveedores_Cupones]
FOREIGN KEY ([provee_cuit]) REFERENCES [MR_ANDERSON].[Datos_Proveedores] ([provee_cuit]) on update cascade;
GO
ALTER TABLE [MR_ANDERSON].[Cliente_Origen] ADD CONSTRAINT [Datos_Clientes_Cliente_Origen]
FOREIGN KEY ([dni]) REFERENCES [MR_ANDERSON].[Datos_Clientes] ([dni]) on update cascade;
GO
ALTER TABLE [MR_ANDERSON].[Cliente_Destino] ADD CONSTRAINT [Datos_Clientes_Cliente_Destino]
FOREIGN KEY ([dni]) REFERENCES [MR_ANDERSON].[Datos_Clientes] ([dni]) on update cascade;
GO
ALTER TABLE [MR_ANDERSON].[Giftcard] ADD CONSTRAINT [Cliente_Destino_Giftcard]
FOREIGN KEY ([id_destino]) REFERENCES [MR_ANDERSON].[Cliente_Destino] ([id_destino])
GO
ALTER TABLE [MR_ANDERSON].[Giftcard] ADD CONSTRAINT [Cliente_Origen_Giftcard]
FOREIGN KEY ([id_origen]) REFERENCES [MR_ANDERSON].[Cliente_Origen] ([id_origen])
GO
ALTER TABLE [MR_ANDERSON].[Cargas] ADD CONSTRAINT [Datos_Clientes_Cargas]
FOREIGN KEY ([dni]) REFERENCES [MR_ANDERSON].[Datos_Clientes] ([dni]) on update cascade;
GO
ALTER TABLE [MR_ANDERSON].[Datos_Proveedores] ADD CONSTRAINT [Login_Datos_Proveedores]
FOREIGN KEY ([username]) REFERENCES [MR_ANDERSON].[Login] ([username])
GO
ALTER TABLE [MR_ANDERSON].[Factura] ADD CONSTRAINT [Datos_Proveedores_Factura]
FOREIGN KEY ([provee_cuit]) REFERENCES [MR_ANDERSON].[Datos_Proveedores] ([provee_cuit]) on update cascade;
GO
ALTER TABLE [MR_ANDERSON].[Factura_Renglon] ADD CONSTRAINT [Factura_Factura_Renglon]
FOREIGN KEY ([factura_nro]) REFERENCES [MR_ANDERSON].[Factura] ([factura_nro])
GO
ALTER TABLE [MR_ANDERSON].[Factura_Renglon] ADD CONSTRAINT [Cupones_Factura_Renglon]
FOREIGN KEY ([codigo]) REFERENCES [MR_ANDERSON].[Cupones] ([codigo])
GO
ALTER TABLE [MR_ANDERSON].[Funcionalidades_Roles] ADD CONSTRAINT [Roles_Funcionalidades_Roles]
FOREIGN KEY ([Rol]) REFERENCES [MR_ANDERSON].[Roles] ([Rol])
GO
ALTER TABLE [MR_ANDERSON].[Login] ADD CONSTRAINT [Roles_Login]
FOREIGN KEY ([Rol]) REFERENCES [MR_ANDERSON].[Roles] ([Rol]) on update cascade;
GO
ALTER TABLE [MR_ANDERSON].[Ciudades] ADD CONSTRAINT [Datos_Clientes_Ciudades]
FOREIGN KEY ([dni]) REFERENCES [MR_ANDERSON].[Datos_Clientes] ([dni]) on update cascade;
GO
ALTER TABLE [MR_ANDERSON].[Direccion] ADD CONSTRAINT [Login_Direccion]
FOREIGN KEY ([username]) REFERENCES [MR_ANDERSON].[Login] ([username])
GO
ALTER TABLE [MR_ANDERSON].[Datos_Tarjeta] ADD CONSTRAINT [Datos_Clientes_Datos_Tarjeta]
FOREIGN KEY ([dni]) REFERENCES [MR_ANDERSON].[Datos_Clientes] ([dni]) on update cascade;
GO
ALTER TABLE [MR_ANDERSON].[Compras] ADD CONSTRAINT [Datos_Clientes_Compras]
FOREIGN KEY ([dni]) REFERENCES [MR_ANDERSON].[Datos_Clientes] ([dni]) on update cascade;
GO
ALTER TABLE [MR_ANDERSON].[Compras] ADD CONSTRAINT [Cupones_Compras]
FOREIGN KEY ([codigo]) REFERENCES [MR_ANDERSON].[Cupones] ([codigo])
GO
ALTER TABLE [MR_ANDERSON].[Devoluciones] ADD CONSTRAINT [Compras_Devoluciones]
FOREIGN KEY ([id_compra]) REFERENCES [MR_ANDERSON].[Compras] ([id_compra])
GO
ALTER TABLE [MR_ANDERSON].[Consumos] ADD CONSTRAINT [Compras_Consumos]
FOREIGN KEY ([id_compra]) REFERENCES [MR_ANDERSON].[Compras] ([id_compra])
GO
GO
--Migracion de Datos
begin tran trn_inserts_tablas
insert into MR_ANDERSON.Roles(Rol,Habilitado)
values('Administrador',1)
insert into MR_ANDERSON.Roles(Rol,Habilitado)
values('Cliente',1)
insert into MR_ANDERSON.Roles(Rol,Habilitado)
values('Proveedor',1)
insert into MR_ANDERSON.Roles (Rol,Habilitado)
values('Administrador General',1)
insert into MR_ANDERSON.Tipo_Usuario (Tipo)
values('Cliente')
insert into MR_ANDERSON.Tipo_Usuario (Tipo)
values('Proveedor')
insert into MR_ANDERSON.Tipo_Usuario (Tipo)
values('Administrador')
insert into MR_ANDERSON.Rol_Tipo (Tipo, Rol)
values('Cliente', 'Cliente')
insert into MR_ANDERSON.Rol_Tipo (Tipo, Rol)
values('Proveedor', 'Proveedor')
insert into MR_ANDERSON.Rol_Tipo (Tipo, Rol)
values('Administrador', 'Administrador')
insert into MR_ANDERSON.Rol_Tipo (Tipo, Rol)
values('Administrador', 'Administrador General')
-- Insertamos el administrador a la tabla de Login (pass: gdadmin2012)
insert into MR_ANDERSON.Login(username,user_password,last_login,intentos_fallidos,Habilitado,Rol,Tipo)
VALUES('admin','e6b87050bfcb8143fcb8db0170a4dc9ed00d904ddd3e2a4ad1b1e8dc0fdc9be7',NULL,0,1,'Administrador General','Administrador')
-- Insertamos los datos de los clientes al Login
insert into MR_ANDERSON.Login(username,user_password,last_login,intentos_fallidos,Habilitado,Rol,Tipo)
select distinct master.Cli_Dni, NULL,NULL,0,1,'Cliente','Cliente'
from gd_esquema.Maestra master
-- Insertamos los datos de los proveedores al Login
insert into MR_ANDERSON.Login(username,user_password,last_login,intentos_fallidos,Habilitado,Rol,Tipo)
select distinct master.Provee_CUIT,NULL,NULL,0,1,'Proveedor','Proveedor'
from gd_esquema.Maestra master
where master.Provee_CUIT is not NULL
--Insertamos los datos viejos de los clientes al modelo nuevo
insert into MR_ANDERSON.Datos_Clientes (dni, nombre, apellido, telefono, mail, fecha_nac, username, saldo )
select distinct master.Cli_Dni, master.Cli_Nombre, master.Cli_Apellido,
master.Cli_Telefono, master.Cli_Mail, master.Cli_Fecha_Nac,cast(master.Cli_Dni as NVARCHAR), 0
from gd_esquema.Maestra master
--Insertamos las direcciones de los clientes al modelo nuevo
insert into MR_ANDERSON.Direccion(calle,nro_piso,depto,localidad,username)
select master.Cli_Direccion,NULL,NULL,master.Cli_Ciudad, cast(Clientes.dni as NVARCHAR)
from gd_esquema.Maestra master
join MR_ANDERSON.Datos_Clientes Clientes
on master.Cli_Dni = Clientes.dni
group by master.Cli_Direccion,master.Cli_Ciudad, Clientes.dni
--Insertamos los clientes que compraron alguna giftcard
insert into MR_ANDERSON.Cliente_Origen(dni)
select distinct master.Cli_Dni
from gd_esquema.Maestra master
where master.Cli_Dni is not NULL
--Insertamos los clientes que recibieron alguna giftcard
insert into MR_ANDERSON.Cliente_Destino(dni)
select distinct master.Cli_Dest_Dni
from gd_esquema.Maestra master
where master.Cli_Dest_Dni is not NULL
--Insertamos los datos de las giftcards
insert into MR_ANDERSON.Giftcard(fecha,monto,id_origen,id_destino)
select master.Giftcard_Fecha, master.Giftcard_Monto, origen.id_origen, destino.id_destino
from gd_esquema.Maestra master
join MR_ANDERSON.Cliente_Origen origen
on master.Cli_Dni = origen.dni
join MR_ANDERSON.Cliente_Destino destino
on master.Cli_Dest_Dni = destino.dni
--Insertamos los datos de las cargas de los clientes
insert into MR_ANDERSON.Cargas(monto,fecha,dni,tipo_pago)
select master.Carga_Credito, master.Carga_Fecha,master.Cli_Dni, master.Tipo_Pago_Desc
from gd_esquema.Maestra master
join MR_ANDERSON.Datos_Clientes Clientes
on master.Cli_Dni = Clientes.dni
where master.Carga_Credito is not NULL and master.Carga_Fecha is not NULL
and master.Tipo_Pago_Desc is not NULL
--Insertamos los datos de los proveedores
insert into MR_ANDERSON.Datos_Proveedores(provee_cuit,provee_rs,provee_telefono,provee_rubro,username,nombre_contacto,provee_email)
select master.Provee_CUIT,master.Provee_RS,master.Provee_Telefono,master.Provee_Rubro,
cast(master.Provee_CUIT as NVARCHAR), NULL,NULL
from gd_esquema.Maestra master
group by master.Provee_CUIT,master.Provee_RS,master.Provee_Telefono,master.Provee_Rubro
having master.Provee_CUIT is not NULL and master.Provee_RS is not null
--Insertamos las direcciones de los proveedores
insert into MR_ANDERSON.Direccion(calle,nro_piso,depto,localidad,username)
select master.Provee_Dom, null,null,master.Provee_Ciudad, cast(master.Provee_CUIT as NVARCHAR)
from gd_esquema.Maestra master
join MR_ANDERSON.Datos_Proveedores Proveedores
on master.Provee_CUIT = Proveedores.provee_cuit
group by master.Provee_Dom, master.Provee_Ciudad, master.Provee_CUIT
insert into MR_ANDERSON.Cupones(codigo,precio,precio_fict,cantidad_x_usuario,descripcion,
stock_disponible,provee_cuit,vencimiento_oferta,
vencimiento_canje,fecha_publicacion, publicado)
select master.Groupon_Codigo, master.Groupon_Precio, master.Groupon_Precio_Ficticio,
1, master.Groupon_Descripcion, master.Groupon_Cantidad,
master.Provee_CUIT,master.Groupon_Fecha_Venc, NULL, master.Groupon_Fecha,1
from gd_esquema.Maestra master
join MR_ANDERSON.Datos_Proveedores Proveedores
on master.Provee_CUIT = Proveedores.provee_cuit
group by master.Groupon_Codigo, master.Groupon_Precio, master.Groupon_Precio_Ficticio,
master.Groupon_Cantidad, master.Groupon_Descripcion, master.Provee_CUIT,
master.Groupon_Fecha_Venc, master.Groupon_Fecha
having master.Groupon_Fecha is not null
insert into MR_ANDERSON.Compras(dni,fecha,codigo)
select master.Cli_Dni, master.Groupon_Fecha_Compra, master.Groupon_Codigo
from gd_esquema.Maestra master
join MR_ANDERSON.Datos_Clientes Clientes
on master.Cli_Dni = Clientes.dni
join MR_ANDERSON.Cupones Cupones
on master.Groupon_Codigo = Cupones.codigo
where master.Groupon_Fecha_Compra is not null
group by master.Cli_Dni, master.Groupon_Fecha_Compra,
master.Groupon_Codigo
insert into MR_ANDERSON.Devoluciones(fecha_devolucion,motivo, id_compra)
select master.Groupon_Devolucion_Fecha, NULL,
(select id_compra
from MR_ANDERSON.Compras C
where master.Cli_Dni = C.dni and master.Groupon_Codigo = C.codigo
and master.Groupon_Fecha_Compra = C.fecha)
from gd_esquema.Maestra master
join MR_ANDERSON.Datos_Clientes Clientes
on master.Cli_Dni = Clientes.dni
join MR_ANDERSON.Cupones Cupones
on master.Groupon_Codigo = Cupones.codigo
where master.Groupon_Devolucion_Fecha is not null
GO
Merge into MR_ANDERSON.Cupones
using(
select count(Cupones.Codigo) as stock, Cupones.codigo as codigo
from MR_ANDERSON.Devoluciones
join MR_ANDERSON.Compras Compras
on Devoluciones.id_compra = Compras.id_compra
join MR_ANDERSON.Cupones Cupones
on Compras.codigo = Cupones.codigo
group by Cupones.codigo
) as source
on MR_ANDERSON.Cupones.codigo = source.codigo
when matched then
update
set MR_ANDERSON.Cupones.stock_disponible = MR_ANDERSON.Cupones.stock_disponible + source.stock;
GO
Merge into MR_ANDERSON.Cupones
using(
select count(Cupones.Codigo) as out, Cupones.Codigo as codigo
from MR_ANDERSON.Compras
join MR_ANDERSON.Cupones Cupones
on Compras.codigo = Cupones.codigo
group by Cupones.codigo
) as source
on MR_ANDERSON.Cupones.codigo = source.codigo
when matched then
update
set MR_ANDERSON.Cupones.stock_disponible = MR_ANDERSON.Cupones.stock_disponible - source.out;
GO
insert into MR_ANDERSON.Consumos(fecha_consumo, id_compra, facturado)
select master.Groupon_Entregado_Fecha, Compras.id_compra, 1
from gd_esquema.Maestra master
join MR_ANDERSON.Compras Compras
on master.Groupon_Fecha_Compra = Compras.fecha
and master.Groupon_Codigo = Compras.codigo
where master.Groupon_Entregado_Fecha is not null
insert into MR_ANDERSON.Factura(factura_nro,factura_fecha,provee_cuit)
select master.Factura_Nro,master.Factura_Fecha, master.Provee_CUIT
from gd_esquema.Maestra master
group by master.Factura_Nro,master.Factura_Fecha, master.Provee_CUIT
having master.Factura_Nro is not null
insert into MR_ANDERSON.Factura_Renglon(cantidad,factura_nro,codigo)
select count(Compras.codigo), master.Factura_Nro, master.Groupon_Codigo
from gd_esquema.Maestra master
join MR_ANDERSON.Factura Factura
on master.Factura_Nro = Factura.factura_nro
join MR_ANDERSON.Cupones Cupones
on master.Groupon_Codigo = Cupones.codigo
join MR_ANDERSON.Compras Compras
on master.Groupon_Codigo = Compras.codigo
group by master.Factura_Nro, master.Groupon_Codigo
insert into MR_ANDERSON.Ciudades(dni, ciudad)
select master.Cli_Dni, master.Cli_Ciudad
from gd_esquema.Maestra master
where master.Cli_Dni is not null and master.Cli_Ciudad is not null
union
select master.Cli_Dest_Dni, master.Cli_Dest_Ciudad
from gd_esquema.Maestra master
where master.Cli_Dest_Dni is not null and master.Cli_Dest_Ciudad is not null
insert into MR_ANDERSON.Ciudades_Cupon(ciudad,codigo)
select distinct master.Cli_Ciudad, master.Groupon_Codigo
from gd_esquema.Maestra master
where master.Cli_Ciudad is not null and master.Groupon_Codigo is not null
commit tran trn_inserts_tablas
GO
--ABM ROL !
/*
create trigger actualizar_habilitaciones on MR_ANDERSON.Roles
after
update
as
begin
if update(Habilitado)
begin
declare @rol NVARCHAR(255)
declare @habilitado BIT
declare @my_fetch_status int
declare roles_a_chequear cursor
for select Rol, Habilitado
from inserted
open roles_a_chequear
fetch roles_a_chequear into @rol, @habilitado
set @my_fetch_status = @@FETCH_STATUS
while (@my_fetch_status = 0)
begin
update MR_ANDERSON.Roles set Habilitado = @habilitado
where Rol = @rol
update MR_ANDERSON.Login set Habilitado = @habilitado
where Rol = @rol and Habilitado = 1
fetch next from roles_a_chequear into @rol, @habilitado
set @my_fetch_status = @@FETCH_STATUS
end
close roles_a_chequear
deallocate roles_a_chequear
end
end
GO
*/
create procedure MR_ANDERSON.sp_new_rol (@nombre_rol NVARCHAR(255))
as
begin
if @nombre_rol not in (select Rol from MR_ANDERSON.Roles)
begin
insert into MR_ANDERSON.Roles(Rol,Habilitado)
VALUES(@nombre_rol, 1)
end
end
GO
create procedure MR_ANDERSON.sp_new_rol_tipo (@nombre_rol NVARCHAR(255), @tipo NVARCHAR(100))
as
begin
if not exists(select Rol, Tipo from Rol_Tipo where Rol = @nombre_rol and Tipo = @tipo)
begin
insert into MR_ANDERSON.Rol_Tipo(Rol,Tipo)
values(@nombre_rol, @tipo)
end
end
GO
create procedure MR_ANDERSON.sp_del_func_rol (@nombre_rol NVARCHAR(255), @Funcionalidad VARCHAR(40))
as
begin
delete from MR_ANDERSON.Funcionalidades_Roles
where Rol = @nombre_rol and Funcionalidad = @Funcionalidad
end
GO
create procedure MR_ANDERSON.sp_add_func_rol (@nombre_rol NVARCHAR(255), @Funcionalidad VARCHAR(40))
as
begin
if not exists(select Rol,Funcionalidad
from MR_ANDERSON.Funcionalidades_Roles
where Rol = @nombre_rol and Funcionalidad = @Funcionalidad)
begin
insert into MR_ANDERSON.Funcionalidades_Roles(Rol, Funcionalidad)
VALUES(@nombre_rol, @Funcionalidad)
end
end
GO
create procedure MR_ANDERSON.sp_change_status_rol (@nombre_rol NVARCHAR(255), @status BIT) --Usar para borrar/inhabilitar, habilitar
as
begin
update MR_ANDERSON.Roles
set Habilitado = @status
where Rol = @nombre_rol
update MR_ANDERSON.Login set Habilitado = @status
where Rol = @nombre_rol and Habilitado = 1
end
GO
create procedure MR_ANDERSON.sp_change_rol_name (@nombre_rol NVARCHAR(255), @nuevo_nombre_rol NVARCHAR(255))
as
begin
update MR_ANDERSON.Funcionalidades_Roles
set rol = @nuevo_nombre_rol
where rol = @nombre_rol
update MR_ANDERSON.Roles
set Rol = @nuevo_nombre_rol
where Rol = @nombre_rol
end
GO
--le cambia el rol a un determinado usuario
create procedure MR_ANDERSON.sp_change_rol_name_a_usr (@username NVARCHAR(255), @nombre_nuevo NVARCHAR(255))
as
begin
update MR_ANDERSON.Login
set rol = @nombre_nuevo
where username = @username
end
GO
create procedure MR_ANDERSON.sp_eliminar_rol (@nombre_rol NVARCHAR(255))
as
begin
delete from MR_ANDERSON.Funcionalidades_Roles
where rol = @nombre_rol
delete from MR_ANDERSON.Rol_tipo
where rol = @nombre_rol
delete from MR_ANDERSON.Roles
where rol = @nombre_rol
end
GO
--Fin ABM ROL!
-- Login !
create procedure MR_ANDERSON.sp_login (@username_sended NVARCHAR(100) , @user_password_sended NVARCHAR(255), @result NVARCHAR(20) output)
as
begin
if @username_sended is null
begin
set @result = 'LOGIN_ERROR'
return 1
end
declare @check_password nvarchar(255)
declare @check_habilitado bit
declare @check_fallidos numeric(10)
if (exists(select user_password from MR_ANDERSON.Login where username = @username_sended))
begin
-- Seleccionamos el hash 'posta'
set @check_password = (select user_password from MR_ANDERSON.Login where username = @username_sended)
-- Nos fijamos si es el primer login
if (@check_password is null)
begin
set @result = 'LOGIN_FIRST'
return 0
end
-- Seleccionamos la cantidad de intentos fallidos que tiene
set @check_habilitado = (select Habilitado from MR_ANDERSON.Login where username = @username_sended)
if (@check_habilitado = 0)
begin
set @result = 'LOGIN_OFF'
return 1
end
-- Si intentos_fallidos = 3, deshabilitar usuario
set @check_fallidos = (select intentos_fallidos from MR_ANDERSON.Login where username = @username_sended)
if (@check_fallidos >= 3)
begin
update MR_ANDERSON.Login
set Habilitado = 0
where username = @username_sended
set @result = 'LOGIN_TOO_MANY_TIMES'
return 1
end
-- Usuario correcto pero contraseña incorrecta
if (@check_password <> @user_password_sended)
begin
update MR_ANDERSON.Login
set intentos_fallidos = intentos_fallidos + 1
where username = @username_sended
set @result = 'LOGIN_PASS_ERR'
return 1
end
-- Login Correcto deberia entrar aca
if (@check_habilitado = 1 and @check_password = @user_password_sended)
begin
update MR_ANDERSON.Login
set intentos_fallidos = 0
where username = @username_sended
set @result = 'LOGIN_OK'
return 0
end
end
-- Si no se cumple ninguna de las condiciones anteriores, (usuario inexistente, otro error, etc) retornar error
set @result = 'LOGIN_ERROR'
return 1
end
GO
-- Listo Login !
-- Insert nuevo login
create procedure MR_ANDERSON.sp_insert_login (@username_sended NVARCHAR(100), @user_password_sended NVARCHAR(255), @rol_sended NVARCHAR(255),
@tipo NVARCHAR(100), @result NVARCHAR(14) output)
as
begin
if @username_sended is null or @rol_sended is null
begin
set @result = 'LOGIN_ERROR'
return 2
end
if exists(select username from MR_ANDERSON.Login where username = @username_sended)
begin
set @result = 'LOGIN_EXISTS'
return 1
end
insert into MR_ANDERSON.Login(username, user_password, last_login, intentos_fallidos, Habilitado, Rol,Tipo)
VALUES(@username_sended, @user_password_sended, NULL, 0, 1, @rol_sended,@tipo)
set @result = 'LOGIN_REG_OK'
return 0
end
GO
-- Listo insert nuevo login
-- Insert nuevo cliente
create procedure MR_ANDERSON.sp_insert_cliente (@nombre_sended NVARCHAR(255), @dni_sended NUMERIC(18), @apellido_sended NVARCHAR(255),
@telefono_sended NUMERIC(18) , @mail_sended NVARCHAR(255), @fecha_nac_sended DATETIME,
@username_sended NVARCHAR(100), @result NVARCHAR(19) output)
as
begin
if @nombre_sended is null
or @dni_sended is null
or @apellido_sended is null
or @telefono_sended is null
or @mail_sended is null
or @fecha_nac_sended is null
or @username_sended is null
begin
set @result = 'SENT_VALUE_NULL'
return 2
end
if exists(select dni from MR_ANDERSON.Datos_Clientes where dni = @dni_sended)
or exists(select username from MR_ANDERSON.Datos_Clientes where username = @username_sended)
begin
set @result = 'CLIENT_EXISTENT'
return 1
end
if exists(select telefono from MR_ANDERSON.Datos_Clientes where telefono = @telefono_sended)
begin
set @result = 'CLIENT_TEL_EXISTENT'
return 1
end
insert into MR_ANDERSON.Datos_Clientes (dni, nombre, apellido, telefono, mail, fecha_nac, username, saldo)
VALUES(@dni_sended, @nombre_sended, @apellido_sended, @telefono_sended, @mail_sended, @fecha_nac_sended, @username_sended, 10)
set @result = 'CLIENT_REG_OK'
return 0
end
GO
-- Listo insert nuevo cliente
-- Insert nuevo proveedor
create procedure MR_ANDERSON.sp_insert_proveedor (@provee_cuit_sended NVARCHAR(20), @provee_rs_sended NVARCHAR(100), @provee_telefono_sended NUMERIC(18),
@provee_rubro_sended NVARCHAR(100), @username_sended NVARCHAR(100), @nombre_contacto_sended VARCHAR(40),
@provee_email_sended NVARCHAR(255), @result NVARCHAR(20) output)
as
begin
if @provee_cuit_sended is null
or @provee_rs_sended is null
or @provee_telefono_sended is null
or @provee_rubro_sended is null
or @username_sended is null