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 340-345 sub non_issues_charges { Link Here
340
450
341
1;
451
1;
342
452
453
=head2 Name mappings
454
455
=head3 $offset_type
456
457
=cut
458
459
our $offset_type = {
460
    'credit'   => 'Payment',
461
    'forgiven' => 'Writeoff',
462
    'payment'  => 'Payment',
463
    'writeoff' => 'Writeoff'
464
};
465
466
=head3 $account_type
467
468
=cut
469
470
our $account_type = {
471
    'credit'   => 'C',
472
    'forgiven' => 'FOR',
473
    'payment'  => 'Pay',
474
    'writeoff' => 'W'
475
};
476
343
=head1 AUTHOR
477
=head1 AUTHOR
344
478
345
Kyle M Hall <kyle.m.hall@gmail.com>
479
Kyle M Hall <kyle.m.hall@gmail.com>
346
- 

Return to bug 20978