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

(-)a/Koha/Account.pm (-5 / +156 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 334-340 sub add_credit { Link Here
334
335
335
    my $schema = Koha::Database->new->schema;
336
    my $schema = Koha::Database->new->schema;
336
337
337
    my $account_type = $Koha::Account::account_type->{$type};
338
    my $account_type = $Koha::Account::account_type_credit->{$type};
338
    $account_type .= $sip
339
    $account_type .= $sip
339
        if defined $sip &&
340
        if defined $sip &&
340
           $type eq 'payment';
341
           $type eq 'payment';
Lines 410-415 sub add_credit { Link Here
410
    return $line;
411
    return $line;
411
}
412
}
412
413
414
=head3 add_debit
415
416
This method allows adding debits to a patron's account
417
418
my $debit_line = Koha::Account->new({ patron_id => $patron_id })->add_debit(
419
    {
420
        amount       => $amount,
421
        description  => $description,
422
        note         => $note,
423
        user_id      => $user_id,
424
        library_id   => $library_id,
425
        type         => $debit_type,
426
        item_id      => $item_id,
427
        issue_id     => $issue_id
428
    }
429
);
430
431
$debit_type can be any of:
432
  - fine
433
  - lost_item
434
  - new_card
435
  - account
436
  - sundry
437
  - processing
438
  - rent
439
  - reserve
440
  - overdue
441
  - manual
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
                    branchcode        => $library_id,
499
                    ( $type eq 'fine' ? ( lastincrement => $amount ) : ()),
500
                }
501
            )->store();
502
503
            # Record the account offset
504
            my $account_offset = Koha::Account::Offset->new(
505
                {   debit_id => $line->id,
506
                    type      => $Koha::Account::offset_type->{$type},
507
                    amount    => $amount
508
                }
509
            )->store();
510
511
            if ( C4::Context->preference("FinesLog") ) {
512
                logaction(
513
                    "FINES", 'CREATE',
514
                    $self->{patron_id},
515
                    Dumper(
516
                        {   action            => "create_$type", #create_fee in chargelostitem will become create_processing and create_lost_item
517
                                                                 #not recorded in AddIssuingCharge will become create_rent
518
                                                                 #not recorded in AddRenewal will become create_rent
519
                                                                 #undef in UpdateFine will become create_fine
520
                            borrowernumber    => $self->{patron_id},
521
                            accountno         => $accountno,
522
                            amount            => $amount,
523
                            description       => $description,
524
                            amountoutstanding => $amount,
525
                            accounttype       => $account_type,
526
                            note              => $note,
527
                            itemnumber        => $item_id,
528
                            manager_id        => $user_id,
529
                        }
530
                    )
531
                );
532
            }
533
        }
534
    );
535
536
    return $line;
537
}
538
413
=head3 balance
539
=head3 balance
414
540
415
my $balance = $self->balance
541
my $balance = $self->balance
Lines 532-545 our $offset_type = { Link Here
532
    'forgiven'         => 'Writeoff',
658
    'forgiven'         => 'Writeoff',
533
    'lost_item_return' => 'Lost Item',
659
    'lost_item_return' => 'Lost Item',
534
    'payment'          => 'Payment',
660
    'payment'          => 'Payment',
535
    'writeoff'         => 'Writeoff'
661
    'writeoff'         => 'Writeoff',
662
    'reserve'          => 'Reserve Fee',
663
    'processing'       => 'Processing Fee',
664
    'lost_item'        => 'Lost Item',
665
    'rent'             => 'Rental Fee',
666
    'fine'             => 'Fine',
667
    'manual_debit'     => 'Manual Debit',
536
};
668
};
537
669
538
=head3 $account_type
670
=head3 $account_type_credit
539
671
540
=cut
672
=cut
541
673
542
our $account_type = {
674
our $account_type_credit = {
543
    'credit'           => 'C',
675
    'credit'           => 'C',
544
    'forgiven'         => 'FOR',
676
    'forgiven'         => 'FOR',
545
    'lost_item_return' => 'CR',
677
    'lost_item_return' => 'CR',
Lines 547-554 our $account_type = { Link Here
547
    'writeoff'         => 'W'
679
    'writeoff'         => 'W'
548
};
680
};
549
681
550
=head1 AUTHOR
682
=head3 $account_type_debit
683
684
=cut
685
686
our $account_type_debit = {
687
    'fine'          => 'FU',
688
    'lost_item'     => 'L',
689
    'new_card'      => 'N',
690
    'account'       => 'A',
691
    'sundry'        => 'M',
692
    'processing'    => 'PF',
693
    'rent'          => 'R',
694
    'reserve'       => 'Res',
695
    'overdue'       => 'O',
696
    'manual_debit'  => 'M',
697
};
698
699
=head1 AUTHORS
551
700
552
Kyle M Hall <kyle.m.hall@gmail.com>
701
Kyle M Hall <kyle.m.hall@gmail.com>
702
Tomás Cohen Arazi <tomascohen@gmail.com>
703
Martin Renvoize <martin.renvoize@ptfs-europe.com>
553
704
554
=cut
705
=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