diff --git a/cg/cg2100.cpp b/cg/cg2100.cpp index 1e610ae72..f33626ead 100755 --- a/cg/cg2100.cpp +++ b/cg/cg2100.cpp @@ -1,1369 +1,1369 @@ -#include -#include -#include -#include - -#include "pagament.h" - -#include "cg2100.h" -#include "cg2102.h" -#include "cg21sld.h" - -// Nomi dei campi -#include -#include -#include - -TPrimanota_application::TPrimanota_application() -: _rel(NULL), _mode(0), _iva(nessuna_iva), _causale(NULL), _giornale(NULL), - _lastreg(0), _last_dreg(TODAY), _last_dcom(TODAY) -{ - memset(_msk, 0, sizeof(_msk)); -} - -TMask* TPrimanota_application::load_mask(int n) -{ - int max_pages = 2; // Numero massimo di pagine della maschera - - if (n == 2) - { - if (is_fattura()) - max_pages = 3; - if (_msk[2] != NULL) - { - // Controlla se esiste pagina 3 - const int pages = (_msk[2]->id2pos(FS_RATESHEET) > 0) ? 3 : 2; - // Se i numeri di pagine non coincidono distruggi la maschera - if (pages != max_pages) - { - delete _msk[2]; - _msk[2] = NULL; - } - } - } - - if (_msk[n] != NULL) - return _msk[n]; - - if (n == 1 || n == 2) - { - for (int i = 3; i >= 0; i--) - { - if (_msk[i] != NULL) - { - delete _msk[i]; - _msk[i] = NULL; - } - } - } - - TFilename name("cg2100"); name << char(n == 3 ? 'o' : 'a'+n); - TMask* m = new TMask(name, 0, max_pages); - - switch (n) - { - case 0: - if (m) - { - 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"); - - // Scelta primo campo col focus - _firstfocus = c.get_bool("PoCuDr") ? F_DATAREG : F_CODCAUS; - m->first_focus(_firstfocus); - - // Registra e inserisci immediatamente un nuovo movimento - _savenew = !c.get_bool("Cg21SN"); - } - break; - case 2: - if (m) - { - m->set_handler(F_DATADOC, doc_handler); - m->set_handler(F_NUMDOC, doc_handler); - m->set_handler(F_DATA74TER, data74ter_handler); - m->set_handler(F_PROTIVA, protiva_handler); - m->set_handler(F_CLIENTE, clifo_handler); - m->set_handler(F_FORNITORE, clifo_handler); - m->set_handler(F_RITFIS, ritfis_handler); - m->set_handler(F_RITSOC, ritsoc_handler); - m->set_handler(F_CORRLIRE, corrlire_handler); - m->set_handler(F_CORRVALUTA, corrvaluta_handler); - m->set_handler(F_CODIVA, main_codiva_handler); - m->set_handler(F_OCCASEDIT, occas_handler); - m->set_handler(F_SOLAIVA, solaiva_handler); - m->set_handler(F_SHEETIVA, iva_handler); - m->set_handler(F_CODPAG, codpag_handler); - m->set_handler(SK_CAMBIO, cambio_handler); - - TSheet_field& is = (TSheet_field&)m->field(F_SHEETIVA); - is.set_notify(iva_notify); - TMask& ism = is.sheet_mask(); - ism.set_handler(101, imponibile_handler); - ism.set_handler(102, codiva_handler); - ism.set_handler(103, detrazione_handler); - ism.set_handler(104, imposta_handler); - ism.set_handler(109, iva_sottoconto_handler); - ism.set_handler(209, sheet_clifo_handler); - ism.set_handler(309, sheet_clifo_handler); - - // Se esiste lo sheet delle rate - if (is_fattura()) - { - TSheet_field& ps = (TSheet_field&)m->field(FS_RATESHEET); - ps.set_notify(pag_notify); - m->set_handler(FS_RATESHEET, pag_sheet_handler); - m->set_handler(F_NUMRIF, numrif_handler); - m->set_handler(FS_RESET, reset_handler); - m->set_handler(FS_NRATE, nrate_handler); - m->set_handler(FS_RECALC, recalc_handler); - m->set_handler(FS_NSCAB, codcab_handler); - m->set_handler(FS_VSCAB, codcab_handler); - } - } - case 1: - if (m) - { - m->first_focus(_firstfocus); - m->set_handler(F_DATAREG, datareg_handler); - m->set_handler(F_DATACOMP, datacomp_handler); - m->set_handler(F_DESCR, descr_handler); - m->set_handler(F_CODCAUS, caus_modify_handler); - m->set_handler(F_TOTALE, totdoc_handler); - m->set_handler(SK_TOTDOCVAL, totdocval_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, cg_conto_handler); - cgm.set_handler(106, suspended_handler); - cgm.set_handler(206, sheet_clifo_handler); - cgm.set_handler(306, sheet_clifo_handler); - cgm.set_handler(112, suspended_handler); - cgm.set_handler(113, suspended_handler); - cgm.set_handler(213, sheet_clifo_handler); - cgm.set_handler(313, sheet_clifo_handler); - } - _sheet_shown = FALSE; - break; - case 3: - m->set_handler(O_CODICE, occas_code_handler); - break; - default: - CHECKD(0, "Che ca$$o di maschera e' la ", n); - break; - } - - _msk[n] = m; - return m; -} - -void TPrimanota_application::open_files(int logicnum, ...) -{ - va_list marker; - va_start(marker, logicnum); - while (logicnum > 0) - { - CHECKD(_file.objptr(logicnum) == NULL, "File gia' aperto: ", logicnum); - _file.add(new TLocalisamfile(logicnum), logicnum); - logicnum = va_arg(marker, int); - } -} - -bool TPrimanota_application::user_create() -{ - open_files(LF_TABCOM, LF_TAB, LF_CAUSALI, LF_RCAUSALI, LF_CLIFO, LF_PCON, 0); - open_files(LF_ATTIV, LF_SALDI, LF_COMUNI, LF_OCCAS, LF_NDITTE, 0); - open_files(LF_SCADENZE, LF_PAGSCA, 0); - - _rel = new TMovimentoPN; - _rel->add(LF_PARTITE, "NREG=NUMREG", 2); // Collega la partita aperta dalla fattura - - _causale = new TCausale(); - _giornale = new TLibro_giornale(); - - set_search_field(F_NUMREG); // Set field for default search - - _pag = NULL; - _pag_rows = NULL; - _is_saldaconto = FALSE; - - load_mask(0); - - return TRUE; -} - - -bool TPrimanota_application::save_and_new() const -{ return _savenew; } - - -bool TPrimanota_application::user_destroy() -{ - for (int m = 3; m >= 0; m--) - if (_msk[m] != NULL) delete _msk[m]; - - if (_pag) delete _pag; - - delete _giornale; - delete _causale; - delete _rel; - close_files(); - - 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) -{ - bool ok = TRUE; - - if (cod != NULL) - ok = causale().read(cod, year); - - bool nob = FALSE, dob = FALSE; - TipoIVA iva = nessuna_iva; - _is_saldaconto = FALSE; - - if (ok) - { - _is_saldaconto = gestione_saldaconto() && causale().saldaconto(); - dob = _is_saldaconto | causale().data_doc(); - nob = _is_saldaconto | causale().num_doc(); - iva = causale().iva(); - } - - TMask* m = _msk[iva == nessuna_iva ? 1 : 2]; - - // Se sono chiamato dai campi di query ho belle che finito - if (_mode == MODE_QUERY || m == NULL) - return TRUE; - - const bool ins = m->insert_mode(); - - m->efield(F_NUMDOC).check_type(nob ? CHECK_REQUIRED : CHECK_NORMAL); // Num. doc. obbligatorio - m->efield(F_DATADOC).check_type(dob ? CHECK_REQUIRED : CHECK_NORMAL); // Data doc. obbligatoria - - const bool nota_credito = is_nota_credito(); - const bool show_games = is_pagamento() || nota_credito; - TMask& cgm = cgs().sheet_mask(); - cgm.set_handler(100, show_games ? showpartite_handler : NULL); // bottoncino riga - cgm.enable(100, show_games); - if (iva == nessuna_iva) - { - m->enable(F_PROVVISORIO, !_is_saldaconto); // Il saldaconto vieta i movimenti provvisori - m->show(SK_TIPORIGA, is_pagamento()); - } - else - { - m->efield(F_CODPAG).check_type(_is_saldaconto ? CHECK_REQUIRED : CHECK_NORMAL); - m->efield(F_CODPAG).enable(!nota_credito); - - m->show(F_ANNORIF, _is_saldaconto); // Mostra/nasconde anno e riferimento partita - m->show(F_NUMRIF, _is_saldaconto); - - TEdit_field& numrif = m->efield(F_NUMRIF); - numrif.set_justify(iva == iva_acquisti ? _num_for : _num_cli); - numrif.set_trim(!numrif.right_justified()); - - const bool av = causale().reg().agenzia_viaggi(); - m->show(F_DATA74TER, av); - if (!av) m->reset(F_DATA74TER); - - if (ins) - { - const long protiva = causale().reg().protocol() + 1; - m->set(F_PROTIVA, protiva, TRUE); // Aggiorna protocollo IVA - } - } - - if (ins && !ci_sono_importi()) - { - cgs().reset(); - for (int i = 1; i < causale().size(); i++) - { - const 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) - - TBill tc; causale().bill(nriga, tc); - if (tc.gruppo() <= 0) continue; // Considera solo gruppi validi - - if (tc.tipo() > ' ' && tc.ok()) - { - TBill clifo(0, 0, tc.sottoconto(), tc.tipo()); - if (clifo.find() && clifo.conto() != 0) // Legge gruppo e conto - tc = clifo; - } - - int err = 0; - if (tc.descrizione() == "Sconosciuto") err = 1; - else if (tc.sospeso()) err = 2; - - if (err) - { - error_box("Il conto della riga %d della causale e' %s", - i, err == 1 ? "sconosciuto" : "sospeso"); - continue; - } - - const char sezione = rcaus->get_char(RCA_SEZIONE); - const TImporto zero('D', ZERO); - const TString80 desc(causale().desc_agg(i)); - - if (nriga == 1) m->set(F_DESCR, desc); - - if (iva == nessuna_iva) - { - char tipr = ' '; - if (_is_saldaconto) - { - if (nriga < 13 && nriga != 10) continue; // Si considerano solo le spese - tipr = 'G'; - } - const int pos = set_cgs_row(-1, zero, tc, desc, tipr); - if (sezione > ' ') - cgs().disable_cell(pos, sezione == 'A' ? 0 : 1); - } - else - { - if (nriga >= 2 && nriga <= 9) - continue; // Conti per IVA detraibile e non, ritenute sociali e fiscali - - const char tipo = nriga == 1 ? 'T' : ' '; - if (nriga == 1 && tc.tipo() > ' ' && tc.ok()) - m->set(tc.tipo() == 'C' ? F_CLIENTE : F_FORNITORE, tc.sottoconto()); - set_cgs_row(-1,zero,tc,desc,tipo); - } - } - fill_sheet(*m); - } - - return ok; -} - - -// Certified 100% -void TPrimanota_application::print() -{ - TExternal_app stampa("cg3 -0"); - stampa.run(); -} - - -bool TPrimanota_application::changing_mask(int mode) -{ - if (mode == MODE_QUERY) - { - const bool flag = _mode != MODE_QUERY; -#if XVT_OS == XVT_OS_WIN - if (flag && !_sheet_shown) - { - curr_mask().show_page(1); - do_events(); - _sheet_shown = TRUE; - } -#endif - return flag; - } - _iva = iva_errata; - return TRUE; -} - - -TMask* TPrimanota_application::get_mask(int mode) -{ - int annoes = 0, annoiva = 0; - TString16 caus; - - _mode = mode; - - switch (mode) - { - case MODE_INS: - annoes = _msk[0]->get_int(F_ANNOES); - annoiva = _msk[0]->get_int(F_ANNOIVA); - caus = _msk[0]->get(F_CODCAUS); - break; - case MODE_MOD: - annoes = _rel->lfile().get_int("ANNOES"); - annoiva = _rel->lfile().get_int("ANNOIVA"); - caus = _rel->lfile().get("CODCAUS"); - break; - default: - return load_mask(0); - } - - if (giornale().year() != annoes) - giornale().read(annoes); - - const TCausale& c = causale(); - if (caus != c.codice() || (c.iva() != nessuna_iva && c.reg().year() != annoiva)) - read_caus(caus, annoiva); - - _iva = c.iva(); - return load_mask(_iva == nessuna_iva ? 1 : 2); -} - - -void TPrimanota_application::on_firm_change() -{ - TRelation_application::on_firm_change(); // May be useful, who knows? - - _rel->lfile().last(); - _lastreg = _rel->lfile().get_long(MOV_NUMREG); // Init last registration number - - _esercizi.update(); // Init exercises -} - -void TPrimanota_application::on_config_change() -{ - TConfig cnf(CONFIG_DITTA); - _ges_val = cnf.get_bool("GesVal"); - _ges_sal = cnf.get_bool("GesSal"); - _npart_is_prot = cnf.get_bool("RifPro"); - - _num_cli = cnf.get_bool("NrCliDx"); - _num_for = cnf.get_bool("NrForDx"); - TPartita::carica_allineamento(); -} - - -void TPrimanota_application::init_mask(TMask& m) -{ - disable_menu_item(M_FILE_PRINT); - - read_caus(NULL, 0); // Setta campi obbligatori - fill_sheet(m); - - const bool valintra = causale().valintra(); - const bool corrisp = causale().corrispettivi(); - - if (_iva == nessuna_iva) - { - m.show(-5, _is_saldaconto); // Abilita campi saldaconto - } - else - { - char clig, forg; - if (_iva == iva_acquisti) - { - forg = 's'; - clig = 'h'; - } - else - { - forg = 'h'; - clig = corrisp ? 'h' : 's'; - } - m.send_key(K_SHIFT+K_CTRL+clig, -1); // GROUP 1 (clienti) - m.send_key(K_SHIFT+K_CTRL+forg, -2); // GROUP 2 (fornitori) - if (corrisp) m.hide(F_STATOPAIV); - - // Show/Hide campi valuta: F_VALUTAINTRA, F_CAMBIOINTRA, F_CORRLIRE, F_CORRVAL (GROUP 4) - m.show(-4, valintra); - - m.show(F_CODIVA, m.mode() == MODE_INS); // Codice IVA standard - - if (is_fattura()) - { - const TPartita* game = partite().first(); - const bool scad_free = game == NULL || !game->esistono_abbuoni_diffcam(m.get_long(F_NUMREG)); - - m.enable(DLG_DELREC, scad_free); // Disabilita tasto cancella - m.enable(-1, scad_free); // Disabilita cliente - m.enable(-2, scad_free); - - m.enable(F_CODPAG, scad_free); // Disabilita codice pagamento - m.enable(FS_RESET, scad_free); // Disabilita tasto di reset - if (!scad_free) - m.set(FS_RECALC, "", TRUE); - m.enable(FS_RECALC, scad_free); // Disabilita ricalcolo automatico - } - } - - // Show/Hide campi valuta: F_VALUTA, F_CAMBIO, F_DATACAMBIO (GROUP 3) - const bool valuta = _ges_val && is_saldaconto() && causale().valuta(); - m.show(-3, valuta); -} - -void TPrimanota_application::init_query_mode(TMask& m) -{ - enable_menu_item(M_FILE_PRINT); - - m.set(F_DATAREG, _last_dreg); - m.set(F_DATACOMP, _last_dcom); -} - - -void TPrimanota_application::fill_sheet(TMask& m) const -{ - if (_iva != nessuna_iva || !is_saldaconto()) - { - 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 (int r = ivas.items(); r < 16; r++) ivas.row(r); - ivas.enable_column(2, _iva == iva_acquisti); // Tipo detrazione - ivas.enable_column(4, !m.insert_mode()); // Tipo costo ricavo - } -} - - -// Ritorna il prossimo numero di registrazione libero -// Certified 100% -const char* TPrimanota_application::get_next_key() -{ - _tmp.cut(0); - _tmp.add(F_NUMREG); _tmp.add(_lastreg+1); - - if (_rel->good()) // Not reinsert - { - TMask& m = curr_mask(); - if (m.insert_mode()) - { - _tmp.add(F_CODCAUS); _tmp.add(m.get(F_CODCAUS)); // Ricopia causale - _tmp.add(F_DATAREG); _tmp.add(m.get(F_DATAREG)); // data operazione - _tmp.add(F_DATACOMP); _tmp.add(m.get(F_DATACOMP)); // competenza - - if (iva() == iva_vendite) - { - _tmp.add(F_DATADOC); _tmp.add(m.get(F_DATADOC)); // Ricopia data documento - - const long n = m.get_long(F_NUMDOC); - if (n > 0) - { _tmp.add(F_NUMDOC); _tmp.add(n+1); } // incrementa numero documento - } - } - } - - return _tmp; -} - - -void TPrimanota_application::init_insert_mode(TMask& m) -{ - init_mask(m); - - m.first_focus(_firstfocus); - - if (causale().reg().agenzia_viaggi()) - m.set(F_DATA74TER, m.get(F_DATAREG)); - - if (iva() != nessuna_iva) - { - occas_mask().reset(); - - if (is_fattura()) - { - const TString16 dt(m.get(F_DATADOC)); - set_pagamento(NULL,dt); - set_scadenze(m); - } - else - set_pagamento(NULL,NULL); // Annulla pagamento - } - else - { - partite().destroy(); - set_pagamento(NULL,NULL); // Annulla pagamento - } - _saldi.reset(); // Inizializza saldi -} - - -void TPrimanota_application::init_modify_mode(TMask& m) -{ - init_mask(m); - calcola_saldo(); // Verifica eventuali sbilanci contabili -} - -// Controlla sulla causale se il segno del totale documento (ritsoc=FALSE) -// o quello delle ritenute sociali (ritsoc=TRUE) e' invertito rispetto al normale -bool TPrimanota_application::test_swap(bool ritsoc) -{ - const char sez = ritsoc ? causale().sezione_ritsoc() : causale().sezione_clifo(); - const bool s = (iva() == iva_vendite) ^ sez == 'D'; - return s; -} - - -int TPrimanota_application::read(TMask& m) -{ - m.autoload(_rel); - - const long numreg = _rel->curr().get_long(MOV_NUMREG); - - cgs().reset(); - - if (iva() != nessuna_iva) - { - ivas().reset(); - - const TString16 occode(_rel->lfile().get("OCFPI")); - occas_mask().set(O_CODICE, occode); - - const char clifo = _rel->lfile().get_char("TIPO"); - 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"); - } - - _saldi.reset(); // Azzera saldi - _saldi.set_movprovv(_rel->lfile().get_char("PROVVIS") > ' '); - _saldi.set_movap(causale().apertura()); - _saldi.set_anno_es(m.get_int(F_ANNOES)); - _saldi.set_num_ulmov(numreg); - _saldi.set_data_ulmov((TDate)m.get(F_DATAREG)); - - for (int i = 0; i < _rel->cg_items(); i++) - { - const TRectype& r = _rel->cg(i); - TToken_string& riga = cgs().row(i); // Vuota la riga - - TImporto import(r.get_char("SEZIONE"), r.get_real("IMPORTO")); - import.add_to(riga, 0); // Dare/Avere 101-102 - - TBill conto; conto.get(r); - riga.add(conto.string(0x3)); // Conto 103-107 - - _saldi.aggiorna(conto, import, FALSE); - - riga.add(""); // Codice descrizione 108 - riga.add(r.get("DESCR")); // Descrizione riga 109 - - conto.get(r, TRUE); - riga.add(conto.string(0x3)); // Contropartita 110-114 - riga.add(r.get("NUMGIO")); - const char tipo = r.get_char("ROWTYPE"); - riga.add(tipo); // Tipo di riga 115 - - 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(); - - const bool to_swap = test_swap(FALSE); - if (to_swap) - { - real totdoc(m.get(F_TOTALE)); - totdoc = -totdoc; - m.set(F_TOTALE, totdoc.string()); - } - for (i = 0; i < _rel->iva_items(); i++) - { - TRectype& r = _rel->iva(i); - TToken_string& riga = ivas().row(i); - - real imponibile(r.get("IMPONIBILE")); - if (to_swap) imponibile = -imponibile; - riga.add(imponibile.string()); // Imponibile 101 - - riga.add(r.get("CODIVA")); // IVA 102 - riga.add(r.get("TIPODET")); // Detrazione 103 - - real imposta(r.get("IMPOSTA")); - if (to_swap) imposta = -imposta; - if (imponibile.sign() * imposta.sign() < 0) - { - error_box("Registrazione con imponibile e imposta con segni discordi:\n" - "assegnato il segno dell'imponibile"); - imposta = -imposta; - } - riga.add(imposta.string()); // Imposta 104 - - TBill c; c.get(r); - c.add_to(riga, 4, 0x7); // Conto 105-110 - } - - calcola_imp(); // Calcola totale imponibile ed imposte - - partite().destroy(); - if (is_fattura()) // Ci sono scadenze - { - const TString16 cp(m.get(F_CODPAG)); - const TString16 dt(m.get(F_DATADOC)); - set_pagamento(cp, dt); - if (!read_scadenze(m)) - set_scadenze(m); - } - - return _rel->status(); -} - - -// Trasferisce i dati da maschera a movimento di prima nota -void TPrimanota_application::mask2rel(const TMask& m) -{ - m.autosave(_rel); - - const long numreg = m.get_long(F_NUMREG); - const TDate datareg(m.get(F_DATAREG)); - const int annoes = m.get_int(F_ANNOES); - - _saldi.set_movprovv(m.get(F_PROVVISORIO)[0] > ' '); - _saldi.set_movap(causale().apertura()); - _saldi.set_anno_es(annoes); - _saldi.set_num_ulmov(numreg); - _saldi.set_data_ulmov((TDate)m.get(F_DATAREG)); - - _rel->destroy_rows(numreg); // Destroy all records - cgs_pack(); // Destroy all null rows - - TArray& rows = cgs().rows_array(); - - // 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]; - - TImporto n; n = row; - const TBill conto(row, 2, 0x3); - _saldi.aggiorna(conto, n, TRUE); - - TRectype &r = _rel->cg(i); - r.zero(); - - r.put("NUMREG", numreg); // Numero registrazione - r.put("ANNOES", annoes); // Anno esercizio - r.put("DATAREG", datareg); // Data di registrazione - r.put("NUMRIG", i+1); // Numero riga - - r.put("SEZIONE", n.sezione()); // Sezione - r.put("IMPORTO", n.valore()); // Importo - conto.put(r); // Conto - - row.get(); // Codice descrizione (ignorato) - const char* descr = row.get(); // Descrizione aggiuntiva - if (i > 0 || m.get(F_DESCR) != descr) // Salva la prima descrizione se diversa da testata - r.put("DESCR", descr); // Descrizione riga - - r.put("TIPOCC", row.get()); // Contropartita - r.put("GRUPPOC", row.get()); - r.put("CONTOC", row.get()); - r.put("SOTTOCONTC", row.get()); - row.get(); // Descrizione contropartita - r.put("NUMGIO", row.get()); // Numero riga giornale - r.put("ROWTYPE", row.get()); // Tipo riga - } - } - - if (_iva == nessuna_iva) return; - - if (causale().corrispettivi()) - { - TRectype& rec = _rel->lfile().curr(); - rec.put("TIPO", ""); - rec.put("CODCF", ""); // Azzera il cliente nei movimenti dei corrispettivi - } - else - { - _rel->lfile().put("TIPO", clifo()); - - int err = ~NOERR; - if (m.field(F_OCCASEDIT).active()) // Se e' un occasionale - { - TRelation occas(LF_OCCAS); - const TMask& om = occas_mask(); - om.autosave(&occas); // Salva i dati anagrafici - - 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"); - } - - const bool intra = causale().intra(); - const bool to_swap = test_swap(FALSE); - if (to_swap) - { - real totale = _rel->lfile().get("TOTDOC"); - totale = -totale; - _rel->lfile().put("TOTDOC", totale); - } - - ivas_pack(); - TArray& irows = ivas().rows_array(); - for (int i = 0; i < irows.items(); i++) - { - TToken_string& row = (TToken_string&)irows[i]; - - TRectype &r = _rel->iva(i); - r.zero(); - r.put("ANNOES", annoes); // Anno d'esercizio della testata (che ca$$ata!) - r.put("NUMREG", numreg); - r.put("INTRA", intra); // Causale intra (che ca$$ata!) - r.put("NUMRIG", i+1); - - real imponibile(row.get(0)); - if (to_swap) imponibile = -imponibile; - r.put("IMPONIBILE", imponibile); - - r.put("CODIVA", row.get()); - r.put("TIPODET", row.get()); - - real imposta(row.get()); - if (to_swap) imposta = -imposta; - r.put("IMPOSTA", imposta); - - r.put("TIPOCR", row.get()); - - const TBill c(row, -1, 0x1); - const int rimp = bill2pos(c, 'I')+1; - r.put("RIGAIMP", rimp); - c.put(r); - } -} - - -void TPrimanota_application::check_saldi() -{ - TString_array& rows = cgs().rows_array(); - const int anno = _rel->lfile().get_int("ANNOES"); - - for (int i = 0; i < rows.items(); i++) - { - TToken_string& row = (TToken_string&)rows[i]; - TBill conto(row, 2, 0x3); - conto.find(); - const char sez = conto.sezione(); - if (sez > ' ') - { - const TConto* c = _saldi.find(conto, anno); - if (c && !c->saldo_finale().is_zero() && c->saldo_finale().sezione() != sez) - warning_box("Il conto della riga %i ha un saldo finale in %s, " - "contrariamente a quanto indicato sul piano dei conti", - i+1, sez == 'A' ? "dare" : "avere"); - } - } -} - - -int TPrimanota_application::write(const TMask& m) -{ - static int lasterr = NOERR; - - const long numreg = m.get_long(F_NUMREG); - if (numreg > _lastreg) _lastreg = numreg; // Aggiorna ultima registrazione libera - - if (lasterr == NOERR) - mask2rel(m); // Altrimenti raddoppia i saldi! - - _last_dreg = m.get(F_DATAREG); - _last_dcom = m.get(F_DATACOMP); - - const int err = _rel->write(TRUE); - if (err == NOERR) - { - _saldi.registra(); - check_saldi(); - - bool salvaconto = FALSE; - - if (iva() != nessuna_iva) - { - causale().reg().reread(); // Aggiorna protocollo IVA - if (is_saldaconto()) - { - switch (causale().tipomov()) - { - case tm_fattura: - write_scadenze(m); break; // Salva fattura - case tm_nota_credito: - salvaconto = TRUE; break; // Salva nota credito - default: - break; - } - } - else - { - // Genera incasso immediato se e' una fattura e non c'e' gestione saldaconto - if (!m.get_bool(F_SOLAIVA) && !gestione_saldaconto()) - { - const TString causimm(causale().causale_inc_imm()); - if (causimm.not_empty()) - genera_incasso(causimm); - } - } - } - else - { - salvaconto = is_pagamento(); - } - - if (salvaconto) - { - const long old_nreg = numreg - (lasterr == _isreinsert ? 1 : 0); - partite().update_reg(_rel->lfile().curr(), old_nreg); - partite().write(); - } - - link_m770(); - } - - lasterr = err; - return err; -} - - -int TPrimanota_application::rewrite(const TMask& m) -{ - mask2rel(m); - const int err = _rel->rewrite(TRUE); - if (err == NOERR) - { - _saldi.registra(); - check_saldi(); - - if (_is_saldaconto) - { - bool salvaconto = FALSE; - if (iva() != nessuna_iva) - { - if (causale().tipomov() == tm_nota_credito) - salvaconto = TRUE; - else - write_scadenze(m); - } - else - { - salvaconto = is_pagamento(); - } - - if (salvaconto) - { - partite().update_reg(_rel->curr()); - partite().rewrite(); - } - } - - link_m770(); - } - - return err; -} - - -bool TPrimanota_application::remove() -{ - const bool ok = TRelation_application::remove(); - if (ok) - { - _saldi.registra(); - check_saldi(); - - if (_is_saldaconto) - { - if (iva() == nessuna_iva) - { - const long nreg = curr_mask().get_long(F_NUMREG); - partite().add_numreg(nreg); - notify_cgline_deletion(-1); // Notify deletion of all cg lines - partite().rewrite(); - } - else - { - TMask& m = curr_mask(); - m.reset(F_ANNORIF); - m.reset(F_NUMRIF); - write_scadenze(m); - } - } - } - return ok; -} - - -HIDDEN bool incasso_handler(TMask_field& f, KEY k) -{ - if (k == K_ENTER) - { - TMask& m = f.mask(); - TImporto tot(m.get(I_SEZIONE1)[0], (real)m.get(F_TOTALE)); - - for (int i = 2; i < 5; i++) - { - const int sid = i == 2 ? I_SEZIONE2 : (i == 3 ? I_SEZIONE3 : I_SEZIONE4); - const real val(m.get(sid+1)); - const TImporto imp(m.get(sid)[0], val); - tot += imp; - } - if (!tot.is_zero()) - return f.error_box("Il movimento e' sbilaciato di %s", tot.valore().string(".")); - } - return TRUE; -} - - -void TPrimanota_application::genera_incasso(const char* causimm) -{ - TMask m("cg2100i"); - if (iva() == iva_acquisti) m.set_caption("Movimento di pagamento immediato"); - - m.set_handler(F_DATAREG, datareg_handler); - m.set_handler(F_DATACOMP, datacomp_handler); - m.set_handler(F_TOTALE, incasso_handler); - - TCausale caus(causimm); // Causale da usare - - if (caus.data_doc()) m.efield(F_DATADOC).check_type(CHECK_REQUIRED); - if (caus.num_doc()) m.efield(F_NUMDOC).check_type(CHECK_REQUIRED); - - m.set(F_DATAREG, curr_mask().get(F_DATAREG)); - m.set(F_DATACOMP, curr_mask().get(F_DATACOMP)); - m.set(F_DATADOC, curr_mask().get(F_DATADOC)); - m.set(F_NUMDOC, curr_mask().get(F_NUMDOC)); - m.set(F_NUMREG, _lastreg+1); // Incrementa numero di registrazione - m.set(F_DESCR, caus.desc_agg(1)); // Descrizione - m.set(F_CODCAUS, causimm); // Cambia causale - m.set(F_DESCRCAUS, caus.descrizione()); - - m.set(I_SEZIONE1, caus.sezione_clifo() == 'D' ? "D" : "A"); - m.set(F_TOTALE, curr_mask().get(F_TOTALE)); - - m.set(F_CLIFO, _rel->cg(0).get("TIPOC")); - m.set(F_GRUPPOCLIFO, _rel->cg(0).get("GRUPPO")); - m.set(F_CONTOCLIFO, _rel->cg(0).get("CONTO")); - const TString& clifo = _rel->cg(0).get("SOTTOCONTO"); - m.set(F_CLIENTE, clifo); m.set(F_FORNITORE, clifo); m.set(I_SOTTOCONTO, clifo); - - TBill conto; caus.bill(2, conto); // Conto della seconda riga della causale - m.set(I_SEZIONE2, caus.sezione_clifo() == 'D' ? "A" : "D"); - m.set(I_IMPORTO2, curr_mask().get(F_TOTALE)); - m.set(I_GRUPPO2, conto.gruppo()); - m.set(I_CONTO2, conto.conto()); - m.set(I_SOTTOCONTO2, conto.sottoconto()); - - if (m.run() == K_SAVE) - { - TMovimentoPN inc; // Nuovo movimento di incasso immediato - TRectype& curr = inc.curr(); - curr.zero(); - m.autosave(&inc); - - curr.put("TIPODOC", caus.tipo_doc()); // Tipo documento - curr.put("REG", caus.reg().name()); // Registro - curr.zero(MOV_TIPO); - curr.zero(MOV_CODCF); - curr.zero(MOV_TOTDOC); - curr.zero(MOV_ANNOIVA); - - const int annoes = m.get_int(F_ANNOES); - const TString16 datareg = m.get(F_DATAREG); - long num = m.get_long(F_NUMREG); - - inc.destroy_rows(num); - - TRectype& r = inc.cg(0); - r.zero(); - r.put("NUMREG", num); - r.put("NUMRIG", 1); - r.put("DATAREG", datareg); - r.put("ANNOES", annoes); - r.put("SEZIONE", m.get(I_SEZIONE1)); - r.put("IMPORTO", m.get(F_TOTALE)); - r.put("TIPOC", m.get(F_CLIFO)); - r.put("GRUPPO", m.get(F_GRUPPOCLIFO)); - r.put("CONTO", m.get(F_CONTOCLIFO)); - - short clifo_id; - switch (m.get(F_CLIFO)[0]) - { - case 'C': - clifo_id = F_CLIENTE; break; - case 'F': - clifo_id = F_FORNITORE; break; - default : - clifo_id = I_SOTTOCONTO; break; - } - r.put("SOTTOCONTO", m.get(clifo_id)); - r.put("DESCR", m.get(F_DESCR)); - r.put("TIPOCC", " "); - r.put("GRUPPOC", m.get(I_GRUPPO2)); - r.put("CONTOC", m.get(I_CONTO2)); - r.put("SOTTOCONTC", m.get(I_SOTTOCONTO2)); - - r.put("ROWTYPE", " "); - - for (int i = 2; i < 5; i++) - { - const int sid = i == 2 ? I_SEZIONE2 : (i == 3 ? I_SEZIONE3 : I_SEZIONE4); - const TString& imp = m.get(sid+1); - if (imp.empty()) break; - - TRectype& r = inc.cg(i-1); - r.zero(); - r.put("IMPORTO", imp); - r.put("NUMREG", num); - r.put("NUMRIG", i); - r.put("DATAREG", datareg); - r.put("ANNOES", annoes); - r.put("SEZIONE", m.get(sid)); - r.put("TIPOC", ' '); - r.put("GRUPPO", m.get(sid+2)); - r.put("CONTO", m.get(sid+3)); - r.put("SOTTOCONTO", m.get(sid+4)); - if (i == 2) - r.put("DESCR", caus.desc_agg(2)); - - r.put("TIPOCC", m.get(F_CLIFO)); - r.put("GRUPPOC", m.get(F_GRUPPOCLIFO)); - r.put("CONTOC", m.get(F_CONTOCLIFO)); - r.put("SOTTOCONTC", m.get(clifo_id)); - - r.put("ROWTYPE", " "); - } - - while (inc.write() == _isreinsert) // In caso di riscrittura - { - num++; // Incrementa numero registrazione - inc.lfile().put("NUMREG", num); - } - - if (inc.good()) - { - if (num > _lastreg) _lastreg = num; - - _saldi.reset(); // Inizializza saldi - _saldi.set_movprovv(FALSE); - _saldi.set_movap(FALSE); - _saldi.set_anno_es(m.get_int(F_ANNOES)); - _saldi.set_num_ulmov(_lastreg); - _saldi.set_data_ulmov((TDate)m.get(F_DATAREG)); - for (int r = 0; r < inc.cg_items(); r++) - { - const TRectype& rec = inc.cg(r); - TBill c; c.get(rec); - const TImporto im(rec.get_char("SEZIONE"), rec.get_real("IMPORTO")); - _saldi.aggiorna(c, im, TRUE); - } - _saldi.registra(); - check_saldi(); - } - } -} - -bool TPrimanota_application::is_percipient(long forn, char& tipper, long& codper) const -{ - TLocalisamfile fornitori(LF_CLIFO); - fornitori.put("TIPOCF", "F"); - fornitori.put("CODCF", forn); - fornitori.read(); - tipper = fornitori.get_char(CLI_TIPOAPER); - codper = fornitori.get_long(CLI_CODANAGPER); - return codper > 0; -} - -long TPrimanota_application::calcola_m770(int tipo_coll, real& spese, real& compenso, - real& iva, real& ritfis) -{ - const int m770 = causale().link_m770(); - long forn = 0; - - if (tipo_coll == 1) - { - TString_array& riva = ivas().rows_array(); - TCodiceIVA codiva; - for (int i = 0; i < riva.items(); i++) - { - TToken_string& row = riva.row(i); - codiva.read(row.get(1)); - const TString& tipoiva = codiva.tipo(); - - const real imponibile(row.get(0)); - if ((tipoiva == "ES" || tipoiva == "NI" ||tipoiva == "NS") && riva.items() > 1) - { - if (m770 != 5) - spese += imponibile; - } - else - compenso += imponibile; - - const real imposta(row.get(3)); - iva += imposta; - } - - i = type2pos('F'); - if (i >= 0) - { - TImporto imp; imp = cgs().row(i); - ritfis = imp.valore(); - } - } - - if (tipo_coll == 3 || tipo_coll == 7) - { - TImporto imp; - TString_array& rcg = cgs().rows_array(); - for (int i = 0; i < rcg.items(); i++) - { - TToken_string& row = rcg.row(i); - imp = row; - - const char cf = row.get_char(2); - if (cf == 'F') // Evviva, e' un fornitore! - { - const long cod = row.get_long(5); - char tipper; - long codper; - if (!is_percipient(cod, tipper, codper)) - continue; // Se non e' percipente ignoralo - - if (forn == 0 && tipo_coll == 3) - { - forn = cod; // E' il primo che trovo! - } - else // Fine ricerca. - { - forn = 0; - compenso = ZERO; - break; - } - compenso = imp.valore(); - } - } - } - - if (tipo_coll == 4) - { - TBill zio; causale().bill(2, zio); - - TString_array& rcg = cgs().rows_array(); - for (int i = 0; i < rcg.items(); i++) - { - TToken_string& r = rcg.row(i); - const TBill bill(r, 3, 0x0); - if (zio == bill) - { - TImporto imp; imp = r; - ritfis = imp.valore(); - break; - } - } - } - - if (tipo_coll == 6) - { - TString_array& rcg = cgs().rows_array(); - TImporto imp; - - for (int i = 0; i < rcg.items(); i++) - { - TToken_string& row = rcg.row(i); - imp = row; - compenso += imp.valore(); - } - } - - return forn; -} - -bool TPrimanota_application::link_m770() -{ - const int m770 = causale().link_m770(); - if (!has_module(4) || iva() == iva_vendite || m770 == 0) - return FALSE; - - int tipo_coll = 0; - char tipper = ' '; - long codper = 0; - real spese, compenso, imposte, ritenute; - - if (iva() == iva_acquisti) - { - if (m770 == 1 || m770 == 5) - { - const long forn = curr_mask().get_long(F_FORNITORE); - if (is_percipient(forn, tipper, codper)) - calcola_m770(tipo_coll = 1, spese, compenso, imposte, ritenute); - } - } - else // Movimento puramente contabile - switch (m770) - { - case 1: - { - tipo_coll = is_saldaconto() ? 7 : 3; - const long forn = calcola_m770(tipo_coll, spese, compenso, imposte, ritenute); - if (forn != 0) - is_percipient(forn, tipper, codper); - else - tipo_coll = 7; - } - break; - case 2: - calcola_m770(tipo_coll = 4, spese, compenso, imposte, ritenute); - break; - case 6: - calcola_m770(tipo_coll = 6, spese, compenso, imposte, ritenute); - break; - default: - break; - } - - if (tipo_coll) - { - TToken_string s(80); - s.add(tipo_coll); - s.add(curr_mask().insert_mode() ? "I" : "M"); - s.add(tipper); - s.add(codper); - s.add(curr_mask().get(F_NUMREG)); - s.add(curr_mask().get(F_NUMDOC)); - s.add(curr_mask().get(F_DATADOC)); - if (iva() == iva_acquisti) - s.add(curr_mask().get(F_TOTALE)); - else - s.add(""); - s.add(spese.string()); - s.add(compenso.string()); - s.add(imposte.string()); - s.add(ritenute.string()); - - const char* name = (tipo_coll == 4) ? "770 -1" : "770 -0"; - TMessage m(name, "LINK770", s); - m.send(); - - TExternal_app a(name); - a.run(); - } - - return tipo_coll > 0; -} - - -int cg2100 (int argc, char** argv) -{ - TPrimanota_application a; - a.run(argc, argv, "Prima nota"); - return 0; -} +#include +#include +#include +#include + +#include "pagament.h" + +#include "cg2100.h" +#include "cg2102.h" +#include "cg21sld.h" + +// Nomi dei campi +#include +#include +#include + +TPrimanota_application::TPrimanota_application() +: _rel(NULL), _mode(0), _iva(nessuna_iva), _causale(NULL), _giornale(NULL), + _lastreg(0), _last_dreg(TODAY), _last_dcom(TODAY) +{ + memset(_msk, 0, sizeof(_msk)); +} + +TMask* TPrimanota_application::load_mask(int n) +{ + int max_pages = 2; // Numero massimo di pagine della maschera + + if (n == 2) + { + if (is_fattura()) + max_pages = 3; + if (_msk[2] != NULL) + { + // Controlla se esiste pagina 3 + const int pages = (_msk[2]->id2pos(FS_RATESHEET) > 0) ? 3 : 2; + // Se i numeri di pagine non coincidono distruggi la maschera + if (pages != max_pages) + { + delete _msk[2]; + _msk[2] = NULL; + } + } + } + + if (_msk[n] != NULL) + return _msk[n]; + + if (n == 1 || n == 2) + { + for (int i = 3; i >= 0; i--) + { + if (_msk[i] != NULL) + { + delete _msk[i]; + _msk[i] = NULL; + } + } + } + + TFilename name("cg2100"); name << char(n == 3 ? 'o' : 'a'+n); + TMask* m = new TMask(name, 0, max_pages); + + switch (n) + { + case 0: + if (m) + { + 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"); + + // Scelta primo campo col focus + _firstfocus = c.get_bool("PoCuDr") ? F_DATAREG : F_CODCAUS; + m->first_focus(_firstfocus); + + // Registra e inserisci immediatamente un nuovo movimento + _savenew = !c.get_bool("Cg21SN"); + } + break; + case 2: + if (m) + { + m->set_handler(F_DATADOC, doc_handler); + m->set_handler(F_NUMDOC, doc_handler); + m->set_handler(F_DATA74TER, data74ter_handler); + m->set_handler(F_PROTIVA, protiva_handler); + m->set_handler(F_CLIENTE, clifo_handler); + m->set_handler(F_FORNITORE, clifo_handler); + m->set_handler(F_RITFIS, ritfis_handler); + m->set_handler(F_RITSOC, ritsoc_handler); + m->set_handler(F_CORRLIRE, corrlire_handler); + m->set_handler(F_CORRVALUTA, corrvaluta_handler); + m->set_handler(F_CODIVA, main_codiva_handler); + m->set_handler(F_OCCASEDIT, occas_handler); + m->set_handler(F_SOLAIVA, solaiva_handler); + m->set_handler(F_SHEETIVA, iva_handler); + m->set_handler(F_CODPAG, codpag_handler); + m->set_handler(SK_CAMBIO, cambio_handler); + + TSheet_field& is = (TSheet_field&)m->field(F_SHEETIVA); + is.set_notify(iva_notify); + TMask& ism = is.sheet_mask(); + ism.set_handler(101, imponibile_handler); + ism.set_handler(102, codiva_handler); + ism.set_handler(103, detrazione_handler); + ism.set_handler(104, imposta_handler); + ism.set_handler(109, iva_sottoconto_handler); + ism.set_handler(209, sheet_clifo_handler); + ism.set_handler(309, sheet_clifo_handler); + + // Se esiste lo sheet delle rate + if (is_fattura()) + { + TSheet_field& ps = (TSheet_field&)m->field(FS_RATESHEET); + ps.set_notify(pag_notify); + m->set_handler(FS_RATESHEET, pag_sheet_handler); + m->set_handler(F_NUMRIF, numrif_handler); + m->set_handler(FS_RESET, reset_handler); + m->set_handler(FS_NRATE, nrate_handler); + m->set_handler(FS_RECALC, recalc_handler); + m->set_handler(FS_NSCAB, codcab_handler); + m->set_handler(FS_VSCAB, codcab_handler); + } + } + case 1: + if (m) + { + m->first_focus(_firstfocus); + m->set_handler(F_DATAREG, datareg_handler); + m->set_handler(F_DATACOMP, datacomp_handler); + m->set_handler(F_DESCR, descr_handler); + m->set_handler(F_CODCAUS, caus_modify_handler); + m->set_handler(F_TOTALE, totdoc_handler); + m->set_handler(SK_TOTDOCVAL, totdocval_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, cg_conto_handler); + cgm.set_handler(106, suspended_handler); + cgm.set_handler(206, sheet_clifo_handler); + cgm.set_handler(306, sheet_clifo_handler); + cgm.set_handler(112, suspended_handler); + cgm.set_handler(113, suspended_handler); + cgm.set_handler(213, sheet_clifo_handler); + cgm.set_handler(313, sheet_clifo_handler); + } + _sheet_shown = FALSE; + break; + case 3: + m->set_handler(O_CODICE, occas_code_handler); + break; + default: + CHECKD(0, "Che ca$$o di maschera e' la ", n); + break; + } + + _msk[n] = m; + return m; +} + +void TPrimanota_application::open_files(int logicnum, ...) +{ + va_list marker; + va_start(marker, logicnum); + while (logicnum > 0) + { + CHECKD(_file.objptr(logicnum) == NULL, "File gia' aperto: ", logicnum); + _file.add(new TLocalisamfile(logicnum), logicnum); + logicnum = va_arg(marker, int); + } +} + +bool TPrimanota_application::user_create() +{ + open_files(LF_TABCOM, LF_TAB, LF_CAUSALI, LF_RCAUSALI, LF_CLIFO, LF_PCON, 0); + open_files(LF_ATTIV, LF_SALDI, LF_COMUNI, LF_OCCAS, LF_NDITTE, 0); + open_files(LF_SCADENZE, LF_PAGSCA, 0); + + _rel = new TMovimentoPN; + _rel->add(LF_PARTITE, "NREG=NUMREG", 2); // Collega la partita aperta dalla fattura + + _causale = new TCausale(); + _giornale = new TLibro_giornale(); + + set_search_field(F_NUMREG); // Set field for default search + + _pag = NULL; + _pag_rows = NULL; + _is_saldaconto = FALSE; + + load_mask(0); + + return TRUE; +} + + +bool TPrimanota_application::save_and_new() const +{ return _savenew; } + + +bool TPrimanota_application::user_destroy() +{ + for (int m = 3; m >= 0; m--) + if (_msk[m] != NULL) delete _msk[m]; + + if (_pag) delete _pag; + + delete _giornale; + delete _causale; + delete _rel; + close_files(); + + 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) +{ + bool ok = TRUE; + + if (cod != NULL) + ok = causale().read(cod, year); + + bool nob = FALSE, dob = FALSE; + TipoIVA iva = nessuna_iva; + _is_saldaconto = FALSE; + + if (ok) + { + _is_saldaconto = gestione_saldaconto() && causale().saldaconto(); + dob = _is_saldaconto || causale().data_doc(); + nob = _is_saldaconto || causale().num_doc(); + iva = causale().iva(); + } + + TMask* m = _msk[iva == nessuna_iva ? 1 : 2]; + + // Se sono chiamato dai campi di query ho belle che finito + if (_mode == MODE_QUERY || m == NULL) + return TRUE; + + const bool ins = m->insert_mode(); + + m->efield(F_NUMDOC).check_type(nob ? CHECK_REQUIRED : CHECK_NORMAL); // Num. doc. obbligatorio + m->efield(F_DATADOC).check_type(dob ? CHECK_REQUIRED : CHECK_NORMAL); // Data doc. obbligatoria + + const bool nota_credito = is_nota_credito(); + const bool show_games = is_pagamento() || nota_credito; + TMask& cgm = cgs().sheet_mask(); + cgm.set_handler(100, show_games ? showpartite_handler : NULL); // bottoncino riga + cgm.enable(100, show_games); + if (iva == nessuna_iva) + { + m->enable(F_PROVVISORIO, !_is_saldaconto); // Il saldaconto vieta i movimenti provvisori + m->show(SK_TIPORIGA, is_pagamento()); + } + else + { + m->efield(F_CODPAG).check_type(_is_saldaconto ? CHECK_REQUIRED : CHECK_NORMAL); + m->efield(F_CODPAG).enable(!nota_credito); + + m->show(F_ANNORIF, _is_saldaconto); // Mostra/nasconde anno e riferimento partita + m->show(F_NUMRIF, _is_saldaconto); + + TEdit_field& numrif = m->efield(F_NUMRIF); + numrif.set_justify(iva == iva_acquisti ? _num_for : _num_cli); + numrif.set_trim(!numrif.right_justified()); + + const bool av = causale().reg().agenzia_viaggi(); + m->show(F_DATA74TER, av); + if (!av) m->reset(F_DATA74TER); + + if (ins) + { + const long protiva = causale().reg().protocol() + 1; + m->set(F_PROTIVA, protiva, TRUE); // Aggiorna protocollo IVA + } + } + + if (ins && !ci_sono_importi()) + { + cgs().reset(); + for (int i = 1; i < causale().size(); i++) + { + const 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) + + TBill tc; causale().bill(nriga, tc); + if (tc.gruppo() <= 0) continue; // Considera solo gruppi validi + + if (tc.tipo() > ' ' && tc.ok()) + { + TBill clifo(0, 0, tc.sottoconto(), tc.tipo()); + if (clifo.find() && clifo.conto() != 0) // Legge gruppo e conto + tc = clifo; + } + + int err = 0; + if (tc.descrizione() == "Sconosciuto") err = 1; + else if (tc.sospeso()) err = 2; + + if (err) + { + error_box("Il conto della riga %d della causale e' %s", + i, err == 1 ? "sconosciuto" : "sospeso"); + continue; + } + + const char sezione = rcaus->get_char(RCA_SEZIONE); + const TImporto zero('D', ZERO); + const TString80 desc(causale().desc_agg(i)); + + if (nriga == 1) m->set(F_DESCR, desc); + + if (iva == nessuna_iva) + { + char tipr = ' '; + if (_is_saldaconto) + { + if (nriga < 13 && nriga != 10) continue; // Si considerano solo le spese + tipr = 'G'; + } + const int pos = set_cgs_row(-1, zero, tc, desc, tipr); + if (sezione > ' ') + cgs().disable_cell(pos, sezione == 'A' ? 0 : 1); + } + else + { + if (nriga >= 2 && nriga <= 9) + continue; // Conti per IVA detraibile e non, ritenute sociali e fiscali + + const char tipo = nriga == 1 ? 'T' : ' '; + if (nriga == 1 && tc.tipo() > ' ' && tc.ok()) + m->set(tc.tipo() == 'C' ? F_CLIENTE : F_FORNITORE, tc.sottoconto()); + set_cgs_row(-1,zero,tc,desc,tipo); + } + } + fill_sheet(*m); + } + + return ok; +} + + +// Certified 100% +void TPrimanota_application::print() +{ + TExternal_app stampa("cg3 -0"); + stampa.run(); +} + + +bool TPrimanota_application::changing_mask(int mode) +{ + if (mode == MODE_QUERY) + { + const bool flag = _mode != MODE_QUERY; +#if XVT_OS == XVT_OS_WIN + if (flag && !_sheet_shown) + { + curr_mask().show_page(1); + do_events(); + _sheet_shown = TRUE; + } +#endif + return flag; + } + _iva = iva_errata; + return TRUE; +} + + +TMask* TPrimanota_application::get_mask(int mode) +{ + int annoes = 0, annoiva = 0; + TString16 caus; + + _mode = mode; + + switch (mode) + { + case MODE_INS: + annoes = _msk[0]->get_int(F_ANNOES); + annoiva = _msk[0]->get_int(F_ANNOIVA); + caus = _msk[0]->get(F_CODCAUS); + break; + case MODE_MOD: + annoes = _rel->lfile().get_int("ANNOES"); + annoiva = _rel->lfile().get_int("ANNOIVA"); + caus = _rel->lfile().get("CODCAUS"); + break; + default: + return load_mask(0); + } + + if (giornale().year() != annoes) + giornale().read(annoes); + + const TCausale& c = causale(); + if (caus != c.codice() || (c.iva() != nessuna_iva && c.reg().year() != annoiva)) + read_caus(caus, annoiva); + + _iva = c.iva(); + return load_mask(_iva == nessuna_iva ? 1 : 2); +} + + +void TPrimanota_application::on_firm_change() +{ + TRelation_application::on_firm_change(); // May be useful, who knows? + + _rel->lfile().last(); + _lastreg = _rel->lfile().get_long(MOV_NUMREG); // Init last registration number + + _esercizi.update(); // Init exercises +} + +void TPrimanota_application::on_config_change() +{ + TConfig cnf(CONFIG_DITTA); + _ges_val = cnf.get_bool("GesVal"); + _ges_sal = cnf.get_bool("GesSal"); + _npart_is_prot = cnf.get_bool("RifPro"); + + _num_cli = cnf.get_bool("NrCliDx"); + _num_for = cnf.get_bool("NrForDx"); + TPartita::carica_allineamento(); +} + + +void TPrimanota_application::init_mask(TMask& m) +{ + disable_menu_item(M_FILE_PRINT); + + read_caus(NULL, 0); // Setta campi obbligatori + fill_sheet(m); + + const bool valintra = causale().valintra(); + const bool corrisp = causale().corrispettivi(); + + if (_iva == nessuna_iva) + { + m.show(-5, _is_saldaconto); // Abilita campi saldaconto + } + else + { + char clig, forg; + if (_iva == iva_acquisti) + { + forg = 's'; + clig = 'h'; + } + else + { + forg = 'h'; + clig = corrisp ? 'h' : 's'; + } + m.send_key(K_SHIFT+K_CTRL+clig, -1); // GROUP 1 (clienti) + m.send_key(K_SHIFT+K_CTRL+forg, -2); // GROUP 2 (fornitori) + if (corrisp) m.hide(F_STATOPAIV); + + // Show/Hide campi valuta: F_VALUTAINTRA, F_CAMBIOINTRA, F_CORRLIRE, F_CORRVAL (GROUP 4) + m.show(-4, valintra); + + m.show(F_CODIVA, m.mode() == MODE_INS); // Codice IVA standard + + if (is_fattura()) + { + const TPartita* game = partite().first(); + const bool scad_free = game == NULL || !game->esistono_abbuoni_diffcam(m.get_long(F_NUMREG)); + + m.enable(DLG_DELREC, scad_free); // Disabilita tasto cancella + m.enable(-1, scad_free); // Disabilita cliente + m.enable(-2, scad_free); + + m.enable(F_CODPAG, scad_free); // Disabilita codice pagamento + m.enable(FS_RESET, scad_free); // Disabilita tasto di reset + if (!scad_free) + m.set(FS_RECALC, "", TRUE); + m.enable(FS_RECALC, scad_free); // Disabilita ricalcolo automatico + } + } + + // Show/Hide campi valuta: F_VALUTA, F_CAMBIO, F_DATACAMBIO (GROUP 3) + const bool valuta = _ges_val && is_saldaconto() && causale().valuta(); + m.show(-3, valuta); +} + +void TPrimanota_application::init_query_mode(TMask& m) +{ + enable_menu_item(M_FILE_PRINT); + + m.set(F_DATAREG, _last_dreg); + m.set(F_DATACOMP, _last_dcom); +} + + +void TPrimanota_application::fill_sheet(TMask& m) const +{ + if (_iva != nessuna_iva || !is_saldaconto()) + { + 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 (int r = ivas.items(); r < 16; r++) ivas.row(r); + ivas.enable_column(2, _iva == iva_acquisti); // Tipo detrazione + ivas.enable_column(4, !m.insert_mode()); // Tipo costo ricavo + } +} + + +// Ritorna il prossimo numero di registrazione libero +// Certified 100% +const char* TPrimanota_application::get_next_key() +{ + _tmp.cut(0); + _tmp.add(F_NUMREG); _tmp.add(_lastreg+1); + + if (_rel->good()) // Not reinsert + { + TMask& m = curr_mask(); + if (m.insert_mode()) + { + _tmp.add(F_CODCAUS); _tmp.add(m.get(F_CODCAUS)); // Ricopia causale + _tmp.add(F_DATAREG); _tmp.add(m.get(F_DATAREG)); // data operazione + _tmp.add(F_DATACOMP); _tmp.add(m.get(F_DATACOMP)); // competenza + + if (iva() == iva_vendite) + { + _tmp.add(F_DATADOC); _tmp.add(m.get(F_DATADOC)); // Ricopia data documento + + const long n = m.get_long(F_NUMDOC); + if (n > 0) + { _tmp.add(F_NUMDOC); _tmp.add(n+1); } // incrementa numero documento + } + } + } + + return _tmp; +} + + +void TPrimanota_application::init_insert_mode(TMask& m) +{ + init_mask(m); + + m.first_focus(_firstfocus); + + if (causale().reg().agenzia_viaggi()) + m.set(F_DATA74TER, m.get(F_DATAREG)); + + if (iva() != nessuna_iva) + { + occas_mask().reset(); + + if (is_fattura()) + { + const TString16 dt(m.get(F_DATADOC)); + set_pagamento(NULL,dt); + set_scadenze(m); + } + else + set_pagamento(NULL,NULL); // Annulla pagamento + } + else + { + partite().destroy(); + set_pagamento(NULL,NULL); // Annulla pagamento + } + _saldi.reset(); // Inizializza saldi +} + + +void TPrimanota_application::init_modify_mode(TMask& m) +{ + init_mask(m); + calcola_saldo(); // Verifica eventuali sbilanci contabili +} + +// Controlla sulla causale se il segno del totale documento (ritsoc=FALSE) +// o quello delle ritenute sociali (ritsoc=TRUE) e' invertito rispetto al normale +bool TPrimanota_application::test_swap(bool ritsoc) +{ + const char sez = ritsoc ? causale().sezione_ritsoc() : causale().sezione_clifo(); + const bool s = (iva() == iva_vendite) ^ sez == 'D'; + return s; +} + + +int TPrimanota_application::read(TMask& m) +{ + m.autoload(_rel); + + const long numreg = _rel->curr().get_long(MOV_NUMREG); + + cgs().reset(); + + if (iva() != nessuna_iva) + { + ivas().reset(); + + const TString16 occode(_rel->lfile().get("OCFPI")); + occas_mask().set(O_CODICE, occode); + + const char clifo = _rel->lfile().get_char("TIPO"); + 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"); + } + + _saldi.reset(); // Azzera saldi + _saldi.set_movprovv(_rel->lfile().get_char("PROVVIS") > ' '); + _saldi.set_movap(causale().apertura()); + _saldi.set_anno_es(m.get_int(F_ANNOES)); + _saldi.set_num_ulmov(numreg); + _saldi.set_data_ulmov((TDate)m.get(F_DATAREG)); + + for (int i = 0; i < _rel->cg_items(); i++) + { + const TRectype& r = _rel->cg(i); + TToken_string& riga = cgs().row(i); // Vuota la riga + + TImporto import(r.get_char("SEZIONE"), r.get_real("IMPORTO")); + import.add_to(riga, 0); // Dare/Avere 101-102 + + TBill conto; conto.get(r); + riga.add(conto.string(0x3)); // Conto 103-107 + + _saldi.aggiorna(conto, import, FALSE); + + riga.add(""); // Codice descrizione 108 + riga.add(r.get("DESCR")); // Descrizione riga 109 + + conto.get(r, TRUE); + riga.add(conto.string(0x3)); // Contropartita 110-114 + riga.add(r.get("NUMGIO")); + const char tipo = r.get_char("ROWTYPE"); + riga.add(tipo); // Tipo di riga 115 + + 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(); + + const bool to_swap = test_swap(FALSE); + if (to_swap) + { + real totdoc(m.get(F_TOTALE)); + totdoc = -totdoc; + m.set(F_TOTALE, totdoc.string()); + } + for (i = 0; i < _rel->iva_items(); i++) + { + TRectype& r = _rel->iva(i); + TToken_string& riga = ivas().row(i); + + real imponibile(r.get("IMPONIBILE")); + if (to_swap) imponibile = -imponibile; + riga.add(imponibile.string()); // Imponibile 101 + + riga.add(r.get("CODIVA")); // IVA 102 + riga.add(r.get("TIPODET")); // Detrazione 103 + + real imposta(r.get("IMPOSTA")); + if (to_swap) imposta = -imposta; + if (imponibile.sign() * imposta.sign() < 0) + { + error_box("Registrazione con imponibile e imposta con segni discordi:\n" + "assegnato il segno dell'imponibile"); + imposta = -imposta; + } + riga.add(imposta.string()); // Imposta 104 + + TBill c; c.get(r); + c.add_to(riga, 4, 0x7); // Conto 105-110 + } + + calcola_imp(); // Calcola totale imponibile ed imposte + + partite().destroy(); + if (is_fattura()) // Ci sono scadenze + { + const TString16 cp(m.get(F_CODPAG)); + const TString16 dt(m.get(F_DATADOC)); + set_pagamento(cp, dt); + if (!read_scadenze(m)) + set_scadenze(m); + } + + return _rel->status(); +} + + +// Trasferisce i dati da maschera a movimento di prima nota +void TPrimanota_application::mask2rel(const TMask& m) +{ + m.autosave(_rel); + + const long numreg = m.get_long(F_NUMREG); + const TDate datareg(m.get(F_DATAREG)); + const int annoes = m.get_int(F_ANNOES); + + _saldi.set_movprovv(m.get(F_PROVVISORIO)[0] > ' '); + _saldi.set_movap(causale().apertura()); + _saldi.set_anno_es(annoes); + _saldi.set_num_ulmov(numreg); + _saldi.set_data_ulmov((TDate)m.get(F_DATAREG)); + + _rel->destroy_rows(numreg); // Destroy all records + cgs_pack(); // Destroy all null rows + + TArray& rows = cgs().rows_array(); + + // 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]; + + TImporto n; n = row; + const TBill conto(row, 2, 0x3); + _saldi.aggiorna(conto, n, TRUE); + + TRectype &r = _rel->cg(i); + r.zero(); + + r.put("NUMREG", numreg); // Numero registrazione + r.put("ANNOES", annoes); // Anno esercizio + r.put("DATAREG", datareg); // Data di registrazione + r.put("NUMRIG", i+1); // Numero riga + + r.put("SEZIONE", n.sezione()); // Sezione + r.put("IMPORTO", n.valore()); // Importo + conto.put(r); // Conto + + row.get(); // Codice descrizione (ignorato) + const char* descr = row.get(); // Descrizione aggiuntiva + if (i > 0 || m.get(F_DESCR) != descr) // Salva la prima descrizione se diversa da testata + r.put("DESCR", descr); // Descrizione riga + + r.put("TIPOCC", row.get()); // Contropartita + r.put("GRUPPOC", row.get()); + r.put("CONTOC", row.get()); + r.put("SOTTOCONTC", row.get()); + row.get(); // Descrizione contropartita + r.put("NUMGIO", row.get()); // Numero riga giornale + r.put("ROWTYPE", row.get()); // Tipo riga + } + } + + if (_iva == nessuna_iva) return; + + if (causale().corrispettivi()) + { + TRectype& rec = _rel->lfile().curr(); + rec.put("TIPO", ""); + rec.put("CODCF", ""); // Azzera il cliente nei movimenti dei corrispettivi + } + else + { + _rel->lfile().put("TIPO", clifo()); + + int err = ~NOERR; + if (m.field(F_OCCASEDIT).active()) // Se e' un occasionale + { + TRelation occas(LF_OCCAS); + const TMask& om = occas_mask(); + om.autosave(&occas); // Salva i dati anagrafici + + 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"); + } + + const bool intra = causale().intra(); + const bool to_swap = test_swap(FALSE); + if (to_swap) + { + real totale = _rel->lfile().get("TOTDOC"); + totale = -totale; + _rel->lfile().put("TOTDOC", totale); + } + + ivas_pack(); + TArray& irows = ivas().rows_array(); + for (int i = 0; i < irows.items(); i++) + { + TToken_string& row = (TToken_string&)irows[i]; + + TRectype &r = _rel->iva(i); + r.zero(); + r.put("ANNOES", annoes); // Anno d'esercizio della testata (che ca$$ata!) + r.put("NUMREG", numreg); + r.put("INTRA", intra); // Causale intra (che ca$$ata!) + r.put("NUMRIG", i+1); + + real imponibile(row.get(0)); + if (to_swap) imponibile = -imponibile; + r.put("IMPONIBILE", imponibile); + + r.put("CODIVA", row.get()); + r.put("TIPODET", row.get()); + + real imposta(row.get()); + if (to_swap) imposta = -imposta; + r.put("IMPOSTA", imposta); + + r.put("TIPOCR", row.get()); + + const TBill c(row, -1, 0x1); + const int rimp = bill2pos(c, 'I')+1; + r.put("RIGAIMP", rimp); + c.put(r); + } +} + + +void TPrimanota_application::check_saldi() +{ + TString_array& rows = cgs().rows_array(); + const int anno = _rel->lfile().get_int("ANNOES"); + + for (int i = 0; i < rows.items(); i++) + { + TToken_string& row = (TToken_string&)rows[i]; + TBill conto(row, 2, 0x3); + conto.find(); + const char sez = conto.sezione(); + if (sez > ' ') + { + const TConto* c = _saldi.find(conto, anno); + if (c && !c->saldo_finale().is_zero() && c->saldo_finale().sezione() != sez) + warning_box("Il conto della riga %i ha un saldo finale in %s, " + "contrariamente a quanto indicato sul piano dei conti", + i+1, sez == 'A' ? "dare" : "avere"); + } + } +} + + +int TPrimanota_application::write(const TMask& m) +{ + static int lasterr = NOERR; + + const long numreg = m.get_long(F_NUMREG); + if (numreg > _lastreg) _lastreg = numreg; // Aggiorna ultima registrazione libera + + if (lasterr == NOERR) + mask2rel(m); // Altrimenti raddoppia i saldi! + + _last_dreg = m.get(F_DATAREG); + _last_dcom = m.get(F_DATACOMP); + + const int err = _rel->write(TRUE); + if (err == NOERR) + { + _saldi.registra(); + check_saldi(); + + bool salvaconto = FALSE; + + if (iva() != nessuna_iva) + { + causale().reg().reread(); // Aggiorna protocollo IVA + if (is_saldaconto()) + { + switch (causale().tipomov()) + { + case tm_fattura: + write_scadenze(m); break; // Salva fattura + case tm_nota_credito: + salvaconto = TRUE; break; // Salva nota credito + default: + break; + } + } + else + { + // Genera incasso immediato se e' una fattura e non c'e' gestione saldaconto + if (!m.get_bool(F_SOLAIVA) && !gestione_saldaconto()) + { + const TString causimm(causale().causale_inc_imm()); + if (causimm.not_empty()) + genera_incasso(causimm); + } + } + } + else + { + salvaconto = is_pagamento(); + } + + if (salvaconto) + { + const long old_nreg = numreg - (lasterr == _isreinsert ? 1 : 0); + partite().update_reg(_rel->lfile().curr(), old_nreg); + partite().write(); + } + + link_m770(); + } + + lasterr = err; + return err; +} + + +int TPrimanota_application::rewrite(const TMask& m) +{ + mask2rel(m); + const int err = _rel->rewrite(TRUE); + if (err == NOERR) + { + _saldi.registra(); + check_saldi(); + + if (_is_saldaconto) + { + bool salvaconto = FALSE; + if (iva() != nessuna_iva) + { + if (causale().tipomov() == tm_nota_credito) + salvaconto = TRUE; + else + write_scadenze(m); + } + else + { + salvaconto = is_pagamento(); + } + + if (salvaconto) + { + partite().update_reg(_rel->curr()); + partite().rewrite(); + } + } + + link_m770(); + } + + return err; +} + + +bool TPrimanota_application::remove() +{ + const bool ok = TRelation_application::remove(); + if (ok) + { + _saldi.registra(); + check_saldi(); + + if (_is_saldaconto) + { + if (iva() == nessuna_iva) + { + const long nreg = curr_mask().get_long(F_NUMREG); + partite().add_numreg(nreg); + notify_cgline_deletion(-1); // Notify deletion of all cg lines + partite().rewrite(); + } + else + { + TMask& m = curr_mask(); + m.reset(F_ANNORIF); + m.reset(F_NUMRIF); + write_scadenze(m); + } + } + } + return ok; +} + + +HIDDEN bool incasso_handler(TMask_field& f, KEY k) +{ + if (k == K_ENTER) + { + TMask& m = f.mask(); + TImporto tot(m.get(I_SEZIONE1)[0], (real)m.get(F_TOTALE)); + + for (int i = 2; i < 5; i++) + { + const int sid = i == 2 ? I_SEZIONE2 : (i == 3 ? I_SEZIONE3 : I_SEZIONE4); + const real val(m.get(sid+1)); + const TImporto imp(m.get(sid)[0], val); + tot += imp; + } + if (!tot.is_zero()) + return f.error_box("Il movimento e' sbilaciato di %s", tot.valore().string(".")); + } + return TRUE; +} + + +void TPrimanota_application::genera_incasso(const char* causimm) +{ + TMask m("cg2100i"); + if (iva() == iva_acquisti) m.set_caption("Movimento di pagamento immediato"); + + m.set_handler(F_DATAREG, datareg_handler); + m.set_handler(F_DATACOMP, datacomp_handler); + m.set_handler(F_TOTALE, incasso_handler); + + TCausale caus(causimm); // Causale da usare + + if (caus.data_doc()) m.efield(F_DATADOC).check_type(CHECK_REQUIRED); + if (caus.num_doc()) m.efield(F_NUMDOC).check_type(CHECK_REQUIRED); + + m.set(F_DATAREG, curr_mask().get(F_DATAREG)); + m.set(F_DATACOMP, curr_mask().get(F_DATACOMP)); + m.set(F_DATADOC, curr_mask().get(F_DATADOC)); + m.set(F_NUMDOC, curr_mask().get(F_NUMDOC)); + m.set(F_NUMREG, _lastreg+1); // Incrementa numero di registrazione + m.set(F_DESCR, caus.desc_agg(1)); // Descrizione + m.set(F_CODCAUS, causimm); // Cambia causale + m.set(F_DESCRCAUS, caus.descrizione()); + + m.set(I_SEZIONE1, caus.sezione_clifo() == 'D' ? "D" : "A"); + m.set(F_TOTALE, curr_mask().get(F_TOTALE)); + + m.set(F_CLIFO, _rel->cg(0).get("TIPOC")); + m.set(F_GRUPPOCLIFO, _rel->cg(0).get("GRUPPO")); + m.set(F_CONTOCLIFO, _rel->cg(0).get("CONTO")); + const TString& clifo = _rel->cg(0).get("SOTTOCONTO"); + m.set(F_CLIENTE, clifo); m.set(F_FORNITORE, clifo); m.set(I_SOTTOCONTO, clifo); + + TBill conto; caus.bill(2, conto); // Conto della seconda riga della causale + m.set(I_SEZIONE2, caus.sezione_clifo() == 'D' ? "A" : "D"); + m.set(I_IMPORTO2, curr_mask().get(F_TOTALE)); + m.set(I_GRUPPO2, conto.gruppo()); + m.set(I_CONTO2, conto.conto()); + m.set(I_SOTTOCONTO2, conto.sottoconto()); + + if (m.run() == K_SAVE) + { + TMovimentoPN inc; // Nuovo movimento di incasso immediato + TRectype& curr = inc.curr(); + curr.zero(); + m.autosave(&inc); + + curr.put("TIPODOC", caus.tipo_doc()); // Tipo documento + curr.put("REG", caus.reg().name()); // Registro + curr.zero(MOV_TIPO); + curr.zero(MOV_CODCF); + curr.zero(MOV_TOTDOC); + curr.zero(MOV_ANNOIVA); + + const int annoes = m.get_int(F_ANNOES); + const TString16 datareg = m.get(F_DATAREG); + long num = m.get_long(F_NUMREG); + + inc.destroy_rows(num); + + TRectype& r = inc.cg(0); + r.zero(); + r.put("NUMREG", num); + r.put("NUMRIG", 1); + r.put("DATAREG", datareg); + r.put("ANNOES", annoes); + r.put("SEZIONE", m.get(I_SEZIONE1)); + r.put("IMPORTO", m.get(F_TOTALE)); + r.put("TIPOC", m.get(F_CLIFO)); + r.put("GRUPPO", m.get(F_GRUPPOCLIFO)); + r.put("CONTO", m.get(F_CONTOCLIFO)); + + short clifo_id; + switch (m.get(F_CLIFO)[0]) + { + case 'C': + clifo_id = F_CLIENTE; break; + case 'F': + clifo_id = F_FORNITORE; break; + default : + clifo_id = I_SOTTOCONTO; break; + } + r.put("SOTTOCONTO", m.get(clifo_id)); + r.put("DESCR", m.get(F_DESCR)); + r.put("TIPOCC", " "); + r.put("GRUPPOC", m.get(I_GRUPPO2)); + r.put("CONTOC", m.get(I_CONTO2)); + r.put("SOTTOCONTC", m.get(I_SOTTOCONTO2)); + + r.put("ROWTYPE", " "); + + for (int i = 2; i < 5; i++) + { + const int sid = i == 2 ? I_SEZIONE2 : (i == 3 ? I_SEZIONE3 : I_SEZIONE4); + const TString& imp = m.get(sid+1); + if (imp.empty()) break; + + TRectype& r = inc.cg(i-1); + r.zero(); + r.put("IMPORTO", imp); + r.put("NUMREG", num); + r.put("NUMRIG", i); + r.put("DATAREG", datareg); + r.put("ANNOES", annoes); + r.put("SEZIONE", m.get(sid)); + r.put("TIPOC", ' '); + r.put("GRUPPO", m.get(sid+2)); + r.put("CONTO", m.get(sid+3)); + r.put("SOTTOCONTO", m.get(sid+4)); + if (i == 2) + r.put("DESCR", caus.desc_agg(2)); + + r.put("TIPOCC", m.get(F_CLIFO)); + r.put("GRUPPOC", m.get(F_GRUPPOCLIFO)); + r.put("CONTOC", m.get(F_CONTOCLIFO)); + r.put("SOTTOCONTC", m.get(clifo_id)); + + r.put("ROWTYPE", " "); + } + + while (inc.write() == _isreinsert) // In caso di riscrittura + { + num++; // Incrementa numero registrazione + inc.lfile().put("NUMREG", num); + } + + if (inc.good()) + { + if (num > _lastreg) _lastreg = num; + + _saldi.reset(); // Inizializza saldi + _saldi.set_movprovv(FALSE); + _saldi.set_movap(FALSE); + _saldi.set_anno_es(m.get_int(F_ANNOES)); + _saldi.set_num_ulmov(_lastreg); + _saldi.set_data_ulmov((TDate)m.get(F_DATAREG)); + for (int r = 0; r < inc.cg_items(); r++) + { + const TRectype& rec = inc.cg(r); + TBill c; c.get(rec); + const TImporto im(rec.get_char("SEZIONE"), rec.get_real("IMPORTO")); + _saldi.aggiorna(c, im, TRUE); + } + _saldi.registra(); + check_saldi(); + } + } +} + +bool TPrimanota_application::is_percipient(long forn, char& tipper, long& codper) const +{ + TLocalisamfile fornitori(LF_CLIFO); + fornitori.put("TIPOCF", "F"); + fornitori.put("CODCF", forn); + fornitori.read(); + tipper = fornitori.get_char(CLI_TIPOAPER); + codper = fornitori.get_long(CLI_CODANAGPER); + return codper > 0; +} + +long TPrimanota_application::calcola_m770(int tipo_coll, real& spese, real& compenso, + real& iva, real& ritfis) +{ + const int m770 = causale().link_m770(); + long forn = 0; + + if (tipo_coll == 1) + { + TString_array& riva = ivas().rows_array(); + TCodiceIVA codiva; + for (int i = 0; i < riva.items(); i++) + { + TToken_string& row = riva.row(i); + codiva.read(row.get(1)); + const TString& tipoiva = codiva.tipo(); + + const real imponibile(row.get(0)); + if ((tipoiva == "ES" || tipoiva == "NI" ||tipoiva == "NS") && riva.items() > 1) + { + if (m770 != 5) + spese += imponibile; + } + else + compenso += imponibile; + + const real imposta(row.get(3)); + iva += imposta; + } + + i = type2pos('F'); + if (i >= 0) + { + TImporto imp; imp = cgs().row(i); + ritfis = imp.valore(); + } + } + + if (tipo_coll == 3 || tipo_coll == 7) + { + TImporto imp; + TString_array& rcg = cgs().rows_array(); + for (int i = 0; i < rcg.items(); i++) + { + TToken_string& row = rcg.row(i); + imp = row; + + const char cf = row.get_char(2); + if (cf == 'F') // Evviva, e' un fornitore! + { + const long cod = row.get_long(5); + char tipper; + long codper; + if (!is_percipient(cod, tipper, codper)) + continue; // Se non e' percipente ignoralo + + if (forn == 0 && tipo_coll == 3) + { + forn = cod; // E' il primo che trovo! + } + else // Fine ricerca. + { + forn = 0; + compenso = ZERO; + break; + } + compenso = imp.valore(); + } + } + } + + if (tipo_coll == 4) + { + TBill zio; causale().bill(2, zio); + + TString_array& rcg = cgs().rows_array(); + for (int i = 0; i < rcg.items(); i++) + { + TToken_string& r = rcg.row(i); + const TBill bill(r, 3, 0x0); + if (zio == bill) + { + TImporto imp; imp = r; + ritfis = imp.valore(); + break; + } + } + } + + if (tipo_coll == 6) + { + TString_array& rcg = cgs().rows_array(); + TImporto imp; + + for (int i = 0; i < rcg.items(); i++) + { + TToken_string& row = rcg.row(i); + imp = row; + compenso += imp.valore(); + } + } + + return forn; +} + +bool TPrimanota_application::link_m770() +{ + const int m770 = causale().link_m770(); + if (!has_module(4) || iva() == iva_vendite || m770 == 0) + return FALSE; + + int tipo_coll = 0; + char tipper = ' '; + long codper = 0; + real spese, compenso, imposte, ritenute; + + if (iva() == iva_acquisti) + { + if (m770 == 1 || m770 == 5) + { + const long forn = curr_mask().get_long(F_FORNITORE); + if (is_percipient(forn, tipper, codper)) + calcola_m770(tipo_coll = 1, spese, compenso, imposte, ritenute); + } + } + else // Movimento puramente contabile + switch (m770) + { + case 1: + { + tipo_coll = is_saldaconto() ? 7 : 3; + const long forn = calcola_m770(tipo_coll, spese, compenso, imposte, ritenute); + if (forn != 0) + is_percipient(forn, tipper, codper); + else + tipo_coll = 7; + } + break; + case 2: + calcola_m770(tipo_coll = 4, spese, compenso, imposte, ritenute); + break; + case 6: + calcola_m770(tipo_coll = 6, spese, compenso, imposte, ritenute); + break; + default: + break; + } + + if (tipo_coll) + { + TToken_string s(80); + s.add(tipo_coll); + s.add(curr_mask().insert_mode() ? "I" : "M"); + s.add(tipper); + s.add(codper); + s.add(curr_mask().get(F_NUMREG)); + s.add(curr_mask().get(F_NUMDOC)); + s.add(curr_mask().get(F_DATADOC)); + if (iva() == iva_acquisti) + s.add(curr_mask().get(F_TOTALE)); + else + s.add(""); + s.add(spese.string()); + s.add(compenso.string()); + s.add(imposte.string()); + s.add(ritenute.string()); + + const char* name = (tipo_coll == 4) ? "770 -1" : "770 -0"; + TMessage m(name, "LINK770", s); + m.send(); + + TExternal_app a(name); + a.run(); + } + + return tipo_coll > 0; +} + + +int cg2100 (int argc, char** argv) +{ + TPrimanota_application a; + a.run(argc, argv, "Prima nota"); + return 0; +}