jomweb/ringgit

Malaysia Ringgit implementation on top of Money PHP

Fund package maintenance!
crynobone
Liberapay

v3.1.0 2024-03-31 13:34 UTC

README

tests Latest Stable Version Total Downloads Latest Unstable Version License Coverage Status

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)));

Installation

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.

Quick Installation

Above installation can also be simplify by using the following command:

composer require "jomweb/ringgit"

Usages

Taxes

GST Declaration

Without GST

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);
Before GST

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());
After GST

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());

SST Declaration

Without SST

Declaring Money (MYR) without GST (Zero).

use Duit\MYR;

$money = new MYR(540);
$money = MYR::given(540);
$money = MYR::withoutTax(540);
Before SST

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());
After SST

Declaring Money (MYR) with SST tax.

use Duit\MYR;
use Duit\Taxable\Sst;

$money = MYR::afterTax(530, new Sst());