Bug 12001: Move GetMemberAccountBalance to Koha::Account->non_issues_charges
[srvgit] / Koha / Account.pm
index 9ec1c1b..3e3d76a 100644 (file)
@@ -21,12 +21,13 @@ use Modern::Perl;
 
 use Carp;
 use Data::Dumper;
+use List::MoreUtils qw( uniq );
 
 use C4::Log qw( logaction );
 use C4::Stats qw( UpdateStats );
 
-use Koha::Account::Line;
 use Koha::Account::Lines;
+use Koha::Account::Offsets;
 use Koha::DateUtils qw( dt_from_string );
 
 =head1 NAME
@@ -49,11 +50,14 @@ This method allows payments to be made against fees/fines
 
 Koha::Account->new( { patron_id => $borrowernumber } )->pay(
     {
-        amount     => $amount,
-        sip        => $sipmode,
-        note       => $note,
-        library_id => $branchcode,
-        lines      => $lines, # Arrayref of Koha::Account::Line objects to pay
+        amount      => $amount,
+        sip         => $sipmode,
+        note        => $note,
+        description => $description,
+        library_id  => $branchcode,
+        lines        => $lines, # Arrayref of Koha::Account::Line objects to pay
+        account_type => $type,  # accounttype code
+        offset_type => $offset_type,    # offset type code
     }
 );
 
@@ -62,12 +66,15 @@ Koha::Account->new( { patron_id => $borrowernumber } )->pay(
 sub pay {
     my ( $self, $params ) = @_;
 
-    my $amount          = $params->{amount};
-    my $sip             = $params->{sip};
-    my $note            = $params->{note} || q{};
-    my $library_id      = $params->{library_id};
-    my $lines           = $params->{lines};
-    my $type            = $params->{type} || 'payment';
+    my $amount       = $params->{amount};
+    my $sip          = $params->{sip};
+    my $description  = $params->{description};
+    my $note         = $params->{note} || q{};
+    my $library_id   = $params->{library_id};
+    my $lines        = $params->{lines};
+    my $type         = $params->{type} || 'payment';
+    my $account_type = $params->{account_type};
+    my $offset_type  = $params->{offset_type} || $type eq 'writeoff' ? 'Writeoff' : 'Payment';
 
     my $userenv = C4::Context->userenv;
 
@@ -89,6 +96,8 @@ sub pay {
     my $balance_remaining = $amount; # Set it now so we can adjust the amount if necessary
     $balance_remaining ||= 0;
 
+    my @account_offsets;
+
     # We were passed a specific line to pay
     foreach my $fine ( @$lines ) {
         my $amount_to_pay =
@@ -106,6 +115,15 @@ sub pay {
             C4::Circulation::ReturnLostItem( $self->{patron_id}, $fine->itemnumber );
         }
 
+        my $account_offset = Koha::Account::Offset->new(
+            {
+                debit_id => $fine->id,
+                type     => $offset_type,
+                amount   => $amount_to_pay * -1,
+            }
+        );
+        push( @account_offsets, $account_offset );
+
         if ( C4::Context->preference("FinesLog") ) {
             logaction(
                 "FINES", 'MODIFY',
@@ -149,6 +167,15 @@ sub pay {
         $fine->amountoutstanding( $old_amountoutstanding - $amount_to_pay );
         $fine->store();
 
+        my $account_offset = Koha::Account::Offset->new(
+            {
+                debit_id => $fine->id,
+                type     => $offset_type,
+                amount   => $amount_to_pay * -1,
+            }
+        );
+        push( @account_offsets, $account_offset );
+
         if ( C4::Context->preference("FinesLog") ) {
             logaction(
                 "FINES", 'MODIFY',
@@ -174,12 +201,12 @@ sub pay {
         last unless $balance_remaining > 0;
     }
 
-    my $account_type =
+    $account_type ||=
         $type eq 'writeoff' ? 'W'
       : defined($sip)       ? "Pay$sip"
       :                       'Pay';
 
-    my $description = $type eq 'writeoff' ? 'Writeoff' : q{};
+    $description ||= $type eq 'writeoff' ? 'Writeoff' : q{};
 
     my $payment = Koha::Account::Line->new(
         {
@@ -195,6 +222,11 @@ sub pay {
         }
     )->store();
 
+    foreach my $o ( @account_offsets ) {
+        $o->credit_id( $payment->id() );
+        $o->store();
+    }
+
     $library_id ||= $userenv ? $userenv->{'branch'} : undef;
 
     UpdateStats(
@@ -248,8 +280,59 @@ sub balance {
             as => ['total_amountoutstanding'],
         }
     );
-    return $fines->count
-      ? $fines->next->get_column('total_amountoutstanding')
+
+    my $total = $fines->count
+      ? $fines->next->get_column('total_amountoutstanding') + 0
+      : 0;
+}
+
+=head3 non_issues_charges
+
+my $non_issues_charges = $self->non_issues_charges
+
+Calculates amount immediately owing by the patron - non-issue charges.
+
+Charges exempt from non-issue are:
+* Res (holds) if HoldsInNoissuesCharge syspref is set to false
+* Rent (rental) if RentalsInNoissuesCharge syspref is set to false
+* Manual invoices if ManInvInNoissuesCharge syspref is set to false
+
+=cut
+
+sub non_issues_charges {
+    my ($self) = @_;
+
+    # FIXME REMOVE And add a warning in the about page + update DB if length(MANUAL_INV) > 5
+    my $ACCOUNT_TYPE_LENGTH = 5;    # this is plain ridiculous...
+
+    my @not_fines;
+    push @not_fines, 'Res'
+      unless C4::Context->preference('HoldsInNoissuesCharge');
+    push @not_fines, 'Rent'
+      unless C4::Context->preference('RentalsInNoissuesCharge');
+    unless ( C4::Context->preference('ManInvInNoissuesCharge') ) {
+        my $dbh = C4::Context->dbh;
+        push @not_fines,
+          @{
+            $dbh->selectcol_arrayref(q|
+                SELECT authorised_value FROM authorised_values WHERE category = 'MANUAL_INV'
+            |)
+          };
+    }
+    @not_fines = map { substr( $_, 0, $ACCOUNT_TYPE_LENGTH ) } uniq(@not_fines);
+
+    my $non_issues_charges = Koha::Account::Lines->search(
+        {
+            borrowernumber => $self->{patron_id},
+            accounttype    => { -not_in => \@not_fines }
+        },
+        {
+            select => [ { sum => 'amountoutstanding' } ],
+            as     => ['non_issues_charges'],
+        }
+    );
+    return $non_issues_charges->count
+      ? $non_issues_charges->next->get_column('non_issues_charges') + 0
       : 0;
 }