File: shippingForm.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 (179 lines) | stat: -rw-r--r-- 5,066 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
/*
 * 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 "shippingForm.h"

#include <QMessageBox>
#include <QSqlError>
#include <QVariant>
#include "errorReporter.h"

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

  _shipformid = -1;

  connect(_name,	SIGNAL(editingFinished()),	this, SLOT(sCheck()));
  connect(_buttonBox,	SIGNAL(accepted()),	this, SLOT(sSave()));
}

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

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

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

  param = pParams.value("shipform_id", &valid);
  if (valid)
  {
    _shipformid = 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;
      _name->setEnabled(false);
      _report->setEnabled(false);
      _buttonBox->clear();
      _buttonBox->addButton(QDialogButtonBox::Close);
    }
  }

  return NoError;
}

void shippingForm::sCheck()
{
  XSqlQuery shippingCheck;
  _name->setText(_name->text().trimmed());
  if ((_mode == cNew) || (_name->text().length()))
  {
    shippingCheck.prepare( "SELECT shipform_id"
               "  FROM shipform"
               " WHERE((UPPER(shipform_name)=UPPER(:shipform_name))"
               "   AND (shipform_id != :shipform_id));" );
    shippingCheck.bindValue(":shipform_name", _name->text());
    shippingCheck.bindValue(":shipform_id", _shipformid);
    shippingCheck.exec();
    if (shippingCheck.first())
    {
      _shipformid = shippingCheck.value("shipform_id").toInt();
      _mode = cEdit;
      populate();

      _name->setEnabled(false);
    }
    else if (ErrorReporter::error(QtCriticalMsg, this, tr("Error Retrieving Shipping Form Information"),
                                  shippingCheck, __FILE__, __LINE__))
    {
      return;
    }
  }
}

void shippingForm::sSave()
{
  XSqlQuery shippingSave;
  if (_name->text().length() == 0)
  {
    QMessageBox::warning( this, tr("Format Name is Invalid"),
                          tr("You must enter a valid name for this Bill of Lading Format.") );
    _name->setFocus();
    return;
  }

  sCheck();

  if (_report->id() == -1)
  {
    QMessageBox::warning( this, tr("Report Name is Invalid"),
                          tr("You must enter a select report for this Bill of Lading Format.") );
    _report->setFocus();
    return;
  }

  if (_mode == cNew)
  {
    shippingSave.exec("SELECT NEXTVAL('shipform_shipform_id_seq') AS shipform_id;");
    if (shippingSave.first())
      _shipformid = shippingSave.value("shipform_id").toInt();
    else if (ErrorReporter::error(QtCriticalMsg, this, tr("Error Retrieving Shipping Form Information"),
                                  shippingSave, __FILE__, __LINE__))
    {
      return;
    }

    shippingSave.prepare( "INSERT INTO shipform "
               "(shipform_id, shipform_name, shipform_report_name) "
               "VALUES "
               "(:shipform_id, :shipform_name, :shipform_report_name);" );

  }
  if (_mode == cEdit)
    shippingSave.prepare( "UPDATE shipform "
               "SET shipform_name=:shipform_name, shipform_report_name=:shipform_report_name "
               "WHERE (shipform_id=:shipform_id);" );

  shippingSave.bindValue(":shipform_id", _shipformid);
  shippingSave.bindValue(":shipform_name", _name->text());
  shippingSave.bindValue(":shipform_report_name", _report->code());
  shippingSave.exec();
  if (ErrorReporter::error(QtCriticalMsg, this, tr("Error Saving Shipping Form Information"),
                                shippingSave, __FILE__, __LINE__))
  {
    return;
  }

  done(_shipformid);
}

void shippingForm::populate()
{
  XSqlQuery shippingpopulate;
  shippingpopulate.prepare( "SELECT * "
       	     "FROM shipform "
	     "WHERE (shipform_id=:shipform_id);" );
  shippingpopulate.bindValue(":shipform_id", _shipformid);
  shippingpopulate.exec();
  if (shippingpopulate.first())
  {
    _name->setText(shippingpopulate.value("shipform_name").toString());
    _report->setCode(shippingpopulate.value("shipform_report_name").toString());
  }
  else if (ErrorReporter::error(QtCriticalMsg, this, tr("Error Retrieving Shipping Form Information"),
                                shippingpopulate, __FILE__, __LINE__))
  {
    return;
  }
}