-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebird-procedures.sql
1155 lines (894 loc) · 27.5 KB
/
firebird-procedures.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
-- DB PROCEDURES FOR FIREBIRD
-------------------------------------------------------------
-- ADD A PRODUCT
-- GET BRANDS
CREATE OR ALTER VIEW getbrands(
NAME)
AS
SELECT name
FROM BRANDS
SELECT * FROM BRANDS;
SELECT name FROM getbrands
SELECT first 1 id
FROM BRANDS b
WHERE b.NAME = 'Optimum'
-- GET TYPES
CREATE OR ALTER VIEW gettypes(
NAME)
AS
SELECT name
FROM TYPES
SELECT name FROM gettypes
EXECUTE PROCEDURE SP_ADDPRODUCT('Whey 500%','Optimum','Whey Protein',0,0,150,'900g')
--ADD A PRODUCT
CREATE OR ALTER PROCEDURE SP_ADDPRODUCT(
PNAME D_NAME,
brandname D_NAME,
typename D_NAME,
minimumquantity D_INT ,
currentquantity D_INT ,
unitvalue D_DECIMAL ,
unittype D_UNIT)
AS
DECLARE VARIABLE IDBRAND D_INT;
DECLARE VARIABLE IDTYPE D_INT;
BEGIN
SELECT first 1 id
FROM BRANDS B
WHERE B.NAME = :BRANDNAME
INTO :IDBRAND;
SELECT first 1 id
FROM TYPES t
WHERE t.NAME = :typename
INTO :IDTYPE;
INSERT INTO PRODUCTS(NAME,BRANDID,TYPEID,MINIMUMQUANTITY,CURRENTQUANTITY,UNITVALUE,UNITTYPE)
VALUES(:PNAME, :IDBRAND, :IDTYPE, :minimumquantity, :currentquantity, :unitvalue, :unittype );
END
SELECT * FROM PRODUCTS
SELECT FIRST 1 ID
FROM PRODUCTS
WHERE NAME = :PNAME
CREATE OR ALTER PROCEDURE hasproduct(PNAME D_NAME)
RETURNS (hasproduct D_INT)
AS
BEGIN
SELECT FIRST 1 ID
FROM PRODUCTS P
WHERE P.NAME = :PNAME
INTO :HASPRODUCT;
END
CREATE OR ALTER PROCEDURE hasproduct(PNAME D_NAME, BRANDNAME D_NAME)
AS
DECLARE VARIABLE BRANDID D_INT;
BEGIN
SELECT FIRST 1 ID
FROM BRANDS B
WHERE B.NAME = :BRANDNAME
INTO :BRANDID;
IF ( EXISTS (SELECT FIRST 1 P.NAME
FROM PRODUCTS P
WHERE P.NAME = :PNAME AND P.BRANDID = :BRANDID ))
THEN
EXCEPTION E_PRODUTO_IGUAL;
END
SELECT * FROM PRODUCTS;
EXECUTE PROCEDURE hasproduct('Whey 100%','ProStar')
---------------------------------------------------------------------
-- SEARCH PRODUCTS
CREATE OR ALTER PROCEDURE get_a_product(prodname D_NAME)
RETURNS (
pname D_NAME,
brandname D_NAME,
typename D_NAME,
minimumquantity D_INT,
currentquantity D_INT,
unitvalue D_DECIMAL,
unittype D_UNIT
)
AS
BEGIN
SELECT p.NAME, b.NAME, t.NAME, p.MINIMUMQUANTITY, p.CURRENTQUANTITY, p.UNITVALUE, p.UNITTYPE
FROM PRODUCTS p
INNER JOIN BRANDS B ON p.BRANDID = b.ID
INNER JOIN TYPES T ON p.TYPEID = t.ID
WHERE p.NAME = :prodname
INTO :pname, :brandname, :typename, :minimumquantity, :currentquantity, :unitvalue, :unittype;
END
EXECUTE PROCEDURE get_a_product('Whey 100%')
CREATE OR ALTER VIEW search_products(pname,brandname,typename,minimumquantity,currentquantity,unitvalue,unittype)
AS
SELECT p.NAME, b.NAME, t.NAME, p.MINIMUMQUANTITY, p.CURRENTQUANTITY, p.UNITVALUE, p.UNITTYPE
FROM PRODUCTS p
INNER JOIN BRANDS B ON p.BRANDID = b.ID
INNER JOIN TYPES T ON p.TYPEID = t.ID
SELECT * FROM search_products;
CREATE OR ALTER VIEW getbrands(
NAME)
AS
SELECT name
FROM BRANDS
---------------------------------------------------------------------
-- ADD A SALE
DROP VIEW get_clients;
CREATE OR ALTER VIEW get_clients(cname)
AS
SELECT c.NAME
FROM CLIENTS c
SELECT cname FROM get_clients
CREATE OR ALTER PROCEDURE addSale(subtotal D_DECIMAL,
total D_DECIMAL,
clientname D_NAME,
discount D_DECIMAL,
finalized D_FINALIZED)
AS
DECLARE VARIABLE idclient D_INT;
BEGIN
SELECT c.ID
FROM CLIENTS C
WHERE C.NAME = :clientname
INTO :idclient;
INSERT INTO SALES(SALEDATE,SUBTOTAL,TOTAL,CLIENTID,DISCOUNT,FINALIZED)
VALUES(CURRENT_TIMESTAMP, :SUBTOTAL, :total, :idclient, :discount, :finalized);
END
SELECT * FROM CLIENTS;
EXECUTE PROCEDURE addSale(0.0,0.0,'Vinicius',0.0,'N')
SELECT * FROM sales;
SELECT max(id) FROM sales;
CREATE EXCEPTION INVOICEEXISTS 'Invoice already exists!';
-- CHECK IF INVOICE ALREADY EXISTS
CREATE OR ALTER PROCEDURE checkInvoiceExists(inputinvoice D_INVOICE)
AS
BEGIN
IF(EXISTS (SELECT id
FROM sales s
WHERE s.INVOICE = :inputinvoice))THEN
EXCEPTION INVOICEEXISTS;
END
SELECT * FROM CLIENTS;
EXECUTE PROCEDURE checkInvoiceExists('987654321');
SELECT * FROM SALES;
SELECT * FROM PURCHASES;
CREATE EXCEPTION NO_SALEID 'No sale found! Access Violation.';
-- SET INVOICE TO THE SALE
CREATE OR ALTER PROCEDURE setInvoice(idsale D_INT, inputinvoice D_INVOICE)
AS
BEGIN
IF( EXISTS(SELECT id
FROM SALES s
WHERE s.ID = :idsale))
THEN
BEGIN
UPDATE SALES
SET INVOICE = :inputinvoice
WHERE id = :idsale;
END
ELSE
EXCEPTION NO_SALEID;
END
SELECT * FROM sales;
EXECUTE PROCEDURE setInvoice(2 , '987654321');
CREATE OR ALTER PROCEDURE finishSale(saleid D_INT, input_subtotal D_DECIMAL, input_total D_DECIMAL)
AS
BEGIN
IF( EXISTS(SELECT id
FROM SALES s
WHERE s.ID = :saleid))
THEN
BEGIN
UPDATE SALES
SET FINALIZED = 'Y',SUBTOTAL = :input_subtotal, TOTAL = :input_total
WHERE id = :saleid;
END
ELSE
EXCEPTION NO_SALEID;
END
SELECT * FROM sales;
EXECUTE PROCEDURE finishSale(1 , 50.0,100.0);
CREATE OR ALTER PROCEDURE removeSale(currentSaleID D_INT)
AS
BEGIN
DELETE FROM saleitems
WHERE SALEID = :currentSaleID;
DELETE FROM SALES
WHERE ID = :currentSaleID;
END
SELECT * FROM saleitems;
SELECT * FROM ;
EXECUTE PROCEDURE removeSale(1);
---------------------------------------------------------------------
-- ADD A SALEITEM
-- ADD ITEM PURCHASE
CREATE OR ALTER PROCEDURE add_saleitem(
saleid_input D_INT,
prodname_input D_NAME,
unitvalue_input D_DECIMAL,
quantity_input D_INT,
total_input D_DECIMAL)
AS
DECLARE IDPROD D_INT;
BEGIN
IF( EXISTS(SELECT p.ID
FROM PRODUCTS p
WHERE p.NAME = :prodname_input))
THEN
BEGIN
SELECT p.ID
FROM PRODUCTS p
WHERE p.NAME = :prodname_input
INTO :IDPROD;
INSERT INTO SALEITEMS(SALEID ,PRODID,UNITVALUE,QUANTITY,TOTAL)
VALUES (:saleid_input, :IDPROD, :unitvalue_input, :quantity_input, :total_input );
END
ELSE
EXCEPTION NO_PRODUCT;
END
SELECT * FROM sales;
SELECT * FROM ;
EXECUTE PROCEDURE add_saleitem(2,'Whey 100%', 150.0, 10, 1500);
CREATE EXCEPTION SIT_ALREADYADDED 'Sale Item already Added!';
DROP PROCEDURE saleItem_exists;
-- CHECK IF SALEITEM HAS NOT BEEN ADDED
CREATE OR ALTER PROCEDURE saleItem_exists(currentSaleID D_INT, pname D_NAME)
AS
DECLARE IDPROD D_INT;
BEGIN
IF( EXISTS(SELECT p.ID
FROM PRODUCTS p
WHERE p.NAME = :pname))
THEN
BEGIN
SELECT p.ID
FROM PRODUCTS p
WHERE p.NAME = :pname
INTO :IDPROD;
IF( EXISTS (SELECT UNITVALUE
FROM SALEITEMS sit
WHERE sit.PRODID = :IDPROD AND sit.SALEID = :currentSaleID))
THEN
EXCEPTION SIT_ALREADYADDED;
END
ELSE
EXCEPTION NO_PRODUCT;
END
EXECUTE PROCEDURE saleItem_exists(2,'Whey 100%');
EXECUTE PROCEDURE saleItem_exists(10,'Whey 100%');
SELECT * FROM sales;
SELECT * FROM ;
---------------------------------------------------------------------
-- ADD A PURCHASE
CREATE OR ALTER VIEW get_suppliers(cname)
AS
SELECT s.FANTASYNAME
FROM SUPPLIERS s
SELECT cname FROM get_suppliers;
CREATE EXCEPTION NO_SUPPLIER 'No Supplier Found!';
--ADD A PURCHASE
CREATE OR ALTER PROCEDURE addPurchase(subtotal D_DECIMAL,
total D_DECIMAL,
suppliername D_NAME,
discount D_DECIMAL,
finalized D_FINALIZED)
AS
DECLARE VARIABLE idsupplier D_INT;
BEGIN
IF( EXISTS (SELECT s.ID
FROM SUPPLIERS s
WHERE s.FANTASYNAME = :suppliername ))
THEN
BEGIN
SELECT s.ID
FROM SUPPLIERS s
WHERE s.FANTASYNAME = :suppliername
INTO idsupplier;
INSERT INTO PURCHASES(PURCHASEDATE,SUBTOTAL,TOTAL,SUPPLIERID,DISCOUNT,FINALIZED)
VALUES(CURRENT_TIMESTAMP, :SUBTOTAL, :total, :idsupplier, :discount, :finalized);
END
ELSE
EXCEPTION NO_SUPPLIER;
END
SELECT * FROM SUPPLIERS;
EXECUTE PROCEDURE addPurchase(0.0,0.0,'Batata Doce Suplementos',0.0,'N');
SELECT * FROM PURCHASES;
CREATE OR ALTER PROCEDURE checkPurchaseInvoiceExists(inputinvoice D_INVOICE)
AS
BEGIN
IF(EXISTS (SELECT id
FROM PURCHASES p
WHERE p.INVOICE = :inputinvoice))THEN
EXCEPTION INVOICEEXISTS;
END
SELECT * FROM PURCHASES;
EXECUTE PROCEDURE checkPurchaseInvoiceExists('123456799');
CREATE EXCEPTION NO_PURCHASEID 'No Purchase with this id has been found! Access Violation.';
CREATE OR ALTER PROCEDURE finishPurchase(purchaseID D_INT, input_subtotal D_DECIMAL, input_total D_DECIMAL)
AS
BEGIN
IF( EXISTS(SELECT id
FROM PURCHASES P
WHERE P.ID = :purchaseID))
THEN
BEGIN
UPDATE PURCHASES
SET FINALIZED = 'Y',SUBTOTAL = :input_subtotal, TOTAL = :input_total
WHERE id = :purchaseID;
END
ELSE
EXCEPTION NO_PURCHASEID;
END
SELECT * FROM PURCHASES;
EXECUTE PROCEDURE finishPurchase(5,100,150);
-- SET INVOICE
CREATE OR ALTER PROCEDURE setInvoicePurchase(idpurchase D_INT, inputinvoice D_INVOICE)
AS
BEGIN
IF( EXISTS(SELECT id
FROM PURCHASES p
WHERE p.ID = :idpurchase))
THEN
BEGIN
UPDATE PURCHASES
SET INVOICE = :inputinvoice
WHERE id = :idpurchase;
END
ELSE
EXCEPTION NO_PURCHASEID;
END
SELECT * FROM PURCHASES;
EXECUTE PROCEDURE setInvoicePurchase(2 , '987654322');
SELECT * FROM sales;
DROP PROCEDURE
-- REMOVE SALE AND IT'S purchaseItems
CREATE OR ALTER PROCEDURE removePurchase(currentPurchaseID D_INT)
AS
BEGIN
DELETE FROM PURCHASEITEMS
WHERE PURCHASEID = :currentPurchaseID;
DELETE FROM PURCHASES
WHERE ID = :currentPurchaseID;
END
SELECT * FROM PURCHASES;
SELECT * FROM PURCHASEITEMS;
EXECUTE PROCEDURE removePurchase(2);
---------------------------------------------------------------------
-- ADD A PURCHASEITEM
DROP VIEW get_products_additem;
-- VIEW TO GET PRODUCTS
CREATE OR ALTER VIEW get_products_additem(productname)
AS
SELECT P.NAME
FROM PRODUCTS P
SELECT productname FROM get_products_purchaseitem;
CREATE EXCEPTION NO_PRODUCT 'No product registered with this Name!';
CREATE OR ALTER PROCEDURE get_itemunitvalue(pname D_NAME)
RETURNS (unitvalue D_DECIMAL)
AS
BEGIN
IF( EXISTS(SELECT id
FROM PRODUCTS p
WHERE p.NAME = :pname))
THEN
BEGIN
SELECT p.UNITVALUE
FROM PRODUCTS p
WHERE p.NAME = :pname
INTO :unitvalue;
END
ELSE
EXCEPTION NO_PRODUCT;
END
SELECT * FROM PRODUCTS;
EXECUTE PROCEDURE get_itemunitvalue('Whey 100%');
CREATE OR ALTER PROCEDURE get_a_typeby_product(pname D_NAME)
RETURNS (typename D_NAME)
AS
DECLARE IDTYPE D_INT;
BEGIN
IF( EXISTS(SELECT id
FROM PRODUCTS p
WHERE p.NAME = :pname))
THEN
BEGIN
SELECT p.TYPEID
FROM PRODUCTS P
WHERE p.NAME = :pname
INTO :IDTYPE;
SELECT t.NAME
FROM TYPES t
WHERE t.ID = :IDTYPE
INTO :typename;
END
ELSE
EXCEPTION NO_PRODUCT;
END
EXECUTE PROCEDURE get_a_typeby_product('Whey 100%')
CREATE OR ALTER PROCEDURE get_a_brandby_product(pname D_NAME)
RETURNS (brandname D_NAME)
AS
DECLARE IDBRAND D_INT;
BEGIN
IF( EXISTS(SELECT id
FROM PRODUCTS p
WHERE p.NAME = :pname))
THEN
BEGIN
SELECT p.BRANDID
FROM PRODUCTS P
WHERE p.NAME = :pname
INTO :IDBRAND;
SELECT b.NAME
FROM BRANDS b
WHERE b.ID = :IDBRAND
INTO :brandname;
END
ELSE
EXCEPTION NO_PRODUCT;
END
EXECUTE PROCEDURE get_a_brandby_product('Whey 100%');
SELECT * FROM PURCHASES;
-- ADD ITEM PURCHASE
CREATE OR ALTER PROCEDURE add_purchaseitem(
purchaseid_input D_INT,
prodname_input D_NAME,
unitvalue_input D_DECIMAL,
quantity_input D_INT,
total_input D_DECIMAL)
AS
DECLARE IDPROD D_INT;
BEGIN
IF( EXISTS(SELECT p.ID
FROM PRODUCTS p
WHERE p.NAME = :prodname_input))
THEN
BEGIN
SELECT p.ID
FROM PRODUCTS p
WHERE p.NAME = :prodname_input
INTO :IDPROD;
INSERT INTO PURCHASEITEMS(purchaseid ,PRODID,UNITVALUE,QUANTITY,TOTAL)
VALUES (:purchaseid_input, :IDPROD, :unitvalue_input, :quantity_input, :total_input );
END
ELSE
EXCEPTION NO_PRODUCT;
END
SELECT * FROM sales;
DROP PROCEDURE add_purchaseitem;
EXECUTE PROCEDURE purchaseItem_exists (2,'Top Whey',150,10,1500.0);
CREATE EXCEPTION PIT_ALREADYADDED 'PurchaseItem already Added!';
-- CHECK IF PURCHASE ITEM HAS NOT BEEN ADDED
CREATE OR ALTER PROCEDURE purchaseItem_exists(currentPurchaseID D_INT, pname D_NAME)
AS
DECLARE IDPROD D_INT;
BEGIN
IF( EXISTS(SELECT p.ID
FROM PRODUCTS p
WHERE p.NAME = :pname))
THEN
BEGIN
SELECT p.ID
FROM PRODUCTS p
WHERE p.NAME = :pname
INTO :IDPROD;
IF( EXISTS (SELECT UNITVALUE
FROM PURCHASEITEMS pit
WHERE pit.PRODID = :IDPROD AND pit.PURCHASEID = :currentPurchaseID))
THEN
EXCEPTION PIT_ALREADYADDED;
END
ELSE
EXCEPTION NO_PRODUCT;
END
SELECT * FROM PRODUCTS;
SELECT * FROM PURCHASEITEMS;
SELECT * FROM PURCHASES;
EXECUTE PROCEDURE purchaseItem_exists(1,'Whey 100%');
CREATE OR ALTER PROCEDURE removePurchase(currentPurchaseID D_INT)
AS
BEGIN
DELETE FROM PURCHASEITEMS
WHERE PURCHASEID = :currentPurchaseID;
DELETE FROM PURCHASES
WHERE ID = :currentPurchaseID;
END
EXECUTE PROCEDURE removePurchase(1);
SELECT * FROM PURCHASES;
SELECT * FROM PURCHASEITEMS;
------------------------------------------------------------
-- SEARCH PURCHASE
-- SEARCH PURCHASES
CREATE OR ALTER VIEW searchPurchase(fantasyname,invoice,subtotal, discount , total,purchasedate )
AS
SELECT s.FANTASYNAME, p.INVOICE, p.SUBTOTAL, p.DISCOUNT, p.TOTAL ,p.PURCHASEDATE
FROM PURCHASES p
INNER JOIN SUPPLIERS s ON p.SUPPLIERID = s.ID
WHERE p.FINALIZED = 'Y'
SELECT * FROM PURCHASES;
SELECT FIRST 50 * FROM searchPurchase;
-- SEARCH a SPECIFIC PURCHASE
CREATE OR ALTER PROCEDURE search_a_purchase(input_fantasyName D_NAME)
RETURNS (
fantasyname D_NAME,
invoice D_INVOICE,
subtotal D_DECIMAL,
discount D_DECIMAL,
total D_DECIMAL,
purchasedate D_DATE
)
AS
DECLARE VARIABLE idSupplier D_INT;
BEGIN
SELECT FIRST 1 ID
FROM SUPPLIERS
WHERE FANTASYNAME = :input_fantasyName
INTO :idSupplier;
FOR SELECT s.FANTASYNAME, p.INVOICE, p.SUBTOTAL, p.DISCOUNT, p.TOTAL, p.PURCHASEDATE
FROM PURCHASES P
INNER JOIN SUPPLIERS S ON P.SUPPLIERID = S.ID
WHERE P.SUPPLIERID = :idSupplier AND p.FINALIZED = 'Y'
INTO :fantasyname, :invoice, :subtotal, :discount, :total, :purchasedate
DO
SUSPEND;
END
SELECT * FROM SUPPLIERS;
SELECT * FROM search_a_purchase('Batata Doce Suplementos');
------------------------------------------------------------
-- SEARCH SALE
DROP VIEW searchSale;
-- SEARCH PURCHASES
CREATE OR ALTER VIEW searchSale(clientname,invoice,subtotal, discount , total, saledate )
AS
SELECT c.NAME, s.INVOICE, s.SUBTOTAL, s.DISCOUNT, s.TOTAL ,s.SALEDATE
FROM SALES s
INNER JOIN CLIENTS c ON s.CLIENTID = c.ID
WHERE s.FINALIZED = 'Y'
SELECT * FROM SALES;
SELECT FIRST 50 * FROM searchSale;
DROP PROCEDURE search_a_sale;
DROP PROCEDURE search_a_sale;
-- SEARCH a SPECIFIC PURCHASE
CREATE OR ALTER PROCEDURE search_a_sale(input_clientname D_NAME)
RETURNS (
clientname D_NAME,
invoice D_INVOICE,
subtotal D_DECIMAL,
discount D_DECIMAL,
total D_DECIMAL,
saledate D_DATE
)
AS
DECLARE VARIABLE idClient D_INT;
DECLARE VARIABLE idSale D_INT;
BEGIN
SELECT FIRST 1 ID
FROM CLIENTS
WHERE NAME = :input_clientname
INTO :idClient;
FOR SELECT c.NAME, s.INVOICE, s.SUBTOTAL, s.DISCOUNT, s.TOTAL, s.SALEDATE
FROM SALES s
INNER JOIN CLIENTS c ON s.CLIENTID = c.ID
WHERE s.FINALIZED = 'Y' AND S.CLIENTID = :idClient
INTO :clientname, :invoice, :subtotal, :discount, :total, :saledate
DO
SUSPEND;
END
SELECT * FROM CLIENTS;
SELECT * FROM SALES;
SELECT * FROM search_a_sale('Vinicius');
------------------------------------------------------------
-- CHECK HISTORY
CREATE EXCEPTION NO_SALE_OR_PURCHASE 'No sale or Purchase found with given date interval';
CREATE OR ALTER PROCEDURE checkAllHistory
RETURNS (
tipo D_NAME,
productname D_NAME,
invoice D_INVOICE,
quantity D_INT,
unitvalue D_DECIMAL,
total D_DECIMAL,
actiondate D_DATE
)
AS
DECLARE VARIABLE idsale D_INT;
DECLARE VARIABLE idItem D_INT;
DECLARE VARIABLE idpurchase D_INT;
BEGIN
IF( EXISTS (SELECT ID
FROM SALES s
WHERE s.FINALIZED = 'Y'
) OR EXISTS(SELECT ID
FROM PURCHASES p
WHERE p.FINALIZED = 'Y' ))
THEN
BEGIN
FOR SELECT S.ID, sit.PRODID
FROM SALES s
INNER JOIN SALEITEMS sit ON s.ID = sit.SALEID
WHERE s.FINALIZED = 'Y'
INTO :idsale, :idItem
DO
BEGIN
FOR SELECT DISTINCT s.INVOICE, prod.NAME ,sit.QUANTITY, sit.UNITVALUE, sit.TOTAL, s.SALEDATE
FROM SALEITEMS sit
INNER JOIN PRODUCTS prod ON sit.PRODID = prod.ID
INNER JOIN SALES s ON sit.SALEID = s.ID
WHERE sit.SALEID = :idsale AND sit.PRODID = :idItem
INTO :invoice, :productname, :quantity, :unitvalue, :total, :actiondate
DO
tipo = 'Sale';
SUSPEND;
END
FOR SELECT p.ID, pit.PRODID
FROM PURCHASES p
INNER JOIN PURCHASEITEMS pit ON p.ID = pit.PURCHASEID
WHERE p.FINALIZED = 'Y'
INTO :idpurchase, :idItem
DO
BEGIN
FOR SELECT DISTINCT p.INVOICE, prod.NAME, pit.QUANTITY, pit.UNITVALUE, pit.TOTAL, p.PURCHASEDATE
FROM PURCHASEITEMS pit
INNER JOIN PRODUCTS prod ON pit.PRODID = prod.ID
INNER JOIN PURCHASES p ON pit.PURCHASEID = p.ID
WHERE pit.PURCHASEID = :idpurchase AND pit.PRODID = :idItem
INTO :invoice, :productname, :quantity, :unitvalue, :total, :actiondate
DO
tipo = 'Purchase';
SUSPEND;
END
END
ELSE
EXCEPTION NO_SALE_OR_PURCHASE;
END
SELECT * FROM checkAllHistory;
CREATE OR ALTER PROCEDURE checkHistoryRange(input_data1 D_DATE, input_data2 D_DATE)
RETURNS (
tipo D_NAME,
productname D_NAME,
invoice D_INVOICE,
quantity D_INT,
unitvalue D_DECIMAL,
total D_DECIMAL,
actiondate D_DATE
)
AS
DECLARE VARIABLE idsale D_INT;
DECLARE VARIABLE idItem D_INT;
DECLARE VARIABLE idpurchase D_INT;
BEGIN
IF( EXISTS (SELECT ID
FROM SALES s
WHERE s.FINALIZED = 'Y'
AND s.SALEDATE BETWEEN :input_data1 AND :input_data2) OR EXISTS(SELECT ID
FROM PURCHASES p
WHERE p.FINALIZED = 'Y' AND p.PURCHASEDATE BETWEEN :input_data1 AND :input_data2 ))
THEN
BEGIN
FOR SELECT S.ID, sit.PRODID
FROM SALES s
INNER JOIN SALEITEMS sit ON s.ID = sit.SALEID
WHERE s.FINALIZED = 'Y' AND s.SALEDATE BETWEEN :input_data1 AND :input_data2
INTO :idsale, :idItem
DO
BEGIN
FOR SELECT DISTINCT s.INVOICE, prod.NAME ,sit.QUANTITY, sit.UNITVALUE, sit.TOTAL, s.SALEDATE
FROM SALEITEMS sit
INNER JOIN PRODUCTS prod ON sit.PRODID = prod.ID
INNER JOIN SALES s ON sit.SALEID = s.ID
WHERE sit.SALEID = :idsale AND sit.PRODID = :idItem
INTO :invoice, :productname, :quantity, :unitvalue, :total, :actiondate
DO
tipo = 'Sale';
SUSPEND;
END
FOR SELECT p.ID, pit.PRODID
FROM PURCHASES p
INNER JOIN PURCHASEITEMS pit ON p.ID = pit.PURCHASEID
WHERE p.FINALIZED = 'Y' AND p.PURCHASEDATE BETWEEN :input_data1 AND :input_data2
INTO :idpurchase, :idItem
DO
BEGIN
FOR SELECT DISTINCT p.INVOICE, prod.NAME, pit.QUANTITY, pit.UNITVALUE, pit.TOTAL, p.PURCHASEDATE
FROM PURCHASEITEMS pit
INNER JOIN PRODUCTS prod ON pit.PRODID = prod.ID
INNER JOIN PURCHASES p ON pit.PURCHASEID = p.ID
WHERE pit.PURCHASEID = :idpurchase AND pit.PRODID = :idItem
INTO :invoice, :productname, :quantity, :unitvalue, :total, :actiondate
DO
tipo = 'Purchase';
SUSPEND;
END
END
ELSE
EXCEPTION NO_SALE_OR_PURCHASE;
END
----------------------------------------------------------------------------------------
-- Top Product Sales
CREATE OR ALTER PROCEDURE listTopProductSales(input_date VARCHAR(4))
RETURNS(
prodname D_NAME,
prodsales D_INT,
prodsalecash D_DECIMAL,
dateyear VARCHAR(4)
)
AS
BEGIN
--PEGAR PRODUTOS MAIS VENDIDOS
FOR SELECT SUM(sit.QUANTITY) NUMBERSALES, SUM(SIT.QUANTITY * SIT.UNITVALUE) TOTALCASH, p.NAME PRODUCT
FROM SALEITEMS sit
INNER JOIN PRODUCTS p ON sit.PRODID = p.ID
INNER JOIN SALES s ON sit.SALEID = s.ID
WHERE EXTRACT(YEAR FROM s.SALEDATE) = :input_date
GROUP BY sit.PRODID, p.NAME
ORDER BY SUM(sit.QUANTITY) DESC
INTO :prodsales, :prodsalecash, :prodname
DO
BEGIN
dateyear = :input_date;
SUSPEND;
END
END
SELECT * FROM listTopProductSales('2017');
SELECT * FROM SALEITEMS;
SELECT * FROM PRODUCTS;
----------------------------------------------------------------------------------------
-- Worst Product Sales
CREATE OR ALTER PROCEDURE listWorstProductSales(input_date VARCHAR(4))
RETURNS(
prodname D_NAME,
prodsales D_INT,
prodsalecash D_DECIMAL,
dateyear VARCHAR(4)
)
AS
BEGIN
FOR SELECT SUM(sit.QUANTITY) NUMBERSALES, SUM(SIT.QUANTITY * SIT.UNITVALUE) TOTALCASH, p.NAME PRODUCT
FROM SALEITEMS sit
INNER JOIN PRODUCTS p ON sit.PRODID = p.ID
INNER JOIN SALES s ON sit.SALEID = s.ID
WHERE EXTRACT(YEAR FROM s.SALEDATE) = :input_date
GROUP BY sit.PRODID, p.NAME
ORDER BY SUM(sit.QUANTITY) ASC
INTO :prodsales, :prodsalecash, :prodname
DO
BEGIN
dateyear = :input_date;
SUSPEND;
END
END
SELECT * FROM listWorstProductSales('2017');
SELECT * FROM SALEITEMS;
SELECT * FROM PRODUCTS;
---------------------------------------------------------------------
-- CLIENTS THAT BUYS THE MOST
CREATE OR ALTER PROCEDURE bestbuyers
RETURNS(
clientname D_NAME,
numbuys D_INT,
totalcash D_DECIMAL
)
AS
BEGIN