A
download class.BillingModule.php
Language: PHP
LOC: 426
Project Info
FreeMED Project(freemed)
Server: SourceForge
Type: cvs
...reemed\freemed\freemed\lib\
   acl.php
   API.php
   bcadd.php
   calendar-functions.php
   class.AdminModule.php
   class.Agata.php
   class.Authorizations.php
   class.BaseModule.php
   class.BillingModule.php
   class.CalendarModule.php
   class.ClaimLog.php
   class.Coverage.php
   class.Debug.php
   class.diagnosis_set.php
   class.Djvu.php
   class.EMRModule.php
   class.Fax.php
   class.FixedFormEntry.php
   ...s.FixedFormRenderer.php
   class.FormRenderer.php
   class.FreeMEDSelfTest.php
   class.GeneralConfig.php
   class.GraphModule.php
   class.Guarantor.php
   class.Handler_HL7v2.php
   ...s.Handler_HL7v2_A04.php
   ...s.Handler_HL7v2_A08.php
   ...s.Handler_HL7v2_S12.php
   ...s.Handler_HL7v2_S15.php
   class.InsuranceCompany.php
   class.LanguageRegistry.php
   class.Ledger.php
   ...s.MaintenanceModule.php
   class.Messages.php
   class.OMBS_Patient.php
   class.OMBS_Wrapper.php
   class.Parser_HL7v2.php
   class.Patient.php
   class.Payer.php
   class.PHPlot.php
   class.Physician.php
   class.Procedure.php
   class.RecordLock.php
   class.Remitt.php
   class.ReportsModule.php
   class.rxlist.php
   class.Scheduler.php
   class.TeX.php
   class.User.php
   class.UtilityModule.php
   class.vCalendar.php
   class.vCalendarEvent.php
   error_handler.php
   freemed.php
   i18n.php
   iso-set.php
   macros.php
   mail-functions.php
   settings.php
   settings.php.tmpl
   xml.php
   xmlrpc_services.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
<?php
	// $Id: class.BillingModule.php,v 1.10 2005/07/06 00:06:44 rufustfirefly Exp $
	// desc: module prototype
	// lic : GPL, v2

LoadObjectDependency('_FreeMED.BaseModule');

class BillingModule extends BaseModule {

	// override variables
	var $CATEGORY_NAME = "Billing";
	var $CATEGORY_VERSION = "0.2";

	// vars to be passed from child modules
	var $order_field;
	var $form_vars;
	var $table_name;
	var $patient_forms;  // array of patient id's that we processed
	var $patient_procs;  // 2d array [patient][ids of procs processed]

	// contructor method
	function BillingModule () {
		// call parent constructor
		$this->BaseModule();
	} // end function BillingModule

	// override check_vars method
	function check_vars ($nullvar = "") {
		global $module;
		if (!isset($module))
		{
			trigger_error("No Module Defined", E_ERROR);
		}
		return true;
	} // end function check_vars

	// function main
	// - generic main function
	function main ($nullvar = "") {
		global $display_buffer;
		global $action;

		switch ($action) {
			case "add":
				$this->add();
				break;

			case "addform":
				$this->addform();
				break;

			case "del":
			case "delete":
				$this->del();
				break;

			case "mod":
			case "modify":
				$this->mod();
				break;

			case "modform":
				global $id;
				if (empty($id) or ($id<1)) {
					template_display();
				}
				$this->modform();
				break;

			case "transport":
				return $this->transport();
				break;

			case "view":
			default:
				$this->view();
				break;
		} // end switch action
	} // end function main

	// ********************** MODULE SPECIFIC ACTIONS *********************

	// function _add
	// - addition routine (can be overridden if need be)
	function _add () {
		global $display_buffer;
		reset ($GLOBALS);
		while (list($k,$v)=each($GLOBALS)) global $$k;

		$display_buffer .= "
			<P><CENTER>
			".__("Adding")." ...
		";

		$result = $sql->query (
			$sql->insert_query (
				$this->table_name,
				$this->variables
			)
		);

		if ($result) { $display_buffer .= "<B>".__("done").".</B>\n"; }
		 else        { $display_buffer .= "<B>".__("ERROR")."</B>\n"; }

		$display_buffer .= "
			</CENTER>
			<P>
			<CENTER>
				<A HREF=\"$this->page_name?module=$module\"
				>".__("back")."</A>
			</CENTER>
		";
	} // end function _add
	function add () { $this->_add(); }

	// function _del
	// - only override this if you *really* have something weird to do
	function _del () {
		global $display_buffer;
		global $id, $sql;
		$display_buffer .= "<P ALIGN=CENTER>".
			__("Deleting")." . . . \n";
		$query = "DELETE FROM $this->table_name ".
			"WHERE id = '".prepare($id)."'";
		$result = $sql->query ($query);
		if ($result) { $display_buffer .= __("done"); }
		 else        { $display_buffer .= "<FONT COLOR=\"#ff0000\">".__("ERROR")."</FONT>"; }
		$display_buffer .= "</P>\n";
	} // end function _del
	function del () { $this->_del(); }

	// function _mod
	// - modification routine (override if neccessary)
	function _mod () {
		global $display_buffer;
		reset ($GLOBALS);
		while (list($k,$v)=each($GLOBALS)) global $$k;

		$display_buffer .= "
			<P><CENTER>
			".__("Modifying")." ...
		";

		$result = $sql->query (
			$sql->update_query (
				$this->table_name,
				$this->variables,
				array (
					"id"	=>	$id
				)
			)
		);

		if ($result) { $display_buffer .= "<B>".__("done").".</B>\n"; }
		 else        { $display_buffer .= "<B>".__("ERROR")."</B>\n"; }

		$display_buffer .= "
			</CENTER>
			<P>
			<CENTER>
				<A HREF=\"$this->page_name?module=$module\"
				>".__("back")."</A>
			</CENTER>
		";
	} // end function _mod
	function mod() { $this->_mod(); }

	// function add/modform
	// - wrappers for form
	function addform () { $this->form(); }
	function modform () { $this->form(); }

	// function form
	// - add/mod form stub
	function form () {
		global $display_buffer;
		global $action, $id, $sql;

		if (is_array($this->form_vars)) {
			reset ($this->form_vars);
			while (list ($k, $v) = each ($this->form_vars)) global $$v;
		} // end if is array

		switch ($action) {
			case "addform":
				break;

			case "modform":
				$result = $sql->query ("SELECT * FROM ".$this->table_name.
					" WHERE ( id = '".prepare($id)."' )");
				$r = $sql->fetch_array ($result);
				extract ($r);
				break;
		} // end of switch action
		
	} // end function form

	// function view
	// - view stub
	function view () {
		global $display_buffer;
		global $sql;
		$result = $sql->query ("SELECT ".$this->order_fields." FROM ".
			$this->table_name." ORDER BY ".$this->order_fields);
		$display_buffer .= freemed_display_itemlist (
			$result,
			"module_loader.php",
			$this->form_vars,
			array ("", __("NO DESCRIPTION")),
			"",
			"t_page"
		);
	} // end function view

	function parent() {
		global $display_buffer;
		$display_buffer .= "parent parent<BR>";
	}


	function MakeDecimal($data,$places) {
		global $display_buffer;
		$data = bcadd($data,0,$places);
		$data = $this->CleanNumber($data);
		return $data;
	}


	// NewKey
	// Create billing control break key
	function NewKey($row) {
		global $display_buffer;
		// if any of these fields change while processing a
        // bill we need to cut a new bill

		$pos = $row["procpos"];
		$doc = $row["procphysician"];
		$ref = $row["procrefdoc"];
		$auth = $row["procauth"];
		$eoc = $row["proceoc"];
		$clmtp = $row["procclmtp"];
		$cov1 = $row["proccov1"];
		$cov2 = $row["proccov2"];

		$date = $row["procdt"];
		$date = str_replace("-","",$date);
		$datey = substr($date,0,4);
		$datem = substr($date,4,2);
		$newkey = $pos.$doc.$ref.$eoc.$clmtp.$auth.$cov1.$cov2.$datey.$datem;
		return $newkey;

	} // end newkey


	// all reporting data must stipped of junk
	// and all upper cased
	function CleanChar($data) {
		global $display_buffer;
		$data = stripslashes($data);
		$data = str_replace("/"," ",$data);
		$data = str_replace("'"," ",$data);
		$data = str_replace("-"," ",$data);
		$data = str_replace(";"," ",$data);
		$data = str_replace("(","",$data);
		$data = str_replace(")","",$data);
		$data = str_replace(":"," ",$data);
		$data = str_replace("."," ",$data);
		$data = str_replace(","," ",$data);
		$data = trim($data);
		$data = strtoupper($data);
            return $data;

	} // end cleanchar


	// all reporting data must stipped of junk
	// and all upper cased then for number all blanks
	// removed
	function CleanNumber($data) {
		global $display_buffer;

		$data = $this->CleanChar($data);
		$data = str_replace(" ","",$data);
		$data = trim($data);
		return $data;

	} // end cleannumber

	// see if any insurance bills are due
	function CheckforInsBills($covtypes="") {
		global $display_buffer;
		global $sql;

		if (empty($covtypes))
			$covs = array(PRIMARY,SECONDARY);
		else
			$covs = $covtypes;

		$c = count($covs);
		$where = "WHERE (";
		for ($i=0;$i<$c;$i++)
		{
			if ($i != 0)
				$where .= " OR ";
			$where .= "proccurcovtp='".$covs[$i]."'";

		}
		$where .= ")";
		//$display_buffer .= "$where<BR>";

	//		"WHERE (proccurcovtp='".PRIMARY."' OR proccurcovtp='".SECONDARY."')".
		$query = "SELECT DISTINCT procpatient,proccurcovid,proccurcovtp FROM procrec ".
			$where.
			" AND procbilled='0' AND procbillable='0' AND procbalcurrent>'0'";
		$result = $sql->query($query);
		if (!$sql->results($result))
			return 0;
		else
			return $result;

	}

	// get a list of bills due for this patient
    // coverage and type
	function GetProcstoBill($covid,$covtype,$covpatient,$forpat=0) {
		global $display_buffer;
		global $sql;

		if ($forpat==0) { // not doing patient bills
			if (!$covid)
				return 0;
			if (!$covtype)
				return 0;
		}
		if (!$covpatient) {
			return 0;
		}

		$query = "SELECT * FROM procrec ".
			"WHERE (proccurcovtp = '$covtype' AND ".
			"proccurcovid = '$covid' AND ".
			"procbalcurrent > '0' AND ".
			"procpatient = '$covpatient' AND ".
//			"procbillable = '0' AND ".
			"procbilled = '0') ".
			"ORDER BY procpos,procphysician,procrefdoc,proceoc,procclmtp,procauth,proccov1,proccov2,procdt";

		$result = $sql->query($query);

		if (!$sql->results($result)) {
			return 0;
		} else {
			return $result;
		}
	}

	// process the resulst set from above and call ProcCallBack
    // function at every control break passing it the like 
    // procedures in an array. User should suppliy the
    // the ProcCallBack method
	function MakeStack($result,$maxloop) {
		global $display_buffer;
		global $sql;

		if (!$result) {
			return 0;
		}
		if (!$maxloop) {
			return 0;
		}

		$first_procedure = 0;
		$proccount=0;
		$totprocs = 0;
    		$diagset = CreateObject('FreeMED.diagnosis_set');
		while ($r = $sql->fetch_array ($result)) 
		{
			if ($first_procedure == 0)
			{
				$prev_key = $this->NewKey($r);
				$diagset = CreateObject('FreeMED.diagnosis_set');
				$first_procedure = 1;
			}

			// keep tally on all bills billed for this patient
			$pat = $r[procpatient];
			$this->patient_procs[$pat][$totprocs] = $r[id];
			$totprocs++;

			$cur_key = $this->NewKey($r);

			if (!($diagset->testAddSet ($r[procdiag1], $r[procdiag2],
										$r[procdiag3], $r[procdiag4])) OR
					($proccount == $maxloop         )  OR
					($prev_key != $cur_key) )
			{
				if ($prev_key != $cur_key)
				{
					$prev_key = $cur_key;
					//$display_buffer .= "keychange $r[procphysician]<BR>";
				}

				$this->ProcCallBack($procstack);
				//call_user_method($callbackfunc,$callbackobj,$procstack);
				//$callback($procstack);
				// reset the diag_set array
				unset ($diagset);
				unset ($procstack);
				$proccount=0;
				$diagset = CreateObject('FreeMED.diagnosis_set');
				$test_AddSet = $diagset->testAddSet ($r[procdiag1], 
								$r[procdiag2], 
								$r[procdiag3], 
								$r[procdiag4]);
				if (!$test_AddSet) {
					$display_buffer .= "AddSet failed!!";
					template_display();
				}

			} 

			$procstack[$proccount] = $r;
			$proccount++;



		} // end of looping for all charges

		// check for last record
		if ($proccount > 0)
		{
			//call_user_method($callbackfunc,$callbackobj,$procstack);
			$this->ProcCallBack($procstack);
		}

	} // end Makestack

   
	// mark all the procedures as billed and add a 
	// billed ledger entry for each procedure 

	function MarkBilled() {
		global $display_buffer;

		reset ($GLOBALS);
		while (list($k,$v)=each($GLOBALS)) global $$k;

	   	if (count($processed)<1) 
		{
			$display_buffer .= "
		 	<P>
		 	<CENTER>
		  	<B>".__("Nothing set to be marked!")."</B>
		 	</CENTER>
		 	<P>
		 	<CENTER>
		  	<A HREF=\"$this->page_name?module=$module\"
		  	>".__("Return to Fixed Forms Generation Menu")."</A>
		 	</CENTER>
		 	<P>
			";
			return;
       	} 

     	for ($i=0;$i<count($processed);$i++) 
		{
       		$display_buffer .= "
       		Marking ".$processed[$i]." ...<BR> 
       		";
			$pat = $processed[$i];
			$procs = count($procids[$pat]);
			
			for ($x=0;$x<$procs;$x++)
			{
				$prc = $procids[$pat][$x];
				//$display_buffer .= "proc $prc for patient $pat<br/>";
       				// start of insert loop for billed legder entries
       				$query = "SELECT procbalcurrent,proccurcovid,proccurcovtp FROM procrec";
				$query .= " WHERE id='".$prc."'";
	       			$result = $sql->query($query);
       				if (!$result) {
	       				$display_buffer .= "Mark failed getting procrecs<br/>";
					template_display();
       				}
				//$display_buffer .= "proc query $query<BR>";
       				$bill_tran = $sql->fetch_array($result);
       				$cur_bal = $bill_tran[procbalcurrent];
          			$proc_id = $bill_tran[id];
          			$cov_id  = $bill_tran[proccurcovid];
          			$cov_tp  = $bill_tran[proccurcovtp];
				$payreccat = BILLED;
				$query = $sql->insert_query("payrec",
					array (
						"payrecdtadd" => $cur_date,
						"payrecdtmod" => $cur_date,
						"payrecpatient" => $pat,
						"payrecdt" => $cur_date,
						"payreccat" => $payreccat,
						"payrecproc" => $prc,
						"payreclink" => $cov_id,
						"payrecsource" => $cov_tp,
						"payrecamt" => $cur_bal,
						"payrecdescrip" => "Billed",
						"payreclock" => "unlocked"
						)	
					);
				//$display_buffer .= "payrec insert query $query<BR>";
           			$pay_result = $sql->query ($query);
	           		if ($pay_result) {
		               		$display_buffer .= __("Adding Bill Date to ledger.")."<br/>\n";
	           		} else {
               				$display_buffer .= __("Failed Adding Bill Date to ledger!!")."<br/>\n";
				}

       				$query = "UPDATE procrec SET procbilled = '1',procdtbilled = '".addslashes($cur_date)."'".
						 " WHERE id = '".$prc."'";
				//$display_buffer .= "procrec update query $query<BR>";
       				$proc_result = $sql->query ($query);
       				if ($result) { 
					$display_buffer .= __("done").".<BR>\n"; 
				} else { 
					$display_buffer .= __("ERROR")."<BR>\n"; 
				}

			} // end proces for patient loop
		
	     	} // end for processed
     	$display_buffer .= "
      	<P>
      	<CENTER>
       	<A HREF=\"$this->page_name?module=$module\"
       	>".__("Back")."</A>
      	</CENTER>
      	<P>
     	";
	} // end markbilled

	function GetRelationShip($rel,$type="NSF") {
		global $display_buffer;
		if ($type=="NSF")
		{
			if ($rel == "S")
				$patrel = "01";
			if ($rel == "LR")  // medicare legal rep
				$patrel = "01";
			if ($rel == "H" OR $rel == "W")
				$patrel = "02";
			if ($rel == "C")
				$patrel = "03";
			if ($rel == "D") // Natural Child insured not financially resp.
				$patrel = "04";
			if ($rel == "SC") // Step child
				$patrel = "05";
			if ($rel == "FC") // Foster child
				$patrel = "06";
			if ($rel == "WC") // Ward of Court
				$patrel = "07";
			if ($rel == "HD") // Handicapped Dependent
				$patrel = "10";
			if ($rel == "SD") // Sponsered Dependent
				$patrel = "16";

			return $patrel;

		}


	}

} // end class BillingModule

?>

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us