1. Go to this page and download the library: Download enimiste/l5-math 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/ */
public function set[Yourattribute]Attribute($value){
$this->attribute['price'] = as_float_number($value);//__toString() function of VONumber is used to passe data to db
}
public function get[Yourattribute]Attribute($value){
return as_float_number($value);
}
interface Calculator {
/**
* Multiplication
*
* @param Number $l
* @param Number $r
*
* @return Number
*/
public function mult( Number $l, Number $r );
/**
* Calculate the TTC price from HT and TVA
*
* @param FloatNumber $ht
* @param FloatNumber $tva between 0 and 1
*
* @return FloatNumber
*/
public function ttc( FloatNumber $ht, FloatNumber $tva );
/**
* Add two Numbers
*
* @param Number $l
* @param Number $r
*
* @return Number
*/
public function add( Number $l, Number $r );
/**
* @param IntegerNumber $quantite
* @param FloatNumber $prixUnitaireHt
* @param FloatNumber $tva
*
* @return PriceResultDto
*/
public function price( IntegerNumber $quantite, FloatNumber $prixUnitaireHt, FloatNumber $tva );
/**
* Build TVA as value betwenn 0 and 1 from a value from 0 to 100
*
* @param FloatNumber $tva
*
* @return FloatNumber
*/
public function tva( FloatNumber $tva );
/**
* Sub two Numbers
* $l - $r
*
* @param Number $l
* @param Number $r
*
* @return Number
*/
public function sub( Number $l, Number $r );
}
class Product extends Model {
protected $fillable = ['title', 'price', 'tva'];
public function setPriceAttribute($value){
$this->attribute['price'] = as_float_number($value);
}
public function getPriceAttribute($value){
return as_float_number($value);
}
public function setTvaAttribute($value){
$this->attribute['tva'] = as_float_number($value);
}
public function getTvaAttribute($value){
return as_float_number($value);
}
}
class Order extends Model {
protected $fillable = ['amount', 'total_ht', 'total_ttc', 'product_id'];
public function setTotalHtAttribute($value){
$this->attribute['total_ht'] = as_float_number($value);
}
public function getTotalHtAttribute($value){
return as_float_number($value);
}
public function setTotalTtcAttribute($value){
$this->attribute['total_ttc'] = as_float_number($value);
}
public function getTotalTtcAttribute($value){
return as_float_number($value);
}
}
$p = Product::create(
[
'price' => 100.76,
'tva' => 20.0
]
);
$calc = calculator();
$o1 = Order::create(
[
'product_id' => $p->id,
'amount' => 33,
'total_ht' => $ht = $calc->mult($p->price, new Enimiste\Math\VO\IntegerNumber(33)),
'total_ttc' => $calc->ttc($ht, $calc->tva($p->tva))
]
);
//NB : $calc->tva convert a tva as % to float value between 0 and 1
//another way
$price = $calc->price(new Enimiste\Math\VO\IntegerNumber(78), $p->price, $calc->tva($p->tva));
$o2 = Order::create(
[
'product_id' => $p->id,
'amount' => $price->quantite,
'total_ht' => $price->ht,
'total_ttc' => $price->ttc
]
);