Computation

Handling mathematical models

Returns eComputation object. It contains methods for setting variables and formulas and getting result values.

Arguments

$type
string Name of the predefiend computation

Calculate vat included/excluded

Set cross-computation for vat included/excluded price:

$Computation = $this->get('computation');
$Computation->newVar('vat')->setDefault(23);
$Computation->newVar('price')->setFormula('priceVat/(100+vat)*100');
$Computation->newVar('priceVat')->setFormula('price*(100+vat)/100');

$Computation->setValue('vat', 24, false); // Prevent calculations (values missing).
$Computation->setValue('price',100);    // Triggers all calculations.
$computation->getValue('priceVat');     // --> 124

Using Field -object

If you set Field object as variable value, all changes made to variable are mirrored to Field:

$Computation->setValue('priceTotal',$Order->total);
$Computation->setValue('amount',4);
// $Order->total --> 496

Documentation

You can set title and description to computation and variables for printing documentation:

$Computation = $this->get('computation')
    ->setTitle('Count total')
    ->setDescription('Count total amount from amount and price');

$Total = $Computation->newVar('total')
    ->setFormula('amount*price')
    ->setDefault(0)
    ->setTitle('Total price')
    ->setDescription('Multiplies unit price with amount')
    ->setUnit('eur');

$Computation->renderDoc();

Computation types

Price

Counting vat, amount and rate for given price:

$Price = $this->get('computation','price')
    ->setValue('price',100)
    ->setValue('vat',24)
    ->setValue('amount',2)
    ->setValue('currencyRate', 0.988);

$Price->getValue('priceVat'); // --> 124
$Price->getValue('priceTotal'); // --> 200
$Price->getValue('basePriceTotalVat'); // --> 245,024
Variables Default  Description
price 0  Price vat excluded
vat 24  Vat percent
priceVat 0  Price vat included
vatAmount 0  Vat amount
amount 0  Amount
priceTotal 0  Total price vat excluded
vatTotalAmount 0  Total vat amount.
priceTotalVat 0  Total price vat included
currency EUR Currency that price is given in. 
currencyBase EUR  Base currency that is calculated with rate.
currencyRate 1  Currency rate against base currency.
basePrice 0  Price vat excluded in base currency.
basePriceVat 0  Price vat included in base currency.
baseVatAmount 0  Vat amount
basePriceTotal 0  Total price vat excluded
baseVatTotalAmount 0  Total vat amount.
basePriceTotalVat 0  Total price vat included

Full method list

Computation

Method Return  Description
setTitle($string) this  Set title for computation.
setDescription($string) this  Set additional description for computation.
newVar($name) Variable  Create new variable to computation.
getVar($name) Variable Get variable by given name.
setValue($name,$value[,$refresh]) this  Set value to given variable. Refresh: Calculate other values or not. Default: TRUE.
setValues($array[,$refresh]) this  Set values from key=>value -pairs. Refresh: Calculate other values or not. Default: TRUE.
getValue($name) mixed  Variable current value.
getString($name) mixed  Text presentation of current value (format, unit..)
renderDoc() string Render HTML documentation of defined computation and current values.
  • Also 'get' equivalents for all 'set' -methods.

Variable

Method Return  Description
setTitle($string) this Set Variable visible name.
setName($string) this Set Variable technical name.
setDescription($string) this Set additional description of Variable meaning.
setDefault($value) this Set default value.
setUnit($string) this Set value unit.
setFormula($needle) bool Set math equation.
setValue($value) this Set Variable value. If object, uses objects setValue() getValue() methods.
render() string Return value in visible form. (unit, decimals, format..)
__toString() string Alias to: render()