diff --git a/cg/cg2100.cpp b/cg/cg2100.cpp index d105c7a35..d5bdbcf44 100755 --- a/cg/cg2100.cpp +++ b/cg/cg2100.cpp @@ -1,726 +1,727 @@ -#include -#include -#include -#include - -#include "cg2100.h" -#include "cg2102.h" - -// Nomi dei campi -#include -#include -#include - - -TPrimanota_application::TPrimanota_application() - : _rel(NULL), _lastreg(0), _mode(0), _iva(nessuna_iva) -{ - memset(_msk, 0, sizeof(_msk)); -} - - -TMask* TPrimanota_application::load_mask(int n) -{ - if (_msk[n] != NULL) - return _msk[n]; - - TFilename name("cg2100"); name << char(n == 3 ? 'o' : 'a'+n); - TMask* m = new TMask(name); - - switch (n) - { - case 0: - m->set_handler(F_NUMREG, num_handler); - m->set_handler(F_CODCAUS, caus_query_handler); - m->set_handler(F_DATAREG, datareg_handler); - m->set_handler(F_DATACOMP, datacomp_handler); - { - TConfig c(CONFIG_STUDIO, "cg"); - const bool dr = c.get_bool("PoCuDr"); // Scelta primo campo col focus - m->first_focus(dr ? F_DATAREG : F_CODCAUS); - } - break; - case 1: - m->set_handler(F_VISVAL, visval_handler); - m->set_handler(F_DESCR, descr_handler); - break; - case 2: - m->set_handler(F_CODREG, reg_handler); - m->set_handler(F_CLIENTE, clifo_handler); - m->set_handler(F_FORNITORE, clifo_handler); - m->set_handler(F_TOTALE, totale_handler); - m->set_handler(F_RITFIS, ritfis_handler); - m->set_handler(F_RITSOC, ritsoc_handler); - m->set_handler(F_VISVAL, visval_handler); - m->set_handler(F_OCCASEDIT, occas_handler); - m->set_handler(F_SOLAIVA, solaiva_handler); - { - TSheet_field& is = (TSheet_field&)m->field(F_SHEETIVA); - is.set_notify(iva_notify); - is.sheet_mask().set_handler(101, imponibile_handler); - is.sheet_mask().set_handler(102, codiva_handler); - is.sheet_mask().set_handler(103, imposta_handler); - is.sheet_mask().set_handler(108, conto_handler); - is.sheet_mask().set_handler(109, suspended_handler); - is.sheet_mask().set_handler(209, suspended_handler); - is.sheet_mask().set_handler(309, suspended_handler); - } - break; - case 3: - m->set_handler(O_CODICE, occas_code_handler); - break; - default: - CHECKD(0, "Che cavolo di maschera e' la ", n); - break; - } - if (n == 1 || n == 2) - { - m->set_handler(F_DATAREG, datareg_handler); - m->set_handler(F_DATACOMP, datacomp_handler); - m->set_handler(F_CODCAUS, caus_modify_handler); - m->set_handler(F_SHEETCG, cg_handler); - { - TSheet_field& cg = (TSheet_field&)m->field(F_SHEETCG); - cg.set_notify(cg_notify); - TMask& cgm = cg.sheet_mask(); - cgm.set_handler(101, dareavere_handler); - cgm.set_handler(102, dareavere_handler); - cgm.set_handler(105, suspended_handler); - cgm.set_handler(106, suspended_handler); - cgm.set_handler(206, suspended_handler); - cgm.set_handler(306, suspended_handler); - cgm.set_handler(112, suspended_handler); - cgm.set_handler(113, suspended_handler); - cgm.set_handler(213, suspended_handler); - cgm.set_handler(313, suspended_handler); - } - } - - return _msk[n] = m; -} - - -bool TPrimanota_application::user_create() -{ - _tabcom = new TLocalisamfile(LF_TABCOM); - _tab = new TLocalisamfile(LF_TAB); - _caus = new TLocalisamfile(LF_CAUSALI); - _rcaus = new TLocalisamfile(LF_RCAUSALI); - _clifo = new TLocalisamfile(LF_CLIFO); - _pcon = new TLocalisamfile(LF_PCON); - _attiv = new TLocalisamfile(LF_ATTIV); - _saldo = new TLocalisamfile(LF_SALDI); - _comuni = new TLocalisamfile(LF_COMUNI); - _occas = new TLocalisamfile(LF_OCCAS); - _nditte = new TLocalisamfile(LF_NDITTE); - - _rel = new TMovimentoPN; - _rel->lfile()->last(); - _lastreg = _rel->lfile()->get_long(MOV_NUMREG); // Init last registration number - - set_search_field(F_NUMREG); // Set field for default search - - return TRUE; -} - - -bool TPrimanota_application::user_destroy() -{ - if (_msk[3] != NULL) delete _msk[3]; - if (_msk[2] != NULL) delete _msk[2]; - if (_msk[1] != NULL) delete _msk[1]; - if (_msk[0] != NULL) delete _msk[0]; - - delete _rel; - delete _nditte; - delete _occas; - delete _comuni; - delete _saldo; - delete _attiv; - delete _pcon; - delete _clifo; - delete _rcaus; - delete _caus; - delete _tab; - delete _tabcom; - return TRUE; -} - -// Legge la causale di codice cod ed il relativo registro dell'anno year -// Certified 99% -bool TPrimanota_application::read_caus(const char* cod, int year) -{ - const bool ok = causale().read(cod, year); - if (ok) - { - TMask& m = *load_mask(_iva == nessuna_iva ? 1 : 2); - - TEdit_field& nd = (TEdit_field&)m.field(F_NUMDOC); // Numero documento obbligatorio? - nd.check_type(_causale.num_doc() ? CHECK_REQUIRED : CHECK_NORMAL); - - TEdit_field& dd = (TEdit_field&)m.field(F_DATADOC); // Data documento obbligatoria? - dd.check_type(_causale.data_doc() ? CHECK_REQUIRED : CHECK_NORMAL); - } - return ok; -} - - -bool TPrimanota_application::changing_mask(int mode) -{ - if (mode == MODE_QUERY) - return _mode != MODE_QUERY; - - TipoIVA oliva = _iva; - - _iva = nessuna_iva; - - TString16 causale; -// TString16 registro; - int annoes = 0, annoiva = 0; - - switch (mode) - { - case MODE_INS: - annoes = _msk[0]->get_int(F_ANNOES); - annoiva = _msk[0]->get_int(F_ANNOIVA); - causale = _msk[0]->get(F_CODCAUS); - read_caus(causale, annoiva); - break; - case MODE_MOD: - annoes = _rel->lfile()->get_int("ANNOES"); - annoiva = _rel->lfile()->get_int("ANNOIVA"); - causale = _rel->lfile()->get("CODCAUS"); - giornale().read(annoes); -// registro = _rel->lfile()->get("REG"); -// _causale.read(causale, 0); -// _causale.reg().read(registro, annoiva); - read_caus(causale, annoiva); - break; - default: - break; - } - _iva = _causale.iva(); - - return _mode == MODE_QUERY || _iva != oliva; -} - - -TMask* TPrimanota_application::get_mask(int mode) -{ - _mode = mode; - const int m = (mode == MODE_QUERY) ? 0 : (iva() == nessuna_iva ? 1 : 2); - return load_mask(m); -} - - -void TPrimanota_application::init_mask(TMask& m) -{ - fill_sheet(m); - - // Show/Hide campi valuta: F_VALUTA, F_CAMBIO, F_VISVAL (GROUP 3) - m.send_key(K_SHIFT+K_CTRL+(_causale.valuta() ? 's' : 'h'), -3); - m.set(F_VISVAL, _causale.valuta() ? "X" : " "); - - if (_iva != nessuna_iva) - { -/* - TString16 fil; fil = "I0="; fil << (_iva == iva_acquisti ? '2' : '1'); - TEdit_field& f = (TEdit_field&)m.field(F_CODREG); - f.browse()->cursor()->filter(fil); -*/ - const int g = (_iva == iva_acquisti) ? 2 : 1; - m.send_key(K_SHIFT+K_CTRL+'s', -g); // if iva_vendite show group 1 - m.send_key(K_SHIFT+K_CTRL+'h', g-3); // hide group 2 - - const bool intra = _causale.intra(); - m.show(F_CORRLIRE, intra); - - const bool corrval = intra && _causale.corrval(); - m.show(F_CORRVALUTA, corrval); - } - - m.disable(DLG_NEWREC); -} - - -void TPrimanota_application::init_query_mode(TMask& m) -{ - m.enable(DLG_NEWREC); -} - - -void TPrimanota_application::fill_sheet(TMask& m) const -{ - TSheet_field& cgs = (TSheet_field&)m.field(F_SHEETCG); - for (int r = cgs.items(); r < 16; r++) - cgs.row(r); - - if (_iva != nessuna_iva) - { - TSheet_field& ivas = (TSheet_field&)m.field(F_SHEETIVA); - for (r = ivas.items(); r < 16; r++) ivas.row(r); - ivas.enable_column(4, _iva == iva_acquisti); - } -} - - -// Ritorna il prossimo numero di registrazione libero -// Certified 100% -const char* TPrimanota_application::get_next_key() -{ - return format("%d|%ld", F_NUMREG, _lastreg+1); -} - - -void TPrimanota_application::init_insert_mode(TMask& m) -{ - m.set(F_DATAREG, _msk[0]->get(F_DATAREG)); - m.set(F_DATACOMP, _msk[0]->get(F_DATACOMP)); - m.set(F_ANNOES, _msk[0]->get(F_ANNOES)); - m.set(F_ANNOIVA, _msk[0]->get(F_ANNOIVA)); - occas_mask().reset(); - - init_mask(m); - _saldi.reset(); // Inizializza saldi - _saldi.set_movprovv(_rel->lfile()->get_char("PROVVIS") > ' ' ? TRUE : FALSE); - _saldi.set_movap(_causale.apertura()); - _saldi.set_anno_es(m.get_int(F_ANNOES)); - _saldi.set_num_ulmov(m.get_long(F_NUMREG)); - _saldi.set_data_ulmov(TDate(m.get(F_DATAREG))); - - if (_iva != nessuna_iva) - { - const long protiva = causale().reg().protocol(); - m.set(F_PROTIVA, protiva+1); - - TMask_field& vv = m.field(F_VISVAL); - if (vv.active() && vv.get() == "X") // Se e' un movimento in valuta - { - const bool ok = m.field(F_VALUTA).on_key(K_F9); // Richiedi valuta - vv.set(ok ? "X" : " "); - if (ok) vv.on_hit(); // Cambia decimali - } - } - - if (m.get(F_CODCAUS).empty()) return; - - TString80 desc; - for (int i = 1; i < _causale.size(); i++) - { - TRectype* rcaus = (TRectype*)_causale.objptr(i); - if (rcaus == NULL) continue; // Evita eventuali righe nulle - - const int nriga = rcaus->get_int(RCA_NRIGA); - if (nriga < 1) continue; // Considera solo righe reali (non riempimenti) - - TConto tc; _causale.bill(nriga, tc); - if (tc.conto() < 1) continue; // Considera solo conti validi - - desc = rcaus->get(RCA_DESC); - - if (iva() == nessuna_iva) - { - set_cgs_row(-1,ZERO,tc,desc,' '); - } - else - { - if (nriga == 3 || nriga == 4 || nriga == 8 || nriga == 9) - continue; // Conti per IVA detraibile e non, ritenute sociali e fiscali - - const bool rigaiva = rcaus->get_bool(RCA_RIGAIVA); - if (nriga == 2 || (nriga >= 5 && nriga <= 7) || rigaiva) - { - const TString16 codiva(rcaus->get(RCA_CODIVA)); - set_ivas_row(-1,codiva,tc,desc); - } - else - { - const char tipo = nriga < 2 ? 'T' : ' '; - set_cgs_row(-1,ZERO,tc,desc,tipo); - - if (nriga == 1 && tc.tipo() > ' ' && tc.sottoconto() > 0) - m.set(tc.tipo() == 'C' ? F_CLIENTE : F_FORNITORE, tc.sottoconto()); - } - } - } -} - - -void TPrimanota_application::init_modify_mode(TMask& m) -{ - init_mask(m); -} - - -bool TPrimanota_application::get_conto(int r, TConto& c) const -{ - const TRectype& cg = _rel->cg(r); - c.set(cg.get_int("GRUPPO"), cg.get_int("CONTO"), - cg.get_long("SOTTOCONTO"), cg.get_char("TIPOC")); - return c.ok(); -} - - -int TPrimanota_application::read(TMask& m) -{ - m.autoload(_rel); - - if (_iva != nessuna_iva) - { - const TString16 occode(_rel->lfile()->get("OCFPI")); - occas_mask().set(O_CODICE, occode); - - const char clifo = toupper(m.get(F_CLIFO)[0]); - - if (_iva == iva_acquisti && clifo == 'C') - error_box("Registrazione di acquisto attribuita ad un cliente"); else - if (_iva == iva_vendite && clifo == 'F') - error_box("Registrazione di vendita attribuita ad un fornitore"); - - if (_iva == iva_vendite) - { - TEdit_field& f = (TEdit_field&)m.field(F_CLIENTE); - f.check_type(clifo == 'C' ? CHECK_REQUIRED : CHECK_NORMAL); - } - } - - TSheet_field& cgs = (TSheet_field&)m.field(F_SHEETCG); - cgs.reset(); - - _saldi.reset(); // Azzera saldi - _saldi.set_movprovv(_rel->lfile()->get_char("PROVVIS") > ' ' ? TRUE : FALSE); - _saldi.set_movap(_causale.apertura()); - _saldi.set_anno_es(m.get_int(F_ANNOES)); - _saldi.set_num_ulmov(m.get_long(F_NUMREG)); - _saldi.set_data_ulmov((TDate)m.get(F_DATAREG)); - - TToken_string riga(180); - - for (int i = 0; i < _rel->cg_items(); i++) - { - TRectype& r = _rel->cg(i); - riga.cut(0); // Vuota la riga - - const real im(r.get_real("IMPORTO")); - const char sezione = toupper(r.get_char("SEZIONE")); - const int rcontr = r.get_int("RCONTR") -1; -#ifdef DBG - if (rcontr < -1 || rcontr >= _rel->cg_items() || rcontr == i) - error_box("La riga %d non puo' avere per contropartita la %d", i+1, rcontr+1); -#endif - const bool dare = sezione == 'D'; - riga.add(dare ? im.string() : " "); // Dare 101 - riga.add(dare ? " " : im.string()); // Avere 102 - - TConto conto; get_conto(i, conto); - riga.add(conto.string(0x3)); // Conto 103-107 - if (conto.ok()) - _saldi.aggiorna(conto, im, sezione, FALSE); - - riga.add(""); // Codice descrizione 108 - riga.add(r.get("DESCR")); // Descrizione riga 109 - - if (rcontr >= 0 && rcontr < _rel->cg_items()) - { - get_conto(rcontr, conto); - riga.add(conto.string(0x3)); // Contropartita 110-114 - } - else riga.add(" | | | | "); - - const char* tipo = r.get("ROWTYPE"); // Tipo di riga 115 - riga.add(tipo); - - cgs.row(i) = riga; - disable_cgs_cells(i, *tipo); - } - - if (_iva == nessuna_iva) - return _rel->status(); - - const bool solaiva = i == 0; - m.set(F_SOLAIVA, solaiva ? "X" : " "); - m.field(F_SOLAIVA).on_hit(); - - TSheet_field& ivas = (TSheet_field&)m.field(F_SHEETIVA); - ivas.reset(); - - for (i = 0; i < _rel->iva_items(); i++) - { - TRectype& r = _rel->iva(i); - riga.cut(0); - - riga.add(r.get("IMPONIBILE")); // Imponibile 101 - riga.add(r.get("CODIVA")); // IVA 102 - riga.add(r.get("IMPOSTA")); // Imposta 103 - riga.add(r.get("TIPOCR")); // C/R 104 - riga.add(r.get("TIPODET")); // Detrazione 105 - - int rigaimp = solaiva ? 0 : r.get_int("RIGAIMP"); - if (rigaimp > 0) - { - const int r = rigaimp-1; - if (r < cgs.items()) - { - TToken_string& s = cgs.row(r); - riga.add(s.get(2)); // Tipo conto 106 - for (int i = 0; i < 4; i++) - riga.add(s.get()); // Gruppo|Conto|Sottoconto|Descrizione - } - else - { - error_box("Riga imponibile %d errata nella riga iva %d", rigaimp, i+1); - rigaimp = 0; - } - } - if (rigaimp < 1) riga.add(" | | | | "); - ivas.row(i) = riga; - } - - TMask_field& vv = m.field(F_VISVAL); - if (vv.active()) vv.on_hit(); - - return _rel->status(); -} - - - -void TPrimanota_application::mask2rel(const TMask& m) -{ - _rel->lfile()->zero(); - m.autosave(_rel); - - const long numreg = m.get_long(F_NUMREG); - const TDate datareg(m.get(F_DATAREG)); - const int annoes = (int) m.get_long(F_ANNOES); - - _saldi.set_movprovv(m.get_bool(F_PROVVISORIO)); - _saldi.set_movap(_causale.apertura()); - _saldi.set_anno_es(annoes); - - _rel->destroy_rows(); // Destroy all records - cgs_pack(); // Destroy all null rows - - TArray& rows = cgs().rows_array(); - int cur = 1; - - // 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]; - - TRectype &r = _rel->cg(cur-1); - r.zero(); - r.put("NUMREG", numreg); - r.put("ANNOES", annoes); - r.put("DATAREG", datareg); - r.put("NUMRIG", cur); - - char sezione = 'D'; - - const char* n = row.get(0); - real val0(n); // Dare - n = row.get(); - real val1(n); // Avere - - if (val0.is_zero()) - { - sezione = 'A'; - val0 = val1; - } - - r.put("IMPORTO", val0); // Importo - r.put("SEZIONE", sezione); // Sezione - - const TConto conto(row, -1, 0x3); - if (conto.ok()) - _saldi.aggiorna(conto, val0, sezione, TRUE); - - r.put("TIPOC", conto.tipo()); // Conto - r.put("GRUPPO", conto.gruppo()); - r.put("CONTO", conto.conto()); - r.put("SOTTOCONTO", conto.sottoconto()); - - row.get(); // Codice descrizione - r.put("DESCR", row.get()); // Descrizione riga - - int rcontr = 0; // Riga contropartita - const TConto contro(row, -1, 0x3); // Conto contropartita - if (contro.ok()) - { - rcontr = bill2contr(contro, sezione)+1; - if (rcontr < 1) - { - warning_box("La riga contabile %d non ha una contropartita", cur); - rcontr = 0; - } - } - r.put("RCONTR", rcontr); // Contropartita - r.put("ROWTYPE", row.get()); - cur++; - } - - if (_iva == nessuna_iva) return; - - int err = ~NOERR; - if (m.get_bool(F_OCCASIONALE)) - { - TRelation occas(LF_OCCAS); - const TMask& om = occas_mask(); - om.autosave(&occas); - - err = occas.write(); - if (err == _isreinsert) - err = occas.rewrite(); - - if (err == NOERR) - _rel->lfile()->put("OCFPI", occas_mask().get(O_CODICE)); - else - error_box("Errore di scrittura sul file dei clienti/fornitori occasionali: %d", err); - } - if (err) _rel->lfile()->zero("OCFPI"); - - TArray& irows = ivas().rows_array(); - cur = 1; - for (int i = 0; i < irows.items(); i++) - { - TToken_string& row = (TToken_string&)irows[i]; - if (row.empty_items()) - continue; - - TRectype &r = _rel->iva(cur-1); - r.zero(); - r.put("ANNOES", annoes); - r.put("NUMREG", numreg); - r.put("NUMRIG", cur); - - const char* imponibile = row.get(0); - r.put("IMPONIBILE", imponibile); - r.put("CODIVA", row.get()); - - const char* imposta = row.get(); - r.put("IMPOSTA", imposta); - - r.put("TIPOCR", row.get()); - r.put("TIPODET", row.get()); - - const TConto c(row, -1, 0x1); - const int rimp = bill2pos(c, 'I')+1; - r.put("RIGAIMP", rimp); - cur++; - } - - // Resetta il flag di calcolato sulla liquidazione IVA del mese di registrazione - const TDate d(m.get(F_DATAREG)); - TString16 chiave; chiave << d.year() << d.month(); - TTable lim("LIM"); - lim.put("CODTAB", chiave); - if (lim.read() == NOERR) - { - const bool calcolato = lim.get_bool("B0"); - if (calcolato) - { - lim.put("B0", FALSE); - lim.rewrite(); - } - } -} - - -int TPrimanota_application::write(const TMask& m) -{ - const long numreg = m.get_long(F_NUMREG); - if (numreg > _lastreg) _lastreg = numreg; - - mask2rel(m); - const int err = _rel->write(TRUE); - if (err == NOERR) - { - _saldi.registra(); - const TString16 causimm(_causale.causale_inc_imm()); - if (causimm.not_empty()) - genera_incasso(causimm); - } - return err; -} - - -int TPrimanota_application::rewrite(const TMask& m) -{ - mask2rel(m); - const int err = _rel->rewrite(TRUE); - if (err == NOERR) - _saldi.registra(); - return err; -} - - -bool TPrimanota_application::remove() -{ - const bool ok = TRelation_application::remove(); - if (ok) - _saldi.registra(); - return ok; -} - - -void TPrimanota_application::genera_incasso(const char* causimm) -{ - TMask m("cg2100i"); - m.set_handler(F_DATAREG, datareg_handler); - m.set_handler(F_DATACOMP, datacomp_handler); - - TFilename tmp; tmp.temp(); // File temporaneo di scambio - _msk[2]->set_workfile(tmp); - _msk[2]->save(); // Copia la maschera principale ... - m.set_workfile(tmp); - m.load(); // ... nella maschera di incasso - ::remove(tmp); // Cancella file temporaneo - - const TCausale caus(causimm); // Causale da usare - m.set(F_NUMREG, ++_lastreg); // Incrementa numero di registrazione - m.set(F_CODCAUS, causimm); // Cambia causale e ... - m.set(F_TIPODOC, caus.tipo_doc()); // relativo tipo documento - - if (m.run() == K_SAVE) - { - TMovimentoPN inc; // Nuovo movimento di incasso immediato - - TRectype r(_rel->cg(0)); // Copia la prima riga contabile - m.autosave(&inc); - - inc.lfile()->put("REG", ""); // Annulla eventuale registro IVA - - const real imp(m.get(F_TOTALE)); - const char sez = r.get_char("SEZIONE"); - - r.put("NUMREG", _lastreg); - r.put("NUMRIG", 1); - r.put("IMPORTO", imp); - r.put("ROWTYPE", ' '); - r.put("SEZIONE", sez == 'A' ? 'D' : 'A'); - inc.cg(0) = r; - - TConto conto; caus.bill(2, conto); // Conto della seconda riga della causale - - r.put("TIPOC", conto.tipo()); - r.put("GRUPPO", conto.gruppo()); - r.put("CONTO", conto.conto()); - r.put("SOTTOCONTO", conto.sottoconto()); - - r.put("NUMRIG", 2); - r.put("SEZIONE", sez); - inc.cg(1) = r; - - while (inc.write() == _isreinsert) // In caso di riscrittura - inc.lfile()->put("NUMREG", ++_lastreg); // Incrementa numero registrazione - } -} - - -int cg2100 (int argc, char** argv) -{ - TPrimanota_application a; - a.run(argc, argv, "Prima nota"); - return 0; -} +#include +#include +#include +#include + +#include "cg2100.h" +#include "cg2102.h" + +// Nomi dei campi +#include +#include +#include + + +TPrimanota_application::TPrimanota_application() + : _rel(NULL), _lastreg(0), _mode(0), _iva(nessuna_iva) +{ + memset(_msk, 0, sizeof(_msk)); +} + + +TMask* TPrimanota_application::load_mask(int n) +{ + if (_msk[n] != NULL) + return _msk[n]; + + TFilename name("cg2100"); name << char(n == 3 ? 'o' : 'a'+n); + TMask* m = new TMask(name); + + switch (n) + { + case 0: + m->set_handler(F_NUMREG, num_handler); + m->set_handler(F_CODCAUS, caus_query_handler); + m->set_handler(F_DATAREG, datareg_handler); + m->set_handler(F_DATACOMP, datacomp_handler); + { + TConfig c(CONFIG_STUDIO, "cg"); + const bool dr = c.get_bool("PoCuDr"); // Scelta primo campo col focus + m->first_focus(dr ? F_DATAREG : F_CODCAUS); + } + break; + case 1: + m->set_handler(F_VISVAL, visval_handler); + m->set_handler(F_DESCR, descr_handler); + break; + case 2: + m->set_handler(F_CODREG, reg_handler); + m->set_handler(F_CLIENTE, clifo_handler); + m->set_handler(F_FORNITORE, clifo_handler); + m->set_handler(F_TOTALE, totale_handler); + m->set_handler(F_RITFIS, ritfis_handler); + m->set_handler(F_RITSOC, ritsoc_handler); + m->set_handler(F_VISVAL, visval_handler); + m->set_handler(F_OCCASEDIT, occas_handler); + m->set_handler(F_SOLAIVA, solaiva_handler); + m->set_handler(F_SHEETIVA, iva_handler); + { + TSheet_field& is = (TSheet_field&)m->field(F_SHEETIVA); + is.set_notify(iva_notify); + is.sheet_mask().set_handler(101, imponibile_handler); + is.sheet_mask().set_handler(102, codiva_handler); + is.sheet_mask().set_handler(103, imposta_handler); + is.sheet_mask().set_handler(108, conto_handler); + is.sheet_mask().set_handler(109, suspended_handler); + is.sheet_mask().set_handler(209, suspended_handler); + is.sheet_mask().set_handler(309, suspended_handler); + } + break; + case 3: + m->set_handler(O_CODICE, occas_code_handler); + break; + default: + CHECKD(0, "Che cavolo di maschera e' la ", n); + break; + } + if (n == 1 || n == 2) + { + m->set_handler(F_DATAREG, datareg_handler); + m->set_handler(F_DATACOMP, datacomp_handler); + m->set_handler(F_CODCAUS, caus_modify_handler); + m->set_handler(F_SHEETCG, cg_handler); + { + TSheet_field& cg = (TSheet_field&)m->field(F_SHEETCG); + cg.set_notify(cg_notify); + TMask& cgm = cg.sheet_mask(); + cgm.set_handler(101, dareavere_handler); + cgm.set_handler(102, dareavere_handler); + cgm.set_handler(105, suspended_handler); + cgm.set_handler(106, suspended_handler); + cgm.set_handler(206, suspended_handler); + cgm.set_handler(306, suspended_handler); + cgm.set_handler(112, suspended_handler); + cgm.set_handler(113, suspended_handler); + cgm.set_handler(213, suspended_handler); + cgm.set_handler(313, suspended_handler); + } + } + + return _msk[n] = m; +} + + +bool TPrimanota_application::user_create() +{ + _tabcom = new TLocalisamfile(LF_TABCOM); + _tab = new TLocalisamfile(LF_TAB); + _caus = new TLocalisamfile(LF_CAUSALI); + _rcaus = new TLocalisamfile(LF_RCAUSALI); + _clifo = new TLocalisamfile(LF_CLIFO); + _pcon = new TLocalisamfile(LF_PCON); + _attiv = new TLocalisamfile(LF_ATTIV); + _saldo = new TLocalisamfile(LF_SALDI); + _comuni = new TLocalisamfile(LF_COMUNI); + _occas = new TLocalisamfile(LF_OCCAS); + _nditte = new TLocalisamfile(LF_NDITTE); + + _rel = new TMovimentoPN; + _rel->lfile()->last(); + _lastreg = _rel->lfile()->get_long(MOV_NUMREG); // Init last registration number + + set_search_field(F_NUMREG); // Set field for default search + + return TRUE; +} + + +bool TPrimanota_application::user_destroy() +{ + if (_msk[3] != NULL) delete _msk[3]; + if (_msk[2] != NULL) delete _msk[2]; + if (_msk[1] != NULL) delete _msk[1]; + if (_msk[0] != NULL) delete _msk[0]; + + delete _rel; + delete _nditte; + delete _occas; + delete _comuni; + delete _saldo; + delete _attiv; + delete _pcon; + delete _clifo; + delete _rcaus; + delete _caus; + delete _tab; + delete _tabcom; + return TRUE; +} + +// Legge la causale di codice cod ed il relativo registro dell'anno year +// Certified 99% +bool TPrimanota_application::read_caus(const char* cod, int year) +{ + const bool ok = causale().read(cod, year); + if (ok) + { + TMask& m = *load_mask(_iva == nessuna_iva ? 1 : 2); + + TEdit_field& nd = (TEdit_field&)m.field(F_NUMDOC); // Numero documento obbligatorio? + nd.check_type(_causale.num_doc() ? CHECK_REQUIRED : CHECK_NORMAL); + + TEdit_field& dd = (TEdit_field&)m.field(F_DATADOC); // Data documento obbligatoria? + dd.check_type(_causale.data_doc() ? CHECK_REQUIRED : CHECK_NORMAL); + } + return ok; +} + + +bool TPrimanota_application::changing_mask(int mode) +{ + if (mode == MODE_QUERY) + return _mode != MODE_QUERY; + + TipoIVA oliva = _iva; + + _iva = nessuna_iva; + + TString16 causale; +// TString16 registro; + int annoes = 0, annoiva = 0; + + switch (mode) + { + case MODE_INS: + annoes = _msk[0]->get_int(F_ANNOES); + annoiva = _msk[0]->get_int(F_ANNOIVA); + causale = _msk[0]->get(F_CODCAUS); + read_caus(causale, annoiva); + break; + case MODE_MOD: + annoes = _rel->lfile()->get_int("ANNOES"); + annoiva = _rel->lfile()->get_int("ANNOIVA"); + causale = _rel->lfile()->get("CODCAUS"); + giornale().read(annoes); +// registro = _rel->lfile()->get("REG"); +// _causale.read(causale, 0); +// _causale.reg().read(registro, annoiva); + read_caus(causale, annoiva); + break; + default: + break; + } + _iva = _causale.iva(); + + return _mode == MODE_QUERY || _iva != oliva; +} + + +TMask* TPrimanota_application::get_mask(int mode) +{ + _mode = mode; + const int m = (mode == MODE_QUERY) ? 0 : (iva() == nessuna_iva ? 1 : 2); + return load_mask(m); +} + + +void TPrimanota_application::init_mask(TMask& m) +{ + fill_sheet(m); + + // Show/Hide campi valuta: F_VALUTA, F_CAMBIO, F_VISVAL (GROUP 3) + m.send_key(K_SHIFT+K_CTRL+(_causale.valuta() ? 's' : 'h'), -3); + m.set(F_VISVAL, _causale.valuta() ? "X" : " "); + + if (_iva != nessuna_iva) + { +/* + TString16 fil; fil = "I0="; fil << (_iva == iva_acquisti ? '2' : '1'); + TEdit_field& f = (TEdit_field&)m.field(F_CODREG); + f.browse()->cursor()->filter(fil); +*/ + const int g = (_iva == iva_acquisti) ? 2 : 1; + m.send_key(K_SHIFT+K_CTRL+'s', -g); // if iva_vendite show group 1 + m.send_key(K_SHIFT+K_CTRL+'h', g-3); // hide group 2 + + const bool intra = _causale.intra(); + m.show(F_CORRLIRE, intra); + + const bool corrval = intra && _causale.corrval(); + m.show(F_CORRVALUTA, corrval); + } + + m.disable(DLG_NEWREC); +} + + +void TPrimanota_application::init_query_mode(TMask& m) +{ + m.enable(DLG_NEWREC); +} + + +void TPrimanota_application::fill_sheet(TMask& m) const +{ + TSheet_field& cgs = (TSheet_field&)m.field(F_SHEETCG); + for (int r = cgs.items(); r < 16; r++) + cgs.row(r); + + if (_iva != nessuna_iva) + { + TSheet_field& ivas = (TSheet_field&)m.field(F_SHEETIVA); + for (r = ivas.items(); r < 16; r++) ivas.row(r); + ivas.enable_column(4, _iva == iva_acquisti); + } +} + + +// Ritorna il prossimo numero di registrazione libero +// Certified 100% +const char* TPrimanota_application::get_next_key() +{ + return format("%d|%ld", F_NUMREG, _lastreg+1); +} + + +void TPrimanota_application::init_insert_mode(TMask& m) +{ + m.set(F_DATAREG, _msk[0]->get(F_DATAREG)); + m.set(F_DATACOMP, _msk[0]->get(F_DATACOMP)); + m.set(F_ANNOES, _msk[0]->get(F_ANNOES)); + m.set(F_ANNOIVA, _msk[0]->get(F_ANNOIVA)); + occas_mask().reset(); + + init_mask(m); + _saldi.reset(); // Inizializza saldi + _saldi.set_movprovv(_rel->lfile()->get_char("PROVVIS") > ' ' ? TRUE : FALSE); + _saldi.set_movap(_causale.apertura()); + _saldi.set_anno_es(m.get_int(F_ANNOES)); + _saldi.set_num_ulmov(m.get_long(F_NUMREG)); + _saldi.set_data_ulmov(TDate(m.get(F_DATAREG))); + + if (_iva != nessuna_iva) + { + const long protiva = causale().reg().protocol(); + m.set(F_PROTIVA, protiva+1); + + TMask_field& vv = m.field(F_VISVAL); + if (vv.active() && vv.get() == "X") // Se e' un movimento in valuta + { + const bool ok = m.field(F_VALUTA).on_key(K_F9); // Richiedi valuta + vv.set(ok ? "X" : " "); + if (ok) vv.on_hit(); // Cambia decimali + } + } + + if (m.get(F_CODCAUS).empty()) return; + + TString80 desc; + for (int i = 1; i < _causale.size(); i++) + { + TRectype* rcaus = (TRectype*)_causale.objptr(i); + if (rcaus == NULL) continue; // Evita eventuali righe nulle + + const int nriga = rcaus->get_int(RCA_NRIGA); + if (nriga < 1) continue; // Considera solo righe reali (non riempimenti) + + TConto tc; _causale.bill(nriga, tc); + if (tc.conto() < 1) continue; // Considera solo conti validi + + desc = rcaus->get(RCA_DESC); + + if (iva() == nessuna_iva) + { + set_cgs_row(-1,ZERO,tc,desc,' '); + } + else + { + if (nriga == 3 || nriga == 4 || nriga == 8 || nriga == 9) + continue; // Conti per IVA detraibile e non, ritenute sociali e fiscali + + const bool rigaiva = rcaus->get_bool(RCA_RIGAIVA); + if (nriga == 2 || (nriga >= 5 && nriga <= 7) || rigaiva) + { + const TString16 codiva(rcaus->get(RCA_CODIVA)); + set_ivas_row(-1,codiva,tc,desc); + } + else + { + const char tipo = nriga < 2 ? 'T' : ' '; + set_cgs_row(-1,ZERO,tc,desc,tipo); + + if (nriga == 1 && tc.tipo() > ' ' && tc.sottoconto() > 0) + m.set(tc.tipo() == 'C' ? F_CLIENTE : F_FORNITORE, tc.sottoconto()); + } + } + } +} + + +void TPrimanota_application::init_modify_mode(TMask& m) +{ + init_mask(m); +} + + +bool TPrimanota_application::get_conto(int r, TConto& c) const +{ + const TRectype& cg = _rel->cg(r); + c.set(cg.get_int("GRUPPO"), cg.get_int("CONTO"), + cg.get_long("SOTTOCONTO"), cg.get_char("TIPOC")); + return c.ok(); +} + + +int TPrimanota_application::read(TMask& m) +{ + m.autoload(_rel); + + if (_iva != nessuna_iva) + { + const TString16 occode(_rel->lfile()->get("OCFPI")); + occas_mask().set(O_CODICE, occode); + + const char clifo = toupper(m.get(F_CLIFO)[0]); + + if (_iva == iva_acquisti && clifo == 'C') + error_box("Registrazione di acquisto attribuita ad un cliente"); else + if (_iva == iva_vendite && clifo == 'F') + error_box("Registrazione di vendita attribuita ad un fornitore"); + + if (_iva == iva_vendite) + { + TEdit_field& f = (TEdit_field&)m.field(F_CLIENTE); + f.check_type(clifo == 'C' ? CHECK_REQUIRED : CHECK_NORMAL); + } + } + + TSheet_field& cgs = (TSheet_field&)m.field(F_SHEETCG); + cgs.reset(); + + _saldi.reset(); // Azzera saldi + _saldi.set_movprovv(_rel->lfile()->get_char("PROVVIS") > ' ' ? TRUE : FALSE); + _saldi.set_movap(_causale.apertura()); + _saldi.set_anno_es(m.get_int(F_ANNOES)); + _saldi.set_num_ulmov(m.get_long(F_NUMREG)); + _saldi.set_data_ulmov((TDate)m.get(F_DATAREG)); + + TToken_string riga(180); + + for (int i = 0; i < _rel->cg_items(); i++) + { + TRectype& r = _rel->cg(i); + riga.cut(0); // Vuota la riga + + const real im(r.get_real("IMPORTO")); + const char sezione = toupper(r.get_char("SEZIONE")); + const int rcontr = r.get_int("RCONTR") -1; +#ifdef DBG + if (rcontr < -1 || rcontr >= _rel->cg_items() || rcontr == i) + error_box("La riga %d non puo' avere per contropartita la %d", i+1, rcontr+1); +#endif + const bool dare = sezione == 'D'; + riga.add(dare ? im.string() : " "); // Dare 101 + riga.add(dare ? " " : im.string()); // Avere 102 + + TConto conto; get_conto(i, conto); + riga.add(conto.string(0x3)); // Conto 103-107 + if (conto.ok()) + _saldi.aggiorna(conto, im, sezione, FALSE); + + riga.add(""); // Codice descrizione 108 + riga.add(r.get("DESCR")); // Descrizione riga 109 + + if (rcontr >= 0 && rcontr < _rel->cg_items()) + { + get_conto(rcontr, conto); + riga.add(conto.string(0x3)); // Contropartita 110-114 + } + else riga.add(" | | | | "); + + const char* tipo = r.get("ROWTYPE"); // Tipo di riga 115 + riga.add(tipo); + + cgs.row(i) = riga; + disable_cgs_cells(i, *tipo); + } + + if (_iva == nessuna_iva) + return _rel->status(); + + const bool solaiva = i == 0; + m.set(F_SOLAIVA, solaiva ? "X" : " "); + m.field(F_SOLAIVA).on_hit(); + + TSheet_field& ivas = (TSheet_field&)m.field(F_SHEETIVA); + ivas.reset(); + + for (i = 0; i < _rel->iva_items(); i++) + { + TRectype& r = _rel->iva(i); + riga.cut(0); + + riga.add(r.get("IMPONIBILE")); // Imponibile 101 + riga.add(r.get("CODIVA")); // IVA 102 + riga.add(r.get("IMPOSTA")); // Imposta 103 + riga.add(r.get("TIPOCR")); // C/R 104 + riga.add(r.get("TIPODET")); // Detrazione 105 + + int rigaimp = solaiva ? 0 : r.get_int("RIGAIMP"); + if (rigaimp > 0) + { + const int r = rigaimp-1; + if (r < cgs.items()) + { + TToken_string& s = cgs.row(r); + riga.add(s.get(2)); // Tipo conto 106 + for (int i = 0; i < 4; i++) + riga.add(s.get()); // Gruppo|Conto|Sottoconto|Descrizione + } + else + { + error_box("Riga imponibile %d errata nella riga iva %d", rigaimp, i+1); + rigaimp = 0; + } + } + if (rigaimp < 1) riga.add(" | | | | "); + ivas.row(i) = riga; + } + + TMask_field& vv = m.field(F_VISVAL); + if (vv.active()) vv.on_hit(); + + return _rel->status(); +} + + + +void TPrimanota_application::mask2rel(const TMask& m) +{ + _rel->lfile()->zero(); + m.autosave(_rel); + + const long numreg = m.get_long(F_NUMREG); + const TDate datareg(m.get(F_DATAREG)); + const int annoes = (int) m.get_long(F_ANNOES); + + _saldi.set_movprovv(m.get_bool(F_PROVVISORIO)); + _saldi.set_movap(_causale.apertura()); + _saldi.set_anno_es(annoes); + + _rel->destroy_rows(); // Destroy all records + cgs_pack(); // Destroy all null rows + + TArray& rows = cgs().rows_array(); + int cur = 1; + + // 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]; + + TRectype &r = _rel->cg(cur-1); + r.zero(); + r.put("NUMREG", numreg); + r.put("ANNOES", annoes); + r.put("DATAREG", datareg); + r.put("NUMRIG", cur); + + char sezione = 'D'; + + const char* n = row.get(0); + real val0(n); // Dare + n = row.get(); + real val1(n); // Avere + + if (val0.is_zero()) + { + sezione = 'A'; + val0 = val1; + } + + r.put("IMPORTO", val0); // Importo + r.put("SEZIONE", sezione); // Sezione + + const TConto conto(row, -1, 0x3); + if (conto.ok()) + _saldi.aggiorna(conto, val0, sezione, TRUE); + + r.put("TIPOC", conto.tipo()); // Conto + r.put("GRUPPO", conto.gruppo()); + r.put("CONTO", conto.conto()); + r.put("SOTTOCONTO", conto.sottoconto()); + + row.get(); // Codice descrizione + r.put("DESCR", row.get()); // Descrizione riga + + int rcontr = 0; // Riga contropartita + const TConto contro(row, -1, 0x3); // Conto contropartita + if (contro.ok()) + { + rcontr = bill2contr(contro, sezione)+1; + if (rcontr < 1) + { + warning_box("La riga contabile %d non ha una contropartita", cur); + rcontr = 0; + } + } + r.put("RCONTR", rcontr); // Contropartita + r.put("ROWTYPE", row.get()); + cur++; + } + + if (_iva == nessuna_iva) return; + + int err = ~NOERR; + if (m.get_bool(F_OCCASIONALE)) + { + TRelation occas(LF_OCCAS); + const TMask& om = occas_mask(); + om.autosave(&occas); + + err = occas.write(); + if (err == _isreinsert) + err = occas.rewrite(); + + if (err == NOERR) + _rel->lfile()->put("OCFPI", occas_mask().get(O_CODICE)); + else + error_box("Errore di scrittura sul file dei clienti/fornitori occasionali: %d", err); + } + if (err) _rel->lfile()->zero("OCFPI"); + + TArray& irows = ivas().rows_array(); + cur = 1; + for (int i = 0; i < irows.items(); i++) + { + TToken_string& row = (TToken_string&)irows[i]; + if (row.empty_items()) + continue; + + TRectype &r = _rel->iva(cur-1); + r.zero(); + r.put("ANNOES", annoes); + r.put("NUMREG", numreg); + r.put("NUMRIG", cur); + + const char* imponibile = row.get(0); + r.put("IMPONIBILE", imponibile); + r.put("CODIVA", row.get()); + + const char* imposta = row.get(); + r.put("IMPOSTA", imposta); + + r.put("TIPOCR", row.get()); + r.put("TIPODET", row.get()); + + const TConto c(row, -1, 0x1); + const int rimp = bill2pos(c, 'I')+1; + r.put("RIGAIMP", rimp); + cur++; + } + + // Resetta il flag di calcolato sulla liquidazione IVA del mese di registrazione + const TDate d(m.get(F_DATAREG)); + TString16 chiave; chiave << d.year() << d.month(); + TTable lim("LIM"); + lim.put("CODTAB", chiave); + if (lim.read() == NOERR) + { + const bool calcolato = lim.get_bool("B0"); + if (calcolato) + { + lim.put("B0", FALSE); + lim.rewrite(); + } + } +} + + +int TPrimanota_application::write(const TMask& m) +{ + const long numreg = m.get_long(F_NUMREG); + if (numreg > _lastreg) _lastreg = numreg; + + mask2rel(m); + const int err = _rel->write(TRUE); + if (err == NOERR) + { + _saldi.registra(); + const TString16 causimm(_causale.causale_inc_imm()); + if (causimm.not_empty()) + genera_incasso(causimm); + } + return err; +} + + +int TPrimanota_application::rewrite(const TMask& m) +{ + mask2rel(m); + const int err = _rel->rewrite(TRUE); + if (err == NOERR) + _saldi.registra(); + return err; +} + + +bool TPrimanota_application::remove() +{ + const bool ok = TRelation_application::remove(); + if (ok) + _saldi.registra(); + return ok; +} + + +void TPrimanota_application::genera_incasso(const char* causimm) +{ + TMask m("cg2100i"); + m.set_handler(F_DATAREG, datareg_handler); + m.set_handler(F_DATACOMP, datacomp_handler); + + TFilename tmp; tmp.temp(); // File temporaneo di scambio + _msk[2]->set_workfile(tmp); + _msk[2]->save(); // Copia la maschera principale ... + m.set_workfile(tmp); + m.load(); // ... nella maschera di incasso + ::remove(tmp); // Cancella file temporaneo + + const TCausale caus(causimm); // Causale da usare + m.set(F_NUMREG, ++_lastreg); // Incrementa numero di registrazione + m.set(F_CODCAUS, causimm); // Cambia causale e ... + m.set(F_TIPODOC, caus.tipo_doc()); // relativo tipo documento + + if (m.run() == K_SAVE) + { + TMovimentoPN inc; // Nuovo movimento di incasso immediato + + TRectype r(_rel->cg(0)); // Copia la prima riga contabile + m.autosave(&inc); + + inc.lfile()->put("REG", ""); // Annulla eventuale registro IVA + + const real imp(m.get(F_TOTALE)); + const char sez = r.get_char("SEZIONE"); + + r.put("NUMREG", _lastreg); + r.put("NUMRIG", 1); + r.put("IMPORTO", imp); + r.put("ROWTYPE", ' '); + r.put("SEZIONE", sez == 'A' ? 'D' : 'A'); + inc.cg(0) = r; + + TConto conto; caus.bill(2, conto); // Conto della seconda riga della causale + + r.put("TIPOC", conto.tipo()); + r.put("GRUPPO", conto.gruppo()); + r.put("CONTO", conto.conto()); + r.put("SOTTOCONTO", conto.sottoconto()); + + r.put("NUMRIG", 2); + r.put("SEZIONE", sez); + inc.cg(1) = r; + + while (inc.write() == _isreinsert) // In caso di riscrittura + inc.lfile()->put("NUMREG", ++_lastreg); // Incrementa numero registrazione + } +} + + +int cg2100 (int argc, char** argv) +{ + TPrimanota_application a; + a.run(argc, argv, "Prima nota"); + return 0; +} diff --git a/cg/cg2102.cpp b/cg/cg2102.cpp index 4050d31c7..0a829b436 100755 --- a/cg/cg2102.cpp +++ b/cg/cg2102.cpp @@ -1,1056 +1,1088 @@ -#include -#include -#include -#include - -#include "cg2100.h" -#include "cg2102.h" - - -// Certified 100% -inline TPrimanota_application& app() -{ return (TPrimanota_application&)*MainApp(); } - -/////////////////////////////////////////////////////////// -// Funzioni di decodifica/calcolo -/////////////////////////////////////////////////////////// - -// Determina il tipo IVA da causale+anno -// Certified 50% -TipoIVA TPrimanota_application::cau2IVA(const char* causale, int annoiva) -{ - TipoIVA i = iva_errata; - - if (*causale > ' ') - { - TCausale c; - if (c.read(causale, annoiva)) - i = c.iva(); - else - error_box("Causale errata: '%s'", causale); - } else i = nessuna_iva; - - return i; -} - -// Calcola l'anno di esercizio di una data -// Certified 99% -int TPrimanota_application::date2esc(const TDate& d, int* prevesc) -{ - if (prevesc) *prevesc = 0; - TTable esc("ESC"); - for (int err = esc.first(); err == NOERR; err = esc.next()) - { - const TDate ia(esc.get("D0")); // Data inizio esercizio - const TDate fa(esc.get("D1")); // Data fine esercizio - const anno = esc.get_int("CODTAB"); - if (d >= ia && d <= fa) - return anno; - if (prevesc) *prevesc = anno; - } - return 0; -} - -// Calcolo della percentuale di un dato codice IVA -// Certified 99% -const real& TPrimanota_application::cod2IVA(const char* codiva) -{ - static TString16 _codiva; - static real _percent; - - if (_codiva != codiva) - { - _codiva = codiva; - TCodiceIVA c(_codiva); - _percent = c.percentuale(); - } - - return _percent; -} - -real TPrimanota_application::scorpora(real& imponibile, const real& percent) -{ - real imposta = imponibile * percent / (percent + 100.0); imposta.ceil(); - imponibile -= imposta; - return imposta; -} - - -// Determina se un codice sospeso o no -// Certified 50% -bool TPrimanota_application::suspended_handler(TMask_field& f, KEY k) -{ - if (f.to_check(k)) - { - CHECKD(f.is_edit(), "Can't check suspension of a non edit-field ", f.dlg()); - const TEdit_field& c = (const TEdit_field&)f; - const TBrowse* b = c.browse(); - CHECKD(b, "Can't check suspension of a edit-field without a USE ", f.dlg()); - const TLocalisamfile* i = b->cursor()->file(); - - const char* sf = i->tab() ? "B2" : "SOSPESO"; - const bool suspended = i->get_bool(sf); - if (suspended) - { - sf = f.get(); - return f.error_box("Il codice '%s' e' sospeso e non puo' essere utilizzato", sf); - } - } - return TRUE; -} - - -// Determina se un codice detrazione e' di tipo detraibile o no -// Certified 70% -bool TPrimanota_application::detraibile(int tipodet) -{ - if (tipodet) - return FALSE; - if (app().iva() != iva_acquisti) - return TRUE; - - const TSheet_field& iva = app().ivas(); - - TString16 chiave; - chiave = iva.mask().get(F_ANNOIVA); - chiave << iva.mask().get(F_CODREG); - TTable reg("REG"); - reg.put("CODTAB", chiave); - reg.read(); -#ifdef DBG - if(reg.status() != NOERR) - error_box("La minchia che trovo il registro '%s'", (const char*)chiave); -#endif - - const char* att = reg.get("S8"); - chiave.cut(4); chiave << att; - - TTable pla("PLA"); - pla.put("CODTAB", chiave); - const int err = pla.read(); - real prorata; - if (err == NOERR) - prorata = pla.get_real("R8"); - - return prorata != 100.0; -} - -/////////////////////////////////////////////////////////// -// Funzioni di ricerca -/////////////////////////////////////////////////////////// - -int TPrimanota_application::type2pos(char tipo) -{ - TSheet_field& cg = app().cgs(); - for (int i = 0; i < cg.items(); i++) - { - TToken_string& s = cg.row(i); - if (s[s.len()-1] == tipo) - return i; - } - return -1; -} - - -int TPrimanota_application::bill2pos(const TConto& conto, char tipo) -{ - TSheet_field& cg = app().cgs(); - for (int i = 0; i < cg.items(); i++) - { - TToken_string& s = cg.row(i); - if (s[s.len()-1] == tipo) - { - const TConto c(s, 3, 0x0); - if (c == conto) - return i; - } - } - return -1; -} - - -int TPrimanota_application::bill2contr(const TConto& conto, char sezione) const -{ - const TArray& rows = cgs().rows_array(); - for (int i = 0; i < rows.items(); i++) - { - TToken_string& row = (TToken_string&)rows[i]; - const char sez = row.get(0)[0] > ' ' ? 'D' : 'A'; - if (sez == sezione) // Devo cercare sezione contraria - continue; - const TConto c(row, 3, 0x0); - if (conto == c) - return i; - } - return -1; -} - - -bool TPrimanota_application::bill_used(const TConto& conto) const -{ - const TArray& rows = ivas().rows_array(); - for (int i = 0; i < rows.items(); i++) - { - TToken_string& row = (TToken_string&)rows[i]; - const TConto c(row, 6, 0x0); - if (conto == c) - return TRUE; - } - return FALSE; -} - - -/////////////////////////////////////////////////////////// -// Gestione sheet CG -/////////////////////////////////////////////////////////// - -TMask& TPrimanota_application::mask() const -{ - TipoIVA i = app().iva(); - return *_msk[i == nessuna_iva ? 1 : 2]; -} - - -TSheet_field& TPrimanota_application::cgs() const -{ - TSheet_field& s = (TSheet_field&)mask().field(F_SHEETCG); - return s; -} - - -// Certified 99% -// Dato un importo stabilisce se deve andare in DARE o AVERE -// ritornandolo poi col segno opportuno + o - -real TPrimanota_application::imp2sez(const real& imp) -{ - const int dare = (imp >= 0.0) ^ (app().iva() == iva_vendite); - return abs(imp) * (dare ? +1.0 : -1.0); -} - - -// Certified 99% -// Scrive l'importo imp nella opportuna sezione della riga n -void TPrimanota_application::set_cgs_imp(int n, const real& imp) -{ - TToken_string& row = cgs().row(n); - const char* val = imp.string(); - if (*val == '-') - { - row.add(" ", 0); - row.add(val+1, 1); - } - else - { - row.add(val, 0); - row.add(" ", 1); - } - cgs().force_update(n); -} - - -// Legge l'importo della riga n e lo ritorna col segno dovuto -// Certified 99% -real TPrimanota_application::get_cgs_imp(int n) -{ - TToken_string& row = cgs().row(n); - const real dare(row.get(0)); - if (dare > 0.0) return dare; - const real avere(row.get()); - return -avere; -} - - -// Certified 90% -void TPrimanota_application::add_cgs_imp(int n, const real& imp) -{ - const real tot = get_cgs_imp(n) + imp; - set_cgs_imp(n, tot); -} - -// Disabilita le celle della riga contabile n in base al suo tipo -void TPrimanota_application::disable_cgs_cells(int n, char tipo) -{ - int last; - switch(tipo) - { - case 'F': // Ritenute Fiscali - case 'S': // Ritenute Sociali - case 'D': // IVA Detraibile - case 'N':last = 3; break; // IVA Non detraibile - case 'T': // Totale documento - case 'I':last = 6; break; // Imponibile - default :last = 0; break; // Solo contabile - } - - TSheet_field& cg = cgs(); - for (int i = 0; i < last; i++) - cg.disable_cell(n, i); - - if (tipo == 'T') - { - cg.enable_cell(n, 3); - cg.enable_cell(n, 4); - } -} - - -int TPrimanota_application::set_cgs_row(int n, const real& imp, - TConto& conto, const char* desc, - char tipo) -{ - TSheet_field& cg = cgs(); - if (n < 0) n = cg.first_empty(); - TToken_string& row = cg.row(n); - row = ""; - set_cgs_imp(n, imp); - row.add(conto.string(0x3)); - row.add(""); - row.add(desc); - - int pos = 0; - if (tipo == 'I' && (pos = type2pos('T')) >= 0) - { - TConto contro(cg.row(pos), 2, 0x3); - row.add(contro.string(0x3)); - } - else - { - row.add(" | | | | "); // Contropartita - } - row << '|' << tipo; - disable_cgs_cells(n, tipo); - cg.force_update(n); - - return n; -} - - -HIDDEN int compare_rows(const TObject** o1, const TObject** o2) -{ - // Totale, Rit.Fisc., Rit.Soc., da riga IVA, riga contabile, IVA detr., IVA non detr. - static char* sort_order = "TFSI DN"; - - const TToken_string* r1 = (const TToken_string*)*o1; - const TToken_string* r2 = (const TToken_string*)*o2; - const char c1 = r1->right(1)[0]; - const char c2 = r2->right(1)[0]; - return int(strchr(sort_order, c1) - strchr(sort_order, c2)); -} - - -void TPrimanota_application::cgs_pack() -{ - TArray& rows = cgs().rows_array(); - - const int max = rows.items(); - for (int i = 0; i < max; i++) - { - TToken_string& r = (TToken_string&)rows[i]; - bool del = FALSE; - if (r.empty_items()) // Remove all empty strings - del = TRUE; - else - { - const real imp = get_cgs_imp(i); - if (imp == 0.0) - { - del = TRUE; -/* - const TConto c(r, 6, 0x0); // Remove if import=0 and Bill not used - del = !bill_used(c); -*/ - } - - } - if (del) - rows.destroy(i, FALSE); - } - - rows.sort(compare_rows); // Pack and sort array -} - - -// Handler dello sheet di contabilita' -// Certified 90% -bool TPrimanota_application::cg_handler(TMask_field& f, KEY k) -{ - if (k == K_ENTER) - { - TSheet_field& cg = app().cgs(); - bool empty = TRUE; - real saldo; - for (int i = 0; i < cg.items(); i++) - { - const real imp = app().get_cgs_imp(i); - if (empty) empty = imp == 0.0; - saldo += imp; - } - if (saldo != 0.0) - { - const char* ss = saldo.string("."); - return f.error_box("Il movimento e' sbilanciato di %s lire.", ss); - } - else - if (empty) - return f.error_box("Il movimento non contiene nessuna riga contabile!"); - } - return TRUE; -} - - -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]; - - if (k == K_SPACE) - { - TMask& m = cg.sheet_mask(); - m.enable(DLG_DELREC, tipo == ' '); - } else - if (k == K_DEL) - { - if (tipo != ' ') - return error_box("La riga %d non puo' essere cancellata", r+1); - } - return TRUE; -} - -bool TPrimanota_application::descr_handler(TMask_field& f, KEY k) -{ - if (k == K_ENTER && f.get().empty()) - { - if (f.mask().get(F_CODCAUS).empty()) - return error_box("Descrizione documento necessaria in mancanza della causale"); - } - return TRUE; -} - - -// Handler per le colonne 'Dare' e 'Avere' dello sheet contabile. -// Scrivendo qualcosa in dare (101) cancella l'importo in avere (102) e viceversa -bool TPrimanota_application::dareavere_handler(TMask_field& f, KEY k) -{ - if (k == K_TAB && f.focusdirty() && !f.get().empty()) - { - const int id = 203-f.dlg(); // Calcola id del campo da resettare - f.mask().reset(id); - } - return TRUE; -} - -/////////////////////////////////////////////////////////// -// Gestione sheet IVA -/////////////////////////////////////////////////////////// - -TSheet_field& TPrimanota_application::ivas() const -{ - TSheet_field& s = (TSheet_field&)_msk[2]->field(F_SHEETIVA); - return s; -} - - -void TPrimanota_application::set_ivas_row(int nriga, const char* codiva, TConto& tc, - const char* desc) -{ - TToken_string& riga = ivas().row(nriga); - riga = " "; // Importo - riga.add (codiva); // codiva - riga.add (" | | "); // Imposta - C/R - Det - riga.add(tc.string(0x3)); // Conto - riga.add(desc); // Descrizione -} - - -bool TPrimanota_application::imponibile_handler(TMask_field& f, KEY key) -{ - if (key == K_TAB && f.dirty()) - { - const TString& iva = f.mask().get(102); - if (iva.not_empty()) // Se c'e' il codice IVA - { - const real& percent = cod2IVA(iva); - const real imponibile(f.get()); - real imposta = imponibile * percent / 100.0; imposta.ceil(); - f.mask().set(103, imposta.string()); - } - } - - return TRUE; -} - - -bool TPrimanota_application::codiva_handler(TMask_field& f, KEY key) -{ - if (key == K_TAB && f.dirty()) - { - TMask_field& i = f.mask().field(101); - i.set_dirty(); - return imponibile_handler(i, key); - } - return TRUE; -} - - -bool TPrimanota_application::imposta_handler(TMask_field& f, KEY key) -{ - if (key == K_ENTER) - { - const real imponibile(f.mask().get(101)); - const real& percent = cod2IVA(f.mask().get(102)); - real imposta = imponibile * percent / 100.0; imposta.ceil(); - const real val(f.get()); - if (val != imposta) - { - const TString16 wrong(val.string(".")); - const TString16 right(imposta.string(".")); - if (f.yesno_box("Imposta di '%s' errata: modificare in '%s'?", - (const char*)wrong, (const char*)right)) - f.set(imposta.string()); - } - } else - if (key == K_F8 && f.get().empty()) - { - real imponibile(f.mask().get(101)); - const real& percent = cod2IVA(f.mask().get(102)); - const real imposta = scorpora(imponibile, percent); - f.set(imposta.string()); - f.mask().set(101, imponibile.string()); - } - - return TRUE; -} - - -// Certified 50% -bool TPrimanota_application::iva_notify(int r, KEY k) -{ - static int oldpos; - static real oldimp; - - static int oldposiva; - static real oldiva; - - TSheet_field& iva = app().ivas(); - TToken_string& row = iva.row(r); - - if (k == K_SPACE) - { - const TConto oldconto(row, 5, 0x1); - oldpos = bill2pos(oldconto, 'I'); - oldimp = imp2sez(real(row.get(0))); - oldposiva = type2pos(detraibile(row.get_int(4)) ? 'D' : 'N'); - oldiva = imp2sez(real(row.get(2))); - } - if (k == K_DEL) - { - row.add("0", 0); // Azzera imponibile - row.add("0", 2); // Azzera imposta - k = K_ENTER; // Elegante o Sporco trucco - } - if (k == K_ENTER) - { - if (oldpos >= 0) // Il conto esisteva anche prima - { - app().add_cgs_imp(oldpos, -oldimp); - oldimp = 0.0; - } - if (oldposiva >= 0) // Il conto IVA esisteva anche prima - { - app().add_cgs_imp(oldposiva, -oldiva); - oldiva = 0.0; - } - -// Aggiorna conto sulla riga contabile - real imp = imp2sez(real(row.get(0))); // Imponibile - TConto conto(row, 5, 0x3); - oldpos = bill2pos(conto, 'I'); - - if (oldpos < 0) - app().set_cgs_row(-1, imp-oldimp, conto, "", 'I'); - else - app().add_cgs_imp(oldpos, imp-oldimp); - oldimp = imp; - -// Aggiorna conto IVA sulla riga contabile - - imp = imp2sez(real(row.get(2))); // Imposta - const bool detrarre = detraibile(row.get_int(4)); // Determina se IVA detraibile - app().causale().bill(detrarre ? 3 : 4, conto); - const char tipod = detrarre ? 'D' : 'N'; - oldposiva = type2pos(tipod); - if (oldposiva < 0) - { - const real val = imp-oldiva; - if (!val.is_zero()) - app().set_cgs_row(-1, val, conto, "", tipod); - } - else - app().add_cgs_imp(oldposiva, imp-oldiva); - oldiva = imp; - } - return TRUE; -} - - -/////////////////////////////////////////////////////////// -// Handlers dei campi della testata -/////////////////////////////////////////////////////////// - - -// Handler of the F_NUMREG field on the query mask -// Certified 90% -bool TPrimanota_application::num_handler(TMask_field& f, KEY key) -{ - if (!f.mask().is_running()) return TRUE; - - if (key == K_TAB && f.get().not_empty()) - { - if (app().find(1)) - dispatch_e_char(f.parent(), K_SHIFT_ENTER); - } - return TRUE; -} - - -// Handler of the F_CODCAUS field on the query mask -// Certified 90% -bool TPrimanota_application::caus_query_handler(TMask_field& f, KEY key) -{ - if (!f.mask().is_running()) return TRUE; - - if (key == K_TAB && f.focusdirty()) - { - const int ann = f.mask().get_int(F_ANNOIVA); - const char* cau = f.get(); - - const TipoIVA i = cau2IVA(cau, ann); - if (i != iva_errata) - { - const bool ok = suspended_handler(f, key); - if (ok) - dispatch_e_char(f.parent(), K_INS); - } - } - - return TRUE; -} - -// Handler of the F_CODCAUS field on the modify mask -// Certified 99% -bool TPrimanota_application::caus_modify_handler(TMask_field& f, KEY key) -{ - if (f.to_check(key)) - { - bool ok = suspended_handler(f, key); - if (!ok) return FALSE; - - const int ann = f.mask().get_int(F_ANNOIVA); - const char* cau = f.get(); - TCausale c(cau, ann); - ok = app().causale().similar(c); - if (!ok) - { - f.error_box("Causale incongruente con quella precedentemente inserita"); - return FALSE; - } - app().read_caus(cau, ann); - } - - return TRUE; -} - - -// Handler of the F_DATAREG field on the modify mask -// Certified 70% -bool TPrimanota_application::datareg_handler(TMask_field& f, KEY key) -{ - bool ok = TRUE; - - if (f.to_check(key)) - { - const TDate dr(f.get()); // Data dell'operazione - if (dr > TDate(TODAY)) - return f.error_box("La data dell'operazione e' superiore quella di sistema"); - - TMask& m = f.mask(); - const int ae = date2esc(dr); // Anno esercizio - - if (m.query_mode()) - { - if (ae == 0) return f.error_box("La data dell'operazione non appartiene a nessun esercizio"); - ok = app().giornale().read(ae); - if (!ok) return f.error_box("Non esiste il libro giornale dell'esercizio %d", ae); - } - if (dr < app().giornale().last_print()) - return f.error_box("La data dell'operazione e' antecedente alla " - "data di stampa del libro giornale dell'esercizio %d", ae); - - if (!m.query_mode() && app().iva() != nessuna_iva) - { - const int annoiva = m.get_int(F_ANNOIVA); - if (dr.year() != annoiva) - { - TRegistro& reg = app().causale().reg(); - const TString16 codreg(reg.name()); - ok = reg.read(codreg, dr.year()); - if (ok && dr < reg.last_reg()) - warning_box("La data dell'operazione e' antecedente al %s, ultima registrazione " - "sul registro '%s' del %d", - reg.last_reg().string(), (const char*)codreg, dr.year()); - } - } - } - return ok; -} - -// Handler of the F_DATACOMP field on the modify mask -// Certified 70% -bool TPrimanota_application::datacomp_handler(TMask_field& f, KEY key) -{ - if (!f.to_check(key, TRUE)) - return TRUE; - - const TDate dc(f.get()); // Data di competenza - const int ae = date2esc(dc); // Esercizio corrispondente - TMask& m = f.mask(); - - if (ae) - { - if (f.dlg() == F_DATACOMP) - m.set(F_ANNOES, ae); - - const TDate dr(m.get(F_DATAREG)); // Data operazione - int pr; // Esercizio precedente - const int ar = date2esc(dr, &pr); // Esercizio in corso - if (ae != ar && ae != pr) - { - TString80 e("La data deve appartenere all'esercizio "); - e << ar; - if (pr > 0) e << " o al " << pr; - return f.error_box(e); - } - } - else - return f.error_box("La data non appartiene a nessun esercizio"); - - return TRUE; -} - -// Handler of the F_DATA74TER field on the modify mask -// Certified 70% -bool TPrimanota_application::data74ter_handler(TMask_field& f, KEY key) -{ - if (!f.to_check(key)) return TRUE; - bool ok = datacomp_handler(f, key); - if (ok) - { - const TDate d74(f.get()); - const TLibro_giornale& g = app().giornale(); - if (d74 < g.last_print()) - { - ok = f.error_box("La data per il 74/ter e' antecedente alla data di stampa " - "del libro giornale dell'esercizio %d", g.year()); - } - } - return ok; -} - -// Handler of the F_CODREG field on the modify mask -// Certified 99% -bool TPrimanota_application::reg_handler(TMask_field& f, KEY key) -{ - if (f.to_check(key, TRUE)) - { -// if (!suspended_handler(f, key)) return FALSE; - - const int ai = f.mask().get_int(F_ANNOIVA); - const char* r = f.get(); - TRegistro reg(r, ai); - - const TipoIVA i = reg.iva(); - if (i == iva_errata) - return f.error_box("Tipo registro errato"); - - if (reg.iva() != app().iva()) - { - const TString16 i1(iva2name(reg.iva())); - const TString16 i2(iva2name(app().iva())); - return f.error_box("Tipo registro (%s) incongruente col tipo di registrazione (%s)", - (const char*)i1, (const char*)i2); - } - - const bool av = reg.agenzia_viaggi(); - f.mask().show(F_DATA74TER, av); - if (!av) f.mask().reset(F_DATA74TER); - - if (key == K_TAB) - app().causale().reg() = reg; - } - - return TRUE; -} - -bool TPrimanota_application::occas_code_handler(TMask_field& f, KEY key) -{ - if (key == K_TAB) - { - const char* code = f.get(); - if (*code) - { - TRelation occas(LF_OCCAS); - occas.lfile()->put("CFPI", code); - if (occas.read() == NOERR) - { - f.mask().autoload(&occas); - f.mask().send_key(K_TAB, O_COMUNE); - f.mask().send_key(K_TAB, O_COMUNENAS); - } - } - } - return TRUE; -} - -bool TPrimanota_application::occas_handler(TMask_field& f, KEY key) -{ - if (key == K_SPACE && f.mask().is_running()) - { - TMask& om = app().occas_mask(); - om.run(); - } - return TRUE; -} - -// Crea o aggiorna la riga contabile col totale documento -// Certified 99% -void TPrimanota_application::add_cgs_tot(TMask& m) -{ - // Lettura del conto dalla maschera - const char tipo = m.get(F_CLIFO)[0]; - int gruppo = m.get_int(F_GRUPPOCLIFO); - int conto = m.get_int(F_CONTOCLIFO); - long codice = m.get_long(tipo == 'C' ? F_CLIENTE : F_FORNITORE); - - // Se l'utente non ha ancora specificato un conto lo prendo dalla prima riga della causale - if (conto == 0) - { - TConto bill; _causale.bill(1, bill); - gruppo = bill.gruppo(); m.set(F_GRUPPOCLIFO, gruppo); - conto = bill.conto(); m.set(F_CONTOCLIFO, conto); - } - - TConto c(gruppo, conto, codice, tipo); - real tot(m.get(F_TOTALE)); - - // Creazione/Aggiornamento riga totale - int pos = type2pos('T'); - pos = set_cgs_row(pos, -imp2sez(tot), c, "Totale documento", 'T'); - - TToken_string& row = ivas().row(0); - const TCodiceIVA iva(m.get(F_CODIVA)); - if (iva.ok()) - { - iva_notify(0, K_SPACE); - const real imposta = scorpora(tot, iva.percentuale()); - row.add(tot.string(), 0); - row.add(iva.codice(), 1); - row.add(imposta.string(), 2); - row.add(" | "); - - TConto bill; // Conto della prima riga IVA - const TString& tipo = iva.tipo(); - if (tipo.not_empty()) - { - if (tipo == "ES") _causale.bill(5, bill); else - if (tipo == "NI") _causale.bill(6, bill); else - if (tipo == "NS") _causale.bill(7, bill); - } - if (!bill.ok()) - bill.set(m.get_int(F_GRUPPORIC), m.get_int(F_CONTORIC), m.get_long(F_SOTTOCONTORIC)); - if (!bill.ok()) - _causale.bill(2, bill); - - row.add(bill.string(0x3)); - app().ivas().force_update(0); - iva_notify(0, K_ENTER); - } -} - - -// Handler of the F_CLIENTE & F_FORNITORE field on the modify mask -// Certified 99% -bool TPrimanota_application::clifo_handler(TMask_field& f, KEY key) -{ - if (!suspended_handler(f, key)) - return FALSE; - - if (key == K_TAB && f.focusdirty()) - { - app().add_cgs_tot(f.mask()); - TLocalisamfile clifo(LF_CLIFO); - const int alleg = clifo.get_int("ALLEG"); - TEdit_field& upi = (TEdit_field&)f.mask().field(F_RIEPILOGO); - upi.check_type(alleg == 3 ? CHECK_REQUIRED : CHECK_NORMAL); - } - return TRUE; -} - - -// Handler of the F_TOTALE -// Certified 99% -bool TPrimanota_application::totale_handler(TMask_field& f, KEY key) -{ - if (key == K_TAB && f.focusdirty()) - { - app().add_cgs_tot(f.mask()); - } else - if (key == K_ENTER && f.get().empty()) - return f.yesno_box("Totale documento nullo: continuare ugualmente?"); - return TRUE; -} - - -void TPrimanota_application::add_cgs_rit(bool fiscali) -{ - const real imp(_msk[2]->get(fiscali ? F_RITFIS : F_RITSOC)); - - const char tipo = fiscali ? 'F' : 'S'; - int pos = type2pos(tipo); - if (pos < 0) - { - const int riga = fiscali ? 8 : 9; - TConto conto; _causale.bill(riga, conto); - - TString80 desc("Ritenute "); - desc << (fiscali ? "fiscali" : "sociali"); - - set_cgs_row(-1, -imp2sez(imp), conto, desc, tipo); - } - else - set_cgs_imp(pos, -imp2sez(imp)); -} - - -// Handler of the F_RITFIS -// Certified 99% -bool TPrimanota_application::ritfis_handler(TMask_field& f, KEY key) -{ - if (key == K_TAB && f.focusdirty()) - app().add_cgs_rit(TRUE); - return TRUE; -} - - -// Handler of F_RITSOC -// Certified 99% -bool TPrimanota_application::ritsoc_handler(TMask_field& f, KEY key) -{ - if (key == K_TAB && f.focusdirty()) - app().add_cgs_rit(FALSE); - return TRUE; -} - - -// Handler of F_VISVAL -// Certified 90% -bool TPrimanota_application::visval_handler(TMask_field& f, KEY key) -{ - if (key == K_SPACE) - { - TMask& m = f.mask(); - const real e(f.get() == "X" ? m.get(F_CAMBIO) : "1"); - m.set_exchange(e); - } - return TRUE; -} - - -bool TPrimanota_application::solaiva_handler(TMask_field& f, KEY key) -{ - if (key == K_SPACE) - { - TMask& m = f.mask(); - const bool anchecg = !m.get_bool(F_SOLAIVA); - - m.enable_page(2, anchecg); - app().cgs().enable(anchecg); - - if (m.is_running() && anchecg) - { - TSheet_field& iva = app().ivas(); - const int righe = iva.items(); - TProgind pi(righe, "Generazione righe contabilita'", FALSE, TRUE, 16); - - app().cgs().reset(); - app().add_cgs_tot(m); - if (m.get(F_RITFIS).not_empty()) app().add_cgs_rit(TRUE); - if (m.get(F_RITSOC).not_empty()) app().add_cgs_rit(FALSE); - - for (int i = 0; i < righe; i++) - { - if (!iva.row(i).empty_items()) - { - iva_notify(i, K_SPACE); - iva_notify(i, K_ENTER); - } - pi.setstatus(i+1); - } - app().fill_sheet(m); - } - } - return TRUE; -} - - -// Fill missing bill -// Certified 50% -bool TPrimanota_application::conto_handler(TMask_field& f, KEY k) -{ - if (k == K_ENTER && f.get().empty()) - { - TMask& m = f.mask(); - if (m.get(101).empty()) return TRUE; // Se non c'e' importo ignora la riga - - int riga = 0; - - TString16 cod(m.get(102)); - TTable iva("%IVA"); - iva.put("CODTAB", cod); - if (iva.read() == NOERR) - { - TConto conto; - cod = iva.get("S1"); - if (cod == "ES") riga = 5; else - if (cod == "NI") riga = 6; else - if (cod == "NS") riga = 7; else - { - TMask& m = app().mask(); - const char tipocf = m.get(F_CLIFO)[0]; - const long codcf = m.get_long(F_CLIENTE); - TLocalisamfile clifo(LF_CLIFO); - clifo.put("TIPOCF", tipocf); - clifo.put("CODCF", codcf); - clifo.read(); // Try Jolly bill - conto.set(clifo.get_int("GRUPPORIC"), - clifo.get_int("CONTORIC"), - clifo.get_long("SOTTOCRIC")); - riga = conto.ok() ? -1 : 2; // else use second row - } - if (riga > 0) - app().causale().bill(riga, conto); - - if (conto.ok()) - { - m.set(106, " "); - m.set(107, conto.gruppo()); - m.set(108, conto.conto()); - m.set(109, conto.sottoconto()); - } else riga = 0; - } - - if (riga == 0) - return f.error_box("Il conto e' obbligatorio sulla riga IVA"); - } - return TRUE; -} +#include +#include +#include +#include + +#include "cg2100.h" +#include "cg2102.h" + + +// Certified 100% +inline TPrimanota_application& app() +{ return (TPrimanota_application&)*MainApp(); } + +/////////////////////////////////////////////////////////// +// Funzioni di decodifica/calcolo +/////////////////////////////////////////////////////////// + +// Determina il tipo IVA da causale+anno +// Certified 50% +TipoIVA TPrimanota_application::cau2IVA(const char* causale, int annoiva) +{ + TipoIVA i = iva_errata; + + if (*causale > ' ') + { + TCausale c; + if (c.read(causale, annoiva)) + i = c.iva(); + else + error_box("Causale errata: '%s'", causale); + } else i = nessuna_iva; + + return i; +} + +// Calcola l'anno di esercizio di una data +// Certified 99% +int TPrimanota_application::date2esc(const TDate& d, int* prevesc) +{ + if (prevesc) *prevesc = 0; + TTable esc("ESC"); + for (int err = esc.first(); err == NOERR; err = esc.next()) + { + const TDate ia(esc.get("D0")); // Data inizio esercizio + const TDate fa(esc.get("D1")); // Data fine esercizio + const anno = esc.get_int("CODTAB"); + if (d >= ia && d <= fa) + return anno; + if (prevesc) *prevesc = anno; + } + return 0; +} + +// Calcolo della percentuale di un dato codice IVA +// Certified 99% +const real& TPrimanota_application::cod2IVA(const char* codiva) +{ + static TString16 _codiva; + static real _percent; + + if (_codiva != codiva) + { + _codiva = codiva; + TCodiceIVA c(_codiva); + _percent = c.percentuale(); + } + + return _percent; +} + +real TPrimanota_application::scorpora(real& imponibile, const real& percent) +{ + real imposta = imponibile * percent / (percent + 100.0); imposta.ceil(); + imponibile -= imposta; + return imposta; +} + + +// Determina se un codice sospeso o no +// Certified 50% +bool TPrimanota_application::suspended_handler(TMask_field& f, KEY k) +{ + if (f.to_check(k)) + { + CHECKD(f.is_edit(), "Can't check suspension of a non edit-field ", f.dlg()); + const TEdit_field& c = (const TEdit_field&)f; + const TBrowse* b = c.browse(); + CHECKD(b, "Can't check suspension of a edit-field without a USE ", f.dlg()); + const TLocalisamfile* i = b->cursor()->file(); + + const char* sf = i->tab() ? "B2" : "SOSPESO"; + const bool suspended = i->get_bool(sf); + if (suspended) + { + sf = f.get(); + return f.error_box("Il codice '%s' e' sospeso e non puo' essere utilizzato", sf); + } + } + return TRUE; +} + + +// Determina se un codice detrazione e' di tipo detraibile o no +// Certified 70% +bool TPrimanota_application::detraibile(int tipodet) +{ + if (tipodet) + return FALSE; + if (app().iva() != iva_acquisti) + return TRUE; + + const TSheet_field& iva = app().ivas(); + + TString16 chiave; + chiave = iva.mask().get(F_ANNOIVA); + chiave << iva.mask().get(F_CODREG); + TTable reg("REG"); + reg.put("CODTAB", chiave); + reg.read(); +#ifdef DBG + if(reg.status() != NOERR) + error_box("La minchia che trovo il registro '%s'", (const char*)chiave); +#endif + + const char* att = reg.get("S8"); + chiave.cut(4); chiave << att; + + TTable pla("PLA"); + pla.put("CODTAB", chiave); + const int err = pla.read(); + real prorata; + if (err == NOERR) + prorata = pla.get_real("R8"); + + return prorata != 100.0; +} + +/////////////////////////////////////////////////////////// +// Funzioni di ricerca +/////////////////////////////////////////////////////////// + +int TPrimanota_application::type2pos(char tipo) +{ + TSheet_field& cg = app().cgs(); + for (int i = 0; i < cg.items(); i++) + { + TToken_string& s = cg.row(i); + if (s[s.len()-1] == tipo) + return i; + } + return -1; +} + + +int TPrimanota_application::bill2pos(const TConto& conto, char tipo) +{ + TSheet_field& cg = app().cgs(); + for (int i = 0; i < cg.items(); i++) + { + TToken_string& s = cg.row(i); + if (s[s.len()-1] == tipo) + { + const TConto c(s, 3, 0x0); + if (c == conto) + return i; + } + } + return -1; +} + + +int TPrimanota_application::bill2contr(const TConto& conto, char sezione) const +{ + const TArray& rows = cgs().rows_array(); + for (int i = 0; i < rows.items(); i++) + { + TToken_string& row = (TToken_string&)rows[i]; + const char sez = row.get(0)[0] > ' ' ? 'D' : 'A'; + if (sez == sezione) // Devo cercare sezione contraria + continue; + const TConto c(row, 3, 0x0); + if (conto == c) + return i; + } + return -1; +} + + +bool TPrimanota_application::bill_used(const TConto& conto) const +{ + const TArray& rows = ivas().rows_array(); + for (int i = 0; i < rows.items(); i++) + { + TToken_string& row = (TToken_string&)rows[i]; + const TConto c(row, 6, 0x0); + if (conto == c) + return TRUE; + } + return FALSE; +} + + +/////////////////////////////////////////////////////////// +// Gestione sheet CG +/////////////////////////////////////////////////////////// + +TMask& TPrimanota_application::mask() const +{ + TipoIVA i = app().iva(); + return *_msk[i == nessuna_iva ? 1 : 2]; +} + + +TSheet_field& TPrimanota_application::cgs() const +{ + TSheet_field& s = (TSheet_field&)mask().field(F_SHEETCG); + return s; +} + + +// Certified 99% +// Dato un importo stabilisce se deve andare in DARE o AVERE +// ritornandolo poi col segno opportuno + o - +real TPrimanota_application::imp2sez(const real& imp) +{ + const int dare = (imp >= 0.0) ^ (app().iva() == iva_vendite); + return abs(imp) * (dare ? +1.0 : -1.0); +} + + +// Certified 99% +// Scrive l'importo imp nella opportuna sezione della riga n +void TPrimanota_application::set_cgs_imp(int n, const real& imp) +{ + TToken_string& row = cgs().row(n); + const char* val = imp.string(); + if (*val == '-') + { + row.add(" ", 0); + row.add(val+1, 1); + } + else + { + row.add(val, 0); + row.add(" ", 1); + } + cgs().force_update(n); +} + + +// Legge l'importo della riga n e lo ritorna col segno dovuto +// Certified 99% +real TPrimanota_application::get_cgs_imp(int n) +{ + TToken_string& row = cgs().row(n); + const real dare(row.get(0)); + if (dare > 0.0) return dare; + const real avere(row.get()); + return -avere; +} + + +// Certified 90% +void TPrimanota_application::add_cgs_imp(int n, const real& imp) +{ + const real tot = get_cgs_imp(n) + imp; + set_cgs_imp(n, tot); +} + +// Disabilita le celle della riga contabile n in base al suo tipo +void TPrimanota_application::disable_cgs_cells(int n, char tipo) +{ + int last; + switch(tipo) + { + case 'F': // Ritenute Fiscali + case 'S': // Ritenute Sociali + case 'D': // IVA Detraibile + case 'N':last = 3; break; // IVA Non detraibile + case 'T': // Totale documento + case 'I':last = 6; break; // Imponibile + default :last = 0; break; // Solo contabile + } + + TSheet_field& cg = cgs(); + for (int i = 0; i < last; i++) + cg.disable_cell(n, i); + + if (tipo == 'T') + { + cg.enable_cell(n, 3); + cg.enable_cell(n, 4); + } +} + + +int TPrimanota_application::set_cgs_row(int n, const real& imp, + TConto& conto, const char* desc, + char tipo) +{ + TSheet_field& cg = cgs(); + if (n < 0) n = cg.first_empty(); + TToken_string& row = cg.row(n); + row = ""; + set_cgs_imp(n, imp); + row.add(conto.string(0x3)); + row.add(""); + row.add(desc); + + int pos = 0; + if (tipo == 'I' && (pos = type2pos('T')) >= 0) + { + TConto contro(cg.row(pos), 2, 0x3); + row.add(contro.string(0x3)); + } + else + { + row.add(" | | | | "); // Contropartita + } + row << '|' << tipo; + disable_cgs_cells(n, tipo); + cg.force_update(n); + + return n; +} + + +HIDDEN int compare_rows(const TObject** o1, const TObject** o2) +{ + // Totale, Rit.Fisc., Rit.Soc., da riga IVA, riga contabile, IVA detr., IVA non detr. + static char* sort_order = "TFSI DN"; + + const TToken_string* r1 = (const TToken_string*)*o1; + const TToken_string* r2 = (const TToken_string*)*o2; + const char c1 = r1->right(1)[0]; + const char c2 = r2->right(1)[0]; + return int(strchr(sort_order, c1) - strchr(sort_order, c2)); +} + + +void TPrimanota_application::cgs_pack() +{ + TArray& rows = cgs().rows_array(); + + const int max = rows.items(); + for (int i = 0; i < max; i++) + { + TToken_string& r = (TToken_string&)rows[i]; + bool del = FALSE; + if (r.empty_items()) // Remove all empty strings + del = TRUE; + else + { + const real imp = get_cgs_imp(i); + if (imp == 0.0) + { + del = TRUE; +/* + const TConto c(r, 6, 0x0); // Remove if import=0 and Bill not used + del = !bill_used(c); +*/ + } + + } + if (del) + rows.destroy(i, FALSE); + } + + rows.sort(compare_rows); // Pack and sort array +} + + +// Handler dello sheet di contabilita' +// Certified 90% +bool TPrimanota_application::cg_handler(TMask_field& f, KEY k) +{ + if (k == K_ENTER) + { + TSheet_field& cg = app().cgs(); + bool empty = TRUE; + real saldo; + for (int i = 0; i < cg.items(); i++) + { + const real imp = app().get_cgs_imp(i); + if (empty) empty = imp == 0.0; + saldo += imp; + } + if (saldo != 0.0) + { + const char* ss = saldo.string("."); + return f.error_box("Il movimento e' sbilanciato di %s lire.", ss); + } + else + if (empty) + return f.error_box("Il movimento non contiene nessuna riga contabile!"); + } + return TRUE; +} + + +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]; + + if (k == K_SPACE) + { + TMask& m = cg.sheet_mask(); + m.enable(DLG_DELREC, tipo == ' '); + } else + if (k == K_DEL) + { + if (tipo != ' ') + return error_box("La riga %d non puo' essere cancellata", r+1); + } + return TRUE; +} + +bool TPrimanota_application::descr_handler(TMask_field& f, KEY k) +{ + if (k == K_ENTER && f.get().empty()) + { + if (f.mask().get(F_CODCAUS).empty()) + return error_box("Descrizione documento necessaria in mancanza della causale"); + } + return TRUE; +} + + +// Handler per le colonne 'Dare' e 'Avere' dello sheet contabile. +// Scrivendo qualcosa in dare (101) cancella l'importo in avere (102) e viceversa +bool TPrimanota_application::dareavere_handler(TMask_field& f, KEY k) +{ + if (k == K_TAB && f.focusdirty() && !f.get().empty()) + { + const int id = 203-f.dlg(); // Calcola id del campo da resettare + f.mask().reset(id); + } + return TRUE; +} + +/////////////////////////////////////////////////////////// +// Gestione sheet IVA +/////////////////////////////////////////////////////////// + +TSheet_field& TPrimanota_application::ivas() const +{ + TSheet_field& s = (TSheet_field&)_msk[2]->field(F_SHEETIVA); + return s; +} + + +void TPrimanota_application::set_ivas_row(int nriga, const char* codiva, TConto& tc, + const char* desc) +{ + TToken_string& riga = ivas().row(nriga); + riga = " "; // Importo + riga.add (codiva); // codiva + riga.add (" | | "); // Imposta - C/R - Det + riga.add(tc.string(0x3)); // Conto + riga.add(desc); // Descrizione +} + + +bool TPrimanota_application::imponibile_handler(TMask_field& f, KEY key) +{ + if (key == K_TAB && f.dirty()) + { + const TString& iva = f.mask().get(102); + if (iva.not_empty()) // Se c'e' il codice IVA + { + const real& percent = cod2IVA(iva); + const real imponibile(f.get()); + real imposta = imponibile * percent / 100.0; imposta.ceil(); + f.mask().set(103, imposta.string()); + } + } + + return TRUE; +} + + +bool TPrimanota_application::codiva_handler(TMask_field& f, KEY key) +{ + if (key == K_TAB && f.dirty()) + { + TMask_field& i = f.mask().field(101); + i.set_dirty(); + return imponibile_handler(i, key); + } + return TRUE; +} + + +bool TPrimanota_application::imposta_handler(TMask_field& f, KEY key) +{ + if (key == K_ENTER) + { + const real imponibile(f.mask().get(101)); + const real& percent = cod2IVA(f.mask().get(102)); + real imposta = imponibile * percent / 100.0; imposta.ceil(); + const real val(f.get()); + if (val != imposta) + { + const TString16 wrong(val.string(".")); + const TString16 right(imposta.string(".")); + if (f.yesno_box("Imposta di '%s' errata: modificare in '%s'?", + (const char*)wrong, (const char*)right)) + f.set(imposta.string()); + } + } else + if (key == K_F8 && f.get().empty()) + { + real imponibile(f.mask().get(101)); + const real& percent = cod2IVA(f.mask().get(102)); + const real imposta = scorpora(imponibile, percent); + f.set(imposta.string()); + f.mask().set(101, imponibile.string()); + } + + return TRUE; +} + + +// Certified 50% +bool TPrimanota_application::iva_notify(int r, KEY k) +{ + static int oldpos; + static real oldimp; + + static int oldposiva; + static real oldiva; + + TSheet_field& iva = app().ivas(); + TToken_string& row = iva.row(r); + + if (k == K_SPACE) + { + const TConto oldconto(row, 5, 0x1); + oldpos = bill2pos(oldconto, 'I'); + oldimp = imp2sez(real(row.get(0))); + oldposiva = type2pos(detraibile(row.get_int(4)) ? 'D' : 'N'); + oldiva = imp2sez(real(row.get(2))); + } + if (k == K_DEL) + { + row.add("0", 0); // Azzera imponibile + row.add("0", 2); // Azzera imposta + k = K_ENTER; // Elegante o Sporco trucco + } + if (k == K_ENTER) + { + if (oldpos >= 0) // Il conto esisteva anche prima + { + app().add_cgs_imp(oldpos, -oldimp); + oldimp = 0.0; + } + if (oldposiva >= 0) // Il conto IVA esisteva anche prima + { + app().add_cgs_imp(oldposiva, -oldiva); + oldiva = 0.0; + } + +// Aggiorna conto sulla riga contabile + real imp = imp2sez(real(row.get(0))); // Imponibile + TConto conto(row, 5, 0x3); + oldpos = bill2pos(conto, 'I'); + + if (oldpos < 0) + app().set_cgs_row(-1, imp-oldimp, conto, "", 'I'); + else + app().add_cgs_imp(oldpos, imp-oldimp); + oldimp = imp; + +// Aggiorna conto IVA sulla riga contabile + + imp = imp2sez(real(row.get(2))); // Imposta + const bool detrarre = detraibile(row.get_int(4)); // Determina se IVA detraibile + app().causale().bill(detrarre ? 3 : 4, conto); + const char tipod = detrarre ? 'D' : 'N'; + oldposiva = type2pos(tipod); + if (oldposiva < 0) + { + const real val = imp-oldiva; + if (!val.is_zero()) + app().set_cgs_row(-1, val, conto, "", tipod); + } + else + app().add_cgs_imp(oldposiva, imp-oldiva); + oldiva = imp; + } + return TRUE; +} + + +// Handler dello sheet di contabilita' +// Certified 90% +bool TPrimanota_application::iva_handler(TMask_field& f, KEY k) +{ + if (k != K_ENTER) return TRUE; + + real imp; + for (int r = 0; r < app().ivas().items(); r++) + { + TToken_string& row = app().ivas().row(r); + imp += real(row.get(0)); // imponibile + row.get(); // IVA + imp += real(row.get()); // imposta + } + + const TMask& m = f.mask(); + real tot(m.get(F_TOTALE)); + tot += real(m.get(F_RITFIS)); + tot += real(m.get(F_RITSOC)); + + if (imp != tot) + { + TString16 t(tot.string(".")); + TString16 i(imp.string(".")); + return error_box("La somma del totale documento e delle ritenute (%s) e' diverso dalla " + "somma degli imponibili e delle imposte (%s)", t, i); + } + + return TRUE; +} + + +/////////////////////////////////////////////////////////// +// Handlers dei campi della testata +/////////////////////////////////////////////////////////// + + +// Handler of the F_NUMREG field on the query mask +// Certified 90% +bool TPrimanota_application::num_handler(TMask_field& f, KEY key) +{ + if (!f.mask().is_running()) return TRUE; + + if (key == K_TAB && f.get().not_empty()) + { + if (app().find(1)) + dispatch_e_char(f.parent(), K_SHIFT_ENTER); + } + return TRUE; +} + + +// Handler of the F_CODCAUS field on the query mask +// Certified 90% +bool TPrimanota_application::caus_query_handler(TMask_field& f, KEY key) +{ + if (!f.mask().is_running()) return TRUE; + + if (key == K_TAB && f.focusdirty()) + { + const int ann = f.mask().get_int(F_ANNOIVA); + const char* cau = f.get(); + + const TipoIVA i = cau2IVA(cau, ann); + if (i != iva_errata) + { + const bool ok = suspended_handler(f, key); + if (ok) + dispatch_e_char(f.parent(), K_INS); + } + } + + return TRUE; +} + +// Handler of the F_CODCAUS field on the modify mask +// Certified 99% +bool TPrimanota_application::caus_modify_handler(TMask_field& f, KEY key) +{ + if (f.to_check(key)) + { + bool ok = suspended_handler(f, key); + if (!ok) return FALSE; + + const int ann = f.mask().get_int(F_ANNOIVA); + const char* cau = f.get(); + TCausale c(cau, ann); + ok = app().causale().similar(c); + if (!ok) + { + f.error_box("Causale incongruente con quella precedentemente inserita"); + return FALSE; + } + app().read_caus(cau, ann); + } + + return TRUE; +} + + +// Handler of the F_DATAREG field on the modify mask +// Certified 70% +bool TPrimanota_application::datareg_handler(TMask_field& f, KEY key) +{ + bool ok = TRUE; + + if (f.to_check(key)) + { + const TDate dr(f.get()); // Data dell'operazione + if (dr > TDate(TODAY)) + return f.error_box("La data dell'operazione e' superiore quella di sistema"); + + TMask& m = f.mask(); + const int ae = date2esc(dr); // Anno esercizio + + if (m.query_mode()) + { + if (ae == 0) return f.error_box("La data dell'operazione non appartiene a nessun esercizio"); + ok = app().giornale().read(ae); + if (!ok) return f.error_box("Non esiste il libro giornale dell'esercizio %d", ae); + } + if (dr < app().giornale().last_print()) + return f.error_box("La data dell'operazione e' antecedente alla " + "data di stampa del libro giornale dell'esercizio %d", ae); + + if (!m.query_mode() && app().iva() != nessuna_iva) + { + const int annoiva = m.get_int(F_ANNOIVA); + if (dr.year() != annoiva) + { + TRegistro& reg = app().causale().reg(); + const TString16 codreg(reg.name()); + ok = reg.read(codreg, dr.year()); + if (ok && dr < reg.last_reg()) + warning_box("La data dell'operazione e' antecedente al %s, ultima registrazione " + "sul registro '%s' del %d", + reg.last_reg().string(), (const char*)codreg, dr.year()); + } + } + } + return ok; +} + +// Handler of the F_DATACOMP field on the modify mask +// Certified 70% +bool TPrimanota_application::datacomp_handler(TMask_field& f, KEY key) +{ + if (!f.to_check(key, TRUE)) + return TRUE; + + const TDate dc(f.get()); // Data di competenza + const int ae = date2esc(dc); // Esercizio corrispondente + TMask& m = f.mask(); + + if (ae) + { + if (f.dlg() == F_DATACOMP) + m.set(F_ANNOES, ae); + + const TDate dr(m.get(F_DATAREG)); // Data operazione + int pr; // Esercizio precedente + const int ar = date2esc(dr, &pr); // Esercizio in corso + if (ae != ar && ae != pr) + { + TString80 e("La data deve appartenere all'esercizio "); + e << ar; + if (pr > 0) e << " o al " << pr; + return f.error_box(e); + } + } + else + return f.error_box("La data non appartiene a nessun esercizio"); + + return TRUE; +} + +// Handler of the F_DATA74TER field on the modify mask +// Certified 70% +bool TPrimanota_application::data74ter_handler(TMask_field& f, KEY key) +{ + if (!f.to_check(key)) return TRUE; + bool ok = datacomp_handler(f, key); + if (ok) + { + const TDate d74(f.get()); + const TLibro_giornale& g = app().giornale(); + if (d74 < g.last_print()) + { + ok = f.error_box("La data per il 74/ter e' antecedente alla data di stampa " + "del libro giornale dell'esercizio %d", g.year()); + } + } + return ok; +} + +// Handler of the F_CODREG field on the modify mask +// Certified 99% +bool TPrimanota_application::reg_handler(TMask_field& f, KEY key) +{ + if (f.to_check(key, TRUE)) + { +// if (!suspended_handler(f, key)) return FALSE; + + const int ai = f.mask().get_int(F_ANNOIVA); + const char* r = f.get(); + TRegistro reg(r, ai); + + const TipoIVA i = reg.iva(); + if (i == iva_errata) + return f.error_box("Tipo registro errato"); + + if (reg.iva() != app().iva()) + { + const TString16 i1(iva2name(reg.iva())); + const TString16 i2(iva2name(app().iva())); + return f.error_box("Tipo registro (%s) incongruente col tipo di registrazione (%s)", + (const char*)i1, (const char*)i2); + } + + const bool av = reg.agenzia_viaggi(); + f.mask().show(F_DATA74TER, av); + if (!av) f.mask().reset(F_DATA74TER); + + if (key == K_TAB) + app().causale().reg() = reg; + } + + return TRUE; +} + +bool TPrimanota_application::occas_code_handler(TMask_field& f, KEY key) +{ + if (key == K_TAB) + { + const char* code = f.get(); + if (*code) + { + TRelation occas(LF_OCCAS); + occas.lfile()->put("CFPI", code); + if (occas.read() == NOERR) + { + f.mask().autoload(&occas); + f.mask().send_key(K_TAB, O_COMUNE); + f.mask().send_key(K_TAB, O_COMUNENAS); + } + } + } + return TRUE; +} + +bool TPrimanota_application::occas_handler(TMask_field& f, KEY key) +{ + if (key == K_SPACE && f.mask().is_running()) + { + TMask& om = app().occas_mask(); + om.run(); + } + return TRUE; +} + +// Crea o aggiorna la riga contabile col totale documento +// Certified 99% +void TPrimanota_application::add_cgs_tot(TMask& m) +{ + // Lettura del conto dalla maschera + const char tipo = m.get(F_CLIFO)[0]; + int gruppo = m.get_int(F_GRUPPOCLIFO); + int conto = m.get_int(F_CONTOCLIFO); + long codice = m.get_long(tipo == 'C' ? F_CLIENTE : F_FORNITORE); + + // Se l'utente non ha ancora specificato un conto lo prendo dalla prima riga della causale + if (conto == 0) + { + TConto bill; _causale.bill(1, bill); + gruppo = bill.gruppo(); m.set(F_GRUPPOCLIFO, gruppo); + conto = bill.conto(); m.set(F_CONTOCLIFO, conto); + } + + TConto c(gruppo, conto, codice, tipo); + real tot(m.get(F_TOTALE)); + + // Creazione/Aggiornamento riga totale + int pos = type2pos('T'); + pos = set_cgs_row(pos, -imp2sez(tot), c, "Totale documento", 'T'); + + TToken_string& row = ivas().row(0); + const TCodiceIVA iva(m.get(F_CODIVA)); + if (iva.ok()) + { + iva_notify(0, K_SPACE); + const real imposta = scorpora(tot, iva.percentuale()); + row.add(tot.string(), 0); + row.add(iva.codice(), 1); + row.add(imposta.string(), 2); + row.add(" | "); + + TConto bill; // Conto della prima riga IVA + const TString& tipo = iva.tipo(); + if (tipo.not_empty()) + { + if (tipo == "ES") _causale.bill(5, bill); else + if (tipo == "NI") _causale.bill(6, bill); else + if (tipo == "NS") _causale.bill(7, bill); + } + if (!bill.ok()) + bill.set(m.get_int(F_GRUPPORIC), m.get_int(F_CONTORIC), m.get_long(F_SOTTOCONTORIC)); + if (!bill.ok()) + _causale.bill(2, bill); + + row.add(bill.string(0x3)); + app().ivas().force_update(0); + iva_notify(0, K_ENTER); + } +} + + +// Handler of the F_CLIENTE & F_FORNITORE field on the modify mask +// Certified 99% +bool TPrimanota_application::clifo_handler(TMask_field& f, KEY key) +{ + if (!suspended_handler(f, key)) + return FALSE; + + if (key == K_TAB && f.focusdirty()) + { + app().add_cgs_tot(f.mask()); + TLocalisamfile clifo(LF_CLIFO); + const int alleg = clifo.get_int("ALLEG"); + TEdit_field& upi = (TEdit_field&)f.mask().field(F_RIEPILOGO); + upi.check_type(alleg == 3 ? CHECK_REQUIRED : CHECK_NORMAL); + } + return TRUE; +} + + +// Handler of the F_TOTALE +// Certified 99% +bool TPrimanota_application::totale_handler(TMask_field& f, KEY key) +{ + if (key == K_TAB && f.focusdirty()) + { + app().add_cgs_tot(f.mask()); + } else + if (key == K_ENTER && f.get().empty()) + return f.yesno_box("Totale documento nullo: continuare ugualmente?"); + return TRUE; +} + + +void TPrimanota_application::add_cgs_rit(bool fiscali) +{ + const real imp(_msk[2]->get(fiscali ? F_RITFIS : F_RITSOC)); + + const char tipo = fiscali ? 'F' : 'S'; + int pos = type2pos(tipo); + if (pos < 0) + { + const int riga = fiscali ? 8 : 9; + TConto conto; _causale.bill(riga, conto); + + TString80 desc("Ritenute "); + desc << (fiscali ? "fiscali" : "sociali"); + + set_cgs_row(-1, -imp2sez(imp), conto, desc, tipo); + } + else + set_cgs_imp(pos, -imp2sez(imp)); +} + + +// Handler of the F_RITFIS +// Certified 99% +bool TPrimanota_application::ritfis_handler(TMask_field& f, KEY key) +{ + if (key == K_TAB && f.focusdirty()) + app().add_cgs_rit(TRUE); + return TRUE; +} + + +// Handler of F_RITSOC +// Certified 99% +bool TPrimanota_application::ritsoc_handler(TMask_field& f, KEY key) +{ + if (key == K_TAB && f.focusdirty()) + app().add_cgs_rit(FALSE); + return TRUE; +} + + +// Handler of F_VISVAL +// Certified 90% +bool TPrimanota_application::visval_handler(TMask_field& f, KEY key) +{ + if (key == K_SPACE) + { + TMask& m = f.mask(); + const real e(f.get() == "X" ? m.get(F_CAMBIO) : "1"); + m.set_exchange(e); + } + return TRUE; +} + + +bool TPrimanota_application::solaiva_handler(TMask_field& f, KEY key) +{ + if (key == K_SPACE) + { + TMask& m = f.mask(); + const bool anchecg = !m.get_bool(F_SOLAIVA); + + m.enable_page(2, anchecg); + app().cgs().enable(anchecg); + + if (m.is_running() && anchecg) + { + TSheet_field& iva = app().ivas(); + const int righe = iva.items(); + TProgind pi(righe, "Generazione righe contabilita'", FALSE, TRUE, 16); + + app().cgs().reset(); + app().add_cgs_tot(m); + if (m.get(F_RITFIS).not_empty()) app().add_cgs_rit(TRUE); + if (m.get(F_RITSOC).not_empty()) app().add_cgs_rit(FALSE); + + for (int i = 0; i < righe; i++) + { + if (!iva.row(i).empty_items()) + { + iva_notify(i, K_SPACE); + iva_notify(i, K_ENTER); + } + pi.setstatus(i+1); + } + app().fill_sheet(m); + } + } + return TRUE; +} + + +// Fill missing bill +// Certified 50% +bool TPrimanota_application::conto_handler(TMask_field& f, KEY k) +{ + if (k == K_ENTER && f.get().empty()) + { + TMask& m = f.mask(); + if (m.get(101).empty()) return TRUE; // Se non c'e' importo ignora la riga + + int riga = 0; + + TString16 cod(m.get(102)); + TTable iva("%IVA"); + iva.put("CODTAB", cod); + if (iva.read() == NOERR) + { + TConto conto; + cod = iva.get("S1"); + if (cod == "ES") riga = 5; else + if (cod == "NI") riga = 6; else + if (cod == "NS") riga = 7; else + { + TMask& m = app().mask(); + const char tipocf = m.get(F_CLIFO)[0]; + const long codcf = m.get_long(F_CLIENTE); + TLocalisamfile clifo(LF_CLIFO); + clifo.put("TIPOCF", tipocf); + clifo.put("CODCF", codcf); + clifo.read(); // Try Jolly bill + conto.set(clifo.get_int("GRUPPORIC"), + clifo.get_int("CONTORIC"), + clifo.get_long("SOTTOCRIC")); + riga = conto.ok() ? -1 : 2; // else use second row + } + if (riga > 0) + app().causale().bill(riga, conto); + + if (conto.ok()) + { + m.set(106, " "); + m.set(107, conto.gruppo()); + m.set(108, conto.conto()); + m.set(109, conto.sottoconto()); + } else riga = 0; + } + + if (riga == 0) + return f.error_box("Il conto e' obbligatorio sulla riga IVA"); + } + return TRUE; +} diff --git a/cg/cg2102.h b/cg/cg2102.h index 27bd37161..3838fad6d 100755 --- a/cg/cg2102.h +++ b/cg/cg2102.h @@ -1,140 +1,141 @@ -#ifndef __CG2102_H -#define __CG2102_H - -#ifndef __MSKSHEET_H -#include -#endif - -#ifndef __RELAPP_H -#include -#endif - -#ifndef __CGLIB_H -#include "cglib.h" -#endif - -#ifndef __CG2101_H -#include "cg2101.h" -#endif - -#ifndef __CG2103_H -#include "cg2103.h" -#endif - -class TPrimanota_application : public TRelation_application -{ - TMovimentoPN* _rel; - TLocalisamfile* _tabcom; - TLocalisamfile* _tab; - TLocalisamfile* _caus; - TLocalisamfile* _rcaus; - TLocalisamfile* _clifo; - TLocalisamfile* _pcon; - TLocalisamfile* _attiv; - TLocalisamfile* _saldo; - TLocalisamfile* _comuni; - TLocalisamfile* _occas; - TLocalisamfile* _nditte; - - TCausale _causale; // Causale corrente - TLibro_giornale _giornale; // Libro giornale corrente - TipoIVA _iva; // Tipo di IVA corrente - - TSaldo_agg _saldi; // Saldi da aggiornare - - TMask* _msk[4]; // Maschere di query, cg, iva - long _lastreg; // Numero ultima registrazione - int _mode; // Modo maschera corrente - - static bool suspended_handler(TMask_field& f, KEY k); - static bool num_handler(TMask_field& f, KEY key); - static bool caus_query_handler(TMask_field& f, KEY key); - static bool caus_modify_handler(TMask_field& f, KEY key); - static bool datareg_handler(TMask_field& f, KEY key); - static bool datacomp_handler(TMask_field& f, KEY key); - static bool data74ter_handler(TMask_field& f, KEY key); - static bool reg_handler(TMask_field& f, KEY key); - static bool clifo_handler(TMask_field& f, KEY key); - static bool totale_handler(TMask_field& f, KEY key); - static bool ritfis_handler(TMask_field& f, KEY key); - static bool ritsoc_handler(TMask_field& f, KEY key); - static bool visval_handler(TMask_field& f, KEY key); - static bool occas_code_handler(TMask_field& f, KEY key); - static bool occas_handler(TMask_field& f, KEY key); - static bool solaiva_handler(TMask_field& f, KEY key); - static bool descr_handler(TMask_field& f, KEY k); - static bool imponibile_handler(TMask_field& f, KEY key); - static bool codiva_handler(TMask_field& f, KEY key); - static bool imposta_handler(TMask_field& f, KEY key); - static bool dareavere_handler(TMask_field& f, KEY k); - static bool conto_handler(TMask_field& f, KEY key); - - static bool iva_notify(int r, KEY key); - static bool cg_notify(int r, KEY key); - static bool cg_handler(TMask_field& f, KEY key); - -protected: - virtual bool user_create(); - virtual bool user_destroy(); - virtual bool changing_mask(int mode); - virtual TMask* get_mask(int mode); - virtual TRelation* get_relation() const { return _rel; } - virtual void init_query_mode(TMask& m); - virtual void init_insert_mode(TMask& m); - virtual void init_modify_mode(TMask& m); - virtual const char* get_next_key(); - virtual int read(TMask& m); - virtual int write(const TMask& m); - virtual int rewrite(const TMask& m); - virtual bool remove(); - - void genera_incasso(const char* causimm); - - TMask* load_mask(int n); - TMask& occas_mask() { return *load_mask(3); } - - bool get_conto(int r, TConto& c) const; - void mask2rel(const TMask& m); - - TipoIVA iva() const { return _iva; } - TMask& TPrimanota_application::mask() const; - - void fill_sheet(TMask& m) const; - void init_mask(TMask& m); - - TSheet_field& cgs() const; - TSheet_field& ivas() const; - - static real imp2sez(const real& importo); - static int bill2pos(const TConto& conto, char tipo); - static TipoIVA reg2IVA(const char* registro, int anno); - static TipoIVA cau2IVA(const char* causale, int anno); - static int type2pos(char tipo); - static const real& cod2IVA(const char* codiva); - static real scorpora(real& imponibile, const real& percentuale); - static int date2esc(const TDate& d, int* prev = NULL); - static bool detraibile(int tipodet); - - bool bill_used(const TConto& conto) const; - int bill2contr(const TConto& c, char sezione) const; - - bool read_caus(const char* cod, int year); - TCausale& causale() { return _causale; } - TLibro_giornale& giornale() { return _giornale; } - - void set_cgs_imp(int n, const real& importo); - real get_cgs_imp(int n); - void add_cgs_imp(int n, const real& importo); - void cgs_pack(); - - void set_ivas_row(int n, const char* codiva, TConto& tc, const char* desc); - int set_cgs_row(int n, const real& importo, TConto& conto, const char* desc, char tipo); - void disable_cgs_cells(int n, char tipo); - void add_cgs_tot(TMask& m); - void add_cgs_rit(bool fisc); - -public: - TPrimanota_application(); -}; - -#endif +#ifndef __CG2102_H +#define __CG2102_H + +#ifndef __MSKSHEET_H +#include +#endif + +#ifndef __RELAPP_H +#include +#endif + +#ifndef __CGLIB_H +#include "cglib.h" +#endif + +#ifndef __CG2101_H +#include "cg2101.h" +#endif + +#ifndef __CG2103_H +#include "cg2103.h" +#endif + +class TPrimanota_application : public TRelation_application +{ + TMovimentoPN* _rel; + TLocalisamfile* _tabcom; + TLocalisamfile* _tab; + TLocalisamfile* _caus; + TLocalisamfile* _rcaus; + TLocalisamfile* _clifo; + TLocalisamfile* _pcon; + TLocalisamfile* _attiv; + TLocalisamfile* _saldo; + TLocalisamfile* _comuni; + TLocalisamfile* _occas; + TLocalisamfile* _nditte; + + TCausale _causale; // Causale corrente + TLibro_giornale _giornale; // Libro giornale corrente + TipoIVA _iva; // Tipo di IVA corrente + + TSaldo_agg _saldi; // Saldi da aggiornare + + TMask* _msk[4]; // Maschere di query, cg, iva + long _lastreg; // Numero ultima registrazione + int _mode; // Modo maschera corrente + + static bool suspended_handler(TMask_field& f, KEY k); + static bool num_handler(TMask_field& f, KEY key); + static bool caus_query_handler(TMask_field& f, KEY key); + static bool caus_modify_handler(TMask_field& f, KEY key); + static bool datareg_handler(TMask_field& f, KEY key); + static bool datacomp_handler(TMask_field& f, KEY key); + static bool data74ter_handler(TMask_field& f, KEY key); + static bool reg_handler(TMask_field& f, KEY key); + static bool clifo_handler(TMask_field& f, KEY key); + static bool totale_handler(TMask_field& f, KEY key); + static bool ritfis_handler(TMask_field& f, KEY key); + static bool ritsoc_handler(TMask_field& f, KEY key); + static bool visval_handler(TMask_field& f, KEY key); + static bool occas_code_handler(TMask_field& f, KEY key); + static bool occas_handler(TMask_field& f, KEY key); + static bool solaiva_handler(TMask_field& f, KEY key); + static bool descr_handler(TMask_field& f, KEY k); + static bool imponibile_handler(TMask_field& f, KEY key); + static bool codiva_handler(TMask_field& f, KEY key); + static bool imposta_handler(TMask_field& f, KEY key); + static bool dareavere_handler(TMask_field& f, KEY k); + static bool conto_handler(TMask_field& f, KEY key); + + static bool iva_notify(int r, KEY key); + static bool iva_handler(TMask_field& f, KEY key); + static bool cg_notify(int r, KEY key); + static bool cg_handler(TMask_field& f, KEY key); + +protected: + virtual bool user_create(); + virtual bool user_destroy(); + virtual bool changing_mask(int mode); + virtual TMask* get_mask(int mode); + virtual TRelation* get_relation() const { return _rel; } + virtual void init_query_mode(TMask& m); + virtual void init_insert_mode(TMask& m); + virtual void init_modify_mode(TMask& m); + virtual const char* get_next_key(); + virtual int read(TMask& m); + virtual int write(const TMask& m); + virtual int rewrite(const TMask& m); + virtual bool remove(); + + void genera_incasso(const char* causimm); + + TMask* load_mask(int n); + TMask& occas_mask() { return *load_mask(3); } + + bool get_conto(int r, TConto& c) const; + void mask2rel(const TMask& m); + + TipoIVA iva() const { return _iva; } + TMask& TPrimanota_application::mask() const; + + void fill_sheet(TMask& m) const; + void init_mask(TMask& m); + + TSheet_field& cgs() const; + TSheet_field& ivas() const; + + static real imp2sez(const real& importo); + static int bill2pos(const TConto& conto, char tipo); + static TipoIVA reg2IVA(const char* registro, int anno); + static TipoIVA cau2IVA(const char* causale, int anno); + static int type2pos(char tipo); + static const real& cod2IVA(const char* codiva); + static real scorpora(real& imponibile, const real& percentuale); + static int date2esc(const TDate& d, int* prev = NULL); + static bool detraibile(int tipodet); + + bool bill_used(const TConto& conto) const; + int bill2contr(const TConto& c, char sezione) const; + + bool read_caus(const char* cod, int year); + TCausale& causale() { return _causale; } + TLibro_giornale& giornale() { return _giornale; } + + void set_cgs_imp(int n, const real& importo); + real get_cgs_imp(int n); + void add_cgs_imp(int n, const real& importo); + void cgs_pack(); + + void set_ivas_row(int n, const char* codiva, TConto& tc, const char* desc); + int set_cgs_row(int n, const real& importo, TConto& conto, const char* desc, char tipo); + void disable_cgs_cells(int n, char tipo); + void add_cgs_tot(TMask& m); + void add_cgs_rit(bool fisc); + +public: + TPrimanota_application(); +}; + +#endif