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

(-)a/Koha/Cash/Register.pm (-1 / +116 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::Action;
25
use Koha::Cash::Register::Actions;
22
use Koha::Database;
26
use Koha::Database;
23
27
24
use base qw(Koha::Object);
28
use base qw(Koha::Object);
Lines 48-53 sub library { Link Here
48
    return Koha::Library->_new_from_dbic($rs);
52
    return Koha::Library->_new_from_dbic($rs);
49
}
53
}
50
54
55
=head3 cashups
56
57
Return a set of cashup actions linked to this cash register
58
59
=cut
60
61
sub cashups {
62
    my ( $self, $conditions, $attrs ) = @_;
63
64
    my $local_conditions = { code => 'CASHUP' };
65
    $conditions //= {};
66
    my $merged_conditions = { %{$conditions}, %{$local_conditions} };
67
68
    my $rs =
69
      $self->_result->search_related( 'cash_register_actions',
70
        $merged_conditions, $attrs );
71
    return unless $rs;
72
    return Koha::Cash::Register::Actions->_new_from_dbic($rs);
73
}
74
75
=head3 last_cashup
76
77
Return a set of cashup actions linked to this cash register
78
79
=cut
80
81
sub last_cashup {
82
    my ( $self, $conditions, $attrs ) = @_;
83
84
    my $rs = $self->_result->search_related(
85
        'cash_register_actions',
86
        { code     => 'CASHUP' },
87
        { order_by => { '-desc' => [ 'timestamp', 'id' ] }, rows => 1 }
88
    )->single;
89
90
    return unless $rs;
91
    return Koha::Cash::Register::Action->_new_from_dbic($rs);
92
}
93
94
=head3 accountlines
95
96
Return a set of accountlines linked to this cash register
97
98
=cut
99
100
sub accountlines {
101
    my ($self) = @_;
102
103
    my $rs = $self->_result->accountlines;
104
    return unless $rs;
105
    return Koha::Account::Lines->_new_from_dbic($rs);
106
}
107
108
=head3 outstanding_accountlines
109
110
  my $lines = Koha::Cash::Registers->find($id)->outstanding_accountlines;
111
112
Return a set of accountlines linked to this cash register since the last cashup action
113
114
=cut
115
116
sub outstanding_accountlines {
117
    my ( $self, $conditions, $attrs ) = @_;
118
119
    my $since = $self->_result->search_related(
120
        'cash_register_actions',
121
        { 'code' => 'CASHUP' },
122
        {
123
            order_by => { '-desc' => [ 'timestamp', 'id' ] },
124
            rows     => 1
125
        }
126
    );
127
128
    my $local_conditions =
129
      $since->count
130
      ? { 'timestamp' => { '>=' => $since->get_column('timestamp')->as_query } }
131
      : {};
132
    my $merged_conditions =
133
      $conditions
134
      ? { %{$conditions}, %{$local_conditions} }
135
      : $local_conditions;
136
137
    my $rs =
138
      $self->_result->search_related( 'accountlines', $merged_conditions,
139
        $attrs );
140
    return unless $rs;
141
    return Koha::Account::Lines->_new_from_dbic($rs);
142
}
143
51
=head3 store
144
=head3 store
52
145
53
Local store method to prevent direct manipulation of the 'branch_default' field
146
Local store method to prevent direct manipulation of the 'branch_default' field
Lines 63-69 sub store { Link Here
63
                    property => 'branch_default' );
156
                    property => 'branch_default' );
64
            }
157
            }
65
            else {
158
            else {
66
                if ($self->_result->is_column_changed('branch') && $self->branch_default) {
159
                if (   $self->_result->is_column_changed('branch')
160
                    && $self->branch_default )
161
                {
67
                }
162
                }
68
                $self = $self->SUPER::store;
163
                $self = $self->SUPER::store;
69
            }
164
            }
Lines 113-118 sub drop_default { Link Here
113
    return $self;
208
    return $self;
114
}
209
}
115
210
211
=head3 add_cashup
212
213
Add a new cashup action to the till, returns the added action.
214
215
=cut
216
217
sub add_cashup {
218
    my ( $self, $params ) = @_;
219
220
    my $rs = $self->_result->add_to_cash_register_actions(
221
        {
222
            code       => 'CASHUP',
223
            manager_id => $params->{user_id},
224
            amount     => $params->{amount}
225
        }
226
    )->discard_changes;
227
228
    return Koha::Cash::Register::Action->_new_from_dbic($rs);
229
}
230
116
=head2 Internal methods
231
=head2 Internal methods
117
232
118
=cut
233
=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