Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 35 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| UpdateConferenceInvoiceService | |
0.00% |
0 / 35 |
|
0.00% |
0 / 3 |
72 | |
0.00% |
0 / 1 |
| update | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
30 | |||
| validateInvoiceItem | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| 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\Invoice\InvoiceItem; |
| 8 | use App\Models\Invoice\InvoiceVerification; |
| 9 | use App\Models\User; |
| 10 | use Domain\Invoice\DTOs\InvoiceConferenceDTO; |
| 11 | use Domain\Invoice\DTOs\StoreInvoiceActivityLogDTO; |
| 12 | use Domain\Invoice\Enums\InvoiceActivetyLogOperationEnum; |
| 13 | use Domain\Invoice\Exceptions\InvoiceItemDoNotBelongsToInvoiceException; |
| 14 | use Illuminate\Support\Facades\DB; |
| 15 | use Throwable; |
| 16 | |
| 17 | readonly class UpdateConferenceInvoiceService |
| 18 | { |
| 19 | /** |
| 20 | * @param InvoiceConferenceDTO $invoiceConferenceDTO |
| 21 | * @param InvoiceVerification $invoiceVerification |
| 22 | * @param User $user |
| 23 | * @throws Throwable |
| 24 | */ |
| 25 | public function update(InvoiceConferenceDTO $invoiceConferenceDTO, InvoiceVerification $invoiceVerification, User $user): void |
| 26 | { |
| 27 | try { |
| 28 | DB::beginTransaction(); |
| 29 | |
| 30 | $invoiceVerificationOld = $invoiceVerification->getAttributes(); |
| 31 | |
| 32 | if(!is_null($invoiceConferenceDTO->invoices_item_id)){ |
| 33 | $this->validateInvoiceItem($invoiceConferenceDTO, $invoiceVerification); |
| 34 | |
| 35 | $invoiceVerification->invoiceItem()->associate($invoiceConferenceDTO->invoices_item_id); |
| 36 | $invoiceVerification->setAttribute('invoice_item_amount', $invoiceConferenceDTO->invoices_item_amount); |
| 37 | $invoiceVerification->setAttribute('product_amount', $invoiceConferenceDTO->product_amount); |
| 38 | } |
| 39 | |
| 40 | if(!is_null($invoiceConferenceDTO->product_id)){ |
| 41 | $invoiceVerification->product()->associate($invoiceConferenceDTO->product_id); |
| 42 | $invoiceVerification->setAttribute('product_amount', $invoiceConferenceDTO->product_amount); |
| 43 | } |
| 44 | |
| 45 | if(!is_null($invoiceConferenceDTO->description)){ |
| 46 | $invoiceVerification->setAttribute('description', $invoiceConferenceDTO->description); |
| 47 | $invoiceVerification->setAttribute('product_amount', $invoiceConferenceDTO->product_amount); |
| 48 | } |
| 49 | |
| 50 | $invoiceVerification->save(); |
| 51 | |
| 52 | $this->processActivetyLog($invoiceVerification, $user, $invoiceVerificationOld); |
| 53 | |
| 54 | DB::commit(); |
| 55 | } catch (Throwable $exception) { |
| 56 | DB::rollBack(); |
| 57 | throw $exception; |
| 58 | } |
| 59 | |
| 60 | ServiceProcessed::dispatch(self::class, $user); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @param InvoiceConferenceDTO $invoiceConferenceDTO |
| 65 | * @param Invoice $invoice |
| 66 | * @throws Throwable |
| 67 | */ |
| 68 | private function validateInvoiceItem(InvoiceConferenceDTO $invoiceConferenceDTO, InvoiceVerification $invoiceVerification) |
| 69 | { |
| 70 | $invoiceItem = InvoiceItem::query() |
| 71 | ->where('invoice_id', $invoiceVerification->invoice->getKey()) |
| 72 | ->where('id', $invoiceConferenceDTO->invoices_item_id->getKey()) |
| 73 | ->first(); |
| 74 | |
| 75 | if(!$invoiceItem){ |
| 76 | throw new InvoiceItemDoNotBelongsToInvoiceException(__('The invoice item informed do not belongs to the invoice informed')); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @param InvoiceVerification $invoiceVerification |
| 82 | * @param User $user |
| 83 | * @param array $details |
| 84 | */ |
| 85 | private function processActivetyLog(InvoiceVerification $invoiceVerification, User $user, array $details) |
| 86 | { |
| 87 | $invoiceActivityLogDTO = new StoreInvoiceActivityLogDTO([ |
| 88 | 'entity_type' => InvoiceVerification::class, |
| 89 | 'entity_id' => $invoiceVerification->getKey(), |
| 90 | 'operation' => InvoiceActivetyLogOperationEnum::UPDATE, |
| 91 | 'user' => $user, |
| 92 | 'detail' => json_encode($details), |
| 93 | 'observation' => 'Atualizado informações da conferência.' |
| 94 | ]); |
| 95 | |
| 96 | (new InvoiceActivityLogStoreService())->store($invoiceActivityLogDTO, $invoiceVerification->invoice, $user); |
| 97 | } |
| 98 | } |