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

(-)a/Koha/Account/Line.pm (+93 lines)
Lines 278-283 sub apply { Link Here
278
    return $available_credit;
278
    return $available_credit;
279
}
279
}
280
280
281
=head3 payout
282
283
  $credit_accountline->payout(
284
    {
285
        payout_type => $payout_type,
286
        register_id => $register_id,
287
        staff_id    => $staff_id,
288
        interface   => 'intranet',
289
        amount      => $amount
290
    }
291
  );
292
293
Used to 'pay out' a credit to a user.
294
295
Payout type may be one of any existing payment types
296
297
Returns the payout debit line that is created via this transaction.
298
299
=cut
300
301
sub payout {
302
    my ( $self, $params ) = @_;
303
304
    # Make sure it is a credit we are paying out
305
    unless ( $self->is_credit ) {
306
        Koha::Exceptions::Account::IsNotCredit->throw(
307
            error => 'Account line ' . $self->id . ' is not a credit' );
308
    }
309
310
    # Check for mandatory parameters
311
    my @mandatory =
312
      ( 'interface', 'staff_id', 'branch', 'payout_type', 'amount' );
313
    for my $param (@mandatory) {
314
        unless ( defined( $params->{$param} ) ) {
315
            Koha::Exceptions::MissingParameter->throw(
316
                error => "The $param parameter is mandatory" );
317
        }
318
    }
319
320
    # Make sure there is outstanding credit to pay out
321
    my $outstanding = -1 * $self->amountoutstanding;
322
    my $amount =
323
      $params->{amount} ? $params->{amount} : $outstanding;
324
    Koha::Exceptions::Account::AmountNotPositive->throw(
325
        error => 'Payout amount passed is not positive' )
326
      unless ( $amount > 0 );
327
    Koha::Exceptions::ParameterTooHigh->throw(
328
        error => "Amount to payout ($amount) is higher than amountoutstanding ($outstanding)" )
329
      unless ($outstanding >= $amount );
330
331
    # Make sure we record the cash register for cash transactions
332
    Koha::Exceptions::Account::RegisterRequired->throw()
333
      if ( C4::Context->preference("UseCashRegisters")
334
        && defined( $params->{payout_type} )
335
        && ( $params->{payout_type} eq 'CASH' )
336
        && !defined( $params->{cash_register} ) );
337
338
    my $payout;
339
    $self->_result->result_source->schema->txn_do(
340
        sub {
341
342
            # A 'payout' is a 'debit'
343
            $payout = Koha::Account::Line->new(
344
                {
345
                    date              => \'NOW()',
346
                    amount            => $amount,
347
                    debit_type_code   => 'PAYOUT',
348
                    payment_type      => $params->{payout_type},
349
                    amountoutstanding => $amount,
350
                    manager_id        => $params->{staff_id},
351
                    borrowernumber    => $self->borrowernumber,
352
                    interface         => $params->{interface},
353
                    branchcode        => $params->{branch},
354
                    register_id       => $params->{cash_register}
355
                }
356
            )->store();
357
358
            my $payout_offset = Koha::Account::Offset->new(
359
                {
360
                    debit_id => $payout->accountlines_id,
361
                    type     => 'PAYOUT',
362
                    amount   => $amount
363
                }
364
            )->store();
365
366
            $self->apply( { debits => [$payout], offset_type => 'PAYOUT' } );
367
            $self->status('PAID')->store;
368
        }
369
    );
370
371
    return $payout;
372
}
373
281
=head3 adjust
374
=head3 adjust
282
375
283
This method allows updating a debit or credit on a patron's account
376
This method allows updating a debit or credit on a patron's account
(-)a/Koha/Exceptions.pm (+4 lines)
Lines 26-31 use Exception::Class ( Link Here
26
        isa => 'Koha::Exceptions::Exception',
26
        isa => 'Koha::Exceptions::Exception',
27
        description => 'A required parameter is missing'
27
        description => 'A required parameter is missing'
28
    },
28
    },
29
    'Koha::Exceptions::ParameterTooHigh' => {
30
        isa => 'Koha::Exceptions::Exception',
31
        description => 'A passed parameter value is too high'
32
    },
29
    'Koha::Exceptions::NoChanges' => {
33
    'Koha::Exceptions::NoChanges' => {
30
        isa => 'Koha::Exceptions::Exception',
34
        isa => 'Koha::Exceptions::Exception',
31
        description => 'No changes were made',
35
        description => 'No changes were made',
(-)a/t/db_dependent/Koha/Account/Lines.t (-2 / +137 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 9;
22
use Test::More tests => 10;
23
use Test::Exception;
23
use Test::Exception;
24
24
25
use C4::Circulation qw/AddIssue AddReturn/;
25
use C4::Circulation qw/AddIssue AddReturn/;
Lines 606-609 subtest "void() tests" => sub { Link Here
606
    $schema->storage->txn_rollback;
606
    $schema->storage->txn_rollback;
607
};
607
};
608
608
609
subtest "payout() tests" => sub {
610
611
    plan tests => 17;
612
613
    $schema->storage->txn_begin;
614
615
    # Create a borrower
616
    my $categorycode =
617
      $builder->build( { source => 'Category' } )->{categorycode};
618
    my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
619
620
    my $borrower = Koha::Patron->new(
621
        {
622
            cardnumber => 'dariahall',
623
            surname    => 'Hall',
624
            firstname  => 'Daria',
625
        }
626
    );
627
    $borrower->categorycode($categorycode);
628
    $borrower->branchcode($branchcode);
629
    $borrower->store;
630
631
    my $staff = Koha::Patron->new(
632
        {
633
            cardnumber => 'bobby',
634
            surname    => 'Bloggs',
635
            firstname  => 'Bobby',
636
        }
637
    );
638
    $staff->categorycode($categorycode);
639
    $staff->branchcode($branchcode);
640
    $staff->store;
641
642
    my $account = Koha::Account->new( { patron_id => $borrower->id } );
643
644
    my $debit1 = Koha::Account::Line->new(
645
        {
646
            borrowernumber    => $borrower->borrowernumber,
647
            amount            => 10,
648
            amountoutstanding => 10,
649
            interface         => 'commandline',
650
            debit_type_code   => 'OVERDUE'
651
        }
652
    )->store();
653
    my $credit1 = Koha::Account::Line->new(
654
        {
655
            borrowernumber    => $borrower->borrowernumber,
656
            amount            => -20,
657
            amountoutstanding => -20,
658
            interface         => 'commandline',
659
            credit_type_code  => 'CREDIT'
660
        }
661
    )->store();
662
663
    is( $account->balance(), -10, "Account balance is -10" );
664
    is( $debit1->amountoutstanding,
665
        10, 'Overdue fee has an amount outstanding of 10' );
666
    is( $credit1->amountoutstanding,
667
        -20, 'Credit has an amount outstanding of -20' );
668
669
    my $pay_params = {
670
        interface   => 'intranet',
671
        staff_id    => $staff->borrowernumber,
672
        branch      => $branchcode,
673
        payout_type => 'CASH',
674
        amount      => 20
675
    };
676
677
    throws_ok { $debit1->payout($pay_params); }
678
    'Koha::Exceptions::Account::IsNotCredit',
679
      '->payout() can only be used with credits';
680
681
    my @required =
682
      ( 'interface', 'staff_id', 'branch', 'payout_type', 'amount' );
683
    for my $required (@required) {
684
        my $params = {%$pay_params};
685
        delete( $params->{$required} );
686
        throws_ok {
687
            $credit1->payout($params);
688
        }
689
        'Koha::Exceptions::MissingParameter',
690
          "->payout() requires the `$required` parameter is passed";
691
    }
692
693
    throws_ok {
694
        $credit1->payout(
695
            {
696
                interface   => 'intranet',
697
                staff_id    => $staff->borrowernumber,
698
                branch      => $branchcode,
699
                payout_type => 'CASH',
700
                amount      => 25
701
            }
702
        );
703
    }
704
    'Koha::Exceptions::ParameterTooHigh',
705
      '->payout() cannot pay out more than the amountoutstanding';
706
707
    t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
708
    throws_ok {
709
        $credit1->payout(
710
            {
711
                interface   => 'intranet',
712
                staff_id    => $staff->borrowernumber,
713
                branch      => $branchcode,
714
                payout_type => 'CASH',
715
                amount      => 10
716
            }
717
        );
718
    }
719
    'Koha::Exceptions::Account::RegisterRequired',
720
      '->payout() requires a cash_register if payout_type is `CASH`';
721
722
    t::lib::Mocks::mock_preference( 'UseCashRegisters', 0 );
723
    my $payout = $credit1->payout(
724
        {
725
            interface   => 'intranet',
726
            staff_id    => $staff->borrowernumber,
727
            branch      => $branchcode,
728
            payout_type => 'CASH',
729
            amount      => 10
730
        }
731
    );
732
733
    is( $payout->amount(),            10, "Payout amount is 10" );
734
    is( $payout->amountoutstanding(), 0,  "Payout amountoutstanding is 0" );
735
    is( $account->balance(),          0,  "Account balance is 0" );
736
    is( $debit1->amountoutstanding,
737
        10, 'Overdue fee still has an amount outstanding of 10' );
738
    is( $credit1->amountoutstanding,
739
        -10, 'Credit has an new amount outstanding of -10' );
740
    is( $credit1->status(), 'PAID', "Credit has a new status of PAID" );
741
742
    $schema->storage->txn_rollback;
743
};
744
609
1;
745
1;
610
- 

Return to bug 24080