2017 © Pedro Peláez
 

yii2-extension cms-module-money

Module for working with money and currency

image

skeeks/cms-module-money

Module for working with money and currency

  • Monday, May 21, 2018
  • by skeeks-semenov
  • Repository
  • 1 Watchers
  • 3 Stars
  • 1,455 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 45 Versions
  • 3 % Grown

The README.md

Module for working with money and currency

Installation

The preferred way to install this extension is through composer., (*1)

Either run, (*2)

php composer.phar require --prefer-dist skeeks/cms-module-money "*"

or add, (*3)

"skeeks/cms-module-money": "*"

Configuration app


'components' => [ 'money' => [ 'class' => 'skeeks\modules\cms\money\components\money\Money', ], 'i18n' => [ 'translations' => [ 'skeeks/money' => [ 'class' => 'yii\i18n\PhpMessageSource', 'basePath' => '@skeeks/modules/cms/money/messages', 'fileMap' => [ 'skeeks/money' => 'main.php', ], ] ] ], ], 'modules' => [ 'money' => [ 'class' => 'skeeks\modules\cms\money\Module', ] ]

Игформация о модуле

Модуль для работы с деньгами и валютой. За основу данного модуля, была взята библиотека https://github.com/sebastianbergmann/money, но ее пришлось изрядно допилить. Поэтому в чистом виде она не была подключена., (*4)

Основная работа была в том, чтобы появилась некоторая прозрачная работа с деньгами. Типичны пример, который мы прозрачно решаем в этой библиотеке. (10$ + 154руб + 12$) = рузальтат нужно показать в GBP, для de_DE локали, (*5)

Сразу пример:, (*6)

use \skeeks\modules\cms\money\Money;
use \skeeks\modules\cms\money\IntlFormatter;

$money  = Money::fromString('10', "USD");
$money2 = Money::fromString('154', "EUR");
$money3 = Money::fromString('12', "EUR");

$money = $money->add($money2);
$money = $money->add($money3);

$money = $money->convertToCurrency('GBP');

$formatter = new IntlFormatter('de_DE');
$formatter->format($money); //результат 132,25 £

Установка

1) Стандартная установка через composer, (*7)

php composer.phar require --prefer-dist skeeks/cms-module-money "*"

or add, (*8)

"skeeks/cms-module-money": "*"

to the require section of your composer.json file., (*9)

2) Установка миграций, (*10)

 php yii migrate --migrationPath=vendor/skeeks/cms-module-money/migrations

Примеры и использование

Оригинальные примеры

Creating a Money object and accessing its monetary value

use skeeks\cms\modules\money\Currency;
use skeeks\cms\modules\money\Money;

// Create Money object that represents 1 EUR
$m = new Money(100, new Currency('EUR'));

// Access the Money object's monetary value
print $m->getAmount();

The code above produces the output shown below:, (*11)

100

Creating a Money object from a string value

use skeeks\cms\modules\money\Currency;
use skeeks\cms\modules\money\Money;

// Create Money object that represents 12.34 EUR
$m = Money::fromString('12.34', new Currency('EUR'))

// Access the Money object's monetary value
print $m->getAmount();

The code above produces the output shown below:, (*12)

1234

Using a Currency-specific subclass of Money

use skeeks\cms\modules\money\EUR;

// Create Money object that represents 1 EUR
$m = new EUR(100);

// Access the Money object's monetary value
print $m->getAmount();

The code above produces the output shown below:, (*13)

100

Please note that there is no subclass of Money that is specific to Turkish Lira as TRY is not a valid class name in PHP., (*14)

Formatting a Money object using PHP's built-in NumberFormatter

use skeeks\cms\modules\money\Currency;
use skeeks\cms\modules\money\Money;
use skeeks\cms\modules\money\IntlFormatter;

// Create Money object that represents 1 EUR
$m = new Money(100, new Currency('EUR'));

// Format a Money object using PHP's built-in NumberFormatter (German locale)
$f = new IntlFormatter('de_DE');

print $f->format($m);

The code above produces the output shown below:, (*15)

1,00 €

Basic arithmetic using Money objects

use skeeks\cms\modules\money\Currency;
use skeeks\cms\modules\money\Money;

// Create two Money objects that represent 1 EUR and 2 EUR, respectively
$a = new Money(100, new Currency('EUR'));
$b = new Money(200, new Currency('EUR'));

// Negate a Money object
$c = $a->negate();
print $c->getAmount();

// Calculate the sum of two Money objects
$c = $a->add($b);
print $c->getAmount();

// Calculate the difference of two Money objects
$c = $b->subtract($a);
print $c->getAmount();

// Multiply a Money object with a factor
$c = $a->multiply(2);
print $c->getAmount();

The code above produces the output shown below:, (*16)

-100
300
100
200

Comparing Money objects

use skeeks\cms\modules\money\Currency;
use skeeks\cms\modules\money\Money;

// Create two Money objects that represent 1 EUR and 2 EUR, respectively
$a = new Money(100, new Currency('EUR'));
$b = new Money(200, new Currency('EUR'));

var_dump($a->lessThan($b));
var_dump($a->greaterThan($b));

var_dump($b->lessThan($a));
var_dump($b->greaterThan($a));

var_dump($a->compareTo($b));
var_dump($a->compareTo($a));
var_dump($b->compareTo($a));

The code above produces the output shown below:, (*17)

bool(true)
bool(false)
bool(false)
bool(true)
int(-1)
int(0)
int(1)

The compareTo() method returns an integer less than, equal to, or greater than zero if the value of one Money object is considered to be respectively less than, equal to, or greater than that of another Money object., (*18)

You can use the compareTo() method to sort an array of Money objects using PHP's built-in sorting functions:, (*19)

use skeeks\cms\modules\money\Currency;
use skeeks\cms\modules\money\Money;

$m = array(
    new Money(300, new Currency('EUR')),
    new Money(100, new Currency('EUR')),
    new Money(200, new Currency('EUR'))
);

usort(
    $m,
    function ($a, $b) { return $a->compareTo($b); }
);

foreach ($m as $_m) {
    print $_m->getAmount() . "\n";
}

The code above produces the output shown below:, (*20)

100
200
300

Allocate the monetary value represented by a Money object among N targets

use skeeks\cms\modules\money\Currency;
use skeeks\cms\modules\money\Money;

// Create a Money object that represents 0,99 EUR
$a = new Money(99, new Currency('EUR'));

foreach ($a->allocateToTargets(10) as $t) {
    print $t->getAmount() . "\n";
}

The code above produces the output shown below:, (*21)

10
10
10
10
10
10
10
10
10
9

Allocate the monetary value represented by a Money object using a list of ratios

use skeeks\cms\modules\money\Currency;
use skeeks\cms\modules\money\Money;

// Create a Money object that represents 0,05 EUR
$a = new Money(5, new Currency('EUR'));

foreach ($a->allocateByRatios(array(3, 7)) as $t) {
    print $t->getAmount() . "\n";
}

The code above produces the output shown below:, (*22)

2
3

Extract a percentage (and a subtotal) from the monetary value represented by a Money object

use skeeks\cms\modules\money\Currency;
use skeeks\cms\modules\money\Money;

// Create a Money object that represents 100,00 EUR
$original = new Money(10000, new Currency('EUR'));

// Extract 21% (and the corresponding subtotal)
$extract = $original->extractPercentage(21);

printf(
    "%d = %d + %d\n",
    $original->getAmount(),
    $extract['subtotal']->getAmount(),
    $extract['percentage']->getAmount()
);

The code above produces the output shown below:, (*23)

10000 = 8265 + 1735

Please note that this extracts the percentage out of a monetary value where the percentage is already included. If you want to get the percentage of the monetary value you should use multiplication (multiply(0.21), for instance, to calculate 21% of a monetary value represented by a Money object) instead., (*24)

Страница на SkeekS CMS Marketplace, (*25)

skeeks!
SkeekS CMS (Yii2) — быстро, просто, эффективно!
skeeks.com | cms.skeeks.com | marketplace.cms.skeeks.com, (*26)

The Versions

21/05 2018

dev-master

9999999-dev http://www.skeeks.com/

Module for working with money and currency

  Sources   Download

GPL-3.0+

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

19/11 2017

3.1.1

3.1.1.0 http://www.skeeks.com/

Module for working with money and currency

  Sources   Download

GPL-3.0+

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

19/11 2017

3.1.0

3.1.0.0 http://www.skeeks.com/

Module for working with money and currency

  Sources   Download

GPL-3.0+

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

15/11 2017

3.0.3

3.0.3.0 http://www.skeeks.com/

Module for working with money and currency

  Sources   Download

GPL-3.0+

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

15/11 2017

3.0.2

3.0.2.0 http://www.skeeks.com/

Module for working with money and currency

  Sources   Download

GPL-3.0+

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

07/11 2017

3.0.1

3.0.1.0 http://www.skeeks.com/

Module for working with money and currency

  Sources   Download

GPL-3.0+

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

18/06 2017

3.0.0-alpha

3.0.0.0-alpha http://www.skeeks.com/

Module for working with money and currency

  Sources   Download

GPL-3.0+

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

18/06 2017

3.0.0

3.0.0.0 http://www.skeeks.com/

Module for working with money and currency

  Sources   Download

GPL-3.0+

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

16/10 2016

2.5.0

2.5.0.0 http://www.skeeks.com/

Module for working with money and currency

  Sources   Download

GPL-3.0+

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

14/10 2016

dev-shop-update

dev-shop-update http://www.skeeks.com/

Module for working with money and currency

  Sources   Download

GPL-3.0+

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

28/07 2016

2.4.2

2.4.2.0 http://www.skeeks.com/

Module for working with money and currency

  Sources   Download

GPL-3.0+

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

05/05 2016

2.4.1

2.4.1.0 http://www.skeeks.com/

Module for working with money and currency

  Sources   Download

GPL-3.0+

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

21/04 2016

2.4.0

2.4.0.0 http://www.skeeks.com/

Module for working with money and currency

  Sources   Download

GPL-3.0+

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

20/04 2016

2.3.0

2.3.0.0 http://www.skeeks.com/

Модуль для работы с деньгами и валютой

  Sources   Download

BSD-3-Clause

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

16/04 2016

2.2.2

2.2.2.0 http://www.skeeks.com/

Модуль для работы с деньгами и валютой

  Sources   Download

BSD-3-Clause

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

05/03 2016

2.2.1

2.2.1.0 http://www.skeeks.com/

Модуль для работы с деньгами и валютой

  Sources   Download

BSD-3-Clause

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

05/03 2016

2.2.0

2.2.0.0 http://www.skeeks.com/

Модуль для работы с деньгами и валютой

  Sources   Download

BSD-3-Clause

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

04/03 2016

2.1.0

2.1.0.0 http://www.skeeks.com/

Модуль для работы с деньгами и валютой

  Sources   Download

BSD-3-Clause

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

17/02 2016

2.0.14

2.0.14.0 http://www.skeeks.com/

Модуль для работы с деньгами и валютой

  Sources   Download

BSD-3-Clause

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

31/10 2015

2.0.13

2.0.13.0 http://www.skeeks.com/

Модуль для работы с деньгами и валютой

  Sources   Download

BSD-3-Clause

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

09/10 2015

2.0.12

2.0.12.0 http://www.skeeks.com/

Модуль для работы с деньгами и валютой

  Sources   Download

BSD-3-Clause

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

02/10 2015

2.0.11

2.0.11.0 http://www.skeeks.com/

Модуль для работы с деньгами и валютой

  Sources   Download

BSD-3-Clause

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

15/09 2015

2.0.10

2.0.10.0 http://www.skeeks.com/

Модуль для работы с деньгами и валютой

  Sources   Download

BSD-3-Clause

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

02/09 2015

2.0.9

2.0.9.0 http://www.skeeks.com/

Модуль для работы с деньгами и валютой

  Sources   Download

BSD-3-Clause

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

01/09 2015

2.0.8

2.0.8.0 http://www.skeeks.com/

Модуль для работы с деньгами и валютой

  Sources   Download

BSD-3-Clause

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

26/08 2015

2.0.7

2.0.7.0 http://www.skeeks.com/

Модуль для работы с деньгами и валютой

  Sources   Download

BSD-3-Clause

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

26/07 2015

2.0.6

2.0.6.0 http://www.skeeks.com/

Модуль для работы с деньгами и валютой

  Sources   Download

BSD-3-Clause

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

01/07 2015

dev-release

dev-release http://www.skeeks.com/

Модуль для работы с деньгами и валютой

  Sources   Download

BSD-3-Clause

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

01/07 2015

2.0.5

2.0.5.0 http://www.skeeks.com/

Модуль для работы с деньгами и валютой

  Sources   Download

BSD-3-Clause

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

01/07 2015

2.0.4

2.0.4.0 http://www.skeeks.com/

Модуль для работы с деньгами и валютой

  Sources   Download

BSD-3-Clause

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

18/06 2015

2.0.3

2.0.3.0 http://www.skeeks.com/

Модуль для работы с деньгами и валютой

  Sources   Download

BSD-3-Clause

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

02/06 2015

2.0.2

2.0.2.0 http://www.skeeks.com/

Модуль для работы с деньгами и валютой

  Sources   Download

BSD-3-Clause

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

01/06 2015

2.0.1

2.0.1.0 http://www.skeeks.com/

Модуль для работы с деньгами и валютой

  Sources   Download

BSD-3-Clause

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

01/06 2015

2.0.0

2.0.0.0 http://www.skeeks.com/

Модуль для работы с деньгами и валютой

  Sources   Download

BSD-3-Clause

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

28/04 2015

dev-v1-stable

dev-v1-stable http://www.skeeks.com/

Модуль для работы с деньгами и валютой

  Sources   Download

BSD-3-Clause

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

28/04 2015

1.0.9

1.0.9.0 http://www.skeeks.com/

Модуль для работы с деньгами и валютой

  Sources   Download

BSD-3-Clause

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

03/04 2015

1.0.8

1.0.8.0 http://www.skeeks.com/

Модуль для работы с деньгами и валютой

  Sources   Download

BSD-3-Clause

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

03/04 2015

1.0.7

1.0.7.0 http://www.skeeks.com/

Модуль для работы с деньгами и валютой

  Sources   Download

BSD-3-Clause

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

31/03 2015

1.0.6

1.0.6.0 http://www.skeeks.com/

Модуль для работы с деньгами и валютой

  Sources   Download

BSD-3-Clause

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

24/03 2015

1.0.5

1.0.5.0 http://www.skeeks.com/

Модуль для работы с деньгами и валютой

  Sources   Download

BSD-3-Clause

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

18/03 2015

1.0.4

1.0.4.0 http://www.skeeks.com/

Модуль для работы с деньгами и валютой

  Sources   Download

BSD-3-Clause

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

23/02 2015

1.0.3

1.0.3.0 http://www.skeeks.com/

Модуль для работы с деньгами и валютой

  Sources   Download

BSD-3-Clause

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

18/02 2015

1.0.2

1.0.2.0 http://www.skeeks.com/

Модуль для работы с деньгами и валютой

  Sources   Download

BSD-3-Clause

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

03/02 2015

1.0.1

1.0.1.0 http://www.skeeks.com/

Модуль для работы с деньгами и валютой

  Sources   Download

BSD-3-Clause

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks

30/01 2015

1.0.0

1.0.0.0 http://www.skeeks.com/

Модуль для работы с деньгами и валютой

  Sources   Download

BSD-3-Clause

The Requires

 

by Semenov Alexander

yii2 app module cms money sx skeeks