test.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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 $dts_created
  37. * @property string $dts_updated
  38. * @property string $dts_assigned
  39. * @property string $dts_verified
  40. */
  41. class Test extends Model {
  42. protected static $table = "test";
  43. protected static $id_field = "id";
  44. 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", "dts_created", "dts_updated", "dts_assigned", "dts_verified"];
  45. const TEST_FIELDS = ["shop_left", "shop_right", "attire", "challenge_stage", "challenge_description", "challenger_name", "challenger_description", "challenger_gender", "challenger_eye_contact", "additional_notes"];
  46. // Constants
  47. const STATUS_CANCELLED = 0;
  48. const STATUS_UNASSIGNED = 1; // Location needs a tester
  49. const STATUS_ASSIGNED = 2; // Tester assigned
  50. const STATUS_CONFIRMED = 3; // Tester has said they can do it
  51. const STATUS_STARTED = 4;
  52. const STATUS_PENDING_VERIFICATION = 8;
  53. const STATUS_COMPLETED = 16;
  54. const STATUS_TESTER_INVOICE_NONE = 0;
  55. const STATUS_TESTER_INVOICE_PENDING = 100;
  56. const STATUS_TESTER_INVOICE_PAID = 200;
  57. const STATUSES = [
  58. 0 => "Cancelled",
  59. 1 => "Unassigned",
  60. 2 => "Assigned",
  61. 3 => "Confirmed",
  62. 4 => "Started",
  63. 8 => "Pending Verification",
  64. 16 => "Completed",
  65. ];
  66. const STATUSES_TESTER_INVOICE = [
  67. Test::STATUS_TESTER_INVOICE_NONE,
  68. Test::STATUS_TESTER_INVOICE_PENDING,
  69. Test::STATUS_TESTER_INVOICE_PAID
  70. ];
  71. const STAGES = [
  72. 0 => "",
  73. 1 => "Challenge on entry",
  74. 2 => "Challenge while browsing",
  75. 3 => "Challenge during play",
  76. 4 => "Challenge on exit",
  77. 5 => "No challenge",
  78. ];
  79. const TEST_TIME_ANYTIME = 1;
  80. const TEST_TIME_DAY = 2;
  81. const TEST_TIME_NIGHT = 3;
  82. // Static methods
  83. /**
  84. * @param int $status
  85. * @return string
  86. */
  87. public static function getStatusTesterInvoiceText(int $status): string {
  88. switch($status) {
  89. case Test::STATUS_TESTER_INVOICE_NONE:
  90. return __("No Invoice Generated");
  91. case Test::STATUS_TESTER_INVOICE_PENDING:
  92. return __("Pending Payment");
  93. case Test::STATUS_TESTER_INVOICE_PAID:
  94. return __("Paid");
  95. }
  96. return __("Unknown");
  97. }
  98. /**
  99. * @param int $status
  100. * @return string
  101. */
  102. public static function getStatusTesterInvoiceClass(int $status): string {
  103. switch($status) {
  104. case Test::STATUS_TESTER_INVOICE_NONE:
  105. return "secondary";
  106. case Test::STATUS_TESTER_INVOICE_PENDING:
  107. return "warning";
  108. case Test::STATUS_TESTER_INVOICE_PAID:
  109. return "success";
  110. }
  111. return "";
  112. }
  113. /**
  114. * @param string $processing_file
  115. * @param array $tests
  116. * @throws Exception
  117. */
  118. public static function exportCSV(string $processing_file, array $tests = []): void {
  119. // Preload
  120. Project::load(array_column($tests, "project_id"));
  121. Location::load(array_column($tests, "location_id"));
  122. User::load(array_column($tests, "tester_id"));
  123. // Get processing file
  124. $processing_file_path = ROOT_DIR . "/export_processing/{$processing_file}";
  125. if(!file_exists($processing_file_path)) throw new Exception("Processing file not found.");
  126. // Output
  127. HTTP::setDownloadHeaders("export.csv");
  128. $handle = fopen("php://output", "w");
  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. * @return bool
  273. * @throws Exception
  274. */
  275. public function sendResultEmail(string $to_address): 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. $previous_test = $this->getPreviousTest();
  316. if($previous_test && $previous_test->hasPDF()) {
  317. $builder->addParagraph("The previous test result has also been attached for your reference.");
  318. $previous_dt = new DateTime($previous_test->dts_start, new DateTimeZone("UTC"));
  319. $previous_pdf_path = $previous_test->getPDFPath();
  320. $previous_pdf_name = implode(" - ", array_filter([$location->internal_id, $previous_dt->format("jS M Y")])) . ".pdf";
  321. }
  322. $content = $builder->build();
  323. // Send
  324. $mail = new Email;
  325. $mail->setHtmlContent($content);
  326. $mail->setFrom(EMAIL_CONTACT, EMAIL_FROM_NAME);
  327. $mail->addAddress($to_address);
  328. $mail->addAttachment($pdf_path, $pdf_name);
  329. if($previous_pdf_name) {
  330. $mail->addAttachment($previous_pdf_path, $previous_pdf_name);
  331. }
  332. $mail->setSubject($subject);
  333. $mail->send();
  334. return true;
  335. }
  336. /**
  337. * @return bool
  338. * @throws Exception
  339. */
  340. public function decline(): bool {
  341. $this->sendDeclineEmail();
  342. $this->status = Test::STATUS_UNASSIGNED;
  343. $this->tester_id = null;
  344. $this->dts_due = null;
  345. $this->dts_assigned = null;
  346. $this->dts_updated = Util::getDateTime();
  347. return $this->commit();
  348. }
  349. /**
  350. * @return string
  351. */
  352. public function getChallengeStageText(): string {
  353. if(is_null($this->challenge_stage)) return "";
  354. return Test::STAGES[$this->challenge_stage];
  355. }
  356. /**
  357. * @return string
  358. */
  359. public function getStatusText(): string {
  360. return Test::STATUSES[$this->status];
  361. }
  362. /**
  363. * @return string
  364. */
  365. public function getStatusClass(): string {
  366. switch($this->status) {
  367. case Test::STATUS_CANCELLED:
  368. return "secondary";
  369. case Test::STATUS_UNASSIGNED:
  370. return "danger";
  371. case Test::STATUS_ASSIGNED:
  372. case Test::STATUS_CONFIRMED:
  373. case Test::STATUS_STARTED:
  374. return "warning";
  375. case Test::STATUS_PENDING_VERIFICATION:
  376. return "primary";
  377. case Test::STATUS_COMPLETED:
  378. return "success";
  379. }
  380. return "";
  381. }
  382. /**
  383. * @return Project
  384. * @throws Exception
  385. */
  386. public function getProject(): Project {
  387. return Project::get($this->project_id);
  388. }
  389. /**
  390. * @return Location
  391. * @throws Exception
  392. */
  393. public function getLocation(): ?Location {
  394. return Location::get($this->location_id);
  395. }
  396. /**
  397. * @return User
  398. * @throws Exception
  399. */
  400. public function getTester(): ?User {
  401. return User::get($this->tester_id);
  402. }
  403. /**
  404. * @return User
  405. * @throws Exception
  406. */
  407. public function getVerifier(): ?User {
  408. return User::get($this->verifier_id);
  409. }
  410. /**
  411. * @param bool $as_assoc_array
  412. * @return array
  413. * @throws Exception
  414. */
  415. public function getValues(bool $as_assoc_array = false): array {
  416. $rows = TestValue::query(["test_id" => $this->id]);
  417. if($as_assoc_array) {
  418. $k = array_column($rows, "name");
  419. $v = array_column($rows, "value");
  420. $values = array_combine($k, $v);
  421. }
  422. else {
  423. $values = $rows;
  424. }
  425. return $values;
  426. }
  427. /**
  428. * @return string
  429. */
  430. public function getDirectory(): string {
  431. $directory = ROOT_DIR . "/files/tests/" . $this->id;
  432. if(!file_exists($directory)) mkdir($directory, 0777, true);
  433. return $directory;
  434. }
  435. /**
  436. * @return bool|float|int
  437. */
  438. public function getDuration() {
  439. if(is_null($this->dts_end)) return false;
  440. $seconds = Util::getDurationFromDTS($this->dts_start, $this->dts_end);
  441. return $seconds;
  442. }
  443. /**
  444. * @return string
  445. */
  446. public function getDisplayDuration(): string {
  447. $secs = $this->getDuration();
  448. $mins = floor($secs / 60);
  449. $secs = $secs % 60;
  450. return "$mins min. $secs sec.";
  451. }
  452. /**
  453. * @return string Bootstrap class name
  454. */
  455. public function getPassClass(): string {
  456. if(is_null($this->pass)) return "secondary";
  457. return $this->pass ? "success" : "danger";
  458. }
  459. /**
  460. * @return string FA icon name without prefix
  461. */
  462. public function getPassIcon(): string {
  463. if(is_null($this->pass)) return "clock";
  464. return $this->pass ? "check-circle" : "times-circle";
  465. }
  466. /**
  467. * @return string Pending, Pass or Fail
  468. */
  469. public function getPassText(): string {
  470. if(is_null($this->pass)) return __("Pending");
  471. return $this->pass ? __("Pass") : __("Fail");
  472. }
  473. public function getMPDF(): Mpdf {
  474. /** @var Mpdf $mpdf */
  475. $id = $this->id;
  476. $output = false;
  477. require ROOT_DIR . "/controllers/test_pdf.php";
  478. return $mpdf;
  479. }
  480. /**
  481. * @param User $tester
  482. * @return bool
  483. * @throws Exception
  484. */
  485. public function assignTester(User $tester): bool {
  486. $this->tester_id = $tester->id;
  487. $this->dts_assigned = Util::getDateTime();
  488. $success = $this->commit();
  489. // TODO: Notify tester
  490. return $success;
  491. }
  492. /**
  493. * @return Test|null
  494. * @throws Exception
  495. */
  496. public function getPreviousTest(): ?Test {
  497. $nvp = [
  498. "dts_start" => (new Constraint("LESS_THAN"))->setValues($this->dts_start),
  499. "location_id" => $this->location_id,
  500. "status" => Test::STATUS_COMPLETED,
  501. ];
  502. $tests = Test::query($nvp, [
  503. "sort" => "dts_start",
  504. "order" => "desc",
  505. ]);
  506. return $tests ? $tests[0] : null;
  507. }
  508. /**
  509. * @return bool
  510. * @throws Exception
  511. */
  512. public function unassignTester(): bool {
  513. $this->status = Test::STATUS_UNASSIGNED;
  514. $this->tester_id = null;
  515. $this->dts_due = null;
  516. $this->dts_assigned = null;
  517. return $this->commit();
  518. }
  519. /**
  520. * @return bool
  521. */
  522. public function isPass(): bool {
  523. if(is_null($this->pass)) return false;
  524. return !empty($this->pass);
  525. }
  526. /**
  527. * @return bool
  528. */
  529. public function isPending(): bool {
  530. return is_null($this->pass);
  531. }
  532. /**
  533. * @return bool
  534. */
  535. public function isFail(): bool {
  536. if(is_null($this->pass)) return false;
  537. return empty($this->pass);
  538. }
  539. /**
  540. * @return bool
  541. */
  542. public function isCompleted(): bool {
  543. return $this->getStatus() === self::STATUS_COMPLETED;
  544. }
  545. /**
  546. * @return bool
  547. */
  548. public function isPendingVerification(): bool {
  549. return $this->getStatus() === self::STATUS_PENDING_VERIFICATION;
  550. }
  551. public function getPDFPath() {
  552. $directory = $this->getDirectory();
  553. return sprintf("%s/pdf.pdf", $directory);
  554. }
  555. /**
  556. * @return bool
  557. */
  558. public function hasPDF(): bool {
  559. return file_exists($this->getPDFPath());
  560. }
  561. /**
  562. * @return string
  563. */
  564. public function getPDFUrl(): string {
  565. return "/account/tests/{$this->id}/files/pdf";
  566. }
  567. public function getTesterInvoicePDFPath(): string {
  568. return sprintf("%s/tester_invoice.pdf", $this->getDirectory());
  569. }
  570. /**
  571. * @return bool
  572. */
  573. public function hasTesterInvoicePDF(): bool {
  574. return file_exists($this->getTesterInvoicePDFPath());
  575. }
  576. public function getTravelCostsPhotoPath(): string {
  577. return sprintf("%s/travel_costs.jpg", $this->getDirectory());
  578. }
  579. public function hasTravelCostsPhoto(): bool {
  580. return file_exists($this->getTravelCostsPhotoPath());
  581. }
  582. /**
  583. * @return bool
  584. * @throws Exception
  585. */
  586. public function markTesterInvoicePaid(): bool {
  587. if($this->status_tester_invoice == Test::STATUS_TESTER_INVOICE_PENDING) {
  588. $this->status_tester_invoice = Test::STATUS_TESTER_INVOICE_PAID;
  589. $this->dts_tester_invoice_paid = Util::getDateTime();
  590. return $this->commit();
  591. }
  592. return false;
  593. }
  594. /**
  595. * @return bool
  596. * @throws Exception
  597. */
  598. public function rejectTesterInvoice(): bool {
  599. if($this->status_tester_invoice == Test::STATUS_TESTER_INVOICE_PENDING) {
  600. $this->status_tester_invoice = Test::STATUS_TESTER_INVOICE_NONE;
  601. $this->dts_tester_invoice_generated = null;
  602. if($this->hasTesterInvoicePDF()) {
  603. unlink($this->getTesterInvoicePDFPath());
  604. }
  605. if($this->hasTravelCostsPhoto()) {
  606. unlink($this->getTravelCostsPhotoPath());
  607. }
  608. return $this->commit();
  609. }
  610. return false;
  611. }
  612. /**
  613. * @return string
  614. * @throws Exception
  615. */
  616. public function getTesterInvoiceReference(): string {
  617. $location = $this->getLocation();
  618. $tester = $this->getTester();
  619. return
  620. "T" . str_pad($this->id, 3, "0", STR_PAD_LEFT) .
  621. "/L" . str_pad($location->id, 3, "0", STR_PAD_LEFT) .
  622. "/U" . str_pad($tester->id, 3, "0", STR_PAD_LEFT);
  623. }
  624. /**
  625. * @return float
  626. * @throws Exception
  627. */
  628. public function getTestFee(): float {
  629. return $this->test_fee !== null ? (float) $this->test_fee : (float) $this->getProject()->default_tester_fee;
  630. }
  631. public function getTravelCosts(): float {
  632. return (float) $this->test_travel_cost;
  633. }
  634. /**
  635. * @return float
  636. * @throws Exception
  637. */
  638. public function getInvoiceTotal(): float {
  639. $template = $this->getProject()->getTemplate();
  640. $values = $this->getValues(true);
  641. // Base cost
  642. $fee = $this->getTestFee();
  643. $travel_cost = $this->getTravelCosts();
  644. $invoice_total = $fee + $travel_cost;
  645. // Additional tester invoice fields
  646. $tester_invoice_fields = $template->getTesterInvoiceFields();
  647. foreach($tester_invoice_fields as $field) {
  648. $invoice_total += (float) ($values[$field->name] ?? 0);
  649. }
  650. return $invoice_total;
  651. }
  652. public function isVoid(): bool {
  653. return (bool) $this->void;
  654. }
  655. public function getStatus(): int {
  656. return $this->status;
  657. }
  658. public function getTesterId(): int {
  659. return $this->tester_id;
  660. }
  661. /**
  662. * @return bool Whether the tester has accepted the test
  663. */
  664. public function isConfirmed(): bool {
  665. return $this->getStatus() === self::STATUS_CONFIRMED;
  666. }
  667. public function hasStarted(): bool {
  668. return $this->getStatus() === self::STATUS_STARTED;
  669. }
  670. public function isAssigned(): bool {
  671. return $this->getStatus() === self::STATUS_ASSIGNED;
  672. }
  673. }