function select_balance_meals($plan, $nutrition_type, $variance, $micros_array, $meat_meals, $fish_meals, $meals_plan, $days_npes_cal, $protin, $fat, $carbo, $meal_cat, $weeknum, $last_category_meals, &$fin_meals, &$fin_meals_db, &$final_meal_ids, &$pescetarian_fish_count, &$nospecial_meat_fish_count)
{
    $micros_diff = array();
    $assigned_days = array();
    if (!empty($micros_array)) {
        // If chosen plan = Sparfuchs then repeat main meals all weeks.
        try {
            if ($plan == 162) {
                if ($weeknum - 1 > 0 && !empty($fin_meals[$weeknum - 1]) && count($fin_meals[$weeknum - 1]) > 0) {
                    if ($meal_cat != "pre_lunch_snack" && $meal_cat != "pre_dinner_snack") {
                        $assigned_days = array("mon", "tue", "wed", "thu", "fri", "sat");
                        shuffle($assigned_days);
                        $random_days = $assigned_days;
                        $assigned_days = array("mon", "tue", "wed", "thu", "fri", "sat");
                        foreach ($assigned_days as $d => $v) {
                            if (array_key_exists($meal_cat, $fin_meals[$weeknum - 1][$random_days[$d]]) && !empty($fin_meals[$weeknum - 1][$random_days[$d]][$meal_cat])) {
                                $final_id = key($fin_meals[$weeknum - 1][$random_days[$d]][$meal_cat]);
                                $final_meal_ids[$final_id] = $final_meal_ids[$final_id] + 1;
                                $micros_diff[$v][$meal_cat][$final_id] = $final_id;
                                $fin_meals_db[$v][$meal_cat][$final_id] = $final_id;
                                $fin_meals[$weeknum][$v][$meal_cat] = $fin_meals[$weeknum - 1][$random_days[$d]][$meal_cat];
                                $fin_meals[$weeknum][$v][$meal_cat][$final_id]['reassigned'] = false;
                                $fin_meals[$weeknum][$v][$meal_cat][$final_id]['repeated'] = true;
                            } else {
                                unset($assigned_days[$v]);
                            }
                        }
                    }
                }
            } else {
                // If chosen plan != Sparfuchs then repeat main meals of first 2 week to next 2 weeks.
                if ($weeknum - 2 > 0 && !empty($fin_meals[$weeknum - 2]) && count($fin_meals[$weeknum - 2]) > 0) {
                    if (in_array($weeknum, array(3, 4, 7, 8, 11, 12))) {
                        if ($meal_cat != "pre_lunch_snack" && $meal_cat != "pre_dinner_snack") {
                            $assigned_days = array("mon", "tue", "wed", "thu", "fri", "sat");
                            shuffle($assigned_days);
                            $random_days = $assigned_days;
                            $assigned_days = array("mon", "tue", "wed", "thu", "fri", "sat");
                            foreach ($assigned_days as $d => $v) {
                                if (array_key_exists($meal_cat, $fin_meals[$weeknum - 1][$random_days[$d]]) && !empty($fin_meals[$weeknum - 2][$random_days[$d]][$meal_cat])) {
                                    $final_id = key($fin_meals[$weeknum - 2][$random_days[$d]][$meal_cat]);
                                    $final_meal_ids[$final_id] = $final_meal_ids[$final_id] + 1;
                                    $micros_diff[$v][$meal_cat][$final_id] = $final_id;
                                    $fin_meals_db[$v][$meal_cat][$final_id] = $final_id;
                                    $fin_meals[$weeknum][$v][$meal_cat] = $fin_meals[$weeknum - 2][$random_days[$d]][$meal_cat];
                                    $fin_meals[$weeknum][$v][$meal_cat][$final_id]['reassigned'] = false;
                                    $fin_meals[$weeknum][$v][$meal_cat][$final_id]['repeated'] = true;
                                } else {
                                    unset($assigned_days[$v]);
                                    //echo "assigned_days:".$v."<pre>";print_r($assigned_days);
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception $e) {
            $error = "In Repitition of meal on different plan:" . $e->getMessage();
            $date1 = date('Y-m-d H:i:s');
            $cron_sql = "insert into up_cron_status (order_id,description,description_done_datetime,site_id) values ('" . $order_id . "','" . $error . "','" . $date1 . "','" . $blog_id . "')";
            $cron = $wpdb->query($cron_sql);
        }
        try {
            foreach ($days_npes_cal as $d => $v) {
                if (!in_array($d, $assigned_days)) {
                    $variance = array_sort($variance, 'exchangble', $d, $meals_plan[$meal_cat]);
                    //echo "day wise ".$d." after sort meal<pre>";print_r($variance);
                    $is_found = true;
                    foreach ($variance as $k => $m) {
                        $is_found = false;
                        $min_variance = $v[$meal_cat][1];
                        $max_variance = $v[$meal_cat][2];
                        /*echo "day:".$d."</br>";
                        		echo "min variance:".$min_variance."</br>";
                        		echo "max variance:".$max_variance."</br>";*/
                        $pro_diff = $protin;
                        $fat_diff = $fat;
                        $carb_diff = $carbo;
                        $new_kcal_meal_cat = $micros_array[$m['meal_id']]['kcal'];
                        if ($meal_cat == "breakfast" || $meal_cat == "lunch" || $meal_cat == "dinner") {
                            if ($new_kcal_meal_cat <= $v[$meal_cat][0]) {
                                $meals_below_kcal[$m['meal_id']] = $new_kcal_meal_cat;
                            } else {
                                $meals_above_kcal[$m['meal_id']] = $new_kcal_meal_cat;
                            }
                        }
                        if (!empty($last_category_meals) && !empty($last_category_meals[$d]) && count($last_category_meals) > 0) {
                            $last_category = key($last_category_meals[$d]);
                            if (!empty($last_category_meals[$d][$last_category]) && !empty(key($last_category_meals[$d][$last_category]))) {
                                $last_meal_id = key($last_category_meals[$d][$last_category]);
                                //echo "last meal id:".key($last_category_meals[$d][$last_category]);
                                if (!is_null($last_meal_id) && $last_meal_id != 0) {
                                    $new_kcal_meal_cat = $v[$last_category][0] - $micros_array[$last_meal_id]['kcal'] + $v[$meal_cat][0];
                                    $min_variance = @round($new_kcal_meal_cat - $new_kcal_meal_cat * (15 / 100), 2);
                                    $max_variance = @round($new_kcal_meal_cat + $new_kcal_meal_cat * (15 / 100), 2);
                                    $pro_diff = $protin - $micros_array[$last_meal_id]['Protein'] + $protin;
                                    $fat_diff = $fat - $micros_array[$last_meal_id]['Fett'] + $fat;
                                    $carb_diff = $carbo - $micros_array[$last_meal_id]['Kohlenhydrate'] + $carbo;
                                }
                            }
                        }
                        $dif = $new_kcal_meal_cat + $pro_diff + $fat_diff + $carb_diff;
                        $kcal = $micros_array[$m['meal_id']]['kcal'];
                        //echo "meal_id: ".$m['meal_id']." meal kcal:".$kcal."</br>";
                        if ($kcal >= $min_variance && $kcal <= $max_variance) {
                            $diff_array = array();
                            $current_meal_variance = $m['variance'][$d][$meals_plan[$meal_cat]];
                            $i = $k;
                            while (!empty($variance[$i])) {
                                $next_meal_variance = $variance[$i]['variance'][$d][$meals_plan[$meal_cat]];
                                if ($current_meal_variance != $next_meal_variance) {
                                    break;
                                } else {
                                    $next_micro_values = $micros_array[$variance[$i]['meal_id']];
                                    $diff_array[$variance[$i]['meal_id']] = $dif - ($next_micro_values['kcal'] + $next_micro_values['Protein_variance'] + $next_micro_values['Fett_variance'] + $next_micro_values['Kohlenhydrate_variance']);
                                    $i++;
                                }
                            }
                            $new_meal_id = $m['meal_id'];
                            if (!empty($diff_array) && count($diff_array) > 1) {
                                asort($diff_array);
                                $new_meal_id = key($diff_array);
                            }
                            //echo "in select balance meal:<pre>";print_r($micros_array[$new_meal_id]);
                            //echo "new meal id:".$new_meal_id."</br>";
                            $micro_values = $micros_array[$new_meal_id];
                            //echo "in select balance meal micro values:<pre>";print_r($micro_values);
                            if (empty($final_meal_ids) || !array_key_exists($new_meal_id, $final_meal_ids)) {
                                //echo "in select balance meal micro values:<pre>";print_r($micro_values);
                                $final_meal_id = meals_output_per_weeks($nutrition_type, $d, $meal_cat, $weeknum, $micro_values, $pescetarian_fish_count, $nospecial_meat_fish_count);
                                if (!empty($final_meal_id) && $final_meal_id != 0) {
                                    $is_found = true;
                                    $final_meal_ids[$final_meal_id] = 1;
                                    $micros_diff[$d][$meal_cat][$final_meal_id] = $final_meal_id;
                                    $fin_meals_db[$d][$meal_cat][$final_meal_id] = $final_meal_id;
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['meal_id'] = $final_meal_id;
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_ideal'] = $v[$meal_cat][0];
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_min'] = $min_variance;
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_max'] = $max_variance;
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal'] = $micros_array[$final_meal_id]['kcal'];
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_new'] = $new_kcal_meal_cat;
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['reassigned'] = false;
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['repeated'] = false;
                                    /*$fin_meals[$d][$meal_cat][$final_meal_id]['Protein'] = $micros_array[$final_meal_id]['Protein'];
                                      $fin_meals[$d][$meal_cat][$final_meal_id]['protein_new'] = $pro_diff;
                                      $fin_meals[$d][$meal_cat][$final_meal_id]['Fatt'] = $micros_array[$final_meal_id]['Fett'];
                                      $fin_meals[$d][$meal_cat][$final_meal_id]['fatt_new'] = $fat_diff;
                                      $fin_meals[$d][$meal_cat][$final_meal_id]['Kohlenhydrate'] = $micros_array[$final_meal_id]['Kohlenhydrate'];
                                      $fin_meals[$d][$meal_cat][$final_meal_id]['Kohlenhydrate_new'] = $carb_diff;*/
                                    break;
                                }
                            }
                        }
                    }
                    //if no compatible food found then assigned that are already assigned.
                    if ($is_found == false && !empty($final_meal_ids) && count($final_meal_ids) > 0) {
                        asort($final_meal_ids);
                        foreach ($final_meal_ids as $key_meal => $meal_id) {
                            $min_variance = $v[$meal_cat][1];
                            $max_variance = $v[$meal_cat][2];
                            $pro_diff = $protin;
                            $fat_diff = $fat;
                            $carb_diff = $carbo;
                            $new_kcal_meal_cat = $micros_array[$key_meal]['kcal'];
                            if (!empty($last_category_meals) && !empty($last_category_meals[$d])) {
                                $last_category = key($last_category_meals[$d]);
                                if (!empty($last_category_meals[$d][$last_category]) && !empty(key($last_category_meals[$d][$last_category]))) {
                                    $last_meal_id = key($last_category_meals[$d][$last_category]);
                                    if (!is_null($last_meal_id) && $last_meal_id != 0) {
                                        $new_kcal_meal_cat = $v[$last_category][0] - $micros_array[$last_meal_id]['kcal'] + $v[$meal_cat][0];
                                        $min_variance = @round($new_kcal_meal_cat - $new_kcal_meal_cat * (15 / 100), 2);
                                        $max_variance = @round($new_kcal_meal_cat + $new_kcal_meal_cat * (15 / 100), 2);
                                    }
                                }
                            }
                            $kcal = $micros_array[$key_meal]['kcal'];
                            if ($kcal >= $min_variance && $kcal <= $max_variance) {
                                if (array_key_exists($new_meal_id, $micros_array)) {
                                    $micro_values = $micros_array[$new_meal_id];
                                }
                                //echo "in select balance meal micro values:<pre>";print_r($micro_values);
                                $final_meal_id = meals_output_per_weeks($nutrition_type, $d, $meal_cat, $weeknum, $micro_values, $pescetarian_fish_count, $nospecial_meat_fish_count);
                                if (!empty($final_meal_id) && $final_meal_id != 0) {
                                    $is_found = true;
                                    //echo "final_meal_id".$final_meal_ids[$final_meal_id]."</br>";
                                    $final_meal_ids[$final_meal_id] = (isset($final_meal_ids[$final_meal_id]) ? $final_meal_ids[$final_meal_id] : 0) + 1;
                                    $micros_diff[$d][$meal_cat][$final_meal_id] = $final_meal_id;
                                    $fin_meals_db[$d][$meal_cat][$final_meal_id] = $final_meal_id;
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['meal_id'] = $final_meal_id;
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_ideal'] = $v[$meal_cat][0];
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_min'] = $min_variance;
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_max'] = $max_variance;
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal'] = $micros_array[$final_meal_id]['kcal'];
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_new'] = $new_kcal_meal_cat;
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['reassigned'] = true;
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['repeated'] = false;
                                    break;
                                }
                            }
                        }
                    }
                    if ($is_found == false) {
                        if (!empty($meals_below_kcal)) {
                            arsort($meals_below_kcal);
                            $final_meal_id = key($meals_below_kcal);
                            $final_meal_ids[$final_meal_id] = (isset($final_meal_ids[$final_meal_id]) ? $final_meal_ids[$final_meal_id] : 0) + 1;
                            $micros_diff[$d][$meal_cat][$final_meal_id] = $final_meal_id;
                            $fin_meals_db[$d][$meal_cat][$final_meal_id] = $final_meal_id;
                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['meal_id'] = $final_meal_id;
                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_ideal'] = $v[$meal_cat][0];
                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_min'] = $min_variance;
                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_max'] = $max_variance;
                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal'] = $micros_array[$final_meal_id]['kcal'];
                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_new'] = $new_kcal_meal_cat;
                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['reassigned'] = false;
                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['repeated'] = true;
                        } else {
                            if (!empty($meals_above_kcal)) {
                                asort($meals_above_kcal);
                                $final_meal_id = key($meals_above_kcal);
                                $final_meal_ids[$final_meal_id] = (isset($final_meal_ids[$final_meal_id]) ? $final_meal_ids[$final_meal_id] : 0) + 1;
                                $micros_diff[$d][$meal_cat][$final_meal_id] = $final_meal_id;
                                $fin_meals_db[$d][$meal_cat][$final_meal_id] = $final_meal_id;
                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['meal_id'] = $final_meal_id;
                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_ideal'] = $v[$meal_cat][0];
                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_min'] = $min_variance;
                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_max'] = $max_variance;
                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal'] = $micros_array[$final_meal_id]['kcal'];
                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_new'] = $new_kcal_meal_cat;
                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['reassigned'] = false;
                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['repeated'] = true;
                            } else {
                                $micros_diff[$d][$meal_cat][NULL] = NULL;
                                $fin_meals_db[$d][$meal_cat][NULL] = NULL;
                                $fin_meals[$weeknum][$d][$meal_cat][NULL] = NULL;
                            }
                        }
                    }
                }
            }
        } catch (Exception $e) {
            $error = "In Assigning meal on each day:" . $e->getMessage();
            $date1 = date('Y-m-d H:i:s');
            $cron_sql = "insert into up_cron_status (order_id,description,description_done_datetime,site_id) values ('" . $order_id . "','" . $error . "','" . $date1 . "','" . $blog_id . "')";
            $cron = $wpdb->query($cron_sql);
        }
    }
    return $micros_diff;
}
function select_balance_meals($plan, $nutrition_type, $variance1, $micros_array, $meat_meals, $fish_meals, $meals_plan, $days_npes_cal, $protin, $fat, $carbo, $meal_cat, $weeknum, $last_category_meals, &$fin_meals, &$fin_meals_db, &$final_meal_ids, &$week_meal_ids, &$pescetarian_fish_count, &$nospecial_meat_fish_count, $sweet_tooth)
{
    $last_week_no = 0;
    $micros_diff = array();
    $week_meals_count = $used_below_kcal_meals = $used_above_kcal_meals = $variance = $assigned_days = array();
    $meals_below_kcal = array();
    $meals_above_kcal = $check_swt_meals = array();
    /*if($meal_cat=='breakfast'){
    			echo 'breakfast Sweet tooth';
    			print_r($variance);
    	}*/
    //echo 'micros_array';
    //print_r($micros_array);
    if (!empty($micros_array)) {
        // If chosen plan = Sparfuchs then repeat main meals all weeks.
        try {
            if ($plan == 162) {
                $last_week_no = $weeknum - 1;
                if ($weeknum - 1 > 0 && !empty($fin_meals[$weeknum - 1]) && count($fin_meals[$weeknum - 1]) > 0) {
                    if ($meal_cat != "pre_lunch_snack" && $meal_cat != "pre_dinner_snack") {
                        $assigned_days = array("mon", "tue", "wed", "thu", "fri", "sat");
                        shuffle($assigned_days);
                        $random_days = $assigned_days;
                        $assigned_days = array("mon", "tue", "wed", "thu", "fri", "sat");
                        foreach ($assigned_days as $d => $v) {
                            if (array_key_exists($meal_cat, $fin_meals[$weeknum - 1][$random_days[$d]]) && !empty($fin_meals[$weeknum - 1][$random_days[$d]][$meal_cat])) {
                                $final_id = key($fin_meals[$weeknum - 1][$random_days[$d]][$meal_cat]);
                                $final_meal_ids[$final_id] = $final_meal_ids[$final_id] + 1;
                                $micros_diff[$v][$meal_cat][$final_id] = $final_id;
                                $fin_meals_db[$v][$meal_cat][$final_id] = $final_id;
                                $fin_meals[$weeknum][$v][$meal_cat] = $fin_meals[$weeknum - 1][$random_days[$d]][$meal_cat];
                                $fin_meals[$weeknum][$v][$meal_cat][$final_id]['reassigned'] = false;
                                $fin_meals[$weeknum][$v][$meal_cat][$final_id]['repeated'] = true;
                                if (empty($week_meal_ids) || !in_array($final_id, $week_meal_ids)) {
                                    $week_meal_ids[$final_id] = $final_id;
                                }
                            } else {
                                unset($assigned_days[$v]);
                            }
                        }
                    }
                }
            } else {
                // If chosen plan != Sparfuchs then repeat main meals of first 2 week to next 2 weeks.
                $last_week_no = $weeknum - 2;
                if ($weeknum - 2 > 0 && !empty($fin_meals[$weeknum - 2]) && count($fin_meals[$weeknum - 2]) > 0) {
                    if (in_array($weeknum, array(3, 4, 7, 8, 11, 12))) {
                        if ($meal_cat != "pre_lunch_snack" && $meal_cat != "pre_dinner_snack") {
                            $assigned_days = array("mon", "tue", "wed", "thu", "fri", "sat");
                            shuffle($assigned_days);
                            $random_days = $assigned_days;
                            $assigned_days = array("mon", "tue", "wed", "thu", "fri", "sat");
                            foreach ($assigned_days as $d => $v) {
                                if (array_key_exists($meal_cat, $fin_meals[$weeknum - 1][$random_days[$d]]) && !empty($fin_meals[$weeknum - 2][$random_days[$d]][$meal_cat])) {
                                    $final_id = key($fin_meals[$weeknum - 2][$random_days[$d]][$meal_cat]);
                                    $final_meal_ids[$final_id] = $final_meal_ids[$final_id] + 1;
                                    $micros_diff[$v][$meal_cat][$final_id] = $final_id;
                                    $fin_meals_db[$v][$meal_cat][$final_id] = $final_id;
                                    $fin_meals[$weeknum][$v][$meal_cat] = $fin_meals[$weeknum - 2][$random_days[$d]][$meal_cat];
                                    $fin_meals[$weeknum][$v][$meal_cat][$final_id]['reassigned'] = false;
                                    $fin_meals[$weeknum][$v][$meal_cat][$final_id]['repeated'] = true;
                                    if (empty($week_meal_ids) || !in_array($final_id, $week_meal_ids)) {
                                        $week_meal_ids[$final_id] = $final_id;
                                    }
                                } else {
                                    unset($assigned_days[$v]);
                                    //echo "assigned_days:".$v."<pre>";print_r($assigned_days);
                                }
                            }
                        }
                    }
                }
            }
            if ($last_week_no > 0 && $sweet_tooth == 'yes' && $meal_cat == 'pre_lunch_snack') {
                $check_swt_meals = search_sweet_tooth_meals($fin_meals[$weeknum]['wed'], 'sweet_tooth', 1, 'yes');
                echo '$check_swt_meals';
                //print_r($check_swt_meals);
                $cnt_st_meals = count($check_swt_meals);
                if ($cnt_st_meals <= 0 && $last_week_no > 0) {
                    $check_swt_meals1 = search_sweet_tooth_meals($fin_meals[$last_week_no], 'sweet_tooth', 1, 'yes');
                    if (!empty($check_swt_meals1)) {
                        $random_key = array_rand($check_swt_meals1);
                        $cats = array('breakfast', 'lunch', 'dinner', 'pre_dinner_snack');
                        $cat_k = array_rand($cats);
                        $temp_key = key($fin_meals[$weeknum]['wed'][$cats[$cat_k]]);
                        unset($fin_meals[$weeknum]['wed'][$cats[$cat_k]][$temp_key]);
                        $fin_meals[$weeknum]['wed'][$cats[$cat_k]][$random_key] = $check_swt_meals1[$random_key];
                    }
                } else {
                    if ($cnt_st_meals > 1) {
                        $except_swt_meals = search_sweet_tooth_meals($fin_meals[$last_week_no], 'sweet_tooth', 1, 'no');
                        echo 'except_swt_meals';
                        //print_r($except_swt_meals);
                        $wed_meals = $fin_meals[$weeknum]['wed'];
                        $sm = 0;
                        foreach ($wed_meals as $k => $wm) {
                            foreach ($wm as $k1 => $w) {
                                if ($w['sweet_tooth'] == 1) {
                                    $sm++;
                                    if ($sm > 1) {
                                        $meal_c = $k;
                                        unset($fin_meals[$weeknum]['wed'][$k][$k1]);
                                        //	print_r($fin_meals[$weeknum]['wed']);
                                        //	echo '$wm='.$k;
                                        //print_r($fin_meals[$weeknum]['wed']);
                                        if (!empty($except_swt_meals)) {
                                            $not_swt_meal = array_rand($except_swt_meals);
                                            $fin_meals[$weeknum]['wed'][$k][$not_swt_meal] = $except_swt_meals[$not_swt_meal];
                                            $final_meal_ids[$not_swt_meal] = $final_meal_ids[$not_swt_meal] + 1;
                                            unset($except_swt_meals[$not_swt_meal]);
                                        }
                                        //print_r($fin_meals[$weeknum]['wed']);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception $e) {
            $error = "In Repitition of meal on different plan:" . $e->getMessage();
            $date1 = date('Y-m-d H:i:s');
            $cron_sql = "insert into up_cron_status (order_id,description,description_done_datetime,site_id) values ('" . $order_id . "','" . $error . "','" . $date1 . "','" . $blog_id . "')";
            $cron = $wpdb->query($cron_sql);
        }
        try {
            foreach ($days_npes_cal as $d => $v) {
                //	echo '<br/>$d1='.$d;
                if (!in_array($d, $assigned_days)) {
                    $st_value = $meals_count_week = array();
                    /*	echo 'swt_count='.count($st_value);
                    			
                    			echo '<br/>Week_no='.$weeknum.'<br/>';
                    			echo 'Meal_cat='.$meal_cat.'<br/>';
                    			*/
                    $st_is_already_taken = 0;
                    $variance = array_sort($variance1, 'exchangble', $d, $meals_plan[$meal_cat]);
                    //     echo "day wise ".$d." after sort meal<pre>";print_r($variance);
                    $is_found = true;
                    $meals_below_kcal = $meals_above_kcal = $skipped_meals = $skipped_reassigned_meals = array();
                    foreach ($variance as $k => $m) {
                        $is_found = false;
                        $min_variance = $v[$meal_cat][1];
                        $max_variance = $v[$meal_cat][2];
                        /*echo "day:".$d."</br>";
                        		echo "min variance:".$min_variance."</br>";
                        		echo "max variance:".$max_variance."</br>";*/
                        $pro_diff = $protin;
                        $fat_diff = $fat;
                        $carb_diff = $carbo;
                        $new_kcal_meal_cat = $micros_array[$m['meal_id']]['kcal'];
                        if ($meal_cat == "breakfast" || $meal_cat == "lunch" || $meal_cat == "dinner") {
                            if ($new_kcal_meal_cat <= $v[$meal_cat][0]) {
                                $meals_below_kcal[$m['meal_id']] = $new_kcal_meal_cat;
                            } else {
                                $meals_above_kcal[$m['meal_id']] = $new_kcal_meal_cat;
                            }
                        }
                        if (!empty($last_category_meals) && !empty($last_category_meals[$d]) && count($last_category_meals) > 0) {
                            $last_category = key($last_category_meals[$d]);
                            if (!empty($last_category_meals[$d][$last_category]) && !empty(key($last_category_meals[$d][$last_category]))) {
                                $last_meal_id = key($last_category_meals[$d][$last_category]);
                                //echo "last meal id:".key($last_category_meals[$d][$last_category]);
                                if (!is_null($last_meal_id) && $last_meal_id != 0) {
                                    $new_kcal_meal_cat = $v[$last_category][0] - $micros_array[$last_meal_id]['kcal'] + $v[$meal_cat][0];
                                    $min_variance = @round($new_kcal_meal_cat - $new_kcal_meal_cat * (15 / 100), 2);
                                    $max_variance = @round($new_kcal_meal_cat + $new_kcal_meal_cat * (15 / 100), 2);
                                    $pro_diff = $protin - $micros_array[$last_meal_id]['Protein'] + $protin;
                                    $fat_diff = $fat - $micros_array[$last_meal_id]['Fett'] + $fat;
                                    $carb_diff = $carbo - $micros_array[$last_meal_id]['Kohlenhydrate'] + $carbo;
                                }
                            }
                        }
                        $dif = $new_kcal_meal_cat + $pro_diff + $fat_diff + $carb_diff;
                        $kcal = $micros_array[$m['meal_id']]['kcal'];
                        /*	echo '<br/>above kcal: '.$meal_cat;
                              				echo '<br/>$d='.$d;
                              				print_r($fin_meals[$weeknum][$d]);
                        				echo '<br/>Week_no='.$weeknum.'<br/>';
                        			echo "meal_id: ".$m['meal_id']." meal kcal:".$kcal."</br>";
                              		*/
                        if ($kcal >= $min_variance && $kcal <= $max_variance) {
                            $diff_array = array();
                            $current_meal_variance = $m['variance'][$d][$meals_plan[$meal_cat]];
                            $i = $k;
                            while (!empty($variance[$i])) {
                                $next_meal_variance = $variance[$i]['variance'][$d][$meals_plan[$meal_cat]];
                                if ($current_meal_variance != $next_meal_variance) {
                                    break;
                                } else {
                                    $next_micro_values = $micros_array[$variance[$i]['meal_id']];
                                    $diff_array[$variance[$i]['meal_id']] = $dif - ($next_micro_values['kcal'] + $next_micro_values['Protein_variance'] + $next_micro_values['Fett_variance'] + $next_micro_values['Kohlenhydrate_variance']);
                                    $i++;
                                }
                            }
                            $new_meal_id = $m['meal_id'];
                            if (!empty($diff_array) && count($diff_array) > 1) {
                                asort($diff_array);
                                $new_meal_id = key($diff_array);
                            }
                            //	echo "in select balance meal:<pre>";print_r($micros_array[$new_meal_id]);
                            //echo "new meal id:".$new_meal_id."</br>";
                            $micro_values = $micros_array[$new_meal_id];
                            //    echo "in select balance meal micro values:<pre>";print_r($micro_values);
                            //print_r($micros_array);
                            if (empty($final_meal_ids) || !array_key_exists($new_meal_id, $final_meal_ids)) {
                                //echo "in select balance meal micro values:<pre>";print_r($micro_values);
                                $final_meal_id = meals_output_per_weeks($nutrition_type, $d, $meal_cat, $weeknum, $micro_values, $pescetarian_fish_count, $nospecial_meat_fish_count);
                                //  $st_is_already_taken=0;
                                echo '<br/>$meal_cat1=' . $meal_cat . PHP_EOL;
                                echo '<br/>$d1=' . $d;
                                echo '<br/>$weeknum1=' . $weeknum;
                                echo '<br/>f_meal1' . $final_meal_id;
                                if (!empty($final_meal_id) && $final_meal_id != 0) {
                                    //echo '<br/>final_meal_id:'.$final_meal_id;
                                    print_r($micros_array[$final_meal_id]);
                                    echo 'sweet_tooth' . $sweet_tooth;
                                    if ($sweet_tooth == 'yes' && ($d == 'wed' || $d == 'sun') || $sweet_tooth == 'sometime' && $d == 'sun') {
                                        //echo '<br/>final_meal_id_if: '.$final_meal_id;
                                        if (!empty($fin_meals[$weeknum][$d])) {
                                            $st_value = search_sweet_tooth_meals($fin_meals[$weeknum][$d], 'sweet_tooth', '1', 'yes');
                                        }
                                        if (count($st_value) <= 0) {
                                            echo 'Sweet meal is not taken <br/>';
                                            if ($micros_array[$final_meal_id]['sweet_tooth'] == 1) {
                                                echo 'assign sweet tooth meal1 <br/>';
                                                $st_is_already_taken = 1;
                                                $is_found = true;
                                                $final_meal_ids[$final_meal_id] = (isset($final_meal_ids[$final_meal_id]) ? $final_meal_ids[$final_meal_id] : 0) + 1;
                                                $week_meals_count[$final_meal_id] = (isset($week_meals_count[$final_meal_id]) ? $week_meals_count[$final_meal_id] : 0) + 1;
                                                $micros_diff[$d][$meal_cat][$final_meal_id] = $final_meal_id;
                                                $fin_meals_db[$d][$meal_cat][$final_meal_id] = $final_meal_id;
                                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['meal_id'] = $final_meal_id;
                                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['csv_id'] = $micros_array[$final_meal_id]['csv_id'];
                                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['sweet_tooth'] = $micros_array[$final_meal_id]['sweet_tooth'];
                                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_ideal'] = $v[$meal_cat][0];
                                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_min'] = $min_variance;
                                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_max'] = $max_variance;
                                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal'] = $micros_array[$final_meal_id]['kcal'];
                                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_new'] = $new_kcal_meal_cat;
                                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['reassigned'] = false;
                                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['repeated'] = false;
                                                if (empty($week_meal_ids) || !in_array($final_meal_id, $week_meal_ids)) {
                                                    $week_meal_ids[$final_meal_id] = $final_meal_id;
                                                }
                                                break;
                                            } else {
                                                //	echo 'in skipp';
                                                $skipped_meals[$final_meal_id] = $micros_array[$final_meal_id];
                                                //$skipped_meals[]=$final_meal_id;
                                            }
                                        } else {
                                            $st_is_already_taken = 1;
                                            $skipped_meals[$final_meal_id] = $micros_array[$final_meal_id];
                                        }
                                    }
                                    if ($st_is_already_taken == 1 || $sweet_tooth == 'no' || !($sweet_tooth == 'yes' && ($d == 'wed' || $d == 'sun')) || !($sweet_tooth == 'sometime' && $d == 'sun')) {
                                        //echo $d.'<br/>';
                                        //echo "<br/>is already taken:".$st_is_already_taken;
                                        if ($sweet_tooth == 'yes' && ($d == 'wed' || $d == 'sun') || $sweet_tooth == 'sometime' && $d == 'sun') {
                                            if ($st_is_already_taken == 1 && $micros_array[$final_meal_id]['sweet_tooth'] == 1) {
                                                //		echo "<br/>is sweet tooth already taken:".$st_is_already_taken;
                                                $skipped_meals[$final_meal_id] = $micros_array[$final_meal_id];
                                                continue;
                                            }
                                        }
                                        echo '<br/><br/>testhere2<br/>';
                                        echo '$d2=' . $d;
                                        echo '<br/>$meal_cat2=' . $meal_cat;
                                        echo '<br/>$weeknum2=' . $weeknum . '<br/>final_meal_id2=' . $final_meal_id;
                                        $meals_count_week = search_assigned_meals($fin_meals[$weeknum], 'meal_id', $final_meal_id, 'yes');
                                        if (count($meals_count_week) < 2) {
                                            $is_found = true;
                                            $final_meal_ids[$final_meal_id] = (isset($final_meal_ids[$final_meal_id]) ? $final_meal_ids[$final_meal_id] : 0) + 1;
                                            $week_meals_count[$final_meal_id] = (isset($week_meals_count[$final_meal_id]) ? $week_meals_count[$final_meal_id] : 0) + 1;
                                            $micros_diff[$d][$meal_cat][$final_meal_id] = $final_meal_id;
                                            $fin_meals_db[$d][$meal_cat][$final_meal_id] = $final_meal_id;
                                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['meal_id'] = $final_meal_id;
                                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['csv_id'] = $micros_array[$final_meal_id]['csv_id'];
                                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['sweet_tooth'] = $micros_array[$final_meal_id]['sweet_tooth'];
                                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_ideal'] = $v[$meal_cat][0];
                                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_min'] = $min_variance;
                                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_max'] = $max_variance;
                                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal'] = $micros_array[$final_meal_id]['kcal'];
                                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_new'] = $new_kcal_meal_cat;
                                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['reassigned'] = false;
                                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['repeated'] = false;
                                            if (empty($week_meal_ids) || !in_array($final_meal_id, $week_meal_ids)) {
                                                $week_meal_ids[$final_meal_id] = $final_meal_id;
                                            }
                                            break;
                                        }
                                    } else {
                                        $skipped_meals[$final_meal_id] = $micros_array[$final_meal_id];
                                    }
                                }
                            }
                        }
                    }
                    //----- Assign from skipped meals if no sweet tooth meal is selected ------ //
                    if ($is_found == false && !empty($skipped_meals) && count($skipped_meals) > 0) {
                        if ($sweet_tooth == 'yes' && ($d == 'wed' || $d == 'sun') || $sweet_tooth == 'sometime' && $d == 'sun') {
                            echo '<br/>is found false for sweet tooth<br/>';
                            //print_r($skipped_meals);
                            if (!empty($skipped_meals) && count($fin_meals[$weeknum][$d][$meal_cat]) <= 0) {
                                //echo 'Sweet meal is not found so assignisng from skipped meals <br/>';
                                //echo $skipped_meals[0];
                                $is_found = true;
                                //print_r($skipped_meals);
                                uasort($skipped_meals, 'sortByKeyCallback');
                                echo 'here skipped_meals';
                                echo '$weekno3=' . $weeknum;
                                echo '$d3=' . $d;
                                asort($final_meal_ids);
                                $skipped_meals_results = array();
                                foreach ($final_meal_ids as $f => $v2) {
                                    if (array_key_exists($f, $skipped_meals)) {
                                        $skipped_meals_results[$f] = $skipped_meals[$f];
                                    }
                                }
                                //print_r($skipped_meals_results);
                                if (!empty($skipped_meals_results)) {
                                    $final_meal_id = key($skipped_meals_results);
                                } else {
                                    $final_meal_id = key($skipped_meals);
                                }
                                $meals_count_week = search_assigned_meals($fin_meals[$weeknum], 'meal_id', $final_meal_id, 'yes');
                                if (count($meals_count_week) < 2) {
                                    //		$final_meal_id=array_keys($skipped_meals_results)[0];
                                    echo '$final_meal_id3=' . $final_meal_id;
                                    $final_meal_ids[$final_meal_id] = (isset($final_meal_ids[$final_meal_id]) ? $final_meal_ids[$final_meal_id] : 0) + 1;
                                    $week_meals_count[$final_meal_id] = (isset($week_meals_count[$final_meal_id]) ? $week_meals_count[$final_meal_id] : 0) + 1;
                                    $micros_diff[$d][$meal_cat][$final_meal_id] = $final_meal_id;
                                    $fin_meals_db[$d][$meal_cat][$final_meal_id] = $final_meal_id;
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['meal_id'] = $final_meal_id;
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['csv_id'] = $micros_array[$final_meal_id]['csv_id'];
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['sweet_tooth'] = $micros_array[$final_meal_id]['sweet_tooth'];
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_ideal'] = $v[$meal_cat][0];
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_min'] = $min_variance;
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_max'] = $max_variance;
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal'] = $micros_array[$final_meal_id]['kcal'];
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_new'] = $new_kcal_meal_cat;
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['reassigned'] = false;
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['repeated'] = false;
                                    if (empty($week_meal_ids) || !in_array($final_meal_id, $week_meal_ids)) {
                                        $week_meal_ids[$final_meal_id] = $final_meal_id;
                                    }
                                }
                                //  break;
                            }
                        }
                    }
                    //if no compatible food found then assigned that are already assigned.
                    if ($is_found == false && !empty($final_meal_ids) && count($final_meal_ids) > 0) {
                        asort($final_meal_ids);
                        foreach ($final_meal_ids as $key_meal => $meal_id) {
                            $min_variance = $v[$meal_cat][1];
                            $max_variance = $v[$meal_cat][2];
                            $pro_diff = $protin;
                            $fat_diff = $fat;
                            $carb_diff = $carbo;
                            $new_kcal_meal_cat = $micros_array[$key_meal]['kcal'];
                            if (!empty($last_category_meals) && !empty($last_category_meals[$d])) {
                                $last_category = key($last_category_meals[$d]);
                                if (!empty($last_category_meals[$d][$last_category]) && !empty(key($last_category_meals[$d][$last_category]))) {
                                    $last_meal_id = key($last_category_meals[$d][$last_category]);
                                    if (!is_null($last_meal_id) && $last_meal_id != 0) {
                                        $new_kcal_meal_cat = $v[$last_category][0] - $micros_array[$last_meal_id]['kcal'] + $v[$meal_cat][0];
                                        $min_variance = @round($new_kcal_meal_cat - $new_kcal_meal_cat * (15 / 100), 2);
                                        $max_variance = @round($new_kcal_meal_cat + $new_kcal_meal_cat * (15 / 100), 2);
                                    }
                                }
                            }
                            $kcal = $micros_array[$key_meal]['kcal'];
                            if ($kcal >= $min_variance && $kcal <= $max_variance) {
                                if (array_key_exists($new_meal_id, $micros_array)) {
                                    $micro_values = $micros_array[$new_meal_id];
                                }
                                //echo "in select balance meal micro values:<pre>";print_r($micro_values);
                                $final_meal_id = meals_output_per_weeks($nutrition_type, $d, $meal_cat, $weeknum, $micro_values, $pescetarian_fish_count, $nospecial_meat_fish_count);
                                if (!empty($final_meal_id) && $final_meal_id != 0) {
                                    if ($sweet_tooth == 'yes' && ($d == 'wed' || $d == 'sun') || $sweet_tooth == 'sometime' && $d == 'sun') {
                                        echo '<br/>$d_compatible=' . $d;
                                        //print_r($fin_meals[$weeknum][$d]);
                                        //print_r($fin_meals[$weeknum][$d]);
                                        $st_value = search_sweet_tooth_meals($fin_meals[$weeknum][$d], 'sweet_tooth', '1', 'yes');
                                        if (count($st_value) <= 0) {
                                            echo '<br/>in if compatible';
                                            //search_assigned_meals($fin_meals[2],'meal_id',275,'yes');
                                            $meals_count_week = search_assigned_meals($fin_meals[$weeknum], 'meal_id', $final_meal_id, 'yes');
                                            if ($micros_array[$final_meal_id]['sweet_tooth'] == 1 && count($meals_count_week) < 2) {
                                                echo '<br/>sweet_tooth_if_compatible';
                                                echo 'Final meal ID4=' . $final_meal_id;
                                                $st_is_already_taken = 1;
                                                $is_found = true;
                                                $final_meal_ids[$final_meal_id] = (isset($final_meal_ids[$final_meal_id]) ? $final_meal_ids[$final_meal_id] : 0) + 1;
                                                $week_meals_count[$final_meal_id] = (isset($week_meals_count[$final_meal_id]) ? $week_meals_count[$final_meal_id] : 0) + 1;
                                                $micros_diff[$d][$meal_cat][$final_meal_id] = $final_meal_id;
                                                $fin_meals_db[$d][$meal_cat][$final_meal_id] = $final_meal_id;
                                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['meal_id'] = $final_meal_id;
                                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['csv_id'] = $micros_array[$final_meal_id]['csv_id'];
                                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['sweet_tooth'] = $micros_array[$final_meal_id]['sweet_tooth'];
                                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_ideal'] = $v[$meal_cat][0];
                                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_min'] = $min_variance;
                                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_max'] = $max_variance;
                                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal'] = $micros_array[$final_meal_id]['kcal'];
                                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_new'] = $new_kcal_meal_cat;
                                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['reassigned'] = false;
                                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['repeated'] = false;
                                                if (empty($week_meal_ids) || !in_array($final_meal_id, $week_meal_ids)) {
                                                    $week_meal_ids[$final_meal_id] = $final_meal_id;
                                                }
                                                break;
                                            } else {
                                                $skipped_reassigned_meals[$final_meal_id] = $micros_array[$final_meal_id];
                                            }
                                        } else {
                                            $st_is_already_taken = 1;
                                            $skipped_reassigned_meals[$final_meal_id] = $micros_array[$final_meal_id];
                                        }
                                    }
                                    if ($st_is_already_taken == 1 || ($sweet_tooth == 'no' || !($sweet_tooth == 'yes' && ($d == 'wed' || $d == 'sun')) || !($sweet_tooth == 'sometime' && $d == 'sun'))) {
                                        if ($sweet_tooth == 'yes' && ($d == 'wed' || $d == 'sun') || $sweet_tooth == 'sometime' && $d == 'sun') {
                                            if ($st_is_already_taken == 1 && $micros_array[$final_meal_id]['sweet_tooth'] == 1) {
                                                $skipped_reassigned_meals[$final_meal_id] = $micros_array[$final_meal_id];
                                                echo '<br/>continue11';
                                                echo $meal_cat . '11<br/>';
                                                echo '$d11=' . $d . '<br/>';
                                                echo '$weeknum11=' . $weeknum . '<br/>';
                                                continue;
                                            }
                                        }
                                        $meals_count_week = search_assigned_meals($fin_meals[$weeknum], 'meal_id', $final_meal_id, 'yes');
                                        echo '<br/>after continue11: ' . $final_meal_id . '<br/>';
                                        echo '<br/>after continue11 count: ' . $final_meal_ids[$final_meal_id] . '<br/>';
                                        echo $meal_cat . '11<br/>';
                                        echo '$d11=' . $d . '<br/>';
                                        echo '$weeknum11=' . $weeknum . '<br/>';
                                        echo 'week_meals_count' . count($meals_count_week);
                                        if (count($meals_count_week) < 2) {
                                            echo 'inside week_meals_count';
                                            $is_found = true;
                                            $final_meal_ids[$final_meal_id] = (isset($final_meal_ids[$final_meal_id]) ? $final_meal_ids[$final_meal_id] : 0) + 1;
                                            $week_meals_count[$final_meal_id] = (isset($week_meals_count[$final_meal_id]) ? $week_meals_count[$final_meal_id] : 0) + 1;
                                            $micros_diff[$d][$meal_cat][$final_meal_id] = $final_meal_id;
                                            $fin_meals_db[$d][$meal_cat][$final_meal_id] = $final_meal_id;
                                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['meal_id'] = $final_meal_id;
                                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['csv_id'] = $micros_array[$final_meal_id]['csv_id'];
                                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['sweet_tooth'] = $micros_array[$final_meal_id]['sweet_tooth'];
                                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_ideal'] = $v[$meal_cat][0];
                                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_min'] = $min_variance;
                                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_max'] = $max_variance;
                                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal'] = $micros_array[$final_meal_id]['kcal'];
                                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_new'] = $new_kcal_meal_cat;
                                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['reassigned'] = false;
                                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['repeated'] = false;
                                            if (empty($week_meal_ids) || !in_array($final_meal_id, $week_meal_ids)) {
                                                $week_meal_ids[$final_meal_id] = $final_meal_id;
                                            }
                                            break;
                                        }
                                    } else {
                                        //$skipped_reassigned_meals[]=$final_meal_id;
                                        $skipped_reassigned_meals[$final_meal_id] = $micros_array[$final_meal_id];
                                    }
                                }
                            }
                        }
                    }
                    //==================== Assign from $skipped_reassigned_meals ====================//
                    if ($is_found == false && !empty($skipped_reassigned_meals) && count($skipped_reassigned_meals) > 0) {
                        if ($sweet_tooth == 'yes' && ($d == 'wed' || $d == 'sun') || $sweet_tooth == 'sometime' && $d == 'sun') {
                            echo '<br/>is found false for sweet tooth<br/>';
                            echo '$weekno=' . $weeknum;
                            echo '$d' . $d;
                            //print_r($skipped_meals);
                            if (!empty($skipped_reassigned_meals) && count($fin_meals[$weeknum][$d][$meal_cat]) <= 0) {
                                //echo 'Sweet meal is not found so assignisng from skipped meals <br/>';
                                //	echo $skipped_reassigned_meals[0];
                                $is_found = true;
                                uasort($skipped_reassigned_meals, 'sortByKeyCallback');
                                //if($d=='sun'){
                                /*	echo 'skipped_reassigned_meals';
                                				print_r($skipped_reassigned_meals);
                                				echo '$weekno='.$weeknum;
                                				echo '$d'.$d;
                                			*/
                                asort($final_meal_ids);
                                $skipped_reassigned_meals_results = array();
                                //	print_r($final_meal_ids);
                                foreach ($final_meal_ids as $f => $v2) {
                                    if (array_key_exists($f, $skipped_reassigned_meals)) {
                                        $skipped_reassigned_meals_results[$f] = $skipped_reassigned_meals[$f];
                                    }
                                }
                                //echo '$skipped_reassigned_meals_results';
                                if (!empty($skipped_reassigned_meals_results)) {
                                    $final_meal_id = key($skipped_reassigned_meals_results);
                                } else {
                                    $final_meal_id = key($skipped_reassigned_meals);
                                }
                                $meals_count_week = search_assigned_meals($fin_meals[$weeknum], 'meal_id', $final_meal_id, 'yes');
                                if (count($meals_count_week) < 2) {
                                    echo '$weekno5=' . $weeknum;
                                    echo '$d5' . $d;
                                    echo '$final_meal_id5' . $final_meal_id;
                                    $final_meal_ids[$final_meal_id] = (isset($final_meal_ids[$final_meal_id]) ? $final_meal_ids[$final_meal_id] : 0) + 1;
                                    $week_meals_count[$final_meal_id] = (isset($week_meals_count[$final_meal_id]) ? $week_meals_count[$final_meal_id] : 0) + 1;
                                    $micros_diff[$d][$meal_cat][$final_meal_id] = $final_meal_id;
                                    $fin_meals_db[$d][$meal_cat][$final_meal_id] = $final_meal_id;
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['meal_id'] = $final_meal_id;
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['csv_id'] = $micros_array[$final_meal_id]['csv_id'];
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['sweet_tooth'] = $micros_array[$final_meal_id]['sweet_tooth'];
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_ideal'] = $v[$meal_cat][0];
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_min'] = $min_variance;
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_max'] = $max_variance;
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal'] = $micros_array[$final_meal_id]['kcal'];
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_new'] = $new_kcal_meal_cat;
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['reassigned'] = false;
                                    $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['repeated'] = false;
                                    if (empty($week_meal_ids) || !in_array($final_meal_id, $week_meal_ids)) {
                                        $week_meal_ids[$final_meal_id] = $final_meal_id;
                                    }
                                    //print_r($fin_meals[$weeknum][$d]);
                                    //  break;
                                }
                            }
                        }
                    }
                    if ($is_found == false) {
                        if (!empty($meals_below_kcal)) {
                            arsort($meals_below_kcal);
                            echo '<br/><br/>inside meals_below_kcal';
                            echo '<br/>weekno =' . $weeknum;
                            echo '<br/>day =' . $d . '<br/>cat =' . $meal_cat;
                            print_r($meals_below_kcal);
                            print_r($used_below_kcal_meals);
                            if (!empty($used_below_kcal_meals) && count($used_below_kcal_meals) > 0) {
                                foreach ($used_below_kcal_meals as $v1) {
                                    //echo $v;
                                    if (array_key_exists($v1, $meals_below_kcal)) {
                                        //								echo 'array_key_exists';
                                        unset($meals_below_kcal[$v1]);
                                    }
                                }
                            }
                            arsort($meals_below_kcal);
                            foreach ($meals_below_kcal as $key => $value) {
                                $meals_count_week = search_assigned_meals($fin_meals[$weeknum], 'meal_id', $key, 'yes');
                                if (count($meals_count_week) < 2) {
                                    $final_meal_id = $key;
                                    break;
                                }
                            }
                            // $final_meal_id = key($meals_below_kcal);
                            echo '<br/>final_meal_id =' . $final_meal_id;
                            $used_below_kcal_meals[$final_meal_id] = $final_meal_id;
                            $final_meal_ids[$final_meal_id] = (isset($final_meal_ids[$final_meal_id]) ? $final_meal_ids[$final_meal_id] : 0) + 1;
                            $week_meals_count[$final_meal_id] = (isset($week_meals_count[$final_meal_id]) ? $week_meals_count[$final_meal_id] : 0) + 1;
                            $micros_diff[$d][$meal_cat][$final_meal_id] = $final_meal_id;
                            $fin_meals_db[$d][$meal_cat][$final_meal_id] = $final_meal_id;
                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['meal_id'] = $final_meal_id;
                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['csv_id'] = $micros_array[$final_meal_id]['csv_id'];
                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['sweet_tooth'] = $micros_array[$final_meal_id]['sweet_tooth'];
                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_ideal'] = $v[$meal_cat][0];
                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_min'] = $min_variance;
                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_max'] = $max_variance;
                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal'] = $micros_array[$final_meal_id]['kcal'];
                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_new'] = $new_kcal_meal_cat;
                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['reassigned'] = false;
                            $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['repeated'] = true;
                            if (empty($week_meal_ids) || !in_array($final_meal_id, $week_meal_ids)) {
                                $week_meal_ids[$final_meal_id] = $final_meal_id;
                            }
                        } else {
                            if (!empty($meals_above_kcal)) {
                                asort($meals_above_kcal);
                                // echo '$meals_above_kcal';
                                if (!empty($used_above_kcal_meals) && count($used_above_kcal_meals) > 0) {
                                    foreach ($used_above_kcal_meals as $v1) {
                                        if (array_key_exists($v1, $meals_above_kcal)) {
                                            unset($meals_above_kcal[$v1]);
                                        }
                                    }
                                }
                                $final_meal_id = key($meals_above_kcal);
                                $used_above_kcal_meals[$final_meal_id] = $final_meal_id;
                                $final_meal_ids[$final_meal_id] = (isset($final_meal_ids[$final_meal_id]) ? $final_meal_ids[$final_meal_id] : 0) + 1;
                                $micros_diff[$d][$meal_cat][$final_meal_id] = $final_meal_id;
                                $fin_meals_db[$d][$meal_cat][$final_meal_id] = $final_meal_id;
                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['meal_id'] = $final_meal_id;
                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['csv_id'] = $micros_array[$final_meal_id]['csv_id'];
                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['sweet_tooth'] = $micros_array[$final_meal_id]['sweet_tooth'];
                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_ideal'] = $v[$meal_cat][0];
                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_min'] = $min_variance;
                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_max'] = $max_variance;
                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal'] = $micros_array[$final_meal_id]['kcal'];
                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['kcal_new'] = $new_kcal_meal_cat;
                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['reassigned'] = false;
                                $fin_meals[$weeknum][$d][$meal_cat][$final_meal_id]['repeated'] = true;
                                if (empty($week_meal_ids) || !in_array($final_meal_id, $week_meal_ids)) {
                                    $week_meal_ids[$final_meal_id] = $final_meal_id;
                                }
                            } else {
                                $micros_diff[$d][$meal_cat][NULL] = NULL;
                                $fin_meals_db[$d][$meal_cat][NULL] = NULL;
                                $fin_meals[$weeknum][$d][$meal_cat][NULL] = NULL;
                            }
                        }
                    }
                }
            }
        } catch (Exception $e) {
            $error = "In Assigning meal on each day:" . $e->getMessage();
            $date1 = date('Y-m-d H:i:s');
            $cron_sql = "insert into up_cron_status (order_id,description,description_done_datetime,site_id) values ('" . $order_id . "','" . $error . "','" . $date1 . "','" . $blog_id . "')";
            $cron = $wpdb->query($cron_sql);
        }
    }
    //    echo 'week_meals_count';
    //  print_r($week_meals_count);
    //echo 'skipped_meals';
    //print_r($fin_meals);
    /*print_r($skipped_meals);/*
    echo 'skipped_reassigned_meals';
    print_r($skipped_reassigned_meals);
        */
    return $micros_diff;
}
function select_balance_meals($nutrition_type, $variance, $micros_array, $meat_meals, $fish_meals, $meals_plan, $days_npes_cal, $protin, $fat, $carbo, $meal_cat, $weeknum, $last_category_meals, &$fin_meals, &$pescetarian_fish_count, &$nospecial_meat_fish_count)
{
    $micros_diff = array();
    if (!empty($micros_array)) {
        foreach ($days_npes_cal as $d => $v) {
            $variance = array_sort($variance, 'exchangble', $d, $meals_plan[$meal_cat]);
            //echo "day wise ".$d." after sort meal<pre>";print_r($variance);
            foreach ($variance as $k => $m) {
                $min_variance = $v[$meal_cat][1];
                $max_variance = $v[$meal_cat][2];
                $pro_diff = $protin;
                $fat_diff = $fat;
                $carb_diff = $carbo;
                if (!empty($last_category_meals) && !empty($last_category_meals[$d])) {
                    $last_category = key($last_category_meals[$d]);
                    $last_meal_id = key($last_category_meals[$d][$last_category]);
                    //echo "<br />last_category:". $last_category;
                    //echo "<br />last_meal_id:". $last_meal_id;
                    $new_kcal_meal_cat = $v[$last_category][0] - $micros_array[$last_meal_id]['kcal'] + $v[$meal_cat][0];
                    $min_variance = @round($new_kcal_meal_cat - $new_kcal_meal_cat * (15 / 100), 2);
                    $max_variance = @round($new_kcal_meal_cat + $new_kcal_meal_cat * (15 / 100), 2);
                    $pro_diff = $protin - $micros_array[$last_meal_id]['Protein'] + $protin;
                    $fat_diff = $fat - $micros_array[$last_meal_id]['Fett'] + $fat;
                    $carb_diff = $carbo - $micros_array[$last_meal_id]['Kohlenhydrate'] + $carbo;
                }
                $dif = $new_kcal_meal_cat + $pro_diff + $fat_diff + $carb_diff;
                $kcal = $micros_array[$m['meal_id']]['kcal'];
                if ($kcal >= $min_variance && $kcal <= $max_variance) {
                    $diff_array = array();
                    $current_meal_variance = $m['variance'][$d][$meals_plan[$meal_cat]];
                    $i = $k;
                    while (!empty($variance[$i])) {
                        $next_meal_variance = $variance[$i]['variance'][$d][$meals_plan[$meal_cat]];
                        if ($current_meal_variance != $next_meal_variance) {
                            break;
                        } else {
                            $next_micro_values = $micros_array[$variance[$i]['meal_id']];
                            $diff_array[$variance[$i]['meal_id']] = $dif - ($next_micro_values['kcal'] + $next_micro_values['Protein_variance'] + $next_micro_values['Fett_variance'] + $next_micro_values['Kohlenhydrate_variance']);
                            $i++;
                        }
                    }
                    $new_meal_id = $m['meal_id'];
                    if (!empty($diff_array) && count($diff_array) > 1) {
                        asort($diff_array);
                        $new_meal_id = key($diff_array);
                    }
                    $micro_values = $micros_array[$new_meal_id];
                    if (empty($fin_meals) || !in_array_r($new_meal_id, $fin_meals)) {
                        $final_meal_id = meals_output_per_weeks($nutrition_type, $d, $meal_cat, $weeknum, $micro_values, $pescetarian_fish_count, $nospecial_meat_fish_count);
                        if (!empty($final_meal_id) && $final_meal_id != 0) {
                            $micros_diff[$d][$meal_cat][$final_meal_id] = $final_meal_id;
                            $fin_meals[$d][$meal_cat][$final_meal_id] = $final_meal_id;
                            break;
                        }
                    }
                }
            }
        }
    }
    return $micros_diff;
}