Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 48 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| MarkInvoiceItemWithDisagreementService | |
0.00% |
0 / 48 |
|
0.00% |
0 / 1 |
42 | |
0.00% |
0 / 1 |
| update | |
0.00% |
0 / 48 |
|
0.00% |
0 / 1 |
42 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\Invoice\Services; |
| 4 | |
| 5 | use Throwable; |
| 6 | |
| 7 | use Illuminate\Support\Facades\DB; |
| 8 | |
| 9 | use App\Events\ServiceProcessed; |
| 10 | |
| 11 | use App\Models\Invoice\Invoice; |
| 12 | use App\Models\Invoice\InvoiceItem; |
| 13 | use App\Models\User; |
| 14 | |
| 15 | use Domain\Invoice\DTOs\InvoiceItemDisagreementDTO; |
| 16 | use Domain\Invoice\DTOs\StoreInvoiceActivityLogDTO; |
| 17 | |
| 18 | use Domain\Invoice\Enums\InvoiceActivetyLogOperationEnum; |
| 19 | use Domain\Invoice\Enums\InvoiceItemStatusEnum; |
| 20 | use Domain\Invoice\Enums\InvoicePurchaseOrderStatusEnum; |
| 21 | |
| 22 | use Domain\Invoice\Exceptions\InvoicePendingException; |
| 23 | use Domain\Invoice\Exceptions\InvoicePurchaseOrderValidatedException; |
| 24 | use Domain\Invoice\Exceptions\NotExistsInvoiceVerificationException; |
| 25 | |
| 26 | |
| 27 | readonly class MarkInvoiceItemWithDisagreementService |
| 28 | { |
| 29 | /** |
| 30 | * @param Invoice $invoice |
| 31 | * @param User $user |
| 32 | * @throws Throwable |
| 33 | */ |
| 34 | public function update(InvoiceItemDisagreementDTO $invoiceItemsDisagreementDTO, User $user): void |
| 35 | { |
| 36 | try { |
| 37 | DB::beginTransaction(); |
| 38 | |
| 39 | $invoiceItems = InvoiceItem::where('id', $invoiceItemsDisagreementDTO->id) |
| 40 | ->where('status', InvoiceItemStatusEnum::PENDING) |
| 41 | ->get(); |
| 42 | |
| 43 | if ($invoiceItems->isEmpty()) { |
| 44 | throw new NotExistsInvoiceVerificationException( |
| 45 | __('There is no pending item related to this id.') |
| 46 | ); |
| 47 | } |
| 48 | |
| 49 | $invoice = $invoiceItems->first()->invoice; |
| 50 | |
| 51 | if(!$invoice->isPending()){ |
| 52 | throw new InvoicePendingException( |
| 53 | __('Invoice is not pending') |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | if($invoice->isValidatedPurchaseOrder()){ |
| 58 | throw new InvoicePurchaseOrderValidatedException( |
| 59 | __('Invoice purchase order is validated.') |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | $invoiceItems->each(function ($item) use ($user, $invoiceItemsDisagreementDTO) { |
| 64 | $item->status = InvoiceItemStatusEnum::WITH_DISAGREEMENT; |
| 65 | $item->save(); |
| 66 | |
| 67 | $invoiceActivityLogDTO = new StoreInvoiceActivityLogDTO([ |
| 68 | 'entity_type' => InvoiceItem::class, |
| 69 | 'entity_id' => $item->getKey(), |
| 70 | 'operation' => InvoiceActivetyLogOperationEnum::UPDATE, |
| 71 | 'user' => $user, |
| 72 | 'detail' => $item->toJson(JSON_PRETTY_PRINT), |
| 73 | 'observation' => $invoiceItemsDisagreementDTO->observation |
| 74 | ] |
| 75 | ); |
| 76 | |
| 77 | (new InvoiceActivityLogStoreService())->store($invoiceActivityLogDTO, $item->invoice, $user); |
| 78 | }); |
| 79 | |
| 80 | if ($invoice->purchase_order_status !== InvoicePurchaseOrderStatusEnum::WITH_DISAGREEMENT) { |
| 81 | $invoice->purchase_order_status = InvoicePurchaseOrderStatusEnum::WITH_DISAGREEMENT; |
| 82 | |
| 83 | $invoice->save(); |
| 84 | |
| 85 | $invoiceActivityLogDTO = new StoreInvoiceActivityLogDTO([ |
| 86 | 'entity_type' => Invoice::class, |
| 87 | 'operation' => InvoiceActivetyLogOperationEnum::UPDATE, |
| 88 | 'user' => $user, |
| 89 | 'detail' => $invoice->toJson(JSON_PRETTY_PRINT), |
| 90 | 'observation' => $invoiceItemsDisagreementDTO->observation |
| 91 | ] |
| 92 | ); |
| 93 | |
| 94 | (new InvoiceActivityLogStoreService())->store($invoiceActivityLogDTO, $invoice, $user); |
| 95 | } |
| 96 | |
| 97 | DB::commit(); |
| 98 | } catch (Throwable $exception) { |
| 99 | DB::rollBack(); |
| 100 | throw $exception; |
| 101 | } |
| 102 | |
| 103 | ServiceProcessed::dispatch(self::class, $user); |
| 104 | } |
| 105 | } |