Money

Money

Money provides a quick and easy methods to manage your model money [150+ Currencies].

Moamen Eltouny (Raggi)
PHP Version : >= 7.2 Laravel Version : >= 6.0 License Support
Source Packagist Version Packagist Downloads

Install

Install the latest version using Composer

composer require pharaonic/laravel-money

Then publish the migration & config files

php artisan vendor:publish --tag=laravel-money
Configuration
// config/Pharaonic/money.php

return [

    // Default Language
    'language'  => 'en',

    // Default Currency
    'currency'  => 'USD',

    // Select specific currencies
    'only'      => [],

    // Except specific currencies
    'except'    => [],

    // Default fields names
    'fields'    => ['price']
];
Options
use Pharaonic\Laravel\Money\Facades\Money;

// Setting default currency
Money::setCurrency('USD');

// Setting default language
// Supported language [ar, en, fr, de, es]
Money::setLanguage('ar');

// Select specific currencies 
Money::only(['USD']);
    
// Except specific currencies
Money::except(['USD']);

// Get current language
$language 		= Money::getLanguage();

// Get currency Code, Name, Symbol
$currency_code 		= Money::getCurrencyCode();
$currency_name 		= Money::getCurrencyName();
$currency_symbol 	= Money::getCurrencySymbol();

// Get currencies list (code => name)
$currencies 		= Money::getCurrenciesList();
Inclusion
...
use Pharaonic\Laravel\Helpers\Traits\HasCustomAttributes;
use Pharaonic\Laravel\Money\HasMoney;

class Person extends Model
{
    use HasCustomAttributes, HasMoney;
    
    // You can include your all monies names here.
	protected $moneyAttributes = ['balance'];
    ...
}
Setting
// Setting money to exists person
$person = Person::find(1);
$person->money('balance', 'USD', 100);

// Setting money to new person with current currency ($currency_code)
$person = Person::create([
    'balance' => 10.00
    ...
]);
Getting
$person = Person::find(1);

// Get money with specific currency
echo $person->money('balance', 'USD');

echo $person->balance; 				    // 100.00
echo $person->balance->amount; 			// 100
echo $person->balance->withName(); 		// 100.00 USD
echo $person->balance->withSymbol();    // $ 100.00
echo $person->balance->toString() 		// one hundred dollars {PHP Extension intl}
Actions
$person = Person::find(1);

$person->balance->withdraw(0.50); 	// withdraw 50 cents
$person->balance->deposit(10.50); 	// deposit 10 dollars and 50 cents
$person->balance->reset();		    // resetting money to zero

$person->balance->isZero();		    // check if it equals Zero
$person->balance->isPositive();     // check if it's Positive amount
$person->balance->isNegative();     // check if it's Negative amount
Relationships & Scopes
// Getting monies with all currencies
$monies = $person->monies;

// Getting all People who has no monies
dd(Person::withoutMonies()->get());

// with Currency only
$pplWithMoney = Person::withMoney('USD')->get(); 
// with Currency and name
$pplWithMoney = Person::withMoney('USD', 'balance')->get(); 

// with Currencies only
$pplWithMoney = Person::withAnyMoney(['USD'])->get();
// with Currencies and names
$pplWithMoney = Person::withAnyMoney(['USD'], ['balance'])->get(); 
Aggregates
// Getting MIN Money
echo Person::minMoney('balance');
echo Person::minMoney('balance', 'USD');

// Getting MAX Money
echo Person::maxMoney('balance');
echo Person::maxMoney('balance', 'USD');

// Getting SUM All Monies
echo Person::sumMoney('balance');
echo Person::sumMoney('balance', 'USD');

// Getting SUM Negative Monies
echo Person::sumNegativeMoney('balance');
echo Person::sumNegativeMoney('balance', 'USD');

// Getting SUM Positive Monies
echo Person::sumPositiveMoney('balance');
echo Person::sumPositiveMoney('balance', 'USD');

// Getting Average Monies
echo Person::avgMoney('balance');
echo Person::avgMoney('balance', 'USD');

// Getting Count OF Monies Rows
echo Person::countMoney();
echo Person::countMoney('balance');
echo Person::countMoney(null, 'USD');
echo Person::countMoney('balance', 'USD');
Events
...
class Person extends Model
{
    ...
    /**
     * Setted Money with (Create/New/money method) Event
     *
     * @param string $name
     * @param string $currency
     * @param float $amount
     * @return void
     */
    public function setted(string $name, string $currency, float $amount)
    {
        //
    }
    
    /**
     * Withdrew Money Event
     *
     * @param string $name
     * @param string $currency
     * @param float $amount
     * @return void
     */
    public function withdrew(string $name, string $currency, float $amount)
    {
        //
    }

    /**
     * Deposited Money Event
     *
     * @param string $name
     * @param string $currency
     * @param float $amount
     * @return void
     */
    public function deposited(string $name, string $currency, float $amount)
    {
        //
    }

    /**
     * Reset Money Event
     *
     * @param string $name
     * @param string $currency
     * @return void
     */
    public function reset(string $name, string $currency)
    {
        //
    }
    ...
}