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

(-)a/Koha/Cash/Register.pm (-1 / +115 lines)
Lines 19-24 use Modern::Perl; Link Here
19
19
20
use Carp;
20
use Carp;
21
21
22
use Koha::Account::Lines;
23
use Koha::Account::Offsets;
24
use Koha::Cash::Register::Actions;
22
use Koha::Database;
25
use Koha::Database;
23
26
24
use base qw(Koha::Object);
27
use base qw(Koha::Object);
Lines 48-53 sub library { Link Here
48
    return Koha::Library->_new_from_dbic($rs);
51
    return Koha::Library->_new_from_dbic($rs);
49
}
52
}
50
53
54
=head3 cashups
55
56
Return a set of cashup actions linked to this cash register
57
58
=cut
59
60
sub cashups {
61
    my ( $self, $conditions, $attrs ) = @_;
62
63
    my $local_conditions = { code => 'CASHUP' };
64
    $conditions //= {};
65
    my $merged_conditions = { %{$conditions}, %{$local_conditions} };
66
67
    my $rs =
68
      $self->_result->search_related( 'cash_register_actions',
69
        $merged_conditions, $attrs );
70
    return unless $rs;
71
    return Koha::Cash::Register::Actions->_new_from_dbic($rs);
72
}
73
74
=head3 last_cashup
75
76
Return a set of cashup actions linked to this cash register
77
78
=cut
79
80
sub last_cashup {
81
    my ( $self, $conditions, $attrs ) = @_;
82
83
    my $rs = $self->_result->search_related(
84
        'cash_register_actions',
85
        { code     => 'CASHUP' },
86
        { order_by => { '-desc' => [ 'timestamp', 'id' ] }, rows => 1 }
87
    )->single;
88
89
    return unless $rs;
90
    return Koha::Cash::Register::Action->_new_from_dbic($rs);
91
}
92
93
=head3 accountlines
94
95
Return a set of accountlines linked to this cash register
96
97
=cut
98
99
sub accountlines {
100
    my ($self) = @_;
101
102
    my $rs = $self->_result->accountlines;
103
    return unless $rs;
104
    return Koha::Account::Lines->_new_from_dbic($rs);
105
}
106
107
=head3 outstanding_accountlines
108
109
  my $lines = Koha::Cash::Registers->find($id)->outstanding_accountlines;
110
111
Return a set of accountlines linked to this cash register since the last cashup action
112
113
=cut
114
115
sub outstanding_accountlines {
116
    my ( $self, $conditions, $attrs ) = @_;
117
118
    my $since = $self->_result->search_related(
119
        'cash_register_actions',
120
        { 'code' => 'CASHUP' },
121
        {
122
            order_by => { '-desc' => [ 'timestamp', 'id' ] },
123
            rows     => 1
124
        }
125
    );
126
127
    my $local_conditions =
128
      $since->count
129
      ? { 'timestamp' => { '>=' => $since->get_column('timestamp')->as_query } }
130
      : {};
131
    my $merged_conditions =
132
      $conditions
133
      ? { %{$conditions}, %{$local_conditions} }
134
      : $local_conditions;
135
136
    my $rs =
137
      $self->_result->search_related( 'accountlines', $merged_conditions,
138
        $attrs );
139
    return unless $rs;
140
    return Koha::Account::Lines->_new_from_dbic($rs);
141
}
142
51
=head3 store
143
=head3 store
52
144
53
Local store method to prevent direct manipulation of the 'branch_default' field
145
Local store method to prevent direct manipulation of the 'branch_default' field
Lines 63-69 sub store { Link Here
63
                    property => 'branch_default' );
155
                    property => 'branch_default' );
64
            }
156
            }
65
            else {
157
            else {
66
                if ($self->_result->is_column_changed('branch') && $self->branch_default) {
158
                if (   $self->_result->is_column_changed('branch')
159
                    && $self->branch_default )
160
                {
67
                }
161
                }
68
                $self = $self->SUPER::store;
162
                $self = $self->SUPER::store;
69
            }
163
            }
Lines 113-118 sub drop_default { Link Here
113
    return $self;
207
    return $self;
114
}
208
}
115
209
210
=head3 add_cashup
211
212
Add a new cashup action to the till, returns the added action.
213
214
=cut
215
216
sub add_cashup {
217
    my ( $self, $params ) = @_;
218
219
    my $rs = $self->_result->add_to_cash_register_actions(
220
        {
221
            code       => 'CASHUP',
222
            manager_id => $params->{staff_id},
223
            amount     => $params->{amount}
224
        }
225
    )->discard_changes;
226
227
    return Koha::Cash::Register::Action->_new_from_dbic($rs);
228
}
229
116
=head2 Internal methods
230
=head2 Internal methods
117
231
118
=cut
232
=cut
(-)a/Koha/Cash/Register/Action.pm (+69 lines)
Line 0 Link Here
1
package Koha::Cash::Register::Action;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it under the
6
# terms of the GNU General Public License as published by the Free Software
7
# Foundation; either version 3 of the License, or (at your option) any later
8
# version.
9
#
10
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License along
15
# with Koha; if not, write to the Free Software Foundation, Inc.,
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18
use Modern::Perl;
19
20
use Carp;
21
22
use Koha::Database;
23
24
use base qw(Koha::Object);
25
26
=encoding utf8
27
28
=head1 NAME
29
30
Koha::Cash::Register::Action - Koha cashregister::action Object class
31
32
=head1 API
33
34
=head2 Class methods
35
36
=cut
37
38
=head3 manager
39
40
Return the manager linked to this cash register::action
41
42
=cut
43
44
sub manager {
45
    my ($self) = @_;
46
    my $rs = $self->_result->manager;
47
    return unless $rs;
48
    return Koha::Patron->_new_from_dbic($rs);
49
}
50
51
=head2 Internal methods
52
53
=cut
54
55
=head3 _type
56
57
=cut
58
59
sub _type {
60
    return 'CashRegisterAction';
61
}
62
63
1;
64
65
=head1 AUTHORS
66
67
Martin Renvoize <martin.renvoize@ptfs-europe.com>
68
69
=cut
(-)a/Koha/Cash/Register/Actions.pm (+58 lines)
Line 0 Link Here
1
package Koha::Cash::Register::Actions;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it under the
6
# terms of the GNU General Public License as published by the Free Software
7
# Foundation; either version 3 of the License, or (at your option) any later
8
# version.
9
#
10
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License along
15
# with Koha; if not, write to the Free Software Foundation, Inc.,
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18
use Modern::Perl;
19
20
use Carp;
21
22
use Koha::Database;
23
24
use Koha::Cash::Register::Action;
25
26
use base qw(Koha::Objects);
27
28
=head1 NAME
29
30
Koha::Cash::Register::Actions - Koha Cash Register Action Object set class
31
32
=head1 API
33
34
=head2 Class methods
35
36
    my $cash_register::actions = Koha::Cash::Register::Actions->search({ ...  });
37
38
Returns a list of cash register::actions.
39
40
=head2 Internal methods
41
42
=head3 _type
43
44
=cut
45
46
sub _type {
47
    return 'CashRegisterAction';
48
}
49
50
=head3 object_class
51
52
=cut
53
54
sub object_class {
55
    return 'Koha::Cash::Register::Action';
56
}
57
58
1;
(-)a/t/db_dependent/Koha/Cash/Register.t (-7 / +139 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 2;
22
use Test::More tests => 4;
23
23
24
use Test::Exception;
24
use Test::Exception;
25
25
Lines 54-59 subtest 'library' => sub { Link Here
54
    $schema->storage->txn_rollback;
54
    $schema->storage->txn_rollback;
55
};
55
};
56
56
57
subtest 'accountlines' => sub {
58
    plan tests => 3;
59
60
    $schema->storage->txn_begin;
61
62
    my $register =
63
      $builder->build_object( { class => 'Koha::Cash::Registers' } );
64
    my $accountline1 = $builder->build_object(
65
        {
66
            class => 'Koha::Account::Lines',
67
            value => { register_id => $register->id },
68
        }
69
    );
70
    my $accountline2 = $builder->build_object(
71
        {
72
            class => 'Koha::Account::Lines',
73
            value => { register_id => $register->id },
74
        }
75
    );
76
77
    my $accountlines = $register->accountlines;
78
    is( ref($accountlines), 'Koha::Account::Lines',
79
'Koha::Cash::Register->accountlines should return a set of Koha::Account::Lines'
80
    );
81
    is( $accountlines->count, 2,
82
'Koha::Cash::Register->accountlines should return the correct number of accountlines'
83
    );
84
85
    $accountline1->delete;
86
    is( $register->accountlines->next->id, $accountline2->id,
87
'Koha::Cash::Register->accountlines should return the correct acocuntlines'
88
    );
89
90
    $schema->storage->txn_rollback;
91
};
92
57
subtest 'branch_default' => sub {
93
subtest 'branch_default' => sub {
58
    plan tests => 3;
94
    plan tests => 3;
59
95
Lines 89-109 subtest 'branch_default' => sub { Link Here
89
    subtest 'make_default' => sub {
125
    subtest 'make_default' => sub {
90
        plan tests => 3;
126
        plan tests => 3;
91
127
92
        ok($register2->make_default,'Koha::Register->make_default ran');
128
        ok( $register2->make_default, 'Koha::Register->make_default ran' );
93
129
94
        $register1 = $register1->get_from_storage;
130
        $register1 = $register1->get_from_storage;
95
        $register2 = $register2->get_from_storage;
131
        $register2 = $register2->get_from_storage;
96
        is($register1->branch_default, 0, 'register1 was unset as expected');
132
        is( $register1->branch_default, 0, 'register1 was unset as expected' );
97
        is($register2->branch_default, 1, 'register2 was set as expected');
133
        is( $register2->branch_default, 1, 'register2 was set as expected' );
98
    };
134
    };
99
135
100
    subtest 'drop_default' => sub {
136
    subtest 'drop_default' => sub {
101
        plan tests => 2;
137
        plan tests => 2;
102
138
103
        ok($register2->drop_default,'Koha::Register->drop_default ran');
139
        ok( $register2->drop_default, 'Koha::Register->drop_default ran' );
104
140
105
        $register2 = $register2->get_from_storage;
141
        $register2 = $register2->get_from_storage;
106
        is($register2->branch_default, 0, 'register2 was unset as expected');
142
        is( $register2->branch_default, 0, 'register2 was unset as expected' );
143
    };
144
145
    $schema->storage->txn_rollback;
146
};
147
148
subtest 'cashup' => sub {
149
    plan tests => 3;
150
151
    $schema->storage->txn_begin;
152
153
    my $register =
154
      $builder->build_object( { class => 'Koha::Cash::Registers' } );
155
    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
156
157
    my $cashup1;
158
    subtest 'add_cashup' => sub {
159
        plan tests => 6;
160
161
        ok(
162
            $cashup1 = $register->add_cashup(
163
                { user_id => $patron->id, amount => '12.00' }
164
            ),
165
            'call successfull'
166
        );
167
168
        is(
169
            ref($cashup1),
170
            'Koha::Cash::Register::Action',
171
            'return is Koha::Cash::Register::Action'
172
        );
173
        is( $cashup1->code, 'CASHUP',
174
            'CASHUP code set in Koha::Cash::Register::Action' );
175
        is( $cashup1->manager_id, $patron->id,
176
            'manager_id set correctly in Koha::Cash::Register::Action' );
177
        is( $cashup1->amount, '12.000000',
178
            'amount set correctly in Koha::Cash::Register::Action' );
179
        isnt( $cashup1->timestamp, undef,
180
            'timestamp set in Koha::Cash::Register::Action' );
181
    };
182
183
    subtest 'last_cashup' => sub {
184
        plan tests => 3;
185
186
        my $cashup2 =
187
          $register->add_cashup( { user_id => $patron->id, amount => '6.00' } );
188
189
        my $last_cashup = $register->last_cashup;
190
        is(
191
            ref($last_cashup),
192
            'Koha::Cash::Register::Action',
193
            'A cashup was returned when one existed'
194
        );
195
        is( $last_cashup->id, $cashup2->id,
196
            'The most recent cashup was returned' );
197
        $cashup1->delete;
198
        $cashup2->delete;
199
        $last_cashup = $register->last_cashup;
200
        is( $last_cashup, undef, 'undef is returned when no cashup exists' );
201
    };
202
203
    subtest 'outstanding_accountlines' => sub {
204
        plan tests => 4;
205
206
        my $accountline1 = $builder->build_object(
207
            {
208
                class => 'Koha::Account::Lines',
209
                value => { register_id => $register->id },
210
            }
211
        );
212
        my $accountline2 = $builder->build_object(
213
            {
214
                class => 'Koha::Account::Lines',
215
                value => { register_id => $register->id },
216
            }
217
        );
218
219
        my $accountlines = $register->outstanding_accountlines;
220
        is( $accountlines->count, 2, 'No cashup, all accountlines returned' );
221
222
        my $cashup3 =
223
          $register->add_cashup( { user_id => $patron->id, amount => '2.50' } );
224
225
        $accountlines = $register->outstanding_accountlines;
226
        is( $accountlines->count, 0, 'Cashup added, no accountlines returned' );
227
228
        my $accountline3 = $builder->build_object(
229
            {
230
                class => 'Koha::Account::Lines',
231
                value => { register_id => $register->id },
232
            }
233
        );
234
235
        $accountlines = $register->outstanding_accountlines;
236
        is( $accountlines->count, 1,
237
            'Accountline added, one accountline returned' );
238
        is( $accountlines->next->id,
239
            $accountline3->id, 'Correct accountline returned' );
107
    };
240
    };
108
241
109
    $schema->storage->txn_rollback;
242
    $schema->storage->txn_rollback;
110
- 

Return to bug 23355