public function monthIndexes() { // Get Work Formula $formula = PredefinedFormulas::findById($this->work->formulaId); // Get Reference Date $startDate = new \DateTime(); $startDate->setTimestamp($this->work->contracts[0]['bidDate']); $startDate->sub(new \DateInterval('P1M')); $startDate->modify('first day of this month'); // Get List Of Months $this->months = array_merge([Date::getMonthArray($startDate->getTimestamp())], Date::getMonthsArray($this->work->contracts[0]['consignmentDate'], $this->work->getEndDate())); // Calculate Months foreach ($this->months as $i => &$monthInfo) { // Get Index Values for the Month $monthValues = IndexValues::findFirst(['conditions' => ['month' => $monthInfo['timestamp'], 'location' => (int) $this->work->location]]); // If doesn't exist = Get Last Month Possible if (!$monthValues) { $this->isDefinitive = false; $monthValues = IndexValues::findFirst(['conditions' => ['location' => $this->work->location], 'sort' => ['month' => -1]]); } // Constant $monthInfo['constant'] = $formula->constant; // Indexes And Coefs $monthInfo['indexes'] = []; $monthInfo['coefs'] = []; foreach ($formula->coefs as $index => $value) { $monthInfo['indexes'][$index] = $monthValues->indexes[$index]; // Base Month ? if ($i == 0) { $monthInfo['coefs'][$index] = $value; continue; } // Next Months $baseCoef = $this->months[0]['coefs'][$index]; $baseIndex = $this->months[0]['indexes'][$index]; $monthInfo['coefs'][$index] = $this->round($monthValues->indexes[$index] * $baseCoef / $baseIndex); } $monthInfo['coef'] = $this->round(array_sum($monthInfo['coefs']) + $monthInfo['constant']); } unset($monthInfo); return $this->months; }