request = $request; } public function execute() : CreateInvoiceResponse { $response = new CreateInvoiceResponse(); if ($this->request->accountIban === 'undefined') { $this->request->accountIban = null; } $invoice = Invoice::create([ 'tenant' => app('tenant')->slug, 'cost_unit_id' => $this->request->costUnit->id, 'invoice_number' => $this->generateInvoiceNumber(), 'status' => InvoiceStatus::INVOICE_STATUS_NEW, 'type' => $this->request->invoiceType, 'type_other' => $this->request->invoiceTypeExtended, 'donation' => $this->request->isDonation, 'user_id' => $this->request->userId, 'contact_name' => $this->request->contactName, 'contact_email' => $this->request->contactEmail, 'contact_phone' => $this->request->contactPhone, 'contact_bank_owner' => $this->request->accountOwner, 'contact_bank_iban' => $this->request->accountIban, 'amount' => $this->request->totalAmount, 'distance' => $this->request->distance, 'travel_direction' => $this->request->travelRoute, 'passengers' => $this->request->passengers, 'transportation' => $this->request->transportations, 'document_filename' => $this->request->receiptFile !== null ? $this->request->receiptFile->fullPath : null, ]); if ($invoice !== null) { $response->success = true; $response->invoice = $invoice; } return $response; } private function generateInvoiceNumber() : string { $lastInvoiceNumber = Invoice::query() ->where('tenant', app('tenant')->slug) ->whereYear('created_at', date('Y')) ->count(); $invoiceNumber = $lastInvoiceNumber + 1; $invoiceNumber = str_pad($invoiceNumber, 4, '0', STR_PAD_LEFT); return sprintf('%1$s-%2$s', date('Y'), $invoiceNumber); } }