1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

Refactors, and create a number helper

This commit is contained in:
David Bomba 2019-08-28 12:36:53 +10:00
parent e5b60195d8
commit 1c31f6de0f
9 changed files with 114 additions and 40 deletions

View File

@ -16,6 +16,7 @@ use App\Http\Controllers\Controller;
use App\Jobs\Entity\ActionEntity;
use App\Models\Invoice;
use App\Repositories\BaseRepository;
use App\Utils\Number;
use App\Utils\Traits\MakesDates;
use App\Utils\Traits\MakesHash;
use Barracuda\ArchiveStream\Archive;
@ -55,11 +56,14 @@ class InvoiceController extends Controller
})
->editColumn('status_id', function ($invoice){
return Invoice::badgeForStatus($invoice->status);
})
->editColumn('invoice_date', function ($invoice){
})->editColumn('invoice_date', function ($invoice){
return $this->createClientDate($invoice->invoice_date, $invoice->client->timezone()->name)->format($invoice->client->date_format());
})->editColumn('due_date', function ($invoice){
return $this->createClientDate($invoice->due_date, $invoice->client->timezone()->name)->format($invoice->client->date_format());
})->editColumn('balance', function ($invoice) {
return Number::formatMoney($invoice->balance, $invoice->client->currency(), $invoice->client->country, $invoice->client->getMergedSettings());
})->editColumn('amount', function ($invoice) {
return Number::formatMoney($invoice->amount, $invoice->client->currency(), $invoice->client->country, $invoice->client->getMergedSettings());
})
->rawColumns(['checkbox', 'action', 'status_id'])
->make(true);

View File

@ -16,6 +16,7 @@ use App\DataMapper\CompanySettings;
use App\Models\Client;
use App\Models\Company;
use App\Models\Country;
use App\Models\Currency;
use App\Models\Filterable;
use App\Models\Timezone;
use App\Utils\Traits\GeneratesCounter;
@ -131,6 +132,11 @@ class Client extends BaseModel
return $this->getMergedSettings()->datetime_format;
}
public function currency()
{
return Currency::find($this->getMergedSettings()->currency_id);
}
public function getMergedSettings()
{
return ClientSettings::buildClientSettings(new CompanySettings($this->company->settings), new ClientSettings($this->settings));

94
app/Utils/Number.php Normal file
View File

@ -0,0 +1,94 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Utils;
use Illuminate\Support\Facades\Log;
/**
* Class Number.
*/
class Number
{
/**
* @param float $value
* @param int $precision
*
* @return float
*/
public static function roundValue(float $value, int $precision = 2) : float
{
return round($value, $precision, PHP_ROUND_HALF_UP);
}
/**
* Formats a given value based on the clients currency
*
* @param float $value The number to be formatted
* @param object $currency The client currency object
*
* @return float The formatted value
*/
public static function formatValue($value, $currency) : float
{
$value = floatval($value);
$thousand = $currency->thousand_separator;
$decimal = $currency->decimal_separator;
$precision = $currency->precision;
return number_format($value, $precision, $decimal, $thousand);
}
/**
* Formats a given value based on the clients currency AND country
*
* @param floatval $value The number to be formatted
* @param object $currency The client currency object
* @param object $country The client country
*
* @return float The formatted value
*/
public static function formatMoney($value, $currency, $country, $settings)
{
Log::error('code = '.$settings->show_currency_code);
Log::error('symbol = '.$settings->show_currency_symbol);
$thousand = $currency->thousand_separator;
$decimal = $currency->decimal_separator;
$precision = $currency->precision;
$code = $currency->code;
$swapSymbol = $country->swap_currency_symbol;
/* Country settings override client settings */
if ($country->thousand_separator)
$thousand = $country->thousand_separator;
if ($country->decimal_separator)
$decimal = $country->decimal_separator;
$value = number_format($value, $precision, $decimal, $thousand);
$symbol = $currency->symbol;
if ($settings->show_currency_code == "TRUE") {
Log::error('in code regardless');
return "{$value} {$code}";
} elseif ($swapSymbol) {
return "{$value} " . trim($symbol);
} elseif ($settings->show_currency_symbol === "TRUE") {
return "{$symbol}{$value}";
} else {
return self::formatValue($value, $currency);
}
}
}

View File

@ -1,29 +0,0 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Utils;
/**
* Class NumberHelper.
*/
class NumberHelper
{
/**
* @param float $value
* @param int $precision
*
* @return float
*/
public static function roundValue(float $value, int $precision = 2) : float
{
return round($value, $precision, PHP_ROUND_HALF_UP);
}
}

View File

@ -28,8 +28,7 @@ trait MakesDates
*/
public function createClientDate($utc_date , $timezone)
{
Log::error($utc_date. ' '. $timezone);
if(is_string($utc_date))
$utc_date = $this->convertToDateObject($utc_date);

View File

@ -6,7 +6,7 @@ use Tests\TestCase;
/**
* @test
* @covers App\Utils\NumberHelper
* @covers App\Utils\Number
*/
class CompareCollectionTest extends TestCase
{

View File

@ -6,7 +6,7 @@ use Tests\TestCase;
/**
* @test
* @covers App\Utils\NumberHelper
* @covers App\Utils\Number
*/
class NestedCollectionTest extends TestCase
{

View File

@ -2,12 +2,12 @@
namespace Tests\Unit;
use App\Utils\NumberHelper;
use App\Utils\Number;
use Tests\TestCase;
/**
* @test
* @covers App\Utils\NumberHelper
* @covers App\Utils\Number
*/
class NumberTest extends TestCase
{
@ -15,21 +15,21 @@ class NumberTest extends TestCase
public function testRoundingThreeLow()
{
$rounded = NumberHelper::roundValue(3.144444444444, 3);
$rounded = Number::roundValue(3.144444444444, 3);
$this->assertEquals(3.144, $rounded);
}
public function testRoundingThreeHigh()
{
$rounded = NumberHelper::roundValue(3.144944444444, 3);
$rounded = Number::roundValue(3.144944444444, 3);
$this->assertEquals(3.145, $rounded);
}
public function testRoundingTwoLow()
{
$rounded = NumberHelper::roundValue(2.145);
$rounded = Number::roundValue(2.145);
$this->assertEquals(2.15, $rounded);
}