Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 8fbb71e4

Von Niclas Zimmermann vor etwa 11 Jahren hinzugefügt

  • ID 8fbb71e402a1ab11d1ed2ecb8d122b14f3b28abc
  • Vorgänger 098e73fe
  • Nachfolger 95795aa0

Auslagern von Konsistenz-Check in neue Helper-Klasse

Es wurde mehrfach die sub check_currencies in den
Csv-Importen kopiert. Dafür wurde jetzt eine neue
Helper-Klasse geschrieben, wo solche Konsistenz-
Prüfungen vorgenommen werden können.

Unterschiede anzeigen:

SL/Controller/CsvImport/Base.pm
5 5
use List::MoreUtils qw(pairwise any);
6 6

  
7 7
use SL::Helper::Csv;
8
use SL::DB::Currency;
9 8
use SL::DB::Customer;
10 9
use SL::DB::Language;
11 10
use SL::DB::PaymentTerm;
......
18 17
use Rose::Object::MakeMethods::Generic
19 18
(
20 19
 scalar                  => [ qw(controller file csv test_run save_with_cascade) ],
21
 'scalar --get_set_init' => [ qw(profile displayable_columns existing_objects class manager_class cvar_columns all_cvar_configs all_languages payment_terms_by all_currencies default_currency_id all_vc vc_by) ],
20
 'scalar --get_set_init' => [ qw(profile displayable_columns existing_objects class manager_class cvar_columns all_cvar_configs all_languages payment_terms_by all_vc vc_by) ],
22 21
);
23 22

  
24 23
sub run {
......
140 139
  return SL::DB::Manager::Language->get_all;
141 140
}
142 141

  
143
sub init_all_currencies {
144
  my ($self) = @_;
145

  
146
  return SL::DB::Manager::Currency->get_all;
147
}
148

  
149
sub init_default_currency_id {
150
  my ($self) = @_;
151

  
152
  return SL::DB::Default->get->currency_id;
153
}
154

  
155 142
sub init_payment_terms_by {
156 143
  my ($self) = @_;
157 144

  
SL/Controller/CsvImport/CustomerVendor.pm
3 3
use strict;
4 4

  
5 5
use SL::Helper::Csv;
6
use SL::Helper::Csv::Consistency;
6 7
use SL::DB::Business;
7 8
use SL::DB::CustomVariable;
8 9
use SL::DB::CustomVariableConfig;
......
13 14

  
14 15
use Rose::Object::MakeMethods::Generic
15 16
(
16
 'scalar --get_set_init' => [ qw(table languages_by businesses_by currencies_by) ],
17
 'scalar --get_set_init' => [ qw(table languages_by businesses_by) ],
17 18
);
18 19

  
19 20
sub init_table {
......
44 45
  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $self->all_languages } } ) } qw(id description article_code) };
45 46
}
46 47

  
47
sub init_currencies_by {
48
  my ($self) = @_;
49

  
50
  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $self->all_currencies } } ) } qw(id name) };
51
}
52

  
53 48
sub check_objects {
54 49
  my ($self) = @_;
55 50

  
......
71 66
    $self->check_language($entry);
72 67
    $self->check_business($entry);
73 68
    $self->check_payment($entry);
74
    $self->check_currency($entry);
69
    SL::Helper::Csv::Consistency->check_currency($entry, take_default => 1);
75 70
    $self->handle_cvars($entry);
76 71

  
77 72
    next if @{ $entry->{errors} };
......
162 157
  return 1;
163 158
}
164 159

  
165
sub check_currency {
166
  my ($self, $entry) = @_;
167

  
168
  my $object = $entry->{object};
169

  
170
  # Check whether or not currency ID is valid.
171
  if ($object->currency_id && !$self->currencies_by->{id}->{ $object->currency_id }) {
172
    push @{ $entry->{errors} }, $::locale->text('Error: Invalid currency');
173
    return 0;
174
  }
175

  
176
  # Map name to ID if given.
177
  if (!$object->currency_id && $entry->{raw_data}->{currency}) {
178
    my $currency = $self->currencies_by->{name}->{  $entry->{raw_data}->{currency} };
179
    if (!$currency) {
180
      push @{ $entry->{errors} }, $::locale->text('Error: Invalid currency');
181
      return 0;
182
    }
183

  
184
    $object->currency_id($currency->id);
185
  }
186

  
187
  # Set default currency if none was given.
188
  $object->currency_id($self->default_currency_id) if !$object->currency_id;
189

  
190
  $entry->{raw_data}->{currency_id} = $object->currency_id;
191

  
192
  return 1;
193
}
194

  
195 160
sub check_business {
196 161
  my ($self, $entry) = @_;
197 162

  
SL/Controller/CsvImport/Order.pm
24 24

  
25 25
use Rose::Object::MakeMethods::Generic
26 26
(
27
 'scalar --get_set_init' => [ qw(settings languages_by parts_by contacts_by departments_by projects_by ct_shiptos_by taxzones_by price_factors_by pricegroups_by currencies_by) ],
27
 'scalar --get_set_init' => [ qw(settings languages_by parts_by contacts_by departments_by projects_by ct_shiptos_by taxzones_by price_factors_by pricegroups_by) ],
28 28
);
29 29

  
30 30

  
......
244 244
  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_pricegroups } } ) } qw(id pricegroup) };
245 245
}
246 246

  
247
sub init_currencies_by {
248
  my ($self) = @_;
249

  
250
  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $self->all_currencies } } ) } qw(id name) };
251
}
252

  
253 247
sub check_objects {
254 248
  my ($self) = @_;
255 249

  
......
330 324
  $self->check_project($entry, global => 1);
331 325
  $self->check_ct_shipto($entry);
332 326
  $self->check_taxzone($entry);
333
  $self->check_currency($entry);
327
  SL::Helper::Csv::Consistency->check_currency($entry, take_default => 0);
334 328

  
335 329
  if ($vc_obj) {
336 330
    # copy from customer if not given
......
649 643
  return 1;
650 644
}
651 645

  
652
sub check_currency {
653
  my ($self, $entry) = @_;
654

  
655
  my $object = $entry->{object};
656

  
657
  # Check whether or not currency ID is valid.
658
  if ($object->currency_id && !$self->currencies_by->{id}->{ $object->currency_id }) {
659
    push @{ $entry->{errors} }, $::locale->text('Error: Invalid currency');
660
    return 0;
661
  }
662

  
663
  # Map name to ID if given.
664
  if (!$object->currency_id && $entry->{raw_data}->{currency}) {
665
    my $currency = $self->currencies_by->{name}->{  $entry->{raw_data}->{currency} };
666
    if (!$currency) {
667
      push @{ $entry->{errors} }, $::locale->text('Error: Invalid currency');
668
      return 0;
669
    }
670

  
671
    $object->currency_id($currency->id);
672
  }
673

  
674
  return 1;
675
}
676

  
677 646
sub add_items_to_order {
678 647
  my ($self) = @_;
679 648

  

Auch abrufbar als: Unified diff