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

(-)a/Koha/Account.pm (-6 / +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 333-339 sub add_credit { Link Here
333
334
334
    my $schema = Koha::Database->new->schema;
335
    my $schema = Koha::Database->new->schema;
335
336
336
    my $account_type = $Koha::Account::account_type->{$type};
337
    my $account_type = $Koha::Account::account_type_credit->{$type};
337
    $account_type .= $sip
338
    $account_type .= $sip
338
        if defined $sip &&
339
        if defined $sip &&
339
           $type eq 'payment';
340
           $type eq 'payment';
Lines 409-414 sub add_credit { Link Here
409
    return $line;
410
    return $line;
410
}
411
}
411
412
413
=head3 add_debit
414
415
This method allows adding debits to a patron's account
416
417
my $debit_line = Koha::Account->new({ patron_id => $patron_id })->add_debit(
418
    {
419
        amount       => $amount,
420
        description  => $description,
421
        note         => $note,
422
        user_id      => $user_id,
423
        library_id   => $library_id,
424
        type         => $debit_type,
425
        item_id      => $item_id,
426
        issue_id     => $issue_id
427
    }
428
);
429
430
$debit_type can be any of:
431
  - fine
432
  - lost_item
433
  - new_card
434
  - account
435
  - sundry
436
  - processing
437
  - rent
438
  - reserve
439
  - overdue
440
  - manual
441
442
=cut
443
444
sub add_debit {
445
446
    my ( $self, $params ) = @_;
447
448
    # amount should always be a positive value
449
    my $amount       = $params->{amount};
450
451
    unless ( $amount > 0 ) {
452
        Koha::Exceptions::Account::AmountNotPositive->throw(
453
            error => 'Debit amount passed is not positive'
454
        );
455
    }
456
457
    my $description  = $params->{description} // q{};
458
    my $note         = $params->{note} // q{};
459
    my $user_id      = $params->{user_id};
460
    my $library_id   = $params->{library_id};
461
    my $type         = $params->{type};
462
    my $item_id      = $params->{item_id};
463
    my $issue_id     = $params->{issue_id};
464
465
    my $schema = Koha::Database->new->schema;
466
467
    unless ( exists($Koha::Account::account_type_debit->{$type}) ) {
468
        Koha::Exceptions::Account::UnrecognisedType->throw(
469
            error => 'Type of debit not recognised'
470
        );
471
    }
472
473
    my $account_type = $Koha::Account::account_type_debit->{$type};
474
475
    my $line;
476
477
    $schema->txn_do(
478
        sub {
479
            # We should remove accountno, it is no longer needed
480
            my $last = Koha::Account::Lines->search( { borrowernumber => $self->{patron_id} },
481
                { order_by => 'accountno' } )->next();
482
            my $accountno = $last ? $last->accountno + 1 : 1;
483
484
            # Insert the account line
485
            $line = Koha::Account::Line->new(
486
                {   borrowernumber    => $self->{patron_id},
487
                    date              => \'NOW()',
488
                    amount            => $amount,
489
                    description       => $description,
490
                    accounttype       => $account_type,
491
                    amountoutstanding => $amount,
492
                    payment_type      => undef,
493
                    note              => $note,
494
                    manager_id        => $user_id,
495
                    itemnumber        => $item_id,
496
                    issue_id          => $issue_id,
497
                    branchcode        => $library_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
            if ( C4::Context->preference("FinesLog") ) {
511
                logaction(
512
                    "FINES", 'CREATE',
513
                    $self->{patron_id},
514
                    Dumper(
515
                        {   action            => "create_$type", #create_fee in chargelostitem will become create_processing and create_lost_item
516
                                                                 #not recorded in AddIssuingCharge will become create_rent
517
                                                                 #not recorded in AddRenewal will become create_rent
518
                                                                 #undef in UpdateFine will become create_fine
519
                            borrowernumber    => $self->{patron_id},
520
                            accountno         => $accountno,
521
                            amount            => $amount,
522
                            description       => $description,
523
                            amountoutstanding => $amount,
524
                            accounttype       => $account_type,
525
                            note              => $note,
526
                            itemnumber        => $item_id,
527
                            manager_id        => $user_id,
528
                        }
529
                    )
530
                );
531
            }
532
        }
533
    );
534
535
    return $line;
536
}
537
412
=head3 balance
538
=head3 balance
413
539
414
my $balance = $self->balance
540
my $balance = $self->balance
Lines 562-575 our $offset_type = { Link Here
562
    'forgiven'         => 'Writeoff',
688
    'forgiven'         => 'Writeoff',
563
    'lost_item_return' => 'Lost Item',
689
    'lost_item_return' => 'Lost Item',
564
    'payment'          => 'Payment',
690
    'payment'          => 'Payment',
565
    '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',
697
    'manual_debit'     => 'Manual Debit',
566
};
698
};
567
699
568
=head3 $account_type
700
=head3 $account_type_credit
569
701
570
=cut
702
=cut
571
703
572
our $account_type = {
704
our $account_type_credit = {
573
    'credit'           => 'C',
705
    'credit'           => 'C',
574
    'forgiven'         => 'FOR',
706
    'forgiven'         => 'FOR',
575
    'lost_item_return' => 'CR',
707
    'lost_item_return' => 'CR',
Lines 577-584 our $account_type = { Link Here
577
    'writeoff'         => 'W'
709
    'writeoff'         => 'W'
578
};
710
};
579
711
580
=head1 AUTHOR
712
=head3 $account_type_debit
713
714
=cut
715
716
our $account_type_debit = {
717
    'fine'          => 'FU',
718
    'lost_item'     => 'L',
719
    'new_card'      => 'N',
720
    'account'       => 'A',
721
    'sundry'        => 'M',
722
    'processing'    => 'PF',
723
    'rent'          => 'R',
724
    'reserve'       => 'Res',
725
    'overdue'       => 'O',
726
    'manual_debit'  => 'M',
727
};
728
729
=head1 AUTHORS
581
730
582
Kyle M Hall <kyle.m.hall@gmail.com>
731
Kyle M Hall <kyle.m.hall@gmail.com>
732
Tomás Cohen Arazi <tomascohen@gmail.com>
733
Martin Renvoize <martin.renvoize@ptfs-europe.com>
583
734
584
=cut
735
=cut
585
- 

Return to bug 21002