forked from israelpascal/care2x-tz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class_tz_billing.php
executable file
·4043 lines (3389 loc) · 149 KB
/
class_tz_billing.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
require_once($root_path.'include/care_api_classes/class_core.php');
require_once($root_path.'include/care_api_classes/class_notes.php');
require_once($root_path.'include/care_api_classes/class_encounter.php');
/**
* Billing methods for tanzania (the product-module is completely rewritten by Robert Meggle.
*
* Note this class should be instantiated only after a "$db" adodb connector object has been established by an adodb instance
* @author Robert Meggle
* @version beta 1.0.0
* @copyright 2005 Robert Meggle (MEROTECH [email protected])
* @package care_api from Elpidio Latirilla
*/
/*
Class Structure:
Class Core
|
`---> Class Notes
|
`--->Class Encounter
|
`----> Class Bill
*/
class Bill extends Encounter {
// Properties
var $fields_tbl_bill=array(
'encounter_nr',
'first_date',
'create_id');
var $tbl_bill='care_tz_billing';
var $tbl_bill_elements='care_tz_billing_elem';
var $tbl_bill_archive='care_tz_billing_archive';
var $tbl_bill_archive_elements='care_tz_billing_archive_elem';
var $tbl_lab_param='care_tz_laboratory_param';
var $tb_drugsandservices='care_tz_drugsandservices';
var $tbl_prescriptions='care_encounter_prescription';
var $tbl_lab_requests='care_test_request_chemlabor';
var $fields_tbl_bill_elements=array (
'nr',
'date_change',
'is_labtest',
'is_medicine',
'is_comment',
'is_paid',
'amount',
'description');
var $result;
var $sql;
var $debug;
var $records;
var $parameter_array;
var $index;
var $value;
var $chemlab_testindex;
var $chemlab_testname;
var $chemlab_amount;
var $price;
var $medical_item_name;
var $medical_item_amount;
var $item_number;
var $user_id;
//------------------------------------------------------------------------------
// Constructor
function Bill() {
return true;
}
// Methods:
/******************************************************************************
* PRIVATE
**/
function _check_tbl_exists($tbl_name) {
global $db;
$this->debug=FALSE;
($this->debug) ? $db->debug=TRUE : $db->debug=FALSE;
$this->sql="SELECT * FROM $tbl_name LIMIT 0";
return ($db->Execute($this->sql)) ? true : false;
}
function _delete_tbl($tbl_name) {
global $db;
$this->debug=FALSE;
($this->debug) ? $db->debug=TRUE : $db->debug=FALSE;
$this->sql="DROP TABLE $tbl_name";
return ($db->Execute($this->sql)) ? true : false;
}
//------------------------------------------------------------------------------
function _GetPendingResults($encounter_nr=FALSE) {
/*
* This private method goes through the main tables and returns an
* result array (adodb) with hits or FALSE
*
* Definition of a pending bill:
* ************************************************************************
* ** A pending bill is a record that is not given by the billing table **
* ** care_tz_billing (encounter_nr) but there are entries in the tables **
* ** care_test_request_chemlabor (encounter_nr) and **
* ** care_encounter_prescription (encounter_nr) **
* ************************************************************************
* if $encounter_nr is set, then this private function just returns the
* records for this encounter. Default is FALSE for this value and this
* method returns a list of all
*/
global $db;
$this->debug=FALSE;
($this->debug) ? $db->debug=TRUE : $db->debug=FALSE;
}
/******************************************************************************
* PUBLIC
**/
function GetFirstPid() {
global $db, $root_path;
/*
* Definition of a pending bill:
* ************************************************************************
* ** A pending bill is a record that is not given by the billing table **
* ** care_tz_billing (encounter_nr) but there are entries in the tables **
* ** care_test_request_chemlabor (encounter_nr) and **
* ** care_encounter_prescription (encounter_nr) **
* ************************************************************************
*/
/*
* Note: 10.June 2005 by RM: This is an old function out of an billing experiment,
* there is the possibility that this function is not working correctely but I haven't
* fond an error right now. Never touch a running and working routine... But:
* If there is a need for it, please replace this function and note that there are
* now flags in the pending encounter and laboratory table to determine this result.
* This function should return the first pid what could have pending bills.
*/
$this->debug=FALSE;
($this->debug) ? $db->debug=TRUE : $db->debug=FALSE;
$this->sql="SELECT
care_person.selian_pid,
care_person.pid,
name_first,
name_last,
bi.encounter_nr,
bi.first_date,
care_person.pid as batch_nr
FROM care_encounter, care_person, care_tz_billing bi, care_tz_billing_elem biel
WHERE care_encounter.pid = care_person.pid
AND bi.encounter_nr = care_encounter.encounter_nr
AND biel.nr = bi.nr
group by batch_nr
ORDER BY batch_nr ASC LIMIT 1";
$this->results = $db->Execute($this->sql);
if($this->first_pn=$this->results->FetchRow()) {
return $this->first_pn['pid'];
} else {
return FALSE;
}
}
//------------------------------------------------------------------------------
function getAllEncounters() {
global $db;
$this->debug=FALSE;
($this->debug) ? $db->debug=TRUE : $db->debug=FALSE;
if ($this->debug) echo "<br><b>statement from Method: class_tz_billing::GetAllEncounters:</b><br>";
$this->sql="SELECT
encounter_nr
FROM care_encounter
ORDER BY encounter_nr DESC";
$this->result=$db->Execute($this->sql);
$i=0;
while($encounters=$this->result->FetchRow()) {
$enc_array[$i] = $encounters['encounter_nr'];
$i++;
}
return $enc_array;
}
//------------------------------------------------------------------------------
function CheckForPendingBill($encounter_nr) {
/**
* returns TRUE if there is a pending Bill existing for this encounter
*/
global $db;
$this->debug=FALSE;
if ($this->debug) echo "<br><b>class_tz_billing::CheckForExistingBill($encounter_nr)</b><br>";
$this->sql="select encounter_nr FROM care_tz_billing where encounter_nr=$encounter_nr LIMIT 0,1";
$this->result=$db->Execute($this->sql);
return ($this->result->RecordCount())? TRUE : FALSE;
}
//------------------------------------------------------------------------------
function PendingBillObjects($encounter_nr) {
/**
* returns TRUE if at least one item in the billing table are missing...
*/
global $db;
$this->debug=false;
($this->debug) ? $db->debug=TRUE : $db->debug=FALSE;
$PRESCRIPTION_GIVEN=FALSE;
$LABORTORY_GIVEN=FALSE;
if ($this->debug) echo "<br><b>class_tz_billing::GetPendingObjects($encounter_nr)</b><br>";
// prescription:
// read all items out of the prescription table where no bill_number is given for this encounter
$this->sql = "select
article_item_number
from $this->tbl_prescriptions
where bill_number = 0 AND encounter_nr=".$encounter_nr;
$this->result=$db->Execute($this->sql);
if ($this->result->RecordCount()>0)
$PRESCRIPTION_GIVEN=TRUE;
//laboratory:
// read all items out of the laboratory table where no bill_number is given for this encounter
$this->sql="select
encounter_nr
FROM
$this->tbl_lab_requests
where bill_number = 0 AND encounter_nr=".$encounter_nr;
// echo $this->sql;
$this->result=$db->Execute($this->sql);
if ($this->result->RecordCount()>0)
$LABORTORY_GIVEN = TRUE;
if ($LABORTORY_GIVEN===TRUE && $LABORTORY_GIVEN ===TRUE)
return TRUE;
else
return FALSE; // We have nothing to do... no pending issues in the tables!
}
//------------------------------------------------------------------------------
function DisplayArchivedBillHeadlines($no) {
global $db;
$enc_obj = New Encounter;
$this->debug=FALSE;
($this->debug) ? $db->debug=TRUE : $db->debug=FALSE;
if ($this->debug) echo "<br><b>class_tz_billing::DisplayArchivedBillHeadlines</b><br>";
$this->sql_pid="SELECT `pid` FROM care_person where selian_pid like '%$no%' OR name_first like '%$no%' OR name_last like '%$no%' ";
$this->request_pid=$db->Execute($this->sql_pid);
$color_change=FALSE;
while ($this->res_pid=$this->request_pid->FetchRow()){
$pid=$this->res_pid['pid'];
$this->sql_enr="SELECT `encounter_nr` FROM care_encounter where pid= '$pid' ";
$this->request_enr=$db->Execute($this->sql_enr);
$color_change=FALSE;
while ($this->res_enr=$this->request_enr->FetchRow()){
$enr=$this->res_enr['encounter_nr'];
$this->sql="SELECT `nr`, `encounter_nr`, `first_date` FROM care_tz_billing_archive where encounter_nr='$enr' order by nr DESC";
$this->request=$db->Execute($this->sql);
$color_change=FALSE;
while ($this->res=$this->request->FetchRow()) {
if ($color_change) {
$BGCOLOR='bgcolor="#ffffdd"';
$color_change=FALSE;
} else {
$BGCOLOR='bgcolor="#ffffaa"';
$color_change=TRUE;
}
//$encoded_batch_number = $enc_obj->ShowPID($batch_nr);
$enc_number = $enc_obj->GetEncounterFromBatchNumber($batch_nr);
$this->bill_number=$this->res['nr'];
$this->encounter_number=$this->res['encounter_nr'];
$this->batch_number=$enc_obj->GetBatchFromEncounterNumber($this->encounter_number);
$this->date=$this->res['first_date'];
$enc_data = $enc_obj->loadEncounterData($this->encounter_number);
echo '<script language="javascript" >
<!--
function printOut_'.$this->bill_number.'()
{
urlholder="show_bill.php?bill_number='.$this->bill_number.'&batch_nr='.$this->batch_number.'&printout=TRUE&show_archived_bill=TRUE";
testprintout=window.open(urlholder,"printout","width=800,height=600,menubar=no,resizable=yes,scrollbars=yes");
}
// -->
</script>
';
echo '<tr>
<td '.$BGCOLOR.'><div align="center"><a href="javascript:printOut_'.$this->bill_number.'()">'.$this->bill_number.'</a></div></td>
<td '.$BGCOLOR.'><div align="center">'.date("d.m.Y",$this->date).'</div></td>
<td '.$BGCOLOR.'><div align="center">'.$enc_obj->ShowPID($this->batch_number).'</div></td>
<td '.$BGCOLOR.'><div align="center">'.$this->encounter_number.'</div></td>
<td '.$BGCOLOR.'><div align="center">'.$enc_obj->FirstName($enc_data).' '.$enc_obj->LastName($enc_data).'
</div></td>
</tr> ';
}
}//
}//
}
//------------------------------------------------------------------------------
function GetBill($encounter_nr) {
global $db;
/**
* Returns the ident bill number
*/
$this->debug=FALSE;
($this->debug) ? $db->debug=TRUE : $db->debug=FALSE;
if ($this->debug) echo "<br><b>Method class_tz_billing::GetBill()</b><br>";
$this->sql = "SELECT nr FROM $this->tbl_bill WHERE encounter_nr=".$encounter_nr." limit 0,1 ";
$this->request = $db->Execute($this->sql);
echo $this->sql;
if ($this->result=$this->request->FetchRow())
return $this->result[0];
}
//------------------------------------------------------------------------------
function GetAllPendingBillNumbers() {
global $db;
/**
* Returns array of all pending bill numbers
*/
$this->debug=TRUE;
($this->debug) ? $db->debug=TRUE : $db->debug=FALSE;
if ($this->debug) echo "<br><b>Method class_tz_billing::GetAllPendingBillNumbers()</b><br>";
$this->sql = "SELECT nr FROM $this->tbl_bill";
$this->request = $db->Execute($this->sql);
if ($this->result=$this->request->GetArray()) {
return $this->result;
} else {
return FALSE;
} // end of
}
//------------------------------------------------------------------------------
function CreateNewBill($encounter_nr) {
global $db;
/**
* Returns the ident bill number
*/
$this->debug=FALSE;
($this->debug) ? $db->debug=TRUE : $db->debug=FALSE;
if ($this->debug) echo "<br><b>Method class_tz_billing::CreateNewBill()</b><br>";
$this->sql = "INSERT INTO care_tz_billing (encounter_nr, first_date, create_id) VALUES ('".$encounter_nr."','".time()."','') ";
$this->request = $db->Execute($this->sql);
return $db->Insert_ID();
}
function StoreToBill($encounter_nr, $bill_number){
global $db;
$this->debug=FALSE;
if ($this->debug) echo "<b>class_tz_billing::StoreToBill(encounter_nr: $encounter_nr, bill_number: $bill_number)</b><br>";
($this->debug) ? $db->debug=TRUE : $db->debug=FALSE;
// do we have pending issues of prescriptions?
// read all items out of the prescription table
$this->sql = "select
article_item_number,
article,
notes,
dosage,
price
from $this->tbl_prescriptions
where bill_number = 0 AND encounter_nr=".$encounter_nr;
$this->result=$db->Execute($this->sql);
while ($this->records=$this->result->FetchRow()) {
$this->item_number=$this->records['article_item_number'];
$this->medical_item_name =$this->records['article'];
$this->medical_item_name .= "(".$this->records['notes'].")";
$this->medical_item_amount=$this->records['dosage'];
$this->price =$this->records['price'];
// The amount of this medical item could be translated into the real amount...
$this->medical_item_amount = $this->ConvertMedicalItemAmount($this->medical_item_amount);
$this->sql ="INSERT INTO $this->tbl_bill_elements (nr, date_change, is_labtest, is_medicine, amount, amount_doc, price, description)
VALUES (".$bill_number.",".time().",0,1,'".$this->medical_item_amount."','".$this->medical_item_amount."','".$this->price."','".$this->medical_item_name."')";
$db->Execute($this->sql);
if ($this->debug) echo $this->sql;
// Mark these lines in the table prescription as "still billed". We can do this
// in that way: Insert the billing number where we can find this article again...
$this->sql="UPDATE $this->tbl_prescriptions SET bill_number='".$bill_number."', bill_status='pending' WHERE bill_number = 0 AND encounter_nr=".$encounter_nr;
$db->Execute($this->sql);
}
// And now the laboratory...
$this->sql = "select
encounter_nr,
parameters
FROM $this->tbl_lab_requests
WHERE encounter_nr=".$encounter_nr." AND bill_number = 0";
$this->parameters = $db->Execute($this->sql);
while ($this->records=$this->parameters->FetchRow()) {
if ($this->debug) echo $this->records['parameters']."<br>";
parse_str($this->records['parameters'],$this->parameter_array);
while(list($this->index,$this->chemlab_amount) = each($this->parameter_array)) {
//Strip the string baggage off to get the task id
$this->chemlab_testindex = substr($this->index,5,strlen($this->index)-6);
$this->chemlab_testname = $this->GetNameOfLAboratoryFromID($this->chemlab_testindex);
$this->price = $this->GetPriceOfLAboratoryItemFromID($this->chemlab_testindex);
if ($this->debug) echo "the name of chemlab is:".$this->chemlab_testname." with a amount of ".$this->chemlab_amount."<br>";
// we have it all... now we store it into the billing-elements-table
$this->sql ="INSERT INTO $this->tbl_bill_elements (nr, date_change, is_labtest, is_medicine, amount, price, description)
VALUES (".$bill_number.",".time().",1,0,".$this->chemlab_amount.",'".$this->price."','".$this->chemlab_testname."')";
$db->Execute($this->sql);
}
}
// Mark these lines in the table prescription as "still billed". We can do this
// in that way: Insert the billing number where we can find this article again...
$this->sql="UPDATE $this->tbl_lab_requests SET bill_number='".$bill_number."' , bill_status='pending' WHERE bill_number = 0 AND encounter_nr=".$encounter_nr;
$db->Execute($this->sql);
}
function StorePrescriptionItemToBill($pid, $prescriptions_nr, $bill_number, $price, $dosage, $notes, $insurance, $timesPerDay, $days, $drug_class){
global $db, $root_path;
$this->debug=FALSE;
if ($this->debug) echo "<b>class_tz_billing::StorePrescriptionItemToBill($pid, $prescriptions_nr, $bill_number, $price, $dosage, $notes, $insurance, $timesPerDay, $days)</b><br>";
if ($this->debug) echo "prescriptions_nr: $prescriptions_nr, bill_number: $bill_number<br>";
if ($this->debug) echo "times_per_day: $timesPerDay, days: $days<br>";
($this->debug) ? $db->debug=TRUE : $db->debug=FALSE;
require_once($root_path.'include/care_api_classes/class_tz_insurance.php');
$insurance_tz = New Insurance_tz();
$contract = $insurance_tz->CheckForValidContract($pid);
// do we have pending issues of prescriptions?
// read all items out of the prescription table
if ($drug_class=='Lab')
{
$is_labtest = 1;
$is_medicine = 0;
}
else
{
$is_labtest = 0;
$is_medicine = 1;
}
$this->sql = "
INSERT INTO $this->tbl_bill_elements (nr, date_change, is_labtest, is_medicine, amount, amount_doc, times_per_day, days, price, description, notes, item_number, balanced_insurance, insurance_id, prescriptions_nr)
SELECT '".$bill_number."', '".time()."' AS date_change, ".$is_labtest.", ".$is_medicine.", '".$dosage."', dosage, $timesPerDay, $days, $price, article, '".$notes."', article_item_number, '".$insurance."', '".$contract['id']."', '".$prescriptions_nr."'
FROM $this->tbl_prescriptions WHERE nr = $prescriptions_nr";
if ($this->debug) echo $this->sql."<br>";
$this->result=$db->Execute($this->sql);
}
//------------------------------------------------------------------------------
function StoreLaboratoryItemToBill($pid, $batch_nr, $bill_number, $insurance){
global $db, $root_path;
$this->debug=FALSE;
if ($this->debug) echo "<b>class_tz_billing::LaboratoryItemToBill(pid: $pid, batch_nr: $batch_nr, bill_number: $bill_number, insurance: $insurance)</b><br>";
($this->debug) ? $db->debug=TRUE : $db->debug=FALSE;
// do we have pending issues of prescriptions?
// read all items out of the prescription table
// old code
// $this->sql = "select
// encounter_nr,
// parameters
// FROM $this->tbl_lab_requests
// WHERE batch_nr=".$batch_nr;
$this->sql = "select *
FROM care_test_request_chemlabor_sub c_sub, care_tz_laboratory_param c_param
WHERE c_sub.sub_id=".$batch_nr." AND c_sub.paramater_name=c_param.id";
$result = $db->Execute($this->sql);
while ($row=$result->FetchRow()) {
//echo $this->records['paramater_name'];
//$this->chemlab_testname = $this->GetNameOfLAboratoryFromID($this->records['id']);
//$this->price = $this->GetPriceOfLAboratoryItemFromID($this->records['id']);
if ($this->debug) echo 'id : '.$row['id'].'<br>';
if ($this->debug) echo 'Testname: '.$row['name'].'<br>';
if ($this->debug) echo 'Price: '.$row['price'].'<br>';
$insurance_tz = New Insurance_tz();
$contract = $insurance_tz->CheckForValidContract($pid);
if ($this->debug) echo 'contract id : '.$contract['id'].'<br>';
//$contract['id'] = 12;
$this->chemlab_amount = 1;
$this->sql ="INSERT INTO $this->tbl_bill_elements (nr, date_change, is_labtest, is_medicine, amount, price, balanced_insurance, insurance_id, description)
VALUES (".$bill_number.",".time().",1,0,".$this->chemlab_amount.",'".$row['price']."','".$insurance."','".$contract['id']."','".$row['name']."')";
if ($this->debug) echo $this->sql;
$db->Execute($this->sql);
}
// Mark these lines in the table prescription as "still billed". We can do this
// in that way: Insert the billing number where we can find this article again...
//herausfinden, was geändert wird, damit Rechnung als billed gekennzeichnet wird
$this->sql="UPDATE $this->tbl_lab_requests SET bill_number='".$bill_number."' , bill_status='pending' WHERE sub_id=".$batch_nr;
if ($this->debug) echo $this->sql;
$db->Execute($this->sql);
// $this->parameters = $db->Execute($this->sql);
// while ($this->records=$this->parameters->FetchRow()) {
// if ($this->debug) echo $this->records['parameters']."<br>";
// parse_str($this->records['parameters'],$this->parameter_array);
// while(list($this->index,$this->chemlab_amount) = each($this->parameter_array)) {
// //Strip the string baggage off to get the task id
// $this->chemlab_testindex = substr($this->index,5,strlen($this->index)-6);
//
// $this->chemlab_testname = $this->GetNameOfLAboratoryFromID($this->chemlab_testindex);
//
// $this->price = $this->GetPriceOfLAboratoryItemFromID($this->chemlab_testindex);
// if ($this->debug) echo "the name of chemlab is:".$this->chemlab_testname." with a amount of ".$this->chemlab_amount." and a price of ".$this->price."<br>";
// require_once($root_path.'include/care_api_classes/class_tz_insurance.php');
// $insurance_tz = New Insurance_tz();
// $contract = $insurance_tz->CheckForValidContract($pid);
// // we have it all... now we store it into the billing-elements-table
// $this->sql ="INSERT INTO $this->tbl_bill_elements (nr, date_change, is_labtest, is_medicine, amount, price, balanced_insurance, insurance_id, description)
// VALUES (".$bill_number.",".time().",1,0,".$this->chemlab_amount.",'".$this->price."','".$insurance."','".$contract['id']."','".$this->chemlab_testname."')";
// if ($this->debug) echo $this->sql;
// $db->Execute($this->sql);
// $insurance=0;
// }
// }
// // Mark these lines in the table prescription as "still billed". We can do this
// // in that way: Insert the billing number where we can find this article again...
// $this->sql="UPDATE $this->tbl_lab_requests SET bill_number='".$bill_number."' , bill_status='pending' WHERE batch_nr=".$batch_nr;
// $db->Execute($this->sql);
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
function ConvertMedicalItemAmount ( $amount ) {
return $amount;
}
//------------------------------------------------------------------------------
function GetPriceOfItem($item_number) {
global $db;
$debug=FALSE;
($debug) ? $db->debug=TRUE : $db->debug=FALSE;
$this->sql="SELECT unit_price as price FROM $this->tb_drugsandservices WHERE item_id = '$item_number' ";
if ($this->result=$db->Execute($this->sql)) {
if ($this->result->RecordCount()) {
$this->item_array = $this->result->GetArray();
while (list($x,$v)=each($this->item_array)) {
$db->debug=FALSE;
return $v['price'];
}
} else {
$db->debug=FALSE;
return false;
}
}
} // end of function GetPriceOfItem($item_number)
//------------------------------------------------------------------------------
function GetNameOfItem($item_number) {
global $db;
$debug=FALSE;
($debug)?$db->debug=TRUE:$db->debug=FALSE;
$this->sql="SELECT item_description as description FROM $this->tb_drugsandservices WHERE item_id = '$item_number' ";
if ($this->result=$db->Execute($this->sql)) {
if ($this->result->RecordCount()) {
$this->item_array = $this->result->GetArray();
while (list($x,$v)=each($this->item_array)) {
$db->debug=FALSE;
return $v['description'];
}
} else {
$db->debug=FALSE;
return false;
}
}
} // end of function GetNameOfDrug($item_number)
//------------------------------------------------------------------------------
function CreateBalanceBill($insurance_id, $value) {
global $db;
$debug=FALSE;
($debug)?$db->debug=TRUE:$db->debug=FALSE;
$this->sql="SELECT b.nr, b.encounter_nr FROM care_tz_billing_archive b, care_tz_billing_archive_elem be WHERE be.nr = b.nr AND be.insurance_id=".$insurance_id." ORDER by b.id DESC Limit 0,1";
$this->result = $db->Execute($this->sql);
if($this->row = $this->result->FetchRow())
{
$this->sql="INSERT INTO care_tz_billing_archive SET nr='balanced".$this->row['nr']."', encounter_nr=".$this->row['encounter_nr'].", first_date=".time().", creation_date=".time();
$this->result = $db->Execute($this->sql);
$this->sql="INSERT INTO care_tz_billing_archive_elem SET nr='balanced".$this->row['nr']."', date_change=".time().", is_labtest=0, is_medicine=0, is_comment=0, is_paid=1, amount=1, price=0, balanced_insurance=".$value.", insurance_id=".$insurance_id.", item_number=0, prescriptions_nr=0";
$this->result = $db->Execute($this->sql);
return true;
}
return false;
}
//------------------------------------------------------------------------------
function GetBillNumbersFromPID($pid) {
global $db;
$debug=FALSE;
($debug)?$db->debug=TRUE:$db->debug=FALSE;
$this->sql="SELECT cb.nr FROM ".$this->tbl_bill." cb, ".$this->tbl_bill_elements." cbe, care_encounter ce, care_person cp
WHERE cb.encounter_nr = ce.encounter_nr
AND ce.pid = cp.pid
AND cbe.nr = cb.nr
AND cp.pid = $pid
GROUP BY cb.nr";
return $db->Execute($this->sql);
}
//------------------------------------------------------------------------------
function GetArchivedBill($bill_nr) {
global $db;
$this->sql="SELECT nr FROM care_tz_billing_archive WHERE nr=".$bill_nr;
return $db->Execute($this->sql);
}
//------------------------------------------------------------------------------
function VerifyBill($bill_nr) {
global $db;
$this->sql="SELECT nr FROM ".$this->tbl_bill." WHERE nr=".$bill_nr;
//echo $this->sql;
return $db->Execute($this->sql);
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
function GetArchivedBillEncounter($bill_nr) {
global $db;
$this->sql="SELECT encounter_nr FROM care_tz_billing_archive WHERE nr=".$bill_nr;
while ($row = $db->Execute($this->sql)->FetchRow() )
{
return $db->Execute($this->sql)->FetchRow();
}
}
//------------------------------------------------------------------------------
function setBillIsTransmit2ERP($bill_number,$transmit) {
global $db;
$db->debug=false;
$this->sql="update care_tz_billing_archive_elem SET is_transmit2ERP='$transmit' where nr='$bill_number'";
$db->Execute($this->sql);
}
function GetElemsOfArchivedBill($nr,$what_kind_of) {
global $db;
$debug=FALSE;
($debug) ? $db->debug=TRUE : $db->debug=FALSE;
if ($what_kind_of=="prescriptions")
$sql_where = " AND is_medicine=1 ";
if ($what_kind_of=="laboratory")
$sql_where = " AND is_labtest=1 ";
$this->sql="SELECT * FROM care_tz_billing_archive_elem
WHERE nr = '".$nr."' ".$sql_where."
ORDER BY date_change ASC";
return $db->Execute($this->sql);
}
function GetElemsOfArchivedBillForERP($nr)
{
global $db;
$debug=false;
($debug) ? $db->debug=true : $db->debug=false;
$this->sql="SELECT a.*, b.item_number FROM care_tz_billing_archive_elem a
inner join care_tz_drugsandservices b on a.item_number = b.item_id where nr=$nr";
return $db->Execute($this->sql);
}
//------------------------------------------------------------------------------
function GetElemsOfBill($nr,$what_kind_of) {
global $db;
$debug=FALSE;
($debug) ? $db->debug=TRUE : $db->debug=FALSE;
if ($what_kind_of=="prescriptions")
$sql_where = " AND is_medicine=1 ";
if ($what_kind_of=="laboratory")
$sql_where = " AND is_labtest=1 ";
$this->sql="SELECT * FROM ".$this->tbl_bill_elements."
WHERE nr = ".$nr." ".$sql_where."
ORDER BY date_change ASC";
return $db->Execute($this->sql);
}
//------------------------------------------------------------------------------
function GetElemsOfBillByPrescriptionNr($nr) {
global $db;
$debug=FALSE;
($debug) ? $db->debug=TRUE : $db->debug=FALSE;
$this->sql="SELECT * FROM ".$this->tbl_bill_elements."
WHERE prescriptions_nr = ".$nr;
return $db->Execute($this->sql);
}
//------------------------------------------------------------------------------
function GetElemsOfBillByPrescriptionNrArchive($nr) {
global $db;
$debug=FALSE;
($debug) ? $db->debug=TRUE : $db->debug=FALSE;
$this->sql="SELECT * FROM ".$this->tbl_bill_archive_elements."
WHERE prescriptions_nr = ".$nr;
//echo $this->sql;
return $db->Execute($this->sql);
}
//------------------------------------------------------------------------------
function GetBillByBatchNr($nr) {
global $db;
$debug=FALSE;
($debug) ? $db->debug=TRUE : $db->debug=FALSE;
$this->sql="SELECT bill_number FROM care_test_request_chemlabor WHERE batch_nr=$nr";
$this->result= $db->Execute($this->sql);
return $this->result->FetchRow();
}
//------------------------------------------------------------------------------
function GetNameOfLAboratoryFromID($id) {
global $db;
$debug=FALSE;
($debug) ? $db->debug=TRUE : $db->debug=FALSE;
$this->sql="SELECT name FROM ".$this->tbl_lab_param."
WHERE id = '".$id."'";
$this->result = $db->Execute($this->sql);
if ($this->records=$this->result->FetchRow())
return $this->records['name'];
}
//------------------------------------------------------------------------------
function GetPriceOfLAboratoryItemFromID($id) {
global $db;
$debug=FALSE;
($debug) ? $db->debug=TRUE : $db->debug=FALSE;
$this->sql="SELECT price FROM ".$this->tbl_lab_param."
WHERE id = '".$id."'";
//echo $this->sql;
$this->result = $db->Execute($this->sql);
if ($this->records=$this->result->FetchRow())
return $this->records['price'];
}
// -----------------------------------------------------------------------------
function GetBillTimestamp($bill_nr) {
global $db;
$debug=false;
($debug) ? $db->debug=TRUE : $db->debug=FALSE;
if(!$bill_nr) return false;
$this->sql="SELECT first_date FROM care_tz_billing WHERE nr=".$bill_nr;
$this->result = $db->Execute($this->sql);
if($this->row = $this->result->FetchRow())
{
return $this->row['first_date'];
}
$this->sql="SELECT first_date FROM care_tz_billing_archive WHERE nr=".$bill_nr;
$this->result = $db->Execute($this->sql);
if($this->row = $this->result->FetchRow())
{
return $this->row['first_date'];
}
return false;
}
//------------------------------------------------------------------------------
function GetBillCostSummaryInTimeframe($PID, $start, $end) {
global $db;
$debug=FALSE;
($debug) ? $db->debug=TRUE : $db->debug=FALSE;
if ($debug) echo 'PID: '.$PID.' - Start: '.$start.' - End: '.$end.'<br>';
/*
$this->sql="SELECT nr FROM care_tz_billing b, care_encounter e WHERE first_date>".$start." AND first_date <=".$end." AND e.encounter_nr = b.encounter_nr AND e.pid=".$PID;
if($this->result = $db->Execute($this->sql))
while($this->row = $this->result->FetchRow())
{
$balanced_insurance = false;
$this->elemsofbill = $this->GetElemsOfBill($this->row['nr'], false);
while($this->elems = $this->elemsofbill->FetchRow())
{
$balanced_insurance += $this->elems['balanced_insurance'];
}
$return[$this->row['nr']] = $balanced_insurance;
}
*/
$this->sql="SELECT nr FROM care_tz_billing_archive b, care_encounter e WHERE first_date>".$start." AND first_date <=".$end." AND e.encounter_nr = b.encounter_nr AND e.pid=".$PID;
if($this->result = $db->Execute($this->sql))
while($this->row = $this->result->FetchRow())
{
$balanced_insurance = false;
$this->elemsofbill = $this->GetElemsOfArchivedBill($this->row['nr'], false);
while($this->elems = $this->elemsofbill->FetchRow())
{
$balanced_insurance += $this->elems['balanced_insurance'];
}
$return[$this->row['nr']] = $balanced_insurance;
}
return $return;
}
//------------------------------------------------------------------------------
function DisplayBillHeadline($bill_nr, $batch_nr) {
global $LDBatchFileNr,$LDEncounterNr,$LDLastName,$LDFirstName,$LDBday,$LDSex,$LDBillNumber,$LDWard;
// Maybe optional parameter given to this funciton: Is it a print-out-page or not (For table resize)
if (func_num_args()>2)
$printout=func_get_arg (2);
$enc_obj = New Encounter;
$encoded_batch_number = $enc_obj->ShowPID($batch_nr);
$enc_number = $enc_obj->GetEncounterFromBatchNumber($batch_nr);
$bill_head=$enc_obj->GetHospitalAddress();
$bill_logo=$enc_obj->GetHospitalLogo();
$bill_name=$enc_obj->GetHospitalName();
if ($enc_obj->EncounterExists($enc_number)) {
// Load the encounter data:
$enc_data = $enc_obj->loadEncounterData($enc_number);
echo ($printout) ? '<table width="100%" border="0" cellspacing=1 cellpadding=0 align="center" >' : '<table width="100%" border="0" cellspacing=1 cellpadding=0 >';
echo '<tr><td></td></td><td width="40%"><b><table border=0 width="100%" align="left"><tr><td align="center"><b>'.$bill_head. '</td></tr></table></td><td align="right"><img src="../../'.$bill_logo.'" border=0 align="absmiddle" alt=""></td></tr> ' ;
echo ($printout) ? '<table width="200" border="0" cellspacing=1 cellpadding=0 >' : '<table width="100%" border="0" cellspacing=1 cellpadding=0 >';
echo '
<tr><td></td></tr>
<td class="headline"><span class=SpellE><span style="font-family:"20 cpi""><font size="4"><b>'.$LDBatchFileNr.'</b></font></span></span></td>
<td class="headline"><span class=SpellE><span style="font-family:"20 cpi""><font size="4">'.$encoded_batch_number.'</font></span></span></b></td>
</tr>'
;
/*
<tr>
<td class="headline"><span class=SpellE><span style="font-family:"20 cpi""><font size="4">Admission-Nr.:</font></span></span></td>
<td bgcolor="#ffffee" class="vi_data"><span style="font-family:"20 cpi""><font size="4">'.$enc_number.'</font></span></span></b></td>
</tr>
echo ' <tr>
<td class="headline"><span class=SpellE><span style="font-family:"20 cpi""><font size="4">Patient Names:</font></span></span></td>
<td bgcolor="#ffffee" class="vi_data"><span style="font-family:"20 cpi""><font size="4">'.$enc_obj->LastName($enc_number).', '.$enc_obj->FirstName($enc_number).'</font></span></span></b></td>
</tr>
<tr>
<td class="headline"><span class=SpellE><span style="font-family:"20 cpi""><font size="4">Encounter Location:</font></span></span></td>
<td bgcolor="#ffffee" class="vi_data"><span style="font-family:"20 cpi""><font size="4">'.$enc_obj->LocationTypeNumber($type_nr).', '.$enc_obj->LocationNumber($loc_nr).' </font></span></span></b></td>
</tr>';
/*
<tr>
<td class="headline"><span class=SpellE><span style="font-family:"20 cpi""><font size="4">First name:</font></span></span></td>
<td bgcolor="#ffffee" class="vi_data"><span style="font-family:"20 cpi""><font size="4">'.$enc_obj->FirstName($enc_number).'</font></span></span></td>
</tr>'
;
*/
/*
<tr>
<td class="headline"><span class=SpellE><span style="font-family:"20 cpi""><font size="4">Date of birth:</font></span></span></td>
<td bgcolor="#ffffee" class="vi_data"><span style="font-family:"20 cpi""><font size="4">'.$enc_obj->BirthDate($enc_number).'</font></span></span></td>
</tr>
<tr>
<td class="headline""><span class=SpellE><span style="font-family:"20 cpi""><font size="4">'.$LDSex.':</font></span></span></td>
<td class="adm_input"><span class=SpellE><span style="font-family:"20 cpi""><font size="4">'.$enc_obj->Sex($enc_number).'</font></span></span></td>
</tr>
*/
echo ' <tr>
<td class="headline"><span class=SpellE><span style="font-family:"20 cpi""><font size="4">Bill Date</font></span></span></td>
<td class="headline"><span class=SpellE><span style="font-family:"20 cpi""><font size="4">'.date('d-m-Y').'</font></span></span></td>
</tr>
<tr>
<td class="headline"><b><span class=SpellE><span style="font-family:"20 cpi""><font size="4">'.$LDBillNumber.'</font></span></span></b></td>
<td class="headline"><span class=SpellE><span style="font-family:"20 cpi""><font size="4">'.$bill_nr.'</font></span></span></b></td>
</tr>
<tr>
</table>';
return TRUE;
}
return FALSE;
}
//------------------------------------------------------------------------------
function DisplayArchivedLaboratoryBill($bill_nr,$edit_fields) {
global $root_path, $billnr, $batch_nr;
global $LDArticle,$LDPrice,$LDAmount,$LDPaidbyInsurance,$LDpartSum,$LDtotalamount,$LDLaboratory;
$sum_price=0;
echo '
<table width="100%" border="0" class="table_content">
<tr>
<!--<td valign="top" >
<p class="billing_topic"><br><span style="font-family:"20 cpi""><font size="-2">'.$LDLaboratory.'</font></span></span></p>
</td>-->
<td>';
$billelems=$this->GetElemsOfArchivedBill($bill_nr,"laboratory");
//echo "edit fields is set to:".$edit_fields;
echo '
<table width="100%" height="100%" class="table_content">
<tr>