project_form.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. /**
  3. * @var Project $project
  4. * @var Template[] $templates
  5. * @var Customer $customer
  6. * @var int[] $location_ids
  7. * @var Location[] $locations
  8. * @var Template $template
  9. */
  10. $user = Core::$user;
  11. // Get test counts for current year cycle
  12. $tests = Test::query([
  13. "status" => Constraint::notEquals(Test::STATUS_CANCELLED),
  14. "location_id" => array_column($locations, "id"),
  15. "dts_start" => $customer->getYearCycleConstraint(TIMEZONE),
  16. ]);
  17. $test_counts = [];
  18. foreach($tests as $test) {
  19. if(!isset($test_counts[$test->location_id])) {
  20. $test_counts[$test->location_id] = 0;
  21. }
  22. if($test->isCompleted()) {
  23. $test_counts[$test->location_id]++;
  24. }
  25. }
  26. $latest_tests = $customer->getLatestTestByLocation();
  27. $dt_start = $project->getDateTimeStart();
  28. $dt_deadline = $project->getDateTimeDeadline();
  29. $dt_report = $project->getDateTimeReport() ?: null;
  30. $date_report = $dt_report ? $dt_report->format("Y-m-d") : "";
  31. $date_invoice = $project->invoice_date;
  32. $name_readonly = $project->id ? "" : "readonly";
  33. $autogen_checked = $project->id ? "" : "checked";
  34. // Figure out which locations are selected
  35. $tests = [];
  36. $location_test = [];
  37. if($project->id) {
  38. $tests = $project->getTests();
  39. foreach($tests as $test) {
  40. $location_test[$test->location_id] = $test;
  41. }
  42. }
  43. $selected_locations = array_filter($locations, static function(Location $l) use ($location_ids) { return in_array($l->id, $location_ids, false); });
  44. $deselected_locations = array_filter($locations, static function(Location $l) use ($location_ids) { return !in_array($l->id, $location_ids, false); });
  45. /**
  46. * @param Location[] $locations
  47. * @param bool $selected
  48. * @return void
  49. */
  50. $output_locations = static function(array $locations, bool $selected) use ($location_test, $test_counts, $customer, $latest_tests, $user) {
  51. foreach($locations as $location) {
  52. $address = $location->getDisplayAddress(["address_1", "address_2", "city", "postal_code"]);
  53. $filter_parts = array_filter([
  54. $address, $location->name
  55. ]);
  56. $filter = strtolower(implode("", $filter_parts));
  57. $button_class = $selected ? "danger" : "success";
  58. $button_text = $selected ? "Deselect" : "Select";
  59. $icon = $selected ? "times" : "check";
  60. $test = $location_test[$location->id] ?? null;
  61. $test_status = $test_class = "";
  62. if($test) {
  63. $test_status = $test->getStatusText();
  64. $test_class = $test->getStatusClass();
  65. }
  66. ?>
  67. <div class="list-group-item" data-location-id="<?=$location->id?>" data-internal-id="<?=htmlspecialchars($location->internal_id)?>" data-name="<?=htmlspecialchars($location->name)?>" data-filter-value="<?=htmlspecialchars($filter)?>">
  68. <div class="row no-gutters">
  69. <div class="col-2 text-right">
  70. <?=$location->id?>
  71. </div>
  72. <div class="col hover-container px-3">
  73. <a href="/account/locations/<?=$location->id?>" target="_blank"><?=htmlspecialchars($location->name)?></a>
  74. <a class="visible-on-hover float-right" href="/account/locations/<?=$location->id?>/edit" target="_blank"><i class="fal fa-pencil"></i> Edit</a>
  75. <div>
  76. <?php
  77. if($test) {
  78. ?>
  79. <div class="badge badge-<?=$test_class?>">
  80. <?=htmlspecialchars($test_status)?>
  81. </div>
  82. <?php
  83. }
  84. if($location->internal_id) {
  85. ?>
  86. <span class="badge badge-primary">Internal ID: <?=htmlspecialchars($location->internal_id)?></span>
  87. <?php
  88. }
  89. ?>
  90. </div>
  91. <small><?=htmlspecialchars($address)?></small><br />
  92. <small>
  93. <strong>Last Test:</strong>
  94. <?php
  95. $test = $latest_tests[$location->id] ?? null;
  96. if($test) {
  97. $test->renderTestStatus($user);
  98. }
  99. else {
  100. ?>
  101. None
  102. <?php
  103. }
  104. ?>
  105. </small><br />
  106. <small title="<?=htmlspecialchars(__("Number of tests completed between %s", $customer->getDisplayYearCycle()))?>">
  107. <strong>Completed:</strong>
  108. <?=htmlspecialchars(sprintf("%d / %d", $test_counts[$location->id] ?? 0, $location->annual_test_count))?>
  109. </small>
  110. </div>
  111. <div class="col-auto align-self-center text-right">
  112. <button class="btn btn-outline-<?=$button_class?>" type="button">
  113. <i class="fas fa-<?=$icon?>"></i>
  114. <span class="sr-only"><?=htmlspecialchars($button_text)?></span>
  115. </button>
  116. </div>
  117. </div>
  118. </div>
  119. <?php
  120. }
  121. }
  122. ?>
  123. <div class="container mt-3 py-4">
  124. <h2>
  125. <?=($project->id ? "Edit" : "Add")?>
  126. Project for <span id="customer-name"><?=htmlspecialchars($customer->name)?></span>
  127. </h2>
  128. <input type="hidden" id="project-id" value="<?=$project->id?>" />
  129. <input type="hidden" id="initial-template-keys" value="<?=htmlspecialchars(json_encode($project->getTemplateKeys()))?>" />
  130. <form method="post" id="project-form">
  131. <h3>Project Details</h3>
  132. <div class="row">
  133. <div class="col-lg">
  134. <div class="row mb-1">
  135. <label class="col-sm-4 col-form-label" for="name">Name</label>
  136. <div class="col-sm">
  137. <input class="form-control" type="text" name="name" id="name" placeholder="Enter name" value="<?=htmlspecialchars($project->name)?>" required <?=$name_readonly?> />
  138. <div class="custom-control custom-switch">
  139. <input type="checkbox" class="custom-control-input" id="autogen-name" <?=$autogen_checked?> />
  140. <label class="custom-control-label" for="autogen-name">Auto-fill name</label>
  141. </div>
  142. </div>
  143. </div>
  144. <div class="row mb-1">
  145. <label class="col-sm-4 col-form-label" for="template-id">Template</label>
  146. <div class="col-sm">
  147. <select class="form-control" name="template_id" id="template-id" required>
  148. <?php
  149. foreach($templates as $template) {
  150. $selected = ((int) $template->id === (int) $project->template_id) ? "selected" : "";
  151. $keys = $template->getTemplateKeys();
  152. $keys_json = json_encode($keys);
  153. ?>
  154. <option value="<?=$template->id?>" data-keys="<?=htmlspecialchars($keys_json)?>" <?=$selected?>>
  155. <?=htmlspecialchars($template->name)?>
  156. </option>
  157. <?php
  158. }
  159. ?>
  160. </select>
  161. </div>
  162. </div>
  163. <div id="template-extra-fields-row" class="row mb-1 collapse">
  164. <div class="col-sm-4"></div>
  165. <div id="template-extra-fields-container" class="col-sm">
  166. </div>
  167. </div>
  168. <div class="row mb-1">
  169. <label class="col-sm-4 col-form-label" for="test-count">Test Count</label>
  170. <div class="col-sm">
  171. <input class="form-control" type="number" name="test_count" id="test-count" placeholder="Enter test count" value="<?=htmlspecialchars($project->test_count)?>" required />
  172. </div>
  173. </div>
  174. <div class="row mb-1">
  175. <label class="col-sm-4 col-form-label" for="default-customer-fee">Default Customer Fee</label>
  176. <div class="col-sm">
  177. <div class="input-group">
  178. <div class="input-group-prepend">
  179. <span class="input-group-text">£</span>
  180. </div>
  181. <input class="form-control" type="number" name="default_customer_fee" min=0" max="999.99" step="0.01" id="default-customer-fee" placeholder="Enter default customer fee" value="<?=htmlspecialchars($project->default_customer_fee)?>" required />
  182. </div>
  183. </div>
  184. </div>
  185. <div class="row mb-1">
  186. <label class="col-sm-4 col-form-label" for="default-retest-fee">Default Retest Fee</label>
  187. <div class="col-sm">
  188. <div class="input-group">
  189. <div class="input-group-prepend">
  190. <span class="input-group-text">£</span>
  191. </div>
  192. <input class="form-control" type="number" name="default_retest_fee" min=0" max="999.99" step="0.01" id="default-retest-fee" placeholder="Enter default retest fee" value="<?=htmlspecialchars($project->default_retest_fee)?>" />
  193. </div>
  194. </div>
  195. </div>
  196. <div class="row mb-1">
  197. <label class="col-sm-4 col-form-label" for="default-tester-fee">Default Tester Fee</label>
  198. <div class="col-sm">
  199. <div class="input-group">
  200. <div class="input-group-prepend">
  201. <span class="input-group-text">£</span>
  202. </div>
  203. <input class="form-control" type="number" name="default_tester_fee" min="0" max="999.99" step="0.01" id="default-tester-fee" placeholder="Enter default tester fee" value="<?=htmlspecialchars($project->default_tester_fee)?>" required />
  204. </div>
  205. </div>
  206. </div>
  207. <div class="row mb-1">
  208. <label class="col-sm-4 col-form-label" for="time-of-day">Time of Day</label>
  209. <div class="col-sm">
  210. <select class="form-control" name="time_of_day" id="time-of-day" required>
  211. <?php
  212. foreach(Project::getTimeOfDayOptions() as $value => $display) {
  213. $selected = ($value === $project->time_of_day) ? "selected" : "";
  214. ?>
  215. <option value="<?=$value?>" <?=$selected?>><?=htmlspecialchars($display)?></option>
  216. <?php
  217. }
  218. ?>
  219. </select>
  220. </div>
  221. </div>
  222. <div class="row mb-1">
  223. <label class="col-sm-4 col-form-label" for="tester-can-claim">Tester Can Claim</label>
  224. <div class="col-sm">
  225. <select class="form-control" name="tester_can_claim" id="tester-can-claim">
  226. <?php
  227. foreach(Form::BOOLEAN_OPTIONS as $value => $display) {
  228. $selected = ($value === (int) $project->tester_can_claim) ? "selected" : "";
  229. ?>
  230. <option value="<?=$value?>" <?=$selected?>><?=htmlspecialchars($display)?></option>
  231. <?php
  232. }
  233. ?>
  234. </select>
  235. <small class="form-text text-muted">Tests will appear in "Find Tests" for testers.</small>
  236. </div>
  237. </div>
  238. </div>
  239. <div class="col-lg">
  240. <div class="row mb-1">
  241. <label class="col-sm-4 col-form-label" for="min-age">Min. Age</label>
  242. <div class="col-sm">
  243. <input class="form-control" type="number" name="min_age" id="min-age" placeholder="Enter minimum age" value="<?=htmlspecialchars($project->min_age)?>" required />
  244. </div>
  245. </div>
  246. <div class="row mb-1">
  247. <label class="col-sm-4 col-form-label" for="max-age">Max. Age</label>
  248. <div class="col-sm">
  249. <input class="form-control" type="number" name="max_age" id="max-age" placeholder="Enter maximum age" value="<?=htmlspecialchars($project->max_age)?>" required />
  250. </div>
  251. </div>
  252. <div class="row mb-1">
  253. <label class="col-sm-4 col-form-label" for="date-start">Start Date</label>
  254. <div class="col-sm">
  255. <input class="form-control" type="date" name="date_start" id="date-start" value="<?=$dt_start->format("Y-m-d")?>" required />
  256. </div>
  257. </div>
  258. <div class="row mb-1">
  259. <label class="col-sm-4 col-form-label" for="date-deadline">Deadline</label>
  260. <div class="col-sm">
  261. <input class="form-control" type="date" name="date_deadline" id="date-deadline" value="<?=$dt_deadline->format("Y-m-d")?>" required />
  262. </div>
  263. </div>
  264. <div class="row mb-1">
  265. <label class="col-sm-4 col-form-label" for="date-report">Report Month</label>
  266. <div class="col-sm">
  267. <input class="form-control" type="date" name="date_report" id="date-report" value="<?=$date_report?>" />
  268. </div>
  269. </div>
  270. <div class="row mb-1">
  271. <label class="col-sm-4 col-form-label" for="date-invoice">Invoice Date</label>
  272. <div class="col-sm">
  273. <input class="form-control" type="date" name="date_invoice" id="date-invoice" value="<?=$date_invoice?>" />
  274. </div>
  275. </div>
  276. <div class="row mb-1">
  277. <label class="col-sm-4 col-form-label" for="browse-time">Browse Time (Mins)</label>
  278. <div class="col-sm">
  279. <input class="form-control" type="number" name="browse_time" id="browse-time" value="<?=$project->browse_time?>" min="0" placeholder="Enter browse time" required />
  280. </div>
  281. </div>
  282. <div class="row mb-1">
  283. <label class="col-sm-4 col-form-label" for="play-money">Play Money</label>
  284. <div class="col-sm">
  285. <input class="form-control" type="number" name="play_money" id="play-money" value="<?=$project->play_money?>" min="0" step="any" placeholder="Enter play money" />
  286. </div>
  287. </div>
  288. <div class="row mb-1">
  289. <label class="col-sm-4 col-form-label" for="play-time">Play Time (Mins)</label>
  290. <div class="col-sm">
  291. <input class="form-control" type="number" name="play_time" id="play-time" value="<?=$project->play_time?>" min="0" placeholder="Enter play time" required />
  292. </div>
  293. </div>
  294. </div>
  295. </div>
  296. <?php
  297. if($project->id) {
  298. ?>
  299. <hr />
  300. <div class="row">
  301. <div class="col">
  302. <h3>Template Overrides</h3>
  303. </div>
  304. <div class="col-auto">
  305. <a class="btn btn-outline-primary" href="/account/projects/<?=$project->id?>/edit/template-overrides">Edit</a>
  306. </div>
  307. </div>
  308. <?php
  309. $overrides = $project->getTemplateOverrides();
  310. if(empty($overrides)) {
  311. ?>
  312. <div class="alert alert-info">There are no template overrides configured for this project.</div>
  313. <?php
  314. }
  315. else {
  316. $fields = $template->getFields();
  317. ?>
  318. <table class="table table-bordered table-hover">
  319. <thead>
  320. <tr>
  321. <th>Field</th>
  322. <th>Element</th>
  323. <th>Value</th>
  324. </tr>
  325. </thead>
  326. <tbody>
  327. <?php
  328. foreach($overrides as $override) {
  329. ?>
  330. <tr>
  331. <td>
  332. <?=htmlspecialchars($fields[$override->field]->label)?>
  333. </td>
  334. <td>
  335. <?=ucfirst($override->element)?>
  336. </td>
  337. <td>
  338. <?=htmlspecialchars($override->value)?>
  339. </td>
  340. </tr>
  341. <?php
  342. }
  343. ?>
  344. </tbody>
  345. </table>
  346. <?php
  347. }
  348. }
  349. ?>
  350. <hr />
  351. <input type="hidden" name="location_ids" id="location-ids" value="<?=implode(", ", $location_ids)?>" />
  352. <h3>Project Locations</h3>
  353. <?php
  354. if($project->id) {
  355. ?>
  356. <div class="alert alert-warning">
  357. <div class="row">
  358. <div class="col-auto">
  359. <i class="fal fa-exclamation-triangle"></i>
  360. </div>
  361. <div class="col pl-0">
  362. Please be aware that deselecting a location will cancel the test for the project no matter its current status.<br />
  363. <strong>This action cannot be undone, you have been warned!</strong>
  364. </div>
  365. </div>
  366. </div>
  367. <?php
  368. }
  369. ?>
  370. <div class="row">
  371. <div class="col-md mb-3">
  372. <h4>Selected</h4>
  373. <div class="card">
  374. <div class="card-body">
  375. <div class="input-group">
  376. <div class="input-group-prepend">
  377. <label class="input-group-text" for="selected-filter">
  378. Filter
  379. </label>
  380. </div>
  381. <input class="form-control" id="selected-filter" type="text" placeholder="Enter name, address, ID or internal ID" data-filter-target="#selected-locations-list-group" />
  382. </div>
  383. <div class="alert alert-info mt-3 mb-0" id="selected-locations-alert">
  384. No locations selected for testing.
  385. </div>
  386. </div>
  387. <div class="list-group list-group-flush limit-height-2" id="selected-locations-list-group">
  388. <?php
  389. $output_locations($selected_locations, true);
  390. ?>
  391. </div>
  392. </div>
  393. </div>
  394. <div class="col-md mb-3">
  395. <h4>Not Selected</h4>
  396. <div class="card">
  397. <div class="card-body">
  398. <div class="input-group">
  399. <div class="input-group-prepend">
  400. <label class="input-group-text" for="not-selected-filter">
  401. Filter
  402. </label>
  403. </div>
  404. <input id="not-selected-filter" class="form-control" type="text" placeholder="Enter name, address, ID or internal ID" data-filter-target="#not-selected-locations-list-group" />
  405. </div>
  406. <div class="alert alert-info mt-3 mb-0" id="not-selected-locations-alert">
  407. No additional locations are available for testing.
  408. </div>
  409. </div>
  410. <div class="list-group list-group-flush limit-height-2" id="not-selected-locations-list-group">
  411. <?php
  412. $output_locations($deselected_locations, false);
  413. ?>
  414. </div>
  415. </div>
  416. </div>
  417. </div>
  418. <hr />
  419. <button class="btn btn-primary" type="submit">Submit</button>
  420. <input type="hidden" name="submit" value="<?=$_SESSION["csrf_token"]?>" />
  421. </form>
  422. </div>