· 9 years ago · Dec 27, 2016, 03:30 AM
1<?php
2
3if (!defined('BASEPATH'))
4 exit('No direct script access allowed');
5
6class Crud_model extends CI_Model {
7
8 function __construct() {
9 parent::__construct();
10 }
11
12 function clear_cache() {
13 $this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
14 $this->output->set_header('Pragma: no-cache');
15 }
16
17 function get_type_name_by_id($type, $type_id = '', $field = 'name') {
18 $this->db->where($type . '_id', $type_id);
19 $query = $this->db->get($type);
20 $result = $query->result_array();
21 foreach ($result as $row)
22 return $row[$field];
23 //return $this->db->get_where($type,array($type.'_id'=>$type_id))->row()->$field;
24 }
25
26
27
28 // Create a new invoice.
29 function create_invoice()
30 {
31 $data['title'] = $this->input->post('title');
32 $data['invoice_number'] = $this->input->post('invoice_number');
33 $data['patient_id'] = $this->input->post('patient_id');
34 $data['creation_timestamp'] = $this->input->post('creation_timestamp');
35 $data['due_timestamp'] = $this->input->post('due_timestamp');
36 $data['vat_percentage'] = $this->input->post('vat_percentage');
37 $data['discount_amount'] = $this->input->post('discount_amount');
38 $data['status'] = $this->input->post('status');
39
40 $invoice_entries = array();
41 $descriptions = $this->input->post('entry_description');
42 $amounts = $this->input->post('entry_amount');
43 $number_of_entries = sizeof($descriptions);
44
45 for ($i = 0; $i < $number_of_entries; $i++)
46 {
47 if ($descriptions[$i] != "" && $amounts[$i] != "")
48 {
49 $new_entry = array('description' => $descriptions[$i], 'amount' => $amounts[$i]);
50 array_push($invoice_entries, $new_entry);
51 }
52 }
53 $data['invoice_entries'] = json_encode($invoice_entries);
54
55 $this->db->insert('invoice', $data);
56 }
57
58 function select_invoice_info()
59 {
60 return $this->db->get('invoice')->result_array();
61 }
62
63 function select_invoice_info_by_patient_id()
64 {
65 $patient_id = $this->session->userdata('login_user_id');
66 return $this->db->get_where('invoice', array('patient_id' => $patient_id))->result_array();
67 }
68
69 function update_invoice($invoice_id)
70 {
71 $data['title'] = $this->input->post('title');
72 $data['invoice_number'] = $this->input->post('invoice_number');
73 $data['patient_id'] = $this->input->post('patient_id');
74 $data['creation_timestamp'] = $this->input->post('creation_timestamp');
75 $data['due_timestamp'] = $this->input->post('due_timestamp');
76 $data['vat_percentage'] = $this->input->post('vat_percentage');
77 $data['discount_amount'] = $this->input->post('discount_amount');
78 $data['status'] = $this->input->post('status');
79
80 $invoice_entries = array();
81 $descriptions = $this->input->post('entry_description');
82 $amounts = $this->input->post('entry_amount');
83 $number_of_entries = sizeof($descriptions);
84
85 for ($i = 0; $i < $number_of_entries; $i++)
86 {
87 if ($descriptions[$i] != "" && $amounts[$i] != "")
88 {
89 $new_entry = array('description' => $descriptions[$i], 'amount' => $amounts[$i]);
90 array_push($invoice_entries, $new_entry);
91 }
92 }
93 $data['invoice_entries'] = json_encode($invoice_entries);
94
95 $this->db->where('invoice_id', $invoice_id);
96 $this->db->update('invoice', $data);
97 }
98
99 function delete_invoice($invoice_id)
100 {
101 $this->db->where('invoice_id', $invoice_id);
102 $this->db->delete('invoice');
103 }
104
105 function calculate_invoice_total_amount($invoice_number)
106 {
107 $total_amount = 0;
108 $invoice = $this->db->get_where('invoice', array('invoice_number' => $invoice_number))->result_array();
109 foreach ($invoice as $row)
110 {
111 $invoice_entries = json_decode($row['invoice_entries']);
112 foreach ($invoice_entries as $invoice_entry)
113 $total_amount += $invoice_entry->amount;
114
115 $vat_amount = $total_amount * $row['vat_percentage'] / 100;
116 $grand_total = $total_amount + $vat_amount - $row['discount_amount'];
117 }
118
119 return $grand_total;
120 }
121
122
123
124 //////system settings//////
125 function update_system_settings() {
126 $data['description'] = $this->input->post('system_name');
127 $this->db->where('type', 'system_name');
128 $this->db->update('settings', $data);
129
130 $data['description'] = $this->input->post('system_title');
131 $this->db->where('type', 'system_title');
132 $this->db->update('settings', $data);
133
134 $data['description'] = $this->input->post('address');
135 $this->db->where('type', 'address');
136 $this->db->update('settings', $data);
137
138 $data['description'] = $this->input->post('phone');
139 $this->db->where('type', 'phone');
140 $this->db->update('settings', $data);
141
142 $data['description'] = $this->input->post('paypal_email');
143 $this->db->where('type', 'paypal_email');
144 $this->db->update('settings', $data);
145
146 $data['description'] = $this->input->post('currency');
147 $this->db->where('type', 'currency');
148 $this->db->update('settings', $data);
149
150 $data['description'] = $this->input->post('system_email');
151 $this->db->where('type', 'system_email');
152 $this->db->update('settings', $data);
153
154 $data['description'] = $this->input->post('buyer');
155 $this->db->where('type', 'buyer');
156 $this->db->update('settings', $data);
157
158 $data['description'] = $this->input->post('system_name');
159 $this->db->where('type', 'system_name');
160 $this->db->update('settings', $data);
161
162 $data['description'] = $this->input->post('purchase_code');
163 $this->db->where('type', 'purchase_code');
164 $this->db->update('settings', $data);
165
166 $data['description'] = $this->input->post('language');
167 $this->db->where('type', 'language');
168 $this->db->update('settings', $data);
169
170 $data['description'] = $this->input->post('text_align');
171 $this->db->where('type', 'text_align');
172 $this->db->update('settings', $data);
173 }
174
175 // SMS settings.
176 function update_sms_settings() {
177
178 $data['description'] = $this->input->post('clickatell_user');
179 $this->db->where('type', 'clickatell_user');
180 $this->db->update('settings', $data);
181
182 $data['description'] = $this->input->post('clickatell_password');
183 $this->db->where('type', 'clickatell_password');
184 $this->db->update('settings', $data);
185
186 $data['description'] = $this->input->post('clickatell_api_id');
187 $this->db->where('type', 'clickatell_api_id');
188 $this->db->update('settings', $data);
189 }
190
191 /////creates log/////
192 function create_log($data) {
193 $data['timestamp'] = strtotime(date('Y-m-d') . ' ' . date('H:i:s'));
194 $data['ip'] = $_SERVER["REMOTE_ADDR"];
195 $location = new SimpleXMLElement(file_get_contents('http://freegeoip.net/xml/' . $_SERVER["REMOTE_ADDR"]));
196 $data['location'] = $location->City . ' , ' . $location->CountryName;
197 $this->db->insert('log', $data);
198 }
199
200 ////////BACKUP RESTORE/////////
201 function create_backup($type) {
202 $this->load->dbutil();
203
204
205 $options = array(
206 'format' => 'txt', // gzip, zip, txt
207 'add_drop' => TRUE, // Whether to add DROP TABLE statements to backup file
208 'add_insert' => TRUE, // Whether to add INSERT data to backup file
209 'newline' => "\n" // Newline character used in backup file
210 );
211
212
213 if ($type == 'all') {
214 $tables = array('');
215 $file_name = 'system_backup';
216 } else {
217 $tables = array('tables' => array($type));
218 $file_name = 'backup_' . $type;
219 }
220
221 $backup = & $this->dbutil->backup(array_merge($options, $tables));
222
223
224 $this->load->helper('download');
225 force_download($file_name . '.sql', $backup);
226 }
227
228 /////////RESTORE TOTAL DB/ DB TABLE FROM UPLOADED BACKUP SQL FILE//////////
229 function restore_backup() {
230 move_uploaded_file($_FILES['userfile']['tmp_name'], 'uploads/backup.sql');
231 $this->load->dbutil();
232
233
234 $prefs = array(
235 'filepath' => 'uploads/backup.sql',
236 'delete_after_upload' => TRUE,
237 'delimiter' => ';'
238 );
239 $restore = & $this->dbutil->restore($prefs);
240 unlink($prefs['filepath']);
241 }
242
243 /////////DELETE DATA FROM TABLES///////////////
244 function truncate($type) {
245 if ($type == 'all') {
246 $this->db->truncate('student');
247 $this->db->truncate('mark');
248 $this->db->truncate('teacher');
249 $this->db->truncate('subject');
250 $this->db->truncate('class');
251 $this->db->truncate('exam');
252 $this->db->truncate('grade');
253 } else {
254 $this->db->truncate($type);
255 }
256 }
257
258 ////////IMAGE URL//////////
259 function get_image_url($type = '', $id = '') {
260 if (file_exists('uploads/' . $type . '_image/' . $id . '.jpg'))
261 $image_url = base_url() . 'uploads/' . $type . '_image/' . $id . '.jpg';
262 else
263 $image_url = base_url() . 'uploads/user.jpg';
264
265 return $image_url;
266 }
267
268 function save_department_info()
269 {
270 $data['name'] = $this->input->post('name');
271 $data['description'] = $this->input->post('description');
272
273 $this->db->insert('department',$data);
274 }
275
276 function select_department_info()
277 {
278 return $this->db->get('department')->result_array();
279 }
280
281 function update_department_info($department_id)
282 {
283 $data['name'] = $this->input->post('name');
284 $data['description'] = $this->input->post('description');
285
286 $this->db->where('department_id',$department_id);
287 $this->db->update('department',$data);
288 }
289
290 function delete_department_info($department_id)
291 {
292 $this->db->where('department_id',$department_id);
293 $this->db->delete('department');
294 }
295
296 function save_doctor_info()
297 {
298 $data['name'] = $this->input->post('name');
299 $data['email'] = $this->input->post('email');
300 $data['password'] = sha1($this->input->post('password'));
301 $data['address'] = $this->input->post('address');
302 $data['phone'] = $this->input->post('phone');
303 $data['department_id'] = $this->input->post('department_id');
304 $data['profile'] = $this->input->post('profile');
305
306 $this->db->insert('doctor',$data);
307
308 $doctor_id = $this->db->insert_id();
309 move_uploaded_file($_FILES["image"]["tmp_name"], "uploads/doctor_image/" . $doctor_id . '.jpg');
310 }
311
312 function select_doctor_info()
313 {
314 return $this->db->get('doctor')->result_array();
315 }
316
317 function update_doctor_info($doctor_id)
318 {
319 $data['name'] = $this->input->post('name');
320 $data['email'] = $this->input->post('email');
321 $data['address'] = $this->input->post('address');
322 $data['phone'] = $this->input->post('phone');
323 $data['department_id'] = $this->input->post('department_id');
324 $data['profile'] = $this->input->post('profile');
325
326 $this->db->where('doctor_id',$doctor_id);
327 $this->db->update('doctor',$data);
328
329 move_uploaded_file($_FILES["image"]["tmp_name"], "uploads/doctor_image/" . $doctor_id . '.jpg');
330 }
331
332 function delete_doctor_info($doctor_id)
333 {
334 $this->db->where('doctor_id',$doctor_id);
335 $this->db->delete('doctor');
336 }
337
338 function save_patient_info()
339 {
340
341 $data['name'] = $this->input->post('name');
342 $data['identitas'] = $this->input->post('identitas');
343 $data['RM'] = $this->input->post('RM');
344 $data['email'] = $this->input->post('email');
345 $data['password'] = sha1($this->input->post('password'));
346 $data['address'] = $this->input->post('address');
347 $data['phone'] = $this->input->post('phone');
348 $data['sex'] = $this->input->post('sex');
349 $data['birth_date'] = strtotime($this->input->post('birth_date'));
350 $data['age'] = $this->input->post('age');
351 $data['agama'] = $this->input->post('agama');
352 $data['status_kawin'] = $this->input->post('status_kawin');
353 $data['blood_group'] = $this->input->post('blood_group');
354 $data['jenis_pasien'] = $this->input->post('jenis_pasien');
355 $data['nokar'] = $this->input->post('nokar');
356 $data['pj'] = $this->input->post('pj');
357 $data['no_pj'] = $this->input->post('no_pj');
358
359 $this->db->insert('patient',$data);
360
361 $patient_id = $this->db->insert_id();
362 move_uploaded_file($_FILES["image"]["tmp_name"], "uploads/patient_image/" . $patient_id . '.jpg');
363 }
364
365 function select_patient_info()
366 {
367 return $this->db->get('patient')->result_array();
368 }
369
370 function select_patient_info_by_patient_id( $patient_id = '' )
371 {
372 return $this->db->get_where('patient', array('patient_id' => $patient_id))->result_array();
373 }
374
375 function update_patient_info($patient_id)
376 {
377
378 $data['name'] = $this->input->post('name');
379 $data['identitas'] = $this->input->post('identitas');
380 $data['RM'] = $this->input->post('RM');
381 $data['email'] = $this->input->post('email');
382 $data['password'] = sha1($this->input->post('password'));
383 $data['address'] = $this->input->post('address');
384 $data['phone'] = $this->input->post('phone');
385 $data['sex'] = $this->input->post('sex');
386 $data['birth_date'] = strtotime($this->input->post('birth_date'));
387 $data['age'] = $this->input->post('age');
388 $data['agama'] = $this->input->post('agama');
389 $data['status_kawin'] = $this->input->post('status_kawin');
390 $data['blood_group'] = $this->input->post('blood_group');
391 $data['Jenis_Pasien'] = $this->input->post('Jenis_Pasien');
392 $data['nokar'] = $this->input->post('nokar');
393 $data['pj'] = $this->input->post('pj');
394 $data['no_pj'] = $this->input->post('no_pj');
395
396
397 $this->db->where('patient_id',$patient_id);
398 $this->db->update('patient',$data);
399
400 move_uploaded_file($_FILES["image"]["tmp_name"], "uploads/patient_image/" . $patient_id . '.jpg');
401 }
402
403 function delete_patient_info($patient_id)
404 {
405 $this->db->where('patient_id',$patient_id);
406 $this->db->delete('patient');
407 }
408
409 function save_nurse_info()
410 {
411 $data['name'] = $this->input->post('name');
412 $data['email'] = $this->input->post('email');
413 $data['password'] = sha1($this->input->post('password'));
414 $data['address'] = $this->input->post('address');
415 $data['phone'] = $this->input->post('phone');
416
417 $this->db->insert('nurse',$data);
418
419 $nurse_id = $this->db->insert_id();
420 move_uploaded_file($_FILES["image"]["tmp_name"], "uploads/nurse_image/" . $nurse_id . '.jpg');
421 }
422
423 function select_nurse_info()
424 {
425 return $this->db->get('nurse')->result_array();
426 }
427
428 function update_nurse_info($nurse_id)
429 {
430 $data['name'] = $this->input->post('name');
431 $data['email'] = $this->input->post('email');
432 $data['address'] = $this->input->post('address');
433 $data['phone'] = $this->input->post('phone');
434
435 $this->db->where('nurse_id',$nurse_id);
436 $this->db->update('nurse',$data);
437
438 move_uploaded_file($_FILES["image"]["tmp_name"], "uploads/nurse_image/" . $nurse_id . '.jpg');
439 }
440
441 function delete_nurse_info($nurse_id)
442 {
443 $this->db->where('nurse_id',$nurse_id);
444 $this->db->delete('nurse');
445 }
446
447 function save_pharmacist_info()
448 {
449 $data['name'] = $this->input->post('name');
450 $data['email'] = $this->input->post('email');
451 $data['password'] = sha1($this->input->post('password'));
452 $data['address'] = $this->input->post('address');
453 $data['phone'] = $this->input->post('phone');
454
455 $this->db->insert('pharmacist',$data);
456
457 $pharmacist_id = $this->db->insert_id();
458 move_uploaded_file($_FILES["image"]["tmp_name"], "uploads/pharmacist_image/" . $pharmacist_id . '.jpg');
459 }
460
461
462
463 function save_poliklinik_info()
464 {
465 $data['nama'] = $this->input->post('nama');
466 $data['pj'] = $this->input->post('pj');
467 $this->db->insert('polikinik',$data);
468 }
469
470 function save_rawat_info($patient_id)
471 {
472 $data['NORJ'] = $this->input->post('NORJ');
473 $data['RM'] = $this->input->post('RM');
474 $data['NAMA'] = $this->input->post('NAMA');
475 $data['PEMBAYARAN'] = $this->input->post('PEMBAYARAN');
476 $data['KARTU'] = $this->input->post('KARTU');
477 $data['TGL_SEP'] = $this->input->post('TGL_SEP');
478 $data['NO_SEP'] = $this->input->post('NO_SEP');
479 $data['FASKES'] = $this->input->post('FASKES');
480 $data['KELAS'] = $this->input->post('KELAS');
481 $data['RUJUKAN'] = $this->input->post('RUJUKAN');
482 $data['TGL_RUJUK'] = $this->input->post('TGL_RUJUK');
483 $data['POLI'] = $this->input->post('POLI');
484 $data['DIAGNOSA'] = $this->input->post('DIAGNOSA');
485 $data['CATATAN'] = $this->input->post('CATATAN');
486 $data['STATUS'] = 'Belum Dilayani';
487 $this->db->where('patient_id',$patient_id);
488 $this->db->insert('rawatjalan',$data);
489 }
490
491
492
493 function kode_jalan_info(){
494
495 $q=$this->db->query("select MAX(RIGHT(RM,2)) as code_max from patient");
496 $code="";
497 if($q->$this->num_rows()>0){
498 foreach ($q->result() as $p){
499 $tmp=((int)$p->code_max)+1;
500 $code=sprintf("%03s",$tmp);
501 }
502 }
503 else{
504 $code="01";
505 }
506 return"P".$code;
507 }
508
509
510
511
512
513 function select_pharmacist_info()
514 {
515 return $this->db->get('pharmacist')->result_array();
516 }
517
518 function update_pharmacist_info($pharmacist_id)
519 {
520 $data['name'] = $this->input->post('name');
521 $data['email'] = $this->input->post('email');
522 $data['address'] = $this->input->post('address');
523 $data['phone'] = $this->input->post('phone');
524
525 $this->db->where('pharmacist_id',$pharmacist_id);
526 $this->db->update('pharmacist',$data);
527
528 move_uploaded_file($_FILES["image"]["tmp_name"], "uploads/pharmacist_image/" . $pharmacist_id . '.jpg');
529 }
530
531 function delete_pharmacist_info($pharmacist_id)
532 {
533 $this->db->where('pharmacist_id',$pharmacist_id);
534 $this->db->delete('pharmacist');
535 }
536
537 function save_laboratorist_info()
538 {
539 $data['name'] = $this->input->post('name');
540 $data['email'] = $this->input->post('email');
541 $data['password'] = sha1($this->input->post('password'));
542 $data['address'] = $this->input->post('address');
543 $data['phone'] = $this->input->post('phone');
544
545 $this->db->insert('laboratorist',$data);
546
547 $laboratorist_id = $this->db->insert_id();
548 move_uploaded_file($_FILES["image"]["tmp_name"], "uploads/laboratorist_image/" . $laboratorist_id . '.jpg');
549 }
550
551 function select_laboratorist_info()
552 {
553 return $this->db->get('laboratorist')->result_array();
554 }
555
556 function update_laboratorist_info($laboratorist_id)
557 {
558 $data['name'] = $this->input->post('name');
559 $data['email'] = $this->input->post('email');
560 $data['address'] = $this->input->post('address');
561 $data['phone'] = $this->input->post('phone');
562
563 $this->db->where('laboratorist_id',$laboratorist_id);
564 $this->db->update('laboratorist',$data);
565
566 move_uploaded_file($_FILES["image"]["tmp_name"], "uploads/laboratorist_image/" . $laboratorist_id . '.jpg');
567 }
568
569 function delete_laboratorist_info($laboratorist_id)
570 {
571 $this->db->where('laboratorist_id',$laboratorist_id);
572 $this->db->delete('laboratorist');
573 }
574
575 function save_accountant_info()
576 {
577 $data['name'] = $this->input->post('name');
578 $data['email'] = $this->input->post('email');
579 $data['password'] = sha1($this->input->post('password'));
580 $data['address'] = $this->input->post('address');
581 $data['phone'] = $this->input->post('phone');
582
583 $this->db->insert('accountant',$data);
584
585 $accountant_id = $this->db->insert_id();
586 move_uploaded_file($_FILES["image"]["tmp_name"], "uploads/accountant_image/" . $accountant_id . '.jpg');
587 }
588
589 function select_accountant_info()
590 {
591 return $this->db->get('accountant')->result_array();
592 }
593
594 function update_accountant_info($accountant_id)
595 {
596 $data['name'] = $this->input->post('name');
597 $data['email'] = $this->input->post('email');
598 $data['address'] = $this->input->post('address');
599 $data['phone'] = $this->input->post('phone');
600
601 $this->db->where('accountant_id',$accountant_id);
602 $this->db->update('accountant',$data);
603
604 move_uploaded_file($_FILES["image"]["tmp_name"], "uploads/accountant_image/" . $accountant_id . '.jpg');
605 }
606
607 function delete_accountant_info($accountant_id)
608 {
609 $this->db->where('accountant_id',$accountant_id);
610 $this->db->delete('accountant');
611 }
612
613 function save_receptionist_info()
614 {
615 $data['name'] = $this->input->post('name');
616 $data['email'] = $this->input->post('email');
617 $data['password'] = sha1($this->input->post('password'));
618 $data['address'] = $this->input->post('address');
619 $data['phone'] = $this->input->post('phone');
620
621 $this->db->insert('receptionist',$data);
622
623 $receptionist_id = $this->db->insert_id();
624 move_uploaded_file($_FILES["image"]["tmp_name"], "uploads/receptionist_image/" . $receptionist_id . '.jpg');
625 }
626
627 function select_receptionist_info()
628 {
629 return $this->db->get('receptionist')->result_array();
630 }
631
632 function update_receptionist_info($receptionist_id)
633 {
634 $data['name'] = $this->input->post('name');
635 $data['email'] = $this->input->post('email');
636 $data['address'] = $this->input->post('address');
637 $data['phone'] = $this->input->post('phone');
638
639 $this->db->where('receptionist_id',$receptionist_id);
640 $this->db->update('receptionist',$data);
641
642 move_uploaded_file($_FILES["image"]["tmp_name"], "uploads/receptionist_image/" . $receptionist_id . '.jpg');
643 }
644
645 function delete_receptionist_info($receptionist_id)
646 {
647 $this->db->where('receptionist_id',$receptionist_id);
648 $this->db->delete('receptionist');
649 }
650
651 function save_bed_allotment_info()
652 {
653 $data['bed_id'] = $this->input->post('bed_id');
654 $data['patient_id'] = $this->input->post('patient_id');
655 $data['allotment_timestamp'] = strtotime($this->input->post('allotment_timestamp'));
656 $data['discharge_timestamp'] = strtotime($this->input->post('discharge_timestamp'));
657
658 $this->db->insert('bed_allotment',$data);
659 }
660
661 function select_bed_allotment_info()
662 {
663 return $this->db->get('bed_allotment')->result_array();
664 }
665
666 function update_bed_allotment_info($bed_allotment_id)
667 {
668 $data['bed_id'] = $this->input->post('bed_id');
669 $data['patient_id'] = $this->input->post('patient_id');
670 $data['allotment_timestamp'] = strtotime($this->input->post('allotment_timestamp'));
671 $data['discharge_timestamp'] = strtotime($this->input->post('discharge_timestamp'));
672
673 $this->db->where('bed_allotment_id',$bed_allotment_id);
674 $this->db->update('bed_allotment',$data);
675 }
676
677 function delete_bed_allotment_info($bed_allotment_id)
678 {
679 $this->db->where('bed_allotment_id',$bed_allotment_id);
680 $this->db->delete('bed_allotment');
681 }
682
683 function select_blood_bank_info()
684 {
685 return $this->db->get('blood_bank')->result_array();
686 }
687
688 function update_blood_bank_info($blood_group_id)
689 {
690 $data['status'] = $this->input->post('status');
691
692 $this->db->where('blood_group_id',$blood_group_id);
693 $this->db->update('blood_bank',$data);
694 }
695
696 function save_report_info()
697 {
698 $data['type'] = $this->input->post('type');
699 $data['description'] = $this->input->post('description');
700 $data['timestamp'] = strtotime($this->input->post('timestamp'));
701 $data['patient_id'] = $this->input->post('patient_id');
702
703 $login_type = $this->session->userdata('login_type');
704 if($login_type=='nurse')
705 $data['doctor_id'] = $this->input->post('doctor_id');
706 else $data['doctor_id'] = $this->session->userdata('login_user_id');
707
708 $this->db->insert('report',$data);
709 }
710
711 function select_report_info()
712 {
713 return $this->db->get('report')->result_array();
714 }
715
716 function update_report_info($report_id)
717 {
718 $data['type'] = $this->input->post('type');
719 $data['description'] = $this->input->post('description');
720 $data['timestamp'] = strtotime($this->input->post('timestamp'));
721 $data['patient_id'] = $this->input->post('patient_id');
722
723 $login_type = $this->session->userdata('login_type');
724 if($login_type=='nurse')
725 $data['doctor_id'] = $this->input->post('doctor_id');
726 else $data['doctor_id'] = $this->session->userdata('login_user_id');
727
728 $this->db->where('report_id',$report_id);
729 $this->db->update('report',$data);
730 }
731
732 function delete_report_info($report_id)
733 {
734 $this->db->where('report_id',$report_id);
735 $this->db->delete('report');
736 }
737
738 function save_bed_info()
739 {
740 $data['bed_number'] = $this->input->post('bed_number');
741 $data['type'] = $this->input->post('type');
742 $data['description'] = $this->input->post('description');
743
744 $this->db->insert('bed',$data);
745 }
746
747 function select_bed_info()
748 {
749 return $this->db->get('bed')->result_array();
750 }
751
752 function update_bed_info($bed_id)
753 {
754 $data['bed_number'] = $this->input->post('bed_number');
755 $data['type'] = $this->input->post('type');
756 $data['description'] = $this->input->post('description');
757
758 $this->db->where('bed_id',$bed_id);
759 $this->db->update('bed',$data);
760 }
761
762 function delete_bed_info($bed_id)
763 {
764 $this->db->where('bed_id',$bed_id);
765 $this->db->delete('bed');
766 }
767
768 function save_blood_donor_info()
769 {
770 $data['name'] = $this->input->post('name');
771 $data['email'] = $this->input->post('email');
772 $data['address'] = $this->input->post('address');
773 $data['phone'] = $this->input->post('phone');
774 $data['sex'] = $this->input->post('sex');
775 $data['age'] = $this->input->post('age');
776 $data['blood_group'] = $this->input->post('blood_group');
777 $data['last_donation_timestamp'] = strtotime($this->input->post('last_donation_timestamp'));
778
779 $this->db->insert('blood_donor',$data);
780 }
781
782 function select_blood_donor_info()
783 {
784 return $this->db->get('blood_donor')->result_array();
785 }
786
787 function update_blood_donor_info($blood_donor_id)
788 {
789 $data['name'] = $this->input->post('name');
790 $data['email'] = $this->input->post('email');
791 $data['address'] = $this->input->post('address');
792 $data['phone'] = $this->input->post('phone');
793 $data['sex'] = $this->input->post('sex');
794 $data['age'] = $this->input->post('age');
795 $data['blood_group'] = $this->input->post('blood_group');
796 $data['last_donation_timestamp'] = strtotime($this->input->post('last_donation_timestamp'));
797
798 $this->db->where('blood_donor_id',$blood_donor_id);
799 $this->db->update('blood_donor',$data);
800 }
801
802 function delete_blood_donor_info($blood_donor_id)
803 {
804 $this->db->where('blood_donor_id',$blood_donor_id);
805 $this->db->delete('blood_donor');
806 }
807
808 function save_medicine_category_info()
809 {
810 $data['name'] = $this->input->post('name');
811 $data['description'] = $this->input->post('description');
812
813 $this->db->insert('medicine_category',$data);
814 }
815
816 function select_medicine_category_info()
817 {
818 return $this->db->get('medicine_category')->result_array();
819 }
820
821 function update_medicine_category_info($medicine_category_id)
822 {
823 $data['name'] = $this->input->post('name');
824 $data['description'] = $this->input->post('description');
825
826 $this->db->where('medicine_category_id',$medicine_category_id);
827 $this->db->update('medicine_category',$data);
828 }
829
830 function delete_medicine_category_info($medicine_category_id)
831 {
832 $this->db->where('medicine_category_id',$medicine_category_id);
833 $this->db->delete('medicine_category');
834 }
835
836 function save_medicine_info()
837 {
838 $data['name'] = $this->input->post('name');
839 $data['medicine_category_id'] = $this->input->post('medicine_category_id');
840 $data['description'] = $this->input->post('description');
841 $data['price'] = $this->input->post('price');
842 $data['stock'] = $this->input->post('stock');
843 $data['manufacturing_company'] = $this->input->post('manufacturing_company');
844 $data['status'] = $this->input->post('status');
845
846 $this->db->insert('medicine',$data);
847 }
848
849 function select_medicine_info()
850 {
851 return $this->db->get('medicine')->result_array();
852 }
853
854 function update_medicine_info($medicine_id)
855 {
856 $data['name'] = $this->input->post('name');
857 $data['medicine_category_id'] = $this->input->post('medicine_category_id');
858 $data['description'] = $this->input->post('description');
859 $data['price'] = $this->input->post('price');
860 $data['stock'] = $this->input->post('stock');
861 $data['manufacturing_company'] = $this->input->post('manufacturing_company');
862 $data['status'] = $this->input->post('status');
863
864 $this->db->where('medicine_id',$medicine_id);
865 $this->db->update('medicine',$data);
866 }
867
868 function delete_medicine_info($medicine_id)
869 {
870 $this->db->where('medicine_id',$medicine_id);
871 $this->db->delete('medicine');
872 }
873
874 function save_appointment_info()
875 {
876 $data['timestamp'] = strtotime($this->input->post('date_timestamp').' '.$this->input->post('time_timestamp') );
877 $data['status'] = 'approved';
878 $data['patient_id'] = $this->input->post('patient_id');
879
880 if($this->session->userdata('login_type') == 'doctor')
881 $data['doctor_id'] = $this->session->userdata('login_user_id');
882 else
883 $data['doctor_id'] = $this->input->post('doctor_id');
884
885 $this->db->insert('appointment',$data);
886
887 // Notify patient with sms.
888 $notify = $this->input->post('notify');
889 if($notify != '') {
890 $patient_name = $this->db->get_where('patient',
891 array('patient_id' => $data['patient_id']))->row()->name;
892 $doctor_name = $this->db->get_where('doctor',
893 array('doctor_id' => $data['doctor_id']))->row()->name;
894 $date = date('l, d F Y', $data['timestamp']);
895 $time = date('g:i a', $data['timestamp']);
896 $message = $patient_name . ', you have an appointment with doctor ' . $doctor_name . ' on ' . $date . ' at ' . $time . '.';
897 $receiver_phone = $this->db->get_where('patient',
898 array('patient_id' => $data['patient_id']))->row()->phone;
899
900 $this->sms_model->send_sms($message, $receiver_phone);
901 }
902 }
903
904 function save_requested_appointment_info()
905 {
906
907 $data['timestamp'] = strtotime($this->input->post('date_timestamp').' '.$this->input->post('time_timestamp') );
908 $data['doctor_id'] = $this->input->post('doctor_id');
909 $data['patient_id'] = $this->session->userdata('login_user_id');
910 $data['status'] = 'pending';
911
912 $this->db->insert('appointment',$data);
913 }
914
915 function select_appointment_info_by_doctor_id()
916 {
917 $doctor_id = $this->session->userdata('login_user_id');
918
919 $this->db->order_by('timestamp' , 'desc');
920 $this->db->where('doctor_id' , $doctor_id);
921 $this->db->where('status' , 'approved');
922
923 return $this->db->get('appointment')->result_array();
924 }
925
926 function select_appointment_info_by_patient_id()
927 {
928 $patient_id = $this->session->userdata('login_user_id');
929 return $this->db->get_where('appointment', array('patient_id' => $patient_id, 'status' => 'approved'))->result_array();
930 }
931
932 function select_appointment_info($doctor_id = '', $start_timestamp = '', $end_timestamp = '')
933 {
934 $response = array();
935 if($doctor_id == 'all') {
936 $this->db->order_by('doctor_id', 'asc');
937 $this->db->order_by('timestamp', 'desc');
938 $appointments = $this->db->get_where('appointment', array('status' => 'approved'))->result_array();
939 foreach ($appointments as $row) {
940 if($row['timestamp'] >= $start_timestamp && $row['timestamp'] <= $end_timestamp)
941 array_push ($response, $row);
942 }
943 }
944 else {
945 $this->db->order_by('timestamp', 'desc');
946 $appointments = $this->db->get_where('appointment', array('doctor_id' => $doctor_id, 'status' => 'approved'))->result_array();
947 foreach ($appointments as $row) {
948 if($row['timestamp'] >= $start_timestamp && $row['timestamp'] <= $end_timestamp)
949 array_push ($response, $row);
950 }
951 }
952 return $response;
953 }
954
955 function select_pending_appointment_info_by_patient_id()
956 {
957 $patient_id = $this->session->userdata('login_user_id');
958 return $this->db->get_where('appointment', array('patient_id' => $patient_id, 'status' => 'pending'))->result_array();
959 }
960
961 function select_requested_appointment_info_by_doctor_id()
962 {
963 $doctor_id = $this->session->userdata('login_user_id');
964 return $this->db->get_where('appointment', array('doctor_id' => $doctor_id, 'status' => 'pending'))->result_array();
965 }
966
967 function select_requested_appointment_info()
968 {
969 $this->db->order_by('doctor_id', 'asc');
970 return $this->db->get_where('appointment', array('status' => 'pending'))->result_array();
971 }
972
973 function select_patient_info_by_doctor_id()
974 {
975 $doctor_id = $this->session->userdata('login_user_id');
976
977 $this->db->group_by('patient_id');
978 return $this->db->get_where('appointment', array('doctor_id' => $doctor_id, 'status' => 'approved'))->result_array();
979 }
980
981 function select_appointments_between_loggedin_patient_and_doctor()
982 {
983 $patient_id = $this->session->userdata('login_user_id');
984
985 $this->db->group_by('doctor_id');
986 return $this->db->get_where('appointment', array('patient_id' => $patient_id, 'status' => 'approved'))->result_array();
987 }
988
989 function update_appointment_info($appointment_id)
990 {
991 $data['timestamp'] = strtotime($this->input->post('date_timestamp').' '.$this->input->post('time_timestamp') );
992 $data['patient_id'] = $this->input->post('patient_id');
993
994 $this->db->where('appointment_id',$appointment_id);
995 $this->db->update('appointment',$data);
996
997 // Notify patient with sms.
998 $notify = $this->input->post('notify');
999 if($notify != '') {
1000 $doctor_id = $this->session->userdata('login_user_id');
1001 $patient_name = $this->db->get_where('patient',
1002 array('patient_id' => $data['patient_id']))->row()->name;
1003 $doctor_name = $this->db->get_where('doctor',
1004 array('doctor_id' => $doctor_id))->row()->name;
1005 $date = date('l, d F Y', $data['timestamp']);
1006 $time = date('g:i a', $data['timestamp']);
1007 $message = $patient_name . ', your appointment with doctor ' . $doctor_name . ' has been updated to ' . $date . ' at ' . $time . '.';
1008 $receiver_phone = $this->db->get_where('patient',
1009 array('patient_id' => $data['patient_id']))->row()->phone;
1010
1011 $this->sms_model->send_sms($message, $receiver_phone);
1012 }
1013 }
1014
1015 function approve_appointment_info($appointment_id)
1016 {
1017 $data['timestamp'] = strtotime($this->input->post('date_timestamp').' '.$this->input->post('time_timestamp') );
1018 $data['status'] = 'approved';
1019
1020 if($this->session->userdata('login_type') == 'receptionist')
1021 $data['doctor_id'] = $this->input->post('doctor_id');
1022
1023 $this->db->where('appointment_id',$appointment_id);
1024 $this->db->update('appointment',$data);
1025
1026 // Notify patient with sms.
1027 $notify = $this->input->post('notify');
1028 if($notify != '') {
1029 $doctor_id = $this->db->get_where('appointment',
1030 array('appointment_id' => $appointment_id))->row()->doctor_id;
1031 $patient_id = $this->db->get_where('appointment',
1032 array('appointment_id' => $appointment_id))->row()->patient_id;
1033 $patient_name = $this->db->get_where('patient',
1034 array('patient_id' => $patient_id))->row()->name;
1035 $doctor_name = $this->db->get_where('doctor',
1036 array('doctor_id' => $doctor_id))->row()->name;
1037 $date = date('l, d F Y', $data['timestamp']);
1038 $time = date('g:i a', $data['timestamp']);
1039 $message = $patient_name . ', your requested appointment with doctor ' . $doctor_name . ' on ' . $date . ' at ' . $time . ' has been approved.';
1040 $receiver_phone = $this->db->get_where('patient',
1041 array('patient_id' => $patient_id))->row()->phone;
1042
1043 $this->sms_model->send_sms($message, $receiver_phone);
1044 }
1045 }
1046
1047 function delete_appointment_info($appointment_id)
1048 {
1049 $this->db->where('appointment_id',$appointment_id);
1050 $this->db->delete('appointment');
1051 }
1052
1053 function save_prescription_info()
1054 {
1055 $data['timestamp'] = strtotime($this->input->post('date_timestamp').' '.$this->input->post('time_timestamp') );
1056 $data['patient_id'] = $this->input->post('patient_id');
1057 $data['case_history'] = $this->input->post('case_history');
1058 $data['medication'] = $this->input->post('medication');
1059 $data['note'] = $this->input->post('note');
1060 $data['doctor_id'] = $this->session->userdata('login_user_id');
1061
1062 $this->db->insert('prescription',$data);
1063 }
1064
1065 function select_prescription_info_by_doctor_id()
1066 {
1067 $doctor_id = $this->session->userdata('login_user_id');
1068 return $this->db->get_where('prescription', array('doctor_id' => $doctor_id))->result_array();
1069 }
1070
1071 function select_medication_history( $patient_id = '' )
1072 {
1073 return $this->db->get_where('prescription', array('patient_id' => $patient_id))->result_array();
1074 }
1075
1076 function select_prescription_info_by_patient_id()
1077 {
1078 $patient_id = $this->session->userdata('login_user_id');
1079 return $this->db->get_where('prescription', array('patient_id' => $patient_id))->result_array();
1080 }
1081
1082 function update_prescription_info($prescription_id)
1083 {
1084 $data['timestamp'] = strtotime($this->input->post('date_timestamp').' '.$this->input->post('time_timestamp') );
1085 $data['patient_id'] = $this->input->post('patient_id');
1086 $data['case_history'] = $this->input->post('case_history');
1087 $data['medication'] = $this->input->post('medication');
1088 $data['note'] = $this->input->post('note');
1089 $data['doctor_id'] = $this->session->userdata('login_user_id');
1090
1091 $this->db->where('prescription_id',$prescription_id);
1092 $this->db->update('prescription',$data);
1093 }
1094
1095 function delete_prescription_info($prescription_id)
1096 {
1097 $this->db->where('prescription_id',$prescription_id);
1098 $this->db->delete('prescription');
1099 }
1100
1101 function save_diagnosis_report_info()
1102 {
1103 $data['timestamp'] = strtotime($this->input->post('date_timestamp').' '.$this->input->post('time_timestamp') );
1104 $data['report_type'] = $this->input->post('report_type');
1105 $data['file_name'] = $_FILES["file_name"]["name"];
1106 $data['document_type'] = $this->input->post('document_type');
1107 $data['description'] = $this->input->post('description');
1108 $data['prescription_id'] = $this->input->post('prescription_id');
1109
1110 $this->db->insert('diagnosis_report',$data);
1111
1112 $diagnosis_report_id = $this->db->insert_id();
1113 move_uploaded_file($_FILES["file_name"]["tmp_name"], "uploads/diagnosis_report/" . $_FILES["file_name"]["name"]);
1114 }
1115
1116 function select_diagnosis_report_info()
1117 {
1118 return $this->db->get('diagnosis_report')->result_array();
1119 }
1120
1121 function delete_diagnosis_report_info($diagnosis_report_id)
1122 {
1123 $this->db->where('diagnosis_report_id',$diagnosis_report_id);
1124 $this->db->delete('diagnosis_report');
1125 }
1126
1127 function save_notice_info()
1128 {
1129 $data['title'] = $this->input->post('title');
1130 $data['description'] = $this->input->post('description');
1131 if($this->input->post('start_timestamp') != '')
1132 $data['start_timestamp'] = strtotime($this->input->post('start_timestamp'));
1133 else
1134 $data['start_timestamp'] = '';
1135 if($this->input->post('end_timestamp') != '')
1136 $data['end_timestamp'] = strtotime($this->input->post('end_timestamp'));
1137 else
1138 $data['end_timestamp'] = $data['start_timestamp'];
1139
1140 $this->db->insert('notice',$data);
1141 }
1142
1143 function select_notice_info()
1144 {
1145 return $this->db->get('notice')->result_array();
1146 }
1147
1148 function update_notice_info($notice_id)
1149 {
1150 $data['title'] = $this->input->post('title');
1151 $data['description'] = $this->input->post('description');
1152 if($this->input->post('start_timestamp') != '')
1153 $data['start_timestamp'] = strtotime($this->input->post('start_timestamp'));
1154 else
1155 $data['start_timestamp'] = '';
1156 if($this->input->post('end_timestamp') != '')
1157 $data['end_timestamp'] = strtotime($this->input->post('end_timestamp'));
1158 else
1159 $data['end_timestamp'] = $data['start_timestamp'];
1160
1161 $this->db->where('notice_id',$notice_id);
1162 $this->db->update('notice',$data);
1163 }
1164
1165 function delete_notice_info($notice_id)
1166 {
1167 $this->db->where('notice_id',$notice_id);
1168 $this->db->delete('notice');
1169 }
1170
1171 ////////private message//////
1172 function send_new_private_message() {
1173 $message = $this->input->post('message');
1174 $timestamp = strtotime(date("Y-m-d H:i:s"));
1175
1176 $reciever = $this->input->post('reciever');
1177 $sender = $this->session->userdata('login_type') . '-' . $this->session->userdata('login_user_id');
1178
1179 //check if the thread between those 2 users exists, if not create new thread
1180 $num1 = $this->db->get_where('message_thread', array('sender' => $sender, 'reciever' => $reciever))->num_rows();
1181 $num2 = $this->db->get_where('message_thread', array('sender' => $reciever, 'reciever' => $sender))->num_rows();
1182
1183 if ($num1 == 0 && $num2 == 0) {
1184 $message_thread_code = substr(md5(rand(100000000, 20000000000)), 0, 15);
1185 $data_message_thread['message_thread_code'] = $message_thread_code;
1186 $data_message_thread['sender'] = $sender;
1187 $data_message_thread['reciever'] = $reciever;
1188 $this->db->insert('message_thread', $data_message_thread);
1189 }
1190 if ($num1 > 0)
1191 $message_thread_code = $this->db->get_where('message_thread', array('sender' => $sender, 'reciever' => $reciever))->row()->message_thread_code;
1192 if ($num2 > 0)
1193 $message_thread_code = $this->db->get_where('message_thread', array('sender' => $reciever, 'reciever' => $sender))->row()->message_thread_code;
1194
1195
1196 $data_message['message_thread_code'] = $message_thread_code;
1197 $data_message['message'] = $message;
1198 $data_message['sender'] = $sender;
1199 $data_message['timestamp'] = $timestamp;
1200 $this->db->insert('message', $data_message);
1201
1202 return $message_thread_code;
1203 }
1204
1205 function send_reply_message($message_thread_code) {
1206 $message = $this->input->post('message');
1207 $timestamp = strtotime(date("Y-m-d H:i:s"));
1208 $sender = $this->session->userdata('login_type') . '-' . $this->session->userdata('login_user_id');
1209
1210
1211 $data_message['message_thread_code'] = $message_thread_code;
1212 $data_message['message'] = $message;
1213 $data_message['sender'] = $sender;
1214 $data_message['timestamp'] = $timestamp;
1215 $this->db->insert('message', $data_message);
1216 }
1217
1218 function mark_thread_messages_read($message_thread_code) {
1219 // mark read only the oponnent messages of this thread, not currently logged in user's sent messages
1220 $current_user = $this->session->userdata('login_type') . '-' . $this->session->userdata('login_user_id');
1221 $this->db->where('sender !=', $current_user);
1222 $this->db->where('message_thread_code', $message_thread_code);
1223 $this->db->update('message', array('read_status' => 1));
1224 }
1225
1226 function count_unread_message_of_thread($message_thread_code) {
1227 $unread_message_counter = 0;
1228 $current_user = $this->session->userdata('login_type') . '-' . $this->session->userdata('login_user_id');
1229 $messages = $this->db->get_where('message', array('message_thread_code' => $message_thread_code))->result_array();
1230 foreach ($messages as $row) {
1231 if ($row['sender'] != $current_user && $row['read_status'] == '0')
1232 $unread_message_counter++;
1233 }
1234 return $unread_message_counter;
1235 }
1236}