test.php 22 KB

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