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; }
public function validation() { // Verify Name $this->requireField("name"); if (strlen($this->name) < 1) { throw new Exceptions\InvalidField("name"); } // Verify Location $this->requireField("location"); $this->location = (int) $this->location; if ($this->location !== 0 && $this->location !== 1) { throw new Exceptions\InvalidField("location"); } // Verify Author $this->requireField("authorId"); $author = UserVault\Users::findById($this->authorId); if (!isset($author) || !$author) { throw new Exceptions\InvalidField("authorId"); } // Verify FormulaId if ($this->formulaId) { $formula = PredefinedFormulas::findById($this->formulaId); if (!isset($formula) || !$formula) { throw new Exceptions\InvalidField("formulaId"); } } // Verify Contracts $autoNames = []; foreach ($this->contracts as $i => &$contractInfo) { $contract = new Works\Contracts(); $contract->fromArray($contractInfo); if ($i == 0) { $contract->validationInitial(); } else { $contract->validation(); } // ConsignmentDate >= (BidDate || Initial->BidDate) $bidDate = $contract->bidDate ? $contract->bidDate : $this->contracts[0]["bidDate"]; if ($contract->consignmentDate < $bidDate) { throw new Exceptions\InvalidField("consignmentDate"); } // Verify unique name of Autos foreach ($contract->autos as $auto) { if (in_array($auto["name"], $autoNames)) { throw new Exceptions\NotUniqueField("autoName"); } $autoNames[] = $auto["name"]; } $contractInfo = $contract->toArray(); } return true; }