$adf_jg = "";
$products_per_sp = array();
$articles = array();
$topa_sold_week = $class_pm->querySoldSalesAndReturnsHistory(7, $adf_sp, $adf_jg);
//LAST 1 WEEK    (7 DAYS) SOLD
$topa_sold_monthly_1 = $class_pm->querySoldSalesAndReturnsHistory(30, $adf_sp, $adf_jg);
//LAST 1 MONTH  (30 DAYS) SOLD
$topa_sold_monthly_2 = $class_pm->querySoldSalesAndReturnsHistory(60, $adf_sp, $adf_jg);
//LAST 2 MONTHS (60 DAYS) SOLD
$topa_sold_monthly_3 = $class_pm->querySoldSalesAndReturnsHistory(90, $adf_sp, $adf_jg);
//LAST 3 MONTHS (90 DAYS) SOLD
$fc = new forecasting();
$fc_start = 'yesterday';
$fc_total_days = 30;
$fc_last_sold_daily_avg = product::retrieveDailyAverageSoldExcludingPromotionOrdersQuery($fc_start, $fc_total_days);
$fc_last_factors_daily_avg = $fc->calcFactorAverage($fc_start, $fc_total_days);
foreach ($topa_sold_week as $p) {
    $pid = $p['products_id'];
    $aid = $p['products_articles_id'];
    $akey = $pid . '-' . $aid;
    if (!isset($products[$pid])) {
        $products[$pid] = array('products_id' => $pid);
    }
    if (!isset($articles[$akey])) {
        $articles[$akey]['products_id'] = $pid;
        $articles[$akey]['products_articles_id'] = $aid;
    }
    $products[$pid]['weekly_sold'] += $p['total'];
    $articles[$akey]['weekly_sold'] += $p['total'];
}
foreach ($topa_sold_monthly_1 as $p) {
     $month = str_pad($m, 2, '0', STR_PAD_LEFT);
     $periods[$month] = date('M', strtotime("{$kw_year}-{$month}-01"));
 }
 $result = '<h2>LONG TERM FORECASTING (LTF) / Levelling</h2>';
 $result .= '<div class="draw-table">';
 $result .= '<table border="0" cellpadding="0" cellspacing="0">';
 $result .= '<tr><th class="w200">Period</th>';
 foreach ($periods as $p) {
     $result .= '<th class="w080">' . $p . '</th>';
 }
 $result .= '</tr>';
 $result .= '<tr class="o"><td class="bold">Period Target</td>';
 $target_period = array('11', '12');
 foreach ($periods as $pk => $p) {
     $last_day_of_period = date('t', strtotime("{$kw_year}-{$pk}-01"));
     $period_target_fc = $fc->calcFactorAverage("{$kw_year}-{$pk}-{$last_day_of_period}", $last_day_of_period);
     if (in_array($pk, $target_period)) {
         $result .= '<td class="bold tac tgtprd" style="background:#ffff00;">' . $period_target_fc . '</td>';
     } else {
         $result .= '<td class="notice tac">' . $period_target_fc . '</td>';
     }
 }
 $result .= '<tr class="e"><td class="bold">Levelling Split (percentage)</td>';
 $target_period_levelling = array('07', '08', '09', '10', '11');
 foreach ($periods as $pk => $p) {
     if (in_array($pk, $target_period_levelling)) {
         $conf_key = forecasting::CONFIG_KEY_PREFIX_LEVEL_SPLIT . $pk;
         $result .= '<td class="tac"><input type="text" id="' . $conf_key . '" value="' . $forecasting_conf[$conf_key] . '" class="w040 tac" /> %</td>';
     } else {
         $result .= '<td class="notice tac">-</td>';
     }