Bug 14850: Funds from inactive budgets appear in 'Funds' dropdown on acqui/invoice.pl
[koha.git] / acqui / invoice.pl
index 0f2933a..994dca8 100755 (executable)
@@ -38,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(
@@ -108,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} );
@@ -116,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;
@@ -145,40 +187,56 @@ foreach my $order (@$orders) {
 
 push @foot_loop, map {$_} values %foot;
 
-my $budgets = GetBudgets();
-my @budgets_loop;
 my $shipmentcost_budgetid = $details->{shipmentcost_budgetid};
-foreach my $budget (@$budgets) {
-    next unless CanUserUseBudget( $loggedinuser, $budget, $flags );
-    my %line = %{$budget};
-    if (    $shipmentcost_budgetid
-        and $budget->{budget_id} == $shipmentcost_budgetid )
-    {
-        $line{selected} = 1;
+
+# build budget list
+my $budget_loop = [];
+my $budgets     = GetBudgetHierarchy();
+foreach my $r ( @{$budgets} ) {
+    next unless ( CanUserUseBudget( $loggedinuser, $r, $flags ) );
+
+    if ( !defined $r->{budget_amount} || $r->{budget_amount} == 0 ) {
+        next;
     }
-    push @budgets_loop, \%line;
+
+    my $selected = $shipmentcost_budgetid ? $r->{budget_id} eq $shipmentcost_budgetid : 0;
+
+    push @{$budget_loop},
+      {
+        b_id     => $r->{budget_id},
+        b_txt    => $r->{budget_name},
+        b_active => $r->{budget_period_active},
+        selected => $selected,
+      };
 }
 
+@{$budget_loop} =
+  sort { uc( $a->{b_txt} ) cmp uc( $b->{b_txt} ) } @{$budget_loop};
+warn Data::Dumper::Dumper( $budget_loop );
+
+my $adjustments = Koha::Acquisition::Invoice::Adjustments->search({ invoiceid => $details->{'invoiceid'} });
+if ( $adjustments ) { $template->param( adjustments => $adjustments ); }
+
 $template->param(
-    invoiceid        => $details->{'invoiceid'},
-    invoicenumber    => $details->{'invoicenumber'},
-    suppliername     => $details->{'suppliername'},
-    booksellerid     => $details->{'booksellerid'},
-    shipmentdate     => $details->{'shipmentdate'},
-    billingdate      => $details->{'billingdate'},
-    invoiceclosedate => $details->{'closedate'},
-    shipmentcost     => $details->{'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},
-    invoiceincgst    => $bookseller->invoiceincgst,
-    currency         => Koha::Acquisition::Currencies->get_active,
-    budgets_loop     => \@budgets_loop,
+    invoiceid                   => $details->{'invoiceid'},
+    invoicenumber               => $details->{'invoicenumber'},
+    suppliername                => $details->{'suppliername'},
+    booksellerid                => $details->{'booksellerid'},
+    shipmentdate                => $details->{'shipmentdate'},
+    billingdate                 => $details->{'billingdate'},
+    invoiceclosedate            => $details->{'closedate'},
+    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 + $shipmentcost,
+    total_tax_included_shipment => $total_tax_included + $shipmentcost,
+    invoiceincgst               => $bookseller->invoiceincgst,
+    currency                    => Koha::Acquisition::Currencies->get_active,
+    budgets                     => $budget_loop,
 );
 
 defined( $invoice_files ) && $template->param( files => $invoice_files->GetFilesInfo() );