mirror of https://github.com/kcal-app/kcal.git
Update nutrient calculation for IngredientAmount
This commit is contained in:
parent
99129e3c76
commit
6b406a63ca
|
|
@ -55,6 +55,8 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
* @method static \Illuminate\Database\Eloquent\Builder|Food whereUpdatedAt($value)
|
* @method static \Illuminate\Database\Eloquent\Builder|Food whereUpdatedAt($value)
|
||||||
* @mixin \Eloquent
|
* @mixin \Eloquent
|
||||||
* @property-read string $serving_size_formatted
|
* @property-read string $serving_size_formatted
|
||||||
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\IngredientAmount[] $ingredientAmounts
|
||||||
|
* @property-read int|null $ingredient_amounts_count
|
||||||
*/
|
*/
|
||||||
class Food extends Model
|
class Food extends Model
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
* @method static \Illuminate\Database\Eloquent\Builder|FoodAmount whereUpdatedAt($value)
|
* @method static \Illuminate\Database\Eloquent\Builder|FoodAmount whereUpdatedAt($value)
|
||||||
* @method static \Illuminate\Database\Eloquent\Builder|FoodAmount whereWeight($value)
|
* @method static \Illuminate\Database\Eloquent\Builder|FoodAmount whereWeight($value)
|
||||||
* @mixin \Eloquent
|
* @mixin \Eloquent
|
||||||
|
* @property-read string $amount_formatted
|
||||||
*/
|
*/
|
||||||
class FoodAmount extends Model
|
class FoodAmount extends Model
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,39 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* App\Models\IngredientAmount
|
||||||
|
*
|
||||||
|
* @property int $id
|
||||||
|
* @property int $ingredient_id
|
||||||
|
* @property string $ingredient_type
|
||||||
|
* @property float $amount
|
||||||
|
* @property string|null $unit
|
||||||
|
* @property string|null $detail
|
||||||
|
* @property int $weight
|
||||||
|
* @property int $parent_id
|
||||||
|
* @property string $parent_type
|
||||||
|
* @property \Illuminate\Support\Carbon|null $created_at
|
||||||
|
* @property \Illuminate\Support\Carbon|null $updated_at
|
||||||
|
* @property-read string $amount_formatted
|
||||||
|
* @property-read Model|\Eloquent $ingredient
|
||||||
|
* @property-read Model|\Eloquent $parent
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount newModelQuery()
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount newQuery()
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount query()
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount whereAmount($value)
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount whereCreatedAt($value)
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount whereDetail($value)
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount whereId($value)
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount whereIngredientId($value)
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount whereIngredientType($value)
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount whereParentId($value)
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount whereParentType($value)
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount whereUnit($value)
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount whereUpdatedAt($value)
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount whereWeight($value)
|
||||||
|
* @mixin \Eloquent
|
||||||
|
*/
|
||||||
class IngredientAmount extends Model
|
class IngredientAmount extends Model
|
||||||
{
|
{
|
||||||
use HasFactory;
|
use HasFactory;
|
||||||
|
|
@ -85,11 +118,15 @@ class IngredientAmount extends Model
|
||||||
*/
|
*/
|
||||||
public function __call($method, $parameters): mixed {
|
public function __call($method, $parameters): mixed {
|
||||||
if (in_array($method, $this->nutrientMethods)) {
|
if (in_array($method, $this->nutrientMethods)) {
|
||||||
return $this->ingredient->{$method} * Nutrients::calculateFoodNutrientMultiplier(
|
return match ($this->ingredient::class) {
|
||||||
|
Food::class => $this->ingredient->{$method} * Nutrients::calculateFoodNutrientMultiplier(
|
||||||
$this->ingredient,
|
$this->ingredient,
|
||||||
$this->amount,
|
$this->amount,
|
||||||
$this->unit
|
$this->unit
|
||||||
);
|
),
|
||||||
|
Recipe::class => $this->ingredient->{"{$method}Total"}(),
|
||||||
|
default => 0
|
||||||
|
};
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return parent::__call($method, $parameters);
|
return parent::__call($method, $parameters);
|
||||||
|
|
|
||||||
|
|
@ -46,6 +46,8 @@ use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
||||||
* @method static \Illuminate\Database\Eloquent\Builder|JournalEntry whereUpdatedAt($value)
|
* @method static \Illuminate\Database\Eloquent\Builder|JournalEntry whereUpdatedAt($value)
|
||||||
* @method static \Illuminate\Database\Eloquent\Builder|JournalEntry whereUserId($value)
|
* @method static \Illuminate\Database\Eloquent\Builder|JournalEntry whereUserId($value)
|
||||||
* @mixin \Eloquent
|
* @mixin \Eloquent
|
||||||
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\IngredientAmount[] $ingredients
|
||||||
|
* @property-read int|null $ingredients_count
|
||||||
*/
|
*/
|
||||||
class JournalEntry extends Model
|
class JournalEntry extends Model
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,10 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
* @method static \Illuminate\Database\Eloquent\Builder|Recipe whereSource($value)
|
* @method static \Illuminate\Database\Eloquent\Builder|Recipe whereSource($value)
|
||||||
* @method static \Illuminate\Database\Eloquent\Builder|Recipe whereUpdatedAt($value)
|
* @method static \Illuminate\Database\Eloquent\Builder|Recipe whereUpdatedAt($value)
|
||||||
* @mixin \Eloquent
|
* @mixin \Eloquent
|
||||||
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\IngredientAmount[] $ingredientAmounts
|
||||||
|
* @property-read int|null $ingredient_amounts_count
|
||||||
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\IngredientAmount[] $ingredients
|
||||||
|
* @property-read int|null $ingredients_count
|
||||||
*/
|
*/
|
||||||
class Recipe extends Model
|
class Recipe extends Model
|
||||||
{
|
{
|
||||||
|
|
@ -62,18 +66,6 @@ class Recipe extends Model
|
||||||
'servings' => 'int',
|
'servings' => 'int',
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
|
||||||
* Nutrient total calculation methods.
|
|
||||||
*/
|
|
||||||
private array $nutrientTotalMethods = [
|
|
||||||
'caloriesTotal',
|
|
||||||
'carbohydratesTotal',
|
|
||||||
'cholesterolTotal',
|
|
||||||
'fatTotal',
|
|
||||||
'proteinTotal',
|
|
||||||
'sodiumTotal',
|
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Nutrient per serving methods.
|
* Nutrient per serving methods.
|
||||||
*/
|
*/
|
||||||
|
|
@ -136,19 +128,4 @@ class Recipe extends Model
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Sum a specific nutrient for all food amounts.
|
|
||||||
*
|
|
||||||
* @param string $nutrient
|
|
||||||
* Nutrient to sum.
|
|
||||||
*
|
|
||||||
* @return float
|
|
||||||
*/
|
|
||||||
private function sumNutrient(string $nutrient): float {
|
|
||||||
$sum = 0;
|
|
||||||
foreach ($this->foodAmounts as $foodAmount) {
|
|
||||||
$sum += $foodAmount->{$nutrient}();
|
|
||||||
}
|
|
||||||
return $sum;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,10 +7,38 @@ use Illuminate\Database\Eloquent\Relations\MorphMany;
|
||||||
|
|
||||||
trait HasIngredients
|
trait HasIngredients
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Nutrient total calculation methods.
|
||||||
|
*/
|
||||||
|
protected array $nutrientTotalMethods = [
|
||||||
|
'caloriesTotal',
|
||||||
|
'carbohydratesTotal',
|
||||||
|
'cholesterolTotal',
|
||||||
|
'fatTotal',
|
||||||
|
'proteinTotal',
|
||||||
|
'sodiumTotal',
|
||||||
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all of the ingredients.
|
* Get all of the ingredients.
|
||||||
*/
|
*/
|
||||||
public function ingredients(): MorphMany {
|
public function ingredients(): MorphMany {
|
||||||
return $this->morphMany(IngredientAmount::class, 'parent');
|
return $this->morphMany(IngredientAmount::class, 'parent');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sum a specific nutrient for all ingredients.
|
||||||
|
*
|
||||||
|
* @param string $nutrient
|
||||||
|
* Nutrient to sum.
|
||||||
|
*
|
||||||
|
* @return float
|
||||||
|
*/
|
||||||
|
private function sumNutrient(string $nutrient): float {
|
||||||
|
$sum = 0;
|
||||||
|
foreach ($this->ingredients as $ingredientAmount) {
|
||||||
|
$sum += $ingredientAmount->{$nutrient}();
|
||||||
|
}
|
||||||
|
return $sum;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue