1
0

project_customer_csv.php 2.5 KB

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