Bug 20123: Make the timezone entry empty and uncommented by default
[koha.git] / serials / serials-edit.pl
blob090e2d319473d0f01d2f0d3ba673904366011a95
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/;
75 use Koha::DateUtils;
76 use Koha::Serial::Items;
78 use List::MoreUtils qw/uniq/;
80 my $query = CGI->new();
81 my $dbh = C4::Context->dbh;
82 my @serialids = $query->multi_param('serialid');
83 my @serialseqs = $query->multi_param('serialseq');
84 my @planneddates = $query->multi_param('planneddate');
85 my @publisheddates = $query->multi_param('publisheddate');
86 my @publisheddatetexts = $query->multi_param('publisheddatetext');
87 my @status = $query->multi_param('status');
88 my @notes = $query->multi_param('notes');
89 my @subscriptionids = $query->multi_param('subscriptionid');
90 my $op = $query->param('op');
91 if ( scalar(@subscriptionids) == 1 && index( $subscriptionids[0], q|,| ) > 0 ) {
92 @subscriptionids = split( /,/, $subscriptionids[0] );
94 my @errors;
95 my @errseq;
97 # If user comes from subscription details
98 unless (@serialids) {
99 my $serstatus = $query->param('serstatus');
100 my @statuses = split ',', $serstatus;
101 if ($serstatus) {
102 foreach my $subscriptionid (@subscriptionids) {
103 my @tmpser = GetSerials2( $subscriptionid, \@statuses );
104 push @serialids, map { $_->{serialid} } @tmpser;
109 unless ( @serialids ) {
110 my $string =
111 'serials-collection.pl?subscriptionid=' . join ',', uniq @subscriptionids;
112 $string =~ s/,$//;
114 print $query->redirect($string);
117 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
119 template_name => 'serials/serials-edit.tt',
120 query => $query,
121 type => 'intranet',
122 authnotrequired => 0,
123 flagsrequired => { serials => 'receive_serials' },
124 debug => 1,
128 my @serialdatalist;
129 my %processedserialid;
131 my $today = output_pref( { dt => dt_from_string, dateonly => 1 } );
133 foreach my $serialid (@serialids) {
135 #filtering serialid for duplication
136 #NEW serial should appear only once and are created afterwards
137 if ( $serialid
138 && $serialid =~ /^[0-9]+$/
139 && !$processedserialid{$serialid} )
141 my $serinfo = GetSerialInformation($serialid); #TODO duplicates work done by GetSerials2 above
143 for my $d ( qw( publisheddate planneddate )){
144 if ( $serinfo->{$d} =~m/^00/ ) {
145 $serinfo->{$d} = q{};
147 else {
148 $serinfo->{$d} = output_pref( { dt => dt_from_string( $serinfo->{$d} ), dateonly => 1 } );
151 $serinfo->{arriveddate} = $today;
153 $serinfo->{'editdisable'} = (
155 HasSubscriptionExpired( $serinfo->{subscriptionid} )
156 && $serinfo->{'status1'}
158 || $serinfo->{'cannotedit'}
160 $serinfo->{editdisable} = 0 if C4::Auth::haspermission( C4::Context->userenv->{id}, { serials => 'receive_serials' } );
161 $serinfo->{editdisable} ||= ($serinfo->{status8} and $serinfo->{closed});
162 push @serialdatalist, $serinfo;
163 $processedserialid{$serialid} = 1;
166 my $biblio = Koha::Biblios->find( $serialdatalist[0]->{biblionumber} );
168 my @newserialloop;
169 my @subscriptionloop;
171 # check, for each subscription edited, that we have an empty item line if applicable for the subscription
172 my %processedsubscriptionid;
173 foreach my $subscriptionid (@subscriptionids) {
175 #Do not process subscriptionid twice if it was already processed.
176 if ( $subscriptionid && !$processedsubscriptionid{$subscriptionid} )
178 my $cell;
179 if ( $serialdatalist[0]->{'serialsadditems'} ) {
181 #Create New empty item
182 $cell =
183 PrepareItemrecordDisplay( $serialdatalist[0]->{'biblionumber'},
184 '', GetSubscription($subscriptionid) );
185 $cell->{serialsadditems} = 1;
187 $cell->{'subscriptionid'} = $subscriptionid;
188 $cell->{biblionumber} = $serialdatalist[0]->{'biblionumber'};
189 $cell->{'itemid'} = 'NNEW';
190 $cell->{'serialid'} = 'NEW';
191 $cell->{'issuesatonce'} = 1;
192 $cell->{arriveddate} = $today;
194 push @newserialloop, $cell;
195 push @subscriptionloop,
197 'subscriptionid' => $subscriptionid,
198 'abouttoexpire' => abouttoexpire($subscriptionid),
199 'subscriptionexpired' => HasSubscriptionExpired($subscriptionid),
201 $processedsubscriptionid{$subscriptionid} = 1;
204 $template->param( newserialloop => \@newserialloop );
205 $template->param( subscriptions => \@subscriptionloop );
207 if ( $op and $op eq 'serialchangestatus' ) {
209 # Convert serialseqs to UTF-8 to prevent encoding problems
210 foreach my $seq (@serialseqs) {
211 $seq = Encode::decode('UTF-8', $seq) unless Encode::is_utf8($seq);
214 my $newserial;
215 for ( my $i = 0 ; $i <= $#serialids ; $i++ ) {
216 my ($plan_date, $pub_date);
218 if (defined $planneddates[$i] && $planneddates[$i] ne 'XXX') {
219 $plan_date = eval { output_pref( { dt => dt_from_string( $planneddates[$i] ), dateonly => 1, dateformat => 'iso' } ); };
221 if (defined $publisheddates[$i] && $publisheddates[$i] ne 'XXX') {
222 $pub_date = eval { output_pref( { dt => dt_from_string( $publisheddates[$i] ), dateonly => 1, dateformat => 'iso' } ); };
225 if ( $serialids[$i] && $serialids[$i] eq 'NEW' ) {
226 if ( $serialseqs[$i] ) {
228 #IF newserial was provided a name Then we have to create a newSerial
229 ### FIXME if NewIssue is modified to use subscription biblionumber, then biblionumber would not be useful.
230 $newserial = NewIssue(
231 $serialseqs[$i],
232 $subscriptionids[0],
233 $serialdatalist[0]->{'biblionumber'},
234 $status[$i],
235 $plan_date,
236 $pub_date,
237 $publisheddatetexts[$i],
238 $notes[$i]
242 elsif ( $serialids[$i] ) {
243 ModSerialStatus(
244 $serialids[$i],
245 $serialseqs[$i],
246 $plan_date,
247 $pub_date,
248 $publisheddatetexts[$i],
249 $status[$i],
250 $notes[$i]
253 my $makePreviousSerialAvailable = C4::Context->preference('makePreviousSerialAvailable');
254 if ($makePreviousSerialAvailable && $serialids[$i] ne "NEW") {
255 # We already have created the new expected serial at this point, so we get the second previous serial
256 my $previous = GetPreviousSerialid($subscriptionids[$i]);
257 if ($previous) {
259 my $serialitem = Koha::Serial::Items->search( {serialid => $previous} )->next;
260 my $itemnumber = $serialitem ? $serialitem->itemnumber : undef;
261 if ($itemnumber) {
263 # Getting the itemtype to set from the database
264 my $subscriptioninfos = GetSubscription($subscriptionids[$i]);
266 # Changing the status to "available" and the itemtype according to the previousitemtype db field
267 ModItem({notforloan => 0, itype => $subscriptioninfos->{'previousitemtype'} }, undef, $itemnumber);
273 my @moditems = $query->multi_param('moditem');
274 if ( scalar(@moditems) ) {
275 my @tags = $query->multi_param('tag');
276 my @subfields = $query->multi_param('subfield');
277 my @field_values = $query->multi_param('field_value');
278 my @serials = $query->multi_param('serial');
279 my @bibnums = $query->multi_param('bibnum');
280 my @itemid = $query->multi_param('itemid');
281 my @ind_tag = $query->multi_param('ind_tag');
282 my @indicator = $query->multi_param('indicator');
284 #Rebuilding ALL the data for items into a hash
285 # parting them on $itemid.
286 my %itemhash;
287 my $countdistinct;
288 my $range = scalar(@itemid);
289 for ( my $i = 0 ; $i < $range ; $i++ ) {
290 unless ( $itemhash{ $itemid[$i] } ) {
291 if ( $serials[$countdistinct]
292 && $serials[$countdistinct] ne "NEW" )
294 $itemhash{ $itemid[$i] }->{'serial'} =
295 $serials[$countdistinct];
297 else {
298 $itemhash{ $itemid[$i] }->{'serial'} = $newserial;
300 $itemhash{ $itemid[$i] }->{'bibnum'} = $bibnums[$countdistinct];
301 $countdistinct++;
303 push @{ $itemhash{ $itemid[$i] }->{'tags'} }, $tags[$i];
304 push @{ $itemhash{ $itemid[$i] }->{'subfields'} }, $subfields[$i];
305 push @{ $itemhash{ $itemid[$i] }->{'field_values'} },
306 $field_values[$i];
307 push @{ $itemhash{ $itemid[$i] }->{'ind_tag'} }, $ind_tag[$i];
308 push @{ $itemhash{ $itemid[$i] }->{'indicator'} }, $indicator[$i];
310 foreach my $item ( keys %itemhash ) {
312 # Verify Itemization is "Valid", i.e. serial status is Arrived or Missing
313 my $index = -1;
314 for ( my $i = 0 ; $i < scalar(@serialids) ; $i++ ) {
315 if (
316 $itemhash{$item}->{serial} eq $serialids[$i]
317 || ( $itemhash{$item}->{serial} == $newserial
318 && $serialids[$i] eq 'NEW' )
320 $index = $i
323 if ( $index >= 0 && $status[$index] == 2 ) {
324 my $xml = TransformHtmlToXml(
325 $itemhash{$item}->{'tags'},
326 $itemhash{$item}->{'subfields'},
327 $itemhash{$item}->{'field_values'},
328 $itemhash{$item}->{'indicator'},
329 $itemhash{$item}->{'ind_tag'}
332 # warn $xml;
333 my $bib_record = MARC::Record::new_from_xml( $xml, 'UTF-8' );
334 if ( $item =~ /^N/ ) {
336 #New Item
338 # if autoBarcode is set to 'incremental', calculate barcode...
339 my ( $barcodetagfield, $barcodetagsubfield ) =
340 GetMarcFromKohaField(
341 'items.barcode',
342 GetFrameworkCode(
343 $serialdatalist[0]->{'biblionumber'}
346 if ( C4::Context->preference('autoBarcode') eq
347 'incremental' )
349 if (
351 $bib_record->field($barcodetagfield)
352 && $bib_record->field($barcodetagfield)->subfield($barcodetagsubfield)
356 my $sth_barcode = $dbh->prepare(
357 'select max(abs(barcode)) from items');
358 $sth_barcode->execute;
359 my ($newbarcode) = $sth_barcode->fetchrow;
361 # OK, we have the new barcode, add the entry in MARC record # FIXME -> should be using barcode plugin here.
362 $bib_record->field($barcodetagfield)
363 ->update( $barcodetagsubfield => ++$newbarcode );
367 # check for item barcode # being unique
368 my $exists;
369 if (
370 $bib_record->subfield(
371 $barcodetagfield, $barcodetagsubfield
375 $exists = GetItemnumberFromBarcode(
376 $bib_record->subfield(
377 $barcodetagfield, $barcodetagsubfield
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 serialsadditems => $serialdatalist[0]->{'serialsadditems'},
425 callnumber => $serialdatalist[0]->{'callnumber'},
426 internalnotes => $serialdatalist[0]->{'internalnotes'},
427 bibliotitle => $biblio->title,
428 biblionumber => $serialdatalist[0]->{'biblionumber'},
429 serialslist => \@serialdatalist,
430 default_bib_view => $default_bib_view,
431 location => $location,
432 (uc(C4::Context->preference("marcflavour"))) => 1
435 output_html_with_http_headers $query, $cookie, $template->output;
437 sub get_default_view {
438 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
439 my %views = C4::Search::enabled_staff_search_views();
440 if ( $defaultview eq 'isbd' && $views{can_view_ISBD} ) {
441 return 'ISBDdetail';
443 elsif ( $defaultview eq 'marc' && $views{can_view_MARC} ) {
444 return 'MARCdetail';
446 elsif ( $defaultview eq 'labeled_marc' && $views{can_view_labeledMARC} ) {
447 return 'labeledMARCdetail';
449 return 'detail';