4 # Copyright 2000-2002 Katipo Communications
5 # copyright 2010 BibLibre
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23 #use warnings; FIXME - Bug 2505
24 use Date::Calc qw/Today Date_to_Days/;
25 use Date::Manip qw/UnixDate/;
26 use List::MoreUtils qw( uniq );
27 use POSIX qw( floor ceil );
28 use Locale::Currency::Format 1.28;
34 use C4::Log; # logaction
37 use Koha::Account::Lines;
38 use Koha::Account::Offsets;
39 use Koha::IssuingRules;
42 use vars qw(@ISA @EXPORT);
48 # subs to rename (and maybe merge some...)
59 &GetOverdueMessageTransportTypes
60 &parse_overdues_letter
68 # check that an equivalent don't exist already before moving
70 # subs to move to Circulation.pm
75 # subs to move to Biblio.pm
83 C4::Circulation::Fines - Koha module dealing with fines
91 This module contains several functions for dealing with fines for
92 overdue items. It is primarily used by the 'misc/fines2.pl' script.
98 $overdues = Getoverdues( { minimumdays => 1, maximumdays => 30 } );
100 Returns the list of all overdue books, with their itemtype.
102 C<$overdues> is a reference-to-array. Each element is a
103 reference-to-hash whose keys are the fields of the issues table in the
111 my $dbh = C4::Context->dbh;
113 if ( C4::Context->preference('item-level_itypes') ) {
115 SELECT issues.*, items.itype as itemtype, items.homebranch, items.barcode, items.itemlost, items.replacementprice
117 LEFT JOIN items USING (itemnumber)
118 WHERE date_due < NOW()
122 SELECT issues.*, biblioitems.itemtype, items.itype, items.homebranch, items.barcode, items.itemlost, replacementprice
124 LEFT JOIN items USING (itemnumber)
125 LEFT JOIN biblioitems USING (biblioitemnumber)
126 WHERE date_due < NOW()
131 if ( exists $params->{'minimumdays'} and exists $params->{'maximumdays'} ) {
132 $statement .= ' AND TO_DAYS( NOW() )-TO_DAYS( date_due ) BETWEEN ? and ? ';
133 push @bind_parameters, $params->{'minimumdays'}, $params->{'maximumdays'};
134 } elsif ( exists $params->{'minimumdays'} ) {
135 $statement .= ' AND ( TO_DAYS( NOW() )-TO_DAYS( date_due ) ) > ? ';
136 push @bind_parameters, $params->{'minimumdays'};
137 } elsif ( exists $params->{'maximumdays'} ) {
138 $statement .= ' AND ( TO_DAYS( NOW() )-TO_DAYS( date_due ) ) < ? ';
139 push @bind_parameters, $params->{'maximumdays'};
141 $statement .= 'ORDER BY borrowernumber';
142 my $sth = $dbh->prepare( $statement );
143 $sth->execute( @bind_parameters );
144 return $sth->fetchall_arrayref({});
150 ($count, $overdueitems) = checkoverdues($borrowernumber);
152 Returns a count and a list of overdueitems for a given borrowernumber
157 my $borrowernumber = shift or return;
158 my $sth = C4::Context->dbh->prepare(
159 "SELECT biblio.*, items.*, issues.*,
162 biblioitems.itemtype,
165 biblioitems.publicationyear,
166 biblioitems.publishercode,
167 biblioitems.volumedate,
168 biblioitems.volumedesc,
169 biblioitems.collectiontitle,
170 biblioitems.collectionissn,
171 biblioitems.collectionvolume,
172 biblioitems.editionstatement,
173 biblioitems.editionresponsibility,
181 biblioitems.cn_source,
182 biblioitems.cn_class,
184 biblioitems.cn_suffix,
186 biblioitems.totalissues
188 LEFT JOIN items ON issues.itemnumber = items.itemnumber
189 LEFT JOIN biblio ON items.biblionumber = biblio.biblionumber
190 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
191 WHERE issues.borrowernumber = ?
192 AND issues.date_due < NOW()"
194 $sth->execute($borrowernumber);
195 my $results = $sth->fetchall_arrayref({});
196 return ( scalar(@$results), $results); # returning the count and the results is silly
201 ($amount, $chargename, $units_minus_grace, $chargeable_units) = &CalcFine($item,
202 $categorycode, $branch,
203 $start_dt, $end_dt );
205 Calculates the fine for a book.
207 The issuingrules table in the Koha database is a fine matrix, listing
208 the penalties for each type of patron for each type of item and each branch (e.g., the
209 standard fine for books might be $0.50, but $1.50 for DVDs, or staff
210 members might get a longer grace period between the first and second
211 reminders that a book is overdue).
214 C<$item> is an item object (hashref).
216 C<$categorycode> is the category code (string) of the patron who currently has
219 C<$branchcode> is the library (string) whose issuingrules govern this transaction.
221 C<$start_date> & C<$end_date> are DateTime objects
222 defining the date range over which to determine the fine.
224 Fines scripts should just supply the date range over which to calculate the fine.
226 C<&CalcFine> returns four values:
228 C<$amount> is the fine owed by the patron (see above).
230 C<$chargename> is the chargename field from the applicable record in
231 the categoryitem table, whatever that is.
233 C<$units_minus_grace> is the number of chargeable units minus the grace period
235 C<$chargeable_units> is the number of chargeable units (days between start and end dates, Calendar adjusted where needed,
236 minus any applicable grace period, or hours)
238 FIXME: previously attempted to return C<$message> as a text message, either "First Notice", "Second Notice",
239 or "Final Notice". But CalcFine never defined any value.
244 my ( $item, $bortype, $branchcode, $due_dt, $end_date ) = @_;
245 my $start_date = $due_dt->clone();
246 # get issuingrules (fines part will be used)
247 my $itemtype = $item->{itemtype} || $item->{itype};
248 my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule({ categorycode => $bortype, itemtype => $itemtype, branchcode => $branchcode });
250 return unless $issuing_rule; # If not rule exist, there is no fine
252 my $fine_unit = $issuing_rule->lengthunit || 'days';
254 my $chargeable_units = get_chargeable_units($fine_unit, $start_date, $end_date, $branchcode);
255 my $units_minus_grace = $chargeable_units - $issuing_rule->firstremind;
257 if ( $issuing_rule->chargeperiod && ( $units_minus_grace > 0 ) ) {
258 my $units = C4::Context->preference('FinesIncludeGracePeriod') ? $chargeable_units : $units_minus_grace;
259 my $charge_periods = $units / $issuing_rule->chargeperiod;
260 # If chargeperiod_charge_at = 1, we charge a fine at the start of each charge period
261 # if chargeperiod_charge_at = 0, we charge at the end of each charge period
262 $charge_periods = $issuing_rule->chargeperiod_charge_at == 1 ? ceil($charge_periods) : floor($charge_periods);
263 $amount = $charge_periods * $issuing_rule->fine;
264 } # else { # a zero (or null) chargeperiod or negative units_minus_grace value means no charge. }
266 $amount = $issuing_rule->overduefinescap if $issuing_rule->overduefinescap && $amount > $issuing_rule->overduefinescap;
267 $amount = $item->{replacementprice} if ( $issuing_rule->cap_fine_to_replacement_price && $item->{replacementprice} && $amount > $item->{replacementprice} );
268 $debug and warn sprintf("CalcFine returning (%s, %s, %s, %s)", $amount, $issuing_rule->chargename, $units_minus_grace, $chargeable_units);
269 return ($amount, $issuing_rule->chargename, $units_minus_grace, $chargeable_units);
270 # FIXME: chargename is NEVER populated anywhere.
274 =head2 get_chargeable_units
276 get_chargeable_units($unit, $start_date_ $end_date, $branchcode);
278 return integer value of units between C<$start_date> and C<$end_date>, factoring in holidays for C<$branchcode>.
280 C<$unit> is 'days' or 'hours' (default is 'days').
282 C<$start_date> and C<$end_date> are the two DateTimes to get the number of units between.
284 C<$branchcode> is the branch whose calendar to use for finding holidays.
288 sub get_chargeable_units {
289 my ($unit, $date_due, $date_returned, $branchcode) = @_;
291 # If the due date is later than the return date
292 return 0 unless ( $date_returned > $date_due );
294 my $charge_units = 0;
296 if ($unit eq 'hours') {
297 if(C4::Context->preference('finesCalendar') eq 'noFinesWhenClosed') {
298 my $calendar = Koha::Calendar->new( branchcode => $branchcode );
299 $charge_duration = $calendar->hours_between( $date_due, $date_returned );
301 $charge_duration = $date_returned->delta_ms( $date_due );
303 if($charge_duration->in_units('hours') == 0 && $charge_duration->in_units('seconds') > 0){
306 return $charge_duration->in_units('hours');
309 if(C4::Context->preference('finesCalendar') eq 'noFinesWhenClosed') {
310 my $calendar = Koha::Calendar->new( branchcode => $branchcode );
311 $charge_duration = $calendar->days_between( $date_due, $date_returned );
313 $charge_duration = $date_returned->delta_days( $date_due );
315 return $charge_duration->in_units('days');
320 =head2 GetSpecialHolidays
322 &GetSpecialHolidays($date_dues,$itemnumber);
324 return number of special days between date of the day and date due
326 C<$date_dues> is the envisaged date of book return.
328 C<$itemnumber> is the book's item number.
332 sub GetSpecialHolidays {
333 my ( $date_dues, $itemnumber ) = @_;
335 # calcul the today date
336 my $today = join "-", &Today();
338 # return the holdingbranch
339 my $iteminfo = GetIssuesIteminfo($itemnumber);
341 # use sql request to find all date between date_due and today
342 my $dbh = C4::Context->dbh;
344 qq|SELECT DATE_FORMAT(concat(year,'-',month,'-',day),'%Y-%m-%d') as date
345 FROM `special_holidays`
346 WHERE DATE_FORMAT(concat(year,'-',month,'-',day),'%Y-%m-%d') >= ?
347 AND DATE_FORMAT(concat(year,'-',month,'-',day),'%Y-%m-%d') <= ?
350 my @result = GetWdayFromItemnumber($itemnumber);
354 my $sth = $dbh->prepare($query);
355 $sth->execute( $date_dues, $today, $iteminfo->{'branchcode'} )
356 ; # FIXME: just use NOW() in SQL instead of passing in $today
358 while ( my $special_date = $sth->fetchrow_hashref ) {
359 push( @result_date, $special_date );
362 my $specialdaycount = scalar(@result_date);
364 for ( my $i = 0 ; $i < scalar(@result_date) ; $i++ ) {
365 $dateinsec = UnixDate( $result_date[$i]->{'date'}, "%o" );
366 ( undef, undef, undef, undef, undef, undef, $wday, undef, undef ) =
367 localtime($dateinsec);
368 for ( my $j = 0 ; $j < scalar(@result) ; $j++ ) {
369 if ( $wday == ( $result[$j]->{'weekday'} ) ) {
375 return $specialdaycount;
378 =head2 GetRepeatableHolidays
380 &GetRepeatableHolidays($date_dues, $itemnumber, $difference,);
382 return number of day closed between date of the day and date due
384 C<$date_dues> is the envisaged date of book return.
386 C<$itemnumber> is item number.
388 C<$difference> numbers of between day date of the day and date due
392 sub GetRepeatableHolidays {
393 my ( $date_dues, $itemnumber, $difference ) = @_;
394 my $dateinsec = UnixDate( $date_dues, "%o" );
395 my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
396 localtime($dateinsec);
397 my @result = GetWdayFromItemnumber($itemnumber);
401 for ( my $i = 0 ; $i < scalar(@result) ; $i++ ) {
404 for ( $j = 0 ; $j < $difference ; $j++ ) {
405 if ( $result[$i]->{'weekday'} == $k ) {
406 push( @dayclosedcount, $k );
409 ( $k = 0 ) if ( $k eq 7 );
412 return scalar(@dayclosedcount);
416 =head2 GetWayFromItemnumber
418 &Getwdayfromitemnumber($itemnumber);
420 return the different week day from repeatable_holidays table
422 C<$itemnumber> is item number.
426 sub GetWdayFromItemnumber {
427 my ($itemnumber) = @_;
428 my $iteminfo = GetIssuesIteminfo($itemnumber);
430 my $query = qq|SELECT weekday
431 FROM repeatable_holidays
434 my $sth = C4::Context->dbh->prepare($query);
436 $sth->execute( $iteminfo->{'branchcode'} );
437 while ( my $weekday = $sth->fetchrow_hashref ) {
438 push( @result, $weekday );
444 =head2 GetIssuesIteminfo
446 &GetIssuesIteminfo($itemnumber);
448 return all data from issues about item
450 C<$itemnumber> is item number.
454 sub GetIssuesIteminfo {
455 my ($itemnumber) = @_;
456 my $dbh = C4::Context->dbh;
457 my $query = qq|SELECT *
461 my $sth = $dbh->prepare($query);
462 $sth->execute($itemnumber);
463 my ($issuesinfo) = $sth->fetchrow_hashref;
470 &UpdateFine({ issue_id => $issue_id, itemnumber => $itemnumber, borrwernumber => $borrowernumber, amount => $amount, type => $type, $due => $date_due });
472 (Note: the following is mostly conjecture and guesswork.)
474 Updates the fine owed on an overdue book.
476 C<$itemnumber> is the book's item number.
478 C<$borrowernumber> is the borrower number of the patron who currently
479 has the book on loan.
481 C<$amount> is the current amount owed by the patron.
483 C<$type> will be used in the description of the fine.
485 C<$due> is the due date formatted to the currently specified date format
487 C<&UpdateFine> looks up the amount currently owed on the given item
488 and sets it to C<$amount>, creating, if necessary, a new entry in the
489 accountlines table of the Koha database.
494 # Question: Why should the caller have to
495 # specify both the item number and the borrower number? A book can't
496 # be on loan to two different people, so the item number should be
499 # Possible Answer: You might update a fine for a damaged item, *after* it is returned.
504 my $issue_id = $params->{issue_id};
505 my $itemnum = $params->{itemnumber};
506 my $borrowernumber = $params->{borrowernumber};
507 my $amount = $params->{amount};
508 my $type = $params->{type};
509 my $due = $params->{due};
511 $debug and warn "UpdateFine({ itemnumber => $itemnum, borrowernumber => $borrowernumber, type => $type, due => $due, issue_id => $issue_id})";
513 unless ( $issue_id ) {
514 carp("No issue_id passed in!");
518 my $dbh = C4::Context->dbh;
519 # FIXME - What exactly is this query supposed to do? It looks up an
520 # entry in accountlines that matches the given item and borrower
521 # numbers, where the description contains $due, and where the
522 # account type has one of several values, but what does this _mean_?
523 # Does it look up existing fines for this item?
524 # FIXME - What are these various account types? ("FU", "O", "F", "M")
526 # "A" is Account Management Fee
531 # "FU" is Fine UPDATE??
533 # "REF" is Cash Refund
534 my $sth = $dbh->prepare(
535 "SELECT * FROM accountlines
536 WHERE borrowernumber=? AND
537 (( accounttype IN ('O','F','M') AND amountoutstanding<>0 ) OR
538 accounttype = 'FU' )"
540 $sth->execute( $borrowernumber );
542 my $total_amount_other = 0.00;
543 my $due_qr = qr/$due/;
544 # Cycle through the fines and
545 # - find line that relates to the requested $itemnum
546 # - accumulate fines for other items
547 # so we can update $itemnum fine taking in account fine caps
548 while (my $rec = $sth->fetchrow_hashref) {
549 if ( $rec->{issue_id} == $issue_id && $rec->{accounttype} eq 'FU' ) {
551 warn "Not a unique accountlines record for issue_id $issue_id";
552 #FIXME Should we still count this one in total_amount ??
559 $total_amount_other += $rec->{'amountoutstanding'};
562 if (my $maxfine = C4::Context->preference('MaxFine')) {
563 if ($total_amount_other + $amount > $maxfine) {
564 my $new_amount = $maxfine - $total_amount_other;
565 return if $new_amount <= 0.00;
566 warn "Reducing fine for item $itemnum borrower $borrowernumber from $amount to $new_amount - MaxFine reached";
567 $amount = $new_amount;
572 # we're updating an existing fine. Only modify if amount changed
573 # Note that in the current implementation, you cannot pay against an accruing fine
574 # (i.e. , of accounttype 'FU'). Doing so will break accrual.
575 if ( $data->{'amount'} != $amount ) {
576 my $accountline = Koha::Account::Lines->find( $data->{accountlines_id} );
577 my $diff = $amount - $data->{'amount'};
579 #3341: diff could be positive or negative!
580 my $out = $data->{'amountoutstanding'} + $diff;
584 date => dt_from_string(),
586 amountoutstanding => $out,
587 lastincrement => $diff,
592 Koha::Account::Offset->new(
594 debit_id => $accountline->id,
595 type => 'Fine Update',
601 if ( $amount ) { # Don't add new fines with an amount of 0
602 my $sth4 = $dbh->prepare(
603 "SELECT title FROM biblio LEFT JOIN items ON biblio.biblionumber=items.biblionumber WHERE items.itemnumber=?"
605 $sth4->execute($itemnum);
606 my $title = $sth4->fetchrow;
608 my $nextaccntno = C4::Accounts::getnextacctno($borrowernumber);
610 my $desc = ( $type ? "$type " : '' ) . "$title $due"; # FIXEDME, avoid whitespace prefix on empty $type
612 my $accountline = Koha::Account::Line->new(
614 borrowernumber => $borrowernumber,
615 itemnumber => $itemnum,
616 date => dt_from_string(),
618 description => $desc,
620 amountoutstanding => $amount,
621 lastincrement => $amount,
622 accountno => $nextaccntno,
623 issue_id => $issue_id,
627 Koha::Account::Offset->new(
629 debit_id => $accountline->id,
641 "due=".$due." amount=".$amount." itemnumber=".$itemnum
642 ) if C4::Context->preference("FinesLog");
647 $borrower = &BorType($borrowernumber);
649 Looks up a patron by borrower number.
651 C<$borrower> is a reference-to-hash whose keys are all of the fields
652 from the borrowers and categories tables of the Koha database. Thus,
653 C<$borrower> contains all information about both the borrower and
654 category they belong to.
659 my ($borrowernumber) = @_;
660 my $dbh = C4::Context->dbh;
661 my $sth = $dbh->prepare(
662 "SELECT * from borrowers
663 LEFT JOIN categories ON borrowers.categorycode=categories.categorycode
664 WHERE borrowernumber=?"
666 $sth->execute($borrowernumber);
667 return $sth->fetchrow_hashref;
672 $data->{'sum(amountoutstanding)'} = &GetFine($itemnum,$borrowernumber);
674 return the total of fine
676 C<$itemnum> is item number
678 C<$borrowernumber> is the borrowernumber
683 my ( $itemnum, $borrowernumber ) = @_;
684 my $dbh = C4::Context->dbh();
685 my $query = q|SELECT sum(amountoutstanding) as fineamount FROM accountlines
686 where accounttype like 'F%'
687 AND amountoutstanding > 0 AND borrowernumber=?|;
689 push @query_param, $borrowernumber;
690 if (defined $itemnum )
692 $query .= " AND itemnumber=?";
693 push @query_param, $itemnum;
695 my $sth = $dbh->prepare($query);
696 $sth->execute( @query_param );
697 my $fine = $sth->fetchrow_hashref();
698 if ($fine->{fineamount}) {
699 return $fine->{fineamount};
704 =head2 NumberNotifyId
706 (@notify) = &NumberNotifyId($borrowernumber);
708 Returns amount for all file per borrowers
709 C<@notify> array contains all file per borrowers
711 C<$notify_id> contains the file number for the borrower number nad item number
716 my ($borrowernumber)=@_;
717 my $dbh = C4::Context->dbh;
718 my $query=qq| SELECT distinct(notify_id)
720 WHERE borrowernumber=?|;
722 my $sth = $dbh->prepare($query);
723 $sth->execute($borrowernumber);
724 while ( my ($numberofnotify) = $sth->fetchrow ) {
725 push( @notify, $numberofnotify );
732 ($totalnotify) = &AmountNotify($notifyid);
734 Returns amount for all file per borrowers
735 C<$notifyid> is the file number
737 C<$totalnotify> contains amount of a file
739 C<$notify_id> contains the file number for the borrower number and item number
744 my ($notifyid,$borrowernumber)=@_;
745 my $dbh = C4::Context->dbh;
746 my $query=qq| SELECT sum(amountoutstanding)
748 WHERE notify_id=? AND borrowernumber = ?|;
749 my $sth=$dbh->prepare($query);
750 $sth->execute($notifyid,$borrowernumber);
751 my $totalnotify=$sth->fetchrow;
753 return ($totalnotify);
758 ($items) = &GetItems($itemnumber);
760 Returns the list of all delays from overduerules.
762 C<$items> is a reference-to-hash whose keys are all of the fields
763 from the items tables of the Koha database. Thus,
765 C<$itemnumber> contains the borrower categorycode
769 # FIXME: This is a bad function to have here.
770 # Shouldn't it be in C4::Items?
771 # Shouldn't it be called GetItem since you only get 1 row?
772 # Shouldn't it be called GetItem since you give it only 1 itemnumber?
775 my $itemnumber = shift or return;
776 my $query = qq|SELECT *
779 my $sth = C4::Context->dbh->prepare($query);
780 $sth->execute($itemnumber);
781 my ($items) = $sth->fetchrow_hashref;
785 =head2 GetBranchcodesWithOverdueRules
787 my @branchcodes = C4::Overdues::GetBranchcodesWithOverdueRules()
789 returns a list of branch codes for branches with overdue rules defined.
793 sub GetBranchcodesWithOverdueRules {
794 my $dbh = C4::Context->dbh;
795 my $branchcodes = $dbh->selectcol_arrayref(q|
796 SELECT DISTINCT(branchcode)
798 WHERE delay1 IS NOT NULL
801 if ( $branchcodes->[0] eq '' ) {
802 # If a default rule exists, all branches should be returned
803 return map { $_->branchcode } Koha::Libraries->search({}, { order_by => 'branchname' });
805 return @$branchcodes;
808 =head2 GetOverduesForBranch
810 Sql request for display all information for branchoverdues.pl
811 2 possibilities : with or without location .
812 display is filtered by branch
814 FIXME: This function should be renamed.
818 sub GetOverduesForBranch {
819 my ( $branch, $location) = @_;
820 my $itype_link = (C4::Context->preference('item-level_itypes')) ? " items.itype " : " biblioitems.itemtype ";
821 my $dbh = C4::Context->dbh;
824 borrowers.cardnumber,
825 borrowers.borrowernumber,
839 items.itemcallnumber,
842 itemtypes.description,
843 accountlines.notify_id,
844 accountlines.notify_level,
845 accountlines.amountoutstanding
847 LEFT JOIN issues ON issues.itemnumber = accountlines.itemnumber
848 AND issues.borrowernumber = accountlines.borrowernumber
849 LEFT JOIN borrowers ON borrowers.borrowernumber = accountlines.borrowernumber
850 LEFT JOIN items ON items.itemnumber = issues.itemnumber
851 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
852 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
853 LEFT JOIN itemtypes ON itemtypes.itemtype = $itype_link
854 LEFT JOIN branches ON branches.branchcode = issues.branchcode
855 WHERE (accountlines.amountoutstanding != '0.000000')
856 AND (accountlines.accounttype = 'FU' )
857 AND (issues.branchcode = ? )
858 AND (issues.date_due < NOW())
861 my $q = "$select AND items.location = ? ORDER BY borrowers.surname, borrowers.firstname";
862 return @{ $dbh->selectall_arrayref($q, { Slice => {} }, $branch, $location ) };
864 my $q = "$select ORDER BY borrowers.surname, borrowers.firstname";
865 return @{ $dbh->selectall_arrayref($q, { Slice => {} }, $branch ) };
869 =head2 GetOverdueMessageTransportTypes
871 my $message_transport_types = GetOverdueMessageTransportTypes( $branchcode, $categorycode, $letternumber);
873 return a arrayref with all message_transport_type for given branchcode, categorycode and letternumber(1,2 or 3)
877 sub GetOverdueMessageTransportTypes {
878 my ( $branchcode, $categorycode, $letternumber ) = @_;
879 return unless $categorycode and $letternumber;
880 my $dbh = C4::Context->dbh;
881 my $sth = $dbh->prepare("
882 SELECT message_transport_type
883 FROM overduerules odr LEFT JOIN overduerules_transport_types ott USING (overduerules_id)
888 $sth->execute( $branchcode, $categorycode, $letternumber );
890 while ( my $mtt = $sth->fetchrow ) {
894 # Put 'print' in first if exists
895 # It avoid to sent a print notice with an email or sms template is no email or sms is defined
896 @mtts = uniq( 'print', @mtts )
897 if grep {/^print$/} @mtts;
902 =head2 parse_overdues_letter
904 parses the letter template, replacing the placeholders with data
905 specific to this patron, biblio, or item for overdues
908 letter - required hashref
909 borrowernumber - required integer
910 substitute - optional hashref of other key/value pairs that should
911 be substituted in the letter content
913 returns the C<letter> hashref, with the content updated to reflect the
914 substituted keys and values.
918 sub parse_overdues_letter {
920 foreach my $required (qw( letter_code borrowernumber )) {
921 return unless ( exists $params->{$required} && $params->{$required} );
924 my $patron = Koha::Patrons->find( $params->{borrowernumber} );
926 my $substitute = $params->{'substitute'} || {};
928 my %tables = ( 'borrowers' => $params->{'borrowernumber'} );
929 if ( my $p = $params->{'branchcode'} ) {
930 $tables{'branches'} = $p;
933 my $active_currency = Koha::Acquisition::Currencies->get_active;
936 $currency_format = $active_currency->currency if defined($active_currency);
939 if ( my $i = $params->{'items'} ) {
940 foreach my $item (@$i) {
941 my $fine = GetFine($item->{'itemnumber'}, $params->{'borrowernumber'});
942 $item->{'fine'} = currency_format($currency_format, "$fine", FMT_SYMBOL);
943 # if active currency isn't correct ISO code fallback to sprintf
944 $item->{'fine'} = sprintf('%.2f', $fine) unless $item->{'fine'};
947 'biblio' => $item->{'biblionumber'},
948 'biblioitems' => $item->{'biblionumber'},
950 'issues' => $item->{'itemnumber'},
955 return C4::Letters::GetPreparedLetter (
956 module => 'circulation',
957 letter_code => $params->{'letter_code'},
958 branchcode => $params->{'branchcode'},
959 lang => $patron->lang,
962 overdues => [ map { $_->{items}->{itemnumber} } @item_tables ],
964 substitute => $substitute,
965 repeat => { item => \@item_tables },
966 message_transport_type => $params->{message_transport_type},
975 Koha Development Team <http://koha-community.org/>