Merge pull request #14651 from marcusmoore/bug/sc-25402

Fixed `purchase_cost` not being allowed to be a string when creating asset via api
This commit is contained in:
snipe 2024-04-27 03:39:10 +01:00 committed by GitHub
commit 19cff25300
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 85 additions and 4 deletions

View file

@ -4,6 +4,7 @@ namespace App\Http\Requests;
use App\Models\Asset; use App\Models\Asset;
use App\Models\Company; use App\Models\Company;
use App\Models\Setting;
use Carbon\Carbon; use Carbon\Carbon;
use Carbon\Exceptions\InvalidFormatException; use Carbon\Exceptions\InvalidFormatException;
use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Gate;
@ -45,12 +46,21 @@ class StoreAssetRequest extends ImageUploadRequest
*/ */
public function rules(): array public function rules(): array
{ {
$rules = array_merge( $modelRules = (new Asset)->getRules();
(new Asset)->getRules(),
if (Setting::getSettings()->digit_separator === '1.234,56' && is_string($this->input('purchase_cost'))) {
// If purchase_cost was submitted as a string with a comma separator
// then we need to ignore the normal numeric rules.
// Since the original rules still live on the model they will be run
// right before saving (and after purchase_cost has been
// converted to a float via setPurchaseCostAttribute).
$modelRules = $this->removeNumericRulesFromPurchaseCost($modelRules);
}
return array_merge(
$modelRules,
parent::rules(), parent::rules(),
); );
return $rules;
} }
private function parseLastAuditDate(): void private function parseLastAuditDate(): void
@ -69,4 +79,20 @@ class StoreAssetRequest extends ImageUploadRequest
} }
} }
} }
private function removeNumericRulesFromPurchaseCost(array $rules): array
{
$purchaseCost = $rules['purchase_cost'];
// If rule is in "|" format then turn it into an array
if (is_string($purchaseCost)) {
$purchaseCost = explode('|', $purchaseCost);
}
$rules['purchase_cost'] = array_filter($purchaseCost, function ($rule) {
return $rule !== 'numeric' && $rule !== 'gte:0';
});
return $rules;
}
} }

View file

@ -2,6 +2,7 @@
namespace Tests\Feature\Api\Assets; namespace Tests\Feature\Api\Assets;
use App\Helpers\Helper;
use App\Models\Asset; use App\Models\Asset;
use App\Models\AssetModel; use App\Models\AssetModel;
use App\Models\Company; use App\Models\Company;
@ -12,6 +13,7 @@ use App\Models\Supplier;
use App\Models\User; use App\Models\User;
use Illuminate\Support\Facades\Crypt; use Illuminate\Support\Facades\Crypt;
use Illuminate\Testing\Fluent\AssertableJson; use Illuminate\Testing\Fluent\AssertableJson;
use Illuminate\Testing\TestResponse;
use Tests\TestCase; use Tests\TestCase;
class AssetStoreTest extends TestCase class AssetStoreTest extends TestCase
@ -278,6 +280,50 @@ class AssetStoreTest extends TestCase
->assertStatusMessageIs('error'); ->assertStatusMessageIs('error');
} }
public function testStoresPeriodAsDecimalSeparatorForPurchaseCost()
{
$this->settings->set([
'default_currency' => 'USD',
'digit_separator' => '1,234.56',
]);
$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,
// API accepts float
'purchase_cost' => 12.34,
])
->assertStatusMessageIs('success');
$asset = Asset::find($response['payload']['id']);
$this->assertEquals(12.34, $asset->purchase_cost);
}
public function testStoresPeriodAsCommaSeparatorForPurchaseCost()
{
$this->settings->set([
'default_currency' => 'EUR',
'digit_separator' => '1.234,56',
]);
$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,
// API also accepts string for comma separated values
'purchase_cost' => '12,34',
])
->assertStatusMessageIs('success');
$asset = Asset::find($response['payload']['id']);
$this->assertEquals(12.34, $asset->purchase_cost);
}
public function testUniqueSerialNumbersIsEnforcedWhenEnabled() public function testUniqueSerialNumbersIsEnforcedWhenEnabled()
{ {
$model = AssetModel::factory()->create(); $model = AssetModel::factory()->create();

View file

@ -16,4 +16,13 @@ class HelperTest extends TestCase
{ {
$this->assertIsString(Helper::defaultChartColors(-1)); $this->assertIsString(Helper::defaultChartColors(-1));
} }
public function testParseCurrencyMethod()
{
$this->settings->set(['default_currency' => 'USD']);
$this->assertSame(12.34, Helper::ParseCurrency('USD 12.34'));
$this->settings->set(['digit_separator' => '1.234,56']);
$this->assertSame(12.34, Helper::ParseCurrency('12,34'));
}
} }