project.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. <?php /** @noinspection PhpIncludeInspection */
  2. /**
  3. * @property int $id
  4. * @property int $customer_id
  5. * @property int $template_id
  6. * @property string $name
  7. * @property int $test_count
  8. * @property int $min_age
  9. * @property int $max_age
  10. * @property float $default_customer_fee
  11. * @property float $default_tester_fee
  12. * @property int $time_of_day
  13. * @property bool $tester_can_claim
  14. * @property string $template_overrides_json
  15. * @property string $dts_start
  16. * @property string $dts_deadline
  17. * @property string $dts_created
  18. * @property string $dts_updated
  19. * @property string $dts_report
  20. * @property string $dts_finished
  21. * @property int $user_id_finished
  22. */
  23. class Project extends Model {
  24. const TOD_ANYTIME = "A";
  25. const TOD_DAYTIME = "D";
  26. const TOD_NIGHTTIME = "N";
  27. protected static $table = "project";
  28. protected static $id_field = "id";
  29. protected static $fillable_fields = ["customer_id", "template_id", "name", "test_count", "min_age", "max_age", "default_customer_fee", "default_tester_fee", "time_of_day", "tester_can_claim", "template_overrides_json", "dts_start", "dts_deadline", "dts_created", "dts_updated", "dts_report", "dts_finished", "user_id_finished"];
  30. /**
  31. * @var Test[] $tests
  32. */
  33. protected $tests = null;
  34. // Static methods
  35. /**
  36. * @param string $processing_file
  37. * @param array $projects
  38. * @throws Exception
  39. */
  40. public static function exportCSV(string $processing_file, /** @noinspection PhpUnusedParameterInspection */ $projects = []) {
  41. // Get processing file
  42. $processing_file_path = ROOT_DIR . "/export_processing/{$processing_file}";
  43. if(!file_exists($processing_file_path)) throw new Exception("Processing file not found.");
  44. // Output
  45. HTTP::setDownloadHeaders("export.csv");
  46. $handle = fopen("php://output", "w");
  47. require $processing_file_path;
  48. fclose($handle);
  49. }
  50. // Object methods
  51. /**
  52. * @return Customer
  53. * @throws Exception
  54. */
  55. public function getCustomer(): ?Customer {
  56. return Customer::get($this->customer_id);
  57. }
  58. /**
  59. * @return Test[]
  60. * @throws Exception
  61. */
  62. public function getTests(): array {
  63. if(!is_null($this->tests)) return $this->tests;
  64. $nvp = [];
  65. $nvp["project_id"] = $this->id;
  66. $nvp["status"] = (new Constraint("MORE_THAN"))->setValues(Test::STATUS_CANCELLED);
  67. $this->tests = Test::query($nvp);
  68. return $this->tests;
  69. }
  70. /**
  71. * @return int
  72. * @throws Exception
  73. */
  74. public function getTestCount(): int {
  75. if(is_null($this->tests)) $this->getTests();
  76. return count($this->tests);
  77. }
  78. /**
  79. * @return int
  80. * @throws Exception
  81. */
  82. public function getPassCount(): int {
  83. $count = 0;
  84. $tests = $this->getTests();
  85. foreach($tests as $test) {
  86. if(!$test->isPass()) continue;
  87. ++$count;
  88. }
  89. return $count;
  90. }
  91. /**
  92. * @return int
  93. * @throws Exception
  94. */
  95. public function getFailCount(): int {
  96. $count = 0;
  97. $tests = $this->getTests();
  98. foreach($tests as $test) {
  99. if(!$test->isFail()) continue;
  100. ++$count;
  101. }
  102. return $count;
  103. }
  104. /**
  105. * @return int
  106. * @throws Exception
  107. */
  108. public function getPendingCount(): int {
  109. $count = 0;
  110. $tests = $this->getTests();
  111. foreach($tests as $test) {
  112. if(!$test->isPending()) continue;
  113. ++$count;
  114. }
  115. return $count;
  116. }
  117. /**
  118. * @return string
  119. * @throws Exception
  120. */
  121. public function getPassRate(): string {
  122. $pass_count = $this->getPassCount();
  123. $fail_count = $this->getFailCount();
  124. $pass_fail_count = $pass_count + $fail_count;
  125. if($pass_fail_count == 0) return "0%";
  126. return (number_format($pass_count / $pass_fail_count, 4) * 100) . "%";
  127. }
  128. /**
  129. * @return Location[]
  130. * @throws Exception
  131. */
  132. public function getLocations(): array {
  133. $tests = $this->getTests();
  134. return Location::query(["id" => array_column($tests, "location_id")]);
  135. }
  136. /**
  137. * @return string
  138. */
  139. public function getDisplayAgeRange(): string {
  140. if($this->min_age && $this->max_age) {
  141. return $this->min_age . " to " . $this->max_age;
  142. }
  143. else if($this->min_age) {
  144. return "At least " . $this->min_age;
  145. }
  146. else if($this->max_age) {
  147. return "Up to " . $this->max_age;
  148. }
  149. return "No age restriction";
  150. }
  151. /**
  152. * @return string
  153. */
  154. public function getNameHtml(): string {
  155. if($this->isAnytime()) {
  156. return htmlspecialchars($this->name);
  157. }
  158. $icon = $this->getTimeOfDayIcon();
  159. $time_of_day = $this->getTimeOfDayText();
  160. return htmlspecialchars($this->name) . " <i class=\"fa fa-fw fa-{$icon}\" data-toggle=\"tooltip\" title=\"{$time_of_day}\"></i>";
  161. }
  162. /**
  163. * @return float
  164. * @throws Exception
  165. */
  166. public function getCompletedPercentage(): float {
  167. $test_count = $this->test_count;
  168. $tests = $this->getTests();
  169. $completed_tests = array_filter($tests, function($test) {
  170. return $test->status == Test::STATUS_COMPLETED;
  171. });
  172. $completed_test_count = count($completed_tests);
  173. return floor(($completed_test_count / $test_count) * 100);
  174. }
  175. /**
  176. * @return Template
  177. * @throws Exception
  178. */
  179. public function getTemplate(): ?Template {
  180. return Template::get($this->template_id);
  181. }
  182. /**
  183. * @return array
  184. */
  185. public function getTemplateOverrides(): array {
  186. $overrides = json_decode($this->template_overrides_json) ?: [];
  187. return $overrides;
  188. }
  189. /**
  190. * @return string
  191. */
  192. public function getTimeOfDayText(): string {
  193. switch($this->time_of_day) {
  194. default:
  195. case self::TOD_ANYTIME: return "Any";
  196. case self::TOD_DAYTIME: return "Day";
  197. case self::TOD_NIGHTTIME: return "Night";
  198. }
  199. }
  200. /**
  201. * @return string
  202. */
  203. public function getTimeOfDayIcon(): string {
  204. switch($this->time_of_day) {
  205. default:
  206. case self::TOD_ANYTIME: return "adjust";
  207. case self::TOD_DAYTIME: return "sun";
  208. case self::TOD_NIGHTTIME: return "moon";
  209. }
  210. }
  211. /**
  212. * @return string
  213. */
  214. public function getURL(): string {
  215. return "/account/projects/{$this->id}";
  216. }
  217. /**
  218. * @return string
  219. */
  220. public function getDirectory(): string {
  221. $directory = ROOT_DIR . "/files/projects/" . $this->id;
  222. if(!file_exists($directory)) mkdir($directory, 0777, true);
  223. return $directory;
  224. }
  225. /**
  226. * @return bool
  227. */
  228. public function hasInvoicePDF(): bool {
  229. $directory = $this->getDirectory();
  230. return file_exists($directory . "/invoice.pdf");
  231. }
  232. /**
  233. * @return bool
  234. */
  235. public function isAnytime(): bool {
  236. return $this->time_of_day == self::TOD_ANYTIME;
  237. }
  238. /**
  239. * @return bool
  240. */
  241. public function isDaytime(): bool {
  242. return $this->time_of_day == self::TOD_DAYTIME;
  243. }
  244. /**
  245. * @return bool
  246. */
  247. public function isNighttime(): bool {
  248. return $this->time_of_day == self::TOD_NIGHTTIME;
  249. }
  250. /**
  251. * @return bool
  252. */
  253. public function isPastDeadline(): bool {
  254. $deadline_unix = Util::getUnixTimeFromUTC($this->dts_deadline);
  255. return time() > $deadline_unix;
  256. }
  257. /**
  258. * @return bool
  259. */
  260. public function isFinished(): bool {
  261. return $this->dts_finished === null;
  262. }
  263. /**
  264. * @return bool
  265. * @throws Exception
  266. */
  267. public function markFinished(): bool {
  268. if(!Core::$user) throw new Exception("Trying to mark project as finished while not logged in.");
  269. if(!Core::$user->isAdmin()) throw new Exception("Trying to mark project as finished with non-admin account.");
  270. $this->dts_finished = Util::getUTCDateTime();
  271. $this->user_id_finished = Core::$user->id;
  272. return $this->commit();
  273. }
  274. /**
  275. * @return bool
  276. * @throws Exception
  277. */
  278. public function markUnfinished(): bool {
  279. $this->dts_finished = null;
  280. $this->user_id_finished = null;
  281. return $this->commit();
  282. }
  283. /**
  284. * @return string
  285. * @throws Exception
  286. */
  287. public function getDisplayReportMonth(): string {
  288. $report_month = "-";
  289. if($this->dts_report) {
  290. $dt_report = new DateTime($this->dts_report . " UTC");
  291. $report_month = $dt_report->format("M Y");
  292. }
  293. return $report_month;
  294. }
  295. /**
  296. * @return array
  297. * @throws Exception
  298. */
  299. public function getDataTableRow(): array {
  300. $tests = $this->getTests();
  301. return [
  302. "id" => (int) $this->id,
  303. "rowId" => "project-row-{$this->id}",
  304. "name" => $this->name,
  305. "testCount" => count($tests),
  306. "timeOfDay" => $this->getTimeOfDayText(),
  307. "unixStart" => Util::getUnixTimeFromUTC($this->dts_start),
  308. "unixEnd" => Util::getUnixTimeFromUTC($this->dts_deadline),
  309. "reportMonth" => $this->getDisplayReportMonth(),
  310. "unixReport" => $this->dts_report ? Util::getUnixTimeFromUTC($this->dts_report) : 0,
  311. ];
  312. }
  313. /**
  314. * @return string
  315. * @throws Exception
  316. */
  317. public function getTestResultsUrl(): string {
  318. $customer = $this->getCustomer();
  319. $customer_slug = Util::slugWithId($customer->name, $customer->id);
  320. $project_slug = Util::slugWithId($this->name, $this->id);
  321. return "/account/test-results/{$customer_slug}/{$project_slug}";
  322. }
  323. /**
  324. * @return Invoice
  325. * @throws Exception
  326. */
  327. public function getCustomerInvoice(): Invoice {
  328. $invoice = new Invoice;
  329. $invoice->setReference($this->getInvoiceReference());
  330. $test_line_items = $this->getTestInvoiceLineItems();
  331. $field_line_items = $this->getFieldInvoiceLineItems();
  332. $invoice->addLineItems($test_line_items);
  333. $invoice->addLineItems($field_line_items);
  334. return $invoice;
  335. }
  336. /**
  337. * @return string
  338. * @throws Exception
  339. */
  340. private function getInvoiceReference(): string {
  341. $customer = $this->getCustomer();
  342. return
  343. "P" . str_pad($this->id, 3, "0", STR_PAD_LEFT) .
  344. "/C" . str_pad($customer->id, 3, "0", STR_PAD_LEFT);
  345. }
  346. /**
  347. * @return InvoiceLineItem[]
  348. * @throws Exception
  349. */
  350. private function getTestInvoiceLineItems(): array {
  351. /**
  352. * @var InvoiceLineItem[] $items
  353. */
  354. $template = $this->getTemplate();
  355. $default_customer_fee = $this->default_customer_fee;
  356. $tests = $this->getTests();
  357. $items = [];
  358. foreach($tests as $test) {
  359. $fee = $test->customer_fee ?? $default_customer_fee;
  360. $key = "fee_{$fee}";
  361. $item = $items[$key] ?? null;
  362. if(!$item) {
  363. $item = new InvoiceLineItem(__("%s Testing", $template->name));
  364. $item->setPrice($fee);
  365. }
  366. $item->incrementQuantity();
  367. $items[$key] = $item;
  368. }
  369. return array_values($items);
  370. }
  371. /**
  372. * @return InvoiceLineItem[]
  373. * @throws Exception
  374. */
  375. private function getFieldInvoiceLineItems(): array {
  376. $template = $this->getTemplate();
  377. $invoice_fields = $template->getCustomerInvoiceFields();
  378. $tests = $this->getTests();
  379. $items = [];
  380. foreach($tests as $test) {
  381. $values = $test->getValues(true);
  382. foreach($invoice_fields as $field) {
  383. $item = $items[$field->name] ?? (new InvoiceLineItem($field->label));
  384. $item->addPrice((float) $values[$field->name] ?? 0);
  385. $items[$field->name] = $item;
  386. }
  387. }
  388. return $items;
  389. }
  390. }