<?php

namespace App\Jobs\Setting;

use App\Abstracts\Job;

class DeleteTax extends Job
{
    protected $tax;

    /**
     * Create a new job instance.
     *
     * @param  $tax
     */
    public function __construct($tax)
    {
        $this->tax = $tax;
    }

    /**
     * Execute the job.
     *
     * @return boolean|Exception
     */
    public function handle()
    {
        $this->authorize();

        $this->tax->delete();

        return true;
    }

    /**
     * Determine if this action is applicable.
     *
     * @return void
     */
    public function authorize()
    {
        if ($relationships = $this->getRelationships()) {
            $message = trans('messages.warning.deleted', ['name' => $this->tax->name, 'text' => implode(', ', $relationships)]);

            throw new \Exception($message);
        }
    }

    public function getRelationships()
    {
        $rels = [
            'items' => 'items',
            'invoice_items' => 'invoices',
            'bill_items' => 'bills',
        ];

        return $this->countRelationships($this->tax, $rels);
    }
}
