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]); } } 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();