PageRenderTime 59ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/application/modules/lab_charges/models/lab_charges_model.php

https://bitbucket.org/marttkip/alexandria-main
PHP | 446 lines | 359 code | 60 blank | 27 comment | 10 complexity | 56cefc5c10139c599e364fffdbac3910 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. class Lab_charges_model extends CI_Model
  3. {
  4. public function get_all_test_list($table, $where, $per_page, $page, $order = 'lab_test_name', $order_method = 'ASC')
  5. {
  6. //retrieve all users
  7. $this->db->from($table);
  8. $this->db->select('*');
  9. $this->db->where($where);
  10. //$this->db->group_by('service_charge.lab_test_id');
  11. $this->db->order_by($order, $order_method);
  12. $query = $this->db->get('', $per_page, $page);
  13. return $query;
  14. }
  15. public function get_all_test_classes($table, $where, $per_page, $page, $order = NULL)
  16. {
  17. //retrieve all users
  18. $this->db->from($table);
  19. $this->db->select('*');
  20. $this->db->where($where);
  21. $this->db->order_by('lab_test_class.lab_test_class_id','ASC');
  22. $query = $this->db->get('', $per_page, $page);
  23. return $query;
  24. }
  25. public function add_classes()
  26. {
  27. $class_name = $this->input->post('class_name');
  28. // check if this class name exisit
  29. $check_rs = $this->check_class($class_name);
  30. if(count($check_rs) > 0)
  31. {
  32. return FALSE;
  33. }
  34. else
  35. {
  36. $insert = array(
  37. "lab_test_class_name" => $class_name
  38. );
  39. $this->database->insert_entry('lab_test_class', $insert);
  40. return TRUE;
  41. }
  42. // end of checking
  43. }
  44. public function edit_lab_test_class($class_id)
  45. {
  46. $class_name = $this->input->post('class_name');
  47. $insert = array(
  48. "lab_test_class_name" => $class_name
  49. );
  50. $this->db->where('lab_test_class_id',$class_id);
  51. $this->db->update('lab_test_class', $insert);
  52. return TRUE;
  53. // end of checking
  54. }
  55. public function check_class($class_name)
  56. {
  57. $table = "lab_test_class";
  58. $where = "lab_test_class_name = '$class_name'";
  59. $items = "*";
  60. $order = "lab_test_class_id";
  61. $result = $this->database->select_entries_where($table, $where, $items, $order);
  62. return $result;
  63. }
  64. public function get_lab_classes()
  65. {
  66. $this->db->order_by('lab_test_class_name');
  67. $query = $this->db->get('lab_test_class');
  68. return $query;
  69. }
  70. public function add_lab_test()
  71. {
  72. $lab_test_class_id = $this->input->post('lab_test_class_id');
  73. $lab_test_name = $this->input->post('lab_test_name');
  74. $units = $this->input->post('units');
  75. $price = $this->input->post('price');
  76. $lab_positive_status = $this->input->post('lab_positive_status');
  77. $male_upper_limit = $this->input->post('male_upper_limit');
  78. $male_lower_limit = $this->input->post('male_lower_limit');
  79. $female_upper_limit = $this->input->post('female_upper_limit');
  80. $female_lower_limit = $this->input->post('female_lower_limit');
  81. // check if this class name exisit
  82. $check_rs = $this->check_lab_test($lab_test_class_id,$lab_test_name);
  83. if(count($check_rs) > 0)
  84. {
  85. return FALSE;
  86. }
  87. else
  88. {
  89. $insert = array(
  90. "lab_test_name" => $lab_test_name,
  91. "lab_test_class_id" => $lab_test_class_id,
  92. "lab_test_price" => $price,
  93. "lab_test_units" => $units,
  94. "lab_positive_status" => $lab_positive_status,
  95. "lab_test_malelowerlimit" => $male_lower_limit,
  96. "lab_test_malelupperlimit" => $male_upper_limit,
  97. "lab_test_femalelowerlimit" => $female_lower_limit,
  98. "lab_test_femaleupperlimit" => $female_upper_limit
  99. );
  100. $this->db->insert('lab_test', $insert);
  101. return TRUE;
  102. }
  103. // end of checking
  104. }
  105. public function get_all_visit_type()
  106. {
  107. $this->db->select('*');
  108. $query = $this->db->get('visit_type');
  109. return $query;
  110. }
  111. public function get_laboratory_service_id()
  112. {
  113. $this->db->where('(service_name = "Lab" OR service_name = "lab" OR service_name = "Laboratory" OR service_name = "laboratory") AND branch_code = "'.$this->session->userdata('branch_code').'"');
  114. $query = $this->db->get('service');
  115. if($query->num_rows() > 0)
  116. {
  117. foreach ($query->result() as $key) {
  118. # code...
  119. $service_id = $key->service_id;
  120. }
  121. }else
  122. {
  123. // get the department id from the department table called laboratory
  124. // insert and return the service id
  125. $department_id = $this->get_department_id();
  126. $data = array('service_name'=>'Laboratory','branch_code'=>$this->session->userdata('branch_code'),'service_status'=>1,'report_distinct'=>1,'service_status'=>1,'created'=>date('Y-m-d H:i:s'),'created_by'=>$this->session->userdata('personnel_id'));
  127. $this->db->insert('service',$data);
  128. $service_id = $this->db->insert_id();
  129. }
  130. return $service_id;
  131. }
  132. public function get_department_id()
  133. {
  134. $this->db->where('(department_name = "Lab" OR department_name = "lab" OR department_name = "Laboratory" OR department_name = "laboratory") AND branch_code = "'.$this->session->userdata('branch_code').'"');
  135. $query = $this->db->get('departments');
  136. if($query->num_rows() > 0)
  137. {
  138. foreach ($query->result() as $key) {
  139. # code...
  140. $department_id = $key->department_id;
  141. }
  142. }else
  143. {
  144. $data = array('department_name'=>'Laboratory','branch_code'=>$this->session->userdata('branch_code'),'department_status'=>1,'created'=>date('Y-m-d H:i:s'),'created_by'=>$this->session->userdata('personnel_id'));
  145. $this->db->insert('departments',$data);
  146. $department_id = $this->db->insert_id();
  147. }
  148. return $department_id;
  149. }
  150. public function add_test_format($test_id)
  151. {
  152. $lab_test_format = $this->input->post('lab_test_format');
  153. $units = $this->input->post('units');
  154. $male_upper_limit = $this->input->post('male_upper_limit');
  155. $male_lower_limit = $this->input->post('male_lower_limit');
  156. $female_upper_limit = $this->input->post('female_upper_limit');
  157. $female_lower_limit = $this->input->post('female_lower_limit');
  158. // check if this class name exisit
  159. $check_rs = $this->check_lab_testformat($test_id,$lab_test_format);
  160. if(count($check_rs) > 0)
  161. {
  162. return FALSE;
  163. }
  164. else
  165. {
  166. $insert = array(
  167. "lab_test_formatname" => $lab_test_format,
  168. "lab_test_id" => $test_id,
  169. "lab_test_format_units" => $units,
  170. "lab_test_format_malelowerlimit" => $male_lower_limit,
  171. "lab_test_format_maleupperlimit" => $male_upper_limit,
  172. "lab_test_format_femalelowerlimit" => $female_lower_limit,
  173. "lab_test_format_femaleupperlimit" => $female_upper_limit
  174. );
  175. $this->database->insert_entry('lab_test_format', $insert);
  176. return TRUE;
  177. }
  178. // end of checking
  179. }
  180. public function check_lab_testformat($test_id,$lab_test_format)
  181. {
  182. $table = "lab_test_format";
  183. $where = "lab_test_id = '$test_id' AND lab_test_formatname = '$lab_test_format'";
  184. $items = "*";
  185. $order = "lab_test_format_id";
  186. $result = $this->database->select_entries_where($table, $where, $items, $order);
  187. return $result;
  188. }
  189. public function check_lab_test($lab_test_class_id,$lab_test_name)
  190. {
  191. $table = "lab_test";
  192. $where = "lab_test_class_id = '$lab_test_class_id' AND lab_test_name = '$lab_test_name'";
  193. $items = "*";
  194. $order = "lab_test_class_id";
  195. $result = $this->database->select_entries_where($table, $where, $items, $order);
  196. return $result;
  197. }
  198. public function get_lab_test_details($test_id)
  199. {
  200. $this->db->from('lab_test');
  201. $this->db->select('*');
  202. $this->db->where('lab_test_id = \''.$test_id.'\'');
  203. $query = $this->db->get();
  204. return $query;
  205. }
  206. public function get_lab_test_format_details($format_id)
  207. {
  208. $this->db->from('lab_test_format');
  209. $this->db->select('*');
  210. $this->db->where('lab_test_format_id = \''.$format_id.'\'');
  211. $query = $this->db->get();
  212. return $query;
  213. }
  214. public function edit_lab_test($test_id)
  215. {
  216. $lab_test_class_id = $this->input->post('lab_test_class_id');
  217. $lab_test_name = $this->input->post('lab_test_name');
  218. $units = $this->input->post('units');
  219. $price = $this->input->post('price');
  220. $male_upper_limit = $this->input->post('male_upper_limit');
  221. $male_lower_limit = $this->input->post('male_lower_limit');
  222. $female_upper_limit = $this->input->post('female_upper_limit');
  223. $female_lower_limit = $this->input->post('female_lower_limit');
  224. $lab_positive_status = $this->input->post('lab_positive_status');
  225. $insert = array(
  226. "lab_test_name" => $lab_test_name,
  227. "lab_test_class_id" => $lab_test_class_id,
  228. "lab_test_price" => $price,
  229. "lab_test_units" => $units,
  230. "lab_test_malelowerlimit" => $male_lower_limit,
  231. "lab_test_malelupperlimit" => $male_upper_limit,
  232. "lab_test_femalelowerlimit" => $female_lower_limit,
  233. "lab_test_femaleupperlimit" => $female_upper_limit,
  234. "lab_positive_status" => $lab_positive_status,
  235. "is_synced" => 0
  236. );
  237. $this->db->where('lab_test_id', $test_id);
  238. $this->db->update('lab_test', $insert);
  239. return TRUE;
  240. // end of checking
  241. }
  242. public function edit_lab_test_format($test_id,$format_id)
  243. {
  244. $lab_test_format = $this->input->post('lab_test_format');
  245. $units = $this->input->post('units');
  246. $male_upper_limit = $this->input->post('male_upper_limit');
  247. $male_lower_limit = $this->input->post('male_lower_limit');
  248. $female_upper_limit = $this->input->post('female_upper_limit');
  249. $female_lower_limit = $this->input->post('female_lower_limit');
  250. $insert = array(
  251. "lab_test_formatname" => $lab_test_format,
  252. "lab_test_format_units" => $units,
  253. "lab_test_format_malelowerlimit" => $male_lower_limit,
  254. "lab_test_format_maleupperlimit" => $male_upper_limit,
  255. "lab_test_format_femalelowerlimit" => $female_lower_limit,
  256. "lab_test_format_femaleupperlimit" => $female_upper_limit
  257. );
  258. $this->db->where('lab_test_format_id', $format_id);
  259. $this->db->update('lab_test_format', $insert);
  260. return TRUE;
  261. }
  262. public function get_all_tests_formats($test_id)
  263. {
  264. $this->db->from('lab_test_format');
  265. $this->db->select('*');
  266. $this->db->where('lab_test_id = \''.$test_id.'\'');
  267. $query = $this->db->get();
  268. return $query;
  269. }
  270. function get_all_tests($class_id)
  271. {
  272. $this->db->from('lab_test');
  273. $this->db->select('*');
  274. $this->db->where('lab_test_class_id = \''.$class_id.'\'');
  275. $query = $this->db->get();
  276. return $query;
  277. }
  278. function get_class_details($class_id)
  279. {
  280. $this->db->from('lab_test_class');
  281. $this->db->select('*');
  282. $this->db->where('lab_test_class_id = \''.$class_id.'\'');
  283. $query = $this->db->get();
  284. return $query;
  285. }
  286. function get_tests_done($lab_test_id)
  287. {
  288. $where = 'service_charge.service_charge_id = visit_lab_test.service_charge_id AND service_charge.lab_test_id = \''.$lab_test_id.'\'';
  289. $search = $this->session->userdata('tests_report_search');
  290. if(!empty($search))
  291. {
  292. $where .= $search;
  293. }
  294. $this->db->where($where);
  295. $this->db->from('service_charge, visit_lab_test');
  296. $this->db->select('count(visit_lab_test_id) AS total_tests');
  297. //echo 'SELECT count(visit_charge_id) AS total_tests FROM service_charge, visit_charge WHERE '.$where;die();
  298. $query = $this->db->get();
  299. $total_tests = 0;
  300. if($query->num_rows() > 0)
  301. {
  302. $row = $query->row();
  303. $total_tests = $row->total_tests;
  304. }
  305. return $total_tests;
  306. }
  307. function get_tests_revenue($lab_test_id)
  308. {
  309. $where = 'visit_lab_test.visit_lab_test_id = visit_charge.visit_lab_test_id AND service_charge.service_charge_id = visit_lab_test.service_charge_id AND service_charge.lab_test_id = \''.$lab_test_id.'\'';
  310. //$where = 'service_charge.service_charge_id = visit_charge.service_charge_id AND service_charge.lab_test_id = \''.$lab_test_id.'\'';
  311. $search = $this->session->userdata('tests_report_search');
  312. if(!empty($search))
  313. {
  314. $where .= $search;
  315. }
  316. $this->db->where($where);
  317. $this->db->from('service_charge, visit_charge, visit_lab_test');
  318. $this->db->select('SUM(visit_charge_units * visit_charge_amount) AS total_test_revenue');
  319. $query = $this->db->get();
  320. $total_test_revenue = 0;
  321. if($query->num_rows() > 0)
  322. {
  323. $row = $query->row();
  324. $total_test_revenue = $row->total_test_revenue;
  325. }
  326. return $total_test_revenue;
  327. }
  328. public function export_results()
  329. {
  330. $this->load->library('excel');
  331. //get all transactions
  332. $where = 'lab_test_class.lab_test_class_id = lab_test.lab_test_class_id AND lab_test.lab_test_delete = 0';
  333. $table = 'lab_test,lab_test_class';
  334. $this->db->where($where);
  335. $this->db->order_by('lab_test_name', 'ASC');
  336. $visits_query = $this->db->get($table);
  337. $title = 'Laboratory test revenue export '.date('jS M Y H:i a',strtotime(date('Y-m-d H:i:s')));
  338. if($visits_query->num_rows() > 0)
  339. {
  340. $count = 0;
  341. /*
  342. -----------------------------------------------------------------------------------------
  343. Document Header
  344. -----------------------------------------------------------------------------------------
  345. */
  346. $row_count = 0;
  347. $report[$row_count][0] = '#';
  348. $report[$row_count][1] = 'Class';
  349. $report[$row_count][2] = 'Test';
  350. $report[$row_count][3] = 'Cash Price';
  351. $report[$row_count][4] = 'No. Done';
  352. $report[$row_count][5] = 'Revenue';
  353. //get & display all services
  354. //display all patient data in the leftmost columns
  355. foreach($visits_query->result() as $row)
  356. {
  357. $row_count++;
  358. $lab_test_class_id = $row->lab_test_class_id;
  359. $lab_test_class = $row->lab_test_class_name;
  360. $lab_test_name = $row->lab_test_name;
  361. $lab_test_units = $row->lab_test_units;
  362. $lab_test_price = $row->lab_test_price;
  363. $lab_test_malelowerlimit = $row->lab_test_malelowerlimit;
  364. $lab_test_malelupperlimit = $row->lab_test_malelupperlimit;
  365. $lab_test_femalelowerlimit = $row->lab_test_femalelowerlimit;
  366. $lab_test_femaleupperlimit = $row->lab_test_femaleupperlimit;
  367. $lab_test_delete = $row->lab_test_delete;
  368. $lab_test_id = $row->lab_test_id;
  369. $no_done = $this->lab_charges_model->get_tests_done($lab_test_id);
  370. $revenue = $this->lab_charges_model->get_tests_revenue($lab_test_id);
  371. $count++;
  372. //display the patient data
  373. $report[$row_count][0] = $count;
  374. $report[$row_count][1] = $lab_test_class;
  375. $report[$row_count][2] = $lab_test_name;
  376. $report[$row_count][3] = number_format($lab_test_price, 2);
  377. $report[$row_count][4] = number_format($no_done, 0);
  378. $report[$row_count][5] = number_format($revenue, 2);
  379. }
  380. }
  381. //create the excel document
  382. $this->excel->addArray ( $report );
  383. $this->excel->generateXML ($title);
  384. }
  385. }
  386. ?>