project.php 11 KB

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