diff --git a/Koha/Account/Line.pm b/Koha/Account/Line.pm
index 5272b34912..d4fbd6bd71 100644
--- a/Koha/Account/Line.pm
+++ b/Koha/Account/Line.pm
@@ -886,7 +886,13 @@ sub store {
my ($self) = @_;
my $AutoCreditNumber = C4::Context->preference('AutoCreditNumber');
- if ($AutoCreditNumber && !$self->in_storage && $self->is_credit && !$self->credit_number) {
+ my $credit_number_enabled = $self->is_credit && $self->credit_type->credit_number_enabled;
+
+ if ($AutoCreditNumber && $credit_number_enabled && !$self->in_storage) {
+ if (defined $self->credit_number) {
+ Koha::Exceptions::Account->throw('AutoCreditNumber is enabled but credit_number is already defined');
+ }
+
my $rs = Koha::Database->new->schema->resultset($self->_type);
if ($AutoCreditNumber eq 'incremental') {
diff --git a/admin/credit_types.pl b/admin/credit_types.pl
index 75baff90bb..6b5da18071 100755
--- a/admin/credit_types.pl
+++ b/admin/credit_types.pl
@@ -77,17 +77,23 @@ if ( $op eq 'add_form' ) {
elsif ( $op eq 'add_validate' ) {
my $description = $input->param('description');
my $can_be_added_manually = $input->param('can_be_added_manually') || 0;
+ my $credit_number_enabled = $input->param('credit_number_enabled') || 0;
my @branches = grep { $_ ne q{} } $input->multi_param('branches');
if ( not defined $credit_type ) {
$credit_type = Koha::Account::CreditType->new( { code => $code } );
}
- $credit_type->description($description);
- $credit_type->can_be_added_manually($can_be_added_manually);
+ unless ($credit_type->is_system) {
+ $credit_type->description($description);
+ $credit_type->can_be_added_manually($can_be_added_manually);
+ }
+ $credit_type->credit_number_enabled($credit_number_enabled);
try {
$credit_type->store;
- $credit_type->replace_library_limits( \@branches );
+ unless ($credit_type->is_system) {
+ $credit_type->replace_library_limits( \@branches );
+ }
push @messages, { type => 'message', code => 'success_on_saving' };
}
catch {
diff --git a/installer/data/mysql/atomicupdate/bug-19036.perl b/installer/data/mysql/atomicupdate/bug-19036.perl
index 7a144bcd94..e92cf096b3 100644
--- a/installer/data/mysql/atomicupdate/bug-19036.perl
+++ b/installer/data/mysql/atomicupdate/bug-19036.perl
@@ -4,8 +4,17 @@ if (CheckVersion($DBversion)) {
$dbh->do('ALTER TABLE accountlines ADD COLUMN credit_number VARCHAR(20) NULL DEFAULT NULL COMMENT "autogenerated number for credits" AFTER debit_type_code');
}
+ unless (column_exists('account_credit_types', 'credit_number_enabled')) {
+ $dbh->do(q{
+ ALTER TABLE account_credit_types
+ ADD COLUMN credit_number_enabled TINYINT(1) NOT NULL DEFAULT 0
+ COMMENT "Is autogeneration of credit number enabled for this credit type"
+ AFTER can_be_added_manually
+ });
+ }
+
$dbh->do('INSERT IGNORE INTO systempreferences (variable, value, options, explanation, type) VALUES(?, ?, ?, ?, ?)', undef, 'AutoCreditNumber', '', '', 'Automatically generate a number for account credits', 'Choice');
SetVersion($DBversion);
- print "Upgrade to $DBversion done (Bug 19036 - Add column accountlines.credit_number)\n";
+ print "Upgrade to $DBversion done (Bug 19036 - Add accountlines.credit_number, account_credit_types.credit_number_enabled and syspref AutoCreditNumber)\n";
}
diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql
index 2e3a42ac1c..27bb117b31 100644
--- a/installer/data/mysql/kohastructure.sql
+++ b/installer/data/mysql/kohastructure.sql
@@ -2663,6 +2663,7 @@ CREATE TABLE `account_credit_types` (
`code` varchar(80) NOT NULL,
`description` varchar(200) DEFAULT NULL,
`can_be_added_manually` tinyint(4) NOT NULL DEFAULT 1,
+ `credit_number_enabled` TINYINT(1) NOT NULL DEFAULT 0 COMMENT "Is autogeneration of credit number enabled for this credit type",
`is_system` tinyint(1) NOT NULL DEFAULT 0,
`archived` tinyint(1) NOT NULL DEFAULT 0, -- boolean flag to denote if this till is archived or not
PRIMARY KEY (`code`)
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/credit_types.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/credit_types.tt
index 11bcfab409..0169ea25b4 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/credit_types.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/credit_types.tt
@@ -75,29 +75,48 @@
- Required
+ [% IF credit_type && credit_type.is_system %]
+ [% credit_type.description | html %]
+ [% ELSE %]
+ Required
+ [% END %]
- [% IF credit_type.can_be_added_manually %]
+ [% IF credit_type && credit_type.is_system %]
+ [% IF credit_type.can_be_added_manually %]Yes[% ELSE %]No[% END %]
+ [% ELSIF credit_type.can_be_added_manually %]
[% ELSE %]
[% END %]
+
+
+ [% IF credit_type.credit_number_enabled %]
+
+ [% ELSE %]
+
+ [% END %]
+ Enable automatic generation of credit number (see AutoCreditNumber)
+
-
- Select 'All libraries' if this credit type should be available at all libraries. Otherwise select libraries you want to associate credit type with.
+ [% IF credit_type && credit_type.is_system %]
+ No library limitation
+ [% ELSE %]
+
+ Select 'All libraries' if this credit type should be available at all libraries. Otherwise select libraries you want to associate credit type with.
+ [% END %]
@@ -123,6 +142,7 @@
Code
Description
Available for
+
Credit number enabled
Library limitations
Actions
@@ -134,6 +154,7 @@
[% credit_type.code | html %]
[% credit_type.description | html %]
[% IF credit_type.can_be_added_manually %]Manual credit[% END %]
+
[% IF credit_type.credit_number_enabled %]Yes[% ELSE %]No[% END %]
[% IF credit_type.library_limits.count > 0 %]
[% library_limits_str = "" %]
@@ -152,11 +173,15 @@
[% END %]
+ [% IF !credit_type.archived %]
+ Edit
+ [% END %]
[% IF !credit_type.is_system && !credit_type.archived %]
- Edit
- Archive
- [% ELSIF credit_type.archived %]
- Restore
+ Archive
+ [% END %]
+
+ [% IF !credit_type.is_system && credit_type.archived %]
+ Restore
[% END %]
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/accounting.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/accounting.pref
index 5cbfe8d3fd..7f4ae68fd9 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/accounting.pref
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/accounting.pref
@@ -40,3 +40,4 @@ Accounting:
annual: 'Automatically generate credit numbers in the form -0001'
branchyyyymmincr: 'Automatically generate credit numbers in the form yyyymm0001'
incremental: 'Automatically generate credit numbers in the form 1, 2, 3'
+ - Automatic generation also has to be enabled for each credit type (Configure credit types)
diff --git a/t/db_dependent/Koha/Account.t b/t/db_dependent/Koha/Account.t
index 9ebad52055..c6341c0cb2 100755
--- a/t/db_dependent/Koha/Account.t
+++ b/t/db_dependent/Koha/Account.t
@@ -26,6 +26,7 @@ use Test::Exception;
use DateTime;
use Koha::Account;
+use Koha::Account::CreditTypes;
use Koha::Account::Lines;
use Koha::Account::Offsets;
use Koha::DateUtils qw( dt_from_string );
@@ -1051,7 +1052,7 @@ subtest 'Koha::Account::Line::apply() handles lost items' => sub {
};
subtest 'Koha::Account::pay() generates credit number' => sub {
- plan tests => 34;
+ plan tests => 37;
$schema->storage->txn_begin;
@@ -1069,6 +1070,10 @@ subtest 'Koha::Account::pay() generates credit number' => sub {
my $month = $now->month;
my ($accountlines_id, $accountline);
+ my $credit_type = Koha::Account::CreditTypes->find('PAYMENT');
+ $credit_type->credit_number_enabled(1);
+ $credit_type->store();
+
t::lib::Mocks::mock_preference('AutoCreditNumber', '');
$accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
$accountline = Koha::Account::Lines->find($accountlines_id);
@@ -1080,6 +1085,9 @@ subtest 'Koha::Account::pay() generates credit number' => sub {
$accountline = Koha::Account::Lines->find($accountlines_id);
is($accountline->credit_number, $i);
}
+ $accountlines_id = $account->pay({ type => 'WRITEOFF', amount => '1.00', library_id => $library->id })->{payment_id};
+ $accountline = Koha::Account::Lines->find($accountlines_id);
+ is($accountline->credit_number, undef);
t::lib::Mocks::mock_preference('AutoCreditNumber', 'annual');
for my $i (1..11) {
@@ -1087,6 +1095,9 @@ subtest 'Koha::Account::pay() generates credit number' => sub {
$accountline = Koha::Account::Lines->find($accountlines_id);
is($accountline->credit_number, sprintf('%s-%04d', $year, $i));
}
+ $accountlines_id = $account->pay({ type => 'WRITEOFF', amount => '1.00', library_id => $library->id })->{payment_id};
+ $accountline = Koha::Account::Lines->find($accountlines_id);
+ is($accountline->credit_number, undef);
t::lib::Mocks::mock_preference('AutoCreditNumber', 'branchyyyymmincr');
for my $i (1..11) {
@@ -1094,6 +1105,9 @@ subtest 'Koha::Account::pay() generates credit number' => sub {
$accountline = Koha::Account::Lines->find($accountlines_id);
is($accountline->credit_number, sprintf('%s%d%02d%04d', $library->id, $year, $month, $i));
}
+ $accountlines_id = $account->pay({ type => 'WRITEOFF', amount => '1.00', library_id => $library->id })->{payment_id};
+ $accountline = Koha::Account::Lines->find($accountlines_id);
+ is($accountline->credit_number, undef);
$schema->storage->txn_rollback;
};