]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/actor.pm
settings server controlled opt-in functionallity for non-home_ou transactions
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Storage / Publisher / actor.pm
1 package OpenILS::Application::Storage::Publisher::actor;
2 use base qw/OpenILS::Application::Storage/;
3 use OpenILS::Application::Storage::CDBI::actor;
4 use OpenSRF::Utils::Logger qw/:level/;
5 use OpenSRF::Utils qw/:datetime/;
6 use OpenILS::Utils::Fieldmapper;
7 use OpenSRF::Utils::SettingsClient;
8
9 use DateTime;           
10 use DateTime::Format::ISO8601;  
11                                                 
12                                                                                                 
13 my $_dt_parser = DateTime::Format::ISO8601->new;    
14
15 my $log = 'OpenSRF::Utils::Logger';
16
17 sub new_usergroup_id {
18         return actor::user->db_Main->selectrow_array("select nextval('actor.usr_usrgroup_seq'::regclass)");
19 }
20 __PACKAGE__->register_method(
21         api_name        => 'open-ils.storage.actor.user.group_id.new',
22         api_level       => 1,
23         method          => 'new_usergroup_id',
24 );
25
26 sub usr_total_owed {
27         my $self = shift;
28         my $client = shift;
29         my $usr = shift;
30
31         my $sql = <<"   SQL";
32                         SELECT  x.usr,
33                                         SUM(COALESCE((SELECT SUM(b.amount) FROM money.billing b WHERE b.voided IS FALSE AND b.xact = x.id),0.0)) -
34                                                 SUM(COALESCE((SELECT SUM(p.amount) FROM money.payment p WHERE p.voided IS FALSE AND p.xact = x.id),0.0))
35                           FROM  money.billable_xact x
36                           WHERE x.usr = ? AND x.xact_finish IS NULL
37                           GROUP BY 1
38         SQL
39
40         my (undef,$val) = actor::user->db_Main->selectrow_array($sql, {}, $usr);
41
42         return $val;
43 }
44 __PACKAGE__->register_method(
45         api_name        => 'open-ils.storage.actor.user.total_owed',
46         api_level       => 1,
47         method          => 'usr_total_owed',
48 );
49
50 sub usr_breakdown_out {
51         my $self = shift;
52         my $client = shift;
53         my $usr = shift;
54
55         my $out_sql = <<"       SQL";
56                         SELECT  id
57                           FROM  action.circulation
58                           WHERE usr = ? AND checkin_time IS NULL AND due_date >= 'today' AND (stop_fines IS NULL OR stop_fines NOT IN ('LOST','CLAIMSRETURNED','LONGOVERDUE'))
59         SQL
60
61         my $out = actor::user->db_Main->selectcol_arrayref($out_sql, {}, $usr);
62
63         my $od_sql = <<"        SQL";
64                         SELECT  id
65                           FROM  action.circulation
66                           WHERE usr = ? AND checkin_time IS NULL AND due_date < 'today' AND (stop_fines IS NULL OR stop_fines NOT IN ('LOST','CLAIMSRETURNED','LONGOVERDUE'))
67         SQL
68
69         my $od = actor::user->db_Main->selectcol_arrayref($od_sql, {}, $usr);
70
71         my $lost_sql = <<"      SQL";
72                         SELECT  id
73                           FROM  action.circulation
74                           WHERE usr = ? AND checkin_time IS NULL AND xact_finish IS NULL AND stop_fines = 'LOST'
75         SQL
76
77         my $lost = actor::user->db_Main->selectcol_arrayref($lost_sql, {}, $usr);
78
79         my $cl_sql = <<"        SQL";
80                         SELECT  id
81                           FROM  action.circulation
82                           WHERE usr = ? AND checkin_time IS NULL AND stop_fines = 'CLAIMSRETURNED'
83         SQL
84
85         my $cl = actor::user->db_Main->selectcol_arrayref($cl_sql, {}, $usr);
86
87         my $lo_sql = <<"        SQL";
88                         SELECT  id
89                           FROM  action.circulation
90                           WHERE usr = ? AND checkin_time IS NULL AND stop_fines = 'LONGOVERDUE'
91         SQL
92
93         my $lo = actor::user->db_Main->selectcol_arrayref($lo_sql, {}, $usr);
94
95         if ($self->api_name =~/count$/o) {
96                 return {        total   => scalar(@$out) + scalar(@$od) + scalar(@$lost) + scalar(@$cl) + scalar(@$lo),
97                                         out             => scalar(@$out),
98                                         overdue => scalar(@$od),
99                                         lost    => scalar(@$lost),
100                                         claims_returned => scalar(@$cl),
101                                         long_overdue            => scalar(@$lo),
102                 };
103         }
104
105         return {        out             => $out,
106                                 overdue => $od,
107                                 lost    => $lost,
108                                 claims_returned => $cl,
109                                 long_overdue            => $lo,
110         };
111 }
112 __PACKAGE__->register_method(
113         api_name        => 'open-ils.storage.actor.user.checked_out',
114         api_level       => 1,
115         method          => 'usr_breakdown_out',
116 );
117 __PACKAGE__->register_method(
118         api_name        => 'open-ils.storage.actor.user.checked_out.count',
119         api_level       => 1,
120         method          => 'usr_breakdown_out',
121 );
122
123 sub usr_total_out {
124         my $self = shift;
125         my $client = shift;
126         my $usr = shift;
127
128         my $sql = <<"   SQL";
129                         SELECT  count(*)
130                           FROM  action.circulation
131                           WHERE usr = ? AND checkin_time IS NULL
132         SQL
133
134         my ($val) = actor::user->db_Main->selectrow_array($sql, {}, $usr);
135
136         return $val;
137 }
138 __PACKAGE__->register_method(
139         api_name        => 'open-ils.storage.actor.user.total_out',
140         api_level       => 1,
141         method          => 'usr_total_out',
142 );
143
144 sub calc_proximity {
145         my $self = shift;
146         my $client = shift;
147
148         local $OpenILS::Application::Storage::WRITE = 1;
149
150         my $delete_sql = <<"    SQL";
151                 DELETE FROM actor.org_unit_proximity;
152         SQL
153
154         my $insert_sql = <<"    SQL";
155                 INSERT INTO actor.org_unit_proximity (from_org, to_org, prox)
156                         SELECT  l.id,
157                                 r.id,
158                                 actor.org_unit_proximity(l.id,r.id)
159                           FROM  actor.org_unit l,
160                                 actor.org_unit r;
161         SQL
162
163         actor::org_unit_proximity->db_Main->do($delete_sql);
164         actor::org_unit_proximity->db_Main->do($insert_sql);
165
166         return 1;
167 }
168 __PACKAGE__->register_method(
169         api_name        => 'open-ils.storage.actor.org_unit.refresh_proximity',
170         api_level       => 1,
171         method          => 'calc_proximity',
172 );
173
174
175 sub org_closed_overlap {
176         my $self = shift;
177         my $client = shift;
178         my $ou = shift;
179         my $date = shift;
180         my $direction = shift || 0;
181         my $no_hoo = shift || 0;
182
183         return undef unless ($date && $ou);
184
185         my $t = actor::org_unit::closed_date->table;
186         my $sql = <<"   SQL";
187                 SELECT  *
188                   FROM  $t
189                   WHERE ? between close_start and close_end
190                         AND org_unit = ?
191                   ORDER BY close_start ASC, close_end DESC
192                   LIMIT 1
193         SQL
194
195         $date = clense_ISO8601($date);
196         my ($begin, $end) = ($date,$date);
197
198         my $hoo = actor::org_unit::hours_of_operation->retrieve($ou);
199
200         if (my $closure = actor::org_unit::closed_date->db_Main->selectrow_hashref( $sql, {}, $date, $ou )) {
201                 $begin = clense_ISO8601($closure->{close_start});
202                 $end = clense_ISO8601($closure->{close_end});
203
204                 if ( $direction <= 0 ) {
205                         $before = $_dt_parser->parse_datetime( $begin );
206                         $before->subtract( minutes => 1 );
207
208                         while ( my $_b = org_closed_overlap($self, $client, $ou, $before->iso8601, -1, 1 ) ) {
209                                 $before = $_dt_parser->parse_datetime( clense_ISO8601($_b->{start}) );
210                         }
211                         $begin = clense_ISO8601($before->iso8601);
212                 }
213
214                 if ( $direction >= 0 ) {
215                         $after = $_dt_parser->parse_datetime( $end );
216                         $after->add( minutes => 1 );
217
218                         while ( my $_a = org_closed_overlap($self, $client, $ou, $after->iso8601, 1, 1 ) ) {
219                                 $after = $_dt_parser->parse_datetime( clense_ISO8601($_a->{end}) );
220                         }
221                         $end = clense_ISO8601($after->iso8601);
222                 }
223         }
224
225         if ( !$no_hoo ) {
226                 if ( $hoo ) {
227
228                         if ( $direction <= 0 ) {
229                                 my $begin_dow = $_dt_parser->parse_datetime( $begin )->day_of_week_0;
230                                 my $begin_open_meth = "dow_".$begin_dow."_open";
231                                 my $begin_close_meth = "dow_".$begin_dow."_close";
232
233                                 my $count = 1;
234                                 while ($hoo->$begin_open_meth eq '00:00:00' and $hoo->$begin_close_meth eq '00:00:00') {
235                                         $begin = clense_ISO8601($_dt_parser->parse_datetime( $begin )->subtract( days => 1)->iso8601);
236                                         $begin_dow++;
237                                         $begin_dow %= 7;
238                                         $count++;
239                                         last if ($count > 6);
240                                         $begin_open_meth = "dow_".$begin_dow."_open";
241                                         $begin_close_meth = "dow_".$begin_dow."_close";
242                                 }
243
244                                 if (my $closure = actor::org_unit::closed_date->db_Main->selectrow_hashref( $sql, {}, $begin, $ou )) {
245                                         $before = $_dt_parser->parse_datetime( $begin );
246                                         $before->subtract( minutes => 1 );
247                                         while ( my $_b = org_closed_overlap($self, $client, $ou, $before->iso8601, -1 ) ) {
248                                                 $before = $_dt_parser->parse_datetime( clense_ISO8601($_b->{start}) );
249                                         }
250                                 }
251                         }
252         
253                         if ( $direction >= 0 ) {
254                                 my $end_dow = $_dt_parser->parse_datetime( $end )->day_of_week_0;
255                                 my $end_open_meth = "dow_".$end_dow."_open";
256                                 my $end_close_meth = "dow_".$end_dow."_close";
257         
258                                 $count = 1;
259                                 while ($hoo->$end_open_meth eq '00:00:00' and $hoo->$end_close_meth eq '00:00:00') {
260                                         $end = clense_ISO8601($_dt_parser->parse_datetime( $end )->add( days => 1)->iso8601);
261                                         $end_dow++;
262                                         $end_dow %= 7;
263                                         $count++;
264                                         last if ($count > 6);
265                                         $end_open_meth = "dow_".$end_dow."_open";
266                                         $end_close_meth = "dow_".$end_dow."_close";
267                                 }
268
269                                 if (my $closure = actor::org_unit::closed_date->db_Main->selectrow_hashref( $sql, {}, $end, $ou )) {
270                                         $after = $_dt_parser->parse_datetime( $end );
271                                         $after->add( minutes => 1 );
272
273                                         while ( my $_a = org_closed_overlap($self, $client, $ou, $after->iso8601, 1 ) ) {
274                                                 $after = $_dt_parser->parse_datetime( clense_ISO8601($_a->{end}) );
275                                         }
276                                         $end = clense_ISO8601($after->iso8601);
277                                 }
278                         }
279
280                 }
281         }
282
283         if ($begin eq $date && $end eq $date) {
284                 return undef;
285         }
286
287         return { start => $begin, end => $end };
288 }
289 __PACKAGE__->register_method(
290         api_name        => 'open-ils.storage.actor.org_unit.closed_date.overlap',
291         api_level       => 1,
292         method          => 'org_closed_overlap',
293 );
294
295 sub user_by_barcode {
296         my $self = shift;
297         my $client = shift;
298         my @barcodes = shift;
299
300         return undef unless @barcodes;
301
302         for my $card ( actor::card->search( { barcode => @barcodes } ) ) {
303                 next unless $card;
304                 if (@barcodes == 1) {
305                         return $card->usr->to_fieldmapper;
306                 }
307                 $client->respond( $card->usr->to_fieldmapper);
308         }
309         return undef;
310 }
311 __PACKAGE__->register_method(
312         api_name        => 'open-ils.storage.direct.actor.user.search.barcode',
313         api_level       => 1,
314         method          => 'user_by_barcode',
315         stream          => 1,
316         cachable        => 1,
317 );
318
319 sub lost_barcodes {
320         my $self = shift;
321         my $client = shift;
322
323         my $c = actor::card->table;
324         my $p = actor::user->table;
325
326         my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.card <> c.id";
327
328         my $list = actor::user->db_Main->selectcol_arrayref($sql);
329         for my $bc ( @$list ) {
330                 $client->respond($bc);
331         }
332         return undef;
333 }
334 __PACKAGE__->register_method(
335         api_name        => 'open-ils.storage.actor.user.lost_barcodes',
336         api_level       => 1,
337         stream          => 1,
338         method          => 'lost_barcodes',
339         signature       => <<'  NOTE',
340                 Returns an array of barcodes that belong to lost cards.
341                 @return array of barcodes
342         NOTE
343 );
344
345 sub expired_barcodes {
346         my $self = shift;
347         my $client = shift;
348
349         my $c = actor::card->table;
350         my $p = actor::user->table;
351
352         my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.expire_date < CURRENT_DATE";
353
354         my $list = actor::user->db_Main->selectcol_arrayref($sql);
355         for my $bc ( @$list ) {
356                 $client->respond($bc);
357         }
358         return undef;
359 }
360 __PACKAGE__->register_method(
361         api_name        => 'open-ils.storage.actor.user.expired_barcodes',
362         api_level       => 1,
363         stream          => 1,
364         method          => 'expired_barcodes',
365         signature       => <<'  NOTE',
366                 Returns an array of barcodes that are currently expired.
367                 @return array of barcodes
368         NOTE
369 );
370
371 sub barred_barcodes {
372         my $self = shift;
373         my $client = shift;
374
375         my $c = actor::card->table;
376         my $p = actor::user->table;
377
378         my $sql = "SELECT c.barcode FROM $c c JOIN $p p ON (c.usr = p.id) WHERE p.barred IS TRUE";
379
380         my $list = actor::user->db_Main->selectcol_arrayref($sql);
381         for my $bc ( @$list ) {
382                 $client->respond($bc);
383         }
384         return undef;
385 }
386 __PACKAGE__->register_method(
387         api_name        => 'open-ils.storage.actor.user.barred_barcodes',
388         api_level       => 1,
389         stream          => 1,
390         method          => 'barred_barcodes',
391         signature       => <<'  NOTE',
392                 Returns an array of barcodes that are currently barred.
393                 @return array of barcodes
394         NOTE
395 );
396
397 sub penalized_barcodes {
398         my $self = shift;
399         my $client = shift;
400         my @ignore = @_;
401
402         my $c = actor::card->table;
403         my $p = actor::user_standing_penalty->table;
404
405         my $sql = "SELECT c.barcode FROM $c c JOIN $p p USING (usr)";
406
407         if (@ignore) {
408                 $sql .= ' WHERE penalty_type NOT IN ('. join(',', map { '?' } @ignore) . ')';
409         }
410
411         $sql .= ' GROUP BY c.barcode;';
412
413         my $list = actor::user->db_Main->selectcol_arrayref($sql, {}, @ignore);
414         for my $bc ( @$list ) {
415                 $client->respond($bc);
416         }
417         return undef;
418 }
419 __PACKAGE__->register_method(
420         api_name        => 'open-ils.storage.actor.user.penalized_barcodes',
421         api_level       => 1,
422         stream          => 1,
423         method          => 'penalized_barcodes',
424         signature       => <<'  NOTE',
425                 Returns an array of barcodes that have penalties not listed
426                 as a parameter.  Supply a list of any penalty types that should
427                 not stop a patron from checking out materials.
428
429                 @param ignore_list Penalty type to ignore
430                 @return array of barcodes
431         NOTE
432 );
433
434
435 sub patron_search {
436         my $self = shift;
437         my $client = shift;
438         my $search = shift;
439         my $limit = shift || 1000;
440         my $sort = shift;
441         my $inactive = shift;
442         my $ws_ou = shift;
443         my $ws_ou_depth = shift || 0;
444
445         my $strict_opt_in = OpenSRF::Utils::SettingsClient->new->config_value( share => user => 'opt_in' );
446
447         $sort = ['family_name','first_given_name'] unless ($$sort[0]);
448         push @$sort,'id';
449
450         # group 0 = user
451         # group 1 = address
452         # group 2 = phone, ident
453
454         my $usr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
455         my @usrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
456
457         my $addr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
458         my @addrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
459
460         my $pv = $$search{phone}{value};
461         my $iv = $$search{ident}{value};
462         my $nv = $$search{name}{value};
463
464         my $phone = '';
465         my @ps;
466         my @phonev;
467         if ($pv) {
468                 for my $p ( qw/day_phone evening_phone other_phone/ ) {
469                         push @ps, "LOWER($p) ~ ?";
470                         push @phonev, "^$pv";
471                 }
472                 $phone = '(' . join(' OR ', @ps) . ')';
473         }
474
475         my $ident = '';
476         my @is;
477         my @identv;
478         if ($iv) {
479                 for my $i ( qw/ident_value ident_value2/ ) {
480                         push @is, "LOWER($i) ~ ?";
481                         push @identv, "^$iv";
482                 }
483                 $ident = '(' . join(' OR ', @is) . ')';
484         }
485
486         my $name = '';
487         my @ns;
488         my @namev;
489         if (0 && $nv) {
490                 for my $n ( qw/first_given_name second_given_name family_name/ ) {
491                         push @ns, "LOWER($i) ~ ?";
492                         push @namev, "^$nv";
493                 }
494                 $name = '(' . join(' OR ', @ns) . ')';
495         }
496
497         my $usr_where = join ' AND ', grep { $_ } ($usr,$phone,$ident,$name);
498         my $addr_where = $addr;
499
500
501         my $u_table = actor::user->table;
502         my $a_table = actor::user_address->table;
503         my $opt_in_table = actor::usr_org_unit_opt_in->table;
504         my $ou_table = actor::org_unit->table;
505
506         my $u_select = "SELECT id as id FROM $u_table u WHERE $usr_where";
507         my $a_select = "SELECT usr as id FROM $a_table a WHERE $addr_where";
508         my $clone_select = '';
509         $clone_select = "JOIN (SELECT cu.id as id FROM $a_table ca ".
510                            "JOIN $u_table cu ON (cu.mailing_address = ca.id OR cu.billing_address = ca.id) ".
511                            "WHERE $addr_where) AS clone USING (id)" if ($addr_where);
512
513         my $select = '';
514         if ($usr_where) {
515                 if ($addr_where) {
516                         $select = "$u_select INTERSECT $a_select";
517                 } else {
518                         $select = $u_select;
519                 }
520         } elsif ($addr_where) {
521                 $select = "$a_select";
522         } else {
523                 return undef;
524         }
525
526         my $order_by = join ', ', map { 'LOWER(users.'. (split / /,$_)[0] . ') ' . (split / /,$_)[1] } @$sort;
527         my $distinct_list = join ', ', map { 'LOWER(users.'. (split / /,$_)[0] . ')' } @$sort;
528
529         if ($inactive) {
530                 $inactive = '';
531         } else {
532                 $inactive = 'AND users.active = TRUE';
533         }
534
535         if (!$ws_ou) {  # XXX This should be required!!
536                 $ws_ou = actor::org_unit->search( { parent_ou => undef } )->[0]->id;
537         }
538
539         my $opt_in_join = '';
540         my $opt_in_where = '';
541         if (lc($strict_opt_in) eq 'true') {
542                 $opt_in_join = "LEFT JOIN $opt_in_table oi ON (oi.org_unit = $ws_ou AND users.id = oi.usr)";
543                 $opt_in_where = "AND (oi.id IS NOT NULL OR users.home_ou = $ws_ou)";
544         }
545
546         my $descendants = "actor.org_unit_descendants($ws_ou, $ws_ou_depth)";
547
548         $select = <<"   SQL";
549                 SELECT  DISTINCT $distinct_list
550                   FROM  $u_table AS users
551                         JOIN ($select) AS search USING (id)
552                         JOIN $descendants d ON (d.id = users.home_ou)
553                         $opt_in_join
554                         $clone_select
555                   WHERE users.deleted = FALSE
556                         $inactive
557                         $opt_in_where
558                   ORDER BY $order_by
559                   LIMIT $limit
560         SQL
561
562         return actor::user->db_Main->selectcol_arrayref($select, {Columns=>[scalar(@$sort)]}, map {lc($_)} (@usrv,@phonev,@identv,@namev,@addrv,@addrv));
563 }
564 __PACKAGE__->register_method(
565         api_name        => 'open-ils.storage.actor.user.crazy_search',
566         api_level       => 1,
567         method          => 'patron_search',
568 );
569
570 =comment not gonna use it...
571
572 sub fleshed_search {
573         my $self = shift;
574         my $client = shift;
575         my $searches = shift;
576
577         return undef unless (defined $searches);
578
579         for my $usr ( actor::user->search( $searches ) ) {
580                 next unless $usr;
581                 $client->respond( flesh_user( $usr ) );
582         }
583         return undef;
584 }
585 __PACKAGE__->register_method(
586         api_name        => 'open-ils.storage.fleshed.actor.user.search',
587         api_level       => 1,
588         method          => 'fleshed_search',
589         stream          => 1,
590         cachable        => 1,
591 );
592
593 sub fleshed_search_like {
594         my $self = shift;
595         my $client = shift;
596         my $searches = shift;
597
598         return undef unless (defined $searches);
599
600         for my $usr ( actor::user->search_like( $searches ) ) {
601                 next unless $usr;
602                 $client->respond( flesh_user( $usr ) );
603         }
604         return undef;
605 }
606 __PACKAGE__->register_method(
607         api_name        => 'open-ils.storage.fleshed.actor.user.search_like',
608         api_level       => 1,
609         method          => 'user_by_barcode',
610         stream          => 1,
611         cachable        => 1,
612 );
613
614 sub retrieve_fleshed_user {
615         my $self = shift;
616         my $client = shift;
617         my @ids = shift;
618
619         return undef unless @ids;
620
621         @ids = ($ids[0]) unless ($self->api_name =~ /batch/o); 
622
623         $client->respond( flesh_user( actor::user->retrieve( $_ ) ) ) for ( @ids );
624
625         return undef;
626 }
627 __PACKAGE__->register_method(
628         api_name        => 'open-ils.storage.fleshed.actor.user.retrieve',
629         api_level       => 1,
630         method          => 'retrieve_fleshed_user',
631         cachable        => 1,
632 );
633 __PACKAGE__->register_method(
634         api_name        => 'open-ils.storage.fleshed.actor.user.batch.retrieve',
635         api_level       => 1,
636         method          => 'retrieve_fleshed_user',
637         stream          => 1,
638         cachable        => 1,
639 );
640
641 sub flesh_user {
642         my $usr = shift;
643
644
645         my $standing = $usr->standing;
646         my $profile = $usr->profile;
647         my $ident_type = $usr->ident_type;
648                 
649         my $maddress = $usr->mailing_address;
650         my $baddress = $usr->billing_address;
651         my $card = $usr->card;
652
653         my @addresses = $usr->addresses;
654         my @cards = $usr->cards;
655
656         my $usr_fm = $usr->to_fieldmapper;
657         $usr_fm->standing( $standing->to_fieldmapper );
658         $usr_fm->profile( $profile->to_fieldmapper );
659         $usr_fm->ident_type( $ident_type->to_fieldmapper );
660
661         $usr_fm->card( $card->to_fieldmapper );
662         $usr_fm->mailing_address( $maddress->to_fieldmapper ) if ($maddress);
663         $usr_fm->billing_address( $baddress->to_fieldmapper ) if ($baddress);
664
665         $usr_fm->cards( [ map { $_->to_fieldmapper } @cards ] );
666         $usr_fm->addresses( [ map { $_->to_fieldmapper } @addresses ] );
667
668         return $usr_fm;
669 }
670
671 =cut
672
673 sub org_unit_list {
674         my $self = shift;
675         my $client = shift;
676
677         my $select =<<" SQL";
678         SELECT  *
679           FROM  actor.org_unit
680           ORDER BY CASE WHEN parent_ou IS NULL THEN 0 ELSE 1 END, name;
681         SQL
682
683         my $sth = actor::org_unit->db_Main->prepare_cached($select);
684         $sth->execute;
685
686         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
687
688         return undef;
689 }
690 __PACKAGE__->register_method(
691         api_name        => 'open-ils.storage.direct.actor.org_unit.retrieve.all',
692         api_level       => 1,
693         stream          => 1,
694         method          => 'org_unit_list',
695 );
696
697 sub org_unit_type_list {
698         my $self = shift;
699         my $client = shift;
700
701         my $select =<<" SQL";
702         SELECT  *
703           FROM  actor.org_unit_type
704           ORDER BY depth, name;
705         SQL
706
707         my $sth = actor::org_unit_type->db_Main->prepare_cached($select);
708         $sth->execute;
709
710         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit_type->construct($_) } $sth->fetchall_hash );
711
712         return undef;
713 }
714 __PACKAGE__->register_method(
715         api_name        => 'open-ils.storage.direct.actor.org_unit_type.retrieve.all',
716         api_level       => 1,
717         stream          => 1,
718         method          => 'org_unit_type_list',
719 );
720
721 sub org_unit_full_path {
722         my $self = shift;
723         my $client = shift;
724         my @binds = @_;
725
726         return undef unless (@binds);
727
728         my $func = 'actor.org_unit_full_path(?)';
729         $func = 'actor.org_unit_full_path(?,?)' if (@binds > 1);
730
731         my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
732         $sth->execute(@binds);
733
734         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
735
736         return undef;
737 }
738 __PACKAGE__->register_method(
739         api_name        => 'open-ils.storage.actor.org_unit.full_path',
740         api_level       => 1,
741         stream          => 1,
742         method          => 'org_unit_full_path',
743 );
744
745 sub org_unit_ancestors {
746         my $self = shift;
747         my $client = shift;
748         my $id = shift;
749
750         return undef unless ($id);
751
752         my $func = 'actor.org_unit_ancestors(?)';
753
754         my $sth = actor::org_unit->db_Main->prepare_cached(<<"  SQL");
755                 SELECT  f.*
756                   FROM  $func f
757                         JOIN actor.org_unit_type t ON (f.ou_type = t.id)
758                   ORDER BY t.depth, f.name;
759         SQL
760         $sth->execute(''.$id);
761
762         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
763
764         return undef;
765 }
766 __PACKAGE__->register_method(
767         api_name        => 'open-ils.storage.actor.org_unit.ancestors',
768         api_level       => 1,
769         stream          => 1,
770         method          => 'org_unit_ancestors',
771 );
772
773 sub org_unit_descendants {
774         my $self = shift;
775         my $client = shift;
776         my $id = shift;
777         my $depth = shift;
778
779         return undef unless ($id);
780
781         my $func = 'actor.org_unit_descendants(?)';
782         if (defined $depth) {
783                 $func = 'actor.org_unit_descendants(?,?)';
784         }
785
786         my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
787         $sth->execute(''.$id, ''.$depth) if (defined $depth);
788         $sth->execute(''.$id) unless (defined $depth);
789
790         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
791
792         return undef;
793 }
794 __PACKAGE__->register_method(
795         api_name        => 'open-ils.storage.actor.org_unit.descendants',
796         api_level       => 1,
797         stream          => 1,
798         method          => 'org_unit_descendants',
799 );
800
801 sub fleshed_actor_stat_cat {
802         my $self = shift;
803         my $client = shift;
804         my @list = @_;
805         
806         @list = ($list[0]) unless ($self->api_name =~ /batch/o);
807
808         for my $sc (@list) {
809                 my $cat = actor::stat_cat->retrieve($sc);
810                 next unless ($cat);
811
812                 my $sc_fm = $cat->to_fieldmapper;
813                 $sc_fm->entries( [ map { $_->to_fieldmapper } $cat->entries ] );
814
815                 $client->respond( $sc_fm );
816
817         }
818
819         return undef;
820 }
821 __PACKAGE__->register_method(
822         api_name        => 'open-ils.storage.fleshed.actor.stat_cat.retrieve',
823         api_level       => 1,
824         argc            => 1,
825         method          => 'fleshed_actor_stat_cat',
826 );
827
828 __PACKAGE__->register_method(
829         api_name        => 'open-ils.storage.fleshed.actor.stat_cat.retrieve.batch',
830         api_level       => 1,
831         argc            => 1,
832         stream          => 1,
833         method          => 'fleshed_actor_stat_cat',
834 );
835
836 #XXX Fix stored proc calls
837 sub ranged_actor_stat_cat_all {
838         my $self = shift;
839         my $client = shift;
840         my $ou = ''.shift();
841         
842         return undef unless ($ou);
843         my $s_table = actor::stat_cat->table;
844
845         my $select = <<"        SQL";
846                 SELECT  s.*
847                   FROM  $s_table s
848                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
849                   ORDER BY name
850         SQL
851
852         $fleshed = 0;
853         $fleshed = 1 if ($self->api_name =~ /fleshed/o);
854
855         my $sth = actor::stat_cat->db_Main->prepare_cached($select);
856         $sth->execute($ou);
857
858         for my $sc ( map { actor::stat_cat->construct($_) } $sth->fetchall_hash ) {
859                 my $sc_fm = $sc->to_fieldmapper;
860                 $sc_fm->entries(
861                         [ $self->method_lookup( 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat' )->run($ou,$sc->id) ]
862                 ) if ($fleshed);
863                 $client->respond( $sc_fm );
864         }
865
866         return undef;
867 }
868 __PACKAGE__->register_method(
869         api_name        => 'open-ils.storage.ranged.fleshed.actor.stat_cat.all',
870         api_level       => 1,
871         argc            => 1,
872         stream          => 1,
873         method          => 'ranged_actor_stat_cat_all',
874 );
875
876 __PACKAGE__->register_method(
877         api_name        => 'open-ils.storage.ranged.actor.stat_cat.all',
878         api_level       => 1,
879         argc            => 1,
880         stream          => 1,
881         method          => 'ranged_actor_stat_cat_all',
882 );
883
884 #XXX Fix stored proc calls
885 sub ranged_actor_stat_cat_entry {
886         my $self = shift;
887         my $client = shift;
888         my $ou = ''.shift();
889         my $sc = ''.shift();
890         
891         return undef unless ($ou);
892         my $s_table = actor::stat_cat_entry->table;
893
894         my $select = <<"        SQL";
895                 SELECT  s.*
896                   FROM  $s_table s
897                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
898                   WHERE stat_cat = ?
899                   ORDER BY name
900         SQL
901
902         my $sth = actor::stat_cat->db_Main->prepare_cached($select);
903         $sth->execute($ou,$sc);
904
905         for my $sce ( map { actor::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
906                 $client->respond( $sce->to_fieldmapper );
907         }
908
909         return undef;
910 }
911 __PACKAGE__->register_method(
912         api_name        => 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat',
913         api_level       => 1,
914         stream          => 1,
915         method          => 'ranged_actor_stat_cat_entry',
916 );
917
918
919 1;