campo-sirio/include/currency.cpp

392 lines
9.2 KiB
C++
Raw Normal View History

#include <currency.h>
#include <prefix.h>
#include <recarray.h>
///////////////////////////////////////////////////////////
// DowJones
///////////////////////////////////////////////////////////
class TDowJones : public TFile_cache
{
struct TExchangeData : public TObject
{
real _chg; // Cambio
int _dec; // Decimali per gli importi normali
int _dec_prices; // Decimali per i prezzi unitari
int _dec_change; // Decimali per i cambi
bool _is_euro; // E' l'EURO in persona
bool _contro_euro; // Il cambio e' espresso contro EURO
public:
const TExchangeData& operator=(const TExchangeData& d)
{
_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) { }
};
TString16 _base_val, _firm_val, _euro_val;
real _euro_chg;
protected:
virtual TObject* rec2obj(const TRectype& rec) const;
virtual void flush();
void test_cache();
const TExchangeData& get(const char* key);
public:
const TString& get_base_val();
const TString& get_firm_val();
const TString& get_euro_val();
const char* expand_value(const char* val);
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);
const real& get_change(const char* val);
TDowJones() : TFile_cache("%VAL") { }
virtual ~TDowJones() { }
} DowJones;
TObject* TDowJones::rec2obj(const TRectype& rec) const
{
TExchangeData* data = new TExchangeData;
const TString16 codval = rec.get("CODTAB");
data->_chg = rec.get_real("S4");
if (data->_chg <= ZERO)
{
data->_chg = rec.get_real("R10");
if (data->_chg <= ZERO)
{
NFCHECK("Codice valuta senza cambio: '%s'", (const char*)codval);
data->_chg = 1.0;
}
}
if (codval.not_empty())
{
data->_dec = rec.get_int("I0");
data->_dec_prices = rec.get_int("I1");
if (data->_dec_prices < data->_dec)
data->_dec_prices = data->_dec;
data->_dec_change = rec.get_int("I2");
data->_is_euro = rec.get_bool("B0");
data->_contro_euro = rec.get_bool("B1");
}
return data;
}
void TDowJones::flush()
{
_base_val.cut(0);
_firm_val.cut(0);
_euro_val.cut(0);
}
void TDowJones::test_cache()
{
if (_base_val.empty())
{
fill();
FOR_EACH_ASSOC_OBJECT(_cache, hash, key, obj) if (*key)
{
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;
}
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?
{
TExchangeData* lira = new TExchangeData;
lira->_chg = 1.0; lira->_dec = lira->_dec_prices = 0;
lira->_is_euro = lira->_contro_euro = FALSE;
_base_val = "LIT";
_cache.add(_base_val, lira);
}
_firm_val = prefix().firm().codice_valuta();
_euro_chg = get_change(_euro_val);
}
}
const char* TDowJones::expand_value(const char* val)
{
if (val)
{
if (*val == '_')
{
if (stricmp(val, "_FIRM") == 0)
val = get_firm_val(); else
if (stricmp(val, "_EURO") == 0)
val = get_euro_val(); else
if (stricmp(val, "_BASE") == 0)
val = "";
}
}
else
val = "";
if (*val == '\0')
val = get_base_val();
return val;
}
const TDowJones::TExchangeData& TDowJones::get(const char* val)
{
test_cache();
return (const TExchangeData&)query(expand_value(val));
}
const TString& TDowJones::get_base_val()
{
test_cache();
return _base_val;
}
const TString& TDowJones::get_firm_val()
{
test_cache();
return _firm_val;
}
const TString& TDowJones::get_euro_val()
{
test_cache();
return _euro_val;
}
real TDowJones::exchange(const real& num, // Importo da convertire
const char* frval, // Dalla valuta
const real& frchg, // Dal cambio
const char* toval, // Alla valuta
const real& tochg, // Al cambio
bool price) // e' un prezzo ?
{
real n = num;
if (!n.is_zero())
{
const TExchangeData& datafr = get(frval);
const TExchangeData& datato = get(toval);
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;
}
n.round(price ? datato._dec_prices : datato._dec); // Arrotonda
}
return n;
}
int TDowJones::get_dec(const char* val, bool price)
{
const TExchangeData& data = get(val);
return price ? data._dec_prices : data._dec;
}
const real& TDowJones::get_change(const char* val)
{
const TExchangeData& data = get(val);
return data._chg;
}
///////////////////////////////////////////////////////////
// TCurrency
///////////////////////////////////////////////////////////
const TString& TCurrency::get_base_val()
{
return DowJones.get_base_val();
}
const TString& TCurrency::get_firm_val()
{
return DowJones.get_firm_val();
}
const TString& TCurrency::get_euro_val()
{
return DowJones.get_euro_val();
}
int TCurrency::get_base_dec(bool price)
{
return DowJones.get_dec(NULL, price);
}
int TCurrency::get_firm_dec(bool price)
{
return DowJones.get_dec(get_firm_val(), price);
}
int TCurrency::get_euro_dec(bool price)
{
return DowJones.get_dec(get_euro_val(), price);
}
void TCurrency::force_value(const char* newval, const real& newchange)
{
newval = DowJones.expand_value(newval);
if (newval && *newval)
{
strncpy(_val, newval, 4);
_val[3] = '\0';
_exchange = newchange;
}
else
{
*_val = '\0';
_exchange = ZERO;
}
}
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());
}
int TCurrency::compare(const TSortable& s) const
{
const TCurrency& cur = (const TCurrency&)s;
if (same_value_as(cur))
{
return _num == cur._num ? 0 : (_num > cur._num ? +1 : -1);
}
TCurrency curr(cur);
curr.change_value(_val);
return _num == curr._num ? 0 : (_num > curr._num ? +1 : -1);
}
void TCurrency::copy(const TCurrency& cur)
{
_num = cur._num;
force_value(cur._val, cur._exchange);
}
const char* TCurrency::string(bool dotted) const
{
if (dotted)
{
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;
}
return *this;
}
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)
{
_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;
}
bool TCurrency::is_base_value() const
{
return *_val == '\0' || get_base_val() == _val;
}
bool TCurrency::same_value_as(const TCurrency& cur) const
{
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;
}
TCurrency::TCurrency(const real& num, const char* val, const real& exchg, bool price)
: _num(num), _price(price)
{
force_value(val, exchg);
}