Fix more number_format madness.

This does two main things:
1) The importer now imports as numbers, not parsed strings.  This allows
is to format values on output instead of input, which is what was
happening in most places.

2) Add a Helper::parseCurrencyString method and port everything to use
this.  This checks to see if the value is numeric or empty, and returns
the appropriate value in all cases.  Should fix all known occurances of
number_format expections.
This commit is contained in:
Daniel Meltzer 2016-08-15 21:57:34 -05:00
parent 44821b9667
commit 0c912bcf49
20 changed files with 51 additions and 43 deletions

View file

@ -43,6 +43,17 @@ class Helper
return array_walk($emails_array, 'trim_value'); return array_walk($emails_array, 'trim_value');
} }
public static function parseCurrencyString($cost)
{
// The importer has formatted number strings since v3, so the value might be a string, or an integer.
// If it's a number, format it as a string
if (is_numeric($cost)) {
return number_format($cost, 2, '.', '');
}
// It's already been parsed.
return $cost;
}
// This doesn't do anything yet // This doesn't do anything yet
public static function trim_value(&$value) public static function trim_value(&$value)
{ {

View file

@ -617,7 +617,7 @@ class AccessoriesController extends Controller
'min_amt' => e($accessory->min_amt), 'min_amt' => e($accessory->min_amt),
'location' => ($accessory->location) ? e($accessory->location->name): '', 'location' => ($accessory->location) ? e($accessory->location->name): '',
'purchase_date' => e($accessory->purchase_date), 'purchase_date' => e($accessory->purchase_date),
'purchase_cost' => number_format($accessory->purchase_cost, 2), 'purchase_cost' => Helper::parseCurrencyString($accessory->purchase_cost),
'numRemaining' => $accessory->numRemaining(), 'numRemaining' => $accessory->numRemaining(),
'actions' => $actions, 'actions' => $actions,
'companyName' => is_null($company) ? '' : e($company->name) 'companyName' => is_null($company) ? '' : e($company->name)

View file

@ -354,7 +354,7 @@ class AssetsController extends Controller
} }
if ($request->has('purchase_cost')) { if ($request->has('purchase_cost')) {
$asset->purchase_cost = e(number_format($request->input('purchase_cost'), 2, '.', '')); $asset->purchase_cost = e(Helper::parseCurrencyString($request->input('purchase_cost')));
} else { } else {
$asset->purchase_cost = null; $asset->purchase_cost = null;
} }
@ -1741,11 +1741,7 @@ class AssetsController extends Controller
} }
} }
// Lots going on here. Importer has parsed numbers before importing, so we need to check and see if it's a number before trying to parse. $purchase_cost = Helper::parseCurrencyString($asset->purchase_cost);
$purchase_cost = $asset->purchase_cost ?: '';
if (is_numeric($purchase_cost)) {
$purchase_cost = number_format($purchase_cost, 2);
}
$row = array( $row = array(
'checkbox' =>'<div class="text-center"><input type="checkbox" name="edit_asset['.$asset->id.']" class="one_required"></div>', 'checkbox' =>'<div class="text-center"><input type="checkbox" name="edit_asset['.$asset->id.']" class="one_required"></div>',

View file

@ -484,7 +484,7 @@ class ComponentsController extends Controller
'category' => ($component->category) ? e($component->category->name) : 'Missing category', 'category' => ($component->category) ? e($component->category->name) : 'Missing category',
'order_number' => e($component->order_number), 'order_number' => e($component->order_number),
'purchase_date' => e($component->purchase_date), 'purchase_date' => e($component->purchase_date),
'purchase_cost' => ($component->purchase_cost!='') ? number_format($component->purchase_cost, 2): '' , 'purchase_cost' => Helper::parseCurrencyString($component->purchase_cost),
'numRemaining' => $component->numRemaining(), 'numRemaining' => $component->numRemaining(),
'actions' => $actions, 'actions' => $actions,
'companyName' => is_null($company) ? '' : e($company->name), 'companyName' => is_null($company) ? '' : e($company->name),

View file

@ -476,7 +476,7 @@ class ConsumablesController extends Controller
'category' => ($consumable->category) ? e($consumable->category->name) : 'Missing category', 'category' => ($consumable->category) ? e($consumable->category->name) : 'Missing category',
'order_number' => e($consumable->order_number), 'order_number' => e($consumable->order_number),
'purchase_date' => e($consumable->purchase_date), 'purchase_date' => e($consumable->purchase_date),
'purchase_cost' => ($consumable->purchase_cost!='') ? number_format($consumable->purchase_cost, 2): '' , 'purchase_cost' => Helper::parseCurrencyString($consumable->purchase_cost),
'numRemaining' => $consumable->numRemaining(), 'numRemaining' => $consumable->numRemaining(),
'actions' => $actions, 'actions' => $actions,
'companyName' => is_null($company) ? '' : e($company->name), 'companyName' => is_null($company) ? '' : e($company->name),

View file

@ -1015,7 +1015,7 @@ class LicensesController extends Controller
'license_email' => e($license->license_email), 'license_email' => e($license->license_email),
'purchase_date' => ($license->purchase_date) ? $license->purchase_date : '', 'purchase_date' => ($license->purchase_date) ? $license->purchase_date : '',
'expiration_date' => ($license->expiration_date) ? $license->expiration_date : '', 'expiration_date' => ($license->expiration_date) ? $license->expiration_date : '',
'purchase_cost' => ($license->purchase_cost) ? number_format($license->purchase_cost, 2) : '', 'purchase_cost' => Helper::parseCurrencyString($license->purchase_cost),
'purchase_order' => ($license->purchase_order) ? e($license->purchase_order) : '', 'purchase_order' => ($license->purchase_order) ? e($license->purchase_order) : '',
'order_number' => ($license->order_number) ? e($license->order_number) : '', 'order_number' => ($license->order_number) ? e($license->order_number) : '',
'notes' => ($license->notes) ? e($license->notes) : '', 'notes' => ($license->notes) ? e($license->notes) : '',

View file

@ -1,25 +1,26 @@
<?php <?php
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Helpers\Helper;
use App\Models\Accessory; use App\Models\Accessory;
use App\Models\Actionlog; use App\Models\Actionlog;
use App\Models\Asset; use App\Models\Asset;
use App\Models\AssetMaintenance; use App\Models\AssetMaintenance;
use Carbon\Carbon; use App\Models\AssetModel;
use App\Models\Company; use App\Models\Company;
use App\Models\CustomField;
use App\Models\License;
use App\Models\Location;
use App\Models\Setting;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Support\Facades\Lang; use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Response; use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\View; use Illuminate\Support\Facades\View;
use Input; use Input;
use League\Csv\Reader; use League\Csv\Reader;
use App\Models\License;
use App\Models\Location;
use App\Models\AssetModel;
use App\Models\CustomField;
use Redirect; use Redirect;
use App\Models\Setting;
use App\Models\User;
/** /**
* This controller handles all actions related to Reports for * This controller handles all actions related to Reports for
@ -166,7 +167,7 @@ class ReportsController extends Controller
$row[] = ''; $row[] = '';
} }
$row[] = $asset->purchase_date; $row[] = $asset->purchase_date;
$row[] = '"' . number_format($asset->purchase_cost, 2) . '"'; $row[] = '"' . Helper::parsePurchasedCost($asset->purchase_cost) . '"';
if ($asset->order_number) { if ($asset->order_number) {
$row[] = e($asset->order_number); $row[] = e($asset->order_number);
} else { } else {
@ -308,9 +309,9 @@ class ReportsController extends Controller
} }
$row[] = $asset->purchase_date; $row[] = $asset->purchase_date;
$row[] = $currency . number_format($asset->purchase_cost, 2); $row[] = $currency . Helper::parseCurrencyString($asset->purchase_cost);
$row[] = $currency . number_format($asset->getDepreciatedValue(), 2); $row[] = $currency . Helper::parseCurrencyString($asset->getDepreciatedValue());
$row[] = $currency . number_format(( $asset->purchase_cost - $asset->getDepreciatedValue() ), 2); $row[] = $currency . Helper::parseCurrencyString(( $asset->purchase_cost - $asset->getDepreciatedValue() ));
$csv->insertOne($row); $csv->insertOne($row);
} }
@ -392,7 +393,7 @@ class ReportsController extends Controller
$row[] = $license->remaincount(); $row[] = $license->remaincount();
$row[] = $license->expiration_date; $row[] = $license->expiration_date;
$row[] = $license->purchase_date; $row[] = $license->purchase_date;
$row[] = '"' . number_format($license->purchase_cost, 2) . '"'; $row[] = '"' . Helper::parseCurrencyString($license->purchase_cost) . '"';
$rows[] = implode($row, ','); $rows[] = implode($row, ',');
} }
@ -416,7 +417,7 @@ class ReportsController extends Controller
public function getCustomReport() public function getCustomReport()
{ {
$customfields = CustomField::get(); $customfields = CustomField::get();
return View::make('reports/custom')->with('customfields',$customfields); return View::make('reports/custom')->with('customfields', $customfields);
} }
/** /**
@ -528,7 +529,7 @@ class ReportsController extends Controller
$row[] = e($asset->purchase_date); $row[] = e($asset->purchase_date);
} }
if (e(Input::get('purchase_cost')) == '1' && ( e(Input::get('depreciation')) != '1' )) { if (e(Input::get('purchase_cost')) == '1' && ( e(Input::get('depreciation')) != '1' )) {
$row[] = '"' . number_format($asset->purchase_cost, 2) . '"'; $row[] = '"' . Helper::parseCurrencyString($asset->purchase_cost) . '"';
} }
if (e(Input::get('order')) == '1') { if (e(Input::get('order')) == '1') {
if ($asset->order_number) { if ($asset->order_number) {
@ -605,9 +606,9 @@ class ReportsController extends Controller
} }
if (e(Input::get('depreciation')) == '1') { if (e(Input::get('depreciation')) == '1') {
$depreciation = $asset->getDepreciatedValue(); $depreciation = $asset->getDepreciatedValue();
$row[] = '"' . number_format($asset->purchase_cost, 2) . '"'; $row[] = '"' . Helper::parseCurrencyString($asset->purchase_cost) . '"';
$row[] = '"' . number_format($depreciation, 2) . '"'; $row[] = '"' . Helper::parseCurrencyString($depreciation) . '"';
$row[] = '"' . number_format($asset->purchase_cost - $depreciation, 2) . '"'; $row[] = '"' . Helper::parseCurrencyString($asset->purchase_cost) . '"';
} }
foreach ($customfields as $customfield) { foreach ($customfields as $customfield) {
@ -698,7 +699,7 @@ class ReportsController extends Controller
$improvementTime = intval($assetMaintenance->asset_maintenance_time); $improvementTime = intval($assetMaintenance->asset_maintenance_time);
} }
$row[] = $improvementTime; $row[] = $improvementTime;
$row[] = trans('general.currency') . number_format($assetMaintenance->cost, 2); $row[] = trans('general.currency') . Helper::parseCurrencyString($assetMaintenance->cost);
$rows[] = implode($row, ','); $rows[] = implode($row, ',');
} }

View file

@ -132,7 +132,7 @@
<span class="input-group-addon"> <span class="input-group-addon">
{{ \App\Models\Setting::first()->default_currency }} {{ \App\Models\Setting::first()->default_currency }}
</span> </span>
<input class="col-md-3 form-control" type="text" name="purchase_cost" id="purchase_cost" value="{{ Input::old('purchase_cost', number_format($accessory->purchase_cost,2)) }}" /> <input class="col-md-3 form-control" type="text" name="purchase_cost" id="purchase_cost" value="{{ Input::old('purchase_cost', \App\Helpers\Helper::parseCurrencyString($accessory->purchase_cost)) }}" />
{!! $errors->first('purchase_cost', '<span class="alert-msg"><i class="fa fa-times"></i> :message</span>') !!} {!! $errors->first('purchase_cost', '<span class="alert-msg"><i class="fa fa-times"></i> :message</span>') !!}
</div> </div>
</div> </div>

View file

@ -122,7 +122,7 @@
<div class="col-md-2"> <div class="col-md-2">
<div class="input-group"> <div class="input-group">
<span class="input-group-addon">{{ \App\Models\Setting::first()->default_currency }}</span> <span class="input-group-addon">{{ \App\Models\Setting::first()->default_currency }}</span>
<input class="col-md-2 form-control" type="text" name="cost" id="cost" value="{{ Input::old('cost', number_format($assetMaintenance->cost,2)) }}" /> <input class="col-md-2 form-control" type="text" name="cost" id="cost" value="{{ Input::old('cost', \App\Helpers\Helper::parseCurrencyString($assetMaintenance->cost)) }}" />
{!! $errors->first('cost', '<span class="alert-msg"><i class="fa fa-times"></i> :message</span>') !!} {!! $errors->first('cost', '<span class="alert-msg"><i class="fa fa-times"></i> :message</span>') !!}
</div> </div>
</div> </div>

View file

@ -156,7 +156,7 @@
<span class="input-group-addon"> <span class="input-group-addon">
{{ \App\Models\Setting::first()->default_currency }} {{ \App\Models\Setting::first()->default_currency }}
</span> </span>
<input class="col-md-2 form-control" type="text" name="purchase_cost" id="purchase_cost" value="{{ Input::old('purchase_cost', number_format($component->purchase_cost,2)) }}" /> <input class="col-md-2 form-control" type="text" name="purchase_cost" id="purchase_cost" value="{{ Input::old('purchase_cost', \App\Helpers\Helper::parseCurrencyString($component->purchase_cost)) }}" />
{!! $errors->first('purchase_cost', '<span class="alert-msg"><i class="fa fa-times"></i> :message</span>') !!} {!! $errors->first('purchase_cost', '<span class="alert-msg"><i class="fa fa-times"></i> :message</span>') !!}
</div> </div>
</div> </div>

View file

@ -83,7 +83,7 @@
<div class="col-md-12" style="padding-bottom: 5px;"><strong>{{ trans('admin/components/general.cost') }}:</strong> <div class="col-md-12" style="padding-bottom: 5px;"><strong>{{ trans('admin/components/general.cost') }}:</strong>
{{ \App\Models\Setting::first()->default_currency }} {{ \App\Models\Setting::first()->default_currency }}
{{ number_format($component->purchase_cost,2) }} </div> {{ \App\Helpers\Helper::parseCurrencyString($component->purchase_cost) }} </div>
@endif @endif
@if ($component->order_number) @if ($component->order_number)

View file

@ -159,7 +159,7 @@
<span class="input-group-addon"> <span class="input-group-addon">
{{ \App\Models\Setting::first()->default_currency }} {{ \App\Models\Setting::first()->default_currency }}
</span> </span>
<input class="col-md-2 form-control" type="text" name="purchase_cost" id="purchase_cost" value="{{ Input::old('purchase_cost', number_format($consumable->purchase_cost,2)) }}" /> <input class="col-md-2 form-control" type="text" name="purchase_cost" id="purchase_cost" value="{{ Input::old('purchase_cost', \App\Helpers\Helper::parseCurrencyString($consumable->purchase_cost)) }}" />
{!! $errors->first('purchase_cost', '<span class="alert-msg"><i class="fa fa-times"></i> :message</span>') !!} {!! $errors->first('purchase_cost', '<span class="alert-msg"><i class="fa fa-times"></i> :message</span>') !!}
</div> </div>
</div> </div>

View file

@ -72,7 +72,7 @@
<div class="col-md-12" style="padding-bottom: 5px;"><strong>{{ trans('admin/consumables/general.cost') }}:</strong> <div class="col-md-12" style="padding-bottom: 5px;"><strong>{{ trans('admin/consumables/general.cost') }}:</strong>
{{ \App\Models\Setting::first()->default_currency }} {{ \App\Models\Setting::first()->default_currency }}
{{ number_format($consumable->purchase_cost,2) }} </div> {{ \App\Helpers\Helper::parseCurrencyString($consumable->purchase_cost) }} </div>
@endif @endif
@if ($consumable->item_no) @if ($consumable->item_no)

View file

@ -215,7 +215,7 @@
</span> </span>
<input class="col-md-2 form-control" type="text" name="purchase_cost" id="purchase_cost" value="{{ Input::old('purchase_cost', number_format($asset->purchase_cost,2, '.', '')) }}" /> <input class="col-md-2 form-control" type="text" name="purchase_cost" id="purchase_cost" value="{{ Input::old('purchase_cost', \App\Helpers\Helper::parseCurrencyString($asset->purchase_cost)) }}" />
{!! $errors->first('purchase_cost', '<span class="alert-msg"><i class="fa fa-times"></i> :message</span>') !!} {!! $errors->first('purchase_cost', '<span class="alert-msg"><i class="fa fa-times"></i> :message</span>') !!}
</div> </div>
</div> </div>

View file

@ -154,7 +154,7 @@
@else @else
{{ \App\Models\Setting::first()->default_currency }} {{ \App\Models\Setting::first()->default_currency }}
@endif @endif
{{ number_format($asset->purchase_cost,2) }} {{ \App\Helpers\Helper::parseCurrencyString($asset->purchase_cost)}}
@if ($asset->order_number) @if ($asset->order_number)
(Order #{{ $asset->order_number }}) (Order #{{ $asset->order_number }})

View file

@ -139,7 +139,7 @@
<div class="col-md-3"> <div class="col-md-3">
<div class="input-group"> <div class="input-group">
<span class="input-group-addon">{{ \App\Models\Setting::first()->default_currency }}</span> <span class="input-group-addon">{{ \App\Models\Setting::first()->default_currency }}</span>
<input class="col-md-3 form-control" type="text" name="purchase_cost" id="purchase_cost" value="{{ Input::old('purchase_cost', number_format($license->purchase_cost,2)) }}" /> <input class="col-md-3 form-control" type="text" name="purchase_cost" id="purchase_cost" value="{{ Input::old('purchase_cost', \App\Helpers\Helper::parseCurrencyString($license->purchase_cost)) }}" />
{!! $errors->first('purchase_cost', '<span class="alert-msg"><i class="fa fa-times"></i> :message</span>') !!} {!! $errors->first('purchase_cost', '<span class="alert-msg"><i class="fa fa-times"></i> :message</span>') !!}
</div> </div>
</div> </div>

View file

@ -246,7 +246,7 @@
</td> </td>
<td> <td>
{{ \App\Models\Setting::first()->default_currency }} {{ \App\Models\Setting::first()->default_currency }}
{{ number_format($license->purchase_cost,2) }} {{ \App\Helpers\Helper::parseCurrencyString($license->purchase_cost) }}
</td> </td>
</tr> </tr>
@endif @endif

View file

@ -77,7 +77,7 @@
</td> </td>
<td>{{ $asset->purchase_date }}</td> <td>{{ $asset->purchase_date }}</td>
<td class="align-right">{{ $settings->default_currency }} <td class="align-right">{{ $settings->default_currency }}
{{ number_format($asset->purchase_cost) }} {{ \App\Helpers\Helper::parseCurrencyString($asset->purchase_cost) }}
</td> </td>
<td> <td>
@if ($asset->order_number) @if ($asset->order_number)

View file

@ -111,7 +111,7 @@
@else @else
{{ \App\Models\Setting::first()->default_currency }} {{ \App\Models\Setting::first()->default_currency }}
@endif @endif
{{ number_format($asset->purchase_cost) }}</td> {{ \App\Helpers\Helper::parseCurrencyString($asset->purchase_cost) }}</td>
<td class="align-right"> <td class="align-right">
@if ($asset->assetloc ) @if ($asset->assetloc )
{{ $asset->assetloc->currency }} {{ $asset->assetloc->currency }}
@ -119,7 +119,7 @@
{{ \App\Models\Setting::first()->default_currency }} {{ \App\Models\Setting::first()->default_currency }}
@endif @endif
{{ number_format($asset->getDepreciatedValue()) }}</td> {{ \App\Helpers\Helper::parseCurrencyString($asset->getDepreciatedValue()) }}</td>
<td class="align-right"> <td class="align-right">
@if ($asset->assetloc) @if ($asset->assetloc)
{{ $asset->assetloc->currency }} {{ $asset->assetloc->currency }}
@ -127,7 +127,7 @@
{{ \App\Models\Setting::first()->default_currency }} {{ \App\Models\Setting::first()->default_currency }}
@endif @endif
-{{ number_format(($asset->purchase_cost - $asset->getDepreciatedValue())) }}</td> -{{ \App\Helpers\Helper::parseCurrencyString(($asset->purchase_cost - $asset->getDepreciatedValue())) }}</td>
@else @else
<td></td> <td></td>
<td></td> <td></td>

View file

@ -74,7 +74,7 @@
@if ($asset->purchase_cost > 0) @if ($asset->purchase_cost > 0)
<td class="align-right"> <td class="align-right">
{{ \App\Models\Setting::first()->default_currency }} {{ \App\Models\Setting::first()->default_currency }}
{{ number_format($asset->purchase_cost) }} {{ \App\Helpers\Helper::parseCurrencyString($asset->purchase_cost) }}
</td> </td>
<td class="align-right"> <td class="align-right">
{{ \App\Models\Setting::first()->default_currency }} {{ \App\Models\Setting::first()->default_currency }}