Calculate installment entry value - PHP

Asked

Viewed 401 times

0

Based on the function below, how can I consider and calculate the value of the entry in the installment, so that the rest is divided in equal parts for the other installments?

function calculo_negociacao($valor_total, $parcelas, $dt_vencimento, $valor_entrada) {  
    $parcelado = [];

    $valor = $valor_total / $parcelas;
    $valor = number_format((float)$valor, 2, '.', '');

    $parcelado = array_fill(0, $parcelas, ['valor' => $valor]);

    $dt_vencimento = explode( '-', $dt_vencimento);
    $dia = $dt_vencimento[0];
    $mes = $dt_vencimento[1];
    $ano = $dt_vencimento[2];

    for($x = 0; $x < $parcelas; $x++){
        $parcelado[$x]['parcela'] = $x + 1; 
        $parcelado[$x]['dt_vencimento'] = date("Y-m-d",strtotime("+".$x." month",mktime(0, 0, 0, $mes, $dia, $ano)));
    }       
    return $parcelado;
}

$valor = 150.07;
$valor_entrada= 50.00;
$parcela = 4;
$dt_vencimento = '04-09-2018';
$negociacao = calculo_negociacao($valor, $parcela, $dt_vencimento, $valor_entrada);
var_dump($negociacao);
echo json_encode($negociacao);
  • All that is left is to subtract the input value from the total value before calculating the plots. I have included your code here.. https://ideone.com/eog1Sa

  • @Andremesquita, thank you, but the entry value may or may not be informed, if it is not, will be divided into equal parts.

  • How so? At what point is to subtract the input value from the total value? It is to be divided also by each parcel?

  • @Milk, if the value of the input is greater than 0, then I must consider the value of the input, as the first installment, the remainder divides equally for the other installments. an incoming installment plan, example 1(entry) + 3 (installments), if the value of the entry is 0, then divide into 4 equal installments. I am quoting 4 installments, because it is in the example, however, it is a variable

  • @Milk, I even got it this way, https://ideone.com/QBpdkE, but I believe there is a simpler way to do it

1 answer

1


You can for only 2 checks if it exists $valor_entrada at specific points of the function to change its behavior, without having to separate into 2 distinct blocks that do something very similar. Look at the code below

<?php

function calculo_negociacao($valor_total, $parcelas, $dt_vencimento, $valor_entrada) {  
    $parcelado = [];
    $inicio = 0;

    if ($valor_entrada > 0) {
        $inicio = 1;
        $valor_total = $valor_total - $valor_entrada;
    }

    $valor = $valor_total / ($parcelas - $inicio); // reduzimos 1 parcela se houver entrada ou 0 se nao
    $valor = number_format((float)$valor, 2, '.', '');

    $parcelado = array_fill($inicio, $parcelas - $inicio, ['valor' => $valor]); // $parcelas - 1 pois será menos 1 com a entrada ou 0 se nao houver

    // só depois de calcularmos todos é que podemos adicionar a entrada no inicio
    if ($valor_entrada > 0) {
        array_unshift($parcelado, ['valor' => number_format((float)$valor_entrada, 2, '.', '')]);
    }

    $dt_vencimento = explode( '-', $dt_vencimento);
    $dia = $dt_vencimento[0];
    $mes = $dt_vencimento[1];
    $ano = $dt_vencimento[2];

    for($x = 0; $x < $parcelas; $x++){
        $parcelado[$x]['parcela'] = $x + 1; 
        $parcelado[$x]['dt_vencimento'] = date("Y-m-d",strtotime("+".$x." month",mktime(0, 0, 0, $mes, $dia, $ano)));
    }

    return $parcelado;
}

$valor = 150.07;
$valor_entrada= 50.00;
$parcela = 4;
$dt_vencimento = '04-09-2018';
$negociacao = calculo_negociacao($valor, $parcela, $dt_vencimento, $valor_entrada);
//var_dump($negociacao);

echo '<pre>';
echo var_dump($negociacao);
  • It worked very well, thank you very much!

  • Hello @Leite, when you can, you can help me with this question https://answall.com/questions/408530/calcular-juros-com-parcela-php ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.