<?php

namespace App\Classes\Modules\Orders\Services\Steps;


use App\Classes\Modules\Orders\DataTransferObjects\OrderObject;
use App\Classes\Modules\Orders\DataTransferObjects\StepObject;
use App\Models\Order;

class CompletesOrderStep {

    /**
     * @param int $orderId
     * @param StepObject $step
     */
    public function execute(int $orderId, StepObject $step){

        /** @var Order $repository */
        $repository = Order::findOrFail($orderId);

        $orderStep = $repository->orderSteps()->where('reference_id', $step->getReferenceId())->firstOrFail();

        $orderStep->complete = 1;
        $orderStep->complete_date = $step->getCompleteDate();

        if($orderStep->save()){
            (new UpdatesOrderCurrentStep())->execute($orderId, $step);
        }


    }


}