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

(-)a/C4/Accounts.pm (-20 / +35 lines)
Lines 27-32 use C4::Circulation qw(ReturnLostItem); Link Here
27
use C4::Log qw(logaction);
27
use C4::Log qw(logaction);
28
use Koha::Account;
28
use Koha::Account;
29
use Koha::Account::Lines;
29
use Koha::Account::Lines;
30
use Koha::DateUtils qw(dt_from_string);
30
31
31
use Data::Dumper qw(Dumper);
32
use Data::Dumper qw(Dumper);
32
33
Lines 136-150 sub chargelostitem{ Link Here
136
    unless ($existing_charge_hashref) {
137
    unless ($existing_charge_hashref) {
137
        my $manager_id = 0;
138
        my $manager_id = 0;
138
        $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
139
        $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
140
        my $branchcode = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
141
139
        # This item is on issue ... add replacement cost to the borrower's record and mark it returned
142
        # This item is on issue ... add replacement cost to the borrower's record and mark it returned
140
        #  Note that we add this to the account even if there's no replacement price, allowing some other
143
        #  Note that we add this to the account even if there's no replacement price, allowing some other
141
        #  process (or person) to update it, since we don't handle any defaults for replacement prices.
144
        #  process (or person) to update it, since we don't handle any defaults for replacement prices.
142
        my $accountno = getnextacctno($borrowernumber);
145
        my $accountno = getnextacctno($borrowernumber);
143
        my $sth2=$dbh->prepare("INSERT INTO accountlines
146
        Koha::Account::Line->new(
144
        (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding,itemnumber,manager_id)
147
            {
145
        VALUES (?,?,now(),?,?,'L',?,?,?)");
148
                borrowernumber    => $borrowernumber,
146
        $sth2->execute($borrowernumber,$accountno,$amount,
149
                accountno         => $accountno,
147
        $description,$amount,$itemnumber,$manager_id);
150
                date              => dt_from_string(),
151
                amount            => $amount,
152
                description       => $description,
153
                accounttype       => 'L',
154
                amountoutstanding => $amount,
155
                itemnumber        => $itemnumber,
156
                manager_id        => $manager_id,
157
                branchcode        => $branchcode,
158
            }
159
        )->store();
148
160
149
        if ( C4::Context->preference("FinesLog") ) {
161
        if ( C4::Context->preference("FinesLog") ) {
150
            logaction("FINES", 'CREATE', $borrowernumber, Dumper({
162
            logaction("FINES", 'CREATE', $borrowernumber, Dumper({
Lines 208-228 sub manualinvoice { Link Here
208
        $notifyid = 1;
220
        $notifyid = 1;
209
    }
221
    }
210
222
211
    if ( $itemnum ) {
223
    my $branchcode = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
212
        $desc .= ' ' . $itemnum;
224
213
        my $sth = $dbh->prepare(
225
    Koha::Account::Line->new(
214
            'INSERT INTO  accountlines
226
        {
215
                        (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding, itemnumber,notify_id, note, manager_id)
227
            borrowernumber    => $borrowernumber,
216
        VALUES (?, ?, now(), ?,?, ?,?,?,?,?,?)');
228
            accountno         => $accountno,
217
     $sth->execute($borrowernumber, $accountno, $amount, $desc, $type, $amountleft, $itemnum,$notifyid, $note, $manager_id) || return $sth->errstr;
229
            date              => dt_from_string(),
218
  } else {
230
            amount            => $amount,
219
    my $sth=$dbh->prepare("INSERT INTO  accountlines
231
            description       => $desc,
220
            (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding,notify_id, note, manager_id)
232
            accounttype       => $type,
221
            VALUES (?, ?, now(), ?, ?, ?, ?,?,?,?)"
233
            amountoutstanding => $amountleft,
222
        );
234
            itemnumber        => $itemnum,
223
        $sth->execute( $borrowernumber, $accountno, $amount, $desc, $type,
235
            notify_id         => $notifyid,
224
            $amountleft, $notifyid, $note, $manager_id );
236
            note              => $note,
225
    }
237
            manager_id        => $manager_id,
238
            branchcode        => $branchcode,
239
        }
240
    )->store();
226
241
227
    if ( C4::Context->preference("FinesLog") ) {
242
    if ( C4::Context->preference("FinesLog") ) {
228
        logaction("FINES", 'CREATE',$borrowernumber,Dumper({
243
        logaction("FINES", 'CREATE',$borrowernumber,Dumper({
(-)a/C4/Circulation.pm (-26 / +57 lines)
Lines 50-55 use Koha::Patrons; Link Here
50
use Koha::Patron::Debarments;
50
use Koha::Patron::Debarments;
51
use Koha::Database;
51
use Koha::Database;
52
use Koha::Libraries;
52
use Koha::Libraries;
53
use Koha::Account::Lines;
53
use Koha::Holds;
54
use Koha::Holds;
54
use Koha::RefundLostItemFeeRule;
55
use Koha::RefundLostItemFeeRule;
55
use Koha::RefundLostItemFeeRules;
56
use Koha::RefundLostItemFeeRules;
Lines 2395-2401 sub _FixAccountForLostAndReturned { Link Here
2395
        WHERE (accountlines_id = ?)");
2396
        WHERE (accountlines_id = ?)");
2396
    $usth->execute($data->{'accountlines_id'});      # We might be adjusting an account for some OTHER borrowernumber now.  Not the one we passed in.
2397
    $usth->execute($data->{'accountlines_id'});      # We might be adjusting an account for some OTHER borrowernumber now.  Not the one we passed in.
2397
    #check if any credit is left if so writeoff other accounts
2398
    #check if any credit is left if so writeoff other accounts
2398
    my $nextaccntno = getnextacctno($data->{'borrowernumber'});
2399
    my $nextaccntno = C4::Accounts::getnextacctno($data->{'borrowernumber'});
2399
    $amountleft *= -1 if ($amountleft < 0);
2400
    $amountleft *= -1 if ($amountleft < 0);
2400
    if ($amountleft > 0) {
2401
    if ($amountleft > 0) {
2401
        my $msth = $dbh->prepare("SELECT * FROM accountlines WHERE (borrowernumber = ?)
2402
        my $msth = $dbh->prepare("SELECT * FROM accountlines WHERE (borrowernumber = ?)
Lines 2425-2435 sub _FixAccountForLostAndReturned { Link Here
2425
        }
2426
        }
2426
    }
2427
    }
2427
    $amountleft *= -1 if ($amountleft > 0);
2428
    $amountleft *= -1 if ($amountleft > 0);
2429
2430
    my $branchcode = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
2431
2428
    my $desc = "Item Returned " . $item_id;
2432
    my $desc = "Item Returned " . $item_id;
2429
    $usth = $dbh->prepare("INSERT INTO accountlines
2433
2430
        (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
2434
    Koha::Account::Line->new(
2431
        VALUES (?,?,now(),?,?,'CR',?)");
2435
        {
2432
    $usth->execute($data->{'borrowernumber'},$nextaccntno,0-$amount,$desc,$amountleft);
2436
            borrowernumber    => $data->{borrowernumber},
2437
            accountno         => $nextaccntno,
2438
            date              => dt_from_string(),
2439
            amount            => 0 - $amount,
2440
            description       => $desc,
2441
            accounttype       => 'CR',
2442
            amountoutstanding => $amountleft,
2443
            branchcode        => $branchcode,
2444
        }
2445
    )->store();
2446
2433
    if ($borrowernumber) {
2447
    if ($borrowernumber) {
2434
        # FIXME: same as query above.  use 1 sth for both
2448
        # FIXME: same as query above.  use 1 sth for both
2435
        $usth = $dbh->prepare("INSERT INTO accountoffsets
2449
        $usth = $dbh->prepare("INSERT INTO accountoffsets
Lines 2858-2875 sub AddRenewal { Link Here
2858
    # Charge a new rental fee, if applicable?
2872
    # Charge a new rental fee, if applicable?
2859
    my ( $charge, $type ) = GetIssuingCharges( $itemnumber, $borrowernumber );
2873
    my ( $charge, $type ) = GetIssuingCharges( $itemnumber, $borrowernumber );
2860
    if ( $charge > 0 ) {
2874
    if ( $charge > 0 ) {
2861
        my $accountno = getnextacctno( $borrowernumber );
2875
        my $accountno = C4::Accounts::getnextacctno( $borrowernumber );
2862
        my $manager_id = 0;
2876
        my $manager_id = 0;
2863
        $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv; 
2877
        $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv; 
2864
        $sth = $dbh->prepare(
2878
        my $branchcode = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
2865
                "INSERT INTO accountlines
2879
        Koha::Account::Line->new(
2866
                    (date, borrowernumber, accountno, amount, manager_id,
2880
            {
2867
                    description,accounttype, amountoutstanding, itemnumber)
2881
                date              => dt_from_string(),
2868
                    VALUES (now(),?,?,?,?,?,?,?,?)"
2882
                borrowernumber    => $borrowernumber,
2869
        );
2883
                accountno         => $accountno,
2870
        $sth->execute( $borrowernumber, $accountno, $charge, $manager_id,
2884
                amount            => $charge,
2871
            "Renewal of Rental Item " . $biblio->title . " $item->{'barcode'}",
2885
                manager_id        => $manager_id,
2872
            'Rent', $charge, $itemnumber );
2886
                accounttype       => 'Rent',
2887
                amountoutstanding => $charge,
2888
                itemnumber        => $itemnumber,
2889
                branchcode        => $branchcode,
2890
                description       => 'Renewal of Rental Item '
2891
                  . $biblio->title
2892
                  . " $item->{'barcode'}",
2893
            }
2894
        )->store();
2873
    }
2895
    }
2874
2896
2875
    # Send a renewal slip according to checkout alert preferencei
2897
    # Send a renewal slip according to checkout alert preferencei
Lines 3194-3212 sub _get_discount_from_rule { Link Here
3194
3216
3195
sub AddIssuingCharge {
3217
sub AddIssuingCharge {
3196
    my ( $itemnumber, $borrowernumber, $charge ) = @_;
3218
    my ( $itemnumber, $borrowernumber, $charge ) = @_;
3197
    my $dbh = C4::Context->dbh;
3219
3198
    my $nextaccntno = getnextacctno( $borrowernumber );
3220
    my $nextaccntno = getnextacctno($borrowernumber);
3221
3199
    my $manager_id = 0;
3222
    my $manager_id = 0;
3200
    $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
3223
    $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
3201
    my $query ="
3224
3202
        INSERT INTO accountlines
3225
    my $branchcode = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
3203
            (borrowernumber, itemnumber, accountno,
3226
3204
            date, amount, description, accounttype,
3227
    Koha::Account::Line->new(
3205
            amountoutstanding, manager_id)
3228
        {
3206
        VALUES (?, ?, ?,now(), ?, 'Rental', 'Rent',?,?)
3229
            date              => dt_from_string(),
3207
    ";
3230
            borrowernumber    => $borrowernumber,
3208
    my $sth = $dbh->prepare($query);
3231
            accountno         => $nextaccntno,
3209
    $sth->execute( $borrowernumber, $itemnumber, $nextaccntno, $charge, $charge, $manager_id );
3232
            amount            => $charge,
3233
            manager_id        => $manager_id,
3234
            accounttype       => 'Rent',
3235
            amountoutstanding => $charge,
3236
            itemnumber        => $itemnumber,
3237
            branchcode        => $branchcode,
3238
            description       => 'Rental',
3239
        }
3240
    )->store();
3210
}
3241
}
3211
3242
3212
=head2 GetTransfers
3243
=head2 GetTransfers
(-)a/C4/Reserves.pm (-7 / +18 lines)
Lines 48-53 use Koha::IssuingRules; Link Here
48
use Koha::Items;
48
use Koha::Items;
49
use Koha::ItemTypes;
49
use Koha::ItemTypes;
50
use Koha::Patrons;
50
use Koha::Patrons;
51
use Koha::Account::Lines;
51
52
52
use List::MoreUtils qw( firstidx any );
53
use List::MoreUtils qw( firstidx any );
53
use Carp;
54
use Carp;
Lines 559-571 sub GetOtherReserves { Link Here
559
560
560
sub ChargeReserveFee {
561
sub ChargeReserveFee {
561
    my ( $borrowernumber, $fee, $title ) = @_;
562
    my ( $borrowernumber, $fee, $title ) = @_;
562
    return if !$fee || $fee==0; # the last test is needed to include 0.00
563
563
    my $accquery = qq{
564
    return if !$fee || $fee == 0;    # the last test is needed to include 0.00
564
INSERT INTO accountlines ( borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding ) VALUES (?, ?, NOW(), ?, ?, 'Res', ?)
565
565
    };
566
    my $branchcode = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
566
    my $dbh = C4::Context->dbh;
567
    my $nextacctno = C4::Accounts::getnextacctno($borrowernumber);
567
    my $nextacctno = &getnextacctno( $borrowernumber );
568
568
    $dbh->do( $accquery, undef, ( $borrowernumber, $nextacctno, $fee, "Reserve Charge - $title", $fee ) );
569
    Koha::Account::Line->new(
570
        {
571
            borrowernumber    => $borrowernumber,
572
            accountno         => $nextacctno,
573
            date              => dt_from_string(),
574
            amount            => $fee,
575
            description       => "Reserve Charge - $title",
576
            accounttype       => 'Res',
577
            amountoutstanding => $fee,
578
        }
579
    )->store();
569
}
580
}
570
581
571
=head2 GetReserveFee
582
=head2 GetReserveFee
(-)a/Koha/Account.pm (-2 / +4 lines)
Lines 68-73 sub pay { Link Here
68
    my $library_id      = $params->{library_id};
68
    my $library_id      = $params->{library_id};
69
    my $lines           = $params->{lines};
69
    my $lines           = $params->{lines};
70
    my $type            = $params->{type} || 'payment';
70
    my $type            = $params->{type} || 'payment';
71
    my $branchcode      = $params->{branchcode};
72
73
    $library_id ||= C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
71
74
72
    my $userenv = C4::Context->userenv;
75
    my $userenv = C4::Context->userenv;
73
76
Lines 191-202 sub pay { Link Here
191
            accounttype       => $account_type,
194
            accounttype       => $account_type,
192
            amountoutstanding => 0 - $balance_remaining,
195
            amountoutstanding => 0 - $balance_remaining,
193
            manager_id        => $manager_id,
196
            manager_id        => $manager_id,
197
            branchcode        => $library_id,
194
            note              => $note,
198
            note              => $note,
195
        }
199
        }
196
    )->store();
200
    )->store();
197
201
198
    $library_id ||= $userenv ? $userenv->{'branch'} : undef;
199
200
    UpdateStats(
202
    UpdateStats(
201
        {
203
        {
202
            branch         => $library_id,
204
            branch         => $library_id,
(-)a/t/db_dependent/Accounts.t (-3 / +49 lines)
Lines 18-24 Link Here
18
18
19
use Modern::Perl;
19
use Modern::Perl;
20
20
21
use Test::More tests => 22;
21
use Test::More tests => 35;
22
use Test::MockModule;
22
use Test::MockModule;
23
use Test::Warn;
23
use Test::Warn;
24
24
Lines 70-75 $context->mock( 'userenv', sub { Link Here
70
        branch => $branchcode,
70
        branch => $branchcode,
71
    };
71
    };
72
});
72
});
73
my $userenv_branchcode = $branchcode;
74
75
# Test chargelostitem
76
my $item   = $builder->build( { source => 'Item' } );
77
my $patron = $builder->build( { source => 'Borrower' } );
78
my $amount = '5.000000';
79
my $description = "Test fee!";
80
chargelostitem( $patron->{borrowernumber}, $item->{itemnumber}, $amount, $description );
81
my ($accountline) = Koha::Account::Lines->search(
82
    {
83
        borrowernumber => $patron->{borrowernumber}
84
    }
85
);
86
is( $accountline->amount, $amount, 'Accountline amount set correctly for chargelostitem' );
87
is( $accountline->description, $description, 'Accountline description set correctly for chargelostitem' );
88
is( $accountline->branchcode, $branchcode, 'Accountline branchcode set correctly for chargelostitem' );
89
$dbh->do(q|DELETE FROM accountlines|);
90
91
# Test manualinvoice, reuse some of the vars from testing chargelostitem
92
my $type = 'L';
93
my $note = 'Test note!';
94
manualinvoice( $patron->{borrowernumber}, $item->{itemnumber}, $description, $type, $amount, $note );
95
($accountline) = Koha::Account::Lines->search(
96
    {
97
        borrowernumber => $patron->{borrowernumber}
98
    }
99
);
100
is( $accountline->accounttype, $type, 'Accountline type set correctly for manualinvoice' );
101
is( $accountline->amount, $amount, 'Accountline amount set correctly for manualinvoice' );
102
ok( $accountline->description =~ /^$description/, 'Accountline description set correctly for manualinvoice' );
103
is( $accountline->note, $note, 'Accountline note set correctly for manualinvoice' );
104
is( $accountline->branchcode, $branchcode, 'Accountline branchcode set correctly for manualinvoice' );
105
106
# Test _FixAccountForLostAndReturned, use the accountline from the manualinvoice to test
107
C4::Circulation::_FixAccountForLostAndReturned( $item->{itemnumber} );
108
my ( $accountline_fee, $accountline_payment ) = Koha::Account::Lines->search(
109
    {
110
        borrowernumber => $patron->{borrowernumber}
111
    }
112
);
113
is( $accountline_fee->accounttype, 'LR', 'Lost item fee account type updated to LR' );
114
is( $accountline_fee->amountoutstanding, '0.000000', 'Lost item fee amount outstanding updated to 0' );
115
is( $accountline_payment->accounttype, 'CR', 'Lost item fee account type is CR' );
116
is( $accountline_payment->amount, "-$amount", 'Lost item refund amount is correct' );
117
is( $accountline_payment->branchcode, $branchcode, 'Lost item refund branchcode is set correctly' );
118
$dbh->do(q|DELETE FROM accountlines|);
73
119
74
# Testing purge_zero_balance_fees
120
# Testing purge_zero_balance_fees
75
121
Lines 135-141 $dbh->do(q|DELETE FROM accountlines|); Link Here
135
181
136
subtest "Koha::Account::pay tests" => sub {
182
subtest "Koha::Account::pay tests" => sub {
137
183
138
    plan tests => 12;
184
    plan tests => 13;
139
185
140
    # Create a borrower
186
    # Create a borrower
141
    my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
187
    my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
Lines 255-260 subtest "Koha::Account::pay tests" => sub { Link Here
255
    is( $payment->amount(), '-42.000000', "Payment paid the specified fine" );
301
    is( $payment->amount(), '-42.000000', "Payment paid the specified fine" );
256
    $line3 = Koha::Account::Lines->find( $line3->id );
302
    $line3 = Koha::Account::Lines->find( $line3->id );
257
    is( $line3->amountoutstanding, '0.000000', "Specified fine is paid" );
303
    is( $line3->amountoutstanding, '0.000000', "Specified fine is paid" );
304
    is( $payment->branchcode, $userenv_branchcode, 'Branchcode set correctly' );
258
};
305
};
259
306
260
subtest "Koha::Account::pay particular line tests" => sub {
307
subtest "Koha::Account::pay particular line tests" => sub {
261
- 

Return to bug 19066