skeeks / cms-module-money
金钱和货币处理模块
Requires
- skeeks/cms: ~5.0 || dev-master
- dev-master
- 3.1.1
- 3.1.0
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 3.0.0-alpha
- 2.5.0
- 2.4.2
- 2.4.1
- 2.4.0
- 2.3.0
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.0
- 2.0.14
- 2.0.13
- 2.0.12
- 2.0.11
- 2.0.10
- 2.0.9
- 2.0.8
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-shop-update
- dev-release
- dev-v1-stable
This package is auto-updated.
Last update: 2024-08-29 04:19:14 UTC
README
安装
通过以下方式安装此扩展程序:通过 composer。
运行以下命令
php composer.phar require --prefer-dist skeeks/cms-module-money "*"
或者添加以下内容
"skeeks/cms-module-money": "*"
配置应用程序
'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库,但需要对其进行大量修改。因此,它没有以纯形式连接。
主要工作是使金钱的操作更加透明。以下是我们在这个库中透明地解决的问题的示例。(10美元 + 154卢布 + 12美元)= 结果需要以GBP显示,对于de_DE区域。
直接示例
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 £
安装
- 通过composer进行标准安装
php composer.phar require --prefer-dist skeeks/cms-module-money "*"
或者添加以下内容
"skeeks/cms-module-money": "*"
添加到您的composer.json
文件的require部分。
- 安装迁移
php yii migrate --migrationPath=vendor/skeeks/cms-module-money/migrations
示例和用法
原始示例
创建一个Money对象并访问其货币值
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();
上面的代码生成以下输出
100
从字符串值创建一个Money对象
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();
上面的代码生成以下输出
1234
使用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();
上面的代码生成以下输出
100
请注意,没有针对土耳其里拉(TRY)的特定于货币的Money
子类,因为在PHP中TRY不是一个有效的类名。
使用PHP的内置NumberFormatter格式化Money对象
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);
上面的代码生成以下输出
1,00 €
使用Money对象进行基本算术运算
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();
上面的代码生成以下输出
-100
300
100
200
比较Money对象
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));
上面的代码生成以下输出
bool(true)
bool(false)
bool(false)
bool(true)
int(-1)
int(0)
int(1)
compareTo()
方法返回一个整数,小于、等于或大于零,如果认为一个Money
对象的值分别小于、等于或大于另一个Money
对象的值。
您可以使用compareTo()
方法使用PHP的内置排序函数对Money
对象的数组进行排序
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"; }
上面的代码生成以下输出
100
200
300
将一个Money对象表示的货币值分配给N个目标
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"; }
上面的代码生成以下输出
10
10
10
10
10
10
10
10
10
9
使用比率列表将一个Money对象表示的货币值分配给
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"; }
上面的代码生成以下输出
2
3
从Money对象表示的货币值中提取百分比(和子总额)
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() );
上面的代码生成以下输出
10000 = 8265 + 1735
请注意,这将从已经包含百分比的货币值中提取百分比。如果您想获取货币值的百分比,应使用乘法(例如,使用multiply(0.21)
计算一个表示为Money对象的货币值的21%)。
SkeekS CMS (Yii2) — 快速、简单、高效!
skeeks.com | cms.skeeks.com | marketplace.cms.skeeks.com