forked from eatvio/eatvio-infra
132 lines
3.9 KiB
PHP
132 lines
3.9 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 Create extends Component
|
|
{
|
|
use WithFileUploads;
|
|
|
|
public string $name = '';
|
|
|
|
public int|string $product_group;
|
|
|
|
public ?UploadedFile $image = null;
|
|
|
|
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 $curProductNutritionalValue;
|
|
|
|
public function render(): View|ViewFactory
|
|
{
|
|
return view('livewire.product-nutritional.create', ['productGroups' => ProductGroup::all()]);
|
|
}
|
|
|
|
public function save(): void
|
|
{
|
|
$productNutritionalVal = ProductNutritionalValue::create([
|
|
'name' => $this->name,
|
|
'product_group_id' => $this->product_group,
|
|
'image' => is_null($this->image) ? '' : $this->image->store('product_nutritional'),
|
|
'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' => $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->curProductNutritionalValue = $productNutritionalVal;
|
|
$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 = $mass / 100;
|
|
|
|
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->reset();
|
|
$this->curProductNutritionalValue->delete();
|
|
}
|
|
}
|