View | Details | Raw Unified | Return to bug 14702
Collapse All | Expand All

(-)a/C4/Reserves.pm (-98 / +31 lines)
Lines 103-109 BEGIN { Link Here
103
        &GetReservesForBranch
103
        &GetReservesForBranch
104
        &GetReservesToBranch
104
        &GetReservesToBranch
105
        &GetReserveCount
105
        &GetReserveCount
106
        &GetReserveFee
107
        &GetReserveInfo
106
        &GetReserveInfo
108
        &GetReserveStatus
107
        &GetReserveStatus
109
108
Lines 156-164 sub AddReserve { Link Here
156
        $bibitems,  $priority, $resdate, $expdate, $notes,
155
        $bibitems,  $priority, $resdate, $expdate, $notes,
157
        $title,      $checkitem, $found
156
        $title,      $checkitem, $found
158
    ) = @_;
157
    ) = @_;
159
    my $fee =
160
          GetReserveFee($borrowernumber, $biblionumber,
161
            $bibitems );
162
    my $dbh     = C4::Context->dbh;
158
    my $dbh     = C4::Context->dbh;
163
    $resdate = format_date_in_iso( $resdate ) if ( $resdate );
159
    $resdate = format_date_in_iso( $resdate ) if ( $resdate );
164
    $resdate = C4::Dates->today( 'iso' ) unless ( $resdate );
160
    $resdate = C4::Dates->today( 'iso' ) unless ( $resdate );
Lines 178-198 sub AddReserve { Link Here
178
        $waitingdate = $resdate;
174
        $waitingdate = $resdate;
179
    }
175
    }
180
176
181
    #eval {
182
    # updates take place here
177
    # updates take place here
183
    if ( $fee > 0 ) {
184
        my $nextacctno = &getnextacctno( $borrowernumber );
185
        my $query      = qq{
186
        INSERT INTO accountlines
187
            (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
188
        VALUES
189
            (?,?,now(),?,?,'Res',?)
190
    };
191
        my $usth = $dbh->prepare($query);
192
        $usth->execute( $borrowernumber, $nextacctno, $fee,
193
            "Reserve Charge - $title", $fee );
194
    }
195
196
    my $query = qq{
178
    my $query = qq{
197
        INSERT INTO reserves
179
        INSERT INTO reserves
198
            (borrowernumber,biblionumber,reservedate,branchcode,
180
            (borrowernumber,biblionumber,reservedate,branchcode,
Lines 207-212 sub AddReserve { Link Here
207
        $notes,          $checkitem,    $found,   $waitingdate, $expdate
189
        $notes,          $checkitem,    $found,   $waitingdate, $expdate
208
    );
190
    );
209
    my $reserve_id = $sth->{mysql_insertid};
191
    my $reserve_id = $sth->{mysql_insertid};
192
    # add a reserve fee if needed
193
    ChargeReserveFee( $borrowernumber, $biblionumber, $title );
210
194
211
    # Send e-mail to librarian if syspref is active
195
    # Send e-mail to librarian if syspref is active
212
    if(C4::Context->preference("emailLibrarianWhenHoldIsPlaced")){
196
    if(C4::Context->preference("emailLibrarianWhenHoldIsPlaced")){
Lines 650-742 sub GetOtherReserves { Link Here
650
    return ( $messages, $nextreservinfo );
634
    return ( $messages, $nextreservinfo );
651
}
635
}
652
636
653
=head2 GetReserveFee
637
=head2 ChargeReserveFee
654
638
655
  $fee = GetReserveFee($borrowernumber,$biblionumber,$biblionumber);
639
    $fee = ChargeReserveFee( $borrowernumber, $biblionumber, $title );
656
640
657
Calculate the fee for a reserve
641
    Charge the fee for a reserve (if applicable).
658
642
659
=cut
643
    NOTE: This sub replaces GetReserveFee. The code has been simplified on bug
644
    14702. The old checks were unneeded and erroneous. This routine is now
645
    called WHEN a hold is filled.
660
646
661
sub GetReserveFee {
647
=cut
662
    my ($borrowernumber, $biblionumber, $bibitems ) = @_;
663
648
664
    #check for issues;
649
sub ChargeReserveFee {
665
    my $dbh   = C4::Context->dbh;
650
    my ( $borrowernumber, $biblionumber, $title ) = @_;
666
    my $query = qq{
651
    my $borquery = qq{
667
      SELECT * FROM borrowers
652
SELECT reservefee FROM borrowers LEFT JOIN categories ON borrowers.categorycode = categories.categorycode WHERE borrowernumber = ?
668
    LEFT JOIN categories ON borrowers.categorycode = categories.categorycode
653
    };
669
    WHERE borrowernumber = ?
654
    my $accquery = qq{
655
INSERT INTO accountlines ( borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding ) VALUES (?, ?, NOW(), ?, ?, 'Res', ?)
670
    };
656
    };
671
    my $sth = $dbh->prepare($query);
672
    $sth->execute($borrowernumber);
673
    my $data = $sth->fetchrow_hashref;
674
    my $fee      = $data->{'reservefee'};
675
    my $cntitems = @- > $bibitems;
676
677
    if ( $fee > 0 ) {
678
679
        # check for items on issue
680
        # first find biblioitem records
681
        my @biblioitems;
682
        my $sth1 = $dbh->prepare(
683
            "SELECT * FROM biblio LEFT JOIN biblioitems on biblio.biblionumber = biblioitems.biblionumber
684
                   WHERE (biblio.biblionumber = ?)"
685
        );
686
        $sth1->execute($biblionumber);
687
        while ( my $data1 = $sth1->fetchrow_hashref ) {
688
            my $found = 0;
689
            my $x     = 0;
690
            while ( $x < $cntitems ) {
691
                if ( @$bibitems->{'biblioitemnumber'} ==
692
                    $data->{'biblioitemnumber'} )
693
                {
694
                    $found = 1;
695
                }
696
                $x++;
697
            }
698
657
699
            if ( $found == 0 ) {
658
    my $dbh = C4::Context->dbh;
700
                push @biblioitems, $data1;
659
    my ( $fee ) = $dbh->selectrow_array( $borquery, undef, ($borrowernumber) );
701
            }
660
    if( $fee && $fee > 0 ) {
702
        }
661
        # This is a reconstruction of the old code:
703
        my $cntitemsfound = @biblioitems;
662
        # Calculate number of items, items issued and holds
704
        my $issues        = 0;
663
        my ( $cnt1 ) = $dbh->selectrow_array( "SELECT COUNT(*) FROM items WHERE biblionumber=?", undef, ( $biblionumber ) );
705
        my $x             = 0;
664
        my ( $cnt2 ) = $dbh->selectrow_array( "SELECT COUNT(*) FROM issues LEFT JOIN items USING (itemnumber) WHERE biblionumber=?", undef, ( $biblionumber ));
706
        my $allissued     = 1;
665
        my ( $cnt3 ) = $dbh->selectrow_array( "SELECT COUNT(*) FROM reserves WHERE biblionumber=? AND borrowernumber<>?", undef, ( $biblionumber, $borrowernumber ) );
707
        while ( $x < $cntitemsfound ) {
666
        # If not all items are issued and there are no holds: charge no fee
708
            my $bitdata = $biblioitems[$x];
667
        # NOTE: Lost, damaged, not-for-loan, etc. are just ignored here
709
            my $sth2    = $dbh->prepare(
668
        return if $cnt1 - $cnt2 > 0 && $cnt3 == 0;
710
                "SELECT * FROM items
669
711
                     WHERE biblioitemnumber = ?"
670
        # Here we need to charge the fee
712
            );
671
        my $nextacctno = &getnextacctno( $borrowernumber );
713
            $sth2->execute( $bitdata->{'biblioitemnumber'} );
672
        $dbh->do( $accquery, undef, ( $borrowernumber, $nextacctno, $fee, "Reserve Charge - $title", $fee ) );
714
            while ( my $itdata = $sth2->fetchrow_hashref ) {
715
                my $sth3 = $dbh->prepare(
716
                    "SELECT * FROM issues
717
                       WHERE itemnumber = ?"
718
                );
719
                $sth3->execute( $itdata->{'itemnumber'} );
720
                if ( my $isdata = $sth3->fetchrow_hashref ) {
721
                }
722
                else {
723
                    $allissued = 0;
724
                }
725
            }
726
            $x++;
727
        }
728
        if ( $allissued == 0 ) {
729
            my $rsth =
730
              $dbh->prepare("SELECT * FROM reserves WHERE biblionumber = ?");
731
            $rsth->execute($biblionumber);
732
            if ( my $rdata = $rsth->fetchrow_hashref ) {
733
            }
734
            else {
735
                $fee = 0;
736
            }
737
        }
738
    }
673
    }
739
    return $fee;
740
}
674
}
741
675
742
=head2 GetReservesToBranch
676
=head2 GetReservesToBranch
743
- 

Return to bug 14702