<?php

namespace App\Classes\Modules\Companies\Services;


use App\Classes\General\Eloquent\AbstractUpdateRecord;
use App\Models\Company;

class UpdatesCompanyEInvoiceRequest extends AbstractUpdateRecord
{

    /**
     * @param Company $model
     * @param bool $eInvoice
     * @return \Illuminate\Database\Eloquent\Model
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function execute(Company $model, bool $eInvoice)
    {
        if($eInvoice){
            if (is_null($model->e_invoice_requested_at)) {
                if($eInvoice){
                    $model->e_invoice_requested_at = now();
                }
            }
            $model->e_invoice = 1;
        }
        else{
            $model->e_invoice_requested_at = null;
            $model->e_invoice = 0;
        }
        return $this->handler($model);
    }
}
