Completed
Push — master ( 6f7ef4...fc758b )
by Andrii
13:22
created

SummaryWidget::run()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 3
nc 4
nop 0
1
<?php
2
3
4
namespace hipanel\widgets;
5
6
7
use Yii;
8
use yii\base\Widget;
9
10
/**
11
 * Class SummaryWidget
12
 * @package hipanel\modules\stock\widgets
13
 */
14
class SummaryWidget extends Widget
15
{
16
    /**
17
     * @var float[]|null
18
     */
19
    public $total_sums;
20
21
    /**
22
     * @var float[]|null
23
     */
24
    public $local_sums;
25
26
    /**
27
     * @inheritDoc
28
     */
29
    public function run()
30
    {
31
        $locals = $this->getSumsString($this->local_sums ?? []);
32
        $totals = $this->getSumsString($this->total_sums ?? []);
33
        return '<div class="summary">' .
34
            ($totals !== '' ? Yii::t('hipanel:stock', 'TOTAL: {sum}', ['sum' => $totals]) : null) .
35
            ($locals !== '' ? '<br><span class="text-muted">' . Yii::t('hipanel:stock', 'on screen: {sum}', ['sum' => $locals]) . '</span>' : null) .
36
            '</div>';
37
    }
38
39
    /**
40
     * @param array $sumsArray
41
     * @return string
42
     * @throws \yii\base\InvalidConfigException
43
     */
44
    private function getSumsString(array $sumsArray): string
45
    {
46
        $totals = '';
47
        foreach ($sumsArray as $cur => $sum) {
48
            if (!$cur) {
49
                continue;
50
            }
51
            if (is_numeric($sum)) {
52
                $totals .= ' &nbsp; <b>' . Yii::$app->formatter->asCurrency($sum, $cur) . '</b>';
53
            } elseif (is_array($sum)) {
54
                $totals .= ' &nbsp; <b>' . Yii::$app->formatter->asCurrency($sum['total'], $cur) . '</b>';
55
                $totals .= ' &nbsp; (+<b>' . Yii::$app->formatter->asCurrency($sum['+'] ?? '0', $cur) . '</b>';
56
                $totals .= ' &nbsp; -<b>' . Yii::$app->formatter->asCurrency($sum['-'] ?? '0', $cur) . '</b>)';
57
            }
58
        }
59
        return $totals;
60
    }
61
}
62