Bug 3587 - OPACdefaultSortField options error
[koha.git] / serials / serials-edit.pl
blob9994e445b489c9581918faf9a04872cfdccf004e
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 CGI;
67 use C4::Auth;
68 use C4::Dates qw/format_date format_date_in_iso/;
69 use C4::Biblio;
70 use C4::Items;
71 use C4::Koha;
72 use C4::Output;
73 use C4::Context;
74 use C4::Serials;
76 #use Smart::Comments;
78 my $query = new CGI;
79 my $dbh = C4::Context->dbh;
80 my @serialids = $query->param('serialid');
81 my @serialseqs = $query->param('serialseq');
82 my @planneddates = $query->param('planneddate');
83 my @publisheddates = $query->param('publisheddate');
84 my @status = $query->param('status');
85 my @notes = $query->param('notes');
86 my @subscriptionids = $query->param('subscriptionid');
87 my $op = $query->param('op');
88 if (scalar(@subscriptionids)==1 && index($subscriptionids[0],",")>0){
89 @subscriptionids =split (/,/,$subscriptionids[0]);
91 my @errors;
92 my @errseq;
93 my $redirectstring;
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 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 = GetItemnumberFromBarcode($record->subfield($barcodetagfield,$barcodetagsubfield)) if ($record->subfield($barcodetagfield,$barcodetagsubfield));
251 # push @errors,"barcode_not_unique" if($exists);
252 # if barcode exists, don't create, but report The problem.
253 if ($exists){
254 push @errors,"barcode_not_unique" if($exists);
255 push @errseq,{"serialseq"=>$serialseqs[$index]};
256 } else {
257 my ($biblionumber,$bibitemnum,$itemnumber) = AddItemFromMarc($record,$itemhash{$item}->{'bibnum'});
258 AddItem2Serial($itemhash{$item}->{'serial'},$itemnumber);
260 } else {
261 #modify item
262 my ($oldbiblionumber,$oldbibnum,$itemnumber) = ModItemFromMarc($record,$itemhash{$item}->{'bibnum'},$item);
267 # ### 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.
268 # my $redirect ="serials-home.pl?";
269 # $redirect.=join("&",map{"serialseq=".$_} @serialseqs);
270 # $redirect.="&".join("&",map{"planneddate=".$_} @planneddates);
271 # $redirect.="&".join("&",map{"publisheddate=".$_} @publisheddates);
272 # $redirect.="&".join("&",map{"status=".$_} @status);
273 # $redirect.="&".join("&",map{"notes=".$_} @notes);
275 if (scalar(@errors)>0){
276 $template->param("Errors" => 1);
277 if (scalar(@errseq)>0){
278 $template->param("barcode_not_unique" => 1);
279 $template->param('errseq'=>\@errseq);
281 } else {
282 my $redirect ="serials-collection.pl?";
283 my %hashsubscription;
284 foreach (@subscriptionids) {
285 $hashsubscription{$_}=1;
287 $redirect.=join("&",map{"subscriptionid=".$_} sort keys %hashsubscription);
288 print $query->redirect("$redirect");
292 $template->param(
293 serialsadditems => $serialdatalist[0]->{'serialsadditems'},
294 bibliotitle => $bibdata->{'title'},
295 biblionumber => $serialdatalist[0]->{'biblionumber'},
296 serialslist => \@serialdatalist,
298 output_html_with_http_headers $query, $cookie, $template->output;