PHP 8.0+ library to make working with money safer, easier, and fun for Malaysia Ringgit!
"If I had a dime for every time I've seen someone use FLOAT to store currency, I'd have $999.997634" -- Bill Karwin
In short: You shouldn't represent monetary values by a float. Wherever you need to represent money, use this Money value object.
<?php
use Duit\MYR;
$fiveMyr = MYR::given(500);
$tenMyr = $fiveMyr->add($fiveMyr);
list($part1, $part2, $part3) = $tenMyr->allocate(array(1, 1, 1));
assert($part1->equals(MYR::given(334)));
assert($part2->equals(MYR::given(333)));
assert($part3->equals(MYR::given(333)));
To install through composer, simply put the following in your composer.json
file:
{
"require": {
"jomweb/ringgit": "^2.0"
}
}
And then run composer install
from the terminal.
Above installation can also be simplify by using the following command:
composer require "jomweb/ringgit"
Declaring Money (MYR) without GST (Zero).
use Duit\MYR;
$money = new MYR(540);
$money = MYR::given(540);
$money = MYR::withoutGst(540);
$money = MYR::withoutTax(540);
Declaring Money (MYR) before GST is applied.
use Duit\MYR;
use Duit\Taxable\Gst\ZeroRate;
use Duit\Taxable\Gst\StandardRate;
$money = MYR::beforeGst(540);
$money = MYR::beforeTax(540, new StandardRate());
$money = MYR::beforeTax(540, new ZeroRate());
$money = MYR::given(540)->useGstStandardRate(); // 6%
$money = MYR::given(540)->useGstZeroRate(); // 0%
$money = MYR::given(540)->enableTax(new StandardRate());
$money = MYR::given(540)->enableTax(new ZeroRate());
Declaring Money (MYR) with GST amount.
use Duit\MYR;
use Duit\Taxable\Gst\ZeroRate;
use Duit\Taxable\Gst\StandardRate;
$money = MYR::afterGst(530); // always going to use 6%
$money = MYR::afterTax(540, new StandardRate());
$money = MYR::afterTax(540, new ZeroRate());
Declaring Money (MYR) without GST (Zero).
use Duit\MYR;
$money = new MYR(540);
$money = MYR::given(540);
$money = MYR::withoutTax(540);
Declaring Money (MYR) before SST is applied.
use Duit\MYR;
use Duit\Taxable\Sst;
$money = MYR::beforeTax(530, new Sst());
$money = MYR::given(530)->enableTax(new Sst());
Declaring Money (MYR) with SST tax.
use Duit\MYR;
use Duit\Taxable\Sst;
$money = MYR::afterTax(530, new Sst());