some changes in serial management :
[koha.git] / C4 / Bull.pm
1 package C4::Bull; #assumes C4/Bull.pm
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
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
11 # version.
12 #
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.
16 #
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
20
21 use strict;
22 use C4::Date;
23 use Date::Manip;
24 use C4::Suggestions;
25 require Exporter;
26
27 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
28
29 # set the version for version checking
30 $VERSION = 0.01;
31
32 =head1 NAME
33
34 C4::Bull - Give functions for serializing.
35
36 =head1 SYNOPSIS
37
38   use C4::Bull;
39
40 =head1 DESCRIPTION
41
42 Give all XYZ functions
43
44 =cut
45
46 @ISA = qw(Exporter);
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
53 sub newsubscription {
54         my ($auser,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
55                 $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
56                 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,
57                 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,
58                 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,
59                 $numberingmethod, $status, $notes) = @_;
60         my $dbh = C4::Context->dbh;
61         #save subscription
62         my $sth=$dbh->prepare("insert into subscription (librarian,aqbooksellerid,cost,aqbudgetid,biblionumber,
63                                                         startdate,periodicity,dow,numberlength,weeklength,monthlength,
64                                                         add1,every1,whenmorethan1,setto1,lastvalue1,
65                                                         add2,every2,whenmorethan2,setto2,lastvalue2,
66                                                         add3,every3,whenmorethan3,setto3,lastvalue3,
67                                                         numberingmethod, status, notes) values 
68                                                         (?,?,?,?,?,?,?,?,?,
69                                                          ?,?,?,?,?,?,?,?,?,?,
70                                                          ?,?,?,?,?,?,?,?,?,?)");
71         $sth->execute($auser,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
72                                         format_date_in_iso($startdate),$periodicity,$dow,$numberlength,$weeklength,$monthlength,
73                                         $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,
74                                         $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,
75                                         $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,
76                                         $numberingmethod, $status, $notes);
77         #then create the 1st waited number
78         my $subscriptionid = $dbh->{'mysql_insertid'};
79         $sth = $dbh->prepare("insert into subscriptionhistory (biblionumber, subscriptionid, histstartdate, enddate, missinglist, recievedlist, opacnote, librariannote) values (?,?,?,?,?,?,?,?)");
80         $sth->execute($biblionumber, $subscriptionid, format_date_in_iso($startdate), 0, "", "", 0, $notes);
81         # reread subscription to get a hash (for calculation of the 1st issue number)
82         $sth = $dbh->prepare("select * from subscription where subscriptionid = ? ");
83         $sth->execute($subscriptionid);
84         my $val = $sth->fetchrow_hashref;
85
86         # calculate issue number
87         my $serialseq = Get_Seq($val);
88         $sth = $dbh->prepare("insert into serial (serialseq,subscriptionid,biblionumber,status, planneddate) values (?,?,?,?,?)");
89         $sth->execute($serialseq, $subscriptionid, $val->{'biblionumber'}, 1, format_date_in_iso($startdate));
90
91         # next issue number
92         #my ($newserialseq,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3) = Get_Next_Seq($val);
93         # next date (calculated from actual date & frequency parameters)
94         #my $nextplanneddate = Get_Next_Date($startdate,$val);
95 #       $sth = $dbh->prepare("update subscription set lastvalue1=?, lastvalue2=?,lastvalue3=?,
96 #                                                                                                       innerloop1=?,innerloop2=?,innerloop3=?
97 #                                                                                                       where subscriptionid = ?");
98 #       $sth->execute($newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3,$subscriptionid);
99 #       $sth->finish;  
100         return $subscriptionid;
101 }
102 sub getsubscription {
103         my ($subscriptionid) = @_;
104         my $dbh = C4::Context->dbh;
105         my $sth = $dbh->prepare('select subscription.*,subscriptionhistory.*,aqbudget.bookfundid,aqbooksellers.name as aqbooksellername,biblio.title as bibliotitle 
106                                                         from subscription 
107                                                         left join subscriptionhistory on subscription.subscriptionid=subscriptionhistory.subscriptionid
108                                                         left join aqbudget on subscription.aqbudgetid=aqbudget.aqbudgetid 
109                                                         left join aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id 
110                                                         left join biblio on biblio.biblionumber=subscription.biblionumber 
111                                                         where subscription.subscriptionid = ?');
112         $sth->execute($subscriptionid);
113         my $subs = $sth->fetchrow_hashref;
114         return $subs;
115 }
116
117 sub getsubscriptionfrombiblionumber {
118         my ($biblionumber) = @_;
119         my $dbh = C4::Context->dbh;
120         my $sth = $dbh->prepare('select subscriptionid from subscription where biblionumber=?');
121         $sth->execute($biblionumber);
122         my $subscriptionid = $sth->fetchrow;
123         return $subscriptionid;
124 }
125
126 sub modsubscription {
127         my ($auser,$aqbooksellerid,$cost,$aqbudgetid,$startdate,
128                                         $periodicity,$dow,$numberlength,$weeklength,$monthlength,
129                                         $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
130                                         $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
131                                         $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
132                                         $numberingmethod, $status, $biblionumber, $notes, $subscriptionid)= @_;
133         my $dbh = C4::Context->dbh;
134         my $sth=$dbh->prepare("update subscription set librarian=?, aqbooksellerid=?,cost=?,aqbudgetid=?,startdate=?,
135                                                  periodicity=?,dow=?,numberlength=?,weeklength=?,monthlength=?,
136                                                 add1=?,every1=?,whenmorethan1=?,setto1=?,lastvalue1=?,innerloop1=?,
137                                                 add2=?,every2=?,whenmorethan2=?,setto2=?,lastvalue2=?,innerloop2=?,
138                                                 add3=?,every3=?,whenmorethan3=?,setto3=?,lastvalue3=?,innerloop3=?,
139                                                 numberingmethod=?, status=?, biblionumber=?, notes=? where subscriptionid = ?");
140         $sth->execute($auser,$aqbooksellerid,$cost,$aqbudgetid,$startdate,
141                                         $periodicity,$dow,$numberlength,$weeklength,$monthlength,
142                                         $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
143                                         $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
144                                         $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
145                                         $numberingmethod, $status, $biblionumber, $notes, $subscriptionid);
146         $sth->finish;
147
148 }
149
150 sub getsubscriptions {
151         my ($title,$ISSN) = @_;
152         my $dbh = C4::Context->dbh;
153         my $sth;
154         $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 = ? )");
155         $sth->execute("%$title%",$ISSN);
156         my @results;
157         while (my $line = $sth->fetchrow_hashref) {
158                 push @results, $line;
159         }
160         return @results;
161 }
162
163 sub modsubscriptionhistory {
164         my ($subscriptionid,$histstartdate,$enddate,$recievedlist,$missinglist,$opacnote,$librariannote)=@_;
165         my $dbh=C4::Context->dbh;
166         my $sth = $dbh->prepare("update subscriptionhistory set histstartdate=?,enddate=?,recievedlist=?,missinglist=?,opacnote=?,librariannote=? where subscriptionid=?");
167         $sth->execute($histstartdate,$enddate,$recievedlist,$missinglist,$opacnote,$librariannote,$subscriptionid);
168 }
169 # get every serial not arrived for a given subscription.
170 sub getserials {
171         my ($subscriptionid) = @_;
172         my $dbh = C4::Context->dbh;
173         # status = 2 is "arrived"
174         my $sth=$dbh->prepare("select serialid,serialseq, status, planneddate from serial where subscriptionid = ? and status <>2 and status <>4");
175         $sth->execute($subscriptionid);
176         my @serials;
177         while(my $line = $sth->fetchrow_hashref) {
178                 $line->{"status".$line->{status}} = 1; # fills a "statusX" value, used for template status select list
179                 $line->{"planneddate"} = format_date($line->{"planneddate"});
180                 push @serials,$line;
181         }
182         return @serials;
183 }
184
185 sub serialchangestatus {
186         my ($serialid,$serialseq,$planneddate,$status)=@_;
187 #       warn "($serialid,$serialseq,$planneddate,$status)";
188         # 1st, get previous status : if we change from "waited" to something else, then we will have to create a new "waited" entry
189         my $dbh = C4::Context->dbh;
190         my $sth = $dbh->prepare("select subscriptionid,status from serial where serialid=?");
191         $sth->execute($serialid);
192         my ($subscriptionid,$oldstatus) = $sth->fetchrow;
193         # change status & update subscriptionhistory
194         $sth = $dbh->prepare("update serial set serialseq=?,planneddate=?,status=? where serialid = ?");
195         $sth->execute($serialseq,$planneddate,$status,$serialid);
196         $sth = $dbh->prepare("select missinglist,recievedlist from subscriptionhistory where subscriptionid=?");
197         $sth->execute($subscriptionid);
198         my ($missinglist,$recievedlist) = $sth->fetchrow;
199         if ($status eq 2) {
200                 $recievedlist .= ",$serialseq";
201         }
202         if ($status eq 4) {
203                 $missinglist .= ",$serialseq";
204         }
205         $sth=$dbh->prepare("update subscriptionhistory set recievedlist=?, missinglist=? where subscriptionid=?");
206         $sth->execute($recievedlist,$missinglist,$subscriptionid);
207         # create new waited entry if needed (ie : was a "waited" and has changed)
208         if ($oldstatus eq 1 && $status ne 1) {
209                 $sth = $dbh->prepare("select * from subscription where subscriptionid = ? ");
210                 $sth->execute($subscriptionid);
211                 my $val = $sth->fetchrow_hashref;
212                 # next issue number
213                 my ($newserialseq,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3) = Get_Next_Seq($val);
214                 # next date (calculated from actual date & frequency parameters)
215                 my $nextplanneddate = Get_Next_Date($planneddate,$val);
216                 $sth = $dbh->prepare("insert into serial (serialseq,subscriptionid,biblionumber,status, planneddate) values (?,?,?,?,?)");
217                 $sth->execute($newserialseq, $subscriptionid, $val->{'biblionumber'}, 1, $nextplanneddate);
218                 $sth = $dbh->prepare("update subscription set lastvalue1=?, lastvalue2=?,lastvalue3=?,
219                                                                                                                 innerloop1=?,innerloop2=?,innerloop3=?
220                                                                                                                 where subscriptionid = ?");
221                 $sth->execute($newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3,$subscriptionid);
222         }
223 }
224
225 sub Get_Next_Date(@) {
226         my ($planneddate,$subscription) = @_;
227         my $resultdate;
228         if ($subscription->{periodicity} == 1) {
229                 $resultdate=DateCalc($planneddate,"1 day");
230         }
231         if ($subscription->{periodicity} == 2) {
232                 $resultdate=DateCalc($planneddate,"1 week");
233         }
234         if ($subscription->{periodicity} == 3) {
235                 $resultdate=DateCalc($planneddate,"2 weeks");
236         }
237         if ($subscription->{periodicity} == 4) {
238                 $resultdate=DateCalc($planneddate,"3 weeks");
239         }
240         if ($subscription->{periodicity} == 5) {
241                 $resultdate=DateCalc($planneddate,"1 month");
242         }
243         if ($subscription->{periodicity} == 6) {
244                 $resultdate=DateCalc($planneddate,"2 months");
245         }
246         if ($subscription->{periodicity} == 7) {
247                 $resultdate=DateCalc($planneddate,"3 months");
248         }
249         if ($subscription->{periodicity} == 8) {
250                 $resultdate=DateCalc($planneddate,"1 quarter");
251         }
252         if ($subscription->{periodicity} == 9) {
253                 $resultdate=DateCalc($planneddate,"2 weeks");
254         }
255         if ($subscription->{periodicity} == 10) {
256                 $resultdate=DateCalc($planneddate,"1 year");
257         }
258         if ($subscription->{periodicity} == 11) {
259                 $resultdate=DateCalc($planneddate,"2 years");
260         }
261     return format_date_in_iso($resultdate);
262 }
263
264 sub Get_Seq {
265         my ($val) =@_;
266         my $calculated = $val->{numberingmethod};
267         my $x=$val->{'lastvalue1'};
268         $calculated =~ s/\{X\}/$x/g;
269         my $y=$val->{'lastvalue2'};
270         $calculated =~ s/\{Y\}/$y/g;
271         my $z=$val->{'lastvalue3'};
272         $calculated =~ s/\{Z\}/$z/g;
273         return $calculated;
274 }
275
276 sub Get_Next_Seq {
277         my ($val) =@_;
278         my ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
279         $calculated = $val->{numberingmethod};
280         # calculate the (expected) value of the next issue recieved.
281         $newlastvalue1 = $val->{lastvalue1};
282         # check if we have to increase the new value.
283         $newinnerloop1 = $val->{innerloop1}+1;
284         $newinnerloop1=0 if ($newinnerloop1 >= $val->{every1});
285         $newlastvalue1 += $val->{add1} if ($newinnerloop1<1); # <1 to be true when 0 or empty.
286         $newlastvalue1=$val->{setto1} if ($newlastvalue1>$val->{whenmorethan1}); # reset counter if needed.
287         $calculated =~ s/\{X\}/$newlastvalue1/g;
288         
289         $newlastvalue2 = $val->{lastvalue2};
290         # check if we have to increase the new value.
291         $newinnerloop2 = $val->{innerloop2}+1;
292         $newinnerloop2=0 if ($newinnerloop2 >= $val->{every2});
293         $newlastvalue2 += $val->{add2} if ($newinnerloop2<1); # <1 to be true when 0 or empty.
294         $newlastvalue2=$val->{setto2} if ($newlastvalue2>$val->{whenmorethan2}); # reset counter if needed.
295         $calculated =~ s/\{Y\}/$newlastvalue2/g;
296         
297         $newlastvalue3 = $val->{lastvalue3};
298         # check if we have to increase the new value.
299         $newinnerloop3 = $val->{innerloop3}+1;
300         $newinnerloop3=0 if ($newinnerloop3 >= $val->{every3});
301         $newlastvalue3 += $val->{add3} if ($newinnerloop3<1); # <1 to be true when 0 or empty.
302         $newlastvalue3=$val->{setto3} if ($newlastvalue3>$val->{whenmorethan3}); # reset counter if needed.
303         $calculated =~ s/\{Z\}/$newlastvalue3/g;
304         return ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
305 }
306
307 # the subscription has expired when the next issue to arrive is out of subscription limit.
308 sub hassubscriptionexpired {
309         my ($subscriptionid) = @_;
310         my $dbh = C4::Context->dbh;
311         my $subscription = getsubscription($subscriptionid);
312         # we don't do the same test if the subscription is based on X numbers or on X weeks/months
313         if ($subscription->{numberlength}) {
314                 my $sth = $dbh->prepare("select count(*) from serial where subscriptionid=?  and planneddate>=?");
315                 $sth->execute($subscriptionid,$subscription->{planneddate});
316                 my $res = $sth->fetchrow;
317                 if ($subscription->{numberlength}>=$res) {
318                         return 0;
319                 } else {
320                         return 1;
321                 }
322         } else {
323                 #a little bit more tricky if based on X weeks/months : search if the latest issue waited is not after subscription startdate + duration
324                 my $sth = $dbh->prepare("select max(planneddate) from serial where subscriptionid=?");
325                 $sth->execute($subscriptionid);
326                 my $res = ParseDate(format_date_in_iso($sth->fetchrow));
327                 my $endofsubscriptiondate;
328                 $endofsubscriptiondate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{monthlength}." months") if ($subscription->{monthlength});
329                 $endofsubscriptiondate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{weeklength}." weeks") if ($subscription->{weeklength});
330                 return 1 if ($res >= $endofsubscriptiondate);
331                 return 0;
332         }
333 }
334
335 sub subscriptionexpirationdate {
336         my ($subscriptionid) = @_;
337         my $dbh = C4::Context->dbh;
338         my $subscription = getsubscription($subscriptionid);
339         my $enddate=$subscription->{startdate};
340         # we don't do the same test if the subscription is based on X numbers or on X weeks/months
341         if ($subscription->{numberlength}) {
342                 #calculate the date of the last issue.
343                 for (my $i=1;$i<=$subscription->{numberlength};$i++) {
344                         $enddate = Get_Next_Date($enddate,$subscription);
345                 }
346         } else {
347                 $enddate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{monthlength}." months") if ($subscription->{monthlength});
348                 $enddate = DateCalc(format_date_in_iso($subscription->{startdate}),$subscription->{weeklength}." weeks") if ($subscription->{weeklength});
349         }
350 #       $enddate=format_date_in_iso($enddate);
351 #       warn "END : $enddate";
352         return $enddate;
353 }
354
355 sub subscriptionrenew {
356         my ($subscriptionid,$user,$startdate,$numberlength,$weeklength,$monthlength,$note) = @_;
357         my $dbh = C4::Context->dbh;
358         my $subscription = getsubscription($subscriptionid);
359         my $sth = $dbh->prepare("select * from biblio,biblioitems where biblio.biblionumber=biblioitems.biblionumber and biblio.biblionumber=?");
360         $sth->execute($subscription->{biblionumber});
361         my $biblio = $sth->fetchrow_hashref;
362         newsuggestion($user,$subscription->{bibliotitle},$biblio->{author},$biblio->{publishercode},$biblio->{note},,,,,$subscription->{biblionumber});
363         # renew subscription
364         $sth=$dbh->prepare("update subscription set startdate=?,numberlength=?,weeklength=?,monthlength=?");
365         $sth->execute(format_date_in_iso($startdate),$numberlength,$weeklength,$monthlength);
366 }
367 END { }       # module clean-up code here (global destructor)