mirror of
https://github.com/snipe/snipe-it.git
synced 2024-12-24 13:14:07 -08:00
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:
commit
19cff25300
|
@ -4,6 +4,7 @@ namespace App\Http\Requests;
|
|||
|
||||
use App\Models\Asset;
|
||||
use App\Models\Company;
|
||||
use App\Models\Setting;
|
||||
use Carbon\Carbon;
|
||||
use Carbon\Exceptions\InvalidFormatException;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
|
@ -45,12 +46,21 @@ class StoreAssetRequest extends ImageUploadRequest
|
|||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
$rules = array_merge(
|
||||
(new Asset)->getRules(),
|
||||
$modelRules = (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(),
|
||||
);
|
||||
|
||||
return $rules;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,50 @@ class AssetStoreTest extends TestCase
|
|||
->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()
|
||||
{
|
||||
$model = AssetModel::factory()->create();
|
||||
|
|
|
@ -16,4 +16,13 @@ class HelperTest extends TestCase
|
|||
{
|
||||
$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'));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue