$xPlan->addBonificacion($bonificaciones, $fecha_de_pago, $letra, $tipo_monto_extra);
     }
     //===================================================================================================================================
     $TParcial = $parcialidad_ahorro + $parcialidad_capital + $parcialidad_cargo + $parcialidad_interes + $parcialidad_iva - $bonificaciones;
     $msgM .= "{$socio}\t{$solicitud}\tPer. {$letra}\t{$dias_normales},{$sumar_dias}\t" . getFMoney($saldo_inicial) . "\t" . getFMoney($saldo_final) . "\t" . getFMoney($parcialidad_capital) . "";
     $msgM .= "\t" . getFMoney($parcialidad_interes) . "\t" . getFMoney($parcialidad_iva) . "\t" . getFMoney($parcialidad_cargo) . "\t" . getFMoney($parcialidad_ahorro) . "\t" . getFMoney($TParcial) . "\t{$fecha_de_referencia}\t{$fecha_de_pago}\r\n";
 }
 //end FOR
 $fecha_final = $fecha_de_pago;
 $fecha_de_vencimiento = $fecha_final;
 $dias_netos = restarfechas($fecha_final, $fecha_ministracion);
 $dias_normales = $dias_netos;
 $interes_diario = $total_de_intereses / $dias_netos;
 //===================================================================================================================================
 $OProd = $xCred->getOProductoDeCredito();
 $OPer = $xCred->getOPeriocidad();
 $fecha_de_mora = $xF->setSumarDias($OProd->getDiasTolerados() + 1, $fecha_de_vencimiento);
 $vencimiento_dinamico = $xF->setSumarDias($OPer->getDiasToleradosEnVencer(), $fecha_de_mora);
 $arrUpdate = array("plazo_en_dias" => $dias_netos, "dias_autorizados" => $dias_netos, "fecha_vencimiento" => $fecha_de_vencimiento, "monto_parcialidad" => $primera_parcialidad, "contrato_corriente_relacionado" => $cuenta_captacion, "tipo_de_pago" => $FormaDePago, "fecha_ministracion" => $fecha_ministracion, "interes_diario" => $interes_diario, "fecha_mora" => $fecha_de_mora, "fecha_vencimiento_dinamico" => $vencimiento_dinamico, "fecha_de_primer_pago" => $fecha_primer_abono);
 if ($xCred->getSaldoActual() == $xCred->getMontoAutorizado() or $xCred->getEsAfectable() == false) {
     $arrUpdate["ultimo_periodo_afectado"] = SYS_CERO;
 }
 //Pagos de solo interes
 if ($xCred->getPagosSinCapital() == true) {
     $arrUpdate["ultimo_periodo_afectado"] = $ultima_parcialidad;
 }
 $xCred->setUpdate($arrUpdate);
 // -------------------------------------------- Actualiza el Saldo del Recibo
 $xCred->init();
 $xFRM->addHTML($xCred->getFicha(true, "", false, true));
 $xFRM->addHTML($xPlan->getFicha());
 function setReestructurarSDPM_Planes($SoloConSaldos = false, $creditoFiltrado = false, $forzarTodos = false, $fechaCorte = false, $fechaInicial = false, $EliminarTodo = true)
 {
     $msg = "";
     $xLog = new cCoreLog();
     $xF = new cFecha();
     $ql = new MQL();
     $creditoFiltrado = setNoMenorQueCero($creditoFiltrado);
     if ($creditoFiltrado > DEFAULT_CREDITO) {
         $xLog->add("Socio\tCredito\tFecha\tMonto\tSaldo\tDias\tOperacion\tEstatus\tInteres\tMoratorios\r\n");
     }
     $fechaCorte = $xF->getFechaISO($fechaCorte);
     $fechaInicial = $fechaInicial == false ? EACP_FECHA_DE_CONSTITUCION : $fechaInicial;
     $wCredito1 = $creditoFiltrado > DEFAULT_CREDITO ? "  `creditos_mvtos_asdpm_planes`.`documento` = {$creditoFiltrado} " : " `documento` > 0";
     $wCredito2 = $creditoFiltrado > DEFAULT_CREDITO ? "  numero_de_credito = {$creditoFiltrado} " : " numero_de_credito > 0 ";
     $ByRTM = $creditoFiltrado > DEFAULT_CREDITO ? " " : " SQL_CACHE ";
     $wFecha1 = " AND (fecha >='{$fechaInicial}' AND fecha <='{$fechaCorte}' ) ";
     $wFecha2 = " AND (`fecha_actual` >= '{$fechaInicial}' AND `fecha_actual`<='{$fechaCorte}' ) ";
     $sql = "SELECT {$ByRTM} `creditos_mvtos_asdpm_planes`.* FROM `creditos_mvtos_asdpm_planes` WHERE {$wCredito1} {$wFecha1} ";
     $rs = $ql->getDataRecord($sql);
     if ($EliminarTodo == true) {
         $DAction = my_query("DELETE FROM creditos_sdpm_historico WHERE {$wCredito2} {$wFecha2}");
     }
     $saldo = 0;
     $creditoA = 0;
     $xT = new cTipos();
     $xF = new cFecha();
     $FECHA_DE_ULTIMO_PAGO = EACP_FECHA_DE_CONSTITUCION;
     $CREDITO_SALDO_ANTERIOR = 0;
     $MvtoAnterior = OPERACION_CLAVE_MINISTRACION;
     $ESTADO_ACTUAL = CREDITO_ESTADO_VIGENTE;
     $DIVISOR_DE_INTERESES = EACP_DIAS_INTERES;
     $DCred = array();
     $IsCredNew = true;
     $xCred = null;
     foreach ($rs as $rw) {
         $socio = $xT->cInt($rw["socio"]);
         $credito = $xT->cInt($rw["documento"]);
         $operacion = $xT->cInt($rw["operacion"]);
         $periodo = $xT->cInt($rw["periodo"]);
         $afectacion = $rw["afectacion"];
         $monto = $xT->cFloat($rw["monto"], 2);
         $fecha = $rw["fecha"];
         $nota = "";
         $dias_tolerados = DIAS_PAGO_VARIOS;
         $IsCredNew = true;
         if ($creditoA != $credito) {
             $saldo = 0;
             $FECHA_DE_ULTIMO_PAGO = $fecha;
             $ESTADO_ACTUAL = CREDITO_ESTADO_VIGENTE;
             $xCred = new cCredito($credito, $socio);
             $xCred->init();
             $DCred = $xCred->getDatosDeCredito();
             $CREDITO_SALDO_ANTERIOR = 0;
             $dias_tolerados = $xCred->getOPeriocidad()->getDiasToleradosEnVencer();
             //si es Ministracion
             if ($MvtoAnterior == OPERACION_CLAVE_MINISTRACION) {
                 $FECHA_DE_ULTIMO_PAGO = $xCred->getFechaDeMinistracion();
             }
             $xLog->add("======\t\tINIT-CREDITO : {$credito}\r\n");
             $letras_en_mora = array();
             if ($EliminarTodo == false) {
                 my_query("DELETE FROM creditos_sdpm_historico WHERE numero_de_credito = {$credito} {$wFecha2}");
             }
         } else {
             $IsCredNew = false;
         }
         $interes = 0;
         $moratorio = 0;
         if ($operacion == OPERACION_CLAVE_PLAN_CAPITAL and $monto > 0) {
             if ($xF->getInt($fecha) <= $xF->getInt($fechaCorte)) {
                 $letras_en_mora[$periodo][SYS_MONTO] = $monto;
                 $letras_en_mora[$periodo][SYS_FECHA] = $fecha;
                 $letras_en_mora[$periodo][SYS_DEFAULT] = $fecha;
                 $letras_en_mora[$periodo][SYS_INTERES_MORATORIO] = $monto * $xCred->getTasaDeMora() / EACP_DIAS_INTERES;
                 //TODO: Formular en SQL.- tasa mora para planes
                 $xLog->add("WARN\tAgregando letra en mora por {$monto} del periodo {$periodo} de {$fecha}\r\n", $xLog->DEVELOPER);
             }
         }
         if ($operacion == OPERACION_CLAVE_PLAN_CAPITAL and $monto > 0) {
             if (!isset($FECHA_DE_COMPROMISO)) {
                 $FECHA_DE_COMPROMISO = $fecha;
                 $xLog->add("WARN\tAgregando fecha de primer atraso a {$fecha} del pago {$periodo}\r\n", $xLog->DEVELOPER);
                 //$xLog->add( , $xLog->DEVELOPER);
             }
         }
         if ($operacion == OPERACION_CLAVE_PLAN_INTERES or $operacion == OPERACION_CLAVE_PAGO_INTERES) {
             $interes = $monto;
         } else {
             $interes = 0;
         }
         //XXX: Checar
         $saldo_calculado = 0;
         if ($operacion == OPERACION_CLAVE_PAGO_CAPITAL or $operacion == OPERACION_CLAVE_MINISTRACION) {
             $dias_transcurridos = $xF->setRestarFechas($fecha, $FECHA_DE_ULTIMO_PAGO);
             $saldo_calculado = $saldo * $dias_transcurridos;
             $saldo += $monto * $afectacion;
             $FECHA_DE_ULTIMO_PAGO = $fecha;
             //disminuye de la letra
             if ($operacion == OPERACION_CLAVE_PAGO_CAPITAL) {
                 if (isset($letras_en_mora[$periodo])) {
                     $letras_en_mora[$periodo][SYS_MONTO] -= $monto;
                     $xLog->add("WARN\t{$periodo}\tDisminuir base de mora por {$monto}\r\n", $xLog->DEVELOPER);
                     if (setNoMenorQueCero($letras_en_mora[$periodo][SYS_MONTO]) <= 0) {
                         unset($letras_en_mora[$periodo]);
                     }
                 }
             }
         } else {
             $monto = 0;
         }
         if ($operacion == OPERACION_CLAVE_FIN_DE_MES) {
             $moratorio = 0;
             //if(MODO_DEBUG == true){ $msg	.= "WARN\tFIN DE MES $operacion\r\n"; }
             //Calcular moratorios
             //setLog($letras_en_mora);
             foreach ($letras_en_mora as $id => $atrasos) {
                 $fecha_letra = $atrasos[SYS_DEFAULT];
                 $xLog->add("WARN\t{$periodo}\t{$id} {$fecha_letra}\r\n", $xLog->DEVELOPER);
                 if ($xF->getInt($fecha) >= $xF->getInt($fecha_letra)) {
                     $dias_mora = $xF->setRestarFechas($fecha, $fecha_letra);
                     $xLog->add("WARN\t{$periodo}\tDias de Mora del periodo {$id} por dias {$dias_mora}\r\n", $xLog->DEVELOPER);
                     $moratorio += $atrasos[SYS_INTERES_MORATORIO] * $dias_mora;
                     $letras_en_mora[$id][SYS_DEFAULT] = $fecha;
                 }
             }
         }
         if (isset($FECHA_DE_COMPROMISO)) {
             $dias_de_atraso = $xF->setRestarFechas($fecha, $FECHA_DE_COMPROMISO);
             if ($dias_de_atraso > 1) {
                 $ESTADO_ACTUAL = CREDITO_ESTADO_MOROSO;
             }
             if ($dias_de_atraso > $dias_tolerados) {
                 $ESTADO_ACTUAL = CREDITO_ESTADO_VENCIDO;
                 $xLog->add("WARN\t{$periodo}\tPeriodo a Vencido por {$dias_de_atraso}|{$dias_tolerados}\r\n", $xLog->DEVELOPER);
             }
         }
         if ($creditoFiltrado > DEFAULT_CREDITO) {
             $xLog->add("{$socio}\t{$credito}\t{$fecha}\t{$monto}\t{$saldo}\t{$dias_transcurridos}\t{$operacion}\t{$ESTADO_ACTUAL}\t{$interes}\t{$moratorio}\t{$nota}\r\n");
         }
         if ($xF->getInt($fecha) <= $xF->getInt(SYS_FECHA_DE_MIGRACION)) {
             $interes = 0;
             $moratorio = 0;
             $xLog->add("WARN\tOMITIR Interes {$interes} y MORA por {$moratorio} por estar antes de la migracion {$fecha}\r\n", $xLog->DEVELOPER);
         }
         //agregando letras en Intereses en mora
         $mm = $monto + $interes + $moratorio;
         if ($xF->getInt($fecha) <= $xF->getInt($fechaCorte) and $mm > 0) {
             $xCred->addSDPM($interes, $moratorio, $FECHA_DE_ULTIMO_PAGO, $saldo, $ESTADO_ACTUAL, $fecha, $operacion, $saldo_calculado, $periodo);
         }
         if ($saldo <= TOLERANCIA_SALDOS) {
             $xLog->add("======\t\tEND-CREDITO : {$credito}\r\n");
         }
         $creditoA = $credito;
         $CREDITO_SALDO_ANTERIOR = $saldo;
         $MvtoAnterior = $operacion;
     }
     return $xLog->getMessages();
 }