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