forked from eatvio/eatvio-infra
157 lines
4.8 KiB
PHP
157 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\ProductNutritional;
|
|
|
|
use App\Models\ProductGroup;
|
|
use App\Models\ProductNutritionalValue;
|
|
use Illuminate\Contracts\View\Factory as ViewFactory;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Livewire\Component;
|
|
use Livewire\WithFileUploads;
|
|
|
|
class Edit extends Component
|
|
{
|
|
use WithFileUploads;
|
|
|
|
public string $name = '';
|
|
|
|
public int|string $product_group;
|
|
|
|
public UploadedFile|string $image = '';
|
|
|
|
public string $description = '';
|
|
|
|
public string $formulations;
|
|
|
|
public int $energy_on_100gr;
|
|
|
|
public float $proteins_on_100gr;
|
|
|
|
public float $fats_on_100gr;
|
|
|
|
public float $carbohydrates_on_100gr;
|
|
|
|
public int $portion_mass_gr = 0;
|
|
|
|
public float $energy_on_portion;
|
|
|
|
public float $proteins_on_portion;
|
|
|
|
public float $fats_on_portion;
|
|
|
|
public float $carbohydrates_on_portion;
|
|
|
|
public ProductNutritionalValue $product;
|
|
|
|
public function mount(ProductNutritionalValue $product): void
|
|
{
|
|
$this->name = $product->name;
|
|
$this->product_group = $product->product_group->id;
|
|
$this->image = $product->image;
|
|
$this->description = $product->description;
|
|
$this->formulations = $product->formulations;
|
|
$this->energy_on_100gr = $product->energy_on_100gr;
|
|
$this->proteins_on_100gr = $product->proteins_on_100gr;
|
|
$this->fats_on_100gr = $product->fats_on_100gr;
|
|
$this->carbohydrates_on_100gr = $product->carbohydrates_on_100gr;
|
|
$this->portion_mass_gr = $product->portion_mass_gr;
|
|
$this->energy_on_portion = $product->energy_on_portion;
|
|
$this->proteins_on_portion = $product->proteins_on_portion;
|
|
$this->fats_on_portion = $product->fats_on_portion;
|
|
$this->carbohydrates_on_portion = $product->carbohydrates_on_portion;
|
|
$this->product = $product;
|
|
}
|
|
|
|
public function render(): View|ViewFactory
|
|
{
|
|
return view('livewire.product-nutritional.edit', ['productGroups' => ProductGroup::all()]);
|
|
}
|
|
|
|
public function save(): void
|
|
{
|
|
if (! is_string($this->image)) {
|
|
$this->product->image = $this->image->store('product_nutritional');
|
|
}
|
|
|
|
$this->product->fill([
|
|
'name' => $this->name,
|
|
'product_group_id' => $this->product_group,
|
|
'description' => $this->description ?? '',
|
|
'formulations' => $this->formulations ?? '',
|
|
'energy_on_100gr' => $this->energy_on_100gr,
|
|
'proteins_on_100gr' => $this->proteins_on_100gr,
|
|
'fats_on_100gr' => $this->fats_on_100gr,
|
|
'carbohydrates_on_100gr' => $this->carbohydrates_on_100gr,
|
|
'portion_mass_gr' => $this->portion_mass_gr,
|
|
'energy_on_portion' => (int) $this->energy_on_portion,
|
|
'proteins_on_portion' => $this->proteins_on_portion,
|
|
'fats_on_portion' => $this->fats_on_portion,
|
|
'carbohydrates_on_portion' => $this->carbohydrates_on_portion,
|
|
]);
|
|
|
|
$this->product->save();
|
|
|
|
$this->redirectRoute('product_nutritional_value.index');
|
|
|
|
}
|
|
|
|
public function recalulateVals(int $mass): void
|
|
{
|
|
$productKeys = [
|
|
['energy_on_100gr', 'energy_on_portion'],
|
|
['proteins_on_100gr', 'proteins_on_portion'],
|
|
['fats_on_100gr', 'fats_on_portion'],
|
|
['carbohydrates_on_100gr', 'carbohydrates_on_portion'],
|
|
];
|
|
|
|
$k = 100 / $mass;
|
|
|
|
foreach ($productKeys as [$key100, $keyPortion]) {
|
|
if ($this->{$keyPortion}) {
|
|
$newVal = $k * (float) $this->{$keyPortion};
|
|
$this->fill([$key100 => $newVal]);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function recalulatePortionVals(int $mass): void
|
|
{
|
|
$productKeys = [
|
|
['energy_on_100gr', 'energy_on_portion'],
|
|
['proteins_on_100gr', 'proteins_on_portion'],
|
|
['fats_on_100gr', 'fats_on_portion'],
|
|
['carbohydrates_on_100gr', 'carbohydrates_on_portion'],
|
|
];
|
|
|
|
$k = $mass / 100;
|
|
foreach ($productKeys as [$key100, $keyPortion]) {
|
|
if ($this->{$key100}) {
|
|
$newVal = $k * (float) $this->{$key100};
|
|
$this->fill([$keyPortion => $newVal]);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function updated(string $name, string $value): void
|
|
{
|
|
if ($name === 'portion_mass_gr') {
|
|
$this->recalulatePortionVals($this->portion_mass_gr);
|
|
}
|
|
|
|
if (
|
|
$this->portion_mass_gr > 0
|
|
&& in_array($name, ['energy_on_portion', 'proteins_on_portion', 'fats_on_portion', 'carbohydrates_on_portion'])
|
|
) {
|
|
$this->recalulateVals($this->portion_mass_gr);
|
|
}
|
|
}
|
|
|
|
public function delete(): void
|
|
{
|
|
$this->product->delete();
|
|
|
|
$this->redirectRoute('product_nutritional_value.index');
|
|
}
|
|
}
|