1998-05-08 12:19:34 +00:00
|
|
|
#include <currency.h>
|
1999-01-19 09:15:17 +00:00
|
|
|
#include <prefix.h>
|
1998-05-08 12:19:34 +00:00
|
|
|
#include <recarray.h>
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////
|
|
|
|
// DowJones
|
|
|
|
///////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class TDowJones : public TFile_cache
|
|
|
|
{
|
|
|
|
struct TExchangeData : public TObject
|
|
|
|
{
|
1999-01-19 09:15:17 +00:00
|
|
|
real _chg; // Cambio
|
|
|
|
int _dec; // Decimali per gli importi normali
|
|
|
|
int _dec_prices; // Decimali per i prezzi unitari
|
1999-02-02 08:43:43 +00:00
|
|
|
int _dec_change; // Decimali per i cambi
|
1999-01-19 09:15:17 +00:00
|
|
|
bool _is_euro; // E' l'EURO in persona
|
|
|
|
bool _contro_euro; // Il cambio e' espresso contro EURO
|
1998-05-08 12:19:34 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
const TExchangeData& operator=(const TExchangeData& d)
|
1999-01-19 09:15:17 +00:00
|
|
|
{
|
|
|
|
_chg = d._chg; _dec = d._dec; _dec_prices = d._dec_prices;
|
|
|
|
_is_euro = d._is_euro; _contro_euro = d._contro_euro;
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
TExchangeData()
|
|
|
|
: _chg(1.0), _dec(0), _dec_prices(0), _is_euro(FALSE), _contro_euro(FALSE) { }
|
1998-05-08 12:19:34 +00:00
|
|
|
};
|
|
|
|
|
1999-01-19 09:15:17 +00:00
|
|
|
TString16 _base_val, _firm_val, _euro_val;
|
|
|
|
real _euro_chg;
|
1998-05-08 12:19:34 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual TObject* rec2obj(const TRectype& rec) const;
|
1998-11-03 10:27:35 +00:00
|
|
|
virtual void flush();
|
1998-05-08 12:19:34 +00:00
|
|
|
|
|
|
|
void test_cache();
|
|
|
|
const TExchangeData& get(const char* key);
|
|
|
|
|
|
|
|
public:
|
1998-11-03 10:27:35 +00:00
|
|
|
const TString& get_base_val();
|
|
|
|
const TString& get_firm_val();
|
1999-01-19 09:15:17 +00:00
|
|
|
const TString& get_euro_val();
|
1998-11-03 10:27:35 +00:00
|
|
|
const char* expand_value(const char* val);
|
1998-05-08 12:19:34 +00:00
|
|
|
|
1998-11-03 10:27:35 +00:00
|
|
|
real exchange(const real& num,
|
|
|
|
const char* fromval, const real& fromchg,
|
|
|
|
const char* toval, const real& tochg,
|
|
|
|
bool price = FALSE);
|
|
|
|
int get_dec(const char* val, bool prices = FALSE);
|
1999-01-19 09:15:17 +00:00
|
|
|
const real& get_change(const char* val);
|
1998-05-08 12:19:34 +00:00
|
|
|
|
|
|
|
TDowJones() : TFile_cache("%VAL") { }
|
|
|
|
virtual ~TDowJones() { }
|
|
|
|
} DowJones;
|
|
|
|
|
|
|
|
TObject* TDowJones::rec2obj(const TRectype& rec) const
|
|
|
|
{
|
|
|
|
TExchangeData* data = new TExchangeData;
|
1999-01-19 09:15:17 +00:00
|
|
|
|
|
|
|
const TString16 codval = rec.get("CODTAB");
|
|
|
|
data->_chg = rec.get_real("S4");
|
|
|
|
if (data->_chg <= ZERO)
|
1998-05-08 12:19:34 +00:00
|
|
|
{
|
1999-01-19 09:15:17 +00:00
|
|
|
data->_chg = rec.get_real("R10");
|
|
|
|
if (data->_chg <= ZERO)
|
|
|
|
{
|
1999-05-24 13:34:11 +00:00
|
|
|
NFCHECK("Codice valuta senza cambio: '%s'", (const char*)codval);
|
1999-01-19 09:15:17 +00:00
|
|
|
data->_chg = 1.0;
|
|
|
|
}
|
1998-05-08 12:19:34 +00:00
|
|
|
}
|
1998-11-03 10:27:35 +00:00
|
|
|
|
1999-01-19 09:15:17 +00:00
|
|
|
if (codval.not_empty())
|
1998-05-08 12:19:34 +00:00
|
|
|
{
|
1999-01-19 09:15:17 +00:00
|
|
|
data->_dec = rec.get_int("I0");
|
|
|
|
data->_dec_prices = rec.get_int("I1");
|
|
|
|
if (data->_dec_prices < data->_dec)
|
|
|
|
data->_dec_prices = data->_dec;
|
1999-02-02 08:43:43 +00:00
|
|
|
data->_dec_change = rec.get_int("I2");
|
1999-01-19 09:15:17 +00:00
|
|
|
data->_is_euro = rec.get_bool("B0");
|
|
|
|
data->_contro_euro = rec.get_bool("B1");
|
1998-05-08 12:19:34 +00:00
|
|
|
}
|
1999-01-19 09:15:17 +00:00
|
|
|
|
1998-05-08 12:19:34 +00:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
1998-11-03 10:27:35 +00:00
|
|
|
void TDowJones::flush()
|
|
|
|
{
|
|
|
|
_base_val.cut(0);
|
|
|
|
_firm_val.cut(0);
|
1999-01-19 09:15:17 +00:00
|
|
|
_euro_val.cut(0);
|
1998-11-03 10:27:35 +00:00
|
|
|
}
|
|
|
|
|
1998-05-08 12:19:34 +00:00
|
|
|
void TDowJones::test_cache()
|
|
|
|
{
|
1999-01-19 09:15:17 +00:00
|
|
|
if (_base_val.empty())
|
1998-05-08 12:19:34 +00:00
|
|
|
{
|
|
|
|
fill();
|
1999-01-19 09:15:17 +00:00
|
|
|
FOR_EACH_ASSOC_OBJECT(_cache, hash, key, obj) if (*key)
|
1998-05-08 12:19:34 +00:00
|
|
|
{
|
1999-01-19 09:15:17 +00:00
|
|
|
const TExchangeData& data = *(const TExchangeData*)obj;
|
|
|
|
if (_base_val.empty() && data._chg == 1.0)
|
|
|
|
_base_val = key; else
|
|
|
|
if (_euro_val.empty() && data._is_euro)
|
|
|
|
_euro_val = key;
|
1998-05-08 12:19:34 +00:00
|
|
|
}
|
1999-01-19 09:15:17 +00:00
|
|
|
|
|
|
|
if (_euro_val.empty()) // Si son dimenticati dell'EURO?
|
|
|
|
{
|
|
|
|
TExchangeData* euro = new TExchangeData;
|
|
|
|
euro->_chg = 1936.27; euro->_dec = euro->_dec_prices = 2;
|
|
|
|
euro->_is_euro = TRUE; euro->_contro_euro = FALSE;
|
|
|
|
_euro_val = "EUR";
|
|
|
|
_cache.add(_euro_val, euro);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_base_val.empty()) // Si son dimenticati delle LIRE?
|
1998-05-08 12:19:34 +00:00
|
|
|
{
|
1999-01-19 09:15:17 +00:00
|
|
|
TExchangeData* lira = new TExchangeData;
|
|
|
|
lira->_chg = 1.0; lira->_dec = lira->_dec_prices = 0;
|
|
|
|
lira->_is_euro = lira->_contro_euro = FALSE;
|
1998-11-03 10:27:35 +00:00
|
|
|
_base_val = "LIT";
|
1999-01-19 09:15:17 +00:00
|
|
|
_cache.add(_base_val, lira);
|
|
|
|
}
|
|
|
|
_firm_val = prefix().firm().codice_valuta();
|
1999-03-22 15:55:50 +00:00
|
|
|
_euro_chg = get_change(_euro_val);
|
1998-05-08 12:19:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-11-03 10:27:35 +00:00
|
|
|
const char* TDowJones::expand_value(const char* val)
|
|
|
|
{
|
|
|
|
if (val)
|
|
|
|
{
|
|
|
|
if (*val == '_')
|
|
|
|
{
|
|
|
|
if (stricmp(val, "_FIRM") == 0)
|
|
|
|
val = get_firm_val(); else
|
1999-01-19 09:15:17 +00:00
|
|
|
if (stricmp(val, "_EURO") == 0)
|
|
|
|
val = get_euro_val(); else
|
1998-11-03 10:27:35 +00:00
|
|
|
if (stricmp(val, "_BASE") == 0)
|
|
|
|
val = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
val = "";
|
|
|
|
if (*val == '\0')
|
|
|
|
val = get_base_val();
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
1998-05-08 12:19:34 +00:00
|
|
|
const TDowJones::TExchangeData& TDowJones::get(const char* val)
|
|
|
|
{
|
|
|
|
test_cache();
|
1998-11-03 10:27:35 +00:00
|
|
|
return (const TExchangeData&)query(expand_value(val));
|
1998-05-08 12:19:34 +00:00
|
|
|
}
|
|
|
|
|
1998-11-03 10:27:35 +00:00
|
|
|
const TString& TDowJones::get_base_val()
|
1998-05-08 12:19:34 +00:00
|
|
|
{
|
|
|
|
test_cache();
|
1998-11-03 10:27:35 +00:00
|
|
|
return _base_val;
|
1998-05-08 12:19:34 +00:00
|
|
|
}
|
|
|
|
|
1998-11-03 10:27:35 +00:00
|
|
|
const TString& TDowJones::get_firm_val()
|
|
|
|
{
|
|
|
|
test_cache();
|
|
|
|
return _firm_val;
|
|
|
|
}
|
|
|
|
|
1999-01-19 09:15:17 +00:00
|
|
|
const TString& TDowJones::get_euro_val()
|
|
|
|
{
|
|
|
|
test_cache();
|
|
|
|
return _euro_val;
|
|
|
|
}
|
1998-11-03 10:27:35 +00:00
|
|
|
|
1998-05-08 12:19:34 +00:00
|
|
|
real TDowJones::exchange(const real& num, // Importo da convertire
|
1999-01-19 09:15:17 +00:00
|
|
|
const char* frval, // Dalla valuta
|
|
|
|
const real& frchg, // Dal cambio
|
1998-11-03 10:27:35 +00:00
|
|
|
const char* toval, // Alla valuta
|
|
|
|
const real& tochg, // Al cambio
|
|
|
|
bool price) // e' un prezzo ?
|
1998-05-08 12:19:34 +00:00
|
|
|
{
|
|
|
|
real n = num;
|
1999-01-19 09:15:17 +00:00
|
|
|
if (!n.is_zero())
|
1998-05-08 12:19:34 +00:00
|
|
|
{
|
1999-01-19 09:15:17 +00:00
|
|
|
const TExchangeData& datafr = get(frval);
|
1998-05-08 12:19:34 +00:00
|
|
|
const TExchangeData& datato = get(toval);
|
1999-01-19 09:15:17 +00:00
|
|
|
const real fr = frchg <= ZERO ? datafr._chg : frchg;
|
|
|
|
const real to = tochg <= ZERO ? datato._chg : tochg;
|
|
|
|
const int mode = (datafr._contro_euro ? 1 : 0)+(datato._contro_euro ? 2 : 0);
|
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
n = n * _euro_chg / (fr * to); // Modo misto 1
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
n = n * (fr * to) / _euro_chg; // Modo misto 2
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
n = n * to / fr; // Nuovo modo
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
n = n * fr / to; // Vecchio modo
|
|
|
|
break;
|
1998-11-03 10:27:35 +00:00
|
|
|
}
|
1999-01-19 09:15:17 +00:00
|
|
|
n.round(price ? datato._dec_prices : datato._dec); // Arrotonda
|
1998-05-08 12:19:34 +00:00
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
1998-11-03 10:27:35 +00:00
|
|
|
int TDowJones::get_dec(const char* val, bool price)
|
1998-05-08 12:19:34 +00:00
|
|
|
{
|
|
|
|
const TExchangeData& data = get(val);
|
1998-11-03 10:27:35 +00:00
|
|
|
return price ? data._dec_prices : data._dec;
|
1998-05-08 12:19:34 +00:00
|
|
|
}
|
|
|
|
|
1999-01-19 09:15:17 +00:00
|
|
|
const real& TDowJones::get_change(const char* val)
|
|
|
|
{
|
|
|
|
const TExchangeData& data = get(val);
|
|
|
|
return data._chg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-05-08 12:19:34 +00:00
|
|
|
///////////////////////////////////////////////////////////
|
|
|
|
// TCurrency
|
|
|
|
///////////////////////////////////////////////////////////
|
|
|
|
|
1998-11-03 10:27:35 +00:00
|
|
|
const TString& TCurrency::get_base_val()
|
1998-10-01 13:52:27 +00:00
|
|
|
{
|
1998-11-03 10:27:35 +00:00
|
|
|
return DowJones.get_base_val();
|
|
|
|
}
|
|
|
|
|
|
|
|
const TString& TCurrency::get_firm_val()
|
|
|
|
{
|
|
|
|
return DowJones.get_firm_val();
|
1998-10-01 13:52:27 +00:00
|
|
|
}
|
|
|
|
|
1999-05-24 13:34:11 +00:00
|
|
|
const TString& TCurrency::get_euro_val()
|
|
|
|
{
|
|
|
|
return DowJones.get_euro_val();
|
|
|
|
}
|
|
|
|
|
1998-11-03 10:27:35 +00:00
|
|
|
int TCurrency::get_base_dec(bool price)
|
1998-05-08 12:19:34 +00:00
|
|
|
{
|
1998-11-03 10:27:35 +00:00
|
|
|
return DowJones.get_dec(NULL, price);
|
1998-05-08 12:19:34 +00:00
|
|
|
}
|
|
|
|
|
1998-11-03 10:27:35 +00:00
|
|
|
int TCurrency::get_firm_dec(bool price)
|
1998-05-08 12:19:34 +00:00
|
|
|
{
|
1999-01-19 09:15:17 +00:00
|
|
|
return DowJones.get_dec(get_firm_val(), price);
|
1998-05-08 12:19:34 +00:00
|
|
|
}
|
|
|
|
|
1999-05-24 13:34:11 +00:00
|
|
|
int TCurrency::get_euro_dec(bool price)
|
|
|
|
{
|
|
|
|
return DowJones.get_dec(get_euro_val(), price);
|
|
|
|
}
|
|
|
|
|
1998-11-03 10:27:35 +00:00
|
|
|
void TCurrency::force_value(const char* newval, const real& newchange)
|
1998-05-08 12:19:34 +00:00
|
|
|
{
|
1998-11-03 10:27:35 +00:00
|
|
|
newval = DowJones.expand_value(newval);
|
|
|
|
if (newval && *newval)
|
1998-05-08 12:19:34 +00:00
|
|
|
{
|
1998-11-03 10:27:35 +00:00
|
|
|
strncpy(_val, newval, 4);
|
|
|
|
_val[3] = '\0';
|
|
|
|
_exchange = newchange;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*_val = '\0';
|
|
|
|
_exchange = ZERO;
|
1998-05-08 12:19:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-11-03 10:27:35 +00:00
|
|
|
void TCurrency::change_value(const char* val, const real& newchange)
|
|
|
|
{
|
|
|
|
val = DowJones.expand_value(val);
|
|
|
|
if (!_num.is_zero() && stricmp(_val, val) != 0)
|
|
|
|
{
|
|
|
|
_num = DowJones.exchange(_num, _val, _exchange, val, newchange, is_price());
|
|
|
|
}
|
|
|
|
force_value(val, newchange);
|
|
|
|
}
|
|
|
|
|
|
|
|
int TCurrency::decimals() const
|
|
|
|
{
|
|
|
|
return DowJones.get_dec(_val, is_price());
|
|
|
|
}
|
1998-05-08 12:19:34 +00:00
|
|
|
|
|
|
|
int TCurrency::compare(const TSortable& s) const
|
|
|
|
{
|
|
|
|
const TCurrency& cur = (const TCurrency&)s;
|
1998-11-03 10:27:35 +00:00
|
|
|
if (same_value_as(cur))
|
1998-05-08 12:19:34 +00:00
|
|
|
{
|
|
|
|
return _num == cur._num ? 0 : (_num > cur._num ? +1 : -1);
|
|
|
|
}
|
|
|
|
TCurrency curr(cur);
|
1998-11-03 10:27:35 +00:00
|
|
|
curr.change_value(_val);
|
1998-05-08 12:19:34 +00:00
|
|
|
return _num == curr._num ? 0 : (_num > curr._num ? +1 : -1);
|
|
|
|
}
|
|
|
|
|
1998-11-03 10:27:35 +00:00
|
|
|
void TCurrency::copy(const TCurrency& cur)
|
1998-05-08 12:19:34 +00:00
|
|
|
{
|
|
|
|
_num = cur._num;
|
1998-11-03 10:27:35 +00:00
|
|
|
force_value(cur._val, cur._exchange);
|
1998-05-08 12:19:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* TCurrency::string(bool dotted) const
|
|
|
|
{
|
|
|
|
if (dotted)
|
|
|
|
{
|
1998-11-03 10:27:35 +00:00
|
|
|
TString16 picture;
|
|
|
|
picture.format(".%d", decimals());
|
|
|
|
return _num.string(picture);
|
|
|
|
}
|
|
|
|
return _num.string(0, decimals());
|
|
|
|
}
|
|
|
|
|
|
|
|
TCurrency& TCurrency::operator+=(const TCurrency& cur)
|
|
|
|
{
|
|
|
|
CHECK(is_price() == cur.is_price(), "Somma di pere e mele!");
|
|
|
|
if (same_value_as(cur))
|
|
|
|
{
|
|
|
|
_num += cur._num;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
real n = DowJones.exchange(cur._num, cur._val, cur._exchange,
|
|
|
|
_val, _exchange, is_price());
|
|
|
|
_num += n;
|
1998-05-08 12:19:34 +00:00
|
|
|
}
|
1998-11-03 10:27:35 +00:00
|
|
|
return *this;
|
1998-05-08 12:19:34 +00:00
|
|
|
}
|
|
|
|
|
1998-11-03 10:27:35 +00:00
|
|
|
TCurrency TCurrency::operator+(const TCurrency& num) const
|
|
|
|
{
|
|
|
|
TCurrency cur(*this);
|
|
|
|
cur += num;
|
|
|
|
return cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
TCurrency& TCurrency::operator-=(const TCurrency& cur)
|
|
|
|
{
|
|
|
|
CHECK(is_price() == cur.is_price(), "Sottrazione di pere e mele!");
|
|
|
|
if (same_value_as(cur))
|
|
|
|
_num -= cur._num;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
real n = DowJones.exchange(cur._num, cur._val, cur._exchange,
|
|
|
|
_val, _exchange, is_price());
|
|
|
|
_num -= n;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
TCurrency& TCurrency::operator*=(const real& num)
|
1998-10-01 13:52:27 +00:00
|
|
|
{
|
1998-11-03 10:27:35 +00:00
|
|
|
_num *= num;
|
|
|
|
// Arrotondo forzando price a FALSE
|
|
|
|
_num.round(DowJones.get_dec(_val, _price = FALSE));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
TCurrency TCurrency::operator*(const real& num) const
|
|
|
|
{
|
|
|
|
TCurrency cur(*this);
|
|
|
|
cur *= num;
|
|
|
|
return cur;
|
1998-10-01 13:52:27 +00:00
|
|
|
}
|
|
|
|
|
1998-11-03 10:27:35 +00:00
|
|
|
bool TCurrency::is_base_value() const
|
|
|
|
{
|
|
|
|
return *_val == '\0' || get_base_val() == _val;
|
|
|
|
}
|
1998-10-01 13:52:27 +00:00
|
|
|
|
1998-11-03 10:27:35 +00:00
|
|
|
bool TCurrency::same_value_as(const TCurrency& cur) const
|
1998-05-08 12:19:34 +00:00
|
|
|
{
|
1998-11-03 10:27:35 +00:00
|
|
|
const int cmp = stricmp(_val, cur._val);
|
|
|
|
if (cmp == 0)
|
|
|
|
return TRUE;
|
|
|
|
if (*_val == '\0' || *cur._val == '\0')
|
|
|
|
return is_base_value() == cur.is_base_value();
|
|
|
|
return FALSE;
|
1998-05-08 12:19:34 +00:00
|
|
|
}
|
|
|
|
|
1998-11-03 10:27:35 +00:00
|
|
|
TCurrency::TCurrency(const real& num, const char* val, const real& exchg, bool price)
|
|
|
|
: _num(num), _price(price)
|
1998-05-08 12:19:34 +00:00
|
|
|
{
|
1998-11-03 10:27:35 +00:00
|
|
|
force_value(val, exchg);
|
1998-05-08 12:19:34 +00:00
|
|
|
}
|
|
|
|
|