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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
/*
* 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.
*/
// TODO: drag/drop support?
// TODO: make a base class for the similarities of this and poitemTableModel
#include "toitemTableModel.h"
#include <QMessageBox>
#include <QSqlError>
#include <QSqlField>
#include <QSqlRecord>
#include <QString>
#include "guiclient.h"
#include "currcluster.h"
#include "errorReporter.h"
ToitemTableModel::ToitemTableModel(QObject * parent, QSqlDatabase db) :
QSqlRelationalTableModel(parent, db)
{
setTable("toitem");
// select statement on which everything else is based
_selectStatement =
QString("SELECT tohead_number, "
" item_number,"
" prj_number,"
" CURRENT_DATE AS earliestdate, "
" toitem.* "
"FROM tohead"
" JOIN toitem ON (toitem_tohead_id=tohead_id)"
" LEFT OUTER JOIN item ON (toitem_item_id=item_id)"
" LEFT OUTER JOIN prj ON (toitem_prj_id=prj_id)"
);
setEditStrategy(QSqlTableModel::OnManualSubmit); // OnRow?
setSort(TOITEM_LINENUMBER_COL, Qt::AscendingOrder);
// insert only those columns not directly part of the toitem table
insertColumns(0, 4);
setHeaderData(TOITEM_LINENUMBER_COL, Qt::Horizontal, tr("#"));
setHeaderData(ITEM_NUMBER_COL, Qt::Horizontal, tr("Item"));
setHeaderData(TOITEM_QTY_ORDERED_COL, Qt::Horizontal, tr("Qty."));
setHeaderData(TOITEM_STDCOST_COL, Qt::Horizontal, tr("Std. Cost"));
setHeaderData(TOITEM_FREIGHT_COL, Qt::Horizontal, tr("Freight"));
setHeaderData(TOITEM_DUEDATE_COL, Qt::Horizontal, tr("Due Date"));
setHeaderData(PRJ_NUMBER_COL, Qt::Horizontal, tr("Project #"));
_toheadid = -1;
_toitemid = -1;
findHeadData();
_dirty = false;
select();
connect(this, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(markDirty(QModelIndex, QModelIndex)));
}
void ToitemTableModel::findHeadData()
{
_toheadcurrid = CurrDisplay::baseId();
_toheaddate = QDate();
_tostatus = "O"; // safest default
_toheadsrcwhsid = -1;
XSqlQuery toheadq;
if (_toheadid > 0)
{
toheadq.prepare("SELECT * "
"FROM tohead "
"WHERE (tohead_id=:tohead_id);");
toheadq.bindValue(":tohead_id", _toheadid);
}
else if (_toitemid > 0)
{
toheadq.prepare("SELECT * "
"FROM tohead "
"WHERE tohead_id IN (SELECT toitem_tohead_id "
" FROM toitem WHERE toitem_id=:toitem_id);");
toheadq.bindValue(":toitem_id", _toitemid);
}
else
return;
toheadq.exec();
if (toheadq.first())
{
_toheadcurrid = toheadq.value("tohead_freight_curr_id").toInt();
_toheaddate = toheadq.value("tohead_orderdate").toDate();
_tostatus = toheadq.value("tohead_status").toString();
_toheadsrcwhsid=toheadq.value("tohead_src_warehous_id").toInt();
}
else if (ErrorReporter::error(QtCriticalMsg, 0, tr("Error Retrieving Transfer Order Information"),
toheadq, __FILE__, __LINE__))
{
return;
}
}
void ToitemTableModel::setHeadId(const int pId)
{
setFilter(QString("toitem_tohead_id=%1").arg(pId));
_toheadid = pId;
_toitemid = -1;
findHeadData();
}
void ToitemTableModel::setItemId(const int pId)
{
setFilter(QString("toitem_id=%1").arg(pId));
_toheadid = -1;
_toitemid = pId;
findHeadData();
}
void ToitemTableModel::setShipDate(const QDate pDate)
{
_toshipdate = pDate;
}
void ToitemTableModel::setSrcWhsId(const int pId)
{
_toheadsrcwhsid = pId;
}
void ToitemTableModel::setTransDate(const QDate pDate)
{
_toheaddate = pDate;
}
void ToitemTableModel::setCurrId(const int pId)
{
_toheadcurrid = pId;
}
QString ToitemTableModel::selectStatement() const
{
return _selectStatement + " WHERE " +
QString((filter().isEmpty()) ? " (toitem_tohead_id=-1) " : filter()) +
" ORDER BY toitem_linenumber"
;
}
bool ToitemTableModel::select()
{
bool returnVal = QSqlRelationalTableModel::select();
if (returnVal)
{
insertRow(rowCount());
_dirty = false;
connect(this, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(markDirty(QModelIndex, QModelIndex)));
}
return returnVal;
}
bool ToitemTableModel::submitAll()
{
XSqlQuery begin("BEGIN;");
bool returnVal = QSqlRelationalTableModel::submitAll();
if (returnVal)
{
_dirty = false;
XSqlQuery commit("COMMIT;");
}
else
XSqlQuery rollback("ROLLBACK;");
return returnVal;
}
Qt::ItemFlags ToitemTableModel::flags(const QModelIndex& index) const
{
Qt::ItemFlags flags = QSqlRelationalTableModel::flags(index);
if (index.column() == TOITEM_STDCOST_COL)
flags &= ~(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled);
return flags;
}
bool ToitemTableModel::isDirty() const
{
return _dirty;
}
bool ToitemTableModel::removeRow(int row, const QModelIndex & parent)
{
return QSqlRelationalTableModel::removeRow(row, parent);
}
/*
Make sure the row is internally consistent. If so then fix it so the
parent class has a valid row to insert (some of the SELECTed columns
shouldn't be directly modified in the db 'cause they're not part of the
model's current table).
*/
bool ToitemTableModel::validRow(QSqlRecord& record)
{
QString errormsg;
QString warningmsg;
if (record.value("item_number").toString().isEmpty())
errormsg = tr("<p>You must select an Item Number before you may save.");
else if (record.value("toitem_qty_ordered").toDouble() <= 0)
errormsg = tr("<p>You must enter a quantity before you may save this "
"Purchase Order Item.");
else if (! record.value("toitem_duedate").toDate().isValid())
errormsg = tr("<p>You must enter a due date.");
else if (record.value("toitem_tohead_id").toInt() <= 0 &&
_toheadid <= 0)
errormsg = tr("<p>There is no Transfer Order header yet. "
"Try entering ????.");
int index = record.indexOf("toitem_tohead_id");
if (index < 0)
{
QSqlField field("toitem_tohead_id", QVariant::Int);
field.setValue(_toheadid);
record.append(field);
}
else
record.setValue(index, _toheadid);
XSqlQuery ln;
ln.prepare("SELECT COUNT(*) + 1 AS newln "
"FROM toitem "
"WHERE (toitem_tohead_id=:tohead_id);");
ln.bindValue(":tohead_id", _toheadid);
if (record.indexOf("toitem_linenumber") < 0)
{
ln.exec();
if (ln.first())
{
QSqlField field("toitem_linenumber", QVariant::Int);
field.setValue(ln.value("newln"));
record.append(field);
}
else if (ln.lastError().type() != QSqlError::NoError)
{
errormsg = ln.lastError().databaseText();
}
}
else if (record.value("toitem_linenumber").toInt() <= 0)
{
ln.exec();
if (ln.first())
record.setValue("toitem_linenumber", ln.value("newln"));
else if (ln.lastError().type() != QSqlError::NoError)
{
errormsg = ln.lastError().databaseText();
}
}
if (record.value("toitem_id").isNull())
{
XSqlQuery idq("SELECT NEXTVAL('toitem_toitem_id_seq') AS toitem_id;");
if (idq.first())
record.setValue("toitem_id", idq.value("toitem_id"));
else if (idq.lastError().type() != QSqlError::NoError)
errormsg = idq.lastError().databaseText();
}
if (_tostatus.isEmpty())
findHeadData();
index = record.indexOf("toitem_freight_curr_id");
if (index < 0)
{
QSqlField field("toitem_freight_curr_id", QVariant::String);
field.setValue(_toheadcurrid);
record.append(field);
}
else if (record.field(index).value().toString().isEmpty())
record.setValue(index, _toheadcurrid);
index = record.indexOf("toitem_status");
if (index < 0)
{
QSqlField field("toitem_status", QVariant::String);
field.setValue(_tostatus);
record.append(field);
}
else if (record.field(index).value().toString().isEmpty())
record.setValue(index, _tostatus);
index = record.indexOf("toitem_schedshipdate");
if (index < 0)
{
QSqlField field("toitem_schedshipdate", QVariant::Date);
field.setValue(_toshipdate);
record.append(field);
}
else if (record.field(index).value().isNull())
record.setValue(index, _toshipdate);
if (! errormsg.isEmpty())
{
setLastError(QSqlError(QString("ToitemTableModel::validRow() error"),
errormsg, QSqlError::UnknownError));
return false;
}
else if (! warningmsg.isEmpty())
{
if (QMessageBox::question(0, tr("Are you sure you want to continue?"),
warningmsg + tr("<p>Do you wish to Save this Order?"),
QMessageBox::Yes,
QMessageBox::No | QMessageBox::Default) == QMessageBox::No)
return false;
}
record.remove(record.indexOf("prj_number"));
record.remove(record.indexOf("item_number"));
record.remove(record.indexOf("tohead_number"));
record.remove(record.indexOf("earliestdate"));
return true;
}
bool ToitemTableModel::insertRowIntoTable(const QSqlRecord& record)
{
if (record.isEmpty())
return true;
bool isNull = true;
for (int i = 0; i < record.count(); i++)
{
if (i == record.indexOf("toitem_tohead_id") ||
(record.value(i).toString().isEmpty() &&
(i == record.indexOf("toitem_status") ||
i == record.indexOf("toitem_comments") )))
continue;
isNull &= record.isNull(i);
}
if (isNull)
return true;
QSqlRecord newRecord(record);
if (! validRow(newRecord))
return false;
return QSqlRelationalTableModel::insertRowIntoTable(newRecord);
}
bool ToitemTableModel::updateRowInTable(int row, const QSqlRecord& record)
{
// touch everything so we can distinguish unchanged fields from NULL/0 as new val
for (int i = 0; i < columnCount(); i++)
setData(index(row, i), data(index(row, i)));
QSqlRecord newRecord(record);
if (! validRow(newRecord))
return false;
return QSqlRelationalTableModel::updateRowInTable(row, newRecord);
}
void ToitemTableModel::markDirty(QModelIndex, QModelIndex)
{
_dirty = true;
disconnect(this, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(markDirty(QModelIndex, QModelIndex)));
}
|