project_customer_csv.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. $has_alcohol = $config->hasAlcohol ?? false;
  21. if($has_alcohol) {
  22. $field_names[0] = "Gaming Test Result";
  23. array_splice($field_names, 1, 0, ["Alcohol Test Result"]);
  24. array_splice($field_names, 3, 0, ["Gaming Void", "Alcohol Void"]);
  25. }
  26. foreach($fields as $field) {
  27. $customer_export = $field->customerExport ?? true;
  28. if(!$customer_export) {
  29. continue;
  30. }
  31. $field_names[] = $field->label;
  32. }
  33. fputcsv($handle, $field_names);
  34. foreach($tests as $test) {
  35. $project = $test->getProject();
  36. $location = $test->getLocation();
  37. $tester = $test->getTester();
  38. $values = $test->getValues(true);
  39. $total_test_count = count($location->getCompletedTests());
  40. $current_test_count = count($location->getCompletedTests(["dts_start" => $year_cycle_constraint]));
  41. $test_date = "";
  42. $test_start = "";
  43. $test_end = "";
  44. $test_duration = "";
  45. $gaming_result = "N/A";
  46. $alcohol_result = "N/A";
  47. $age = "";
  48. $gender = "";
  49. $travel_costs = "";
  50. $output = [];
  51. if($test->pass !== null) {
  52. $test_start_dt = new DateTime($test->dts_start);
  53. $test_end_dt = new DateTime($test->dts_end);
  54. $test_duration_diff = $test_start_dt->diff($test_end_dt, true);
  55. $test_date = $test_start_dt->format("d/m/Y");
  56. $test_start = $test_start_dt->format("H:i:s");
  57. $test_end = $test_end_dt->format("H:i:s");
  58. $test_duration = $test_duration_diff->h . ":" . $test_duration_diff->i . ":" . $test_duration_diff->s;
  59. if($tester) {
  60. $tester_dob_dt = new DateTime($tester->date_of_birth);
  61. $dob_diff = $test_start_dt->diff($tester_dob_dt, true);
  62. $age = $dob_diff->y;
  63. $gender = $tester->gender;
  64. $travel_costs = $test->test_travel_costs;
  65. }
  66. }
  67. $test_values = $test->getValues(true);
  68. if($test->isVoid()) {
  69. $gaming_void = "Y";
  70. $alcohol_void = "Y";
  71. $gaming_result = "Void";
  72. $alcohol_result = "Void";
  73. }
  74. else {
  75. $gaming_void = $test->void_primary ? "Y" : "N";
  76. if($test->void_primary) {
  77. $gaming_result = "Void";
  78. }
  79. elseif($test->pass !== null) {
  80. $gaming_result = $test->pass ? "Pass" : "Fail";
  81. }
  82. $alcohol_void = $test->void_secondary ? "Y" : "N";
  83. if($test->void_secondary) {
  84. $alcohol_result = "Void";
  85. }
  86. elseif($test->pass !== null && $has_alcohol) {
  87. $alcohol_purchased = (bool) ($test_values["alcohol_purchased"] ?? false);
  88. $alcohol_pass = ($alcohol_purchased === false);
  89. $alcohol_result = $alcohol_pass ? "Pass" : "Fail";
  90. }
  91. }
  92. $output[] = $gaming_result;
  93. if($has_alcohol) {
  94. $output[] = $alcohol_result;
  95. }
  96. $output[] = $test->isVoid() ? "Y" : "N";
  97. if($has_alcohol) {
  98. $output[] = $gaming_void;
  99. $output[] = $alcohol_void;
  100. }
  101. $output[] = $location->name;
  102. $output[] = $location->getDisplayAddress();
  103. $output[] = $location->brand;
  104. $output[] = $location->region;
  105. $output[] = $location->internal_id;
  106. $output[] = $location->area_manager;
  107. $output[] = $test_date;
  108. $output[] = $total_test_count;
  109. $output[] = $current_test_count;
  110. foreach($fields as $field) {
  111. $customer_export = $field->customerExport ?? true;
  112. if(!$customer_export) continue;
  113. $field_name = $field->name;
  114. $value = $test->$field_name ?? ($values[$field->name] ?? "");
  115. if(!empty($field->displayStrings)) {
  116. $index = array_search($value, $field->values);
  117. $value = $field->displayStrings[$index];
  118. }
  119. $output[] = $value;
  120. }
  121. fputcsv($handle, $output);
  122. }