Hardy/Finder/BedCapacity.php line 15

Open in your IDE?
  1. <?php
  2. namespace Hardy\Finder;
  3. class BedCapacity extends AbstractFinder
  4. {
  5.     public function getWardTypes()
  6.     {
  7.         $sql 'SELECT id, name FROM ward_types';
  8.         $this->connectPhc();
  9.         return $this->connPhc->fetchAllAssociative($sql);
  10.     }
  11.     public function getAllSummary(?string $type, ?string $level, ?string $departmentId)
  12.     {
  13.         $params = [];
  14.         $sql "SELECT d.id district_id, d.name district, SUM(w.male_num_bed_func + w.female_num_bed_func) available,
  15.                 COUNT(wb.id) occupied, COUNT(h.id) hospitals, SUM(bs.male_num_bed + bs.female_num_bed) iphs
  16.             FROM districts d
  17.             INNER JOIN hospitals h ON h.district_id = d.id
  18.             INNER JOIN wards w ON w.hospital_id = h.id
  19.             LEFT JOIN ward_beds wb ON wb.ward_id = w.id AND wb.status = 'Admitted'
  20.             INNER JOIN ward_types wt ON w.ward_type_id = wt.id
  21.             INNER JOIN iphs_bed_strength bs ON bs.ward_type_id = wt.id";
  22.         if (!empty($type) || !empty($level) || !empty($departmentId)) {
  23.             $sql .= ' WHERE 1 ';
  24.             if (!empty($type)) {
  25.                 $sql .= ' AND h.type = ?';
  26.                 $params[] = $type;
  27.             }
  28.             if (!empty($departmentId)) {
  29.                 $sql .= ' AND wt.id = ?';
  30.                 $params[] = $departmentId;
  31.             }
  32.         }
  33.         $sql .= ' GROUP BY d.id';
  34.         $this->connectPhc();
  35.         return $this->connPhc->fetchAllAssociative($sql$params);
  36.     }
  37.     public function getDetailsByDistrictId($districtId, ?string $type, ?string $level, ?string $department)
  38.     {
  39.         $params = [$districtId];
  40.         $sql "SELECT d.name, h.id, h.name facility, wt.name department, SUM(w.male_num_bed_func) avail_m, SUM(w.female_num_bed_func) avail_f,
  41.                 SUM(bs.male_num_bed) iphs_m, SUM(bs.female_num_bed) iphs_f,
  42.                 SUM(IF(p.gender = 'Male', 1, 0)) occ_m, SUM(IF(p.gender = 'Female', 1, 0)) occ_f
  43.             FROM hospitals h
  44.             INNER JOIN districts d ON h.district_id = d.id
  45.             INNER JOIN wards w ON w.hospital_id = h.id
  46.             INNER JOIN ward_types wt ON w.ward_type_id = wt.id
  47.             INNER JOIN iphs_bed_strength bs ON bs.ward_type_id = wt.id
  48.             LEFT JOIN ward_beds wb ON wb.ward_id = w.id AND wb.status = 'Admitted'
  49.             LEFT JOIN cases c ON wb.case_id = c.id
  50.             LEFT JOIN patients p ON c.patient_id = p.id
  51.             WHERE h.district_id = ?";
  52.         if (!empty($type) || !empty($level) || !empty($departmentId)) {
  53.             if (!empty($type)) {
  54.                 $sql .= ' AND h.type = ?';
  55.                 $params[] = $type;
  56.             }
  57.             if (!empty($departmentId)) {
  58.                 $sql .= ' AND wt.id = ?';
  59.                 $params[] = $departmentId;
  60.             }
  61.         }
  62.         $sql .= ' GROUP BY wt.id';
  63.         $this->connectPhc();
  64.         return $this->connPhc->fetchAllAssociative($sql$params);
  65.     }
  66.     public function getDetailsByHospitalId($hospitalId, ?string $type, ?string $level, ?string $department)
  67.     {
  68.         $sql "SELECT h.id, h.name facility, w.id ward_id, wt.name department, SUM(w.male_num_bed_func) avail_m, SUM(w.female_num_bed_func) avail_f,
  69.                 SUM(bs.male_num_bed) iphs_m, SUM(bs.female_num_bed) iphs_f,
  70.                 SUM(IF(p.gender = 'Male', 1, 0)) occ_m, SUM(IF(p.gender = 'Female', 1, 0)) occ_f
  71.             FROM hospitals h
  72.             INNER JOIN wards w ON w.hospital_id = h.id
  73.             INNER JOIN ward_types wt ON w.ward_type_id = wt.id
  74.             INNER JOIN iphs_bed_strength bs ON bs.ward_type_id = wt.id
  75.             LEFT JOIN ward_beds wb ON wb.ward_id = w.id AND wb.status = 'Admitted'
  76.             LEFT JOIN cases c ON wb.case_id = c.id
  77.             LEFT JOIN patients p ON c.patient_id = p.id
  78.             WHERE h.id = ?
  79.             GROUP BY wt.id";
  80.         $this->connectPhc();
  81.         return $this->connPhc->fetchAllAssociative($sql, [$hospitalId]);
  82.     }
  83.     public function getDetailsByDepartmentId($departmentId)
  84.     {
  85.         $sql "SELECT h.name facility, h.phone, h.address, v.name village, w.id ward_id, wt.name department,
  86.                 sp.first_name, sp.last_name, sp.mobile, SUM(w.male_num_bed_func + w.female_num_bed_func) sanc,
  87.                 SUM(IF((DATE(wb.created_at) = CURDATE() - INTERVAL 1 DAY OR DATE(wb.updated_at) = CURDATE() - INTERVAL 1 DAY) AND wb.status = 'Admitted', 1, 0)) yesterday,
  88.                 SUM(IF(DATE(wb.created_at) = CURDATE() AND wb.status = 'Admitted', 1, 0)) today,
  89.                 SUM(IF(DATE(wb.updated_at) = CURDATE() AND wb.status = 'Discharged', 1, 0)) discharged,
  90.                 SUM(IF(wb.status = 'Admitted', 1, 0)) occupied
  91.             FROM wards w
  92.             INNER JOIN hospitals h ON w.hospital_id = h.id
  93.             INNER JOIN villages v ON h.village_id = v.id
  94.             INNER JOIN ward_types wt ON w.ward_type_id = wt.id
  95.             LEFT JOIN ward_beds wb ON wb.ward_id = w.id
  96.             LEFT JOIN cases c ON wb.case_id = c.id
  97.             LEFT JOIN users u ON c.doctor_id = u.id
  98.             LEFT JOIN staff_profiles sp ON u.profile_id = sp.id
  99.             WHERE w.id = ?
  100.             GROUP BY w.id";
  101.         $this->connectPhc();
  102.         return $this->connPhc->fetchAllAssociative($sql, [$departmentId]);
  103.     }
  104. }