Bug 15184: Change permission (+x) on .pl
[koha.git] / acqui / invoice.pl
index dd1360f..bdf9485 100755 (executable)
@@ -26,8 +26,7 @@ Invoice details
 
 =cut
 
-use strict;
-use warnings;
+use Modern::Perl;
 
 use CGI qw ( -utf8 );
 use C4::Auth;
@@ -39,6 +38,7 @@ use Koha::Acquisition::Booksellers;
 use Koha::Acquisition::Currencies;
 use Koha::DateUtils;
 use Koha::Misc::Files;
+use Koha::Acquisition::Invoice::Adjustments;
 
 my $input = new CGI;
 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
@@ -109,7 +109,47 @@ elsif ( $op && $op eq 'delete' ) {
         exit 0;
     }
 }
+elsif ( $op && $op eq 'del_adj' ) {
+    my $adjustment_id  = $input->param('adjustment_id');
+    my $del_adj = Koha::Acquisition::Invoice::Adjustments->find( $adjustment_id );
+    $del_adj->delete() if ($del_adj);
+}
+elsif ( $op && $op eq 'mod_adj' ) {
+    my @adjustment_id  = $input->multi_param('adjustment_id');
+    my @adjustment     = $input->multi_param('adjustment');
+    my @reason         = $input->multi_param('reason');
+    my @note           = $input->multi_param('note');
+    my @budget_id      = $input->multi_param('budget_id');
+    my @encumber_open  = $input->multi_param('encumber_open');
+    my %e_open = map { $_ => 1 } @encumber_open;
 
+    for( my $i=0; $i < scalar @adjustment; $i++ ){
+        if( $adjustment_id[$i] eq 'new' ){
+            next unless ( $adjustment[$i] || $reason[$i] );
+            my $new_adj = Koha::Acquisition::Invoice::Adjustment->new({
+                invoiceid => $invoiceid,
+                adjustment => $adjustment[$i],
+                reason => $reason[$i],
+                note => $note[$i],
+                budget_id => $budget_id[$i] || undef,
+                encumber_open => defined $e_open{ $adjustment_id[$i] } ? 1 : 0,
+            });
+            $new_adj->store();
+        }
+        else {
+            my $old_adj = Koha::Acquisition::Invoice::Adjustments->find( $adjustment_id[$i] );
+            unless ( $old_adj->adjustment == $adjustment[$i] && $old_adj->reason eq $reason[$i] && $old_adj->budget_id == $budget_id[$i] && $old_adj->encumber_open == $e_open{$adjustment_id[$i]} && $old_adj->note eq $note[$i] ){
+                $old_adj->timestamp(undef);
+                $old_adj->adjustment( $adjustment[$i] );
+                $old_adj->reason( $reason[$i] );
+                $old_adj->note( $note[$i] );
+                $old_adj->budget_id( $budget_id[$i] || undef );
+                $old_adj->encumber_open( $e_open{$adjustment_id[$i]} ? 1 : 0 );
+                $old_adj->update();
+            }
+        }
+    }
+}
 
 my $details = GetInvoiceDetails($invoiceid);
 my $bookseller = Koha::Acquisition::Booksellers->find( $details->{booksellerid} );
@@ -117,6 +157,7 @@ my @orders_loop = ();
 my $orders = $details->{'orders'};
 my @foot_loop;
 my %foot;
+my $shipmentcost = $details->{shipmentcost} || 0;
 my $total_quantity = 0;
 my $total_tax_excluded = 0;
 my $total_tax_included = 0;
@@ -160,6 +201,9 @@ foreach my $budget (@$budgets) {
     push @budgets_loop, \%line;
 }
 
+my $adjustments = Koha::Acquisition::Invoice::Adjustments->search({ invoiceid => $details->{'invoiceid'} });
+if ( $adjustments ) { $template->param( adjustments => $adjustments ); }
+
 $template->param(
     invoiceid        => $details->{'invoiceid'},
     invoicenumber    => $details->{'invoicenumber'},
@@ -168,15 +212,15 @@ $template->param(
     shipmentdate     => $details->{'shipmentdate'},
     billingdate      => $details->{'billingdate'},
     invoiceclosedate => $details->{'closedate'},
-    shipmentcost     => $details->{'shipmentcost'},
+    shipmentcost     => $shipmentcost,
     orders_loop      => \@orders_loop,
     foot_loop        => \@foot_loop,
     total_quantity   => $total_quantity,
     total_tax_excluded => $total_tax_excluded,
     total_tax_included => $total_tax_included,
     total_tax_value  => $total_tax_value,
-    total_tax_excluded_shipment => $total_tax_excluded + $details->{shipmentcost},
-    total_tax_included_shipment => $total_tax_included + $details->{shipmentcost},
+    total_tax_excluded_shipment => $total_tax_excluded + $shipmentcost,
+    total_tax_included_shipment => $total_tax_included + $shipmentcost,
     invoiceincgst    => $bookseller->invoiceincgst,
     currency         => Koha::Acquisition::Currencies->get_active,
     budgets_loop     => \@budgets_loop,
@@ -201,7 +245,6 @@ sub get_infos {
     $line{budget_name}    = $budget->{budget_name};
 
     if ( $line{uncertainprice} ) {
-        $template->param( uncertainprices => 1 );
         $line{rrp} .= ' (Uncertain)';
     }
     if ( $line{'title'} ) {