Files
SGF/app/Modules/Accounting/InternalStatementTotals.php
T
2026-11-11 09:00:17 +02:00

21 lines
2.0 KiB
PHP

<?php
namespace App\Modules\Accounting;
use Brick\Math\BigInteger;
/** Adds original report values only; never reconstructs journal balances. */
final class InternalStatementTotals
{
public const CATEGORIES=['asset','liability','income','expense','unclassified'];
private array $groups=[];
public function add(array $row,string $category):void
{
if(!in_array($category,self::CATEGORIES,true))throw new \InvalidArgumentException('Rubrica inválida.');$currency=$row['currency'];foreach(self::CATEGORIES as$c)$this->groups[$currency][$c]??=['accounts'=>0,'opening_net_minor'=>'0','debit_minor'=>'0','credit_minor'=>'0','closing_net_minor'=>'0','inverted_accounts'=>0];$g=&$this->groups[$currency][$category];$g['accounts']++;
foreach(['opening_net_minor','debit_minor','credit_minor','closing_net_minor']as$key)$g[$key]=(string)BigInteger::of($g[$key])->plus($row[$key]);
if(($category==='asset'&&BigInteger::of($row['closing_net_minor'])->isNegative())||($category==='liability'&&BigInteger::of($row['closing_net_minor'])->isPositive()))$g['inverted_accounts']++;
}
public function result():array
{
$out=[];foreach($this->groups as$currency=>$groups){foreach($groups as$category=>&$g){$net=BigInteger::of($g['debit_minor'])->minus($g['credit_minor']);$g['period_net_minor']=(string)$net;$g['presentation_minor']=$g['accounts']===0?null:(string)(match($category){'liability'=>BigInteger::of($g['closing_net_minor'])->negated(),'income'=>$net->negated(),'expense'=>$net,default=>BigInteger::of($g['closing_net_minor'])});}unset($g);$known=BigInteger::of($groups['income']['presentation_minor']??'0')->minus($groups['expense']['presentation_minor']??'0');$out[$currency]=['groups'=>$groups,'known_result_subtotal_minor'=>(string)$known,'complete_result_minor'=>$groups['unclassified']['accounts']===0?(string)$known:null,'equity_minor'=>null,'complete_balance_sheet'=>false,'unclassified_accounts'=>$groups['unclassified']['accounts']];}return$out;
}
}