WIP: allow EU style purchase cost via api

This commit is contained in:
Marcus Moore 2024-04-24 17:18:29 -07:00
parent 783a24eb68
commit 12418ae91b
No known key found for this signature in database
2 changed files with 50 additions and 1 deletions

View file

@ -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(),
);

View file

@ -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();