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

(-)a/Koha/Cash/Register/Cashup.pm (+166 lines)
Line 0 Link Here
1
package Koha::Cash::Register::Cashup;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Carp;
21
22
use Koha::Database;
23
24
use base qw(Koha::Cash::Register::Action);
25
26
=head1 NAME
27
28
Koha::Cash::Register::Actions - Koha Cash Register Action Object set class
29
30
=head1 API
31
32
=head2 Class methods
33
34
=head3 search
35
36
    my $cashup = Koha::Cash::Register::Actions::Cashup->search( $where, $attr );
37
38
Returns a list of cash register cashup.
39
40
=cut
41
42
sub search {
43
    my ( $self, $where, $attr ) = @_;
44
45
    $where->{code} = 'CASHUP';
46
47
    unless ( exists $attr->{order_by} ) {
48
        $attr->{order_by} =
49
          [ { '-asc' => 'register_id' }, { '-desc' => 'timestamp' } ];
50
    }
51
52
    return $self->SUPER::search( $where, $attr );
53
}
54
55
=head3 summary
56
57
  my $summary = $cashup->summary;
58
59
Return a hashref containing a summary of transactions that make up this cashup.
60
61
=cut
62
63
sub summary {
64
    my ($self) = @_;
65
    my $summary;
66
    my $prior_cashup = Koha::Cash::Register::Cashups->search(
67
        {
68
            'timestamp' => { '<' => $self->timestamp },
69
            register_id => $self->register_id
70
        },
71
        {
72
            order_by => { '-desc' => [ 'timestamp', 'id' ] },
73
            rows     => 1
74
        }
75
    );
76
77
    my $previous = $prior_cashup->single;
78
79
    my $conditions =
80
      $previous
81
      ? {
82
        'date' => {
83
            '-between' =>
84
              [ $previous->_result->get_column('timestamp'), $self->timestamp ]
85
        }
86
      }
87
      : { 'date' => { '<' => $self->timestamp } };
88
89
    my $outgoing_transactions = $self->register->accountlines->search(
90
        { %{$conditions}, credit_type_code => undef },
91
    );
92
    my $income_transactions = $self->register->accountlines->search(
93
        { %{$conditions}, debit_type_code => undef },
94
    );
95
96
    my $income_summary = Koha::Account::Offsets->search(
97
        {
98
            'me.credit_id' =>
99
              { '-in' => $income_transactions->_resultset->get_column('accountlines_id')->as_query },
100
            'me.debit_id' => { '!=' => undef }
101
        },
102
        {
103
            join     => { 'debit' => 'debit_type_code' },
104
            group_by => [ 'debit.debit_type_code', 'debit_type_code.description' ],
105
            'select' => [ { sum => 'me.amount' }, 'debit.debit_type_code', 'debit_type_code.description' ],
106
            'as'     => [ 'total', 'debit_type_code', 'debit_description' ],
107
        }
108
    );
109
110
    my $outgoing_summary = Koha::Account::Offsets->search(
111
        {
112
            'me.debit_id' =>
113
              { '-in' => $outgoing_transactions->_resultset->get_column('accountlines_id')->as_query },
114
            'me.credit_id' => { '!=' => undef }
115
        },
116
        {
117
            join     => { 'credit' => 'credit_type_code' },
118
            group_by => [ 'credit.credit_type_code', 'credit_type_code.description' ],
119
            'select' => [ { sum => 'me.amount' }, 'credit.credit_type_code', 'credit_type_code.description' ],
120
            'as'     => [ 'total', 'credit_type_code', 'credit_description' ],
121
        }
122
    );
123
124
    my @income = map {
125
        {
126
            total           => $_->get_column('total'),
127
            debit_type_code => $_->get_column('debit_type_code'),
128
            debit_type      => { description => $_->get_column('debit_description') }
129
        }
130
    } $income_summary->as_list;
131
    my @outgoing = map {
132
        {
133
            total            => $_->get_column('total'),
134
            credit_type_code => $_->get_column('credit_type_code'),
135
            credit_type      => { description => $_->get_column('credit_description') }
136
        }
137
    } $outgoing_summary->as_list;
138
139
    $summary = {
140
        from_date             => $previous ? $previous->timestamp : undef,
141
        to_date               => $self->timestamp,
142
        income                => \@income,
143
        outgoing              => \@outgoing,
144
        income_transactions   => $income_transactions,
145
        outgoing_transactions => $outgoing_transactions,
146
    };
147
148
    return $summary;
149
}
150
151
=head3 to_api_mapping
152
153
This method returns the mapping for representing a Koha::Cash::Regiser::Cashup object
154
on the API.
155
156
=cut
157
158
sub to_api_mapping {
159
    return {
160
        id          => 'cashup_id',
161
        register_id => 'cash_register_id',
162
        code        => undef
163
    };
164
}
165
166
1;
(-)a/Koha/Cash/Register/Cashups.pm (+64 lines)
Line 0 Link Here
1
package Koha::Cash::Register::Cashups;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Carp;
21
22
use Koha::Database;
23
use Koha::Cash::Register::Cashup;
24
25
use base qw(Koha::Cash::Register::Actions);
26
27
=head1 NAME
28
29
Koha::Cash::Register::Actions - Koha Cash Register Action Object set class
30
31
=head1 API
32
33
=head2 Class methods
34
35
=head3 search
36
37
    my $cashups = Koha::Cash::Register::Cashups->search( $where, $attr );
38
39
Returns a list of cash register cashups.
40
41
=cut
42
43
sub search {
44
    my ( $self, $where, $attr ) = @_;
45
46
    $where->{code} = 'CASHUP';
47
48
    unless ( exists $attr->{order_by} ) {
49
        $attr->{order_by} =
50
          [ { '-asc' => 'register_id' }, { '-desc' => 'timestamp' } ];
51
    }
52
53
    return $self->SUPER::search( $where, $attr );
54
}
55
56
=head3 object_class
57
58
=cut
59
60
sub object_class {
61
    return 'Koha::Cash::Register::Cashup';
62
}
63
64
1;
(-)a/Koha/REST/V1/CashRegisters/Cashups.pm (+75 lines)
Line 0 Link Here
1
package Koha::REST::V1::CashRegisters::Cashups;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Mojo::Base 'Mojolicious::Controller';
21
22
use Scalar::Util qw(blessed);
23
use Try::Tiny;
24
25
use Koha::Cash::Register::Cashups;
26
27
=head1 NAME
28
29
Koha::REST::V1::CashRegisters::Cashups
30
31
=head1 API
32
33
=head2 Methods
34
35
=head3 list
36
37
=cut
38
39
sub list {
40
    my $c = shift->openapi->valid_input or return;
41
42
    return try {
43
        my $cashups_set = Koha::Cash::Register::Cashups->new;
44
        my $cashups = $c->objects->search( $cashups_set );
45
        return $c->render( status => 200, openapi => $cashups );
46
    }
47
    catch {
48
        $c->unhandled_exception($_);
49
    };
50
}
51
52
=head3 get
53
54
Controller function that handles retrieving a cash register cashup
55
56
=cut
57
58
sub get {
59
    my $c = shift->openapi->valid_input or return;
60
61
    return try {
62
        my $cashup = Koha::Cash::Register::Cashups->find( $c->validation->param('cashup_id') );
63
        unless ($cashup) {
64
            return $c->render( status  => 404,
65
                            openapi => { error => "Cashup not found" } );
66
        }
67
68
        return $c->render( status => 200, openapi => $cashup->to_api );
69
    }
70
    catch {
71
        $c->unhandled_exception($_);
72
    }
73
}
74
75
1;
(-)a/api/v1/swagger/definitions.json (+3 lines)
Lines 5-10 Link Here
5
  "basket": {
5
  "basket": {
6
    "$ref": "definitions/basket.json"
6
    "$ref": "definitions/basket.json"
7
  },
7
  },
8
  "cashup": {
9
    "$ref": "definitions/cashup.json"
10
  },
8
  "circ-rule-kind": {
11
  "circ-rule-kind": {
9
    "$ref": "definitions/circ-rule-kind.json"
12
    "$ref": "definitions/circ-rule-kind.json"
10
  },
13
  },
(-)a/api/v1/swagger/definitions/cashup.json (+26 lines)
Line 0 Link Here
1
{
2
    "type": "object",
3
    "properties": {
4
        "cashup_id": {
5
            "type": "integer",
6
            "description": "Internal cashup identifier"
7
        },
8
        "cash_register_id": {
9
            "type": "integer",
10
            "description": "Internal identifier for the register the cashup belongs to"
11
        },
12
        "manager_id": {
13
            "type": "integer",
14
            "description": "Internal identifier for the manager the cashup was performed by"
15
        },
16
        "amount": {
17
            "type": "number",
18
            "description": "Account line amount"
19
        },
20
        "timestamp": {
21
            "type": "string",
22
            "format": "date-time",
23
            "description": "Timestamp for the latest line update"
24
        }
25
    }
26
}
(-)a/api/v1/swagger/parameters.json (+6 lines)
Lines 44-49 Link Here
44
  "seen_pp": {
44
  "seen_pp": {
45
    "$ref": "parameters/checkout.json#/seen_pp"
45
    "$ref": "parameters/checkout.json#/seen_pp"
46
  },
46
  },
47
  "cash_register_id_pp": {
48
    "$ref": "parameters/cash_register.json#/cash_register_id_pp"
49
  },
50
  "cashup_id_pp": {
51
    "$ref": "parameters/cashup.json#/cashup_id_pp"
52
  },
47
  "match": {
53
  "match": {
48
    "name": "_match",
54
    "name": "_match",
49
    "in": "query",
55
    "in": "query",
(-)a/api/v1/swagger/parameters/cash_register.json (+9 lines)
Line 0 Link Here
1
{
2
    "cash_register_id_pp": {
3
      "name": "cash_register_id",
4
      "in": "path",
5
      "description": "Cash register internal identifier",
6
      "required": true,
7
      "type": "integer"
8
    }
9
}
(-)a/api/v1/swagger/parameters/cashup.json (+9 lines)
Line 0 Link Here
1
{
2
    "cashup_id_pp": {
3
      "name": "cashup_id",
4
      "in": "path",
5
      "description": "Cashup internal identifier",
6
      "required": true,
7
      "type": "integer"
8
    }
9
}
(-)a/api/v1/swagger/paths.json (+6 lines)
Lines 17-22 Link Here
17
  "/acquisitions/funds": {
17
  "/acquisitions/funds": {
18
    "$ref": "paths/acquisitions_funds.json#/~1acquisitions~1funds"
18
    "$ref": "paths/acquisitions_funds.json#/~1acquisitions~1funds"
19
  },
19
  },
20
  "/cash_registers/{cash_register_id}/cashups": {
21
    "$ref": "paths/cash_registers.json#/~1cash_registers~1{cash_register_id}~1cashups"
22
  },
23
  "/cashups/{cashup_id}": {
24
    "$ref": "paths/cash_registers.json#/~1cashups~1{cashup_id}"
25
  },
20
  "/checkouts": {
26
  "/checkouts": {
21
    "$ref": "paths/checkouts.json#/~1checkouts"
27
    "$ref": "paths/checkouts.json#/~1checkouts"
22
  },
28
  },
(-)a/api/v1/swagger/paths/cash_registers.json (-1 / +101 lines)
Line 0 Link Here
0
- 
1
{
2
    "/cash_registers/{cash_register_id}/cashups": {
3
        "get": {
4
            "x-mojo-to": "CashRegisters::Cashups#list",
5
            "operationId": "listCashups",
6
            "tags": ["cash_registers", "cashups"],
7
            "produces": ["application/json"],
8
            "parameters": [{
9
                    "$ref": "../parameters.json#/cash_register_id_pp"
10
                },
11
                {
12
                    "$ref": "../parameters.json#/match"
13
                },
14
                {
15
                    "$ref": "../parameters.json#/order_by"
16
                },
17
                {
18
                    "$ref": "../parameters.json#/page"
19
                },
20
                {
21
                    "$ref": "../parameters.json#/per_page"
22
                },
23
                {
24
                    "$ref": "../parameters.json#/q_param"
25
                },
26
                {
27
                    "$ref": "../parameters.json#/q_body"
28
                },
29
                {
30
                    "$ref": "../parameters.json#/q_header"
31
                }
32
            ],
33
            "responses": {
34
                "200": {
35
                    "description": "Cashups performed on this register",
36
                    "schema": {
37
                        "type": "array",
38
                        "items": {
39
                            "$ref": "../definitions.json#/cashup"
40
                        }
41
                    }
42
                },
43
                "403": {
44
                    "description": "Access forbidden",
45
                    "schema": {
46
                        "$ref": "../definitions.json#/error"
47
                    }
48
                },
49
                "404": {
50
                    "description": "Register not found",
51
                    "schema": {
52
                        "$ref": "../definitions.json#/error"
53
                    }
54
                }
55
            },
56
            "x-koha-authorization": {
57
                "permissions": {
58
                    "cash_management": "cashup"
59
                }
60
            }
61
        }
62
    },
63
    "/cashups/{cashup_id}": {
64
        "get": {
65
            "x-mojo-to": "CashRegisters::Cashups#get",
66
            "operationId": "getCashup",
67
            "tags": ["cash_registers", "cashups"],
68
            "parameters": [{
69
                "$ref": "../parameters.json#/cashup_id_pp"
70
            }],
71
            "produces": [
72
                "application/json"
73
            ],
74
            "responses": {
75
                "200": {
76
                    "description": "A cashup",
77
                    "schema": {
78
                        "$ref": "../definitions.json#/cashup"
79
                    }
80
                },
81
                "403": {
82
                    "description": "Access forbidden",
83
                    "schema": {
84
                        "$ref": "../definitions.json#/error"
85
                    }
86
                },
87
                "404": {
88
                    "description": "Patron not found",
89
                    "schema": {
90
                        "$ref": "../definitions.json#/error"
91
                    }
92
                }
93
            },
94
            "x-koha-authorization": {
95
                "permissions": {
96
                    "cash_management": "cashup"
97
                }
98
            }
99
        }
100
    }
101
}

Return to bug 26274