Bug 32070: Consistent classes for primary buttons: Acquisitions
[koha-ffzg.git] / acqui / invoice.pl
index 2e05c0d..0e21a20 100755 (executable)
@@ -33,13 +33,16 @@ use C4::Auth qw( get_template_and_user );
 use C4::Output qw( output_and_exit output_html_with_http_headers );
 use C4::Acquisition qw( CloseInvoice ReopenInvoice ModInvoice MergeInvoices DelInvoice GetInvoice GetInvoiceDetails get_rounded_price );
 use C4::Budgets qw( GetBudgetHierarchy GetBudget CanUserUseBudget );
+use JSON qw( encode_json );
 use C4::Log qw(logaction);
 
 use Koha::Acquisition::Booksellers;
 use Koha::Acquisition::Currencies qw( get_active );
+use Koha::AdditionalFields;
 use Koha::DateUtils qw( output_pref );
 use Koha::Misc::Files;
 use Koha::Acquisition::Invoice::Adjustments;
+use Koha::Acquisition::Invoices;
 
 my $input = CGI->new;
 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
@@ -101,8 +104,8 @@ elsif ( $op && $op eq 'mod' ) {
     ModInvoice(
         invoiceid             => $invoiceid,
         invoicenumber         => $invoicenumber,
-        shipmentdate          => scalar output_pref( { str => scalar $input->param('shipmentdate'), dateformat => 'iso', dateonly => 1 } ),
-        billingdate           => scalar output_pref( { str => scalar $input->param('billingdate'),  dateformat => 'iso', dateonly => 1 } ),
+        shipmentdate          => scalar $input->param('shipmentdate'),
+        billingdate           => scalar $input->param('billingdate'),
         shipmentcost          => $shipmentcost,
         shipmentcost_budgetid => $shipment_budget_id
     );
@@ -124,6 +127,18 @@ elsif ( $op && $op eq 'mod' ) {
         MergeInvoices($invoiceid, \@sources);
         defined($invoice_files) && $invoice_files->MergeFileRecIds(@sources);
     }
+
+    my @additional_fields;
+    my $invoice_fields = Koha::AdditionalFields->search({ tablename => 'aqinvoices' });
+    while ( my $field = $invoice_fields->next ) {
+        my $value = $input->param('additional_field_' . $field->id);
+        push @additional_fields, {
+            id => $field->id,
+            value => $value,
+        };
+    }
+    Koha::Acquisition::Invoices->find($invoiceid)->set_additional_fields(\@additional_fields);
+
     $template->param( modified => 1 );
 }
 elsif ( $op && $op eq 'delete' ) {
@@ -147,20 +162,19 @@ elsif ( $op && $op eq 'del_adj' ) {
     my $adjustment_id  = $input->param('adjustment_id');
     my $del_adj = Koha::Acquisition::Invoice::Adjustments->find( $adjustment_id );
     if ($del_adj) {
-        if (C4::Context->preference("AcqLog")) {
-            my $reason_length = length $del_adj->reason;
-            my $reason_padded = ( ' ' x (80 - $reason_length) ) . $del_adj->reason;
-            my $infos =
-                sprintf("%010d", $del_adj->invoiceid) .
-                sprintf("%010d", $del_adj->budget_id) .
-                sprintf("%010d", $del_adj->encumber_open) .
-                sprintf("%010.2f", $del_adj->adjustment) .
-                $reason_padded;
+        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,
-                $infos
+                encode_json($infos)
             );
         }
         $del_adj->delete();
@@ -190,17 +204,16 @@ elsif ( $op && $op eq 'mod_adj' ) {
                 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("AcqLog")) {
-                my $infos = get_log_string($adj);
+            if (C4::Context->preference("AcquisitionLog")) {
                 logaction(
                     'ACQUISITIONS',
                     'CREATE_INVOICE_ADJUSTMENT',
                     $new_adj->adjustment_id,
-                    $infos
+                    encode_json($adj)
                 );
             }
         }
@@ -208,8 +221,8 @@ elsif ( $op && $op eq 'mod_adj' ) {
             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("AcqLog")) {
-                    my $infos = get_log_string({
+                if (C4::Context->preference("AcquisitionLog")) {
+                    my $infos = {
                         adjustment        => $adjustment[$i],
                         reason            => $reason[$i],
                         budget_id         => $budget_id[$i],
@@ -218,12 +231,12 @@ elsif ( $op && $op eq 'mod_adj' ) {
                         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],
-                        $infos
+                        encode_json($infos)
                     );
                 }
                 $old_adj->timestamp(undef);
@@ -290,6 +303,8 @@ foreach my $r ( @{$budgets} ) {
         b_txt    => $r->{budget_name},
         b_active => $r->{budget_period_active},
         selected => $selected,
+        b_sort1_authcat => $r->{'sort1_authcat'},
+        b_sort2_authcat => $r->{'sort2_authcat'},
       };
 }
 
@@ -299,12 +314,21 @@ foreach my $r ( @{$budgets} ) {
 my $adjustments = Koha::Acquisition::Invoice::Adjustments->search({ invoiceid => $details->{'invoiceid'} });
 if ( $adjustments ) { $template->param( adjustments => $adjustments ); }
 
+my $invoice = Koha::Acquisition::Invoices->find($invoiceid);
+$template->param(
+    available_additional_fields => Koha::AdditionalFields->search( { tablename => 'aqinvoices' } ),
+    additional_field_values => { map {
+                $_->field->id => $_->value
+            } $invoice->additional_field_values->as_list },
+);
+
 $template->param(
     invoiceid                   => $details->{'invoiceid'},
     invoicenumber               => $details->{'invoicenumber'},
     suppliername                => $details->{'suppliername'},
     booksellerid                => $details->{'booksellerid'},
     shipmentdate                => $details->{'shipmentdate'},
+    shipment_budget_id          => $shipmentcost_budgetid,
     billingdate                 => $details->{'billingdate'},
     invoiceclosedate            => $details->{'closedate'},
     shipmentcost                => $shipmentcost,
@@ -350,35 +374,4 @@ sub get_infos {
     return \%line;
 }
 
-sub get_log_string {
-    my ($data) = @_;
-
-    # We specify the keys we're dealing for logging within an array in order
-    # to preserve order, if we just iterate the hash keys, we could get
-    # the values in any order
-    my @keys = (
-        'budget_id',
-        'encumber_open',
-        'budget_id_old',
-        'encumber_open_old'
-    );
-    # Adjustment amount
-    my $return = sprintf("%010.2f", $data->{adjustment});
-    # Left pad "reason" to the maximum length of aqinvoice_adjustments.reason
-    # (80 characters)
-    my $reason_len = length $data->{reason};
-    $return .= ( ' ' x (80 - $reason_len) ) . $data->{reason};
-    # Append the remaining fields
-    foreach my $key(@keys) {
-        $return .= sprintf("%010d", $data->{$key});
-    }
-    # Old adjustment amount
-    $return .= sprintf("%010.2f", $data->{adjustment_old});
-    # Left pad "reason_old" to the maximum length of aqinvoice_adjustments.reason
-    # (80 characters)
-    my $reason_old_len = length $data->{reason_old};
-    $return .= ( ' ' x (80 - $reason_old_len) ) . $data->{reason_old};
-    return $return;
-}
-
 output_html_with_http_headers $input, $cookie, $template->output;