<?php


namespace App\Classes\Modules\Transactions\ControllersLogic;

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

class GenerateCreditNotePdfLogic
{

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

    /** @var FetchesCompanyModule */
    private $fetchesCompanyModule;

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

    /**
     * @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')]);

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

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

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