forked from edruid/Nitroxy-retail-system
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nitroxy_retail.sql
337 lines (295 loc) · 13.7 KB
/
nitroxy_retail.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
-- MySQL dump 10.13 Distrib 5.1.58, for debian-linux-gnu (x86_64)
--
-- Host: localhost Database: nitroxy_retail
-- ------------------------------------------------------
-- Server version 5.1.58-1ubuntu1
/*!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 utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `account`
--
DROP TABLE IF EXISTS `account`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `account` (
`account_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(30) COLLATE utf8_swedish_ci NOT NULL,
`default_sign` enum('debit','kredit') COLLATE utf8_swedish_ci NOT NULL,
`warn_on_non_default` tinyint(1) NOT NULL DEFAULT '0',
`description` text COLLATE utf8_swedish_ci,
`account_type` enum('balance','result') COLLATE utf8_swedish_ci DEFAULT NULL,
`code_name` varchar(16) COLLATE utf8_swedish_ci DEFAULT NULL,
PRIMARY KEY (`account_id`),
UNIQUE KEY `name` (`name`),
UNIQUE KEY `code_name` (`code_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `account_transaction`
--
DROP TABLE IF EXISTS `account_transaction`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `account_transaction` (
`account_transaction_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`description` text COLLATE utf8_swedish_ci,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`user_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`account_transaction_id`),
KEY `user_id` (`user_id`),
CONSTRAINT `account_transaction_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `account_transaction_contents`
--
DROP TABLE IF EXISTS `account_transaction_contents`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `account_transaction_contents` (
`account_transaction_id` int(10) unsigned NOT NULL,
`account_id` int(10) unsigned NOT NULL,
`amount` decimal(10,2) NOT NULL,
PRIMARY KEY (`account_transaction_id`,`account_id`),
KEY `account_id` (`account_id`),
CONSTRAINT `account_transaction_contents_ibfk_1` FOREIGN KEY (`account_transaction_id`) REFERENCES `account_transaction` (`account_transaction_id`),
CONSTRAINT `account_transaction_contents_ibfk_2` FOREIGN KEY (`account_id`) REFERENCES `account` (`account_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `categories`
--
DROP TABLE IF EXISTS `categories`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `categories` (
`category_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(30) COLLATE utf8_swedish_ci NOT NULL,
PRIMARY KEY (`category_id`),
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `daily_count`
--
DROP TABLE IF EXISTS `daily_count`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `daily_count` (
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`amount` decimal(10,2) NOT NULL,
`account_transaction_id` int(10) unsigned DEFAULT NULL,
`user_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`time`),
KEY `account_transaction_id` (`account_transaction_id`),
KEY `user_id` (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `deliveries`
--
DROP TABLE IF EXISTS `deliveries`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `deliveries` (
`delivery_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`description` text COLLATE utf8_swedish_ci NOT NULL,
`user_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`delivery_id`),
KEY `user_id` (`user_id`),
CONSTRAINT `deliveries_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `delivery_contents`
--
DROP TABLE IF EXISTS `delivery_contents`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `delivery_contents` (
`delivery_id` int(10) unsigned NOT NULL,
`product_id` int(10) unsigned NOT NULL,
`count` int(11) NOT NULL,
`cost` decimal(8,4) NOT NULL,
PRIMARY KEY (`delivery_id`,`product_id`),
KEY `product_id` (`product_id`),
CONSTRAINT `delivery_contents_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `products` (`product_id`),
CONSTRAINT `delivery_contents_ibfk_2` FOREIGN KEY (`delivery_id`) REFERENCES `deliveries` (`delivery_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `product_log`
--
DROP TABLE IF EXISTS `product_log`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `product_log` (
`product_log_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`product_id` int(11) unsigned NOT NULL,
`old_price` decimal(10,2) NOT NULL,
`new_price` decimal(10,2) NOT NULL,
`changed_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`user_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`product_log_id`),
KEY `product_id` (`product_id`),
KEY `user_id` (`user_id`),
CONSTRAINT `product_log_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `products` (`product_id`),
CONSTRAINT `product_log_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `product_package`
--
DROP TABLE IF EXISTS `product_package`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `product_package` (
`product_id` int(10) unsigned NOT NULL,
`package` int(10) unsigned NOT NULL,
`count` smallint(6) NOT NULL DEFAULT '1',
PRIMARY KEY (`product_id`,`package`),
KEY `package` (`package`),
CONSTRAINT `product_package_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `products` (`product_id`),
CONSTRAINT `product_package_ibfk_2` FOREIGN KEY (`package`) REFERENCES `products` (`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `products`
--
DROP TABLE IF EXISTS `products`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `products` (
`product_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(50) COLLATE utf8_swedish_ci NOT NULL,
`price` decimal(10,2) NOT NULL,
`ean` varchar(30) COLLATE utf8_swedish_ci NOT NULL,
`category_id` int(10) unsigned NOT NULL,
`value` decimal(8,4) NOT NULL,
`count` int(11) NOT NULL DEFAULT '0',
`inventory_threshold` int(10) unsigned DEFAULT NULL,
`active` tinyint(1) NOT NULL DEFAULT '1',
`account_id` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`product_id`),
UNIQUE KEY `name` (`name`),
UNIQUE KEY `ean` (`ean`),
KEY `category_id` (`category_id`),
KEY `account_id` (`account_id`),
CONSTRAINT `products_ibfk_2` FOREIGN KEY (`account_id`) REFERENCES `account` (`account_id`),
CONSTRAINT `products_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `categories` (`category_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `transaction_contents`
--
DROP TABLE IF EXISTS `transaction_contents`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `transaction_contents` (
`transaction_id` int(11) unsigned NOT NULL,
`product_id` int(11) unsigned NOT NULL,
`count` int(11) NOT NULL,
`amount` decimal(10,2) NOT NULL,
`stock_usage` decimal(10,4) NOT NULL DEFAULT '0.0000',
PRIMARY KEY (`transaction_id`,`product_id`),
KEY `product_id` (`product_id`),
KEY `transaction_id` (`transaction_id`),
CONSTRAINT `transaction_contents_ibfk_2` FOREIGN KEY (`transaction_id`) REFERENCES `transactions` (`transaction_id`),
CONSTRAINT `transaction_contents_ibfk_3` FOREIGN KEY (`product_id`) REFERENCES `products` (`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `transactions`
--
DROP TABLE IF EXISTS `transactions`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `transactions` (
`transaction_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`amount` decimal(10,2) NOT NULL,
PRIMARY KEY (`transaction_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `users`
--
DROP TABLE IF EXISTS `users`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `users` (
`user_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`first_name` varchar(100) COLLATE utf8_swedish_ci NOT NULL,
`surname` varchar(100) COLLATE utf8_swedish_ci NOT NULL,
`username` varchar(100) COLLATE utf8_swedish_ci NOT NULL,
`password` varchar(70) COLLATE utf8_swedish_ci DEFAULT NULL,
PRIMARY KEY (`user_id`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping routines for database 'nitroxy_retail'
--
/*!50003 DROP PROCEDURE IF EXISTS `merge_products` */;
/*!50003 SET @saved_cs_client = @@character_set_client */ ;
/*!50003 SET @saved_cs_results = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode = @@sql_mode */ ;
/*!50003 SET sql_mode = '' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50020 DEFINER=`root`@`localhost`*/ /*!50003 PROCEDURE `merge_products`( IN _main int, IN _merge int )
MODIFIES SQL DATA
BEGIN
DECLARE _value decimal(10,4);
DECLARE _count int;
DECLARE _inventory_threshold int;
INSERT INTO transaction_contents (transaction_id, product_id, count, amount, stock_usage)
(SELECT transaction_id, _main, count, amount, stock_usage FROM transaction_contents AS tc WHERE tc.product_id = _merge)
ON DUPLICATE KEY UPDATE
count = transaction_contents.count + tc.count,
stock_usage = transaction_contents.stock_usage + tc.stock_usage,
amount = transaction_contents.amount + tc.amount;
DELETE FROM transaction_contents WHERE product_id = _merge;
DELETE FROM product_log WHERE product_id = _merge;
INSERT INTO delivery_contents (delivery_id, product_id, count, cost)
(SELECT delivery_id, _main, count, cost FROM delivery_contents AS dc WHERE dc.product_id = _merge)
ON DUPLICATE KEY UPDATE
cost = COALESCE((delivery_contents.cost * delivery_contents.count + dc.cost * dc.count) / (delivery_contents.count + dc.count), delivery_contents.cost),
count = delivery_contents.count + dc.count;
DELETE FROM delivery_contents WHERE product_id = _merge;
UPDATE product_package SET product_id = _main WHERE product_id = _merge;
UPDATE product_package SET package = _main WHERE package = _merge;
SELECT count, value, inventory_threshold INTO _count, _value, _inventory_threshold FROM products WHERE product_id = _merge;
UPDATE products SET
value = COALESCE((value * count + _count * _value) / (count + _count), value),
count = count + _count,
inventory_threshold = COALESCE(inventory_threshold, _inventory_threshold)
WHERE products.product_id = _main;
DELETE FROM products WHERE product_id = _merge;
END */;;
DELIMITER ;
/*!50003 SET sql_mode = @saved_sql_mode */ ;
/*!50003 SET character_set_client = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection = @saved_col_connection */ ;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!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 */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2012-02-29 20:15:47