enimiste/l5-math

Laravel 5 数学计算,数字表示包

v1.1.1 2017-01-23 16:41 UTC

This package is auto-updated.

Last update: 2024-08-29 04:03:35 UTC


README

如果您想要构建一个管理财务数字(增值税、价格等)的Laravel 5应用程序,则此包适合您。由于浮点数的浮点部分,PHP浮点数并不精确。

浮点数

对于十进制数字,您应该使用FloatNumber类。

辅助函数

calculator 获取计算器实例

 $calculator = calculator();
 $calculator->add(.. , ...);
 ....

check_is_integer 检查一个值是否为整数。PHP的is_int不会检查作为字符串给出的整数值

as_float_number 将给定值转换为FloatNumber实例。默认比例为2

用法

将服务提供者和外观添加到config/app.php文件中

    Enimiste\L5Math\Providers\L5MathServiceProvider::class;
    'Calculator' => 'Enimiste\L5Math\Facades\CalculatorFacade::class;

您可以通过两种方式访问计算器

  • Calculator 外观类
  • calculator() 辅助函数

添加模型修改器

对于您想要将其从FloatNumber转换为DB decimal类型的字段,您应该添加

   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 );
}

示例

假设我们有一个Product和一个Ordre实体,我们必须在我们的应用程序中管理这些实体。

迁移

   //products table
   $table->increments('id');
   $table->string('title')->unique();
   $table->decimal('price', 8, 2);//000000.00
   $table->decimal('tva', 5, 2)->default(20.0);//between 0.00 and 100.00
   
   //orders table
   $table->increments('id');
   $table->unsignedInteger('amount');//count of product ordered
   $table->decimal('total_ht', 8, 2);// amount * price
   $table->decimal('total_ttc', 8, 2);
   $table->unsignedInteger('product_id');

模型

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
        ]
 );

Echo 1

 $products = Product::all();
 
 foreach($products as $p){
    echo 'Price : $' . $p->price->__toString() . PHP_EOL;
    echo 'Tva : ' . $p->tva->__toString() . '%' . PHP_EOL;
 }

将输出

 Price : $100.76
 Tva : 20.00%

Echo 2

 $orders = Order::all();
 
 foreach($orders as $o){
    echo 'Amount : $' . $o->amount . PHP_EOL;
    echo 'Total Ht : $' . $o->total_ht->__toString() . PHP_EOL;
    echo 'Total Ttc : ' . $o->total_ttc->__toString() . '%' . PHP_EOL;
    echo PHP_EOL;
 }

将输出

 Amount : 33
 Total Ht : $3 325.08
 Total Ttc : $3 990.10//exact is 3 990.096 but the scale is 2 sà 0.096 become 0.10
 
 Amount : 78
 Total Ht : $7 859.28
 Total Ttc : $9 431.14//exact is 9 431.136 but the scale is 2 sà 0.136 become 0.14