From 2810423a208577e07440813548d26d12816c7ced Mon Sep 17 00:00:00 2001 From: Sevan Nerse Date: Thu, 16 Dec 2021 12:37:44 +0300 Subject: [PATCH 1/2] negative quantity not allowed #1xphe64 --- app/Http/Requests/Document/Document.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/Http/Requests/Document/Document.php b/app/Http/Requests/Document/Document.php index d67ba226a..89a0cdc99 100644 --- a/app/Http/Requests/Document/Document.php +++ b/app/Http/Requests/Document/Document.php @@ -75,7 +75,7 @@ class Document extends FormRequest $size = 7; } - $rules['items.' . $key . '.quantity'] = 'required|max:' . $size; + $rules['items.' . $key . '.quantity'] = 'required|max:' . $size . '|numeric|gt:0'; $this->items_quantity_size[$key] = $size; } } @@ -108,6 +108,7 @@ class Document extends FormRequest if ($this->items_quantity_size) { foreach ($this->items_quantity_size as $key => $quantity_size) { $messages['items.' . $key . '.quantity.max'] = trans('validation.size', ['attribute' => Str::lower(trans('invoices.quantity')), 'size' => $quantity_size]); + $messages['items.' . $key . '.quantity.gt'] = trans('validation.gt.numeric', ['attribute' => Str::lower(trans('invoices.quantity')), 'value' => 0]); } } From 87668494269127a2b388a6d1767efd5954e2ef00 Mon Sep 17 00:00:00 2001 From: Sevan Nerse Date: Thu, 16 Dec 2021 17:34:22 +0300 Subject: [PATCH 2/2] test added #1xphe64 --- tests/Feature/Purchases/BillsTest.php | 18 ++++++++++++++++++ tests/Feature/Sales/InvoicesTest.php | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/tests/Feature/Purchases/BillsTest.php b/tests/Feature/Purchases/BillsTest.php index 279c58320..68104ecac 100644 --- a/tests/Feature/Purchases/BillsTest.php +++ b/tests/Feature/Purchases/BillsTest.php @@ -112,6 +112,24 @@ class BillsTest extends FeatureTestCase ]); } + public function testItShouldNotCreateBillWithNegativeQuantity() + { + $request = $this->getRequest(); + + $request['items'][0]['quantity'] = '-1'; + + $this->handleValidationExceptions(); + + $this->loginAs() + ->post(route('bills.store'), $request) + ->assertRedirect() + ->assertInvalid(['items.0.quantity']); + + $this->assertDatabaseMissing('documents', [ + 'document_number' => $request['document_number'], + ]); + } + public function testItShouldSeeBillUpdatePage() { $request = $this->getRequest(); diff --git a/tests/Feature/Sales/InvoicesTest.php b/tests/Feature/Sales/InvoicesTest.php index c5a1a439d..e645e483c 100644 --- a/tests/Feature/Sales/InvoicesTest.php +++ b/tests/Feature/Sales/InvoicesTest.php @@ -123,6 +123,24 @@ class InvoicesTest extends FeatureTestCase ]); } + public function testItShouldNotCreateInvoiceWithNegativeQuantity() + { + $request = $this->getRequest(); + + $request['items'][0]['quantity'] = '-1'; + + $this->handleValidationExceptions(); + + $this->loginAs() + ->post(route('invoices.store'), $request) + ->assertRedirect() + ->assertInvalid(['items.0.quantity']); + + $this->assertDatabaseMissing('documents', [ + 'document_number' => $request['document_number'], + ]); + } + public function testItShouldSeeInvoiceUpdatePage() { $request = $this->getRequest();