File: customerselector.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 (236 lines) | stat: -rw-r--r-- 7,486 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
/*
 * 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 <xsqlquery.h>
#include <parameter.h>
#include <metasql.h>
#include <errorReporter.h>

#include "customerselector.h"

CustomerSelector::CustomerSelector(QWidget *pParent, const char *pName) : QWidget(pParent)
{
  setupUi(this);

  if (pName)
    setObjectName(pName);

  setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));

  _allowedStates = 0;
  populate(AllCust | SelectedCust | SelectedGroup | SelectedType | TypePattern);

  _select->setCurrentIndex(AllCust);
  _cust->setType(CLineEdit::AllCustomers);
  _cust->setId(-1);
  _customerTypes->setId(-1);
  _customerGroup->setId(-1);
  _customerType->setText("");



  connect(_select,         SIGNAL(currentIndexChanged(int)), this, SIGNAL(updated()));
  connect(_select,         SIGNAL(currentIndexChanged(int)), this, SIGNAL(newState(int)));
  connect(_select,         SIGNAL(currentIndexChanged(int)), this, SLOT(setStackElement()));
  connect(_cust,           SIGNAL(newId(int)), this, SIGNAL(updated()));
  connect(_cust,           SIGNAL(newId(int)), this, SIGNAL(newCustId(int)));
  connect(_cust,           SIGNAL(valid(bool)), this, SIGNAL(validCust(bool)));
  connect(_customerTypes,  SIGNAL(newID(int)), this, SIGNAL(updated()));
  connect(_customerTypes,  SIGNAL(newID(int)), this, SIGNAL(newCustTypeId(int)));
  connect(_customerType,   SIGNAL(editingFinished()), this, SIGNAL(updated()));
  connect(_customerType,   SIGNAL(editingFinished()), this, SLOT(sTypePatternFinished()));
  connect(_customerType,   SIGNAL(textChanged(QString)), this, SIGNAL(newTypePattern(QString)));
  connect(_customerGroup,  SIGNAL(newID(int)), this, SIGNAL(updated()));
  connect(_customerGroup,  SIGNAL(newID(int)), this, SIGNAL(newCustGroupId(int)));
  connect(_customerGroup,  SIGNAL(valid(bool)), this, SIGNAL(validCustGroup(bool)));


  setFocusProxy(_select);
}

CustomerSelector::~CustomerSelector()
{
}

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

void CustomerSelector::appendValue(ParameterList &pParams)
{
  if (isSelectedCust())
    pParams.append("cust_id", _cust->id());
  if (isSelectedGroup())
    pParams.append("custgrp_id", _customerGroup->id());
  if (isSelectedType())
    pParams.append("custtype_id", _customerTypes->id());
  if (isTypePattern())
    pParams.append("custtype_pattern", _customerType->text());
}

void CustomerSelector::bindValue(XSqlQuery &pQuery)
{
  if (isSelectedCust())
    pQuery.bindValue(":cust_id", _cust->id());
  if (isSelectedGroup())
    pQuery.bindValue(":custgrp_id", _customerGroup->id());
  if (isSelectedType())
    pQuery.bindValue(":custtype_id", _customerTypes->id());
  if (isTypePattern())
    pQuery.bindValue(":custtype_pattern", _customerType->text());
}

bool CustomerSelector::isValid()
{
  if (isSelectedCust())
    return _cust->isValid();
  else if (isSelectedGroup())
    return _customerGroup->isValid();
  else if (isSelectedType())
    return _customerTypes->isValid();
  else if (isTypePattern())
    return ! _customerType->text().trimmed().isEmpty();
  else if (isAll())
    return true;

  return false;
}

void CustomerSelector::setCustId(int p)
{
  _cust->setId(p);
  setState(SelectedCust);
}

void CustomerSelector::setCustTypeId(int p)
{
  _customerTypes->setId(p);
  setState(SelectedType);
}

void CustomerSelector::setTypePattern(const QString &p)
{
  _customerType->setText(p);
  sTypePatternFinished();
  setState(TypePattern);
}

void CustomerSelector::setCustGroupId(int p)
{
  _customerGroup->setId(p);
  setState(SelectedGroup);
}

void CustomerSelector::setState(enum CustomerSelectorState p)
{
  if (p == AllCust)
    _select->setCode("A");
  else if (p == SelectedCust)
    _select->setCode("C");
  else if (p == SelectedGroup)
    _select->setCode("G");
  else if (p == SelectedType)
    _select->setCode("T");
  else if (p == TypePattern)
    _select->setCode("P");
  else
    _select->setCode("N");
}

void CustomerSelector::sTypePatternFinished()
{
  emit newTypePattern(_customerType->text());
  emit updated();
}

/*
   keep the contents of another customer group (p) synchronized with this in a
   manner which allows any changes on this to propagate to p and any objects
   listening for signals from p will respond to changes to this.
   hide p because it's superfluous.
*/
void CustomerSelector::synchronize(CustomerSelector *p)
{
  connect(this, SIGNAL(newTypePattern(QString)), p, SLOT(setTypePattern(QString)));
  connect(this, SIGNAL(newState(int)),           p, SLOT(setState(int)));
  connect(this, SIGNAL(newCustId(int)),          p, SLOT(setCustId(int)));
  connect(this, SIGNAL(newCustTypeId(int)),      p, SLOT(setCustTypeId(int)));
  connect(this, SIGNAL(newCustGroupId(int)),     p, SLOT(setCustGroupId(int)));

  p->hide();
}

void CustomerSelector::populate(int selection)
{
  QString sql = "SELECT -1 AS selkey, '[Please Select]', 'N'"
                "<? if exists('allcust') ?>"
                " UNION "
                "SELECT 1 AS selkey, 'All Customers' AS selname, 'A' AS selcode"
                "<? endif ?>"
                "<? if exists('cust') ?>"
                " UNION "
                "SELECT 2 AS selkey, 'Customer' as selname, 'C' as selcode"
                "<? endif ?>"
                "<? if exists('custgrp') ?>"
                " UNION "
                "SELECT 3 AS selkey, 'Customer Group' as selname, 'G' as selcode"
                "<? endif ?>"
                "<? if exists('custtype') ?>"
                " UNION "
                "SELECT 4 AS selkey, 'Customer Type' AS selname, 'T' as selcode"
                "<? endif ?>"
                "<? if exists('typepattern') ?>"
                " UNION "
                "SELECT 5 AS selkey, 'Customer Type Pattern' AS selname, 'P' as selcode"
                "<? endif ?>"
                " ORDER BY selkey;";
  ParameterList params;
  MetaSQLQuery mql(sql);

  if (isAllowedType(selection))
  {
    if (selection & AllCust) params.append("allcust");
    if (selection & SelectedCust)  params.append("cust");
    if (selection & SelectedGroup) params.append("custgrp");
    if (selection & SelectedType) params.append("custtype");
    if (selection & TypePattern) params.append("typepattern");
    _allowedStates = selection;
  }
  else
    ErrorReporter::error(QtCriticalMsg, this, tr("Invalid type"), QString::number(selection), __FILE__, __LINE__);
  XSqlQuery q = mql.toQuery(params);
  _select->populate(q);
}

void CustomerSelector::setStackElement()
{
  if (isSelectedCust())
    _selectStack->setCurrentIndex(1);
  else if (isSelectedGroup())
    _selectStack->setCurrentIndex(2);
  else if (isSelectedType())
    _selectStack->setCurrentIndex(3);
  else if (isTypePattern())
    _selectStack->setCurrentIndex(4);
  else
    _selectStack->setCurrentIndex(0);
}

QString CustomerSelector::selectCode()
{
  return _select->code();
}

bool CustomerSelector::isAllowedType(const int s)
{
  return ((s & (AllCust | SelectedCust | SelectedGroup | SelectedType | TypePattern)) ||
          (s & (          SelectedCust | SelectedGroup  )));
}