Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 2c6717d9

Von Moritz Bunkus vor mehr als 13 Jahren hinzugefügt

  • ID 2c6717d9653f11fd560fdac0a2aa06b4096ba2b6
  • Vorgänger 2c139ad7
  • Nachfolger 9d6a2ef2

Refactoring: gemeinsamen Code ausgelagert; Ansprechpersonen/Lieferadressen: Kunden-/Lieferantennamen anzeigen

Unterschiede anzeigen:

SL/Controller/CsvImport.pm
18 18
use Rose::Object::MakeMethods::Generic
19 19
(
20 20
 scalar => [ qw(type profile file all_profiles all_charsets sep_char all_sep_chars quote_char all_quote_chars escape_char all_escape_chars all_buchungsgruppen
21
                import_status errors headers raw_data_headers data num_imported num_importable displayable_columns) ],
21
                import_status errors headers raw_data_headers info_headers data num_imported num_importable displayable_columns) ],
22 22
);
23 23

  
24 24
__PACKAGE__->run_before('check_auth');
SL/Controller/CsvImport/Base.pm
5 5
use List::MoreUtils qw(pairwise);
6 6

  
7 7
use SL::Helper::Csv;
8
use SL::DB::Customer;
8 9
use SL::DB::Language;
9 10
use SL::DB::PaymentTerm;
11
use SL::DB::Vendor;
10 12

  
11 13
use parent qw(Rose::Object);
12 14

  
13 15
use Rose::Object::MakeMethods::Generic
14 16
(
15 17
 scalar                  => [ qw(controller file csv) ],
16
 'scalar --get_set_init' => [ qw(profile displayable_columns existing_objects class manager_class cvar_columns all_cvar_configs all_languages payment_terms_by) ],
18
 '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) ],
17 19
);
18 20

  
19 21
sub run {
......
39 41
  $headers->{used}    = { map { ($_ => 1) }      @{ $headers->{headers} } };
40 42
  $self->controller->headers($headers);
41 43
  $self->controller->raw_data_headers({ used => { }, headers => [ ] });
44
  $self->controller->info_headers({ used => { }, headers => [ ] });
42 45

  
43
  # my @data;
44
  # foreach my $object ($self->csv->get_objects)
45 46
  my @objects  = $self->csv->get_objects;
46 47
  my @raw_data = @{ $self->csv->get_data };
47
  $self->controller->data([ pairwise { { object => $a, raw_data => $b, errors => [], information => [] } } @objects, @raw_data ]);
48
  $::lxdebug->dump(0, "DATA", $self->controller->data);
48
  $self->controller->data([ pairwise { { object => $a, raw_data => $b, errors => [], information => [], info_data => {} } } @objects, @raw_data ]);
49 49

  
50 50
  $self->check_objects;
51 51
  $self->check_duplicates if $self->controller->profile->get('duplicates', 'no_check') ne 'no_check';
......
64 64
  }
65 65
}
66 66

  
67
sub add_info_columns {
68
  my ($self, @columns) = @_;
69

  
70
  my $h = $self->controller->info_headers;
71

  
72
  foreach my $column (grep { !$h->{used}->{ $_->{method} } } map { ref $_ eq 'HASH' ? $_ : { method => $_, header => $_ } } @columns) {
73
    $h->{used}->{ $column->{method} } = 1;
74
    push @{ $h->{methods} }, $column->{method};
75
    push @{ $h->{headers} }, $column->{header};
76
  }
77
}
78

  
67 79
sub add_raw_data_columns {
68 80
  my ($self, @columns) = @_;
69 81

  
......
100 112
  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_payment_terms } } ) } qw(id description) };
101 113
}
102 114

  
115
sub init_all_vc {
116
  my ($self) = @_;
117

  
118
  return { customers => SL::DB::Manager::Customer->get_all,
119
           vendors   => SL::DB::Manager::Vendor->get_all };
120
}
121

  
122
sub init_vc_by {
123
  my ($self)    = @_;
124

  
125
  my %by_id     = map { ( $_->id => $_ ) } @{ $self->all_vc->{customers} }, @{ $self->all_vc->{vendors} };
126
  my %by_number = ( customers => { map { ( $_->customernumber => $_ ) } @{ $self->all_vc->{customers} } },
127
                    vendors   => { map { ( $_->vendornumber   => $_ ) } @{ $self->all_vc->{vendors}   } } );
128
  my %by_name   = ( customers => { map { ( $_->name           => $_ ) } @{ $self->all_vc->{customers} } },
129
                    vendors   => { map { ( $_->name           => $_ ) } @{ $self->all_vc->{vendors}   } } );
130

  
131
  return { id     => \%by_id,
132
           number => \%by_number,
133
           name   => \%by_name,   };
134
}
135

  
136
sub check_vc {
137
  my ($self, $entry, $id_column) = @_;
138

  
139
  if ($entry->{object}->$id_column) {
140
    $entry->{object}->$id_column(undef) if !$self->vc_by->{id}->{ $entry->{object}->$id_column };
141
  }
142

  
143
  if (!$entry->{object}->$id_column) {
144
    my $vc = $self->vc_by->{number}->{customers}->{ $entry->{raw_data}->{customernumber} }
145
          || $self->vc_by->{number}->{vendors}->{   $entry->{raw_data}->{vendornumber}   };
146
    $entry->{object}->$id_column($vc->id) if $vc;
147
  }
148

  
149
  if (!$entry->{object}->$id_column) {
150
    my $vc = $self->vc_by->{name}->{customers}->{ $entry->{raw_data}->{customer} }
151
          || $self->vc_by->{name}->{vendors}->{   $entry->{raw_data}->{vendor}   };
152
    $entry->{object}->$id_column($vc->id) if $vc;
153
  }
154

  
155
  if ($entry->{object}->$id_column) {
156
    $entry->{info_data}->{vc_name} = $self->vc_by->{id}->{ $entry->{object}->$id_column }->name;
157
  } else {
158
    push @{ $entry->{errors} }, $::locale->text('Error: Customer/vendor not found');
159
  }
160
}
161

  
103 162
sub handle_cvars {
104 163
  my ($self, $entry) = @_;
105 164

  
SL/Controller/CsvImport/Contact.pm
8 8

  
9 9
use Rose::Object::MakeMethods::Generic
10 10
(
11
 scalar                  => [ qw(table) ],
12
 'scalar --get_set_init' => [ qw(all_vc) ],
11
 scalar => [ qw(table) ],
13 12
);
14 13

  
15 14
sub init_class {
......
17 16
  $self->class('SL::DB::Contact');
18 17
}
19 18

  
20
sub init_all_vc {
21
  my ($self) = @_;
22

  
23
  $self->all_vc({ customers => SL::DB::Manager::Customer->get_all(with_objects => [ 'contacts' ]),
24
                  vendors   => SL::DB::Manager::Vendor->get_all(  with_objects => [ 'contacts' ]) });
25
}
26

  
27 19
sub check_objects {
28 20
  my ($self) = @_;
29 21

  
30
  my %by_id     = map { ( $_->id => $_ ) } @{ $self->all_vc->{customers} }, @{ $self->all_vc->{vendors} };
31
  my %by_number = ( customers => { map { ( $_->customernumber => $_->id ) } @{ $self->all_vc->{customers} } },
32
                    vendors   => { map { ( $_->vendornumber   => $_->id ) } @{ $self->all_vc->{vendors}   } } );
33
  my %by_name   = ( customers => { map { ( $_->name           => $_->id ) } @{ $self->all_vc->{customers} } },
34
                    vendors   => { map { ( $_->name           => $_->id ) } @{ $self->all_vc->{vendors}   } } );
35

  
36 22
  foreach my $entry (@{ $self->controller->data }) {
37
    my $object   = $entry->{object};
38
    my $raw_data = $entry->{raw_data};
39

  
40
    my $name     =  $object->cp_name;
41
    $name        =~ s/^\s+//;
42
    $name        =~ s/\s+$//;
43

  
44
    if (!$name) {
45
      push @{ $entry->{errors} }, $::locale->text('Error: Name missing');
46
      next;
47
    }
23
    $self->check_name($entry);
24
    $self->check_vc($entry, 'cp_cv_id');
25
    $self->check_gender($entry);
26
  }
48 27

  
49
    if ($object->cp_cv_id) {
50
      $object->cp_cv_id(undef) if !$by_id{ $object->cp_cv_id };
51
    }
28
  $self->add_info_columns({ header => $::locale->text('Customer/Vendor'), method => 'vc_name' });
29
}
52 30

  
53
    if (!$object->cp_cv_id) {
54
      my $vc_id = $by_number{customers}->{ $raw_data->{customernumber} } || $by_number{vendors}->{ $raw_data->{vendornumber} };
55
      $object->cp_cv_id($vc_id) if $vc_id;
56
    }
31
sub check_name {
32
  my ($self, $entry) = @_;
57 33

  
58
    if (!$object->cp_cv_id) {
59
      my $vc_id = $by_name{customers}->{ $raw_data->{customer} } || $by_name{vendors}->{ $raw_data->{vendor} };
60
      $object->cp_cv_id($vc_id) if $vc_id;
61
    }
34
  my $name     =  $entry->{object}->cp_name;
35
  $name        =~ s/^\s+//;
36
  $name        =~ s/\s+$//;
62 37

  
63
    if (!$object->cp_cv_id) {
64
      push @{ $entry->{errors} }, $::locale->text('Error: Customer/vendor not found');
65
      next;
66
    }
38
  push @{ $entry->{errors} }, $::locale->text('Error: Name missing') unless $name;
39
}
67 40

  
68
    $entry->{vc} = $by_id{ $object->cp_cv_id };
41
sub check_gender {
42
  my ($self, $entry) = @_;
69 43

  
70
    if (($object->cp_gender ne 'm') && ($object->cp_gender ne 'f')) {
71
      push @{ $entry->{errors} }, $::locale->text('Error: Gender (cp_gender) missing or invalid');
72
      next;
73
    }
74
  }
44
  push @{ $entry->{errors} }, $::locale->text('Error: Gender (cp_gender) missing or invalid') if ($entry->{object}->cp_gender ne 'm') && ($entry->{object}->cp_gender ne 'f');
75 45
}
76 46

  
77 47
sub check_duplicates {
SL/Controller/CsvImport/Shipto.pm
8 8

  
9 9
use Rose::Object::MakeMethods::Generic
10 10
(
11
 scalar                  => [ qw(table) ],
12
 'scalar --get_set_init' => [ qw(all_vc) ],
11
 scalar => [ qw(table) ],
13 12
);
14 13

  
15 14
sub init_class {
......
17 16
  $self->class('SL::DB::Shipto');
18 17
}
19 18

  
20
sub init_all_vc {
21
  my ($self) = @_;
22

  
23
  $self->all_vc({ customers => SL::DB::Manager::Customer->get_all,
24
                  vendors   => SL::DB::Manager::Vendor->get_all });
25
}
26

  
27 19
sub check_objects {
28 20
  my ($self) = @_;
29 21

  
30
  my %by_id     = map { ( $_->id => $_ ) } @{ $self->all_vc->{customers} }, @{ $self->all_vc->{vendors} };
31
  my %by_number = ( customers => { map { ( $_->customernumber => $_->id ) } @{ $self->all_vc->{customers} } },
32
                    vendors   => { map { ( $_->vendornumber   => $_->id ) } @{ $self->all_vc->{vendors}   } } );
33
  my %by_name   = ( customers => { map { ( $_->name           => $_->id ) } @{ $self->all_vc->{customers} } },
34
                    vendors   => { map { ( $_->name           => $_->id ) } @{ $self->all_vc->{vendors}   } } );
35

  
36 22
  foreach my $entry (@{ $self->controller->data }) {
37
    my $object   = $entry->{object};
38
    my $raw_data = $entry->{raw_data};
39

  
40
    if ($object->trans_id) {
41
      $object->trans_id(undef) if !$by_id{ $object->trans_id };
42
    }
43

  
44
    if (!$object->trans_id) {
45
      my $vc_id = $by_number{customers}->{ $raw_data->{customernumber} } || $by_number{vendors}->{ $raw_data->{vendornumber} };
46
      $object->trans_id($vc_id) if $vc_id;
47
    }
48

  
49
    if (!$object->trans_id) {
50
      my $vc_id = $by_name{customers}->{ $raw_data->{customer} } || $by_name{vendors}->{ $raw_data->{vendor} };
51
      $object->trans_id($vc_id) if $vc_id;
52
    }
53

  
54
    if (!$object->trans_id) {
55
      push @{ $entry->{errors} }, $::locale->text('Error: Customer/vendor not found');
56
      next;
57
    }
58

  
59
    $object->module('CT');
60

  
61
    $entry->{vc} = $by_id{ $object->trans_id };
23
    $self->check_vc($entry, 'trans_id');
24
    $entry->{object}->module('CT');
62 25
  }
26

  
27
  $self->add_info_columns({ header => $::locale->text('Customer/Vendor'), method => 'vc_name' });
63 28
}
64 29

  
65 30
sub check_duplicates {
locale/de/all
490 490
  'Customer not on file!'       => 'Kunde ist nicht in der Datenbank!',
491 491
  'Customer saved!'             => 'Kunde gespeichert!',
492 492
  'Customer type'               => 'Kundentyp',
493
  'Customer/Vendor'             => 'Kunde/Lieferant',
493 494
  'Customer/Vendor (database ID)' => 'Kunde/Lieferant (Datenbank-ID)',
494 495
  'Customername'                => 'Kundenname',
495 496
  'Customernumberinit'          => 'Kunden-/Lieferantennummernkreis',
templates/webpages/csv_import/_preview.html
12 12

  
13 13
 <table>
14 14
  <tr class="listheading">
15
   [%- FOREACH column = SELF.info_headers.headers %]
16
    <th>[%- HTML.escape(column) %]</th>
17
   [%- END %]
15 18
   [%- FOREACH column = SELF.headers.headers %]
16 19
    <th>[%- HTML.escape(column) %]</th>
17 20
   [%- END %]
......
23 26

  
24 27
  [%- FOREACH row = SELF.data %]
25 28
  <tr class="[% IF row.errors.size %]redrow[% ELSE %]listrow[% END %][% loop.count % 2 %]">
29
   [%- FOREACH method = SELF.info_headers.methods %]
30
    <td>[%- HTML.escape(row.info_data.$method) %]</td>
31
   [%- END %]
26 32
   [%- FOREACH method = SELF.headers.methods %]
27 33
    <td>[%- HTML.escape(row.object.$method) %]</td>
28 34
   [%- END %]

Auch abrufbar als: Unified diff