test.php 25 KB

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