project_customer_csv.php 3.1 KB

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