File: applyAPCreditMemo.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 (216 lines) | stat: -rw-r--r-- 7,570 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
/*
 * 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 "applyAPCreditMemo.h"

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

#include <metasql.h>

#include "apCreditMemoApplication.h"
#include "errorReporter.h"
#include "guiErrorCheck.h"
#include "storedProcErrorLookup.h"
#include "mqlutil.h"

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

  connect(_apply,          SIGNAL(clicked()), this, SLOT(sApply()));
  connect(_applyToBalance, SIGNAL(clicked()), this, SLOT(sApplyBalance()));
  connect(_available, SIGNAL(idChanged(int)), this, SLOT(sPriceGroup()));
  connect(_clear,          SIGNAL(clicked()), this, SLOT(sClear()));
  connect(_buttonBox,      SIGNAL(accepted()), this, SLOT(sPost()));
  connect(_buttonBox,      SIGNAL(rejected()), this, SLOT(sClose()));

  _buttonBox->button(QDialogButtonBox::Save)->setText(tr("Post"));

  _captive = false;

  _apopen->addColumn(tr("Doc. Type"),   _docTypeColumn,  Qt::AlignCenter,true, "doctype");
  _apopen->addColumn(tr("Doc. Number"), -1,              Qt::AlignCenter,true, "apopen_docnumber");
  _apopen->addColumn(tr("Doc. Date"),   _dateColumn,     Qt::AlignCenter,true, "apopen_docdate");
  _apopen->addColumn(tr("Due Date"),    _dateColumn,     Qt::AlignCenter,true, "apopen_duedate");
  _apopen->addColumn(tr("Open"),        _moneyColumn,    Qt::AlignRight, true, "openamount");
  _apopen->addColumn(tr("Currency"),    _currencyColumn, Qt::AlignLeft,  !omfgThis->singleCurrency(), "opencurrabbr");
  _apopen->addColumn(tr("Applied"),     _moneyColumn,    Qt::AlignRight, true, "apcreditapply_amount");
  _apopen->addColumn(tr("Currency"),    _currencyColumn, Qt::AlignLeft,  !omfgThis->singleCurrency(), "appliedcurrabbr");

  if (_privileges->check("ApplyAPMemos"))
      connect(_apopen, SIGNAL(valid(bool)), _apply, SLOT(setEnabled(bool)));

  _vend->setReadOnly(true);
  sPriceGroup();
  adjustSize();
}

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

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

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

  param = pParams.value("apopen_id", &valid);
  if (valid)
  {
    _captive = true;
    _apopenid = param.toInt();
    populate();
  }

  return NoError;
}

void applyAPCreditMemo::sPost()
{
  XSqlQuery applyPost;

  applyPost.prepare("SELECT postAPCreditMemoApplication(:apopen_id) AS result;");
  applyPost.bindValue(":apopen_id", _apopenid);
  applyPost.exec();
  if (ErrorReporter::error(QtCriticalMsg, this,
                           tr("Error posting"), applyPost,
                           __FILE__, __LINE__))
    return;

  accept();
}

void applyAPCreditMemo::sApplyBalance()
{
  XSqlQuery applyApplyBalance;
  applyApplyBalance.prepare("SELECT applyAPCreditMemoToBalance(:apopen_id) AS result;");
  applyApplyBalance.bindValue(":apopen_id", _apopenid);
  applyApplyBalance.exec();
  if (applyApplyBalance.first())
  {
    int result = applyApplyBalance.value("result").toInt();
    if (result < 0)
    {
      ErrorReporter::error(QtCriticalMsg, this, tr("Error Applying A/P Credit Memo"),
                             storedProcErrorLookup("applyAPCreditMemoToBalance", result),
                             __FILE__, __LINE__);
      return;
    }
  }
  else if (ErrorReporter::error(QtCriticalMsg, this, tr("Error Applying A/P Credit Memo"),
                                applyApplyBalance, __FILE__, __LINE__))
  {
    return;
  }

  populate();
}

void applyAPCreditMemo::sApply()
{
  ParameterList params;
  params.append("sourceApopenid", _apopenid);
  params.append("targetApopenid", _apopen->id());

  apCreditMemoApplication newdlg(this, "", true);
  newdlg.set(params);
  if (newdlg.exec() != XDialog::Rejected)
    populate();
}

void applyAPCreditMemo::sClear()
{
  XSqlQuery applyClear;
  applyClear.prepare( "DELETE FROM apcreditapply "
             "WHERE ( (apcreditapply_source_apopen_id=:sourceApopenid) "
             " AND (apcreditapply_target_apopen_id=:targetApopenid) );" );
  applyClear.bindValue(":sourceApopenid", _apopenid);
  applyClear.bindValue(":targetApopenid", _apopen->id());
  applyClear.exec();
  ErrorReporter::error(QtCriticalMsg, this, tr("Error Clearing Application of A/P CM"),
                                applyClear, __FILE__, __LINE__);

  populate();
}

void applyAPCreditMemo::sClose()
{
  XSqlQuery applyClose;
  applyClose.prepare( "DELETE FROM apcreditapply "
             "WHERE (apcreditapply_source_apopen_id=:sourceApopenid);" );
  applyClose.bindValue(":sourceApopenid", _apopenid);
  applyClose.exec();
  if (ErrorReporter::error(QtCriticalMsg, this, tr("Error Cancelling Application of A/P CM"),
                                applyClose, __FILE__, __LINE__))
  {
    return;
  }

  reject();
}

void applyAPCreditMemo::populate()
{
  XSqlQuery applypopulate;
  applypopulate.prepare( "SELECT apopen_vend_id, apopen_docnumber, apopen_docdate,"
             "       (apopen_amount - apopen_paid) AS available, apopen_curr_id, "
             "       COALESCE(SUM(currToCurr(apcreditapply_curr_id,"
	     "				apopen_curr_id, apcreditapply_amount, "
	     "				CURRENT_DATE)), 0) AS f_applied "
             "FROM apopen LEFT OUTER JOIN apcreditapply ON (apcreditapply_source_apopen_id=apopen_id) "
             "WHERE (apopen_id=:apopen_id) "
             "GROUP BY apopen_vend_id, apopen_docnumber, apopen_docdate,"
             "         apopen_curr_id, apopen_amount, apopen_paid;" );
  applypopulate.bindValue(":apopen_id", _apopenid);
  applypopulate.exec();
  if (applypopulate.first())
  {
    _vend->setId(applypopulate.value("apopen_vend_id").toInt());
    _docDate->setDate(applypopulate.value("apopen_docdate").toDate(), true);
    _available->setId(applypopulate.value("apopen_curr_id").toInt());
    _available->setLocalValue(applypopulate.value("available").toDouble());
    _applied->setLocalValue(applypopulate.value("f_applied").toDouble());
    _balance->setLocalValue(_available->localValue() - _applied->localValue());
    _docNumber->setText(applypopulate.value("apopen_docnumber").toString());
  
    _cachedAmount = applypopulate.value("available").toDouble();
  }
  else (ErrorReporter::error(QtCriticalMsg, this, tr("Error Retrieving A/P Information"),
                                applypopulate, __FILE__, __LINE__));


  MetaSQLQuery mql = mqlLoad("applyAPMemo", "details");
  ParameterList params;
  params.append("parentApopenid", _apopenid);
  params.append("vend_id", _vend->id());
  params.append("voucher", tr("Voucher"));
  params.append("debitMemo", tr("Debit Memo"));

  applypopulate = mql.toQuery(params);
  _apopen->populate(applypopulate);
  ErrorReporter::error(QtCriticalMsg, this, tr("Error Retrieving A/P Information"),
                                applypopulate, __FILE__, __LINE__);
}

void applyAPCreditMemo::sPriceGroup()
{
  if (! omfgThis->singleCurrency())
    _priceGroup->setTitle(tr("In %1:").arg(_available->currAbbr()));
}