-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstore_management.sql
More file actions
527 lines (451 loc) · 13.3 KB
/
store_management.sql
File metadata and controls
527 lines (451 loc) · 13.3 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
-- phpMyAdmin SQL Dump
-- version 4.5.1
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Nov 15, 2016 at 07:01 PM
-- Server version: 10.1.16-MariaDB
-- PHP Version: 5.6.24
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `wholesale_management`
--
DELIMITER $$
--
-- Procedures
--
CREATE DEFINER=`root`@`localhost` PROCEDURE `discount_calc` (IN `product` INT(10), IN `quant` INT(10), OUT `disc` INT(10)) BEGIN
declare price int;
declare disc int;
declare total int;
select USP into price from price_list where ProductID = product;
set total=quant*price;
if (total >= 20000 and total < 40000) THEN
set disc=total*0.025;
elseif (total >= 40000 and total < 60000) THEN
set disc=total*0.05;
elseif (total >= 60000 and total < 100000) THEN
set disc=total*0.075;
elseif (total >= 100000) THEN
set disc=total*0.1;
end if;
end$$
DELIMITER ;
-- --------------------------------------------------------
--
-- Table structure for table `category`
--
CREATE TABLE `category` (
`CategoryID` int(11) NOT NULL,
`CategoryName` varchar(30) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `category`
--
INSERT INTO `category` (`CategoryID`, `CategoryName`) VALUES
(1, 'Food'),
(2, 'Cosmetics'),
(3, 'Personal Hygiene'),
(4, 'Pets');
-- --------------------------------------------------------
--
-- Table structure for table `customer_information`
--
CREATE TABLE `customer_information` (
`CustomerID` varchar(30) NOT NULL,
`Name` varchar(30) NOT NULL,
`Address` varchar(50) NOT NULL,
`Phone` varchar(15) NOT NULL,
`Password` varchar(15) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `customer_information`
--
INSERT INTO `customer_information` (`CustomerID`, `Name`, `Address`, `Phone`, `Password`) VALUES
('A1', 'Usman Yaqoob', 'Gulshan', '03231234509', 'abc12'),
('A2', 'Manahil Fatima', 'Johar', '03351356204', 'mypassword'),
('A3', 'Anas Ahmed', 'DHA', '03247894651', 'whocares');
-- --------------------------------------------------------
--
-- Table structure for table `depleted_product`
--
CREATE TABLE `depleted_product` (
`ProductID` int(11) NOT NULL,
`Quantity` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table `payment`
--
CREATE TABLE `payment` (
`TransactionID` int(11) NOT NULL,
`Amount_Paid` int(11) NOT NULL,
`Mode` varchar(30) NOT NULL,
`Transaction_Date` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `payment`
--
INSERT INTO `payment` (`TransactionID`, `Amount_Paid`, `Mode`, `Transaction_Date`) VALUES
(16, 3400, 'credit card', 2022),
(23, 5440, 'cash', 2022),
(27, 1560, 'debit card', 2022),
(28, 5254, 'debit card', 2022);
-- --------------------------------------------------------
--
-- Table structure for table `price_list`
--
CREATE TABLE `price_list` (
`ProductID` int(11) NOT NULL,
`USP` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `price_list`
--
INSERT INTO `price_list` (`ProductID`, `USP`) VALUES
(1, 230),
(2, 100),
(3, 560),
(4, 40),
(5, 30);
-- --------------------------------------------------------
--
-- Table structure for table `product`
--
CREATE TABLE `product` (
`ProductID` int(11) NOT NULL,
`Pname` varchar(30) NOT NULL,
`CategoryID` int(11) NOT NULL,
`SupplierID` int(11) NOT NULL,
`Quantity_in_stock` int(11) NOT NULL,
`UnitPrice` int(11) NOT NULL,
`ReorderLevel` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `product`
--
INSERT INTO `product` (`ProductID`, `Pname`, `CategoryID`, `SupplierID`, `Quantity_in_stock`, `UnitPrice`, `ReorderLevel`) VALUES
(1, 'KnS Qeema Extra Lean', 1, 12, 150, 480, 50),
(2, 'Pringles Salted', 1, 12, 55, 100, 30),
(3, 'Blue for Women Deodorant', 2, 2, 20, 300, 20),
(4, 'Maybelline Sky High Mascara', 3, 6, 30, 680, 10),
(5, 'Fluffy Cat Food', 4, 8, 102, 500, 20);
--
-- Triggers `product`
--
DELIMITER $$
CREATE TRIGGER `depleted_check_update` BEFORE UPDATE ON `product` FOR EACH ROW BEGIN
Declare finished integer default 0;
Declare cust varchar(30);
declare flag integer default 0;
Declare c1 cursor for select ProductID from depleted_product;
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET finished = 1;
if NEW.Quantity_in_stock < NEW.ReorderLevel THEN
insert into depleted_product(ProductID,Quantity) values(NEW.ProductID,NEW.Quantity_in_stock);
else
open c1;
get_cust: LOOP
Fetch c1 into cust;
if finished=1 then
leave get_cust;
end if;
if cust=NEW.ProductID then
set finished=1;
set flag=1;
end if;
end loop get_cust;
close c1;
if flag=1 then
Delete from depleted_product where ProductID=NEW.ProductID;
END if;
end if;
END
$$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER `supplier_check` BEFORE INSERT ON `product` FOR EACH ROW BEGIN
Declare finished integer default 0;
Declare cust varchar(30);
declare flag integer default 0;
Declare c1 cursor for select supplierID from supplier_information;
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET finished = 1;
if NEW.Quantity_in_stock < NEW.ReorderLevel THEN
insert into depleted_product(ProductID,Quantity) values(NEW.ProductID, NEW.Quantity_in_stock);
end if;
open c1;
get_cust: LOOP
Fetch c1 into cust;
if finished=1 then
leave get_cust;
end if;
if cust=NEW.SupplierID then
set flag=1;
end if;
end loop get_cust;
close c1;
if flag=0 then
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Supplier does not exist';
END if;
END
$$
DELIMITER ;
-- --------------------------------------------------------
--
-- Table structure for table `supplier_information`
--
CREATE TABLE `supplier_information` (
`SupplierID` int(11) NOT NULL,
`SName` varchar(30) NOT NULL,
`Address` varchar(50) NOT NULL,
`Phone` varchar(15) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `supplier_information`
--
INSERT INTO `supplier_information` (`SupplierID`, `SName`, `Address`, `Phone`) VALUES
(2, 'Arham', 'Saddar', '03011567430'),
(6, 'Lohit', 'Gulshan', '03318746556'),
(8, 'Sarim', 'Malir', '03231264454'),
(12, 'Farzan', 'Layari', '03003325459');
-- --------------------------------------------------------
--
-- Table structure for table `transaction_detail`
--
CREATE TABLE `transaction_detail` (
`TransactionID` int(11) NOT NULL,
`ProductID` int(11) NOT NULL,
`Quantity` int(11) NOT NULL,
`Discount` int(11) NOT NULL DEFAULT '0',
`Total_Amount` int(11) NOT NULL,
`Trans_Init_Date` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `transaction_detail`
--
INSERT INTO `transaction_detail` (`TransactionID`, `ProductID`, `Quantity`, `Discount`, `Total_Amount`, `Trans_Init_Date`) VALUES
(16, 4, 2, 0, 960, '2022-12-1'),
(16, 5, 5, 0, 2500, '2022-12-1'),
(23, 1, 5, 0, 2400, '2022-12-3'),
(23, 2, 10, 0, 1000, '2022-12-3'),
(27, 1, 15, 0, 7200, '2022-12-4'),
(27, 2, 30, 0, 3000, '2022-12-4'),
(27, 3, 12, 0, 3600, '2022-12-4'),
(28, 5, 10, 0, 5000, '2022-12-4');
--
-- Triggers `transaction_detail`
--
DELIMITER $$
CREATE TRIGGER `max_min_quantity` BEFORE INSERT ON `transaction_detail` FOR EACH ROW BEGIN
declare var1 int;
declare var2 int;
select ReorderLevel into var1 from Product where ProductID = NEW.ProductID;
select Quantity_in_stock into var2 from Product where ProductID = NEW.ProductID;
if new.Quantity<var1 THEN
signal sqlstate '45000' set message_text = 'Less than min quantity';
end if;
if new.Quantity>var2 THEN
signal sqlstate '45000' set message_text = 'More than max quantity';
end if;
update product set Quantity_in_stock = Quantity_in_stock - NEW.Quantity where ProductID = NEW.ProductID;
END
$$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER `max_min_quantity_update` BEFORE UPDATE ON `transaction_detail` FOR EACH ROW BEGIN
declare var1 int;
declare var2 int;
select ReorderLevel into var1 from Product where ProductID = NEW.ProductID;
select Quantity_in_stock into var2 from Product where ProductID = NEW.ProductID;
if new.Quantity<var1 THEN
signal sqlstate '45000' set message_text = 'Less than min quantity';
end if;
if new.Quantity>var2 THEN
signal sqlstate '45000' set message_text = 'More than max quantity';
end if;
update product set Quantity_in_stock = Quantity_in_stock - NEW.Quantity where ProductID=NEW.ProductID;
END
$$
DELIMITER ;
-- --------------------------------------------------------
--
-- Table structure for table `transaction_information`
--
CREATE TABLE `transaction_information` (
`TransactionID` int(11) NOT NULL,
`CustomerID` varchar(30) NOT NULL,
`Trans_Init_Date` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `transaction_information`
--
INSERT INTO `transaction_information` (`TransactionID`, `CustomerID`, `Trans_Init_Date`) VALUES
(16, 'A1', '2022-12-4'),
(23, 'A2', '2022-12-3'),
(27, 'A2', '2022-12-3'),
(28, 'A3', '2022-12-1');
--
-- Triggers `transaction_information`
--
DELIMITER $$
CREATE TRIGGER `customer_check` BEFORE INSERT ON `transaction_information` FOR EACH ROW BEGIN
Declare finished integer default 0;
Declare cust varchar(30);
declare flag integer default 0;
Declare c1 cursor for select customerID from customer_information;
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET finished = 1;
open c1;
get_cust: LOOP
Fetch c1 into cust;
if finished=1 then
leave get_cust;
end if;
if cust=NEW.CustomerID then
set flag=1;
end if;
end loop get_cust;
close c1;
if flag=0 then
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Customer does not exist';
END if;
END
$$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER `customer_check_update` BEFORE UPDATE ON `transaction_information` FOR EACH ROW BEGIN
Declare finished integer default 0;
Declare cust varchar(30);
declare flag integer default 0;
Declare c1 cursor for select customerID from customer_information;
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET finished = 1;
open c1;
get_cust: LOOP
Fetch c1 into cust;
if finished=1 then
leave get_cust;
end if;
if cust=NEW.CustomerID then
set flag=1;
end if;
end loop get_cust;
close c1;
if flag=0 then
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Customer does not exist';
END if;
END
$$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER `decrease_quantity` BEFORE DELETE ON `transaction_information` FOR EACH ROW BEGIN
Declare finished integer default 0;
Declare cust integer;
Declare quant integer default 0;
Declare c1 cursor for select ProductID from transaction_detail where TransactionID=OLD.TransactionID;
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET finished = 1;
CREATE TEMPORARY TABLE IF NOT EXISTS my_temp_table
SELECT ProductID, Quantity from transaction_detail where TransactionID=OLD.TransactionID;
open c1;
get_cust: LOOP
Fetch c1 into cust;
if finished=1 then
leave get_cust;
end if;
Select Quantity into quant from my_temp_table where ProductID=cust;
Update Product set quantity_in_stock=quantity_in_stock+quant where ProductID=cust;
end loop;
close c1;
Delete from transaction_detail where transactionID=OLD.TransactionID;
END
$$
DELIMITER ;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `category`
--
ALTER TABLE `category`
ADD PRIMARY KEY (`CategoryID`);
--
-- Indexes for table `customer_information`
--
ALTER TABLE `customer_information`
ADD PRIMARY KEY (`CustomerID`);
--
-- Indexes for table `depleted_product`
--
ALTER TABLE `depleted_product`
ADD PRIMARY KEY (`ProductID`);
--
-- Indexes for table `payment`
--
ALTER TABLE `payment`
ADD PRIMARY KEY (`TransactionID`);
--
-- Indexes for table `price_list`
--
ALTER TABLE `price_list`
ADD PRIMARY KEY (`ProductID`);
--
-- Indexes for table `product`
--
ALTER TABLE `product`
ADD PRIMARY KEY (`ProductID`),
ADD KEY `product_ibfk_2` (`CategoryID`),
ADD KEY `product_ibfk_3` (`SupplierID`);
--
-- Indexes for table `supplier_information`
--
ALTER TABLE `supplier_information`
ADD PRIMARY KEY (`SupplierID`);
--
-- Indexes for table `transaction_detail`
--
ALTER TABLE `transaction_detail`
ADD PRIMARY KEY (`TransactionID`,`ProductID`),
ADD KEY `td_ibfk_2` (`ProductID`);
--
-- Indexes for table `transaction_information`
--
ALTER TABLE `transaction_information`
ADD PRIMARY KEY (`TransactionID`),
ADD KEY `ti_ibfk_1` (`CustomerID`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `transaction_information`
--
ALTER TABLE `transaction_information`
MODIFY `TransactionID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=29;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `product`
--
ALTER TABLE `product`
ADD CONSTRAINT `product_ibfk_2` FOREIGN KEY (`CategoryID`) REFERENCES `category` (`CategoryID`),
ADD CONSTRAINT `product_ibfk_3` FOREIGN KEY (`SupplierID`) REFERENCES `supplier_information` (`SupplierID`);
--
-- Constraints for table `transaction_detail`
--
ALTER TABLE `transaction_detail`
ADD CONSTRAINT `td_ibfk_2` FOREIGN KEY (`ProductID`) REFERENCES `product` (`ProductID`);
--
-- Constraints for table `transaction_information`
--
ALTER TABLE `transaction_information`
ADD CONSTRAINT `ti_ibfk_1` FOREIGN KEY (`CustomerID`) REFERENCES `customer_information` (`CustomerID`);
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;