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

(-)a/Koha/Account/Line.pm (+58 lines)
Lines 876-881 sub renew_item { Link Here
876
876
877
}
877
}
878
878
879
=head3 store
880
881
Specific store method to generate credit number before saving
882
883
=cut
884
885
sub store {
886
    my ($self) = @_;
887
888
    my $AutoCreditNumber = C4::Context->preference('AutoCreditNumber');
889
    if ($AutoCreditNumber && !$self->in_storage && $self->is_credit && !$self->credit_number) {
890
        my $rs = Koha::Database->new->schema->resultset($self->_type);
891
892
        if ($AutoCreditNumber eq 'incremental') {
893
            my $max = $rs->search({
894
                credit_number => { -regexp => '^[0-9]+$' }
895
            }, {
896
                select => \'CAST(credit_number AS UNSIGNED)',
897
                as => ['credit_number'],
898
            })->get_column('credit_number')->max;
899
            $max //= 0;
900
            $self->credit_number($max + 1);
901
        } elsif ($AutoCreditNumber eq 'annual') {
902
            my $now = DateTime->now;
903
            my $prefix = sprintf('%d-', $now->year);
904
            my $max = $rs->search({
905
                -and => [
906
                    credit_number => { -regexp => '[0-9]{4}$' },
907
                    credit_number => { -like => "$prefix%" },
908
                ],
909
            })->get_column('credit_number')->max;
910
            $max //= $prefix . '0000';
911
            my $incr = substr($max, length $prefix);
912
            $self->credit_number(sprintf('%s%04d', $prefix, $incr + 1));
913
        } elsif ($AutoCreditNumber eq 'branchyyyymmincr') {
914
            my $userenv = C4::Context->userenv;
915
            if ($userenv) {
916
                my $branch = $userenv->{branch};
917
                my $now = DateTime->now;
918
                my $prefix = sprintf('%s%d%02d', $branch, $now->year, $now->month);
919
                my $pattern = $prefix;
920
                $pattern =~ s/([\?%_])/\\$1/g;
921
                my $max = $rs->search({
922
                    -and => [
923
                        credit_number => { -regexp => '[0-9]{4}$' },
924
                        credit_number => { -like => "$pattern%" },
925
                    ],
926
                })->get_column('credit_number')->max;
927
                $max //= $prefix . '0000';
928
                my $incr = substr($max, length $prefix);
929
                $self->credit_number(sprintf('%s%04d', $prefix, $incr + 1));
930
            }
931
        }
932
    }
933
934
    return $self->SUPER::store();
935
}
936
879
=head2 Internal methods
937
=head2 Internal methods
880
938
881
=cut
939
=cut
(-)a/admin/columns_settings.yml (+3 lines)
Lines 651-656 modules: Link Here
651
            -
651
            -
652
              columnname: date
652
              columnname: date
653
            -
653
            -
654
              columnname: credit_number
655
              is_hidden: 1
656
            -
654
              columnname: account_type
657
              columnname: account_type
655
            -
658
            -
656
              columnname: description
659
              columnname: description
(-)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 2723-2728 CREATE TABLE `accountlines` ( Link Here
2723
  `description` LONGTEXT,
2723
  `description` LONGTEXT,
2724
  `credit_type_code` varchar(80) default NULL,
2724
  `credit_type_code` varchar(80) default NULL,
2725
  `debit_type_code` varchar(80) default NULL,
2725
  `debit_type_code` varchar(80) default NULL,
2726
  `credit_number` varchar(20) NULL DEFAULT NULL COMMENT 'autogenerated number for credits',
2726
  `status` varchar(16) default NULL,
2727
  `status` varchar(16) default NULL,
2727
  `payment_type` varchar(80) default NULL, -- optional authorised value PAYMENT_TYPE
2728
  `payment_type` varchar(80) default NULL, -- optional authorised value PAYMENT_TYPE
2728
  `amountoutstanding` decimal(28,6) default NULL,
2729
  `amountoutstanding` decimal(28,6) default NULL,
(-)a/installer/data/mysql/sysprefs.sql (+1 lines)
Lines 70-75 INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` Link Here
70
('AuthSuccessLog','',NULL,'If enabled, log successful authentications','YesNo'),
70
('AuthSuccessLog','',NULL,'If enabled, log successful authentications','YesNo'),
71
('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
('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'),
72
('AutoCreateAuthorities','0',NULL,'Automatically create authorities that do not exist when cataloging records.','YesNo'),
72
('AutoCreateAuthorities','0',NULL,'Automatically create authorities that do not exist when cataloging records.','YesNo'),
73
('AutoCreditNumber', '', '', 'Automatically generate a number for account credits', 'Choice'),
73
('AutoEmailOpacUser','0',NULL,'Sends notification emails containing new account details to patrons - when account is created.','YesNo'),
74
('AutoEmailOpacUser','0',NULL,'Sends notification emails containing new account details to patrons - when account is created.','YesNo'),
74
('AutoEmailPrimaryAddress','OFF','email|emailpro|B_email|cardnumber|OFF','Defines the default email address where \'Account Details\' emails are sent.','Choice'),
75
('AutoEmailPrimaryAddress','OFF','email|emailpro|B_email|cardnumber|OFF','Defines the default email address where \'Account Details\' emails are sent.','Choice'),
75
('AutoShareWithMana','subscription','','defines datas automatically shared with mana','multiple'),
76
('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 (+2 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 64-69 Link Here
64
65
65
   <tr>
66
   <tr>
66
   <td><span title="[% account.date | html %]">[% account.date |$KohaDates %]</span></td>
67
   <td><span title="[% account.date | html %]">[% account.date |$KohaDates %]</span></td>
68
        <td>[% account.credit_number | html %]</td>
67
        <td>[% PROCESS account_type_description account=account %]</td>
69
        <td>[% PROCESS account_type_description account=account %]</td>
68
      <td>
70
      <td>
69
        [%- IF account.payment_type %][% AuthorisedValues.GetByCode('PAYMENT_TYPE', account.payment_type) | html %][% END %]
71
        [%- IF account.payment_type %][% AuthorisedValues.GetByCode('PAYMENT_TYPE', account.payment_type) | html %][% END %]
(-)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