<?php

declare(strict_types=1);

namespace Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\Pracovisko\PocetKontrolAObratuPodlaSubjektuFakturacie;

use Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\EChartSourceAbstract;
use Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\EChartSourceItemContract;

/**
 * Class PocetKontrolAObratuPodlaSubjektuFakturacieEChartSource
 *
 * @package Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\Pracovisko\PocetKontrolAObratuPodlaSubjektuFakturacie
 */
final class PocetKontrolAObratuPodlaSubjektuFakturacieEChartSource extends EChartSourceAbstract
{
    /**
     * @param \Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\EChartSourceItemContract $sourceItem
     *
     * @return $this
     */
    public function addItem(EChartSourceItemContract $sourceItem): self
    {
        $addIt = true;

        if (!empty($this->items)) {
            foreach ($this->items as $existItem) {
                if ($existItem->nazovSubjektu() == $sourceItem->nazovSubjektu()) {
                    $existItem->incrementData($sourceItem->nazovSubjektu(), $sourceItem->pocet(), $sourceItem->obrat());
                    $addIt = false;
                    break;
                }
            }
        }

        if ($addIt) $this->items[] = $sourceItem;

        return $this;
    }
}
