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

(-)a/Koha/Account/Line.pm (-48 / +30 lines)
Lines 349-360 sub void { Link Here
349
349
350
=head3 cancel
350
=head3 cancel
351
351
352
  $debit_accountline->cancel();
352
  $debit_accountline->cancel({ cancellation_type => $type });
353
353
354
Cancel a charge. It will mark the debit as 'cancelled' by updating its
354
Cancel a charge. It will mark the debit as 'cancelled' by updating its
355
status to 'CANCELLED'.
355
status to 'CANCELLED'.
356
356
357
Charges that have been fully or partially paid cannot be cancelled.
357
Charges that have been fully or partially paid will be refunded upto 
358
the amount paid excluding discounts, writeoffs and refunds.
358
359
359
Returns the cancellation accountline.
360
Returns the cancellation accountline.
360
361
Lines 379-437 sub cancel { Link Here
379
            error => 'Account line ' . $self->id . 'is already cancelled' );
380
            error => 'Account line ' . $self->id . 'is already cancelled' );
380
    }
381
    }
381
382
382
    # Make sure it has not be paid yet
383
    my $cancellation_type = $params->{cancellation_type} // 'CANCELLATION';
384
385
    my $cancellation_amount = $self->amountoutstanding;
383
    if ( $self->amount != $self->amountoutstanding ) {
386
    if ( $self->amount != $self->amountoutstanding ) {
384
        Koha::Exceptions::Account->throw(
387
        my $credit_offsets = Koha::Account::Offsets->search(
385
            error => 'Account line ' . $self->id . 'is already offset' );
388
            {
386
    }
389
                debit_id  => $self->id,
390
                credit_id => { '!='->undef },    # not the debt itself
391
                type      => { '!=' => [ 'Writeoff', 'Refund', 'Discount' ] }
392
                ,    # Don't refund writeoffs, refunds or discounts
393
                amount => { '<' => 0 }    # credits are negative on the DB
394
            }
395
        );
387
396
388
    # Check for mandatory parameters
397
        $cancellation_amount +=
389
    my @mandatory = ( 'staff_id', 'branch' );
398
          $credit_offsets->count > 0 ? $credit_offsets->total * -1 : 0;
390
    for my $param (@mandatory) {
391
        unless ( defined( $params->{$param} ) ) {
392
            Koha::Exceptions::MissingParameter->throw(
393
                error => "The $param parameter is mandatory" );
394
        }
395
    }
399
    }
396
400
397
    my $cancellation;
401
    my $cancellation;
398
    $self->_result->result_source->schema->txn_do(
402
    $self->_result->result_source->schema->txn_do(
399
        sub {
403
        sub {
400
404
            $cancellation = $charge_accountline->reduce(
401
            # A 'cancellation' is a 'credit'
402
            $cancellation = Koha::Account::Line->new(
403
                {
405
                {
404
                    date              => \'NOW()',
406
                    reduction_type => $cancellation_type,
405
                    amount            => 0 - $self->amount,
407
                    amount         => $cancellation_amount,
406
                    credit_type_code  => 'CANCELLATION',
408
                    staff_id       => $params->{staff_id},
407
                    status            => 'ADDED',
409
                    interface      => $params->{interface},
408
                    amountoutstanding => 0 - $self->amount,
410
                    branch         => $params->{branch}
409
                    manager_id        => $params->{staff_id},
410
                    borrowernumber    => $self->borrowernumber,
411
                    interface         => 'intranet',
412
                    branchcode        => $params->{branch},
413
                }
414
            )->store();
415
416
            my $cancellation_offset = Koha::Account::Offset->new(
417
                {
418
                    credit_id => $cancellation->accountlines_id,
419
                    type      => 'CANCELLATION',
420
                    amount    => $self->amount
421
                }
422
            )->store();
423
424
            # Link cancellation to charge
425
            $cancellation->apply(
426
                {
427
                    debits      => [$self],
428
                    offset_type => 'CANCELLATION'
429
                }
411
                }
430
            );
412
            );
431
            $cancellation->status('APPLIED')->store();
432
433
            # Update status of original debit
434
            $self->status('CANCELLED')->store;
435
        }
413
        }
436
    );
414
    );
437
415
Lines 461-466 Reduction type may be one of: Link Here
461
439
462
* REFUND
440
* REFUND
463
* DISCOUNT
441
* DISCOUNT
442
* CANCELLATION
464
443
465
Returns the reduction accountline (which will be a credit)
444
Returns the reduction accountline (which will be a credit)
466
445
Lines 509-522 sub reduce { Link Here
509
"Amount to reduce ($params->{amount}) is higher than original amount ($original)"
488
"Amount to reduce ($params->{amount}) is higher than original amount ($original)"
510
    ) unless ( $original >= $params->{amount} );
489
    ) unless ( $original >= $params->{amount} );
511
    my $reduced =
490
    my $reduced =
512
      $self->credits( { credit_type_code => [ 'DISCOUNT', 'REFUND' ] } )->total;
491
      $self->credits( { credit_type_code => [ 'WRITEOFF', 'DISCOUNT', 'REFUND' ] } )->total;
513
    Koha::Exceptions::ParameterTooHigh->throw( error =>
492
    Koha::Exceptions::ParameterTooHigh->throw( error =>
514
"Combined reduction ($params->{amount} + $reduced) is higher than original amount ("
493
"Combined reduction ($params->{amount} + $reduced) is higher than original amount ("
515
          . abs($original)
494
          . abs($original)
516
          . ")" )
495
          . ")" )
517
      unless ( $original >= ( $params->{amount} + abs($reduced) ) );
496
      unless ( $original >= ( $params->{amount} + abs($reduced) ) );
518
497
519
    my $status = { 'REFUND' => 'REFUNDED', 'DISCOUNT' => 'DISCOUNTED' };
498
    my $status = {
499
        'REFUND'       => 'REFUNDED',
500
        'DISCOUNT'     => 'DISCOUNTED',
501
        'CANCELLATION' => 'CANCELLED'
502
    };
520
503
521
    my $reduction;
504
    my $reduction;
522
    $self->_result->result_source->schema->txn_do(
505
    $self->_result->result_source->schema->txn_do(
523
- 

Return to bug 28656