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

(-)a/Koha/Account/Line.pm (-1 / +106 lines)
Lines 189-194 sub void { Link Here
189
189
190
}
190
}
191
191
192
=head3 reduce
193
194
  $charge_accountline->reduce({
195
      reduction_type => $reduction_type
196
  });
197
198
Used to 'reduce' a charge/debit by adding a credit to offset against the amount
199
outstanding.
200
201
May be used to apply a discount whilst retaining the original debit amounts or
202
to apply a full or partial refund for example when a lost item is found and
203
returned.
204
205
It will immediately be applied to the given debit unless the debit has already
206
been paid, in which case a 'zero' offset will be added to maintain a link to
207
the debit but the outstanding credit will be left so it may be applied to other
208
debts.
209
210
Reduction type may be one of:
211
212
* DISCOUNT
213
* REFUND
214
215
Returns the reduction accountline (which will be a credit)
216
217
=cut
218
219
sub reduce {
220
    my ( $self, $params ) = @_;
221
222
    # Make sure it is a charge we are reducing
223
    unless ( $self->is_debit ) {
224
        Koha::Exceptions::Account::IsNotDebit->throw(
225
            error => 'Account line ' . $self->id . 'is not a debit' );
226
    }
227
228
    unless ( $params->{interface} ) {
229
        Koha::Exceptions::MissingParameter->throw(
230
            error => 'The interface parameter is mandatory'
231
        );
232
    }
233
234
    my $status = { 'REFUND' => 'REFUNDED', 'DISCOUNT' => 'DISCOUNTED' };
235
236
    $params->{branch} //= $self->branchcode;
237
238
    my $reduction;
239
    $self->_result->result_source->schema->txn_do(
240
        sub {
241
242
            # A 'reduction' is a 'credit'
243
            $reduction = Koha::Account::Line->new(
244
                {
245
                    date              => \'NOW()',
246
                    amount            => 0 - $params->{amount},
247
                    credit_type_code  => $params->{reduction_type},
248
                    status            => 'ADDED',
249
                    amountoutstanding => 0 - $params->{amount},
250
                    manager_id        => $params->{staff_id},
251
                    borrowernumber    => $self->borrowernumber,
252
                    interface         => $params->{interface},
253
                    branchcode        => $params->{branch},
254
                }
255
            )->store();
256
257
            my $reduction_offset = Koha::Account::Offset->new(
258
                {
259
                    credit_id => $reduction->accountlines_id,
260
                    type      => uc( $params->{reduction_type} ),
261
                    amount    => $params->{amount}
262
                }
263
            )->store();
264
265
            # Link reduction to charge (and apply as required)
266
            my $debit_outstanding = $self->amountoutstanding;
267
            if ( $debit_outstanding >= $params->{amount} ) {
268
269
                my $credit_outstanding = $reduction->apply(
270
                    {
271
                        debits      => [$self],
272
                        offset_type => uc( $params->{reduction_type} )
273
                    }
274
                );
275
                $reduction->status('APPLIED')->store();
276
            }
277
            else {
278
279
        # Zero amount offset used to link original 'debit' to reduction 'credit'
280
                my $link_reduction_offset = Koha::Account::Offset->new(
281
                    {
282
                        credit_id => $reduction->accountlines_id,
283
                        debit_id  => $self->accountlines_id,
284
                        type      => uc( $params->{reduction_type} ),
285
                        amount    => 0
286
                    }
287
                )->store();
288
            }
289
290
            # Update status of original debit
291
            $self->status( $status->{ $params->{reduction_type} } )->store;
292
        }
293
    );
294
295
    return $reduction;
296
}
297
192
=head3 apply
298
=head3 apply
193
299
194
    my $debits = $account->outstanding_debits;
300
    my $debits = $account->outstanding_debits;
195
- 

Return to bug 23442