#include // Togliere quando si toglie il metodo mask() #include #include #include "velib01.h" /////////////////////////////////////////////////////////// // Funzioni per il calcolo dei prezzi netti/lordi /////////////////////////////////////////////////////////// real lordo2netto(real& lordo, const TString& codiva, bool is_valuta) { TTable tabiva("%IVA"); real aliquota = 0.0; tabiva.put("CODTAB", codiva); if (tabiva.read() == NOERR) aliquota = tabiva.get_real("R0"); return lordo2netto(lordo,aliquota,is_valuta); } real netto2lordo(const real& netto, const TString& codiva, bool is_valuta) { TTable tabiva("%IVA"); real aliquota = 0.0; tabiva.put("CODTAB", codiva); if (tabiva.read() == NOERR) aliquota = tabiva.get_real("R0"); return netto2lordo(netto,aliquota,is_valuta); } real lordo2netto(real& lordo, const real& iva, bool is_valuta) { real netto; real imposta = 0.0; real imposta_rec = 0.0; if (!iva.is_zero()) { imposta = (lordo * iva) / (iva + 100.0); // Calcola l'imposta... imposta.ceil(is_valuta ? 3 : 0); } netto = lordo - imposta; // Questo e' l'importo netto imposta_rec = (netto * iva) / 100.0; // Ricalcola l'imposta con il nuovo imponibile imposta_rec.ceil(is_valuta ? 3 : 0); if (imposta != imposta_rec) // In questo caso corregge l'importo lordo lordo = netto + imposta_rec; return netto; } real netto2lordo(const real& netto, const real& iva, bool is_valuta) { real lordo; real imposta = 0.0; if (!iva.is_zero()) { imposta = (netto * iva) / 100.0; // Calcola l'imposta imposta.ceil(is_valuta ? 3 : 0); } lordo = imposta + netto; // prezzo lordo return lordo; } /////////////////////////////////////////////////////////// // Tipo documento /////////////////////////////////////////////////////////// TTipo_documento::TTipo_documento(const char* tipodoc) : _rec(LF_TABCOM), _mask(NULL) { if (tipodoc && *tipodoc) read(tipodoc); } TTipo_documento::TTipo_documento(const TRectype& rec) : _rec(rec), _mask(NULL) { } TTipo_documento::~TTipo_documento() { if (_mask != NULL) delete _mask; } int TTipo_documento::read(const char* tipodoc) { TTable t("%TIP"); t.put("CODTAB", tipodoc); int err = t.read(); if (err == NOERR) _rec = t.curr(); return err; } TMask& TTipo_documento::mask() { if (_mask == NULL) { TFilename profile_file = _rec.get("S4"); profile_file.ext("ini"); TConfig p(profile_file); _mask = new TVariable_mask(p.get("MSKFILE", "MAIN")); } return *_mask; } /////////////////////////////////////////////////////////// // Documento per vendite /////////////////////////////////////////////////////////// TDocumento::TDocumento() : _head(LF_DOC), _rows(LF_RIGHEDOC, "NRIGA") { } TDocumento::TDocumento(const char* codnum, int anno, char provv, long numdoc) : _head(LF_DOC), _rows(LF_RIGHEDOC, "NRIGA") { read(codnum, anno, provv, numdoc); } int TDocumento::read(const TRectype& rec) { _head = rec; // Inizializza i campi chiave in ogni caso TLocalisamfile doc(LF_DOC); int err = _head.read(doc); if (err == NOERR) { TRectype* key = new TRectype(LF_RIGHEDOC); key->put("CODNUM", doc.get("CODNUM")); key->put("ANNO", doc.get("ANNO")); key->put("PROVV", doc.get("PROVV")); key->put("NUMDOC", doc.get("NUMDOC")); err = _rows.read(key); } return err; } int TDocumento::read(const char* codnum, int anno, char provv, long numdoc) { CHECK(codnum && *codnum && anno > 0 && (provv == 'D' || provv == 'P') && numdoc > 0, "Codice documento non valido"); _head.zero(); _head.put("CODNUM", codnum); _head.put("ANNO", anno); _head.put("PROVV", provv); _head.put("NUMDOC", numdoc); return read(_head); } int TDocumento::write(bool re) const { TLocalisamfile doc(LF_DOC); int err = re ? _head.rewrite(doc) : _head.write(doc); if (err != NOERR) err = re ? _head.write(doc) : _head.rewrite(doc); if (err == NOERR) err = _rows.write(re); return err; } int TDocumento::remove() const { TLocalisamfile doc(LF_DOC); int err = _head.remove(doc); if (err == NOERR) err = _rows.remove(); return err; } const TTipo_documento& TDocumento::tipo() const { const char* tipodoc = _head.get("TIPODOC"); CHECK(*tipodoc, "Tipo documento nullo"); TTipo_documento* o = (TTipo_documento*)_tipi.objptr(tipodoc); if (o == NULL) { o = new TTipo_documento(tipodoc); _tipi.add(tipodoc, o); } return *o; } /////////////////////////////////////////////////////////// // Cliente/Fornitore per vendite /////////////////////////////////////////////////////////// void TLista_clifo::TClifo::init(const TRectype& rec) { CHECK(rec.num() == LF_CFVEN, "E' necessario un cliente per vendite"); _codice = rec.get_long(CLI_CODCF); _agente = rec.get_long(CLI_CODAG); _zona = rec.get_long(CLI_CODZONA); } bool TLista_clifo::TClifo::read(char tipo, long cod) { TRelation clifo(LF_CLIFO); clifo.add(LF_CFVEN, "TIPOCF=TIPOCF|CODCF=CODCF"); clifo.curr().put(CLI_TIPOCF, tipo); clifo.curr().put(CLI_CODCF, cod); if (clifo.read() == NOERR) init(clifo.curr(LF_CFVEN)); else zero(); return ok(); } TLista_clifo::TClifo::TClifo(const TRectype& rec) { if (rec.num() == LF_CFVEN) init(rec); else { const char tipo = rec.get_char(CLI_TIPOCF); const long codice = rec.get_long(CLI_CODCF); read(tipo, codice); } } int TLista_clifo::leggi(long dc, long ac, long da, long aa, long dz, long az) { TRelation clifo(LF_CLIFO); clifo.add(LF_CFVEN, "TIPOCF=TIPOCF|CODCF=CODCF"); TRectype start(LF_CLIFO), stop(LF_CLIFO); start.put(CLI_TIPOCF, tipo()); if (dc > 0) start.put(CLI_CODCF, dc); stop.put(CLI_TIPOCF, tipo()); if (ac > 0) stop.put(CLI_CODCF, ac); TString filter(32); if (da > 0) filter << '(' << LF_CFVEN << "->" << CLI_CODAG << ">=" << da << ')'; if (aa > 0) { if (filter.not_empty()) filter << "&&"; filter << '(' << LF_CFVEN << "->" << CLI_CODAG << "<=" << aa << ')'; } if (dz > 0) { if (filter.not_empty()) filter << "&&"; filter << '(' << LF_CFVEN << "->" << CLI_CODZONA << ">=" << dz << ')'; } if (az > 0) { if (filter.not_empty()) filter << "&&"; filter << '(' << LF_CFVEN << "->" << CLI_CODZONA << "<=" << az << ')'; } TCursor cur(&clifo, filter, 1, &start, &stop); for (cur = 0; cur.ok(); ++cur) { TClifo* c = new TClifo(cur.curr(LF_CFVEN)); _clifo.add(c); } if (dc > 0 || ac > 0) ordina_per_codice(); else if (da > 0 || aa > 0) ordina_per_agente(); else if (dz > 0 || az > 0) ordina_per_zona(); return _clifo.items(); } int TLista_clifo::sort_by_code(const TObject** o1, const TObject** o2) { TLista_clifo::TClifo* c1 = (TLista_clifo::TClifo*)*o1; TLista_clifo::TClifo* c2 = (TLista_clifo::TClifo*)*o2; const long d = c1->codice() - c2->codice(); return d == 0L ? 0 : (d > 0 ? +1 : -1); } int TLista_clifo::sort_by_agent(const TObject** o1, const TObject** o2) { TLista_clifo::TClifo* c1 = (TLista_clifo::TClifo*)*o1; TLista_clifo::TClifo* c2 = (TLista_clifo::TClifo*)*o2; const long d = c1->agente() - c2->agente(); return d == 0L ? 0 : (d > 0 ? +1 : -1); } int TLista_clifo::sort_by_zone(const TObject** o1, const TObject** o2) { TLista_clifo::TClifo* c1 = (TLista_clifo::TClifo*)*o1; TLista_clifo::TClifo* c2 = (TLista_clifo::TClifo*)*o2; const long d = c1->zona() - c2->zona(); return d == 0L ? 0 : (d > 0 ? +1 : -1); } int TLista_clifo::ordina_per_codice() { _clifo.sort(sort_by_code); return _clifo.items(); } int TLista_clifo::ordina_per_agente() { _clifo.sort(sort_by_agent); return _clifo.items(); } int TLista_clifo::ordina_per_zona() { _clifo.sort(sort_by_zone); return _clifo.items(); } int TLista_clifo::find(long cod) const { for (int i = items()-1; i >= 0; i--) if (clifo(i).codice() == cod) break; return i; } int TLista_clifo::add(long cod) { int pos = find(cod); if (pos < 0) { TClifo* c = new TClifo(tipo(), cod); pos = _clifo.add(c); } return pos; }