+ # Record the fact that this book was issued.
+ &UpdateStats(
+ C4::Context->userenv->{'branch'},
+ 'issue', $charge,
+ ($sipmode ? "SIP-$sipmode" : ''), $item->{'itemnumber'},
+ $item->{'itype'}, $borrower->{'borrowernumber'}
+ );
+
+ # Send a checkout slip.
+ my $circulation_alert = 'C4::ItemCirculationAlertPreference';
+ my %conditions = (
+ branchcode => $branch,
+ categorycode => $borrower->{categorycode},
+ item_type => $item->{itype},
+ notification => 'CHECKOUT',
+ );
+ if ($circulation_alert->is_enabled_for(\%conditions)) {
+ SendCirculationAlert({
+ type => 'CHECKOUT',
+ item => $item,
+ borrower => $borrower,
+ branch => $branch,
+ });
+ }
+ }
+
+ logaction("CIRCULATION", "ISSUE", $borrower->{'borrowernumber'}, $biblio->{'biblionumber'})
+ if C4::Context->preference("IssueLog");
+ }
+ return ($datedue); # not necessarily the same as when it came in!
+}
+
+=head2 GetLoanLength
+
+Get loan length for an itemtype, a borrower type and a branch
+
+my $loanlength = &GetLoanLength($borrowertype,$itemtype,branchcode)
+
+=cut
+
+sub GetLoanLength {
+ my ( $borrowertype, $itemtype, $branchcode ) = @_;
+ my $dbh = C4::Context->dbh;
+ my $sth =
+ $dbh->prepare(
+"select issuelength from issuingrules where categorycode=? and itemtype=? and branchcode=? and issuelength is not null"
+ );
+# warn "in get loan lenght $borrowertype $itemtype $branchcode ";
+# try to find issuelength & return the 1st available.
+# check with borrowertype, itemtype and branchcode, then without one of those parameters
+ $sth->execute( $borrowertype, $itemtype, $branchcode );
+ my $loanlength = $sth->fetchrow_hashref;
+ return $loanlength->{issuelength}
+ if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
+
+ $sth->execute( $borrowertype, "*", $branchcode );
+ $loanlength = $sth->fetchrow_hashref;
+ return $loanlength->{issuelength}
+ if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
+
+ $sth->execute( "*", $itemtype, $branchcode );
+ $loanlength = $sth->fetchrow_hashref;
+ return $loanlength->{issuelength}
+ if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
+
+ $sth->execute( "*", "*", $branchcode );
+ $loanlength = $sth->fetchrow_hashref;
+ return $loanlength->{issuelength}
+ if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
+
+ $sth->execute( $borrowertype, $itemtype, "*" );
+ $loanlength = $sth->fetchrow_hashref;
+ return $loanlength->{issuelength}
+ if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
+
+ $sth->execute( $borrowertype, "*", "*" );
+ $loanlength = $sth->fetchrow_hashref;
+ return $loanlength->{issuelength}
+ if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
+
+ $sth->execute( "*", $itemtype, "*" );
+ $loanlength = $sth->fetchrow_hashref;
+ return $loanlength->{issuelength}
+ if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
+
+ $sth->execute( "*", "*", "*" );
+ $loanlength = $sth->fetchrow_hashref;
+ return $loanlength->{issuelength}
+ if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
+
+ # if no rule is set => 21 days (hardcoded)
+ return 21;
+}
+
+=head2 GetIssuingRule
+
+FIXME - This is a copy-paste of GetLoanLength
+as a stop-gap. Do not wish to change API for GetLoanLength
+this close to release, however, Overdues::GetIssuingRules is broken.
+
+Get the issuing rule for an itemtype, a borrower type and a branch
+Returns a hashref from the issuingrules table.
+
+my $irule = &GetIssuingRule($borrowertype,$itemtype,branchcode)
+
+=cut
+
+sub GetIssuingRule {
+ my ( $borrowertype, $itemtype, $branchcode ) = @_;
+ my $dbh = C4::Context->dbh;
+ my $sth = $dbh->prepare( "select * from issuingrules where categorycode=? and itemtype=? and branchcode=? and issuelength is not null" );
+ my $irule;
+
+ $sth->execute( $borrowertype, $itemtype, $branchcode );
+ $irule = $sth->fetchrow_hashref;
+ return $irule if defined($irule) ;
+
+ $sth->execute( $borrowertype, "*", $branchcode );
+ $irule = $sth->fetchrow_hashref;
+ return $irule if defined($irule) ;
+
+ $sth->execute( "*", $itemtype, $branchcode );
+ $irule = $sth->fetchrow_hashref;
+ return $irule if defined($irule) ;
+
+ $sth->execute( "*", "*", $branchcode );
+ $irule = $sth->fetchrow_hashref;
+ return $irule if defined($irule) ;
+
+ $sth->execute( $borrowertype, $itemtype, "*" );
+ $irule = $sth->fetchrow_hashref;
+ return $irule if defined($irule) ;
+
+ $sth->execute( $borrowertype, "*", "*" );
+ $irule = $sth->fetchrow_hashref;
+ return $irule if defined($irule) ;
+
+ $sth->execute( "*", $itemtype, "*" );
+ $irule = $sth->fetchrow_hashref;
+ return $irule if defined($irule) ;
+
+ $sth->execute( "*", "*", "*" );
+ $irule = $sth->fetchrow_hashref;
+ return $irule if defined($irule) ;
+
+ # if no rule matches,
+ return undef;
+}
+
+=head2 GetBranchBorrowerCircRule
+
+=over 4
+
+my $branch_cat_rule = GetBranchBorrowerCircRule($branchcode, $categorycode);
+
+=back
+
+Retrieves circulation rule attributes that apply to the given
+branch and patron category, regardless of item type.
+The return value is a hashref containing the following key:
+
+maxissueqty - maximum number of loans that a
+patron of the given category can have at the given
+branch. If the value is undef, no limit.
+
+This will first check for a specific branch and
+category match from branch_borrower_circ_rules.
+
+If no rule is found, it will then check default_branch_circ_rules
+(same branch, default category). If no rule is found,
+it will then check default_borrower_circ_rules (default
+branch, same category), then failing that, default_circ_rules
+(default branch, default category).
+
+If no rule has been found in the database, it will default to
+the buillt in rule:
+
+maxissueqty - undef
+
+C<$branchcode> and C<$categorycode> should contain the
+literal branch code and patron category code, respectively - no
+wildcards.
+
+=cut
+
+sub GetBranchBorrowerCircRule {
+ my $branchcode = shift;
+ my $categorycode = shift;
+
+ my $branch_cat_query = "SELECT maxissueqty
+ FROM branch_borrower_circ_rules
+ WHERE branchcode = ?
+ AND categorycode = ?";
+ my $dbh = C4::Context->dbh();
+ my $sth = $dbh->prepare($branch_cat_query);
+ $sth->execute($branchcode, $categorycode);
+ my $result;
+ if ($result = $sth->fetchrow_hashref()) {
+ return $result;
+ }
+
+ # try same branch, default borrower category
+ my $branch_query = "SELECT maxissueqty
+ FROM default_branch_circ_rules
+ WHERE branchcode = ?";
+ $sth = $dbh->prepare($branch_query);
+ $sth->execute($branchcode);
+ if ($result = $sth->fetchrow_hashref()) {
+ return $result;
+ }
+
+ # try default branch, same borrower category
+ my $category_query = "SELECT maxissueqty
+ FROM default_borrower_circ_rules
+ WHERE categorycode = ?";
+ $sth = $dbh->prepare($category_query);
+ $sth->execute($categorycode);
+ if ($result = $sth->fetchrow_hashref()) {
+ return $result;
+ }
+
+ # try default branch, default borrower category
+ my $default_query = "SELECT maxissueqty
+ FROM default_circ_rules";
+ $sth = $dbh->prepare($default_query);
+ $sth->execute();
+ if ($result = $sth->fetchrow_hashref()) {
+ return $result;