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

(-)a/Koha/Account/Line.pm (+58 lines)
Lines 762-767 sub to_api_mapping { Link Here
762
    };
762
    };
763
}
763
}
764
764
765
=head3 store
766
767
Specific store method to generate credit number before saving
768
769
=cut
770
771
sub store {
772
    my ($self) = @_;
773
774
    my $AutoCreditNumber = C4::Context->preference('AutoCreditNumber');
775
    if ($AutoCreditNumber && !$self->in_storage && $self->is_credit && !$self->credit_number) {
776
        my $rs = Koha::Database->new->schema->resultset($self->_type);
777
778
        if ($AutoCreditNumber eq 'incremental') {
779
            my $max = $rs->search({
780
                credit_number => { -regexp => '^[0-9]+$' }
781
            }, {
782
                select => \'CAST(credit_number AS UNSIGNED)',
783
                as => ['credit_number'],
784
            })->get_column('credit_number')->max;
785
            $max //= 0;
786
            $self->credit_number($max + 1);
787
        } elsif ($AutoCreditNumber eq 'annual') {
788
            my $now = DateTime->now;
789
            my $prefix = sprintf('%d-', $now->year);
790
            my $max = $rs->search({
791
                -and => [
792
                    credit_number => { -regexp => '[0-9]{4}$' },
793
                    credit_number => { -like => "$prefix%" },
794
                ],
795
            })->get_column('credit_number')->max;
796
            $max //= $prefix . '0000';
797
            my $incr = substr($max, length $prefix);
798
            $self->credit_number(sprintf('%s%04d', $prefix, $incr + 1));
799
        } elsif ($AutoCreditNumber eq 'branchyyyymmincr') {
800
            my $userenv = C4::Context->userenv;
801
            if ($userenv) {
802
                my $branch = $userenv->{branch};
803
                my $now = DateTime->now;
804
                my $prefix = sprintf('%s%d%02d', $branch, $now->year, $now->month);
805
                my $pattern = $prefix;
806
                $pattern =~ s/([\?%_])/\\$1/g;
807
                my $max = $rs->search({
808
                    -and => [
809
                        credit_number => { -regexp => '[0-9]{4}$' },
810
                        credit_number => { -like => "$pattern%" },
811
                    ],
812
                })->get_column('credit_number')->max;
813
                $max //= $prefix . '0000';
814
                my $incr = substr($max, length $prefix);
815
                $self->credit_number(sprintf('%s%04d', $prefix, $incr + 1));
816
            }
817
        }
818
    }
819
820
    return $self->SUPER::store();
821
}
822
765
=head2 Internal methods
823
=head2 Internal methods
766
824
767
=cut
825
=cut
(-)a/admin/columns_settings.yml (+3 lines)
Lines 516-521 modules: Link Here
516
      account-fines:
516
      account-fines:
517
        -
517
        -
518
          columnname: date
518
          columnname: date
519
        -
520
          columnname: credit_number
521
          is_hidden: 1
519
        -
522
        -
520
          columnname: account_type
523
          columnname: account_type
521
        -
524
        -
(-)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 2654-2659 CREATE TABLE `accountlines` ( Link Here
2654
  `description` LONGTEXT,
2654
  `description` LONGTEXT,
2655
  `credit_type_code` varchar(80) default NULL,
2655
  `credit_type_code` varchar(80) default NULL,
2656
  `debit_type_code` varchar(80) default NULL,
2656
  `debit_type_code` varchar(80) default NULL,
2657
  `credit_number` varchar(20) NULL DEFAULT NULL COMMENT 'autogenerated number for credits',
2657
  `status` varchar(16) default NULL,
2658
  `status` varchar(16) default NULL,
2658
  `payment_type` varchar(80) default NULL, -- optional authorised value PAYMENT_TYPE
2659
  `payment_type` varchar(80) default NULL, -- optional authorised value PAYMENT_TYPE
2659
  `amountoutstanding` decimal(28,6) default NULL,
2660
  `amountoutstanding` decimal(28,6) default NULL,
(-)a/installer/data/mysql/sysprefs.sql (+1 lines)
Lines 67-72 INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` Link Here
67
('AuthoritySeparator','--','10','Used to separate a list of authorities in a display. Usually --','free'),
67
('AuthoritySeparator','--','10','Used to separate a list of authorities in a display. Usually --','free'),
68
('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'),
68
('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'),
69
('AutoCreateAuthorities','0',NULL,'Automatically create authorities that do not exist when cataloging records.','YesNo'),
69
('AutoCreateAuthorities','0',NULL,'Automatically create authorities that do not exist when cataloging records.','YesNo'),
70
('AutoCreditNumber', '', '', 'Automatically generate a number for account credits', 'Choice'),
70
('AutoEmailOpacUser','0',NULL,'Sends notification emails containing new account details to patrons - when account is created.','YesNo'),
71
('AutoEmailOpacUser','0',NULL,'Sends notification emails containing new account details to patrons - when account is created.','YesNo'),
71
('AutoEmailPrimaryAddress','OFF','email|emailpro|B_email|cardnumber|OFF','Defines the default email address where \'Account Details\' emails are sent.','Choice'),
72
('AutoEmailPrimaryAddress','OFF','email|emailpro|B_email|cardnumber|OFF','Defines the default email address where \'Account Details\' emails are sent.','Choice'),
72
('AutoShareWithMana','subscription','','defines datas automatically shared with mana','multiple'),
73
('AutoShareWithMana','subscription','','defines datas automatically shared with mana','multiple'),
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/accounting.pref (+7 lines)
Lines 27-29 Accounting: Link Here
27
                yes: "Enable"
27
                yes: "Enable"
28
                no: "Disable"
28
                no: "Disable"
29
            - " the point of sale feature to allow anonymous transactions with the accounting system. (Requires UseCashRegisters)"
29
            - " the point of sale feature to allow anonymous transactions with the accounting system. (Requires UseCashRegisters)"
30
        -
31
            - pref: AutoCreditNumber
32
              choices:
33
                '': 'Do not automatically generate credit numbers'
34
                annual: 'Automatically generate credit numbers in the form <year>-0001'
35
                branchyyyymmincr: 'Automatically generate credit numbers in the form <branchcode>yyyymm0001'
36
                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 44-49 Link Here
44
    <thead>
44
    <thead>
45
      <tr>
45
      <tr>
46
          <th class="title-string">Date</th>
46
          <th class="title-string">Date</th>
47
          <th>Credit number</th>
47
          <th>Account type</th>
48
          <th>Account type</th>
48
          <th>Description of charges</th>
49
          <th>Description of charges</th>
49
          <th>Barcode</th>
50
          <th>Barcode</th>
Lines 62-67 Link Here
62
63
63
   <tr>
64
   <tr>
64
   <td><span title="[% account.date | html %]">[% account.date |$KohaDates %]</span></td>
65
   <td><span title="[% account.date | html %]">[% account.date |$KohaDates %]</span></td>
66
        <td>[% account.credit_number | html %]</td>
65
        <td>[% PROCESS account_type_description account=account %]</td>
67
        <td>[% PROCESS account_type_description account=account %]</td>
66
      <td>
68
      <td>
67
        [%- IF account.payment_type %][% AuthorisedValues.GetByCode('PAYMENT_TYPE', account.payment_type) | html %][% END %]
69
        [%- IF account.payment_type %][% AuthorisedValues.GetByCode('PAYMENT_TYPE', account.payment_type) | html %][% END %]
(-)a/t/db_dependent/Koha/Account.t (-2 / +49 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 11;
22
use Test::More tests => 12;
23
use Test::MockModule;
23
use Test::MockModule;
24
use Test::Exception;
24
use Test::Exception;
25
25
Lines 979-981 subtest 'Koha::Account::Line::apply() handles lost items' => sub { Link Here
979
979
980
    $schema->storage->txn_rollback;
980
    $schema->storage->txn_rollback;
981
};
981
};
982
- 
982
983
subtest 'Koha::Account::pay() generates credit number' => sub {
984
    plan tests => 34;
985
986
    $schema->storage->txn_begin;
987
988
    Koha::Account::Lines->delete();
989
990
    my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
991
    my $library = $builder->build_object( { class => 'Koha::Libraries' } );
992
    my $account = $patron->account;
993
994
    my $context = Test::MockModule->new('C4::Context');
995
    $context->mock( 'userenv', { branch => $library->id } );
996
997
    my $now = DateTime->now;
998
    my $year = $now->year;
999
    my $month = $now->month;
1000
    my ($accountlines_id, $accountline);
1001
1002
    t::lib::Mocks::mock_preference('AutoCreditNumber', '');
1003
    $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id });
1004
    $accountline = Koha::Account::Lines->find($accountlines_id);
1005
    is($accountline->credit_number, undef, 'No credit number is generated when syspref is off');
1006
1007
    t::lib::Mocks::mock_preference('AutoCreditNumber', 'incremental');
1008
    for my $i (1..11) {
1009
        $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id });
1010
        $accountline = Koha::Account::Lines->find($accountlines_id);
1011
        is($accountline->credit_number, $i);
1012
    }
1013
1014
    t::lib::Mocks::mock_preference('AutoCreditNumber', 'annual');
1015
    for my $i (1..11) {
1016
        $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id });
1017
        $accountline = Koha::Account::Lines->find($accountlines_id);
1018
        is($accountline->credit_number, sprintf('%s-%04d', $year, $i));
1019
    }
1020
1021
    t::lib::Mocks::mock_preference('AutoCreditNumber', 'branchyyyymmincr');
1022
    for my $i (1..11) {
1023
        $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id });
1024
        $accountline = Koha::Account::Lines->find($accountlines_id);
1025
        is($accountline->credit_number, sprintf('%s%d%02d%04d', $library->id, $year, $month, $i));
1026
    }
1027
1028
    $schema->storage->txn_rollback;
1029
};

Return to bug 19036