]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/biblio.pm
circ rules fixups
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Storage / Publisher / biblio.pm
1 package OpenILS::Application::Storage::Publisher::biblio;
2 use base qw/OpenILS::Application::Storage/;
3 use vars qw/$VERSION/;
4 use OpenSRF::EX qw/:try/;
5 #use OpenILS::Application::Storage::CDBI::biblio;
6 #use OpenILS::Application::Storage::CDBI::asset;
7 use OpenILS::Utils::Fieldmapper;
8
9 $VERSION = 1;
10
11 sub record_copy_count {
12         my $self = shift;
13         my $client = shift;
14
15         my %args = @_;
16
17         my $cn_table = asset::call_number->table;
18         my $cp_table = asset::copy->table;
19         my $out_table = actor::org_unit_type->table;
20         my $descendants = "actor.org_unit_descendants(u.id)";
21         my $ancestors = "actor.org_unit_ancestors(?)";
22
23         my $sql = <<"   SQL";
24                 SELECT  t.depth,
25                         u.id AS org_unit,
26                         sum(
27                                 (SELECT count(cp.id)
28                                   FROM  $cn_table cn
29                                         JOIN $cp_table cp ON (cn.id = cp.call_number)
30                                         JOIN $descendants a ON (cp.circ_lib = a.id)
31                                   WHERE cn.record = ?)
32                         ) AS count,
33                         sum(
34                                 (SELECT count(cp.id)
35                                   FROM  $cn_table cn
36                                         JOIN $cp_table cp ON (cn.id = cp.call_number)
37                                         JOIN $descendants a ON (cp.circ_lib = a.id)
38                                   WHERE cn.record = ?
39                                         AND cp.status = 0)
40                         ) AS available
41                   FROM  $ancestors u
42                         JOIN $out_table t ON (u.ou_type = t.id)
43                   GROUP BY 1,2
44         SQL
45
46         my $sth = biblio::record_entry->db_Main->prepare_cached($sql);
47         $sth->execute(''.$args{record}, ''.$args{record}, ''.$args{org_unit});
48         while ( my $row = $sth->fetchrow_hashref ) {
49                 $client->respond( $row );
50         }
51         return undef;
52 }
53 __PACKAGE__->register_method(
54         api_name        => 'open-ils.storage.biblio.record_entry.copy_count',
55         method          => 'record_copy_count',
56         api_level       => 1,
57         stream          => 1,
58         cachable        => 1,
59 );
60
61 sub record_by_copy {
62         my $self = shift;
63         my $client = shift;
64
65         my $cn_table = asset::call_number->table;
66         my $cp_table = asset::copy->table;
67
68         my $id = ''.shift;
69         my ($r) = biblio::record_entry->db_Main->selectrow_array( <<"   SQL", {}, $id );
70                 SELECT  cn.record
71                   FROM  $cn_table cn
72                         JOIN $cp_table cp ON (cp.call_number = cn.id)
73                   WHERE cp.id = ?
74         SQL
75
76         my $rec = biblio::record_entry->retrieve( $r );
77         my $r_fm = $rec->to_fieldmapper;
78         $r_fm->fixed_fields( $rec->record_descriptor->next->to_fieldmapper );
79
80         return $r_fm;
81 }
82 __PACKAGE__->register_method(
83         api_name        => 'open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy',
84         method          => 'record_by_copy',
85         api_level       => 1,
86         cachable        => 1,
87 );
88
89
90 =comment Old version
91
92 my $org_unit_lookup;
93 sub record_copy_count {
94         my $self = shift;
95         my $client = shift;
96         my $oid = shift;
97         my @recs = @_;
98
99         if ($self->api_name !~ /batch/o) {
100                 @recs = ($recs[0]);
101         }
102
103         throw OpenSRF::EX::InvalidArg ( "No org_unit id passed!" )
104                 unless ($oid);
105
106         throw OpenSRF::EX::InvalidArg ( "No record id passed!" )
107                 unless (@recs);
108
109         $org_unit_lookup ||= $self->method_lookup('open-ils.storage.direct.actor.org_unit.retrieve');
110         my ($org_unit) = $org_unit_lookup->run($oid);
111
112         # XXX Use descendancy tree here!!!
113         my $short_name_hack = $org_unit->shortname;
114         $short_name_hack = '' if (!$org_unit->parent_ou);
115         $short_name_hack .= '%';
116         # XXX Use descendancy tree here!!!
117
118         my $rec_list = join(',',@recs);
119
120         my $cp_table = asset::copy->table;
121         my $cn_table = asset::call_number->table;
122
123         my $select =<<" SQL";
124                 SELECT  count(cp.*) as copies
125                   FROM  $cn_table cn
126                         JOIN $cp_table cp ON (cp.call_number = cn.id)
127                   WHERE cn.owning_lib LIKE ? AND
128                         cn.record IN ($rec_list)
129         SQL
130
131         my $sth = asset::copy->db_Main->prepare_cached($select);
132         $sth->execute($short_name_hack);
133
134         my $results = $sth->fetchall_hashref('record');
135
136         $client->respond($$results{$_}{copies} || 0) for (@recs);
137
138         return undef;
139 }
140 __PACKAGE__->register_method(
141         method          => 'record_copy_count',
142         api_name        => 'open-ils.storage.direct.biblio.record_copy_count',
143         api_level       => 1,
144         argc            => 1,
145 );
146 __PACKAGE__->register_method(
147         method          => 'record_copy_count',
148         api_name        => 'open-ils.storage.direct.biblio.record_copy_count.batch',
149         api_level       => 1,
150         argc            => 1,
151         stream          => 1,
152 );
153
154 =cut
155
156 1;