added appointments-encounters report
[openemr.git] / library / billrep.inc
blob2a9408f518e8a3251db13c69d317362a9a379f3d
1 <?php
2         require_once("{$GLOBALS['srcdir']}/sql.inc");
4         //date must be in nice format (e.g. 2002-07-11)
6 //      function getBillsBy($date, $qtype = "day", $auth="%", $cols = "date,pid,code_type,code,user,authorized")
7 //      {
8 //              if ($qtype == "all") {
9 //                      $res = sqlStatement("select $cols from billing order by date ASC");
10 //              }
11 //              elseif ($qtype == "day") {
12 //                      $res = sqlStatement("select $cols from billing where date like '$date' order by date ASC");
13 //              }
14 //              elseif ($qtype == "month") {
15 //                      $m = substr($date,5,2);
16 //                      $y = substr($date,0,4);
17 //                      $res = sqlStatement("select $cols from billing where MONTH(date) = '$m' and YEAR(date) = '$y' ORDER by date ASC");
18 //              }
19 //              elseif ($qtype == "year") {
20 //                      $y = substr($date,0,4);
21 //                      $res = sqlStatement("select $cols from billing where YEAR(date) = '$y' ORDER by date ASC");
22 //              }
24 //              for($iter=0; $row=sqlFetchArray($res); $iter++)
25 //              {
26 //                      $all[$iter] = $row;
27 //              }
29 //              return $all;
30 //      }
32         function getBillsBetween($date, $date2, $auth="%", $unbilled, $code_type,
33                 $cols = "id,date,pid,code_type,code,user,authorized,x12_partner_id")
34         {
35                 $billstring = "billing.billed = '0'";
36                 if ($unbilled == "0") {
37                         //3 is an error condition
38                         $billstring = "billing.billed = '0' or (billing.billed = '1' and billing.bill_process = '3')";
39                 }
40                 else {
41                         $billstring = "billing.billed = '1'";
42                 }
44 //      $sql = "select * from billing where date >= '$date' and " .
45 //              "date <= '$date2 23:59:59' and authorized like '$auth' and " .
46 //              "($billstring) and code_type like '$code_type' and activity = 1 " .
47 //              "order by encounter, pid, code_type, code ASC";
49                 // Selecting by the date in the billing table is wrong, because that is
50                 // just the data entry date; instead we want to go by the encounter date
51                 // which is the date in the form_encounter table.
52                 //
53                 $sql = "select form_encounter.date as enc_date, billing.* " .
54                         "from form_encounter, billing where " .
55                         "form_encounter.date >= '$date' and " .
56                         "form_encounter.date <= '$date2 23:59:59' and " .
57                         "billing.encounter = form_encounter.encounter and " .
58                         "billing.authorized like '$auth' and " .
59                         "($billstring) and " .
60                         "billing.code_type like '$code_type' and " .
61                         "billing.activity = 1 " .
62                         "order by billing.encounter, billing.pid, billing.code_type, billing.code ASC";
64                 $res = sqlStatement($sql);
66                 for($iter=0; $row=sqlFetchArray($res); $iter++)
67                 {
68                         $all[$iter] = $row;
69                 }
71                 return $all;
72         }
74         function getBillsListBetween($date, $date2, $auth="%", $unbilled, $code_type,
75                 $cols = "billing.id, form_encounter.date, billing.pid, billing.code_type, billing.code, billing.user")
76         {
77                 $billstring = "billed = '0'";
78                 if ($unbilled == "0") {
79                         //3 is an error condition
80                         $billstring = "billed = '0' or (billed = '1' and bill_process = '3')";
81                 }
82                 else {
83                         $billstring = "billed = '1'";
84                 }
86 //      $sql = "select $cols from billing where date >= '$date' and date <= '$date2 23:59:59' and authorized like '$auth' and ($billstring) and code_type like '$code_type' and activity = 1 order by pid,date ASC";
88                 // See above comment in getBillsBetween().
89                 //
90                 $sql = "select $cols " .
91                         "from form_encounter, billing where " .
92                         "form_encounter.date >= '$date' and " .
93                         "form_encounter.date <= '$date2 23:59:59' and " .
94                         "billing.encounter = form_encounter.encounter and " .
95                         "billing.authorized like '$auth' and " .
96                         "($billstring) and " .
97                         "billing.code_type like '$code_type' and " .
98                         "billing.activity = 1 " .
99                         "order by billing.pid, billing.date ASC";
101                 $res = sqlStatement($sql);
102                 //print "select $cols from billing where date >= '$date' and date <= '$date2 23:59:59' and authorized like '$auth' and billed like '$unbilled' and code_type like '$code_type' order by pid,date ASC";
103                 $string = "( ";
104                 for($iter=0; $row=sqlFetchArray($res); $iter++)
105                 {
106                         $string .= $row{"id"}.",";
107                 }
108                 $string = substr($string,0,strlen($string)-1);
109                 $string .= ")";
110                 return $string;
111         }
113 // function billCodesBetween($date, $date2, $auth="%", $code_type)
114 // {
115 //      sqlStatement("update billing set billed=1 where date >= '$date' and date <= '$date2 23:59:59' and authorized like '$auth' and code_type like '$code_type'");
116 // }
118         function billCodesList($list,$skip = "()") {
119                 if ($list == "()")
120                         return;
122                 if ($skip == "()")
123                         sqlStatement("update billing set billed=1 where id in $list");
124                 else
125                         sqlStatement("update billing set billed=1 where id in $list and id not in $skip");
127                 return;
128         }
130 //      function getCodeTypes () {
131 //              $res = sqlStatement("select distinct code_type from billing order by code_type");
133 //              for($iter=0; $row=sqlFetchArray($res); $iter++)
134 //              {
135 //                      $all[$iter] = $row;
136 //              }
138 //              return $all;
139 //      }