File: format.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 (248 lines) | stat: -rw-r--r-- 8,131 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
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
/*
 * 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 <string.h>

#include <QSqlError>
#include <QVariant>

#include "format.h"
#include "xsqlquery.h"
#include "errorReporter.h"

#define MONEYSCALE            2
#define COSTEXTRASCALE        2
#define WEIGHTSCALE           2
#define QTYSCALE              2
#define QTYPERSCALE           6
#define SALESPRICEEXTRASCALE  2
#define PURCHPRICEEXTRASCALE  2
#define UOMRATIOSCALE         6
#define PERCENTSCALE          2

#define DEBUG                 false

static QColor error("red");
static QColor warning("orange");
static QColor emphasis("blue");
static QColor altemphasis("green");
static QColor expired("red");
static QColor future("blue");

static int    costscale        = MONEYSCALE + COSTEXTRASCALE;
static int    currvalscale     = MONEYSCALE;
static int    extpricescale    = MONEYSCALE;
static int    percentscale     = PERCENTSCALE;
static int    purchpricescale  = MONEYSCALE + PURCHPRICEEXTRASCALE;
static int    qtyscale         = QTYSCALE;
static int    qtyperscale      = QTYPERSCALE;
static int    salespricescale  = MONEYSCALE + SALESPRICEEXTRASCALE;
static int    uomratioscale    = UOMRATIOSCALE;
static int    weightscale      = WEIGHTSCALE;

static bool   loadedLocales    = false;

static bool   loadLocale()
{
  if (!loadedLocales)
  {
    QString user;
    XSqlQuery userq("SELECT getEffectiveXtUser() AS user;");
    if (userq.lastError().type() != QSqlError::NoError)
      userq.exec("SELECT CURRENT_USER AS user;");
    if (userq.first())
      user = userq.value("user").toString();
    else if (ErrorReporter::error(QtCriticalMsg, 0,
                                  QObject::tr("Error Getting User"),
                                  userq, __FILE__, __LINE__))
    {
      return false;
    }

    XSqlQuery localeq;
    localeq.prepare("SELECT locale.*"
                    "  FROM usr"
                    "  JOIN locale ON (usr_locale_id=locale_id)"
                    " WHERE (usr_username=:user);");
    localeq.bindValue(":user", user);
    localeq.exec();
    if (localeq.first())
    {
      if (!localeq.value("locale_error_color").toString().isEmpty())
        error = QColor(localeq.value("locale_error_color").toString());
      if (!localeq.value("locale_warning_color").toString().isEmpty())
        warning = QColor(localeq.value("locale_warning_color").toString());
      if (!localeq.value("locale_emphasis_color").toString().isEmpty())
        emphasis = QColor(localeq.value("locale_emphasis_color").toString());
      if (!localeq.value("locale_altemphasis_color").toString().isEmpty())
        altemphasis = QColor(localeq.value("locale_altemphasis_color").toString());
      if (!localeq.value("locale_expired_color").toString().isEmpty())
        expired = QColor(localeq.value("locale_expired_color").toString());
      if (!localeq.value("locale_future_color").toString().isEmpty())
        future = QColor(localeq.value("locale_future_color").toString());

      if (!localeq.value("locale_cost_scale").toString().isEmpty())
        costscale = localeq.value("locale_cost_scale").toInt();
      if (!localeq.value("locale_curr_scale").toString().isEmpty())
        currvalscale = localeq.value("locale_curr_scale").toInt();
      if (!localeq.value("locale_extprice_scale").toString().isEmpty())
        extpricescale = localeq.value("locale_extprice_scale").toInt();
      if (!localeq.value("locale_percent_scale").toString().isEmpty())
        percentscale = localeq.value("locale_percent_scale").toInt();
      if (!localeq.value("locale_purchprice_scale").toString().isEmpty())
        purchpricescale = localeq.value("locale_purchprice_scale").toInt();
      if (!localeq.value("locale_qty_scale").toString().isEmpty())
        qtyscale = localeq.value("locale_qty_scale").toInt();
      if (!localeq.value("locale_qtyper_scale").toString().isEmpty())
        qtyperscale = localeq.value("locale_qtyper_scale").toInt();
      if (!localeq.value("locale_salesprice_scale").toString().isEmpty())
        salespricescale = localeq.value("locale_salesprice_scale").toInt();
      if (!localeq.value("locale_uomratio_scale").toString().isEmpty())
        uomratioscale = localeq.value("locale_uomratio_scale").toInt();
      if (!localeq.value("locale_weight_scale").toString().isEmpty())
        weightscale = localeq.value("locale_weight_scale").toInt();

      // TODO: add locale_percent_scale
    }
    else if (ErrorReporter::error(QtCriticalMsg, 0,
                                  QObject::tr("Error Getting Locale"),
                                  userq, __FILE__, __LINE__))
    {
      return false;
    }
    loadedLocales = true;
  }
  return true;
}

int decimalPlaces(QString pName)
{
  int returnVal = MONEYSCALE;
  if (!loadedLocales)
    loadLocale();

  QByteArray  tba  = pName.toLatin1();
  char        *ptr = tba.data();
  // the following order is based on the relative frequencies of usage
  // for xtnumericrole in the .cpp and .mql files except for the final
  // else, which ranked 7th overall
  if (strcmp(ptr, "qty") == 0)
    returnVal = qtyscale;
  else if (strncmp(ptr, "curr", 4) == 0)
    returnVal = currvalscale;   // TODO: change this to currency-specific value?
  else if (strcmp(ptr, "percent") == 0)
    returnVal = percentscale;
  else if (strcmp(ptr, "cost") == 0)
    returnVal = costscale;
  else if (strcmp(ptr, "qtyper") == 0)
    returnVal = qtyperscale;
  else if (strcmp(ptr, "salesprice") == 0)
    returnVal = salespricescale;
  else if (strcmp(ptr, "purchprice") == 0)
    returnVal = purchpricescale;
  else if (strcmp(ptr, "uomratio") == 0)
    returnVal = uomratioscale;
  else if (strcmp(ptr, "extprice") == 0)
    returnVal = extpricescale;
  else if (strcmp(ptr, "weight") == 0)
    returnVal = weightscale;
  else
  {
    bool ok = false;
    returnVal = pName.toInt(&ok);
    if (!ok)
      returnVal = MONEYSCALE;
  }

  return returnVal;
}

QColor namedColor(QString pName)
{
  (void)loadLocale();

  if (pName == "error")
    return error;
  else if (pName == "warning")
    return warning;
  else if (pName == "emphasis")
    return emphasis;
  else if (pName == "altemphasis")
    return altemphasis;
  else if (pName == "expired")
    return expired;
  else if (pName == "future")
    return future;

  return QColor(pName);
}

QString formatNumber(double value, int decimals)
{
  if (DEBUG)
    qDebug("formatNumber(%f, %d)", value, decimals);
  return QLocale().toString(value, 'f', decimals);
}

/*
  different currencies have different rounding conventions, so we need
  the currency id to find the right rounding rules.
  we need extra decimal places for some data because some monetary values,
  like unit costs, are stored with extra precision.
*/
QString formatMoney(double value, int /* curr_id */, int extraDecimals)
{
  return QLocale().toString(value, 'f', currvalscale + extraDecimals);
}

QString formatCost(double value, int /* curr_id */)
{
  return formatNumber(value, costscale);
}

QString formatExtPrice(double value, int /* curr_id */)
{
  return formatNumber(value, extpricescale);
}

QString formatWeight(double value)
{
  return formatNumber(value, weightscale);
}

QString formatQty(double value)
{
  return formatNumber(value, qtyscale);
}

QString formatQtyPer(double value)
{
  return formatNumber(value, qtyperscale);
}

QString formatSalesPrice(double value, int /* curr_id */)
{
  return formatNumber(value, salespricescale);
}

QString formatPurchPrice(double value, int /* curr_id */)
{
  return formatNumber(value, purchpricescale);
}

QString formatUOMRatio(double value)
{
  return formatNumber(value, uomratioscale);
}

QString formatPercent(double value)
{
  return formatNumber(value * 100.0, percentscale);
}