<?php
namespace Modules\Invoices\CashBook\Management\CashbookView; class Model extends \App\Mvc\ModelAbstract { public function getViewData(array $params) : array { $idOffice = (int)$params['office']['id']; $periodStart = $params['period']['start']; $periodEnd = $params['period']['end']; $office = \Entity\Office::select($idOffice); if (!$office->recordExists() || $office->deleted) { throw new \RuntimeException('Invalid office.'); } if ($periodStart >= $periodEnd) { throw new \RuntimeException('Period start must be before period end.'); } $totalsClass = new \App\Cashbook\Totals($idOffice); $lastClosedDate = $totalsClass->getLastClosedDate(); if ($lastClosedDate === null) { return ['processedDocs' => [], 'lastClosedDate' => null]; } $lastClosedBeforePeriod = $totalsClass->getLastClosedDate($periodStart); if ($lastClosedBeforePeriod === null) { return ['processedDocs' => [], 'lastClosedDate' => $lastClosedDate]; } $effectiveStart = \Engine\DateTime::createFromIsoDate($lastClosedBeforePeriod)->modify('+1day')->formatDateIso(); $processedDocs = (new \App\Cashbook\Documents($idOffice))->getProcessedDocs($effectiveStart, $periodEnd); $showEquivDef = !empty($params['equivDefCurr']); $showEquivWork = !empty($params['equivWorkCurr']); $showCurrExch = !empty($params['currExchDisplay']); $defaultCurrency = $this->globalCfg['currencies']['defaultCurrency']; $workCurrency = $this->globalCfg['currencies']['workCurrency']; if ($showEquivDef || $showEquivWork || $showCurrExch) { foreach ($processedDocs as $date => &$dayData) { if ($showCurrExch) { foreach (\App\Config\Currencies::getInstance()->getSimple() as $curr) { $dayData['exchangeRates'][$curr][$defaultCurrency] = \App\CurrencyExchange\Rates::getInstance($date)->convert(1, $curr, $defaultCurrency, null); } } foreach ($dayData['docs'] as &$doc) { if ($showEquivDef) { $doc['eqvDefValue'] = \App\CurrencyExchange\Rates::getInstance($date)->convert($doc['value'], $doc['currency'], $defaultCurrency); } if ($showEquivWork) { $doc['eqvWorkValue'] = \App\CurrencyExchange\Rates::getInstance($date)->convert($doc['value'], $doc['currency'], $workCurrency); } } unset($doc); if (!empty($dayData['totals'])) { if ($showEquivDef) { $eqvDef = 0.0; foreach ($dayData['totals'] as $curr => $val) { $eqvDef += \App\CurrencyExchange\Rates::getInstance($date)->convert($val, $curr, $defaultCurrency); } $dayData['eqvDefTotal'] = $eqvDef; } if ($showEquivWork) { $eqvWork = 0.0; foreach ($dayData['totals'] as $curr => $val) { $eqvWork += \App\CurrencyExchange\Rates::getInstance($date)->convert($val, $curr, $workCurrency); } $dayData['eqvWorkTotal'] = $eqvWork; } } } unset($dayData); } return array_merge($params, [ 'processedDocs' => $processedDocs, 'lastClosedDate' => $lastClosedDate, ]); } }