forked from eatvio/eatvio-infra
118 lines
3.7 KiB
PHP
118 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\ProductNutritional;
|
|
|
|
use Livewire\Component;
|
|
use Livewire\WithFileUploads;
|
|
use App\Models\ProductGroup;
|
|
use App\Models\ProductNutritionalValue;
|
|
|
|
class Create extends Component
|
|
{
|
|
use WithFileUploads;
|
|
|
|
public $name;
|
|
public $product_group;
|
|
public $image;
|
|
public $description;
|
|
public $formulations;
|
|
public $energy_on_100gr;
|
|
public $proteins_on_100gr;
|
|
public $fats_on_100gr;
|
|
public $carbohydrates_on_100gr;
|
|
public $portion_mass_gr;
|
|
public $energy_on_portion;
|
|
public $proteins_on_portion;
|
|
public $fats_on_portion;
|
|
public $carbohydrates_on_portion;
|
|
public $curProductNutritionalValue;
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.product-nutritional.create', ['productGroups' => ProductGroup::all()]);
|
|
}
|
|
|
|
public function save()
|
|
{
|
|
|
|
$productNutritionalVal = ProductNutritionalValue::create([
|
|
'name' => $this->name,
|
|
'product_group_id' => $this->product_group,
|
|
'image' => is_null($this->image) ? "" : $this->image->store('product_nutritional', 'public'),
|
|
'description' => $this->description ?? "",
|
|
'formulations' => $this->formulations ?? "",
|
|
'energy_on_100gr' => (int) $this->energy_on_100gr,
|
|
'proteins_on_100gr' => (float) $this->proteins_on_100gr,
|
|
'fats_on_100gr' => (float) $this->fats_on_100gr,
|
|
'carbohydrates_on_100gr' => (float) $this->carbohydrates_on_100gr,
|
|
'portion_mass_gr' => (int) $this->portion_mass_gr,
|
|
'energy_on_portion' => (float) $this->energy_on_portion,
|
|
'proteins_on_portion' => (float) $this->proteins_on_portion,
|
|
'fats_on_portion' => (float) $this->fats_on_portion,
|
|
'carbohydrates_on_portion' => (float) $this->carbohydrates_on_portion,
|
|
]);
|
|
|
|
$this->curProductNutritionalValue = $productNutritionalVal;
|
|
|
|
return redirect()->route('product_nutritional_value.index');
|
|
}
|
|
|
|
public function recalulateVals($mass)
|
|
{
|
|
$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 = (float) ($k * (float) $this->{$keyPortion});
|
|
$this->fill([$key100 => $newVal]);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function recalulatePortionVals($mass)
|
|
{
|
|
$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 = (float) ($k * (float) $this->{$key100});
|
|
$this->fill([$keyPortion => $newVal]);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public function updated($name, $value)
|
|
{
|
|
if ($name == 'portion_mass_gr') {
|
|
$this->recalulatePortionVals((int) $this->portion_mass_gr);
|
|
}
|
|
|
|
if (
|
|
(int)$this->portion_mass_gr > 0
|
|
&& in_array($name, ['energy_on_portion', 'proteins_on_portion', 'fats_on_portion', 'carbohydrates_on_portion'])
|
|
) {
|
|
$this->recalulateVals((int)$this->portion_mass_gr);
|
|
}
|
|
}
|
|
|
|
public function delete()
|
|
{
|
|
$this->reset();
|
|
$this->curProductNutritionalValue->delete();
|
|
}
|
|
}
|