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

(-)a/C4/Circulation.pm (-25 / +63 lines)
Lines 2299-2337 sub GetIssuingCharges { Link Here
2299
    my $item_type;
2299
    my $item_type;
2300
2300
2301
    # Get the book's item type and rental charge (via its biblioitem).
2301
    # Get the book's item type and rental charge (via its biblioitem).
2302
    my $qcharge =     "SELECT itemtypes.itemtype,rentalcharge FROM items
2302
    my $charge_query = 'SELECT itemtypes.itemtype,rentalcharge FROM items
2303
            LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber";
2303
        LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber';
2304
	$qcharge .= (C4::Context->preference('item-level_itypes'))
2304
    $charge_query .= (C4::Context->preference('item-level_itypes'))
2305
                ? " LEFT JOIN itemtypes ON items.itype = itemtypes.itemtype "
2305
        ? ' LEFT JOIN itemtypes ON items.itype = itemtypes.itemtype'
2306
                : " LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype ";
2306
        : ' LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype';
2307
	
2307
2308
    $qcharge .=      "WHERE items.itemnumber =?";
2308
    $charge_query .= ' WHERE items.itemnumber =?';
2309
   
2309
2310
    my $sth1 = $dbh->prepare($qcharge);
2310
    my $sth = $dbh->prepare($charge_query);
2311
    $sth1->execute($itemnumber);
2311
    $sth->execute($itemnumber);
2312
    if ( my $data1 = $sth1->fetchrow_hashref ) {
2312
    if ( my $item_data = $sth->fetchrow_hashref ) {
2313
        $item_type = $data1->{'itemtype'};
2313
        $item_type = $item_data->{itemtype};
2314
        $charge    = $data1->{'rentalcharge'};
2314
        $charge    = $item_data->{rentalcharge};
2315
        my $q2 = "SELECT rentaldiscount FROM borrowers
2315
        my $branch = C4::Branch::mybranch();
2316
        my $discount_query = q|SELECT rentaldiscount,
2317
            issuingrules.itemtype, issuingrules.branchcode
2318
            FROM borrowers
2316
            LEFT JOIN issuingrules ON borrowers.categorycode = issuingrules.categorycode
2319
            LEFT JOIN issuingrules ON borrowers.categorycode = issuingrules.categorycode
2317
            WHERE borrowers.borrowernumber = ?
2320
            WHERE borrowers.borrowernumber = ?
2318
            AND (issuingrules.itemtype = ? OR issuingrules.itemtype = '*')";
2321
            AND (issuingrules.itemtype = ? OR issuingrules.itemtype = '*')
2319
        my $sth2 = $dbh->prepare($q2);
2322
            AND (issuingrules.branchcode = ? OR issuingrules.branchcode = '*')|;
2320
        $sth2->execute( $borrowernumber, $item_type );
2323
        my $discount_sth = $dbh->prepare($discount_query);
2321
        if ( my $data2 = $sth2->fetchrow_hashref ) {
2324
        $discount_sth->execute( $borrowernumber, $item_type, $branch );
2322
            my $discount = $data2->{'rentaldiscount'};
2325
        my $discount_rules = $discount_sth->fetchall_arrayref({});
2323
            if ( $discount eq 'NULL' ) {
2326
        if (@{$discount_rules}) {
2324
                $discount = 0;
2327
            # We may have multiple rules so get the most specific
2325
            }
2328
            my $discount = _get_discount_from_rule($discount_rules, $branch, $item_type);
2326
            $charge = ( $charge * ( 100 - $discount ) ) / 100;
2329
            $charge = ( $charge * ( 100 - $discount ) ) / 100;
2327
        }
2330
        }
2328
        $sth2->finish;
2329
    }
2331
    }
2330
2332
2331
    $sth1->finish;
2333
    $sth->finish; # we havent _explicitly_ fetched all rows
2332
    return ( $charge, $item_type );
2334
    return ( $charge, $item_type );
2333
}
2335
}
2334
2336
2337
# Select most appropriate discount rule from those returned
2338
sub _get_discount_from_rule {
2339
    my ($rules_ref, $branch, $itemtype) = @_;
2340
    my $discount;
2341
2342
    if (@{$rules_ref} == 1) { # only 1 applicable rule use it
2343
        $discount = $rules_ref->[0]->{rentaldiscount};
2344
        return (defined $discount) ? $discount : 0;
2345
    }
2346
    # could have up to 4 does one match $branch and $itemtype
2347
    my @d = grep { $_->{branchcode} eq $branch && $_->{itemtype} eq $itemtype } @{$rules_ref};
2348
    if (@d) {
2349
        $discount = $d[0]->{rentaldiscount};
2350
        return (defined $discount) ? $discount : 0;
2351
    }
2352
    # do we have item type + all branches
2353
    @d = grep { $_->{branchcode} eq q{*} && $_->{itemtype} eq $itemtype } @{$rules_ref};
2354
    if (@d) {
2355
        $discount = $d[0]->{rentaldiscount};
2356
        return (defined $discount) ? $discount : 0;
2357
    }
2358
    # do we all item types + this branch
2359
    @d = grep { $_->{branchcode} eq $branch && $_->{itemtype} eq q{*} } @{$rules_ref};
2360
    if (@d) {
2361
        $discount = $d[0]->{rentaldiscount};
2362
        return (defined $discount) ? $discount : 0;
2363
    }
2364
    # so all and all (surely we wont get here)
2365
    @d = grep { $_->{branchcode} eq q{*} && $_->{itemtype} eq q{*} } @{$rules_ref};
2366
    if (@d) {
2367
        $discount = $d[0]->{rentaldiscount};
2368
        return (defined $discount) ? $discount : 0;
2369
    }
2370
    # none of the above
2371
    return 0;
2372
}
2373
2335
=head2 AddIssuingCharge
2374
=head2 AddIssuingCharge
2336
2375
2337
  &AddIssuingCharge( $itemno, $borrowernumber, $charge )
2376
  &AddIssuingCharge( $itemno, $borrowernumber, $charge )
2338
- 

Return to bug 5708