1 package C4::Bull; #assumes C4/Bull.pm
4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
27 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
29 # set the version for version checking
34 C4::Bull - Give functions for serializing.
42 Give all XYZ functions
47 @EXPORT = qw(&newsubscription &modsubscription &delsubscription &getsubscriptions &getsubscription
48 &getsubscriptionfrombiblionumber &get_subscription_list_from_biblionumber
49 &modsubscriptionhistory
50 &getserials &serialchangestatus
51 &Find_Next_Date, &Get_Next_Seq
52 &hassubscriptionexpired &subscriptionexpirationdate &subscriptionrenew
53 &getSupplierListWithLateIssues &GetLateIssues);
55 sub getSupplierListWithLateIssues {
56 my $dbh = C4::Context->dbh;
57 my $sth = $dbh->prepare("SELECT DISTINCT id, name
58 FROM subscription, serial
59 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
60 WHERE subscription.subscriptionid = serial.subscriptionid AND
61 (planneddate < now( ) OR serial.STATUS = 3)
65 while (my ($id,$name) = $sth->fetchrow) {
66 $supplierlist{$id} = $name;
71 my ($supplierid) = @_;
72 my $dbh = C4::Context->dbh;
75 $sth = $dbh->prepare("SELECT name,title,planneddate,serialseq,serial.subscriptionid
76 FROM subscription, serial, biblio
77 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
78 WHERE subscription.subscriptionid = serial.subscriptionid AND
79 ((planneddate < now() and serial.STATUS =1) OR serial.STATUS = 3) and
80 subscription.aqbooksellerid=$supplierid and
81 biblio.biblionumber = subscription.biblionumber
84 $sth = $dbh->prepare("SELECT name,title,planneddate,serialseq,serial.subscriptionid
85 FROM subscription, serial, biblio
86 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
87 WHERE subscription.subscriptionid = serial.subscriptionid AND
88 ((planneddate < now() and serial.STATUS <=3) OR serial.STATUS = 3) and
89 biblio.biblionumber = subscription.biblionumber
95 while (my $line = $sth->fetchrow_hashref) {
96 $line->{title} = "" if $line->{title} eq $last_title;
97 $last_title = $line->{title} if ($line->{title});
98 $line->{planneddate} = format_date($line->{planneddate});
99 push @issuelist,$line;
103 sub newsubscription {
104 my ($auser,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
105 $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
106 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,
107 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,
108 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,
109 $numberingmethod, $status, $notes) = @_;
110 my $dbh = C4::Context->dbh;
112 my $sth=$dbh->prepare("insert into subscription (librarian,aqbooksellerid,cost,aqbudgetid,biblionumber,
113 startdate,periodicity,dow,numberlength,weeklength,monthlength,
114 add1,every1,whenmorethan1,setto1,lastvalue1,
115 add2,every2,whenmorethan2,setto2,lastvalue2,
116 add3,every3,whenmorethan3,setto3,lastvalue3,
117 numberingmethod, status, notes) values
119 ?,?,?,?,?,?,?,?,?,?,
120 ?,?,?,?,?,?,?,?,?,?)");
121 $sth->execute($auser,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
122 format_date_in_iso($startdate),$periodicity,$dow,$numberlength,$weeklength,$monthlength,
123 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,
124 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,
125 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,
126 $numberingmethod, $status, $notes);
127 #then create the 1st waited number
128 my $subscriptionid = $dbh->{'mysql_insertid'};
129 $sth = $dbh->prepare("insert into subscriptionhistory (biblionumber, subscriptionid, histstartdate, enddate, missinglist, recievedlist, opacnote, librariannote) values (?,?,?,?,?,?,?,?)");
130 $sth->execute($biblionumber, $subscriptionid, format_date_in_iso($startdate), 0, "", "", "", $notes);
131 # reread subscription to get a hash (for calculation of the 1st issue number)
132 $sth = $dbh->prepare("select * from subscription where subscriptionid = ? ");
133 $sth->execute($subscriptionid);
134 my $val = $sth->fetchrow_hashref;
136 # calculate issue number
137 my $serialseq = Get_Seq($val);
138 $sth = $dbh->prepare("insert into serial (serialseq,subscriptionid,biblionumber,status, planneddate) values (?,?,?,?,?)");
139 $sth->execute($serialseq, $subscriptionid, $val->{'biblionumber'}, 1, format_date_in_iso($startdate));
140 return $subscriptionid;
142 sub getsubscription {
143 my ($subscriptionid) = @_;
144 my $dbh = C4::Context->dbh;
145 my $sth = $dbh->prepare('select subscription.*,subscriptionhistory.*,aqbudget.bookfundid,aqbooksellers.name as aqbooksellername,biblio.title as bibliotitle
147 left join subscriptionhistory on subscription.subscriptionid=subscriptionhistory.subscriptionid
148 left join aqbudget on subscription.aqbudgetid=aqbudget.aqbudgetid
149 left join aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id
150 left join biblio on biblio.biblionumber=subscription.biblionumber
151 where subscription.subscriptionid = ?');
152 $sth->execute($subscriptionid);
153 my $subs = $sth->fetchrow_hashref;
157 sub getsubscriptionfrombiblionumber {
158 my ($biblionumber) = @_;
159 my $dbh = C4::Context->dbh;
160 my $sth = $dbh->prepare('select count(*) from subscription where biblionumber=?');
161 $sth->execute($biblionumber);
162 my $subscriptionsnumber = $sth->fetchrow;
163 return $subscriptionsnumber;
166 sub get_subscription_list_from_biblionumber {
167 my ($biblionumber) = @_;
168 my $dbh = C4::Context->dbh;
169 my $sth = $dbh->prepare('select subscription.*,subscriptionhistory.*,aqbudget.bookfundid,aqbooksellers.name as aqbooksellername,biblio.title as bibliotitle
171 left join subscriptionhistory on subscription.subscriptionid=subscriptionhistory.subscriptionid
172 left join aqbudget on subscription.aqbudgetid=aqbudget.aqbudgetid
173 left join aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id
174 left join biblio on biblio.biblionumber=subscription.biblionumber
175 where subscription.biblionumber = ?');
176 $sth->execute($biblionumber);
178 while (my $subs = $sth->fetchrow_hashref) {
179 $subs->{'startdate'} = format_date($subs->{'startdate'});
186 sub modsubscription {
187 my ($auser,$aqbooksellerid,$cost,$aqbudgetid,$startdate,
188 $periodicity,$dow,$numberlength,$weeklength,$monthlength,
189 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
190 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
191 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
192 $numberingmethod, $status, $biblionumber, $notes, $subscriptionid)= @_;
193 my $dbh = C4::Context->dbh;
194 my $sth=$dbh->prepare("update subscription set librarian=?, aqbooksellerid=?,cost=?,aqbudgetid=?,startdate=?,
195 periodicity=?,dow=?,numberlength=?,weeklength=?,monthlength=?,
196 add1=?,every1=?,whenmorethan1=?,setto1=?,lastvalue1=?,innerloop1=?,
197 add2=?,every2=?,whenmorethan2=?,setto2=?,lastvalue2=?,innerloop2=?,
198 add3=?,every3=?,whenmorethan3=?,setto3=?,lastvalue3=?,innerloop3=?,
199 numberingmethod=?, status=?, biblionumber=?, notes=? where subscriptionid = ?");
200 $sth->execute($auser,$aqbooksellerid,$cost,$aqbudgetid,$startdate,
201 $periodicity,$dow,$numberlength,$weeklength,$monthlength,
202 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
203 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
204 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
205 $numberingmethod, $status, $biblionumber, $notes, $subscriptionid);
210 sub delsubscription {
211 my ($subscriptionid) = @_;
212 # check again there is only one issue (the subscription is new)
213 my ($totalissues) = getserials($subscriptionid);
214 if ($totalissues eq 1) {
215 my $dbh = C4::Context->dbh;
216 $subscriptionid=$dbh->quote($subscriptionid);
217 $dbh->do("delete from subscription where subscriptionid=$subscriptionid");
218 $dbh->do("delete from subscriptionhistory where subscriptionid=$subscriptionid");
219 $dbh->do("delete from serial where subscriptionid=$subscriptionid");
222 sub getsubscriptions {
223 my ($title,$ISSN,$biblionumber) = @_;
224 return unless $title or $ISSN or $biblionumber;
225 my $dbh = C4::Context->dbh;
228 $sth = $dbh->prepare("select subscription.subscriptionid,biblio.title,biblioitems.issn,subscription.notes from subscription,biblio,biblioitems where biblio.biblionumber = biblioitems.biblionumber and biblio.biblionumber=subscription.biblionumber and biblio.biblionumber=?");
229 $sth->execute($biblionumber);
231 if ($ISSN and $title)
233 $sth = $dbh->prepare("select subscription.subscriptionid,biblio.title,biblioitems.issn,subscription.notes from subscription,biblio,biblioitems where biblio.biblionumber = biblioitems.biblionumber and biblio.biblionumber=subscription.biblionumber and (biblio.title like ? or biblioitems.issn = ? )");
234 $sth->execute("%$title%",$ISSN);
240 $sth = $dbh->prepare("select subscription.subscriptionid,biblio.title,biblioitems.issn,subscription.notes from subscription,biblio,biblioitems where biblio.biblionumber = biblioitems.biblionumber and biblio.biblionumber=subscription.biblionumber and biblioitems.issn = ?");
241 $sth->execute($ISSN);
245 $sth = $dbh->prepare("select subscription.subscriptionid,biblio.title,biblioitems.issn,subscription.notes from subscription,biblio,biblioitems where biblio.biblionumber = biblioitems.biblionumber and
246 biblio.biblionumber=subscription.biblionumber and biblio.title like ? ");
247 $sth->execute("%$title%");
252 while (my $line = $sth->fetchrow_hashref) {
253 push @results, $line;
258 sub modsubscriptionhistory {
259 my ($subscriptionid,$histstartdate,$enddate,$recievedlist,$missinglist,$opacnote,$librariannote)=@_;
260 my $dbh=C4::Context->dbh;
261 my $sth = $dbh->prepare("update subscriptionhistory set histstartdate=?,enddate=?,recievedlist=?,missinglist=?,opacnote=?,librariannote=? where subscriptionid=?");
262 $sth->execute($histstartdate,$enddate,$recievedlist,$missinglist,$opacnote,$librariannote,$subscriptionid);
264 # get every serial not arrived for a given subscription
265 # as well as the number of issues registered in the database (all types)
266 # this number is used to see if a subscription can be deleted (=it must have only 1 issue)
268 my ($subscriptionid) = @_;
269 my $dbh = C4::Context->dbh;
270 # status = 2 is "arrived"
271 my $sth=$dbh->prepare("select serialid,serialseq, status, planneddate from serial where subscriptionid = ? and status <>2 and status <>4");
272 $sth->execute($subscriptionid);
274 while(my $line = $sth->fetchrow_hashref) {
275 $line->{"status".$line->{status}} = 1; # fills a "statusX" value, used for template status select list
276 $line->{"planneddate"} = format_date($line->{"planneddate"});
279 $sth=$dbh->prepare("select count(*) from serial where subscriptionid=?");
280 $sth->execute($subscriptionid);
281 my ($totalissues) = $sth->fetchrow;
282 return ($totalissues,@serials);
285 sub serialchangestatus {
286 my ($serialid,$serialseq,$planneddate,$status)=@_;
287 # warn "($serialid,$serialseq,$planneddate,$status)";
288 # 1st, get previous status : if we change from "waited" to something else, then we will have to create a new "waited" entry
289 my $dbh = C4::Context->dbh;
290 my $sth = $dbh->prepare("select subscriptionid,status from serial where serialid=?");
291 $sth->execute($serialid);
292 my ($subscriptionid,$oldstatus) = $sth->fetchrow;
293 # change status & update subscriptionhistory
294 $sth = $dbh->prepare("update serial set serialseq=?,planneddate=?,status=? where serialid = ?");
295 $sth->execute($serialseq,$planneddate,$status,$serialid);
296 $sth = $dbh->prepare("select missinglist,recievedlist from subscriptionhistory where subscriptionid=?");
297 $sth->execute($subscriptionid);
298 my ($missinglist,$recievedlist) = $sth->fetchrow;
300 $recievedlist .= ",$serialseq";
303 $missinglist .= ",$serialseq";
305 $sth=$dbh->prepare("update subscriptionhistory set recievedlist=?, missinglist=? where subscriptionid=?");
306 $sth->execute($recievedlist,$missinglist,$subscriptionid);
307 # create new waited entry if needed (ie : was a "waited" and has changed)
308 if ($oldstatus eq 1 && $status ne 1) {
309 $sth = $dbh->prepare("select * from subscription where subscriptionid = ? ");
310 $sth->execute($subscriptionid);
311 my $val = $sth->fetchrow_hashref;
313 my ($newserialseq,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3) = Get_Next_Seq($val);
314 # next date (calculated from actual date & frequency parameters)
315 my $nextplanneddate = Get_Next_Date($planneddate,$val);
316 $sth = $dbh->prepare("insert into serial (serialseq,subscriptionid,biblionumber,status, planneddate) values (?,?,?,?,?)");
317 $sth->execute($newserialseq, $subscriptionid, $val->{'biblionumber'}, 1, $nextplanneddate);
318 $sth = $dbh->prepare("update subscription set lastvalue1=?, lastvalue2=?,lastvalue3=?,
319 innerloop1=?,innerloop2=?,innerloop3=?
320 where subscriptionid = ?");
321 $sth->execute($newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3,$subscriptionid);
325 sub Get_Next_Date(@) {
326 my ($planneddate,$subscription) = @_;
328 if ($subscription->{periodicity} == 1) {
329 $resultdate=DateCalc($planneddate,"1 day");
331 if ($subscription->{periodicity} == 2) {
332 $resultdate=DateCalc($planneddate,"1 week");
334 if ($subscription->{periodicity} == 3) {
335 $resultdate=DateCalc($planneddate,"2 weeks");
337 if ($subscription->{periodicity} == 4) {
338 $resultdate=DateCalc($planneddate,"3 weeks");
340 if ($subscription->{periodicity} == 5) {
341 $resultdate=DateCalc($planneddate,"1 month");
343 if ($subscription->{periodicity} == 6) {
344 $resultdate=DateCalc($planneddate,"2 months");
346 if ($subscription->{periodicity} == 7) {
347 $resultdate=DateCalc($planneddate,"3 months");
349 if ($subscription->{periodicity} == 8) {
350 $resultdate=DateCalc($planneddate,"3 months");
352 if ($subscription->{periodicity} == 9) {
353 $resultdate=DateCalc($planneddate,"2 weeks");
355 if ($subscription->{periodicity} == 10) {
356 $resultdate=DateCalc($planneddate,"1 year");
358 if ($subscription->{periodicity} == 11) {
359 $resultdate=DateCalc($planneddate,"2 years");
361 return format_date_in_iso($resultdate);
366 my $calculated = $val->{numberingmethod};
367 my $x=$val->{'lastvalue1'};
368 $calculated =~ s/\{X\}/$x/g;
369 my $y=$val->{'lastvalue2'};
370 $calculated =~ s/\{Y\}/$y/g;
371 my $z=$val->{'lastvalue3'};
372 $calculated =~ s/\{Z\}/$z/g;
378 my ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
379 $calculated = $val->{numberingmethod};
380 # calculate the (expected) value of the next issue recieved.
381 $newlastvalue1 = $val->{lastvalue1};
382 # check if we have to increase the new value.
383 $newinnerloop1 = $val->{innerloop1}+1;
384 $newinnerloop1=0 if ($newinnerloop1 >= $val->{every1});
385 $newlastvalue1 += $val->{add1} if ($newinnerloop1<1); # <1 to be true when 0 or empty.
386 $newlastvalue1=$val->{setto1} if ($newlastvalue1>$val->{whenmorethan1}); # reset counter if needed.
387 $calculated =~ s/\{X\}/$newlastvalue1/g;
389 $newlastvalue2 = $val->{lastvalue2};
390 # check if we have to increase the new value.
391 $newinnerloop2 = $val->{innerloop2}+1;
392 $newinnerloop2=0 if ($newinnerloop2 >= $val->{every2});
393 $newlastvalue2 += $val->{add2} if ($newinnerloop2<1); # <1 to be true when 0 or empty.
394 $newlastvalue2=$val->{setto2} if ($newlastvalue2>$val->{whenmorethan2}); # reset counter if needed.
395 $calculated =~ s/\{Y\}/$newlastvalue2/g;
397 $newlastvalue3 = $val->{lastvalue3};
398 # check if we have to increase the new value.
399 $newinnerloop3 = $val->{innerloop3}+1;
400 $newinnerloop3=0 if ($newinnerloop3 >= $val->{every3});
401 $newlastvalue3 += $val->{add3} if ($newinnerloop3<1); # <1 to be true when 0 or empty.
402 $newlastvalue3=$val->{setto3} if ($newlastvalue3>$val->{whenmorethan3}); # reset counter if needed.
403 $calculated =~ s/\{Z\}/$newlastvalue3/g;
404 return ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
407 # the subscription has expired when the next issue to arrive is out of subscription limit.
408 sub hassubscriptionexpired {
409 my ($subscriptionid) = @_;
410 my $dbh = C4::Context->dbh;
411 my $subscription = getsubscription($subscriptionid);
412 # we don't do the same test if the subscription is based on X numbers or on X weeks/months
413 if ($subscription->{numberlength}) {
414 my $sth = $dbh->prepare("select count(*) from serial where subscriptionid=? and planneddate>=?");
415 $sth->execute($subscriptionid,$subscription->{startdate});
416 my $res = $sth->fetchrow;
417 if ($subscription->{numberlength}>=$res) {
423 #a little bit more tricky if based on X weeks/months : search if the latest issue waited is not after subscription startdate + duration
424 my $sth = $dbh->prepare("select max(planneddate) from serial where subscriptionid=?");
425 $sth->execute($subscriptionid);
426 my $res = ParseDate(format_date_in_iso($sth->fetchrow));
427 my $endofsubscriptiondate;
428 $endofsubscriptiondate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{monthlength}." months") if ($subscription->{monthlength});
429 $endofsubscriptiondate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{weeklength}." weeks") if ($subscription->{weeklength});
430 return 1 if ($res >= $endofsubscriptiondate);
435 sub subscriptionexpirationdate {
436 my ($subscriptionid) = @_;
437 my $dbh = C4::Context->dbh;
438 my $subscription = getsubscription($subscriptionid);
439 my $enddate=$subscription->{startdate};
440 # we don't do the same test if the subscription is based on X numbers or on X weeks/months
441 if ($subscription->{numberlength}) {
442 #calculate the date of the last issue.
443 for (my $i=1;$i<=$subscription->{numberlength};$i++) {
444 $enddate = Get_Next_Date($enddate,$subscription);
447 $enddate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{monthlength}." months") if ($subscription->{monthlength});
448 $enddate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{weeklength}." weeks") if ($subscription->{weeklength});
450 # $enddate=format_date_in_iso($enddate);
451 # warn "END : $enddate";
455 sub subscriptionrenew {
456 my ($subscriptionid,$user,$startdate,$numberlength,$weeklength,$monthlength,$note) = @_;
457 my $dbh = C4::Context->dbh;
458 my $subscription = getsubscription($subscriptionid);
459 my $sth = $dbh->prepare("select * from biblio,biblioitems where biblio.biblionumber=biblioitems.biblionumber and biblio.biblionumber=?");
460 $sth->execute($subscription->{biblionumber});
461 my $biblio = $sth->fetchrow_hashref;
462 newsuggestion($user,$subscription->{bibliotitle},$biblio->{author},$biblio->{publishercode},$biblio->{note},,,,,$subscription->{biblionumber});
464 $sth=$dbh->prepare("update subscription set startdate=?,numberlength=?,weeklength=?,monthlength=?");
465 $sth->execute(format_date_in_iso($startdate),$numberlength,$weeklength,$monthlength);
467 END { } # module clean-up code here (global destructor)