mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Refactors, and create a number helper
This commit is contained in:
parent
e5b60195d8
commit
1c31f6de0f
@ -16,6 +16,7 @@ use App\Http\Controllers\Controller;
|
|||||||
use App\Jobs\Entity\ActionEntity;
|
use App\Jobs\Entity\ActionEntity;
|
||||||
use App\Models\Invoice;
|
use App\Models\Invoice;
|
||||||
use App\Repositories\BaseRepository;
|
use App\Repositories\BaseRepository;
|
||||||
|
use App\Utils\Number;
|
||||||
use App\Utils\Traits\MakesDates;
|
use App\Utils\Traits\MakesDates;
|
||||||
use App\Utils\Traits\MakesHash;
|
use App\Utils\Traits\MakesHash;
|
||||||
use Barracuda\ArchiveStream\Archive;
|
use Barracuda\ArchiveStream\Archive;
|
||||||
@ -55,11 +56,14 @@ class InvoiceController extends Controller
|
|||||||
})
|
})
|
||||||
->editColumn('status_id', function ($invoice){
|
->editColumn('status_id', function ($invoice){
|
||||||
return Invoice::badgeForStatus($invoice->status);
|
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());
|
return $this->createClientDate($invoice->invoice_date, $invoice->client->timezone()->name)->format($invoice->client->date_format());
|
||||||
})->editColumn('due_date', function ($invoice){
|
})->editColumn('due_date', function ($invoice){
|
||||||
return $this->createClientDate($invoice->due_date, $invoice->client->timezone()->name)->format($invoice->client->date_format());
|
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'])
|
->rawColumns(['checkbox', 'action', 'status_id'])
|
||||||
->make(true);
|
->make(true);
|
||||||
|
@ -16,6 +16,7 @@ use App\DataMapper\CompanySettings;
|
|||||||
use App\Models\Client;
|
use App\Models\Client;
|
||||||
use App\Models\Company;
|
use App\Models\Company;
|
||||||
use App\Models\Country;
|
use App\Models\Country;
|
||||||
|
use App\Models\Currency;
|
||||||
use App\Models\Filterable;
|
use App\Models\Filterable;
|
||||||
use App\Models\Timezone;
|
use App\Models\Timezone;
|
||||||
use App\Utils\Traits\GeneratesCounter;
|
use App\Utils\Traits\GeneratesCounter;
|
||||||
@ -131,6 +132,11 @@ class Client extends BaseModel
|
|||||||
return $this->getMergedSettings()->datetime_format;
|
return $this->getMergedSettings()->datetime_format;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function currency()
|
||||||
|
{
|
||||||
|
return Currency::find($this->getMergedSettings()->currency_id);
|
||||||
|
}
|
||||||
|
|
||||||
public function getMergedSettings()
|
public function getMergedSettings()
|
||||||
{
|
{
|
||||||
return ClientSettings::buildClientSettings(new CompanySettings($this->company->settings), new ClientSettings($this->settings));
|
return ClientSettings::buildClientSettings(new CompanySettings($this->company->settings), new ClientSettings($this->settings));
|
||||||
|
94
app/Utils/Number.php
Normal file
94
app/Utils/Number.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -28,7 +28,6 @@ trait MakesDates
|
|||||||
*/
|
*/
|
||||||
public function createClientDate($utc_date , $timezone)
|
public function createClientDate($utc_date , $timezone)
|
||||||
{
|
{
|
||||||
Log::error($utc_date. ' '. $timezone);
|
|
||||||
|
|
||||||
if(is_string($utc_date))
|
if(is_string($utc_date))
|
||||||
$utc_date = $this->convertToDateObject($utc_date);
|
$utc_date = $this->convertToDateObject($utc_date);
|
||||||
|
@ -6,7 +6,7 @@ use Tests\TestCase;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @test
|
* @test
|
||||||
* @covers App\Utils\NumberHelper
|
* @covers App\Utils\Number
|
||||||
*/
|
*/
|
||||||
class CompareCollectionTest extends TestCase
|
class CompareCollectionTest extends TestCase
|
||||||
{
|
{
|
||||||
|
@ -6,7 +6,7 @@ use Tests\TestCase;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @test
|
* @test
|
||||||
* @covers App\Utils\NumberHelper
|
* @covers App\Utils\Number
|
||||||
*/
|
*/
|
||||||
class NestedCollectionTest extends TestCase
|
class NestedCollectionTest extends TestCase
|
||||||
{
|
{
|
||||||
|
@ -2,12 +2,12 @@
|
|||||||
|
|
||||||
namespace Tests\Unit;
|
namespace Tests\Unit;
|
||||||
|
|
||||||
use App\Utils\NumberHelper;
|
use App\Utils\Number;
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @test
|
* @test
|
||||||
* @covers App\Utils\NumberHelper
|
* @covers App\Utils\Number
|
||||||
*/
|
*/
|
||||||
class NumberTest extends TestCase
|
class NumberTest extends TestCase
|
||||||
{
|
{
|
||||||
@ -15,21 +15,21 @@ class NumberTest extends TestCase
|
|||||||
|
|
||||||
public function testRoundingThreeLow()
|
public function testRoundingThreeLow()
|
||||||
{
|
{
|
||||||
$rounded = NumberHelper::roundValue(3.144444444444, 3);
|
$rounded = Number::roundValue(3.144444444444, 3);
|
||||||
|
|
||||||
$this->assertEquals(3.144, $rounded);
|
$this->assertEquals(3.144, $rounded);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testRoundingThreeHigh()
|
public function testRoundingThreeHigh()
|
||||||
{
|
{
|
||||||
$rounded = NumberHelper::roundValue(3.144944444444, 3);
|
$rounded = Number::roundValue(3.144944444444, 3);
|
||||||
|
|
||||||
$this->assertEquals(3.145, $rounded);
|
$this->assertEquals(3.145, $rounded);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testRoundingTwoLow()
|
public function testRoundingTwoLow()
|
||||||
{
|
{
|
||||||
$rounded = NumberHelper::roundValue(2.145);
|
$rounded = Number::roundValue(2.145);
|
||||||
|
|
||||||
$this->assertEquals(2.15, $rounded);
|
$this->assertEquals(2.15, $rounded);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user