-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStock_Add_Part.cpp
139 lines (118 loc) · 4.56 KB
/
Stock_Add_Part.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
#include "Stock_Add_Part.h"
#include "ui_Stock_Add_Part.h"
#include "System_Services_and_Info.h"
#include "Partgroup_Add.h"
#include "QSqlQuery"
#include "QDebug"
#include "QSqlError"
#include "QMessageBox"
#include "QSettings"
Stock_Add_Part::Stock_Add_Part(QWidget *parent) :
QDialog(parent),
ui(new Ui::Stock_Add_Part)
{
ui->setupUi(this);
LoadSettings();
ui->line_Name->setFocus();
}
Stock_Add_Part::~Stock_Add_Part()
{
SaveSettings();
delete ui;
}
void Stock_Add_Part::Clear_Form()
{
ui->line_Name->setText("");
ui->txt_Part_Description->setPlainText("");
ui->line_Brand->setText("");
ui->double_Spin_Cost->setValue(0);
ui->spin_Quantity->setValue(0);
ui->double_Spin_Interest_Rate->setValue(0);
}
bool Stock_Add_Part::Check_Empty_Fields_On_Form()
{
if ( ui->line_Name->text() == ""
||ui->txt_Part_Description->toPlainText() == ""
||ui->double_Spin_Cost->text() == ""
||ui->spin_Quantity->text() == ""
||ui->double_Spin_Interest_Rate->text() == ""
)
{
ui->lbl_Feedback->setText(tr("Error: All fields need to be filled!"));
ui->line_Name->setFocus();
return false;
}
//Only returns true when all the fields are filled.
return true;
}
//Check car description Filed size(This function only limits entered text to 250)//
void Stock_Add_Part::check_Car_Description_Size(){
const int max_size = 250;
if(ui->txt_Part_Description->toPlainText().length() > max_size){
ui->txt_Part_Description->setPlainText(System_Services_and_Info::Check_Text_Size(max_size, ui->txt_Part_Description->toPlainText()));
//Warn the user:
QMessageBox::warning(this, tr("Warning!"), tr("Keep the text shorter then %1 chars.").arg(max_size));
//Put cursor back to the end of the text//
QTextCursor cursor = ui->txt_Part_Description->textCursor();
cursor.setPosition(ui->txt_Part_Description->document()->characterCount() - 1);
ui->txt_Part_Description->setTextCursor(cursor);
}
}
void Stock_Add_Part::on_txt_Part_Description_textChanged()
{
check_Car_Description_Size();
}
void Stock_Add_Part::on_buttonBox_accepted()
{
if(Check_Empty_Fields_On_Form()){
QSqlQuery query;
query.prepare("INSERT INTO Part (Part_Name, Part_Brand, Part_Description, Part_Cost, Part_Sell_Value_With_Interest_Rate, Part_Quantity, Part_Interrest_Rate)"
" VALUES (:Name, :Brand, :Description, :Cost, :Part_Sell_Value_With_Interest_Rate, :Quantity, :Interrest_Rate)");
query.bindValue(":Name", ui->line_Name->text());
query.bindValue(":Brand", ui->line_Brand->text());
query.bindValue(":Description", ui->txt_Part_Description->toPlainText());
query.bindValue(":Cost", ui->double_Spin_Cost->value());
query.bindValue(":Interrest_Rate", ui->double_Spin_Interest_Rate->value());
//double_Spin_Sell_Price is percentage
double Price_With_Interrest = ui->double_Spin_Cost->value() * ui->double_Spin_Interest_Rate->value() / 100 + ui->double_Spin_Cost->value();
//TEST//
qDebug() << "Cost Price: " << ui->double_Spin_Cost->value();
qDebug() << "Inputed Interest: " << ui->double_Spin_Interest_Rate->value();
qDebug() << "Cost Price with Interest: " << Price_With_Interrest;
query.bindValue(":Part_Sell_Value_With_Interest_Rate", Price_With_Interrest);
query.bindValue(":Quantity", ui->spin_Quantity->text().toInt());
if (query.exec() == false){
qDebug() << query.lastError();
QMessageBox::critical(this, tr("Error!"), tr("This Part was not added!! Classe: Stock_Add_Part::on_btn_Cadastrar_clicked()"));
}else{
ui->lbl_Feedback->setText(ui->line_Name->text() + tr(" added to Stock!"));
QMessageBox::information(this, tr("Success!"), tr("Part added to Stock."));
Clear_Form();
ui->line_Name->setFocus();
}
}
}
void Stock_Add_Part::on_buttonBox_rejected()
{
close();
}
void Stock_Add_Part::LoadSettings()
{
QSettings setting("bedi1982","Oficina");
setting.beginGroup("Stock_Add_Part");
QRect myrect = setting.value("position").toRect();
setGeometry(myrect);
setting.endGroup();
}
void Stock_Add_Part::SaveSettings()
{
QSettings setting("bedi1982","Oficina");
setting.beginGroup("Stock_Add_Part");
setting.setValue("position",this->geometry());
setting.endGroup();
}
void Stock_Add_Part::on_btn_New_Group_clicked()
{
PartGroup_Add PartGroup_Add;
PartGroup_Add.exec();
}