-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewInvoiceDlg.cpp
More file actions
214 lines (182 loc) · 6.74 KB
/
Copy pathNewInvoiceDlg.cpp
File metadata and controls
214 lines (182 loc) · 6.74 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
#include "NewInvoiceDlg.h"
NewInvoiceDlg::NewInvoiceDlg(Model *_ModelObject)
: ModelObject(_ModelObject)
{
QLocale::setDefault(QLocale(QLocale::Dutch, QLocale::Netherlands));
setupUi(this);
connect(SaveButton, SIGNAL(clicked()), this, SLOT(onSave()));
connect(VATCheck, SIGNAL(stateChanged(int)), this, SLOT(onVATChanged(int)));
connect(NettCheck, SIGNAL(stateChanged(int)), this, SLOT(onNettChanged(int)));
connect(GrossamountSpin, SIGNAL(valueChanged(const QString&)), this, SLOT(onAmountChanged(const QString&)));
connect(VATBox, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(onAmountChanged(const QString&)));
connect(VATamountSpin, SIGNAL(valueChanged(const QString&)), this, SLOT(onAmountChanged(const QString&)));
connect(InvoiceDateEdit, SIGNAL(dateChanged(const QDate&)), this, SLOT(onInvoiceDateChanged(const QDate&)));
InvoiceDateEdit->setDate(QDate::currentDate());
PayedDateEdit->setDate(QDate::currentDate());
RelationList = ModelObject->getRelations();
QStringList RelationsForCompleter;
for(int i = 0; i < RelationList.size(); i++)
{
RelationsForCompleter << RelationList.at(i).Name;
}
/* QStringListModel *StringListModel;
StringListModel = (QStringListModel*)(Completer->model());
if(StringListModel == NULL)
{
StringListModel = new QStringListModel();
}
StringListModel->setStringList(RelationsForCompleter);*/
Completer = new QCompleter(RelationsForCompleter, this);
//Completer->setModel(StringListModel);
Completer->setCaseSensitivity(Qt::CaseInsensitive);
RelationBox->addItems(RelationsForCompleter);
RelationBox->setCompleter(Completer);
restoreState();
}
NewInvoiceDlg::~NewInvoiceDlg()
{
if(Completer != NULL)
{
delete Completer;
}
}
void NewInvoiceDlg::onSave()
{
Invoice NewInvoiceData;
NewInvoiceData.Id = IDEdit->text().toInt();
NewInvoiceData.Type = TypeBox->currentText();
NewInvoiceData.RelationId = getRelationId(RelationBox->currentText());
NewInvoiceData.InvoiceDate = InvoiceDateEdit->date();
NewInvoiceData.InvoiceNumber = InvoiceNumberEdit->text();
NewInvoiceData.Grossamount = GrossamountSpin->cleanText();
NewInvoiceData.VAT = VATBox->currentText().toInt();
NewInvoiceData.VATamount = VATamountSpin->cleanText();
NewInvoiceData.Nettamount = NettamountSpin->cleanText();
NewInvoiceData.ExtraCosts = ExtraCostSpin->cleanText();
NewInvoiceData.PayedAt = PayedDateEdit->date();
NewInvoiceData.VATYear = VATYearEdit->date().year();
NewInvoiceData.VATPeriod = VATPeriodBox->currentText();
NewInvoiceData.Comment = CommentEdit->toPlainText();
saveState();
if(ModelObject->saveInvoice(NewInvoiceData))
{
accept();
}
else
{
reject();
}
}
int NewInvoiceDlg::getRelationId(const QString &RelationName)
{
int ReturnId = 0;
for(int i = 0; i < RelationList.size(); i++)
{
if(RelationList.at(i).Name == RelationName)
{
ReturnId = RelationList.at(i).Id;
break;
}
}
return ReturnId;
}
QString NewInvoiceDlg::getRelationName(const int &RelationId)
{
QString ReturnName = "";
for(int i = 0; i < RelationList.size(); i++)
{
if(RelationList.at(i).Id == RelationId)
{
ReturnName = RelationList.at(i).Name;
break;
}
}
return ReturnName;
}
void NewInvoiceDlg::setInvoiceData(Invoice InvoiceData)
{
if(InvoiceData.VATamount != getVATAmount(InvoiceData.Grossamount, QString::number(InvoiceData.VAT)))
{
VATCheck->setChecked(true);
}
if(InvoiceData.Nettamount != getNettAmount(InvoiceData.Grossamount, InvoiceData.VATamount))
{
NettCheck->setChecked(true);
}
IDEdit->setText(QString::number(InvoiceData.Id));
TypeBox->setCurrentIndex(TypeBox->findText(InvoiceData.Type));
RelationBox->setCurrentIndex(RelationBox->findText(getRelationName(InvoiceData.RelationId)));
InvoiceDateEdit->setDate(InvoiceData.InvoiceDate);
InvoiceNumberEdit->setText(InvoiceData.InvoiceNumber);
GrossamountSpin->setValue(GrossamountSpin->valueFromText(InvoiceData.Grossamount));
VATBox->setCurrentIndex(VATBox->findText(QString::number(InvoiceData.VAT)));
VATamountSpin->setValue(VATamountSpin->valueFromText(InvoiceData.VATamount));
NettamountSpin->setValue(NettamountSpin->valueFromText(InvoiceData.Nettamount));
ExtraCostSpin->setValue(ExtraCostSpin->valueFromText(InvoiceData.ExtraCosts));
PayedDateEdit->setDate(InvoiceData.PayedAt);
VATYearEdit->setDate(QDate(InvoiceData.VATYear, 1, 1));
VATPeriodBox->setCurrentIndex(VATPeriodBox->findText(InvoiceData.VATPeriod));
CommentEdit->setText(InvoiceData.Comment);
setWindowTitle(tr("Edit invoice"));
QIcon EditIcon;
EditIcon.addFile(QString::fromUtf8(":/Icons/document-edit.png"), QSize(), QIcon::Normal, QIcon::Off);
setWindowIcon(EditIcon);
}
void NewInvoiceDlg::restoreState()
{
QSettings LocalSettings("./MainSettings", QSettings::IniFormat);
restoreGeometry(LocalSettings.value("NewInvoiceDlgGeometry").toByteArray());
QList<QSplitter *> AllSplitters = this->findChildren<QSplitter *>();
for(int i = 0; i < AllSplitters.size(); i++)
{
QString State = LocalSettings.value("NewInvoiceDlg_State_" + AllSplitters.at(i)->objectName()).toString();
AllSplitters.at(i)->restoreState(QByteArray::fromBase64(State.toLatin1()));
}
}
void NewInvoiceDlg::saveState()
{
QSettings LocalSettings("./MainSettings", QSettings::IniFormat);
LocalSettings.setValue("NewInvoiceDlgGeometry", this->saveGeometry());
QList<QSplitter *> AllSplitters = this->findChildren<QSplitter *>();
for(int i = 0; i < AllSplitters.size(); i++)
{
LocalSettings.setValue("NewInvoiceDlg_State_" + AllSplitters.at(i)->objectName(), AllSplitters.at(i)->saveState().toBase64());
}
LocalSettings.sync();
}
void NewInvoiceDlg::onVATChanged(int State)
{
VATamountSpin->setEnabled(State);
QString Value = "";
onAmountChanged(Value);
}
void NewInvoiceDlg::onNettChanged(int State)
{
NettamountSpin->setEnabled(State);
QString Value = "";
onAmountChanged(Value);
}
void NewInvoiceDlg::onAmountChanged(const QString &Value)
{
QString GrossAmount = GrossamountSpin->cleanText();
if(!VATCheck->isChecked())
{
QString VAT = VATBox->currentText();
QString VATAmount = getVATAmount(GrossAmount, VAT);
VATamountSpin->setValue(VATamountSpin->valueFromText(VATAmount));
}
if(!NettCheck->isChecked())
{
int GrossInt = stringToInt(GrossAmount);
int NewVAT = stringToInt(VATamountSpin->cleanText());
QString NettAmount = intToString(GrossInt + NewVAT);
NettamountSpin->setValue(NettamountSpin->valueFromText(NettAmount));
}
}
void NewInvoiceDlg::onInvoiceDateChanged(const QDate &Date)
{
double Month = Date.month();
QString Period = "Q" + QString::number(ceil(Month / 3));
VATYearEdit->setDate(Date);
VATPeriodBox->setCurrentIndex(VATPeriodBox->findText(Period));
}