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

(-)a/Koha/REST/V1/Patrons/Account.pm (-11 / +19 lines)
Lines 45-62 sub get { Link Here
45
        return $c->render( status => 404, openapi => { error => "Patron not found." } );
45
        return $c->render( status => 404, openapi => { error => "Patron not found." } );
46
    }
46
    }
47
47
48
    my $account = $patron->account;
48
    my $balance;
49
    my $balance;
49
50
50
    $balance->{balance} = $patron->account->balance;
51
    $balance->{balance} = $account->balance;
51
52
52
    my @outstanding_lines = Koha::Account::Lines->search(
53
    # get outstanding debits
53
        {   borrowernumber    => $patron->borrowernumber,
54
    my ( $debits_total,  $debits )  = $account->outstanding_debits;
54
            amountoutstanding => { '!=' => 0 }
55
    my ( $credits_total, $credits ) = $account->outstanding_credits;
55
        }
56
56
    );
57
    my @debit_lines = map { _to_api( $_->TO_JSON ) } @{ $debits->as_list };
57
    foreach my $line ( @outstanding_lines ) {
58
    $balance->{outstanding_debits} = {
58
        push @{ $balance->{outstanding_lines} },  _to_api($line->TO_JSON)
59
        total => $debits_total,
59
    }
60
        lines => \@debit_lines
61
    };
62
63
    my @credit_lines = map { _to_api( $_->TO_JSON ) } @{ $credits->as_list };
64
    $balance->{outstanding_credits} = {
65
        total => $credits_total,
66
        lines => \@credit_lines
67
    };
60
68
61
    return $c->render( status => 200, openapi => $balance );
69
    return $c->render( status => 200, openapi => $balance );
62
}
70
}
Lines 135-141 our $to_api_mapping = { Link Here
135
    dispute           => undef,
143
    dispute           => undef,
136
    issue_id          => 'checkout_id',
144
    issue_id          => 'checkout_id',
137
    itemnumber        => 'item_id',
145
    itemnumber        => 'item_id',
138
    manager_id        => 'staff_id',
146
    manager_id        => 'user_id',
139
    note              => 'internal_note',
147
    note              => 'internal_note',
140
};
148
};
141
149
Lines 151-157 our $to_model_mapping = { Link Here
151
    internal_note      => 'note',
159
    internal_note      => 'note',
152
    item_id            => 'itemnumber',
160
    item_id            => 'itemnumber',
153
    patron_id          => 'borrowernumber',
161
    patron_id          => 'borrowernumber',
154
    staff_id           => 'manager_id'
162
    user_id            => 'manager_id'
155
};
163
};
156
164
157
1;
165
1;
(-)a/api/v1/swagger/definitions/account_line.json (-1 / +1 lines)
Lines 73-79 Link Here
73
      ],
73
      ],
74
      "description": "Internal note"
74
      "description": "Internal note"
75
    },
75
    },
76
    "staff_id": {
76
    "user_id": {
77
      "type": "integer",
77
      "type": "integer",
78
      "description": "Internal patron identifier for the staff member that introduced the account line"
78
      "description": "Internal patron identifier for the staff member that introduced the account line"
79
    }
79
    }
(-)a/api/v1/swagger/definitions/patron_balance.json (-4 / +25 lines)
Lines 5-14 Link Here
5
      "type": "number",
5
      "type": "number",
6
      "description": "Signed decimal number"
6
      "description": "Signed decimal number"
7
    },
7
    },
8
    "outstanding_lines": {
8
    "outstanding_credits": {
9
      "type": "array",
9
      "properties": {
10
      "items": {
10
        "total": {
11
        "$ref": "account_line.json"
11
            "type": "number"
12
        },
13
        "lines": {
14
            "type": "array",
15
            "items": {
16
                "$ref": "account_line.json"
17
            }
18
        }
19
      }
20
    },
21
    "outstanding_debits": {
22
      "type": "object",
23
      "properties": {
24
        "total": {
25
            "type": "number"
26
        },
27
        "lines": {
28
            "type": "array",
29
            "items": {
30
                "$ref": "account_line.json"
31
            }
32
        }
12
      }
33
      }
13
    }
34
    }
14
  },
35
  },
(-)a/t/db_dependent/api/v1/patrons_accounts.t (-14 / +48 lines)
Lines 41-59 my $t = Test::Mojo->new('Koha::REST::V1'); Link Here
41
41
42
subtest 'get_balance() tests' => sub {
42
subtest 'get_balance() tests' => sub {
43
43
44
    plan tests => 9;
44
    plan tests => 12;
45
45
46
    $schema->storage->txn_begin;
46
    $schema->storage->txn_begin;
47
47
48
    my ( $patron_id, $session_id ) = create_user_and_session({ authorized => 0 });
48
    my ( $patron_id, $session_id ) = create_user_and_session({ authorized => 0 });
49
    my $patron = Koha::Patrons->find($patron_id);
49
    my $patron  = Koha::Patrons->find($patron_id);
50
    my $account = $patron->account;
50
51
51
    my $tx = $t->ua->build_tx(GET => "/api/v1/patrons/$patron_id/account");
52
    my $tx = $t->ua->build_tx(GET => "/api/v1/patrons/$patron_id/account");
52
    $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
53
    $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
53
    $tx->req->env({ REMOTE_ADDR => '127.0.0.1' });
54
    $tx->req->env({ REMOTE_ADDR => '127.0.0.1' });
54
    $t->request_ok($tx)
55
    $t->request_ok($tx)->status_is(200)->json_is(
55
      ->status_is(200)
56
        {   balance             => 0.00,
56
      ->json_is( { balance => 0.00 } );
57
            outstanding_debits  => { total => 0, lines => [] },
58
            outstanding_credits => { total => 0, lines => [] }
59
        }
60
    );
57
61
58
    my $account_line_1 = Koha::Account::Line->new(
62
    my $account_line_1 = Koha::Account::Line->new(
59
        {
63
        {
Lines 85-100 subtest 'get_balance() tests' => sub { Link Here
85
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
89
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
86
    $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
90
    $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
87
    $t->request_ok($tx)->status_is(200)->json_is(
91
    $t->request_ok($tx)->status_is(200)->json_is(
88
        {   balance           => 100.01,
92
        {   balance            => 100.01,
89
            outstanding_lines => [
93
            outstanding_debits => {
90
                Koha::REST::V1::Patrons::Account::_to_api( $account_line_1->TO_JSON ),
94
                total => 100.01,
91
                Koha::REST::V1::Patrons::Account::_to_api( $account_line_2->TO_JSON )
95
                lines => [
92
96
                    Koha::REST::V1::Patrons::Account::_to_api( $account_line_1->TO_JSON ),
93
            ]
97
                    Koha::REST::V1::Patrons::Account::_to_api( $account_line_2->TO_JSON )
98
                ]
99
            },
100
            outstanding_credits => {
101
                total => 0,
102
                lines => []
103
            }
94
        }
104
        }
95
    );
105
    );
96
106
97
    Koha::Account->new({ patron_id => $patron_id })->pay(
107
    $account->pay(
98
        {   amount       => 100.01,
108
        {   amount       => 100.01,
99
            note         => 'He paid!',
109
            note         => 'He paid!',
100
            description  => 'Finally!',
110
            description  => 'Finally!',
Lines 107-113 subtest 'get_balance() tests' => sub { Link Here
107
    $tx = $t->ua->build_tx( GET => "/api/v1/patrons/$patron_id/account" );
117
    $tx = $t->ua->build_tx( GET => "/api/v1/patrons/$patron_id/account" );
108
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
118
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
109
    $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
119
    $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
110
    $t->request_ok($tx)->status_is(200)->json_is( { balance => 0 } );
120
    $t->request_ok($tx)->status_is(200)->json_is(
121
        {   balance             => 0,
122
            outstanding_debits  => { total => 0, lines => [] },
123
            outstanding_credits => { total => 0, lines => [] }
124
        }
125
    );
126
127
    # add a credit
128
    my $credit_line = $account->add_credit({ amount => 10, user_id => $patron->id });
129
    # re-read from the DB
130
    $credit_line->discard_changes;
131
    $tx = $t->ua->build_tx( GET => "/api/v1/patrons/$patron_id/account" );
132
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
133
    $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
134
    $t->request_ok($tx)->status_is(200)->json_is(
135
        {   balance            => -10,
136
            outstanding_debits => {
137
                total => 0,
138
                lines => []
139
            },
140
            outstanding_credits => {
141
                total => -10,
142
                lines => [ Koha::REST::V1::Patrons::Account::_to_api( $credit_line->TO_JSON ) ]
143
            }
144
        }
145
    );
111
146
112
    $schema->storage->txn_rollback;
147
    $schema->storage->txn_rollback;
113
};
148
};
114
- 

Return to bug 20942