diff --git a/app/Http/Requests/StoreAssetRequest.php b/app/Http/Requests/StoreAssetRequest.php index 8e7559673e..ff2cf7f2b3 100644 --- a/app/Http/Requests/StoreAssetRequest.php +++ b/app/Http/Requests/StoreAssetRequest.php @@ -45,8 +45,14 @@ class StoreAssetRequest extends ImageUploadRequest */ public function rules(): array { + $modelRules = (new Asset)->getRules(); + + $modelRules['purchase_cost'] = array_filter(explode('|', $modelRules['purchase_cost']), function ($rule) { + return $rule !== 'numeric' && $rule !== 'gte:0'; + }); + $rules = array_merge( - (new Asset)->getRules(), + $modelRules, parent::rules(), ); diff --git a/tests/Feature/Api/Assets/AssetStoreTest.php b/tests/Feature/Api/Assets/AssetStoreTest.php index c874745510..790c1a97f1 100644 --- a/tests/Feature/Api/Assets/AssetStoreTest.php +++ b/tests/Feature/Api/Assets/AssetStoreTest.php @@ -2,6 +2,7 @@ namespace Tests\Feature\Api\Assets; +use App\Helpers\Helper; use App\Models\Asset; use App\Models\AssetModel; use App\Models\Company; @@ -12,6 +13,7 @@ use App\Models\Supplier; use App\Models\User; use Illuminate\Support\Facades\Crypt; use Illuminate\Testing\Fluent\AssertableJson; +use Illuminate\Testing\TestResponse; use Tests\TestCase; class AssetStoreTest extends TestCase @@ -278,6 +280,47 @@ class AssetStoreTest extends TestCase ->assertStatusMessageIs('error'); } + private function purchaseCosts(): array + { + return [ + 'with decimal' => [ + [ + 'separator' => '1.234.56', + 'input' => 12.34, + 'expectation' => 12.34 + ] + ], + 'EU style' => [ + [ + 'separator' => '1.234,56', + 'input' => "12,34", + 'expectation' => 12.34 + ] + ], + ]; + } + + /** + * @dataProvider purchaseCosts + */ + public function testPurchaseCost($costs) + { + $this->settings->set(['digit_separator' => $costs['separator']]); + + $response = $this->actingAsForApi(User::factory()->superuser()->create()) + ->postJson(route('api.assets.store'), [ + 'asset_tag' => 'random-string', + 'model_id' => AssetModel::factory()->create()->id, + 'status_id' => Statuslabel::factory()->create()->id, + 'purchase_cost' => $costs['input'], + ]) + ->assertStatusMessageIs('success'); + + $asset = Asset::find($response['payload']['id']); + + $this->assertEquals($costs['expectation'], $asset->purchase_cost); + } + public function testUniqueSerialNumbersIsEnforcedWhenEnabled() { $model = AssetModel::factory()->create();