Skip to content

Commit

Permalink
[TCDA-232] Fix/lunch meal portion (#669)
Browse files Browse the repository at this point in the history
* fix/select now shows food values

* fix:modal updated and necessary API routes created

* fix/lunch meal controller adjusted

* fix/model updated, database adjusted but still missing controller fixes

* fix/change in render partial params

* fix/some forms are already working

* fix/form and action finished

* fix/remove unnecessary comments

* CHANGELOG updated

* fix/sonarlint fixes

* fix(lunch): Corrigindo comando sql da migration

---------

Co-authored-by: TI GUSTAVO <ti.gustavo@ipti.org.br>
Co-authored-by: Gustavo Santos <gustavo.santos23703@gmail.com>
  • Loading branch information
3 people committed Apr 26, 2024
1 parent 3f84401 commit f0ed77d
Show file tree
Hide file tree
Showing 8 changed files with 295 additions and 200 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -30,7 +30,8 @@
- Correção no conteúdo ministrado em sala de aula

## [Versão 3.76.129]
- Correção na descrição da séries para as inconsistências sagres
- Corrigir a funcionalidade de cadastrar porções em refeições de cardápio
- Ajustar banco de dados para adequar ao novo fluxo da tela de atualizar cardápio

## [Versão 3.76.128]
- Corrigido erro que não mostrava o nome da escola e das turmas nas inconsistências sagres
Expand Down
21 changes: 21 additions & 0 deletions app/migrations/2024-03-27_lunch_meal_portion/sql.sql
@@ -0,0 +1,21 @@
ALTER TABLE lunch_portion
ADD COLUMN food_fk INT NOT NULL;

ALTER TABLE lunch_portion
ADD CONSTRAINT fk_lunch_portion_food
FOREIGN KEY (food_fk) REFERENCES food(id);

ALTER TABLE lunch_portion
DROP FOREIGN KEY portion_unity;

ALTER TABLE lunch_portion
ADD CONSTRAINT fk_lunch_unity_fk
FOREIGN KEY (unity_fk) REFERENCES food_measurement(id);


ALTER TABLE lunch_portion
DROP FOREIGN KEY portion_item;

ALTER TABLE lunch_portion DROP COLUMN item_fk;

ALTER TABLE lunch_menu_meal DROP COLUMN amount;
113 changes: 79 additions & 34 deletions app/modules/lunch/controllers/LunchController.php
@@ -1,5 +1,6 @@
<?php

Yii::import('application.modules.foods.models.FoodMeasurement', true);
/**
* Class LunchController
*
Expand Down Expand Up @@ -50,6 +51,29 @@ public function actionUpdate($id){
$menuPost = $request->getPost("Menu", false);

$menu = Menu::model()->findByPk($id);
$menuMeals = MenuMeal::model()->findAllByAttributes(array("menu_fk" => $menu->id));
$meals = [];
foreach($menuMeals as $menuMeal)
{
// Array temporário para inserir porções de uma refeição
$tmpPortions = [];
// Encontrar todas as porções que pertencem à uma refeição
$mealsPortion = MealPortion::model()->findAllByAttributes(array("meal_fk" => $menuMeal->id));

// Inserir porções em array temporário
foreach($mealsPortion as $mealPortion){
array_push($tmpPortions, Portion::model()->findByPk($mealPortion->portion_fk));
}

// Inserir par Refeição:Porções em array Refeições
array_push(
$meals,
[
"meal" => Meal::model()->findByPk($menuMeal->meal_fk),
"portions" => $tmpPortions
]
);
}

if($menuPost){
$menu->name = $menuPost['name'];
Expand All @@ -65,7 +89,7 @@ public function actionUpdate($id){
$this->render('update', ["menu" => $menu]);
}
}else {
$this->render('update', ["menu" => $menu]);
$this->render('update', ["menu" => $menu, "meals" => $meals]);
}
}
public function actionLunchDelete(){
Expand All @@ -78,23 +102,13 @@ public function actionRemovePortion(){
/* @var $mealPortion MealPortion */

$request = Yii::app()->getRequest();
$menuPost = $request->getPost("Menu", false);
$mealPortionPost = $request->getPost("MealPortion", false);
$amountPost = $mealPortionPost['amount'] > 0 ? -1 *$mealPortionPost['amount'] : $mealPortionPost['amount'];

$mealPortion = MealPortion::model()->findByPk($mealPortionPost['id']);
if(isset($mealPortion)){
$amount = $mealPortion->amount;
if($amount + $amountPost > 0){
$mealPortion->amount = $amount + $amountPost;
if($mealPortion->validate()){
$mealPortion->save();
Yii::app()->user->setFlash('success', Yii::t('lunchModule.lunch', 'Portion decreased successfully!'));
}else{
Yii::app()->user->setFlash('error', Yii::t('lunchModule.lunch', 'Error when removing portion.'));
}
}else{
$mealPortion->delete();
$portionId = $request->getPost("id", false);
$menuPost = $request->getPost("menu", false);

$portion = Portion::model()->findByPk($portionId);
$mealPortion = MealPortion::model()->findByAttributes(["portion_fk" => $portion->id]);
if(isset($portion) && isset($mealPortion)){
if($mealPortion->delete() && $portion->delete()){
Yii::app()->user->setFlash('success', Yii::t('lunchModule.lunch', 'Portion removed successfully!'));
}
}else{
Expand All @@ -103,21 +117,56 @@ public function actionRemovePortion(){
$this->redirect(yii::app()->createUrl('lunch/lunch/update',["id"=>$menuPost['id']]));
}

public function actionGetFoodAlias()
{
$criteria = new CDbCriteria();
$criteria->select = 'id, description, measurementUnit';
$criteria->condition = 'alias_id = t.id';

$foods_description = Food::model()->findAll($criteria);

return $foods_description;
}

public function actionGetFoodMeasurement()
{
$foodMeasurements = FoodMeasurement::model()->findAll();
$options = array();
foreach ($foodMeasurements as $foodMeasurement) {
array_push(
$options,
array(
"id" => $foodMeasurement->id,
"unit" => $foodMeasurement->unit,
"value" => $foodMeasurement->value,
"measure" => $foodMeasurement->measure
)
);
}
return $options;
}

public function actionGetUnityMeasure()
{
$measureId = Yii::app()->request->getPost('id');
$modal = FoodMeasurement::model()->findByPk($measureId);

echo CJSON::encode($modal);
}

public function actionAddPortion(){
/* @var $mealPortion MealPortion */
/* @var $portion Portion */

$request = Yii::app()->getRequest();
$menuPost = $request->getPost("Menu", false);
$portionPost = $request->getPost("Portion", false);
$mealPortionPost = $request->getPost("MealPortion", false);


$isNewPortion = false;
if($portionPost){
if($mealPortionPost){
$portion = new Portion();
$portion->item_fk = $portionPost["item_fk"];
$portion->measure = $portionPost["measure"];
$portion->unity_fk = $portionPost["unity_fk"];
$portion->amount = 1;
$portion->setAttributes($mealPortionPost);
if($portion->validate()){
$portion->save();
$isNewPortion = true;
Expand All @@ -128,17 +177,15 @@ public function actionAddPortion(){
}

$mealId = $mealPortionPost["meal_fk"];
$portionId = $isNewPortion ? $portion->id : $mealPortionPost["portion_fk"];
$mealPortion = MealPortion::model()->findByAttributes(["meal_fk" => $mealId, "portion_fk"=>$portionId]);
$portionId = $portion->id;
$mealPortion = MealPortion::model()->findByAttributes(["meal_fk" => $mealId, "portion_fk" => $portionId]);
$isNewMealPortion = !isset($mealPortion);

if($isNewMealPortion){
$mealPortion = new MealPortion();
$mealPortion->meal_fk = $mealId;
$mealPortion->portion_fk = $portionId;
$mealPortion->amount = $mealPortionPost["amount"];
}else{
$mealPortion->amount = $mealPortion->amount + $mealPortionPost["amount"];
}

if($mealPortion->validate()){
Expand Down Expand Up @@ -172,7 +219,6 @@ public function actionAddMeal(){
$menuMeal = new MenuMeal();
$menuMeal->meal_fk = $meal->id;
$menuMeal->menu_fk = $menuMealPost['menu_fk'];
$menuMeal->amount = $menuMealPost['amount'];

if($menuMeal->validate()){
$menuMeal->save();
Expand Down Expand Up @@ -201,14 +247,13 @@ public function actionChangeMeal(){
if($mealPost && $menuMealPost) {
$mealId = $menuMealPost['meal_fk'];
$menuId = $menuMealPost['menu_fk'];
$amount = $menuMealPost['amount'];
$restrictions = $mealPost['restrictions'];

$menuMeal = MenuMeal::model()->findByAttributes(['meal_fk'=>$mealId, 'menu_fk'=>$menuId]);
$menuMeal->amount = $amount;
$menuMeal->meal->restrictions = $restrictions;
if($menuMeal->meal->validate() && $menuMeal->validate()) {
$menuMeal->meal->save();
$meal = Meal::model()->findByPk($menuMeal->meal_fk);
$meal->restrictions = $restrictions;
if($meal->validate() && $menuMeal->validate()) {
$meal->save();
$menuMeal->save();
Log::model()->saveAction("lunch_meal", $menuMeal->id, "U", $menuMeal->menu->name);
Yii::app()->user->setFlash('success', Yii::t('lunchModule.lunch', 'Meal updated successfully!'));
Expand Down
54 changes: 29 additions & 25 deletions app/modules/lunch/models/MenuMeal.php
Expand Up @@ -7,24 +7,13 @@
* @property integer $id
* @property integer $menu_fk
* @property integer $meal_fk
* @property double $amount
*
* The followings are the available model relations:
* @property Menu $menu
* @property Meal $meal
* @property Menu $menuFk
* @property Meal $mealFk
*/
class MenuMeal extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return MenuMeal the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}

/**
* @return string the associated database table name
*/
Expand All @@ -41,12 +30,11 @@ public function rules()
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('menu_fk, meal_fk, amount', 'required'),
array('menu_fk, meal_fk', 'required'),
array('menu_fk, meal_fk', 'numerical', 'integerOnly'=>true),
array('amount', 'numerical'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, menu_fk, meal_fk, amount', 'safe', 'on'=>'search'),
// @todo Please remove those attributes that should not be searched.
array('id, menu_fk, meal_fk', 'safe', 'on'=>'search'),
);
}

Expand All @@ -69,31 +57,47 @@ public function relations()
public function attributeLabels()
{
return array(
'id' => Yii::t('lunchModule.labels', 'ID'),
'menu_fk' => Yii::t('lunchModule.labels', 'Menu'),
'id' => 'ID',
'menu_fk' => Yii::t('lunchModule.labels', 'Menu'),
'meal_fk' => Yii::t('lunchModule.labels', 'Meal'),
'amount' => Yii::t('lunchModule.labels', 'Amount'),
);
}

/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
*
* @return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
// @todo Please modify the following code to remove attributes that should not be searched.

$criteria=new CDbCriteria;

$criteria->compare('id',$this->id);
$criteria->compare('menu_fk',$this->menu_fk);
$criteria->compare('meal_fk',$this->meal_fk);
$criteria->compare('amount',$this->amount);

return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}

/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return MenuMeal the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}

0 comments on commit f0ed77d

Please sign in to comment.