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

(-)a/Koha/Account.pm (-5 / +161 lines)
Lines 32-37 use Koha::Patrons; Link Here
32
use Koha::Account::Lines;
32
use Koha::Account::Lines;
33
use Koha::Account::Offsets;
33
use Koha::Account::Offsets;
34
use Koha::DateUtils qw( dt_from_string );
34
use Koha::DateUtils qw( dt_from_string );
35
use Koha::Exceptions::Account;
35
36
36
=head1 NAME
37
=head1 NAME
37
38
Lines 340-346 sub add_credit { Link Here
340
341
341
    my $schema = Koha::Database->new->schema;
342
    my $schema = Koha::Database->new->schema;
342
343
343
    my $account_type = $Koha::Account::account_type->{$type};
344
    my $account_type = $Koha::Account::account_type_credit->{$type};
344
    $account_type .= $sip
345
    $account_type .= $sip
345
        if defined $sip &&
346
        if defined $sip &&
346
           $type eq 'payment';
347
           $type eq 'payment';
Lines 411-416 sub add_credit { Link Here
411
    return $line;
412
    return $line;
412
}
413
}
413
414
415
=head3 add_debit
416
417
This method allows adding debits to a patron's account
418
419
my $debit_line = Koha::Account->new({ patron_id => $patron_id })->add_debit(
420
    {
421
        amount       => $amount,
422
        description  => $description,
423
        note         => $note,
424
        user_id      => $user_id,
425
        library_id   => $library_id,
426
        type         => $debit_type,
427
        item_id      => $item_id,
428
        issue_id     => $issue_id
429
    }
430
);
431
432
$debit_type can be any of:
433
  - fine
434
  - lost_item
435
  - new_card
436
  - account
437
  - sundry
438
  - processing
439
  - rent
440
  - reserve
441
  - overdue
442
443
=cut
444
445
sub add_debit {
446
447
    my ( $self, $params ) = @_;
448
449
    # amount should always be a positive value
450
    my $amount       = $params->{amount};
451
452
    unless ( $amount > 0 ) {
453
        Koha::Exceptions::Account::AmountNotPositive->throw(
454
            error => 'Debit amount passed is not positive'
455
        );
456
    }
457
458
    my $description  = $params->{description} // q{};
459
    my $note         = $params->{note} // q{};
460
    my $user_id      = $params->{user_id};
461
    my $library_id   = $params->{library_id};
462
    my $type         = $params->{type};
463
    my $item_id      = $params->{item_id};
464
    my $issue_id     = $params->{issue_id};
465
466
    my $schema = Koha::Database->new->schema;
467
468
    unless ( exists($Koha::Account::account_type_debit->{$type}) ) {
469
        Koha::Exceptions::Account::UnrecognisedType->throw(
470
            error => 'Type of debit not recognised'
471
        );
472
    }
473
474
    my $account_type = $Koha::Account::account_type_debit->{$type};
475
476
    my $line;
477
478
    $schema->txn_do(
479
        sub {
480
            # We should remove accountno, it is no longer needed
481
            my $last = Koha::Account::Lines->search( { borrowernumber => $self->{patron_id} },
482
                { order_by => 'accountno' } )->next();
483
            my $accountno = $last ? $last->accountno + 1 : 1;
484
485
            # Insert the account line
486
            $line = Koha::Account::Line->new(
487
                {   borrowernumber    => $self->{patron_id},
488
                    date              => \'NOW()',
489
                    amount            => $amount,
490
                    description       => $description,
491
                    accounttype       => $account_type,
492
                    amountoutstanding => $amount,
493
                    payment_type      => undef,
494
                    note              => $note,
495
                    manager_id        => $user_id,
496
                    itemnumber        => $item_id,
497
                    issue_id          => $issue_id,
498
                    ( $type eq 'fine' ? ( lastincrement => $amount ) : ()),
499
                }
500
            )->store();
501
502
            # Record the account offset
503
            my $account_offset = Koha::Account::Offset->new(
504
                {   debit_id => $line->id,
505
                    type      => $Koha::Account::offset_type->{$type},
506
                    amount    => $amount
507
                }
508
            )->store();
509
510
            UpdateStats(
511
                {   branch         => $library_id,
512
                    type           => $type,
513
                    amount         => $amount,
514
                    borrowernumber => $self->{patron_id},
515
                    accountno      => $accountno,
516
                }
517
            ) if grep { $type eq $_ } ('renew', 'issue', 'localuse', 'return', 'onsite_checkout' );
518
519
            if ( C4::Context->preference("FinesLog") ) {
520
                logaction(
521
                    "FINES", 'CREATE',
522
                    $self->{patron_id},
523
                    Dumper(
524
                        {   action            => "create_$type", #create_fee in chargelostitem will become create_processing and create_lost_item
525
                                                                 #not recorded in AddIssuingCharge will become create_rent
526
                                                                 #not recorded in AddRenewal will become create_rent
527
                                                                 #undef in UpdateFine will become create_fine
528
                            borrowernumber    => $self->{patron_id},
529
                            accountno         => $accountno,
530
                            amount            => $amount,
531
                            description       => $description,
532
                            amountoutstanding => $amount,
533
                            accounttype       => $account_type,
534
                            note              => $note,
535
                            itemnumber        => $item_id,
536
                            manager_id        => $user_id,
537
                        }
538
                    )
539
                );
540
            }
541
        }
542
    );
543
544
    return $line;
545
}
546
414
=head3 balance
547
=head3 balance
415
548
416
my $balance = $self->balance
549
my $balance = $self->balance
Lines 555-568 our $offset_type = { Link Here
555
    'forgiven'         => 'Writeoff',
688
    'forgiven'         => 'Writeoff',
556
    'lost_item_return' => 'Lost Item',
689
    'lost_item_return' => 'Lost Item',
557
    'payment'          => 'Payment',
690
    'payment'          => 'Payment',
558
    'writeoff'         => 'Writeoff'
691
    'writeoff'         => 'Writeoff',
692
    'reserve'          => 'Reserve Fee',
693
    'processing'       => 'Processing Fee',
694
    'lost_item'        => 'Lost Item',
695
    'rent'             => 'Rental Fee',
696
    'fine'             => 'Fine',
559
};
697
};
560
698
561
=head3 $account_type
699
=head3 $account_type_credit
562
700
563
=cut
701
=cut
564
702
565
our $account_type = {
703
our $account_type_credit = {
566
    'credit'           => 'C',
704
    'credit'           => 'C',
567
    'forgiven'         => 'FOR',
705
    'forgiven'         => 'FOR',
568
    'lost_item_return' => 'CR',
706
    'lost_item_return' => 'CR',
Lines 570-577 our $account_type = { Link Here
570
    'writeoff'         => 'W'
708
    'writeoff'         => 'W'
571
};
709
};
572
710
573
=head1 AUTHOR
711
=head3 $account_type_debit
712
713
=cut
714
715
our $account_type_debit = {
716
    'fine'          => 'FU',
717
    'lost_item'     => 'L',
718
    'new_card'      => 'N',
719
    'account'       => 'A',
720
    'sundry'        => 'M',
721
    'processing'    => 'PF',
722
    'rent'          => 'R',
723
    'reserve'       => 'Res',
724
    'overdue'       => 'O'
725
};
726
727
=head1 AUTHORS
574
728
575
Kyle M Hall <kyle.m.hall@gmail.com>
729
Kyle M Hall <kyle.m.hall@gmail.com>
730
Tomás Cohen Arazi <tomascohen@gmail.com>
731
Martin Renvoize <martin.renvoize@ptfs-europe.com>
576
732
577
=cut
733
=cut
(-)a/Koha/Exceptions/Account.pm (-1 / +17 lines)
Lines 33-38 use Exception::Class ( Link Here
33
    'Koha::Exceptions::Account::NoAvailableCredit' => {
33
    'Koha::Exceptions::Account::NoAvailableCredit' => {
34
        isa => 'Koha::Exceptions::Account',
34
        isa => 'Koha::Exceptions::Account',
35
        description => 'No outstanding credit'
35
        description => 'No outstanding credit'
36
    },
37
    'Koha::Exceptions::Account::AmountNotPositive' => {
38
        isa => 'Koha::Exceptions::Account',
39
        description => 'Amount should be a positive decimal'
40
    },
41
    'Koha::Exceptions::Account::UnrecognisedType' => {
42
        isa => 'Koha::Exceptions::Account',
43
        description => 'Account type was not recognised'
36
    }
44
    }
37
);
45
);
38
46
Lines 61-66 debit and it isn't. Link Here
61
Exception to be used when a credit has no amount outstanding and is required
69
Exception to be used when a credit has no amount outstanding and is required
62
to be applied to outstanding debits.
70
to be applied to outstanding debits.
63
71
72
=head2 Koha::Exceptions::Account::AmountNotPositive
73
74
Exception to be used when a passed credit or debit amount is not a positive
75
decimal value.
76
77
=head2 Koha::Exceptions::Account::UnrecognisedType
78
79
Exception to be used when a passed credit or debit is not of a recognised type.
80
64
=cut
81
=cut
65
82
66
1;
83
1;
67
- 

Return to bug 21002