From b404553548c06d5fb7b75463ffd6455df5129071 Mon Sep 17 00:00:00 2001
From: villa <villa@c028cbd2-c16b-5b4b-a496-9718f37d4682>
Date: Mon, 8 Jan 1996 11:32:33 +0000
Subject: [PATCH] Last mods -- should work now

git-svn-id: svn://10.65.10.50/trunk@2402 c028cbd2-c16b-5b4b-a496-9718f37d4682
---
 cg/pagament.cpp | 87 ++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 76 insertions(+), 11 deletions(-)

diff --git a/cg/pagament.cpp b/cg/pagament.cpp
index 8d8064ce3..481fe3630 100755
--- a/cg/pagament.cpp
+++ b/cg/pagament.cpp
@@ -9,8 +9,7 @@
 
 // se settato, si usa per rate nuove l'intervallo rate default (letto da
 // tab. pagamenti)
-#define USE_DEFAULT_INT_RATE
-
+#define USE_DEFAULT_INT_RATE  1
 
 inline void swap(int& x, int& y) {int tmp = x; x = y; y = tmp; }
 
@@ -958,7 +957,7 @@ word TPagamento::change_value_differenziate(int row, real user_val, bool is_perc
   if (row == 0 && _tpr > 0 && _tpr < 4 && !is_perc) 
     to_subtract = _secndr; 
   real user_div    = user_val - to_subtract;
-  int first        = _tpr > 3 ? 1 : 0;
+  const int first        = _tpr > 3 ? 1 : 0;
   _rdiff = TRUE;
   
   // togli rate gia' presenti
@@ -971,8 +970,8 @@ word TPagamento::change_value_differenziate(int row, real user_val, bool is_perc
 
   real remainder   = to_share - user_div;
   
-  int tok_ind = is_perc ? 1 : 4;
-  rata(row).add(user_div.string(), tok_ind);
+  const int tok_ind = is_perc ? 1 : 4;
+  rata(row).add(user_val.string(), tok_ind);
   
   if (!remainder.is_zero()) 
   {
@@ -995,8 +994,8 @@ word TPagamento::change_value_uguali(int row, real user_val, bool is_perc)
     to_subtract = _secndr; 
   real user_div    = user_val - to_subtract;
   real remainder   = to_share - user_div;
-  int tok_ind      = is_perc ? 1 : 4;
-  int first        = _tpr > 3 ? 1 : 0;
+  const int tok_ind      = is_perc ? 1 : 4;
+  const int first        = _tpr > 3 ? 1 : 0;
   _rdiff = FALSE;
   
   if (user_div.is_zero() || user_div.sign() == -1)
@@ -1080,21 +1079,87 @@ word TPagamento::change_value_uguali(int row, real user_val, bool is_perc)
        real tp = is_perc ? perc_rata(first) : tpay_rata(first);
        tp += delta;
        rata(first).add(tp.string(), tok_ind);
-     } 
+     }      
   }
 
   return 0;
 }                        
 
-word TPagamento::change_value_uguali_prossima(int rata, real user_val, bool is_perc)
+word TPagamento::change_value_uguali_prossima(int row, real user_val, bool is_perc)
 {                                                   
-  _rdiff = TRUE;
+  _rdiff = TRUE;           
+  // uguali finche' possibile da row in poi; residuo su first 
+  // importi totali, da non suddividere, da suddividere
+  real to_share = is_perc ? real(100.0) : (_tpr < 4 ? _firstr : _secndr);
+  real to_subtract = ZERO;
+  if (row == 0 && _tpr > 0 && _tpr < 4 && !is_perc) 
+    to_subtract = _secndr; 
+  real user_div    = user_val - to_subtract;
+  const int tok_ind      = is_perc ? 1 : 4;
+  const int first        = _tpr > 3 ? 1 : 0;
+  
+  // togli rate gia' presenti
+  for (int i = first; i < row; i++)
+  {
+    to_share -= (is_perc ? perc_rata(i): tpay_rata(i));
+    if (i == 0 && !is_perc && _tpr > 0 && _tpr < 4)
+      to_share += _secndr;
+  }
+
+  real div   = to_share/user_div;
+  if (div < real(1.0)) return P_TROP;
+  
+  real delta = to_share - (user_div * div.integer());
+  for (i = row; i < (row + div.integer()); i++)
+  {
+     if (n_rate() <= i) add_rata();
+     rata(i).add(user_div.string(), tok_ind);
+  }
+  remove_rate_from(i);
+  if (!delta.is_zero()) 
+  { 
+    real r = is_perc ? perc_rata(first) : tpay_rata(first);
+    r += delta;
+    rata(first).add(r.string(), tok_ind);
+  }   
   return 0;
 }  
 
-word TPagamento::change_value_uguali_possible(int rata, real user_val, bool is_perc)
+word TPagamento::change_value_uguali_possible(int row, real user_val, bool is_perc)
 {                                                   
   _rdiff = TRUE;
+  // uguali finche' possibile da row in poi; residuo come rata a parte 
+  real to_share = is_perc ? real(100.0) : (_tpr < 4 ? _firstr : _secndr);
+  real to_subtract = ZERO;
+  if (row == 0 && _tpr > 0 && _tpr < 4 && !is_perc) 
+    to_subtract = _secndr; 
+  real user_div    = user_val - to_subtract;
+  const int tok_ind      = is_perc ? 1 : 4;
+  const int first        = _tpr > 3 ? 1 : 0;
+  
+  // togli rate gia' presenti
+  for (int i = first; i < row; i++)
+  {
+    to_share -= (is_perc ? perc_rata(i): tpay_rata(i));
+    if (i == 0 && !is_perc && _tpr > 0 && _tpr < 4)
+      to_share += _secndr;
+  }
+
+  real div   = to_share/user_div;
+  if (div < real(1.0)) return P_TROP;
+  
+  real delta = to_share - (user_div * div.integer());
+  for (i = row; i < (row + div.integer()); i++)
+  {
+     if (n_rate() <= i) add_rata();
+     rata(i).add(user_div.string(), tok_ind);
+  }
+  if (!delta.is_zero()) 
+  {                      
+    if (n_rate() <= i) add_rata();
+    rata(i).add(delta.string(), tok_ind);
+  }
+  remove_rate_from(i);
   return 0;
 }