Ver Mensaje Individual
  #2 (permalink)  
Antiguo 21/07/2009, 01:29
Suyta
(Desactivado)
 
Fecha de Ingreso: septiembre-2004
Mensajes: 360
Antigüedad: 19 años, 8 meses
Puntos: 1
Respuesta: Bucle for o while

Me respondo a mí misma y lo dejo por si alguien lo necesita.
Con esta función podemos saber rápidamente el número de pagos que podemos cancelar a partir del monto recibido y del monto de la cuota a pagar. Luego simplemente recorremos los registros (sin todo ese lío que yo hacía) para cancelar las órdenes a pagar o modificar el saldo deudor, según sea el caso:
Código PHP:
function installmentRegime($amount$installments)
    {
        
/*** initialize number of payments required ***/
        
$num_installments 1;

        
/*** initialize the amount of the last payment ***/
        
$last_payment $amount;

        
/*** calculate the number of payments and the amount of the last payment ***/
        
while($last_payment $installments)
        {
            
/*** subtract from last payment ***/
            
$last_payment -= $installments;
            
$num_installments++;
        }

        
/*** return array has number of payments plus the amount of the final payment ***/
        
return array('num_installments'=>$num_installments'last_payment'=>$last_payment);
    } 
Ejemplo:
Código PHP:
/*** amount to pay ***/
    
$amount 1270;

    
/*** amount of installments ***/
    
$installments 120;

    
$info installmentRegime($amount$installments);

    echo 
$info['num_installments'].' installments. Last installment is '.$info['last_payment'];