PHP Classes

PHP Price Calculator: Calculate prices using arbitrary precision math

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStarStar 82%Total: 282 All time: 7,595 This week: 39Up
Version License PHP version Categories
price-calculator 1.0.0BSD License5.3.0PHP 5, E-Commerce
Description 

Author

This package can calculate prices using arbitrary precision math.

It can calculate the prices of goods using different strategies to compute the final price.

Currently it implements strategies to calculate the sales prices based on a value set manually, adding percentage or a fixed value over the original price.

The calculations of the prices are done using arbitrary precision math to avoid losing precision over multiple operations.

Innovation Award
PHP Programming Innovation award nominee
June 2014
Number 6


Prize: One subscription to the PDF edition of the PHP Architect magazine
When dealing with money operations it is important to preserve the precision of the operation results as much as possible, so rounding errors do not result in serious financial losses.

This class uses arbitrary precision math to avoid rounding errors when performing operations on prices.

Manuel Lemos
Picture of Sergii Pryz
  Performance   Level  
Name: Sergii Pryz <contact>
Classes: 10 packages by
Country: Ukraine Ukraine
Age: 40
All time rank: 133921 in Ukraine Ukraine
Week rank: 178 Up4 in Ukraine Ukraine Up
Innovation award
Innovation award
Nominee: 4x

Example

<?php
/**
 * =======================================
 * Example of usage Price Calculator
 * =======================================
 */

namespace PriceCalculator;
use
PriceCalculator\Strategy\PriceNet\PercentOver;

// PriceCalculator Autoload
require_once ('./src/Autoload.php');

// shop buy apples by 10$ from farmer
$price_purchase = 10;

// shop has 3% over purchase price
$strategy = new PercentOver(array('percent_over' => 3));

// vat is 23%
$vat_percent = 23;

// calculate price
$calculator = new PriceCalculator($strategy, $vat_percent);

$price = $calculator->setPricePurchase($price_purchase)
                    ->
getPrice();

var_dump($price); // output: array(3) {
                    // ["price_net"]=>
                    // string(5) "10.30"
                    // ["price_gross"]=>
                    // string(7) "12.6690"
                    // ["vat_value"]=>
                    // string(6) "2.3690"
                    //}


Details

PriceCalculator ============== Introduction ------------ Library helps calculate products sales price components such as net, gross, vat with using BCMath (http://www.php.net/manual/en/book.bc.php) for all arithmetics operations. Moreover PriceCalculator offers several algorithms to build price net: * Fixed price over purchase price * Fixed percent over purchase price * Manually set net without any margin Additionally it is possible extend library by adding own net price strategy. ### The areas of usage * Calculation product sales price * Form price that's includes several fees like delivery, insurance etc. * Build price that vary from region * Simply calculate VAT, gross, net ### Examples Example can be found here: `/Example.php`

  Files folder image Files (19)  
File Role Description
Files folder imagedocs (1 directory)
Files folder imagesrc (1 file, 1 directory)
Files folder imagetests (2 files, 1 directory)
Accessible without login Plain text file CHANGELOG.md Data Documentation
Accessible without login Plain text file Example.php Example Example script
Accessible without login Plain text file LICENSE.txt Lic. Documentation
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (19)  /  docs  
File Role Description
Files folder imageuml (1 file)

  Files folder image Files (19)  /  docs  /  uml  
File Role Description
  Accessible without login Image file class.diagram.png Data Auxiliary data

  Files folder image Files (19)  /  src  
File Role Description
Files folder imagePriceCalculator (4 files, 1 directory)
  Accessible without login Plain text file Autoload.php Aux. Class source

  Files folder image Files (19)  /  src  /  PriceCalculator  
File Role Description
Files folder imageStrategy (2 files, 1 directory)
  Plain text file AbstractPriceCalculator.php Class Class source
  Plain text file Exception.php Class Class source
  Plain text file PriceCalculator.php Class Class source
  Plain text file PriceCalculatorInterface.php Class Class source

  Files folder image Files (19)  /  src  /  PriceCalculator  /  Strategy  
File Role Description
Files folder imagePriceNet (3 files)
  Plain text file AbstractPriceNet.php Class Class source
  Plain text file PriceNetInterface.php Class Class source

  Files folder image Files (19)  /  src  /  PriceCalculator  /  Strategy  /  PriceNet  
File Role Description
  Plain text file Manual.php Class Class source
  Plain text file PercentOver.php Class Class source
  Plain text file PriceOver.php Class Class source

  Files folder image Files (19)  /  tests  
File Role Description
Files folder imagesrc (1 directory)
  Accessible without login Plain text file bootstrap.php Aux. Class source
  Accessible without login Plain text file phpunit.xml Data Auxiliary data

  Files folder image Files (19)  /  tests  /  src  
File Role Description
Files folder imagePriceCalculator (2 files)

  Files folder image Files (19)  /  tests  /  src  /  PriceCalculator  
File Role Description
  Accessible without login Plain text file BaseTest.php Test Class source
  Accessible without login Plain text file CalculatorTest.php Test Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:282
This week:0
All time:7,595
This week:39Up
 User Ratings  
 
 All time
Utility:91%StarStarStarStarStar
Consistency:91%StarStarStarStarStar
Documentation:100%StarStarStarStarStarStar
Examples:91%StarStarStarStarStar
Tests:83%StarStarStarStarStar
Videos:-
Overall:82%StarStarStarStarStar
Rank:7