1
0

test.php 23 KB

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