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') {
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 {
$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";
}
`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`)
</li>
<li>
<label for="description" class="required">Description: </label>
- <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>
+ [% IF credit_type && credit_type.is_system %]
+ <span>[% credit_type.description | html %]</span>
+ [% ELSE %]
+ <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>
+ [% END %]
</li>
<li>
<label for="can_be_added_manually">Can be manually added ? </label>
- [% 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 %]
<input type="checkbox" name="can_be_added_manually" id="can_be_added_manually" checked="checked" value="1" />
[% ELSE %]
<input type="checkbox" name="can_be_added_manually" id="can_be_added_manually" value="1" />
[% END %]
</li>
<li>
+ <label for="credit_number_enabled">Enable credit number</label>
+ [% IF credit_type.credit_number_enabled %]
+ <input type="checkbox" name="credit_number_enabled" id="credit_number_enabled" checked="checked" value="1" />
+ [% ELSE %]
+ <input type="checkbox" name="credit_number_enabled" id="credit_number_enabled" value="1" />
+ [% END %]
+ <span>Enable automatic generation of credit number (see <a href="/cgi-bin/koha/admin/preferences.pl?op=search&searchfield=AutoCreditNumber">AutoCreditNumber</a>)</span>
+ </li>
+ <li>
<label for="branches">Libraries limitation: </label>
- <select id="branches" name="branches" multiple size="10">
- <option value="">All libraries</option>
- [% FOREACH branch IN branches_loop %]
- [% IF ( branch.selected ) %]
- <option selected="selected" value="[% branch.branchcode | html %]">[% branch.branchname | html %]</option>
- [% ELSE %]
- <option value="[% branch.branchcode | html %]">[% branch.branchname | html %]</option>
- [% END %]
- [% END %]
- </select>
- <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>
+ [% IF credit_type && credit_type.is_system %]
+ No library limitation
+ [% ELSE %]
+ <select id="branches" name="branches" multiple size="10">
+ <option value="">All libraries</option>
+ [% FOREACH branch IN branches_loop %]
+ [% IF ( branch.selected ) %]
+ <option selected="selected" value="[% branch.branchcode | html %]">[% branch.branchname | html %]</option>
+ [% ELSE %]
+ <option value="[% branch.branchcode | html %]">[% branch.branchname | html %]</option>
+ [% END %]
+ [% END %]
+ </select>
+ <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>
+ [% END %]
</li>
</ol>
</fieldset>
<th>Code</th>
<th>Description</th>
<th>Available for</th>
+ <th>Credit number enabled</th>
<th>Library limitations</th>
<th>Actions</th>
</thead>
<td>[% credit_type.code | html %]</td>
<td>[% credit_type.description | html %]</td>
<td>[% IF credit_type.can_be_added_manually %]Manual credit[% END %]</td>
+ <td>[% IF credit_type.credit_number_enabled %]Yes[% ELSE %]No[% END %]</td>
<td>
[% IF credit_type.library_limits.count > 0 %]
[% library_limits_str = "" %]
[% END %]
</td>
<td class="actions">
+ [% IF !credit_type.archived %]
+ <a class="btn btn-default btn-xs" href="/cgi-bin/koha/admin/credit_types.pl?op=add_form&code=[% credit_type.code | uri %]&type=credit"><i class="fa fa-pencil"></i> Edit</a>
+ [% END %]
[% IF !credit_type.is_system && !credit_type.archived %]
- <a class="btn btn-default btn-xs" href="/cgi-bin/koha/admin/credit_types.pl?op=add_form&code=[% credit_type.code | uri %]&type=credit"><i class="fa fa-pencil"></i> Edit</a>
- <a class="btn btn-default btn-xs" href="/cgi-bin/koha/admin/credit_types.pl?op=archive&code=[% credit_type.code | uri %]"><i class="fa fa-archive"></i> Archive</a>
- [% ELSIF credit_type.archived %]
- <a class="btn btn-default btn-xs" href="/cgi-bin/koha/admin/credit_types.pl?op=unarchive&code=[% credit_type.code | uri %]"><i class="fa fa-undo"></i> Restore</a>
+ <a class="btn btn-default btn-xs" href="/cgi-bin/koha/admin/credit_types.pl?op=archive&code=[% credit_type.code | uri %]"><i class="fa fa-archive"></i> Archive</a>
+ [% END %]
+
+ [% IF !credit_type.is_system && credit_type.archived %]
+ <a class="btn btn-default btn-xs" href="/cgi-bin/koha/admin/credit_types.pl?op=unarchive&code=[% credit_type.code | uri %]"><i class="fa fa-undo"></i> Restore</a>
[% END %]
</td>
</tr>
annual: 'Automatically generate credit numbers in the form <year>-0001'
branchyyyymmincr: 'Automatically generate credit numbers in the form <branchcode>yyyymm0001'
incremental: 'Automatically generate credit numbers in the form 1, 2, 3'
+ - Automatic generation also has to be enabled for each credit type (<a href="/cgi-bin/koha/admin/credit_types.pl">Configure credit types</a>)
use DateTime;
use Koha::Account;
+use Koha::Account::CreditTypes;
use Koha::Account::Lines;
use Koha::Account::Offsets;
use Koha::DateUtils qw( dt_from_string );
};
subtest 'Koha::Account::pay() generates credit number' => sub {
- plan tests => 34;
+ plan tests => 37;
$schema->storage->txn_begin;
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);
$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) {
$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) {
$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;
};