#include #include #include #include #include "cg2100.h" #include "cg2102.h" #include #include // Certified 100% inline TPrimanota_application& app() { return (TPrimanota_application&)main_app(); } /////////////////////////////////////////////////////////// // Funzioni di decodifica/calcolo /////////////////////////////////////////////////////////// // Determina il tipo IVA da causale+anno // Certified 100% TipoIVA TPrimanota_application::cau2IVA(const char* causale, int annoiva) { TipoIVA i = nessuna_iva; if (*causale > ' ') { TCausale& c = app().causale(); if (c.read(causale, annoiva)) i = c.iva(); else { error_box("Causale errata: '%s'", causale); i = iva_errata; } } return i; } // Calcolo della percentuale di un dato codice IVA // Certified 99% const real& TPrimanota_application::cod2IVA(const TMask& m) { static TString16 _codiva; static real _percent; if (app().iva() == iva_acquisti && m.get_int(103) == 3) return ZERO; const TString& codiva = m.get(102); if (_codiva != codiva) { _codiva = codiva; TCodiceIVA c(_codiva); _percent = c.percentuale(); } return _percent; } real TPrimanota_application::scorpora(real& imponibile, const real& percent) { real imposta = abs(imponibile) * percent / (percent + 100.0); imposta.ceil(); if (imponibile.sign() < 0) imposta = -imposta; imponibile -= imposta; return imposta; } // Calcola il totale del documento tenenod conto del segno della prima riga e di quella delle // ritenute sociali sulla causale real TPrimanota_application::totale_documento() { const TMask& m = curr_mask(); const bool swapt = test_swap(FALSE); // Totale invertito ? const bool swaps = test_swap(TRUE); // Ritenute sociali invertite ? real tot(m.get(F_TOTALE)); // Legge totale const real ritfis(m.get(F_RITFIS)); tot += ritfis; // Somma ritenute fiscali real ritsoc(m.get(F_RITSOC)); if (swapt ^ swaps) ritsoc = -ritsoc; tot += ritsoc; // Somma ritenute sociali con segno return tot; } // Determina se un codice sospeso o no // Certified 90% 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(TToken_string& row) { const int tipo_det = row.get_int(2); // Leggi tipo detraibilita if (tipo_det != 0) return FALSE; if (app().iva() == iva_vendite) // Vendite sempre detraibili return TRUE; TRegistro& reg = app().causale().reg(); TString16 chiave; chiave << app().curr_mask().get(F_ANNOIVA) << reg.attivita(); int tipoatt = 1; if (reg.attivita_mista()) // Se attivita mista ... { TBill bill(row, 5, 0x1); tipoatt = bill.tipo_att(); // ... determina attivita } chiave << tipoatt; // anno|attivita|tipo_attivita 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); const int l = s.len()-1; if (l > 0 && s[l] == tipo) return i; } return -1; } int TPrimanota_application::bill2pos(const TBill& conto, char tipo) { TSheet_field& cg = app().cgs(); for (int i = 0; i < cg.items(); i++) { TToken_string& s = cg.row(i); const int l = s.len()-1; if (l > 0 && s[l] == tipo) { const TBill c(s, 3, 0x0); if (c == conto) return i; } } return -1; } int TPrimanota_application::bill2contr(const TBill& 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 TBill c(row, 3, 0x0); if (conto == c) return i; } return -1; } // Controlla se un conto e' usato nelle righe IVA int TPrimanota_application::bill_used(const TBill& conto) const { int users = 0; const TArray& rows = ivas().rows_array(); for (int i = 0; i < rows.items(); i++) { TToken_string& row = (TToken_string&)rows[i]; if (!row.empty_items()) { const TBill c(row, 6, 0x0); if (conto == c) users++; } } return users; } int TPrimanota_application::det_used(char det) const { int users = 0; const bool detraib = det == 'D'; const TArray& rows = ivas().rows_array(); for (int i = 0; i < rows.items(); i++) { TToken_string& row = (TToken_string&)rows[i]; if (!row.empty_items()) { const bool d = detraibile(row); if (detraib == d) users++; } } return users; } /////////////////////////////////////////////////////////// // Gestione sheet CG /////////////////////////////////////////////////////////// TSheet_field& TPrimanota_application::cgs() const { TSheet_field& s = (TSheet_field&)curr_mask().field(F_SHEETCG); return s; } // Certified 99% // Scrive l'importo imp nella opportuna sezione della riga n void TPrimanota_application::set_cgs_imp(int n, const TImporto& imp) { imp.add_to(cgs().row(n)); cgs().force_update(n); } // Legge l'importo della riga n e lo ritorna col segno dovuto // Certified 99% TImporto TPrimanota_application::get_cgs_imp(int n) { TImporto importo; importo = cgs().row(n); return importo; } // Certified 90% TImporto TPrimanota_application::add_cgs_imp(int n, const TImporto& imp) { TImporto tot(get_cgs_imp(n)); tot.set(imp.sezione(), tot.valore() + imp.valore()); set_cgs_imp(n, tot); return tot; } // Certified 90% TImporto TPrimanota_application::sub_cgs_imp(int n, const real& imp) { TImporto tot(get_cgs_imp(n)); tot.set(tot.sezione(), tot.valore() - imp); set_cgs_imp(n, tot); return tot; } TImporto TPrimanota_application::real2imp(const real& r, char row_type) { bool dare; if (row_type == 'S') { dare = causale().sezione_ritsoc() == 'D'; } else { dare = causale().sezione_clifo() == 'D'; if (row_type != 'T' && row_type != 'F') dare = !dare; } TImporto importo(dare ? 'D' : 'A', r); return importo; } // Disabilita le celle della riga contabile n in base al suo tipo void TPrimanota_application::disable_cgs_cells(int n, char tipo) { int last = 0; switch(tipo) { case 'D': // IVA Detraibile case 'F': // Ritenute Fiscali case 'N': case 'S': // Ritenute Sociali case 'T': // Totale documento last = 3; // IVA Non detraibile break; case 'I': last = 7; // Imponibile break; default: last = 0; // Solo contabile break; } if (last > 0) { TSheet_field& cg = cgs(); for (int i = 0; i < last; i++) cg.disable_cell(n, i); if (tipo == 'T' && !causale().corrispettivi()) { cg.disable_cell(n, 5); cg.disable_cell(n, 6); } } } void TPrimanota_application::reset_cgs_row(int n) { TSheet_field& cg = cgs(); cg.row(cg.items()); // Append a new line cg.destroy(n); // Remove line n } int TPrimanota_application::set_cgs_row(int n, const TImporto& imp, TBill& conto, const char* desc, char tipo) { TSheet_field& cg = cgs(); if (n < 0) n = cg.first_empty(); TToken_string& row = cg.row(n); row = ""; imp.add_to(row); row.add(conto.string(0x3)); row.add(""); // Codice decrizione row.add(desc); // Descrizione aggiuntiva switch(tipo) // Calcolo contropartita { case 'T': { TToken_string& irow = ivas().row(0); for (int i = 5; i < 10; i++) row.add(irow.get(i == 5 ? 5 : -1)); } break; default: { const int pos = type2pos('T'); if (pos >= 0) { TBill contro(cg.row(pos), 2, 0x3); row.add(contro.string(0x3)); } else row.add(" | | | | "); } break; } 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 TImporto& imp = get_cgs_imp(i); if (imp.valore() == ZERO) del = TRUE; } if (del) rows.destroy(i, FALSE); } rows.sort(compare_rows); // Pack and sort array } real TPrimanota_application::calcola_saldo() const { TArray& rows = cgs().rows_array(); const int max = rows.items(); real tdare, tavere; for (int i = 0; i < max; i++) { TToken_string& r = (TToken_string&)rows[i]; tdare += real(r.get(0)); tavere += real(r.get()); } real sbilancio = abs(tdare)-abs(tavere); switch (sbilancio.sign()) { case 1: curr_mask().set(F_DARE, (tdare-tavere).string()); curr_mask().reset(F_AVERE); break; case -1: curr_mask().reset(F_DARE); curr_mask().set(F_AVERE, (tavere-tdare).string()); break; default: curr_mask().reset(F_DARE); curr_mask().reset(F_AVERE); break; } return sbilancio; } // Handler dello sheet di contabilita' // Certified 90% bool TPrimanota_application::cg_handler(TMask_field& f, KEY k) { if ((k == K_TAB && !f.mask().is_running()) || k == K_ENTER) { const real saldo = app().calcola_saldo(); if (k == K_ENTER) { if (saldo != ZERO) { const char* ss = saldo.string("."); return f.error_box("Il movimento e' sbilanciato di %s lire.", ss); } TSheet_field& cg = app().cgs(); bool empty = TRUE; const int max = cg.items(); for (int i = 0; i < max; i++) { const TImporto im(app().get_cgs_imp(i)); if (!im.valore().is_zero()) { const TBill c(cg.row(i), 3, 0x0); if (!c.ok()) return f.error_box("Il conto della riga %d non e' completo", i+1); const TBill co(cg.row(i), 10, 0x0); if (!c.empty() && !co.ok()) return f.error_box("La contropartita della riga %d non e' completa", i+1); empty = FALSE; } } if (empty) return error_box("Il movimento non ha nessuna riga contabile con un importo"); } } 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]; switch(k) { case K_SPACE: cg.sheet_mask().enable(DLG_DELREC, tipo == ' '); break; case K_ENTER: if (r == 0 && app().iva() == nessuna_iva && cg.row(1).empty_items()) { TImporto i; i = row; if (i.valore() != 0.0) { TBill contro(row, 9, 0x3); if (contro.ok()) { i.swap_section(); app().set_cgs_row(1, i, contro, "", ' '); TBill conto(row, 2, 0x3); conto.add_to(cg.row(1), 9, 0x3); app().cgs().force_update(1); } } } app().calcola_saldo(); break; case K_DEL: if (tipo != ' ') return error_box("La riga %d non puo' essere cancellata", r+1); break; default: break; } 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 f.error_box("La descrizione del documento e' 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, TBill& tc, const char* desc) { TToken_string& riga = ivas().row(nriga); riga = " "; // Importo riga.add (codiva); // codiva riga.add (" | "); // Det - Imposta riga.add(tc.string(0x7)); // Conto riga.add(desc); // Descrizione } bool TPrimanota_application::imponibile_handler(TMask_field& f, KEY key) { if (key == K_TAB && f.dirty()) { TMask& m = f.mask(); TString16 iva(m.get(102)); if (iva.empty()) { iva = app().curr_mask().get(F_CODIVA); m.set(102, iva); } if (iva.not_empty() && !app().causale().corrispettivi()) { const real& percent = cod2IVA(m); const real imponibile(f.get()); real imposta = abs(imponibile) * percent / 100.0; imposta.ceil(); if (imponibile.sign() < 0) imposta = -imposta; m.set(104, imposta.string()); } } return TRUE; } bool TPrimanota_application::codiva_handler(TMask_field& f, KEY key) { if (key == K_TAB && f.dirty()) { TMask& m = f.mask(); TCodiceIVA iva(f.get()); TBill b; app().IVA2bill(iva, b); const char tipo[2] = { b.tipo(), '\0' }; m.set(106, tipo); m.set(107, b.gruppo()); m.set(108, b.conto()); const short id = b.tipo() == 'C' ? 209 : (b.tipo() == 'F' ? 309 : 109); m.set(id, b.sottoconto()); m.set(id+1, b.descrizione()); TMask_field& im = m.field(101); im.set_dirty(); im.on_hit(); } else if (key == K_ENTER) { if (f.get().empty() && f.mask().get(101).not_empty()) return f.error_box("Codice IVA obbligatorio"); } return TRUE; } bool TPrimanota_application::detrazione_handler(TMask_field& f, KEY key) { if (key == K_SPACE && app().iva() == iva_acquisti) { TMask_field& ci = f.mask().field(101); ci.set_dirty(); ci.on_hit(); } 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()); real imposta = abs(imponibile) * percent / 100.0; imposta.ceil(); if (imponibile.sign() < 0) imposta = -imposta; const real val(f.get()); if (val != imposta) { if (val != 0.0 || !app().causale().corrispettivi()) { const TString16 wrong(val.string(".")); const TString16 right(imposta.string(".")); f.warning_box("Imposta di '%s' errata: dovrebbe essere '%s'", (const char*)wrong, (const char*)right); } } } else if (key == K_F8) { real imposta(f.get()); if (imposta.is_zero()) { real imponibile(f.mask().get(101)); const real& percent = cod2IVA(f.mask()); imposta = scorpora(imponibile, percent); f.mask().set(101, imponibile.string()); f.set(imposta.string()); } else f.warning_box("Cancellare l'imposta (tasto F2) prima di effettuare lo scorporo"); } return TRUE; } real TPrimanota_application::calcola_imp() const { TArray& rows = ivas().rows_array(); const int max = rows.items(); real imponibili, imposte; for (int r = 0; r < max; r++) { TToken_string& row = (TToken_string&)rows[r]; imponibili += real(row.get(0)); imposte += real(row.get(3)); } curr_mask().set(F_IMPONIBILI, imponibili.string()); curr_mask().set(F_IMPOSTE, imposte.string()); return imponibili+imposte; } // Certified 50% bool TPrimanota_application::iva_notify(int r, KEY k) { static int oldpos,oldposiva; static real oldimp, oldiva; TSheet_field& iva = app().ivas(); TToken_string& row = iva.row(r); const TCausale& cau = app().causale(); if (k == K_SPACE) { oldimp = real(row.get(0)); // Imponibile 0 oldiva = real(row.get(3)); // Imposta 3 if (oldiva.is_zero() && app().causale().corrispettivi()) { const TCodiceIVA i(row.get(1)); const real percent = i.percentuale(); oldiva = scorpora(oldimp, percent); } const char tipod = detraibile(row) ? 'D' : 'N'; oldposiva = type2pos(tipod); if (oldposiva < 0 && oldiva != ZERO) { const int ri = tipod == 'D' ? 3 : 4; TBill c; cau.bill(ri, c); const TString80 d(cau.desc_agg(ri)); oldposiva = app().set_cgs_row(-1, app().real2imp(ZERO, 'I'), c, d, tipod); } TBill oldconto(row, 6, 0x0); // g/c/s 6 7 8 oldpos = bill2pos(oldconto, 'I'); if (oldpos < 0 && oldconto.ok()) { const TString80 d(cau.desc_agg(2)); oldpos = app().set_cgs_row(-1, app().real2imp(ZERO, 'I'), oldconto, d, 'I'); } } if (k == K_DEL) { row.add("0", 0); // Azzera imponibile row.add("0", 3); // Azzera imposta k = K_ENTER; // Elegante o Sporco trucco (dipende dai gusti!) } if (k == K_ENTER) { int delimp = -1, deliva = -1; if (oldpos >= 0) // Il conto esisteva anche prima { const TImporto& imp = app().sub_cgs_imp(oldpos, oldimp); if (imp.valore().is_zero) delimp = oldpos; } if (oldposiva >= 0) // Il conto IVA esisteva anche prima { const TImporto& imp = app().sub_cgs_imp(oldposiva, oldiva); if (imp.valore().is_zero) deliva = oldposiva; } // Aggiorna conto sulla riga contabile real imp(row.get(0)); // Imponibile real imposta(row.get(3)); // Imposta if (imposta.is_zero() && app().causale().corrispettivi()) { const TCodiceIVA i(row.get(1)); const real percent = i.percentuale(); imposta = scorpora(imp, percent); } TBill conto(row, 5, 0x3); int newpos = bill2pos(conto, 'I'); TString80 d; if (newpos < 0) { if (delimp >= 0) app().reset_cgs_row(delimp); const TImporto val(app().real2imp(imp, 'I')); if (val.valore() != ZERO) { d = cau.desc_agg(2); app().set_cgs_row(-1, val, conto, d, 'I'); } } else { TImporto val(app().real2imp(imp, 'I')); val = app().add_cgs_imp(newpos, val); if (val.valore().is_zero()) { app().reset_cgs_row(newpos); newpos = -1; } } oldimp = imp; oldpos = newpos; // Aggiorna conto IVA sulla riga contabile const bool detrarre = detraibile(row); // Determina se IVA detraibile const int ri = detrarre ? 3 : 4; app().causale().bill(ri, conto); const char tipod = detrarre ? 'D' : 'N'; int newposiva = type2pos(tipod); if (newposiva < 0) { const TImporto val(app().real2imp(imposta, 'I')); if (val.valore() != ZERO) { d = cau.desc_agg(ri); newposiva = app().set_cgs_row(-1, val, conto, d, tipod); } } else { TImporto val(app().real2imp(imposta, 'I')); val = app().add_cgs_imp(newposiva, val); if (val.valore().is_zero()) { app().reset_cgs_row(newposiva); newposiva = -1; } } oldiva = imposta; oldposiva = newposiva; if (r == 0) app().add_cgs_tot(app().curr_mask()); app().calcola_imp(); app().calcola_saldo(); } return TRUE; } // Handler dello sheet di contabilita' // Certified 90% bool TPrimanota_application::iva_handler(TMask_field& f, KEY k) { if ((k == K_TAB && !f.mask().is_running()) || k == K_ENTER) { const real imp = app().calcola_imp(); const real tot = app().totale_documento(); if (k == K_ENTER) { 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)", (const char*)t, (const char*)i); } TSheet_field& iva = app().ivas(); for (int i = 0; i < iva.items(); i++) { TToken_string& row = iva.row(i); const real im(row.get(0)); if (!im.is_zero()) { const TBill c(row, 6, 0x0); if (!c.ok()) return f.error_box("Il conto della riga iva %d non e' completo", i+1); } } } } return TRUE; } bool TPrimanota_application::cg_conto_handler(TMask_field& f, KEY key) { if (key == K_TAB || key == K_ENTER) { TMask& m = f.mask(); if (m.get(115)[0] == 'T') // Se riga totale documento { const char cf = m.get(f.dlg()-2)[0]; char tipo = app().clifo(); if (app().causale().corrispettivi()) tipo = ' '; if (cf != tipo) { const char* d = tipo == ' ' ? "normale" : (tipo == 'C' ? "clienti" : "fornitori"); return f.error_box("E' richiesto un conto %s.", d); } } } return TRUE; } bool TPrimanota_application::iva_sottoconto_handler(TMask_field& f, KEY key) { if (!suspended_handler(f, key)) return FALSE; if (key == K_TAB && f.dirty() && !app().causale().corrispettivi()) { TMask& m = f.mask(); const TFixed_string td(app().causale().tipo_doc()); if (td == "FV" || td == "NC") // Nun lo vulevo fa' CASAP { const int cr = m.get_int(105); if (cr == 2 || cr == 3) m.set(105, 4); } } return TRUE; } bool TPrimanota_application::sheet_clifo_handler(TMask_field& f, KEY k) { if (!suspended_handler(f, k)) return FALSE; if (k == K_TAB || k == K_ENTER) { TMask& m = f.mask(); const short cid = 100 + (f.dlg() % 100) -1; const int conto = m.get_int(cid); if (conto == 0) { const long codice = atol(f.get()); if (codice > 0L) { TBill c(0, 0, codice, m.get(cid-2)[0]); c.descrizione(); // Carica gruppo e conto m.set(cid-1, c.gruppo()); m.set(cid, c.conto()); } } } 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_AUTO_ENTER); } return TRUE; } // Handler of the F_CODCAUS field on the query mask // Certified 99% 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); // Cerca causale e suo tipo if (i != iva_errata) { const bool ok = suspended_handler(f, key); // Controlla sospensione if (ok) f.mask().stop_run(K_INS); // Entra in modo inserimento } else return FALSE; } 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 TString16 cau(f.get()); const TCausale c(cau, ann); if (!c.ok()) return FALSE; ok = app().causale().similar(c); if (!ok) { f.error_box("Causale incongruente con quella inserita precedentemente"); return FALSE; } app().causale().read(cau, ann); } return TRUE; } // Handler of the F_DATAREG field // Certified 70% bool TPrimanota_application::datareg_handler(TMask_field& f, KEY key) { bool ok = TRUE; if ((key == K_TAB && f.focusdirty()) || key == K_ENTER) { 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 (ae == 0) return f.error_box("La data dell'operazione non appartiene a nessun esercizio"); if (m.query_mode() || app().giornale().year() != ae) ok = app().giornale().read(ae); else ok = TRUE; if (!ok) return f.error_box("Non esiste il libro giornale dell'esercizio %d", ae); if (f.dirty() || f.mask().query_mode()) { const TLibro_giornale& gio = app().giornale(); if (dr <= gio.last_print()) return f.error_box("La data dell'operazione e' antecedente al %s,\n" "ultima stampa del libro giornale dell'esercizio %d", gio.last_reg().string(), ae); if (dr < gio.last_reg()) f.warning_box("La data dell'operazione e' antecedente al %s,\n" "ultima registrazione sul libro giornale dell'esercizio %d", gio.last_reg().string(), ae); if (m.query_mode()) { const long numreg = m.get_long(F_NUMREG); if (numreg > 0) { TLocalisamfile& mov = app().get_relation()->lfile(); mov.put(MOV_NUMREG, numreg); if (mov.read() == NOERR) m.set(F_CODCAUS, mov.get(MOV_CODCAUS)); } // Forza aggiornamento registro causale essendo cambiato l'anno const bool ok = app().causale().read(m.get(F_CODCAUS), dr.year()); if (!ok) return FALSE; } TRegistro& reg = app().causale().reg(); const TString16 codreg(reg.name()); if (codreg.not_empty()) { if (reg.year() != dr.year()) { const bool ok = reg.reread(); if (!ok) return FALSE; } if (dr < reg.last_print()) return f.error_box("La data dell'operazione e' antecedente al %s,\n" "ultima stampa del registro '%s' dell'anno %d", reg.last_print().string(), (const char*)codreg, dr.year()); if (dr < reg.last_reg()) f.warning_box("La data dell'operazione e' antecedente al %s,\n" "ultima registrazione sul registro '%s' dell'anno %d", reg.last_reg().string(), (const char*)codreg, dr.year()); } } } return ok; } // Handler of the F_DATACOMP field on the modify mask // Certified 90% bool TPrimanota_application::datacomp_handler(TMask_field& f, KEY key) { if (key == K_TAB || key == K_ENTER) { const TDate dc(f.get()); // Data di competenza const int ae = date2esc(dc); // Esercizio corrispondente TMask& m = f.mask(); const char* data = ""; if (f.dlg() == F_DATACOMP) { m.set(F_ANNOES, ae); data = "di competenza"; } else data = "del 74/ter"; if (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; e << "La data " << data << " deve appartenere all'esercizio " << ar; if (pr > 0) e << " o al " << pr; return f.error_box(e); } } else return f.error_box("La data %s non appartiene a nessun esercizio", data); } return TRUE; } // Handler of the F_DATA74TER field on the modify mask // Certified 90% 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 (key == K_TAB) { TRegistro& reg = app().causale().reg(); const bool av = reg.agenzia_viaggi(); f.mask().show(F_DATA74TER, av); if (!av) f.mask().reset(F_DATA74TER); } 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(_isequal) == NOERR) { f.mask().autoload(&occas); f.mask().send_key(K_TAB, O_COMUNE); // Forza decodifica comuni 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(); f.set_focus(); } 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 char tipo = app().clifo(); 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 TBill bill; _causale.bill(1, bill); if (conto == 0) { gruppo = bill.gruppo(); m.set(F_GRUPPOCLIFO, gruppo); conto = bill.conto(); m.set(F_CONTOCLIFO, conto); codice = bill.sottoconto(); } if (tipo == 'C' && causale().corrispettivi()) { codice = bill.sottoconto(); tipo = ' '; } TBill c(gruppo, conto, codice, tipo); real tot(m.get(F_TOTALE)); // Creazione/Aggiornamento riga totale const int pos = type2pos('T'); set_cgs_row(pos, real2imp(tot, 'T'), c, m.get(F_DESCR), 'T'); calcola_saldo(); } // 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()) { TMask& m = f.mask(); app().add_cgs_tot(m); TLocalisamfile clifo(LF_CLIFO); const int alleg = clifo.get_int(CLI_ALLEG); TEdit_field& upi = m.efield(F_RIEPILOGO); upi.check_type(alleg == 3 ? CHECK_REQUIRED : CHECK_NORMAL); if (clifo.get_bool(CLI_OCCAS)) m.send_key(K_SPACE, F_OCCASEDIT); // Lancia maschera occasionali } return TRUE; } bool TPrimanota_application::IVA2bill(const TCodiceIVA& iva, TBill& bill) { const TCausale& cau = causale(); const TString& tipo = iva.tipo(); if (tipo.not_empty()) { if (tipo == "ES") cau.bill(5, bill); else if (tipo == "NI") cau.bill(6, bill); else if (tipo == "NS") cau.bill(7, bill); } if (!bill.ok() && !cau.corrispettivi()) { const TMask& m = curr_mask(); bill.set(m.get_int(F_GRUPPORIC), m.get_int(F_CONTORIC), m.get_long(F_SOTTOCONTORIC)); } if (!bill.ok()) cau.bill(2, bill); return bill.ok(); } // Handler of the F_CODIVA // Certified 99% bool TPrimanota_application::main_codiva_handler(TMask_field& f, KEY key) { if (key == K_TAB && f.get().not_empty()) { const real imp(app().ivas().row(1).get(0)); // Se il totale non e' stato spezzato if (imp == ZERO) { TMask& m = f.mask(); iva_notify(0, K_SPACE); const TCodiceIVA iva(f.get()); const bool corr = app().causale().corrispettivi(); real tot = app().totale_documento(); real imposta; if (!corr) imposta = app().scorpora(tot, iva.percentuale()); TToken_string& row = app().ivas().row(0); row.add(tot.string(), 0); // imponibile row.add(imposta.string(), 3); // imposta if (iva.codice() != row.get(1)) { row.add(iva.codice(), 1); // Aggiorna codice IVA TBill bill; // Aggiorna conto della prima riga IVA app().IVA2bill(iva, bill); bill.add_to(row, 4, 0x7); } app().ivas().force_update(0); iva_notify(0, K_ENTER); } } return TRUE; } // Handler of the F_TOTALE // Certified 99% bool TPrimanota_application::totale_handler(TMask_field& f, KEY key) { bool ok = TRUE; if (key == K_TAB && f.focusdirty()) { app().add_cgs_tot(f.mask()); f.mask().field(F_CODIVA).on_hit(); } if (key == K_ENTER && f.get().empty()) ok = f.yesno_box("Totale documento nullo: continuare ugualmente?"); return ok; } 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; TBill conto; _causale.bill(riga, conto); const TString80 desc(_causale.desc_agg(riga)); set_cgs_row(-1, real2imp(imp, tipo), conto, desc, tipo); } else set_cgs_imp(pos, real2imp(imp, tipo)); } // Handler of the F_PROTIVA bool TPrimanota_application::protiva_handler(TMask_field& f, KEY key) { if (key == K_ENTER && f.dirty() && f.mask().mode() == MODE_INS) { const long protiva = atol(f.get()); const long protocol = app().causale().reg().protocol() + 1; if (protiva != protocol) f.warning_box("Protocollo IVA fuori sequenza: %ld invece di %ld", protiva, protocol); } return TRUE; } // 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"); if (e > ZERO) m.set_exchange(e); else { error_box("Impostare un valore maggiore di zero per il cambio"); f.reset(); } } 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.show(F_SHEETCG, anchecg); m.show(F_DARE, anchecg); m.show(F_AVERE, 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); TToken_string oldrow(128); for (int i = 0; i < righe; i++) { TToken_string& r = iva.row(i); if (!r.empty_items()) { oldrow = r; r = ""; iva_notify(i, K_SPACE); r = oldrow; iva_notify(i, K_ENTER); } pi.setstatus(i+1); } app().fill_sheet(m); app().cgs().force_update(); m.set_focus(); } } return TRUE; }