To make it easy to download the latest version from online CVS browser.
[openemr.git] / accounting / ws_server.pl
blob1dbb978ee715296df0d58f20bb9a7c461a36a515
1 #!/usr/bin/perl
3 use Frontier::Responder;
4 use DBI;
6 # You need to modify this as needed:
7 use lib qw (/srv/www/sql-ledger);
9 use SL::User;
10 use SL::Form;
11 use SL::CT;
12 use SL::HR;
13 use SL::IS;
14 use SL::IC;
15 use SL::AR;
16 require "sql-ledger.conf";
18 my $add_customer = \&rpc_add_customer;
19 my $add_salesman = \&rpc_add_employee;
20 my $add_invoice = \&rpc_add_invoice;
21 my $customer_balance = \&rpc_customer_balance;
23 my $res = Frontier::Responder->new( methods => {
24 'ezybiz.add_invoice' => $add_invoice,
25 'ezybiz.add_salesman' => $add_salesman,
26 'ezybiz.customer_balance' =>$customer_balance,
27 'ezybiz.add_customer' => $add_customer
28 }, );
30 print $res->answer;
32 sub rpc_customer_balance {
33 my ($post_hash) = @_;
34 if ($$post_hash{id} > 0 ) {
35 my $myconfig = new User "$memberfile", "$oemr_username";
36 $myconfig->{dbpasswd} = unpack 'u', $myconfig->{dbpasswd};
37 my $form = new Form;
38 $form->{title} = "AR Outstanding";
39 $form->{outstanding} = "1";
40 $form->{customer_id} = $$post_hash{id};
41 $form->{sort} = "transdate" ;
42 $form->{l_due} = 1;
43 $form->{nextsub} = "ar_transaction";
44 $form->{action} = 'Continue';
46 AR::ar_transactions("",\%$myconfig, \%$form);
47 my ($paid,$amount) =0;
49 # Exclude invoices that are not yet due (i.e. waiting for insurance).
51 my @now = localtime;
52 my $today = sprintf("%04u-%02u-%02u", $now[5] + 1900, $now[4] + 1, $now[3]);
53 foreach my $resref (@{$$form{transactions}}) {
54 my $duedate = substr($$resref{duedate}, 6) . "-" . substr($$resref{duedate}, 0, 5);
55 if ($duedate le $today) {
56 $paid += $$resref{paid};
57 $amount += $$resref{amount};
61 my $retval = $amount - $paid;
62 return($retval);
66 sub rpc_add_customer
68 use lib '/usr/lib/perl5/site_perl/5.8.3';
70 my ($post_hash) = @_;
72 #take struct of data and map to post data to create the customer, return the id
73 my $myconfig = new User "$memberfile", "$oemr_username";
74 $myconfig->{dbpasswd} = unpack 'u', $myconfig->{dbpasswd};
75 my $form = new Form;
76 $form->{name} = substr($$post_hash{'firstname'} . " " . $$post_hash{'lastname'}, 0, 64);
77 $form->{discount} = "";
78 $form->{terms} = "";
79 $form->{taxincluded} = "1";
80 $form->{creditlimit} = "0";
81 $form->{id} = $$post_hash{'foreign_id'};
82 $form->{login} = "";
83 $form->{employee} = "";
84 $form->{pricegroup} = "";
85 $form->{business} = "";
86 $form->{language} = "";
87 $form->{address1} = substr($$post_hash{'address'}, 0, 32);
88 $form->{address2} = substr($$post_hash{'address'}, 32, 32);
89 $form->{city} = substr($$post_hash{'suburb'}, 0, 32);
91 if($$post_hash{'state'}){
92 $form->{state} = substr($$post_hash{'state'}, 0, 32);
93 }else{
94 $form->{state} = substr($$post_hash{'geo_zone_id'}, 0, 32);
96 $form->{zipcode} = substr($$post_hash{'postcode'}, 0, 10);
97 $form->{country} = "";
98 $form->{contact} = "";
99 $form->{phone} = substr($$post_hash{'phone1'}, 0, 20);
100 $form->{fax} = "";
101 $form->{email} = $$post_hash{'email'};
102 $form->{taxnumber} = substr($$post_hash{'ssn'}, 0, 32);
103 $form->{curr} = "USD";
104 $form->{customernumber} = $$post_hash{'customernumber'};
105 @t = localtime(time);
106 $dd = $t[3];
107 $mm = $t[4] + 1;
108 $yy = $t[5] + 1900;
110 $form->{startdate} = "$mm-$dd-$yy";
112 CT::save_customer('', \%$myconfig, \%$form);
113 my $retVal = $form->{id};
115 return($retVal);
118 sub rpc_add_employee
120 my ($post_hash) = @_;
121 my $myconfig = new User "$memberfile", "$oemr_username";
122 $myconfig->{dbpasswd} = unpack 'u', $myconfig->{dbpasswd};
123 my $form = new Form;
124 $form->{id} = $$post_hash{'foreign_id'};
125 $form->{name} = $$post_hash{'fname'} . " " . $$post_hash{'lname'};
126 $form->{sales} = $$post_hash{'authorized'};
127 @t = localtime(time);
128 $dd = $t[3];
129 $mm = $t[4] + 1;
130 $yy = $t[5] + 1900;
132 $form->{startdate} = "$mm-$dd-$yy";
133 HR::save_employee("",\%$myconfig, \%$form);
134 my $retVal = $form->{id};
135 return($retVal);
138 sub rpc_add_invoice
140 my ($post_hash) = @_;
142 my $myconfig = new User "$memberfile", "$oemr_username";
143 $myconfig->{dbpasswd} = unpack 'u', $myconfig->{dbpasswd};
144 my $form = new Form;
145 $form->{id};
146 $form->{employee} = "--" . $$post_hash{'salesman'};
147 $form->{customer_id} = $$post_hash{'customerid'};
148 $form->{invnumber} = $$post_hash{'invoicenumber'};
149 $form->{amount} = $$post_hash{'total'};
150 $form->{netamount} = $$post_hash{'total'};
151 $form->{notes} = $$post_hash{'notes'};
152 $form->{department} = "";
153 $form->{currency} = "USD";
154 $form->{defaultcurrency} = "USD";
156 # This is the AR account number, needed by IS::post_invoice.
157 $form->{AR} = $oemr_ar_acc;
159 # This will use the posting date as the billing date
160 @t = localtime(time);
161 $dd = $t[3];
162 $mm = $t[4] + 1;
163 $yy = $t[5] + 1900;
165 $form->{transdate} = sprintf("%02u-%02u-%04u", $t[4] + 1, $t[3], $t[5] + 1900);
167 # This overrides the above statement to use the date of service as the
168 # invoice date, which should be preferable for most practices. Comment
169 # out the following line if you really want the billing date instead.
171 $form->{transdate} = $$post_hash{'dosdate'};
173 # If there is insurance, set a future due date so we don't bother
174 # the patient for a while.
176 if ($$post_hash{'payer_id'}) {
177 @t = localtime(60 * 60 * 24 * $oemr_due_days + time);
178 $form->{duedate} = sprintf("%02u-%02u-%04u", $t[4] + 1, $t[3], $t[5] + 1900);
179 } else {
180 $form->{duedate} = $form->{transdate};
183 # Get out if the invoice already exists.
184 my $trans_id = 0;
185 my $dbh = $form->dbconnect($myconfig);
186 my $query = qq|SELECT id FROM ar WHERE invnumber = ?|;
187 my $eth = $dbh->prepare($query) || die "Failed to prepare ar query";
188 $eth->execute($$post_hash{'invoicenumber'}) || die "Failed to execute ar query";
189 ($trans_id) = $eth->fetchrow_array;
190 $eth->finish;
191 $dbh->disconnect;
192 if ($trans_id) {
193 print STDERR "Skipping invoice $trans_id = " . $$post_hash{'invoicenumber'} . "\n";
194 return 0;
197 #loop through line items and add them to invoice
198 my $i = 1;
199 my $j = 1; #this is for copays should only be one but who knows -j
200 my $count = 0;
201 my $items = $$post_hash{'items'};
203 foreach my $line_item (@$items)
205 if($$line_item{'itemtext'} =~ /COPAY/){
206 $form->{"datepaid_$j"} = "$mm-$dd-$yy";
207 $form->{"source_$j"} = "Co-pay";
208 $form->{"memo_$j"} ='Co-pay';
209 $form->{"paid_$j"} = abs($$line_item{'price'});
210 $form->{"AR_paid_$j"} = "$oemr_cash_acc" . "--";
211 $j++;
213 else{
214 $form->{"qty_$i"} = $$line_item{'qty'};
215 $form->{"discount_$i"} = 0;
216 $form->{"sellprice_$i"} = $$line_item{'price'};
218 $form->{taxincluded} = 1;
219 $form->{"taxaccounts_$i"} = 0;
220 $form->{"income_accno_$i"} = $$line_item{'glaccountid'};
222 $form->{"id_$i"} = add_goodsandservices(\%$myconfig, \%$form, $oemr_services_partnumber,
223 'Medical Services', '');
225 $form->{"description_$i"} = $$line_item{'itemtext'};
226 $form->{"unit_$i"} = '';
227 $form->{"serialnumber_$i"} = $$line_item{'maincode'};
229 # Save the insurance company ID as the SL project ID. This gives us a way
230 # to associate each invoice item with its insurance payer. The clinic will
231 # probably want to write some reporting software taking advantage of this.
233 $form->{"projectnumber_$i"} = "--" . $$post_hash{'payer_id'}
234 if ($$post_hash{'payer_id'});
235 $i++;
239 $form->{paidaccounts} = $j - 1;
240 $form->{rowcount} = $i - 1;
241 IS::post_invoice("", \%$myconfig, \%$form);
242 my $retVal = $form->{id};
243 return($retVal);
246 sub get_partid
248 my ($myconfig, $form, $number) = @_;
249 my $retval = 0;
250 # connect to database
251 my $dbh = $form->dbconnect($myconfig);
253 my $query = qq|SELECT id FROM parts WHERE partnumber = ?|;
254 my $eth = $dbh->prepare($query) || die "Failed to create select id from parts query";
255 $eth->execute($number) || die "Failed to execute select id from parts query";
256 ($retval) = $eth->fetchrow_array;
257 $eth->finish;
258 $dbh->disconnect;
259 return($retval);
262 sub add_goodsandservices
264 my ($myconfig, $form, $code, $desc, $price) = @_;
265 my $retval = 0;
266 $retval = get_partid($myconfig, $form, $code);
268 if($retval == 0)
270 # connect to database, turn off autocommit
271 my $dbh = $form->dbconnect_noauto($myconfig);
272 my $query = qq|insert into parts (partnumber,description,listprice,sellprice) values(?,?,?,?)|;
273 my $eth = $dbh->prepare($query) || die "failed to create insert into parts query" . $dbh->errstr;
274 $eth->execute($code,$desc,$price,$price) || die "failed to execute insert into parts query" . $dbh->errstr;
275 $dbh->commit || die $dbh->errstr;
276 $eth->finish || die "cannot finish " . $dbh->errstr;
277 $dbh->disconnect;
278 $retval = get_partid($myconfig, $form, $code);
281 return($retval);