<?php

namespace KupShop\PohodaBundle\Admin;

use Query\Operator;

class pohodaMissingInvoices
{
    public function showMissingInvoices()
    {
        $qb = sqlQueryBuilder()
                ->select('o.id, o.order_no, o.invoice_name, o.invoice_surname, o.invoice_firm')
                ->from('orders', 'o')
                ->leftJoin('o', 'orders_pohoda_invoices', 'opi', 'o.id = opi.id_order')
                ->andWhere(Operator::andX([
                    'o.pohoda_id IS NOT NULL',
                    'o.status_storno=0',
                    'o.date_handle IS NOT NULL',
                    'o.source != "import"',
                    'o.date_created >= NOW() - INTERVAL 365 DAY',
                    'o.date_created <= NOW() - INTERVAL 1 DAY',
                    'opi.id IS NULL', ]));
        foreach ($qb->execute()->fetchAllAssociative() as $order) {
            $parts = [];
            if ($order['invoice_firm']) {
                $parts[] = $order['invoice_firm'];
            }

            if ($order['invoice_name'] || $order['invoice_surname']) {
                $parts[] = join(' ', [$order['invoice_surname'], $order['invoice_name']]);
            }
            $name = join(', ', $parts);

            echo "Kód objednávky {$order['order_no']} (ID: {$order['id']}) {$name} <a href=\"launch.php?s=orders.php&acn=edit&ID={$order['id']}\">{$order['order_no']}</a><br>";
        }
    }
}

$missingOrders = new pohodaMissingInvoices();
$missingOrders->showMissingInvoices();
