File: correctProductionPosting.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 (217 lines) | stat: -rw-r--r-- 6,144 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
/*
 * 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 "correctProductionPosting.h"

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

#include "closeWo.h"
#include "distributeInventory.h"
#include "inputManager.h"
#include "storedProcErrorLookup.h"

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

  connect(_correct, SIGNAL(clicked()), this, SLOT(sCorrect()));
  connect(_wo,     SIGNAL(newId(int)), this, SLOT(populate()));

  _captive = false;
  _transDate->setEnabled(_privileges->check("AlterTransactionDates"));
  _transDate->setDate(omfgThis->dbDate(), true);
  _qtyReceivedCache = 0.0;

  omfgThis->inputManager()->notify(cBCWorkOrder, this, _wo, SLOT(setId(int)));

  _wo->setType(cWoIssued);
  _qty->setValidator(omfgThis->qtyVal());
  _qtyOrdered->setPrecision(omfgThis->transQtyVal());
  _qtyReceived->setPrecision(omfgThis->transQtyVal());
  _qtyBalance->setPrecision(omfgThis->transQtyVal());

  if (_preferences->boolean("XCheckBox/forgetful"))
    _backFlush->setChecked(true);
  _nonPickItems->setEnabled(_backFlush->isChecked() &&
			    _privileges->check("ChangeNonPickItems"));

  // TODO: unhide as part of implementation of 5847
  _nonPickItems->hide();

  adjustSize();
}

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

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

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

  param = pParams.value("wo_id", &valid);
  if (valid)
  {
    _captive = true;

    _wo->setId(param.toInt());
    _wo->setReadOnly(true);
  }

  return NoError;
}

bool correctProductionPosting::okToPost()
{
  if (!_transDate->isValid())
  {
    QMessageBox::critical(this, tr("Invalid date"),
                          tr("You must enter a valid transaction date.") );
    _transDate->setFocus();
    return false;
  }
  else if (_qty->toDouble() > _qtyReceivedCache)
  {
    QMessageBox::warning( this, tr("Cannot Post Correction"),
                          tr( "The Quantity to correct must be less than or equal to the Quantity already Posted." ) );
    _qty->setFocus();
    return false;
  }

  XSqlQuery itemtypeq;
  itemtypeq.prepare( "SELECT itemsite_costmethod "
             "FROM wo, itemsite "
             "WHERE ( (wo_itemsite_id=itemsite_id)"
             " AND (wo_id=:wo_id) );" );
  itemtypeq.bindValue(":wo_id", _wo->id());
  itemtypeq.exec();
  if (itemtypeq.first() && (itemtypeq.value("itemsite_costmethod").toString() == "J"))
  {
    QMessageBox::warning(this, tr("Cannot Post Correction"),
                         tr("You may not post a correction to a Work Order for a "
                            "Item Site with the Job cost method. You must, "
                            "instead, adjust shipped quantities."));
    return false;
  }
  else if (itemtypeq.lastError().type() != QSqlError::NoError)
  {
    systemError(this, itemtypeq.lastError().databaseText(), __FILE__, __LINE__);
    return false;
  }

  return true;
}

void correctProductionPosting::clear()
{
  _wo->setId(-1);
  _qty->clear();
  _close->setText(tr("&Close"));
  _wo->setFocus();
}

void correctProductionPosting::sCorrect()
{
  XSqlQuery correctCorrect;
  if (! okToPost())
    return;

  XSqlQuery rollback;
  rollback.prepare("ROLLBACK;");

  correctCorrect.exec("BEGIN;");	// handle cancel of lot, serial, or loc distribution
  correctCorrect.prepare("SELECT correctProduction(:wo_id, :qty, :backflushMaterials, 0, :date)"
            "        AS result;");
  correctCorrect.bindValue(":wo_id", _wo->id());
  if (_wo->method() == "A")
    correctCorrect.bindValue(":qty", _qty->toDouble());
  else
    correctCorrect.bindValue(":qty", _qty->toDouble() * -1);
  correctCorrect.bindValue(":backflushMaterials",  QVariant(_backFlush->isChecked()));
  correctCorrect.bindValue(":date",  _transDate->date());
  correctCorrect.exec();
  if (correctCorrect.first())
  {
    int result = correctCorrect.value("result").toInt();
    if (result < 0)
    {
      rollback.exec();
      systemError(this, storedProcErrorLookup("correctProduction", result),
                  __FILE__, __LINE__);
      return;
    }

    if (distributeInventory::SeriesAdjust(result, this) == XDialog::Rejected)
    {
      rollback.exec();
      QMessageBox::information( this, tr("Correct Production Posting"), tr("Transaction Canceled") );
      return;
    }

    correctCorrect.exec("COMMIT;");
    omfgThis->sWorkOrdersUpdated(_wo->id(), true);
  }
  else if (correctCorrect.lastError().type() != QSqlError::NoError)
  {
    rollback.exec();
    systemError(this, correctCorrect.lastError().databaseText(), __FILE__, __LINE__);
    return;
  }

  if (_captive)
    accept();
  else
    clear();
}

void correctProductionPosting::populate()
{
  if (_wo->id() != -1)
  {
    if (_wo->method() == "D")
    {
      _backFlush->setEnabled(false);
      _backFlush->setChecked(false);
      _qtyOrderedLit->setText(tr("Qty. to Disassemble:"));
      _qtyReceivedLit->setText(tr("Qty. Disassembled:"));
    }
    else
    {
      _backFlush->setEnabled(true);
      _qtyOrderedLit->setText(tr("Qty. Ordered:"));
      _qtyReceivedLit->setText(tr("Qty. Received:"));
    }
    
    _qtyOrdered->setText(_wo->qtyOrdered());
    _qtyReceived->setText(_wo->qtyReceived());
    _qtyBalance->setText(_wo->qtyBalance());

    _qtyReceivedCache = _wo->qtyReceived();
  }
  else
  {
    _qtyOrdered->clear();
    _qtyReceived->clear();
    _qtyBalance->clear();

    _qtyReceivedCache = 0;
  }
}