]> Shamusworld >> Repos - schematic/blob - src/scmwidget.cpp
a04e8947050a424dcc4ba53e844b8ef8187796a3
[schematic] / src / scmwidget.cpp
1 //
2 // scmwidget.cpp - Main widget container
3 //
4 // by James Hammons
5 // (C) 2012 Underground Software
6 //
7 // JLH = James Hammons <jlhamm@acm.org>
8 //
9 // Who  When        What
10 // ---  ----------  -------------------------------------------------------------
11 // JLH  09/24/2012  Created this file
12 //
13
14 #include "scmwidget.h"
15 #include <QtSql>
16 #include "addresswidget.h"
17 #include "contactwidget.h"
18 #include "notedialog.h"
19
20
21 SCMWidget::SCMWidget(QWidget * parent/*= 0*/): QWidget(parent),
22         purchaseOrders(new QTreeView),
23         vendorName(new QLabel),
24         ndaSigned(new QLabel),
25         vendorAddress(new QTabWidget),
26         vendorContact(new QTabWidget),
27         vendorClass(new QListWidget),
28         username(new QLabel),
29         vendorLevel(new VendorLevelWidget),
30         alerts(new QListWidget),
31         notes(new QListWidget),
32         vaw1(new AddressWidget(this)),
33         vcw1(new ContactWidget(this)),
34
35         nextVendorButton(new QToolButton),
36         previousVendorButton(new QToolButton),
37         editVendor(new QPushButton("Edit Vendor")),
38         addVendor(new QPushButton("Add Vendor")),
39         addLocation(new QPushButton("Add Location")),
40         addContact(new QPushButton("Add Contact")),
41         createAlert(new QPushButton("Create")),
42         createNote(new QPushButton("Create")),
43         showOpen(new QPushButton("Open")),
44         showClosed(new QPushButton("Closed")),
45         showAll(new QPushButton("All")),
46         createPO(new QPushButton("Create")),
47         vendorRelated(new QCheckBox("Show POs for this Vendor only")),
48         vidCursor(0)
49 {
50         // Create main page widgets & layout
51
52         purchaseOrders->setRootIsDecorated(false);
53         purchaseOrders->setAlternatingRowColors(true);
54
55         QAbstractItemModel * model = new QStandardItemModel(0, 7);
56         model->setHeaderData(0, Qt::Horizontal, tr("PO #"));
57         model->setHeaderData(1, Qt::Horizontal, tr("Vendor"));
58         model->setHeaderData(2, Qt::Horizontal, tr("Desc."));
59         model->setHeaderData(3, Qt::Horizontal, tr("OAD"));
60         model->setHeaderData(4, Qt::Horizontal, tr("UAD"));
61         model->setHeaderData(5, Qt::Horizontal, tr("Docs"));
62         model->setHeaderData(6, Qt::Horizontal, tr("FAI"));
63
64         model->insertRow(0);
65         model->setData(model->index(0, 0), "FE823724");
66         model->setData(model->index(0, 1), "Sanford & Sons");
67         model->setData(model->index(0, 2), "Misc. Junk");
68         model->setData(model->index(0, 3), "09/30/2012");
69         model->setData(model->index(0, 4), "");
70         model->setData(model->index(0, 5), "");
71         model->setData(model->index(0, 6), "");
72
73         model->insertRow(0);
74         model->setData(model->index(0, 0), "89237923-123");
75         model->setData(model->index(0, 1), "Digikey");
76         model->setData(model->index(0, 2), "Small capacitors");
77         model->setData(model->index(0, 3), "10/08/2012");
78         model->setData(model->index(0, 4), "11/30/2012");
79         model->setData(model->index(0, 5), "1");
80         model->setData(model->index(0, 6), "Yes");
81
82         model->insertRow(0);
83         model->setData(model->index(0, 0), "T9234CS32");
84         model->setData(model->index(0, 1), "Big, Faceless Company");
85         model->setData(model->index(0, 2), "Important Stuff you need RIGHT NOW");
86         model->setData(model->index(0, 3), "09/28/2012");
87         model->setData(model->index(0, 4), "09/29/2012");
88         model->setData(model->index(0, 5), "5");
89         model->setData(model->index(0, 6), "Yes");
90
91         purchaseOrders->setModel(model);
92
93         nextVendorButton->setArrowType(Qt::RightArrow);
94         nextVendorButton->setToolTip(tr("Move to next Vendor in Database."));
95         previousVendorButton->setArrowType(Qt::LeftArrow);
96         previousVendorButton->setToolTip(tr("Move to previous Vendor in Database."));
97
98         QFont * vendorFont = new QFont;
99         vendorFont->setPointSize(12);
100         vendorName->setFont(*vendorFont);
101         vendorName->setText("Vendor Name");
102
103         QHBoxLayout * hbox1 = new QHBoxLayout;
104         hbox1->addWidget(previousVendorButton);
105         hbox1->addWidget(nextVendorButton);
106         hbox1->addWidget(vendorName);
107
108         // Vendor address widgets
109
110 //      vaw1->SetFields("123 Any Street", "Any City", "Anywhere", "USA", "01234");
111         vendorAddress->addTab(vaw1, tr("Primary"));
112         vendorAddress->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
113
114         // Vendor contact widgets
115
116 //      vcw1->SetFields("Sales", "Joe Blow", "joe.blow@widget.com", "Singapore", "512-222-2222", "", "512-122-2123");
117         vendorContact->addTab(vcw1, tr("Contact #1"));
118         vendorContact->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
119
120         // Vendor class list
121
122         QVBoxLayout * vbox1 = new QVBoxLayout;
123         vbox1->addWidget(vendorClass);
124         QGroupBox * gb1 = new QGroupBox(tr("Vendor Classes"));
125         gb1->setLayout(vbox1);
126
127         // Vendor action push buttons
128
129         QVBoxLayout * vbox4 = new QVBoxLayout;
130         QHBoxLayout * hbox3 = new QHBoxLayout;
131         QGroupBox * gb2 = new QGroupBox(tr("Vendor Actions"));
132
133         vbox4->addWidget(editVendor);
134         vbox4->addWidget(addVendor);
135         vbox4->addWidget(addLocation);
136         vbox4->addWidget(addContact);
137         gb2->setLayout(vbox4);
138
139         hbox3->addWidget(vendorLevel);
140         hbox3->addWidget(gb2);
141
142         // Alerts & notes
143
144         QVBoxLayout * vbox5 = new QVBoxLayout;
145         QHBoxLayout * hbox4 = new QHBoxLayout;
146         QHBoxLayout * hbox5 = new QHBoxLayout;
147
148         QLabel * label1 = new QLabel(tr("Alerts"));
149         QLabel * label2 = new QLabel(tr("Notes"));
150         hbox4->addWidget(label1);
151         hbox4->addStretch();
152         hbox4->addWidget(createAlert);
153         hbox5->addWidget(label2);
154         hbox5->addStretch();
155         hbox5->addWidget(createNote);
156
157         vbox5->addLayout(hbox4);
158         vbox5->addWidget(alerts);
159         vbox5->addLayout(hbox5);
160         vbox5->addWidget(notes);
161
162         // Purchase Order headers
163
164         QHBoxLayout * hbox6 = new QHBoxLayout;
165         QLabel * label3 = new QLabel(tr("Purchase Orders"));
166         hbox6->addWidget(label3);
167         hbox6->addSpacing(32);
168         hbox6->addWidget(showOpen);
169         hbox6->addWidget(showClosed);
170         hbox6->addWidget(showAll);
171         hbox6->addSpacing(16);
172         hbox6->addWidget(vendorRelated);
173         hbox6->addStretch();
174         hbox6->addWidget(createPO);
175
176         // Horizontal line
177
178         QFrame * hline = new QFrame(this);
179         hline->setFrameShape(QFrame::HLine);
180         hline->setFrameShadow(QFrame::Sunken);
181
182         QFrame * hline2 = new QFrame(this);
183         hline2->setFrameShape(QFrame::HLine);
184         hline2->setFrameShadow(QFrame::Sunken);
185
186         QHBoxLayout * hbox2 = new QHBoxLayout;
187         QVBoxLayout * vbox2 = new QVBoxLayout;
188         QVBoxLayout * vbox3 = new QVBoxLayout;
189
190         vbox3->addWidget(vendorContact);
191 //      vbox3->addWidget(vendorLevel);
192         vbox3->addLayout(hbox3);
193
194         vbox2->addWidget(vendorAddress);
195         vbox2->addWidget(gb1);
196
197         hbox2->addLayout(vbox2);
198         hbox2->addLayout(vbox3);
199         hbox2->addLayout(vbox5);
200
201         QVBoxLayout * mainLayout = new QVBoxLayout;
202         mainLayout->addWidget(username);
203         mainLayout->addWidget(hline);
204         mainLayout->addLayout(hbox1);
205         mainLayout->addLayout(hbox2);
206         mainLayout->addWidget(hline2);
207         mainLayout->addLayout(hbox6);
208         mainLayout->addWidget(purchaseOrders);
209         setLayout(mainLayout);
210
211         // Set up actions
212
213         connect(nextVendorButton, SIGNAL(clicked()), this, SLOT(GetNextVendor()));
214         connect(previousVendorButton, SIGNAL(clicked()), this, SLOT(GetPreviousVendor()));
215         connect(createNote, SIGNAL(clicked()), this, SLOT(CreateNote()));
216         connect(createAlert, SIGNAL(clicked()), this, SLOT(CreateAlert()));
217         connect(createPO, SIGNAL(clicked()), this, SLOT(CreatePurchaseOrder()));
218         connect(showOpen, SIGNAL(clicked()), this, SLOT(ShowOpenPOs()));
219         connect(showClosed, SIGNAL(clicked()), this, SLOT(ShowClosedPOs()));
220         connect(showAll, SIGNAL(clicked()), this, SLOT(ShowAllPOs()));
221 //      connect(addVendor, SIGNAL(clicked()), this, SLOT(AddVendor()));
222         connect(addLocation, SIGNAL(clicked()), this, SLOT(AddLocation()));
223         connect(addContact, SIGNAL(clicked()), this, SLOT(AddContact()));
224         connect(editVendor, SIGNAL(clicked()), this, SLOT(EditVendor()));
225
226         GetVendorIDs();
227         GetVendor(vendorID[vidCursor]);
228 }
229
230
231 void SCMWidget::GetNextVendor(void)
232 {
233         if (vidCursor < (vendorID.size() - 1))
234         {
235                 vidCursor++;
236                 GetVendor(vendorID[vidCursor]);
237         }
238 }
239
240
241 void SCMWidget::GetPreviousVendor(void)
242 {
243         if (vidCursor > 0)
244         {
245                 vidCursor--;
246                 GetVendor(vendorID[vidCursor]);
247         }
248 }
249
250
251 void SCMWidget::CreateNote(void)
252 {
253         NoteDialog dlg;
254         
255         if (dlg.exec() == true)
256         {
257                 // Add note to DB for this user
258                 QString note = dlg.note->document()->toPlainText();
259
260                 QSqlQuery query;
261                 query.prepare("INSERT INTO Notes VALUES ('', ?, NULL, ?)");
262                 query.addBindValue(currentUID);
263 //              query.addBindValue(NULL);
264                 query.addBindValue(note.toAscii());
265                 query.exec();
266
267                 UpdateNotes();
268         }
269 }
270
271
272 void SCMWidget::CreateAlert(void)
273 {
274         QMessageBox::warning(this, "Approaching Singularity!", "TODO: Implementation");
275 }
276
277
278 void SCMWidget::CreatePurchaseOrder(void)
279 {
280         QMessageBox::warning(this, "Approaching Singularity!", "TODO: Implementation");
281 }
282
283
284 void SCMWidget::ShowOpenPOs(void)
285 {
286         QMessageBox::warning(this, "Approaching Singularity!", "TODO: Implementation");
287 }
288
289
290 void SCMWidget::ShowClosedPOs(void)
291 {
292         QMessageBox::warning(this, "Approaching Singularity!", "TODO: Implementation");
293 }
294
295
296 void SCMWidget::ShowAllPOs(void)
297 {
298         QMessageBox::warning(this, "Approaching Singularity!", "TODO: Implementation");
299 }
300
301
302 void SCMWidget::AddVendor(void)
303 {
304         QMessageBox::warning(this, "Approaching Singularity!", "TODO: Implementation");
305 }
306
307
308 void SCMWidget::AddLocation(void)
309 {
310         QMessageBox::warning(this, "Approaching Singularity!", "TODO: Implementation");
311 }
312
313
314 void SCMWidget::AddContact(void)
315 {
316         QMessageBox::warning(this, "Approaching Singularity!", "TODO: Implementation");
317 }
318
319
320 void SCMWidget::EditVendor(void)
321 {
322         QMessageBox::warning(this, "Approaching Singularity!", "TODO: Implementation");
323 }
324
325
326 void SCMWidget::GetVendorIDs(void)
327 {
328         vendorID.clear();
329
330         QSqlQuery query;
331         query.prepare("SELECT vid FROM Vendor ORDER BY name");
332         query.exec();
333
334         while (query.next())
335         {
336                 int vid = query.value(0).toInt();
337                 vendorID.push_back(vid);
338         }
339 }
340
341
342 void SCMWidget::GetVendor(int key)
343 {
344         // Get main vendor data
345         QSqlQuery query;
346         query.prepare("SELECT v.name, signedNDA, l.address, city, state, country, code, "
347                 "c.name, email, c.address, phone1, phone2, fax, description, v.vlid FROM "
348                 "Vendor AS v LEFT OUTER JOIN Location AS l ON v.vid = l.vid "
349                 "LEFT OUTER JOIN (Contact AS c JOIN ContactType AS ct ON c.ctid = ct.ctid) "
350                 "ON v.vid = c.vid WHERE v.vid = ?");
351         query.addBindValue(key);
352         query.exec();
353
354         // There's a possibility that the query will return multiple rows (which we need
355         // to handle, with multiple tabs), but for now, we ignore any and grab the first.
356         if (query.next())
357         {
358                 vendorName->setText(query.value(0).toString());
359
360                 vaw1->SetFields(query.value(2).toString(),
361                         query.value(3).toString(), query.value(4).toString(),
362                         query.value(5).toString(), query.value(6).toString());
363                 vcw1->SetFields(query.value(13).toString(),
364                         query.value(7).toString(), query.value(8).toString(),
365                         query.value(9).toString(), query.value(10).toString(),
366                         query.value(11).toString(), query.value(12).toString());
367
368                 int vlid = query.value(14).toInt();
369                 vendorLevel->DoQuery(vlid);
370         }
371
372         // Get vendor classes
373         query.prepare("SELECT description FROM VendorSpecificTypes AS vst, VendorType AS vt "
374                 "WHERE vst.vid = ? AND vst.vtid = vt.vtid ORDER BY seqNo");
375         query.addBindValue(key);
376         query.exec();
377
378         vendorClass->clear();
379
380         while (query.next())
381         {
382                 QListWidgetItem * item = new QListWidgetItem(query.value(0).toString());
383                 vendorClass->addItem(item);
384         }
385 }
386
387
388 void SCMWidget::UpdateNotes(void)
389 {
390 //              QMessageBox::warning(this, "Approaching Singularity!", "TODO: Implementation");
391 //NID (P-key) | UID | POID | Note
392
393         QSqlQuery query;
394         query.prepare("SELECT note FROM Notes WHERE uid = ?");
395 //              "c.name, email, c.address, phone1, phone2, fax, description, v.vlid FROM "
396 //              "Vendor AS v LEFT OUTER JOIN Location AS l ON v.vid = l.vid "
397 //              "LEFT OUTER JOIN (Contact AS c JOIN ContactType AS ct ON c.ctid = ct.ctid) "
398 //              "ON v.vid = c.vid WHERE v.vid = ?");
399         query.addBindValue(currentUID);
400         query.exec();
401
402         notes->clear();
403
404         while (query.next())
405         {
406                 QListWidgetItem * item = new QListWidgetItem(query.value(0).toString());
407                 notes->addItem(item);
408         }
409 }
410