added a Test Scheduling type to patient notes
[openemr.git] / accounting / ws_server_26.pl
blobf88b1518a8a0a845d0ad260471d58c3bad513d29
1 #!/usr/bin/perl
3 ######################################################################
4 # This module is compatible only with SQL-Ledger version 2.6.x.
5 # Copy it to your SQL-Ledger installation directory as ws_server.pl.
6 ######################################################################
8 use Frontier::Responder;
9 use DBI;
11 ######################################################################
12 # IMPORTANT - modify this to point to your SQL-Ledger installation!
13 ######################################################################
14 use lib qw (/var/www/sql-ledger);
16 use SL::User;
17 use SL::Form;
18 use SL::CT;
19 use SL::HR;
20 use SL::IS;
21 use SL::IC;
22 use SL::AA;
24 require "sql-ledger.conf";
26 my $add_customer = \&rpc_add_customer;
27 my $add_salesman = \&rpc_add_employee;
28 my $add_invoice = \&rpc_add_invoice;
29 my $customer_balance = \&rpc_customer_balance;
31 my $res = Frontier::Responder->new( methods => {
32 'ezybiz.add_invoice' => $add_invoice,
33 'ezybiz.add_salesman' => $add_salesman,
34 'ezybiz.customer_balance' =>$customer_balance,
35 'ezybiz.add_customer' => $add_customer
36 }, );
38 print $res->answer;
40 sub rpc_customer_balance {
41 my ($post_hash) = @_;
42 if ($$post_hash{id} > 0 ) {
43 my $myconfig = new User "$memberfile", "$oemr_username";
44 $myconfig->{dbpasswd} = unpack 'u', $myconfig->{dbpasswd};
45 my $form = new Form;
46 $form->{title} = "AR Outstanding";
47 $form->{outstanding} = "1";
48 $form->{customer_id} = $$post_hash{id};
49 $form->{sort} = "transdate" ;
50 $form->{l_due} = 1;
51 $form->{nextsub} = "transaction";
52 $form->{vc} = "customer" ;
53 $form->{action} = 'Continue';
55 AA::transactions("",\%$myconfig, \%$form);
57 my ($paid,$amount) = 0;
59 # Exclude invoices that are not yet due (i.e. waiting for insurance).
61 my @now = localtime;
62 my $today = sprintf("%04u-%02u-%02u", $now[5] + 1900, $now[4] + 1, $now[3]);
63 foreach my $resref (@{$$form{transactions}}) {
64 my $duedate = substr($$resref{duedate}, 6) . "-" . substr($$resref{duedate}, 0, 5);
65 if ($duedate le $today) {
66 $paid += $$resref{paid};
67 $amount += $$resref{amount};
71 my $retval = $amount - $paid;
72 return($retval);
76 sub rpc_add_customer
78 use lib '/usr/lib/perl5/site_perl/5.8.3';
80 my ($post_hash) = @_;
82 #take struct of data and map to post data to create the customer, return the id
83 my $myconfig = new User "$memberfile", "$oemr_username";
84 $myconfig->{dbpasswd} = unpack 'u', $myconfig->{dbpasswd};
85 my $form = new Form;
86 $form->{name} = substr($$post_hash{'firstname'} . " " . $$post_hash{'lastname'}, 0, 64);
87 $form->{discount} = "";
88 $form->{terms} = "";
89 $form->{taxincluded} = "1";
90 $form->{creditlimit} = "0";
91 $form->{id} = $$post_hash{'foreign_id'};
92 $form->{login} = "";
93 $form->{employee} = "";
94 $form->{pricegroup} = "";
95 $form->{business} = "";
96 $form->{language} = "";
97 $form->{address1} = substr($$post_hash{'address'}, 0, 32);
98 $form->{address2} = substr($$post_hash{'address'}, 32, 32);
99 $form->{city} = substr($$post_hash{'suburb'}, 0, 32);
101 if($$post_hash{'state'}){
102 $form->{state} = substr($$post_hash{'state'}, 0, 32);
103 }else{
104 $form->{state} = substr($$post_hash{'geo_zone_id'}, 0, 32);
106 $form->{zipcode} = substr($$post_hash{'postcode'}, 0, 10);
107 $form->{country} = "";
108 $form->{contact} = "";
109 $form->{phone} = substr($$post_hash{'phone1'}, 0, 20);
110 $form->{fax} = "";
111 $form->{email} = $$post_hash{'email'};
112 $form->{taxnumber} = substr($$post_hash{'ssn'}, 0, 32);
113 $form->{curr} = "USD";
114 $form->{customernumber} = $$post_hash{'customernumber'};
115 @t = localtime(time);
116 $dd = $t[3];
117 $mm = $t[4] + 1;
118 $yy = $t[5] + 1900;
120 $form->{startdate} = "$mm-$dd-$yy";
122 CT::save_customer('', \%$myconfig, \%$form);
123 my $retVal = $form->{id};
125 return($retVal);
128 sub rpc_add_employee
130 my ($post_hash) = @_;
131 my $myconfig = new User "$memberfile", "$oemr_username";
132 $myconfig->{dbpasswd} = unpack 'u', $myconfig->{dbpasswd};
133 my $form = new Form;
134 $form->{id} = $$post_hash{'foreign_id'};
135 $form->{name} = $$post_hash{'fname'} . " " . $$post_hash{'lname'};
136 $form->{sales} = $$post_hash{'authorized'};
137 @t = localtime(time);
138 $dd = $t[3];
139 $mm = $t[4] + 1;
140 $yy = $t[5] + 1900;
142 $form->{startdate} = "$mm-$dd-$yy";
143 HR::save_employee("",\%$myconfig, \%$form);
144 my $retVal = $form->{id};
145 return($retVal);
148 sub rpc_add_invoice
150 my ($post_hash) = @_;
152 my $myconfig = new User "$memberfile", "$oemr_username";
153 $myconfig->{dbpasswd} = unpack 'u', $myconfig->{dbpasswd};
154 my $form = new Form;
155 $form->{id};
156 $form->{employee} = "--" . $$post_hash{'salesman'};
157 $form->{customer_id} = $$post_hash{'customerid'};
158 $form->{invnumber} = $$post_hash{'invoicenumber'};
159 $form->{amount} = $$post_hash{'total'};
160 $form->{netamount} = $$post_hash{'total'};
161 $form->{notes} = $$post_hash{'notes'};
162 $form->{department} = "";
163 $form->{currency} = "USD";
164 $form->{defaultcurrency} = "USD";
166 # This is the AR account number, needed by IS::post_invoice.
167 $form->{AR} = $oemr_ar_acc;
169 # This will use the posting date as the billing date
170 @t = localtime(time);
171 $dd = $t[3];
172 $mm = $t[4] + 1;
173 $yy = $t[5] + 1900;
175 $form->{transdate} = sprintf("%02u-%02u-%04u", $t[4] + 1, $t[3], $t[5] + 1900);
178 #Uncoment this following line if you rather use the DOS as the billing date.
180 # $form->{transdate} = $$post_hash{'dosdate'};
183 # If there is insurance, set a future due date so we don't bother
184 # the patient for a while.
186 if ($$post_hash{'payer_id'}) {
187 @t = localtime(60 * 60 * 24 * $oemr_due_days + time);
188 $form->{duedate} = sprintf("%02u-%02u-%04u", $t[4] + 1, $t[3], $t[5] + 1900);
189 } else {
190 $form->{duedate} = $form->{transdate};
193 # Get out if the invoice already exists.
194 my $trans_id = 0;
195 my $dbh = $form->dbconnect($myconfig);
196 my $query = qq|SELECT id FROM ar WHERE invnumber = ?|;
197 my $eth = $dbh->prepare($query) || die "Failed to prepare ar query";
198 $eth->execute($$post_hash{'invoicenumber'}) || die "Failed to execute ar query";
199 ($trans_id) = $eth->fetchrow_array;
200 $eth->finish;
202 if ($trans_id) {
203 print STDERR "Skipping invoice $trans_id = " . $$post_hash{'invoicenumber'} . "\n";
204 $dbh->disconnect;
205 return 0;
208 #loop through line items and add them to invoice
209 my $i = 1;
210 my $j = 1; #this is for copays should only be one but who knows -j
211 my $count = 0;
212 my $items = $$post_hash{'items'};
214 foreach my $line_item (@$items)
216 if($$line_item{'itemtext'} =~ /COPAY/){
217 $form->{"datepaid_$j"} = "$mm-$dd-$yy";
218 $form->{"source_$j"} = "Co-pay";
219 $form->{"memo_$j"} ='Co-pay';
220 $form->{"paid_$j"} = abs($$line_item{'price'});
221 $form->{"AR_paid_$j"} = "$oemr_cash_acc" . "--";
222 $j++;
224 else{
226 my $chart_id = 0;
227 my $query = qq|SELECT id FROM chart WHERE accno = ?|;
228 my $eth = $dbh->prepare($query) || die "Failed to prepare chart query";
229 $eth->execute($$line_item{'glaccountid'}) || die "Failed to execute chart query";
230 ($chart_id) = $eth->fetchrow_array;
231 $eth->finish;
233 $form->{"qty_$i"} = $$line_item{'qty'};
234 $form->{"discount_$i"} = 0;
235 $form->{"sellprice_$i"} = $$line_item{'price'};
237 $form->{taxincluded} = 1;
238 $form->{"taxaccounts_$i"} = 0;
239 $form->{"income_accno_$i"} = $$line_item{'glaccountid'};
240 $form->{"income_accno_id_$i"} = $chart_id;
242 $form->{"id_$i"} = add_goodsandservices(\%$myconfig, \%$form, $oemr_services_partnumber,
243 'Medical Services', '');
245 $form->{"description_$i"} = $$line_item{'itemtext'};
246 $form->{"unit_$i"} = '';
247 $form->{"serialnumber_$i"} = $$line_item{'maincode'};
249 # Save the insurance company ID as the SL project ID. This gives us a way
250 # to associate each invoice item with its insurance payer. The clinic will
251 # probably want to write some reporting software taking advantage of this.
253 $form->{"projectnumber_$i"} = "--" . $$post_hash{'payer_id'}
254 if ($$post_hash{'payer_id'});
255 $i++;
259 $dbh->disconnect;
261 $form->{paidaccounts} = $j - 1;
262 $form->{rowcount} = $i - 1;
263 IS::post_invoice("", \%$myconfig, \%$form);
264 my $retVal = $form->{id};
265 return($retVal);
268 sub get_partid
270 my ($myconfig, $form, $number) = @_;
271 my $retval = 0;
272 # connect to database
273 my $dbh = $form->dbconnect($myconfig);
275 my $query = qq|SELECT id FROM parts WHERE partnumber = ?|;
276 my $eth = $dbh->prepare($query) || die "Failed to create select id from parts query";
277 $eth->execute($number) || die "Failed to execute select id from parts query";
278 ($retval) = $eth->fetchrow_array;
279 $eth->finish;
280 $dbh->disconnect;
281 return($retval);
284 sub add_goodsandservices
286 my ($myconfig, $form, $code, $desc, $price) = @_;
287 my $retval = 0;
288 $retval = get_partid($myconfig, $form, $code);
290 if($retval == 0)
292 # connect to database, turn off autocommit
293 my $dbh = $form->dbconnect_noauto($myconfig);
294 my $query = qq|insert into parts (partnumber,description,listprice,sellprice) values(?,?,?,?)|;
295 my $eth = $dbh->prepare($query) || die "failed to create insert into parts query" . $dbh->errstr;
296 $eth->execute($code,$desc,$price,$price) || die "failed to execute insert into parts query" . $dbh->errstr;
297 $dbh->commit || die $dbh->errstr;
298 $eth->finish || die "cannot finish " . $dbh->errstr;
299 $dbh->disconnect;
300 $retval = get_partid($myconfig, $form, $code);
303 return($retval);