diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..76bc197 --- /dev/null +++ b/composer.json @@ -0,0 +1,23 @@ +{ + "name": "sineld/money", + "description": "Simple yet useful money operations for php.", + "license": "MIT", + "authors": [{ + "name": "Sinan Eldem", + "email": "sinan@sinaneldem.com.tr", + "homepage": "https://sinaneldem.com.tr" + }], + "homepage": "https://github.com/sineld/money", + "keywords": [ + "php", + "money", + "math", + "laravel" + ], + "require": { + "php": ">=5.6" + }, + "autoload": { + "files": ["src/Money.php"] + } +} diff --git a/contributing.md b/contributing.md new file mode 100644 index 0000000..0d4189e --- /dev/null +++ b/contributing.md @@ -0,0 +1,19 @@ +# Contributing + +Contributions are welcome and will be fully credited. + +Contributions are accepted via Pull Requests on [Github](https://github.com/sineld/money). + +# Things you could do +Send new features to improve the package. + +## Pull Requests + +- **Document any change in behaviour** - Make sure the `readme.md` and any other relevant documentation are kept up-to-date. + +- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. + +- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. + + +**Happy coding**! diff --git a/license.md b/license.md new file mode 100644 index 0000000..6d77e4f --- /dev/null +++ b/license.md @@ -0,0 +1,13 @@ +MIT License + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..aacbc68 --- /dev/null +++ b/readme.md @@ -0,0 +1,315 @@ +# sineld/money + +[![Latest Version][badge-release]][release] +[![Total Downloads][badge-downloads]][downloads] + +`sineld/money` is a PHP library to make working with money easier! No static properties or methods! + +Any number you have passed to the class will automatically prepared to make math operations. +Class uses `,` for thousands and `.` for decimals. + +Package can be used with any framework or spagetty application. Just mail me if anything goes wrong. + +## Installation + +Via Composer + +``` bash +$ composer require sineld/money +``` + +Add usecase to the top of your file + +``` bash +use Sineld\Money\Money; +``` + +Start using. + +## Non-Composer Users + +Simply copy Money.php in src folder to your project and begin using. No extra dependencies. + +### Request method aliases + +For parametres to use with methods. + +##### money->setDecimals(default = 2) +##### money->addTax(default = 18) +##### money->removeTax(default = 18) +##### money->setLocaleActive(default = false) +##### money->setLocaleCode(default = TRL) +##### money->setLocalePosition(default = prefix, (use "suffix" instead of reverse)) + +## Usage Examples + +All in one place: + +``` bash +make($number1) // Create a new Money instance. + ->setDecimals(4) // Set decimals size. + ->sum($number2) // Add $number2 variable(s) value to the $money + ->subtract($number2) // Remove $number2 variable(s) value from the $money + ->multiply('3') // Multiply $money with $numbers variable(s) value. + ->divide('3') // Divide $money with $numbers variable(s) value. + ->addTax(18) // Add $percent variable to the $money with calculated value. + ->removeTax(18) // Remove $percent variable to the $money with calculated value. + ->setLocaleActive(true) // Enable Locale Usage. + ->setLocaleCode('₺ ') // Set Locale Code preference + ->setLocalePosition('prefix') // Set Locale Position preference + + // ->getTax(); // Return calculated $taxAmount variable. + ->get(); // Return $money variable according to the locale usage. + // ->all(); // Return the $money and $taxAmount variables in a array. + +var_dump($money); +``` + +Basic Sum Operation: + +``` bash +make($number1) + ->sum($number2) + ->get(); + +echo $money; +``` + +Basic Sum Operation with Two numbers: + +``` bash +make($number1) + ->setDecimals(4) + ->sum($number2, $number3) + // ->sum($number2, $number3, $number4, ...) // add parametres as much as you need + ->get(); + +echo $money; +``` + +Basic Subtract Operation: + +``` bash +make($number1) + ->subtract($number2) + ->get(); + +echo $money; +``` + +Basic Subtract Operation with Two numbers: + +``` bash +make($number1) + ->setDecimals(4) + ->subtract($number2, $number3) + // ->subtract($number2, $number3, $number4, ...) // add parametres as much as you need + ->get(); + +echo $money; +``` + +Basic Sum and Subtract Operations in one place: + +``` bash +make($number1) + ->setDecimals(4) + ->sum($number2) + ->subtract($number3) + ->get(); + +echo $money; +``` + +Basic Multiplication and Division Operations in one place: + +``` bash +make($number1) + ->setDecimals(0) + ->multiply($number2) + ->divide($number3) + ->get(); + +echo $money; +``` + +Basic Tax Operations: + +``` bash +make($number1) + ->setDecimals(2) + ->addTax($taxPercent) + ->get(); + +// tax added number +echo $money; + +$tax = (new Money()) + ->make($number1) + ->setDecimals(2) + ->addTax($taxPercent) + ->getTax(); + +// calculated tax after addtition +echo $tax; + +$money = (new Money()) + ->make($number1) + ->setDecimals(2) + ->addTax($taxPercent) + ->all(); + +// tax added number and calculated tax together +// var_dump($money); +echo $money['amount']; +echo $money['tax']; +``` + +Remove tax percent from money: + +``` bash +make($number1) + ->setDecimals(2) + ->removeTax($taxPercent) + ->get(); + +echo $money; +``` + +Remove tax percent from money and add new tax: + +``` bash +make($number1) + ->setDecimals(2) + ->removeTax($taxPercent1rst) + ->addTax($taxPercent2nd) + ->get(); + +echo $money; +``` + +Enable Locale String Output in the Prefix: + +``` bash +make($number1) // Create a new Money instance. + ->setDecimals(4) // Set decimals size. + ->setLocaleActive(true) // Enable Locale Usage. + ->setLocaleCode('₺ ') // Set Locale Code preference + ->setLocalePosition('prefix') // Set Locale Position preference + + ->get(); // Return $money variable according to the locale usage. + +echo $money; +``` + +Enable Locale String Output in the Suffix: + +``` bash +make($number1) // Create a new Money instance. + ->setDecimals(4) // Set decimals size. + ->setLocaleActive(true) // Enable Locale Usage. + ->setLocaleCode(' €') // Set Locale Code preference + ->setLocalePosition('suffix') // Set Locale Position preference + + ->get(); // Return $money variable according to the locale usage. + +echo $money; +``` + +## Contributing + +Please see [contributing.md](contributing.md) for details and a todolist. + +## Security + +If you discover any security related issues, please email author email instead of using the issue tracker. + +## Credits + +- [Sinan Eldem](https://www.sinaneldem.com.tr) + +## License + +Please see the [license file](license.md) for more information. + +[badge-release]: https://img.shields.io/packagist/v/sineld/money.svg?style=flat-square +[badge-downloads]: https://img.shields.io/packagist/dt/sineld/money.svg?style=flat-square + +[release]: https://packagist.org/packages/sineld/money +[downloads]: https://packagist.org/packages/sineld/money diff --git a/src/Money.php b/src/Money.php new file mode 100644 index 0000000..7c1fde1 --- /dev/null +++ b/src/Money.php @@ -0,0 +1,271 @@ + + * @license http://opensource.org/licenses/MIT MIT + * + * @see readme.md for Documentation + * @see https://packagist.org/packages/sineld/money Packagist + * @see https://github.com/sineld/money GitHub + */ + +namespace Sineld\Money; + +class Money +{ + private $amount; + private $taxRate = 18; + private $decimals = 2; + private $taxAmount = 0; + private $localeCode = 'TRL'; + private $localeActive = false; + private $localePrefix; + private $localeSuffix; + private $localePosition = 'prefix'; + + /** + * Creates a new Money instance. + * + * @param mixed $amount + */ + public function make($amount) + { + $this->amount = $amount; + + return $this; + } + + /** + * Set decimals size. + * + * @param int $decimals + */ + public function setDecimals($decimals) + { + $this->decimals = is_numeric($decimals) ? $decimals : $this->decimals; + + return $this; + } + + /** + * Enable Locale Usage. + * + * @param bool $bool + */ + public function setLocaleActive($bool) + { + $this->localeActive = $bool; + + return $this; + } + + /** + * Set Locale Code preference. + * + * @param mixed $code + */ + public function setLocaleCode($code) + { + $this->localeCode = $code; + + return $this; + } + + /** + * Set Locale Position preference + * Use "prefix" to display Locale Code on left, "suffix" for right. + * + * @param mixed $position + */ + public function setLocalePosition($position) + { + $sides = ['prefix', 'suffix']; + + $this->localePosition = in_array($position, $sides) ? $position : $this->localePosition; + + foreach ($sides as $side) { + if ($side == $this->localePosition) { + $this->{'locale'.ucfirst($side)} = $this->localeCode; + } else { + $this->{'locale'.ucfirst($side)} = null; + } + } + + return $this; + } + + /** + * Add $numbers variable(s) value to the $amount + * Seperate multiple variables with comma. + * + * @param mixed $numbers + */ + public function sum(...$numbers) + { + foreach ($numbers as $number) { + $this->amount = $this->clear($this->amount) + $this->clear($number); + } + + return $this; + } + + /** + * Remove $numbers variable(s) value from the $amount + * Seperate multiple variables with comma. + * + * @param mixed $numbers + */ + public function subtract(...$numbers) + { + foreach ($numbers as $number) { + $this->amount = $this->clear($this->amount) - $this->clear($number); + } + + return $this; + } + + /** + * Multiply $amount with $numbers variable(s) value. + * + * @param mixed $numbers + */ + public function multiply($number) + { + $this->amount = $this->clear( + $this->amount + ) * $this->clear( + $number + ); + + return $this; + } + + /** + * Divide $amount with $numbers variable(s) value. + * + * @param mixed $numbers + */ + public function divide($number) + { + $this->amount = $number != 0 + ? $this->clear($this->amount) / $this->clear($number) + : 0; + + return $this; + } + + /** + * Add $percent variable to the $amount with calculated value. + * + * @param mixed $percent + */ + public function addTax(int $percent = null) + { + $this->taxAmount = $this->clear( + $this->amount + ) * ( + $this->clear($percent ?? $this->taxRate) / 100 + ); + + $this->amount = $this->clear($this->amount) + $this->taxAmount; + + return $this; + } + + /** + * Remove $percent variable to the $amount with calculated value. + * + * @param mixed $percent + */ + public function removeTax(int $percent = null) + { + $this->taxAmount = $this->amount - ( + $this->clear($this->amount) / ( + 1 + ($this->clear($percent ?? $this->taxRate) / 100) + ) + ); + + $this->amount = $this->amount - $this->taxAmount; + + return $this; + } + + /** + * Return $amount variable according to the locale usage. + * + * @param none + */ + public function get() + { + $this->format(); + + if ($this->localeActive) { + return sprintf('%s%s%s', $this->localePrefix, $this->amount, $this->localeSuffix); + } + + return $this->amount; + } + + /** + * Return calculated $taxAmount variable. + * + * @param none + */ + public function getTax() + { + return $this->taxAmount; + } + + /** + * Return the $amount and $taxAmount variables in array. + * + * @param none + */ + public function all() + { + return [ + 'amount' => $this->get(), + 'tax' => $this->getTax(), + ]; + } + + /** + * Format the $amount and $taxAmount variables for a clear display. + * + * @param none + */ + private function format() + { + $this->amount = number_format( + floatval($this->clear($this->amount)), + $this->decimals, + ',', + '.' + ); + + $this->taxAmount = number_format( + floatval($this->clear($this->taxAmount)), + $this->decimals, + ',', + '.' + ); + + return $this; + } + + /** + * Remove commas from $number variable to the make it available + * for the operations. + * + * @param mixed $percent + */ + private function clear($number) + { + return str_replace(',', '', $number); + } +}