Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 22 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| DeniedInvoiceService | |
0.00% |
0 / 22 |
|
0.00% |
0 / 2 |
20 | |
0.00% |
0 / 1 |
| denied | |
0.00% |
0 / 13 |
|
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 App\Events\ServiceProcessed; |
| 6 | use App\Models\Invoice\Invoice; |
| 7 | use App\Models\User; |
| 8 | use Domain\Invoice\DTOs\InvoiceDeniedDTO; |
| 9 | use Domain\Invoice\DTOs\StoreInvoiceActivityLogDTO; |
| 10 | use Domain\Invoice\Enums\InvoiceActivetyLogOperationEnum; |
| 11 | use Domain\Invoice\Enums\InvoiceStatusEnum; |
| 12 | use Domain\Invoice\Exceptions\InvoiceReleasedException; |
| 13 | use Illuminate\Support\Facades\DB; |
| 14 | use Throwable; |
| 15 | |
| 16 | readonly class DeniedInvoiceService |
| 17 | { |
| 18 | /** |
| 19 | * @param InvoiceDeniedDTO $invoiceDeniedDTO |
| 20 | * @param Invoice $invoice |
| 21 | * @param User $user |
| 22 | * @throws Throwable |
| 23 | */ |
| 24 | public function denied(InvoiceDeniedDTO $invoiceDeniedDTO, Invoice $invoice, User $user): void |
| 25 | { |
| 26 | try { |
| 27 | DB::beginTransaction(); |
| 28 | |
| 29 | if($invoice->isDenied()){ |
| 30 | throw new InvoiceReleasedException(__('Invoice has already been denied')); |
| 31 | } |
| 32 | |
| 33 | $invoiceOld = $invoice->getAttributes(); |
| 34 | |
| 35 | $invoice->setAttribute('observation', $invoiceDeniedDTO->observation); |
| 36 | $invoice->setAttribute('status', InvoiceStatusEnum::DENIED); |
| 37 | $invoice->save(); |
| 38 | |
| 39 | $this->processActivetyLog($invoice, $user, $invoiceDeniedDTO->observation, $invoiceOld); |
| 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 string $observation |
| 54 | * @param array $details |
| 55 | */ |
| 56 | private function processActivetyLog(Invoice $invoice, User $user, string $observation, array $details) |
| 57 | { |
| 58 | $invoiceActivityLogDTO = new StoreInvoiceActivityLogDTO([ |
| 59 | 'entity_type' => Invoice::class, |
| 60 | 'entity_id' => $invoice->getKey(), |
| 61 | 'operation' => InvoiceActivetyLogOperationEnum::UPDATE, |
| 62 | 'user' => $user, |
| 63 | 'detail' => json_encode($details), |
| 64 | 'observation' => 'Nota fiscal negada.' |
| 65 | ]); |
| 66 | |
| 67 | (new InvoiceActivityLogStoreService())->store($invoiceActivityLogDTO, $invoice, $user); |
| 68 | } |
| 69 | } |