Guest User

Customer Class

a guest
Mar 6th, 2023
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 KB | None | 0 0
  1. // get customers
  2. public function getCustomers($user, $skip=0, $search=false, $asearch=false, $sort=false, $order=false){
  3. if($this->authorized){
  4.  
  5. $this->user_affiliation = $user->affiliation();
  6.  
  7. $customers = [];
  8. $collection = $this->db->Customer;
  9. $collection1 = $this->db->Customers;
  10.  
  11. // criteria
  12. $criteria = $this->initializeCustomersCriteria();
  13.  
  14. // New Criteria for getting student recently registered from past 48 hrs ('Added By: Ron Ivin Gregorio - 02/20/2023')
  15. if(!$search && !$asearch){
  16. array_push($criteria,['UDateCreated' => [ '$gte' => strtotime('-48 hours') , '$lt' => time()]]);
  17. }
  18.  
  19. // include search criteria if available
  20. if($search){
  21. array_push($criteria, $this->searchCustomers($search));
  22.  
  23. // include advanced search criteria if available
  24. } else if($asearch){
  25. $criteria = array_merge($criteria, $this->advancedSearchCustomers($asearch));
  26. }
  27.  
  28. if(!$user->super){
  29. $user_states = $user->states;
  30. if(in_array('zz', $user_states)){
  31. array_push($user_states, 'generic');
  32. }
  33. array_push($criteria, array("CustomerCoursesStates" => array('$in' => $user_states)));
  34. if($user->isCourtAdmin()){
  35. $courts_assigned = $user->getCourtsAssigned();
  36. if($courts_assigned){
  37. array_push($criteria, array("DLNumber" => array('$in' => $this->getCustomersByCourts($courts_assigned))));
  38. } else {
  39. array_push($criteria, array("DLNumber" => "no_courts"));
  40. }
  41. } else {
  42. if($user->isFleet()){
  43. $this->fleet_courses = $this->getFleetCourses($user);
  44. array_push($criteria, array("DLNumber" => array('$in' => $this->getFleetStudents())));
  45. } else {
  46. array_push($criteria, array("CoursesAffiliates" => array('$in' => [$this->user_affiliation['affiliate']])));
  47. if($this->user_affiliation['company'] != ''){
  48. array_push($criteria, array("CourseCompanies" => array('$in' => [$this->user_affiliation['company']])));
  49. }
  50. }
  51. }
  52. }
  53.  
  54. // finalize criteria
  55. $criteria = array('$and' => $criteria);
  56.  
  57. // include sorting if available
  58. $sort = $this->sortCustomers($sort, $order);
  59.  
  60. // select fields
  61. $select = ["_id" => 0, "ID" => 1, "DLNumber" => 1, "FirstName" => 1, "MiddleName" => 1, "LastName" => 1,
  62. "RecentRegUDate" => 1, "RecentRegDate" => 1, "Status" => 1];
  63.  
  64. // get records
  65. $cursor = $collection->find($criteria, $select);
  66. $this->total_count = $cursor->count() - 0;
  67. $this->total_pages = ceil($this->total_count / $this->page_size);
  68.  
  69. // Getting the total length of the customers in first iteration ('Added By: Ron Ivin Gregorio - 02/20/2023')
  70. $customer_length = 0;
  71. if($skip == 0){
  72. $customer_length = $this->total_count;
  73. }
  74. // Limit into 20 students only per load ('Added By: Ron Ivin Gregorio - 02/20/2023')
  75. $cursor->sort($sort)->skip($skip)->limit(20);
  76.  
  77.  
  78. foreach($cursor as $document){
  79. $registration = $this->getCustomerRegistrationDate($document);
  80. $registration_date = $registration ? $registration['date'] : false;
  81. $registration_time = $registration ? $registration['time'] : '';
  82. $student_ids = $this->getCustomerStudentIDs($document['DLNumber']);
  83. $customer_courses = implode(", ", $this->getCustomerCourses($student_ids));
  84. $customer_classes = $this->getCustomerClasses($student_ids);
  85. $affiliations = $this->getCustomerCourseAffiliations($student_ids);
  86. $affiliates = implode(", ", $affiliations['affiliates']);
  87. $companies = count($affiliations['companies']) > 0 ? $affiliations['companies'] : false;
  88. $companies = $companies ? implode(", ", $companies) : '-';
  89. $customer = [
  90. "id" => $document['ID'],
  91. "registration_date" => $registration_date,
  92. "registration_time" => $registration_time,
  93. "name" => $this->getCustomerFullnameLastFirst($document),
  94. "dlnum" => strstr($document['DLNumber'], "TEMP") ? '-' : $document['DLNumber'],
  95. "student_ids" => implode(", ", $student_ids),
  96. "courses" => $customer_courses,
  97. "classes" => $customer_classes ? implode(', ', $customer_classes) : '-',
  98. "affiliates" => $affiliates,
  99. "companies" => $companies,
  100. "status" => implode(", ", $this->getCustomerCoursesStatus($student_ids, $document['Status']))
  101. ];
  102. array_push($customers, $customer);
  103. }
  104.  
  105. // Adding customer length when returning data ('Added By: Ron Ivin Gregorio - 02/20/2023')
  106.  
  107. $data = [
  108. 'customers' => $customers,
  109. 'customer_length' => $customer_length
  110. ];
  111.  
  112. return $data;
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment