Bug 12365: Notes for rules
[koha.git] / serials / serials-edit.pl
blob33e11ad3bc8f02002c31ca356b66a909664e0246
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
4 # Parts Copyright 2010 Biblibre
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 =head1 NAME
23 serials-edit.pl
25 =head1 Parameters
27 =over 4
29 =item op
30 op can be :
31 * modsubscriptionhistory :to modify the subscription history
32 * serialchangestatus :to modify the status of this subscription
34 =item subscriptionid
36 =item user
38 =item histstartdate
40 =item enddate
42 =item recievedlist
44 =item missinglist
46 =item opacnote
48 =item librariannote
50 =item serialid
52 =item serialseq
54 =item planneddate
56 =item notes
58 =item status
60 =back
62 =cut
64 use Modern::Perl;
65 use CGI qw ( -utf8 );
66 use Encode qw( decode is_utf8 );
67 use C4::Auth;
68 use C4::Biblio;
69 use C4::Items;
70 use C4::Koha;
71 use C4::Output;
72 use C4::Context;
73 use C4::Serials;
74 use C4::Search qw/enabled_staff_search_views/;
76 use Koha::DateUtils;
77 use Koha::Items;
78 use Koha::Serial::Items;
80 use List::MoreUtils qw/uniq/;
82 my $query = CGI->new();
83 my $dbh = C4::Context->dbh;
84 my @serialids = $query->multi_param('serialid');
85 my @serialseqs = $query->multi_param('serialseq');
86 my @planneddates = $query->multi_param('planneddate');
87 my @publisheddates = $query->multi_param('publisheddate');
88 my @publisheddatetexts = $query->multi_param('publisheddatetext');
89 my @status = $query->multi_param('status');
90 my @notes = $query->multi_param('notes');
91 my @subscriptionids = $query->multi_param('subscriptionid');
92 my $op = $query->param('op');
93 if ( scalar(@subscriptionids) == 1 && index( $subscriptionids[0], q|,| ) > 0 ) {
94 @subscriptionids = split( /,/, $subscriptionids[0] );
96 my @errors;
97 my @errseq;
99 # If user comes from subscription details
100 unless (@serialids) {
101 my $serstatus = $query->param('serstatus');
102 my @statuses = split ',', $serstatus;
103 if ($serstatus) {
104 foreach my $subscriptionid (@subscriptionids) {
105 my @tmpser = GetSerials2( $subscriptionid, \@statuses );
106 push @serialids, map { $_->{serialid} } @tmpser;
111 unless ( @serialids ) {
112 my $string =
113 'serials-collection.pl?subscriptionid=' . join ',', uniq @subscriptionids;
114 $string =~ s/,$//;
116 print $query->redirect($string);
117 exit;
120 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
122 template_name => 'serials/serials-edit.tt',
123 query => $query,
124 type => 'intranet',
125 authnotrequired => 0,
126 flagsrequired => { serials => 'receive_serials' },
127 debug => 1,
131 my @serialdatalist;
132 my %processedserialid;
134 my $today = output_pref( { dt => dt_from_string, dateonly => 1 } );
136 foreach my $serialid (@serialids) {
138 #filtering serialid for duplication
139 #NEW serial should appear only once and are created afterwards
140 if ( $serialid
141 && $serialid =~ /^[0-9]+$/
142 && !$processedserialid{$serialid} )
144 my $serinfo = GetSerialInformation($serialid); #TODO duplicates work done by GetSerials2 above
146 for my $d ( qw( publisheddate planneddate )){
147 if ( $serinfo->{$d} =~m/^00/ ) {
148 $serinfo->{$d} = q{};
150 else {
151 $serinfo->{$d} = output_pref( { dt => dt_from_string( $serinfo->{$d} ), dateonly => 1 } );
154 $serinfo->{arriveddate} = $today;
156 $serinfo->{'editdisable'} = (
158 HasSubscriptionExpired( $serinfo->{subscriptionid} )
159 && $serinfo->{'status1'}
161 || $serinfo->{'cannotedit'}
163 $serinfo->{editdisable} = 0 if C4::Auth::haspermission( C4::Context->userenv->{id}, { serials => 'receive_serials' } );
164 $serinfo->{editdisable} ||= ($serinfo->{status8} and $serinfo->{closed});
165 push @serialdatalist, $serinfo;
166 $processedserialid{$serialid} = 1;
169 my $biblio = Koha::Biblios->find( $serialdatalist[0]->{biblionumber} );
171 my @newserialloop;
172 my @subscriptionloop;
174 # check, for each subscription edited, that we have an empty item line if applicable for the subscription
175 my %processedsubscriptionid;
176 foreach my $subscriptionid (@subscriptionids) {
178 #Do not process subscriptionid twice if it was already processed.
179 if ( $subscriptionid && !$processedsubscriptionid{$subscriptionid} )
181 my $cell;
182 if ( $serialdatalist[0]->{'serialsadditems'} ) {
184 #Create New empty item
185 $cell =
186 PrepareItemrecordDisplay( $serialdatalist[0]->{'biblionumber'},
187 '', GetSubscription($subscriptionid) );
188 $cell->{serialsadditems} = 1;
190 $cell->{'subscriptionid'} = $subscriptionid;
191 $cell->{biblionumber} = $serialdatalist[0]->{'biblionumber'};
192 $cell->{'itemid'} = 'NNEW';
193 $cell->{'serialid'} = 'NEW';
194 $cell->{'issuesatonce'} = 1;
195 $cell->{arriveddate} = $today;
197 push @newserialloop, $cell;
198 push @subscriptionloop,
200 'subscriptionid' => $subscriptionid,
201 'abouttoexpire' => abouttoexpire($subscriptionid),
202 'subscriptionexpired' => HasSubscriptionExpired($subscriptionid),
204 $processedsubscriptionid{$subscriptionid} = 1;
207 $template->param( newserialloop => \@newserialloop );
208 $template->param( subscriptions => \@subscriptionloop );
210 if ( $op and $op eq 'serialchangestatus' ) {
212 # Convert serialseqs to UTF-8 to prevent encoding problems
213 foreach my $seq (@serialseqs) {
214 $seq = Encode::decode('UTF-8', $seq) unless Encode::is_utf8($seq);
217 my $newserial;
218 for ( my $i = 0 ; $i <= $#serialids ; $i++ ) {
219 my ($plan_date, $pub_date);
221 if (defined $planneddates[$i] && $planneddates[$i] ne 'XXX') {
222 $plan_date = eval { output_pref( { dt => dt_from_string( $planneddates[$i] ), dateonly => 1, dateformat => 'iso' } ); };
224 if (defined $publisheddates[$i] && $publisheddates[$i] ne 'XXX') {
225 $pub_date = eval { output_pref( { dt => dt_from_string( $publisheddates[$i] ), dateonly => 1, dateformat => 'iso' } ); };
228 if ( $serialids[$i] && $serialids[$i] eq 'NEW' ) {
229 if ( $serialseqs[$i] ) {
231 #IF newserial was provided a name Then we have to create a newSerial
232 ### FIXME if NewIssue is modified to use subscription biblionumber, then biblionumber would not be useful.
233 $newserial = NewIssue(
234 $serialseqs[$i],
235 $subscriptionids[0],
236 $serialdatalist[0]->{'biblionumber'},
237 $status[$i],
238 $plan_date,
239 $pub_date,
240 $publisheddatetexts[$i],
241 $notes[$i]
245 elsif ( $serialids[$i] ) {
246 ModSerialStatus(
247 $serialids[$i],
248 $serialseqs[$i],
249 $plan_date,
250 $pub_date,
251 $publisheddatetexts[$i],
252 $status[$i],
253 $notes[$i]
256 my $makePreviousSerialAvailable = C4::Context->preference('makePreviousSerialAvailable');
257 if ($makePreviousSerialAvailable && $serialids[$i] ne "NEW") {
258 # We already have created the new expected serial at this point, so we get the second previous serial
259 my $previous = GetPreviousSerialid($subscriptionids[$i]);
260 if ($previous) {
262 my $serialitem = Koha::Serial::Items->search( {serialid => $previous} )->next;
263 my $itemnumber = $serialitem ? $serialitem->itemnumber : undef;
264 if ($itemnumber) {
266 # Getting the itemtype to set from the database
267 my $subscriptioninfos = GetSubscription($subscriptionids[$i]);
269 # Changing the status to "available" and the itemtype according to the previousitemtype db field
270 ModItem({notforloan => 0, itype => $subscriptioninfos->{'previousitemtype'} }, undef, $itemnumber);
276 my @moditems = $query->multi_param('moditem');
277 if ( scalar(@moditems) ) {
278 my @tags = $query->multi_param('tag');
279 my @subfields = $query->multi_param('subfield');
280 my @field_values = $query->multi_param('field_value');
281 my @serials = $query->multi_param('serial');
282 my @bibnums = $query->multi_param('bibnum');
283 my @itemid = $query->multi_param('itemid');
284 my @ind_tag = $query->multi_param('ind_tag');
285 my @indicator = $query->multi_param('indicator');
287 #Rebuilding ALL the data for items into a hash
288 # parting them on $itemid.
289 my %itemhash;
290 my $countdistinct;
291 my $range = scalar(@itemid);
292 for ( my $i = 0 ; $i < $range ; $i++ ) {
293 unless ( $itemhash{ $itemid[$i] } ) {
294 if ( $serials[$countdistinct]
295 && $serials[$countdistinct] ne "NEW" )
297 $itemhash{ $itemid[$i] }->{'serial'} =
298 $serials[$countdistinct];
300 else {
301 $itemhash{ $itemid[$i] }->{'serial'} = $newserial;
303 $itemhash{ $itemid[$i] }->{'bibnum'} = $bibnums[$countdistinct];
304 $countdistinct++;
306 push @{ $itemhash{ $itemid[$i] }->{'tags'} }, $tags[$i];
307 push @{ $itemhash{ $itemid[$i] }->{'subfields'} }, $subfields[$i];
308 push @{ $itemhash{ $itemid[$i] }->{'field_values'} },
309 $field_values[$i];
310 push @{ $itemhash{ $itemid[$i] }->{'ind_tag'} }, $ind_tag[$i];
311 push @{ $itemhash{ $itemid[$i] }->{'indicator'} }, $indicator[$i];
313 foreach my $item ( keys %itemhash ) {
315 # Verify Itemization is "Valid", i.e. serial status is Arrived or Missing
316 my $index = -1;
317 for ( my $i = 0 ; $i < scalar(@serialids) ; $i++ ) {
318 if (
319 $itemhash{$item}->{serial} eq $serialids[$i]
320 || ( $itemhash{$item}->{serial} == $newserial
321 && $serialids[$i] eq 'NEW' )
323 $index = $i
326 if ( $index >= 0 && $status[$index] == 2 ) {
327 my $xml = TransformHtmlToXml(
328 $itemhash{$item}->{'tags'},
329 $itemhash{$item}->{'subfields'},
330 $itemhash{$item}->{'field_values'},
331 $itemhash{$item}->{'indicator'},
332 $itemhash{$item}->{'ind_tag'}
335 # warn $xml;
336 my $bib_record = MARC::Record::new_from_xml( $xml, 'UTF-8' );
337 if ( $item =~ /^N/ ) {
339 #New Item
341 # if autoBarcode is set to 'incremental', calculate barcode...
342 my ( $barcodetagfield, $barcodetagsubfield ) =
343 GetMarcFromKohaField(
344 'items.barcode',
345 GetFrameworkCode(
346 $serialdatalist[0]->{'biblionumber'}
349 if ( C4::Context->preference('autoBarcode') eq
350 'incremental' )
352 if (
354 $bib_record->field($barcodetagfield)
355 && $bib_record->field($barcodetagfield)->subfield($barcodetagsubfield)
359 my $sth_barcode = $dbh->prepare(
360 'select max(abs(barcode)) from items');
361 $sth_barcode->execute;
362 my ($newbarcode) = $sth_barcode->fetchrow;
364 # OK, we have the new barcode, add the entry in MARC record # FIXME -> should be using barcode plugin here.
365 $bib_record->field($barcodetagfield)
366 ->update( $barcodetagsubfield => ++$newbarcode );
370 # check for item barcode # being unique
371 my $exists;
372 if (
373 $bib_record->subfield(
374 $barcodetagfield, $barcodetagsubfield
378 my $barcode = $bib_record->subfield( $barcodetagfield, $barcodetagsubfield );
379 $exists = Koha::Items->find({barcode => $barcode});
382 # push @errors,"barcode_not_unique" if($exists);
383 # if barcode exists, don't create, but report The problem.
384 if ($exists) {
385 push @errors, 'barcode_not_unique';
386 push @errseq, { serialseq => $serialseqs[$index] };
388 else {
389 my ( $biblionumber, $bibitemnum, $itemnumber ) =
390 AddItemFromMarc( $bib_record,
391 $itemhash{$item}->{bibnum} );
392 AddItem2Serial( $itemhash{$item}->{serial},
393 $itemnumber );
396 else {
398 #modify item
399 my ( $oldbiblionumber, $oldbibnum, $itemnumber ) =
400 ModItemFromMarc( $bib_record,
401 $itemhash{$item}->{'bibnum'}, $item );
408 if ( @errors ) {
409 $template->param( Errors => 1 );
410 if ( @errseq ) {
411 $template->param( barcode_not_unique => 1, errseq => \@errseq );
414 else {
415 my $redirect = 'serials-collection.pl?';
416 $redirect .= join( '&', map { 'subscriptionid=' . $_ } @subscriptionids );
417 print $query->redirect($redirect);
420 my $location = $serialdatalist[0]->{'location'};
421 my $default_bib_view = get_default_view();
423 $template->param(
424 subscriptionid => $serialdatalist[0]->{subscriptionid},
425 serialsadditems => $serialdatalist[0]->{'serialsadditems'},
426 callnumber => $serialdatalist[0]->{'callnumber'},
427 internalnotes => $serialdatalist[0]->{'internalnotes'},
428 bibliotitle => $biblio->title,
429 biblionumber => $serialdatalist[0]->{'biblionumber'},
430 serialslist => \@serialdatalist,
431 default_bib_view => $default_bib_view,
432 location => $location,
433 (uc(C4::Context->preference("marcflavour"))) => 1
436 output_html_with_http_headers $query, $cookie, $template->output;
438 sub get_default_view {
439 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
440 my %views = C4::Search::enabled_staff_search_views();
441 if ( $defaultview eq 'isbd' && $views{can_view_ISBD} ) {
442 return 'ISBDdetail';
444 elsif ( $defaultview eq 'marc' && $views{can_view_MARC} ) {
445 return 'MARCdetail';
447 elsif ( $defaultview eq 'labeled_marc' && $views{can_view_labeledMARC} ) {
448 return 'labeledMARCdetail';
450 return 'detail';