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

(-)a/Koha/Account/Line.pm (+58 lines)
Lines 855-860 sub renew_item { Link Here
855
855
856
}
856
}
857
857
858
=head3 store
859
860
Specific store method to generate credit number before saving
861
862
=cut
863
864
sub store {
865
    my ($self) = @_;
866
867
    my $AutoCreditNumber = C4::Context->preference('AutoCreditNumber');
868
    if ($AutoCreditNumber && !$self->in_storage && $self->is_credit && !$self->credit_number) {
869
        my $rs = Koha::Database->new->schema->resultset($self->_type);
870
871
        if ($AutoCreditNumber eq 'incremental') {
872
            my $max = $rs->search({
873
                credit_number => { -regexp => '^[0-9]+$' }
874
            }, {
875
                select => \'CAST(credit_number AS UNSIGNED)',
876
                as => ['credit_number'],
877
            })->get_column('credit_number')->max;
878
            $max //= 0;
879
            $self->credit_number($max + 1);
880
        } elsif ($AutoCreditNumber eq 'annual') {
881
            my $now = DateTime->now;
882
            my $prefix = sprintf('%d-', $now->year);
883
            my $max = $rs->search({
884
                -and => [
885
                    credit_number => { -regexp => '[0-9]{4}$' },
886
                    credit_number => { -like => "$prefix%" },
887
                ],
888
            })->get_column('credit_number')->max;
889
            $max //= $prefix . '0000';
890
            my $incr = substr($max, length $prefix);
891
            $self->credit_number(sprintf('%s%04d', $prefix, $incr + 1));
892
        } elsif ($AutoCreditNumber eq 'branchyyyymmincr') {
893
            my $userenv = C4::Context->userenv;
894
            if ($userenv) {
895
                my $branch = $userenv->{branch};
896
                my $now = DateTime->now;
897
                my $prefix = sprintf('%s%d%02d', $branch, $now->year, $now->month);
898
                my $pattern = $prefix;
899
                $pattern =~ s/([\?%_])/\\$1/g;
900
                my $max = $rs->search({
901
                    -and => [
902
                        credit_number => { -regexp => '[0-9]{4}$' },
903
                        credit_number => { -like => "$pattern%" },
904
                    ],
905
                })->get_column('credit_number')->max;
906
                $max //= $prefix . '0000';
907
                my $incr = substr($max, length $prefix);
908
                $self->credit_number(sprintf('%s%04d', $prefix, $incr + 1));
909
            }
910
        }
911
    }
912
913
    return $self->SUPER::store();
914
}
915
858
=head2 Internal methods
916
=head2 Internal methods
859
917
860
=cut
918
=cut
(-)a/admin/columns_settings.yml (+3 lines)
Lines 520-525 modules: Link Here
520
      account-fines:
520
      account-fines:
521
        -
521
        -
522
          columnname: date
522
          columnname: date
523
        -
524
          columnname: credit_number
525
          is_hidden: 1
523
        -
526
        -
524
          columnname: account_type
527
          columnname: account_type
525
        -
528
        -
(-)a/installer/data/mysql/atomicupdate/bug-19036.perl (+11 lines)
Line 0 Link Here
1
$DBversion = 'XXX';
2
if (CheckVersion($DBversion)) {
3
    unless (column_exists('accountlines', 'credit_number')) {
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
    }
6
7
    $dbh->do('INSERT IGNORE INTO systempreferences (variable, value, options, explanation, type) VALUES(?, ?, ?, ?, ?)', undef, 'AutoCreditNumber', '', '', 'Automatically generate a number for account credits', 'Choice');
8
9
    SetVersion($DBversion);
10
    print "Upgrade to $DBversion done (Bug 19036 - Add column accountlines.credit_number)\n";
11
}
(-)a/installer/data/mysql/kohastructure.sql (+1 lines)
Lines 2660-2665 CREATE TABLE `accountlines` ( Link Here
2660
  `description` LONGTEXT,
2660
  `description` LONGTEXT,
2661
  `credit_type_code` varchar(80) default NULL,
2661
  `credit_type_code` varchar(80) default NULL,
2662
  `debit_type_code` varchar(80) default NULL,
2662
  `debit_type_code` varchar(80) default NULL,
2663
  `credit_number` varchar(20) NULL DEFAULT NULL COMMENT 'autogenerated number for credits',
2663
  `status` varchar(16) default NULL,
2664
  `status` varchar(16) default NULL,
2664
  `payment_type` varchar(80) default NULL, -- optional authorised value PAYMENT_TYPE
2665
  `payment_type` varchar(80) default NULL, -- optional authorised value PAYMENT_TYPE
2665
  `amountoutstanding` decimal(28,6) default NULL,
2666
  `amountoutstanding` decimal(28,6) default NULL,
(-)a/installer/data/mysql/sysprefs.sql (+1 lines)
Lines 69-74 INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` Link Here
69
('AuthoritySeparator','--','10','Used to separate a list of authorities in a display. Usually --','free'),
69
('AuthoritySeparator','--','10','Used to separate a list of authorities in a display. Usually --','free'),
70
('autoBarcode','OFF','incremental|annual|hbyymmincr|EAN13|OFF','Used to autogenerate a barcode: incremental will be of the form 1, 2, 3; annual of the form 2007-0001, 2007-0002; hbyymmincr of the form HB08010001 where HB=Home Branch','Choice'),
70
('autoBarcode','OFF','incremental|annual|hbyymmincr|EAN13|OFF','Used to autogenerate a barcode: incremental will be of the form 1, 2, 3; annual of the form 2007-0001, 2007-0002; hbyymmincr of the form HB08010001 where HB=Home Branch','Choice'),
71
('AutoCreateAuthorities','0',NULL,'Automatically create authorities that do not exist when cataloging records.','YesNo'),
71
('AutoCreateAuthorities','0',NULL,'Automatically create authorities that do not exist when cataloging records.','YesNo'),
72
('AutoCreditNumber', '', '', 'Automatically generate a number for account credits', 'Choice'),
72
('AutoEmailOpacUser','0',NULL,'Sends notification emails containing new account details to patrons - when account is created.','YesNo'),
73
('AutoEmailOpacUser','0',NULL,'Sends notification emails containing new account details to patrons - when account is created.','YesNo'),
73
('AutoEmailPrimaryAddress','OFF','email|emailpro|B_email|cardnumber|OFF','Defines the default email address where \'Account Details\' emails are sent.','Choice'),
74
('AutoEmailPrimaryAddress','OFF','email|emailpro|B_email|cardnumber|OFF','Defines the default email address where \'Account Details\' emails are sent.','Choice'),
74
('AutoShareWithMana','subscription','','defines datas automatically shared with mana','multiple'),
75
('AutoShareWithMana','subscription','','defines datas automatically shared with mana','multiple'),
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/accounting.pref (+7 lines)
Lines 33-35 Accounting: Link Here
33
                yes: "Enable"
33
                yes: "Enable"
34
                no: "Disable"
34
                no: "Disable"
35
            - " the point of sale feature to allow anonymous transactions with the accounting system. (Requires UseCashRegisters)"
35
            - " the point of sale feature to allow anonymous transactions with the accounting system. (Requires UseCashRegisters)"
36
        -
37
            - pref: AutoCreditNumber
38
              choices:
39
                '': 'Do not automatically generate credit numbers'
40
                annual: 'Automatically generate credit numbers in the form <year>-0001'
41
                branchyyyymmincr: 'Automatically generate credit numbers in the form <branchcode>yyyymm0001'
42
                incremental: 'Automatically generate credit numbers in the form 1, 2, 3'
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/members/boraccount.tt (-1 / +3 lines)
Lines 45-50 Link Here
45
    <thead>
45
    <thead>
46
      <tr>
46
      <tr>
47
          <th class="title-string">Date</th>
47
          <th class="title-string">Date</th>
48
          <th>Credit number</th>
48
          <th>Account type</th>
49
          <th>Account type</th>
49
          <th>Description of charges</th>
50
          <th>Description of charges</th>
50
          <th>Barcode</th>
51
          <th>Barcode</th>
Lines 63-68 Link Here
63
64
64
   <tr>
65
   <tr>
65
   <td><span title="[% account.date | html %]">[% account.date |$KohaDates %]</span></td>
66
   <td><span title="[% account.date | html %]">[% account.date |$KohaDates %]</span></td>
67
        <td>[% account.credit_number | html %]</td>
66
        <td>[% PROCESS account_type_description account=account %]</td>
68
        <td>[% PROCESS account_type_description account=account %]</td>
67
      <td>
69
      <td>
68
        [%- IF account.payment_type %][% AuthorisedValues.GetByCode('PAYMENT_TYPE', account.payment_type) | html %][% END %]
70
        [%- IF account.payment_type %][% AuthorisedValues.GetByCode('PAYMENT_TYPE', account.payment_type) | html %][% END %]
Lines 100-106 Link Here
100
  [% END %]
102
  [% END %]
101
<tfoot>
103
<tfoot>
102
  <tr>
104
  <tr>
103
    <td colspan="9">Total due</td>
105
    <td colspan="10">Total due</td>
104
    [% IF ( totalcredit ) %]
106
    [% IF ( totalcredit ) %]
105
        <td class="credit" style="text-align: right;">[% total | $Price %]</td>
107
        <td class="credit" style="text-align: right;">[% total | $Price %]</td>
106
    [% ELSE %]
108
    [% ELSE %]
(-)a/t/db_dependent/Koha/Account.t (-1 / +48 lines)
Lines 1049-1051 subtest 'Koha::Account::Line::apply() handles lost items' => sub { Link Here
1049
1049
1050
    $schema->storage->txn_rollback;
1050
    $schema->storage->txn_rollback;
1051
};
1051
};
1052
- 
1052
1053
subtest 'Koha::Account::pay() generates credit number' => sub {
1054
    plan tests => 34;
1055
1056
    $schema->storage->txn_begin;
1057
1058
    Koha::Account::Lines->delete();
1059
1060
    my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
1061
    my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1062
    my $account = $patron->account;
1063
1064
    my $context = Test::MockModule->new('C4::Context');
1065
    $context->mock( 'userenv', { branch => $library->id } );
1066
1067
    my $now = DateTime->now;
1068
    my $year = $now->year;
1069
    my $month = $now->month;
1070
    my ($accountlines_id, $accountline);
1071
1072
    t::lib::Mocks::mock_preference('AutoCreditNumber', '');
1073
    $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
1074
    $accountline = Koha::Account::Lines->find($accountlines_id);
1075
    is($accountline->credit_number, undef, 'No credit number is generated when syspref is off');
1076
1077
    t::lib::Mocks::mock_preference('AutoCreditNumber', 'incremental');
1078
    for my $i (1..11) {
1079
        $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
1080
        $accountline = Koha::Account::Lines->find($accountlines_id);
1081
        is($accountline->credit_number, $i);
1082
    }
1083
1084
    t::lib::Mocks::mock_preference('AutoCreditNumber', 'annual');
1085
    for my $i (1..11) {
1086
        $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
1087
        $accountline = Koha::Account::Lines->find($accountlines_id);
1088
        is($accountline->credit_number, sprintf('%s-%04d', $year, $i));
1089
    }
1090
1091
    t::lib::Mocks::mock_preference('AutoCreditNumber', 'branchyyyymmincr');
1092
    for my $i (1..11) {
1093
        $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
1094
        $accountline = Koha::Account::Lines->find($accountlines_id);
1095
        is($accountline->credit_number, sprintf('%s%d%02d%04d', $library->id, $year, $month, $i));
1096
    }
1097
1098
    $schema->storage->txn_rollback;
1099
};

Return to bug 19036