-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabaseconnection.cpp
177 lines (153 loc) · 5.02 KB
/
databaseconnection.cpp
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
#include "databaseconnection.h"
#include <QMessageBox>
#include <QFileInfo>
DatabaseConnection::DatabaseConnection()
{
database = QSqlDatabase::addDatabase("QSQLITE");
}
void DatabaseConnection::close()
{
database.close();
}
bool DatabaseConnection::open()
{
// Init database configurations
QString path = qApp->applicationDirPath();
QString db = path + "/database/InventoryManagement.db";
QFileInfo fileInfo(db);
// Check if the file exists
if (!fileInfo.exists())
{
if(!createDatabase(db))
{
return false;
}
}
// Check if the database is open
if(this->isOpen())
{
return true;
}
else
{
// Set database name
database.setDatabaseName(db);
// Return status
return database.open();
}
}
bool DatabaseConnection::isOpen()
{
return database.isOpen();
}
int DatabaseConnection::createDatabase(QString databasePath)
{
// Create database directory
QString path = qApp->applicationDirPath();
QDir directory(path + "/database");
if(!directory.exists())
{
if (!directory.mkpath("."))
{
qDebug() << "Error to create directory for database";
return false;
}
}
// Set database name
database.setDatabaseName(databasePath);
// Open database
if (!database.open())
{
qDebug() << "Error to create database";
return false;
}
// Create table tb_access_type
QSqlQuery query;
if (!query.exec("CREATE TABLE tb_access_type ("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"type TEXT NOT NULL,"
"description TEXT"
")"))
{
qDebug() << "Error to create tb_access_type table: " << query.lastError().text();
return false;
}
// Create table tb_collaborators
if (!query.exec("CREATE TABLE tb_collaborators ("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"name TEXT NOT NULL,"
"username TEXT NOT NULL,"
"password TEXT NOT NULL,"
"telephone TEXT NOT NULL,"
"access INTEGER NOT NULL"
")"))
{
qDebug() << "Error to create tb_collaborators table: " << query.lastError().text();
return false;
}
// Create table tb_inventory
if (!query.exec("CREATE TABLE tb_inventory ("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"description TEXT NOT NULL,"
"supplier TEXT NOT NULL,"
"quantity INTEGER NOT NULL,"
"purchase_price REAL NOT NULL,"
"sale_price NUMERIC NOT NULL"
")"))
{
qDebug() << "Error to create tb_inventory table: " << query.lastError().text();
return false;
}
// Create table tb_products_sales
if (!query.exec("CREATE TABLE tb_products_sales ("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"id_sale INTEGER NOT NULL,"
"id_product INTEGER NOT NULL,"
"quantity INTEGER NOT NULL,"
"sale_price REAL NOT NULL"
")"))
{
qDebug() << "Error to create tb_products_sales table: " << query.lastError().text();
return false;
}
// Create table tb_sales
if (!query.exec("CREATE TABLE tb_sales ("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"date TEXT NOT NULL,"
"id_collaborator INTEGER NOT NULL,"
"total_value REAL NOT NULL"
")"))
{
qDebug() << "Error to create tb_sales table: " << query.lastError().text();
return false;
}
// Insert admin user into tb_collaborators
if (!query.exec("INSERT INTO tb_collaborators (name, username, password, telephone, access) "
"VALUES ('admin', 'admin', 'admin', '', 1)"))
{
qDebug() << "Error to insert admin user into tb_collaborators: " << query.lastError().text();
return false;
}
// Insert access type A
if (!query.exec("INSERT INTO tb_access_type (type, description) "
"VALUES ('A', 'Full access')"))
{
qDebug() << "Error to insert access type A into tb_access_type: " << query.lastError().text();
return false;
}
// Insert access type B
if (!query.exec("INSERT INTO tb_access_type (type, description) "
"VALUES ('B', 'Can create, but cannot edit')"))
{
qDebug() << "Error to insert access type B into tb_access_type: " << query.lastError().text();
return false;
}
// Insert access type C
if (!query.exec("INSERT INTO tb_access_type (type, description) "
"VALUES ('C', 'Access only to sale page')"))
{
qDebug() << "Error to insert access type C into tb_access_type: " << query.lastError().text();
return false;
}
return true;
}