1
0

test.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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. * @return bool
  272. * @throws Exception
  273. */
  274. public function sendResultEmail(string $to_address): bool {
  275. $location = $this->getLocation();
  276. $project = $this->getProject();
  277. $template = $project->getTemplate();
  278. $subject_format_file_path = $template->getProcessingFilePath("result_email_subject_format.txt");
  279. $subject_format = file_get_contents($subject_format_file_path);
  280. // Content
  281. $pass_text = strtoupper($this->getPassText());
  282. $stage = $this->challenge_stage;
  283. $location_parts = [$location->internal_id, $location->name];
  284. $location_string = implode(" : ", array_filter($location_parts));
  285. $unix_start = strtotime($this->dts_start . " UTC");
  286. $date_time = date("d/m/Y H:i", $unix_start);
  287. // Subject
  288. $search_replace = [
  289. "{pass}" => $pass_text,
  290. "{stage}" => $stage,
  291. "{location}" => $location_string,
  292. "{date-time}" => $date_time,
  293. ];
  294. $subject = str_replace(
  295. array_keys($search_replace),
  296. array_values($search_replace),
  297. $subject_format
  298. );
  299. // Attachments
  300. $pdf_path = $this->getDirectory() . "/pdf.pdf";
  301. $pdf_name = implode(" - ", array_filter([$location->internal_id, date("jS M Y")])) . ".pdf";
  302. // Lead paragraph
  303. $lead_paragraph = "";
  304. if($this->pass) {
  305. $lead_paragraph .= "Congratulations!<br />";
  306. }
  307. $lead_paragraph .= "A test has been completed at your location.";
  308. // Build
  309. $builder = new EmailBuilder;
  310. $builder->addLeadParagraph($lead_paragraph);
  311. $builder->addParagraph("Please see attached PDF and do not hesitate to contact us if you have any questions.");
  312. // Previous test
  313. $previous_pdf_path = $previous_pdf_name = null;
  314. $previous_test = $this->getPreviousTest();
  315. if($previous_test && $previous_test->hasPDF()) {
  316. $builder->addParagraph("The previous test result has also been attached for your reference.");
  317. $previous_dt = new DateTime($previous_test->dts_start, new DateTimeZone("UTC"));
  318. $previous_pdf_path = $previous_test->getPDFPath();
  319. $previous_pdf_name = implode(" - ", array_filter([$location->internal_id, $previous_dt->format("jS M Y")])) . ".pdf";
  320. }
  321. $content = $builder->build();
  322. // Send
  323. $mail = new Email;
  324. $mail->setHtmlContent($content);
  325. $mail->setFrom(EMAIL_CONTACT, EMAIL_FROM_NAME);
  326. $mail->addAddress($to_address);
  327. $mail->addAttachment($pdf_path, $pdf_name);
  328. if($previous_pdf_name) {
  329. $mail->addAttachment($previous_pdf_path, $previous_pdf_name);
  330. }
  331. $mail->setSubject($subject);
  332. $mail->send();
  333. return true;
  334. }
  335. /**
  336. * @return bool
  337. * @throws Exception
  338. */
  339. public function decline(): bool {
  340. $this->sendDeclineEmail();
  341. $this->status = self::STATUS_UNASSIGNED;
  342. $this->tester_id = null;
  343. $this->dts_due = null;
  344. $this->dts_assigned = null;
  345. $this->dts_updated = Util::getDateTime();
  346. return $this->commit();
  347. }
  348. /**
  349. * @return string
  350. */
  351. public function getChallengeStageText(): string {
  352. if(is_null($this->challenge_stage)) return "";
  353. return self::STAGES[$this->challenge_stage];
  354. }
  355. /**
  356. * @return string
  357. */
  358. public function getStatusText(): string {
  359. return self::STATUSES[$this->status];
  360. }
  361. /**
  362. * @return string
  363. */
  364. public function getStatusClass(): string {
  365. switch($this->status) {
  366. case self::STATUS_CANCELLED:
  367. return "secondary";
  368. case self::STATUS_UNASSIGNED:
  369. return "danger";
  370. case self::STATUS_ASSIGNED:
  371. case self::STATUS_CONFIRMED:
  372. case self::STATUS_STARTED:
  373. return "warning";
  374. case self::STATUS_PENDING_VERIFICATION:
  375. return "primary";
  376. case self::STATUS_COMPLETED:
  377. return "success";
  378. }
  379. return "";
  380. }
  381. /**
  382. * @return Project
  383. * @throws Exception
  384. */
  385. public function getProject(): Project {
  386. return Project::get($this->project_id);
  387. }
  388. /**
  389. * @return Location
  390. * @throws Exception
  391. */
  392. public function getLocation(): ?Location {
  393. return Location::get($this->location_id);
  394. }
  395. /**
  396. * @return User
  397. * @throws Exception
  398. */
  399. public function getTester(): ?User {
  400. return User::get($this->tester_id);
  401. }
  402. /**
  403. * @return User
  404. * @throws Exception
  405. */
  406. public function getVerifier(): ?User {
  407. return User::get($this->verifier_id);
  408. }
  409. /**
  410. * @param bool $as_assoc_array
  411. * @return array
  412. * @throws Exception
  413. */
  414. public function getValues(bool $as_assoc_array = false): array {
  415. $rows = TestValue::query(["test_id" => $this->id]);
  416. if($as_assoc_array) {
  417. $values = [];
  418. foreach($rows as $row) {
  419. $values[$row->name] = $row->value;
  420. }
  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($this->dts_end === null) return false;
  440. return Util::getDurationFromDTS($this->dts_start, $this->dts_end);
  441. }
  442. /**
  443. * @return string
  444. */
  445. public function getDisplayDuration(): string {
  446. $secs = $this->getDuration();
  447. $mins = floor($secs / 60);
  448. $secs %= 60;
  449. return "$mins min. $secs sec.";
  450. }
  451. /**
  452. * @return string Bootstrap class name
  453. */
  454. public function getPassClass(): string {
  455. if($this->isVoid()) return "muted";
  456. if($this->pass === null) 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($this->isVoid()) return "minus-circle";
  464. if($this->pass === null) return "clock";
  465. return $this->pass ? "check-circle" : "times-circle";
  466. }
  467. /**
  468. * @return string Pending, Pass or Fail
  469. */
  470. public function getPassText(): string {
  471. if($this->isVoid()) return __("Void");
  472. if($this->pass === null) return __("Pending");
  473. return $this->pass ? __("Pass") : __("Fail");
  474. }
  475. /** @noinspection PhpUnusedLocalVariableInspection */
  476. public function getMPDF(): Mpdf {
  477. $id = $this->id;
  478. $output = false;
  479. require ROOT_DIR . "/controllers/test_pdf.php";
  480. /** @var Mpdf $mpdf */
  481. return $mpdf;
  482. }
  483. /**
  484. * @param User $tester
  485. * @return bool
  486. * @throws Exception
  487. */
  488. public function assignTester(User $tester): bool {
  489. $this->tester_id = $tester->id;
  490. $this->dts_assigned = Util::getDateTime();
  491. // TODO: Notify tester
  492. return $this->commit();
  493. }
  494. /**
  495. * @return Test|null
  496. * @throws Exception
  497. */
  498. public function getPreviousTest(): ?Test {
  499. $nvp = [
  500. "dts_start" => (new Constraint("LESS_THAN"))->setValues($this->dts_start),
  501. "location_id" => $this->location_id,
  502. "status" => self::STATUS_COMPLETED,
  503. ];
  504. $tests = self::query($nvp, [
  505. "sort" => "dts_start",
  506. "order" => "desc",
  507. ]);
  508. return $tests ? $tests[0] : null;
  509. }
  510. /**
  511. * @return bool
  512. * @throws Exception
  513. */
  514. public function unassignTester(): bool {
  515. $this->status = self::STATUS_UNASSIGNED;
  516. $this->tester_id = null;
  517. $this->dts_due = null;
  518. $this->dts_assigned = null;
  519. return $this->commit();
  520. }
  521. /**
  522. * @return bool
  523. */
  524. public function isPass(): bool {
  525. if(is_null($this->pass)) return false;
  526. return !empty($this->pass);
  527. }
  528. /**
  529. * @return bool
  530. */
  531. public function isPending(): bool {
  532. return is_null($this->pass);
  533. }
  534. /**
  535. * @return bool
  536. */
  537. public function isFail(): bool {
  538. if(is_null($this->pass)) return false;
  539. return empty($this->pass);
  540. }
  541. /**
  542. * @return bool
  543. */
  544. public function isCompleted(): bool {
  545. return $this->getStatus() === self::STATUS_COMPLETED;
  546. }
  547. /**
  548. * @return bool
  549. */
  550. public function isPendingVerification(): bool {
  551. return $this->getStatus() === self::STATUS_PENDING_VERIFICATION;
  552. }
  553. public function getPDFPath(): string {
  554. $directory = $this->getDirectory();
  555. return sprintf("%s/pdf.pdf", $directory);
  556. }
  557. /**
  558. * @return bool
  559. */
  560. public function hasPDF(): bool {
  561. return file_exists($this->getPDFPath());
  562. }
  563. /**
  564. * @return string
  565. */
  566. public function getPDFUrl(): string {
  567. return "/account/tests/{$this->id}/files/pdf";
  568. }
  569. public function getTesterInvoicePDFPath(): string {
  570. return sprintf("%s/tester_invoice.pdf", $this->getDirectory());
  571. }
  572. /**
  573. * @return bool
  574. */
  575. public function hasTesterInvoicePDF(): bool {
  576. return file_exists($this->getTesterInvoicePDFPath());
  577. }
  578. public function getTravelCostsPhotoPath(): string {
  579. return sprintf("%s/travel_costs.jpg", $this->getDirectory());
  580. }
  581. public function hasTravelCostsPhoto(): bool {
  582. return file_exists($this->getTravelCostsPhotoPath());
  583. }
  584. /**
  585. * @return bool
  586. * @throws Exception
  587. */
  588. public function markTesterInvoicePaid(): bool {
  589. if($this->getTesterInvoiceStatus() === self::STATUS_TESTER_INVOICE_PENDING) {
  590. $this->status_tester_invoice = self::STATUS_TESTER_INVOICE_PAID;
  591. $this->dts_tester_invoice_paid = Util::getDateTime();
  592. return $this->commit();
  593. }
  594. return false;
  595. }
  596. /**
  597. * @return bool
  598. * @throws Exception
  599. */
  600. public function rejectTesterInvoice(): bool {
  601. if($this->getTesterInvoiceStatus() === self::STATUS_TESTER_INVOICE_PENDING) {
  602. $this->status_tester_invoice = self::STATUS_TESTER_INVOICE_NONE;
  603. $this->dts_tester_invoice_generated = null;
  604. if($this->hasTesterInvoicePDF()) {
  605. unlink($this->getTesterInvoicePDFPath());
  606. }
  607. if($this->hasTravelCostsPhoto()) {
  608. unlink($this->getTravelCostsPhotoPath());
  609. }
  610. return $this->commit();
  611. }
  612. return false;
  613. }
  614. /**
  615. * @return string
  616. * @throws Exception
  617. */
  618. public function getTesterInvoiceReference(): string {
  619. $location = $this->getLocation();
  620. $tester = $this->getTester();
  621. return
  622. "T" . str_pad($this->id, 3, "0", STR_PAD_LEFT) .
  623. "/L" . str_pad($location->id, 3, "0", STR_PAD_LEFT) .
  624. "/U" . str_pad($tester->id, 3, "0", STR_PAD_LEFT);
  625. }
  626. /**
  627. * @return float
  628. * @throws Exception
  629. */
  630. public function getTestFee(): float {
  631. return $this->test_fee !== null ? (float) $this->test_fee : (float) $this->getProject()->default_tester_fee;
  632. }
  633. public function getTravelCosts(): float {
  634. return (float) $this->test_travel_cost;
  635. }
  636. /**
  637. * @return float
  638. * @throws Exception
  639. */
  640. public function getInvoiceTotal(): float {
  641. $template = $this->getProject()->getTemplate();
  642. $values = $this->getValues(true);
  643. // Base cost
  644. $fee = $this->getTestFee();
  645. $travel_cost = $this->getTravelCosts();
  646. $invoice_total = $fee + $travel_cost;
  647. // Additional tester invoice fields
  648. $tester_invoice_fields = $template->getTesterInvoiceFields();
  649. foreach($tester_invoice_fields as $field) {
  650. $invoice_total += (float) ($values[$field->name] ?? 0);
  651. }
  652. return $invoice_total;
  653. }
  654. public function isVoid(): bool {
  655. return (bool) $this->void;
  656. }
  657. public function getStatus(): int {
  658. return $this->status;
  659. }
  660. public function getTesterId(): int {
  661. return $this->tester_id;
  662. }
  663. /**
  664. * @return bool Whether the tester has accepted the test
  665. */
  666. public function isConfirmed(): bool {
  667. return $this->getStatus() === self::STATUS_CONFIRMED;
  668. }
  669. public function hasStarted(): bool {
  670. return $this->getStatus() === self::STATUS_STARTED;
  671. }
  672. public function isAssigned(): bool {
  673. return $this->getStatus() === self::STATUS_ASSIGNED;
  674. }
  675. /**
  676. * @return array
  677. * @throws Exception
  678. */
  679. public function getFieldsForVerifyForm(): array {
  680. $json = json_decode($this->getProject()->getTemplate()->getJSON());
  681. if($this->isVoid()) {
  682. return $json->beforeTest;
  683. }
  684. return array_merge($json->beforeTest, [(object) ["type" => "hr"]], $json->duringTest);
  685. }
  686. public function getTesterInvoiceStatus(): int {
  687. return $this->status_tester_invoice;
  688. }
  689. }