holds.compute.pl 8.38 KB
Newer Older
1
#!/usr/bin/perl
Petter Goksøyr Åsen's avatar
Petter Goksøyr Åsen committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
use strict;
use warnings;
$|++;
use Data::Dumper;
use Date::Parse;
use DBI;
use JSON; our $JSON = JSON->new()->utf8(1)->pretty(1)->canonical(1); # TODO utf8
use utf8;

# http://github.com/ohait/perl-dbi-sugar.git
use lib 'perl-dbi-sugar/lib';
use DBI::Sugar;
BEGIN {
    # find Koha's Perl modules
    use FindBin;
    eval { require "$FindBin::Bin/usr/share/koha/bin/kohalib.pl" };
}
use C4::Context;
20
alarm(280);
Petter Goksøyr Åsen's avatar
Petter Goksøyr Åsen committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

my $output = $ARGV[0] or die("missing filename");

DBI::Sugar::factory {
    my $dbh = C4::Context->dbh;
    return $dbh, SELECT_TIME => sub {
        my ($q, $b, $dt) = @_;
        print STDERR Dumper(\@_) if $dt > 0.5;
    };
};


sub category {
    my (%meta) = @_;
    my $mt = $meta{mediatypes};
    my $fmt = $meta{loc_format}; # 090$b
    my $dewey = $meta{loc_dewey}; # 090$c
    my $location = $meta{location}//'';
39
    my $fiction = $meta{fiction};
Petter Goksøyr Åsen's avatar
Petter Goksøyr Åsen committed
40

41
    if ($mt =~ m{^(Film|Spill|Tegneserie|Noter|Lydbok|Musikkopptak)$}) {
42
        return $mt;
Petter Goksøyr Åsen's avatar
Petter Goksøyr Åsen committed
43
44
45
46
47
48
    }
    if ($mt eq 'Andre') {
        return 'Annen';
    }

    if ($fmt =~ m{^\w*[bu]} or $location =~ m{^(BARNEFAG|UNG|UNGFAG)$}) {
49
        if ($fiction eq "0") {
Petter Goksøyr Åsen's avatar
Petter Goksøyr Åsen committed
50
51
52
53
54
            return 'Fag barn';
        } else {
            return 'Skjønn barn';
        }
    } else {
55
        if ($fiction eq "0") {
Petter Goksøyr Åsen's avatar
Petter Goksøyr Åsen committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
            return 'Fag voksen';
        } else {
            return 'Skjønn voksen';
        }
    }
    warn "unknown category, using Annen: ".Dumper(\%meta)."...";
    return 'Annen';
}

sub compute {
    my ($bnum) = @_;

    my %biblio = SELECT_ROW "* FROM biblio WHERE biblionumber = ?" => [$bnum] or return;

    my %meta = SELECT_ROW q`
        SUBSTRING(ExtractValue(metadata, '//controlfield[@tag="008"]'), 34, 1) AS fiction,
        ExtractValue(metadata, '//datafield[@tag="041"]/subfield[@code="a"]') AS language,
        ExtractValue(metadata, '//datafield[@tag="090"]/subfield[@code="b"]') AS loc_format,
        ExtractValue(metadata, '//datafield[@tag="090"]/subfield[@code="c"]') AS loc_dewey,
        ExtractValue(metadata, '//datafield[@tag="090"]/subfield[@code="d"]') AS loc_location,
        ExtractValue(metadata, '//datafield[@tag="337"]/subfield[@code="a"]') AS mediatypes,
        ExtractValue(metadata, '//datafield[@tag="260"]/subfield[@code="c"][1]') AS pub_year,
        ExtractValue(metadata, '//datafield[@tag="385"]/subfield[@code="a"]') AS audiences
    FROM biblio_metadata WHERE biblionumber = ?` => [$bnum];


    my @items = SELECT "items.*, branchtransfers.tobranch transfer_to FROM items
        LEFT JOIN branchtransfers ON branchtransfers.itemnumber = items.itemnumber AND datearrived IS NULL
84
85
86
        LEFT JOIN reserves ON reserves.itemnumber = items.itemnumber
        WHERE items.biblionumber = ?
        AND deleted_on IS NULL
87
        AND homebranch=holdingbranch
88
        AND found IS NULL"
Petter Goksøyr Åsen's avatar
Petter Goksøyr Åsen committed
89
90
91
92
93
    => [$bnum] => sub {
        return \%_;
    };

    my $oldest = 0;
94
    my @holds = SELECT "biblionumber, reserve_id, branchcode, itemnumber, found, priority, reservedate FROM reserves WHERE biblionumber = ? AND suspend=0"
Petter Goksøyr Åsen's avatar
Petter Goksøyr Åsen committed
95
96
97
98
99
100
101
102
103
104
105
    => [$bnum] => sub {
        my $age = int((time()-str2time($_{reservedate}))/86400);
        $oldest = $age if $age > $oldest;
        return { %_, age => $age, };
    };

    my @pick = sort { $a->{itemnumber} <=> $b->{itemnumber} } _compute(\%biblio, \@items, \@holds);
    return unless @pick;

    my $out = {
        #meta => \%meta,
106
        fiction => $meta{fiction},
Petter Goksøyr Åsen's avatar
Petter Goksøyr Åsen committed
107
108
109
110
111
        dewey => $meta{loc_dewey},
        format => $meta{loc_format},
        sort_author => $meta{loc_location},
        biblionumber => $biblio{biblionumber},
        pub_year => $meta{pub_year},
112
        title => $biblio{title},
Petter Goksøyr Åsen's avatar
Petter Goksøyr Åsen committed
113
114
115
        author => $biblio{author},
        holds => [],
        age => $oldest,
116
        barcodes => [map { $_->{barcode} // () } @items],
Petter Goksøyr Åsen's avatar
Petter Goksøyr Åsen committed
117
118
119
120
121
    };
    for my $pick (@pick) {
        my $loc = $pick->{loc} or die Dumper($pick)."...";
        my $i = {
            itemnumber => $pick->{itemnumber},
122
            onlyex => $pick->{onlyex},
Petter Goksøyr Åsen's avatar
Petter Goksøyr Åsen committed
123
124
125
126
127
128
            barcode => $pick->{barcode},
            location => $pick->{location},
            itemcallnumber => $pick->{itemcallnumber},
            ccode => $pick->{ccode},
            category => category(%meta, location => $pick->{location}),
        };
129
        if (not $pick->{onloan}) {
Petter Goksøyr Åsen's avatar
Petter Goksøyr Åsen committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
            $out->{pick}//={};
            my $b = $out->{pick}->{$pick->{loc}}//=[];
            push @$b, $i;
        }
    }
    for my $hold (sort { $a->{priority} <=> $b->{priority} } @holds) {
        push @{$out->{holds}}, {
            reserve_id => $hold->{reserve_id},
            itemnumber => $hold->{itemnumber},
            found => $hold->{found},
            branch => $hold->{branchcode},
            age => $hold->{age},
        };
    }
    return $out;
}

sub _compute {
    my ($bib, $items, $holds) = @_;

    my %available;
    my %pick;

    my %bybranch;
    my $bybranch = sub {
        my ($code) = @_;
        $code //= 'unknown';
        $bybranch{$code}//={
            code => $code,
            items => 0,
            available => 0,
        };
    };

    for my $item (sort { $a->{itemnumber} <=> $b->{itemnumber} } @$items) {
165
166
167
168

        next if $item->{notforloan} or $item->{damaged} or $item->{itemlost};
        next if $item->{itype} and $item->{itype} =~ m{^(DAGSLAAN|UKESLAAN|10DLAAN|TOUKESLAAN|SETT)$};
        next if $item->{transfer_to};
Petter Goksøyr Åsen's avatar
Petter Goksøyr Åsen committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188

        my $branch = $item->{homebranch} // 'unknown';
        if ($item->{location} and $item->{location} =~ m{^([a-z]{4})(\.[a-z\d]+)+$}) {
            $branch = $1;
            $item->{loc} = $branch;
            $item->{shelf} = $item->{location};
        } else {
            $item->{loc} = $branch;
            $item->{shelf} = $branch;
        }

        my $b = $bybranch->($branch);
        $b->{items}++;

        $item->{available}++ if !$item->{onloan};
        if ($item->{available}) {
            $available{$item->{itemnumber}} = $item;
            $b->{available}++;
        }
    }
189

Petter Goksøyr Åsen's avatar
Petter Goksøyr Åsen committed
190
191
    for my $hold (sort { $a->{priority} <=> $b->{priority} } @$holds) {
        if (my $i = $hold->{itemnumber}) { # item number is locked
192
193
194
            next if $hold->{found};
            my $item = $available{$i} or next;
            $item->{reserve} = $hold->{reserve_id};
Petter Goksøyr Åsen's avatar
Petter Goksøyr Åsen committed
195
            $item->{found} = $hold->{found};
196
            $item->{onlyex} = $item->{copynumber};
Petter Goksøyr Åsen's avatar
Petter Goksøyr Åsen committed
197
            $pick{$i} = $item;
198
            $bybranch->($item->{loc})->{available}-- if $item->{loc};
Petter Goksøyr Åsen's avatar
Petter Goksøyr Åsen committed
199
200
201
202
203
            delete $available{$i};
        }
    }

    for my $hold (sort { $a->{priority} <=> $b->{priority} } @$holds) {
204
205
206
207
208
209
        if (my $i = $hold->{itemnumber}) { # item number is locked, handeled in above for loop
            next;
        }

        next if $hold->{found};

Petter Goksøyr Åsen's avatar
Petter Goksøyr Åsen committed
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
        my $to = $hold->{branchcode};
        my $age = eval { no warnings; str2time($hold->{reservedate}) } // 0;
        $age = int( (time()-$age)/86400) if $age;
        $hold->{age} = $age;
        next if $age < 0; # not active yet (TODO, maybe start working on it 2 days before?)


        my $b = $bybranch->($to);
        #print Dumper([branch => $b]);
        if ($b->{available}) {
            my (@list) = grep { $_->{loc} eq $to } values %available;
            if (!@list) {
                $b->{available} = 0; # there were none!
            }
            else {
                $pick{$_->{itemnumber}} = $_ for @list;
                my $first = $list[0];
                $bybranch->($first->{loc})->{available}--;
                next;
            }
        }
        # no good items found, mark everything for pick
232
        return values %available;
Petter Goksøyr Åsen's avatar
Petter Goksøyr Åsen committed
233
234
235
236
237
238
    }
    return values %pick;
}

my %bnums;
TX {
239
    SELECT "DISTINCT biblionumber FROM reserves WHERE reservedate < NOW() AND found IS NULL ORDER BY biblionumber" => [] => sub { $bnums{$_{biblionumber}}++; };
Petter Goksøyr Åsen's avatar
Petter Goksøyr Åsen committed
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
};
#print "got ".scalar(keys %bnums)." distinct bibnums\n";

my @out;
for my $bnum (sort { $a <=> $b } keys %bnums) {
    TX {
        #printf "; bibnum: %s\n", $bnum;
        #print Dumper(compute($bnum));
        push @out, compute($bnum);


        #for my $pick (@picks) {
            #printf "%s %s (%s)\n", $pick->{barcode}, $pick->{shelf}, $pick->{homebranch};
        #}

        #printf "bibnum: %s\n", $bnum;
        #for my $hold (@holds) {
        #    printf "%16s %4s => %4s %d\n", $hold->{barcode}//'', $hold->{from}//'', $hold->{to}, $hold->{reserve_id};
        #}
        #print "\n";
        #print Dumper({biblionumber => $bnum, holds => \@holds});
    };
}

open my $f, '>', $output or die("can't open output file: $!");
print $f $JSON->encode(\@out);