<?php

namespace KupShop\WarehouseBundle\Util;

use Query\Operator;

class ReturnCheckoutWorker extends \KupShop\CheckAppBundle\Util\ReturnCheckoutWorker
{
    protected function getAdditionalData($id_product, $id_variation)
    {
        $data = $this->workerUtil->getProductAdditionalData($id_product, $id_variation);

        $warehouse_data = sqlQueryBuilder()
            ->select('GROUP_CONCAT(wpos.code) as positions, GROUP_CONCAT(pos.ean SEPARATOR ";") as supplier_eans, GROUP_CONCAT(pos.code SEPARATOR ";") as supplier_codes')
            ->from('warehouse_products', 'wp')
            ->leftJoin('wp', 'warehouse_positions', 'wpos', 'wpos.id=wp.id_position')
            ->leftJoin('wpos', 'warehouse_locations', 'wl', 'wl.id = wpos.id_location')
            ->leftJoin('wp', 'products_of_suppliers', 'pos', '((wp.id_product=pos.id_product AND wp.id_variation IS NULL) OR (wp.id_product=pos.id_product AND wp.id_variation=pos.id_variation))')
            ->groupBy('wp.id_product, wp.id_variation')
            ->where(Operator::equalsNullable([
                'wp.id_product' => $id_product,
                'wp.id_variation' => $id_variation,
            ]))
            ->andWhere(Operator::not(Operator::equals(['wl.id' => StoreItemWorker::LOCATION_BOX])))
            ->execute()->fetch();

        if ($warehouse_data) {
            $data = array_merge($data, $warehouse_data);
        }

        return $data;
    }
}
