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

(-)a/Koha/Account.pm (-1 / +134 lines)
Lines 264-269 sub pay { Link Here
264
    return $payment->id;
264
    return $payment->id;
265
}
265
}
266
266
267
=head3 add_credit
268
269
This method allows adding credits to a patron's account
270
271
my $credit_line = Koha::Account->new({ patron_id => $patron_id })->add_credit(
272
    {
273
        amount       => $amount,
274
        description  => $description,
275
        note         => $note,
276
        user_id      => $user_id,
277
        library_id   => $library_id,
278
        sip          => $sip,
279
        payment_type => $payment_type,
280
        type         => $credit_type,
281
        item_id      => $item_id
282
    }
283
);
284
285
$credit_type can be any of 'credit', 'payment', 'forgiven' or 'writeoff'
286
287
=cut
288
289
sub add_credit {
290
291
    my ( $self, $params ) = @_;
292
293
    # amount is passed as a positive value, but we store credit as negative values
294
    my $amount       = $params->{amount} * -1;
295
    my $description  = $params->{description} // q{};
296
    my $note         = $params->{note} // q{};
297
    my $user_id      = $params->{user_id};
298
    my $library_id   = $params->{library_id};
299
    my $sip          = $params->{sip};
300
    my $payment_type = $params->{payment_type};
301
    my $type         = $params->{type} || 'payment';
302
    my $item_id      = $params->{item_id};
303
304
    my $schema = Koha::Database->new->schema;
305
306
    my $account_type = $Koha::Account::account_type->{$type};
307
    $account_type .= $sip
308
        if defined $sip &&
309
           $type eq 'payment';
310
311
    my $line;
312
313
    $schema->txn_do(
314
        sub {
315
            # We should remove accountno, it is no longer needed
316
            my $last = Koha::Account::Lines->search( { borrowernumber => $self->{patron_id} },
317
                { order_by => 'accountno' } )->next();
318
            my $accountno = $last ? $last->accountno + 1 : 1;
319
320
            # Insert the account line
321
            $line = Koha::Account::Line->new(
322
                {   borrowernumber    => $self->{patron_id},
323
                    date              => \'NOW()',
324
                    amount            => $amount,
325
                    description       => $description,
326
                    accounttype       => $account_type,
327
                    amountoutstanding => $amount,
328
                    payment_type      => $payment_type,
329
                    note              => $note,
330
                    manager_id        => $user_id,
331
                    itemnumber        => $item_id
332
                }
333
            )->store();
334
335
            # Record the account offset
336
            my $account_offset = Koha::Account::Offset->new(
337
                {   credit_id => $line->id,
338
                    type      => $Koha::Account::offset_type->{$type},
339
                    amount    => $amount
340
                }
341
            )->store();
342
343
            UpdateStats(
344
                {   branch         => $library_id,
345
                    type           => $type,
346
                    amount         => $amount,
347
                    borrowernumber => $self->{patron_id},
348
                    accountno      => $accountno,
349
                }
350
            );
351
352
            if ( C4::Context->preference("FinesLog") ) {
353
                logaction(
354
                    "FINES", 'CREATE',
355
                    $self->{patron_id},
356
                    Dumper(
357
                        {   action            => "create_$type",
358
                            borrowernumber    => $self->{patron_id},
359
                            accountno         => $accountno,
360
                            amount            => $amount,
361
                            description       => $description,
362
                            amountoutstanding => $amount,
363
                            accounttype       => $account_type,
364
                            note              => $note,
365
                            itemnumber        => $item_id,
366
                            manager_id        => $user_id,
367
                        }
368
                    )
369
                );
370
            }
371
        }
372
    );
373
374
    return $line;
375
}
376
267
=head3 balance
377
=head3 balance
268
378
269
my $balance = $self->balance
379
my $balance = $self->balance
Lines 363-368 sub non_issues_charges { Link Here
363
473
364
1;
474
1;
365
475
476
=head2 Name mappings
477
478
=head3 $offset_type
479
480
=cut
481
482
our $offset_type = {
483
    'credit'   => 'Payment',
484
    'forgiven' => 'Writeoff',
485
    'payment'  => 'Payment',
486
    'writeoff' => 'Writeoff'
487
};
488
489
=head3 $account_type
490
491
=cut
492
493
our $account_type = {
494
    'credit'   => 'C',
495
    'forgiven' => 'FOR',
496
    'payment'  => 'Pay',
497
    'writeoff' => 'W'
498
};
499
366
=head1 AUTHOR
500
=head1 AUTHOR
367
501
368
Kyle M Hall <kyle.m.hall@gmail.com>
502
Kyle M Hall <kyle.m.hall@gmail.com>
369
- 

Return to bug 20978