PHP code example of hansel23 / prices

1. Go to this page and download the library: Download hansel23/prices library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hansel23 / prices example snippets


$net     = new Money( 1672, 'EUR' );
$gross   = new Money( 1990, 'EUR' );
$vatRate = new VatRate( 19 );

$price = new GrossBasedPrice( $net, $gross, $vatRate );
$price->getGrossAmount(); //new Money( 1990, 'EUR' )
$price->getNetAmount(); //new Money( 1672, 'EUR' )
$price->getVatAmount(); //new Money( 318, 'EUR' )
$price->getVatRate(); //new VatRate( 19 )

$gross   = new Money( 1990, 'EUR' );
$vatRate = new VatRate( 19 );

$price = GrossBasedPrice::fromGrossAmount( $gross, $vatRate );
$price->getGrossAmount(); //new Money( 1990, 'EUR' )
$price->getNetAmount(); //new Money( 1672, 'EUR' )
$price->getVatAmount(); //new Money( 318, 'EUR' )
$price->getVatRate(); //new VatRate( 19 )

$net     = new Money( 1672, 'EUR' );
$vatRate = new VatRate( 19 );

$price = GrossBasedPrice::fromNetAmount( $net, $vatRate );
$price->getGrossAmount(); //new Money( 1990, 'EUR' )
$price->getNetAmount(); //new Money( 1672, 'EUR' )
$price->getVatAmount(); //new Money( 318, 'EUR' )
$price->getVatRate(); //new VatRate( 19 )

$grossBasedPrice = GrossBasedPrice::fromGrossAmount( new Money( 1990, 'EUR' ), new VatRate(19) );
$netBasedPrice   = NetBasedPrice::fromPrice( $price );

$netBasedPrice->getGrossAmount(); //new Money( 1990, 'EUR' )
$netBasedPrice->getNetAmount(); //new Money( 1672, 'EUR' )
$netBasedPrice->getVatAmount(); //new Money( 318, 'EUR' )
$netBasedPrice->getVatRate(); //new VatRate( 19 )

$vatRateByFloat = new VatRate( 21.7 );
$vatRateByInt = VatRate::fromInt( 2170 );
$vatRateByFloat->equals( $vatRateByInt ); //true

$unitPrice  = NetBasedPrice::fromGrossAmount( new Money( 10808, 'EUR' ) );
$totalPrice = $price->multiply( 10 );

$unitPrice->getGrossAmount(); //new Money( 10808, 'EUR' )
$unitPrice->getNetAmount(); //new Money( 9082, 'EUR' )
$totalPrice->getGrossAmount(); //new Money( 108076, 'EUR' )
$totalPrice->getNetAmount(); //new Money( 90820, 'EUR' )

$unitPrice  = GrossBasedPrice::fromNetAmount( new Money( 9082, 'EUR' ) );
$totalPrice = $price->multiply( 10 );

$unitPrice->getGrossAmount(); //new Money( 10808, 'EUR' )
$unitPrice->getNetAmount(); //new Money( 9082, 'EUR' )
$totalPrice->getGrossAmount(); //new Money( 108080, 'EUR' )
$totalPrice->getNetAmount(); //new Money( 90824, 'EUR' )

$price  = GrossBasedPrice::fromGrossAmount( new Money( 1000, 'EUR' ) );

$sum    = $price->add( GrossBasedPrice::fromGrossAmount( new Money( 1000, 'EUR' ) ) );
$sum->getGrossAmount(); //new Money( 2000, 'EUR' )

$difference = $price->subtract( GrossBasedPrice::fromGrossAmount( new Money( 1000, 'EUR' ) ) );
$difference->getGrossAmount(); //new Money( 0, 'EUR' )

$prices = [
    GrossBasedPrice::fromGrossAmount( new Money( 100, 'EUR' ), new VatRate( 19 ) ),
    FakePriceImplementation::fromGrossAmount( new Money( 300, 'EUR' ), new VatRate( 7 ) ),
];
$additionalPrice = GrossBasedPrice::fromGrossAmount( new Money( 100,  'EUR' ), new VatRate( 16.5 ) );

$totalPrice = new TotalPrice( 'EUR', $prices );
$totalPrice->addPrice( $additionalPrice );

$anotherTotalPrice = new TotalPrice( 
  'EUR', 
  [ 
    NetBasedPrice::fromGrossAmount( new Money( 200,  'EUR'  ), new VatRate( 16.5 ) ),
    FakePriceImplementation::fromGrossAmount( new Money( 300,  'EUR' ), new VatRate( 16.5 ) ),
  ] 
);

$totalPrice->addTotalPrice( $anotherTotalPrice );

$totalPrice->getPrices(); // Array with prices from $prices, $additionalPrice and the prices from $anotherTotalPrice
$totalPrice->getTotalGrossAmount(); // new Money( 1000, 'EUR' ) (100 + 300 + 100 + 200 + 300)
$totalPrice->getTotalNetAmount(); // new Money( 794, 'EUR' ) (100/1,19 + 300/1,07 + (200 + 300)/1,165) 
$totalPrice->getTotalVatAmount(); // new Money( 206, 'EUR ) (1000 - 794)

$prices = [
    GrossBasedPrice::fromGrossAmount( new Money( 100, 'EUR' ), new VatRate( 19 ) ),
    GrossBasedPrice::fromGrossAmount( new Money( 200, 'EUR' ), new VatRate( 19 ) ),
    FakePriceImplementation::fromGrossAmount( new Money( 300, 'EUR' ), new VatRate( 7 ) ),
];
$totalPrice = new TotalPrice( 'EUR', $prices );
$totalPrice->getPricesGroupedByVatRates(); 
/** 
  [ 
    1900 => [ 
      GrossBasedPrice::fromGrossAmount( new Money( 100, 'EUR' ), new VatRate( 19 ) ),
      GrossBasedPrice::fromGrossAmount( new Money( 200, 'EUR' ), new VatRate( 19 ) ),
    ],
    700 => [ FakePriceImplementation::fromGrossAmount( new Money( 300, 'EUR' ), new VatRate( 7 ) ) ]
  ]
**/

$prices = [
  GrossBasedPrice::fromGrossAmount( new Money( 100,  'EUR'  ), new VatRate( 19 ) ),
  FakePriceImplementation::fromGrossAmount( new Money( 300,  'EUR'  ), new VatRate( 19 ) ),
  NetBasedPrice::fromGrossAmount( new Money( 200,  'EUR'  ), new VatRate( 7 ) ),
];
$totalPrice = new TotalPrice( 'EUR', $prices );
json_encode( $totalPrice, JSON_PRETTY_PRINT ); 
/**
{
	"1900": [{
		"gross": {
			"amount": "100",
			"currency": "EUR"
		},
		"net": {
			"amount": "84",
			"currency": "EUR"
		},
		"vat": {
			"amount": "16",
			"currency": "EUR"
		}
	}, {
		"gross": {
			"amount": "300",
			"currency": "EUR"
		},
		"net": {
			"amount": "252",
			"currency": "EUR"
		},
		"vat": {
			"amount": "48",
			"currency": "EUR"
		}
	}],
	"700": [{
		"gross": {
			"amount": "200",
			"currency": "EUR"
		},
		"net": {
			"amount": "187",
			"currency": "EUR"
		},
		"vat": {
			"amount": "13",
			"currency": "EUR"
		}
	}]
}
**/