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
|
/*
* 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 "uom.h"
#include <QVariant>
#include <QMessageBox>
#include <QSqlError>
#include <parameter.h>
#include "storedProcErrorLookup.h"
#include "uomConv.h"
#include "errorReporter.h"
uom::uom(QWidget* parent, const char* name, bool modal, Qt::WindowFlags fl)
: XDialog(parent, name, modal, fl)
{
setupUi(this);
connect(_buttonBox, SIGNAL(accepted()), this, SLOT(sSave()));
connect(_name, SIGNAL(editingFinished()), this, SLOT(sCheck()));
connect(_buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
connect(_uomconv, SIGNAL(itemSelectionChanged()), this, SLOT(sSelected()));
connect(_new, SIGNAL(clicked()), this, SLOT(sNew()));
connect(_edit, SIGNAL(clicked()), this, SLOT(sEdit()));
connect(_delete, SIGNAL(clicked()), this, SLOT(sDelete()));
_uomconv->addColumn(tr("UOM/UOM"), -1, Qt::AlignLeft, true, "uomuom");
_uomconv->addColumn(tr("From Value"), -1, Qt::AlignRight, true, "uomconv_from_value");
_uomconv->addColumn(tr("To Value"), -1, Qt::AlignRight, true, "uomconv_to_value");
_uomconv->addColumn(tr("Fractional"), _ynColumn, Qt::AlignCenter, true, "uomconv_fractional");
}
uom::~uom()
{
// no need to delete child widgets, Qt does it all for us
}
void uom::languageChange()
{
retranslateUi(this);
}
enum SetResponse uom::set(const ParameterList &pParams)
{
XDialog::set(pParams);
QVariant param;
bool valid;
param = pParams.value("uom_id", &valid);
if (valid)
{
_uomid = param.toInt();
populate();
}
param = pParams.value("mode", &valid);
if (valid)
{
if (param.toString() == "new")
_mode = cNew;
else if (param.toString() == "edit")
{
_mode = cEdit;
_new->setEnabled(true);
}
else if (param.toString() == "view")
{
_mode = cView;
_name->setEnabled(false);
_description->setEnabled(false);
_weightUom->setEnabled(false);
_buttonBox->clear();
_buttonBox->addButton(QDialogButtonBox::Close);
_edit->setText(tr("&View"));
}
}
return NoError;
}
void uom::sSave()
{
XSqlQuery uomSave;
if (_name->text().length() == 0)
{
QMessageBox::information( this, tr("No UOM Name Entered"),
tr("You must enter a valid UOM name before saving this UOM.") );
_name->setFocus();
return;
}
if (_weightUom->isChecked())
{
if (_mode == cNew)
uomSave.exec("SELECT uom_id FROM uom WHERE (uom_item_weight);");
else
{
uomSave.prepare("SELECT uom_id FROM uom WHERE ((uom_item_weight) AND (uom_id<>:uom_id));");
uomSave.bindValue(":uom_id", _uomid);
uomSave.exec();
}
if (uomSave.first())
{
int response = QMessageBox::warning (this, tr("Set Item Weight?"),
tr("The Item Weight UOM has already been set. "
"Are you sure you want to clear the existing entry and set "
"%1 to be the Item Weight UOM?")
.arg(_name->text()),
QMessageBox::Yes | QMessageBox::Escape,
QMessageBox::No | QMessageBox::Default);
if (response == QMessageBox::Yes)
uomSave.exec("UPDATE uom SET uom_item_weight=false;");
else
return;
}
}
if (_mode == cNew)
{
uomSave.exec("SELECT NEXTVAL('uom_uom_id_seq') AS uom_id;");
if (uomSave.first())
_uomid = uomSave.value("uom_id").toInt();
else
{
ErrorReporter::error(QtCriticalMsg, this, tr("Error Saving UOM Information"),
uomSave, __FILE__, __LINE__);
return;
}
uomSave.prepare( "INSERT INTO uom "
"( uom_id, uom_name, uom_descrip, uom_item_weight ) "
"VALUES "
"( :uom_id, :uom_name, :uom_descrip, :uom_item_weight );" );
}
else if (_mode == cEdit)
uomSave.prepare( "UPDATE uom "
"SET uom_name=:uom_name, uom_descrip=:uom_descrip,"
" uom_item_weight=:uom_item_weight "
"WHERE (uom_id=:uom_id);" );
uomSave.bindValue(":uom_id", _uomid);
uomSave.bindValue(":uom_name", _name->text());
uomSave.bindValue(":uom_descrip", _description->text());
uomSave.bindValue(":uom_item_weight", QVariant(_weightUom->isChecked()));
uomSave.exec();
if (ErrorReporter::error(QtCriticalMsg, this, tr("Error Saving UOM Information"),
uomSave, __FILE__, __LINE__))
{
return;
}
done(_uomid);
}
void uom::sCheck()
{
XSqlQuery uomCheck;
_name->setText(_name->text().trimmed().toUpper());
if ( (_mode == cNew) && (_name->text().length()) )
{
uomCheck.prepare( "SELECT uom_id "
"FROM uom "
"WHERE (UPPER(uom_name)=UPPER(:uom_name));" );
uomCheck.bindValue(":uom_name", _name->text());
uomCheck.exec();
if (uomCheck.first())
{
_uomid = uomCheck.value("uom_id").toInt();
_mode = cEdit;
populate();
_name->setEnabled(false);
}
}
}
void uom::populate()
{
XSqlQuery uompopulate;
uompopulate.prepare( "SELECT uom_name, uom_descrip, uom_item_weight "
" FROM uom "
" WHERE(uom_id=:uom_id);" );
uompopulate.bindValue(":uom_id", _uomid);
uompopulate.exec();
if (uompopulate.first())
{
_name->setText(uompopulate.value("uom_name").toString());
_description->setText(uompopulate.value("uom_descrip").toString());
_weightUom->setChecked(uompopulate.value("uom_item_weight").toBool());
sFillList();
}
}
void uom::sFillList()
{
XSqlQuery uomFillList;
uomFillList.prepare("SELECT uomconv_id,"
" (nuom.uom_name||'/'||duom.uom_name) AS uomuom,"
" uomconv_from_value, uomconv_to_value,"
" uomconv_fractional,"
" 'uomratio' AS uomconv_from_value_xtnumericrole,"
" 'uomratio' AS uomconv_to_value_xtnumericrole "
" FROM uomconv"
" JOIN uom AS nuom ON (uomconv_from_uom_id=nuom.uom_id)"
" JOIN uom AS duom ON (uomconv_to_uom_id=duom.uom_id)"
" WHERE((uomconv_from_uom_id=:uom_id)"
" OR (uomconv_to_uom_id=:uom_id));");
uomFillList.bindValue(":uom_id", _uomid);
uomFillList.exec();
_uomconv->populate(uomFillList);
}
void uom::sSelected()
{
if(_uomconv->id() != -1)
{
_edit->setEnabled(true);
_edit->setText((_mode==cView?tr("&View"):tr("&Edit")));
_delete->setEnabled(!(_mode==cView));
}
else
{
_edit->setEnabled(false);
_delete->setEnabled(false);
}
}
void uom::sNew()
{
ParameterList params;
params.append("mode", "new");
params.append("from_uom_id", _uomid);
uomConv newdlg(this, "", true);
newdlg.set(params);
if(newdlg.exec() == XDialog::Accepted)
sFillList();
}
void uom::sEdit()
{
ParameterList params;
if(_mode == cView)
params.append("mode", "view");
else
params.append("mode", "edit");
params.append("uomconv_id", _uomconv->id());
uomConv newdlg(this, "", true);
newdlg.set(params);
if(newdlg.exec() == XDialog::Accepted)
sFillList();
}
void uom::sDelete()
{
XSqlQuery uomDelete;
uomDelete.prepare( "SELECT deleteUOMConv(:uomconv_id) AS result;" );
uomDelete.bindValue(":uomconv_id", _uomconv->id());
uomDelete.exec();
if (uomDelete.first())
{
int result = uomDelete.value("result").toInt();
if (result < 0)
{
ErrorReporter::error(QtCriticalMsg, this, tr("Error Deleting UOM Information"),
storedProcErrorLookup("deleteUOMConv", result),
__FILE__, __LINE__);
return;
}
}
else if (ErrorReporter::error(QtCriticalMsg, this, tr("Error Deleting UOM Information"),
uomDelete, __FILE__, __LINE__))
{
return;
}
sFillList();
}
|