added support for sql-ledger 2.6.x
[openemr.git] / accounting / ws_server_26.pl
blobf47d4a5350b800e8c966239c8667c7de95f29b6a
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 @t = localtime(time);
170 $dd = $t[3];
171 $mm = $t[4] + 1;
172 $yy = $t[5] + 1900;
174 $form->{transdate} = sprintf("%02u-%02u-%04u", $t[4] + 1, $t[3], $t[5] + 1900);
176 # If there is insurance, set a future due date so we don't bother
177 # the patient for a while.
179 if ($$post_hash{'payer_id'}) {
180 @t = localtime(60 * 60 * 24 * $oemr_due_days + time);
181 $form->{duedate} = sprintf("%02u-%02u-%04u", $t[4] + 1, $t[3], $t[5] + 1900);
182 } else {
183 $form->{duedate} = $form->{transdate};
186 # Get out if the invoice already exists.
187 my $trans_id = 0;
188 my $dbh = $form->dbconnect($myconfig);
189 my $query = qq|SELECT id FROM ar WHERE invnumber = ?|;
190 my $eth = $dbh->prepare($query) || die "Failed to prepare ar query";
191 $eth->execute($$post_hash{'invoicenumber'}) || die "Failed to execute ar query";
192 ($trans_id) = $eth->fetchrow_array;
193 $eth->finish;
195 if ($trans_id) {
196 print STDERR "Skipping invoice $trans_id = " . $$post_hash{'invoicenumber'} . "\n";
197 $dbh->disconnect;
198 return 0;
201 #loop through line items and add them to invoice
202 my $i = 1;
203 my $j = 1; #this is for copays should only be one but who knows -j
204 my $count = 0;
205 my $items = $$post_hash{'items'};
207 foreach my $line_item (@$items)
209 if($$line_item{'itemtext'} =~ /COPAY/){
210 $form->{"datepaid_$j"} = "$mm-$dd-$yy";
211 $form->{"source_$j"} = "Co-pay";
212 $form->{"memo_$j"} ='Co-pay';
213 $form->{"paid_$j"} = abs($$line_item{'price'});
214 $form->{"AR_paid_$j"} = "$oemr_cash_acc" . "--";
215 $j++;
217 else{
219 my $chart_id = 0;
220 my $query = qq|SELECT id FROM chart WHERE accno = ?|;
221 my $eth = $dbh->prepare($query) || die "Failed to prepare chart query";
222 $eth->execute($$line_item{'glaccountid'}) || die "Failed to execute chart query";
223 ($chart_id) = $eth->fetchrow_array;
224 $eth->finish;
226 $form->{"qty_$i"} = $$line_item{'qty'};
227 $form->{"discount_$i"} = 0;
228 $form->{"sellprice_$i"} = $$line_item{'price'};
230 $form->{taxincluded} = 1;
231 $form->{"taxaccounts_$i"} = 0;
232 $form->{"income_accno_$i"} = $$line_item{'glaccountid'};
233 $form->{"income_accno_id_$i"} = $chart_id;
235 $form->{"id_$i"} = add_goodsandservices(\%$myconfig, \%$form, $oemr_services_partnumber,
236 'Medical Services', '');
238 $form->{"description_$i"} = $$line_item{'itemtext'};
239 $form->{"unit_$i"} = '';
240 $form->{"serialnumber_$i"} = $$line_item{'maincode'};
242 # Save the insurance company ID as the SL project ID. This gives us a way
243 # to associate each invoice item with its insurance payer. The clinic will
244 # probably want to write some reporting software taking advantage of this.
246 $form->{"projectnumber_$i"} = "--" . $$post_hash{'payer_id'}
247 if ($$post_hash{'payer_id'});
248 $i++;
252 $dbh->disconnect;
254 $form->{paidaccounts} = $j - 1;
255 $form->{rowcount} = $i - 1;
256 IS::post_invoice("", \%$myconfig, \%$form);
257 my $retVal = $form->{id};
258 return($retVal);
261 sub get_partid
263 my ($myconfig, $form, $number) = @_;
264 my $retval = 0;
265 # connect to database
266 my $dbh = $form->dbconnect($myconfig);
268 my $query = qq|SELECT id FROM parts WHERE partnumber = ?|;
269 my $eth = $dbh->prepare($query) || die "Failed to create select id from parts query";
270 $eth->execute($number) || die "Failed to execute select id from parts query";
271 ($retval) = $eth->fetchrow_array;
272 $eth->finish;
273 $dbh->disconnect;
274 return($retval);
277 sub add_goodsandservices
279 my ($myconfig, $form, $code, $desc, $price) = @_;
280 my $retval = 0;
281 $retval = get_partid($myconfig, $form, $code);
283 if($retval == 0)
285 # connect to database, turn off autocommit
286 my $dbh = $form->dbconnect_noauto($myconfig);
287 my $query = qq|insert into parts (partnumber,description,listprice,sellprice) values(?,?,?,?)|;
288 my $eth = $dbh->prepare($query) || die "failed to create insert into parts query" . $dbh->errstr;
289 $eth->execute($code,$desc,$price,$price) || die "failed to execute insert into parts query" . $dbh->errstr;
290 $dbh->commit || die $dbh->errstr;
291 $eth->finish || die "cannot finish " . $dbh->errstr;
292 $dbh->disconnect;
293 $retval = get_partid($myconfig, $form, $code);
296 return($retval);