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

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

Return to bug 20978