test.php 24 KB

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