<?php

namespace KupShop\OrderingBundle\Resources\script;

use KupShop\AdminBundle\Util\Script\Script;

class UpdatePaymentsScript extends Script
{
    protected static $name = 'Aktualizovat stav zaplacení objednávek';

    protected static $defaultParameters = [
        'date_updated_from' => '2024-10-03',
    ];

    public function run(array $arguments)
    {
        // Select not paid but with payments
        $orders = sqlQueryBuilder()->select('o.id, o.order_no')
            ->from('orders', 'o')
            ->join('o', 'order_payments', 'op', 'o.id = op.id_order')
            ->where('date_updated > :date_updated_from AND status_payed=0')
            ->setParameter('date_updated_from', self::$defaultParameters['date_updated_from'])
            ->groupBy('o.id')
            ->execute();

        foreach ($orders as $row) {
            $this->log("{$row['order_no']}");

            $order = new \Order();
            $order->createFromDB($row['id']);
            $order->updatePayments();
        }
    }
}

return UpdatePaymentsScript::class;
