Create new ParseCurrency helper and use it in the appropriate controllers

This commit is contained in:
Brady Wetherington 2021-09-28 18:20:39 -07:00
parent f380da3f19
commit f3338667c7
10 changed files with 50 additions and 16 deletions

View file

@ -403,6 +403,19 @@ class Helper
*/ */
public static function ParseFloat($floatString) public static function ParseFloat($floatString)
{ {
/*******
*
* WARNING: This does conversions based on *locale* - a Unix-ey-like thing.
*
* Everything else in the system tends to convert based on the Snipe-IT settings
*
* So it's very likely this is *not* what you want - instead look for the new
*
* ParseCurrency($currencyString)
*
* Which should be directly below here
*
*/
$LocaleInfo = localeconv(); $LocaleInfo = localeconv();
$floatString = str_replace(",", "", $floatString); $floatString = str_replace(",", "", $floatString);
$floatString = str_replace($LocaleInfo["decimal_point"], ".", $floatString); $floatString = str_replace($LocaleInfo["decimal_point"], ".", $floatString);
@ -416,6 +429,26 @@ class Helper
$floatString = str_replace($currencySymbol, '', $floatString); $floatString = str_replace($currencySymbol, '', $floatString);
return floatval($floatString); return floatval($floatString);
} }
/**
* Format currency using comma or period for thousands, and period or comma for decimal, based on settings.
*
* @author [B. Wetherington] [<bwetherington@grokability.com>]
* @since [v5.2]
* @return Float
*/
public static function ParseCurrency($currencyString) {
$without_currency = str_replace(Setting::getSettings()->default_currency, '', $currencyString); //generally shouldn't come up, since we don't do this in fields, but just in case it does...
if(Setting::getSettings()->digit_separator=='1.234,56') {
//EU format
$without_thousands = str_replace('.', '', $without_currency);
$corrected_decimal = str_replace(',', '.', $without_thousands);
} else {
$without_thousands = str_replace(',', '', $without_currency);
$corrected_decimal = $without_thousands; // decimal is already OK
}
return floatval($corrected_decimal);
}
/** /**
* Get the list of status labels in an array to make a dropdown menu * Get the list of status labels in an array to make a dropdown menu

View file

@ -74,7 +74,7 @@ class AccessoriesController extends Controller
$accessory->manufacturer_id = request('manufacturer_id'); $accessory->manufacturer_id = request('manufacturer_id');
$accessory->model_number = request('model_number'); $accessory->model_number = request('model_number');
$accessory->purchase_date = request('purchase_date'); $accessory->purchase_date = request('purchase_date');
$accessory->purchase_cost = Helper::ParseFloat(request('purchase_cost')); $accessory->purchase_cost = Helper::ParseCurrency(request('purchase_cost'));
$accessory->qty = request('qty'); $accessory->qty = request('qty');
$accessory->user_id = Auth::user()->id; $accessory->user_id = Auth::user()->id;
$accessory->supplier_id = request('supplier_id'); $accessory->supplier_id = request('supplier_id');
@ -137,7 +137,7 @@ class AccessoriesController extends Controller
$accessory->order_number = request('order_number'); $accessory->order_number = request('order_number');
$accessory->model_number = request('model_number'); $accessory->model_number = request('model_number');
$accessory->purchase_date = request('purchase_date'); $accessory->purchase_date = request('purchase_date');
$accessory->purchase_cost = request('purchase_cost'); $accessory->purchase_cost = Helper::ParseCurrency(request('purchase_cost'));
$accessory->qty = request('qty'); $accessory->qty = request('qty');
$accessory->supplier_id = request('supplier_id'); $accessory->supplier_id = request('supplier_id');

View file

@ -105,7 +105,7 @@ class AssetMaintenancesController extends Controller
$assetMaintenance = new AssetMaintenance(); $assetMaintenance = new AssetMaintenance();
$assetMaintenance->supplier_id = $request->input('supplier_id'); $assetMaintenance->supplier_id = $request->input('supplier_id');
$assetMaintenance->is_warranty = $request->input('is_warranty'); $assetMaintenance->is_warranty = $request->input('is_warranty');
$assetMaintenance->cost = e($request->input('cost')); $assetMaintenance->cost = Helper::ParseCurrency($request->input('cost'));
$assetMaintenance->notes = e($request->input('notes')); $assetMaintenance->notes = e($request->input('notes'));
$asset = Asset::find(e($request->input('asset_id'))); $asset = Asset::find(e($request->input('asset_id')));
@ -162,7 +162,7 @@ class AssetMaintenancesController extends Controller
$assetMaintenance->supplier_id = e($request->input('supplier_id')); $assetMaintenance->supplier_id = e($request->input('supplier_id'));
$assetMaintenance->is_warranty = e($request->input('is_warranty')); $assetMaintenance->is_warranty = e($request->input('is_warranty'));
$assetMaintenance->cost = Helper::ParseFloat(e($request->input('cost'))); $assetMaintenance->cost = Helper::ParseCurrency($request->input('cost'));
$assetMaintenance->notes = e($request->input('notes')); $assetMaintenance->notes = e($request->input('notes'));
$asset = Asset::find(request('asset_id')); $asset = Asset::find(request('asset_id'));

View file

@ -493,7 +493,7 @@ class AssetsController extends Controller
$asset->depreciate = '0'; $asset->depreciate = '0';
$asset->status_id = $request->get('status_id', 0); $asset->status_id = $request->get('status_id', 0);
$asset->warranty_months = $request->get('warranty_months', null); $asset->warranty_months = $request->get('warranty_months', null);
$asset->purchase_cost = Helper::ParseFloat($request->get('purchase_cost')); $asset->purchase_cost = Helper::ParseCurrency($request->get('purchase_cost')); // this is the API's store method, so I don't know that I want to do this? Confusing. FIXME (or not?!)
$asset->purchase_date = $request->get('purchase_date', null); $asset->purchase_date = $request->get('purchase_date', null);
$asset->assigned_to = $request->get('assigned_to', null); $asset->assigned_to = $request->get('assigned_to', null);
$asset->supplier_id = $request->get('supplier_id', 0); $asset->supplier_id = $request->get('supplier_id', 0);

View file

@ -100,7 +100,7 @@ class AssetMaintenancesController extends Controller
$assetMaintenance = new AssetMaintenance(); $assetMaintenance = new AssetMaintenance();
$assetMaintenance->supplier_id = $request->input('supplier_id'); $assetMaintenance->supplier_id = $request->input('supplier_id');
$assetMaintenance->is_warranty = $request->input('is_warranty'); $assetMaintenance->is_warranty = $request->input('is_warranty');
$assetMaintenance->cost = $request->input('cost'); $assetMaintenance->cost = Helper::ParseCurrency($request->input('cost'));
$assetMaintenance->notes = $request->input('notes'); $assetMaintenance->notes = $request->input('notes');
$asset = Asset::find($request->input('asset_id')); $asset = Asset::find($request->input('asset_id'));
@ -211,7 +211,7 @@ class AssetMaintenancesController extends Controller
$assetMaintenance->supplier_id = $request->input('supplier_id'); $assetMaintenance->supplier_id = $request->input('supplier_id');
$assetMaintenance->is_warranty = $request->input('is_warranty'); $assetMaintenance->is_warranty = $request->input('is_warranty');
$assetMaintenance->cost = Helper::ParseFloat($request->input('cost')); $assetMaintenance->cost = Helper::ParseCurrency($request->input('cost'));
$assetMaintenance->notes = $request->input('notes'); $assetMaintenance->notes = $request->input('notes');
$asset = Asset::find(request('asset_id')); $asset = Asset::find(request('asset_id'));

View file

@ -138,7 +138,7 @@ class AssetsController extends Controller
$asset->depreciate = '0'; $asset->depreciate = '0';
$asset->status_id = request('status_id', 0); $asset->status_id = request('status_id', 0);
$asset->warranty_months = request('warranty_months', null); $asset->warranty_months = request('warranty_months', null);
$asset->purchase_cost = Helper::ParseFloat($request->get('purchase_cost')); $asset->purchase_cost = Helper::ParseCurrency($request->get('purchase_cost'));
$asset->purchase_date = request('purchase_date', null); $asset->purchase_date = request('purchase_date', null);
$asset->assigned_to = request('assigned_to', null); $asset->assigned_to = request('assigned_to', null);
$asset->supplier_id = request('supplier_id', 0); $asset->supplier_id = request('supplier_id', 0);
@ -302,7 +302,7 @@ class AssetsController extends Controller
$asset->status_id = $request->input('status_id', null); $asset->status_id = $request->input('status_id', null);
$asset->warranty_months = $request->input('warranty_months', null); $asset->warranty_months = $request->input('warranty_months', null);
$asset->purchase_cost = Helper::ParseFloat($request->input('purchase_cost', null)); $asset->purchase_cost = Helper::ParseCurrency($request->input('purchase_cost', null));
$asset->purchase_date = $request->input('purchase_date', null); $asset->purchase_date = $request->input('purchase_date', null);
$asset->supplier_id = $request->input('supplier_id', null); $asset->supplier_id = $request->input('supplier_id', null);
$asset->expected_checkin = $request->input('expected_checkin', null); $asset->expected_checkin = $request->input('expected_checkin', null);

View file

@ -102,7 +102,7 @@ class BulkAssetsController extends Controller
->conditionallyAddItem('warranty_months'); ->conditionallyAddItem('warranty_months');
if ($request->filled('purchase_cost')) { if ($request->filled('purchase_cost')) {
$this->update_array['purchase_cost'] = Helper::ParseFloat($request->input('purchase_cost')); $this->update_array['purchase_cost'] = Helper::ParseCurrency($request->input('purchase_cost'));
} }
if ($request->filled('company_id')) { if ($request->filled('company_id')) {

View file

@ -5,6 +5,7 @@ use App\Http\Controllers\Controller;
use App\Http\Requests\ImageUploadRequest; use App\Http\Requests\ImageUploadRequest;
use App\Models\Company; use App\Models\Company;
use App\Models\Component; use App\Models\Component;
use App\Helpers\Helper;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input; use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
@ -74,7 +75,7 @@ class ComponentsController extends Controller
$component->min_amt = $request->input('min_amt', null); $component->min_amt = $request->input('min_amt', null);
$component->serial = $request->input('serial', null); $component->serial = $request->input('serial', null);
$component->purchase_date = $request->input('purchase_date', null); $component->purchase_date = $request->input('purchase_date', null);
$component->purchase_cost = $request->input('purchase_cost', null); $component->purchase_cost = Helper::ParseCurrency($request->input('purchase_cost', null));
$component->qty = $request->input('qty'); $component->qty = $request->input('qty');
$component->user_id = Auth::id(); $component->user_id = Auth::id();
@ -144,7 +145,7 @@ class ComponentsController extends Controller
$component->min_amt = $request->input('min_amt'); $component->min_amt = $request->input('min_amt');
$component->serial = $request->input('serial'); $component->serial = $request->input('serial');
$component->purchase_date = $request->input('purchase_date'); $component->purchase_date = $request->input('purchase_date');
$component->purchase_cost = request('purchase_cost'); $component->purchase_cost = Helper::ParseCurrency(request('purchase_cost'));
$component->qty = $request->input('qty'); $component->qty = $request->input('qty');
$component = $request->handleImages($component); $component = $request->handleImages($component);

View file

@ -75,7 +75,7 @@ class ConsumablesController extends Controller
$consumable->model_number = $request->input('model_number'); $consumable->model_number = $request->input('model_number');
$consumable->item_no = $request->input('item_no'); $consumable->item_no = $request->input('item_no');
$consumable->purchase_date = $request->input('purchase_date'); $consumable->purchase_date = $request->input('purchase_date');
$consumable->purchase_cost = Helper::ParseFloat($request->input('purchase_cost')); $consumable->purchase_cost = Helper::ParseCurrency($request->input('purchase_cost'));
$consumable->qty = $request->input('qty'); $consumable->qty = $request->input('qty');
$consumable->user_id = Auth::id(); $consumable->user_id = Auth::id();
@ -141,7 +141,7 @@ class ConsumablesController extends Controller
$consumable->model_number = $request->input('model_number'); $consumable->model_number = $request->input('model_number');
$consumable->item_no = $request->input('item_no'); $consumable->item_no = $request->input('item_no');
$consumable->purchase_date = $request->input('purchase_date'); $consumable->purchase_date = $request->input('purchase_date');
$consumable->purchase_cost = Helper::ParseFloat($request->input('purchase_cost')); $consumable->purchase_cost = Helper::ParseCurrency($request->input('purchase_cost'));
$consumable->qty = Helper::ParseFloat($request->input('qty')); $consumable->qty = Helper::ParseFloat($request->input('qty'));
$consumable = $request->handleImages($consumable); $consumable = $request->handleImages($consumable);

View file

@ -88,7 +88,7 @@ class LicensesController extends Controller
$license->name = $request->input('name'); $license->name = $request->input('name');
$license->notes = $request->input('notes'); $license->notes = $request->input('notes');
$license->order_number = $request->input('order_number'); $license->order_number = $request->input('order_number');
$license->purchase_cost = $request->input('purchase_cost'); $license->purchase_cost = Helper::ParseCurrency($request->input('purchase_cost'));
$license->purchase_date = $request->input('purchase_date'); $license->purchase_date = $request->input('purchase_date');
$license->purchase_order = $request->input('purchase_order'); $license->purchase_order = $request->input('purchase_order');
$license->purchase_order = $request->input('purchase_order'); $license->purchase_order = $request->input('purchase_order');
@ -165,7 +165,7 @@ class LicensesController extends Controller
$license->name = $request->input('name'); $license->name = $request->input('name');
$license->notes = $request->input('notes'); $license->notes = $request->input('notes');
$license->order_number = $request->input('order_number'); $license->order_number = $request->input('order_number');
$license->purchase_cost = $request->input('purchase_cost'); $license->purchase_cost = Helper::ParseCurrency($request->input('purchase_cost'));
$license->purchase_date = $request->input('purchase_date'); $license->purchase_date = $request->input('purchase_date');
$license->purchase_order = $request->input('purchase_order'); $license->purchase_order = $request->input('purchase_order');
$license->reassignable = $request->input('reassignable', 0); $license->reassignable = $request->input('reassignable', 0);