test.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. <?php /** @noinspection HtmlDeprecatedTag */
  2. /** @noinspection HtmlDeprecatedAttribute */
  3. use Carbon\CarbonImmutable;
  4. use Mpdf\Mpdf;
  5. /**
  6. * @property int $id
  7. * @property int $project_id
  8. * @property int $location_id
  9. * @property int $tester_id
  10. * @property int $status
  11. * @property int $status_tester_invoice
  12. * @property float $customer_fee
  13. * @property float $test_fee
  14. * @property string $test_time
  15. * @property float $test_travel_cost
  16. * @property string $test_travel_cost_description
  17. * @property string $shop_left
  18. * @property string $shop_right
  19. * @property string $attire
  20. * @property int $challenge_stage
  21. * @property string $challenge_description
  22. * @property string $challenger_name
  23. * @property string $challenger_description
  24. * @property string $challenger_gender
  25. * @property string $challenger_eye_contact
  26. * @property string $additional_notes
  27. * @property string $dts_due
  28. * @property string $dts_start
  29. * @property string $dts_end
  30. * @property string $dts_tester_invoice_generated
  31. * @property string $dts_tester_invoice_paid
  32. * @property int $verifier_id
  33. * @property string $verifier_notes
  34. * @property bool $pass
  35. * @property bool $retest
  36. * @property bool $void
  37. * @property string $void_notes
  38. * @property string $dts_created
  39. * @property string $dts_updated
  40. * @property string $dts_assigned
  41. * @property string $dts_rejected
  42. * @property string $dts_verified
  43. */
  44. class Test extends Model {
  45. protected static $table = "test";
  46. protected static $id_field = "id";
  47. protected static $fillable_fields = [
  48. "project_id", "location_id", "tester_id",
  49. "status", "status_tester_invoice",
  50. "customer_fee", "test_fee", "test_time", "test_travel_cost", "test_travel_cost_description",
  51. "shop_left", "shop_right", "attire",
  52. "challenge_stage",
  53. "challenge_description", "challenger_name", "challenger_description", "challenger_gender", "challenger_eye_contact",
  54. "additional_notes",
  55. "dts_due", "dts_start", "dts_end", "dts_tester_invoice_generated", "dts_tester_invoice_paid",
  56. "verifier_id", "verifier_notes",
  57. "pass", "retest", "void", "void_notes",
  58. "dts_created", "dts_updated",
  59. "dts_assigned", "dts_rejected", "dts_verified"
  60. ];
  61. public const TEST_FIELDS = ["shop_left", "shop_right", "attire", "challenge_stage", "challenge_description", "challenger_name", "challenger_description", "challenger_gender", "challenger_eye_contact", "additional_notes"];
  62. // Constants
  63. public const STATUS_CANCELLED = 0;
  64. public const STATUS_UNASSIGNED = 1; // Location needs a tester
  65. public const STATUS_ASSIGNED = 2; // Tester assigned
  66. public const STATUS_CONFIRMED = 3; // Tester has said they can do it
  67. public const STATUS_STARTED = 4;
  68. public const STATUS_PENDING_VERIFICATION = 8;
  69. public const STATUS_COMPLETED = 16;
  70. public const STATUS_TESTER_INVOICE_NONE = 0;
  71. public const STATUS_TESTER_INVOICE_PENDING = 100;
  72. public const STATUS_TESTER_INVOICE_PAID = 200;
  73. public const STATUSES = [
  74. 0 => "Cancelled",
  75. 1 => "Unassigned",
  76. 2 => "Assigned",
  77. 3 => "Confirmed",
  78. 4 => "Started",
  79. 8 => "Pending Verification",
  80. 16 => "Completed",
  81. ];
  82. public const STATUSES_TESTER_INVOICE = [
  83. Test::STATUS_TESTER_INVOICE_NONE,
  84. Test::STATUS_TESTER_INVOICE_PENDING,
  85. Test::STATUS_TESTER_INVOICE_PAID
  86. ];
  87. public const STAGES = [
  88. 0 => "",
  89. 1 => "Challenge on entry",
  90. 2 => "Challenge while browsing",
  91. 3 => "Challenge during play",
  92. 4 => "Challenge on exit",
  93. 5 => "No challenge",
  94. ];
  95. // Static methods
  96. /**
  97. * @param int $status
  98. * @return string
  99. */
  100. public static function getStatusTesterInvoiceText(int $status): string {
  101. switch($status) {
  102. case self::STATUS_TESTER_INVOICE_NONE:
  103. return __("No Invoice Generated");
  104. case self::STATUS_TESTER_INVOICE_PENDING:
  105. return __("Pending Payment");
  106. case self::STATUS_TESTER_INVOICE_PAID:
  107. return __("Paid");
  108. }
  109. return __("Unknown");
  110. }
  111. /**
  112. * @param int $status
  113. * @return string
  114. */
  115. public static function getStatusTesterInvoiceClass(int $status): string {
  116. switch($status) {
  117. case self::STATUS_TESTER_INVOICE_NONE:
  118. return "secondary";
  119. case self::STATUS_TESTER_INVOICE_PENDING:
  120. return "warning";
  121. case self::STATUS_TESTER_INVOICE_PAID:
  122. return "success";
  123. }
  124. return "";
  125. }
  126. /**
  127. * @param string $processing_file
  128. * @param array $tests
  129. * @throws Exception
  130. */
  131. public static function exportCSV(string $processing_file, array $tests = []): void {
  132. // Preload
  133. Project::load(array_column($tests, "project_id"));
  134. Location::load(array_column($tests, "location_id"));
  135. User::load(array_column($tests, "tester_id"));
  136. // Get processing file
  137. $processing_file_path = ROOT_DIR . "/export_processing/{$processing_file}";
  138. if(!file_exists($processing_file_path)) throw new Exception("Processing file not found.");
  139. // Output
  140. HTTP::setDownloadHeaders("export.csv");
  141. $handle = fopen("php://output", "wb");
  142. /** @noinspection PhpIncludeInspection */
  143. require $processing_file_path;
  144. fclose($handle);
  145. }
  146. // Object methods
  147. /**
  148. * @param string $message
  149. * @return bool
  150. * @throws Exception
  151. */
  152. public function sendAssignEmail(string $message): bool {
  153. $tester = $this->getTester();
  154. $location = $this->getLocation();
  155. $project = $this->getProject();
  156. // Content
  157. $name = htmlspecialchars($tester->first_name);
  158. $message = htmlspecialchars($message);
  159. $base_url = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/account/tests/" . $this->id);
  160. $address = htmlspecialchars($location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]));
  161. $date_due = date("j M Y", strtotime($this->dts_due));
  162. $admin_first_name = htmlspecialchars(Core::$user->first_name);
  163. $admin_phone = htmlspecialchars(Core::$user->phone);
  164. // Build
  165. $builder = new EmailBuilder;
  166. $builder->addParagraph("<b>" . sprintf(__("Hi %s"), $name) . "</b>");
  167. $builder->addLeadParagraph(__("Congrats, you have been allocated a test!"));
  168. if($project->isNighttime()) {
  169. $builder->addParagraph("<b><font color=\"red\">" . __("Evening Test to be done between 7PM and midnight.") . "</font></b>");
  170. }
  171. if($message) {
  172. $builder->addParagraph($message);
  173. }
  174. $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."),
  175. '<b>', '</b>',
  176. '<b>', '</b>',
  177. '<b>', '</b>'
  178. ));
  179. $builder->addAcceptDeclineButtons($base_url);
  180. $builder->addParagraph(sprintf(__("The site address is: %s"), "<b>{$address}</b>"));
  181. $builder->addParagraph(sprintf(__("The date that need to complete and submit the results by is: %s"), "<font color=\"red\"><b>$date_due</b></font>"));
  182. $builder->addParagraph(sprintf(__("For methodology and further notes please check the %sUseful Documents%s section on the %sAccount%s page."),
  183. '<b>', '</b>',
  184. '<b>', '</b>'
  185. ));
  186. $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."),
  187. '<b>', '</b>',
  188. '<b>', '</b>'
  189. ));
  190. $builder->addParagraph(sprintf(__("If you have any questions please send them to %s or call %s on %s."),
  191. "<a href=\"mailto:tests@check-policy.com\">tests@check-policy.com</a>",
  192. "<b>$admin_first_name</b>",
  193. "<b>$admin_phone</b>"
  194. ));
  195. $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."),
  196. '<b>', '</b>'
  197. ));
  198. $content = $builder->build();
  199. // Send
  200. $mail = new Email;
  201. $mail->setHtmlContent($content);
  202. $mail->setFrom(EMAIL_CONTACT, EMAIL_FROM_NAME);
  203. $mail->addAddress($tester->email, $tester->getDisplayName());
  204. $mail->setSubject(__("You have been allocated a test"));
  205. $mail->send();
  206. return true;
  207. }
  208. /**
  209. * @return bool
  210. * @throws Exception
  211. */
  212. public function sendClaimedEmail(): bool {
  213. $tester = $this->getTester();
  214. $location = $this->getLocation();
  215. $project = $this->getProject();
  216. // Content
  217. $name = htmlspecialchars($tester->first_name);
  218. $address = htmlspecialchars($location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]));
  219. $date_due = date("j M Y", strtotime($this->dts_due));
  220. // Build
  221. $builder = new EmailBuilder;
  222. $builder->addParagraph("<b>" . sprintf(__("Hi %s"), $name) . "</b>");
  223. $builder->addLeadParagraph(__("Congrats, you have claimed a test!"));
  224. if($project->isNighttime()) {
  225. $builder->addParagraph("<b><font color=\"red\">" . __("Evening Test to be done between 7PM and midnight.") . "</font></b>");
  226. }
  227. $builder->addParagraph(sprintf(__("The site address is: %s"), "<b>$address</b>"));
  228. $builder->addParagraph(sprintf(__("The date that need to complete and submit the results by is: %s"), "<font color=\"red\"><b>$date_due</b></font>"));
  229. $builder->addParagraph(sprintf(__("For methodology and further notes please check the %sUseful Documents%s section on the %sAccount%s page."),
  230. '<b>', '</b>',
  231. '<b>', '</b>'
  232. ));
  233. $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."),
  234. '<b>', '</b>',
  235. '<b>', '</b>'
  236. ));
  237. $builder->addParagraph(sprintf(__("If you have any questions please send them to %s."),
  238. "<a href=\"mailto:tests@check-policy.com\">tests@check-policy.com</a>"
  239. ));
  240. $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."));
  241. $content = $builder->build();
  242. // Send
  243. $mail = new Email;
  244. $mail->setHtmlContent($content);
  245. $mail->setFrom(EMAIL_CONTACT, EMAIL_FROM_NAME);
  246. $mail->addAddress($tester->email, $tester->getDisplayName());
  247. $mail->setSubject(__("You have claimed a test"));
  248. $mail->send();
  249. return true;
  250. }
  251. /**
  252. * @return bool
  253. * @throws Exception
  254. */
  255. public function sendDeclineEmail(): bool {
  256. $tester = $this->getTester();
  257. $location = $this->getLocation();
  258. $project = $this->getProject();
  259. // Content
  260. $name = htmlspecialchars($tester->getDisplayName());
  261. $address = htmlspecialchars($location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]));
  262. $project_name = htmlspecialchars($project->name);
  263. $project_url = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/account/projects/" . $project->id);
  264. $internal_id = "N/A";
  265. if($location->internal_id) $internal_id = htmlspecialchars($location->internal_id);
  266. // Build
  267. $builder = new EmailBuilder;
  268. $builder->addParagraph("<b>Hi,</b>");
  269. $builder->addLeadParagraph("A tester has declined a test.");
  270. $builder->addParagraph("<b>Project:</b><br />$project_name");
  271. $builder->addParagraph("<b>Site:</b><br />$address<br />ID: $location->id | Internal ID: $internal_id");
  272. $builder->addParagraph("<b>Who:</b><br />$name<br />ID: $tester->id");
  273. $builder->addParagraph("<a href=\"$project_url\">Click here</a> to manage the project and reassign the test.");
  274. $content = $builder->build();
  275. // Send
  276. $mail = new Email;
  277. $mail->setHtmlContent($content);
  278. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  279. $mail->addAddress(EMAIL_CONTACT, APP_NAME . " Contact");
  280. $mail->setSubject("Test declined: $project->name / $location->name");
  281. $mail->send();
  282. return true;
  283. }
  284. /**
  285. * @param string $to_address
  286. * @param bool $include_previous_test
  287. * @return bool
  288. * @throws Exception
  289. */
  290. public function sendResultEmail(string $to_address, bool $include_previous_test): bool {
  291. $location = $this->getLocation();
  292. $project = $this->getProject();
  293. $template = $project->getTemplate();
  294. $subject_format_file_path = $template->getProcessingFilePath("result_email_subject_format.txt");
  295. $subject_format = file_get_contents($subject_format_file_path);
  296. // Content
  297. $pass_text = strtoupper($this->getPassText());
  298. $stage = $this->challenge_stage;
  299. $location_parts = [$location->internal_id, $location->name];
  300. $location_string = implode(" : ", array_filter($location_parts));
  301. $unix_start = strtotime($this->dts_start . " UTC");
  302. $date_time = date("d/m/Y H:i", $unix_start);
  303. // Subject
  304. $search_replace = [
  305. "{pass}" => $pass_text,
  306. "{stage}" => $stage,
  307. "{location}" => $location_string,
  308. "{date-time}" => $date_time,
  309. ];
  310. $subject = str_replace(
  311. array_keys($search_replace),
  312. array_values($search_replace),
  313. $subject_format
  314. );
  315. // Attachments
  316. $pdf_path = $this->getDirectory() . "/pdf.pdf";
  317. $pdf_name = implode(" - ", array_filter([$location->internal_id, date("jS M Y")])) . ".pdf";
  318. // Lead paragraph
  319. $lead_paragraph = "";
  320. if($this->pass) {
  321. $lead_paragraph .= "Congratulations!<br />";
  322. }
  323. $lead_paragraph .= "A test has been completed at your location.";
  324. // Build
  325. $builder = new EmailBuilder;
  326. $builder->addLeadParagraph($lead_paragraph);
  327. $builder->addParagraph("Please see attached PDF and do not hesitate to contact us if you have any questions.");
  328. // Previous test
  329. $previous_pdf_path = $previous_pdf_name = null;
  330. if($include_previous_test) {
  331. $previous_test = $this->getPreviousTest();
  332. if($previous_test && $previous_test->hasPDF()) {
  333. $builder->addParagraph("The previous test result has also been attached for your reference.");
  334. $previous_dt = new DateTime($previous_test->dts_start, new DateTimeZone("UTC"));
  335. $previous_pdf_path = $previous_test->getPDFPath();
  336. $previous_pdf_name = implode(" - ", array_filter([$location->internal_id, $previous_dt->format("jS M Y")])) . ".pdf";
  337. }
  338. }
  339. $content = $builder->build();
  340. // Send
  341. $mail = new Email;
  342. $mail->setHtmlContent($content);
  343. $mail->setFrom(EMAIL_PDF_FROM_ADDRESS, EMAIL_FROM_NAME);
  344. $mail->addAddress($to_address);
  345. $mail->addAttachment($pdf_path, $pdf_name);
  346. if($previous_pdf_name) {
  347. $mail->addAttachment($previous_pdf_path, $previous_pdf_name);
  348. }
  349. $mail->setSubject($subject);
  350. $mail->send();
  351. return true;
  352. }
  353. /**
  354. * @return bool
  355. * @throws Exception
  356. */
  357. public function decline(): bool {
  358. $this->sendDeclineEmail();
  359. $user_id = $this->tester_id;
  360. $location_id = $this->location_id;
  361. $this->status = self::STATUS_UNASSIGNED;
  362. $this->tester_id = null;
  363. $this->dts_due = null;
  364. $this->dts_assigned = null;
  365. $this->dts_rejected = Util::getDateTime();
  366. $this->dts_updated = Util::getDateTime();
  367. $this->commit();
  368. $reject = UserLocationReject::get($user_id, $location_id);
  369. if(!$reject) {
  370. $reject = new UserLocationReject();
  371. $reject->user_id = $user_id;
  372. $reject->location_id = $location_id;
  373. $reject->reject_count = 0;
  374. }
  375. $reject->reject_count++;
  376. $reject->dts_last_rejected = DB::dts();
  377. $reject->commit();
  378. return true;
  379. }
  380. /**
  381. * @return string
  382. */
  383. public function getChallengeStageText(): string {
  384. if(is_null($this->challenge_stage)) return "";
  385. return self::STAGES[$this->challenge_stage];
  386. }
  387. /**
  388. * @return string
  389. */
  390. public function getStatusText(): string {
  391. return self::STATUSES[$this->status];
  392. }
  393. /**
  394. * @return string
  395. */
  396. public function getStatusClass(): string {
  397. switch($this->status) {
  398. case self::STATUS_CANCELLED:
  399. return "secondary";
  400. case self::STATUS_UNASSIGNED:
  401. return "danger";
  402. case self::STATUS_ASSIGNED:
  403. case self::STATUS_CONFIRMED:
  404. case self::STATUS_STARTED:
  405. return "warning";
  406. case self::STATUS_PENDING_VERIFICATION:
  407. return "primary";
  408. case self::STATUS_COMPLETED:
  409. return "success";
  410. }
  411. return "";
  412. }
  413. /**
  414. * @return Project
  415. * @throws Exception
  416. */
  417. public function getProject(): Project {
  418. return Project::get($this->project_id);
  419. }
  420. /**
  421. * @return Location
  422. * @throws Exception
  423. */
  424. public function getLocation(): Location {
  425. return Location::get($this->location_id);
  426. }
  427. /**
  428. * @return User
  429. * @throws Exception
  430. */
  431. public function getTester(): ?User {
  432. return User::get($this->tester_id);
  433. }
  434. /**
  435. * @return User
  436. * @throws Exception
  437. */
  438. public function getVerifier(): ?User {
  439. return User::get($this->verifier_id);
  440. }
  441. /**
  442. * @param bool $as_assoc_array
  443. * @return TestValue[]
  444. * @throws Exception
  445. */
  446. public function getValues(bool $as_assoc_array = false): array {
  447. $rows = TestValue::query(["test_id" => $this->id]);
  448. if($as_assoc_array) {
  449. $values = [];
  450. foreach($rows as $row) {
  451. $values[$row->name] = $row->value;
  452. }
  453. }
  454. else {
  455. $values = $rows;
  456. }
  457. return $values;
  458. }
  459. /**
  460. * @return string
  461. */
  462. public function getDirectory(): string {
  463. $directory = ROOT_DIR . "/files/tests/" . $this->id;
  464. if(!file_exists($directory)) mkdir($directory, 0777, true);
  465. return $directory;
  466. }
  467. /**
  468. * @return bool|float|int
  469. */
  470. public function getDuration() {
  471. if($this->dts_end === null) return false;
  472. return Util::getDurationFromDTS($this->dts_start, $this->dts_end);
  473. }
  474. /**
  475. * @return string
  476. */
  477. public function getDisplayDuration(): string {
  478. $secs = $this->getDuration();
  479. $mins = floor($secs / 60);
  480. $secs %= 60;
  481. return "$mins min. $secs sec.";
  482. }
  483. /**
  484. * @return string Bootstrap class name
  485. */
  486. public function getPassClass(): string {
  487. if($this->isVoid()) return "muted";
  488. if($this->pass === null) return "secondary";
  489. return $this->pass ? "success" : "danger";
  490. }
  491. /**
  492. * @return string FA icon name without prefix
  493. */
  494. public function getPassIcon(): string {
  495. if($this->isVoid()) return "minus-circle";
  496. if($this->pass === null) return "clock";
  497. return $this->pass ? "check-circle" : "times-circle";
  498. }
  499. /**
  500. * @return string Pending, Pass or Fail
  501. */
  502. public function getPassText(): string {
  503. if($this->isVoid()) return __("Void");
  504. if($this->pass === null) return __("Pending");
  505. return $this->pass ? __("Pass") : __("Fail");
  506. }
  507. /** @noinspection PhpUnusedLocalVariableInspection */
  508. public function getMPDF(): Mpdf {
  509. $id = $this->id;
  510. $output = false;
  511. require ROOT_DIR . "/controllers/test_pdf.php";
  512. /** @var Mpdf $mpdf */
  513. return $mpdf;
  514. }
  515. /**
  516. * @param User $tester
  517. * @return bool
  518. * @throws Exception
  519. */
  520. public function assignTester(User $tester): bool {
  521. $this->tester_id = $tester->id;
  522. $this->dts_assigned = Util::getDateTime();
  523. $this->dts_rejected = null;
  524. // TODO: Notify tester
  525. return $this->commit();
  526. }
  527. /**
  528. * @return Test|null
  529. * @throws Exception
  530. */
  531. public function getPreviousTest(): ?Test {
  532. $nvp = [
  533. "dts_start" => (new Constraint("LESS_THAN"))->setValues($this->dts_start),
  534. "location_id" => $this->location_id,
  535. "status" => self::STATUS_COMPLETED,
  536. ];
  537. $tests = self::query($nvp, [
  538. "sort" => "dts_start",
  539. "order" => "desc",
  540. ]);
  541. return $tests ? $tests[0] : null;
  542. }
  543. /**
  544. * @return bool
  545. * @throws Exception
  546. */
  547. public function unassignTester(): bool {
  548. $this->status = self::STATUS_UNASSIGNED;
  549. $this->tester_id = null;
  550. $this->dts_due = null;
  551. $this->dts_assigned = null;
  552. $this->dts_rejected = null;
  553. return $this->commit();
  554. }
  555. /**
  556. * @return bool
  557. */
  558. public function isPass(): bool {
  559. if($this->isPending()) return false;
  560. return !empty($this->pass);
  561. }
  562. /**
  563. * @return bool
  564. */
  565. public function isPending(): bool {
  566. return $this->pass === null;
  567. }
  568. /**
  569. * @return bool
  570. */
  571. public function isFail(): bool {
  572. if($this->isPending()) return false;
  573. return empty($this->pass);
  574. }
  575. /**
  576. * @return bool
  577. */
  578. public function isCompleted(): bool {
  579. return $this->getStatus() === self::STATUS_COMPLETED;
  580. }
  581. public function isDone(): bool {
  582. return in_array($this->getStatus(), [self::STATUS_CANCELLED, self::STATUS_COMPLETED]);
  583. }
  584. /**
  585. * @return bool
  586. */
  587. public function isPendingVerification(): bool {
  588. return $this->getStatus() === self::STATUS_PENDING_VERIFICATION;
  589. }
  590. public function getPDFPath(): string {
  591. $directory = $this->getDirectory();
  592. return sprintf("%s/pdf.pdf", $directory);
  593. }
  594. /**
  595. * @return bool
  596. */
  597. public function hasPDF(): bool {
  598. return file_exists($this->getPDFPath());
  599. }
  600. /**
  601. * @return string
  602. */
  603. public function getPDFUrl(): string {
  604. return "/account/tests/{$this->id}/files/pdf";
  605. }
  606. public function getTesterInvoicePDFPath(): string {
  607. return sprintf("%s/tester_invoice.pdf", $this->getDirectory());
  608. }
  609. /**
  610. * @return bool
  611. */
  612. public function hasTesterInvoicePDF(): bool {
  613. return file_exists($this->getTesterInvoicePDFPath());
  614. }
  615. public function getTravelCostsPhotoPath(): string {
  616. return sprintf("%s/travel_costs.jpg", $this->getDirectory());
  617. }
  618. public function hasTravelCostsPhoto(): bool {
  619. return file_exists($this->getTravelCostsPhotoPath());
  620. }
  621. /**
  622. * @return bool
  623. * @throws Exception
  624. */
  625. public function markTesterInvoicePaid(): bool {
  626. if($this->getTesterInvoiceStatus() === self::STATUS_TESTER_INVOICE_PENDING) {
  627. $this->status_tester_invoice = self::STATUS_TESTER_INVOICE_PAID;
  628. $this->dts_tester_invoice_paid = Util::getDateTime();
  629. return $this->commit();
  630. }
  631. return false;
  632. }
  633. /**
  634. * @return bool
  635. * @throws Exception
  636. */
  637. public function rejectTesterInvoice(): bool {
  638. if($this->getTesterInvoiceStatus() === self::STATUS_TESTER_INVOICE_PENDING) {
  639. $this->status_tester_invoice = self::STATUS_TESTER_INVOICE_NONE;
  640. $this->dts_tester_invoice_generated = null;
  641. if($this->hasTesterInvoicePDF()) {
  642. unlink($this->getTesterInvoicePDFPath());
  643. }
  644. if($this->hasTravelCostsPhoto()) {
  645. unlink($this->getTravelCostsPhotoPath());
  646. }
  647. return $this->commit();
  648. }
  649. return false;
  650. }
  651. /**
  652. * @return string
  653. * @throws Exception
  654. */
  655. public function getTesterInvoiceReference(): string {
  656. $location = $this->getLocation();
  657. $tester = $this->getTester();
  658. return
  659. "T" . str_pad($this->id, 3, "0", STR_PAD_LEFT) .
  660. "/L" . str_pad($location->id, 3, "0", STR_PAD_LEFT) .
  661. "/U" . str_pad($tester->id, 3, "0", STR_PAD_LEFT);
  662. }
  663. /**
  664. * @return float
  665. * @throws Exception
  666. */
  667. public function getTestFee(): float {
  668. return $this->test_fee !== null ? (float) $this->test_fee : (float) $this->getProject()->default_tester_fee;
  669. }
  670. public function getTravelCosts(): float {
  671. return (float) $this->test_travel_cost;
  672. }
  673. /**
  674. * @return float
  675. * @throws Exception
  676. */
  677. public function getInvoiceTotal(): float {
  678. $template = $this->getProject()->getTemplate();
  679. $values = $this->getValues(true);
  680. // Base cost
  681. $fee = $this->getTestFee();
  682. $travel_cost = $this->getTravelCosts();
  683. $invoice_total = $fee + $travel_cost;
  684. // Additional tester invoice fields
  685. $tester_invoice_fields = $template->getTesterInvoiceFields();
  686. foreach($tester_invoice_fields as $field) {
  687. $invoice_total += (float) ($values[$field->name] ?? 0);
  688. }
  689. return $invoice_total;
  690. }
  691. public function isVoid(): bool {
  692. return (bool) $this->void;
  693. }
  694. public function getStatus(): int {
  695. return $this->status;
  696. }
  697. public function getTesterId(): int {
  698. return $this->tester_id;
  699. }
  700. /**
  701. * @return bool Whether the tester has accepted the test
  702. */
  703. public function isConfirmed(): bool {
  704. return $this->getStatus() === self::STATUS_CONFIRMED;
  705. }
  706. public function hasStarted(): bool {
  707. return $this->getStatus() === self::STATUS_STARTED;
  708. }
  709. public function isAssigned(): bool {
  710. return $this->getStatus() === self::STATUS_ASSIGNED;
  711. }
  712. /**
  713. * @return array
  714. * @throws Exception
  715. */
  716. public function getFieldsForVerifyForm(): array {
  717. $json = json_decode($this->getProject()->getTemplate()->getJSON());
  718. return array_merge($json->beforeTest, [(object) ["type" => "hr"]], $json->duringTest);
  719. }
  720. public function getTesterInvoiceStatus(): int {
  721. return $this->status_tester_invoice;
  722. }
  723. public function isCancelled(): bool {
  724. return $this->getStatus() === self::STATUS_CANCELLED;
  725. }
  726. public function hasDueDateTime(): bool {
  727. return $this->dts_due !== null;
  728. }
  729. public function getTimeDue(): int {
  730. if(!$this->dts_due) {
  731. return 0;
  732. }
  733. return $this->getDateTimeDue()->getTimestamp();
  734. }
  735. public function hasNotStarted(): bool {
  736. return $this->status < self::STATUS_STARTED;
  737. }
  738. public function isOverdue(): bool {
  739. return time() > $this->getTimeDue();
  740. }
  741. public function getDateTimeDue(): ?CarbonImmutable {
  742. return Util::getDateTimeFromUtcString($this->dts_due);
  743. }
  744. public function getDateTimeStart(): ?CarbonImmutable {
  745. return Util::getDateTimeFromUtcString($this->dts_start);
  746. }
  747. public function getDateTimeEnd(): ?CarbonImmutable {
  748. return Util::getDateTimeFromUtcString($this->dts_end);
  749. }
  750. public function getVerifierNotesHtml(): string {
  751. return nl2br(htmlspecialchars($this->verifier_notes));
  752. }
  753. /**
  754. * @return float
  755. * @throws Exception
  756. */
  757. public function getTesterInvoiceCosts(): float {
  758. return $this->getProject()->getTemplate()->getTesterInvoiceCosts($this->getValues(true));
  759. }
  760. public function getDateTimeRejected(): ?CarbonImmutable {
  761. if(!$this->dts_rejected) {
  762. return null;
  763. }
  764. return CarbonImmutable::parse($this->dts_rejected, "UTC");
  765. }
  766. public function renderTestStatus(User $user): void {
  767. ?>
  768. <a href="/account/tests/<?=$this->id?>">
  769. <?php
  770. if($this->isPass()) {
  771. ?>
  772. <i class="fa fa-fw fa-check-circle text-success"></i> Passed on <?=$user->formatDate($this->getDateTimeStart())?>
  773. <?php
  774. }
  775. elseif($this->isVoid()) {
  776. ?>
  777. <i class="fa fa-fw fa-minus-circle text-secondary"></i> Void on <?=$user->formatDate($this->getDateTimeStart())?>
  778. <?php
  779. }
  780. elseif($this->isFail()) {
  781. ?>
  782. <i class="fa fa-fw fa-times-circle text-danger"></i> Failed on <?=$user->formatDate($this->getDateTimeStart())?>
  783. <?php
  784. }
  785. else {
  786. ?>
  787. <i class="fa fa-fw fa-question-circle text-warning"></i> Result Pending (Test <?=htmlspecialchars($this->getStatusText())?>)
  788. <?php
  789. }
  790. ?>
  791. </a>
  792. <?php
  793. }
  794. }