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

(-)a/Koha/Account/Line.pm (-1 / +7 lines)
Lines 865-871 sub store { Link Here
865
    my ($self) = @_;
865
    my ($self) = @_;
866
866
867
    my $AutoCreditNumber = C4::Context->preference('AutoCreditNumber');
867
    my $AutoCreditNumber = C4::Context->preference('AutoCreditNumber');
868
    if ($AutoCreditNumber && !$self->in_storage && $self->is_credit && !$self->credit_number) {
868
    my $credit_number_enabled = $self->is_credit && $self->credit_type->credit_number_enabled;
869
870
    if ($AutoCreditNumber && $credit_number_enabled && !$self->in_storage) {
871
        if (defined $self->credit_number) {
872
            Koha::Exceptions::Account->throw('AutoCreditNumber is enabled but credit_number is already defined');
873
        }
874
869
        my $rs = Koha::Database->new->schema->resultset($self->_type);
875
        my $rs = Koha::Database->new->schema->resultset($self->_type);
870
876
871
        if ($AutoCreditNumber eq 'incremental') {
877
        if ($AutoCreditNumber eq 'incremental') {
(-)a/admin/credit_types.pl (-3 / +9 lines)
Lines 77-93 if ( $op eq 'add_form' ) { Link Here
77
elsif ( $op eq 'add_validate' ) {
77
elsif ( $op eq 'add_validate' ) {
78
    my $description           = $input->param('description');
78
    my $description           = $input->param('description');
79
    my $can_be_added_manually = $input->param('can_be_added_manually') || 0;
79
    my $can_be_added_manually = $input->param('can_be_added_manually') || 0;
80
    my $credit_number_enabled = $input->param('credit_number_enabled') || 0;
80
    my @branches = grep { $_ ne q{} } $input->multi_param('branches');
81
    my @branches = grep { $_ ne q{} } $input->multi_param('branches');
81
82
82
    if ( not defined $credit_type ) {
83
    if ( not defined $credit_type ) {
83
        $credit_type = Koha::Account::CreditType->new( { code => $code } );
84
        $credit_type = Koha::Account::CreditType->new( { code => $code } );
84
    }
85
    }
85
    $credit_type->description($description);
86
    unless ($credit_type->is_system) {
86
    $credit_type->can_be_added_manually($can_be_added_manually);
87
        $credit_type->description($description);
88
        $credit_type->can_be_added_manually($can_be_added_manually);
89
    }
90
    $credit_type->credit_number_enabled($credit_number_enabled);
87
91
88
    try {
92
    try {
89
        $credit_type->store;
93
        $credit_type->store;
90
        $credit_type->replace_library_limits( \@branches );
94
        unless ($credit_type->is_system) {
95
            $credit_type->replace_library_limits( \@branches );
96
        }
91
        push @messages, { type => 'message', code => 'success_on_saving' };
97
        push @messages, { type => 'message', code => 'success_on_saving' };
92
    }
98
    }
93
    catch {
99
    catch {
(-)a/installer/data/mysql/atomicupdate/bug-19036.perl (-1 / +10 lines)
Lines 4-11 if (CheckVersion($DBversion)) { Link Here
4
        $dbh->do('ALTER TABLE accountlines ADD COLUMN credit_number VARCHAR(20) NULL DEFAULT NULL COMMENT "autogenerated number for credits" AFTER debit_type_code');
4
        $dbh->do('ALTER TABLE accountlines ADD COLUMN credit_number VARCHAR(20) NULL DEFAULT NULL COMMENT "autogenerated number for credits" AFTER debit_type_code');
5
    }
5
    }
6
6
7
    unless (column_exists('account_credit_types', 'credit_number_enabled')) {
8
        $dbh->do(q{
9
            ALTER TABLE account_credit_types
10
            ADD COLUMN credit_number_enabled TINYINT(1) NOT NULL DEFAULT 0
11
                COMMENT "Is autogeneration of credit number enabled for this credit type"
12
                AFTER can_be_added_manually
13
        });
14
    }
15
7
    $dbh->do('INSERT IGNORE INTO systempreferences (variable, value, options, explanation, type) VALUES(?, ?, ?, ?, ?)', undef, 'AutoCreditNumber', '', '', 'Automatically generate a number for account credits', 'Choice');
16
    $dbh->do('INSERT IGNORE INTO systempreferences (variable, value, options, explanation, type) VALUES(?, ?, ?, ?, ?)', undef, 'AutoCreditNumber', '', '', 'Automatically generate a number for account credits', 'Choice');
8
17
9
    SetVersion($DBversion);
18
    SetVersion($DBversion);
10
    print "Upgrade to $DBversion done (Bug 19036 - Add column accountlines.credit_number)\n";
19
    print "Upgrade to $DBversion done (Bug 19036 - Add accountlines.credit_number, account_credit_types.credit_number_enabled and syspref AutoCreditNumber)\n";
11
}
20
}
(-)a/installer/data/mysql/kohastructure.sql (+1 lines)
Lines 2600-2605 CREATE TABLE `account_credit_types` ( Link Here
2600
  `code` varchar(80) NOT NULL,
2600
  `code` varchar(80) NOT NULL,
2601
  `description` varchar(200) DEFAULT NULL,
2601
  `description` varchar(200) DEFAULT NULL,
2602
  `can_be_added_manually` tinyint(4) NOT NULL DEFAULT 1,
2602
  `can_be_added_manually` tinyint(4) NOT NULL DEFAULT 1,
2603
  `credit_number_enabled` TINYINT(1) NOT NULL DEFAULT 0 COMMENT "Is autogeneration of credit number enabled for this credit type",
2603
  `is_system` tinyint(1) NOT NULL DEFAULT 0,
2604
  `is_system` tinyint(1) NOT NULL DEFAULT 0,
2604
  `archived` tinyint(1) NOT NULL DEFAULT 0, -- boolean flag to denote if this till is archived or not
2605
  `archived` tinyint(1) NOT NULL DEFAULT 0, -- boolean flag to denote if this till is archived or not
2605
  PRIMARY KEY (`code`)
2606
  PRIMARY KEY (`code`)
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/credit_types.tt (-17 / +42 lines)
Lines 75-103 Link Here
75
                                </li>
75
                                </li>
76
                                <li>
76
                                <li>
77
                                    <label for="description" class="required">Description: </label>
77
                                    <label for="description" class="required">Description: </label>
78
                                    <input type="text" name="description" id="description" required="required" class="required" size="80" maxlength="100" value="[% credit_type.description | html %]" /> <span class="required">Required</span>
78
                                    [% IF credit_type && credit_type.is_system %]
79
                                        <span>[% credit_type.description | html %]</span>
80
                                    [% ELSE %]
81
                                        <input type="text" name="description" id="description" required="required" class="required" size="80" maxlength="100" value="[% credit_type.description | html %]" /> <span class="required">Required</span>
82
                                    [% END %]
79
                                </li>
83
                                </li>
80
                                <li>
84
                                <li>
81
                                    <label for="can_be_added_manually">Can be manually added ? </label>
85
                                    <label for="can_be_added_manually">Can be manually added ? </label>
82
                                    [% IF credit_type.can_be_added_manually %]
86
                                    [% IF credit_type && credit_type.is_system %]
87
                                        [% IF credit_type.can_be_added_manually %]Yes[% ELSE %]No[% END %]
88
                                    [% ELSIF credit_type.can_be_added_manually %]
83
                                        <input type="checkbox" name="can_be_added_manually" id="can_be_added_manually" checked="checked" value="1" />
89
                                        <input type="checkbox" name="can_be_added_manually" id="can_be_added_manually" checked="checked" value="1" />
84
                                    [% ELSE %]
90
                                    [% ELSE %]
85
                                        <input type="checkbox" name="can_be_added_manually" id="can_be_added_manually" value="1" />
91
                                        <input type="checkbox" name="can_be_added_manually" id="can_be_added_manually" value="1" />
86
                                    [% END %]
92
                                    [% END %]
87
                                </li>
93
                                </li>
94
                                <li>
95
                                    <label for="credit_number_enabled">Enable credit number</label>
96
                                    [% IF credit_type.credit_number_enabled %]
97
                                        <input type="checkbox" name="credit_number_enabled" id="credit_number_enabled" checked="checked" value="1" />
98
                                    [% ELSE %]
99
                                        <input type="checkbox" name="credit_number_enabled" id="credit_number_enabled" value="1" />
100
                                    [% END %]
101
                                    <span>Enable automatic generation of credit number (see <a href="/cgi-bin/koha/admin/preferences.pl?op=search&searchfield=AutoCreditNumber">AutoCreditNumber</a>)</span>
102
                                </li>
88
                                <li>
103
                                <li>
89
                                    <label for="branches">Libraries limitation: </label>
104
                                    <label for="branches">Libraries limitation: </label>
90
                                    <select id="branches" name="branches" multiple size="10">
105
                                    [% IF credit_type && credit_type.is_system %]
91
                                        <option value="">All libraries</option>
106
                                        No library limitation
92
                                        [% FOREACH branch IN branches_loop %]
107
                                    [% ELSE %]
93
                                        [% IF ( branch.selected ) %]
108
                                        <select id="branches" name="branches" multiple size="10">
94
                                        <option selected="selected" value="[% branch.branchcode | html %]">[% branch.branchname | html %]</option>
109
                                            <option value="">All libraries</option>
95
                                        [% ELSE %]
110
                                            [% FOREACH branch IN branches_loop %]
96
                                        <option value="[% branch.branchcode | html %]">[% branch.branchname | html %]</option>
111
                                            [% IF ( branch.selected ) %]
97
                                        [% END %]
112
                                            <option selected="selected" value="[% branch.branchcode | html %]">[% branch.branchname | html %]</option>
98
                                        [% END %]
113
                                            [% ELSE %]
99
                                    </select>
114
                                            <option value="[% branch.branchcode | html %]">[% branch.branchname | html %]</option>
100
                                    <span>Select 'All libraries' if this credit type should be available at all libraries. Otherwise select libraries you want to associate credit type with.</span>
115
                                            [% END %]
116
                                            [% END %]
117
                                        </select>
118
                                        <span>Select 'All libraries' if this credit type should be available at all libraries. Otherwise select libraries you want to associate credit type with.</span>
119
                                    [% END %]
101
                                </li>
120
                                </li>
102
                            </ol>
121
                            </ol>
103
                        </fieldset>
122
                        </fieldset>
Lines 123-128 Link Here
123
                                <th>Code</th>
142
                                <th>Code</th>
124
                                <th>Description</th>
143
                                <th>Description</th>
125
                                <th>Available for</th>
144
                                <th>Available for</th>
145
                                <th>Credit number enabled</th>
126
                                <th>Library limitations</th>
146
                                <th>Library limitations</th>
127
                                <th>Actions</th>
147
                                <th>Actions</th>
128
                            </thead>
148
                            </thead>
Lines 134-139 Link Here
134
                                    <td>[% credit_type.code | html %]</td>
154
                                    <td>[% credit_type.code | html %]</td>
135
                                    <td>[% credit_type.description | html %]</td>
155
                                    <td>[% credit_type.description | html %]</td>
136
                                    <td>[% IF credit_type.can_be_added_manually %]Manual credit[% END %]</td>
156
                                    <td>[% IF credit_type.can_be_added_manually %]Manual credit[% END %]</td>
157
                                    <td>[% IF credit_type.credit_number_enabled %]Yes[% ELSE %]No[% END %]</td>
137
                                    <td>
158
                                    <td>
138
                                        [% IF credit_type.library_limits.count > 0 %]
159
                                        [% IF credit_type.library_limits.count > 0 %]
139
                                            [% library_limits_str = "" %]
160
                                            [% library_limits_str = "" %]
Lines 152-162 Link Here
152
                                        [% END %]
173
                                        [% END %]
153
                                    </td>
174
                                    </td>
154
                                    <td class="actions">
175
                                    <td class="actions">
176
                                        [% IF !credit_type.archived %]
177
                                            <a class="btn btn-default btn-xs" href="/cgi-bin/koha/admin/credit_types.pl?op=add_form&amp;code=[% credit_type.code | uri %]&type=credit"><i class="fa fa-pencil"></i> Edit</a>
178
                                        [% END %]
155
                                        [% IF !credit_type.is_system && !credit_type.archived %]
179
                                        [% IF !credit_type.is_system && !credit_type.archived %]
156
                                        <a class="btn btn-default btn-xs" href="/cgi-bin/koha/admin/credit_types.pl?op=add_form&amp;code=[% credit_type.code | uri %]&type=credit"><i class="fa fa-pencil"></i> Edit</a>
180
                                            <a class="btn btn-default btn-xs" href="/cgi-bin/koha/admin/credit_types.pl?op=archive&amp;code=[% credit_type.code | uri %]"><i class="fa fa-archive"></i> Archive</a>
157
                                        <a class="btn btn-default btn-xs" href="/cgi-bin/koha/admin/credit_types.pl?op=archive&amp;code=[% credit_type.code | uri %]"><i class="fa fa-archive"></i> Archive</a>
181
                                        [% END %]
158
                                        [% ELSIF credit_type.archived %]
182
159
                                        <a class="btn btn-default btn-xs" href="/cgi-bin/koha/admin/credit_types.pl?op=unarchive&amp;code=[% credit_type.code | uri %]"><i class="fa fa-undo"></i> Restore</a>
183
                                        [% IF !credit_type.is_system && credit_type.archived %]
184
                                            <a class="btn btn-default btn-xs" href="/cgi-bin/koha/admin/credit_types.pl?op=unarchive&amp;code=[% credit_type.code | uri %]"><i class="fa fa-undo"></i> Restore</a>
160
                                        [% END %]
185
                                        [% END %]
161
                                    </td>
186
                                    </td>
162
                                </tr>
187
                                </tr>
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/accounting.pref (+1 lines)
Lines 40-42 Accounting: Link Here
40
                annual: 'Automatically generate credit numbers in the form <year>-0001'
40
                annual: 'Automatically generate credit numbers in the form <year>-0001'
41
                branchyyyymmincr: 'Automatically generate credit numbers in the form <branchcode>yyyymm0001'
41
                branchyyyymmincr: 'Automatically generate credit numbers in the form <branchcode>yyyymm0001'
42
                incremental: 'Automatically generate credit numbers in the form 1, 2, 3'
42
                incremental: 'Automatically generate credit numbers in the form 1, 2, 3'
43
            - Automatic generation also has to be enabled for each credit type (<a href="/cgi-bin/koha/admin/credit_types.pl">Configure credit types</a>)
(-)a/t/db_dependent/Koha/Account.t (-2 / +15 lines)
Lines 26-31 use Test::Exception; Link Here
26
use DateTime;
26
use DateTime;
27
27
28
use Koha::Account;
28
use Koha::Account;
29
use Koha::Account::CreditTypes;
29
use Koha::Account::Lines;
30
use Koha::Account::Lines;
30
use Koha::Account::Offsets;
31
use Koha::Account::Offsets;
31
use Koha::DateUtils qw( dt_from_string );
32
use Koha::DateUtils qw( dt_from_string );
Lines 1051-1057 subtest 'Koha::Account::Line::apply() handles lost items' => sub { Link Here
1051
};
1052
};
1052
1053
1053
subtest 'Koha::Account::pay() generates credit number' => sub {
1054
subtest 'Koha::Account::pay() generates credit number' => sub {
1054
    plan tests => 34;
1055
    plan tests => 37;
1055
1056
1056
    $schema->storage->txn_begin;
1057
    $schema->storage->txn_begin;
1057
1058
Lines 1069-1074 subtest 'Koha::Account::pay() generates credit number' => sub { Link Here
1069
    my $month = $now->month;
1070
    my $month = $now->month;
1070
    my ($accountlines_id, $accountline);
1071
    my ($accountlines_id, $accountline);
1071
1072
1073
    my $credit_type = Koha::Account::CreditTypes->find('PAYMENT');
1074
    $credit_type->credit_number_enabled(1);
1075
    $credit_type->store();
1076
1072
    t::lib::Mocks::mock_preference('AutoCreditNumber', '');
1077
    t::lib::Mocks::mock_preference('AutoCreditNumber', '');
1073
    $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
1078
    $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
1074
    $accountline = Koha::Account::Lines->find($accountlines_id);
1079
    $accountline = Koha::Account::Lines->find($accountlines_id);
Lines 1080-1085 subtest 'Koha::Account::pay() generates credit number' => sub { Link Here
1080
        $accountline = Koha::Account::Lines->find($accountlines_id);
1085
        $accountline = Koha::Account::Lines->find($accountlines_id);
1081
        is($accountline->credit_number, $i);
1086
        is($accountline->credit_number, $i);
1082
    }
1087
    }
1088
    $accountlines_id = $account->pay({ type => 'WRITEOFF', amount => '1.00', library_id => $library->id })->{payment_id};
1089
    $accountline = Koha::Account::Lines->find($accountlines_id);
1090
    is($accountline->credit_number, undef);
1083
1091
1084
    t::lib::Mocks::mock_preference('AutoCreditNumber', 'annual');
1092
    t::lib::Mocks::mock_preference('AutoCreditNumber', 'annual');
1085
    for my $i (1..11) {
1093
    for my $i (1..11) {
Lines 1087-1092 subtest 'Koha::Account::pay() generates credit number' => sub { Link Here
1087
        $accountline = Koha::Account::Lines->find($accountlines_id);
1095
        $accountline = Koha::Account::Lines->find($accountlines_id);
1088
        is($accountline->credit_number, sprintf('%s-%04d', $year, $i));
1096
        is($accountline->credit_number, sprintf('%s-%04d', $year, $i));
1089
    }
1097
    }
1098
    $accountlines_id = $account->pay({ type => 'WRITEOFF', amount => '1.00', library_id => $library->id })->{payment_id};
1099
    $accountline = Koha::Account::Lines->find($accountlines_id);
1100
    is($accountline->credit_number, undef);
1090
1101
1091
    t::lib::Mocks::mock_preference('AutoCreditNumber', 'branchyyyymmincr');
1102
    t::lib::Mocks::mock_preference('AutoCreditNumber', 'branchyyyymmincr');
1092
    for my $i (1..11) {
1103
    for my $i (1..11) {
Lines 1094-1099 subtest 'Koha::Account::pay() generates credit number' => sub { Link Here
1094
        $accountline = Koha::Account::Lines->find($accountlines_id);
1105
        $accountline = Koha::Account::Lines->find($accountlines_id);
1095
        is($accountline->credit_number, sprintf('%s%d%02d%04d', $library->id, $year, $month, $i));
1106
        is($accountline->credit_number, sprintf('%s%d%02d%04d', $library->id, $year, $month, $i));
1096
    }
1107
    }
1108
    $accountlines_id = $account->pay({ type => 'WRITEOFF', amount => '1.00', library_id => $library->id })->{payment_id};
1109
    $accountline = Koha::Account::Lines->find($accountlines_id);
1110
    is($accountline->credit_number, undef);
1097
1111
1098
    $schema->storage->txn_rollback;
1112
    $schema->storage->txn_rollback;
1099
};
1113
};
1100
- 

Return to bug 19036