project.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <?php
  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. * @method Project get(int $id)
  24. */
  25. class Project extends Model {
  26. const TOD_ANYTIME = "A";
  27. const TOD_DAYTIME = "D";
  28. const TOD_NIGHTTIME = "N";
  29. protected static $table = "project";
  30. protected static $id_field = "id";
  31. 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"];
  32. /**
  33. * @var Test[] $tests
  34. */
  35. protected $tests = null;
  36. // Static methods
  37. public static function exportCSV($processing_file, $projects = []) {
  38. // Get processing file
  39. $processing_file_path = ROOT_DIR . "/export_processing/{$processing_file}";
  40. if(!file_exists($processing_file_path)) throw new Exception("Processing file not found.");
  41. // Output
  42. HTTP::setDownloadHeaders("export.csv");
  43. $handle = fopen("php://output", "w");
  44. require $processing_file_path;
  45. fclose($handle);
  46. }
  47. // Object methods
  48. /**
  49. * @return Customer
  50. */
  51. public function getCustomer() {
  52. return Customer::get($this->customer_id);
  53. }
  54. /**
  55. * @return Test[]
  56. */
  57. public function getTests() {
  58. if(!is_null($this->tests)) return $this->tests;
  59. $nvp = [];
  60. $nvp["project_id"] = $this->id;
  61. $nvp["status"] = (new Constraint("MORE_THAN"))->setValues(Test::STATUS_CANCELLED);
  62. $this->tests = Test::query($nvp);
  63. return $this->tests;
  64. }
  65. public function getTestCount() {
  66. if(is_null($this->tests)) $this->getTests();
  67. return count($this->tests);
  68. }
  69. public function getPassCount() {
  70. $count = 0;
  71. $tests = $this->getTests();
  72. foreach($tests as $test) {
  73. if(!$test->isPass()) continue;
  74. ++$count;
  75. }
  76. return $count;
  77. }
  78. public function getFailCount() {
  79. $count = 0;
  80. $tests = $this->getTests();
  81. foreach($tests as $test) {
  82. if(!$test->isFail()) continue;
  83. ++$count;
  84. }
  85. return $count;
  86. }
  87. public function getPendingCount() {
  88. $count = 0;
  89. $tests = $this->getTests();
  90. foreach($tests as $test) {
  91. if(!$test->isPending()) continue;
  92. ++$count;
  93. }
  94. return $count;
  95. }
  96. public function getPassRate(): string {
  97. $pass_count = $this->getPassCount();
  98. $fail_count = $this->getFailCount();
  99. $pass_fail_count = $pass_count + $fail_count;
  100. if($pass_fail_count == 0) return "0%";
  101. return (number_format($pass_count / $pass_fail_count, 4) * 100) . "%";
  102. }
  103. public function getLocations() {
  104. $tests = $this->getTests();
  105. return Location::query(["id" => array_column($tests, "location_id")]);
  106. }
  107. public function getDisplayAgeRange() {
  108. if($this->min_age && $this->max_age) {
  109. return $this->min_age . " to " . $this->max_age;
  110. }
  111. else if($this->min_age) {
  112. return "At least " . $this->min_age;
  113. }
  114. else if($this->max_age) {
  115. return "Up to " . $this->max_age;
  116. }
  117. return "No age restriction";
  118. }
  119. public function getNameHtml(): string {
  120. if($this->isAnytime()) {
  121. return htmlspecialchars($this->name);
  122. }
  123. $icon = $this->getTimeOfDayIcon();
  124. $time_of_day = $this->getTimeOfDayText();
  125. return htmlspecialchars($this->name) . " <i class=\"fa fa-fw fa-{$icon}\" data-toggle=\"tooltip\" title=\"{$time_of_day}\"></i>";
  126. }
  127. public function getCompletedPercentage() {
  128. $test_count = $this->test_count;
  129. $tests = $this->getTests();
  130. $completed_tests = array_filter($tests, function($test) {
  131. return $test->status == Test::STATUS_COMPLETED;
  132. });
  133. $completed_test_count = count($completed_tests);
  134. return floor(($completed_test_count / $test_count) * 100);
  135. }
  136. /**
  137. * @return Template
  138. */
  139. public function getTemplate() {
  140. return Template::get($this->template_id);
  141. }
  142. public function getTemplateOverrides() {
  143. $overrides = json_decode($this->template_overrides_json) ?: [];
  144. return $overrides;
  145. }
  146. public function getTimeOfDayText() {
  147. switch($this->time_of_day) {
  148. case self::TOD_ANYTIME: return "Any";
  149. case self::TOD_DAYTIME: return "Day";
  150. case self::TOD_NIGHTTIME: return "Night";
  151. }
  152. }
  153. public function getTimeOfDayIcon() {
  154. switch($this->time_of_day) {
  155. case self::TOD_ANYTIME: return "adjust";
  156. case self::TOD_DAYTIME: return "sun";
  157. case self::TOD_NIGHTTIME: return "moon";
  158. }
  159. }
  160. public function getURL() {
  161. return "/account/projects/{$this->id}";
  162. }
  163. public function getDirectory() {
  164. $directory = ROOT_DIR . "/files/projects/" . $this->id;
  165. if(!file_exists($directory)) mkdir($directory, 0777, true);
  166. return $directory;
  167. }
  168. public function hasInvoicePDF() {
  169. $directory = $this->getDirectory();
  170. return file_exists($directory . "/invoice.pdf");
  171. }
  172. public function isAnytime() {
  173. return $this->time_of_day == self::TOD_ANYTIME;
  174. }
  175. public function isDaytime() {
  176. return $this->time_of_day == self::TOD_DAYTIME;
  177. }
  178. public function isNighttime() {
  179. return $this->time_of_day == self::TOD_NIGHTTIME;
  180. }
  181. public function isPastDeadline() {
  182. $deadline_unix = Util::getUnixTimeFromUTC($this->dts_deadline);
  183. return time() > $deadline_unix;
  184. }
  185. public function isFinished() {
  186. return $this->dts_finished === null;
  187. }
  188. public function markFinished() {
  189. if(!Core::$user) throw new Exception("Trying to mark project as finished while not logged in.");
  190. if(!Core::$user->isAdmin()) throw new Exception("Trying to mark project as finished with non-admin account.");
  191. $this->dts_finished = Util::getUTCDateTime();
  192. $this->user_id_finished = Core::$user->id;
  193. return $this->commit();
  194. }
  195. public function markUnfinished() {
  196. $this->dts_finished = null;
  197. $this->user_id_finished = null;
  198. return $this->commit();
  199. }
  200. public function getDisplayReportMonth() {
  201. $report_month = "-";
  202. if($this->dts_report) {
  203. $dt_report = new DateTime($this->dts_report . " UTC");
  204. $report_month = $dt_report->format("M Y");
  205. }
  206. return $report_month;
  207. }
  208. public function getDataTableRow() {
  209. $tests = $this->getTests();
  210. return [
  211. "id" => (int) $this->id,
  212. "rowId" => "project-row-{$this->id}",
  213. "name" => $this->name,
  214. "testCount" => count($tests),
  215. "timeOfDay" => $this->getTimeOfDayText(),
  216. "unixStart" => Util::getUnixTimeFromUTC($this->dts_start),
  217. "unixEnd" => Util::getUnixTimeFromUTC($this->dts_end),
  218. "reportMonth" => $this->getDisplayReportMonth(),
  219. "unixReport" => $this->dts_report ? Util::getUnixTimeFromUTC($this->dts_report) : 0,
  220. ];
  221. }
  222. public function getTestResultsUrl(): string {
  223. $customer = $this->getCustomer();
  224. $customer_slug = Util::slugWithId($customer->name, $customer->id);
  225. $project_slug = Util::slugWithId($this->name, $this->id);
  226. return "/account/test-results/{$customer_slug}/{$project_slug}";
  227. }
  228. public function getCustomerInvoice(): Invoice {
  229. $invoice = new Invoice;
  230. $invoice->setReference($this->getInvoiceReference());
  231. $test_line_items = $this->getTestInvoiceLineItems();
  232. $field_line_items = $this->getFieldInvoiceLineItems();
  233. $invoice->addLineItems($test_line_items);
  234. $invoice->addLineItems($field_line_items);
  235. return $invoice;
  236. }
  237. private function getInvoiceReference(): string {
  238. $customer = $this->getCustomer();
  239. return
  240. "P" . str_pad($this->id, 3, "0", STR_PAD_LEFT) .
  241. "/C" . str_pad($customer->id, 3, "0", STR_PAD_LEFT);
  242. }
  243. private function getTestInvoiceLineItems(): array {
  244. /**
  245. * @var InvoiceLineItem[] $items
  246. */
  247. $template = $this->getTemplate();
  248. $default_customer_fee = $this->default_customer_fee;
  249. $tests = $this->getTests();
  250. $items = [];
  251. foreach($tests as $test) {
  252. $fee = $test->customer_fee ?? $default_customer_fee;
  253. $item = $items[$fee] ?? null;
  254. if(!$item) {
  255. $item = new InvoiceLineItem(__("%s Testing", $template->name));
  256. $item->setPrice($fee);
  257. }
  258. $item->incrementQuantity();
  259. $items[$fee] = $item;
  260. }
  261. return array_values($items);
  262. }
  263. private function getFieldInvoiceLineItems(): array {
  264. $template = $this->getTemplate();
  265. $invoice_fields = $template->getCustomerInvoiceFields();
  266. $tests = $this->getTests();
  267. $items = [];
  268. foreach($tests as $test) {
  269. $values = $test->getValues(true);
  270. foreach($invoice_fields as $field) {
  271. $item = $items[$field->name] ?? (new InvoiceLineItem($field->label));
  272. $item->addPrice((float) $values[$field->name] ?? 0);
  273. $items[$field->name] = $item;
  274. }
  275. }
  276. return $items;
  277. }
  278. }