gestion des prix pour articles facturé au poids et vendu à la pièce

(necessite la gestion stricte des unités de vente)
This commit is contained in:
Olivier Legat 2021-09-02 12:34:43 +02:00
parent e8d29e3733
commit 8d71046626
5 changed files with 88 additions and 11 deletions

View file

@ -39,9 +39,9 @@ class CreateAdmin extends Command
*/
public function handle()
{
$firstname = $this->ask('What is the firstname?','Informatique');
$lastname = $this->ask('What is the lastname?','Internet');
$email = $this->ask('What is the email?', 'informatique.internet@ldc.fr');
$firstname = $this->ask('What is the firstname?','Equipe');
$lastname = $this->ask('What is the lastname?','Web');
$email = $this->ask('What is the email?', 'equipe.web@ldc.fr');
$pwd = $this->secret('What is the password?');
$user = new User();

45
app/Enums/WeightUnit.php Normal file
View file

@ -0,0 +1,45 @@
<?php
namespace App\Enums;
use Kwaadpepper\Enum\BaseEnum;
/**
* @method static self g()
* @method static self kg()
* @method static self ton()
*/
final class WeightUnit extends BaseEnum
{
protected static function values(): array
{
return [
'g' => 'G',
'kg' => 'KG',
'ton' => 'T',
];
}
protected static function labels(): array
{
return [
'g' => __('Gramme'),
'kg' => __('Kg'),
'ton' => __('Tonne'),
];
}
public static function convert(): array
{
return [
'G' => 0.001,
'KG' => 1,
'T' => 1000,
];
}
public function __toString(): string
{
return (string)$this->label;
}
}

View file

@ -2,6 +2,9 @@
namespace App\Models;
use App\Enums\InvoicingUnit;
use App\Enums\SellUnit;
use App\Enums\WeightUnit;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
@ -107,8 +110,26 @@ class Product extends Model implements Buyable
if(isset($this->priceHt)) {
return $this->priceHt;
} else {
//no calculations, alias of db property
return $this->priceHt = $this->attributes['price'];
//tenir compte de l'unité de facturation et de l'unité de vente (qui doit être stricte)
//si facturation KG et vente piece
// prendre le poids, convertir en KG => pre-requis l'unité de poids doit être un enum
// multiplier le $this->attributes['price'] par les KG
//sinon
// value => value
if (config('bosettings.sell_unit_management') == "strict") {
$calculate = ($this->invoicing_unit == InvoicingUnit::kgm()->value && $this->sell_unit == SellUnit::pce()->value);
} else {
$calculate = false;
}
if ($calculate) {
//weight converter
$weighted_price = $this->attributes['price'] * ($this->weight * WeightUnit::convert()[$this->weight_unit]);
return $this->priceHt = round($weighted_price * 100) / 100;
} else {
//no calculations, alias of db property
return $this->priceHt = $this->attributes['price'];
}
}
}
@ -226,10 +247,10 @@ class Product extends Model implements Buyable
// //Buyable interface
// public function getBuyablePrice($options = null)
// {
// return $this->discountedPriceHt;
// }
public function getBuyablePrice($options = null)
{
return $this->priceHt;
}
//Relations
public function range()

View file

@ -245,6 +245,7 @@ return [
'SellUnit' => \App\Enums\SellUnit::class,
'OrderUnit' => \App\Enums\OrderUnit::class,
'InvoicingUnit' => \App\Enums\InvoicingUnit::class,
'WeightUnit' => \App\Enums\WeightUnit::class,
],
];

View file

@ -166,8 +166,18 @@
<input type="number" id="weight" name="weight" min="0" step=".01" class="form-control" value="{{ old('weight', $product->weight)}}">
</div>
<div class="form-group col">
<label for="weight_unit">Unité de poids</label>
<input type="text" id="weight_unit" name="weight_unit" class="form-control" value="{{ old('weight_unit', $product->weight_unit)}}">
<label for="weight_unit">Unité de poids
<span class="tipped"
data-tippy-content="Unité de poids normalisée">
<i class="fas fa-question-circle"></i>
</span>
</label>
<select class="custom-select col-md-9" id="weight_unit" name="weight_unit">
@foreach (WeightUnit::toArray() as $wheightunit_type)
<option value="{{$wheightunit_type->value}}" @if (old('weight_unit', $product->weight_unit) == $wheightunit_type->value)selected @endif>{{$wheightunit_type->label}}</option>
@endforeach
</select>
{{--<input type="text" id="weight_unit" name="weight_unit" class="form-control" value="{{ old('weight_unit', $product->weight_unit)}}">--}}
</div>
<div class="form-group col"></div>
</div>