From e9beff8f243627d51b3279e59dbd44a53dc2291a Mon Sep 17 00:00:00 2001 From: bonazzi Date: Tue, 13 Dec 2016 13:53:33 +0000 Subject: [PATCH] Patch level : 12.0 310 314 Files correlati : Modifica CRPA Protolollo partita per mese. git-svn-id: svn://10.65.10.50/branches/R_10_00@23444 c028cbd2-c16b-5b4b-a496-9718f37d4682 --- src/cg/cg2104.cpp | 2973 +++++++++++++++++++++++---------------------- 1 file changed, 1490 insertions(+), 1483 deletions(-) diff --git a/src/cg/cg2104.cpp b/src/cg/cg2104.cpp index 4cec58a5f..7eccc784d 100755 --- a/src/cg/cg2104.cpp +++ b/src/cg/cg2104.cpp @@ -1,1483 +1,1490 @@ -// -------------------------------------------------------------------------- -// Scadenzario -// fv 24/8/94 -// -------------------------------------------------------------------------- - -#include -#include "cgpagame.h" -#include "cgsaldac.h" - -#include "cg2102.h" -#include "cg2100.h" -#include "cg21sld.h" - -#include -#include - -TPagamento& TPrimanota_application::pagamento() const -{ - CHECK(_pag, "Pagamento nullo"); - return *_pag; -} - -bool TPrimanota_application::pag_notify(TSheet_field& ps, int r, KEY k) -{ - TMask* msk = &(ps.mask()); - -// if (!msk->is_running()) return true; // Necessario per evitare il CHECK sul pagamento nullo - if (!app().is_fattura() || app()._pag == NULL) return true; // Necessario per evitare il CHECK sul pagamento nullo - - TPagamento& pag = app().pagamento(); - TString_array& rws = app().pag_rows(); - - const int rdiff = msk->get_int(FS_RDIFFER); - const bool in_valuta = pag.in_valuta(); -// const int impos = in_valuta ? 2 : 1; - - // ts contiene la vecchia riga, ns la nuova - TToken_string ts(128), ns(128); - TString16 banca; - - bool doit = true, m_impl = FALSE, m_impv = FALSE, m_perc = FALSE; - bool m_scad = FALSE, m_tipo = FALSE, m_ulc = FALSE, mod = FALSE; - word ahiahi = P_OK; - - TString news(10), newl(15), newv(15), newp(15), newt(1), newu(1); - - bool recalc = msk->get_bool(FS_RECALC); - bool mcomm = msk->get_bool(FS_MCOMM); - - switch (k) - { - case K_SPACE: - { - const bool can = !recalc && pag.n_rate() > 1 && ps.row(r).get_char(14) != 'X'; - ps.sheet_mask().enable(DLG_DELREC, can); - } - break; - case K_ENTER: - ns = ps.row(r); - ts = rws.row(r); - - news = ns.get(0); // Data scadenza - newl = ns.get(1); // Imponibile (lire) - newv = ns.get(2); // Imponibile (valuta) - newp = ns.get(3); // Percentuale - newt = ns.get(4); // Tipo pagamento - newu = ns.get(5); // Ulteriore classificazione - - // qui viene il bello, si fa per dire - if (news != ts.get(0)) // modificata data scadenza - mod = m_scad = true; - if (real(newp) != real(ts.get(3))) // modificata percentuale - mod = m_perc = true; - if (real(newl) != real(ts.get(1))) // modificato importo lire - { - // se si modifica la percentuale nessun importo viene cagato - if ((recalc && !m_perc) || (!recalc)) - mod = m_impl = true; - } - if (in_valuta && real(newv) != real(ts.get(2))) - { - // se si modifica la percentuale nessun importo viene cagato - if ((recalc && !m_perc) || (!recalc)) - mod = m_impv = true; - } - if (newt != ts.get(4)) // modificato tipo pagamento - mod = m_tipo = m_ulc = true; - - if (newu != ts.get(5)) // modificata ulteriore classificazione - mod = m_ulc = true; - else if (m_tipo) - { - // forza reset di ulc se si e' modificato il tipo rata - m_ulc = true; - newu = ""; - } - - break; - case K_DEL: - doit = !recalc && pag.n_rate() > 1 && ps.row(r).get_char(15) != 'X'; // non cancellabile - if (doit) - { - pag.remove_rata(r); - msk->set(FS_NRATE, pag.n_rate()); - } - break; - case K_INS: - // permette aggiunta e cancellazione solo se non c'e' ricalcolo automatico - doit = !recalc; - break; - case K_CTRL+K_INS: // Post inserimento - { - const int prev = r - 1; - const int gio_scad = pag.scad_rata(prev); - TDate data_scad(pag.data_rata(prev)); - - pag.add_rata(ZERO, gio_scad, pag.tipo_rata(prev), pag.ulc_rata(prev)); - pag.next_scad(data_scad, gio_scad, pag.mese_commerciale(), r); - pag.set_datarata(r, data_scad); -// pag.adjust_fixed_scad(); - pag.set_sheet(ps); - rws = ps.rows_array(); - } - break; - default: - break; - } - - // settato da recalc_rate se occorre ridefinire lo sheet - // aggiungendo o togliendo righe - bool need_recalc = FALSE; - - if (k == K_ENTER) - { - if (mod && recalc) - { - // ricalcola sheet come sai fare tu - ahiahi = pag.recalc_rate(r, m_perc, - ((m_perc || m_impv) ? - (m_perc ? (const char*)newp : (const char*)newv) : NULL), - (m_impl ? (const char*)newl : NULL), - (m_scad ? (const char*)news : NULL), - (m_tipo ? (const char*)newt : NULL), - (m_ulc ? (const char*)newu : NULL), - rdiff, mcomm, need_recalc); - - // see if rdiff changed - msk->set(FS_RDIFFER, pag.rate_differenziate() ? "1" : "2"); - msk->set(FS_NRATE, pag.n_rate()); - } - if (!recalc) - { - ahiahi = P_OK; - // put data as they are - TToken_string& trw = pag.rata(r); - TToken_string& trs = ps.row(r); - if (m_tipo) - trw.add(newt,2); - if (m_ulc) - trw.add(newu, 5); - if (m_scad) - { - trw.add(news,3); - if (r > 0) - { - TDate d(news); - int scd = (int)(d - pag.data_rata(r-1)); - if (pag.mese_commerciale() && (scd % 30) != 0) - scd = 30 * ((scd/30)+1); - trw.add(scd, 0); - } - trs.add(news, 0); - } - if (m_impl) - { - trw.add(newl,7); - if (!in_valuta) - { - // si ricalcola la percentuale a modifica importo in lire solo - // se non c'e' l'importo in valuta - const real p = pag.recalc_percrata(r, FALSE); - trs.add(p.string(), 3); - } - } - if (m_impv) - { - trw.add(newv,4); - const real p = pag.recalc_percrata(r, true); - trs.add(p.string(), 3); - } - if (mod) - { - rws.row(r) = trs; - ps.force_update(r); - } - } - - if (ahiahi) // any error? Rimetti le righe com'erano prima - { - if (recalc) - { - TString err(80); pag.strerr(ahiahi,err); - warning_box(err); - ps.row(r) = rws.row(r); - ps.force_update(r); - } - } - else if (recalc && mod && need_recalc) - { - // ridefinisci lo sheet sulla base delle nuove rate - pag.set_sheet(ps); - rws = ps.rows_array(); - } - } - - return doit; -} - -bool TPrimanota_application::pag_sheet_handler(TMask_field& f, KEY key) -{ - bool ok = true; - if (key == K_TAB || key == K_ENTER) - { - TMask& m = f.mask(); - - TPagamento& pag = app().pagamento(); - const TDate dt(m.get(F_DATADOC)); - - pag.set_datadoc(dt); - word err = pag.validate(); - if (m.edit_mode()) - err &= ~(P_RSUM | P_TOTNCLIT | P_TOTNCVAL ); // Ignora totale importi e rate - if ((err && P_ZEROLIT) && yesno_box(TR("Almeno una rata ha importo nullo in Euro si desidera continuare?"))) - err &= ~P_ZEROLIT; - if ((err && P_ZEROVAL) && yesno_box(TR("Almeno una rata ha importo nullo in valuta si desidera continuare?"))) - err &= ~P_ZEROVAL; - if (err != P_OK) - { - TString s(256); - pag.strerr(err, s); - return f.error_box(s); - } - - const bool in_valuta = pag.in_valuta(); - real imp; - - if (key == K_ENTER) - { - real tot = m.get_real(in_valuta ? SK_TOTDOCVAL : F_TOTALE); - if (!in_valuta && app().is_split_payment()) - tot -= m.get_real(F_IMPOSTE); - - for (int r = pag.n_rate()-1; r >= 0; r--) - imp += in_valuta ? pag.tval_rata(r) : pag.tlit_rata(r); - - imp.round(pag.round(in_valuta)); - tot.round(pag.round(in_valuta)); - - if (imp != tot) - { - const TString& codval = in_valuta ? m.get(SK_VALUTA) : EMPTY_STRING; - TCurrency curr(imp, codval); - const TString is = curr.string(true); - curr.set_num(tot); - const TString ts = curr.string(true); - ok = yesno_box(FR("Il totale delle rate è %s mentre\n" - "il totale del documento è %s.\n" - "Si desidera registrare ugualmente?"), - (const char*)is, (const char*)ts); - } - } - - if (key == K_ENTER && in_valuta) - { - const real totlit = m.get_real(F_TOTALE); - imp = ZERO; - for (int r = pag.n_rate()-1; r >= 0; r--) - imp += pag.tlit_rata(r); - - imp.round(pag.round(FALSE)); - - if (imp != totlit) - { - TCurrency curr(imp); - const TString is = curr.string(true); - curr.set_num(totlit); - const TString ts = curr.string(true); - const TRectype & rec = cache().get("%VAL", TCurrency::get_firm_val()); - const char * name = (const char *) rec.get("S0"); - - ok = yesno_box(FR("Il totale in %s delle rate è %s mentre\n" - "il totale del documento è %s.\n" - "Si desidera registrare ugualmente?"), - name, (const char*)is, (const char*)ts); - } - } - } - return ok; -} - -bool TPrimanota_application::tipopag_handler(TMask_field& f, KEY key) -{ - if ((key == K_TAB || key == K_ENTER) && f.dirty()) - { - TMask& m = f.mask(); - int t = m.get_int(105); - if (t <= 0) - { - t = 1; - f.set("1"); - } - const char u = m.get(106)[0]; - const TPagamento& pag = app().pagamento(); - bool ok; - const char* s = pag.desc_tipo(t, u, &ok); - if (!ok) m.reset(106); - m.set(107, s); - } - - return true; -} - -bool TPrimanota_application::codpag_handler(TMask_field& f, KEY key) -{ - bool ok = true; - - TMask& m = f.mask(); - TPrimanota_application& a = app(); - const bool set_scad = a.is_fattura() && m.page_enabled(2); // E' attiva pagina 3 - if (key == K_ENTER) - { - if (set_scad && f.empty()) - { - ok = f.error_box(TR("Il codice di pagamento è obbligatorio!")); - - // Nella maschera iva cerco di reperirlo dal cliente - if (m.id2pos(F_CLIENTE) > 0) - { - const char typ = a.clifo(); - const long cod = m.get_long(typ == 'C' ? F_CLIENTE : F_FORNITORE); - TLocalisamfile clifo(LF_CLIFO); - clifo.put(CLI_TIPOCF, typ); - clifo.put(CLI_CODCF, cod); - if (clifo.read() == NOERR) - { - const TString& cp = clifo.get(CLI_CODPAG); - if (cp.not_empty()) - { - f.set(cp); - key = K_TAB; - } - } - } - } - } - - if (key == K_TAB && f.focusdirty() && m.insert_mode() && set_scad) - a.set_scadenze(m); - - return ok; -} - -bool TPrimanota_application::annorif_handler(TMask_field& f, KEY key) -{ - bool ok = true; - if (f.to_check(key)) - { - const TMask& m = f.mask(); - if (f.empty() && !m.get(F_NUMRIF).blank()) - { - const TString& anno = m.get(F_DATADOC).right(4); - f.set(anno); - ok = error_box(TR("L'anno di riferimento partita è obbligatorio!")); - } - } - return ok; -} - -bool TPrimanota_application::numrif_handler(TMask_field& f, KEY key) -{ - bool ok = true; - - if (f.to_check(key, true)) - { - TPrimanota_application& a = app(); - TMask& m = f.mask(); - const bool moviva = a.iva() != nessuna_iva; // Maschera IVA? - - bool required = m.insert_mode() && a.is_fattura(); - required &= key == K_ENTER || f.focusdirty(); - if (moviva) - required &= !(m.get_bool(F_SOLAIVA) || m.field(F_OCCASEDIT).active()); - - if (required && f.get().blank()) - { - if (moviva && m.field(F_CODPAG).empty()) - { - TMask_field& clifo = m.field(a.iva() == iva_vendite ? F_CLIENTE : F_FORNITORE); - clifo.set_dirty(); - clifo.on_hit(); - } - -// TMask_field& anno = m.field(F_ANNORIF); - if (m.get(F_ANNORIF).empty()) - m.set(F_ANNORIF, m.get(F_DATADOC).right(4)); - - f.set(m.get(a._npart_is_prot ? F_PROTIVA : F_NUMDOC)); - key = K_TAB; // Forza la successiva activate_numrif - - ok = f.error_box(TR("Il numero di riferimento partita è obbligatorio!")); - } - - if (key == K_TAB && a.is_fattura()) - a.activate_numrif(m, true); - - if (ok && key == K_ENTER && m.edit_mode()) - { - const TPartita* old = a.partite().first(); - if (old && old->ok() && f.get().blank()) // Se esisteva una partita ma ora non piu' - { - const KEY k = yesnocancel_box(FR("Attenzione la fattura della partita %d %s verra' eliminata.\n" - "Premere SI per cancellarla, NO per ripristinarla, ANNULLA per modificarla"), - old->anno(), (const char*)old->numero()); - switch (k) - { - case K_NO: - f.set(old->numero()); - if (m.get(F_ANNORIF).empty()) - m.set(F_ANNORIF, old->anno()); - break; - case K_ESC: - ok = FALSE; - break; - default: - break; - } - } - } - } - return ok; -} - -bool TPrimanota_application::valuta_handler(TMask_field& f, KEY key) -{ - if (key == K_TAB && f.to_check(key, true)) - { - TMask& m = f.mask(); - const TString& val = f.get(); - const bool full = val.not_empty() && val != TCurrency::get_firm_val(); - - if (full) - { - TEdit_field& dc = m.efield(SK_DATACAMBIO); - if (dc.get().empty()) // Inizializza data cambio se assente - dc.set(m.get(F_DATADOC)); - - if (f.focusdirty()) - { - dc.set_dirty(); - dc.check(RUNNING_CHECK); // Forza ricerca cambio - dc.on_hit(); // Forza messaggi (eventuale copia a pag. 3) - if (m.get(SK_CAMBIO).empty()) - { - TCursor& cur = *dc.browse()->cursor(); - if (cur.items() == 0) // Uso cambio standard - { - const TRectype& recval = cache().get("%VAL", val); - dc.set(recval.get("D0")); - m.enable(SK_CAMBIO); // Abilito per far funzionare la prossima riga - m.set(SK_CAMBIO, recval.get("S4"), true); - } - } - - if (m.id2pos(F_VALUTAINTRA) >= 0) - { - TEdit_field& vi = m.efield(F_VALUTAINTRA); - if (vi.active() && vi.empty()) - { - m.set(F_VALUTAINTRA, val); - m.set(F_CAMBIOINTRA, m.get(SK_CAMBIO), true); - } - } - } - } - else - { - m.set(SK_CAMBIO, "", true); - m.set(SK_TOTDOCVAL, "", true); - } - m.enable(SK_CAMBIO, full); - m.enable(SK_TOTDOCVAL, full); - - if (app().is_fattura()) - m.set(FS_VALUTA, val); // Copia a pagina 3 - } - return true; -} - -bool TPrimanota_application::datacambio_handler(TMask_field& f, KEY key) -{ - if (key == K_TAB && f.to_check(key, true)) - { - if (app().is_fattura()) - f.mask().set(FS_DATACAMBIO, f.get()); // Copia a pagina 3 - } - return true; -} - -bool TPrimanota_application::cambio_handler(TMask_field& f, KEY key) -{ - if (key == K_TAB && f.to_check(key, true)) - { - TMask& m = f.mask(); - TPrimanota_application& a = app(); - - bool update = a.is_fattura(); // Devo ricopiarmi a pagina 3? - if (f.focusdirty()) - { - // Forza il ricalcolo del totale in lire se necessario - a.gioca_cambi(m.insert_mode() ? 0x1 : 0x0); - } - else - update &= !m.is_running(); - - if (update) - m.set(FS_CAMBIO, f.get()); // Copia a pagina 3 - } - return true; -} - -bool TPrimanota_application::reset_handler(TMask_field& f, KEY key) -{ - if (key == K_SPACE) - { - app().reset_pagamento(); - app().set_scadenze(f.mask()); - } - return true; -} - -bool TPrimanota_application::recalc_handler(TMask_field& f, KEY key) -{ - TPrimanota_application& a = app(); - if (key == K_SPACE && a.is_fattura()) - { - TMask& m = f.mask(); - TSheet_field& ps = m.sfield(FS_RATESHEET); - TPagamento& pag = a.pagamento(); - - const bool recalc_aut = f.get().not_empty(); - - if (!recalc_aut && pag.tipo_prima_rata() > 3) - { - // SE CAMBIANO I SIGNIFICATI DEL TIPO PRIMA RATA BISOGNA RIFARE TUTTO - pag.set_tipo_prima_rata(pag.tipo_prima_rata() - 3); - m.set(FS_TIPOPR, pag.desc_tpr()); - pag.set_tpr4(true); - } - else if (recalc_aut && pag.was_tpr4()) - { - // SE CAMBIANO I SIGNIFICATI DEL TIPO PRIMA RATA BISOGNA RIFARE TUTTO - pag.set_tpr4(FALSE); - bool yak = pag.tlit_rata(0) != pag.importo_da_non_dividere(FALSE); - if (pag.in_valuta()) yak |= (pag.tval_rata(0) != pag.importo_da_non_dividere(true)); - if (yak) - { - if (yesno_box(TR("L'importo della prima rata è stato modificato. " - "Con il ricalcolo automatico esso non sarà più" - " modificabile. Si desidera " - "riportare le rate alle condizioni iniziali?"))) - { - pag.set_tipo_prima_rata(pag.tipo_prima_rata() + 3); - m.set(FS_TIPOPR, pag.desc_tpr()); - a.reset_pagamento(); - a.set_scadenze(m); - } - else warning_box(FR("Il tipo prima rata è stato modificato in \"%s\" per " - "poter mantenere la rateazione scelta e la possibilità di " - "ricalcolo automatico"), - pag.desc_tpr()); - } - else - { - pag.set_tipo_prima_rata(pag.tipo_prima_rata() + 3); - m.set(FS_TIPOPR, pag.desc_tpr()); - } - } - - const bool on = pag.tipo_prima_rata() < 4 || !recalc_aut; - ps.enable_cell(0, 1, on); - ps.enable_cell(0, 2, on); - ps.enable_cell(0, 3, on); - - ps.enable_column(3, recalc_aut); - ps.sheet_mask().enable(DLG_DELREC, !recalc_aut); - } - - return true; -} - -bool TPrimanota_application::nrate_handler(TMask_field& f, KEY key) -{ - if (key == K_TAB && f.focusdirty()) - { - TPagamento& pag = app().pagamento(); - pag.set_numero_rate(atoi(f.get())); - if (pag.dirty()) - { - TMask& m = f.mask(); - TSheet_field& ps = m.sfield(FS_RATESHEET); - pag.set_sheet(ps); - app().set_banche(m); - app().pag_rows() = ps.rows_array(); - } - TString4 r; r.format("%d", pag.n_rate()); - f.set(r); - } - return true; -} - -bool TPrimanota_application::codcab_handler(TMask_field& f, KEY key) -{ - if (key == K_TAB && f.focusdirty()) - { - const TMask& m = f.mask(); - const short id = f.dlg() == FS_NSCAB ? FS_NSABI : FS_VSABI; - const int pos = id == FS_NSABI ? 7 : 9; // Posizione codici ABI sullo sheet - const TString16 abi = m.get(id); - const TString16 cab = m.get(id+1); - TSheet_field& ps = m.sfield(FS_RATESHEET); - for (int i = ps.items()-1; i >= 0; i--) // Setta la banca in tutte le righe - { - TToken_string& row = ps.row(i); - row.add(abi, pos); - row.add(cab, pos+1); - } - ps.force_update(); - } - - return true; -} - -bool TPrimanota_application::rataeur_handler(TMask_field& f, KEY key) -{ - TMask& m = app().curr_mask(); - if (key == K_F8 || (key == K_TAB && f.get().empty() && m.is_running())) - { - const TPagamento& pag = app().pagamento(); - if (pag.in_valuta()) - { - const TValuta v(m, FS_VALUTA, FS_DATACAMBIO, FS_CAMBIO); - const real other(f.mask().get(103)); - const char* s = v.val2eur(other).string(); - f.set(s); - } - } - return true; -} - -bool TPrimanota_application::rataval_handler(TMask_field& f, KEY key) -{ - if (key == K_F8 || (key == K_TAB && f.get().empty())) - { - TPagamento& pag = app().pagamento(); - if (pag.in_valuta()) - { - const TValuta v(app().curr_mask(), FS_VALUTA, FS_DATACAMBIO, FS_CAMBIO); - const real other(f.mask().get(102)); - TString80 s(v.eur2val(other).string()); - f.set(s); - } - } - return true; -} - -void TPrimanota_application::reset_pagamento() -{ - TPagamento& pag = pagamento(); - pag.read(); - pag.set_rate_auto(); -} - -void TPrimanota_application::recalc_scadenze(const TDate& d) -{ - TPagamento& pag = pagamento(); - pag.set_inizio(d); - // ridefinisci lo sheet sulla base delle nuove rate - TSheet_field& ps = pags(); - TString_array& rws = pag_rows(); - - pag.set_sheet(ps); - rws = ps.rows_array(); -} - -void TPrimanota_application::set_pagamento(const char* c, const char* d) -{ - if (_pag != NULL) - { - delete _pag; - _pag = NULL; - } - if (c != NULL || d != NULL) - { - if (!TDate::isdate(d)) - d = ""; - _pag = new TPagamento(c, d); - } - if (_pag != NULL && iva() == iva_vendite) - _pag->set_clifo(curr_mask().get_long(F_CLIENTE)); // Dato indispensabile per non accettazione effetti - -} - -void TPrimanota_application::set_totale_pagamento(bool update) -{ - const TMask& m = curr_mask(); - TPagamento& pag = pagamento(); - - const real imposta = m.id2pos(F_IMPOSTE) >= 0 ? m.get_real(F_IMPOSTE) : ZERO; - const real imponibile = m.get_real(F_TOTALE) - imposta; - const real spese = ZERO; - - const TValuta cambio(m, SK_VALUTA, SK_DATACAMBIO, SK_CAMBIO); - if (cambio.in_valuta()) - { - const real imposval = cambio.eur2val(imposta); - const real imponval = m.get_real(SK_TOTDOCVAL) - imposval; - pag.set_total_valuta(imponval, imposval, spese, cambio.cambio(), - imponibile, imposta, spese, cambio.codice()); - } - else - { - if (is_split_payment()) - pag.set_total(imponibile, ZERO, spese); - else - pag.set_total(imponibile, imposta, spese); - } - - if (update) - { - TSheet_field& ps = m.sfield(FS_RATESHEET); - pagamento().set_sheet(ps); // Aggiorna lo sheet - pag_rows() = ps.rows_array(); // copia sheet - } -} - -void TPrimanota_application::set_banche(TMask& m) -{ - // Aggiunge banche: CM000033 - TSheet_field& ps = m.sfield(FS_RATESHEET); - bool update = false; - for (int i = ps.items()-1; i >= 0; i--) - { - TToken_string& row = ps.row(i); - if (row.get_char(7) <= ' ') - { - row.add(m.get(FS_NSABI), 7); - row.add(m.get(FS_NSCAB), 8); - update = true; - } - if (row.get_char(9) <= ' ') - { - row.add(m.get(FS_VSABI), 9); - row.add(m.get(FS_VSCAB), 10); - update = true; - } - } - if (update) - ps.force_update(); -} - -void TPrimanota_application::set_scadenze(TMask& m) -{ - const TString& cp = m.get(F_CODPAG); - const TString& dt = m.get(F_DATADOC); - set_pagamento(cp, dt); - set_totale_pagamento(false); - - TPagamento& pag = pagamento(); - if (m.field(FS_RECALC).enabled()) - { - m.set(FS_RDIFFER, pag.rate_differenziate() ? "1" : "2"); - m.set(FS_NAMEPAG, pag.name()); - m.set(FS_TIPOPR, pag.desc_tpr()); - m.set(FS_MCOMM, pag.mese_commerciale() ? "X" : ""); - m.set(FS_RECALC, m.insert_mode() ? "X" : "", true); - m.set(FS_NRATE, pag.n_rate()); - } - pag.set_rate_auto(); - TSheet_field& ps = m.sfield(FS_RATESHEET); - pag.set_sheet(ps); // prepara lo sheet - set_banche(m); - _pag_rows = ps.rows_array(); // copia sheet -} - -bool TPrimanota_application::read_scadenze(TMask& m) -{ - const int anno = m.get_int(F_ANNORIF); - const TString& numpart = m.get(F_NUMRIF); - - if (anno <= 0 || numpart.blank()) // Partita vuota - return false; - - TBill clifo; cerca_conto_cf(clifo); - const TPartita& part = partite().partita(clifo, anno, numpart); - - const TRectype& testa = _rel->curr(); - const long nreg = testa.get_long(MOV_NUMREG); - const TDate datadoc = testa.get_date(MOV_DATADOC); - - const int npart = part.prima_fattura(nreg); - if (npart <= 0) // Non esiste una riga per questo movimento - { - partite().destroy(); - return false; // Non dovrebbe succedere mai, in quanto gia' beccato sopra! - } - - // Determino il codice pagamento dalla riga di fattura - TString4 codpag; - const TRiga_partite& partita = part.riga(npart); - if (partita.rate() > 0) - { - const TRiga_scadenze& primarata = partita.rata(1); - codpag = primarata.get(SCAD_CODPAG); - } - set_pagamento(codpag, datadoc.string()); - TPagamento& pag = pagamento(); - - const bool in_valuta = ::is_true_value(m.get(SK_VALUTA)); - const bool is_split = !in_valuta && clifo.tipo() == 'C' && anno >= 2015 && is_split_payment(); - - const real totale_cg = m.get_real(in_valuta ? SK_TOTDOCVAL : F_TOTALE); - real totale_sc = partita.importo(in_valuta).valore(); - if (is_split) - totale_sc += m.get_real(F_IMPOSTE); - - if (totale_cg != totale_sc) - { - const TString& codval = m.get(SK_VALUTA); - const TCurrency totdoc_c(totale_cg, codval); - const TCurrency totdoc_s(totale_sc, codval); - const char* i1 = totdoc_c.string(true); - const char* i2 = totdoc_s.string(true); - warning_box(FR("Il totale documento (%s) non corrisponde a quello del saldaconto (%s)."), i1, i2); - } - if (iva() != nessuna_iva && !is_split) - { - const real imposte_cg = m.get_real(F_IMPOSTE); - const real imposte_sc = partita.get_real(PART_IMPOSTA); - if (imposte_cg != imposte_sc) - { - const TCurrency imposte_c(imposte_cg); - const TCurrency imposte_s(imposte_sc); - const TString i1 = imposte_c.string(true); - const TString i2 = imposte_s.string(true); - warning_box(FR("Il totale delle imposte (%s) non corrisponde a quello del saldaconto (%s)."), - (const char*)i1, (const char*)i2); - } - } - - set_totale_pagamento(false); - - TSheet_field& ps = m.sfield(FS_RATESHEET); - - const int protette = partita.ultima_rata_con_abbuoni_diffcam(); - - pag.zap_rate(); // Azzera pagamento - for (int i = 1; i <= partita.rate(); i++) // E' necessario andare in avanti!!! - { - const TRiga_scadenze& scadenza = partita.rata(i); - real importo = scadenza.get_real(SCAD_IMPORTO); - const TDate scad = scadenza.get_date(SCAD_DATASCAD); - const int tipop = scadenza.get_int(SCAD_TIPOPAG); - const bool prot = i <= protette; // Non cancellabile - const TString16 ulc = scadenza.get(SCAD_ULTCLASS); - if (in_valuta) - { - const real impval(scadenza.get(SCAD_IMPORTOVAL)); - pag.set_rata(i-1, impval, importo, scad, tipop, ulc, prot); - } - else - pag.set_rata(i-1, ZERO, importo, scad, tipop, ulc, prot); - - TToken_string& str = ps.row(i-1); - str.add(scadenza.get(SCAD_CODABIPR), 7); - str.add(scadenza.get(SCAD_CODCABPR), 8); - str.add(scadenza.get(SCAD_CODABI), 9); - str.add(scadenza.get(SCAD_CODCAB), 10); - if (i == 1) - { - m.set(FS_NSABI, str.get(7)); - m.set(FS_NSCAB, str.get(8)); - m.set(FS_VSABI, str.get(9)); - m.set(FS_VSCAB, str.get(10)); - m.set(FS_AGENTE, scadenza.get(SCAD_CODAG)); - } - str.add(scadenza.get(SCAD_DESCR), 11); - bool bloc = scadenza.get_bool(SCAD_BLOCCATA); - const TString8 motivo = scadenza.get(SCAD_MOTIVO); - if (bloc || motivo.full()) - { - str.add("X", 12); - str.add(motivo, 13); - str.add(cache().get("%CBP", motivo, "S0"), 14); - } - str.add(prot ? "X" : " ", 15); - } - m.set(FS_RDIFFER, pag.rate_differenziate() ? "1" : "2"); - m.set(FS_NAMEPAG, pag.name()); - m.set(FS_TIPOPR, pag.desc_tpr()); - m.set(FS_MCOMM, pag.mese_commerciale() ? "X" : "", true); - m.set(FS_RECALC, m.insert_mode() ? "X" : "", true); - m.set(FS_NRATE, pag.n_rate()); - - pag.set_sheet(ps); - _pag_rows = ps.rows_array(); - - return true; -} - -int TPrimanota_application::cerca_conto_cf(TBill& bill) const -{ - TString_array& a = cgs().rows_array(); - if (iva() != nessuna_iva) - { - const char tipocf = clifo(); - const long codcf = curr_mask().get_long(tipocf == 'C' ? F_CLIENTE : F_FORNITORE); - for (int r = 0; r < a.items(); r++) - { - TToken_string& row = a.row(r); - bill.get(row, 2, 0x1); - if (bill.tipo() == tipocf && bill.codclifo() == codcf) - return r; - } - bill.set(0, 0, codcf, tipocf); - } - else - { - for (int r = 0; r < a.items(); r++) - { - TToken_string& row = a.row(r); - bill.get(row, 2, 0x1); - if (bill.tipo() > ' ') - return r; - } - if (a.items() > 0) - { - TToken_string& row = a.row(0); - bill.get(row, 2, 0x1); - return 0; - } - else - bill.set(0, 0, 0, ' '); - } - return -1; -} - -void TPrimanota_application::write_scadenze(const TMask& m) -{ - const long nreg = m.get_long(F_NUMREG); - const int anno = m.get_int(F_ANNORIF); - const TString numpart(m.get(F_NUMRIF)); - - TPartita* oldgame = partite().first(); - TPartita* newgame = NULL; - - int nuova_riga = 0; - - if (anno > 0 && !numpart.blank() && m.page_enabled(2) && !m.get_real(F_TOTALE).is_zero()) - { - const int tmov = causale().tipomov(); - const TString& desc = m.get(F_DESCR); - const TString& codpag = m.get(F_CODPAG); - const TString8 codcaus = causale().codice(); - const TValuta cambio(m, SK_VALUTA, SK_DATACAMBIO, SK_CAMBIO); - const TString& agente = m.get(FS_AGENTE); - const char sezione = causale().sezione(1); //get_cgs_imp(0).sezione(); // Dare/Avere - - TBill clifo; - const int riga_clifo = cerca_conto_cf(clifo); - CHECK(riga_clifo >= 0, "Impossibile generare la fattura senza gruppo/conto cliente"); - newgame = new TPartita(clifo, anno, numpart); - - const int row = newgame->prima_fattura(nreg); // Riga fattura di questo movimento - TRiga_partite& partita = row <= 0 ? newgame->new_row() : newgame->riga(row); - nuova_riga = partita.get_int(PART_NRIGA); - - TPagamento& pag = pagamento(); - - // put data on partita - partita.put(PART_TIPOMOV, tmov); - partita.put(PART_NREG, nreg); - partita.put(PART_NUMRIG, riga_clifo+1); - partita.put(PART_DATAREG, m.get(F_DATAREG)); - partita.put(PART_DATADOC, m.get(F_DATADOC)); - partita.put(PART_NUMDOC, m.get(F_NUMDOC)); - partita.put(PART_DESCR, desc); - partita.put(PART_CODCAUS, codcaus); - partita.put(PART_REG, causale().reg().name()); - partita.put(PART_SEZ, sezione); - - if (clifo.gruppo() != partita.get_int(PART_GRUPPOCL) || - clifo.conto() != partita.get_int(PART_CONTOCL)) - { - partita.put(PART_GRUPPOCL, clifo.gruppo()); - partita.put(PART_CONTOCL, clifo.conto()); - } - - if (iva() != nessuna_iva) - partita.put(PART_PROTIVA, m.get_long(F_PROTIVA)); - - TImporto totdoc = get_cgs_imp(riga_clifo); - totdoc.normalize(sezione); - partita.put(PART_IMPTOTDOC, totdoc.valore()); - - cambio.put(partita); - const bool in_valuta = cambio.in_valuta(); - - if (in_valuta) - partita.put(PART_IMPTOTVAL, m.get(SK_TOTDOCVAL)); - - real imponibile, imponibile_val; - int i; - - for (i = pag.n_rate()-1; i >= 0; i--) - { - if (in_valuta) - imponibile_val += pag.tval_rata(i); - imponibile += pag.tlit_rata(i); - } - - partita.put(PART_IMPORTO, imponibile); - partita.put(PART_IMPORTOVAL, imponibile_val); - partita.put(PART_SPESE, pag.spese(in_valuta)); - if (iva() != nessuna_iva) - partita.put(PART_IMPOSTA, pag.imposta(in_valuta)); - else - partita.zero(PART_IMPOSTA); - - TSheet_field& ps = m.sfield(FS_RATESHEET); - const int new_rate = ps.items(); - const int nrate = partita.rate(); - TArray ant; - - for (int i = 0; i < new_rate; i++) - { - real* r = new real; - ant.add(r); - if (i < nrate) - *r = partita.rata(i + 1).get_real(SCAD_IMPORTOANT); - } - for (int i = new_rate; i < nrate; i++) - ((real &)ant[new_rate - 1]) += partita.rata(i + 1).get_real(SCAD_IMPORTOANT); - - partita.elimina_rata(-1); // Elimina tutte le rate eventuali - for (i = 0; i < ps.items(); i++) - { - TToken_string& row = ps.row(i); - TRiga_scadenze& scadenza = partita.new_row(); - - scadenza.put(SCAD_CODPAG, codpag); // Codice pagamento - scadenza.put(SCAD_CODAG, agente); // Codice agente - - scadenza.put(SCAD_DATASCAD, row.get(0)); // 0 = Data scadenza - scadenza.put(SCAD_IMPORTO, row.get()); // 1 = Importo - scadenza.put(SCAD_IMPORTOVAL, row.get()); // 2 = Importo in valuta - row.get(); // 3 = Percentuale - scadenza.put(SCAD_TIPOPAG, row.get()); // 4 = Tipo pagamento - scadenza.put(SCAD_ULTCLASS, row.get()); // 5 = Ulteriore classificazione - row.get(); // 6 = Descrizione pagamento - scadenza.put(SCAD_CODABIPR, row.get()); // 7 = Ns ABI - scadenza.put(SCAD_CODCABPR, row.get()); // 8 = Ns CAB - scadenza.put(SCAD_CODABI, row.get()); // 9 = Vs ABI - scadenza.put(SCAD_CODCAB, row.get()); //10 = Vs CAB - scadenza.put(SCAD_DESCR, row.get()); //11 = Note - scadenza.put(SCAD_BLOCCATA, row.get()); //12 = Non pagabile - scadenza.put(SCAD_MOTIVO, row.get()); //13 = Motivo blocco - scadenza.put(SCAD_IMPORTOANT, ((real &)ant[i])); - } - } - - if (oldgame != NULL) - { - const int rigafatt = oldgame->prima_fattura(nreg); - if (rigafatt > 0) - { - if (newgame == NULL || *oldgame != *newgame) - { - bool sposta = newgame != NULL; - if (sposta) - { - sposta = (oldgame->conto().sottoconto() == newgame->conto().sottoconto()); - if (sposta) - sposta = yesno_box(FR("Si desidera spostare la fattura e gli eventuali\n" - "pagamenti relativi nella partita %d %s?"), anno, (const char*)numpart); - } - if (sposta) - { - oldgame->sposta_riga(rigafatt, *newgame, nuova_riga); - } - else - { - oldgame->scollega_pagamenti(rigafatt); - oldgame->rimuovi_riga(rigafatt); - } - oldgame->rewrite(); - } - else - { - oldgame->sposta_riga(rigafatt, *newgame, nuova_riga); - } - } - } - - if (newgame != NULL) // Se non ho cancellato il numero partita ... - { - newgame->write(); // Salva nuova partita - delete newgame; - } -} - - -/////////////////////////////////////////////////////////// -// Gestione pagamenti -/////////////////////////////////////////////////////////// - -bool TPrimanota_application::showpartite_handler(TMask_field& f, KEY k) -{ - bool ok = true; - TMask& m = f.mask(); - TPrimanota_application& a = app(); - TMask& cm = a.curr_mask(); - - const char tipo = m.get(CG_ROWTYPE)[0]; - const bool is_nota = tipo == 'T' && a.causale().tipomov() == 2 && cm.field(F_NUMRIF).active(); - const bool tras = a._as400 && a.causale().tipomov() > 1 && m.get(CG_TIPO).not_empty(); - - if (tipo == 'K' || is_nota || tras) - { - TSheet_field& s = *m.get_sheet(); - const int riga = s.selected(); - - if (k == K_SPACE) - { - const bool ok = a.edit_partite(m, riga); - if (ok && riga < s.items()) - k = K_ENTER; - } - - if (k == K_ENTER) - { - const long curreg = cm.insert_mode() ? NUMREG_PROVVISORIO : cm.get_long(F_NUMREG); - const TImporto importo(a.get_cgs_imp(riga)); - const TImporto speso(a.partite().importo_speso(curreg, riga+1)); - - if (tipo == 'K' && !speso.is_zero()) - { - a.disable_cgs_cells(riga, 'K'); - a.cgs().force_update(riga); - } - - if (importo != speso) - { - TString msg(128); - const TCurrency curr(speso.valore()); - msg.format(FR("L'importo sul saldaconto della riga %d è %s"), (riga+1), (const char*)curr.string(true)); - if (!speso.is_zero()) - msg << (speso.sezione() == 'A' ? TR(" Avere") : TR(" Dare")); - return f.error_box(msg); - } - f.set_focus(); - } - } - return ok; -} - - -// deleting significato -// -1 sto cancellando tutto -// 0 non sto cancellando nulla -// 1 sto cancellando la prima riga contabile -// n sto cancellando la ennesima riga contabile - -bool TPrimanota_application::notify_edit_pagamento(TPartita& p, TRectype& new_pag, - const TValuta& valuta, int deleting) -{ - const int nriga = new_pag.get_int(PAGSCA_NRIGA); // Riga fattura - const int nrata = new_pag.get_int(PAGSCA_NRATA); // Numero rata - const int nrigp = new_pag.get_int(PAGSCA_NRIGP); // Riga pagamento - const TRectype& old_pag = p.pagamento(nriga, nrata, nrigp); - const TRiga_partite& somma = p.riga(nrigp); - const int riga_contabile = somma.get_int(PART_NUMRIG); // Riga movimento - TSheet_field& sheet = cgs(); - TBill new_conto; new_conto.get(new_pag, true); - - if (_as400 || !new_conto.ok()) - deleting = -1; // Ignora contropartite - - if (deleting >= 0) - { - const char sez = somma.sezione(); // Sezione importo e ritenute - const char controsez = sez == 'A' ? 'D' : 'A'; // Sezione contropartita - - // Aggiornamento contopartita - const TImporto old_importo(controsez, old_pag.get_real(PAGSCA_IMPORTO)); - TBill old_conto; old_conto.get(old_pag, true); - - const TImporto new_importo(controsez, new_pag.get_real(PAGSCA_IMPORTO)); - - if (old_importo != new_importo || old_conto != new_conto) - { - int old_riga = _easy_sal ? type2pos('I') : bill2pos(old_conto, 'I'); - if (old_riga >= 0) - { - const bool empty = sub_cgs_imp(old_riga, old_importo); - if (empty && (new_importo.is_zero() || new_conto != old_conto)) - sheet.destroy(old_riga); - } - else - if (!old_importo.is_zero()) - { - TImporto imp = old_importo; imp.swap_section(); - set_cgs_row(old_riga, imp, old_conto, "", 'I'); - } - - // Importo della contropartita - if (!new_importo.is_zero() && new_conto.ok()) - { - int new_riga = _easy_sal ? type2pos('I') : bill2pos(new_conto, 'I'); - if (new_riga < 0) - { - const int tp = somma.get_int(PART_TIPOPAG); - const int da = p.tipopag2causale(tp); - const TString descagg(causale().desc_agg(da)); - new_riga = set_cgs_row(new_riga, new_importo, new_conto, descagg, 'I'); - - TToken_string& cli_row = sheet.row(riga_contabile-1); - TBill contro; contro.get(cli_row, 9, 0x3); - if (!contro.ok()) // Completa controparita della riga cliente se necessario - new_conto.add_to(cli_row, 9, 0x3); - TString80 descr = cli_row.get(CG_DESCR-FIRST_FIELD); - if (descr.blank()) // Compila descrizione della riga cliente se necessario - { - descr = somma.get(PART_DESCR); - cli_row.add(descr, CG_DESCR-FIRST_FIELD); - } - - // Compila contropartita della nuova riga - TBill clifo; clifo.get(cli_row, 2, 0x3); - TToken_string& new_row = sheet.row(new_riga); - clifo.add_to(new_row, 9, 0x3); - } - else - { - add_cgs_imp(new_riga, new_importo); // Incrementa importo sulla banca - if (_easy_sal) - { - // Aggiorna conto della riga della banca - TToken_string& new_row = sheet.row(new_riga); - new_conto.add_to(new_row, 2, 0x3); - // Aggiorna contropartita delle righe cliente - FOR_EACH_SHEET_ROW(sheet, r, cli_row) if (row_type(*cli_row) == 'K') - new_conto.add_to(*cli_row, 9, 0x3); - } - } - } - } - - // Aggiornamento ritenute fiscali - const real old_ritenute(old_pag.get(PAGSCA_RITENUTE)); - const real new_ritenute(new_pag.get(PAGSCA_RITENUTE)); - if (old_ritenute != new_ritenute) - { - const TImporto grow_ritenute(controsez, new_ritenute-old_ritenute); - const int riga = type2pos('F'); - if (riga < 0) - { - TBill conto_rit; causale().bill(RIGA_PAG_RITFIS, conto_rit); - const TString desc(causale().desc_agg(RIGA_PAG_RITFIS)); - set_cgs_row(riga, grow_ritenute, conto_rit, desc, 'F'); - } - else - { - const bool empty = add_cgs_imp(riga, grow_ritenute); - if (empty) sheet.destroy(riga); - } - } - - // Aggiornamento ritenute sociali - const real old_ritsoc(old_pag.get(PAGSCA_RITSOC)); - const real new_ritsoc(new_pag.get(PAGSCA_RITSOC)); - if (old_ritsoc != new_ritsoc) - { - const TImporto grow_ritenute(causale().sezione_ritsoc(), new_ritsoc-old_ritsoc); - const int riga = type2pos('S'); - if (riga < 0) - { - TBill conto_rit; causale().bill(RIGA_PAG_RITSOC, conto_rit); - const TString desc(causale().desc_agg(RIGA_PAG_RITSOC)); - set_cgs_row(riga, grow_ritenute, conto_rit, desc, 'S'); - } - else - { - const bool empty = add_cgs_imp(riga, grow_ritenute); - if (empty) sheet.destroy(riga); - } - } - } // if (deleting >= 0) - - char old_ap, new_ap; - TImporto old_abbuono, new_abbuono, old_diffcam, new_diffcam; - const bool empty = p.modifica_pagamento(new_pag, valuta, - old_ap, old_abbuono, old_diffcam, - new_ap, new_abbuono, new_diffcam, true); - - if (deleting >= 0) - { - // Se c'e' differenza negli abbuoni - if (old_abbuono != new_abbuono || old_ap != new_ap) - { - if (old_ap != ' ') // Se c'era un abbuono ... - { - const int riga_abb = type2pos(old_ap); - CHECK(riga_abb >= 0, "Chiss'è fregato gli abbuoni?"); - const bool empty = add_cgs_imp(riga_abb, old_abbuono); - if (empty && new_ap != old_ap) - sheet.destroy(riga_abb); - - if (deleting != riga_contabile) - { - // Sottrae l'abbuono con la sezione invertita dalla riga contabile - sub_cgs_imp(riga_contabile-1, old_abbuono); - } - } - if (new_ap != ' ') // Se ci sono abbuoni - { - const int riga_abb = type2pos(new_ap); - - new_abbuono.swap_section(); - new_abbuono.normalize(); - - if (riga_abb < 0) - { - const int rc = new_ap == 'A' ? RIGA_ABBUONI_ATTIVI : RIGA_ABBUONI_PASSIVI; - TBill conto_abb; causale().bill(rc, conto_abb); - const TString desc(causale().desc_agg(rc)); - app().set_cgs_row(riga_abb, new_abbuono, conto_abb, desc, new_ap); - } - else - add_cgs_imp(riga_abb, new_abbuono); - - if (deleting != riga_contabile) - { - // Aggiunge l'abbuono con la sezione invertita - sub_cgs_imp(riga_contabile-1, new_abbuono); - } - } - } - - // Se c'e' variazione nella differenza cambi - if (old_diffcam != new_diffcam) - { - const int riga_diffcam = type2pos('C'); - TImporto grow_diffcam(old_diffcam); - grow_diffcam -= new_diffcam; - grow_diffcam.normalize(); - - if (riga_diffcam < 0) - { - TBill conto_diffcam; causale().bill(RIGA_DIFFCAM, conto_diffcam); - const TString desc(causale().desc_agg(RIGA_DIFFCAM)); - set_cgs_row(riga_diffcam, grow_diffcam, conto_diffcam, desc, 'C'); - } - else - { - const bool empty = add_cgs_imp(riga_diffcam, grow_diffcam); - if (empty) - sheet.destroy(riga_diffcam); - } - - if (deleting != riga_contabile) - { - // Aggiunge la differenza con la sezione invertita - sub_cgs_imp(riga_contabile-1, grow_diffcam); - } - } - } // if (deleting >= 0) - - return empty; -} - -// Scorre tutte le righe della partita ed azzera i pagamenti relativi a alla riga contabile -// cancellata e decrementa i numeri riga superiori a numrig -bool TPrimanota_application::notify_cgline_deletion(TPartita& partita, long nreg, int numrig) -{ - bool found = FALSE; - int p; - - // Al primo "giro" elimino i pagamenti dalla rate - for (p = partita.last(); p > 0; p = partita.pred(p)) - { - TRiga_partite& part = partita.riga(p); - if (part.is_fattura()) - { - for (int r = part.rate(); r > 0; r--) - { - const TRiga_scadenze& scad = part.rata(r); - for (int s = scad.last(); s > 0; s = scad.pred(s)) - { - const TRiga_partite& sum = partita.riga(s); - if (sum.get_long(PART_NREG) == nreg && - (numrig <= 0 || sum.get_int(PART_NUMRIG) == numrig)) - { - TRectype nul_pag(scad.row(s)); - nul_pag.zero(PAGSCA_IMPORTO); - nul_pag.zero(PAGSCA_IMPORTOVAL); - nul_pag.zero(PAGSCA_RITENUTE); - nul_pag.zero(PAGSCA_RITSOC); - nul_pag.put(PAGSCA_ACCSAL, 'A'); - const TValuta val; // Non cambiare valuta - notify_edit_pagamento(partita, nul_pag, val, numrig); - found = true; - } - } - } - } - } - - // Alla fine del "giro" elimino i pagamenti non assegnati - TRecord_array& unas = partita.unassigned(); - for (int u = unas.last_row(); u > 0; u = unas.pred_row(u)) - { - const TRiga_partite& sum = partita.riga(u); - if (sum.get_long(PART_NREG) == nreg && - (numrig <= 0 || sum.get_int(PART_NUMRIG) == numrig)) - { - TRectype nul_pag(unas.row(u, FALSE)); - nul_pag.zero(PAGSCA_IMPORTO); - nul_pag.zero(PAGSCA_IMPORTOVAL); - nul_pag.zero(PAGSCA_RITENUTE); - nul_pag.zero(PAGSCA_RITSOC); - nul_pag.put(PAGSCA_ACCSAL, 'A'); // Inutile, ma l'ho copiato da sopra - const TValuta val; // Non cambiare valuta - notify_edit_pagamento(partita, nul_pag, val, numrig); - found = true; - } - } - - // Al secondo "giro" decremento NRIGP dalle righe di pagamento - if (numrig > 0) - { - for (p = partita.last(); p > 0; p = partita.pred(p)) - { - TRiga_partite& part = partita.riga(p); - if (!part.is_fattura()) - { - if (part.get_long(PART_NREG) == nreg) - { - const int nr = part.get_int(PART_NUMRIG); - if (nr > numrig) - part.put(PART_NUMRIG, nr-1); - } - } - } - } - - return found; -} - -bool TPrimanota_application::notify_cgline_deletion(int numrig) -{ - bool found = FALSE; - - const TMask& m = curr_mask(); - const long nreg = m.insert_mode() ? NUMREG_PROVVISORIO : m.get_long(F_NUMREG); - -// if (curr_mask().edit_mode()) -// partite().add_numreg(nreg); // Gia' fatto dalla read - - for (TPartita* game = partite().first(); game; game = partite().next()) - found |= notify_cgline_deletion(*game, nreg, numrig); - - return found; -} +// -------------------------------------------------------------------------- +// Scadenzario +// fv 24/8/94 +// -------------------------------------------------------------------------- + +#include +#include "cgpagame.h" +#include "cgsaldac.h" + +#include "cg2102.h" +#include "cg2100.h" +#include "cg21sld.h" + +#include +#include +#include + +TPagamento& TPrimanota_application::pagamento() const +{ + CHECK(_pag, "Pagamento nullo"); + return *_pag; +} + +bool TPrimanota_application::pag_notify(TSheet_field& ps, int r, KEY k) +{ + TMask* msk = &(ps.mask()); + +// if (!msk->is_running()) return true; // Necessario per evitare il CHECK sul pagamento nullo + if (!app().is_fattura() || app()._pag == NULL) return true; // Necessario per evitare il CHECK sul pagamento nullo + + TPagamento& pag = app().pagamento(); + TString_array& rws = app().pag_rows(); + + const int rdiff = msk->get_int(FS_RDIFFER); + const bool in_valuta = pag.in_valuta(); +// const int impos = in_valuta ? 2 : 1; + + // ts contiene la vecchia riga, ns la nuova + TToken_string ts(128), ns(128); + TString16 banca; + + bool doit = true, m_impl = FALSE, m_impv = FALSE, m_perc = FALSE; + bool m_scad = FALSE, m_tipo = FALSE, m_ulc = FALSE, mod = FALSE; + word ahiahi = P_OK; + + TString news(10), newl(15), newv(15), newp(15), newt(1), newu(1); + + bool recalc = msk->get_bool(FS_RECALC); + bool mcomm = msk->get_bool(FS_MCOMM); + + switch (k) + { + case K_SPACE: + { + const bool can = !recalc && pag.n_rate() > 1 && ps.row(r).get_char(14) != 'X'; + ps.sheet_mask().enable(DLG_DELREC, can); + } + break; + case K_ENTER: + ns = ps.row(r); + ts = rws.row(r); + + news = ns.get(0); // Data scadenza + newl = ns.get(1); // Imponibile (lire) + newv = ns.get(2); // Imponibile (valuta) + newp = ns.get(3); // Percentuale + newt = ns.get(4); // Tipo pagamento + newu = ns.get(5); // Ulteriore classificazione + + // qui viene il bello, si fa per dire + if (news != ts.get(0)) // modificata data scadenza + mod = m_scad = true; + if (real(newp) != real(ts.get(3))) // modificata percentuale + mod = m_perc = true; + if (real(newl) != real(ts.get(1))) // modificato importo lire + { + // se si modifica la percentuale nessun importo viene cagato + if ((recalc && !m_perc) || (!recalc)) + mod = m_impl = true; + } + if (in_valuta && real(newv) != real(ts.get(2))) + { + // se si modifica la percentuale nessun importo viene cagato + if ((recalc && !m_perc) || (!recalc)) + mod = m_impv = true; + } + if (newt != ts.get(4)) // modificato tipo pagamento + mod = m_tipo = m_ulc = true; + + if (newu != ts.get(5)) // modificata ulteriore classificazione + mod = m_ulc = true; + else if (m_tipo) + { + // forza reset di ulc se si e' modificato il tipo rata + m_ulc = true; + newu = ""; + } + + break; + case K_DEL: + doit = !recalc && pag.n_rate() > 1 && ps.row(r).get_char(15) != 'X'; // non cancellabile + if (doit) + { + pag.remove_rata(r); + msk->set(FS_NRATE, pag.n_rate()); + } + break; + case K_INS: + // permette aggiunta e cancellazione solo se non c'e' ricalcolo automatico + doit = !recalc; + break; + case K_CTRL+K_INS: // Post inserimento + { + const int prev = r - 1; + const int gio_scad = pag.scad_rata(prev); + TDate data_scad(pag.data_rata(prev)); + + pag.add_rata(ZERO, gio_scad, pag.tipo_rata(prev), pag.ulc_rata(prev)); + pag.next_scad(data_scad, gio_scad, pag.mese_commerciale(), r); + pag.set_datarata(r, data_scad); +// pag.adjust_fixed_scad(); + pag.set_sheet(ps); + rws = ps.rows_array(); + } + break; + default: + break; + } + + // settato da recalc_rate se occorre ridefinire lo sheet + // aggiungendo o togliendo righe + bool need_recalc = FALSE; + + if (k == K_ENTER) + { + if (mod && recalc) + { + // ricalcola sheet come sai fare tu + ahiahi = pag.recalc_rate(r, m_perc, + ((m_perc || m_impv) ? + (m_perc ? (const char*)newp : (const char*)newv) : NULL), + (m_impl ? (const char*)newl : NULL), + (m_scad ? (const char*)news : NULL), + (m_tipo ? (const char*)newt : NULL), + (m_ulc ? (const char*)newu : NULL), + rdiff, mcomm, need_recalc); + + // see if rdiff changed + msk->set(FS_RDIFFER, pag.rate_differenziate() ? "1" : "2"); + msk->set(FS_NRATE, pag.n_rate()); + } + if (!recalc) + { + ahiahi = P_OK; + // put data as they are + TToken_string& trw = pag.rata(r); + TToken_string& trs = ps.row(r); + if (m_tipo) + trw.add(newt,2); + if (m_ulc) + trw.add(newu, 5); + if (m_scad) + { + trw.add(news,3); + if (r > 0) + { + TDate d(news); + int scd = (int)(d - pag.data_rata(r-1)); + if (pag.mese_commerciale() && (scd % 30) != 0) + scd = 30 * ((scd/30)+1); + trw.add(scd, 0); + } + trs.add(news, 0); + } + if (m_impl) + { + trw.add(newl,7); + if (!in_valuta) + { + // si ricalcola la percentuale a modifica importo in lire solo + // se non c'e' l'importo in valuta + const real p = pag.recalc_percrata(r, FALSE); + trs.add(p.string(), 3); + } + } + if (m_impv) + { + trw.add(newv,4); + const real p = pag.recalc_percrata(r, true); + trs.add(p.string(), 3); + } + if (mod) + { + rws.row(r) = trs; + ps.force_update(r); + } + } + + if (ahiahi) // any error? Rimetti le righe com'erano prima + { + if (recalc) + { + TString err(80); pag.strerr(ahiahi,err); + warning_box(err); + ps.row(r) = rws.row(r); + ps.force_update(r); + } + } + else if (recalc && mod && need_recalc) + { + // ridefinisci lo sheet sulla base delle nuove rate + pag.set_sheet(ps); + rws = ps.rows_array(); + } + } + + return doit; +} + +bool TPrimanota_application::pag_sheet_handler(TMask_field& f, KEY key) +{ + bool ok = true; + if (key == K_TAB || key == K_ENTER) + { + TMask& m = f.mask(); + + TPagamento& pag = app().pagamento(); + const TDate dt(m.get(F_DATADOC)); + + pag.set_datadoc(dt); + word err = pag.validate(); + if (m.edit_mode()) + err &= ~(P_RSUM | P_TOTNCLIT | P_TOTNCVAL ); // Ignora totale importi e rate + if ((err && P_ZEROLIT) && yesno_box(TR("Almeno una rata ha importo nullo in Euro si desidera continuare?"))) + err &= ~P_ZEROLIT; + if ((err && P_ZEROVAL) && yesno_box(TR("Almeno una rata ha importo nullo in valuta si desidera continuare?"))) + err &= ~P_ZEROVAL; + if (err != P_OK) + { + TString s(256); + pag.strerr(err, s); + return f.error_box(s); + } + + const bool in_valuta = pag.in_valuta(); + real imp; + + if (key == K_ENTER) + { + real tot = m.get_real(in_valuta ? SK_TOTDOCVAL : F_TOTALE); + if (!in_valuta && app().is_split_payment()) + tot -= m.get_real(F_IMPOSTE); + + for (int r = pag.n_rate()-1; r >= 0; r--) + imp += in_valuta ? pag.tval_rata(r) : pag.tlit_rata(r); + + imp.round(pag.round(in_valuta)); + tot.round(pag.round(in_valuta)); + + if (imp != tot) + { + const TString& codval = in_valuta ? m.get(SK_VALUTA) : EMPTY_STRING; + TCurrency curr(imp, codval); + const TString is = curr.string(true); + curr.set_num(tot); + const TString ts = curr.string(true); + ok = yesno_box(FR("Il totale delle rate è %s mentre\n" + "il totale del documento è %s.\n" + "Si desidera registrare ugualmente?"), + (const char*)is, (const char*)ts); + } + } + + if (key == K_ENTER && in_valuta) + { + const real totlit = m.get_real(F_TOTALE); + imp = ZERO; + for (int r = pag.n_rate()-1; r >= 0; r--) + imp += pag.tlit_rata(r); + + imp.round(pag.round(FALSE)); + + if (imp != totlit) + { + TCurrency curr(imp); + const TString is = curr.string(true); + curr.set_num(totlit); + const TString ts = curr.string(true); + const TRectype & rec = cache().get("%VAL", TCurrency::get_firm_val()); + const char * name = (const char *) rec.get("S0"); + + ok = yesno_box(FR("Il totale in %s delle rate è %s mentre\n" + "il totale del documento è %s.\n" + "Si desidera registrare ugualmente?"), + name, (const char*)is, (const char*)ts); + } + } + } + return ok; +} + +bool TPrimanota_application::tipopag_handler(TMask_field& f, KEY key) +{ + if ((key == K_TAB || key == K_ENTER) && f.dirty()) + { + TMask& m = f.mask(); + int t = m.get_int(105); + if (t <= 0) + { + t = 1; + f.set("1"); + } + const char u = m.get(106)[0]; + const TPagamento& pag = app().pagamento(); + bool ok; + const char* s = pag.desc_tipo(t, u, &ok); + if (!ok) m.reset(106); + m.set(107, s); + } + + return true; +} + +bool TPrimanota_application::codpag_handler(TMask_field& f, KEY key) +{ + bool ok = true; + + TMask& m = f.mask(); + TPrimanota_application& a = app(); + const bool set_scad = a.is_fattura() && m.page_enabled(2); // E' attiva pagina 3 + if (key == K_ENTER) + { + if (set_scad && f.empty()) + { + ok = f.error_box(TR("Il codice di pagamento è obbligatorio!")); + + // Nella maschera iva cerco di reperirlo dal cliente + if (m.id2pos(F_CLIENTE) > 0) + { + const char typ = a.clifo(); + const long cod = m.get_long(typ == 'C' ? F_CLIENTE : F_FORNITORE); + TLocalisamfile clifo(LF_CLIFO); + clifo.put(CLI_TIPOCF, typ); + clifo.put(CLI_CODCF, cod); + if (clifo.read() == NOERR) + { + const TString& cp = clifo.get(CLI_CODPAG); + if (cp.not_empty()) + { + f.set(cp); + key = K_TAB; + } + } + } + } + } + + if (key == K_TAB && f.focusdirty() && m.insert_mode() && set_scad) + a.set_scadenze(m); + + return ok; +} + +bool TPrimanota_application::annorif_handler(TMask_field& f, KEY key) +{ + bool ok = true; + if (f.to_check(key)) + { + const TMask& m = f.mask(); + if (f.empty() && !m.get(F_NUMRIF).blank()) + { + const TString& anno = m.get(F_DATADOC).right(4); + f.set(anno); + ok = error_box(TR("L'anno di riferimento partita è obbligatorio!")); + } + } + return ok; +} + +bool TPrimanota_application::numrif_handler(TMask_field& f, KEY key) +{ + bool ok = true; + + if (f.to_check(key, true)) + { + TPrimanota_application& a = app(); + TMask& m = f.mask(); + const bool moviva = a.iva() != nessuna_iva; // Maschera IVA? + + bool required = m.insert_mode() && a.is_fattura(); + required &= key == K_ENTER || f.focusdirty(); + if (moviva) + required &= !(m.get_bool(F_SOLAIVA) || m.field(F_OCCASEDIT).active()); + + if (required && f.get().blank()) + { + if (moviva && m.field(F_CODPAG).empty()) + { + TMask_field& clifo = m.field(a.iva() == iva_vendite ? F_CLIENTE : F_FORNITORE); + clifo.set_dirty(); + clifo.on_hit(); + } + if (m.get(F_ANNORIF).empty()) + m.set(F_ANNORIF, m.get_date(F_DATADOC).year()); + + if (a.npart_is_prot() && m.id2pos(F_PROTIVA) > 0) + f.set(m.get(F_PROTIVA)); + else + { + if (a.npart_is_month() && m.id2pos(F_PROTIVA) > 0 && m.field(F_CLIENTE).shown()) + f.set(format("%02d", m.get_date(F_DATADOC).month())); + else + f.set(m.get(F_NUMDOC)); + } + key = K_TAB; // Forza la successiva activate_numrif + + ok = f.error_box(TR("Il numero di riferimento partita è obbligatorio!")); + } + + if (key == K_TAB && a.is_fattura()) + a.activate_numrif(m, true); + + if (ok && key == K_ENTER && m.edit_mode()) + { + const TPartita* old = a.partite().first(); + if (old && old->ok() && f.get().blank()) // Se esisteva una partita ma ora non piu' + { + const KEY k = yesnocancel_box(FR("Attenzione la fattura della partita %d %s verra' eliminata.\n" + "Premere SI per cancellarla, NO per ripristinarla, ANNULLA per modificarla"), + old->anno(), (const char*)old->numero()); + switch (k) + { + case K_NO: + f.set(old->numero()); + if (m.get(F_ANNORIF).empty()) + m.set(F_ANNORIF, old->anno()); + break; + case K_ESC: + ok = FALSE; + break; + default: + break; + } + } + } + } + return ok; +} + +bool TPrimanota_application::valuta_handler(TMask_field& f, KEY key) +{ + if (key == K_TAB && f.to_check(key, true)) + { + TMask& m = f.mask(); + const TString& val = f.get(); + const bool full = val.not_empty() && val != TCurrency::get_firm_val(); + + if (full) + { + TEdit_field& dc = m.efield(SK_DATACAMBIO); + if (dc.get().empty()) // Inizializza data cambio se assente + dc.set(m.get(F_DATADOC)); + + if (f.focusdirty()) + { + dc.set_dirty(); + dc.check(RUNNING_CHECK); // Forza ricerca cambio + dc.on_hit(); // Forza messaggi (eventuale copia a pag. 3) + if (m.get(SK_CAMBIO).empty()) + { + TCursor& cur = *dc.browse()->cursor(); + if (cur.items() == 0) // Uso cambio standard + { + const TRectype& recval = cache().get("%VAL", val); + dc.set(recval.get("D0")); + m.enable(SK_CAMBIO); // Abilito per far funzionare la prossima riga + m.set(SK_CAMBIO, recval.get("S4"), true); + } + } + + if (m.id2pos(F_VALUTAINTRA) >= 0) + { + TEdit_field& vi = m.efield(F_VALUTAINTRA); + if (vi.active() && vi.empty()) + { + m.set(F_VALUTAINTRA, val); + m.set(F_CAMBIOINTRA, m.get(SK_CAMBIO), true); + } + } + } + } + else + { + m.set(SK_CAMBIO, "", true); + m.set(SK_TOTDOCVAL, "", true); + } + m.enable(SK_CAMBIO, full); + m.enable(SK_TOTDOCVAL, full); + + if (app().is_fattura()) + m.set(FS_VALUTA, val); // Copia a pagina 3 + } + return true; +} + +bool TPrimanota_application::datacambio_handler(TMask_field& f, KEY key) +{ + if (key == K_TAB && f.to_check(key, true)) + { + if (app().is_fattura()) + f.mask().set(FS_DATACAMBIO, f.get()); // Copia a pagina 3 + } + return true; +} + +bool TPrimanota_application::cambio_handler(TMask_field& f, KEY key) +{ + if (key == K_TAB && f.to_check(key, true)) + { + TMask& m = f.mask(); + TPrimanota_application& a = app(); + + bool update = a.is_fattura(); // Devo ricopiarmi a pagina 3? + if (f.focusdirty()) + { + // Forza il ricalcolo del totale in lire se necessario + a.gioca_cambi(m.insert_mode() ? 0x1 : 0x0); + } + else + update &= !m.is_running(); + + if (update) + m.set(FS_CAMBIO, f.get()); // Copia a pagina 3 + } + return true; +} + +bool TPrimanota_application::reset_handler(TMask_field& f, KEY key) +{ + if (key == K_SPACE) + { + app().reset_pagamento(); + app().set_scadenze(f.mask()); + } + return true; +} + +bool TPrimanota_application::recalc_handler(TMask_field& f, KEY key) +{ + TPrimanota_application& a = app(); + if (key == K_SPACE && a.is_fattura()) + { + TMask& m = f.mask(); + TSheet_field& ps = m.sfield(FS_RATESHEET); + TPagamento& pag = a.pagamento(); + + const bool recalc_aut = f.get().not_empty(); + + if (!recalc_aut && pag.tipo_prima_rata() > 3) + { + // SE CAMBIANO I SIGNIFICATI DEL TIPO PRIMA RATA BISOGNA RIFARE TUTTO + pag.set_tipo_prima_rata(pag.tipo_prima_rata() - 3); + m.set(FS_TIPOPR, pag.desc_tpr()); + pag.set_tpr4(true); + } + else if (recalc_aut && pag.was_tpr4()) + { + // SE CAMBIANO I SIGNIFICATI DEL TIPO PRIMA RATA BISOGNA RIFARE TUTTO + pag.set_tpr4(FALSE); + bool yak = pag.tlit_rata(0) != pag.importo_da_non_dividere(FALSE); + if (pag.in_valuta()) yak |= (pag.tval_rata(0) != pag.importo_da_non_dividere(true)); + if (yak) + { + if (yesno_box(TR("L'importo della prima rata è stato modificato. " + "Con il ricalcolo automatico esso non sarà più" + " modificabile. Si desidera " + "riportare le rate alle condizioni iniziali?"))) + { + pag.set_tipo_prima_rata(pag.tipo_prima_rata() + 3); + m.set(FS_TIPOPR, pag.desc_tpr()); + a.reset_pagamento(); + a.set_scadenze(m); + } + else warning_box(FR("Il tipo prima rata è stato modificato in \"%s\" per " + "poter mantenere la rateazione scelta e la possibilità di " + "ricalcolo automatico"), + pag.desc_tpr()); + } + else + { + pag.set_tipo_prima_rata(pag.tipo_prima_rata() + 3); + m.set(FS_TIPOPR, pag.desc_tpr()); + } + } + + const bool on = pag.tipo_prima_rata() < 4 || !recalc_aut; + ps.enable_cell(0, 1, on); + ps.enable_cell(0, 2, on); + ps.enable_cell(0, 3, on); + + ps.enable_column(3, recalc_aut); + ps.sheet_mask().enable(DLG_DELREC, !recalc_aut); + } + + return true; +} + +bool TPrimanota_application::nrate_handler(TMask_field& f, KEY key) +{ + if (key == K_TAB && f.focusdirty()) + { + TPagamento& pag = app().pagamento(); + pag.set_numero_rate(atoi(f.get())); + if (pag.dirty()) + { + TMask& m = f.mask(); + TSheet_field& ps = m.sfield(FS_RATESHEET); + pag.set_sheet(ps); + app().set_banche(m); + app().pag_rows() = ps.rows_array(); + } + TString4 r; r.format("%d", pag.n_rate()); + f.set(r); + } + return true; +} + +bool TPrimanota_application::codcab_handler(TMask_field& f, KEY key) +{ + if (key == K_TAB && f.focusdirty()) + { + const TMask& m = f.mask(); + const short id = f.dlg() == FS_NSCAB ? FS_NSABI : FS_VSABI; + const int pos = id == FS_NSABI ? 7 : 9; // Posizione codici ABI sullo sheet + const TString16 abi = m.get(id); + const TString16 cab = m.get(id+1); + TSheet_field& ps = m.sfield(FS_RATESHEET); + for (int i = ps.items()-1; i >= 0; i--) // Setta la banca in tutte le righe + { + TToken_string& row = ps.row(i); + row.add(abi, pos); + row.add(cab, pos+1); + } + ps.force_update(); + } + + return true; +} + +bool TPrimanota_application::rataeur_handler(TMask_field& f, KEY key) +{ + TMask& m = app().curr_mask(); + if (key == K_F8 || (key == K_TAB && f.get().empty() && m.is_running())) + { + const TPagamento& pag = app().pagamento(); + if (pag.in_valuta()) + { + const TValuta v(m, FS_VALUTA, FS_DATACAMBIO, FS_CAMBIO); + const real other(f.mask().get(103)); + const char* s = v.val2eur(other).string(); + f.set(s); + } + } + return true; +} + +bool TPrimanota_application::rataval_handler(TMask_field& f, KEY key) +{ + if (key == K_F8 || (key == K_TAB && f.get().empty())) + { + TPagamento& pag = app().pagamento(); + if (pag.in_valuta()) + { + const TValuta v(app().curr_mask(), FS_VALUTA, FS_DATACAMBIO, FS_CAMBIO); + const real other(f.mask().get(102)); + TString80 s(v.eur2val(other).string()); + f.set(s); + } + } + return true; +} + +void TPrimanota_application::reset_pagamento() +{ + TPagamento& pag = pagamento(); + pag.read(); + pag.set_rate_auto(); +} + +void TPrimanota_application::recalc_scadenze(const TDate& d) +{ + TPagamento& pag = pagamento(); + pag.set_inizio(d); + // ridefinisci lo sheet sulla base delle nuove rate + TSheet_field& ps = pags(); + TString_array& rws = pag_rows(); + + pag.set_sheet(ps); + rws = ps.rows_array(); +} + +void TPrimanota_application::set_pagamento(const char* c, const char* d) +{ + if (_pag != NULL) + { + delete _pag; + _pag = NULL; + } + if (c != NULL || d != NULL) + { + if (!TDate::isdate(d)) + d = ""; + _pag = new TPagamento(c, d); + } + if (_pag != NULL && iva() == iva_vendite) + _pag->set_clifo(curr_mask().get_long(F_CLIENTE)); // Dato indispensabile per non accettazione effetti + +} + +void TPrimanota_application::set_totale_pagamento(bool update) +{ + const TMask& m = curr_mask(); + TPagamento& pag = pagamento(); + + const real imposta = m.id2pos(F_IMPOSTE) >= 0 ? m.get_real(F_IMPOSTE) : ZERO; + const real imponibile = m.get_real(F_TOTALE) - imposta; + const real spese = ZERO; + + const TValuta cambio(m, SK_VALUTA, SK_DATACAMBIO, SK_CAMBIO); + if (cambio.in_valuta()) + { + const real imposval = cambio.eur2val(imposta); + const real imponval = m.get_real(SK_TOTDOCVAL) - imposval; + pag.set_total_valuta(imponval, imposval, spese, cambio.cambio(), + imponibile, imposta, spese, cambio.codice()); + } + else + { + if (is_split_payment()) + pag.set_total(imponibile, ZERO, spese); + else + pag.set_total(imponibile, imposta, spese); + } + + if (update) + { + TSheet_field& ps = m.sfield(FS_RATESHEET); + pagamento().set_sheet(ps); // Aggiorna lo sheet + pag_rows() = ps.rows_array(); // copia sheet + } +} + +void TPrimanota_application::set_banche(TMask& m) +{ + // Aggiunge banche: CM000033 + TSheet_field& ps = m.sfield(FS_RATESHEET); + bool update = false; + for (int i = ps.items()-1; i >= 0; i--) + { + TToken_string& row = ps.row(i); + if (row.get_char(7) <= ' ') + { + row.add(m.get(FS_NSABI), 7); + row.add(m.get(FS_NSCAB), 8); + update = true; + } + if (row.get_char(9) <= ' ') + { + row.add(m.get(FS_VSABI), 9); + row.add(m.get(FS_VSCAB), 10); + update = true; + } + } + if (update) + ps.force_update(); +} + +void TPrimanota_application::set_scadenze(TMask& m) +{ + const TString& cp = m.get(F_CODPAG); + const TString& dt = m.get(F_DATADOC); + set_pagamento(cp, dt); + set_totale_pagamento(false); + + TPagamento& pag = pagamento(); + if (m.field(FS_RECALC).enabled()) + { + m.set(FS_RDIFFER, pag.rate_differenziate() ? "1" : "2"); + m.set(FS_NAMEPAG, pag.name()); + m.set(FS_TIPOPR, pag.desc_tpr()); + m.set(FS_MCOMM, pag.mese_commerciale() ? "X" : ""); + m.set(FS_RECALC, m.insert_mode() ? "X" : "", true); + m.set(FS_NRATE, pag.n_rate()); + } + pag.set_rate_auto(); + TSheet_field& ps = m.sfield(FS_RATESHEET); + pag.set_sheet(ps); // prepara lo sheet + set_banche(m); + _pag_rows = ps.rows_array(); // copia sheet +} + +bool TPrimanota_application::read_scadenze(TMask& m) +{ + const int anno = m.get_int(F_ANNORIF); + const TString& numpart = m.get(F_NUMRIF); + + if (anno <= 0 || numpart.blank()) // Partita vuota + return false; + + TBill clifo; cerca_conto_cf(clifo); + const TPartita& part = partite().partita(clifo, anno, numpart); + + const TRectype& testa = _rel->curr(); + const long nreg = testa.get_long(MOV_NUMREG); + const TDate datadoc = testa.get_date(MOV_DATADOC); + + const int npart = part.prima_fattura(nreg); + if (npart <= 0) // Non esiste una riga per questo movimento + { + partite().destroy(); + return false; // Non dovrebbe succedere mai, in quanto gia' beccato sopra! + } + + // Determino il codice pagamento dalla riga di fattura + TString4 codpag; + const TRiga_partite& partita = part.riga(npart); + if (partita.rate() > 0) + { + const TRiga_scadenze& primarata = partita.rata(1); + codpag = primarata.get(SCAD_CODPAG); + } + set_pagamento(codpag, datadoc.string()); + TPagamento& pag = pagamento(); + + const bool in_valuta = ::is_true_value(m.get(SK_VALUTA)); + const bool is_split = !in_valuta && clifo.tipo() == 'C' && anno >= 2015 && is_split_payment(); + + const real totale_cg = m.get_real(in_valuta ? SK_TOTDOCVAL : F_TOTALE); + real totale_sc = partita.importo(in_valuta).valore(); + if (is_split) + totale_sc += m.get_real(F_IMPOSTE); + + if (totale_cg != totale_sc) + { + const TString& codval = m.get(SK_VALUTA); + const TCurrency totdoc_c(totale_cg, codval); + const TCurrency totdoc_s(totale_sc, codval); + const char* i1 = totdoc_c.string(true); + const char* i2 = totdoc_s.string(true); + warning_box(FR("Il totale documento (%s) non corrisponde a quello del saldaconto (%s)."), i1, i2); + } + if (iva() != nessuna_iva && !is_split) + { + const real imposte_cg = m.get_real(F_IMPOSTE); + const real imposte_sc = partita.get_real(PART_IMPOSTA); + if (imposte_cg != imposte_sc) + { + const TCurrency imposte_c(imposte_cg); + const TCurrency imposte_s(imposte_sc); + const TString i1 = imposte_c.string(true); + const TString i2 = imposte_s.string(true); + warning_box(FR("Il totale delle imposte (%s) non corrisponde a quello del saldaconto (%s)."), + (const char*)i1, (const char*)i2); + } + } + + set_totale_pagamento(false); + + TSheet_field& ps = m.sfield(FS_RATESHEET); + + const int protette = partita.ultima_rata_con_abbuoni_diffcam(); + + pag.zap_rate(); // Azzera pagamento + for (int i = 1; i <= partita.rate(); i++) // E' necessario andare in avanti!!! + { + const TRiga_scadenze& scadenza = partita.rata(i); + real importo = scadenza.get_real(SCAD_IMPORTO); + const TDate scad = scadenza.get_date(SCAD_DATASCAD); + const int tipop = scadenza.get_int(SCAD_TIPOPAG); + const bool prot = i <= protette; // Non cancellabile + const TString16 ulc = scadenza.get(SCAD_ULTCLASS); + if (in_valuta) + { + const real impval(scadenza.get(SCAD_IMPORTOVAL)); + pag.set_rata(i-1, impval, importo, scad, tipop, ulc, prot); + } + else + pag.set_rata(i-1, ZERO, importo, scad, tipop, ulc, prot); + + TToken_string& str = ps.row(i-1); + str.add(scadenza.get(SCAD_CODABIPR), 7); + str.add(scadenza.get(SCAD_CODCABPR), 8); + str.add(scadenza.get(SCAD_CODABI), 9); + str.add(scadenza.get(SCAD_CODCAB), 10); + if (i == 1) + { + m.set(FS_NSABI, str.get(7)); + m.set(FS_NSCAB, str.get(8)); + m.set(FS_VSABI, str.get(9)); + m.set(FS_VSCAB, str.get(10)); + m.set(FS_AGENTE, scadenza.get(SCAD_CODAG)); + } + str.add(scadenza.get(SCAD_DESCR), 11); + bool bloc = scadenza.get_bool(SCAD_BLOCCATA); + const TString8 motivo = scadenza.get(SCAD_MOTIVO); + if (bloc || motivo.full()) + { + str.add("X", 12); + str.add(motivo, 13); + str.add(cache().get("%CBP", motivo, "S0"), 14); + } + str.add(prot ? "X" : " ", 15); + } + m.set(FS_RDIFFER, pag.rate_differenziate() ? "1" : "2"); + m.set(FS_NAMEPAG, pag.name()); + m.set(FS_TIPOPR, pag.desc_tpr()); + m.set(FS_MCOMM, pag.mese_commerciale() ? "X" : "", true); + m.set(FS_RECALC, m.insert_mode() ? "X" : "", true); + m.set(FS_NRATE, pag.n_rate()); + + pag.set_sheet(ps); + _pag_rows = ps.rows_array(); + + return true; +} + +int TPrimanota_application::cerca_conto_cf(TBill& bill) const +{ + TString_array& a = cgs().rows_array(); + if (iva() != nessuna_iva) + { + const char tipocf = clifo(); + const long codcf = curr_mask().get_long(tipocf == 'C' ? F_CLIENTE : F_FORNITORE); + for (int r = 0; r < a.items(); r++) + { + TToken_string& row = a.row(r); + bill.get(row, 2, 0x1); + if (bill.tipo() == tipocf && bill.codclifo() == codcf) + return r; + } + bill.set(0, 0, codcf, tipocf); + } + else + { + for (int r = 0; r < a.items(); r++) + { + TToken_string& row = a.row(r); + bill.get(row, 2, 0x1); + if (bill.tipo() > ' ') + return r; + } + if (a.items() > 0) + { + TToken_string& row = a.row(0); + bill.get(row, 2, 0x1); + return 0; + } + else + bill.set(0, 0, 0, ' '); + } + return -1; +} + +void TPrimanota_application::write_scadenze(const TMask& m) +{ + const long nreg = m.get_long(F_NUMREG); + const int anno = m.get_int(F_ANNORIF); + const TString numpart(m.get(F_NUMRIF)); + + TPartita* oldgame = partite().first(); + TPartita* newgame = NULL; + + int nuova_riga = 0; + + if (anno > 0 && !numpart.blank() && m.page_enabled(2) && !m.get_real(F_TOTALE).is_zero()) + { + const int tmov = causale().tipomov(); + const TString& desc = m.get(F_DESCR); + const TString& codpag = m.get(F_CODPAG); + const TString8 codcaus = causale().codice(); + const TValuta cambio(m, SK_VALUTA, SK_DATACAMBIO, SK_CAMBIO); + const TString& agente = m.get(FS_AGENTE); + const char sezione = causale().sezione(1); //get_cgs_imp(0).sezione(); // Dare/Avere + + TBill clifo; + const int riga_clifo = cerca_conto_cf(clifo); + CHECK(riga_clifo >= 0, "Impossibile generare la fattura senza gruppo/conto cliente"); + newgame = new TPartita(clifo, anno, numpart); + + const int row = newgame->prima_fattura(nreg); // Riga fattura di questo movimento + TRiga_partite& partita = row <= 0 ? newgame->new_row() : newgame->riga(row); + nuova_riga = partita.get_int(PART_NRIGA); + + TPagamento& pag = pagamento(); + + // put data on partita + partita.put(PART_TIPOMOV, tmov); + partita.put(PART_NREG, nreg); + partita.put(PART_NUMRIG, riga_clifo+1); + partita.put(PART_DATAREG, m.get(F_DATAREG)); + partita.put(PART_DATADOC, m.get(F_DATADOC)); + partita.put(PART_NUMDOC, m.get(F_NUMDOC)); + partita.put(PART_DESCR, desc); + partita.put(PART_CODCAUS, codcaus); + partita.put(PART_REG, causale().reg().name()); + partita.put(PART_SEZ, sezione); + + if (clifo.gruppo() != partita.get_int(PART_GRUPPOCL) || + clifo.conto() != partita.get_int(PART_CONTOCL)) + { + partita.put(PART_GRUPPOCL, clifo.gruppo()); + partita.put(PART_CONTOCL, clifo.conto()); + } + + if (iva() != nessuna_iva) + partita.put(PART_PROTIVA, m.get_long(F_PROTIVA)); + + TImporto totdoc = get_cgs_imp(riga_clifo); + totdoc.normalize(sezione); + partita.put(PART_IMPTOTDOC, totdoc.valore()); + + cambio.put(partita); + const bool in_valuta = cambio.in_valuta(); + + if (in_valuta) + partita.put(PART_IMPTOTVAL, m.get(SK_TOTDOCVAL)); + + real imponibile, imponibile_val; + int i; + + for (i = pag.n_rate()-1; i >= 0; i--) + { + if (in_valuta) + imponibile_val += pag.tval_rata(i); + imponibile += pag.tlit_rata(i); + } + + partita.put(PART_IMPORTO, imponibile); + partita.put(PART_IMPORTOVAL, imponibile_val); + partita.put(PART_SPESE, pag.spese(in_valuta)); + if (iva() != nessuna_iva) + partita.put(PART_IMPOSTA, pag.imposta(in_valuta)); + else + partita.zero(PART_IMPOSTA); + + TSheet_field& ps = m.sfield(FS_RATESHEET); + const int new_rate = ps.items(); + const int nrate = partita.rate(); + TArray ant; + + for (int i = 0; i < new_rate; i++) + { + real* r = new real; + ant.add(r); + if (i < nrate) + *r = partita.rata(i + 1).get_real(SCAD_IMPORTOANT); + } + for (int i = new_rate; i < nrate; i++) + ((real &)ant[new_rate - 1]) += partita.rata(i + 1).get_real(SCAD_IMPORTOANT); + + partita.elimina_rata(-1); // Elimina tutte le rate eventuali + for (i = 0; i < ps.items(); i++) + { + TToken_string& row = ps.row(i); + TRiga_scadenze& scadenza = partita.new_row(); + + scadenza.put(SCAD_CODPAG, codpag); // Codice pagamento + scadenza.put(SCAD_CODAG, agente); // Codice agente + + scadenza.put(SCAD_DATASCAD, row.get(0)); // 0 = Data scadenza + scadenza.put(SCAD_IMPORTO, row.get()); // 1 = Importo + scadenza.put(SCAD_IMPORTOVAL, row.get()); // 2 = Importo in valuta + row.get(); // 3 = Percentuale + scadenza.put(SCAD_TIPOPAG, row.get()); // 4 = Tipo pagamento + scadenza.put(SCAD_ULTCLASS, row.get()); // 5 = Ulteriore classificazione + row.get(); // 6 = Descrizione pagamento + scadenza.put(SCAD_CODABIPR, row.get()); // 7 = Ns ABI + scadenza.put(SCAD_CODCABPR, row.get()); // 8 = Ns CAB + scadenza.put(SCAD_CODABI, row.get()); // 9 = Vs ABI + scadenza.put(SCAD_CODCAB, row.get()); //10 = Vs CAB + scadenza.put(SCAD_DESCR, row.get()); //11 = Note + scadenza.put(SCAD_BLOCCATA, row.get()); //12 = Non pagabile + scadenza.put(SCAD_MOTIVO, row.get()); //13 = Motivo blocco + scadenza.put(SCAD_IMPORTOANT, ((real &)ant[i])); + } + } + + if (oldgame != NULL) + { + const int rigafatt = oldgame->prima_fattura(nreg); + if (rigafatt > 0) + { + if (newgame == NULL || *oldgame != *newgame) + { + bool sposta = newgame != NULL; + if (sposta) + { + sposta = (oldgame->conto().sottoconto() == newgame->conto().sottoconto()); + if (sposta) + sposta = yesno_box(FR("Si desidera spostare la fattura e gli eventuali\n" + "pagamenti relativi nella partita %d %s?"), anno, (const char*)numpart); + } + if (sposta) + { + oldgame->sposta_riga(rigafatt, *newgame, nuova_riga); + } + else + { + oldgame->scollega_pagamenti(rigafatt); + oldgame->rimuovi_riga(rigafatt); + } + oldgame->rewrite(); + } + else + { + oldgame->sposta_riga(rigafatt, *newgame, nuova_riga); + } + } + } + + if (newgame != NULL) // Se non ho cancellato il numero partita ... + { + newgame->write(); // Salva nuova partita + delete newgame; + } +} + + +/////////////////////////////////////////////////////////// +// Gestione pagamenti +/////////////////////////////////////////////////////////// + +bool TPrimanota_application::showpartite_handler(TMask_field& f, KEY k) +{ + bool ok = true; + TMask& m = f.mask(); + TPrimanota_application& a = app(); + TMask& cm = a.curr_mask(); + + const char tipo = m.get(CG_ROWTYPE)[0]; + const bool is_nota = tipo == 'T' && a.causale().tipomov() == 2 && cm.field(F_NUMRIF).active(); + const bool tras = a._as400 && a.causale().tipomov() > 1 && m.get(CG_TIPO).not_empty(); + + if (tipo == 'K' || is_nota || tras) + { + TSheet_field& s = *m.get_sheet(); + const int riga = s.selected(); + + if (k == K_SPACE) + { + const bool ok = a.edit_partite(m, riga); + if (ok && riga < s.items()) + k = K_ENTER; + } + + if (k == K_ENTER) + { + const long curreg = cm.insert_mode() ? NUMREG_PROVVISORIO : cm.get_long(F_NUMREG); + const TImporto importo(a.get_cgs_imp(riga)); + const TImporto speso(a.partite().importo_speso(curreg, riga+1)); + + if (tipo == 'K' && !speso.is_zero()) + { + a.disable_cgs_cells(riga, 'K'); + a.cgs().force_update(riga); + } + + if (importo != speso) + { + TString msg(128); + const TCurrency curr(speso.valore()); + msg.format(FR("L'importo sul saldaconto della riga %d è %s"), (riga+1), (const char*)curr.string(true)); + if (!speso.is_zero()) + msg << (speso.sezione() == 'A' ? TR(" Avere") : TR(" Dare")); + return f.error_box(msg); + } + f.set_focus(); + } + } + return ok; +} + + +// deleting significato +// -1 sto cancellando tutto +// 0 non sto cancellando nulla +// 1 sto cancellando la prima riga contabile +// n sto cancellando la ennesima riga contabile + +bool TPrimanota_application::notify_edit_pagamento(TPartita& p, TRectype& new_pag, + const TValuta& valuta, int deleting) +{ + const int nriga = new_pag.get_int(PAGSCA_NRIGA); // Riga fattura + const int nrata = new_pag.get_int(PAGSCA_NRATA); // Numero rata + const int nrigp = new_pag.get_int(PAGSCA_NRIGP); // Riga pagamento + const TRectype& old_pag = p.pagamento(nriga, nrata, nrigp); + const TRiga_partite& somma = p.riga(nrigp); + const int riga_contabile = somma.get_int(PART_NUMRIG); // Riga movimento + TSheet_field& sheet = cgs(); + TBill new_conto; new_conto.get(new_pag, true); + + if (_as400 || !new_conto.ok()) + deleting = -1; // Ignora contropartite + + if (deleting >= 0) + { + const char sez = somma.sezione(); // Sezione importo e ritenute + const char controsez = sez == 'A' ? 'D' : 'A'; // Sezione contropartita + + // Aggiornamento contopartita + const TImporto old_importo(controsez, old_pag.get_real(PAGSCA_IMPORTO)); + TBill old_conto; old_conto.get(old_pag, true); + + const TImporto new_importo(controsez, new_pag.get_real(PAGSCA_IMPORTO)); + + if (old_importo != new_importo || old_conto != new_conto) + { + int old_riga = _easy_sal ? type2pos('I') : bill2pos(old_conto, 'I'); + if (old_riga >= 0) + { + const bool empty = sub_cgs_imp(old_riga, old_importo); + if (empty && (new_importo.is_zero() || new_conto != old_conto)) + sheet.destroy(old_riga); + } + else + if (!old_importo.is_zero()) + { + TImporto imp = old_importo; imp.swap_section(); + set_cgs_row(old_riga, imp, old_conto, "", 'I'); + } + + // Importo della contropartita + if (!new_importo.is_zero() && new_conto.ok()) + { + int new_riga = _easy_sal ? type2pos('I') : bill2pos(new_conto, 'I'); + if (new_riga < 0) + { + const int tp = somma.get_int(PART_TIPOPAG); + const int da = p.tipopag2causale(tp); + const TString descagg(causale().desc_agg(da)); + new_riga = set_cgs_row(new_riga, new_importo, new_conto, descagg, 'I'); + + TToken_string& cli_row = sheet.row(riga_contabile-1); + TBill contro; contro.get(cli_row, 9, 0x3); + if (!contro.ok()) // Completa controparita della riga cliente se necessario + new_conto.add_to(cli_row, 9, 0x3); + TString80 descr = cli_row.get(CG_DESCR-FIRST_FIELD); + if (descr.blank()) // Compila descrizione della riga cliente se necessario + { + descr = somma.get(PART_DESCR); + cli_row.add(descr, CG_DESCR-FIRST_FIELD); + } + + // Compila contropartita della nuova riga + TBill clifo; clifo.get(cli_row, 2, 0x3); + TToken_string& new_row = sheet.row(new_riga); + clifo.add_to(new_row, 9, 0x3); + } + else + { + add_cgs_imp(new_riga, new_importo); // Incrementa importo sulla banca + if (_easy_sal) + { + // Aggiorna conto della riga della banca + TToken_string& new_row = sheet.row(new_riga); + new_conto.add_to(new_row, 2, 0x3); + // Aggiorna contropartita delle righe cliente + FOR_EACH_SHEET_ROW(sheet, r, cli_row) if (row_type(*cli_row) == 'K') + new_conto.add_to(*cli_row, 9, 0x3); + } + } + } + } + + // Aggiornamento ritenute fiscali + const real old_ritenute(old_pag.get(PAGSCA_RITENUTE)); + const real new_ritenute(new_pag.get(PAGSCA_RITENUTE)); + if (old_ritenute != new_ritenute) + { + const TImporto grow_ritenute(controsez, new_ritenute-old_ritenute); + const int riga = type2pos('F'); + if (riga < 0) + { + TBill conto_rit; causale().bill(RIGA_PAG_RITFIS, conto_rit); + const TString desc(causale().desc_agg(RIGA_PAG_RITFIS)); + set_cgs_row(riga, grow_ritenute, conto_rit, desc, 'F'); + } + else + { + const bool empty = add_cgs_imp(riga, grow_ritenute); + if (empty) sheet.destroy(riga); + } + } + + // Aggiornamento ritenute sociali + const real old_ritsoc(old_pag.get(PAGSCA_RITSOC)); + const real new_ritsoc(new_pag.get(PAGSCA_RITSOC)); + if (old_ritsoc != new_ritsoc) + { + const TImporto grow_ritenute(causale().sezione_ritsoc(), new_ritsoc-old_ritsoc); + const int riga = type2pos('S'); + if (riga < 0) + { + TBill conto_rit; causale().bill(RIGA_PAG_RITSOC, conto_rit); + const TString desc(causale().desc_agg(RIGA_PAG_RITSOC)); + set_cgs_row(riga, grow_ritenute, conto_rit, desc, 'S'); + } + else + { + const bool empty = add_cgs_imp(riga, grow_ritenute); + if (empty) sheet.destroy(riga); + } + } + } // if (deleting >= 0) + + char old_ap, new_ap; + TImporto old_abbuono, new_abbuono, old_diffcam, new_diffcam; + const bool empty = p.modifica_pagamento(new_pag, valuta, + old_ap, old_abbuono, old_diffcam, + new_ap, new_abbuono, new_diffcam, true); + + if (deleting >= 0) + { + // Se c'e' differenza negli abbuoni + if (old_abbuono != new_abbuono || old_ap != new_ap) + { + if (old_ap != ' ') // Se c'era un abbuono ... + { + const int riga_abb = type2pos(old_ap); + CHECK(riga_abb >= 0, "Chiss'è fregato gli abbuoni?"); + const bool empty = add_cgs_imp(riga_abb, old_abbuono); + if (empty && new_ap != old_ap) + sheet.destroy(riga_abb); + + if (deleting != riga_contabile) + { + // Sottrae l'abbuono con la sezione invertita dalla riga contabile + sub_cgs_imp(riga_contabile-1, old_abbuono); + } + } + if (new_ap != ' ') // Se ci sono abbuoni + { + const int riga_abb = type2pos(new_ap); + + new_abbuono.swap_section(); + new_abbuono.normalize(); + + if (riga_abb < 0) + { + const int rc = new_ap == 'A' ? RIGA_ABBUONI_ATTIVI : RIGA_ABBUONI_PASSIVI; + TBill conto_abb; causale().bill(rc, conto_abb); + const TString desc(causale().desc_agg(rc)); + app().set_cgs_row(riga_abb, new_abbuono, conto_abb, desc, new_ap); + } + else + add_cgs_imp(riga_abb, new_abbuono); + + if (deleting != riga_contabile) + { + // Aggiunge l'abbuono con la sezione invertita + sub_cgs_imp(riga_contabile-1, new_abbuono); + } + } + } + + // Se c'e' variazione nella differenza cambi + if (old_diffcam != new_diffcam) + { + const int riga_diffcam = type2pos('C'); + TImporto grow_diffcam(old_diffcam); + grow_diffcam -= new_diffcam; + grow_diffcam.normalize(); + + if (riga_diffcam < 0) + { + TBill conto_diffcam; causale().bill(RIGA_DIFFCAM, conto_diffcam); + const TString desc(causale().desc_agg(RIGA_DIFFCAM)); + set_cgs_row(riga_diffcam, grow_diffcam, conto_diffcam, desc, 'C'); + } + else + { + const bool empty = add_cgs_imp(riga_diffcam, grow_diffcam); + if (empty) + sheet.destroy(riga_diffcam); + } + + if (deleting != riga_contabile) + { + // Aggiunge la differenza con la sezione invertita + sub_cgs_imp(riga_contabile-1, grow_diffcam); + } + } + } // if (deleting >= 0) + + return empty; +} + +// Scorre tutte le righe della partita ed azzera i pagamenti relativi a alla riga contabile +// cancellata e decrementa i numeri riga superiori a numrig +bool TPrimanota_application::notify_cgline_deletion(TPartita& partita, long nreg, int numrig) +{ + bool found = FALSE; + int p; + + // Al primo "giro" elimino i pagamenti dalla rate + for (p = partita.last(); p > 0; p = partita.pred(p)) + { + TRiga_partite& part = partita.riga(p); + if (part.is_fattura()) + { + for (int r = part.rate(); r > 0; r--) + { + const TRiga_scadenze& scad = part.rata(r); + for (int s = scad.last(); s > 0; s = scad.pred(s)) + { + const TRiga_partite& sum = partita.riga(s); + if (sum.get_long(PART_NREG) == nreg && + (numrig <= 0 || sum.get_int(PART_NUMRIG) == numrig)) + { + TRectype nul_pag(scad.row(s)); + nul_pag.zero(PAGSCA_IMPORTO); + nul_pag.zero(PAGSCA_IMPORTOVAL); + nul_pag.zero(PAGSCA_RITENUTE); + nul_pag.zero(PAGSCA_RITSOC); + nul_pag.put(PAGSCA_ACCSAL, 'A'); + const TValuta val; // Non cambiare valuta + notify_edit_pagamento(partita, nul_pag, val, numrig); + found = true; + } + } + } + } + } + + // Alla fine del "giro" elimino i pagamenti non assegnati + TRecord_array& unas = partita.unassigned(); + for (int u = unas.last_row(); u > 0; u = unas.pred_row(u)) + { + const TRiga_partite& sum = partita.riga(u); + if (sum.get_long(PART_NREG) == nreg && + (numrig <= 0 || sum.get_int(PART_NUMRIG) == numrig)) + { + TRectype nul_pag(unas.row(u, FALSE)); + nul_pag.zero(PAGSCA_IMPORTO); + nul_pag.zero(PAGSCA_IMPORTOVAL); + nul_pag.zero(PAGSCA_RITENUTE); + nul_pag.zero(PAGSCA_RITSOC); + nul_pag.put(PAGSCA_ACCSAL, 'A'); // Inutile, ma l'ho copiato da sopra + const TValuta val; // Non cambiare valuta + notify_edit_pagamento(partita, nul_pag, val, numrig); + found = true; + } + } + + // Al secondo "giro" decremento NRIGP dalle righe di pagamento + if (numrig > 0) + { + for (p = partita.last(); p > 0; p = partita.pred(p)) + { + TRiga_partite& part = partita.riga(p); + if (!part.is_fattura()) + { + if (part.get_long(PART_NREG) == nreg) + { + const int nr = part.get_int(PART_NUMRIG); + if (nr > numrig) + part.put(PART_NUMRIG, nr-1); + } + } + } + } + + return found; +} + +bool TPrimanota_application::notify_cgline_deletion(int numrig) +{ + bool found = FALSE; + + const TMask& m = curr_mask(); + const long nreg = m.insert_mode() ? NUMREG_PROVVISORIO : m.get_long(F_NUMREG); + +// if (curr_mask().edit_mode()) +// partite().add_numreg(nreg); // Gia' fatto dalla read + + for (TPartita* game = partite().first(); game; game = partite().next()) + found |= notify_cgline_deletion(*game, nreg, numrig); + + return found; +}