Skip to content

Commit

Permalink
Configurable default measurement units
Browse files Browse the repository at this point in the history
Remove partly wrong default measurement units
from database, allow configuration of default
measurement units in config file shipping.php
with 'default' => true value and define correct
values it attribute getter method.
  • Loading branch information
netzknecht committed Nov 13, 2023
1 parent 8416c0b commit da095d0
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 49 deletions.
147 changes: 100 additions & 47 deletions packages/core/src/Base/Traits/HasDimensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,91 +3,144 @@
namespace Lunar\Base\Traits;

use Cartalyst\Converter\Laravel\Facades\Converter;
use Illuminate\Database\Eloquent\Casts\Attribute;

trait HasDimensions
{
/**
* Method when trait is booted.
* Initialize the trait
*
* @return void
*/
public static function bootHasDimensions()
public function initializeHasDimensions()
{
self::retrieved(function ($model) {
$model->mergeCasts([
'length_value' => 'float',
'width_value' => 'float',
'height_value' => 'float',
'volume_value' => 'float',
'weight_value' => 'float',
]);
});
$this->mergeCasts([
'length_value' => 'float',
'width_value' => 'float',
'height_value' => 'float',
'volume_value' => 'float',
'weight_value' => 'float',
]);
}

/**
* Getter for the length attribute.
*
* @return \Cartalyst\Converter\Converter
* Get the length unit
*/
public function getLengthAttribute()
protected function lengthUnit(): Attribute
{
$unit = $this->length_unit ?: 'mm';
return Attribute::make(
get: fn (?string $value) => $value
?? collect(config('lunar.shipping.measurements.length'))->where('default')->keys()->first()
?? 'mm'
);
}

return Converter::from("length.{$unit}")->value($this->length_value ?: 0);
/**
* Get the width unit
*/
protected function widthUnit(): Attribute
{
return Attribute::make(
get: fn(?string $value) => $value
?? collect(config('lunar.shipping.measurements.length'))->where('default')->keys()->first()
?? 'mm'
);
}

/**
* Getter for the width attribute.
*
* @return \Cartalyst\Converter\Converter
* Get the height unit
*/
public function getWidthAttribute()
protected function heightUnit(): Attribute
{
$unit = $this->width_unit ?: 'mm';
return Attribute::make(
get: fn(?string $value) => $value
?? collect(config('lunar.shipping.measurements.length'))->where('default')->keys()->first()
?? 'mm'
);
}

return Converter::from("length.{$unit}")->value($this->width_value ?: 0);
/**
* Get the weight unit
*/
protected function weightUnit(): Attribute
{
return Attribute::make(
get: fn(?string $value) => $value
?? collect(config('lunar.shipping.measurements.weight'))->where('default')->keys()->first()
?? 'kg'
);
}

/**
* Getter for height attribute.
*
* @return \Cartalyst\Converter\Converter
* Get the volume unit
*/
protected function volumeUnit(): Attribute
{
return Attribute::make(
get: fn(?string $value) => $value
?? collect(config('lunar.shipping.measurements.volume'))->where('default')->keys()->first()
?? 'l'
);
}

/**
* Get the length
*/
public function getHeightAttribute()
protected function length(): Attribute
{
$unit = $this->height_unit ?: 'mm';
return Attribute::make(
get: fn() => Converter::from("length.{$this->length_unit}")->value($this->length_value ?? 0)
);
}

return Converter::from("length.{$unit}")->value($this->height_value ?: 0);
/**
* Get the width
*/
protected function width(): Attribute
{
return Attribute::make(
get: fn() => Converter::from("length.{$this->width_unit}")->value($this->width_value ?? 0)
);
}

/**
* Getter for weight attribute.
*
* @return \Cartalyst\Converter\Converter
* Get the height
*/
public function getWeightAttribute()
protected function height(): Attribute
{
$unit = $this->weight_unit ?: 'kg';
return Attribute::make(
get: fn() => Converter::from("length.{$this->height_unit}")->value($this->height_value ?? 0)
);
}

return Converter::from("weight.{$unit}")->value($this->weight_value ?: 0);
/**
* Get the weight
*/
protected function weight(): Attribute
{
return Attribute::make(
get: fn() => Converter::from("weight.{$this->weight_unit}")->value($this->weight_value ?? 0)
);
}

/**
* Getter for the volume attribute.
*
* @return \Cartalyst\Converter\Converter
* Get the volume
*/
public function getVolumeAttribute()
protected function volume(): Attribute
{
if ($this->volume_value && $this->volume_unit) {
return Converter::from("volume.{$this->volume_unit}")
->to("volume.{$this->volume_unit}")->value($this->volume_value);
}
return Attribute::make(
get: function() {
if ($this->volume_value && $this->volume_unit) {
return Converter::from("volume.{$this->volume_unit}")
->to("volume.{$this->volume_unit}")->value($this->volume_value);
}

$length = $this->length->to('length.cm')->convert()->getValue();
$width = $this->width->to('length.cm')->convert()->getValue();
$height = $this->height->to('length.cm')->convert()->getValue();
$length = $this->length->to('length.cm')->convert()->getValue();
$width = $this->width->to('length.cm')->convert()->getValue();
$height = $this->height->to('length.cm')->convert()->getValue();

return Converter::from('volume.ml')->to('volume.l')->value($length * $width * $height)->convert();
return Converter::from('volume.ml')->to('volume.l')->value($length * $width * $height)->convert();
}
);
}
}
4 changes: 2 additions & 2 deletions packages/core/src/LunarServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,8 @@ protected function registerBlueprintMacros(): void
/** @var Blueprint $this */
$columns = ['length', 'width', 'height', 'weight', 'volume'];
foreach ($columns as $column) {
$this->decimal("{$column}_value", 10, 4)->default(0)->nullable()->index();
$this->string("{$column}_unit")->default('mm')->nullable();
$this->decimal("{$column}_value", 10, 4)->nullable()->index();
$this->string("{$column}_unit")->nullable();
}
});

Expand Down

0 comments on commit da095d0

Please sign in to comment.