format("Y-m"); $report_months[$key] = $current_report_month->format("M Y"); $current_report_month->add($month_interval); $time = $current_report_month->format("U"); if($time > time()) break; } while(true); return $report_months; } // Object methods /** * @return Customer * @throws Exception */ public function getCustomer(): ?Customer { return Customer::get($this->customer_id); } /** * @return Test[] * @throws Exception */ public function getTests(): array { if(!is_null($this->tests)) return $this->tests; $nvp = []; $nvp["project_id"] = $this->id; $nvp["status"] = (new Constraint("MORE_THAN"))->setValues(Test::STATUS_CANCELLED); $this->tests = Test::query($nvp); return $this->tests; } /** * @return int * @throws Exception */ public function getTestCount(): int { if(is_null($this->tests)) $this->getTests(); return count($this->tests); } /** * @return int * @throws Exception */ public function getPassCount(): int { $count = 0; $tests = $this->getTests(); foreach($tests as $test) { if(!$test->isPass()) continue; ++$count; } return $count; } /** * @return int * @throws Exception */ public function getFailCount(): int { $count = 0; $tests = $this->getTests(); foreach($tests as $test) { if(!$test->isFail()) continue; ++$count; } return $count; } /** * @return int * @throws Exception */ public function getPendingCount(): int { $count = 0; $tests = $this->getTests(); foreach($tests as $test) { if(!$test->isPending()) continue; ++$count; } return $count; } /** * @return string * @throws Exception */ public function getPassRate(): string { $pass_count = $this->getPassCount(); $fail_count = $this->getFailCount(); $pass_fail_count = $pass_count + $fail_count; if($pass_fail_count == 0) return "0%"; return (number_format($pass_count / $pass_fail_count, 4) * 100) . "%"; } /** * @return Location[] * @throws Exception */ public function getLocations(): array { $tests = $this->getTests(); return Location::query(["id" => array_column($tests, "location_id")]); } /** * @return string */ public function getDisplayAgeRange(): string { if($this->min_age && $this->max_age) { return $this->min_age . " to " . $this->max_age; } else if($this->min_age) { return "At least " . $this->min_age; } else if($this->max_age) { return "Up to " . $this->max_age; } return "No age restriction"; } /** * @return string */ public function getNameHtml(): string { if($this->isAnytime()) { return htmlspecialchars($this->name); } $icon = $this->getTimeOfDayIcon(); $time_of_day = $this->getTimeOfDayText(); return htmlspecialchars($this->name) . " "; } /** * @return float * @throws Exception */ public function getCompletedPercentage(): float { $test_count = $this->test_count; $tests = $this->getTests(); $completed_tests = array_filter($tests, function($test) { return $test->status == Test::STATUS_COMPLETED; }); $completed_test_count = count($completed_tests); return floor(($completed_test_count / $test_count) * 100); } /** * @return Template * @throws Exception */ public function getTemplate(): ?Template { return Template::get($this->template_id); } /** * @return array */ public function getTemplateOverrides(): array { $overrides = json_decode($this->template_overrides_json) ?: []; return $overrides; } /** * @return string */ public function getTimeOfDayText(): string { switch($this->time_of_day) { default: case self::TOD_ANYTIME: return "Any"; case self::TOD_DAYTIME: return "Day"; case self::TOD_NIGHTTIME: return "Night"; } } /** * @return string */ public function getTimeOfDayIcon(): string { switch($this->time_of_day) { default: case self::TOD_ANYTIME: return "adjust"; case self::TOD_DAYTIME: return "sun"; case self::TOD_NIGHTTIME: return "moon"; } } /** * @return string */ public function getURL(): string { return "/account/projects/{$this->id}"; } /** * @return string */ public function getDirectory(): string { $directory = ROOT_DIR . "/files/projects/" . $this->id; if(!file_exists($directory)) mkdir($directory, 0777, true); return $directory; } /** * @return bool */ public function hasInvoicePDF(): bool { $directory = $this->getDirectory(); return file_exists($directory . "/invoice.pdf"); } /** * @return bool */ public function isAnytime(): bool { return $this->time_of_day == self::TOD_ANYTIME; } /** * @return bool */ public function isDaytime(): bool { return $this->time_of_day == self::TOD_DAYTIME; } /** * @return bool */ public function isNighttime(): bool { return $this->time_of_day == self::TOD_NIGHTTIME; } /** * @return bool */ public function isPastDeadline(): bool { $deadline_unix = Util::getUnixTimeFromUTC($this->dts_deadline); return time() > $deadline_unix; } /** * @return bool */ public function isFinished(): bool { return $this->dts_finished === null; } /** * @return bool * @throws Exception */ public function markFinished(): bool { if(!Core::$user) throw new Exception("Trying to mark project as finished while not logged in."); if(!Core::$user->isAdmin()) throw new Exception("Trying to mark project as finished with non-admin account."); $this->dts_finished = Util::getUTCDateTime(); $this->user_id_finished = Core::$user->id; return $this->commit(); } /** * @return bool * @throws Exception */ public function markUnfinished(): bool { $this->dts_finished = null; $this->user_id_finished = null; return $this->commit(); } /** * @return string * @throws Exception */ public function getDisplayReportMonth(): string { $report_month = "-"; if($this->dts_report) { $dt_report = new DateTime($this->dts_report . " UTC"); $report_month = $dt_report->format("M Y"); } return $report_month; } /** * @return array * @throws Exception */ public function getDataTableRow(): array { $tests = $this->getTests(); return [ "id" => (int) $this->id, "rowId" => "project-row-{$this->id}", "name" => $this->name, "testCount" => count($tests), "timeOfDay" => $this->getTimeOfDayText(), "unixStart" => Util::getUnixTimeFromUTC($this->dts_start), "unixEnd" => Util::getUnixTimeFromUTC($this->dts_deadline), "reportMonth" => $this->getDisplayReportMonth(), "unixReport" => $this->dts_report ? Util::getUnixTimeFromUTC($this->dts_report) : 0, ]; } /** * @return string * @throws Exception */ public function getTestResultsUrl(): string { $customer = $this->getCustomer(); $customer_slug = Util::slugWithId($customer->name, $customer->id); $project_slug = Util::slugWithId($this->name, $this->id); return "/account/test-results/{$customer_slug}/{$project_slug}"; } /** * @return Invoice * @throws Exception */ public function getCustomerInvoice(): Invoice { $invoice = new Invoice; $invoice->setReference($this->getInvoiceReference()); $test_line_items = $this->getTestInvoiceLineItems(); $field_line_items = $this->getFieldInvoiceLineItems(); $invoice->addLineItems($test_line_items); $invoice->addLineItems($field_line_items); return $invoice; } /** * @return string * @throws Exception */ private function getInvoiceReference(): string { $customer = $this->getCustomer(); return "P" . str_pad($this->id, 3, "0", STR_PAD_LEFT) . "/C" . str_pad($customer->id, 3, "0", STR_PAD_LEFT); } /** * @return InvoiceLineItem[] * @throws Exception */ private function getTestInvoiceLineItems(): array { /** * @var InvoiceLineItem[] $items */ $template = $this->getTemplate(); $default_customer_fee = $this->default_customer_fee; $tests = $this->getTests(); $items = []; foreach($tests as $test) { $fee = $test->customer_fee ?? $default_customer_fee; $key = "fee_{$fee}"; $item = $items[$key] ?? null; if(!$item) { $item = new InvoiceLineItem(__("%s Testing", $template->name)); $item->setPrice($fee); } $item->incrementQuantity(); $items[$key] = $item; } return array_values($items); } /** * @return InvoiceLineItem[] * @throws Exception */ private function getFieldInvoiceLineItems(): array { $template = $this->getTemplate(); $invoice_fields = $template->getCustomerInvoiceFields(); $tests = $this->getTests(); $items = []; foreach($tests as $test) { $values = $test->getValues(true); foreach($invoice_fields as $field) { $item = $items[$field->name] ?? (new InvoiceLineItem($field->label)); $item->addPrice((float) $values[$field->name] ?? 0); $items[$field->name] = $item; } } return $items; } }