+elsif ( $op && $op eq 'delete' ) {
+
+ output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
+ unless $logged_in_patron->has_permission( { acquisition => 'delete_invoices' } );
+
+ DelInvoice($invoiceid);
+ defined($invoice_files) && $invoice_files->DelAllFiles();
+ my $referer = $input->param('referer') || 'invoices.pl';
+ if ($referer) {
+ print $input->redirect($referer);
+ exit 0;
+ }
+}
+elsif ( $op && $op eq 'del_adj' ) {
+
+ output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
+ unless $logged_in_patron->has_permission( { acquisition => 'edit_invoices' } );
+
+ my $adjustment_id = $input->param('adjustment_id');
+ my $del_adj = Koha::Acquisition::Invoice::Adjustments->find( $adjustment_id );
+ if ($del_adj) {
+ if (C4::Context->preference("AcquisitionLog")) {
+ my $infos = {
+ invoiceid => $del_adj->invoiceid,
+ budget_id => $del_adj->budget_id,
+ encumber_open => $del_adj->encumber_open,
+ adjustment => $del_adj->adjustment,
+ reason => $del_adj->reason
+ };
+ logaction(
+ 'ACQUISITIONS',
+ 'DELETE_INVOICE_ADJUSTMENT',
+ $adjustment_id,
+ encode_json($infos)
+ );
+ }
+ $del_adj->delete();
+ }
+}
+elsif ( $op && $op eq 'mod_adj' ) {
+
+ output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
+ unless $logged_in_patron->has_permission( { acquisition => 'edit_invoices' } );
+
+ 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;
+
+ my @keys = ('adjustment', 'reason', 'budget_id', 'encumber_open');
+ for( my $i=0; $i < scalar @adjustment; $i++ ){
+ if( $adjustment_id[$i] eq 'new' ){
+ next unless ( $adjustment[$i] || $reason[$i] );
+ my $adj = {
+ 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,
+ };
+ my $new_adj = Koha::Acquisition::Invoice::Adjustment->new($adj);
+ $new_adj->store();
+ # Log this addition
+ if (C4::Context->preference("AcquisitionLog")) {
+ logaction(
+ 'ACQUISITIONS',
+ 'CREATE_INVOICE_ADJUSTMENT',
+ $new_adj->adjustment_id,
+ encode_json($adj)
+ );
+ }
+ }
+ 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] ){
+ # Log this modification
+ if (C4::Context->preference("AcquisitionLog")) {
+ my $infos = {
+ adjustment => $adjustment[$i],
+ reason => $reason[$i],
+ budget_id => $budget_id[$i],
+ encumber_open => $e_open{$adjustment_id[$i]},
+ adjustment_old => $old_adj->adjustment,
+ reason_old => $old_adj->reason,
+ budget_id_old => $old_adj->budget_id,
+ encumber_open_old => $old_adj->encumber_open
+ };
+ logaction(
+ 'ACQUISITIONS',
+ 'UPDATE_INVOICE_ADJUSTMENT',
+ $adjustment_id[$i],
+ encode_json($infos)
+ );
+ }
+ $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();
+ }
+ }
+ }
+}