function getItemDetails(&$Items, $fromDate, $toDate, $periodFrom, $periodTo, $params = array())
 {
     $c = 0;
     $itStat = null;
     foreach ($Items as $item) {
         $itStat[$c] = new ItemStatistics();
         $itStat[$c]->item = $item;
         ItemStatistics::find($itStat[$c], $fromDate, $toDate, $periodFrom, $periodTo);
         $reqDet[$c] = new RequestDetail();
         $reqDet[$c]->id = $c;
         $reqDet[$c]->request_id = 0;
         $reqDet[$c]->item_id = $itStat[$c]->item->id;
         //Custom period defaults to 3 months;
         $reqDet[$c]->itemName = $itStat[$c]->item->Name;
         $reqDet[$c]->Line = $itStat[$c]->item->Line;
         $reqDet[$c]->Code = $itStat[$c]->item->Code;
         $reqDet[$c]->pendingStock = $itStat[$c]->item->Incoming;
         $reqDet[$c]->StockTime = 1;
         if (isset($params[$c]['ShipTime'])) {
             $reqDet[$c]->ShipTime = $params[$c]['ShipTime'];
         } else {
             $reqDet[$c]->ShipTime = $itStat[$c]->item->ShipDays / 30;
         }
         if ($itStat[$c]->stdDev == 0) {
             $itStat[$c]->stdDev = 1.0E-7;
         }
         if ($itStat[$c]->periodStdDev == 0) {
             $itStat[$c]->periodStdDev = 1.0E-7;
         }
         if ($itStat[$c]->trendStdDev == 0) {
             $itStat[$c]->trendStdDev = 1.0E-7;
         }
         $reqDet[$c]->estimation1 = $itStat[$c]->mean;
         $reqDet[$c]->estimation2 = $itStat[$c]->periodMean;
         $reqDet[$c]->estimation3 = $itStat[$c]->trendMean;
         $sum = 0;
         if ($reqDet[$c]->estimation1) {
             $sum += 1 / $itStat[$c]->stdDev;
             $tc1 = 1 / $itStat[$c]->stdDev;
         } else {
             $tc1 = 0;
         }
         if ($reqDet[$c]->estimation2) {
             $sum += 1 / $itStat[$c]->periodStdDev;
             $tc2 = 1 / $itStat[$c]->periodStdDev;
         } else {
             $tc2 = 0;
         }
         if ($reqDet[$c]->estimation3) {
             $sum += 1 / $itStat[$c]->trendStdDev;
             $tc3 = 1 / $itStat[$c]->trendStdDev;
         } else {
             $tc3 = 0;
         }
         if ($sum != 0) {
             $reqDet[$c]->weight1 = $tc1 / $sum;
             $reqDet[$c]->weight2 = $tc2 / $sum;
             $reqDet[$c]->weight3 = $tc3 / $sum;
         } else {
             $reqDet[$c]->weight1 = 0;
             $reqDet[$c]->weight2 = 0;
             $reqDet[$c]->weight3 = 1;
         }
         $reqDet[$c]->estimatedSales = $reqDet[$c]->estimation1 * $reqDet[$c]->weight1 + $reqDet[$c]->estimation2 * $reqDet[$c]->weight2 + $reqDet[$c]->estimation3 * $reqDet[$c]->weight3;
         $reqDet[$c]->currentStock = $itStat[$c]->meanWithinSales;
         // Media de los meses en los que SE vendio.
         if ($reqDet[$c]->estimatedSales != 0) {
             $reqDet[$c]->StockTime = $reqDet[$c]->currentStock / $reqDet[$c]->estimatedSales;
             // La estimacion es para el mes siguiente puede variar por temporada
         } else {
             $reqDet[$c]->StockTime = -1;
         }
         $reqDet[$c]->price = $itStat[$c]->item->Price;
         $reqDet[$c]->stockBreaksCount;
         // Media de tendencias.
         if (isset($params[$c]['StockTime'])) {
             $reqDet[$c]->desiredStockTime = $params[$c]['StockTime'];
         } else {
             $reqDet[$c]->desiredStockTime = 3;
         }
         $reqDet[$c]->desiredStock = $reqDet[$c]->desiredStockTime * $reqDet[$c]->estimatedSales;
         if ($reqDet[$c]->desiredStock > $reqDet[$c]->currentStock + $reqDet[$c]->pendingStock) {
             $reqDet[$c]->suggestedQty = $reqDet[$c]->desiredStock - ($reqDet[$c]->currentStock + $reqDet[$c]->pendingStock);
         } else {
             $reqDet[$c]->suggestedQty = 0;
         }
         $reqDet[$c]->orderTotal = $reqDet[$c]->suggestedQty * $reqDet[$c]->price;
         // If there's no manual quantity set the pict the app's suggestion.
         if (isset($params[$c]['ManualQty'])) {
             $reqDet[$c]->ManualQty = $params[$c]['ManualQty'];
         } else {
             $reqDet[$c]->ManualQty = $reqDet[$c]->suggestedQty;
         }
         $c++;
     }
     return $reqDet;
 }
 public static function find($itStat, $fromDate, $toDate, $periodFrom, $periodTo)
 {
     $itStat->meanWithinSales = $itStat->Stock = $itStat->item->getStock($toDate);
     list($totalRows, $months, $x, $Qtys, $itStat->mean) = ItemStatistics::getDataAndMean($itStat->item['id'], $fromDate, $toDate);
     list($pTotalRows, $pMonths, $pX, $pQtys, $itStat->periodMean) = ItemStatistics::getDataAndMean($itStat->item['id'], $periodFrom, $periodTo);
     $itStat->period = (substr($periodTo, 0, 4) - substr($periodFrom, 0, 4)) * 12;
     $itStat->period += substr($periodTo, 5, 2) - substr($periodFrom, 5, 2) + 1;
     $itStat->totalPeriod = (substr($toDate, 0, 4) - substr($fromDate, 0, 4)) * 12;
     $itStat->totalPeriod += substr($toDate, 5, 2) - substr($fromDate, 5, 2) + 1;
     if ($totalRows > 0) {
         list($itStat->periodStdDev, $itStat->periodMeanDev) = ItemStatistics::getDeviations($itStat->periodMean, $pQtys);
         list($itStat->stdDev, $itStat->meanDev) = ItemStatistics::getDeviations($itStat->mean, $Qtys);
         $itStat->periodMean += $itStat->periodMeanDev;
         $lr = ItemStatistics::linear_regression($x, $Qtys);
         $itStat->trend = $lr['b'] + $lr['m'] * $months;
         $itStat->trend2 = $lr['b'] + $lr['m'] * ($months + 1);
         $itStat->trend3 = $lr['b'] + $lr['m'] * ($months + 2);
         $itStat->trendMean = ($itStat->trend + $itStat->trend2 + $itStat->trend3) / 3;
         $itStat->trendStdDev = ItemStatistics::getLinearDeviation($lr, $Qtys);
         $itStat->trendStdDevX3 = $itStat->trendStdDev * 3;
     }
     return $itStat;
 }
 /**
  * Returns the data model based on the primary key given in the GET variable.
  * If the data model is not found, an HTTP exception will be raised.
  * @param integer the ID of the model to be loaded
  */
 public function loadModel($id)
 {
     $x = 3;
     $from = new DateTime();
     $to = new DateTime();
     $xPeriod = new DateTime();
     $from->sub(new DateInterval("P12M"));
     $to->sub(new DateInterval("P1M"));
     //        error_log("loadModel()->From".$from);
     $xPeriod->sub(new DateInterval("P" . $x . "M"));
     //Calcular el Ultimo dia del mes
     $result = strtotime($to->format('Y') . "-" . $to->format('m') . "-01");
     $result = date('Y-m-d', strtotime('-1 second', strtotime('+1 month', $result)));
     $model = ItemStatistics::find($id, $from->format('Y') . '-' . $from->format('m') . '-01', $result, $xPeriod->format('Y') . '-' . $xPeriod->format('m') . '-01');
     if ($model === null) {
         throw new CHttpException(404, 'The requested page does not exist.');
     }
     return $model;
 }