Insert account with Fortnightly, Monthly, Quarterly, Biannual and Annual period

Asked

Viewed 526 times

2

I have a script to do financial control, in it I inform the value and quantity of each installment. I would like an idea of how to define the space of these plots to define whether they will be weekly, fortnightly, monthly, quarterly, half-yearly or annual plots. The script I use today generates installments only monthly.

include 'conectar.php';

$nParcelas = $_POST['PARCELAS'];
$dataPrimeiraParcela = $_POST['VENCIMENTO'];

function calcularParcelas($nParcelas, $dataPrimeiraParcela = null){
  if($dataPrimeiraParcela != null){
    $dataPrimeiraParcela = explode( "/",$dataPrimeiraParcela);
    $dia = $dataPrimeiraParcela[0];
    $mes = $dataPrimeiraParcela[1]-1;
    $ano = $dataPrimeiraParcela[2];

  } else {
    $dia = date("d");
    $mes = date("m")-1;
    $ano = date("Y");
  }

for($x = 1; $x <= $nParcelas; $x++){
$parcela = date("Y-m-d",strtotime("+".$x." month",mktime(0, 0, 0,$mes,$dia,$ano)));
$PAGO = $_POST['PAGO'];
$ID_TIPO = $_POST['ID_TIPO'];
$ID_PESSOA = $_POST['ID_PESSOA'];
$DESCRICAO = $_POST['DESCRICAO'];
$ID_CATEGORIA = $_POST['ID_CATEGORIA'];
$VALOR = $_POST['VALOR'];
$ID_RECEBIMENTO = $_POST['ID_RECEBIMENTO'];
$NP = $x."/".$nParcelas;

   if(mysql_query("INSERT INTO CONTAS
        (ID_TIPO, ID_PESSOA, VENCIMENTO, DESCRICAO, ID_CATEGORIA, VALOR, ID_RECEBIMENTO, PARCELA, ESTADO) 
        VALUES ('".$ID_TIPO."', '".$ID_PESSOA."', '".$parcela."', '".$DESCRICAO."', '".$ID_CATEGORIA."', '".$VALOR."', '".$ID_RECEBIMENTO."', '".$NP."', '".$PAGO."' )"))
  {


    echo "Parcela [".$x."]: ".$parcela."<br/>";

  } else {
    die("Erro ao inserir a parcela ".$x.": ".mysql_error());
  }
}
}
calcularParcelas($nParcelas, $dataPrimeiraParcela);

mysql_close($conexao);
    echo "<a href='conta_nova.php'>Voltar ao sistema</a>";

1 answer

0

I use in a financial system currently for installments something similar, and make the addition in Mysql even with the DATE_ADD function, like this SELECT * FROM table WHERE date = DATE_ADD(CURDATE(), INTERVAL 8 DAY), there is also DATE_SUB, and of course you can put this in your INSERT

In my system the user chooses in his interface at the time of inserting and would come a $_POST['periodicidade'] and I make a switch to check, for example:

  • Fortnightly: DATE_ADD(CURDATE(), INTERVAL 15 DAY)
  • Monthly: DATE_ADD(CURDATE(), INTERVAL 1 MONTH)
  • Quarterly: DATE_ADD(CURDATE(), INTERVAL 3 MONTH)
  • Half-yearly: DATE_ADD(CURDATE(), INTERVAL 6 MONTH)
  • Yearly: DATE_ADD(CURDATE(), INTERVAL 1 YEAR)

And by this logic I will insert the installments each with the maturity in the correct periodicity

Browser other questions tagged

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