| Lines 26-31
          use C4::Members;
      
      
        Link Here | 
        
          | 26 | use C4::Circulation qw(ReturnLostItem); | 26 | use C4::Circulation qw(ReturnLostItem); | 
        
          | 27 | use C4::Log qw(logaction); | 27 | use C4::Log qw(logaction); | 
        
          | 28 | use Koha::Account; | 28 | use Koha::Account; | 
            
              |  |  | 29 | use Koha::Account::Line; | 
            
              | 30 | use Koha::Account::Lines; | 
            
              | 31 | use Koha::Account::Offset; | 
        
          | 29 |  | 32 |  | 
        
          | 30 | use Data::Dumper qw(Dumper); | 33 | use Data::Dumper qw(Dumper); | 
        
          | 31 |  | 34 |  | 
  
    | Lines 118-150
          EOT
      
      
        Link Here | 
        
          | 118 |  | 121 |  | 
        
          | 119 | =cut | 122 | =cut | 
        
          | 120 |  | 123 |  | 
            
              |  |  | 124 | =head2 chargelostitem | 
            
              | 125 |  | 
            
              | 126 | In a default install of Koha the following lost values are set | 
            
              | 127 | 1 = Lost | 
            
              | 128 | 2 = Long overdue | 
            
              | 129 | 3 = Lost and paid for | 
            
              | 130 |  | 
            
              | 131 | FIXME: itemlost should be set to 3 after payment is made, should be a warning to the interface that a charge has been added | 
            
              | 132 | FIXME : if no replacement price, borrower just doesn't get charged? | 
            
              | 133 |  | 
            
              | 134 | =cut | 
            
              | 135 |  | 
        
          | 121 | sub chargelostitem{ | 136 | sub chargelostitem{ | 
            
              | 122 | # lost ==1 Lost, lost==2 longoverdue, lost==3 lost and paid for |  |  | 
            
              | 123 | # FIXME: itemlost should be set to 3 after payment is made, should be a warning to the interface that | 
            
              | 124 | # a charge has been added | 
            
              | 125 | # FIXME : if no replacement price, borrower just doesn't get charged? | 
        
          | 126 |     my $dbh = C4::Context->dbh(); | 137 |     my $dbh = C4::Context->dbh(); | 
        
          | 127 |     my ($borrowernumber, $itemnumber, $amount, $description) = @_; | 138 |     my ($borrowernumber, $itemnumber, $amount, $description) = @_; | 
        
          | 128 |  | 139 |  | 
        
          | 129 |     # first make sure the borrower hasn't already been charged for this item | 140 |     # first make sure the borrower hasn't already been charged for this item | 
          
            
              | 130 |     my $sth1=$dbh->prepare("SELECT * from accountlines | 141 |     my $existing_charges = Koha::Account::Lines->search( | 
            
              | 131 |     WHERE borrowernumber=? AND itemnumber=? and accounttype='L'"); | 142 |         { | 
            
              | 132 |     $sth1->execute($borrowernumber,$itemnumber); | 143 |             borrowernumber => $borrowernumber, | 
            
              | 133 |     my $existing_charge_hashref=$sth1->fetchrow_hashref(); | 144 |             itemnumber     => $itemnumber, | 
            
              |  |  | 145 |             accounttype    => 'L', | 
            
              | 146 |         } | 
            
              | 147 |     )->count(); | 
        
          | 134 |  | 148 |  | 
        
          | 135 |     # OK, they haven't | 149 |     # OK, they haven't | 
          
            
              | 136 |     unless ($existing_charge_hashref) { | 150 |     unless ($existing_charges) { | 
        
          | 137 |         my $manager_id = 0; | 151 |         my $manager_id = 0; | 
        
          | 138 |         $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv; | 152 |         $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv; | 
        
          | 139 |         # This item is on issue ... add replacement cost to the borrower's record and mark it returned | 153 |         # 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 | 154 |         #  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. | 155 |         #  process (or person) to update it, since we don't handle any defaults for replacement prices. | 
        
          | 142 |         my $accountno = getnextacctno($borrowernumber); | 156 |         my $accountno = getnextacctno($borrowernumber); | 
          
            
              | 143 |         my $sth2=$dbh->prepare("INSERT INTO accountlines | 157 |  | 
            
              | 144 |         (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding,itemnumber,manager_id) | 158 |         my $accountline = Koha::Account::Line->new( | 
            
              | 145 |         VALUES (?,?,now(),?,?,'L',?,?,?)"); | 159 |             { | 
            
              | 146 |         $sth2->execute($borrowernumber,$accountno,$amount, | 160 |                 borrowernumber    => $borrowernumber, | 
            
              | 147 |         $description,$amount,$itemnumber,$manager_id); | 161 |                 accountno         => $accountno, | 
            
              |  |  | 162 |                 date              => \'NOW()', | 
            
              | 163 |                 amount            => $amount, | 
            
              | 164 |                 description       => $description, | 
            
              | 165 |                 accounttype       => 'L', | 
            
              | 166 |                 amountoutstanding => $amount, | 
            
              | 167 |                 itemnumber        => $itemnumber, | 
            
              | 168 |                 manager_id        => $manager_id, | 
            
              | 169 |             } | 
            
              | 170 |         )->store(); | 
            
              | 171 |  | 
            
              | 172 |         my $account_offset = Koha::Account::Offset->new( | 
            
              | 173 |             { | 
            
              | 174 |                 debit_id => $accountline->id, | 
            
              | 175 |                 type     => 'Lost Item', | 
            
              | 176 |                 amount   => $amount, | 
            
              | 177 |             } | 
            
              | 178 |         )->store(); | 
        
          | 148 |  | 179 |  | 
        
          | 149 |         if ( C4::Context->preference("FinesLog") ) { | 180 |         if ( C4::Context->preference("FinesLog") ) { | 
        
          | 150 |             logaction("FINES", 'CREATE', $borrowernumber, Dumper({ | 181 |             logaction("FINES", 'CREATE', $borrowernumber, Dumper({ | 
  
    | Lines 208-228
          sub manualinvoice {
      
      
        Link Here | 
        
          | 208 |         $notifyid = 1; | 239 |         $notifyid = 1; | 
        
          | 209 |     } | 240 |     } | 
        
          | 210 |  | 241 |  | 
          
            
              | 211 |     if ( $itemnum ) { | 242 |     my $accountline = Koha::Account::Line->new( | 
            
              | 212 |         $desc .= ' ' . $itemnum; | 243 |         { | 
            
              | 213 |         my $sth = $dbh->prepare( | 244 |             borrowernumber    => $borrowernumber, | 
            
              | 214 |             'INSERT INTO  accountlines | 245 |             accountno         => $accountno, | 
            
              | 215 |                         (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding, itemnumber,notify_id, note, manager_id) | 246 |             date              => \'NOW()', | 
            
              | 216 |         VALUES (?, ?, now(), ?,?, ?,?,?,?,?,?)'); | 247 |             amount            => $amount, | 
            
              | 217 |      $sth->execute($borrowernumber, $accountno, $amount, $desc, $type, $amountleft, $itemnum,$notifyid, $note, $manager_id) || return $sth->errstr; | 248 |             description       => $desc, | 
            
              | 218 |   } else { | 249 |             accounttype       => $type, | 
            
              | 219 |     my $sth=$dbh->prepare("INSERT INTO  accountlines | 250 |             amountoutstanding => $amountleft, | 
            
              | 220 |             (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding,notify_id, note, manager_id) | 251 |             itemnumber        => $itemnum || undef, | 
            
              | 221 |             VALUES (?, ?, now(), ?, ?, ?, ?,?,?,?)" | 252 |             notify_id         => $notifyid, | 
            
              | 222 |         ); | 253 |             note              => $note, | 
            
              | 223 |         $sth->execute( $borrowernumber, $accountno, $amount, $desc, $type, | 254 |             manager_id        => $manager_id, | 
            
              | 224 |             $amountleft, $notifyid, $note, $manager_id ); | 255 |         } | 
            
              | 225 |     } | 256 |     )->store(); | 
            
              |  |  | 257 |  | 
            
              | 258 |     my $account_offset = Koha::Account::Offset->new( | 
            
              | 259 |         { | 
            
              | 260 |             debit_id => $accountline->id, | 
            
              | 261 |             type     => 'Manual Debit', | 
            
              | 262 |             amount   => $amount, | 
            
              | 263 |         } | 
            
              | 264 |     )->store(); | 
        
          | 226 |  | 265 |  | 
        
          | 227 |     if ( C4::Context->preference("FinesLog") ) { | 266 |     if ( C4::Context->preference("FinesLog") ) { | 
        
          | 228 |         logaction("FINES", 'CREATE',$borrowernumber,Dumper({ | 267 |         logaction("FINES", 'CREATE',$borrowernumber,Dumper({ | 
  
    | Lines 308-352
          sub getrefunds {
      
      
        Link Here | 
        
          | 308 |     return (@results); | 347 |     return (@results); | 
        
          | 309 | } | 348 | } | 
        
          | 310 |  | 349 |  | 
            
              |  |  | 350 | #FIXME: ReversePayment should be replaced with a Void Payment feature | 
        
          | 311 | sub ReversePayment { | 351 | sub ReversePayment { | 
          
            
              | 312 |     my ( $accountlines_id ) = @_; | 352 |     my ($accountlines_id) = @_; | 
        
          | 313 |     my $dbh = C4::Context->dbh; | 353 |     my $dbh = C4::Context->dbh; | 
        
          | 314 |  | 354 |  | 
          
            
              | 315 |     my $sth = $dbh->prepare('SELECT * FROM accountlines WHERE accountlines_id = ?'); | 355 |     my $accountline        = Koha::Account::Lines->find($accountlines_id); | 
            
              | 316 |     $sth->execute( $accountlines_id ); | 356 |     my $amount_outstanding = $accountline->amountoutstanding; | 
            
              | 317 |     my $row = $sth->fetchrow_hashref(); | 357 |  | 
            
              | 318 |     my $amount_outstanding = $row->{'amountoutstanding'}; | 358 |     my $new_amountoutstanding = | 
            
              | 319 |  | 359 |       $amount_outstanding <= 0 ? $accountline->amount * -1 : 0; | 
            
              | 320 |     if ( $amount_outstanding <= 0 ) { | 360 |  | 
            
              | 321 |         $sth = $dbh->prepare('UPDATE accountlines SET amountoutstanding = amount * -1, description = CONCAT( description, " Reversed -" ) WHERE accountlines_id = ?'); | 361 |     $accountline->description( $accountline->description . " Reversed -" ); | 
            
              | 322 |         $sth->execute( $accountlines_id ); | 362 |     $accountline->amountoutstanding($new_amountoutstanding); | 
            
              | 323 |     } else { | 363 |     $accountline->store(); | 
            
              | 324 |         $sth = $dbh->prepare('UPDATE accountlines SET amountoutstanding = 0, description = CONCAT( description, " Reversed -" ) WHERE accountlines_id = ?'); | 364 |  | 
            
              | 325 |         $sth->execute( $accountlines_id ); | 365 |     my $account_offset = Koha::Account::Offset->new( | 
            
              | 326 |     } | 366 |         { | 
            
              |  |  | 367 |             credit_id => $accountline->id, | 
            
              | 368 |             type      => 'Reverse Payment', | 
            
              | 369 |             amount    => $amount_outstanding - $new_amountoutstanding, | 
            
              | 370 |         } | 
            
              | 371 |     )->store(); | 
        
          | 327 |  | 372 |  | 
        
          | 328 |     if ( C4::Context->preference("FinesLog") ) { | 373 |     if ( C4::Context->preference("FinesLog") ) { | 
        
          | 329 |         my $manager_id = 0; | 374 |         my $manager_id = 0; | 
        
          | 330 |         $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv; | 375 |         $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv; | 
        
          | 331 |  | 376 |  | 
          
            
              | 332 |         if ( $amount_outstanding <= 0 ) { | 377 |         logaction( | 
            
              | 333 |             $row->{'amountoutstanding'} *= -1; | 378 |             "FINES", 'MODIFY', | 
            
              | 334 |         } else { | 379 |             $accountline->borrowernumber, | 
            
              | 335 |             $row->{'amountoutstanding'} = '0'; | 380 |             Dumper( | 
            
              | 336 |         } | 381 |                 { | 
            
              | 337 |         $row->{'description'} .= ' Reversed -'; | 382 |                     action                => 'reverse_fee_payment', | 
            
              | 338 |         logaction("FINES", 'MODIFY', $row->{'borrowernumber'}, Dumper({ | 383 |                     borrowernumber        => $accountline->borrowernumber, | 
            
              | 339 |             action                => 'reverse_fee_payment', | 384 |                     old_amountoutstanding => $amount_outstanding, | 
            
              | 340 |             borrowernumber        => $row->{'borrowernumber'}, | 385 |                     new_amountoutstanding => $new_amountoutstanding, | 
            
              | 341 |             old_amountoutstanding => $row->{'amountoutstanding'}, | 386 |                     , | 
            
              | 342 |             new_amountoutstanding => 0 - $amount_outstanding,, | 387 |                     accountlines_id => $accountline->id, | 
            
              | 343 |             accountlines_id       => $row->{'accountlines_id'}, | 388 |                     accountno       => $accountline->accountno, | 
            
              | 344 |             accountno             => $row->{'accountno'}, | 389 |                     manager_id      => $manager_id, | 
            
              | 345 |             manager_id            => $manager_id, | 390 |                 } | 
            
              | 346 |         })); | 391 |             ) | 
            
              | 347 |  | 392 |         ); | 
        
          | 348 |     } | 393 |     } | 
            
              | 349 |  |  |  | 
        
          | 350 | } | 394 | } | 
        
          | 351 |  | 395 |  | 
        
          | 352 | =head2 WriteOffFee | 396 | =head2 WriteOffFee | 
  
    | Lines 366-425
          C<$payment_note> is the note to attach to this payment
      
      
        Link Here | 
        
          | 366 |  | 410 |  | 
        
          | 367 | sub WriteOffFee { | 411 | sub WriteOffFee { | 
        
          | 368 |     my ( $borrowernumber, $accountlines_id, $itemnum, $accounttype, $amount, $branch, $payment_note ) = @_; | 412 |     my ( $borrowernumber, $accountlines_id, $itemnum, $accounttype, $amount, $branch, $payment_note ) = @_; | 
            
              |  |  | 413 |  | 
        
          | 369 |     $payment_note //= ""; | 414 |     $payment_note //= ""; | 
            
              | 370 |     $branch ||= C4::Context->userenv->{branch}; |  |  | 
        
          | 371 |     my $manager_id = 0; | 415 |     my $manager_id = 0; | 
          
            
              | 372 |     $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv; | 416 |  | 
            
              |  |  | 417 |     if ( C4::Context->userenv ) { | 
            
              | 418 |         $manager_id = C4::Context->userenv->{number}; | 
            
              | 419 |         $branch ||= C4::Context->userenv->{branch}; | 
            
              | 420 |     } | 
        
          | 373 |  | 421 |  | 
        
          | 374 |     # if no item is attached to fine, make sure to store it as a NULL | 422 |     # if no item is attached to fine, make sure to store it as a NULL | 
        
          | 375 |     $itemnum ||= undef; | 423 |     $itemnum ||= undef; | 
        
          | 376 |  | 424 |  | 
          
            
              | 377 |     my ( $sth, $query ); | 425 |     my $accountline = Koha::Account::Lines->find($accountlines_id); | 
            
              | 378 |     my $dbh = C4::Context->dbh(); | 426 |     return unless $accountline; | 
            
              | 379 |  | 427 |     $accountline->amountoutstanding(0); | 
            
              | 380 |     $query = " | 428 |     $accountline->store(); | 
            
              | 381 |         UPDATE accountlines SET amountoutstanding = 0 |  |  | 
            
              | 382 |         WHERE accountlines_id = ? AND borrowernumber = ? | 
            
              | 383 |     "; | 
            
              | 384 |     $sth = $dbh->prepare( $query ); | 
            
              | 385 |     $sth->execute( $accountlines_id, $borrowernumber ); | 
        
          | 386 |  | 429 |  | 
        
          | 387 |     if ( C4::Context->preference("FinesLog") ) { | 430 |     if ( C4::Context->preference("FinesLog") ) { | 
          
            
              | 388 |         logaction("FINES", 'MODIFY', $borrowernumber, Dumper({ | 431 |         logaction( | 
            
              | 389 |             action                => 'fee_writeoff', | 432 |             "FINES", 'MODIFY', | 
            
              | 390 |             borrowernumber        => $borrowernumber, | 433 |             $borrowernumber, | 
            
              | 391 |             accountlines_id       => $accountlines_id, | 434 |             Dumper( | 
            
              | 392 |             manager_id            => $manager_id, | 435 |                 { | 
            
              | 393 |         })); | 436 |                     action          => 'fee_writeoff', | 
            
              |  |  | 437 |                     borrowernumber  => $borrowernumber, | 
            
              | 438 |                     accountlines_id => $accountlines_id, | 
            
              | 439 |                     manager_id      => $manager_id, | 
            
              | 440 |                 } | 
            
              | 441 |             ) | 
            
              | 442 |         ); | 
        
          | 394 |     } | 443 |     } | 
        
          | 395 |  | 444 |  | 
            
              | 396 |     $query =" |  |  | 
            
              | 397 |         INSERT INTO accountlines | 
            
              | 398 |         ( borrowernumber, accountno, itemnumber, date, amount, description, accounttype, manager_id, note ) | 
            
              | 399 |         VALUES ( ?, ?, ?, NOW(), ?, 'Writeoff', 'W', ?, ? ) | 
            
              | 400 |     "; | 
            
              | 401 |     $sth = $dbh->prepare( $query ); | 
        
          | 402 |     my $acct = getnextacctno($borrowernumber); | 445 |     my $acct = getnextacctno($borrowernumber); | 
          
            
              | 403 |     $sth->execute( $borrowernumber, $acct, $itemnum, $amount, $manager_id, $payment_note ); | 446 |  | 
            
              |  |  | 447 |     my $writeoff = Koha::Account::Line->new( | 
            
              | 448 |         { | 
            
              | 449 |             borrowernumber => $borrowernumber, | 
            
              | 450 |             accountno      => $acct, | 
            
              | 451 |             itemnumber     => $itemnum || undef, | 
            
              | 452 |             date           => \'NOW()', | 
            
              | 453 |             amount         => $amount * -1, | 
            
              | 454 |             description    => 'Writeoff', | 
            
              | 455 |             accounttype    => 'W', | 
            
              | 456 |             manager_id     => $manager_id, | 
            
              | 457 |             note           => $payment_note, | 
            
              | 458 |         } | 
            
              | 459 |     )->store(); | 
            
              | 460 |  | 
            
              | 461 |     Koha::Account::Offset->new( | 
            
              | 462 |         { | 
            
              | 463 |             debit_id  => $accountline->id, | 
            
              | 464 |             credit_id => $writeoff->id, | 
            
              | 465 |             type      => 'Writeoff', | 
            
              | 466 |             amount    => $amount * -1, | 
            
              | 467 |         } | 
            
              | 468 |     )->store(); | 
        
          | 404 |  | 469 |  | 
        
          | 405 |     if ( C4::Context->preference("FinesLog") ) { | 470 |     if ( C4::Context->preference("FinesLog") ) { | 
          
            
              | 406 |         logaction("FINES", 'CREATE',$borrowernumber,Dumper({ | 471 |         logaction( | 
            
              | 407 |             action            => 'create_writeoff', | 472 |             "FINES", 'CREATE', | 
            
              | 408 |             borrowernumber    => $borrowernumber, | 473 |             $borrowernumber, | 
            
              | 409 |             accountno         => $acct, | 474 |             Dumper( | 
            
              | 410 |             amount            => 0 - $amount, | 475 |                 { | 
            
              | 411 |             accounttype       => 'W', | 476 |                     action            => 'create_writeoff', | 
            
              | 412 |             itemnumber        => $itemnum, | 477 |                     borrowernumber    => $borrowernumber, | 
            
              | 413 |             accountlines_paid => [ $accountlines_id ], | 478 |                     accountno         => $acct, | 
            
              | 414 |             manager_id        => $manager_id, | 479 |                     amount            => 0 - $amount, | 
            
              | 415 |         })); | 480 |                     accounttype       => 'W', | 
            
              |  |  | 481 |                     itemnumber        => $itemnum, | 
            
              | 482 |                     accountlines_paid => [$accountlines_id], | 
            
              | 483 |                     manager_id        => $manager_id, | 
            
              | 484 |                 } | 
            
              | 485 |             ) | 
            
              | 486 |         ); | 
        
          | 416 |     } | 487 |     } | 
        
          | 417 |  | 488 |  | 
          
            
              | 418 |     UpdateStats({ | 489 |     UpdateStats( | 
            
              | 419 |                 branch => $branch, | 490 |         { | 
            
              | 420 |                 type => 'writeoff', | 491 |             branch         => $branch, | 
            
              | 421 |                 amount => $amount, | 492 |             type           => 'writeoff', | 
            
              | 422 |                 borrowernumber => $borrowernumber} | 493 |             amount         => $amount, | 
            
              |  |  | 494 |             borrowernumber => $borrowernumber | 
            
              | 495 |         } | 
        
          | 423 |     ); | 496 |     ); | 
        
          | 424 |  | 497 |  | 
        
          | 425 | } | 498 | } |