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 &getsubscriptions &getsubscription &getsubscriptionfrombiblionumber
48 &modsubscriptionhistory
49 &getserials &serialchangestatus
50 &Find_Next_Date, &Get_Next_Seq
51 &hassubscriptionexpired &subscriptionexpirationdate &subscriptionrenew
52 &getSupplierListWithLateIssues &GetLateIssues);
54 sub getSupplierListWithLateIssues {
55 my $dbh = C4::Context->dbh;
56 my $sth = $dbh->prepare("SELECT DISTINCT id, name
57 FROM subscription, serial
58 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
59 WHERE subscription.subscriptionid = serial.subscriptionid AND
60 (planneddate < now( ) OR serial.STATUS = 3)
64 while (my ($id,$name) = $sth->fetchrow) {
65 $supplierlist{$id} = $name;
70 my ($supplierid) = @_;
71 my $dbh = C4::Context->dbh;
74 $sth = $dbh->prepare("SELECT name,title,planneddate,serialseq,serial.subscriptionid
75 FROM subscription, serial, biblio
76 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
77 WHERE subscription.subscriptionid = serial.subscriptionid AND
78 ((planneddate < now() and serial.STATUS =1) OR serial.STATUS = 3) and
79 subscription.aqbooksellerid=$supplierid and
80 biblio.biblionumber = subscription.biblionumber
83 $sth = $dbh->prepare("SELECT name,title,planneddate,serialseq,serial.subscriptionid
84 FROM subscription, serial, biblio
85 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
86 WHERE subscription.subscriptionid = serial.subscriptionid AND
87 ((planneddate < now() and serial.STATUS <=3) OR serial.STATUS = 3) and
88 biblio.biblionumber = subscription.biblionumber
94 while (my $line = $sth->fetchrow_hashref) {
95 $line->{title} = "" if $line->{title} eq $last_title;
96 $last_title = $line->{title} if ($line->{title});
97 $line->{planneddate} = format_date($line->{planneddate});
98 push @issuelist,$line;
102 sub newsubscription {
103 my ($auser,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
104 $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
105 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,
106 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,
107 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,
108 $numberingmethod, $status, $notes) = @_;
109 my $dbh = C4::Context->dbh;
111 my $sth=$dbh->prepare("insert into subscription (librarian,aqbooksellerid,cost,aqbudgetid,biblionumber,
112 startdate,periodicity,dow,numberlength,weeklength,monthlength,
113 add1,every1,whenmorethan1,setto1,lastvalue1,
114 add2,every2,whenmorethan2,setto2,lastvalue2,
115 add3,every3,whenmorethan3,setto3,lastvalue3,
116 numberingmethod, status, notes) values
118 ?,?,?,?,?,?,?,?,?,?,
119 ?,?,?,?,?,?,?,?,?,?)");
120 $sth->execute($auser,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
121 format_date_in_iso($startdate),$periodicity,$dow,$numberlength,$weeklength,$monthlength,
122 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,
123 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,
124 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,
125 $numberingmethod, $status, $notes);
126 #then create the 1st waited number
127 my $subscriptionid = $dbh->{'mysql_insertid'};
128 $sth = $dbh->prepare("insert into subscriptionhistory (biblionumber, subscriptionid, histstartdate, enddate, missinglist, recievedlist, opacnote, librariannote) values (?,?,?,?,?,?,?,?)");
129 $sth->execute($biblionumber, $subscriptionid, format_date_in_iso($startdate), 0, "", "", "", $notes);
130 # reread subscription to get a hash (for calculation of the 1st issue number)
131 $sth = $dbh->prepare("select * from subscription where subscriptionid = ? ");
132 $sth->execute($subscriptionid);
133 my $val = $sth->fetchrow_hashref;
135 # calculate issue number
136 my $serialseq = Get_Seq($val);
137 $sth = $dbh->prepare("insert into serial (serialseq,subscriptionid,biblionumber,status, planneddate) values (?,?,?,?,?)");
138 $sth->execute($serialseq, $subscriptionid, $val->{'biblionumber'}, 1, format_date_in_iso($startdate));
141 #my ($newserialseq,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3) = Get_Next_Seq($val);
142 # next date (calculated from actual date & frequency parameters)
143 #my $nextplanneddate = Get_Next_Date($startdate,$val);
144 # $sth = $dbh->prepare("update subscription set lastvalue1=?, lastvalue2=?,lastvalue3=?,
145 # innerloop1=?,innerloop2=?,innerloop3=?
146 # where subscriptionid = ?");
147 # $sth->execute($newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3,$subscriptionid);
149 return $subscriptionid;
151 sub getsubscription {
152 my ($subscriptionid) = @_;
153 my $dbh = C4::Context->dbh;
154 my $sth = $dbh->prepare('select subscription.*,subscriptionhistory.*,aqbudget.bookfundid,aqbooksellers.name as aqbooksellername,biblio.title as bibliotitle
156 left join subscriptionhistory on subscription.subscriptionid=subscriptionhistory.subscriptionid
157 left join aqbudget on subscription.aqbudgetid=aqbudget.aqbudgetid
158 left join aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id
159 left join biblio on biblio.biblionumber=subscription.biblionumber
160 where subscription.subscriptionid = ?');
161 $sth->execute($subscriptionid);
162 my $subs = $sth->fetchrow_hashref;
166 sub getsubscriptionfrombiblionumber {
167 my ($biblionumber) = @_;
168 my $dbh = C4::Context->dbh;
169 my $sth = $dbh->prepare('select subscriptionid from subscription where biblionumber=?');
170 $sth->execute($biblionumber);
171 my $subscriptionid = $sth->fetchrow;
172 return $subscriptionid;
175 sub modsubscription {
176 my ($auser,$aqbooksellerid,$cost,$aqbudgetid,$startdate,
177 $periodicity,$dow,$numberlength,$weeklength,$monthlength,
178 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
179 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
180 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
181 $numberingmethod, $status, $biblionumber, $notes, $subscriptionid)= @_;
182 my $dbh = C4::Context->dbh;
183 my $sth=$dbh->prepare("update subscription set librarian=?, aqbooksellerid=?,cost=?,aqbudgetid=?,startdate=?,
184 periodicity=?,dow=?,numberlength=?,weeklength=?,monthlength=?,
185 add1=?,every1=?,whenmorethan1=?,setto1=?,lastvalue1=?,innerloop1=?,
186 add2=?,every2=?,whenmorethan2=?,setto2=?,lastvalue2=?,innerloop2=?,
187 add3=?,every3=?,whenmorethan3=?,setto3=?,lastvalue3=?,innerloop3=?,
188 numberingmethod=?, status=?, biblionumber=?, notes=? where subscriptionid = ?");
189 $sth->execute($auser,$aqbooksellerid,$cost,$aqbudgetid,$startdate,
190 $periodicity,$dow,$numberlength,$weeklength,$monthlength,
191 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
192 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
193 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
194 $numberingmethod, $status, $biblionumber, $notes, $subscriptionid);
199 sub getsubscriptions {
200 my ($title,$ISSN) = @_;
201 return unless $title or $ISSN;
202 my $dbh = C4::Context->dbh;
204 $sth = $dbh->prepare("select subscription.subscriptionid,biblio.title,biblioitems.issn from subscription,biblio,biblioitems where biblio.biblionumber = biblioitems.biblionumber and biblio.biblionumber=subscription.biblionumber and (biblio.title like ? or biblioitems.issn = ? )");
205 $sth->execute("%$title%",$ISSN);
207 while (my $line = $sth->fetchrow_hashref) {
208 push @results, $line;
213 sub modsubscriptionhistory {
214 my ($subscriptionid,$histstartdate,$enddate,$recievedlist,$missinglist,$opacnote,$librariannote)=@_;
215 my $dbh=C4::Context->dbh;
216 my $sth = $dbh->prepare("update subscriptionhistory set histstartdate=?,enddate=?,recievedlist=?,missinglist=?,opacnote=?,librariannote=? where subscriptionid=?");
217 $sth->execute($histstartdate,$enddate,$recievedlist,$missinglist,$opacnote,$librariannote,$subscriptionid);
219 # get every serial not arrived for a given subscription.
221 my ($subscriptionid) = @_;
222 my $dbh = C4::Context->dbh;
223 # status = 2 is "arrived"
224 my $sth=$dbh->prepare("select serialid,serialseq, status, planneddate from serial where subscriptionid = ? and status <>2 and status <>4");
225 $sth->execute($subscriptionid);
227 while(my $line = $sth->fetchrow_hashref) {
228 $line->{"status".$line->{status}} = 1; # fills a "statusX" value, used for template status select list
229 $line->{"planneddate"} = format_date($line->{"planneddate"});
235 sub serialchangestatus {
236 my ($serialid,$serialseq,$planneddate,$status)=@_;
237 # warn "($serialid,$serialseq,$planneddate,$status)";
238 # 1st, get previous status : if we change from "waited" to something else, then we will have to create a new "waited" entry
239 my $dbh = C4::Context->dbh;
240 my $sth = $dbh->prepare("select subscriptionid,status from serial where serialid=?");
241 $sth->execute($serialid);
242 my ($subscriptionid,$oldstatus) = $sth->fetchrow;
243 # change status & update subscriptionhistory
244 $sth = $dbh->prepare("update serial set serialseq=?,planneddate=?,status=? where serialid = ?");
245 $sth->execute($serialseq,$planneddate,$status,$serialid);
246 $sth = $dbh->prepare("select missinglist,recievedlist from subscriptionhistory where subscriptionid=?");
247 $sth->execute($subscriptionid);
248 my ($missinglist,$recievedlist) = $sth->fetchrow;
250 $recievedlist .= ",$serialseq";
253 $missinglist .= ",$serialseq";
255 $sth=$dbh->prepare("update subscriptionhistory set recievedlist=?, missinglist=? where subscriptionid=?");
256 $sth->execute($recievedlist,$missinglist,$subscriptionid);
257 # create new waited entry if needed (ie : was a "waited" and has changed)
258 if ($oldstatus eq 1 && $status ne 1) {
259 $sth = $dbh->prepare("select * from subscription where subscriptionid = ? ");
260 $sth->execute($subscriptionid);
261 my $val = $sth->fetchrow_hashref;
263 my ($newserialseq,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3) = Get_Next_Seq($val);
264 # next date (calculated from actual date & frequency parameters)
265 my $nextplanneddate = Get_Next_Date($planneddate,$val);
266 $sth = $dbh->prepare("insert into serial (serialseq,subscriptionid,biblionumber,status, planneddate) values (?,?,?,?,?)");
267 $sth->execute($newserialseq, $subscriptionid, $val->{'biblionumber'}, 1, $nextplanneddate);
268 $sth = $dbh->prepare("update subscription set lastvalue1=?, lastvalue2=?,lastvalue3=?,
269 innerloop1=?,innerloop2=?,innerloop3=?
270 where subscriptionid = ?");
271 $sth->execute($newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3,$subscriptionid);
275 sub Get_Next_Date(@) {
276 my ($planneddate,$subscription) = @_;
278 if ($subscription->{periodicity} == 1) {
279 $resultdate=DateCalc($planneddate,"1 day");
281 if ($subscription->{periodicity} == 2) {
282 $resultdate=DateCalc($planneddate,"1 week");
284 if ($subscription->{periodicity} == 3) {
285 $resultdate=DateCalc($planneddate,"2 weeks");
287 if ($subscription->{periodicity} == 4) {
288 $resultdate=DateCalc($planneddate,"3 weeks");
290 if ($subscription->{periodicity} == 5) {
291 $resultdate=DateCalc($planneddate,"1 month");
293 if ($subscription->{periodicity} == 6) {
294 $resultdate=DateCalc($planneddate,"2 months");
296 if ($subscription->{periodicity} == 7) {
297 $resultdate=DateCalc($planneddate,"3 months");
299 if ($subscription->{periodicity} == 8) {
300 $resultdate=DateCalc($planneddate,"3 months");
302 if ($subscription->{periodicity} == 9) {
303 $resultdate=DateCalc($planneddate,"2 weeks");
305 if ($subscription->{periodicity} == 10) {
306 $resultdate=DateCalc($planneddate,"1 year");
308 if ($subscription->{periodicity} == 11) {
309 $resultdate=DateCalc($planneddate,"2 years");
311 return format_date_in_iso($resultdate);
316 my $calculated = $val->{numberingmethod};
317 my $x=$val->{'lastvalue1'};
318 $calculated =~ s/\{X\}/$x/g;
319 my $y=$val->{'lastvalue2'};
320 $calculated =~ s/\{Y\}/$y/g;
321 my $z=$val->{'lastvalue3'};
322 $calculated =~ s/\{Z\}/$z/g;
328 my ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
329 $calculated = $val->{numberingmethod};
330 # calculate the (expected) value of the next issue recieved.
331 $newlastvalue1 = $val->{lastvalue1};
332 # check if we have to increase the new value.
333 $newinnerloop1 = $val->{innerloop1}+1;
334 $newinnerloop1=0 if ($newinnerloop1 >= $val->{every1});
335 $newlastvalue1 += $val->{add1} if ($newinnerloop1<1); # <1 to be true when 0 or empty.
336 $newlastvalue1=$val->{setto1} if ($newlastvalue1>$val->{whenmorethan1}); # reset counter if needed.
337 $calculated =~ s/\{X\}/$newlastvalue1/g;
339 $newlastvalue2 = $val->{lastvalue2};
340 # check if we have to increase the new value.
341 $newinnerloop2 = $val->{innerloop2}+1;
342 $newinnerloop2=0 if ($newinnerloop2 >= $val->{every2});
343 $newlastvalue2 += $val->{add2} if ($newinnerloop2<1); # <1 to be true when 0 or empty.
344 $newlastvalue2=$val->{setto2} if ($newlastvalue2>$val->{whenmorethan2}); # reset counter if needed.
345 $calculated =~ s/\{Y\}/$newlastvalue2/g;
347 $newlastvalue3 = $val->{lastvalue3};
348 # check if we have to increase the new value.
349 $newinnerloop3 = $val->{innerloop3}+1;
350 $newinnerloop3=0 if ($newinnerloop3 >= $val->{every3});
351 $newlastvalue3 += $val->{add3} if ($newinnerloop3<1); # <1 to be true when 0 or empty.
352 $newlastvalue3=$val->{setto3} if ($newlastvalue3>$val->{whenmorethan3}); # reset counter if needed.
353 $calculated =~ s/\{Z\}/$newlastvalue3/g;
354 return ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
357 # the subscription has expired when the next issue to arrive is out of subscription limit.
358 sub hassubscriptionexpired {
359 my ($subscriptionid) = @_;
360 my $dbh = C4::Context->dbh;
361 my $subscription = getsubscription($subscriptionid);
362 # we don't do the same test if the subscription is based on X numbers or on X weeks/months
363 if ($subscription->{numberlength}) {
364 my $sth = $dbh->prepare("select count(*) from serial where subscriptionid=? and planneddate>=?");
365 $sth->execute($subscriptionid,$subscription->{startdate});
366 my $res = $sth->fetchrow;
367 if ($subscription->{numberlength}>=$res) {
373 #a little bit more tricky if based on X weeks/months : search if the latest issue waited is not after subscription startdate + duration
374 my $sth = $dbh->prepare("select max(planneddate) from serial where subscriptionid=?");
375 $sth->execute($subscriptionid);
376 my $res = ParseDate(format_date_in_iso($sth->fetchrow));
377 my $endofsubscriptiondate;
378 $endofsubscriptiondate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{monthlength}." months") if ($subscription->{monthlength});
379 $endofsubscriptiondate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{weeklength}." weeks") if ($subscription->{weeklength});
380 return 1 if ($res >= $endofsubscriptiondate);
385 sub subscriptionexpirationdate {
386 my ($subscriptionid) = @_;
387 my $dbh = C4::Context->dbh;
388 my $subscription = getsubscription($subscriptionid);
389 my $enddate=$subscription->{startdate};
390 # we don't do the same test if the subscription is based on X numbers or on X weeks/months
391 if ($subscription->{numberlength}) {
392 #calculate the date of the last issue.
393 for (my $i=1;$i<=$subscription->{numberlength};$i++) {
394 $enddate = Get_Next_Date($enddate,$subscription);
397 $enddate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{monthlength}." months") if ($subscription->{monthlength});
398 $enddate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{weeklength}." weeks") if ($subscription->{weeklength});
400 # $enddate=format_date_in_iso($enddate);
401 # warn "END : $enddate";
405 sub subscriptionrenew {
406 my ($subscriptionid,$user,$startdate,$numberlength,$weeklength,$monthlength,$note) = @_;
407 my $dbh = C4::Context->dbh;
408 my $subscription = getsubscription($subscriptionid);
409 my $sth = $dbh->prepare("select * from biblio,biblioitems where biblio.biblionumber=biblioitems.biblionumber and biblio.biblionumber=?");
410 $sth->execute($subscription->{biblionumber});
411 my $biblio = $sth->fetchrow_hashref;
412 newsuggestion($user,$subscription->{bibliotitle},$biblio->{author},$biblio->{publishercode},$biblio->{note},,,,,$subscription->{biblionumber});
414 $sth=$dbh->prepare("update subscription set startdate=?,numberlength=?,weeklength=?,monthlength=?");
415 $sth->execute(format_date_in_iso($startdate),$numberlength,$weeklength,$monthlength);
417 END { } # module clean-up code here (global destructor)