MT 2146 :Adding index on issn
[koha.git] / serials / serials-edit.pl
blob08eae33f88064311fde35e2bee49b8cd7425d002
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
21 =head1 NAME
23 serials-recieve.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
65 use strict;
66 use warnings;
67 use CGI;
68 use C4::Auth;
69 use C4::Dates qw/format_date format_date_in_iso/;
70 use C4::Biblio;
71 use C4::Items;
72 use C4::Koha;
73 use C4::Output;
74 use C4::Context;
75 use C4::Serials;
77 #use Smart::Comments;
79 my $query = new CGI;
80 my $dbh = C4::Context->dbh;
81 my @serialids = $query->param('serialid');
82 my @serialseqs = $query->param('serialseq');
83 my @planneddates = $query->param('planneddate');
84 my @publisheddates = $query->param('publisheddate');
85 my @status = $query->param('status');
86 my @notes = $query->param('notes');
87 my @subscriptionids = $query->param('subscriptionid');
88 my $op = $query->param('op');
89 if (scalar(@subscriptionids)==1 && index($subscriptionids[0],",")>0){
90 @subscriptionids =split (/,/,$subscriptionids[0]);
92 my @errors;
93 my @errseq;
94 # If user comes from subscription details
95 unless (@serialids){
96 foreach my $subscriptionid (@subscriptionids){
97 my $serstatus=$query->param('serstatus');
98 if ($serstatus){
99 my ($count,@tmpser)=GetSerials2($subscriptionid,$serstatus);
100 foreach (@tmpser) {
101 push @serialids, $_->{'serialid'};
107 unless (scalar(@serialids)){
108 my $string="serials-collection.pl?subscriptionid=".join(",",@subscriptionids);
109 $string=~s/,$//;
110 # warn $string;
111 print $query->redirect($string);
113 my ($template, $loggedinuser, $cookie)
114 = get_template_and_user({template_name => "serials/serials-edit.tmpl",
115 query => $query,
116 type => "intranet",
117 authnotrequired => 0,
118 flagsrequired => {serials => 1},
119 debug => 1,
122 my @serialdatalist;
123 my %processedserialid;
124 foreach my $tmpserialid (@serialids){
125 #filtering serialid for duplication
126 #NEW serial should appear only once and are created afterwards
127 next unless (defined($tmpserialid) && $tmpserialid =~/^[0-9]+$/ && !$processedserialid{$tmpserialid});
128 my $data=GetSerialInformation($tmpserialid);
129 $data->{publisheddate}=format_date($data->{publisheddate});
130 $data->{planneddate}=format_date($data->{planneddate});
131 $data->{'editdisable'}=((HasSubscriptionExpired($data->{subscriptionid})&& $data->{'status1'})||$data->{'cannotedit'});
132 push @serialdatalist,$data;
133 $processedserialid{$tmpserialid}=1;
135 my $bibdata=GetBiblioData($serialdatalist[0]->{'biblionumber'});
137 my @newserialloop;
138 my @subscriptionloop;
139 # check, for each subscription edited, that we have an empty item line if applicable for the subscription
140 my %processedsubscriptionid;
141 foreach my $subscriptionid (@subscriptionids){
142 #Donot process subscriptionid twice if it was already processed.
143 next unless (defined($subscriptionid) && !$processedsubscriptionid{$subscriptionid});
144 my $cell;
145 if ($serialdatalist[0]->{'serialsadditems'}){
146 #Create New empty item
147 $cell =
148 PrepareItemrecordDisplay( $serialdatalist[0]->{'biblionumber'},'', GetSubscription($subscriptionid));
149 $cell->{serialsadditems} = 1;
151 $cell->{'subscriptionid'}=$subscriptionid;
152 $cell->{'itemid'} = "NNEW";
153 $cell->{'serialid'} = "NEW";
154 $cell->{'issuesatonce'} = 1;
155 push @newserialloop,$cell;
156 push @subscriptionloop, {'subscriptionid'=>$subscriptionid,
157 'abouttoexpire'=>abouttoexpire($subscriptionid),
158 'subscriptionexpired'=>HasSubscriptionExpired($subscriptionid),
160 $processedsubscriptionid{$subscriptionid}=1;
162 $template->param(newserialloop=>\@newserialloop);
163 $template->param(subscriptions=>\@subscriptionloop);
165 if ($op and $op eq 'serialchangestatus') {
166 # my $sth = $dbh->prepare("select status from serial where serialid=?");
167 my $newserial;
168 for (my $i=0;$i<=$#serialids;$i++) {
169 # $sth->execute($serialids[$i]);
170 # my ($oldstatus) = $sth->fetchrow;
171 if ($serialids[$i] && $serialids[$i] eq "NEW") {
172 if ($serialseqs[$i]){
173 #IF newserial was provided a name Then we have to create a newSerial
174 ### FIXME if NewIssue is modified to use subscription biblionumber, then biblionumber would not be useful.
175 $newserial = NewIssue( $serialseqs[$i],$subscriptionids[$i],$serialdatalist[0]->{'biblionumber'},
176 $status[$i],
177 format_date_in_iso($planneddates[$i]),
178 format_date_in_iso($publisheddates[$i]),
179 $notes[$i]);
181 }elsif ($serialids[$i]){
182 ModSerialStatus($serialids[$i],
183 $serialseqs[$i],
184 format_date_in_iso($planneddates[$i]),
185 format_date_in_iso($publisheddates[$i]),
186 $status[$i],
187 $notes[$i]);
190 my @moditems = $query->param('moditem');
191 if (scalar(@moditems)){
192 my @tags = $query->param('tag');
193 my @subfields = $query->param('subfield');
194 my @field_values = $query->param('field_value');
195 my @serials = $query->param('serial');
196 my @bibnums = $query->param('bibnum');
197 my @itemid = $query->param('itemid');
198 my @ind_tag = $query->param('ind_tag');
199 my @indicator = $query->param('indicator');
200 #Rebuilding ALL the data for items into a hash
201 # parting them on $itemid.
202 my %itemhash;
203 my $countdistinct;
204 my $range=scalar(@itemid);
205 for (my $i=0; $i<$range; $i++){
206 unless ($itemhash{$itemid[$i]}){
207 if ($serials[$countdistinct] && $serials[$countdistinct] ne "NEW"){
208 $itemhash{$itemid[$i]}->{'serial'}=$serials[$countdistinct];
209 } else {
210 $itemhash{$itemid[$i]}->{'serial'}=$newserial;
212 $itemhash{$itemid[$i]}->{'bibnum'}=$bibnums[$countdistinct];
213 $countdistinct++;
215 push @{$itemhash{$itemid[$i]}->{'tags'}},$tags[$i];
216 push @{$itemhash{$itemid[$i]}->{'subfields'}},$subfields[$i];
217 push @{$itemhash{$itemid[$i]}->{'field_values'}},$field_values[$i];
218 push @{$itemhash{$itemid[$i]}->{'ind_tag'}},$ind_tag[$i];
219 push @{$itemhash{$itemid[$i]}->{'indicator'}},$indicator[$i];
221 foreach my $item (keys %itemhash){
222 # Verify Itemization is "Valid", i.e. serial status is Arrived or Missing
223 my $index=-1;
224 for (my $i=0; $i<scalar(@serialids);$i++){
225 $index = $i if ($itemhash{$item}->{'serial'} eq $serialids[$i] || ($itemhash{$item}->{'serial'} == $newserial && $serialids[$i] eq "NEW"));
227 if ($index>=0 && $status[$index]==2){
228 my $xml = TransformHtmlToXml( $itemhash{$item}->{'tags'},
229 $itemhash{$item}->{'subfields'},
230 $itemhash{$item}->{'field_values'},
231 $itemhash{$item}->{'ind_tag'},
232 $itemhash{$item}->{'indicator'});
233 # warn $xml;
234 my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
235 if ($item=~/^N/){
236 #New Item
238 # if autoBarcode is set to 'incremental', calculate barcode...
239 my ($barcodetagfield,$barcodetagsubfield) = &GetMarcFromKohaField("items.barcode", GetFrameworkCode($serialdatalist[0]->{'biblionumber'}));
240 if (C4::Context->preference("autoBarcode") eq 'incremental' ) {
241 if (!$record->field($barcodetagfield)->subfield($barcodetagsubfield)) {
242 my $sth_barcode = $dbh->prepare("select max(abs(barcode)) from items");
243 $sth_barcode->execute;
244 my ($newbarcode) = $sth_barcode->fetchrow;
245 # OK, we have the new barcode, add the entry in MARC record # FIXME -> should be using barcode plugin here.
246 $record->field($barcodetagfield)->update( $barcodetagsubfield => ++$newbarcode );
249 # check for item barcode # being unique
250 my $exists;
251 if ($record->subfield($barcodetagfield,$barcodetagsubfield)) {
252 $exists = GetItemnumberFromBarcode($record->subfield($barcodetagfield,$barcodetagsubfield));
254 # push @errors,"barcode_not_unique" if($exists);
255 # if barcode exists, don't create, but report The problem.
256 if ($exists){
257 push @errors,"barcode_not_unique" if($exists);
258 push @errseq,{"serialseq"=>$serialseqs[$index]};
259 } else {
260 my ($biblionumber,$bibitemnum,$itemnumber) = AddItemFromMarc($record,$itemhash{$item}->{'bibnum'});
261 AddItem2Serial($itemhash{$item}->{'serial'},$itemnumber);
263 } else {
264 #modify item
265 my ($oldbiblionumber,$oldbibnum,$itemnumber) = ModItemFromMarc($record,$itemhash{$item}->{'bibnum'},$item);
270 # ### FIXME this part of code is not very pretty. Nor is it very efficient... There MUST be a more perlish way to write it. But it works.
271 # my $redirect ="serials-home.pl?";
272 # $redirect.=join("&",map{"serialseq=".$_} @serialseqs);
273 # $redirect.="&".join("&",map{"planneddate=".$_} @planneddates);
274 # $redirect.="&".join("&",map{"publisheddate=".$_} @publisheddates);
275 # $redirect.="&".join("&",map{"status=".$_} @status);
276 # $redirect.="&".join("&",map{"notes=".$_} @notes);
278 if (scalar(@errors)>0){
279 $template->param("Errors" => 1);
280 if (scalar(@errseq)>0){
281 $template->param("barcode_not_unique" => 1);
282 $template->param('errseq'=>\@errseq);
284 } else {
285 my $redirect ="serials-collection.pl?";
286 my %hashsubscription;
287 foreach (@subscriptionids) {
288 $hashsubscription{$_}=1;
290 $redirect.=join("&",map{"subscriptionid=".$_} sort keys %hashsubscription);
291 print $query->redirect("$redirect");
295 $template->param(
296 serialsadditems => $serialdatalist[0]->{'serialsadditems'},
297 bibliotitle => $bibdata->{'title'},
298 biblionumber => $serialdatalist[0]->{'biblionumber'},
299 serialslist => \@serialdatalist,
301 output_html_with_http_headers $query, $cookie, $template->output;