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

(-)a/Koha/REST/V1/Patrons/Account.pm (-1 / +7 lines)
Lines 220-234 sub add_debit { Link Here
220
    }
220
    }
221
221
222
    return try {
222
    return try {
223
        my $data = Koha::Patron::Account::Debit->new_from_api(
223
        my $data = Koha::Account::Debit->new_from_api(
224
            $c->validation->param('body') )->unblessed;
224
            $c->validation->param('body') )->unblessed;
225
225
226
        $data->{library_id} = $data->{branchcode};
227
        $data->{type} = $data->{debit_type_code};
228
        $data->{cash_register} = $data->{register_id};
229
        $data->{item_id} = $data->{itemnumber};
226
        $data->{interface} = 'api'; # Should this always be API, or should we allow the API consumer to choose?
230
        $data->{interface} = 'api'; # Should this always be API, or should we allow the API consumer to choose?
227
        $data->{user_id} = $patron->borrowernumber; # Should this be API user OR staff the API may be acting on behalf of?
231
        $data->{user_id} = $patron->borrowernumber; # Should this be API user OR staff the API may be acting on behalf of?
228
232
229
        my $debit = $patron->account->add_debit($data);
233
        my $debit = $patron->account->add_debit($data);
230
234
231
        $c->res->headers->location( $c->req->url->to_string . '/' . $debit->id );
235
        $c->res->headers->location( $c->req->url->to_string . '/' . $debit->id );
236
        $debit->discard_changes;
237
232
        return $c->render(
238
        return $c->render(
233
            status  => 201,
239
            status  => 201,
234
            openapi => $debit->to_api
240
            openapi => $debit->to_api
(-)a/api/v1/swagger/definitions/debit.yaml (-1 lines)
Lines 32-38 properties: Link Here
32
    type:
32
    type:
33
      - string
33
      - string
34
      - "null"
34
      - "null"
35
    readOnly: true
36
    description: Account line description
35
    description: Account line description
37
  account_type:
36
  account_type:
38
    type:
37
    type:
(-)a/api/v1/swagger/paths/patrons_account.yaml (-1 / +1 lines)
Lines 190-196 Link Here
190
        description: A JSON object containing debit information
190
        description: A JSON object containing debit information
191
        required: true
191
        required: true
192
        schema:
192
        schema:
193
          $ref: "../swagger.yaml#/definitions/patron_account_debit"
193
          $ref: "../swagger.yaml#/definitions/debit"
194
    produces:
194
    produces:
195
      - application/json
195
      - application/json
196
    responses:
196
    responses:
(-)a/t/db_dependent/api/v1/patrons_accounts.t (-2 / +72 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 2;
20
use Test::More tests => 3;
21
21
22
use Test::Mojo;
22
use Test::Mojo;
23
23
Lines 268-270 subtest 'add_credit() tests' => sub { Link Here
268
268
269
    $schema->storage->txn_rollback;
269
    $schema->storage->txn_rollback;
270
};
270
};
271
- 
271
272
subtest 'add_debit() tests' => sub {
273
274
    plan tests => 13;
275
276
    $schema->storage->txn_begin;
277
278
    my $patron = $builder->build_object({
279
        class => 'Koha::Patrons',
280
        value => { flags => 1 }
281
    });
282
    my $password = 'thePassword123';
283
    $patron->set_password({ password => $password, skip_validation => 1 });
284
    my $userid = $patron->userid;
285
286
    my $library   = $builder->build_object({ class => 'Koha::Libraries' });
287
    my $patron_id = $patron->id;
288
    my $account   = $patron->account;
289
290
    is( $account->outstanding_debits->count,  0, 'No outstanding debits for patron' );
291
    is( $account->outstanding_credits->count, 0, 'No outstanding credits for patron' );
292
293
    my $debit = {
294
        amount      => 100,
295
        description => "A description",
296
        debit_type  => "NEW_CARD",
297
        user_id     => $patron->borrowernumber,
298
        interface   => 'test',
299
        library_id  => $library->id,
300
    };
301
302
    my $ret = $t->post_ok("//$userid:$password@/api/v1/patrons/$patron_id/account/debits" => json => $debit)
303
      ->status_is(201)->tx->res->json;
304
    my $account_line = Koha::Account::Lines->find( $ret->{account_line_id} );
305
306
    is_deeply( $ret, $account_line->to_api, 'Line returned correctly' );
307
308
    is( $account_line->branchcode,
309
        $library->id, 'Library id is sored correctly' );
310
311
    my $outstanding_debits = $account->outstanding_debits;
312
    is( $outstanding_debits->count,             1 );
313
    is( $outstanding_debits->total_outstanding, 100 );
314
315
    my $credit_1 = $account->add_credit(
316
        {
317
            amount    => 10,
318
            interface => 'test',
319
        }
320
    )->store()->apply({ debits => [ $account_line ] });
321
    my $credit_2 = $account->add_credit(
322
        {
323
            amount    => 15,
324
            interface => 'test'
325
        }
326
    )->store()->apply({ debits => [ $account_line ]});
327
328
    is( $account->outstanding_credits->total_outstanding, 0 );
329
    is( $account->outstanding_debits->total_outstanding,
330
        75, "Credits partially cancelled debit" );
331
332
    my $account_line_id = $ret->{account_line_id};
333
334
    $t->post_ok("//$userid:$password@/api/v1/patrons/$patron_id/account/debits" => json => $debit)
335
      ->status_is(201);
336
337
    is( $account->outstanding_debits->total_outstanding,
338
        175, "Debit added to total outstanding debits" );
339
340
    $schema->storage->txn_rollback;
341
};

Return to bug 21043