591 lines
14 KiB
C++
Executable File
591 lines
14 KiB
C++
Executable File
#include "mglib02.h"
|
|
|
|
bool TArticolo::load_um()
|
|
{
|
|
if (_um)
|
|
return TRUE;
|
|
|
|
const char * codice = (const char *) _codice;
|
|
|
|
if (!*codice)
|
|
return FALSE;
|
|
|
|
TRectype rec(LF_UMART);
|
|
|
|
_um == new TRecord_array(LF_UMART, UMART_NRIGA);
|
|
rec.put(UMART_CODART, codice);
|
|
_um->destroy_rows();
|
|
_um->read(rec);
|
|
return TRUE;
|
|
}
|
|
|
|
bool TArticolo::load_codcorr()
|
|
{
|
|
if (_codcorr)
|
|
return TRUE;
|
|
|
|
const char * codice = (const char *) _codice;
|
|
|
|
if (!*codice)
|
|
return FALSE;
|
|
TRectype rec(LF_CODCORR);
|
|
|
|
_codcorr == new TRecord_array(LF_CODCORR, CODCORR_NRIGA);
|
|
rec.put(CODCORR_CODART, codice);
|
|
_codcorr->destroy_rows();
|
|
_codcorr->read(rec);
|
|
return TRUE;
|
|
}
|
|
|
|
bool TArticolo::load_deslin()
|
|
{
|
|
if (_deslin)
|
|
return TRUE;
|
|
|
|
const char * codice = (const char *) _codice;
|
|
|
|
if (* codice)
|
|
return FALSE;
|
|
|
|
TRectype rec(LF_DESLIN);
|
|
|
|
_deslin == new TRecord_array(LF_DESLIN, DESLIN_NRIGA);
|
|
rec.put(DESLIN_CODART, codice);
|
|
_deslin->destroy_rows();
|
|
_deslin->read(rec);
|
|
return TRUE;
|
|
}
|
|
|
|
|
|
HIDDEN TString16 _mg_null_str;
|
|
|
|
const TString & TArticolo::get_str(const char* fieldname) const
|
|
{
|
|
if (*fieldname != '#')
|
|
return TRectype::get_str(fieldname);
|
|
char * fname = (char *) fieldname + 1;
|
|
const int logicnum = atoi(fname);
|
|
const char * op1 = strchr(fname, '_');
|
|
int index = 0;
|
|
if (op1 && *op1 == '#')
|
|
index = atoi(fname + 1);
|
|
const char* op2 = strstr(fname, "->");
|
|
CHECKS(op2, "Can't find '->' in string ", fieldname);
|
|
|
|
switch (logicnum)
|
|
{
|
|
case LF_UMART:
|
|
{
|
|
if (((TArticolo *) this)->load_um())
|
|
{
|
|
if (index == 0)
|
|
index = find_um(op1);
|
|
if (index > 0 && op2)
|
|
return _um->row(index).get(op2);
|
|
}
|
|
}
|
|
break;
|
|
case LF_CODCORR:
|
|
{
|
|
if (((TArticolo *) this)->load_codcorr())
|
|
{
|
|
if (index == 0)
|
|
index = find_codcorr(op1);
|
|
if (index > 0 && op2)
|
|
return _codcorr->row(index).get(op2);
|
|
}
|
|
}
|
|
break;
|
|
case LF_DESLIN:
|
|
{
|
|
if (((TArticolo *) this)->load_deslin())
|
|
{
|
|
if (index == 0)
|
|
index = find_deslin(op1);
|
|
if (index > 0 && op2)
|
|
return _deslin->row(index).get(op2);
|
|
}
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
return _mg_null_str;
|
|
}
|
|
|
|
int TArticolo::find(const TRecord_array & array, const char * fieldname, const char * s,
|
|
int from) const
|
|
{
|
|
const int last = array.last_row();
|
|
if (from > last)
|
|
{
|
|
const int len = s ? strlen(s) : 0;
|
|
if (len == 0)
|
|
return from + 1;
|
|
for (int i = array.succ_row(from); i <= last; i = _um->succ_row(i))
|
|
if (_um->row(i).get(UMART_UM) == s)
|
|
return i;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int TArticolo::read(const char * cod)
|
|
{
|
|
if (strcmp(cod, (const char *) _codice) == 0)
|
|
return NOERR;
|
|
if (_um)
|
|
{
|
|
delete _um;
|
|
_um = NULL;
|
|
}
|
|
if (_codcorr)
|
|
{
|
|
delete _codcorr;
|
|
_codcorr = NULL;
|
|
}
|
|
if (_deslin)
|
|
{
|
|
delete _deslin;
|
|
_deslin == NULL;
|
|
}
|
|
|
|
TLocalisamfile anamag(LF_ANAMAG);
|
|
|
|
*_codice = cod;
|
|
const int err = TRectype::read(anamag);
|
|
if (err != NOERR)
|
|
zero();
|
|
return err;
|
|
}
|
|
|
|
const TString & TArticolo::descrizione(const char* lingua) const
|
|
{
|
|
if (lingua && *lingua)
|
|
{
|
|
TString16 f; f.format("#%d_%s->%s", LF_DESLIN, lingua, DESLIN_DESCR);
|
|
|
|
return get(f);
|
|
}
|
|
else
|
|
return get(ANAMAG_DESCR);
|
|
}
|
|
|
|
TArticolo::TArticolo(const char* codice)
|
|
: TRectype(LF_ANAMAG), _um(NULL), _codcorr(NULL),
|
|
_deslin(NULL)
|
|
|
|
{
|
|
_codice = new TRecfield(*this, ANAMAG_CODART);
|
|
if (codice && *codice)
|
|
read(codice);
|
|
}
|
|
|
|
TArticolo::TArticolo(const TRectype& rec)
|
|
: TRectype(rec), _um(NULL), _codcorr(NULL),
|
|
_deslin(NULL)
|
|
{
|
|
_codice = new TRecfield(*this, ANAMAG_CODART);
|
|
}
|
|
|
|
TArticolo::~TArticolo()
|
|
{
|
|
delete _codice;
|
|
if (_um)
|
|
delete _um;
|
|
if (_codcorr)
|
|
delete _codcorr;
|
|
if (_deslin)
|
|
delete _deslin;
|
|
}
|
|
|
|
int TArticolo_giacenza::read(const char * cod)
|
|
{
|
|
if (strcmp(cod, codice()) == 0)
|
|
return NOERR;
|
|
if (_mag)
|
|
{
|
|
delete _mag;
|
|
_mag = NULL;
|
|
}
|
|
if (_storico)
|
|
{
|
|
delete _storico;
|
|
_storico = NULL;
|
|
}
|
|
return TArticolo::read(cod);
|
|
}
|
|
|
|
|
|
bool TArticolo_giacenza::load_mag(const char * annoes)
|
|
{
|
|
if (_mag)
|
|
{
|
|
if (_mag->key().get(MAG_ANNOES) == annoes)
|
|
return TRUE;
|
|
delete _mag;
|
|
}
|
|
|
|
const char * cod = (const char *) codice();
|
|
|
|
if (!*cod)
|
|
return FALSE;
|
|
|
|
TRectype rec(LF_MAG);
|
|
|
|
rec.put(MAG_ANNOES, annoes);
|
|
rec.put(MAG_CODART, cod);
|
|
_mag == new TRecord_array(rec, MAG_NRIGA);
|
|
return TRUE;
|
|
}
|
|
|
|
bool TArticolo_giacenza::load_storico(const char * annoesrif)
|
|
{
|
|
if (_storico)
|
|
{
|
|
if (_storico->key().get(STOMAG_ANNOESRIF) == annoesrif)
|
|
return TRUE;
|
|
delete _storico;
|
|
}
|
|
|
|
const char * cod = (const char *) codice();
|
|
|
|
if (!*cod)
|
|
return FALSE;
|
|
TRectype rec(LF_STOMAG);
|
|
|
|
rec.put(STOMAG_CODART, cod);
|
|
rec.put(STOMAG_ANNOESRIF, annoesrif);
|
|
_mag == new TRecord_array(rec, STOMAG_NRIGA);
|
|
return TRUE;
|
|
}
|
|
|
|
const TString & TArticolo_giacenza::get_str(const char* fieldname) const
|
|
{
|
|
if (*fieldname != '#')
|
|
return TRectype::get_str(fieldname);
|
|
char * fname = (char *) fieldname + 1;
|
|
const int logicnum = atoi(fname);
|
|
const char * op1 = strchr(fname, '_');
|
|
int index = 0;
|
|
if (op1 && *op1 == '#')
|
|
index = atoi(fname + 1);
|
|
fname = (char *) op1 + 1;
|
|
const char * op2 = strchr(fname, '_');
|
|
fname = (char *) op2 + 1;
|
|
const char * op3 = strchr(fname, '_');
|
|
const char * op4 = strstr(fname, "->");
|
|
CHECKS(op4, "Can't find '->' in string ", fieldname);
|
|
|
|
switch (logicnum)
|
|
{
|
|
case LF_MAG:
|
|
if (((TArticolo_giacenza *) this)->load_mag(op1))
|
|
{
|
|
if (index == 0)
|
|
index = find_mag(op1, op2, op3);
|
|
if (index > 0 && op3)
|
|
return _mag->row(index).get(op4);
|
|
}
|
|
break;
|
|
case LF_STOMAG:
|
|
if (((TArticolo_giacenza *) this)->load_storico(op1))
|
|
{
|
|
if (index = 0)
|
|
index = find_storico(op1, op2);
|
|
if (index > 0)
|
|
return _storico->row(index).get(op4);
|
|
}
|
|
break;
|
|
default:
|
|
return TArticolo::get_str(fieldname);
|
|
break;
|
|
}
|
|
return _mg_null_str;
|
|
}
|
|
|
|
int TArticolo_giacenza::find_mag(const char * annoes, const char * codmag, const char * livello, int from) const
|
|
{
|
|
((TArticolo_giacenza *) this)->load_mag(annoes);
|
|
const int last = _mag->last_row();
|
|
|
|
if (from <= last)
|
|
{
|
|
const int codmag_len = codmag ? strlen(codmag) : 0;
|
|
const int livello_len = livello ? strlen(livello) : 0;
|
|
|
|
if (codmag_len == 0 && livello_len == 0)
|
|
return from + 1;
|
|
|
|
for (int i = _mag->succ_row(from); i <= last; i = _mag->succ_row(i))
|
|
{
|
|
const TRectype & rec = _mag->row(i);
|
|
|
|
if (codmag_len == 0 || rec.get(MAG_CODMAG).compare(codmag, codmag_len) == 0)
|
|
if (livello_len == 0 || rec.get(MAG_LIVELLO).compare(livello, livello_len) == 0)
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
real TArticolo_giacenza::ultimo_costo(const char * annoes) const
|
|
{
|
|
int index = find_storico(annoes, annoes);
|
|
|
|
if (index < 0)
|
|
{
|
|
const real costo = get_real(ANAMAG_ULTCOS1);
|
|
return costo == ZERO ? costo_standard(annoes) : costo;
|
|
}
|
|
// else
|
|
// {
|
|
// const real costo = _storico->row(index).get_real(STOMAG_ULTCOS1);
|
|
// return costo == ZERO ? costo_standard(annoes) : costo;
|
|
}
|
|
|
|
real TArticolo_giacenza::media_costi(const char * annoes) const
|
|
{
|
|
int index = find_storico(annoes, annoes);
|
|
|
|
if (index < 0)
|
|
{
|
|
real costo = get_real(ANAMAG_ULTCOS1);
|
|
|
|
costo = (costo + get_real(ANAMAG_ULTCOS2)) / 2.0;
|
|
return costo == ZERO ? costo_standard(annoes) : costo;
|
|
}
|
|
// else
|
|
// {
|
|
// TRectype & rec = _storico->row(index);
|
|
// real costo = rec.get_real(STOMAG_ULTCOS1);
|
|
//
|
|
// costo = (costo + rec.get_real(STOMAG_ULTCOS2)) / 2.0;
|
|
// return costo == ZERO ? costo_standard(annoes) : costo;
|
|
// }
|
|
}
|
|
|
|
real TArticolo_giacenza::prezzo_listino(const char * annoes, const char * codlist) const
|
|
{
|
|
int index = find_storico(annoes, annoes);
|
|
|
|
if (index < 0)
|
|
{
|
|
static TString16 f;
|
|
if (f.empty())
|
|
f.format("#%d->%s", LF_UMART, UMART_PREZZO);
|
|
return get_real(f); // dat terminare
|
|
}
|
|
// else
|
|
// return _storico->row(index).get_real(STOMAG_VALVEN);
|
|
}
|
|
|
|
real TArticolo_giacenza::costo_standard(const char * annoes) const
|
|
{
|
|
int index = find_storico(annoes, annoes);
|
|
|
|
if (index < 0)
|
|
return get_real(ANAMAG_COSTSTD);
|
|
// else
|
|
// return _storico->row(index).get_real(STOMAG_COSTSTD);
|
|
}
|
|
|
|
|
|
real TArticolo_giacenza::costo_medio(const char * annoes, const char * codmag, const char * livello) const
|
|
{
|
|
real acq;
|
|
real valacq;
|
|
for (int i = find_mag(annoes, codmag, livello); i > 0; i = find_mag(annoes, codmag, livello, i))
|
|
{
|
|
const TRectype & rec = _mag->row(i);
|
|
|
|
acq += rec.get_real(MAG_ACQ);
|
|
valacq += rec.get_real(MAG_VALACQ);
|
|
}
|
|
return acq == ZERO ? costo_standard(annoes) : valacq / acq;
|
|
}
|
|
|
|
real TArticolo_giacenza::LIFO_annuale(const char * annoes, const char * codmag, const char * livello,
|
|
bool giacenza_effettiva, bool valorizza_componenti) const
|
|
{
|
|
real rim;
|
|
real valrim;
|
|
real acq;
|
|
real valacq;
|
|
real giacenza;
|
|
for (int i = find_mag(annoes, codmag, livello); i > 0; i = find_mag(annoes, codmag, livello, i))
|
|
{
|
|
const TRectype & rec = _mag->row(i);
|
|
|
|
rim += rec.get_real(MAG_RIM);
|
|
valrim += rec.get_real(MAG_VALRIM);
|
|
acq += rec.get_real(MAG_ACQ);
|
|
valacq += rec.get_real(MAG_VALACQ);
|
|
giacenza += rec.get_real(MAG_GIAC);
|
|
if (giacenza_effettiva)
|
|
{
|
|
giacenza += rec.get_real(MAG_INCL);
|
|
giacenza -= rec.get_real(MAG_ACL);
|
|
giacenza += valorizza_componenti ? rec.get_real(MAG_PRODCOMP) : rec.get_real(MAG_PRODFIN);
|
|
|
|
}
|
|
}
|
|
if (giacenza <= ZERO)
|
|
return ZERO;
|
|
if (giacenza > rim)
|
|
return ((giacenza - rim) * (acq == ZERO ? costo_standard(annoes) : valacq / acq) + valrim) / giacenza;
|
|
return valrim / rim;
|
|
}
|
|
|
|
real TArticolo_giacenza::FIFO_annuale(const char * annoes, const char * codmag, const char * livello,
|
|
bool giacenza_effettiva, bool valorizza_componenti) const
|
|
{
|
|
real rim;
|
|
real valrim;
|
|
real acq;
|
|
real valacq;
|
|
real giacenza;
|
|
for (int i = find_mag(annoes, codmag, livello); i > 0; i = find_mag(annoes, codmag, livello, i))
|
|
{
|
|
const TRectype & rec = _mag->row(i);
|
|
|
|
rim += rec.get_real(MAG_RIM);
|
|
valrim += rec.get_real(MAG_VALRIM);
|
|
acq += rec.get_real(MAG_ACQ);
|
|
valacq += rec.get_real(MAG_VALACQ);
|
|
giacenza += rec.get_real(MAG_GIAC);
|
|
if (giacenza_effettiva)
|
|
{
|
|
giacenza += rec.get_real(MAG_INCL);
|
|
giacenza -= rec.get_real(MAG_ACL);
|
|
giacenza += valorizza_componenti ? rec.get_real(MAG_PRODCOMP) : rec.get_real(MAG_PRODFIN);
|
|
|
|
}
|
|
}
|
|
if (giacenza <= ZERO)
|
|
return ZERO;
|
|
|
|
if (giacenza > acq)
|
|
return ((giacenza - acq) * (valrim / rim) + valacq) / giacenza;
|
|
return valacq / acq;
|
|
}
|
|
|
|
real TArticolo_giacenza::LIFO(const char * annoes, const char * codmag, const char * livello,
|
|
bool giacenza_effettiva, bool valorizza_componenti) const
|
|
{
|
|
real rim;
|
|
real valrim;
|
|
real acq;
|
|
real valacq;
|
|
real giacenza;
|
|
for (int i = find_mag(annoes, codmag, livello); i > 0; i = find_mag(annoes, codmag, livello, i))
|
|
{
|
|
const TRectype & rec = _mag->row(i);
|
|
|
|
rim += rec.get_real(MAG_RIM);
|
|
valrim += rec.get_real(MAG_VALRIM);
|
|
acq += rec.get_real(MAG_ACQ);
|
|
valacq += rec.get_real(MAG_VALACQ);
|
|
giacenza += rec.get_real(MAG_GIAC);
|
|
if (giacenza_effettiva)
|
|
{
|
|
giacenza += rec.get_real(MAG_INCL);
|
|
giacenza -= rec.get_real(MAG_ACL);
|
|
giacenza += valorizza_componenti ? rec.get_real(MAG_PRODCOMP) : rec.get_real(MAG_PRODFIN);
|
|
|
|
}
|
|
}
|
|
|
|
if (giacenza <= ZERO)
|
|
return ZERO;
|
|
if (giacenza > rim)
|
|
return ((giacenza - rim) * (acq == ZERO ? costo_standard(annoes) : valacq / acq) + valrim) / giacenza;
|
|
|
|
((TArticolo_giacenza *) this)->load_storico(annoes);
|
|
const int last = _storico->last_row();
|
|
|
|
rim = giacenza;
|
|
for (i = _storico->first_row(); i <= last; i = _storico->succ_row(i))
|
|
{
|
|
const TRectype & rec = _storico->row(i);
|
|
const real qta = rec.get(STOMAG_QUANT);
|
|
|
|
if (qta > giacenza)
|
|
{
|
|
valrim += (rec.get_real(STOMAG_VALORE) / qta) * giacenza;
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
valrim += rec.get_real(STOMAG_VALORE);
|
|
giacenza -= qta;
|
|
}
|
|
}
|
|
return valrim / rim;
|
|
}
|
|
|
|
real TArticolo_giacenza::FIFO(const char * annoes, const char * codmag, const char * livello,
|
|
bool giacenza_effettiva, bool valorizza_componenti) const
|
|
{
|
|
real rim;
|
|
real valrim;
|
|
real acq;
|
|
real valacq;
|
|
real giacenza;
|
|
for (int i = find_mag(annoes, codmag, livello); i > 0; i = find_mag(annoes, codmag, livello, i))
|
|
{
|
|
const TRectype & rec = _mag->row(i);
|
|
|
|
rim += rec.get_real(MAG_RIM);
|
|
valrim += rec.get_real(MAG_VALRIM);
|
|
acq += rec.get_real(MAG_ACQ);
|
|
valacq += rec.get_real(MAG_VALACQ);
|
|
giacenza += rec.get_real(MAG_GIAC);
|
|
if (giacenza_effettiva)
|
|
{
|
|
giacenza += rec.get_real(MAG_INCL);
|
|
giacenza -= rec.get_real(MAG_ACL);
|
|
giacenza += valorizza_componenti ? rec.get_real(MAG_PRODCOMP) : rec.get_real(MAG_PRODFIN);
|
|
|
|
}
|
|
}
|
|
|
|
if (giacenza <= ZERO)
|
|
return ZERO;
|
|
if (giacenza <= acq)
|
|
return valacq / acq;
|
|
|
|
((TArticolo_giacenza *) this)->load_storico(annoes);
|
|
|
|
const int last = _storico->last_row();
|
|
|
|
real res = giacenza - acq;
|
|
rim = ZERO;
|
|
valrim = ZERO;
|
|
for (i = last; i > 0; i--)
|
|
{
|
|
const TRectype & rec = _storico->row(i);
|
|
const real qta = rec.get(STOMAG_QUANT);
|
|
|
|
if (qta > res)
|
|
{
|
|
rim += res;
|
|
valrim += (rec.get_real(STOMAG_VALORE) / qta) * res;
|
|
}
|
|
else
|
|
{
|
|
rim += qta;
|
|
valrim += rec.get_real(STOMAG_VALORE);
|
|
res -= qta;
|
|
}
|
|
}
|
|
return ((giacenza - acq) * (valrim / rim) + valacq) / giacenza;
|
|
}
|
|
|
|
TArticolo_giacenza::~TArticolo_giacenza()
|
|
{
|
|
if (_mag)
|
|
delete _mag;
|
|
}
|
|
|
|
|