Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 35 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| AddItemConferenceInvoiceService | |
0.00% |
0 / 35 |
|
0.00% |
0 / 3 |
42 | |
0.00% |
0 / 1 |
| addItemConference | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
12 | |||
| validateAdditionalItem | |
0.00% |
0 / 9 |
|
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\InvoiceVerification; |
| 8 | use App\Models\Product\Product; |
| 9 | use App\Models\User; |
| 10 | use Domain\Invoice\DTOs\InvoiceAddItemConferenceDTO; |
| 11 | use Domain\Invoice\DTOs\StoreInvoiceActivityLogDTO; |
| 12 | use Domain\Invoice\Enums\InvoiceActivetyLogOperationEnum; |
| 13 | use Domain\Invoice\Enums\InvoiceVerificationsStatusEnum; |
| 14 | use Domain\Invoice\Exceptions\ExistsInvoiceVerificationAdditionalItemException; |
| 15 | use Domain\Invoice\Exceptions\InvoiceNotReleasedException; |
| 16 | use Illuminate\Support\Facades\DB; |
| 17 | use Throwable; |
| 18 | |
| 19 | readonly class AddItemConferenceInvoiceService |
| 20 | { |
| 21 | /** |
| 22 | * @param Invoice $invoice |
| 23 | * @param User $user |
| 24 | * @throws Throwable |
| 25 | */ |
| 26 | public function addItemConference(InvoiceAddItemConferenceDTO $invoiceAddItemConferenceDTO, Invoice $invoice, User $user): void |
| 27 | { |
| 28 | try { |
| 29 | DB::beginTransaction(); |
| 30 | |
| 31 | if(!$invoice->isReleased()){ |
| 32 | throw new InvoiceNotReleasedException(__('Invoice is not released')); |
| 33 | } |
| 34 | |
| 35 | $this->validateAdditionalItem($invoiceAddItemConferenceDTO->product_id, $invoice); |
| 36 | |
| 37 | $invoiceVerification = new InvoiceVerification(); |
| 38 | $invoiceVerification->invoice()->associate($invoice); |
| 39 | $invoiceVerification->responsible()->associate($user); |
| 40 | $invoiceVerification->product()->associate($invoiceAddItemConferenceDTO->product_id); |
| 41 | $invoiceVerification->setAttribute('status', InvoiceVerificationsStatusEnum::ADDITIONAL_ITEM->value); |
| 42 | $invoiceVerification->setAttribute('product_amount', $invoiceAddItemConferenceDTO->product_amount); |
| 43 | $invoiceVerification->save(); |
| 44 | |
| 45 | $this->processActivetyLog($invoice, $user, $invoiceVerification); |
| 46 | |
| 47 | DB::commit(); |
| 48 | } catch (Throwable $exception) { |
| 49 | DB::rollBack(); |
| 50 | throw $exception; |
| 51 | } |
| 52 | |
| 53 | ServiceProcessed::dispatch(self::class, $user); |
| 54 | } |
| 55 | |
| 56 | private function validateAdditionalItem(Product $product, Invoice $invoice) |
| 57 | { |
| 58 | $invoiceVerification = InvoiceVerification::query() |
| 59 | ->where('invoice_id', $invoice->getKey()) |
| 60 | ->where('product_id', $product->getKey()) |
| 61 | ->where('status', InvoiceVerificationsStatusEnum::ADDITIONAL_ITEM) |
| 62 | ->first(); |
| 63 | |
| 64 | if($invoiceVerification) |
| 65 | { |
| 66 | throw new ExistsInvoiceVerificationAdditionalItemException( |
| 67 | __('Already exists a additional item to the informed product, to the informed invoice') |
| 68 | ); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @param Invoice $invoice |
| 74 | * @param User $user |
| 75 | * @param InvoiceVerification $invoiceVerification |
| 76 | */ |
| 77 | private function processActivetyLog(Invoice $invoice, User $user, InvoiceVerification $invoiceVerification) |
| 78 | { |
| 79 | $invoiceActivityLogDTO = new StoreInvoiceActivityLogDTO([ |
| 80 | 'entity_type' => InvoiceVerification::class, |
| 81 | 'entity_id' => $invoiceVerification->getKey(), |
| 82 | 'operation' => InvoiceActivetyLogOperationEnum::INSERT, |
| 83 | 'user' => $user, |
| 84 | 'detail' => json_encode($invoiceVerification->getAttributes()), |
| 85 | 'observation' => 'Inserido item adicional a conferĂȘncia.' |
| 86 | ]); |
| 87 | |
| 88 | (new InvoiceActivityLogStoreService())->store($invoiceActivityLogDTO, $invoice, $user); |
| 89 | } |
| 90 | } |