Awesome
<picture> <source media="(prefers-color-scheme: dark)" srcset="https://banners.beyondco.de/Laravel%20Commission.png?theme=dark&packageManager=composer+require&packageName=mkeremcansev%2Flaravel-commission&pattern=architect&style=style_1&description=A+flexible+package+to+calculate+and+log+commissions+in+Laravel.&md=1&showWatermark=1&fontSize=100px&images=receipt-tax"> <source media="(prefers-color-scheme: light)" srcset="https://banners.beyondco.de/Laravel%20Commission.png?theme=light&packageManager=composer+require&packageName=mkeremcansev%2Flaravel-commission&pattern=architect&style=style_1&description=A+flexible+package+to+calculate+and+log+commissions+in+Laravel.&md=1&showWatermark=1&fontSize=100px&images=receipt-tax"> <img alt="Package Image" src="https://banners.beyondco.de/Laravel%20Commission.png?theme=light&packageManager=composer+require&packageName=mkeremcansev%2Flaravel-commission&pattern=architect&style=style_1&description=A+flexible+package+to+calculate+and+log+commissions+in+Laravel.&md=1&showWatermark=1&fontSize=100px&images=receipt-tax"> </picture>What is Laravel Commission?
Laravel Commission is a powerful package designed to simplify the management and calculation of commissions within Laravel applications. This package provides a flexible and extensible system for defining various types of commissions, whether they are based on percentages, fixed amounts, or more complex criteria.
Key Features:
-
Multiple Commission Types: Laravel Commission allows you to define various commission types, such as percentage-based, fixed amount, or a combination of both. This flexibility enables developers to tailor commission structures to meet specific business requirements.
-
Dynamic Calculation: The package supports dynamic commission calculations based on the original product price or total price after applying other commissions. It accommodates different scenarios through the use of parameters such as
is_total
,min_amount
,max_amount
, androunding
. -
Comprehensive History Tracking: Laravel Commission provides functionality to track the history of commission calculations, allowing businesses to maintain an accurate record of commissions applied over time.
-
Integration with Eloquent Models: The package seamlessly integrates with Laravel's Eloquent ORM, enabling developers to associate commissions with various models, such as products, orders, or services.
-
Extensibility and Customization: Developers can easily extend the functionality of the package to create custom commission logic that suits their business needs. This makes it easy to adapt to different use cases and scenarios.
Use Cases:
-
E-commerce Platforms: Laravel Commission is particularly useful for e-commerce applications where various commission rates need to be applied based on different products, sales channels, or user roles.
-
Affiliate Programs: Businesses can manage affiliate commissions efficiently, calculating payouts based on sales generated by affiliates with flexible commission structures.
-
Sales Teams: Organizations can implement commission structures for sales representatives, rewarding them based on their performance and sales targets.
In summary, Laravel Commission is a versatile and robust solution for managing commissions in Laravel applications, providing developers with the tools they need to implement and customize commission calculations effectively. Whether you're building an e-commerce site, an affiliate platform, or a sales management system, this package can help streamline your commission management processes.
Installation
You can install the package via composer:
composer require mkeremcansev/laravel-commission
You can publish the migrations with:
php artisan vendor:publish --tag="laravel-commission-migrations"
You can run the migrations with:
php artisan migrate
You can publish the config file with:
php artisan vendor:publish --tag="laravel-commission-config"
Usage
-
Implement the Interface: Ensure that the model you want to apply commissions to implements the
HasCommissionInterface
. This interface defines the methods required for commission calculations. -
Use the Trait: Use the
HasCommission
trait within your model to gain access to the commission calculation methods. -
Implement the Method: Implement the
getCommissionableColumns()
method in your model to specify which columns can have commissions applied.use Mkeremcansev\LaravelCommission\Contracts\HasCommissionInterface; use Mkeremcansev\LaravelCommission\Traits\HasCommission; class YourModel extends Model implements HasCommissionInterface { use HasCommission; // Use the HasCommission trait // Implement the required method from the HasCommissionInterface public function getCommissionableColumns(): array { return ['price', 'total']; // Example columns that can have commissions applied } }
-
Calculate Commissions: Use the
calculate('price')
method to apply commissions to your model.$model = YourModel::find(1); $calculatedCommission = $model->calculate('price'); $calculatedCommission->totalCommissionAmount; // The total commission amount applied $calculatedCommission->totalIncludedPreviousCommissionAmount; // The total amount including previous commissions $calculatedCommission->totalAmount; // The total amount after commissions $calculatedCommission->originalAmount; // The original amount before commissions
Note: If you want to calculate based on a custom value instead of using the column value, you can provide a second parameter to the calculate method. For example,
$model->calculate('price', 100)
will calculate the commission based on the value100
instead of theprice
column value. For example;$model = YourModel::find(1); $calculatedCommission = $model->calculate('price', 100); $calculatedCommission->totalCommissionAmount; // The total commission amount applied $calculatedCommission->totalIncludedPreviousCommissionAmount; // The total amount including previous commissions $calculatedCommission->totalAmount; // The total amount after commissions $calculatedCommission->originalAmount; // The original amount before commissions
Important Note
The calculate()
method returns a \Mkeremcansev\LaravelCommission\Services\Contexts\CommissionCalculationResultContext
or array
or null
.
Null: If the model getCommissionableColumns()
method is return a empty array.
Array: If the model getCommissionableColumns()
method has multiple columns. (Return array of \Mkeremcansev\LaravelCommission\Services\Contexts\CommissionCalculationResultContext
)
CommissionCalculationResultContext: If the model getCommissionableColumns()
method has only one column.
Adding Custom Pipes After Commission Calculation
1. Creating a CustomPipe
Class
Create a pipe class that will run after the commission calculation is done. Each pipe
class receives the commission calculation context in the handle
method, allowing you to further process the result. In this example, we use the BaseCommissionCalculatorContext
class as a type hint for the context:
use Mkeremcansev\LaravelCommission\Services\Contexts\BaseCommissionCalculatorContext;
class CustomPipe
{
public function handle(BaseCommissionCalculatorContext $context, Closure $next)
{
// Custom logic after the commission is calculated
return $next($context);
}
}
2. Registering the CustomPipe
Class
Add the CustomPipe
class to the commission.pipes
configuration array in the config/commission.php
file. This configuration array defines the sequence of pipes that will be executed after the commission calculation:
'pipes' => [
CustomPipe::class,
],
If you want a commission not to be calculated after a specific date, refer to the following documentation:
Testing
composer test
Credits
License
The MIT License (MIT). Please see License File for more information.