<?php

namespace KupShop\WarehouseBundle\Query;

class Orders
{
    public static function orderLocationPositionField($fieldAlias = 'wl_position')
    {
        $sql = '(
    SELECT MIN(wl.sort_index)
    FROM warehouse_locations wl
    WHERE wl.id >2 AND
    wl.id IN (SELECT wpos.id_location FROM warehouse_positions wpos WHERE
        wpos.id IN (SELECT wp.id_position FROM warehouse_products wp WHERE wp.pieces > 0 AND wp.over_supply = "N" AND
        (wp.id_product IN (SELECT oi.id_product FROM order_items oi WHERE oi.id_order = o.id) AND (wp.id_variation IS NULL OR wp.id_variation IN (SELECT oi.id_variation FROM order_items oi WHERE oi.id_order = o.id)
            ))))) '.$fieldAlias;

        return $sql;
    }
}
