Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 34 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| ReturnConferenceInvoiceService | |
0.00% |
0 / 34 |
|
0.00% |
0 / 2 |
20 | |
0.00% |
0 / 1 |
| returnConference | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
12 | |||
| processActivetyLog | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\Invoice\Services; |
| 4 | |
| 5 | use Throwable; |
| 6 | |
| 7 | use Illuminate\Support\Facades\DB; |
| 8 | |
| 9 | use App\Events\ServiceProcessed; |
| 10 | use App\Models\Invoice\Invoice; |
| 11 | use App\Models\Invoice\InvoiceVerification; |
| 12 | use App\Models\User; |
| 13 | |
| 14 | use Domain\Invoice\Enums\InvoiceVerificationsStatusEnum; |
| 15 | use Domain\Invoice\Exceptions\InvoiceNotReleasedException; |
| 16 | use Domain\Invoice\Enums\VerificationStatusInvoiceEnum; |
| 17 | use Domain\Invoice\Enums\InvoiceActivetyLogOperationEnum; |
| 18 | use Domain\Invoice\DTOs\StoreInvoiceActivityLogDTO; |
| 19 | |
| 20 | readonly class ReturnConferenceInvoiceService |
| 21 | { |
| 22 | /** |
| 23 | * @param Invoice $invoice |
| 24 | * @param User $user |
| 25 | * @throws Throwable |
| 26 | */ |
| 27 | public function returnConference(Invoice $invoice, User $user): void |
| 28 | { |
| 29 | try { |
| 30 | DB::beginTransaction(); |
| 31 | |
| 32 | if(!$invoice->isReleased()){ |
| 33 | throw new InvoiceNotReleasedException( |
| 34 | __('Invoice is not released') |
| 35 | ); |
| 36 | } |
| 37 | |
| 38 | $invoice->setAttribute('verification_status', VerificationStatusInvoiceEnum::PENDING); |
| 39 | $invoice->save(); |
| 40 | |
| 41 | $details = $invoice->getAttributes(); |
| 42 | |
| 43 | $this->processActivetyLog( |
| 44 | $invoice, |
| 45 | Invoice::class, |
| 46 | $invoice->getKey(), |
| 47 | InvoiceActivetyLogOperationEnum::INSERT, |
| 48 | $user, |
| 49 | $details, |
| 50 | 'Retornada para uma nova conferĂȘncia.' |
| 51 | ); |
| 52 | |
| 53 | // (new InvoiceActivityLogStoreService())->store($invoiceActivityLogDTO, $invoice, $user); |
| 54 | |
| 55 | InvoiceVerification::where('invoice_id', $invoice->id) |
| 56 | ->where('status', InvoiceVerificationsStatusEnum::WITH_DISAGREEMENT) |
| 57 | ->delete(); |
| 58 | |
| 59 | DB::commit(); |
| 60 | } catch (Throwable $exception) { |
| 61 | DB::rollBack(); |
| 62 | throw $exception; |
| 63 | } |
| 64 | |
| 65 | ServiceProcessed::dispatch(self::class, $user); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | /** |
| 70 | * @param Invoice $invoice |
| 71 | * @param string $entityType |
| 72 | * @param int $entityId |
| 73 | * @param InvoiceActivetyLogOperationEnum $operation |
| 74 | * @param User $user |
| 75 | * @param array $details |
| 76 | * @param string $observation |
| 77 | * @return void |
| 78 | */ |
| 79 | private function processActivetyLog( |
| 80 | Invoice $invoice, |
| 81 | string $entityType, |
| 82 | int $entityId, |
| 83 | InvoiceActivetyLogOperationEnum $operation, |
| 84 | User $user, |
| 85 | array $details, |
| 86 | string $observation |
| 87 | ): void { |
| 88 | $invoiceActivityLogDTO = new StoreInvoiceActivityLogDTO([ |
| 89 | 'entity_type' => $entityType, |
| 90 | 'entity_id' => $entityId, |
| 91 | 'operation' => $operation, |
| 92 | 'user' => $user, |
| 93 | 'detail' => json_encode($details), |
| 94 | 'observation' => $observation |
| 95 | ]); |
| 96 | |
| 97 | (new InvoiceActivityLogStoreService())->store($invoiceActivityLogDTO, $invoice, $user); |
| 98 | } |
| 99 | } |