Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 46 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ConferenceInvoiceService | |
0.00% |
0 / 46 |
|
0.00% |
0 / 4 |
132 | |
0.00% |
0 / 1 |
| conference | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
42 | |||
| validateInvoiceItemBelongsToInvoice | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| 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\ExistsInvoiceVerificationToInformedItemException; |
| 14 | use Domain\Invoice\Exceptions\InvoiceItemDoNotBelongsToInvoiceException; |
| 15 | use Domain\Invoice\Exceptions\InvoiceNotReleasedException; |
| 16 | use Illuminate\Support\Facades\DB; |
| 17 | use Throwable; |
| 18 | |
| 19 | readonly class ConferenceInvoiceService |
| 20 | { |
| 21 | /** |
| 22 | * @param Invoice $invoice |
| 23 | * @param User $user |
| 24 | * @throws Throwable |
| 25 | */ |
| 26 | public function conference(InvoiceConferenceDTO $invoiceConferenceDTO, 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 | $invoiceVerification = new InvoiceVerification(); |
| 36 | $invoiceVerification->invoice()->associate($invoice); |
| 37 | $invoiceVerification->responsible()->associate($user); |
| 38 | |
| 39 | if(!is_null($invoiceConferenceDTO->invoices_item_id)){ |
| 40 | $this->validateInvoiceItemBelongsToInvoice($invoiceConferenceDTO, $invoice); |
| 41 | $this->validateInvoiceItem($invoiceConferenceDTO, $invoice); |
| 42 | |
| 43 | $invoiceVerification->invoiceItem()->associate($invoiceConferenceDTO->invoices_item_id); |
| 44 | $invoiceVerification->setAttribute('invoice_item_amount', $invoiceConferenceDTO->invoices_item_amount); |
| 45 | $invoiceVerification->setAttribute('product_amount', $invoiceConferenceDTO->product_amount); |
| 46 | } |
| 47 | |
| 48 | if(!is_null($invoiceConferenceDTO->product_id)){ |
| 49 | $invoiceVerification->product()->associate($invoiceConferenceDTO->product_id); |
| 50 | $invoiceVerification->setAttribute('product_amount', $invoiceConferenceDTO->product_amount); |
| 51 | } |
| 52 | |
| 53 | if(!is_null($invoiceConferenceDTO->description)){ |
| 54 | $invoiceVerification->setAttribute('description', $invoiceConferenceDTO->description); |
| 55 | $invoiceVerification->setAttribute('product_amount', $invoiceConferenceDTO->product_amount); |
| 56 | } |
| 57 | |
| 58 | $invoiceVerification->save(); |
| 59 | |
| 60 | $this->processActivetyLog($invoice, $user, $invoiceVerification); |
| 61 | |
| 62 | DB::commit(); |
| 63 | } catch (Throwable $exception) { |
| 64 | DB::rollBack(); |
| 65 | throw $exception; |
| 66 | } |
| 67 | |
| 68 | ServiceProcessed::dispatch(self::class, $user); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @param InvoiceConferenceDTO $invoiceConferenceDTO |
| 73 | * @param Invoice $invoice |
| 74 | * @throws Throwable |
| 75 | */ |
| 76 | private function validateInvoiceItemBelongsToInvoice(InvoiceConferenceDTO $invoiceConferenceDTO, Invoice $invoice) |
| 77 | { |
| 78 | $invoiceItem = InvoiceItem::query() |
| 79 | ->where('invoice_id', $invoice->getKey()) |
| 80 | ->where('id', $invoiceConferenceDTO->invoices_item_id->getKey()) |
| 81 | ->first(); |
| 82 | |
| 83 | if(!$invoiceItem){ |
| 84 | throw new InvoiceItemDoNotBelongsToInvoiceException(__('The invoice item informed do not belongs to the invoice informed')); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @param InvoiceConferenceDTO $invoiceConferenceDTO |
| 90 | * @param Invoice $invoice |
| 91 | * @throws Throwable |
| 92 | */ |
| 93 | private function validateInvoiceItem(InvoiceConferenceDTO $invoiceConferenceDTO, Invoice $invoice) |
| 94 | { |
| 95 | $invoiceVerification = InvoiceVerification::query() |
| 96 | ->where('invoice_id', $invoice->getKey()) |
| 97 | ->where('invoice_item_id', $invoiceConferenceDTO->invoices_item_id->getKey()) |
| 98 | ->first(); |
| 99 | |
| 100 | if($invoiceVerification){ |
| 101 | throw new ExistsInvoiceVerificationToInformedItemException(__('The informed invoice item is already checked')); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * @param Invoice $invoice |
| 107 | * @param User $user |
| 108 | * @param InvoiceVerification $invoiceVerification |
| 109 | */ |
| 110 | private function processActivetyLog(Invoice $invoice, User $user, InvoiceVerification $invoiceVerification) |
| 111 | { |
| 112 | $invoiceActivityLogDTO = new StoreInvoiceActivityLogDTO([ |
| 113 | 'entity_type' => InvoiceVerification::class, |
| 114 | 'entity_id' => $invoiceVerification->getKey(), |
| 115 | 'operation' => InvoiceActivetyLogOperationEnum::INSERT, |
| 116 | 'user' => $user, |
| 117 | 'detail' => json_encode($invoiceVerification->getAttributes()), |
| 118 | 'observation' => 'Realizado a conferĂȘncia do item da nota fiscal.' |
| 119 | ]); |
| 120 | |
| 121 | (new InvoiceActivityLogStoreService())->store($invoiceActivityLogDTO, $invoice, $user); |
| 122 | } |
| 123 | } |