File: itemGroup.cpp

package info (click to toggle)
postbooks 4.10.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 112,660 kB
  • ctags: 22,890
  • sloc: cpp: 310,358; sh: 607; xml: 214; python: 140; awk: 104; makefile: 50
file content (376 lines) | stat: -rw-r--r-- 12,319 bytes parent folder | download | duplicates (3)
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
 * This file is part of the xTuple ERP: PostBooks Edition, a free and
 * open source Enterprise Resource Planning software suite,
 * Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple.
 * It is licensed to you under the Common Public Attribution License
 * version 1.0, the full text of which (including xTuple-specific Exhibits)
 * is available at www.xtuple.com/CPAL.  By using this software, you agree
 * to be bound by its terms.
 */

#include "itemGroup.h"

#include <QMessageBox>
#include <QVariant>

#include "itemcluster.h"

itemGroup::itemGroup(QWidget* parent, const char* name, Qt::WindowFlags fl)
    : XWidget(parent, name, fl)
{
  setupUi(this);

  connect(_new, SIGNAL(clicked()), this, SLOT(sNew()));
  connect(_delete, SIGNAL(clicked()), this, SLOT(sDelete()));
  connect(_newParent, SIGNAL(clicked()), this, SLOT(sNewParent()));
  connect(_deleteParent, SIGNAL(clicked()), this, SLOT(sDeleteParent()));
  connect(_save, SIGNAL(clicked()), this, SLOT(sSave()));
  connect(_close, SIGNAL(clicked()), this, SLOT(sClose()));
  connect(_name, SIGNAL(editingFinished()), this, SLOT(sCheck()));

  _itemgrpitem->addColumn(tr("Name"),        _itemColumn,  Qt::AlignLeft, true, "item_number" );
  _itemgrpitem->addColumn(tr("Description"), -1,           Qt::AlignLeft, true, "item_descrip" );

  _itemgrpparent->addColumn(tr("Name"),        _itemColumn,  Qt::AlignLeft, true, "itemgrp_name" );
  _itemgrpparent->addColumn(tr("Description"), -1,           Qt::AlignLeft, true, "itemgrp_descrip" );
}

itemGroup::~itemGroup()
{
  // no need to delete child widgets, Qt does it all for us
}

void itemGroup::languageChange()
{
  retranslateUi(this);
}

enum SetResponse itemGroup::set(const ParameterList &pParams)
{
  XSqlQuery itemet;
  XWidget::set(pParams);
  QVariant param;
  bool     valid;

  param = pParams.value("itemgrp_id", &valid);
  if (valid)
  {
    _itemgrpid = param.toInt();
    populate();
    emit newId(_itemgrpid);
  }

  param = pParams.value("mode", &valid);
  if (valid)
  {
    if (param.toString() == "new")
    {
      _mode = cNew;

      itemet.exec("SELECT NEXTVAL('itemgrp_itemgrp_id_seq') AS itemgrp_id;");
      if (itemet.first())
        _itemgrpid = itemet.value("itemgrp_id").toInt();

      sFillList();

      connect(_itemgrpitem, SIGNAL(valid(bool)), _delete, SLOT(setEnabled(bool)));
      connect(_itemgrpparent, SIGNAL(valid(bool)), _deleteParent, SLOT(setEnabled(bool)));
      emit newId(_itemgrpid);
      emit newMode(_mode);
    }
    else if (param.toString() == "edit")
    {
      _mode = cEdit;

      connect(_itemgrpitem, SIGNAL(valid(bool)), _delete, SLOT(setEnabled(bool)));
      connect(_itemgrpparent, SIGNAL(valid(bool)), _deleteParent, SLOT(setEnabled(bool)));
      emit newMode(_mode);
    }
    else if (param.toString() == "view")
    {
      _mode = cView;
      _name->setEnabled(false);
      _descrip->setEnabled(false);
      _catalog->setEnabled(false);
      _new->setEnabled(false);
      _newParent->setEnabled(false);
      _close->setText(tr("&Close"));
      _save->hide();
      emit newMode(_mode);
    }
  }

  return NoError;
}

void itemGroup::sCheck()
{
  XSqlQuery itemCheck;
  _name->setText(_name->text().trimmed());
  if ((_mode == cNew) && (_name->text().length()))
  {
    itemCheck.prepare( "SELECT itemgrp_id "
               "FROM itemgrp "
               "WHERE (UPPER(itemgrp_name)=UPPER(:itemgrp_name));" );
    itemCheck.bindValue(":itemgrp_name", _name->text());
    itemCheck.exec();
    if (itemCheck.first())
    {
      _itemgrpid = itemCheck.value("itemgrp_id").toInt();
      _mode = cEdit;
      populate();

      _name->setEnabled(false);

      emit newId(_itemgrpid);
      emit newMode(_mode);
    }
  }
}

void itemGroup::sClose()
{
  XSqlQuery itemClose;
  if (_mode == cNew)
  {
    itemClose.prepare( "DELETE FROM itemgrpitem "
               "WHERE (itemgrpitem_itemgrp_id=:itemgrp_id);"

                "DELETE FROM itemgrp "
                "WHERE (itemgrp_id=:itemgrp_id);" );
    itemClose.bindValue(":itemgrp_id", _itemgrpid);
    itemClose.exec();
  }

  close();
}

void itemGroup::sSave()
{
  XSqlQuery itemSave;
  if (_name->text().trimmed().length() == 0)
  {
    QMessageBox::warning( this, tr("Cannot Save Item Group"),
                          tr("You must enter a Name for this Item Group before you may save it.") );
    _name->setFocus();
    return;
  }

  if (_mode == cNew)
    itemSave.prepare( "INSERT INTO itemgrp "
               "(itemgrp_id, itemgrp_name, itemgrp_descrip, itemgrp_catalog) "
               "VALUES "
               "(:itemgrp_id, :itemgrp_name, :itemgrp_descrip, :itemgrp_catalog);" );
  else
    itemSave.prepare( "UPDATE itemgrp "
               "SET itemgrp_name=:itemgrp_name,"
               "    itemgrp_descrip=:itemgrp_descrip,"
               "    itemgrp_catalog=:itemgrp_catalog "
               "WHERE (itemgrp_id=:itemgrp_id);" );

  itemSave.bindValue(":itemgrp_id", _itemgrpid);
  itemSave.bindValue(":itemgrp_name", _name->text());
  itemSave.bindValue(":itemgrp_descrip", _descrip->text());
  itemSave.bindValue(":itemgrp_catalog", QVariant(_catalog->isChecked()));
  itemSave.exec();
  
  if (_catalog->isChecked())
  {
    itemSave.prepare( "UPDATE itemgrp "
                     "SET itemgrp_catalog=false "
                     "WHERE (itemgrp_id != :itemgrp_id);" );
    
    itemSave.bindValue(":itemgrp_id", _itemgrpid);
    itemSave.exec();
  }

  omfgThis->sItemGroupsUpdated(_itemgrpid, true);

  close();
}

void itemGroup::sDelete()
{
  XSqlQuery itemDelete;
  itemDelete.prepare( "DELETE FROM itemgrpitem "
             "WHERE (itemgrpitem_id=:itemgrpitem_id);" );
  itemDelete.bindValue(":itemgrpitem_id", _itemgrpitem->id());
  itemDelete.exec();

  sFillList();
}

void itemGroup::sDeleteParent()
{
  XSqlQuery itemDelete;
  itemDelete.prepare( "DELETE FROM itemgrpitem "
                     "WHERE (itemgrpitem_id=:itemgrpitem_id);" );
  itemDelete.bindValue(":itemgrpitem_id", _itemgrpparent->id());
  itemDelete.exec();
  
  sFillList();
}

void itemGroup::sNew()
{
  if (!_memberItem->isValid())
  {
    QMessageBox::warning( this, tr("Cannot Add Item to Item Group"),
                         tr("Please select a new Member Item.") );
    return;
  }
  
  XSqlQuery itemNew;
  int itemid = _memberItem->id();
  if (itemid != -1)
  {
    itemNew.prepare( "SELECT itemgrpitem_id "
               "FROM itemgrpitem "
               "WHERE ( (itemgrpitem_itemgrp_id=:itemgrp_id)"
               " AND (itemgrpitem_item_type='I')"
               " AND (itemgrpitem_item_id=:item_id) );" );
    itemNew.bindValue(":itemgrp_id", _itemgrpid);
    itemNew.bindValue(":item_id", itemid);
    itemNew.exec();
    if (itemNew.first())
    {
      QMessageBox::warning( this, tr("Cannot Add Item to Item Group"),
                            tr("The selected Item is already a member of this Item Group") );
      return;
    }

    itemNew.prepare( "INSERT INTO itemgrpitem "
               "(itemgrpitem_itemgrp_id, itemgrpitem_item_type, itemgrpitem_item_id) "
               "VALUES "
               "(:itemgrpitem_itemgrp_id, 'I', :itemgrpitem_item_id);" );
    itemNew.bindValue(":itemgrpitem_itemgrp_id", _itemgrpid);
    itemNew.bindValue(":itemgrpitem_item_id", itemid);
    itemNew.exec();

    _memberItem->setId(-1);
    sFillList();
  }
}

void itemGroup::sNewParent()
{
  if (_parentGroup->id() < 1)
  {
    QMessageBox::warning( this, tr("Cannot Add Parent Group to Item Group"),
                         tr("Please select a new Parent Group.") );
    return;
  }
  
  XSqlQuery itemNew;
  int itemid = _parentGroup->id();
  if (itemid != -1)
  {
    itemNew.prepare( "SELECT itemgrpitem_id "
                    "FROM itemgrpitem "
                    "WHERE ( (itemgrpitem_item_id=:itemgrp_id)"
                    " AND (itemgrpitem_item_type='G')"
                    " AND (itemgrpitem_itemgrp_id=:item_id) );" );
    itemNew.bindValue(":itemgrp_id", _itemgrpid);
    itemNew.bindValue(":item_id", itemid);
    itemNew.exec();
    if (itemNew.first())
    {
      QMessageBox::warning( this, tr("Cannot Add Parent Group to Item Group"),
                           tr("The selected Group is already a parent of this Item Group") );
      return;
    }
    
    itemNew.prepare( "WITH RECURSIVE indentedgroups(id, name, descrip, catalog, depth, path, cycle) AS ( "
                    "SELECT itemgrp_id AS id, "
                    "       itemgrp_name AS name, "
                    "       itemgrp_descrip AS descrip, "
                    "       itemgrp_catalog AS catalog, "
                    "       0 AS depth, array[itemgrp_id] AS path, false AS cycle "
                    "FROM itemgrp WHERE (itemgrp_id=:itemgrp_id) "
                    "UNION ALL "
                    "SELECT itemgrp_id AS id, "
                    "       itemgrp_name AS name, "
                    "       itemgrp_descrip AS descrip, "
                    "       NULL AS catalog, "
                    "       (depth+1) AS depth, (path || itemgrp_id) AS path, (itemgrp_id = any(path)) AS cycle "
                    "FROM indentedgroups JOIN itemgrpitem ON (itemgrpitem_itemgrp_id=id) "
                    "  JOIN itemgrp ON (itemgrp_id=itemgrpitem_item_id AND itemgrpitem_item_type='G') "
                    "WHERE (NOT cycle) AND (itemgrp_id=:item_id) "
                    ") "
                    "SELECT id, name, descrip, catalog, depth, path, cycle "
                    "FROM indentedgroups "
                    "WHERE (id != :itemgrp_id);");
    itemNew.bindValue(":itemgrp_id", _itemgrpid);
    itemNew.bindValue(":item_id", itemid);
    itemNew.exec();
    if (itemNew.first())
    {
      QMessageBox::warning( this, tr("Cannot Add Parent Group to Item Group"),
                           tr("The selected Group is already a child of this Item Group") );
      return;
    }
    
    itemNew.prepare( "INSERT INTO itemgrpitem "
                    "(itemgrpitem_itemgrp_id, itemgrpitem_item_type, itemgrpitem_item_id) "
                    "VALUES "
                    "(:itemgrpitem_item_id, 'G', :itemgrpitem_itemgrp_id);" );
    itemNew.bindValue(":itemgrpitem_itemgrp_id", _itemgrpid);
    itemNew.bindValue(":itemgrpitem_item_id", itemid);
    itemNew.exec();
    
    _parentGroup->setId(-1);
    sFillList();
  }
}

void itemGroup::sFillList()
{
  XSqlQuery itemFillList;
  itemFillList.prepare( "SELECT itemgrpitem_id, item_number, (item_descrip1 || ' ' || item_descrip2) AS item_descrip "
             "FROM itemgrpitem JOIN item ON (item_id=itemgrpitem_item_id) "
             "WHERE ( (itemgrpitem_item_type='I') "
             " AND (itemgrpitem_itemgrp_id=:itemgrp_id) ) "
             "ORDER BY item_number;" );
  itemFillList.bindValue(":itemgrp_id", _itemgrpid);
  itemFillList.exec();
  _itemgrpitem->populate(itemFillList);

  XSqlQuery parentFillList;
  parentFillList.prepare( "SELECT itemgrpitem_id, itemgrp_name, itemgrp_descrip "
                       "FROM itemgrpitem JOIN itemgrp ON (itemgrp_id=itemgrpitem_itemgrp_id) "
                       "WHERE ( (itemgrpitem_item_type='G') "
                       " AND (itemgrpitem_item_id=:itemgrp_id) ) "
                       "ORDER BY itemgrp_name;" );
  parentFillList.bindValue(":itemgrp_id", _itemgrpid);
  parentFillList.exec();
  _itemgrpparent->populate(parentFillList);
  
  XSqlQuery groupFillList;
  groupFillList.prepare( "SELECT itemgrp_id, itemgrp_name "
                        "FROM itemgrp "
                        "WHERE (itemgrp_id != :itemgrp_id) "
                        "ORDER BY itemgrp_name;" );
  groupFillList.bindValue(":itemgrp_id", _itemgrpid);
  groupFillList.exec();
  _parentGroup->populate(groupFillList);
}

void itemGroup::populate()
{
  XSqlQuery itempopulate;
  itempopulate.prepare( "SELECT * "
             "FROM itemgrp "
             "WHERE (itemgrp_id=:itemgrp_id);" );
  itempopulate.bindValue(":itemgrp_id", _itemgrpid);
  itempopulate.exec();
  if (itempopulate.first())
  {
    _name->setText(itempopulate.value("itemgrp_name").toString());
    _descrip->setText(itempopulate.value("itemgrp_descrip").toString());
    _catalog->setChecked(itempopulate.value("itemgrp_catalog").toBool());
    if (_catalog->isChecked())
      _catalog->setEnabled(false);

    sFillList();
  }
}