project_customer_csv.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. /**
  3. * @var Template $template
  4. * @var object[] $fields
  5. * @var resource $handle
  6. * @var Test[] $tests
  7. * @var Constraint $year_cycle_constraint
  8. */
  9. $config = $template->getConfiguration();
  10. $field_names = [
  11. "Test Result", "Test Void",
  12. "Location Name", "Address", "Brand", "Region", "Internal ID", "Area Manager",
  13. "Date",
  14. "All-Time Test Count", "Current Year Period Test Count"
  15. ];
  16. if($config->hasAlcohol ?? false) {
  17. array_splice($field_names, 2, 0, ["Gaming Void", "Alcohol Void"]);
  18. }
  19. foreach($fields as $field) {
  20. $customer_export = $field->customerExport ?? true;
  21. if(!$customer_export) {
  22. continue;
  23. }
  24. $field_names[] = $field->label;
  25. }
  26. fputcsv($handle, $field_names);
  27. foreach($tests as $test) {
  28. $project = $test->getProject();
  29. $location = $test->getLocation();
  30. $tester = $test->getTester();
  31. $values = $test->getValues(true);
  32. $total_test_count = count($location->getCompletedTests());
  33. $current_test_count = count($location->getCompletedTests(["dts_start" => $year_cycle_constraint]));
  34. $test_date = "";
  35. $test_start = "";
  36. $test_end = "";
  37. $test_duration = "";
  38. $result = "Pending";
  39. $age = "";
  40. $gender = "";
  41. $travel_costs = "";
  42. $output = [];
  43. if($test->pass !== null) {
  44. $result = $test->pass ? "Pass" : "Fail";
  45. $test_start_dt = new DateTime($test->dts_start);
  46. $test_end_dt = new DateTime($test->dts_end);
  47. $test_duration_diff = $test_start_dt->diff($test_end_dt, true);
  48. $test_date = $test_start_dt->format("d/m/Y");
  49. $test_start = $test_start_dt->format("H:i:s");
  50. $test_end = $test_end_dt->format("H:i:s");
  51. $test_duration = $test_duration_diff->h . ":" . $test_duration_diff->i . ":" . $test_duration_diff->s;
  52. if($tester) {
  53. $tester_dob_dt = new DateTime($tester->date_of_birth);
  54. $dob_diff = $test_start_dt->diff($tester_dob_dt, true);
  55. $age = $dob_diff->y;
  56. $gender = $tester->gender;
  57. $travel_costs = $test->test_travel_costs;
  58. }
  59. }
  60. if($test->isVoid()) {
  61. $gaming_void = "Y";
  62. $alcohol_void = "Y";
  63. }
  64. else {
  65. $gaming_void = $test->void_primary ? "Y" : "N";
  66. $alcohol_void = $test->void_secondary ? "Y" : "N";
  67. }
  68. $output[] = $result;
  69. $output[] = $test->isVoid() ? "Y" : "N";
  70. if($config->hasAlcohol ?? false) {
  71. $output[] = $gaming_void;
  72. $output[] = $alcohol_void;
  73. }
  74. $output[] = $location->name;
  75. $output[] = $location->getDisplayAddress();
  76. $output[] = $location->brand;
  77. $output[] = $location->region;
  78. $output[] = $location->internal_id;
  79. $output[] = $location->area_manager;
  80. $output[] = $test_date;
  81. $output[] = $total_test_count;
  82. $output[] = $current_test_count;
  83. foreach($fields as $field) {
  84. $customer_export = $field->customerExport ?? true;
  85. if(!$customer_export) continue;
  86. $field_name = $field->name;
  87. $value = $test->$field_name ?? ($values[$field->name] ?? "");
  88. if(!empty($field->displayStrings)) {
  89. $index = array_search($value, $field->values);
  90. $value = $field->displayStrings[$index];
  91. }
  92. $output[] = $value;
  93. }
  94. fputcsv($handle, $output);
  95. }