test.php 23 KB

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