diff --git a/app/Http/Controllers/ClientPortal/InvoiceController.php b/app/Http/Controllers/ClientPortal/InvoiceController.php index 1dc70a306b3d..23f2c13d849e 100644 --- a/app/Http/Controllers/ClientPortal/InvoiceController.php +++ b/app/Http/Controllers/ClientPortal/InvoiceController.php @@ -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); diff --git a/app/Models/Client.php b/app/Models/Client.php index 257716a8817f..32a71af21443 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -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)); diff --git a/app/Utils/Number.php b/app/Utils/Number.php new file mode 100644 index 000000000000..402d07710308 --- /dev/null +++ b/app/Utils/Number.php @@ -0,0 +1,94 @@ +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); + } + } +} diff --git a/app/Utils/NumberHelper.php b/app/Utils/NumberHelper.php deleted file mode 100644 index 77a6da2e02ce..000000000000 --- a/app/Utils/NumberHelper.php +++ /dev/null @@ -1,29 +0,0 @@ -convertToDateObject($utc_date); diff --git a/tests/Unit/CompareCollectionTest.php b/tests/Unit/CompareCollectionTest.php index c1349b9fa656..3f9bf798045c 100644 --- a/tests/Unit/CompareCollectionTest.php +++ b/tests/Unit/CompareCollectionTest.php @@ -6,7 +6,7 @@ use Tests\TestCase; /** * @test - * @covers App\Utils\NumberHelper + * @covers App\Utils\Number */ class CompareCollectionTest extends TestCase { diff --git a/tests/Unit/NestedCollectionTest.php b/tests/Unit/NestedCollectionTest.php index c2f969ddeedf..df2854bca04b 100644 --- a/tests/Unit/NestedCollectionTest.php +++ b/tests/Unit/NestedCollectionTest.php @@ -6,7 +6,7 @@ use Tests\TestCase; /** * @test - * @covers App\Utils\NumberHelper + * @covers App\Utils\Number */ class NestedCollectionTest extends TestCase { diff --git a/tests/Unit/NumberTest.php b/tests/Unit/NumberTest.php index f2d92c891229..f1b370f5bde5 100644 --- a/tests/Unit/NumberTest.php +++ b/tests/Unit/NumberTest.php @@ -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); }