diff --git a/cg/cg2104.cpp b/cg/cg2104.cpp index f140e1fc6..e8ac187ae 100755 --- a/cg/cg2104.cpp +++ b/cg/cg2104.cpp @@ -90,10 +90,14 @@ bool TPrimanota_application::pag_notify(TSheet_field& ps, int r, KEY k) if (doit) { const int prev = r - 1; - const int nrate = msk->get_int(FS_NRATE) + 1; + const int nrate = msk->get_int(FS_NRATE) + 1; + const int gio_scad = pag.scad_rata(prev); + TDate data_scad(pag.data_rata(prev)); msk->set(FS_NRATE, nrate); - pag.add_rata(ZERO, 0, pag.tipo_rata(prev), pag.ulc_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(); @@ -383,20 +387,20 @@ bool TPrimanota_application::recalc_handler(TMask_field& f, KEY key) { if (key == K_SPACE) { - const bool on = f.get().not_empty(); + const bool recalc_aut = f.get().not_empty(); const TMask& m = f.mask(); TSheet_field& ps = (TSheet_field&)m.field(FS_RATESHEET); - ps.enable_column(3, on); - ps.sheet_mask().enable(DLG_DELREC, !on); + ps.enable_column(3, recalc_aut); + ps.sheet_mask().enable(DLG_DELREC, !recalc_aut); TPagamento& pag = app().pagamento(); if (pag.tipo_prima_rata() >= 4) { - ps.enable_cell(0, 1, !on); - ps.enable_cell(0, 2, !on); - if (on) + ps.enable_cell(0, 1, !recalc_aut); + ps.enable_cell(0, 2, !recalc_aut); + if (recalc_aut) ps.disable_cell(0, 3); - ps.force_update(0); } + ps.force_update(); } return TRUE; @@ -443,7 +447,7 @@ bool TPrimanota_application::codcab_handler(TMask_field& f, KEY key) bool TPrimanota_application::ratalit_handler(TMask_field& f, KEY key) { - if (key == K_F8 || (key == K_TAB && f.focusdirty() && f.get().empty())) + if (key == K_F8 || (key == K_TAB && f.get().empty())) { TPagamento& pag = app().pagamento(); if (pag.in_valuta()) @@ -459,7 +463,7 @@ bool TPrimanota_application::ratalit_handler(TMask_field& f, KEY key) bool TPrimanota_application::rataval_handler(TMask_field& f, KEY key) { - if (key == K_F8 || (key == K_TAB && f.focusdirty() && f.get().empty())) + if (key == K_F8 || (key == K_TAB && f.get().empty())) { TPagamento& pag = app().pagamento(); if (pag.in_valuta()) diff --git a/cg/pagament.cpp b/cg/pagament.cpp index e0536e488..6efcad489 100755 --- a/cg/pagament.cpp +++ b/cg/pagament.cpp @@ -201,7 +201,9 @@ void TPagamento::set_numero_rate(int n, int sscad, int rdiff) div.round(_round); real perc = (100.0 * div) / tot; real rem(tot); + real p(perc); + p.round(2); for (i = first; i < n; i++) { if (i > first) @@ -210,19 +212,30 @@ void TPagamento::set_numero_rate(int n, int sscad, int rdiff) { set_imprata (i, div); rem -= tpay_rata(i); - } + } + else + rem -= p; set_percrata(i, perc); } if (_inited && _tpr > 0 && _tpr < 4) set_imprata(0, tpay_rata(0) + _secndr); - if (_inited && rem != ZERO) + if (rem != ZERO) { - real r(tpay_rata(first) + rem); - set_imprata(first, r); - if (_inited && _tpr > 0 && _tpr < 4) - r -= _secndr; - set_percrata(first, 100 * r / tot); + if (_inited) + { + real r(tpay_rata(first) + rem); + set_imprata(first, r); + if (_inited && _tpr > 0 && _tpr < 4) + r -= _secndr; + set_percrata(first, 100 * r / tot); + } + else + { + const real r(perc_rata(first) + rem); + + set_percrata(first, r); + } } } else if (nr > first + 1) @@ -241,7 +254,9 @@ void TPagamento::set_numero_rate(int n, int sscad, int rdiff) real perc = (100.0 * div)/tot; real rem(rest); - + real p(perc); + + p.round(2); for (i = 1; i < n; i++) { if (i >= nr) @@ -252,21 +267,32 @@ void TPagamento::set_numero_rate(int n, int sscad, int rdiff) set_imprata (i, div); rem -= tpay_rata(i); } + else + rem -= p; set_percrata(i, perc); } - if (_inited && rem != ZERO) + if (rem != ZERO) { - real r(tpay_rata(first) + rem); - set_imprata(first, r); - if (_inited && _tpr > 0 && _tpr < 4) - r -= _secndr; - set_percrata(first, 100 * r / tot); + if (_inited) + { + real r(tpay_rata(first) + rem); + set_imprata(first, r); + if (_inited && _tpr > 0 && _tpr < 4) + r -= _secndr; + set_percrata(first, 100 * r / tot); + } + else + { + const real r(perc_rata(first) + rem); + + set_percrata(first, r); + } } } } } else - { + { for (i = first; sum < real(100.0); i++) { if ((real(100.0) - sum) < p) @@ -472,6 +498,12 @@ void TPagamento::set_percrata(int i, real r) tt.add(r.string(), 1); } +void TPagamento::set_datarata(int i, const TDate & d) +{ + TToken_string& tt = (TToken_string&)_rate[i]; + tt.add((const char *) d, 3); +} + real TPagamento::recalc_percrata(int i) { real hm(_tpr < 4 ? _firstr : _secndr); @@ -676,26 +708,27 @@ word TPagamento::recalc_rate(int row, bool is_perc_modified, if (_rate.items() == 0) return P_OK; real rsum(0.0), newv(0.0), rmax(0.0); - int oldtype = tipo_rata(0); - TString oldulc = ulc_rata(0); - int oldscad = scad_rata(0); + const int last_rata = _rate.items() - 1; + int oldtype = tipo_rata(last_rata); + TString oldulc = ulc_rata(last_rata); + int oldscad = scad_rata(last_rata); TDate lastdate = data_rata(0); int first = _tpr < 4 ? 0 : 1; TString_array srate(_rate); // rate come erano - if (srate.items() > 1) - { +// if (srate.items() > 1) +// { // calcola defaults per tipo pagamento e scadenza // nel caso di rate nuove - oldscad = scad_rata(srate.items() - 1); - if (_mcomm) - { - int mesi = oldscad / 30; - if (mesi == 0) mesi = 1; - oldscad = 30 * mesi; - } +// oldscad = scad_rata(srate.items() - 1); +// if (_mcomm) +// { +// int mesi = oldscad / 30; +// if (mesi == 0) mesi = 1; +// oldscad = 30 * mesi; +// } - } +// } if (oldscad <= 0) oldscad = _int_rate; if (oldtype <= 0) oldtype = _def_tpr; @@ -846,11 +879,15 @@ word TPagamento::recalc_rate(int row, bool is_perc_modified, if (rdiff == 1) { // cancella tutte le rate successive, aggiungi un'unica rata - // con il resto dell'importo + // con il resto dell'importo + real imp(remainder); + + if (_inited && _tpr >= 4 ) + imp += _firstr; if (row < (srate.items()-1)) { TToken_string& trt = rata(row+1); - trt.add(remainder.string(), is_perc_modified ? 1 : 4); + trt.add(imp.string(), is_perc_modified ? 1 : 4); for(int j = row+2; j < srate.items(); j++) _rate.destroy(j); } @@ -860,9 +897,9 @@ word TPagamento::recalc_rate(int row, bool is_perc_modified, for(int j = row+1; j < srate.items(); j++) _rate.destroy(j); - TToken_string& trt = add_rata(is_perc_modified ? remainder : (real) 0.0, - oldscad, oldtype); - if (!is_perc_modified) trt.add(remainder.string(),4); + TToken_string& trt = add_rata(is_perc_modified ? imp : (real) 0.0, + oldscad, oldtype, oldulc); + if (!is_perc_modified) trt.add(imp.string(),4); if (_inited) { TDate dd = data_rata(row); @@ -952,8 +989,8 @@ word TPagamento::recalc_rate(int row, bool is_perc_modified, for (int k = first; k < nr; k++) { nscd = oldscad; - type = _def_tpr; - TString16 ulc(_def_ulc); + type = oldtype; + TString16 ulc(oldulc); if (srate.items() > k) { @@ -982,12 +1019,14 @@ word TPagamento::recalc_rate(int row, bool is_perc_modified, { // se c'e' la vecchia rata si tengono i parametri // altrimenti si calcolano + TString16 ulc(oldulc); if (j < srate.items()) { TToken_string& trt = (TToken_string&)srate[j]; if (_inited) dd = trt.get(3); type = atoi(trt.get(2)); nscd = atoi(trt.get(0)); + ulc = trt.get(5); if (type == 0) type = 1; if (j > 0 && nscd == 0) nscd = oldscad; if (_inited && dd == lastdate && j > frs) @@ -1003,7 +1042,7 @@ word TPagamento::recalc_rate(int row, bool is_perc_modified, real imp(newv); TToken_string& ttr = set_rata(j, is_perc_modified ? imp : ZERO, - nscd, type); + nscd, type, ulc); if (_inited) ttr.add(dd.string(), 3); if (!is_perc_modified) @@ -1044,13 +1083,15 @@ word TPagamento::recalc_rate(int row, bool is_perc_modified, basta = TRUE; } // se c'e' la vecchia rata si tengono i parametri - // altrimenti si calcolano + // altrimenti si calcolano + TString16 ulc(oldulc); if (j < srate.items()) { TToken_string& trt = (TToken_string&)srate[j]; if (_inited) dd = trt.get(3); type = atoi(trt.get(2)); nscd = atoi(trt.get(0)); + ulc = trt.get(5); if (type == 0) type = 1; if (j > 0 && nscd == 0) nscd = oldscad; if (_inited && dd == lastdate && j > 0) @@ -1060,7 +1101,7 @@ word TPagamento::recalc_rate(int row, bool is_perc_modified, next_scad(dd,nscd,mcomm,j); TToken_string& ttr = set_rata(j, is_perc_modified ? newv : ZERO, - nscd, type); + nscd, type, ulc); if (_inited) ttr.add(dd.string(), 3); if (!is_perc_modified) @@ -1166,8 +1207,8 @@ word TPagamento::recalc_rate(int row, bool is_perc_modified, rsum -= _secndr; } lastdate = data_rata(i); - oldtype = tipo_rata(i); - if (scad_rata(i) > 0) oldscad = scad_rata(i); +// oldtype = tipo_rata(i); +// if (scad_rata(i) > 0) oldscad = scad_rata(i); if (_inited && i > 0) { if (data_rata(i) <= data_rata(i-1)) diff --git a/cg/pagament.h b/cg/pagament.h index 8c14399c1..846eabf5a 100755 --- a/cg/pagament.h +++ b/cg/pagament.h @@ -103,6 +103,7 @@ public: void set_rate_differenziate(int v); void set_tipo_prima_rata(int v, int sscad = -1); void set_percrata(int n, real r); + void set_datarata(int n, const TDate & d); real recalc_percrata(int i); void set_numero_rate(int n, int sscad = -1, int rdiff = 1);