179

Product support

Get help in the plugin support forum.

Categories

Currency and amount management made simple. A fast little plugin that quickly addresses the problem of floating point numbers and currency rounding errors in your code.

Here’s a great article that explains the potential problem in more detail - https://spin.atomicobject.com/2014/08/14/currency-rounding-errors/

The plugin requires:

Installation note (since January 2019)

Since January 2019 Brick/Money package requires PHP7.1 as a minimum version while OctoberCMS overrides platform to PHP7.0 (described here). As a consequence, if you want to install the plugin from Marketplace right now, ensure you have changed your root composer.json PHP version to 7.1.

Backend form widget

The plugin adds a backend form widget called money. You can use it in your config_form.yaml files:

price:
    type: money
    mode: amountcurrency

It is designed to input money made of two factors: amount and currency. By default you will get two fields:

  • Amount input which gets parsed every time you refresh the page and input a value (using currency)
  • Select with currencies defined in Responsiv.Currency.

You have to have defined two columns for every money type field. After that, you have to write your own mutator and accessor or use MoneyFields behavior described below.

Modes

Right now there are two modes of the widget:

  • amountcurrency - the default one
  • amount

The latter will use currency set as default in Responsiv.Currency settings in the backend while the first one will render you a dropdown with other currencies.

I currently work on the third mode: currency. This will be useful for changing all currencies on page with one dropdown (for example when you have one currency on the whole invoice where you have a lot of amount inputs).

MoneyFields behavior

The behavior uses $moneyFields attribute to add dynamically mutators and accessors for money fields.

Using the behavior looks like in this example:

class Product extends Model
{
    public $moneyFields = [
        'price' => [
            'amountColumn' => 'amount',
            'currencyIdColumn' => 'currency_id'
        ]
    ];
    //...
    public $implement = [
        'Initbiz.Money.Behaviors.MoneyFields'
    ];
    //...
}

This code will dynamically add two methods to the Product model: setPriceAttribute and getPriceAttribute. Both of them will be using amount as an amount column name (and model attribute) and currency_id as a currency column name (and model attribute as well).

If you want to have only one currency in your model there should not be problems with using the same column name for more than one field. Remember though that if you will not change the currency for all inputs that have the same column while saving the value might be overridden.

Future plans (TODO)

  • Currency mode (to change all currencies on the page)
  • Automatically converting and displaying amounts in the selected format (extending Responsiv.Currency)
  • Register settings to define if we want to use localized config or 'per currency' format
  • Found the plugin useful on 24 Sep, 2020

    Nice plugin, get's rid of all your money problems ;) Also very fast and good support !

  • author

    Replied on 25 Sep, 2020

    Thank you for your kind words.

    We're afraid there are a lot of money problems the plugin cannot solve, unfortunately ;)

1.2.4

Fix adding dinero.js

Feb 05, 2024

1.2.2

Cast formatMoney parameter to int

Jun 07, 2023

1.2.1

Cast number_format parameter to int

Jan 29, 2023

1.2.0

Add valueMode to Money form widget so that it can use different syntax of storing value

Oct 20, 2022

1.1.0

Add compatibility with october 3

Aug 05, 2022

1.0.12

Add int from float helper

Aug 05, 2022

1.0.11

Small fix & new accessors types: price_decimal and price_formatted

Jun 10, 2022

1.0.10

Code cleanup

Jun 10, 2022

1.0.9

Fixed mutators and accessors with zero and null values

Mar 08, 2020

1.0.8

Minor bug fixes, a few features added

Nov 19, 2019

1.0.7

Added money column type

Aug 06, 2019

1.0.6

Updating partials with moneyFields works

Aug 06, 2019

1.0.5

Yet another small fix

May 10, 2019

1.0.4

Minor bug fixes

May 10, 2019

1.0.3

Small extending fix for better DX

May 06, 2019

1.0.2

MoneyFields behavior fixed

Jan 12, 2019

1.0.1

Initialize plugin.

Oct 02, 2018