Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 22 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| ConcludeInvoiceService | |
0.00% |
0 / 22 |
|
0.00% |
0 / 2 |
30 | |
0.00% |
0 / 1 |
| conclude | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
| processActivetyLog | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\Invoice\Services; |
| 4 | |
| 5 | use App\Events\ServiceProcessed; |
| 6 | use App\Models\Invoice\Invoice; |
| 7 | use App\Models\User; |
| 8 | use Domain\Invoice\DTOs\StoreInvoiceActivityLogDTO; |
| 9 | use Domain\Invoice\Enums\InvoiceActivetyLogOperationEnum; |
| 10 | use Domain\Invoice\Enums\InvoiceStatusEnum; |
| 11 | use Domain\Invoice\Enums\VerificationStatusInvoiceEnum; |
| 12 | use Domain\Invoice\Exceptions\InvoiceReleasedException; |
| 13 | use Illuminate\Support\Facades\DB; |
| 14 | use Throwable; |
| 15 | |
| 16 | readonly class ConcludeInvoiceService |
| 17 | { |
| 18 | /** |
| 19 | * @param Invoice $invoice |
| 20 | * @param User $user |
| 21 | * @throws Throwable |
| 22 | */ |
| 23 | public function conclude(Invoice $invoice, User $user): void |
| 24 | { |
| 25 | try { |
| 26 | DB::beginTransaction(); |
| 27 | |
| 28 | if($invoice->isConclude()){ |
| 29 | throw new InvoiceReleasedException(__('Invoice has already been conclude')); |
| 30 | } |
| 31 | |
| 32 | $invoiceOld = $invoice->getAttributes(); |
| 33 | |
| 34 | if($invoice->getAttribute('verification_status') == VerificationStatusInvoiceEnum::WITH_DISAGREEMENT){ |
| 35 | $invoice->setAttribute('status', InvoiceStatusEnum::CONCLUDED); |
| 36 | $invoice->save(); |
| 37 | |
| 38 | $this->processActivetyLog($invoice, $user, $invoiceOld); |
| 39 | } |
| 40 | |
| 41 | DB::commit(); |
| 42 | } catch (Throwable $exception) { |
| 43 | DB::rollBack(); |
| 44 | throw $exception; |
| 45 | } |
| 46 | |
| 47 | ServiceProcessed::dispatch(self::class, $user); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @param Invoice $invoice |
| 52 | * @param User $user |
| 53 | * @param array $details |
| 54 | */ |
| 55 | private function processActivetyLog(Invoice $invoice, User $user, array $details) |
| 56 | { |
| 57 | $invoiceActivityLogDTO = new StoreInvoiceActivityLogDTO([ |
| 58 | 'entity_type' => Invoice::class, |
| 59 | 'entity_id' => $invoice->getKey(), |
| 60 | 'operation' => InvoiceActivetyLogOperationEnum::UPDATE, |
| 61 | 'user' => $user, |
| 62 | 'detail' => json_encode($details), |
| 63 | 'observation' => 'Nota fiscal concluĂda.' |
| 64 | ]); |
| 65 | |
| 66 | (new InvoiceActivityLogStoreService())->store($invoiceActivityLogDTO, $invoice, $user); |
| 67 | } |
| 68 | } |