From 4dcf5f74ad9727fc7c751e7e55cae673c714137e Mon Sep 17 00:00:00 2001 From: guy Date: Thu, 10 Nov 1994 13:42:56 +0000 Subject: [PATCH] Generazione automatizzata delle righe contabili nei movimenti senza IVA git-svn-id: svn://10.65.10.50/trunk@586 c028cbd2-c16b-5b4b-a496-9718f37d4682 --- cg/cg2100.cpp | 17 +- cg/cg2102.cpp | 124 ++++-- cg/cg2102.h | 1 + cg/cg2103.cpp | 1176 ++++++++++++++++++++++++------------------------- cg/cg2104.cpp | 440 +++++++++--------- 5 files changed, 913 insertions(+), 845 deletions(-) diff --git a/cg/cg2100.cpp b/cg/cg2100.cpp index 947bd8c27..84eb2a63f 100755 --- a/cg/cg2100.cpp +++ b/cg/cg2100.cpp @@ -612,7 +612,7 @@ void TPrimanota_application::mask2rel(const TMask& m) // Controlla se e' un movimento con righe contabili if (iva() == nessuna_iva || !m.get_bool(F_SOLAIVA)) - { + { for (int i = 0; i < rows.items(); i++) { TToken_string& row = (TToken_string&)rows[i]; @@ -624,14 +624,13 @@ void TPrimanota_application::mask2rel(const TMask& m) TRectype &r = _rel->cg(i); r.zero(); - r.put("IMPORTO", n.valore()); // Importo - r.put("SEZIONE", n.sezione()); // Sezione - - r.put("NUMREG", numreg); - r.put("ANNOES", annoes); - r.put("DATAREG", datareg); - r.put("NUMRIG", i+1); + r.put("NUMREG", numreg); // Numero registrazione + r.put("ANNOES", annoes); // Anno esercizio + r.put("DATAREG", datareg); // Data di registrazione + r.put("NUMRIG", i+1); // Numero riga + r.put("SEZIONE", n.sezione()); // Sezione + r.put("IMPORTO", n.valore()); // Importo put_conto(r, conto); // Conto row.get(); // Codice descrizione @@ -689,6 +688,7 @@ void TPrimanota_application::mask2rel(const TMask& m) if (err) _rel->lfile().zero("OCFPI"); } + const bool intra = causale().intra(); const bool to_swap = test_swap(FALSE); if (to_swap) { @@ -708,6 +708,7 @@ void TPrimanota_application::mask2rel(const TMask& m) r.zero(); r.put("ANNOES", annoes); r.put("NUMREG", numreg); + r.put("INTRA", intra); // Causale intra r.put("NUMRIG", i+1); real imponibile(row.get(0)); diff --git a/cg/cg2102.cpp b/cg/cg2102.cpp index 9b9053776..eb9fc001e 100755 --- a/cg/cg2102.cpp +++ b/cg/cg2102.cpp @@ -487,45 +487,111 @@ bool TPrimanota_application::cg_handler(TMask_field& f, KEY k) } +void TPrimanota_application::generazione_righe_cg(int r) +{ + TSheet_field& cg = cgs(); + TToken_string& row = cg.row(r); + + TImporto importo; importo = row; + + if (importo.is_zero()) + return; + + if (r == 0 && cg.row(1).empty_items()) + { + TBill contro(row, 9, 0x3); + if (!contro.ok()) + { + causale().bill(2, contro); + contro.add_to(row, 9, 0x3); + cg.force_update(r); + } + if (contro.ok()) + { + importo.swap_section(); + set_cgs_row(1, importo, contro, "", ' '); + TBill conto(row, 2, 0x3); + conto.add_to(cg.row(1), 9, 0x3); + cg.force_update(1); + } + } + + if (causale().codice()[0] > ' ') + { + int first_not_empty = 0; + for (int i = 0; i < r; i++) + { + TImporto im; im = cg.row(i); + if (!im.is_zero()) + { + first_not_empty = i; + break; + } + } + + TBill conto(row, 2, 0x3); + if (first_not_empty == r) + { + for (i = r+1; i < cg.items(); i++) + { + TToken_string& rowi = cg.row(i); + int gruppo = rowi.get_int(3); + if (gruppo != 0) + { + gruppo = rowi.get_int(10); + if (gruppo == 0) + { + char sez = 'D'; + if (cg.cell_disabled(i,0)) sez = 'A'; + if (importo.sezione() != sez) + { + conto.add_to(rowi, 9, 0x3); + cg.force_update(i); + } + } + } + else + break; + } + } + else + { + TToken_string& rowi = cg.row(first_not_empty); + const int gruppo = rowi.get_int(10); + if (gruppo == 0) + { + conto.add_to(rowi, 9, 0x3); + cg.force_update(first_not_empty); + } + } + } +} + + bool TPrimanota_application::cg_notify(int r, KEY k) { - TSheet_field& cg = app().cgs(); - TToken_string& row = cg.row(r); - const char tipo = row.empty() ? ' ' : row.right(1)[0]; - switch(k) { case K_SPACE: - cg.sheet_mask().enable(DLG_DELREC, tipo == ' '); + { + TSheet_field& cg = app().cgs(); + TToken_string& row = cg.row(r); + const char tipo = row.right(1)[0]; + cg.sheet_mask().enable(DLG_DELREC, tipo <= ' '); + } break; case K_ENTER: - if (r == 0 && app().iva() == nessuna_iva && cg.row(1).empty_items()) - { - TImporto i; i = row; - if (!i.is_zero()) - { - TBill contro(row, 9, 0x3); - if (!contro.ok() && *app().causale().codice() > ' ') - { - app().causale().bill(2, contro); - contro.add_to(row, 9, 0x3); - cg.force_update(r); - } - if (contro.ok()) - { - i.swap_section(); - app().set_cgs_row(1, i, contro, "", ' '); - TBill conto(row, 2, 0x3); - conto.add_to(cg.row(1), 9, 0x3); - app().cgs().force_update(1); - } - } - } + if (app().iva() == nessuna_iva) + app().generazione_righe_cg(r); app().calcola_saldo(); break; case K_DEL: - if (tipo != ' ') - return error_box("La riga %d non puo' essere cancellata", r+1); + { + TToken_string& row = app().cgs().row(r); + const char tipo = row.right(1)[0]; + if (tipo > ' ') + return error_box("La riga %d non puo' essere cancellata", r+1); + } break; default: break; diff --git a/cg/cg2102.h b/cg/cg2102.h index 65fd9c2f0..39949d835 100755 --- a/cg/cg2102.h +++ b/cg/cg2102.h @@ -165,6 +165,7 @@ protected: void disable_cgs_cells(int n, char tipo); void add_cgs_tot(TMask& m); void add_cgs_rit(bool fisc); + void generazione_righe_cg(int r); void* get_app_data() { return _app_data; } void set_app_data(void* v) { _app_data = v; } diff --git a/cg/cg2103.cpp b/cg/cg2103.cpp index 8b1a1cbc8..022c42745 100755 --- a/cg/cg2103.cpp +++ b/cg/cg2103.cpp @@ -1,588 +1,588 @@ -#include -#include - -#include "cg2103.h" - -#include -#include - -const char* iva2name(TipoIVA iva) -{ - const char* i; - switch(iva) - { - case nessuna_iva: - i = "Nessuna IVA"; break; - case iva_acquisti: - i = "IVA Acquisti"; break; - case iva_vendite: - i = "IVA Vendite"; break; - case iva_generica: - i = "IVA Generica"; break; - default: - i = "IVA ERRATA!"; break; - } - return i; -} - -/////////////////////////////////////////////////////////// -// Registro -/////////////////////////////////////////////////////////// - -TRegistro::TRegistro(const char* cod, int year) : _rec(LF_TAB), _att(LF_ATTIV) -{ - read(cod, year); -} - - -bool TRegistro::read(const char* cod, int year) -{ - if (year <= 0) - { - const TDate oggi(TODAY); - year = oggi.year(); - } - - int err = ~NOERR; - - TTable reg("REG"); - if (cod && *cod > ' ') - { - TString16 chiave; chiave.format("%04d%s", year, cod); - reg.put("CODTAB", chiave); - err = reg.read(); - } - _rec = reg.curr(); - - if (_att.ok()) - _att.zero(); - if (err != NOERR) - _rec.zero(); - _prorata = -1.0; - - return err == NOERR; -} - -bool TRegistro::reread() -{ - if (ok()) - { - const TString16 n(name()); - const int y = year(); - return read(n, y); - } - return FALSE; -} - -int TRegistro::year() const -{ - int anno = 0; - if (ok()) - { - TString16 sanno(_rec.get("CODTAB")); sanno.cut(4); - anno = atoi(sanno); - } - return anno; -} - - -static TString16 n; -const TString& TRegistro::name() const -{ - if (ok()) n = _rec.get("CODTAB").mid(4); - else n = ""; - return n; -} - - -TRegistro& TRegistro::operator =(const TRegistro& r) -{ - _rec = r._rec; - _att = r._att; - _prorata = r._prorata; - return *this; -} - - -int TRegistro::tipo() const -{ - const int t = ok() ? _rec.get_int("I0") : 0; - return t; -} - - -bool TRegistro::corrispettivi() const -{ - const bool c = ok() ? _rec.get_bool("B0") : FALSE; - return c; -} - - -TipoIVA TRegistro::iva() const -{ - TipoIVA i = (TipoIVA)tipo(); - switch (i) - { - case nessuna_iva: - case iva_vendite: - case iva_acquisti: - break; - case libro_giornale: - i = nessuna_iva; - break; - default: - error_box("Il registro '%s' non e' un registro IVA o contabile: tipo %d", - (const char*)name(), i); - i = nessuna_iva; - break; - } - return i; -} - -bool TRegistro::read_att() -{ - if (!_att.empty()) - return TRUE; - - TLocalisamfile attiv(LF_ATTIV); - attiv.put("CODDITTA", MainApp()->get_firm()); - attiv.put("CODATT", attivita()); - const int err = attiv.read(); - _att = attiv.curr(); - if (err != NOERR) - _att.zero(); - - return err == NOERR; -} - -bool TRegistro::agenzia_viaggi() -{ - bool av = FALSE; - if (iva() == iva_vendite && read_att()) - av = _att.get_bool("REG74TER"); - return av; -} - -const TString& TRegistro::tipo_attivita() -{ - read_att(); - return _att.get("TIPOATT"); -} - -const real& TRegistro::prorata() -{ - if (_prorata.sign() < 0) - { - TString16 chiave; // Anno - Attivita' - Tipo Attivita' (fissata a 1) - chiave << year(); - chiave << attivita() << "1"; - - TTable pla("PLA"); - pla.put("CODTAB", chiave); - if (pla.read() == NOERR) - _prorata = pla.get_real("R8"); - else - _prorata = 0.0; - } - - return _prorata; -} - -// Certified 99% -bool TRegistro::update(long protiva, const TDate& datareg) -{ - bool updated = TRUE; - - if (protiva > _rec.get_long("I5")) - { - _rec.put("I5", protiva); - updated = FALSE; - } - if (datareg > _rec.get_date("D2")) - { - _rec.put("D2", datareg); - updated = FALSE; - } - if (!updated) - { - TTable reg("REG"); - updated = reg.rewrite(_rec) == NOERR; - } - - return updated; -} - -/////////////////////////////////////////////////////////// -// Libro giornale -/////////////////////////////////////////////////////////// - -// Legge il libro giornale dell'anno specificato -bool TLibro_giornale::read(int y) -{ - if (y <= 0) - { - const TDate oggi(TODAY); - y = oggi.year(); - } - - TTable reg("REG"); - bool found = FALSE; - - TString16 anno; anno.format("%04d", y); - - reg.setkey(1); - reg.put("CODTAB", anno); // Cerca il primo registro dell'anno y - - for (int err = reg.read(_isgteq); err == NOERR; err = reg.next()) - { - if (strncmp(reg.get("CODTAB"), anno, 4) != 0) break; - if (reg.get_int("I0") == libro_giornale) - { - found = TRUE; - break; - } - } - - if (!found) reg.zero(); // Memorizza record (anche vuoto) - _rec = reg.curr(); - - return found; -} - -/* Now obsolete - TDate TLibro_giornale::global_last_print() const - { - TTable reg("REG"); - reg.setkey(1); - - TDate last(botime); - for (int err = reg.first(); err == NOERR; err = reg.next()) - { - if (reg.get_int("I0") == libro_giornale) - { - const TDate l(reg.get("D3")); - if (l > last) last = l; - } - } - return last; - } - */ - -TLibro_giornale::TLibro_giornale(int y) -{ - read(y); -} - -/////////////////////////////////////////////////////////// -// Causale -/////////////////////////////////////////////////////////// - -TCausale::TCausale(const char* cod, int year) -: TArray(12), _rec(LF_CAUSALI), _iva(iva_errata), - _sezione_clifo(' '), _sezione_ritsoc(' '), _corrisp(FALSE) -{ - if (*cod) read(cod, year); -} - - -// Legge le righe della causale attualmente selezionata sulla maschera -bool TCausale::read(const char* cod, int year) -{ - TLocalisamfile caus(LF_CAUSALI); - - _rec = caus.curr(); _rec.zero(); // Delete header - destroy(); // Delete all rows - _iva = iva_errata; // Delete misc info - _sezione_clifo = _sezione_ritsoc = ' '; - _corrisp = FALSE; - - if (*cod > ' ') - { - caus.setkey(1); - caus.zero(); - caus.put(CAU_CODCAUS, cod); - - int err = caus.read(); - if (err != NOERR) return FALSE; - _rec = caus.curr(); - - TLocalisamfile rcaus(LF_RCAUSALI); - rcaus.setkey(1); - rcaus.zero(); - rcaus.put(CAU_CODCAUS, cod); - rcaus.put(CAU_NRIGA, 0); - - err = rcaus.read(_isgteq); // Find first line - if (err != NOERR) return FALSE; - - while (err == NOERR && rcaus.get(CAU_CODCAUS) == cod) - { - const int riga = rcaus.get_int(CAU_NRIGA); - add(rcaus.curr(), riga); - err = rcaus.next(); // Read next line - } - rcaus.zero(); - for (int riga = 1; riga < size(); riga++) // Fill gaps - if (objptr(riga) == NULL) add(rcaus.curr(), riga); - - TString16 codreg(caus.get("REG")); - const bool ok = _reg.read(codreg, year); // Read register - if (!ok && codreg.not_empty()) - return error_box("Non esiste il registro '%s' per l'anno %d", - (const char*)codreg, year); - } - else - { - _iva = nessuna_iva; // Clear IVA data - _corrisp = FALSE; - _reg.read("", year); - } - - return TRUE; -} - - -const TRectype& TCausale::row(int num) const -{ - const TRectype* rec = (const TRectype*)objptr(num); - CHECKD(rec, "Manca la riga di causale ", num); - return *rec; -} - - -TBill& TCausale::bill(int num, TBill& conto) const -{ - const TRectype& rec = row(num); - conto.set(rec.get_int(RCA_GRUPPO), rec.get_int(RCA_CONTO), - rec.get_long(RCA_SOTTOCONTO), rec.get_char(RCA_TIPOCF)); - return conto; -} - -const char* TCausale::desc_agg(int num) const -{ - const TRectype& rec = row(num); - TString16 cod(rec.get(RCA_CODDESC)); - - TTable da("%DPN"); - da.put("CODTAB", cod); - if (da.read() != NOERR) - da.zero(); - return da.get("S0"); -} - -const char* TCausale::descrizione() const -{ return _rec.ok() ? _rec.get(CAU_DESCR) : ""; } - -const char* TCausale::codice() const -{ return _rec.ok() ? _rec.get(CAU_CODCAUS) : ""; } - - -bool TCausale::data_doc() const -{ return _rec.ok() ? _rec.get_bool("DATADOC") : FALSE; } - -bool TCausale::num_doc() const -{ return _rec.ok() ? _rec.get_bool("NUMDOC") : FALSE; } - -bool TCausale::apertura() const -{ return _rec.ok() ? _rec.get_char("MOVAP") == 'A' : FALSE; } - -bool TCausale::sezionale() const -{ return _rec.ok() ? _rec.get_bool("MOVSEZ") : FALSE; } - -bool TCausale::valuta() const -{ return _rec.ok() ? _rec.get_bool("MOVVAL") : FALSE; } - -bool TCausale::intra() const -{ return _rec.ok() ? _rec.get_bool("INTRACOM") : FALSE; } - -bool TCausale::valintra() const -{ return _rec.ok() ? _rec.get_bool("VALINTRA") : FALSE; } - -const char* TCausale::causale_inc_imm() const -{ return _rec.ok() ? _rec.get("CODCAUSIM") : ""; } - -const char* TCausale::tipo_doc() const -{ return _rec.ok() ? _rec.get("TIPODOC") : ""; } - -bool TCausale::saldaconto() const -{ return _rec.ok() ? (_rec.get_int("TIPOMOV") > 0) : FALSE; } - -bool TCausale::ok() const -{ - if (iva() == iva_errata) - return FALSE; - - if (corrispettivi() != reg().corrispettivi()) - return error_box("Tipo documento e registro incongruenti per i corrispettivi"); - - return TRUE; -} - -char TCausale::sezione(int riga) const -{ - const TRectype& r = row(riga); - char sez = toupper(r.get_char(RCA_SEZIONE)); - if (sez <= ' ') // Guess section on tipocf - { - char tipocf = toupper(row(1).get_char(RCA_TIPOCF)); - if (tipocf <= ' ') - tipocf = (iva() == iva_vendite) ? 'C' : 'F'; // Guess tipocf on IVA - sez = (tipocf == 'C') ? 'D' : 'A'; - } - return sez; -} - -char TCausale::sezione_clifo() -{ - if (_sezione_clifo == ' ') - _sezione_clifo = sezione(1); - return _sezione_clifo; -} - -char TCausale::sezione_ritsoc() -{ - if (_sezione_ritsoc == ' ') - _sezione_ritsoc = sezione(9); - return _sezione_ritsoc; -} - -TipoIVA TCausale::iva() const -{ - if (_iva == iva_errata) - { - TipoIVA i = nessuna_iva; // Tipo IVA di default - bool c = FALSE; // Corrispettivi di default - - TString16 tipodoc(_rec.get("TIPODOC")); - if (tipodoc.not_empty()) - { - TTable tpd("%TPD"); - tpd.put("CODTAB", tipodoc); - if (tpd.read() == NOERR) - { - i = (TipoIVA)tpd.get_int("I0"); // IVA acquisti, vendite, generica - const TipoIVA ri = _reg.iva(); - if (i == iva_generica) - i = ri; - if (i != ri) - { - error_box("Tipo documento incompatibile con tipo registro"); - i = iva_errata; - } - c = tpd.get_bool("B0"); - } - } - ((TCausale*)this)->_iva = i; // Ricorda IVA per la prossima volta - ((TCausale*)this)->_corrisp = c; // Ricorda flag per la prossima volta - } - return _iva; -} - -bool TCausale::corrispettivi() const -{ - if (_iva == iva_errata) iva(); - return _corrisp; -} - -bool TCausale::similar(const TCausale& c) const -{ - const char* err = ""; - if (sezionale() != c.sezionale()) err = "il segnale di sezionale"; - if (intra() != c.intra()) err = "la gestione dei movimenti intra"; - if (valuta() != c.valuta()) err = "la gestione valuta"; - if (corrispettivi() != c.corrispettivi()) err = "la gestione dei corrispettivi"; - if (iva() != c.iva()) err = "il tipo di IVA"; - - if (*err) - error_box("La causale e' diversa per %s", err); - - return *err ? FALSE : TRUE; -} - -/////////////////////////////////////////////////////////// -// Codice IVA -/////////////////////////////////////////////////////////// - -TCodiceIVA::TCodiceIVA(const char* cod) : TRectype(LF_TABCOM) -{ - read(cod); -} - -bool TCodiceIVA::read(const char* cod) -{ - int err = ~NOERR; - TTable iva("%IVA"); - if (cod && *cod) - { - iva.setkey(1); - iva.put("CODTAB", cod); - err = iva.read(); - } - TRectype::operator=(iva.curr()); - if (err != NOERR) zero(); - return err == NOERR; -} - - -/////////////////////////////////////////////////////////// -// Importo -/////////////////////////////////////////////////////////// - -const TImporto& TImporto::add_to(TToken_string& s) const -{ - const bool dare = sezione() == 'D'; - const char* v = valore().string(); - s.add(dare ? v : "", 0); - s.add(dare ? "" : v, 1); - return *this; -} - -// Cerified 99% -// Ambigous section for ZERO -const TImporto& TImporto::operator =(TToken_string& sv) -{ - _valore = real(sv.get(0)); - if (_valore == ZERO) - { - _valore = real(sv.get()); - _sezione = 'A'; - } - else - _sezione = 'D'; - return *this; -} - - -const TImporto& TImporto::set(char s, const real& v) -{ - CHECKD(s == 'D' || s == 'A', "Sezione errata per importo: codice ", (int)s); - _sezione = s; _valore = v; - return *this; -} - - -const TImporto& TImporto::operator += (const TImporto& i) -{ - if (_sezione == i._sezione) - _valore += i._valore; - else - _valore -= i._valore; - return *this; -} - - -const TImporto& TImporto::operator -= (const TImporto& i) -{ - if (_sezione == i._sezione) - _valore -= i._valore; - else - _valore += i._valore; - return *this; -} - -const TImporto& TImporto::swap_section() -{ - _sezione = (_sezione == 'D') ? 'A' : 'D'; - return *this; -} - +#include +#include + +#include "cg2103.h" + +#include +#include + +const char* iva2name(TipoIVA iva) +{ + const char* i; + switch(iva) + { + case nessuna_iva: + i = "Nessuna IVA"; break; + case iva_acquisti: + i = "IVA Acquisti"; break; + case iva_vendite: + i = "IVA Vendite"; break; + case iva_generica: + i = "IVA Generica"; break; + default: + i = "IVA ERRATA!"; break; + } + return i; +} + +/////////////////////////////////////////////////////////// +// Registro +/////////////////////////////////////////////////////////// + +TRegistro::TRegistro(const char* cod, int year) : _rec(LF_TAB), _att(LF_ATTIV) +{ + read(cod, year); +} + + +bool TRegistro::read(const char* cod, int year) +{ + if (year <= 0) + { + const TDate oggi(TODAY); + year = oggi.year(); + } + + int err = ~NOERR; + + TTable reg("REG"); + if (cod && *cod > ' ') + { + TString16 chiave; chiave.format("%04d%s", year, cod); + reg.put("CODTAB", chiave); + err = reg.read(); + } + _rec = reg.curr(); + + if (_att.ok()) + _att.zero(); + if (err != NOERR) + _rec.zero(); + _prorata = -1.0; + + return err == NOERR; +} + +bool TRegistro::reread() +{ + if (ok()) + { + const TString16 n(name()); + const int y = year(); + return read(n, y); + } + return FALSE; +} + +int TRegistro::year() const +{ + int anno = 0; + if (ok()) + { + TString16 sanno(_rec.get("CODTAB")); sanno.cut(4); + anno = atoi(sanno); + } + return anno; +} + + +static TString16 n; +const TString& TRegistro::name() const +{ + if (ok()) n = _rec.get("CODTAB").mid(4); + else n = ""; + return n; +} + + +TRegistro& TRegistro::operator =(const TRegistro& r) +{ + _rec = r._rec; + _att = r._att; + _prorata = r._prorata; + return *this; +} + + +int TRegistro::tipo() const +{ + const int t = ok() ? _rec.get_int("I0") : 0; + return t; +} + + +bool TRegistro::corrispettivi() const +{ + const bool c = ok() ? _rec.get_bool("B0") : FALSE; + return c; +} + + +TipoIVA TRegistro::iva() const +{ + TipoIVA i = (TipoIVA)tipo(); + switch (i) + { + case nessuna_iva: + case iva_vendite: + case iva_acquisti: + break; + case libro_giornale: + i = nessuna_iva; + break; + default: + error_box("Il registro '%s' non e' un registro IVA o contabile: tipo %d", + (const char*)name(), i); + i = nessuna_iva; + break; + } + return i; +} + +bool TRegistro::read_att() +{ + if (!_att.empty()) + return TRUE; + + TLocalisamfile attiv(LF_ATTIV); + attiv.put("CODDITTA", main_app().get_firm()); + attiv.put("CODATT", attivita()); + const int err = attiv.read(); + _att = attiv.curr(); + if (err != NOERR) + _att.zero(); + + return err == NOERR; +} + +bool TRegistro::agenzia_viaggi() +{ + bool av = FALSE; + if (iva() == iva_vendite && read_att()) + av = _att.get_bool("REG74TER"); + return av; +} + +const TString& TRegistro::tipo_attivita() +{ + read_att(); + return _att.get("TIPOATT"); +} + +const real& TRegistro::prorata() +{ + if (_prorata.sign() < 0) + { + TString16 chiave; // Anno - Attivita' - Tipo Attivita' (fissata a 1) + chiave << year(); + chiave << attivita() << "1"; + + TTable pla("PLA"); + pla.put("CODTAB", chiave); + if (pla.read() == NOERR) + _prorata = pla.get_real("R8"); + else + _prorata = 0.0; + } + + return _prorata; +} + +// Certified 99% +bool TRegistro::update(long protiva, const TDate& datareg) +{ + bool updated = TRUE; + + if (protiva > _rec.get_long("I5")) + { + _rec.put("I5", protiva); + updated = FALSE; + } + if (datareg > _rec.get_date("D2")) + { + _rec.put("D2", datareg); + updated = FALSE; + } + if (!updated) + { + TTable reg("REG"); + updated = reg.rewrite(_rec) == NOERR; + } + + return updated; +} + +/////////////////////////////////////////////////////////// +// Libro giornale +/////////////////////////////////////////////////////////// + +// Legge il libro giornale dell'anno specificato +bool TLibro_giornale::read(int y) +{ + if (y <= 0) + { + const TDate oggi(TODAY); + y = oggi.year(); + } + + TTable reg("REG"); + bool found = FALSE; + + TString16 anno; anno.format("%04d", y); + + reg.setkey(1); + reg.put("CODTAB", anno); // Cerca il primo registro dell'anno y + + for (int err = reg.read(_isgteq); err == NOERR; err = reg.next()) + { + if (strncmp(reg.get("CODTAB"), anno, 4) != 0) break; + if (reg.get_int("I0") == libro_giornale) + { + found = TRUE; + break; + } + } + + if (!found) reg.zero(); // Memorizza record (anche vuoto) + _rec = reg.curr(); + + return found; +} + +/* Now obsolete + TDate TLibro_giornale::global_last_print() const + { + TTable reg("REG"); + reg.setkey(1); + + TDate last(botime); + for (int err = reg.first(); err == NOERR; err = reg.next()) + { + if (reg.get_int("I0") == libro_giornale) + { + const TDate l(reg.get("D3")); + if (l > last) last = l; + } + } + return last; + } + */ + +TLibro_giornale::TLibro_giornale(int y) +{ + read(y); +} + +/////////////////////////////////////////////////////////// +// Causale +/////////////////////////////////////////////////////////// + +TCausale::TCausale(const char* cod, int year) +: TArray(12), _rec(LF_CAUSALI), _iva(iva_errata), + _sezione_clifo(' '), _sezione_ritsoc(' '), _corrisp(FALSE) +{ + if (*cod) read(cod, year); +} + + +// Legge le righe della causale attualmente selezionata sulla maschera +bool TCausale::read(const char* cod, int year) +{ + TLocalisamfile caus(LF_CAUSALI); + + _rec = caus.curr(); _rec.zero(); // Delete header + destroy(); // Delete all rows + _iva = iva_errata; // Delete misc info + _sezione_clifo = _sezione_ritsoc = ' '; + _corrisp = FALSE; + + if (*cod > ' ') + { + caus.setkey(1); + caus.zero(); + caus.put(CAU_CODCAUS, cod); + + int err = caus.read(); + if (err != NOERR) return FALSE; + _rec = caus.curr(); + + TLocalisamfile rcaus(LF_RCAUSALI); + rcaus.setkey(1); + rcaus.zero(); + rcaus.put(CAU_CODCAUS, cod); + rcaus.put(CAU_NRIGA, 0); + + err = rcaus.read(_isgteq); // Find first line + if (err != NOERR) return FALSE; + + while (err == NOERR && rcaus.get(CAU_CODCAUS) == cod) + { + const int riga = rcaus.get_int(CAU_NRIGA); + add(rcaus.curr(), riga); + err = rcaus.next(); // Read next line + } + rcaus.zero(); + for (int riga = 1; riga < size(); riga++) // Fill gaps + if (objptr(riga) == NULL) add(rcaus.curr(), riga); + + TString16 codreg(caus.get("REG")); + const bool ok = _reg.read(codreg, year); // Read register + if (!ok && codreg.not_empty()) + return error_box("Non esiste il registro '%s' per l'anno %d", + (const char*)codreg, year); + } + else + { + _iva = nessuna_iva; // Clear IVA data + _corrisp = FALSE; + _reg.read("", year); + } + + return TRUE; +} + + +const TRectype& TCausale::row(int num) const +{ + const TRectype* rec = (const TRectype*)objptr(num); + CHECKD(rec, "Manca la riga di causale ", num); + return *rec; +} + + +TBill& TCausale::bill(int num, TBill& conto) const +{ + const TRectype& rec = row(num); + conto.set(rec.get_int(RCA_GRUPPO), rec.get_int(RCA_CONTO), + rec.get_long(RCA_SOTTOCONTO), rec.get_char(RCA_TIPOCF)); + return conto; +} + +const char* TCausale::desc_agg(int num) const +{ + const TRectype& rec = row(num); + TString16 cod(rec.get(RCA_CODDESC)); + + TTable da("%DPN"); + da.put("CODTAB", cod); + if (da.read() != NOERR) + da.zero(); + return da.get("S0"); +} + +const char* TCausale::descrizione() const +{ return _rec.ok() ? _rec.get(CAU_DESCR) : ""; } + +const char* TCausale::codice() const +{ return _rec.ok() ? _rec.get(CAU_CODCAUS) : ""; } + + +bool TCausale::data_doc() const +{ return _rec.ok() ? _rec.get_bool("DATADOC") : FALSE; } + +bool TCausale::num_doc() const +{ return _rec.ok() ? _rec.get_bool("NUMDOC") : FALSE; } + +bool TCausale::apertura() const +{ return _rec.ok() ? _rec.get_char("MOVAP") == 'A' : FALSE; } + +bool TCausale::sezionale() const +{ return _rec.ok() ? _rec.get_bool("MOVSEZ") : FALSE; } + +bool TCausale::valuta() const +{ return _rec.ok() ? _rec.get_bool("MOVVAL") : FALSE; } + +bool TCausale::intra() const +{ return _rec.ok() ? _rec.get_bool("INTRACOM") : FALSE; } + +bool TCausale::valintra() const +{ return _rec.ok() ? _rec.get_bool("VALINTRA") : FALSE; } + +const char* TCausale::causale_inc_imm() const +{ return _rec.ok() ? _rec.get("CODCAUSIM") : ""; } + +const char* TCausale::tipo_doc() const +{ return _rec.ok() ? _rec.get("TIPODOC") : ""; } + +bool TCausale::saldaconto() const +{ return _rec.ok() ? (_rec.get_int("TIPOMOV") > 0) : FALSE; } + +bool TCausale::ok() const +{ + if (iva() == iva_errata) + return FALSE; + + if (corrispettivi() != reg().corrispettivi()) + return error_box("Tipo documento e registro incongruenti per i corrispettivi"); + + return TRUE; +} + +char TCausale::sezione(int riga) const +{ + const TRectype& r = row(riga); + char sez = toupper(r.get_char(RCA_SEZIONE)); + if (sez <= ' ') // Guess section on tipocf + { + char tipocf = toupper(row(1).get_char(RCA_TIPOCF)); + if (tipocf <= ' ') + tipocf = (iva() == iva_vendite) ? 'C' : 'F'; // Guess tipocf on IVA + sez = (tipocf == 'C') ? 'D' : 'A'; + } + return sez; +} + +char TCausale::sezione_clifo() +{ + if (_sezione_clifo == ' ') + _sezione_clifo = sezione(1); + return _sezione_clifo; +} + +char TCausale::sezione_ritsoc() +{ + if (_sezione_ritsoc == ' ') + _sezione_ritsoc = sezione(9); + return _sezione_ritsoc; +} + +TipoIVA TCausale::iva() const +{ + if (_iva == iva_errata) + { + TipoIVA i = nessuna_iva; // Tipo IVA di default + bool c = FALSE; // Corrispettivi di default + + TString16 tipodoc(_rec.get("TIPODOC")); + if (tipodoc.not_empty()) + { + TTable tpd("%TPD"); + tpd.put("CODTAB", tipodoc); + if (tpd.read() == NOERR) + { + i = (TipoIVA)tpd.get_int("I0"); // IVA acquisti, vendite, generica + const TipoIVA ri = _reg.iva(); + if (i == iva_generica) + i = ri; + if (i != ri) + { + error_box("Tipo documento incompatibile con tipo registro"); + i = iva_errata; + } + c = tpd.get_bool("B0"); + } + } + ((TCausale*)this)->_iva = i; // Ricorda IVA per la prossima volta + ((TCausale*)this)->_corrisp = c; // Ricorda flag per la prossima volta + } + return _iva; +} + +bool TCausale::corrispettivi() const +{ + if (_iva == iva_errata) iva(); + return _corrisp; +} + +bool TCausale::similar(const TCausale& c) const +{ + const char* err = ""; + if (sezionale() != c.sezionale()) err = "il segnale di sezionale"; + if (intra() != c.intra()) err = "la gestione dei movimenti intra"; + if (valuta() != c.valuta()) err = "la gestione valuta"; + if (corrispettivi() != c.corrispettivi()) err = "la gestione dei corrispettivi"; + if (iva() != c.iva()) err = "il tipo di IVA"; + + if (*err) + error_box("La causale e' diversa per %s", err); + + return *err ? FALSE : TRUE; +} + +/////////////////////////////////////////////////////////// +// Codice IVA +/////////////////////////////////////////////////////////// + +TCodiceIVA::TCodiceIVA(const char* cod) : TRectype(LF_TABCOM) +{ + read(cod); +} + +bool TCodiceIVA::read(const char* cod) +{ + int err = ~NOERR; + TTable iva("%IVA"); + if (cod && *cod) + { + iva.setkey(1); + iva.put("CODTAB", cod); + err = iva.read(); + } + TRectype::operator=(iva.curr()); + if (err != NOERR) zero(); + return err == NOERR; +} + + +/////////////////////////////////////////////////////////// +// Importo +/////////////////////////////////////////////////////////// + +const TImporto& TImporto::add_to(TToken_string& s) const +{ + const bool dare = sezione() == 'D'; + const char* v = valore().string(); + s.add(dare ? v : "", 0); + s.add(dare ? "" : v, 1); + return *this; +} + +// Cerified 99% +// Ambigous section for ZERO +const TImporto& TImporto::operator =(TToken_string& sv) +{ + _valore = real(sv.get(0)); + if (_valore == ZERO) + { + _valore = real(sv.get()); + _sezione = 'A'; + } + else + _sezione = 'D'; + return *this; +} + + +const TImporto& TImporto::set(char s, const real& v) +{ + CHECKD(s == 'D' || s == 'A', "Sezione errata per importo: codice ", (int)s); + _sezione = s; _valore = v; + return *this; +} + + +const TImporto& TImporto::operator += (const TImporto& i) +{ + if (_sezione == i._sezione) + _valore += i._valore; + else + _valore -= i._valore; + return *this; +} + + +const TImporto& TImporto::operator -= (const TImporto& i) +{ + if (_sezione == i._sezione) + _valore -= i._valore; + else + _valore += i._valore; + return *this; +} + +const TImporto& TImporto::swap_section() +{ + _sezione = (_sezione == 'D') ? 'A' : 'D'; + return *this; +} + diff --git a/cg/cg2104.cpp b/cg/cg2104.cpp index c8f4a137c..7d03488be 100755 --- a/cg/cg2104.cpp +++ b/cg/cg2104.cpp @@ -1,220 +1,220 @@ -// -------------------------------------------------------------------------- -// Scadenzario -// fv 24/8/94 -// -------------------------------------------------------------------------- - -#include "cg2102.h" -#include "cg2100.h" -#include "cg2104a.h" -#include "pagament.h" - -struct shuttle -{ - Pagamento* _pag; // pagamento - TSheet_field* _sheet; // sheet - TMask* _mask; // maschera - TArray* _rows; // righe spreadsheet nella versione originale -}; - -bool TPrimanota_application::pag_notify(int r, KEY k) -{ - // questo e' il bello - shuttle* sh = (shuttle*)(((TPrimanota_application*)MainApp())->get_app_data()); - - Pagamento* pag = sh->_pag; - TSheet_field* ps = sh->_sheet; - TMask* msk = sh->_mask; - TArray* rws = sh->_rows; - - int rdiff = atoi(msk->get(F_RDIFFER)); - - // ts contiene la vecchia riga, ns la nuova - TToken_string ts(36), ns(36); - - - bool doit = TRUE, m_imp = FALSE, m_perc = FALSE, m_pag = FALSE; - bool m_scad = FALSE, m_tipo = FALSE, mod = FALSE; - word ahiahi = P_OK; - - TString news(15), newi(15), newp(15), newt(2); - - bool recalc = msk->get_bool(F_RECALC); - bool mcomm = msk->get_bool(F_MCOMM); - - switch (k) - { - case K_SPACE: - break; - case K_ENTER: - ns = ps->row(r); - ts = (TToken_string&)(*rws)[r]; - - news = ns.get(0); - newp = ns.get(1); - newi = ns.get(2); - newt = ns.get(3); - - // qui viene il bello, si fa per dire - if (strcmp(ts.get(0),news) != 0) // modificata data scadenza - { - mod = m_scad = TRUE; - } - if (strcmp(ts.get(1),newp) != 0) // modificata percentuale - { - mod = m_perc = TRUE; - } - if (strcmp(ts.get(2),newi) != 0) // modificato importo - { - if ((recalc && !m_perc) || (!recalc)) // se si modifica la percentuale l'importo non viene cagato - { - mod = m_imp = TRUE; - } - } - if (strcmp(ts.get(3),newt) != 0) // modificato tipo pagamento - { - mod = m_tipo = TRUE; - } - break; - case K_DEL: - case K_INS: - doit = FALSE; - break; - default: - break; - } - - - // settato da recalc_rate se occorre ridefinire lo sheet - // aggiungendo o togliendo righe - bool need_recalc = FALSE; - - if (k == K_ENTER) - { - if (mod && recalc) - { - // ricalcola sheet come sai fare tu - ahiahi = pag->recalc_rate(r, m_perc, - ((m_perc || m_imp) ? - (m_perc ? (const char*)newp : (const char*)newi) : - NULL), - (m_scad ? (const char*)news : NULL), - (m_tipo ? (const char*)newt : NULL), - rdiff, mcomm, need_recalc); - // see if rdiff changed - msk->field(F_RDIFFER).set(pag->rate_differenziate() ? "1" : "2"); - } - if (!recalc) - { - ahiahi = P_OK; - // put data as they are - TToken_string& trw = pag->rata(r); - TToken_string srw = trw; - if (m_scad) trw.add(news,3); - if (m_perc) trw.add(newp,1); - if (m_imp) trw.add(newi,4); - if (m_tipo) trw.add(newt,2); - // validate the payment - if ((ahiahi = pag->validate()) != P_OK) - pag->rata(r) = srw; - } - if (ahiahi) // any error? - // rimetti le righe com'erano prima - { - beep(); - - // se gli errori sono voluti, spiegali - if (!recalc) - { - TString s(256); - pag->strerr(ahiahi,s); - warning_box(s); - } - - ps->row(r) = (TToken_string&)(*rws)[r]; - ps->force_update(r); - } - - else if (recalc && mod && need_recalc) - { - // ridefinisci lo sheet sulla base delle nuove rate - pag->set_sheet(*ps); - ps->force_update(-1); - rws->destroy(); - (*rws) = ps->rows_array(); - } - } - - return doit; -} - -void TPrimanota_application::aggiorna_scadenzario(const TMask& m) -{ - real imponibile(0.0); - real imposta(0.0); - real spese(0.0); - TString dt(m.get(F_DATAREG)); - Pagamento pag(m.get(F_CODPAG), (const char*)dt); - - if (pag.is_new()) return; - - TMask ms("cg2104a"); - TSheet_field& ps = (TSheet_field&)ms.field(F_RATESHEET); - ps.set_notify(pag_notify); - - ms.field(F_RDIFFER).set(pag.rate_differenziate() ? "1" : "2"); - ms.field(F_NAMEPAG).set(pag.name()); - ms.field(F_TIPOPR).set(pag.desc_tpr()); - ms.field(F_MCOMM).set(pag.mese_commerciale() ? "X" : ""); - - TSheet_field& iva_sh = ivas(); - - for (int i = 0; i < iva_sh.items(); i++) - { - TToken_string& tt = iva_sh.row(i); - real mpo(tt.get(0)); - real imp(tt.get(3)); - imponibile += mpo; - imposta += imp; - // TBI: le spese che cazzo sono? - // ----------------------------- - } - - ms.field(F_IMPONIBILE).set(imponibile.string()); - ms.field(F_IMPOSTA).set(imposta.string()); - ms.field(F_SPESE).set(spese.string()); - ms.field(F_RECALC).set("X"); - - pag.set_total(imponibile, imposta, spese); - - // TBI TBI TBI TBI TBI TBI TBI TBI TBI - // controllare se c'e' una rateazione non standard gia' registrata - // in tal caso occorre zappare e settare le rate a mano - pag.set_rate_auto(); - - // se la prima rata e' fissa non si tocca - if (pag.tipo_prima_rata() > 0) - { - ps.disable_cell(0,0); - ps.disable_cell(0,1); - ps.disable_cell(0,2); - ps.disable_cell(0,3); - } - - // prepara lo sheet - pag.set_sheet(ps); - TArray rows(ps.rows_array()); - - shuttle sh; - - sh._pag = &pag; - sh._sheet = &ps; - sh._mask = &ms; - sh._rows = &rows; - - set_app_data(&sh); - ms.run(); - - // TBI se modificato riaggiustare i files - // chiamando la write del caso - // ------------------------------------------------- -} +// -------------------------------------------------------------------------- +// Scadenzario +// fv 24/8/94 +// -------------------------------------------------------------------------- + +#include "cg2102.h" +#include "cg2100.h" +#include "cg2104a.h" +#include "pagament.h" + +struct shuttle +{ + Pagamento* _pag; // pagamento + TSheet_field* _sheet; // sheet + TMask* _mask; // maschera + TArray* _rows; // righe spreadsheet nella versione originale +}; + +bool TPrimanota_application::pag_notify(int r, KEY k) +{ + // questo e' il bello + shuttle* sh = (shuttle*)(((TPrimanota_application&)main_app()).get_app_data()); + + Pagamento* pag = sh->_pag; + TSheet_field* ps = sh->_sheet; + TMask* msk = sh->_mask; + TArray* rws = sh->_rows; + + int rdiff = atoi(msk->get(F_RDIFFER)); + + // ts contiene la vecchia riga, ns la nuova + TToken_string ts(36), ns(36); + + + bool doit = TRUE, m_imp = FALSE, m_perc = FALSE, m_pag = FALSE; + bool m_scad = FALSE, m_tipo = FALSE, mod = FALSE; + word ahiahi = P_OK; + + TString news(15), newi(15), newp(15), newt(2); + + bool recalc = msk->get_bool(F_RECALC); + bool mcomm = msk->get_bool(F_MCOMM); + + switch (k) + { + case K_SPACE: + break; + case K_ENTER: + ns = ps->row(r); + ts = (TToken_string&)(*rws)[r]; + + news = ns.get(0); + newp = ns.get(1); + newi = ns.get(2); + newt = ns.get(3); + + // qui viene il bello, si fa per dire + if (strcmp(ts.get(0),news) != 0) // modificata data scadenza + { + mod = m_scad = TRUE; + } + if (strcmp(ts.get(1),newp) != 0) // modificata percentuale + { + mod = m_perc = TRUE; + } + if (strcmp(ts.get(2),newi) != 0) // modificato importo + { + if ((recalc && !m_perc) || (!recalc)) // se si modifica la percentuale l'importo non viene cagato + { + mod = m_imp = TRUE; + } + } + if (strcmp(ts.get(3),newt) != 0) // modificato tipo pagamento + { + mod = m_tipo = TRUE; + } + break; + case K_DEL: + case K_INS: + doit = FALSE; + break; + default: + break; + } + + + // settato da recalc_rate se occorre ridefinire lo sheet + // aggiungendo o togliendo righe + bool need_recalc = FALSE; + + if (k == K_ENTER) + { + if (mod && recalc) + { + // ricalcola sheet come sai fare tu + ahiahi = pag->recalc_rate(r, m_perc, + ((m_perc || m_imp) ? + (m_perc ? (const char*)newp : (const char*)newi) : + NULL), + (m_scad ? (const char*)news : NULL), + (m_tipo ? (const char*)newt : NULL), + rdiff, mcomm, need_recalc); + // see if rdiff changed + msk->field(F_RDIFFER).set(pag->rate_differenziate() ? "1" : "2"); + } + if (!recalc) + { + ahiahi = P_OK; + // put data as they are + TToken_string& trw = pag->rata(r); + TToken_string srw = trw; + if (m_scad) trw.add(news,3); + if (m_perc) trw.add(newp,1); + if (m_imp) trw.add(newi,4); + if (m_tipo) trw.add(newt,2); + // validate the payment + if ((ahiahi = pag->validate()) != P_OK) + pag->rata(r) = srw; + } + if (ahiahi) // any error? + // rimetti le righe com'erano prima + { + beep(); + + // se gli errori sono voluti, spiegali + if (!recalc) + { + TString s(256); + pag->strerr(ahiahi,s); + warning_box(s); + } + + ps->row(r) = (TToken_string&)(*rws)[r]; + ps->force_update(r); + } + + else if (recalc && mod && need_recalc) + { + // ridefinisci lo sheet sulla base delle nuove rate + pag->set_sheet(*ps); + ps->force_update(-1); + rws->destroy(); + (*rws) = ps->rows_array(); + } + } + + return doit; +} + +void TPrimanota_application::aggiorna_scadenzario(const TMask& m) +{ + real imponibile(0.0); + real imposta(0.0); + real spese(0.0); + TString dt(m.get(F_DATAREG)); + Pagamento pag(m.get(F_CODPAG), (const char*)dt); + + if (pag.is_new()) return; + + TMask ms("cg2104a"); + TSheet_field& ps = (TSheet_field&)ms.field(F_RATESHEET); + ps.set_notify(pag_notify); + + ms.field(F_RDIFFER).set(pag.rate_differenziate() ? "1" : "2"); + ms.field(F_NAMEPAG).set(pag.name()); + ms.field(F_TIPOPR).set(pag.desc_tpr()); + ms.field(F_MCOMM).set(pag.mese_commerciale() ? "X" : ""); + + TSheet_field& iva_sh = ivas(); + + for (int i = 0; i < iva_sh.items(); i++) + { + TToken_string& tt = iva_sh.row(i); + real mpo(tt.get(0)); + real imp(tt.get(3)); + imponibile += mpo; + imposta += imp; + // TBI: le spese che cazzo sono? + // ----------------------------- + } + + ms.field(F_IMPONIBILE).set(imponibile.string()); + ms.field(F_IMPOSTA).set(imposta.string()); + ms.field(F_SPESE).set(spese.string()); + ms.field(F_RECALC).set("X"); + + pag.set_total(imponibile, imposta, spese); + + // TBI TBI TBI TBI TBI TBI TBI TBI TBI + // controllare se c'e' una rateazione non standard gia' registrata + // in tal caso occorre zappare e settare le rate a mano + pag.set_rate_auto(); + + // se la prima rata e' fissa non si tocca + if (pag.tipo_prima_rata() > 0) + { + ps.disable_cell(0,0); + ps.disable_cell(0,1); + ps.disable_cell(0,2); + ps.disable_cell(0,3); + } + + // prepara lo sheet + pag.set_sheet(ps); + TArray rows(ps.rows_array()); + + shuttle sh; + + sh._pag = &pag; + sh._sheet = &ps; + sh._mask = &ms; + sh._rows = &rows; + + set_app_data(&sh); + ms.run(); + + // TBI se modificato riaggiustare i files + // chiamando la write del caso + // ------------------------------------------------- +}