Skip to content

A free open source IT asset/license management system

Added currency helper + dropdown

snipe4eda70e88c8f
diff --git a/app/Helpers/CurrencyHelper.php b/app/Helpers/CurrencyHelper.php
new file mode 100644
index 0000000000..389a1d0971
--- /dev/null
+++ b/app/Helpers/CurrencyHelper.php
@@ -0,0 +1,370 @@
+<?php
+
+namespace App\Helpers;
+
+/**
+ * ISO 4217 currency code list and symbol lookups.
+ *
+ * Codes are locale-invariant and live here, not in lang files. Only
+ * widely-recognized symbols are mapped. Codes without a known symbol
+ * return null and callers should fall back to the ISO code itself
+ * for display.
+ */
+class CurrencyHelper
+{
+ /**
+ * Every active ISO 4217 code the dropdown offers. Includes codes
+ * that have no widely-recognized symbol. The symbol lookup falls
+ * back to the code itself for those.
+ *
+ * @var array<int, string>
+ */
+ private const CODES = [
+ 'AED',
+ 'AFN',
+ 'ALL',
+ 'AMD',
+ 'ANG',
+ 'AOA',
+ 'ARS',
+ 'AUD',
+ 'AWG',
+ 'AZN',
+ 'BAM',
+ 'BBD',
+ 'BDT',
+ 'BGN',
+ 'BHD',
+ 'BIF',
+ 'BMD',
+ 'BND',
+ 'BOB',
+ 'BRL',
+ 'BSD',
+ 'BTN',
+ 'BWP',
+ 'BYN',
+ 'BZD',
+ 'CAD',
+ 'CDF',
+ 'CHF',
+ 'CLP',
+ 'CNY',
+ 'COP',
+ 'CRC',
+ 'CUC',
+ 'CUP',
+ 'CVE',
+ 'CZK',
+ 'DJF',
+ 'DKK',
+ 'DOP',
+ 'DZD',
+ 'EGP',
+ 'ERN',
+ 'ETB',
+ 'EUR',
+ 'FJD',
+ 'FKP',
+ 'GBP',
+ 'GEL',
+ 'GHS',
+ 'GIP',
+ 'GMD',
+ 'GNF',
+ 'GTQ',
+ 'GYD',
+ 'HKD',
+ 'HNL',
+ 'HTG',
+ 'HUF',
+ 'IDR',
+ 'ILS',
+ 'INR',
+ 'IQD',
+ 'IRR',
+ 'ISK',
+ 'JMD',
+ 'JOD',
+ 'JPY',
+ 'KES',
+ 'KGS',
+ 'KHR',
+ 'KMF',
+ 'KPW',
+ 'KRW',
+ 'KWD',
+ 'KYD',
+ 'KZT',
+ 'LAK',
+ 'LBP',
+ 'LKR',
+ 'LRD',
+ 'LSL',
+ 'LYD',
+ 'MAD',
+ 'MDL',
+ 'MGA',
+ 'MKD',
+ 'MMK',
+ 'MNT',
+ 'MOP',
+ 'MRU',
+ 'MUR',
+ 'MVR',
+ 'MWK',
+ 'MXN',
+ 'MYR',
+ 'MZN',
+ 'NAD',
+ 'NGN',
+ 'NIO',
+ 'NOK',
+ 'NPR',
+ 'NZD',
+ 'OMR',
+ 'PAB',
+ 'PEN',
+ 'PGK',
+ 'PHP',
+ 'PKR',
+ 'PLN',
+ 'PYG',
+ 'QAR',
+ 'RON',
+ 'RSD',
+ 'RUB',
+ 'RWF',
+ 'SAR',
+ 'SBD',
+ 'SCR',
+ 'SDG',
+ 'SEK',
+ 'SGD',
+ 'SHP',
+ 'SLE',
+ 'SOS',
+ 'SRD',
+ 'SSP',
+ 'STN',
+ 'SYP',
+ 'SZL',
+ 'THB',
+ 'TJS',
+ 'TMT',
+ 'TND',
+ 'TOP',
+ 'TRY',
+ 'TTD',
+ 'TWD',
+ 'TZS',
+ 'UAH',
+ 'UGX',
+ 'USD',
+ 'UYU',
+ 'UZS',
+ 'VES',
+ 'VND',
+ 'VUV',
+ 'WST',
+ 'XAF',
+ 'XCD',
+ 'XOF',
+ 'XPF',
+ 'YER',
+ 'ZAR',
+ 'ZMW',
+ 'ZWL',
+ ];
+
+ /**
+ * Code → symbol map. Only widely-recognized single-character or
+ * short symbols are included. Obscure codes return null and
+ * callers should fall back to the ISO code in display.
+ *
+ * @var array<string, string>
+ */
+ private const SYMBOLS = [
+ 'AED' => 'د.إ',
+ 'AFN' => '؋',
+ 'ALL' => 'L',
+ 'AMD' => '֏',
+ 'ANG' => 'ƒ',
+ 'ARS' => '$',
+ 'AUD' => 'A$',
+ 'AWG' => 'ƒ',
+ 'AZN' => '₼',
+ 'BAM' => 'KM',
+ 'BBD' => 'Bds$',
+ 'BDT' => '৳',
+ 'BGN' => 'лв',
+ 'BHD' => '.د.ب',
+ 'BMD' => 'BD$',
+ 'BND' => 'B$',
+ 'BOB' => 'Bs.',
+ 'BRL' => 'R$',
+ 'BSD' => 'B$',
+ 'BTN' => 'Nu.',
+ 'BWP' => 'P',
+ 'BYN' => 'Br',
+ 'BZD' => 'BZ$',
+ 'CAD' => 'C$',
+ 'CHF' => 'CHF',
+ 'CLP' => '$',
+ 'CNY' => '¥',
+ 'COP' => '$',
+ 'CRC' => '₡',
+ 'CUC' => '$',
+ 'CUP' => '₱',
+ 'CVE' => '$',
+ 'CZK' => 'Kč',
+ 'DKK' => 'kr',
+ 'DOP' => 'RD$',
+ 'DZD' => 'د.ج',
+ 'EGP' => '£',
+ 'ERN' => 'Nfk',
+ 'ETB' => 'Br',
+ 'EUR' => '€',
+ 'FJD' => 'FJ$',
+ 'FKP' => '£',
+ 'GBP' => '£',
+ 'GEL' => '₾',
+ 'GHS' => '₵',
+ 'GIP' => '£',
+ 'GMD' => 'D',
+ 'GTQ' => 'Q',
+ 'GYD' => 'G$',
+ 'HKD' => 'HK$',
+ 'HNL' => 'L',
+ 'HTG' => 'G',
+ 'HUF' => 'Ft',
+ 'IDR' => 'Rp',
+ 'ILS' => '₪',
+ 'INR' => '₹',
+ 'IQD' => 'ع.د',
+ 'IRR' => '﷼',
+ 'ISK' => 'kr',
+ 'JMD' => 'J$',
+ 'JOD' => 'د.ا',
+ 'JPY' => '¥',
+ 'KES' => 'KSh',
+ 'KGS' => 'с',
+ 'KHR' => '៛',
+ 'KPW' => '₩',
+ 'KRW' => '₩',
+ 'KWD' => 'د.ك',
+ 'KYD' => 'CI$',
+ 'KZT' => '₸',
+ 'LAK' => '₭',
+ 'LBP' => 'ل.ل',
+ 'LKR' => 'Rs',
+ 'LRD' => 'L$',
+ 'LSL' => 'L',
+ 'LYD' => 'ل.د',
+ 'MAD' => 'د.م.',
+ 'MDL' => 'L',
+ 'MKD' => 'ден',
+ 'MMK' => 'K',
+ 'MNT' => '₮',
+ 'MOP' => 'MOP$',
+ 'MUR' => '₨',
+ 'MVR' => 'Rf',
+ 'MWK' => 'MK',
+ 'MXN' => '$',
+ 'MYR' => 'RM',
+ 'MZN' => 'MT',
+ 'NAD' => 'N$',
+ 'NGN' => '₦',
+ 'NIO' => 'C$',
+ 'NOK' => 'kr',
+ 'NPR' => '₨',
+ 'NZD' => 'NZ$',
+ 'OMR' => 'ر.ع.',
+ 'PAB' => 'B/.',
+ 'PEN' => 'S/',
+ 'PGK' => 'K',
+ 'PHP' => '₱',
+ 'PKR' => '₨',
+ 'PLN' => 'zł',
+ 'PYG' => '₲',
+ 'QAR' => 'ر.ق',
+ 'RON' => 'lei',
+ 'RSD' => 'дин.',
+ 'RUB' => '₽',
+ 'SAR' => 'ر.س',
+ 'SBD' => 'SI$',
+ 'SCR' => '₨',
+ 'SDG' => 'ج.س.',
+ 'SEK' => 'kr',
+ 'SGD' => 'S$',
+ 'SHP' => '£',
+ 'SOS' => 'Sh',
+ 'SRD' => 'Sr$',
+ 'SSP' => 'SS£',
+ 'SYP' => '£S',
+ 'SZL' => 'E',
+ 'THB' => '฿',
+ 'TJS' => 'ЅМ',
+ 'TMT' => 'm',
+ 'TND' => 'د.ت',
+ 'TOP' => 'T$',
+ 'TRY' => '₺',
+ 'TTD' => 'TT$',
+ 'TWD' => 'NT$',
+ 'TZS' => 'TSh',
+ 'UAH' => '₴',
+ 'UGX' => 'USh',
+ 'USD' => '$',
+ 'UYU' => '$U',
+ 'UZS' => "so'm",
+ 'VES' => 'Bs.S',
+ 'VND' => '₫',
+ 'XAF' => 'FCFA',
+ 'XCD' => 'EC$',
+ 'XOF' => 'CFA',
+ 'XPF' => '₣',
+ 'YER' => '﷼',
+ 'ZAR' => 'R',
+ 'ZMW' => 'ZK',
+ ];
+
+ /**
+ * Every ISO 4217 code the picker offers, in alphabetical order.
+ *
+ * @return array<int, string>
+ */
+ public static function codes(): array
+ {
+ return self::CODES;
+ }
+
+ /**
+ * Resolve a currency code's symbol, or null when there's no widely-
+ * recognized symbol for that code. Codes are matched case-
+ * insensitively against the standard three-letter ISO 4217 form so
+ * stored lowercase values still resolve.
+ */
+ public static function symbol(?string $code): ?string
+ {
+ if ($code === null || $code === '') {
+ return null;
+ }
+
+ return self::SYMBOLS[strtoupper($code)] ?? null;
+ }
+
+ /**
+ * Shortest display form for a currency. Returns its symbol when
+ * available, otherwise the ISO code itself. Useful for inline
+ * cost strings where a column of "$" aligns better than "USD".
+ */
+ public static function displayLabel(?string $code): string
+ {
+ $code = $code === null ? '' : trim($code);
+ if ($code === '') {
+ return '';
+ }
+
+ return self::symbol($code) ?? strtoupper($code);
+ }
+}
diff --git a/resources/lang/en-US/general.php b/resources/lang/en-US/general.php
index 5c364d7356..8e10f0b6e6 100644
--- a/resources/lang/en-US/general.php
+++ b/resources/lang/en-US/general.php
@@ -773,6 +773,7 @@ return [
'label' => 'Label',
'import_asset_tag_exists' => 'An asset with the asset tag :asset_tag already exists and an update was not requested. No change was made.',
'countries_manually_entered_help' => 'Values with an asterisk (*) were manually entered and do not match existing ISO 3166 dropdown values',
+ 'currency_manually_entered_help' => 'Values with an asterisk (*) were manually entered and do not match existing ISO 4217 dropdown values',
'accessories_assigned' => 'Assigned Accessories',
'user_managed_passwords' => 'Password Management',
'user_managed_passwords_disallow' => 'Disallow users from managing their own passwords',
diff --git a/resources/lang/en-US/localizations.php b/resources/lang/en-US/localizations.php
index ebf89d75e0..c13f0f9ae2 100644
--- a/resources/lang/en-US/localizations.php
+++ b/resources/lang/en-US/localizations.php
@@ -70,6 +70,7 @@ return [
],
'select_country' => 'Select a Country',
+ 'select_currency' => 'Select a currency',
'countries' => [
'AC' => 'Ascension Island',
diff --git a/resources/views/blade/form/row.blade.php b/resources/views/blade/form/row.blade.php
index 8dc14b5a64..8f137930f4 100644
--- a/resources/views/blade/form/row.blade.php
+++ b/resources/views/blade/form/row.blade.php
@@ -10,7 +10,7 @@
'label' => null,
'label_class' => 'col-md-3',
'input_div_class' => 'col-md-7',
- 'help_class' => 'col-md-9 col-md-offset-3',
+ 'help_class' => null,
'input_icon' => null,
'input_group_addon' => null,
'maxlength' => null,
@@ -136,14 +136,16 @@
<div class="clearfix"></div>
@endif
- {{-- Error + help block width matches the input's width so long
- lines wrap under the input rather than spilling past into
- the empty column beyond it. Uses $input_div_class (default
- col-md-7) plus the label's col-md-offset-3 so it aligns
- under the input on horizontal-form rows. Stacked layouts
- (mobile / modal) drop the offset because they're single-
- column. --}}
- <div @class([$input_div_class.' col-md-offset-3' => ! $stacked])>
+ {{-- Error + help block width matches the input's width by
+ default so long lines wrap under the input rather than
+ spilling past into the empty column beyond it. For narrow
+ inputs where the matching help column makes the hint
+ unreadable (e.g. a col-md-3 select with a sentence-long
+ help string), callers can pass an explicit `help_class`
+ like "col-md-9 col-md-offset-3" to widen just the help
+ block. Stacked layouts (mobile / modal) drop the offset
+ because they're single-column. --}}
+ <div @class([($help_class ?? $input_div_class.' col-md-offset-3') => ! $stacked])>
<x-form.error :name="$name" />
@if ($help_text)
diff --git a/resources/views/blade/input/currency-select.blade.php b/resources/views/blade/input/currency-select.blade.php
new file mode 100644
index 0000000000..be7c4f0816
--- /dev/null
+++ b/resources/views/blade/input/currency-select.blade.php
@@ -0,0 +1,34 @@
+@use('App\Helpers\CurrencyHelper')
+@props([
+ 'name' => 'currency',
+ 'selected' => null,
+])
+
+@php
+ $codes = CurrencyHelper::codes();
+@endphp
+
+<select
+ name="{{ $name }}"
+ {{ $attributes->merge(['class' => 'select2', 'style' => 'width: 100%']) }}
+ aria-label="{{ $name }}"
+ data-placeholder="{{ trans('localizations.select_currency') }}"
+ data-allow-clear="true"
+>
+ <option value=""></option>
+ @foreach($codes as $code)
+ @php
+ $symbol = CurrencyHelper::symbol($code);
+ $label = $symbol ? $code.' ('.$symbol.')' : $code;
+ @endphp
+ <option value="{{ $code }}" {{ $selected === $code ? 'selected' : '' }}>
+ {{ $label }}
+ </option>
+ @endforeach
+
+ {{-- Preserve any pre-existing non-ISO value so round-trips don't
+ wipe custom entries like a bare symbol or vendor label. --}}
+ @if ($selected !== null && $selected !== '' && ! in_array($selected, $codes, true))
+ <option value="{{ e($selected) }}" selected>{{ e($selected) }} *</option>
+ @endif
+</select>
diff --git a/resources/views/components/edit.blade.php b/resources/views/components/edit.blade.php
index 499b82ad64..14af7b6b4f 100644
--- a/resources/views/components/edit.blade.php
+++ b/resources/views/components/edit.blade.php
@@ -42,6 +42,10 @@
categoryType="component"
/>
+ @if (! $item->id)
+ <x-input.quantity :item="$item"/>
+ @endif
+
<x-input.minimum-quantity :item="$item" />
<x-form.row
@@ -100,25 +104,19 @@
/>
<x-form.row
- :label="trans('general.currency')"
+ :label="trans('general.currency_text')"
name="currency"
input_div_class="col-md-3"
- :help_text="trans('general.currency_prefilled_hint')"
+ help_class="col-md-7 col-md-offset-3"
+ :help_text="trans('general.currency_prefilled_hint').' '.trans('general.currency_manually_entered_help')"
>
<x-slot:input>
- <input
- type="text"
- class="form-control"
- id="currency"
+ <x-input.currency-select
name="currency"
- maxlength="10"
- value="{{ old('currency', $item->location->currency ?? $snipeSettings->default_currency) }}"
- aria-label="{{ trans('general.currency') }}"
+ :selected="old('currency', $item->location->currency ?? $snipeSettings->default_currency)"
/>
</x-slot:input>
</x-form.row>
-
- <x-input.quantity :item="$item" />
@endif
<x-input.supplier-select