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

(-)a/Koha/Account.pm (-1 / +128 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
    }
281
);
282
283
$credit_type can be any of 'credit', 'payment', 'forgiven' or 'writeoff'
284
285
=cut
286
287
sub add_credit {
288
289
    my ( $self, $params ) = @_;
290
291
    # amount is passed as a positive value, but we store credit as negative values
292
    my $amount       = $params->{amount} * -1;
293
    my $description  = $params->{description} // q{};
294
    my $note         = $params->{note} // q{};
295
    my $user_id      = $params->{user_id};
296
    my $library_id   = $params->{library_id};
297
    my $sip          = $params->{sip};
298
    my $payment_type = $params->{payment_type};
299
    my $type         = $params->{type} || 'payment';
300
301
    my $schema = Koha::Database->new->schema;
302
303
    my $account_type = $Koha::Account::account_type->{$type};
304
    $account_type .= $sip
305
        if defined $sip &&
306
           $type eq 'payment';
307
308
    my $line;
309
310
    $schema->txn_do(
311
        sub {
312
            # We should remove accountno, it is no longer needed
313
            my $last = Koha::Account::Lines->search( { borrowernumber => $self->{patron_id} },
314
                { order_by => 'accountno' } )->next();
315
            my $accountno = $last ? $last->accountno + 1 : 1;
316
317
            # Insert the account line
318
            $line = Koha::Account::Line->new(
319
                {   borrowernumber    => $self->{patron_id},
320
                    date              => \'NOW()',
321
                    amount            => $amount,
322
                    description       => $description,
323
                    accounttype       => $account_type,
324
                    amountoutstanding => $amount,
325
                    payment_type      => $payment_type,
326
                    note              => $note,
327
                    manager_id        => $user_id
328
                }
329
            )->store();
330
331
            # Record the account offset
332
            my $account_offset = Koha::Account::Offset->new(
333
                {   debit_id => $line->id,
334
                    type     => $Koha::Account::offset_type->{$type},
335
                    amount   => $amount
336
                }
337
            )->store();
338
339
            UpdateStats(
340
                {   branch         => $library_id,
341
                    type           => $type,
342
                    amount         => $amount,
343
                    borrowernumber => $self->{patron_id},
344
                    accountno      => $accountno,
345
                }
346
            );
347
348
            if ( C4::Context->preference("FinesLog") ) {
349
                logaction(
350
                    "FINES", 'CREATE',
351
                    $self->{patron_id},
352
                    Dumper(
353
                        {   action            => "create_$type",
354
                            borrowernumber    => $self->{patron_id},
355
                            accountno         => $accountno,
356
                            amount            => $amount,
357
                            amountoutstanding => $amount,
358
                            accounttype       => $Koha::Account::account_type->{$type},
359
                            manager_id        => $user_id,
360
                        }
361
                    )
362
                );
363
            }
364
        }
365
    );
366
367
    return $line;
368
}
369
266
=head3 balance
370
=head3 balance
267
371
268
my $balance = $self->balance
372
my $balance = $self->balance
Lines 340-345 sub non_issues_charges { Link Here
340
444
341
1;
445
1;
342
446
447
=head2 Name mappings
448
449
=head3 $offset_type
450
451
=cut
452
453
our $offset_type = {
454
    'credit'   => 'Payment',
455
    'forgiven' => 'Writeoff',
456
    'payment'  => 'Payment',
457
    'writeoff' => 'Writeoff'
458
};
459
460
=head3 $account_type
461
462
=cut
463
464
our $account_type = {
465
    'credit'   => 'C',
466
    'forgiven' => 'FOR',
467
    'payment'  => 'Pay',
468
    'writeoff' => 'W'
469
};
470
343
=head1 AUTHOR
471
=head1 AUTHOR
344
472
345
Kyle M Hall <kyle.m.hall@gmail.com>
473
Kyle M Hall <kyle.m.hall@gmail.com>
346
- 

Return to bug 20978