Get the balance of the previous month every month dynamically

Asked

Viewed 96 times

0

How do I get the previous month dynamically;

I have the Inputs and Outputs. In my report I show the Balance of the month (Inputs - Outputs), then I show the balance of the previous month, This works up to 2 months, in the third month the sum is wrong! What I want is that every month it takes the previous balance!

$entradas = Entradas::whereIn('entrada.cliente_id',$cliente_id)->whereBetween('dtarecebimento', [$dt_inicio, $dt_fim])->get();

$saida = Saida::whereIn('saida.cliente_id',$cliente_id)->whereBetween('dtarecebimento', [$dt_inicio, $dt_fim])->get();

Balance of the month $totalEntradas = $entradas - $saida;

$dataIniAnterior = Carbon::parse($dt_inicio)->subMonth(1)->format('Y-m-d');
$dataFimAnterior = Carbon::parse($dt_fim)->subMonth(1)->format('Y-m-d');

$entradasAnterior = Entradas::whereBetween('dtarecebimento', [$dataIniAnterior, $dataFimAnterior])->get();
$saidasAnterior = Saida::whereBetween('data', [$dataIniAnterior, $dataFimAnterior])->get();

Balance of the previous month

$entradaMesAnterior = $entradasAnterior - $saidasAnterior;
  • Pq you do not use the query to bring this information straight from the database and grouped by month?

  • Problem solved ...

No answers

Browser other questions tagged

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