<?php

declare(strict_types=1);

namespace KupShop\ReturnsBundle\Resources\script;

use KupShop\AdminBundle\Util\Script\Script;

class StornoReturnsOrdersScript extends Script
{
    protected static $name = 'Stornovat vratkové objednávky stornovaných vratek';

    protected static $defaultParameters = [
        'run' => false,
    ];

    protected function run(array $arguments)
    {
        if (isLocalDevelopment()) {
            $arguments['run'] = true;
        }

        if (!$arguments['run']) {
            return;
        }

        $qb = sqlQueryBuilder()->select('o.id')
            ->from('orders', 'o')
            ->innerJoin('o', 'returns', 'r', 'o.id = r.id_order  
                        AND o.status = 100 AND o.status_storno = 0 AND r.status = 2');

        foreach ($qb->execute() as $order) {
            $this->log("Stornuji objednávku {$order['id']}");
            $orderObj = new \Order();
            $orderObj->createFromDB($order['id']);
            $orderObj->storno(false, 'Storno objednávky na základě storna vratky.', false);
        }
    }
}

return StornoReturnsOrdersScript::class;
