<?php


namespace App\Classes\Modules\Transactions\ControllersLogic;

use App\Classes\Modules\Transactions\Processors\CreateSupplierTransactionProcessor;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Mccarlosen\LaravelMpdf\Facades\LaravelMpdf;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\Transactions\Services\FetchesTransaction;
use App\Classes\ValueObjects\Constants\TransactionType;

class GenerateCreditNotePdfLogic
{

    /** @var FetchesTransaction */
    private $fetchesTransaction;

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

    /**
     * GenerateCreditNotePdfLogic constructor.
     * @param FetchesTransaction $fetchesTransaction
     * @param FetchesCompany $fetchesCompany
     */
    public function __construct(FetchesTransaction $fetchesTransaction, FetchesCompany $fetchesCompany)
    {
        $this->fetchesTransaction = $fetchesTransaction;
        $this->fetchesCompany = $fetchesCompany;
    }

    /**
     * @param Request $request
     * @return string|\Symfony\Component\HttpFoundation\Response
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function execute(Request $request)
    {
        $transaction = $this->fetchesTransaction->execute(['id' => $request->route('id')]);

        $booking = $transaction->booking;

        $supplier = $this->fetchesCompany->execute(['id' => $transaction->receiver]);

        $pdf = LaravelMpdf::loadView('pages.pdfs.credit_note', ['transaction' => $transaction, 'booking' => $booking, 'supplier' => $supplier]);

        return $pdf->stream('CreditNote.pdf');
    }
}
