Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 0dfb1453

Von Bernd Bleßmann vor etwa 11 Jahren hinzugefügt

  • ID 0dfb1453080f4bcd515bff166f35d5577a08b75b
  • Vorgänger 21187f8b
  • Nachfolger 01a4e61c

weitere Methoden aus aus check_objects ausgelagert

Unterschiede anzeigen:

SL/Controller/CsvImport/Order.pm
267 267
                     map { "${_}_id" } grep { exists $self->controller->data->[1]->{raw_data}->{$_} } qw(project price_factor pricegroup));
268 268
  $self->add_columns($self->_item_column, 'project_id') if exists $self->controller->data->[1]->{raw_data}->{projectnumber};
269 269

  
270
  # add orderitems to order
271
  my $order_entry;
272
  my @orderitems;
273
  foreach my $entry (@{ $self->controller->data }) {
274
    # search first Order
275
    if ($entry->{raw_data}->{datatype} eq $self->_order_column) {
276 270

  
277
      # new order entry: add collected orderitems to the last one
278
      if (defined $order_entry) {
279
        $order_entry->{object}->orderitems(@orderitems);
280
        @orderitems = ();
281
      }
271
  $self->add_items_to_order();
272
  $self->handle_prices_and_taxes();
282 273

  
283
      $order_entry = $entry;
284

  
285
    } elsif ( defined $order_entry && $entry->{raw_data}->{datatype} eq $self->_item_column ) {
286
      # collect orderitems to add to order (if they have no errors)
287
      # ( add_orderitems does not work here if we want to call
288
      #   calculate_prices_and_taxes afterwards ...
289
      #   so collect orderitems and add them at once)
290
      if (scalar @{ $entry->{errors} } == 0) {
291
        push @orderitems, $entry->{object};
292
      }
293
    }
294
  }
295
  # add last collected orderitems to last order
296
  if ($order_entry) {
297
    $order_entry->{object}->orderitems(@orderitems);
298
  }
299

  
300
  # calculate prices and taxes
301
  foreach my $entry (@{ $self->controller->data }) {
302
    next if @{ $entry->{errors} };
303

  
304
    if ($entry->{raw_data}->{datatype} eq $self->_order_column) {
305

  
306
      $entry->{object}->calculate_prices_and_taxes;
307

  
308
      $entry->{info_data}->{calc_amount}    = $entry->{object}->amount_as_number;
309
      $entry->{info_data}->{calc_netamount} = $entry->{object}->netamount_as_number;
310
    }
311
  }
312

  
313
  # If amounts are given, show calculated amounts as info and given amounts (verify_xxx).
314
  # And throw an error if the differences are too big.
315
  my @to_verify = ( { column      => 'amount',
316
                      raw_column  => 'verify_amount',
317
                      info_header => 'Calc. Amount',
318
                      info_method => 'calc_amount',
319
                      err_msg     => 'Amounts differ too much',
320
                    },
321
                    { column      => 'netamount',
322
                      raw_column  => 'verify_netamount',
323
                      info_header => 'Calc. Net amount',
324
                      info_method => 'calc_netamount',
325
                      err_msg     => 'Net amounts differ too much',
326
                    } );
327

  
328
  foreach my $tv (@to_verify) {
329
    # Todo: access via ->[0] ok? Better: search first order column and use this
330
    if (exists $self->controller->data->[0]->{raw_data}->{ $tv->{raw_column} }) {
331
      $self->add_raw_data_columns($self->_order_column, $tv->{raw_column});
332
      $self->add_info_columns($self->_order_column,
333
                              { header => $::locale->text($tv->{info_header}), method => $tv->{info_method} });
334
    }
335

  
336
    # check differences
337
    foreach my $entry (@{ $self->controller->data }) {
338
      next if @{ $entry->{errors} };
339
      if ($entry->{raw_data}->{datatype} eq $self->_order_column) {
340
        next if !$entry->{raw_data}->{ $tv->{raw_column} };
341
        my $parsed_value = $::form->parse_amount(\%::myconfig, $entry->{raw_data}->{ $tv->{raw_column} });
342
        if (abs($entry->{object}->${ \$tv->{column} } - $parsed_value) > $self->settings->{'max_amount_diff'}) {
343
          push @{ $entry->{errors} }, $::locale->text($tv->{err_msg});
344
        }
345
      }
346
    }
347
  }
348 274

  
349 275
  # If order has errors set error for orderitems as well
350
  $order_entry = undef;
276
  my $order_entry;
351 277
  foreach my $entry (@{ $self->controller->data }) {
352 278
    # Search first order
353 279
    if ($entry->{raw_data}->{datatype} eq $self->_order_column) {
......
726 652
  return 1;
727 653
}
728 654

  
655
sub add_items_to_order {
656
  my ($self) = @_;
657

  
658
  # add orderitems to order
659
  my $order_entry;
660
  my @orderitems;
661
  foreach my $entry (@{ $self->controller->data }) {
662
    # search first order
663
    if ($entry->{raw_data}->{datatype} eq $self->_order_column) {
664

  
665
      # new order entry: add collected orderitems to the previous one
666
      if (defined $order_entry) {
667
        $order_entry->{object}->orderitems(@orderitems);
668
        @orderitems = ();
669
      }
670

  
671
      $order_entry = $entry;
672

  
673
    } elsif ( defined $order_entry && $entry->{raw_data}->{datatype} eq $self->_item_column ) {
674
      # collect orderitems to add to order (if they have no errors)
675
      # ( add_orderitems does not work here if we want to call
676
      #   calculate_prices_and_taxes afterwards ...
677
      #   so collect orderitems and add them at once )
678
      push @orderitems, $entry->{object} if (scalar @{ $entry->{errors} } == 0);
679
    }
680
  }
681
  # add last collected orderitems to last order
682
  $order_entry->{object}->orderitems(@orderitems) if $order_entry;
683
}
684

  
685
sub handle_prices_and_taxes() {
686
  my ($self) = @_;
687

  
688
  # calculate prices and taxes
689
  foreach my $entry (@{ $self->controller->data }) {
690
    next if @{ $entry->{errors} };
691

  
692
    if ($entry->{raw_data}->{datatype} eq $self->_order_column) {
693

  
694
      $entry->{object}->calculate_prices_and_taxes;
695

  
696
      $entry->{info_data}->{calc_amount}    = $entry->{object}->amount_as_number;
697
      $entry->{info_data}->{calc_netamount} = $entry->{object}->netamount_as_number;
698
    }
699
  }
700

  
701
  # If amounts are given, show calculated amounts as info and given amounts (verify_xxx).
702
  # And throw an error if the differences are too big.
703
  my @to_verify = ( { column      => 'amount',
704
                      raw_column  => 'verify_amount',
705
                      info_header => 'Calc. Amount',
706
                      info_method => 'calc_amount',
707
                      err_msg     => 'Amounts differ too much',
708
                    },
709
                    { column      => 'netamount',
710
                      raw_column  => 'verify_netamount',
711
                      info_header => 'Calc. Net amount',
712
                      info_method => 'calc_netamount',
713
                      err_msg     => 'Net amounts differ too much',
714
                    } );
715

  
716
  foreach my $tv (@to_verify) {
717
    # Todo: access via ->[0] ok? Better: search first order column and use this
718
    if (exists $self->controller->data->[0]->{raw_data}->{ $tv->{raw_column} }) {
719
      $self->add_raw_data_columns($self->_order_column, $tv->{raw_column});
720
      $self->add_info_columns($self->_order_column,
721
                              { header => $::locale->text($tv->{info_header}), method => $tv->{info_method} });
722
    }
723

  
724
    # check differences
725
    foreach my $entry (@{ $self->controller->data }) {
726
      next if @{ $entry->{errors} };
727
      if ($entry->{raw_data}->{datatype} eq $self->_order_column) {
728
        next if !$entry->{raw_data}->{ $tv->{raw_column} };
729
        my $parsed_value = $::form->parse_amount(\%::myconfig, $entry->{raw_data}->{ $tv->{raw_column} });
730
        if (abs($entry->{object}->${ \$tv->{column} } - $parsed_value) > $self->settings->{'max_amount_diff'}) {
731
          push @{ $entry->{errors} }, $::locale->text($tv->{err_msg});
732
        }
733
      }
734
    }
735
  }
736

  
737
}
729 738

  
730 739
sub save_objects {
731 740
  my ($self, %params) = @_;

Auch abrufbar als: Unified diff