vaened/php-price-engine
Composer 安装命令:
composer require vaened/php-price-engine
包简介
A powerful pricing calculator for products and services, with comprehensive tax and discount calculations.
README 文档
README
The Price Calculation Library is a tool that allows you to perform complex calculations for prices, taxes, discounts, and charges in your applications. This library is based on the Brick\Money library to ensure precise monetary calculations.
Installation
You can install the library using composer.
composer require vaened/php-price-engine
Usage
Initializing the Cashier
To start using the Price Engine, create an instance of the any Cashier and provide an Amount.
$cashier = new SimpleCashier( Amount::taxable( Money::of(100, 'USD'), TaxCodes::any() ), quantity : 1, taxes : Taxes::from([ Tax\Inclusive::proportional(21, TaxCode::IVA), ]) );
Updating Quantity
You can update the quantity using the update() method, that will calculate the totals in the next summary.
$cashier->update(10);
Applying Discounts
To apply discounts, use the apply() method, that will calculate the totals in the next summary, and receive as parameter N amount
of Discount.
$cashier->apply( Discount::proportional(2)->named('NEW_USERS'), Discount::fixed(5)->named('PROMOTIONAL'), );
Adding Charges
To add charges, use the add() method, it will calculate the totals in the next summary, and received as parameter N amount
of Charge.
$cashier->add( Charge::proportional(5)->named('POS'), Charge::fixed(10)->named('DELIVERY') );
Obtaining Individual Totals
To obtain individual values of any price adjustment, you can use the tax() charge(), or discount() functions, all of which receive the code established during creation and return an instance of Adjustment
$cashier->taxes()->locate('IVA'); $cashier->charges()->locate('DELIVERY'); $cashier->discounts()->locate('NEW_USERS');
Obtaining Totals
To obtain the total, you can use individual functions, each of which returns an instance of Brick\Money.
$cashier->quantity(); $cashier->unitPrice(); $cashier->subtotal(); $cashier->taxes()->total(); $cashier->charges()->total(); $cashier->discounts()->total(); $cashier->total();
Configuration
Currently there are 2 built-in ways to handle calculations
Cashiers
-
SimpleCashier: This
cashieroperates based on the gross price concept. It means that the price provided will be cleaned to get the final price without tax. Adjustments are applied directly to the gross price and taxes are calculated separately after adjustments are applied -
RegularCashier: This
cashieroperates based on the concept of unit price + taxes. It means that the configured taxes will be maintained or added to the indicated price, and discounts and charges will be applied to this price with taxes included.
Choose the
cashierthat best suits your specific business needs and requirements. You can create your own cashier to fit any specific logic or rule related to your business. If you need to create additional cashiers or implement custom logic, you can do so by extendingCashier. This library provides a flexible foundation to handle various pricing scenarios effectively.
Amounts
There are 2 ways to create the amount.
-
Amount with applicable taxes: these amounts are subject to taxes, whether they are
inclusive taxesorexclusive taxesand can be defined as follows.Amount::taxable( Money::of(100, 'PEN'), TaxCodes::only(['IGV']) );
The tax codes establish what taxes are applicable for the amount
Allow All Only Allowed Allow Nothing TaxCodes::any() TaxCodes::only(['IGV', ...]) TaxCodes::none() -
Amounts without applicable taxes: These amounts are not subject to any tax and will not have taxes applied.
Amount::taxexempt( Money::of(10, 'PEN') );
These would be the same as creating a taxable amount but passing TaxCodes::none() as the allowed codes.
Taxes
Taxes can be established in two ways.
- Inclusive: Taxes included in the unit price, and will be cleared during calculations
use Vaened\PriceEngine\Adjustments\Taxation; $amount->impose([ Taxation\Inclusive::proportional(18, 'IGV'); // 18% Taxation\Inclusive::fixed(2, 'ISC'); // 2 PEN ]); // or $cashier = new RegularCashier( ... taxes : Taxes::from([ Taxation\Inclusive::proportional(18, 'IGV'); // 18% Taxation\Inclusive::fixed(2, 'ISC'); // 2 PEN ]) );
- Exclusive: Taxes not included in the unit price, and will be added for the final calculations.
use Vaened\PriceEngine\Adjustments\Taxation; $amount->impose([ Taxation\Exclusive::proportional(18, 'IGV'); // 18% Taxation\Exclusive::fixed(2, 'ISC'); // 2 PEN ]); // or $cashier = new RegularCashier( ... taxes : Taxes::from([ Taxation\Exclusive::proportional(18, 'IGV'); // 18% Taxation\Exclusive::fixed(2, 'ISC'); // 2 PEN ]) );
License
This library is licensed under the MIT License. For more information, please see the license file.
vaened/php-price-engine 适用场景与选型建议
vaened/php-price-engine 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 130.3k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2023 年 07 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 vaened/php-price-engine 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vaened/php-price-engine 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 130.3k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 20
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-07-24