<?php

namespace App\Classes\Modules\Rules\Standards\Rules;

use App\Classes\Exceptions\CriteriaNotFulfilledException;
use App\Classes\General\Abstracts\AbstractRule;
use App\Classes\Modules\Bookings\Services\FetchesBooking;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\Bookings\Services\CalculatesBookingPayableAmount;
use App\Classes\Modules\Bookings\Services\CalculatesBookingRefundAmount;
use App\Classes\ValueObjects\Constants\TransactionType;

class CanPassEditingPORule extends AbstractRule
{

    /** @var FetchesBooking */
    private $fetchesBooking;

    /** @var FetchesCompany */
    private $fetchesCompany;

    /**
     * CanPassEditingPORule constructor.
     * @param FetchesBooking $fetchesBooking
     * @param FetchesCompany $fetchesCompany
     */
    public function __construct(FetchesBooking $fetchesBooking, FetchesCompany $fetchesCompany)
    {
        $this->fetchesBooking = $fetchesBooking;
        $this->fetchesCompany = $fetchesCompany;
    }

    /**
     * @return bool
     */
    protected function authorized($object): bool
    {
        return true;

    }

    /**
     * @return bool
     */
    protected function validators($object): bool
    {
        return true;

    }


    /**
     * @return bool
     */
    protected function criteria($object): bool
    {
        //Check if user is allow to edit purchase order
        $booking = $this->fetchesBooking->execute(['id' => $object->bookingId]);

        $paidAmount = floatval((App()->make(CalculatesBookingPayableAmount::class))->execute($booking, $booking->fix_currency_id)) - floatval((App()->make(CalculatesBookingRefundAmount::class))->execute($booking, $booking->fix_currency_id));

        if($paidAmount > 0){
            throw new CriteriaNotFulfilledException("Purchase order form is no longer allow to be edited.");
        }
        return true;
    }
}
