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

(-)a/Koha/Account.pm (-77 / +87 lines)
Lines 334-341 sub add_credit { Link Here
334
334
335
    my ( $self, $params ) = @_;
335
    my ( $self, $params ) = @_;
336
336
337
    # amount is passed as a positive value, but we store credit as negative values
337
    # check for mandatory params
338
    my $amount        = $params->{amount} * -1;
338
    my @mandatory = ( 'interface', 'amount' );
339
    for my $param (@mandatory) {
340
        unless ( defined( $params->{$param} ) ) {
341
            Koha::Exceptions::MissingParameter->throw(
342
                error => "The $param parameter is mandatory" );
343
        }
344
    }
345
346
    # amount should always be passed as a positive value
347
    my $amount = $params->{amount} * -1;
348
    unless ( $amount < 0 ) {
349
        Koha::Exceptions::Account::AmountNotPositive->throw(
350
            error => 'Debit amount passed is not positive' );
351
    }
352
339
    my $description   = $params->{description} // q{};
353
    my $description   = $params->{description} // q{};
340
    my $note          = $params->{note} // q{};
354
    my $note          = $params->{note} // q{};
341
    my $user_id       = $params->{user_id};
355
    my $user_id       = $params->{user_id};
Lines 343-427 sub add_credit { Link Here
343
    my $library_id    = $params->{library_id};
357
    my $library_id    = $params->{library_id};
344
    my $cash_register = $params->{cash_register};
358
    my $cash_register = $params->{cash_register};
345
    my $payment_type  = $params->{payment_type};
359
    my $payment_type  = $params->{payment_type};
346
    my $type          = $params->{type} || 'PAYMENT';
360
    my $credit_type   = $params->{type} || 'PAYMENT';
347
    my $item_id       = $params->{item_id};
361
    my $item_id       = $params->{item_id};
348
362
349
    unless ( $interface ) {
350
        Koha::Exceptions::MissingParameter->throw(
351
            error => 'The interface parameter is mandatory'
352
        );
353
    }
354
355
    Koha::Exceptions::Account::RegisterRequired->throw()
363
    Koha::Exceptions::Account::RegisterRequired->throw()
356
      if ( C4::Context->preference("UseCashRegisters")
364
      if ( C4::Context->preference("UseCashRegisters")
357
        && defined($payment_type)
365
        && defined($payment_type)
358
        && ( $payment_type eq 'CASH' )
366
        && ( $payment_type eq 'CASH' )
359
        && !defined($cash_register) );
367
        && !defined($cash_register) );
360
368
361
    my $schema = Koha::Database->new->schema;
362
363
    my $credit_type = $Koha::Account::account_type_credit->{$type};
364
    my $line;
369
    my $line;
370
    my $schema = Koha::Database->new->schema;
371
    try {
372
        $schema->txn_do(
373
            sub {
365
374
366
    $schema->txn_do(
375
                # Insert the account line
367
        sub {
376
                $line = Koha::Account::Line->new(
377
                    {
378
                        borrowernumber    => $self->{patron_id},
379
                        date              => \'NOW()',
380
                        amount            => $amount,
381
                        description       => $description,
382
                        credit_type_code  => $credit_type,
383
                        amountoutstanding => $amount,
384
                        payment_type      => $payment_type,
385
                        note              => $note,
386
                        manager_id        => $user_id,
387
                        interface         => $interface,
388
                        branchcode        => $library_id,
389
                        register_id       => $cash_register,
390
                        itemnumber        => $item_id,
391
                    }
392
                )->store();
368
393
369
            # Insert the account line
394
                # Record the account offset
370
            $line = Koha::Account::Line->new(
395
                my $account_offset = Koha::Account::Offset->new(
371
                {   borrowernumber    => $self->{patron_id},
396
                    {
372
                    date              => \'NOW()',
397
                        credit_id => $line->id,
373
                    amount            => $amount,
398
                        type   => $Koha::Account::offset_type->{$credit_type},
374
                    description       => $description,
399
                        amount => $amount
375
                    credit_type_code  => $credit_type,
400
                    }
376
                    amountoutstanding => $amount,
401
                )->store();
377
                    payment_type      => $payment_type,
378
                    note              => $note,
379
                    manager_id        => $user_id,
380
                    interface         => $interface,
381
                    branchcode        => $library_id,
382
                    register_id       => $cash_register,
383
                    itemnumber        => $item_id,
384
                }
385
            )->store();
386
402
387
            # Record the account offset
403
                UpdateStats(
388
            my $account_offset = Koha::Account::Offset->new(
404
                    {
389
                {   credit_id => $line->id,
405
                        branch         => $library_id,
390
                    type      => $Koha::Account::offset_type->{$type},
406
                        type           => $credit_type,
391
                    amount    => $amount
407
                        amount         => $amount,
392
                }
408
                        borrowernumber => $self->{patron_id},
393
            )->store();
409
                    }
410
                ) if grep { $credit_type eq $_ } ( 'PAYMENT', 'WRITEOFF' );
394
411
395
            UpdateStats(
412
                if ( C4::Context->preference("FinesLog") ) {
396
                {   branch         => $library_id,
413
                    logaction(
397
                    type           => $type,
414
                        "FINES", 'CREATE',
398
                    amount         => $amount,
415
                        $self->{patron_id},
399
                    borrowernumber => $self->{patron_id},
416
                        Dumper(
417
                            {
418
                                action            => "create_$credit_type",
419
                                borrowernumber    => $self->{patron_id},
420
                                amount            => $amount,
421
                                description       => $description,
422
                                amountoutstanding => $amount,
423
                                credit_type_code  => $credit_type,
424
                                note              => $note,
425
                                itemnumber        => $item_id,
426
                                manager_id        => $user_id,
427
                                branchcode        => $library_id,
428
                            }
429
                        ),
430
                        $interface
431
                    );
400
                }
432
                }
401
            ) if grep { $type eq $_ } ('PAYMENT', 'WRITEOFF') ;
433
            }
402
434
        );
403
            if ( C4::Context->preference("FinesLog") ) {
435
    }
404
                logaction(
436
    catch {
405
                    "FINES", 'CREATE',
437
        if ( ref($_) eq 'Koha::Exceptions::Object::FKConstraint' ) {
406
                    $self->{patron_id},
438
            if ( $_->broken_fk eq 'credit_type_code' ) {
407
                    Dumper(
439
                Koha::Exceptions::Account::UnrecognisedType->throw(
408
                        {   action            => "create_$type",
440
                    error => 'Type of credit not recognised' );
409
                            borrowernumber    => $self->{patron_id},
441
            }
410
                            amount            => $amount,
442
            else {
411
                            description       => $description,
443
                $_->rethrow;
412
                            amountoutstanding => $amount,
413
                            credit_type_code  => $credit_type,
414
                            note              => $note,
415
                            itemnumber        => $item_id,
416
                            manager_id        => $user_id,
417
                            branchcode        => $library_id,
418
                        }
419
                    ),
420
                    $interface
421
                );
422
            }
444
            }
423
        }
445
        }
424
    );
446
    };
425
447
426
    return $line;
448
    return $line;
427
}
449
}
Lines 734-751 our $offset_type = { Link Here
734
    'RESERVE_EXPIRED'  => 'Hold Expired'
756
    'RESERVE_EXPIRED'  => 'Hold Expired'
735
};
757
};
736
758
737
=head3 $account_type_credit
738
739
=cut
740
741
our $account_type_credit = {
742
    'CREDIT'           => 'CREDIT',
743
    'FORGIVEN'         => 'FORGIVEN',
744
    'LOST_RETURN'      => 'LOST_RETURN',
745
    'PAYMENT'          => 'PAYMENT',
746
    'WRITEOFF'         => 'WRITEOFF'
747
};
748
749
=head1 AUTHORS
759
=head1 AUTHORS
750
760
751
=encoding utf8
761
=encoding utf8
(-)a/t/db_dependent/Koha/Account.t (-3 / +2 lines)
Lines 201-207 subtest 'add_credit() tests' => sub { Link Here
201
    is( $account->balance, -25, 'Patron has a balance of -25' );
201
    is( $account->balance, -25, 'Patron has a balance of -25' );
202
    is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No log was added' );
202
    is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No log was added' );
203
    is( $schema->resultset('Statistic')->count(), $statistics + 1, 'Action added to statistics' );
203
    is( $schema->resultset('Statistic')->count(), $statistics + 1, 'Action added to statistics' );
204
    is( $line_1->credit_type_code, $Koha::Account::account_type_credit->{'PAYMENT'}, 'Account type is correctly set' );
204
    is( $line_1->credit_type_code, 'PAYMENT', 'Account type is correctly set' );
205
205
206
    # Enable logs
206
    # Enable logs
207
    t::lib::Mocks::mock_preference( 'FinesLog', 1 );
207
    t::lib::Mocks::mock_preference( 'FinesLog', 1 );
Lines 219-225 subtest 'add_credit() tests' => sub { Link Here
219
    is( $account->balance, -62, 'Patron has a balance of -25' );
219
    is( $account->balance, -62, 'Patron has a balance of -25' );
220
    is( $schema->resultset('ActionLog')->count(), $action_logs + 1, 'Log was added' );
220
    is( $schema->resultset('ActionLog')->count(), $action_logs + 1, 'Log was added' );
221
    is( $schema->resultset('Statistic')->count(), $statistics + 2, 'Action added to statistics' );
221
    is( $schema->resultset('Statistic')->count(), $statistics + 2, 'Action added to statistics' );
222
    is( $line_2->credit_type_code, $Koha::Account::account_type_credit->{'PAYMENT'}, 'Account type is correctly set' );
222
    is( $line_2->credit_type_code, 'PAYMENT', 'Account type is correctly set' );
223
223
224
    # offsets have the credit_id set to accountlines_id, and debit_id is undef
224
    # offsets have the credit_id set to accountlines_id, and debit_id is undef
225
    my $offset_1 = Koha::Account::Offsets->search({ credit_id => $line_1->id })->next;
225
    my $offset_1 = Koha::Account::Offsets->search({ credit_id => $line_1->id })->next;
226
- 

Return to bug 23805