So I have the following method:
public function generateTrialBalance( $period ) {
$total_dr = 0;
$total_cr = 0;
$date = NULL;
$group_dr = 0;
$group_cr = 0;
$accountData = $this->find();
$groupData = $this->findAllGroups();
foreach( $groupData as $accountGroup ) {
foreach( $accountData as $ledger ) {
$group = $this->findGroup( $ledger['group_id'] );
$ledgerFormat = gl_format( $ledger['gl_account_main'], $ledger['gl_account_dept'], $ledger['gl_account_sub']);
$ledgerEntries = $this->findLedgerEntries( $ledger['id'] );
foreach( $ledgerEntries as $entries ) {
$journalData = $this->findJournalEntry( $entries['entry_id'] );
$date = date( 'm', $journalData['date'] );
if ( $date <= $period ) {
$position = $entries['position'];
$amount = $entries['amount'];
$position == "D" ? $total_dr += $amount : $total_cr += $amount;
}
}
$total = $total_dr - $total_cr;
$total > 0 ? $group_dr += $total : $group_cr +=$total;
if( $accountGroup['id'] == $group['id']) {
$this->ledgerTable[$group['name']][] = array(
"account" => $ledgerFormat,
"description" => $ledger['name'],
"total" => $total,
"group_dr" => $group_dr,
"group_cr" => $group_cr,
);
}
$total_dr = 0;
$total_cr = 0;
}
$group_dr = 0; // Here is where the issue needs resolving
$group_cr = 0; // Resetting the variables to prepare for next iter
}
return $this->ledgerTable;
}
To give an idea as to what some of my variables contain or do, $groupData
has 9 rows. $accountData
has about 33 rows
My goal is for every iteration of $groupData
, find the rows in $accountData
that match to $groupData
and then append to a new array based on the findings. I get an array that looks like this:
array(
"Group1" => array(
array(
"account" => 'string';
"description" => 'string';
"total" => 'float';
"group_dr" => 'float';
"group_cr" => 'float';
),
array(
"account" => 'string';
"description" => 'string';
"total" => 'float';
"group_dr" => 'float';
"group_cr" => 'float';
)
),
"Group2" => array(
array(
"account" => 'string';
"description" => 'string';
"total" => 'float';
"group_dr" => 'float';
"group_cr" => 'float';
),
array(
"account" => 'string';
"description" => 'string';
"total" => 'float';
"group_dr" => 'float';
"group_cr" => 'float';
)
),
Everything works perfectly except for the critical moment when the first iteration of $groupData
has finished, the $group_dr
and $group_cr
is set to 0 so as to prepare a new iteration for the next group. However, it doesn't seem to be set to zero. And I end up with this :
Perhaps even weirder, if I remove the two variables at the bottom I end up with this:
Which makes me wonder if the variable is being cached somewhere else. But it isn't. It's just upsetting because the second loop has $total_dr
and $total_cr
with the exact same logic and resetting to zero without issue.
I feel like I've been overthinking and have tried several methods. If anyone could please explain where I am possibly going wrong because I know I am somewhere. Thank you
EDIT:
I am editing because there is another place where $group_dr
and $group_cr
is being edited and that is in:
$total > 0 ? $group_dr += $total : $group_cr +=$total;
However this shouldn't have an effect because this is in the second iteration. But I figured I would point it out