<?php

namespace KupShop\OrderingBundle\Resources\script;

use KupShop\AdminBundle\Util\Script\Script;

class RecalculateOrdersScript extends Script
{
    protected static $name = 'Recalculate orders - fix total prices';

    protected static $defaultParameters = [
        'orderNo' => ['123', '456'],
    ];

    public function run(array $arguments)
    {
        foreach ($arguments['orderNo'] ?? [] as $orderNo) {
            $message = $orderNo.': ';
            try {
                $order = \Order::createFromDbOrderNo($orderNo);
            } catch (\Exception $exception) {
                echo $message.$exception->getMessage().'<br>';
                continue;
            }

            $total = $order->total_price;
            $message .= "total_price = {$order->total_price} {$order->currency} -> recalculate -> ";
            $order->recalculate();
            $message .= "total_price = {$order->total_price} {$order->currency}";
            if ($total != $order->total_price) {
                $message .= ' - FIXED';
            }
            echo $message.'<br>';
        }
    }
}

return RecalculateOrdersScript::class;
