précision sur 3 chiffres après la virgule pour les prix

This commit is contained in:
Olivier Legat 2021-11-18 14:47:57 +01:00
parent 3f275d8541
commit ec0a21dcf3
4 changed files with 44 additions and 8 deletions

View file

@ -125,7 +125,7 @@ class Product extends Model implements Buyable
if ($calculate) {
//weight converter
$weighted_price = $this->attributes['price'] * ($this->weight * WeightUnit::convert()[$this->weight_unit]);
return $this->priceHt = round($weighted_price * 100) / 100;
return $this->priceHt = round($weighted_price * 1000) / 1000;
} else {
//no calculations, alias of db property
return $this->priceHt = $this->attributes['price'];
@ -145,7 +145,7 @@ class Product extends Model implements Buyable
return $this->discountedPriceHt;
} else {
$discountedPriceHt = $this->priceHt - $this->discountAmount;
$roundingdiscountedPriceHt = round($discountedPriceHt * 100) / 100;
$roundingdiscountedPriceHt = round($discountedPriceHt * 1000) / 1000;
return $this->discountedPriceHt = $roundingdiscountedPriceHt;
}
}
@ -163,7 +163,8 @@ class Product extends Model implements Buyable
} else {
$priceTtc = $this->priceHt * ( 1 + ( $this->tax->rate / 100));
//on travaille avec des centimes puis on redivise par 100 pour avoir des unités monetaires
$roundingPriceTtc = round($priceTtc * 100) / 100;
//on augmente la précision à 3 digits, l'affichage formate les prix sur 2 décimales
$roundingPriceTtc = round($priceTtc * 1000) / 1000;
return $this->priceTtc = $roundingPriceTtc;
}
}
@ -181,7 +182,8 @@ class Product extends Model implements Buyable
} else {
$discountedPriceTtc = $this->discountedPriceHt * ( 1 + ( $this->tax->rate / 100));
//on travaille avec des centimes puis on redivise par 100 pour avoir des unités monetaires
$roundingDiscountedPriceTTC = round($discountedPriceTtc * 100) / 100;
//on augmente la précision à 3 digits, l'affichage formate les prix sur 2 décimales
$roundingDiscountedPriceTTC = round($discountedPriceTtc * 1000) / 1000;
return $this->discountedPriceTtc = $roundingDiscountedPriceTTC;
}
}
@ -207,9 +209,9 @@ class Product extends Model implements Buyable
if ($promo_type == 1) {
$discountAmount = $applicablePromotion->discount_value;
} else {
//TODO : rounding au centime cf getDiscountedPriceTtcAttribute?
//on augmente la précision à 3 digits, l'affichage formate les prix sur 2 décimales
$discountAmount = $this->priceHt * ($applicablePromotion->discount_value / 100);
$discountAmount = round($discountAmount * 100) / 100;
$discountAmount = round($discountAmount * 1000) / 1000;
}
} else {
$discountAmount = 0;

View file

@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AlterPriceToProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('products', function (Blueprint $table) {
$table->decimal('price', 8, 3)->change();
$table->decimal('price_buy', 8, 3)->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('products', function (Blueprint $table) {
$table->decimal('price', 8, 2)->change();
$table->decimal('price_buy', 8, 2)->change();
});
}
}

View file

@ -37,7 +37,7 @@
<div class="form-row">
<div class="form-group col">
<label for="price">Prix en € *</label>
<input type="number" id="price" name="price" min="0" step=".01" class="form-control" value="{{ old('price') }}" required>
<input type="number" id="price" name="price" min="0" step=".001" class="form-control" value="{{ old('price') }}" required>
</div>
<div class="form-group col">
<label for="invoicing_unit">Unité de facturation

View file

@ -87,7 +87,7 @@
<div class="form-row">
<div class="form-group col">
<label for="price">Prix en € *</label>
<input type="number" id="price" name="price" min="0" step=".01" class="form-control" value="{{ old('price', $product->price)}}" required>
<input type="number" id="price" name="price" min="0" step=".001" class="form-control" value="{{ old('price', $product->price)}}" required>
</div>
<div class="form-group col">
<label for="invoicing_unit">Unité de facturation