Get every month the balance of the previous month dynamically

0

How do I get the previous month dynamically;

I have the Entries and Exits tables. In my report I show the Balance of the month , then I show the balance of the previous month , this works for up to 2 months, in the third month the sum is wrong! What I want is that every month he 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();

Previous Month Balance

$entradaMesAnterior = $entradasAnterior - $saidasAnterior;
    
asked by anonymous 09.08.2018 / 20:45

0 answers