File: expenseCategory.cpp

package info (click to toggle)
postbooks 4.9.5-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 103,120 kB
  • sloc: cpp: 288,269; sh: 607; xml: 214; awk: 104; makefile: 49
file content (220 lines) | stat: -rw-r--r-- 7,572 bytes parent folder | download
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
/*
 * 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 "errorReporter.h"
#include "guiErrorCheck.h"
#include "expenseCategory.h"

#include <QVariant>
#include <QMessageBox>

expenseCategory::expenseCategory(QWidget* parent, const char* name, bool modal, Qt::WindowFlags fl)
    : XDialog(parent, name, modal, fl)
{
  setupUi(this);

  _expcatid = -1;

  // signals and slots connections
  connect(_buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
  connect(_buttonBox, SIGNAL(accepted()), this, SLOT(sSave()));
  connect(_category, SIGNAL(editingFinished()), this, SLOT(sCheck()));

  _expense->setType(GLCluster::cExpense | GLCluster::cAsset | GLCluster::cLiability);
  _purchasePrice->setType(GLCluster::cAsset | GLCluster::cExpense);
  _liability->setType(GLCluster::cLiability);
  _freight->setType(GLCluster::cExpense);

}

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

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

enum SetResponse expenseCategory::set(const ParameterList &pParams)
{
  XDialog::set(pParams);
  QVariant param;
  bool     valid;

  param = pParams.value("expcat_id", &valid);
  if (valid)
  {
    _expcatid = param.toInt();
    populate();
  }

  param = pParams.value("mode", &valid);
  if (valid)
  {
    if (param.toString() == "new")
    {
      _mode = cNew;
    }
    else if (param.toString() == "edit")
    {
      _mode = cEdit;
    }
    else if (param.toString() == "copy")
    {
      _mode = cCopy;
      _category->clear();
    }
    else if (param.toString() == "view")
    {
      _mode = cView;

      _category->setEnabled(false);
      _active->setEnabled(false);
      _description->setEnabled(false);
      _expense->setReadOnly(true);
      _purchasePrice->setReadOnly(true);
      _liability->setReadOnly(true);
      _freight->setReadOnly(true);
      _buttonBox->clear();
      _buttonBox->addButton(QDialogButtonBox::Close);
    }
  }

  return NoError;
}

void expenseCategory::sCheck()
{
  XSqlQuery expenseCheck;
  _category->setText(_category->text().trimmed().toUpper());
  if ( ((_mode == cNew) || (_mode == cCopy)) && (_category->text().length() != 0) )
  {
    expenseCheck.prepare( "SELECT expcat_id "
               "FROM expcat "
               "WHERE (UPPER(expcat_code)=:expcat_code);" );
    expenseCheck.bindValue(":expcat_code", _category->text().trimmed());
    expenseCheck.exec();
    if (expenseCheck.first())
    {
      _expcatid = expenseCheck.value("expcat_id").toInt();
      _mode = cEdit;
      populate();

      _category->setEnabled(false);
    }
  }
}

void expenseCategory::sSave()
{
  XSqlQuery expenseSave;
  QList<GuiErrorCheck> errors;

  errors << GuiErrorCheck(_category->text().trimmed().isEmpty(), _category,
                         tr("<p>You must specify a name."));

  if (_mode == cEdit)
  {
    expenseSave.prepare( "SELECT expcat_id "
               "  FROM expcat "
               " WHERE((UPPER(expcat_code)=UPPER(:expcat_code))"
               "   AND (expcat_id != :expcat_id));" );
    expenseSave.bindValue(":expcat_id", _expcatid);
    expenseSave.bindValue(":expcat_code", _category->text().trimmed());
    expenseSave.exec();
    if (expenseSave.first())
    {
      errors << GuiErrorCheck(true, _category,
                             tr("<p>The name you have specified is already in use."));
    }
  }

  if (_metrics->boolean("InterfaceAPToGL"))
  {
    errors << GuiErrorCheck(!_expense->isValid(), _expense,
                            tr("<p>You must select a Expense Account Number for this Expense Category before you may save it."))
           << GuiErrorCheck(!_purchasePrice->isValid(), _purchasePrice,
                            tr("<p>You must select a Purchase Price Variance Account Number for this Expense Category before you may save it."))
           << GuiErrorCheck(!_liability->isValid(), _liability,
                            tr("<p>You must select a P/O Liability Clearing Account Number for this Expense Category before you may save it."))
           << GuiErrorCheck(!_freight->isValid(), _freight,
                            tr("<p>You must select a Freight Receiving Account Number for this Expense Category before you may save it."))
           ;
  }

  if (GuiErrorCheck::reportErrors(this, tr("Cannot Save Expense Category"), errors))
    return;

  if ( (_mode == cNew) || (_mode == cCopy) )
  {
    expenseSave.exec("SELECT NEXTVAL('expcat_expcat_id_seq') AS expcat_id");
    if (expenseSave.first())
      _expcatid = expenseSave.value("expcat_id").toInt();

    expenseSave.prepare( "INSERT INTO expcat"
               "( expcat_id, expcat_code, expcat_active, expcat_descrip,"
               "  expcat_exp_accnt_id, expcat_purchprice_accnt_id,"
               "  expcat_liability_accnt_id, expcat_freight_accnt_id ) "
               "VALUES "
               "( :expcat_id, :expcat_code, :expcat_active, :expcat_descrip,"
               "  :expcat_exp_accnt_id, :expcat_purchprice_accnt_id,"
               "  :expcat_liability_accnt_id, :expcat_freight_accnt_id );" );
  }
  else if (_mode == cEdit)
    expenseSave.prepare( "UPDATE expcat "
               "SET expcat_code=:expcat_code, expcat_active=:expcat_active,"
               "    expcat_descrip=:expcat_descrip,"
               "    expcat_exp_accnt_id=:expcat_exp_accnt_id,"
               "    expcat_purchprice_accnt_id=:expcat_purchprice_accnt_id,"
               "    expcat_liability_accnt_id=:expcat_liability_accnt_id,"
               "    expcat_freight_accnt_id=:expcat_freight_accnt_id "
               "WHERE (expcat_id=:expcat_id);" );

  expenseSave.bindValue(":expcat_id", _expcatid);
  expenseSave.bindValue(":expcat_code", _category->text().trimmed());
  expenseSave.bindValue(":expcat_active", QVariant(_active->isChecked()));
  expenseSave.bindValue(":expcat_descrip", _description->text().trimmed());
  expenseSave.bindValue(":expcat_exp_accnt_id", _expense->id());
  expenseSave.bindValue(":expcat_purchprice_accnt_id", _purchasePrice->id());
  expenseSave.bindValue(":expcat_liability_accnt_id", _liability->id());
  expenseSave.bindValue(":expcat_freight_accnt_id", _freight->id());
  expenseSave.exec();

  done(_expcatid);
}

void expenseCategory::populate()
{
  XSqlQuery expensepopulate;
  expensepopulate.prepare( "SELECT * "
             "FROM expcat "
             "WHERE (expcat_id=:expcat_id);" );
  expensepopulate.bindValue(":expcat_id", _expcatid);
  expensepopulate.exec();
  if (expensepopulate.first())
  {
    if (_mode != cCopy)
    {
      _category->setText(expensepopulate.value("expcat_code").toString());
      _description->setText(expensepopulate.value("expcat_descrip").toString());
      _active->setChecked(expensepopulate.value("expcat_active").toBool());
    }
    else
      _active->setChecked(true);

    _expense->setId(expensepopulate.value("expcat_exp_accnt_id").toInt());
    _purchasePrice->setId(expensepopulate.value("expcat_purchprice_accnt_id").toInt());
    _liability->setId(expensepopulate.value("expcat_liability_accnt_id").toInt());
    _freight->setId(expensepopulate.value("expcat_freight_accnt_id").toInt());
  }
}