<?php

namespace KupShop\CheckAppBundle\Util;

use Query\Operator;

class WorkerUtil
{
    public function checkIfAnyItemChecked(array $items)
    {
        $checkedItems = array_filter($items, function ($x) { return $x->checked ?? false; });
        if (empty($checkedItems)) {
            return 'Nebyla zkontrolována žádná položka!';
        }

        return null;
    }

    public function getProductAdditionalData($id_product, $id_variation)
    {
        $data = sqlQueryBuilder()
            ->select('GROUP_CONCAT(pos.ean SEPARATOR ";") as supplier_eans, GROUP_CONCAT(pos.code SEPARATOR ";") as supplier_codes')
            ->from('products_of_suppliers', 'pos')
            ->where(Operator::equalsNullable(['pos.id_product' => $id_product, 'pos.id_variation' => $id_variation]))
            ->groupBy('pos.id_product, pos.id_variation')
            ->execute()->fetch();

        if (findModule(\Modules::PRODUCTS_SERIAL_NUMBERS)) {
            $serial_number_require = sqlQueryBuilder()->select('serial_number_require')->from('products')
                ->where(Operator::equals(['id' => $id_product]))
                ->execute()->fetchColumn();
            $data['serial_number_require'] = $serial_number_require;
        }

        return $data ?: [];
    }
}
