Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/recommendationFood #729

Open
wants to merge 32 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
## [Versão 3.78.151]
## [Versão 3.80.151]
- Realizadas correções no módulo de merenda, criando uma nova funcionalidade para recomendar itens faltantes do estoque.

## [Versão 3.79.151]
- Realizadas correções no módulo Sagres para evitar a contagem de CPFs de estudantes, evitando duplicações.

## [Versão 3.79.150]
Expand Down

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions app/modules/foods/controllers/FoodinventoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,33 @@ public function actionGetFoodInventory()
echo json_encode($values);
}

public function actionGetstatusFoodInventory(){

$schoolFk = Yii::app()->user->school;

$criteria = new CDbCriteria();
$criteria->with = array('foodRelation');
$criteria->compare('school_fk', $schoolFk);

$foodInventoryData = FoodInventory::model()->findAll($criteria);

$values = [];
foreach ($foodInventoryData as $stock) {
$values[] = array(
'id' => $stock->id,
'foodId' => $stock->food_fk,
'description' => $stock->foodRelation->description,
'amount' => $stock->amount,
'measurementUnit' => $stock->measurementUnit,
'expiration_date' => ($stock->expiration_date != null) ? date('d/m/Y', strtotime($stock->expiration_date)) : "Não informada",
'status' => $stock->status,
'spent' => ($stock->amount > 0) ? false : true
);
}

echo json_encode($values);
}

public function actionUpdateFoodInventoryStatus()
{
$foodInventoryId = Yii::app()->request->getPost('foodInventoryId');
Expand Down Expand Up @@ -379,4 +406,6 @@ protected function performAjaxValidation($model)
Yii::app()->end();
}
}


}
211 changes: 180 additions & 31 deletions app/modules/foods/controllers/FoodmenuController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,12 @@ class FoodmenuController extends Controller
public $modelFoodMenu = 'FoodMenu';
public $defaultAction = "viewlunch";

/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate()
{
$modelFoodMenu = new FoodMenu;
$request = Yii::app()->request->getPost('foodMenu');
$transaction = Yii::app()->db->beginTransaction();
// Verifica se há dados na requisição enviada
// Caso negativo, renderiza o formulário

if ($request === null) {
$mealTypeList = $this->actionGetMealType();
$tacoFoodsList = $this->actionGetTacoFoods();
Expand Down Expand Up @@ -54,7 +49,7 @@ public function actionCreate()
// Verifica se a ação de salvar foodMenu ocorreu com sucesso, caso falhe encerra a aplicação
$saveFoodMenuResult = $modelFoodMenu->save();

if ($saveFoodMenuResult == false) {
if (!$saveFoodMenuResult) {
$message = 'Ocorreu um erro ao salvar o cardápio! Tente novamente.';
$transaction->rollback();
throw new CHttpException(500, $message);
Expand All @@ -63,14 +58,14 @@ public function actionCreate()
/* Atribui valores às propriedades do model FoodMenuVsFoodPublicTarget
(Tabela N:N entre cardápio e publico alvo) */
$publicTarget = FoodPublicTarget::model()->findByPk($request['food_public_target']);
$foodMenuVsPublicTarget = new FoodMenuVsFoodPublicTarget;
$foodMenuVsPublicTarget->food_menu_fk = $modelFoodMenu->id;
$foodMenuVsPublicTarget->food_public_target_fk = $publicTarget->id;
$foodMenuVsPublicTarget->save();
$foodMenuTarget = new FoodMenuVsFoodPublicTarget;
$foodMenuTarget->food_menu_fk = $modelFoodMenu->id;
$foodMenuTarget->food_public_target_fk = $publicTarget->id;
$foodMenuTarget->save();

// Chamando método que irá adicionar novos registros relacionados ao cardápio
$createFoodMenuRelations = new CreateFoodMenuRelations();
$createFoodMenuRelations->exec($modelFoodMenu, $request, $transaction);
$createMenuRelations = new CreateFoodMenuRelations();
$createMenuRelations->exec($modelFoodMenu, $request, $transaction);
// Salvar alterações no banco
$transaction->commit();
header('HTTP/1.1 201 Created');
Expand Down Expand Up @@ -117,8 +112,7 @@ public function actionUpdate($id)
// Trecho do código para excluir todos os registros associados ao cardápio
$transaction = Yii::app()->db->beginTransaction();

if($modelFoodMenu != null)
{
if ($modelFoodMenu != null) {
$modelFoodMenu->start_date = DateTime::createFromFormat('d/m/Y', $request["start_date"])->format("Y-m-d");
$modelFoodMenu->final_date = DateTime::createFromFormat('d/m/Y', $request["final_date"])->format("Y-m-d");
$modelFoodMenu->week = $request['week'];
Expand All @@ -127,11 +121,11 @@ public function actionUpdate($id)
$modelFoodMenu->save();

//atualiza FoodMenuvVsPublicTarget
$foodMenuVsPublicTarget = FoodMenuVsFoodPublicTarget::model()
$foodMenuTarget = FoodMenuVsFoodPublicTarget::model()
->findByAttributes(array('food_menu_fk' => $modelFoodMenu->id));
$publicTarget = FoodPublicTarget::model()->findByPk($request['food_public_target']);
$foodMenuVsPublicTarget->food_public_target_fk = $publicTarget->id;
$foodMenuVsPublicTarget->save();
$foodMenuTarget->food_public_target_fk = $publicTarget->id;
$foodMenuTarget->save();
}

foreach ($modelMenuMeals as $modelMenuMeal) {
Expand All @@ -150,8 +144,8 @@ public function actionUpdate($id)
$modelMenuMeal->delete();
}
// Chamada de função que irá salvar as novas informações do cardápio
$createFoodMenuRelations = new CreateFoodMenuRelations();
$createFoodMenuRelations->exec($modelFoodMenu, $request, $transaction);
$createMenuRelations = new CreateFoodMenuRelations();
$createMenuRelations->exec($modelFoodMenu, $request, $transaction);
$transaction->commit();
header('HTTP/1.1 200 OK');
Log::model()->saveAction("foodMenu", $modelFoodMenu->id, "U", $modelFoodMenu->description);
Expand Down Expand Up @@ -199,20 +193,20 @@ public function actionDelete($id)
array('food_menuId' => $modelFoodMenu->id)
);
foreach ($modelFoodMenuMeals as $modelFoodMenuMeal) {
$modelFoodMealComponents = FoodMenuMealComponent::model()->findAllByAttributes(
$modelMealComponents = FoodMenuMealComponent::model()->findAllByAttributes(
array('food_menu_mealId' => $modelFoodMenuMeal->id)
);
foreach ($modelFoodMealComponents as $modelFoodMealComponent) {
foreach ($modelMealComponents as $modelMealComponent) {
FoodIngredient::model()->deleteAllByAttributes(
array('food_menu_meal_componentId' => $modelFoodMealComponent->id)
array('food_menu_meal_componentId' => $modelMealComponent->id)
);
}
$modelFoodMenuMeal->delete();
}
$modelFoodMenuVsPublicTarget = FoodMenuVsFoodPublicTarget::model()->findByAttributes(
$modelFoodMenuTarget = FoodMenuVsFoodPublicTarget::model()->findByAttributes(
array('food_menu_fk' => $modelFoodMenu->id)
);
$modelFoodMenuVsPublicTarget->delete();
$modelFoodMenuTarget->delete();
$modelFoodMenu->delete();
$transaction->commit();
header('HTTP/1.1 200 OK');
Expand Down Expand Up @@ -258,7 +252,6 @@ public function actionViewLunch()


$result[$turn] = $totalStudents;

}

$this->render('viewlunch', array(
Expand All @@ -271,18 +264,153 @@ public function actionGetMealsOfWeek()

$getMelsOfWeek = new GetMelsOfWeek();
$foodMenu = $getMelsOfWeek->exec();

$response = json_encode((array) $foodMenu);
echo $response;
}

/**
* Lists all models.
*/
public function actionGetMealsRecommendation()
{
$getMelsOfWeek = new GetMelsOfWeek();
$foodMenu = $getMelsOfWeek->exec();
$response = json_encode((array) $foodMenu);
$data = json_decode($response, true);


$userSchool = Yii::app()->user->school;

$result = array();

$extraData = array(

'id' => $data['id'],
'week' => $data['week'],
'description' => $data['description'],
'observation' => $data['observation'],
'foodPublicTarget' => $data['foodPublicTarget'],
'startDate' => $data['startDate'],
'finalDate' => $data['finalDate'],
);

$result = array_merge($result, $extraData);

foreach ($data as $key => $day) {
if (!is_array($day)) {
continue;
}

$dayMeals = array();
foreach ($day as $meal) {
/*$idMeal = $meal['idMeal'];*/
$mealData = array(
'escola_acionada' => $userSchool,
'time' => $meal['time'],
'sequence' => $meal['sequence'],
'turn' => $meal['turn'],
'foodMealType' => $meal['foodMealType'],
'foodPublicTargetId' => $meal['foodPublicTargetId'],
'foodPublicTargetName' => $meal['foodPublicTargetName'],
'foodMealTypeDescription' => $meal['foodMealTypeDescription'],

);

foreach ($meal['mealsComponent'] as $component) {



$mealData['mealsComponent'][] = array(

'id_meal_food' => $component['idMeal'],
'description' => $component['description'],
'ingredients' => array_map(function ($ingredient) use ($component) {
$idMeal = $component['idMeal'];
$isInStock = ($ingredient['statusInventoryFood'] == 'Emfalta') ? false : true;
$itemRecommendation = array();
if (!$isInStock) {
$itemRecommendation = Recommendations::model()->findAllByAttributes(array('item_reference_id' => $ingredient['foodIdFk']));
}

return array(
'id_food' => $ingredient['foodIdFk'],
'id_meal_food' => $idMeal,
'foodName' => $ingredient['foodName'],
"amount" => $ingredient['amount'],
"foodMeasureUnitId" => $ingredient['foodMeasureUnitId'],
"lip" => $ingredient['lip'],
"pt" => $ingredient['pt'],
"cho" => $ingredient['cho'],
"kcal" => $ingredient['kcal'],
"nameFood" => $ingredient['nameFood'],
"measurementUnit" => $ingredient['measurementUnit'],
'statusInventoryFood' => $ingredient['statusInventoryFood'],
'isInStock' => $isInStock,
'itemReference' => $this->mapRecommendations($itemRecommendation),
);
}, $component['ingredients']),
);
}

$dayMeals[] = $mealData;
}

$result[$key] = $dayMeals;
}

echo CJSON::encode($result);
}




private function mapRecommendations($itemRecommendation)
{
$resultRecommendation = array();
$userSchool = Yii::app()->user->school;

foreach ($itemRecommendation as $recommendationItem) {
$foodInventoryItem = FoodInventory::model()->findByAttributes(array('food_fk' => $recommendationItem->item_codigo));
if ($foodInventoryItem !== null &&
/* ($foodInventoryItem->status === 'Disponivel' || $foodInventoryItem->status === 'Acabando')) {*/
($foodInventoryItem->status === 'Disponivel' || $foodInventoryItem->status === 'Acabando') &&
($foodInventoryItem->school_fk === $userSchool)) {

$item = array(
'escola_banco' => $foodInventoryItem->school_fk,
'codigo' => $recommendationItem->item_codigo,
'item_nome' => $recommendationItem->item_nome,
'score' => $recommendationItem->score,
'normalized_score' => $recommendationItem->normalized_score,
'semaforo' => $recommendationItem->traffic_light_color,
'amount' => $foodInventoryItem->amount,
'measurementUnit' => $foodInventoryItem->measurementUnit,
);
$resultRecommendation[] = $item;
}
}

return $resultRecommendation;
}

public function actionUpdateFoodMeal($id, $idMeal, $idFoodSubst)
{
$foodIngredient = FoodIngredient::model()->findAllByAttributes(array(
'food_menu_meal_componentId' => $idMeal,
'food_id_fk' => $idFoodSubst
));

$result = array();
foreach ($foodIngredient as $ingredient) {
$ingredient->food_id_fk = $id;
$ingredient->save();
$result[] = $ingredient->id;
}

echo json_encode($result);
}

public function actionIndex()
{
$dataProvider = new CActiveDataProvider(
'foodmenu',
'foodmenu',
array(
'pagination' => false
)
Expand Down Expand Up @@ -396,4 +524,25 @@ public function actionGetFoodMeasurement()
}
return $options;
}

public function actionGetRecommendation()
{
$itemRecommendation = Recommendations::model()->findAll();
$resultRecommendation = array();

foreach ($itemRecommendation as $recommendationItem) {
$item = array(
'codigo' => $recommendationItem->item_codigo,
'nome' => $recommendationItem->item_nome,
'grupo' => $recommendationItem->score,
'caloria' => $recommendationItem->normalized_score,
'proteina' => $recommendationItem->traffic_light_color,
);
$resultRecommendation[] = $item;
}

// Retorna os resultados como JSON
header('Content-Type: application/json');
echo json_encode($resultRecommendation);
}
}
Loading
Loading