File: bankAdjustment.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 (205 lines) | stat: -rw-r--r-- 6,689 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
/*
 * 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 "bankAdjustment.h"

#include <QVariant>
#include <QMessageBox>
#include <QSqlError>
#include <QValidator>

#include "guiErrorCheck.h"
#include "errorReporter.h"

bankAdjustment::bankAdjustment(QWidget* parent, const char* name, Qt::WindowFlags fl)
: XWidget(parent, name, fl)
{
  setupUi(this);
  
  connect(_close, SIGNAL(clicked()), this, SLOT(close()));
  connect(_save, SIGNAL(clicked()), this, SLOT(sSave()));
  connect(_bankaccnt, SIGNAL(newID(int)), this, SLOT(sBankAccount(int)));
  
  _bankaccnt->populate("SELECT bankaccnt_id,"
                       "       (bankaccnt_name || '-' || bankaccnt_descrip),"
                       "       bankaccnt_name "
                       "FROM bankaccnt "
                       "ORDER BY bankaccnt_name;");
  _bankadjtype->populate("SELECT bankadjtype_id,"
                         "       (bankadjtype_name || '-' || bankadjtype_descrip),"
                         "       bankadjtype_name "
                         "FROM bankadjtype "
                         "ORDER BY bankadjtype_name;");
  
  _bankadjid = -1;
}

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

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

SetResponse bankAdjustment::set( const ParameterList & pParams )
{
  XWidget::set(pParams);
  QVariant param;
  bool     valid;
  
  param = pParams.value("bankaccnt_id", &valid);
  if (valid)
  {
    _bankaccnt->setId(param.toInt());
    //_bankaccnt->setEnabled(false);
  }
  
  param = pParams.value("bankadj_id", &valid);
  if(valid)
  {
    _bankadjid = 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() == "view")
    {
      _mode = cView;
      
      _bankaccnt->setEnabled(false);
      _bankadjtype->setEnabled(false);
      _date->setEnabled(false);
      _docNumber->setEnabled(false);
      _amount->setEnabled(false);
      _notes->setReadOnly(true);
      _save->hide();
    }
  }
  
  return NoError;
}

void bankAdjustment::sSave()
{
  XSqlQuery bankSave;
  
  QList<GuiErrorCheck> errors;
  errors << GuiErrorCheck(!_date->isValid(), _date,
                          tr("You must enter a date before posting this Bank Adjustment."))
         << GuiErrorCheck(_amount->isZero(), _amount,
                          tr("You must enter an amount before posting this Bank Adjustment."))
  ;
  
  bankSave.prepare ("SELECT period_id "
                    "FROM period "
                    "WHERE :date BETWEEN period_start and period_end;");
  bankSave.bindValue(":date", _date->date());
  bankSave.exec();
  if (!bankSave.first())
  {
    errors << GuiErrorCheck(true, _date,
                            tr("You must enter a valid fiscal period date before posting this Bank Adjustment."));
  }
  
  if (GuiErrorCheck::reportErrors(this, tr("Cannot Save Bank Adjustment"), errors))
    return;
  
  if (_mode == cNew)
    bankSave.prepare("INSERT INTO bankadj "
                     "(bankadj_bankaccnt_id, bankadj_bankadjtype_id,"
                     " bankadj_date, bankadj_docnumber, bankadj_amount, "
                     " bankadj_notes, bankadj_curr_id ) "
                     "VALUES "
                     "(:bankaccnt_id, :bankadjtype_id,"
                     " :date, :docnumber, :amount, :notes, :curr_id);" );
  else if (_mode == cEdit)
  {
    bankSave.prepare ("UPDATE bankadj "
                      "SET bankadj_bankaccnt_id=:bankaccnt_id,"
                      " bankadj_bankadjtype_id=:bankadjtype_id,"
                      " bankadj_date=:date,"
                      " bankadj_docnumber=:docnumber,"
                      " bankadj_amount=:amount,"
                      " bankadj_notes=:notes, "
                      " bankadj_curr_id=:curr_id "
                      "WHERE ((bankadj_id=:bankadj_id)"
                      " AND (NOT bankadj_posted) ); ");
    bankSave.bindValue(":bankadj_id", _bankadjid);
  }
  
  bankSave.bindValue(":bankaccnt_id", _bankaccnt->id());
  bankSave.bindValue(":bankadjtype_id", _bankadjtype->id());
  bankSave.bindValue(":date", _date->date());
  bankSave.bindValue(":docnumber", _docNumber->text());
  bankSave.bindValue(":amount", _amount->localValue());
  bankSave.bindValue(":notes",   _notes->toPlainText());
  bankSave.bindValue(":curr_id", _amount->id());
  
  if(!bankSave.exec())
  {
    ErrorReporter::error(QtCriticalMsg, this, tr("Error Saving Bank Adjustment"),
                                    bankSave, __FILE__, __LINE__);
    return;
  }
  
  omfgThis->sBankAdjustmentsUpdated(_bankadjid, true);
  
  close();
}

void bankAdjustment::populate()
{
  XSqlQuery bankpopulate;
  bankpopulate.prepare("SELECT bankadj.* "
                       "FROM bankadj "
                       "WHERE (bankadj_id=:bankadj_id);" );
  bankpopulate.bindValue(":bankadj_id", _bankadjid);
  bankpopulate.exec();
  if(bankpopulate.first())
  {
    _bankaccnt->setId(bankpopulate.value("bankadj_bankaccnt_id").toInt());
    _bankadjtype->setId(bankpopulate.value("bankadj_bankadjtype_id").toInt());
    _date->setDate(bankpopulate.value("bankadj_date").toDate());
    _docNumber->setText(bankpopulate.value("bankadj_docnumber").toString());
    _amount->set(bankpopulate.value("bankadj_amount").toDouble(),
                 bankpopulate.value("bankadj_curr_id").toInt(),
                 bankpopulate.value("bankadj_date").toDate(), false);
    _notes->setText(bankpopulate.value("bankadj_notes").toString());
  }
}

void bankAdjustment::sBankAccount(int accountId)
{
  XSqlQuery bankQ;
  bankQ.prepare("SELECT bankaccnt_curr_id "
                "FROM bankaccnt WHERE bankaccnt_id = :accntId;");
  bankQ.bindValue(":accntId", accountId);
  bankQ.exec();
  if (bankQ.first())
    _amount->setId(bankQ.value("bankaccnt_curr_id").toInt());
  if (bankQ.lastError().type() != QSqlError::NoError)
    QMessageBox::critical(this, tr("A System Error occurred at %1::%2.")
                          .arg(__FILE__)
                          .arg(__LINE__),
                          bankQ.lastError().databaseText());
}