<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED);

$db = $GLOBALS['db'];
global $app_list_strings;

$smarty = new Sugar_Smarty();

if (isset($_REQUEST['date_from'])) {
    $DEBUG = $_REQUEST['debug'] == '1' ? true : false;
    $smarty->assign('debug', $DEBUG ? '1' : '');

    $invoices = getInvoices($_REQUEST['date_from'], $_REQUEST['date_to']);

    $invoiceIds = array();
    foreach ($invoices as $invoice) {
        $invoiceIds[] = $invoice['id'];
    }
    $sessionId = create_guid();
    $_SESSION[$sessionId] = $invoiceIds;
    $smarty->assign('sessionId', $sessionId);

    $invoiceNo = array();
    foreach ($invoices as $invoice) {
        $invoiceNo[] = $invoice['document_no'];
    }
    $smarty->assign('invoicesNo', join(", ", $invoiceNo));

    $smarty->assign('date_from', $_REQUEST['date_from']);
    $smarty->assign('date_to', $_REQUEST['date_to']);
    echo $smarty->display(getcwd() . '/modules/EcmSales/eCommerceZS/eCommerceZS.tpl');
} else {
    $smarty->assign('date_from', date('d.m.Y'));
    $smarty->assign('date_to', date('d.m.Y'));
    echo $smarty->display(getcwd() . '/modules/EcmSales/eCommerceZS/eCommerceZS.tpl');
}

function getInvoices($dateFrom, $dateTo) {
    $db = $GLOBALS['db'];
    $invoices = array();
    $dateFrom = date('Y-m-d', strtotime($dateFrom));
    $dateTo = date('Y-m-d', strtotime($dateTo));
    $query = "SELECT ip.ecmproduct_id, ip.code, i.document_no, SUM(ip.quantity) AS quantity, i.register_date, i.id
FROM ecommerce_invoices_products AS ip
INNER JOIN ecommerce_invoices AS i ON ip.invoice_id = i.id AND i.register_date BETWEEN '$dateFrom' AND '$dateTo' AND i.type='normal'
AND i.origin IN ('allegro', 'shop')
WHERE ip.ecmproduct_id != '' AND ip.ecmproduct_id !='165f364e-9301-25ac-5906-58e38f1de4ca' 
AND i.ecmstockdocout_id IS NULL AND i.ecmsale_id IS NULL
GROUP BY i.id, ip.ecmproduct_id
ORDER BY i.register_date;";
    $res = $db->query($query);
    while ($row = $db->fetchByAssoc($res)) {
        if (!isset($invoices[$row['document_no']])) {
            $invoices[$row['document_no']] = array();
            $invoices[$row['document_no']]['document_no'] = $row['document_no'];
            $invoices[$row['document_no']]['register_date'] = $row['register_date'];
            $invoices[$row['document_no']]['id'] = $row['id'];
            $invoices[$row['document_no']]['products'] = array();
            $invoices[$row['document_no']]['products'][] = array (
                'ecmproduct_id' => $row['ecmproduct_id'],
                'code' => $row['code'],
                'quantity' => $row['quantity'],
            );
        } else {
            $invoices[$row['document_no']]['products'][] = array (
                'ecmproduct_id' => $row['ecmproduct_id'],
                'code' => $row['code'],
                'quantity' => $row['quantity'],
            );
        }
    }
    $invoices = array_values($invoices);
    usort($invoices, function($a, $b) {
        return $a['id'] - $b['id'];
    });
    return $invoices;
}


