customer_id); } /** * @return Test[] */ public function getTests() { 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; } public function getTestCount() { if(is_null($this->tests)) $this->getTests(); return count($this->tests); } public function getPassCount() { $count = 0; $tests = $this->getTests(); foreach($tests as $test) { if(!$test->isPass()) continue; ++$count; } return $count; } public function getFailCount() { $count = 0; $tests = $this->getTests(); foreach($tests as $test) { if(!$test->isFail()) continue; ++$count; } return $count; } public function getPendingCount() { $count = 0; $tests = $this->getTests(); foreach($tests as $test) { if(!$test->isPending()) continue; ++$count; } return $count; } 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) . "%"; } public function getLocations() { $tests = $this->getTests(); return Location::query(["id" => array_column($tests, "location_id")]); } public function getDisplayAgeRange() { 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"; } public function getNameHtml(): string { if($this->isAnytime()) { return htmlspecialchars($this->name); } $icon = $this->getTimeOfDayIcon(); $time_of_day = $this->getTimeOfDayText(); return htmlspecialchars($this->name) . " "; } public function getCompletedPercentage() { $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 */ public function getTemplate() { return Template::get($this->template_id); } public function getTemplateOverrides() { $overrides = json_decode($this->template_overrides_json) ?: []; return $overrides; } public function getTimeOfDayText() { switch($this->time_of_day) { case self::TOD_ANYTIME: return "Any"; case self::TOD_DAYTIME: return "Day"; case self::TOD_NIGHTTIME: return "Night"; } } public function getTimeOfDayIcon() { switch($this->time_of_day) { case self::TOD_ANYTIME: return "adjust"; case self::TOD_DAYTIME: return "sun"; case self::TOD_NIGHTTIME: return "moon"; } } public function getURL() { return "/account/projects/{$this->id}"; } public function getDirectory() { $directory = ROOT_DIR . "/files/projects/" . $this->id; if(!file_exists($directory)) mkdir($directory, 0777, true); return $directory; } public function hasInvoicePDF() { $directory = $this->getDirectory(); return file_exists($directory . "/invoice.pdf"); } public function isAnytime() { return $this->time_of_day == self::TOD_ANYTIME; } public function isDaytime() { return $this->time_of_day == self::TOD_DAYTIME; } public function isNighttime() { return $this->time_of_day == self::TOD_NIGHTTIME; } public function isPastDeadline() { $deadline_unix = Util::getUnixTimeFromUTC($this->dts_deadline); return time() > $deadline_unix; } public function isFinished() { return $this->dts_finished === null; } public function markFinished() { 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(); } public function markUnfinished() { $this->dts_finished = null; $this->user_id_finished = null; return $this->commit(); } public function getDisplayReportMonth() { $report_month = "-"; if($this->dts_report) { $dt_report = new DateTime($this->dts_report . " UTC"); $report_month = $dt_report->format("M Y"); } return $report_month; } public function getDataTableRow() { $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_end), "reportMonth" => $this->getDisplayReportMonth(), "unixReport" => $this->dts_report ? Util::getUnixTimeFromUTC($this->dts_report) : 0, ]; } 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}"; } 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; } 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); } 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; $item = $items[$fee] ?? null; if(!$item) { $item = new InvoiceLineItem(__("%s Testing", $template->name)); $item->setPrice($fee); } $item->incrementQuantity(); $items[$fee] = $item; } return array_values($items); } 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; } }