| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013 |
- <?php /** @noinspection HtmlDeprecatedTag */
- /** @noinspection HtmlDeprecatedAttribute */
- use Carbon\CarbonImmutable;
- use Mpdf\Mpdf;
- /**
- * @property int $id
- * @property int $project_id
- * @property int $location_id
- * @property int $tester_id
- * @property int $previous_test_id
- * @property int $stripe_invoice_id
- * @property int $status
- * @property int $status_tester_invoice
- * @property int $payment_required
- * @property float $customer_fee
- * @property float $test_fee
- * @property string $test_time
- * @property float $test_travel_cost
- * @property string $test_travel_cost_description
- * @property string $shop_left
- * @property string $shop_right
- * @property string $attire
- * @property int $challenge_stage
- * @property string $challenge_description
- * @property string $challenger_name
- * @property string $challenger_description
- * @property string $challenger_gender
- * @property string $challenger_eye_contact
- * @property string $additional_notes
- * @property string $dts_due
- * @property string $dts_start
- * @property string $dts_end
- * @property string $dts_tester_invoice_generated
- * @property string $dts_tester_invoice_paid
- * @property int $verifier_id
- * @property string $verifier_notes
- * @property int $pass
- * @property int $retest
- * @property int $void
- * @property int $void_primary
- * @property int $void_secondary
- * @property string $void_notes
- * @property string $dts_created
- * @property string $dts_updated
- * @property string $dts_assigned
- * @property string $dts_rejected
- * @property string $dts_verified
- * @property string $dts_paid
- */
- class Test extends Model {
- protected static $table = "test";
- protected static $id_field = "id";
- protected static $fillable_fields = [
- "project_id", "location_id", "tester_id", "previous_test_id", "stripe_invoice_id",
- "status", "status_tester_invoice",
- "payment_required",
- "customer_fee", "test_fee", "test_time", "test_travel_cost", "test_travel_cost_description",
- "shop_left", "shop_right", "attire",
- "challenge_stage",
- "challenge_description", "challenger_name", "challenger_description", "challenger_gender", "challenger_eye_contact",
- "additional_notes",
- "dts_due", "dts_start", "dts_end", "dts_tester_invoice_generated", "dts_tester_invoice_paid",
- "verifier_id", "verifier_notes",
- "pass", "retest",
- "void", "void_notes",
- "void_primary",
- "void_secondary",
- "dts_created", "dts_updated",
- "dts_assigned", "dts_rejected", "dts_verified", "dts_paid",
- ];
- public const TEST_FIELDS = ["shop_left", "shop_right", "attire", "challenge_stage", "challenge_description", "challenger_name", "challenger_description", "challenger_gender", "challenger_eye_contact", "additional_notes"];
- // Constants
- public const STATUS_CANCELLED = 0;
- public const STATUS_UNASSIGNED = 1; // Location needs a tester
- public const STATUS_ASSIGNED = 2; // Tester assigned
- public const STATUS_CONFIRMED = 3; // Tester has said they can do it
- public const STATUS_STARTED = 4;
- public const STATUS_PENDING_VERIFICATION = 8;
- public const STATUS_COMPLETED = 16;
- public const STATUS_TESTER_INVOICE_NONE = 0;
- public const STATUS_TESTER_INVOICE_PENDING = 100;
- public const STATUS_TESTER_INVOICE_PAID = 200;
- public const STATUSES = [
- 0 => "Cancelled",
- 1 => "Unassigned",
- 2 => "Assigned",
- 3 => "Confirmed",
- 4 => "Started",
- 8 => "Pending Verification",
- 16 => "Completed",
- ];
- public const STATUSES_TESTER_INVOICE = [
- Test::STATUS_TESTER_INVOICE_NONE,
- Test::STATUS_TESTER_INVOICE_PENDING,
- Test::STATUS_TESTER_INVOICE_PAID
- ];
- public const STAGES = [
- 0 => "",
- 1 => "Challenge on entry",
- 2 => "Challenge while browsing",
- 3 => "Challenge during play",
- 4 => "Challenge on exit",
- 5 => "No challenge",
- ];
- // Static methods
- /**
- * @param int $status
- * @return string
- */
- public static function getStatusTesterInvoiceText(int $status): string {
- switch($status) {
- case self::STATUS_TESTER_INVOICE_NONE:
- return __("No Invoice Generated");
- case self::STATUS_TESTER_INVOICE_PENDING:
- return __("Pending Payment");
- case self::STATUS_TESTER_INVOICE_PAID:
- return __("Paid");
- }
- return __("Unknown");
- }
- /**
- * @param int $status
- * @return string
- */
- public static function getStatusTesterInvoiceClass(int $status): string {
- switch($status) {
- case self::STATUS_TESTER_INVOICE_NONE:
- return "secondary";
- case self::STATUS_TESTER_INVOICE_PENDING:
- return "warning";
- case self::STATUS_TESTER_INVOICE_PAID:
- return "success";
- }
- return "";
- }
- /**
- * @param string $processing_file
- * @param array $tests
- * @throws Exception
- */
- public static function exportCSV(string $processing_file, array $tests = []): void {
- // Preload
- Project::load(array_column($tests, "project_id"));
- Location::load(array_column($tests, "location_id"));
- User::load(array_column($tests, "tester_id"));
- // Get processing file
- $processing_file_path = ROOT_DIR . "/export_processing/{$processing_file}";
- if(!file_exists($processing_file_path)) throw new Exception("Processing file not found.");
- // Output
- HTTP::setDownloadHeaders("export.csv");
- $handle = fopen("php://output", "wb");
- /** @noinspection PhpIncludeInspection */
- require $processing_file_path;
- fclose($handle);
- }
- // Object methods
- /**
- * @param string $message
- * @return bool
- * @throws Exception
- */
- public function sendAssignEmail(string $message): bool {
- $tester = $this->getTester();
- $location = $this->getLocation();
- $project = $this->getProject();
- // Content
- $name = htmlspecialchars($tester->first_name);
- $message = htmlspecialchars($message);
- $base_url = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/account/tests/" . $this->id);
- $address = htmlspecialchars($location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]));
- $date_due = date("j M Y", strtotime($this->dts_due));
- $admin_first_name = htmlspecialchars(Core::$user->first_name);
- $admin_phone = htmlspecialchars(Core::$user->phone);
- // Build
- $builder = new EmailBuilder;
- $builder->addParagraph("<b>" . sprintf(__("Hi %s"), $name) . "</b>");
- $builder->addLeadParagraph(__("Congrats, you have been allocated a test!"));
- if($project->isNighttime()) {
- $builder->addParagraph("<b><font color=\"red\">" . __("Evening Test to be done between 7PM and midnight.") . "</font></b>");
- }
- if($message) {
- $builder->addParagraph($message);
- }
- $builder->addParagraph(sprintf(__("We need you to confirm if you are able to do the test. Please check the details below and then confirm by pressing the %sAccept%s or %sDecline%s buttons and once you tap %sAccept%s, the test will be allocated to you."),
- '<b>', '</b>',
- '<b>', '</b>',
- '<b>', '</b>'
- ));
- $builder->addAcceptDeclineButtons($base_url);
- $builder->addParagraph(sprintf(__("The site address is: %s"), "<b>{$address}</b>"));
- $builder->addParagraph(sprintf(__("The date that need to complete and submit the results by is: %s"), "<font color=\"red\"><b>$date_due</b></font>"));
- $builder->addParagraph(sprintf(__("For methodology and further notes please check the %sUseful Documents%s section on the %sAccount%s page."),
- '<b>', '</b>',
- '<b>', '</b>'
- ));
- $builder->addParagraph(sprintf(__("If you agree to conduct the test, log in and then go to %sTests%s where you will be able to see the tests available to you. Please tap the %sBegin%s button and follow the instructions that is shown."),
- '<b>', '</b>',
- '<b>', '</b>'
- ));
- $builder->addParagraph(sprintf(__("If you have any questions please send them to %s or call %s on %s."),
- "<a href=\"mailto:tests@check-policy.com\">tests@check-policy.com</a>",
- "<b>$admin_first_name</b>",
- "<b>$admin_phone</b>"
- ));
- $builder->addParagraph(sprintf(__("If you are unable to submit the results via the web-based app please contact us and we can send you an offline form to complete. Please accept the test by tapping the %sAccept%s button and email us back saying you would like an offline form."),
- '<b>', '</b>'
- ));
- $content = $builder->build();
- // Send
- $mail = new Email;
- $mail->setHtmlContent($content);
- $mail->setFrom(EMAIL_CONTACT, EMAIL_FROM_NAME);
- $mail->addAddress($tester->email, $tester->getDisplayName());
- $mail->setSubject(__("You have been allocated a test"));
- $mail->send();
- return true;
- }
- /**
- * @return bool
- * @throws Exception
- */
- public function sendClaimedEmail(): bool {
- $tester = $this->getTester();
- $location = $this->getLocation();
- $project = $this->getProject();
- // Content
- $name = htmlspecialchars($tester->first_name);
- $address = htmlspecialchars($location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]));
- $date_due = date("j M Y", strtotime($this->dts_due));
- // Build
- $builder = new EmailBuilder;
- $builder->addParagraph("<b>" . sprintf(__("Hi %s"), $name) . "</b>");
- $builder->addLeadParagraph(__("Congrats, you have claimed a test!"));
- if($project->isNighttime()) {
- $builder->addParagraph("<b><font color=\"red\">" . __("Evening Test to be done between 7PM and midnight.") . "</font></b>");
- }
- $builder->addParagraph(sprintf(__("The site address is: %s"), "<b>$address</b>"));
- $builder->addParagraph(sprintf(__("The date that need to complete and submit the results by is: %s"), "<font color=\"red\"><b>$date_due</b></font>"));
- $builder->addParagraph(sprintf(__("For methodology and further notes please check the %sUseful Documents%s section on the %sAccount%s page."),
- '<b>', '</b>',
- '<b>', '</b>'
- ));
- $builder->addParagraph(sprintf(__("Log in and then go to %sTests%s where you will be able to see the tests available to you. Please tap the %sBegin%s button and follow the instructions that is shown."),
- '<b>', '</b>',
- '<b>', '</b>'
- ));
- $builder->addParagraph(sprintf(__("If you have any questions please send them to %s."),
- "<a href=\"mailto:tests@check-policy.com\">tests@check-policy.com</a>"
- ));
- $builder->addParagraph(__("If you are unable to submit the results via the web-based app please contact us and we can send you an offline form to complete."));
- $content = $builder->build();
- // Send
- $mail = new Email;
- $mail->setHtmlContent($content);
- $mail->setFrom(EMAIL_CONTACT, EMAIL_FROM_NAME);
- $mail->addAddress($tester->email, $tester->getDisplayName());
- $mail->setSubject(__("You have claimed a test"));
- $mail->send();
- return true;
- }
- /**
- * @return bool
- * @throws Exception
- */
- public function sendDeclineEmail(): bool {
- $tester = $this->getTester();
- $location = $this->getLocation();
- $project = $this->getProject();
- // Content
- $name = htmlspecialchars($tester->getDisplayName());
- $address = htmlspecialchars($location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]));
- $project_name = htmlspecialchars($project->name);
- $project_url = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/account/projects/" . $project->id);
- $internal_id = "N/A";
- if($location->internal_id) $internal_id = htmlspecialchars($location->internal_id);
- // Build
- $builder = new EmailBuilder;
- $builder->addParagraph("<b>Hi,</b>");
- $builder->addLeadParagraph("A tester has declined a test.");
- $builder->addParagraph("<b>Project:</b><br />$project_name");
- $builder->addParagraph("<b>Site:</b><br />$address<br />ID: $location->id | Internal ID: $internal_id");
- $builder->addParagraph("<b>Who:</b><br />$name<br />ID: $tester->id");
- $builder->addParagraph("<a href=\"$project_url\">Click here</a> to manage the project and reassign the test.");
- $content = $builder->build();
- // Send
- $mail = new Email;
- $mail->setHtmlContent($content);
- $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
- $mail->addAddress(EMAIL_CONTACT, APP_NAME . " Contact");
- $mail->setSubject("Test declined: $project->name / $location->name");
- $mail->send();
- return true;
- }
- /**
- * @param string $to_address
- * @param bool $include_previous_test
- * @return bool
- * @throws Exception
- */
- public function sendResultEmail(string $to_address, bool $include_previous_test): bool {
- $location = $this->getLocation();
- $project = $this->getProject();
- $template = $project->getTemplate();
- $subject_format_file_path = $template->getProcessingFilePath("result_email_subject_format.txt");
- $subject_format = file_get_contents($subject_format_file_path);
- // Content
- $pass_text = strtoupper($this->getPassText());
- $stage = $this->challenge_stage;
- $location_parts = [$location->internal_id, $location->name];
- $location_string = implode(" : ", array_filter($location_parts));
- $unix_start = strtotime($this->dts_start . " UTC");
- $date_time = date("d/m/Y H:i", $unix_start);
- // Subject
- $search_replace = [
- "{pass}" => $pass_text,
- "{stage}" => $stage,
- "{location}" => $location_string,
- "{date-time}" => $date_time,
- ];
- $subject = str_replace(
- array_keys($search_replace),
- array_values($search_replace),
- $subject_format
- );
- // Attachments
- $pdf_path = $this->getDirectory() . "/pdf.pdf";
- $pdf_name = implode(" - ", array_filter([$location->internal_id, date("jS M Y")])) . ".pdf";
- // Lead paragraph
- $lead_paragraph = "";
- if($this->pass) {
- $lead_paragraph .= "Congratulations!<br />";
- }
- $lead_paragraph .= "A test has been completed at your location.";
- // Build
- $builder = new EmailBuilder;
- $builder->addLeadParagraph($lead_paragraph);
- $builder->addParagraph("Please see attached PDF and do not hesitate to contact us if you have any questions.");
- // Previous test
- $previous_pdf_path = $previous_pdf_name = null;
- if($include_previous_test) {
- $previous_test = $this->getPreviousTest();
- if($previous_test && $previous_test->hasPDF()) {
- $builder->addParagraph("The previous test result has also been attached for your reference.");
- $previous_dt = new DateTime($previous_test->dts_start, new DateTimeZone("UTC"));
- $previous_pdf_path = $previous_test->getPDFPath();
- $previous_pdf_name = implode(" - ", array_filter([$location->internal_id, $previous_dt->format("jS M Y")])) . ".pdf";
- }
- }
- $content = $builder->build();
- // Send
- $mail = new Email();
- $mail->setHtmlContent($content);
- $mail->setFrom(EMAIL_PDF_FROM_ADDRESS, EMAIL_FROM_NAME);
- $mail->addAddress($to_address);
- $mail->addAttachment($pdf_path, $pdf_name);
- if($previous_pdf_name) {
- $mail->addAttachment($previous_pdf_path, $previous_pdf_name);
- }
- $mail->setSubject($subject);
- $mail->send();
- return true;
- }
- /**
- * @return bool
- * @throws Exception
- */
- public function decline(): bool {
- $this->sendDeclineEmail();
- $user_id = $this->tester_id;
- $location_id = $this->location_id;
- $this->status = self::STATUS_UNASSIGNED;
- $this->tester_id = null;
- $this->dts_due = null;
- $this->dts_assigned = null;
- $this->dts_rejected = Util::getDateTime();
- $this->dts_updated = Util::getDateTime();
- $this->commit();
- $reject = UserLocationReject::get($user_id, $location_id);
- if(!$reject) {
- $reject = new UserLocationReject();
- $reject->user_id = $user_id;
- $reject->location_id = $location_id;
- $reject->reject_count = 0;
- }
- $reject->reject_count++;
- $reject->dts_last_rejected = DB::dts();
- $reject->commit();
- return true;
- }
- /**
- * @return string
- */
- public function getChallengeStageText(): string {
- if(is_null($this->challenge_stage)) return "";
- return self::STAGES[$this->challenge_stage];
- }
- /**
- * @return string
- */
- public function getStatusText(): string {
- return self::STATUSES[$this->status];
- }
- /**
- * @return string
- */
- public function getStatusClass(): string {
- switch($this->status) {
- case self::STATUS_CANCELLED:
- return "secondary";
- case self::STATUS_UNASSIGNED:
- return "danger";
- case self::STATUS_ASSIGNED:
- case self::STATUS_CONFIRMED:
- case self::STATUS_STARTED:
- return "warning";
- case self::STATUS_PENDING_VERIFICATION:
- return "primary";
- case self::STATUS_COMPLETED:
- return "success";
- }
- return "";
- }
- /**
- * @return Project
- * @throws Exception
- */
- public function getProject(): Project {
- return Project::get($this->project_id);
- }
- /**
- * @return Location
- * @throws Exception
- */
- public function getLocation(): Location {
- return Location::get($this->location_id);
- }
- /**
- * @return User
- * @throws Exception
- */
- public function getTester(): ?User {
- return User::get($this->tester_id);
- }
- /**
- * @return User
- * @throws Exception
- */
- public function getVerifier(): ?User {
- return User::get($this->verifier_id);
- }
- /**
- * @param bool $as_assoc_array
- * @return TestValue[]
- * @throws Exception
- */
- public function getValues(bool $as_assoc_array = false): array {
- $rows = TestValue::query(["test_id" => $this->id]);
- if($as_assoc_array) {
- $values = [];
- foreach($rows as $row) {
- $values[$row->name] = $row->value;
- }
- }
- else {
- $values = $rows;
- }
- return $values;
- }
- /**
- * @return string
- */
- public function getDirectory(): string {
- $directory = ROOT_DIR . "/files/tests/" . $this->id;
- if(!file_exists($directory)) mkdir($directory, 0777, true);
- return $directory;
- }
- /**
- * @return bool|float|int
- */
- public function getDuration() {
- if($this->dts_end === null) return false;
- return Util::getDurationFromDTS($this->dts_start, $this->dts_end);
- }
- /**
- * @return string
- */
- public function getDisplayDuration(): string {
- $secs = $this->getDuration();
- $mins = floor($secs / 60);
- $secs %= 60;
- return "$mins min. $secs sec.";
- }
- /**
- * @return string Bootstrap class name
- */
- public function getPassClass(): string {
- if($this->isVoid()) return "muted";
- if($this->pass === null) return "secondary";
- return $this->pass ? "success" : "danger";
- }
- /**
- * @return string FA icon name without prefix
- */
- public function getPassIcon(): string {
- if($this->isVoid()) return "minus-circle";
- if($this->pass === null) return "clock";
- return $this->pass ? "check-circle" : "times-circle";
- }
- /**
- * @return string Pending, Pass or Fail
- */
- public function getPassText(): string {
- if($this->isVoid() || $this->isPrimaryVoid()) return __("Void");
- if($this->pass === null) return __("Pending");
- return $this->pass ? __("Pass") : __("Fail");
- }
- /** @noinspection PhpUnusedLocalVariableInspection */
- public function getMPDF(): Mpdf {
- $id = $this->id;
- $output = false;
- require ROOT_DIR . "/controllers/test_pdf.php";
- /** @var Mpdf $mpdf */
- return $mpdf;
- }
- /**
- * @param User $tester
- * @return bool
- * @throws Exception
- */
- public function assignTester(User $tester): bool {
- $this->tester_id = $tester->id;
- $this->dts_assigned = Util::getDateTime();
- $this->dts_rejected = null;
- // TODO: Notify tester
- return $this->commit();
- }
- /**
- * @return Test|null
- * @throws Exception
- */
- public function getPreviousTest(): ?Test {
- if($this->previous_test_id) {
- return self::get($this->previous_test_id);
- }
- if($this->dts_start === null) {
- return null;
- }
- $nvp = [
- "dts_start" => Constraint::lessThan($this->dts_start),
- "location_id" => $this->location_id,
- "status" => self::STATUS_COMPLETED,
- ];
- $tests = self::query($nvp, [
- "sort" => "dts_start",
- "order" => "desc",
- ]);
- return $tests ? $tests[0] : null;
- }
- /**
- * @return Test|null
- * @throws Exception
- */
- public function getNextTest(): ?Test {
- $test = self::queryOne(["previous_test_id" => $this->id]);
- if($test) {
- return $test;
- }
- if($this->dts_start === null) {
- return null;
- }
- $nvp = [
- "dts_start" => Constraint::moreThan($this->dts_start),
- "location_id" => $this->location_id,
- ];
- $tests = self::query($nvp, [
- "sort" => "dts_start",
- "order" => "asc",
- ]);
- return $tests ? $tests[0] : null;
- }
- /**
- * @return bool
- * @throws Exception
- */
- public function unassignTester(): bool {
- $this->status = self::STATUS_UNASSIGNED;
- $this->tester_id = null;
- $this->dts_due = null;
- $this->dts_assigned = null;
- $this->dts_rejected = null;
- return $this->commit();
- }
- /**
- * @return bool
- */
- public function isPass(): bool {
- return (int) $this->pass === 1;
- }
- /**
- * @return bool
- */
- public function isPending(): bool {
- return $this->pass === null;
- }
- /**
- * @return bool
- */
- public function isFail(): bool {
- return (int) $this->pass === 0;
- }
- /**
- * @return bool
- */
- public function isCompleted(): bool {
- return $this->getStatus() === self::STATUS_COMPLETED;
- }
- public function isDone(): bool {
- return in_array($this->getStatus(), [self::STATUS_CANCELLED, self::STATUS_COMPLETED]);
- }
- /**
- * @return bool
- */
- public function isPendingVerification(): bool {
- return $this->getStatus() === self::STATUS_PENDING_VERIFICATION;
- }
- public function getPDFPath(): string {
- $directory = $this->getDirectory();
- return sprintf("%s/pdf.pdf", $directory);
- }
- /**
- * @return bool
- */
- public function hasPDF(): bool {
- return file_exists($this->getPDFPath());
- }
- /**
- * @return string
- */
- public function getPDFUrl(): string {
- return "/account/tests/{$this->id}/files/pdf";
- }
- public function getTesterInvoicePDFPath(): string {
- return sprintf("%s/tester_invoice.pdf", $this->getDirectory());
- }
- /**
- * @return bool
- */
- public function hasTesterInvoicePDF(): bool {
- return file_exists($this->getTesterInvoicePDFPath());
- }
- public function getTravelCostsPhotoPath(): string {
- return sprintf("%s/travel_costs.jpg", $this->getDirectory());
- }
- public function hasTravelCostsPhoto(): bool {
- return file_exists($this->getTravelCostsPhotoPath());
- }
- /**
- * @return bool
- * @throws Exception
- */
- public function markTesterInvoicePaid(): bool {
- if($this->getTesterInvoiceStatus() === self::STATUS_TESTER_INVOICE_PENDING) {
- $this->status_tester_invoice = self::STATUS_TESTER_INVOICE_PAID;
- $this->dts_tester_invoice_paid = Util::getDateTime();
- return $this->commit();
- }
- return false;
- }
- /**
- * @return bool
- * @throws Exception
- */
- public function rejectTesterInvoice(): bool {
- if($this->getTesterInvoiceStatus() === self::STATUS_TESTER_INVOICE_PENDING) {
- $this->status_tester_invoice = self::STATUS_TESTER_INVOICE_NONE;
- $this->dts_tester_invoice_generated = null;
- if($this->hasTesterInvoicePDF()) {
- unlink($this->getTesterInvoicePDFPath());
- }
- if($this->hasTravelCostsPhoto()) {
- unlink($this->getTravelCostsPhotoPath());
- }
- return $this->commit();
- }
- return false;
- }
- /**
- * @return string
- * @throws Exception
- */
- public function getTesterInvoiceReference(): string {
- $location = $this->getLocation();
- $tester = $this->getTester();
- return
- "T" . str_pad($this->id, 3, "0", STR_PAD_LEFT) .
- "/L" . str_pad($location->id, 3, "0", STR_PAD_LEFT) .
- "/U" . str_pad($tester->id, 3, "0", STR_PAD_LEFT);
- }
- /**
- * @return float
- * @throws Exception
- */
- public function getTestFee(): float {
- return $this->test_fee !== null ? (float) $this->test_fee : (float) $this->getProject()->default_tester_fee;
- }
- public function getTravelCosts(): float {
- return (float) $this->test_travel_cost;
- }
- /**
- * @return float
- * @throws Exception
- */
- public function getInvoiceTotal(): float {
- $template = $this->getProject()->getTemplate();
- $values = $this->getValues(true);
- // Base cost
- $fee = $this->getTestFee();
- $travel_cost = $this->getTravelCosts();
- $invoice_total = $fee + $travel_cost;
- // Additional tester invoice fields
- $tester_invoice_fields = $template->getTesterInvoiceFields();
- foreach($tester_invoice_fields as $field) {
- $invoice_total += (float) ($values[$field->name] ?? 0);
- }
- return $invoice_total;
- }
- public function isVoid(): bool {
- return (bool) $this->void;
- }
- public function isPrimaryVoid(): bool {
- return (bool) $this->void_primary;
- }
- public function getStatus(): int {
- return $this->status;
- }
- public function getTesterId(): int {
- return $this->tester_id;
- }
- /**
- * @return bool Whether the tester has accepted the test
- */
- public function isConfirmed(): bool {
- return $this->getStatus() === self::STATUS_CONFIRMED;
- }
- public function hasStarted(): bool {
- return $this->getStatus() === self::STATUS_STARTED;
- }
- public function isAssigned(): bool {
- return $this->getStatus() === self::STATUS_ASSIGNED;
- }
- /**
- * @return array
- * @throws Exception
- */
- public function getFieldsForVerifyForm(): array {
- $json = json_decode($this->getProject()->getTemplate()->getJSON());
- return array_merge($json->beforeTest, [(object) ["type" => "hr"]], $json->duringTest);
- }
- public function getTesterInvoiceStatus(): int {
- return $this->status_tester_invoice;
- }
- public function isCancelled(): bool {
- return $this->getStatus() === self::STATUS_CANCELLED;
- }
- public function hasDueDateTime(): bool {
- return $this->dts_due !== null;
- }
- public function getTimeDue(): int {
- if(!$this->dts_due) {
- return 0;
- }
- return $this->getDateTimeDue()->getTimestamp();
- }
- public function hasNotStarted(): bool {
- return $this->status < self::STATUS_STARTED;
- }
- public function isOverdue(): bool {
- return time() > $this->getTimeDue();
- }
- public function getDateTimeDue(): ?CarbonImmutable {
- return Util::getDateTimeFromUtcString($this->dts_due);
- }
- public function getDateTimeStart(): ?CarbonImmutable {
- return Util::getDateTimeFromUtcString($this->dts_start);
- }
- public function getDateTimeEnd(): ?CarbonImmutable {
- return Util::getDateTimeFromUtcString($this->dts_end);
- }
- public function getVerifierNotesHtml(): string {
- return nl2br(htmlspecialchars($this->verifier_notes));
- }
- /**
- * @return float
- * @throws Exception
- */
- public function getTesterInvoiceCosts(): float {
- return $this->getProject()->getTemplate()->getTesterInvoiceCosts($this->getValues(true));
- }
- public function getDateTimeRejected(): ?CarbonImmutable {
- if(!$this->dts_rejected) {
- return null;
- }
- return CarbonImmutable::parse($this->dts_rejected, "UTC");
- }
- public function renderTestStatus(User $user): void {
- ?>
- <a href="/account/tests/<?=$this->id?>">
- <?php
-
- if($this->isPass()) {
- ?>
- <i class="fa fa-fw fa-check-circle text-success"></i> Passed on <?=$user->formatDate($this->getDateTimeStart())?>
- <?php
- }
- elseif($this->isVoid()) {
- ?>
- <i class="fa fa-fw fa-minus-circle text-secondary"></i> Void on <?=$user->formatDate($this->getDateTimeStart())?>
- <?php
- }
- elseif($this->isFail()) {
- ?>
- <i class="fa fa-fw fa-times-circle text-danger"></i> Failed on <?=$user->formatDate($this->getDateTimeStart())?>
- <?php
- }
- else {
- ?>
- <i class="fa fa-fw fa-question-circle text-warning"></i> Result Pending (Test <?=htmlspecialchars($this->getStatusText())?>)
- <?php
- }
- ?>
- </a>
- <?php
- }
- /**
- * @return string
- * @throws Exception
- */
- public function getCustomerFee(): string {
- $project = $this->getProject();
- if($this->customer_fee) {
- $fee = $this->customer_fee;
- }
- elseif($this->retest) {
- $fee = $project->getDefaultRetestFee();
- }
- else {
- $fee = $project->default_customer_fee;
- }
- return $fee;
- }
- }
|