File: lotserialCluster.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 (248 lines) | stat: -rw-r--r-- 7,751 bytes parent folder | download | duplicates (2)
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
/*
 * 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 "lotserialcluster.h"

#include <QMessageBox>
#include <QSqlError>

#include "xsqlquery.h"

LotserialList::LotserialList(QWidget* pParent, Qt::WindowFlags flags)
  : VirtualList(pParent, flags)
{
  setObjectName("LotserialList");

  _listTab->headerItem()->setText(0, tr("Lot/Serial #"));
  _listTab->headerItem()->setText(1, tr("Item #"));
}

LotserialSearch::LotserialSearch(QWidget* pParent, Qt::WindowFlags flags)
  : VirtualSearch(pParent, flags)
{
  setObjectName("LotserialSearch");

  _listTab->headerItem()->setText(0, tr("Lot/Serial #"));
  _listTab->headerItem()->setText(1, tr("Item #"));
}

LotserialCluster::LotserialCluster(QWidget* pParent, const char* pName) :
    VirtualCluster(pParent, pName)
{
    LotserialLineEdit* lsle=new LotserialLineEdit(this, pName);
    addNumberWidget(lsle);
    
    connect(lsle, SIGNAL(newItemId(int)), this, SIGNAL(newItemId(int)));
}

LotserialLineEdit::LotserialLineEdit(QWidget* pParent, const char* pName) :
    VirtualClusterLineEdit(pParent, "ls", "ls_id", "ls_number", "item_number", "item_id", 0, pName)
{
    _itemid = -1;
    setTitles(tr("Lot/Serial Number"), tr("Lot/Serial Numbers"));
    _query = QString("SELECT ls_id AS id, "
                     "       ls_number AS number, "
                     "       item_id,"
                     "       item_number AS name, "
                     "       ls_notes AS description "
                     "FROM ls "
                     "  JOIN item ON (ls_item_id=item_id) "
                     "WHERE (true) ");
    _strict = true;
}

int LotserialCluster::itemId() const
{
  if (_number && _number->inherits("LotserialLineEdit"))
    return ((LotserialLineEdit*)(_number))->itemId();
  return true;
}

void LotserialCluster::setItemId(const int p)
{
  if (_number && _number->inherits("LotserialLineEdit"))
    ((LotserialLineEdit*)(_number))->setItemId(p);
}

bool LotserialCluster::strict() const
{
  if (_number && _number->inherits("LotserialLineEdit"))
    return ((LotserialLineEdit*)(_number))->strict();
  return true;
}

void LotserialCluster::setStrict(const bool p)
{
  if (_number && _number->inherits("LotserialLineEdit"))
    ((LotserialLineEdit*)(_number))->setStrict(p);
}

void LotserialLineEdit::setItemId(const int itemid)
{
  if (_itemid == itemid)
    return;
  else if (itemid <= 0)
  {
    _itemid = -1;
    _extraClause = "";
  }
  else
  {
    _itemid = itemid;
    _extraClause = QString(" (item_id=%1) ").arg(itemid);
  }

  if (_id != -1)
  {
    XSqlQuery qry;
    qry.prepare("SELECT ls_id "
                "FROM ls "
                "WHERE ((ls_id=:ls_id) "
                " AND (ls_item_id=:item_id)); ");
    qry.bindValue(":ls_id", _id);
    qry.bindValue(":item_id", _itemid);
    qry.exec();
    if (!qry.first())
      VirtualClusterLineEdit::clear();
  }
}

void LotserialLineEdit::clear()
{
  VirtualClusterLineEdit::clear();
}

void LotserialLineEdit::setId(const int lsid)
{
  if (_x_metrics)
  {
    if (_x_metrics->boolean("LotSerialControl"))
    {
      VirtualClusterLineEdit::setId(lsid);
      XSqlQuery qq;
      qq.prepare("SELECT ls_item_id"
                 "  FROM ls"
                 " WHERE(ls_id=:id);");
      qq.bindValue(":id", id());
      qq.exec();
      if(qq.first())
        emit newItemId(qq.value("ls_item_id").toInt());
    }
  }
}

/* copied from virtualCluster.cpp but with one important difference:
   if a not-strict flag is set then warn the user but don't clear the
   lotserial field
 */
void LotserialLineEdit::sParse()
{
  if (_x_metrics)
    if (_x_metrics->boolean("LotSerialControl"))
    {
      if (! _parsed)
      {
          QString stripped = text().trimmed().toUpper();
          if (stripped.length() == 0)
          {
              _parsed = true;
              setId(-1);
          }
          else
          {
              XSqlQuery numQ;
              numQ.prepare(_query + _numClause +
                           (_extraClause.isEmpty() ? "" : " AND " + _extraClause) +
                           QString("ORDER BY %1 LIMIT 1;").arg(_numColName));
              numQ.bindValue(":number", "^" + stripped);
              numQ.exec();
              if (numQ.first())
              {
                  _valid = true;
                  setId(numQ.value("id").toInt());
                  _name = (numQ.value("name").toString());
                  _itemid = (numQ.value("item_id").toInt());
              }
              else if (numQ.lastError().type() != QSqlError::NoError)
              {
                QMessageBox::critical(this, tr("A System Error Occurred at %1::%2.")
                                              .arg(__FILE__)
                                              .arg(__LINE__),
                                      numQ.lastError().databaseText());
                return;
              }
              else if (_strict || _itemid == -1)
                VirtualClusterLineEdit::clear();
              else if (isVisible() &&
                       QMessageBox::question(this, tr("Lot/Serial # Not Found"),
                        (_itemid > 0 ?
                          tr("This Lot/Serial # was not found for this item.") :
                          tr("This Lot/Serial # was not found.")) + 
                        tr(" Are you sure it is correct?"),
                     QMessageBox::Yes | QMessageBox::No,
                     QMessageBox::No) == QMessageBox::No)
              {
                VirtualClusterLineEdit::clear();
                return;
              }
              else
              {
                int lsid=-1;
                numQ.exec("SELECT nextval('ls_ls_id_seq') AS ls_id;");
                if (numQ.first())
                  lsid= numQ.value("ls_id").toInt();
                else if (numQ.lastError().type() != QSqlError::NoError)
                {
                  QMessageBox::critical(this, tr("A System Error Occurred at %1::%2.")
                                                .arg(__FILE__)
                                                .arg(__LINE__),
                                        numQ.lastError().databaseText());
                  return;
                }
                numQ.prepare("INSERT INTO ls (ls_id,ls_item_id,ls_number) "
                             "VALUES (:ls_id,:item_id,:number)");
                numQ.bindValue(":ls_id", lsid);
                numQ.bindValue(":item_id", _itemid);
                numQ.bindValue(":number", stripped);
                numQ.exec();
                if (numQ.lastError().type() != QSqlError::NoError)
                {
                  QMessageBox::critical(this, tr("A System Error Occurred at %1::%2.")
                                                .arg(__FILE__)
                                                .arg(__LINE__),
                                        numQ.lastError().databaseText());
                  return;
                }
                setId(lsid);
              }
          }
      }

      _parsed = true;
      emit parsed();
    }
}

LotserialList* LotserialLineEdit::listFactory()
{
  return new LotserialList(this);
}

LotserialSearch* LotserialLineEdit::searchFactory()
{
  return new LotserialSearch(this);
}

/*
LotserialInfo* LotserialLineEdit::infoFactory()
{
  return new LotserialInfo(this, "LotserialInfo", true);
}
*/