<?php

class OrdersMenu extends Menu
{
    public function get_vars()
    {
        global $cfg;

        $vars = parent::get_vars();

        $complete = 0;
        //        if (findModule('orders', 'complete')) {
        //            sqlQuery(
        //                'SELECT SQL_CALC_FOUND_ROWS 1
        //				FROM orders o
        //				LEFT JOIN order_items oi ON oi.id_order=o.id
        //				LEFT JOIN products p ON oi.id_product=p.id
        //				LEFT JOIN products_variations pv ON pv.id=oi.id_variation
        //				WHERE o.status_storno=0 AND o.status IN ('.join(',', getStatuses('complete')).')
        //				GROUP BY o.id
        //				HAVING MIN(COALESCE(pv.in_store, p.in_store) - oi.pieces + (
        //				 SELECT SUM(oi2.pieces)
        //				 FROM order_items oi2
        //				 LEFT JOIN orders o2 ON oi2.id_order=o2.id
        //				 WHERE oi2.id_product=p.id AND o2.date_created >= o.date_created AND o2.status_storno=0 AND o2.status IN ('.join(',', getStatuses('active')).')
        //				AND ((oi2.id_variation IS NULL AND oi.id_variation IS NULL) OR oi.id_variation=oi2.id_variation)
        //				)) >= 0'
        //            );
        //            $complete = returnSQLResult('SELECT FOUND_ROWS()');
        //        }

        $orders = [];
        if (isset($GLOBALS['cfg']['Order']['Status']['global'])) {
            foreach ($GLOBALS['cfg']['Order']['Status']['global'] as $key => $value) {
                $orders[$key] = (array_search($key, getStatuses('active')) !== false ? ' ('.returnSQLResult("SELECT COUNT(*) FROM orders WHERE status={$key} AND status_storno = 0").')' : '');
            }
        }

        $flags = [];
        if (!empty($cfg['Order']['Flags'])) {
            foreach ($cfg['Order']['Flags'] as $key => $flag) {
                $flags[$key] = returnSQLResult("SELECT COUNT(*) FROM orders WHERE FIND_IN_SET('{$key}',flags)>0 AND status IN (".join(',', getStatuses('active')).')');
            }
        }

        return array_merge($vars, [
            'complete' => $complete,
            'orders' => $orders,
            'flags' => $flags,
        ]
        );
    }
}
