Bug 17843: Replace C4::Koha::getitemtypeinfo with Koha::ItemTypes
[koha.git] / members / pay.pl
index 1ac140d..3400ee0 100755 (executable)
@@ -39,8 +39,11 @@ use C4::Accounts;
 use C4::Stats;
 use C4::Koha;
 use C4::Overdues;
-use C4::Branch;
 use C4::Members::Attributes qw(GetBorrowerAttributes);
+use Koha::Patron::Images;
+
+use Koha::Patron::Categories;
+use URI::Escape;
 
 our $input = CGI->new;
 
@@ -67,8 +70,7 @@ our $borrower = GetMember( borrowernumber => $borrowernumber );
 our $user = $input->remote_user;
 $user ||= q{};
 
-my $branches = GetBranches();
-our $branch = GetBranch( $input, $branches );
+our $branch = C4::Context->userenv->{'branch'};
 
 my $writeoff_item = $input->param('confirm_writeoff');
 my $paycollect    = $input->param('paycollect');
@@ -86,11 +88,18 @@ if ($writeoff_all) {
     writeoff_all(@names);
 } elsif ($writeoff_item) {
     my $accountlines_id = $input->param('accountlines_id');
-    my $itemno       = $input->param('itemnumber');
-    my $account_type = $input->param('accounttype');
     my $amount       = $input->param('amountoutstanding');
     my $payment_note = $input->param("payment_note");
-    WriteOffFee( $borrowernumber, $accountlines_id, $itemno, $account_type, $amount, $branch, $payment_note );
+
+    Koha::Account->new( { patron_id => $borrowernumber } )->pay(
+        {
+            amount     => $amount,
+            lines      => [ scalar Koha::Account::Lines->find($accountlines_id) ],
+            type       => 'writeoff',
+            note       => $payment_note,
+            library_id => $branch,
+        }
+    );
 }
 
 for (@names) {
@@ -105,7 +114,6 @@ for (@names) {
 
 $template->param(
     finesview => 1,
-    activeBorrowerRelationship => (C4::Context->preference('borrowerRelationship') ne ''),
     RoutingSerials => C4::Context->preference('RoutingSerials'),
 );
 
@@ -134,12 +142,14 @@ sub add_accounts_to_template {
     }
     borrower_add_additional_fields($borrower);
 
-    #workaround for Bug 4041 to get rid of duplicated code in circ-menu.tt, use circ-menu.inc instad
     $template->param(%$borrower);
 
+    my $patron_image = Koha::Patron::Images->find($borrower->{borrowernumber});
+    $template->param( picture => 1 ) if $patron_image;
     $template->param(
         accounts => $accounts,
         borrower => $borrower,
+        categoryname => $borrower->{'description'},
         total    => $total,
     );
     return;
@@ -149,7 +159,7 @@ sub add_accounts_to_template {
 sub get_for_redirect {
     my ( $name, $name_in, $money ) = @_;
     my $s     = q{&} . $name . q{=};
-    my $value = $input->param($name_in);
+    my $value = uri_escape_utf8( $input->param($name_in) );
     if ( !defined $value ) {
         $value = ( $money == 1 ) ? 0 : q{};
     }
@@ -171,8 +181,8 @@ sub redirect_to_paycollect {
     $redirect .= get_for_redirect( 'amount', "amount$line_no", 1 );
     $redirect .=
       get_for_redirect( 'amountoutstanding', "amountoutstanding$line_no", 1 );
-    $redirect .= get_for_redirect( 'accountno',    "accountno$line_no",    0 );
-    $redirect .= get_for_redirect( 'title',        "title$line_no",        0 );
+    $redirect .= get_for_redirect( 'description', "description$line_no", 0 );
+    $redirect .= get_for_redirect( 'title', "title$line_no", 0 );
     $redirect .= get_for_redirect( 'itemnumber',   "itemnumber$line_no",   0 );
     $redirect .= get_for_redirect( 'notify_id',    "notify_id$line_no",    0 );
     $redirect .= get_for_redirect( 'notify_level', "notify_level$line_no", 0 );
@@ -185,25 +195,29 @@ sub redirect_to_paycollect {
 
 sub writeoff_all {
     my @params = @_;
-    my @wo_lines = grep { /^accountno\d+$/ } @params;
+    my @wo_lines = grep { /^accountlines_id\d+$/ } @params;
+
+    my $borrowernumber = $input->param('borrowernumber');
+
     for (@wo_lines) {
         if (/(\d+)/) {
-            my $value       = $1;
-            my $accounttype = $input->param("accounttype$value");
-
-            #    my $borrowernum    = $input->param("borrowernumber$value");
-            my $itemno    = $input->param("itemnumber$value");
-            my $amount    = $input->param("amountoutstanding$value");
-            my $accountno = $input->param("accountno$value");
+            my $value           = $1;
+            my $amount          = $input->param("amountoutstanding$value");
             my $accountlines_id = $input->param("accountlines_id$value");
-            my $payment_note = $input->param("payment_note_$value");
-            WriteOffFee( $borrowernumber, $accountlines_id, $itemno, $accounttype, $amount, $branch, $payment_note );
+            my $payment_note    = $input->param("payment_note_$value");
+            Koha::Account->new( { patron_id => $borrowernumber } )->pay(
+                {
+                    amount => $amount,
+                    lines  => [ scalar Koha::Account::Lines->find($accountlines_id) ],
+                    type   => 'writeoff',
+                    note   => $payment_note,
+                    library_id => $branch,
+                }
+            );
         }
     }
 
-    $borrowernumber = $input->param('borrowernumber');
-    print $input->redirect(
-        "/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
+    print $input->redirect("/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
     return;
 }
 
@@ -214,24 +228,12 @@ sub borrower_add_additional_fields {
 # in a number of templates. It should not be the business of this script but in lieu of
 # a revised api here it is ...
     if ( $b_ref->{category_type} eq 'C' ) {
-        my ( $catcodes, $labels ) =
-          GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
-        if ( @{$catcodes} ) {
-            if ( @{$catcodes} > 1 ) {
-                $b_ref->{CATCODE_MULTI} = 1;
-            } elsif ( @{$catcodes} == 1 ) {
-                $b_ref->{catcode} = $catcodes->[0];
-            }
-        }
-    } elsif ( $b_ref->{category_type} eq 'A' ) {
+        my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
+        $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
+        $template->param( 'catcode' => $patron_categories->next )  if $patron_categories->count == 1;
+    } elsif ( $b_ref->{category_type} eq 'A' || $b_ref->{category_type} eq 'I' ) {
         $b_ref->{adultborrower} = 1;
     }
-    my ( $picture, $dberror ) = GetPatronImage( $b_ref->{borrowernumber} );
-    if ($picture) {
-        $b_ref->{has_picture} = 1;
-    }
-    my $roadtype = C4::Koha::GetAuthorisedValueByCode( 'ROADTYPE', $borrower->{streettype} );
-    $b_ref->{roadtype} = $roadtype;
 
     if (C4::Context->preference('ExtendedPatronAttributes')) {
         $b_ref->{extendedattributes} = GetBorrowerAttributes($borrowernumber);
@@ -240,7 +242,6 @@ sub borrower_add_additional_fields {
         );
     }
 
-    $b_ref->{branchname} = GetBranchName( $b_ref->{branchcode} );
     return;
 }
 
@@ -251,7 +252,7 @@ sub payselected {
     foreach (@params) {
         if (/^incl_par_(\d+)$/) {
             my $index = $1;
-            push @lines_to_pay, $input->param("accountno$index");
+            push @lines_to_pay, $input->param("accountlines_id$index");
             $amt += $input->param("amountoutstanding$index");
         }
     }