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

(-)a/C4/Accounts.pm (-59 / +102 lines)
Lines 26-32 use C4::Members; Link Here
26
use C4::Circulation qw(ReturnLostItem);
26
use C4::Circulation qw(ReturnLostItem);
27
use C4::Log qw(logaction);
27
use C4::Log qw(logaction);
28
use Koha::Account;
28
use Koha::Account;
29
use Koha::Account::Line;
29
use Koha::Account::Lines;
30
use Koha::Account::Lines;
31
use Koha::Account::Offset;
30
32
31
use Data::Dumper qw(Dumper);
33
use Data::Dumper qw(Dumper);
32
34
Lines 118-150 EOT Link Here
118
120
119
=cut
121
=cut
120
122
123
=head2 chargelostitem
124
125
In a default install of Koha the following lost values are set
126
1 = Lost
127
2 = Long overdue
128
3 = Lost and paid for
129
130
FIXME: itemlost should be set to 3 after payment is made, should be a warning to the interface that a charge has been added
131
FIXME : if no replacement price, borrower just doesn't get charged?
132
133
=cut
134
121
sub chargelostitem{
135
sub chargelostitem{
122
# lost ==1 Lost, lost==2 longoverdue, lost==3 lost and paid for
123
# FIXME: itemlost should be set to 3 after payment is made, should be a warning to the interface that
124
# a charge has been added
125
# FIXME : if no replacement price, borrower just doesn't get charged?
126
    my $dbh = C4::Context->dbh();
136
    my $dbh = C4::Context->dbh();
127
    my ($borrowernumber, $itemnumber, $amount, $description) = @_;
137
    my ($borrowernumber, $itemnumber, $amount, $description) = @_;
128
138
129
    # first make sure the borrower hasn't already been charged for this item
139
    # first make sure the borrower hasn't already been charged for this item
130
    my $sth1=$dbh->prepare("SELECT * from accountlines
140
    my $existing_charges = Koha::Account::Lines->search(
131
    WHERE borrowernumber=? AND itemnumber=? and accounttype='L'");
141
        {
132
    $sth1->execute($borrowernumber,$itemnumber);
142
            borrowernumber => $borrowernumber,
133
    my $existing_charge_hashref=$sth1->fetchrow_hashref();
143
            itemnumber     => $itemnumber,
144
            accounttype    => 'L',
145
        }
146
    )->count();
134
147
135
    # OK, they haven't
148
    # OK, they haven't
136
    unless ($existing_charge_hashref) {
149
    unless ($existing_charges) {
137
        my $manager_id = 0;
150
        my $manager_id = 0;
138
        $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
151
        $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
139
        # This item is on issue ... add replacement cost to the borrower's record and mark it returned
152
        # This item is on issue ... add replacement cost to the borrower's record and mark it returned
140
        #  Note that we add this to the account even if there's no replacement price, allowing some other
153
        #  Note that we add this to the account even if there's no replacement price, allowing some other
141
        #  process (or person) to update it, since we don't handle any defaults for replacement prices.
154
        #  process (or person) to update it, since we don't handle any defaults for replacement prices.
142
        my $accountno = getnextacctno($borrowernumber);
155
        my $accountno = getnextacctno($borrowernumber);
143
        my $sth2=$dbh->prepare("INSERT INTO accountlines
156
144
        (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding,itemnumber,manager_id)
157
        my $accountline = Koha::Account::Line->new(
145
        VALUES (?,?,now(),?,?,'L',?,?,?)");
158
            {
146
        $sth2->execute($borrowernumber,$accountno,$amount,
159
                borrowernumber    => $borrowernumber,
147
        $description,$amount,$itemnumber,$manager_id);
160
                accountno         => $accountno,
161
                date              => \'NOW()',
162
                amount            => $amount,
163
                description       => $description,
164
                accounttype       => 'L',
165
                amountoutstanding => $amount,
166
                itemnumber        => $itemnumber,
167
                manager_id        => $manager_id,
168
            }
169
        )->store();
170
171
        my $account_offset = Koha::Account::Offset->new(
172
            {
173
                debit_id => $accountline->id,
174
                type     => 'Lost Item',
175
                amount   => $amount,
176
            }
177
        )->store();
148
178
149
        if ( C4::Context->preference("FinesLog") ) {
179
        if ( C4::Context->preference("FinesLog") ) {
150
            logaction("FINES", 'CREATE', $borrowernumber, Dumper({
180
            logaction("FINES", 'CREATE', $borrowernumber, Dumper({
Lines 208-228 sub manualinvoice { Link Here
208
        $notifyid = 1;
238
        $notifyid = 1;
209
    }
239
    }
210
240
211
    if ( $itemnum ) {
241
    my $accountline = Koha::Account::Line->new(
212
        $desc .= ' ' . $itemnum;
242
        {
213
        my $sth = $dbh->prepare(
243
            borrowernumber    => $borrowernumber,
214
            'INSERT INTO  accountlines
244
            accountno         => $accountno,
215
                        (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding, itemnumber,notify_id, note, manager_id)
245
            date              => \'NOW()',
216
        VALUES (?, ?, now(), ?,?, ?,?,?,?,?,?)');
246
            amount            => $amount,
217
     $sth->execute($borrowernumber, $accountno, $amount, $desc, $type, $amountleft, $itemnum,$notifyid, $note, $manager_id) || return $sth->errstr;
247
            description       => $desc,
218
  } else {
248
            accounttype       => $type,
219
    my $sth=$dbh->prepare("INSERT INTO  accountlines
249
            amountoutstanding => $amountleft,
220
            (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding,notify_id, note, manager_id)
250
            itemnumber        => $itemnum || undef,
221
            VALUES (?, ?, now(), ?, ?, ?, ?,?,?,?)"
251
            notify_id         => $notifyid,
222
        );
252
            note              => $note,
223
        $sth->execute( $borrowernumber, $accountno, $amount, $desc, $type,
253
            manager_id        => $manager_id,
224
            $amountleft, $notifyid, $note, $manager_id );
254
        }
225
    }
255
    )->store();
256
257
    my $account_offset = Koha::Account::Offset->new(
258
        {
259
            debit_id => $accountline->id,
260
            type     => 'Manual Debit',
261
            amount   => $amount,
262
        }
263
    )->store();
226
264
227
    if ( C4::Context->preference("FinesLog") ) {
265
    if ( C4::Context->preference("FinesLog") ) {
228
        logaction("FINES", 'CREATE',$borrowernumber,Dumper({
266
        logaction("FINES", 'CREATE',$borrowernumber,Dumper({
Lines 308-352 sub getrefunds { Link Here
308
    return (@results);
346
    return (@results);
309
}
347
}
310
348
349
#FIXME: ReversePayment should be replaced with a Void Payment feature
311
sub ReversePayment {
350
sub ReversePayment {
312
    my ( $accountlines_id ) = @_;
351
    my ($accountlines_id) = @_;
313
    my $dbh = C4::Context->dbh;
352
    my $dbh = C4::Context->dbh;
314
353
315
    my $sth = $dbh->prepare('SELECT * FROM accountlines WHERE accountlines_id = ?');
354
    my $accountline        = Koha::Account::Lines->find($accountlines_id);
316
    $sth->execute( $accountlines_id );
355
    my $amount_outstanding = $accountline->amountoutstanding;
317
    my $row = $sth->fetchrow_hashref();
356
318
    my $amount_outstanding = $row->{'amountoutstanding'};
357
    my $new_amountoutstanding =
319
358
      $amount_outstanding <= 0 ? $accountline->amount * -1 : 0;
320
    if ( $amount_outstanding <= 0 ) {
359
321
        $sth = $dbh->prepare('UPDATE accountlines SET amountoutstanding = amount * -1, description = CONCAT( description, " Reversed -" ) WHERE accountlines_id = ?');
360
    $accountline->description( $accountline->description . " Reversed -" );
322
        $sth->execute( $accountlines_id );
361
    $accountline->amountoutstanding($new_amountoutstanding);
323
    } else {
362
    $accountline->store();
324
        $sth = $dbh->prepare('UPDATE accountlines SET amountoutstanding = 0, description = CONCAT( description, " Reversed -" ) WHERE accountlines_id = ?');
363
325
        $sth->execute( $accountlines_id );
364
    my $account_offset = Koha::Account::Offset->new(
326
    }
365
        {
366
            credit_id => $accountline->id,
367
            type      => 'Reverse Payment',
368
            amount    => $amount_outstanding - $new_amountoutstanding,
369
        }
370
    )->store();
327
371
328
    if ( C4::Context->preference("FinesLog") ) {
372
    if ( C4::Context->preference("FinesLog") ) {
329
        my $manager_id = 0;
373
        my $manager_id = 0;
330
        $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
374
        $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
331
375
332
        if ( $amount_outstanding <= 0 ) {
376
        logaction(
333
            $row->{'amountoutstanding'} *= -1;
377
            "FINES", 'MODIFY',
334
        } else {
378
            $accountline->borrowernumber,
335
            $row->{'amountoutstanding'} = '0';
379
            Dumper(
336
        }
380
                {
337
        $row->{'description'} .= ' Reversed -';
381
                    action                => 'reverse_fee_payment',
338
        logaction("FINES", 'MODIFY', $row->{'borrowernumber'}, Dumper({
382
                    borrowernumber        => $accountline->borrowernumber,
339
            action                => 'reverse_fee_payment',
383
                    old_amountoutstanding => $amount_outstanding,
340
            borrowernumber        => $row->{'borrowernumber'},
384
                    new_amountoutstanding => $new_amountoutstanding,
341
            old_amountoutstanding => $row->{'amountoutstanding'},
385
                    ,
342
            new_amountoutstanding => 0 - $amount_outstanding,,
386
                    accountlines_id => $accountline->id,
343
            accountlines_id       => $row->{'accountlines_id'},
387
                    accountno       => $accountline->accountno,
344
            accountno             => $row->{'accountno'},
388
                    manager_id      => $manager_id,
345
            manager_id            => $manager_id,
389
                }
346
        }));
390
            )
347
391
        );
348
    }
392
    }
349
350
}
393
}
351
394
352
=head2 purge_zero_balance_fees
395
=head2 purge_zero_balance_fees
(-)a/C4/Circulation.pm (-93 / +84 lines)
Lines 53-58 use Koha::Libraries; Link Here
53
use Koha::Holds;
53
use Koha::Holds;
54
use Koha::RefundLostItemFeeRule;
54
use Koha::RefundLostItemFeeRule;
55
use Koha::RefundLostItemFeeRules;
55
use Koha::RefundLostItemFeeRules;
56
use Koha::Account::Lines;
57
use Koha::Account::Line;
58
use Koha::Account::Offset;
56
use Carp;
59
use Carp;
57
use List::MoreUtils qw( uniq );
60
use List::MoreUtils qw( uniq );
58
use Scalar::Util qw( looks_like_number );
61
use Scalar::Util qw( looks_like_number );
Lines 2319-2357 sub _FixOverduesOnReturn { Link Here
2319
    my $dbh = C4::Context->dbh;
2322
    my $dbh = C4::Context->dbh;
2320
2323
2321
    # check for overdue fine
2324
    # check for overdue fine
2322
    my $sth = $dbh->prepare(
2325
    my $accountline = Koha::Account::Lines->search(
2323
"SELECT * FROM accountlines WHERE (borrowernumber = ?) AND (itemnumber = ?) AND (accounttype='FU' OR accounttype='O')"
2326
        {
2324
    );
2327
            borrowernumber => $borrowernumber,
2325
    $sth->execute( $borrowernumber, $item );
2328
            itemnumber     => $item,
2326
2329
            -or            => [
2327
    # alter fine to show that the book has been returned
2330
                accounttype => 'FU',
2328
    my $data = $sth->fetchrow_hashref;
2331
                accounttype => 'O',
2329
    return 0 unless $data;    # no warning, there's just nothing to fix
2332
            ],
2333
        }
2334
    )->next();
2335
    return 0 unless $accountline;    # no warning, there's just nothing to fix
2330
2336
2331
    my $uquery;
2337
    my $uquery;
2332
    my @bind = ($data->{'accountlines_id'});
2333
    if ($exemptfine) {
2338
    if ($exemptfine) {
2334
        $uquery = "update accountlines set accounttype='FFOR', amountoutstanding=0";
2339
        my $amountoutstanding = $accountline->amountoutstanding;
2340
2341
        $accountline->accounttype('FFOR');
2342
        $accountline->amountoutstanding(0);
2343
2344
        Koha::Account::Offset->new(
2345
            {
2346
                debit_id => $accountline->id,
2347
                type => 'Forgiven',
2348
                amount => $amountoutstanding * -1,
2349
            }
2350
        );
2351
2335
        if (C4::Context->preference("FinesLog")) {
2352
        if (C4::Context->preference("FinesLog")) {
2336
            &logaction("FINES", 'MODIFY',$borrowernumber,"Overdue forgiven: item $item");
2353
            &logaction("FINES", 'MODIFY',$borrowernumber,"Overdue forgiven: item $item");
2337
        }
2354
        }
2338
    } elsif ($dropbox && $data->{lastincrement}) {
2355
    } elsif ($dropbox && $accountline->lastincrement) {
2339
        my $outstanding = $data->{amountoutstanding} - $data->{lastincrement} ;
2356
        my $outstanding = $accountline->amountoutstanding - $accountline->lastincrement;
2340
        my $amt = $data->{amount} - $data->{lastincrement} ;
2357
        my $amt = $accountline->amount - $accountline->lastincrement;
2341
        if (C4::Context->preference("FinesLog")) {
2358
2342
            &logaction("FINES", 'MODIFY',$borrowernumber,"Dropbox adjustment $amt, item $item");
2359
        Koha::Account::Offset->new(
2360
            {
2361
                debit_id => $accountline->id,
2362
                type => 'Dropbox',
2363
                amount => $accountline->lastincrement * -1,
2364
            }
2365
        );
2366
2367
        if ( C4::Context->preference("FinesLog") ) {
2368
            &logaction( "FINES", 'MODIFY', $borrowernumber,
2369
                "Dropbox adjustment $amt, item $item" );
2343
        }
2370
        }
2344
         $uquery = "update accountlines set accounttype='F' ";
2371
2345
         if($outstanding  >= 0 && $amt >=0) {
2372
        $accountline->accounttype('F');
2346
            $uquery .= ", amount = ? , amountoutstanding=? ";
2373
2347
            unshift @bind, ($amt, $outstanding) ;
2374
        if ( $outstanding >= 0 && $amt >= 0 ) {
2375
            $accountline->amount($amt);
2376
            $accountline->amountoutstanding($outstanding);
2348
        }
2377
        }
2378
2349
    } else {
2379
    } else {
2350
        $uquery = "update accountlines set accounttype='F' ";
2380
        $accountline->accounttype('F');
2351
    }
2381
    }
2352
    $uquery .= " where (accountlines_id = ?)";
2382
2353
    my $usth = $dbh->prepare($uquery);
2383
    return $accountline->store();
2354
    return $usth->execute(@bind);
2355
}
2384
}
2356
2385
2357
=head2 _FixAccountForLostAndReturned
2386
=head2 _FixAccountForLostAndReturned
Lines 2362-2444 Calculates the charge for a book lost and returned. Link Here
2362
2391
2363
Internal function, not exported, called only by AddReturn.
2392
Internal function, not exported, called only by AddReturn.
2364
2393
2365
FIXME: This function reflects how inscrutable fines logic is.  Fix both.
2366
FIXME: Give a positive return value on success.  It might be the $borrowernumber who received credit, or the amount forgiven.
2367
2368
=cut
2394
=cut
2369
2395
2370
sub _FixAccountForLostAndReturned {
2396
sub _FixAccountForLostAndReturned {
2371
    my $itemnumber     = shift or return;
2397
    my $itemnumber     = shift or return;
2372
    my $borrowernumber = @_ ? shift : undef;
2398
    my $borrowernumber = @_ ? shift : undef;
2373
    my $item_id        = @_ ? shift : $itemnumber;  # Send the barcode if you want that logged in the description
2399
    my $item_id        = @_ ? shift : $itemnumber;  # Send the barcode if you want that logged in the description
2374
    my $dbh = C4::Context->dbh;
2400
2375
    # check for charge made for lost book
2401
    # check for charge made for lost book
2376
    my $sth = $dbh->prepare("SELECT * FROM accountlines WHERE itemnumber = ? AND accounttype IN ('L', 'Rep', 'W') ORDER BY date DESC, accountno DESC");
2402
    my $accountline = Koha::Account::Lines->search(
2377
    $sth->execute($itemnumber);
2403
        {
2378
    my $data = $sth->fetchrow_hashref;
2404
            itemnumber  => $itemnumber,
2379
    $data or return;    # bail if there is nothing to do
2405
            accounttype => { -in => [ 'L', 'Rep', 'W' ] },
2380
    $data->{accounttype} eq 'W' and return;    # Written off
2406
        },
2381
2407
        {
2382
    # writeoff this amount
2408
            order_by => { -desc => [ 'date', 'accountno' ] }
2383
    my $offset;
2409
        }
2384
    my $amount = $data->{'amount'};
2410
    )->next();
2385
    my $acctno = $data->{'accountno'};
2411
2386
    my $amountleft;                                             # Starts off undef/zero.
2412
    return unless $accountline;
2387
    if ($data->{'amountoutstanding'} == $amount) {
2413
    return if $accountline->accounttype eq 'W';    # Written off
2388
        $offset     = $data->{'amount'};
2414
2389
        $amountleft = 0;                                        # Hey, it's zero here, too.
2415
    $accountline->accounttype('LR');
2390
    } else {
2416
    $accountline->store();
2391
        $offset     = $amount - $data->{'amountoutstanding'};   # Um, isn't this the same as ZERO?  We just tested those two things are ==
2417
2392
        $amountleft = $data->{'amountoutstanding'} - $amount;   # Um, isn't this the same as ZERO?  We just tested those two things are ==
2418
    my $account = Koha::Account->new( { patron_id => $accountline->borrowernumber } );
2393
    }
2419
    my $credit_id = $account->pay(
2394
    my $usth = $dbh->prepare("UPDATE accountlines SET accounttype = 'LR',amountoutstanding='0'
2420
        {
2395
        WHERE (accountlines_id = ?)");
2421
            amount       => $accountline->amount,
2396
    $usth->execute($data->{'accountlines_id'});      # We might be adjusting an account for some OTHER borrowernumber now.  Not the one we passed in.
2422
            description  => "Item Returned " . $item_id,
2397
    #check if any credit is left if so writeoff other accounts
2423
            account_type => 'CR',
2398
    my $nextaccntno = getnextacctno($data->{'borrowernumber'});
2424
            offset_type  => 'Lost Item Return',
2399
    $amountleft *= -1 if ($amountleft < 0);
2425
            accounlines  => [$accountline],
2400
    if ($amountleft > 0) {
2426
2401
        my $msth = $dbh->prepare("SELECT * FROM accountlines WHERE (borrowernumber = ?)
2427
        }
2402
                            AND (amountoutstanding >0) ORDER BY date");     # might want to order by amountoustanding ASC (pay smallest first)
2428
    );
2403
        $msth->execute($data->{'borrowernumber'});
2429
2404
        # offset transactions
2430
    ModItem( { paidfor => '' }, undef, $itemnumber );
2405
        my $newamtos;
2431
2406
        my $accdata;
2432
    return $credit_id;
2407
        while (($accdata=$msth->fetchrow_hashref) and ($amountleft>0)){
2408
            if ($accdata->{'amountoutstanding'} < $amountleft) {
2409
                $newamtos = 0;
2410
                $amountleft -= $accdata->{'amountoutstanding'};
2411
            }  else {
2412
                $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
2413
                $amountleft = 0;
2414
            }
2415
            my $thisacct = $accdata->{'accountlines_id'};
2416
            # FIXME: move prepares outside while loop!
2417
            my $usth = $dbh->prepare("UPDATE accountlines SET amountoutstanding= ?
2418
                    WHERE (accountlines_id = ?)");
2419
            $usth->execute($newamtos,$thisacct);
2420
            $usth = $dbh->prepare("INSERT INTO accountoffsets
2421
                (borrowernumber, accountno, offsetaccount,  offsetamount)
2422
                VALUES
2423
                (?,?,?,?)");
2424
            $usth->execute($data->{'borrowernumber'},$accdata->{'accountno'},$nextaccntno,$newamtos);
2425
        }
2426
    }
2427
    $amountleft *= -1 if ($amountleft > 0);
2428
    my $desc = "Item Returned " . $item_id;
2429
    $usth = $dbh->prepare("INSERT INTO accountlines
2430
        (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
2431
        VALUES (?,?,now(),?,?,'CR',?)");
2432
    $usth->execute($data->{'borrowernumber'},$nextaccntno,0-$amount,$desc,$amountleft);
2433
    if ($borrowernumber) {
2434
        # FIXME: same as query above.  use 1 sth for both
2435
        $usth = $dbh->prepare("INSERT INTO accountoffsets
2436
            (borrowernumber, accountno, offsetaccount,  offsetamount)
2437
            VALUES (?,?,?,?)");
2438
        $usth->execute($borrowernumber, $data->{'accountno'}, $nextaccntno, $offset);
2439
    }
2440
    ModItem({ paidfor => '' }, undef, $itemnumber);
2441
    return;
2442
}
2433
}
2443
2434
2444
=head2 _GetCircControlBranch
2435
=head2 _GetCircControlBranch
(-)a/C4/Overdues.pm (+17 lines)
Lines 36-41 use C4::Debug; Link Here
36
use Koha::DateUtils;
36
use Koha::DateUtils;
37
use Koha::Account::Line;
37
use Koha::Account::Line;
38
use Koha::Account::Lines;
38
use Koha::Account::Lines;
39
use Koha::Account::Offset;
39
use Koha::IssuingRules;
40
use Koha::IssuingRules;
40
use Koha::Libraries;
41
use Koha::Libraries;
41
42
Lines 591-596 sub UpdateFine { Link Here
591
                    accounttype   => 'FU',
592
                    accounttype   => 'FU',
592
                }
593
                }
593
            )->store();
594
            )->store();
595
596
            Koha::Account::Offset->new(
597
                {
598
                    debit_id => $accountline->id,
599
                    type     => 'Fine Update',
600
                    amount   => $diff,
601
                }
602
            )->store();
594
        }
603
        }
595
    } else {
604
    } else {
596
        if ( $amount ) { # Don't add new fines with an amount of 0
605
        if ( $amount ) { # Don't add new fines with an amount of 0
Lines 618-623 sub UpdateFine { Link Here
618
                    issue_id          => $issue_id,
627
                    issue_id          => $issue_id,
619
                }
628
                }
620
            )->store();
629
            )->store();
630
631
            Koha::Account::Offset->new(
632
                {
633
                    debit_id => $accountline->id,
634
                    type     => 'Fine',
635
                    amount   => $amount,
636
                }
637
            )->store();
621
        }
638
        }
622
    }
639
    }
623
    # logging action
640
    # logging action
(-)a/Koha/Account.pm (-14 / +45 lines)
Lines 27-32 use C4::Stats qw( UpdateStats ); Link Here
27
27
28
use Koha::Account::Line;
28
use Koha::Account::Line;
29
use Koha::Account::Lines;
29
use Koha::Account::Lines;
30
use Koha::Account::Offset;
30
use Koha::DateUtils qw( dt_from_string );
31
use Koha::DateUtils qw( dt_from_string );
31
32
32
=head1 NAME
33
=head1 NAME
Lines 49-59 This method allows payments to be made against fees/fines Link Here
49
50
50
Koha::Account->new( { patron_id => $borrowernumber } )->pay(
51
Koha::Account->new( { patron_id => $borrowernumber } )->pay(
51
    {
52
    {
52
        amount     => $amount,
53
        amount      => $amount,
53
        sip        => $sipmode,
54
        sip         => $sipmode,
54
        note       => $note,
55
        note        => $note,
55
        library_id => $branchcode,
56
        description => $description,
56
        lines      => $lines, # Arrayref of Koha::Account::Line objects to pay
57
        library_id  => $branchcode,
58
        lines        => $lines, # Arrayref of Koha::Account::Line objects to pay
59
        account_type => $type,  # accounttype code
60
        offset_type => $offset_type,    # offset type code
57
    }
61
    }
58
);
62
);
59
63
Lines 62-73 Koha::Account->new( { patron_id => $borrowernumber } )->pay( Link Here
62
sub pay {
66
sub pay {
63
    my ( $self, $params ) = @_;
67
    my ( $self, $params ) = @_;
64
68
65
    my $amount          = $params->{amount};
69
    my $amount       = $params->{amount};
66
    my $sip             = $params->{sip};
70
    my $sip          = $params->{sip};
67
    my $note            = $params->{note} || q{};
71
    my $description  = $params->{description};
68
    my $library_id      = $params->{library_id};
72
    my $note         = $params->{note} || q{};
69
    my $lines           = $params->{lines};
73
    my $library_id   = $params->{library_id};
70
    my $type            = $params->{type} || 'payment';
74
    my $lines        = $params->{lines};
75
    my $type         = $params->{type} || 'payment';
76
    my $account_type = $params->{account_type};
77
    my $offset_type  = $params->{offset_type} || $type eq 'writeoff' ? 'Writeoff' : 'Payment';
71
78
72
    my $userenv = C4::Context->userenv;
79
    my $userenv = C4::Context->userenv;
73
80
Lines 89-94 sub pay { Link Here
89
    my $balance_remaining = $amount; # Set it now so we can adjust the amount if necessary
96
    my $balance_remaining = $amount; # Set it now so we can adjust the amount if necessary
90
    $balance_remaining ||= 0;
97
    $balance_remaining ||= 0;
91
98
99
    my @account_offsets;
100
92
    # We were passed a specific line to pay
101
    # We were passed a specific line to pay
93
    foreach my $fine ( @$lines ) {
102
    foreach my $fine ( @$lines ) {
94
        my $amount_to_pay =
103
        my $amount_to_pay =
Lines 106-111 sub pay { Link Here
106
            C4::Circulation::ReturnLostItem( $self->{patron_id}, $fine->itemnumber );
115
            C4::Circulation::ReturnLostItem( $self->{patron_id}, $fine->itemnumber );
107
        }
116
        }
108
117
118
        my $account_offset = Koha::Account::Offset->new(
119
            {
120
                debit_id => $fine->id,
121
                type     => $offset_type,
122
                amount   => $amount_to_pay * -1,
123
            }
124
        );
125
        push( @account_offsets, $account_offset );
126
109
        if ( C4::Context->preference("FinesLog") ) {
127
        if ( C4::Context->preference("FinesLog") ) {
110
            logaction(
128
            logaction(
111
                "FINES", 'MODIFY',
129
                "FINES", 'MODIFY',
Lines 149-154 sub pay { Link Here
149
        $fine->amountoutstanding( $old_amountoutstanding - $amount_to_pay );
167
        $fine->amountoutstanding( $old_amountoutstanding - $amount_to_pay );
150
        $fine->store();
168
        $fine->store();
151
169
170
        my $account_offset = Koha::Account::Offset->new(
171
            {
172
                debit_id => $fine->id,
173
                type     => $offset_type,
174
                amount   => $amount_to_pay * -1,
175
            }
176
        );
177
        push( @account_offsets, $account_offset );
178
152
        if ( C4::Context->preference("FinesLog") ) {
179
        if ( C4::Context->preference("FinesLog") ) {
153
            logaction(
180
            logaction(
154
                "FINES", 'MODIFY',
181
                "FINES", 'MODIFY',
Lines 174-185 sub pay { Link Here
174
        last unless $balance_remaining > 0;
201
        last unless $balance_remaining > 0;
175
    }
202
    }
176
203
177
    my $account_type =
204
    $account_type ||=
178
        $type eq 'writeoff' ? 'W'
205
        $type eq 'writeoff' ? 'W'
179
      : defined($sip)       ? "Pay$sip"
206
      : defined($sip)       ? "Pay$sip"
180
      :                       'Pay';
207
      :                       'Pay';
181
208
182
    my $description = $type eq 'writeoff' ? 'Writeoff' : q{};
209
    $description ||= $type eq 'writeoff' ? 'Writeoff' : q{};
183
210
184
    my $payment = Koha::Account::Line->new(
211
    my $payment = Koha::Account::Line->new(
185
        {
212
        {
Lines 195-200 sub pay { Link Here
195
        }
222
        }
196
    )->store();
223
    )->store();
197
224
225
    foreach my $o ( @account_offsets ) {
226
        $o->credit_id( $payment->id() );
227
        $o->store();
228
    }
229
198
    $library_id ||= $userenv ? $userenv->{'branch'} : undef;
230
    $library_id ||= $userenv ? $userenv->{'branch'} : undef;
199
231
200
    UpdateStats(
232
    UpdateStats(
201
- 

Return to bug 14826