Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 7b9d1666

Von Moritz Bunkus vor mehr als 13 Jahren hinzugefügt

  • ID 7b9d1666e389bfedba43b2d0fd5d1b0e3f129e88
  • Vorgänger efc63086
  • Nachfolger 136a063c

Import von Ansprechpartnern

Unterschiede anzeigen:

SL/Controller/CsvImport.pm
6 6
use SL::DB::CsvImportProfile;
7 7
use SL::Helper::Flash;
8 8
use SL::SessionFile;
9
use SL::Controller::CsvImport::Contact;
9 10
use SL::Controller::CsvImport::CustomerVendor;
10 11

  
11 12
use List::MoreUtils qw(none);
SL/Controller/CsvImport/Base.pm
2 2

  
3 3
use strict;
4 4

  
5
use List::MoreUtils qw(pairwise);
6

  
5 7
use SL::Helper::Csv;
6 8

  
7 9
use parent qw(Rose::Object);
......
33 35
  $headers->{methods} = [ map { $profile->{$_} } @{ $headers->{headers} } ];
34 36
  $self->controller->headers($headers);
35 37

  
36
  $self->controller->data([ map { { object => $_, errors => [] } } $self->csv->get_objects ]);
38
  # my @data;
39
  # foreach my $object ($self->csv->get_objects)
40
  my @objects  = $self->csv->get_objects;
41
  my @raw_data = @{ $self->csv->get_data };
42
  $self->controller->data([ pairwise { { object => $a, raw_data => $b, errors => [] } } @objects, @raw_data ]);
37 43

  
38 44
  $self->check_objects;
39 45
  $self->check_duplicates if $self->controller->profile->get('duplicates', 'no_check') ne 'no_check';
SL/Controller/CsvImport/Contact.pm
1
package SL::Controller::CsvImport::Contact;
2

  
3
use strict;
4

  
5
use SL::Helper::Csv;
6

  
7
use parent qw(SL::Controller::CsvImport::Base);
8

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

  
15
sub init_class {
16
  my ($self) = @_;
17
  $self->class('SL::DB::Contact');
18
}
19

  
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
sub check_objects {
28
  my ($self) = @_;
29

  
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
  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
    }
48

  
49
    if ($object->cp_cv_id) {
50
      $object->cp_cv_id(undef) if !$by_id{ $object->cp_cv_id };
51
    }
52

  
53
    if (!$object->cp_cv_id) {
54
      $::lxdebug->message(0, "cnum" . $raw_data->{customernumber});
55
      my $vc_id = $by_number{customers}->{ $raw_data->{customernumber} } || $by_number{vendors}->{ $raw_data->{vendornumber} };
56
      $object->cp_cv_id($vc_id) if $vc_id;
57
    }
58

  
59
    if (!$object->cp_cv_id) {
60
      my $vc_id = $by_name{customers}->{ $raw_data->{customer} } || $by_name{vendors}->{ $raw_data->{vendor} };
61
      $object->cp_cv_id($vc_id) if $vc_id;
62
    }
63

  
64
    if (!$object->cp_cv_id) {
65
      push @{ $entry->{errors} }, $::locale->text('Error: Customer/vendor not found');
66
      next;
67
    }
68

  
69
    $entry->{vc} = $by_id{ $object->cp_cv_id };
70

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

  
78
sub check_duplicates {
79
  my ($self, %params) = @_;
80

  
81
  my $normalizer = sub { my $name = $_[0]; $name =~ s/[\s,\.\-]//g; return $name; };
82

  
83
  my %by_id_and_name;
84
  if ('check_db' eq $self->controller->profile->get('duplicates')) {
85
    foreach my $type (qw(customers vendors)) {
86
      foreach my $vc (@{ $self->all_vc->{$type} }) {
87
        $by_id_and_name{ $vc->id } = { map { ( $normalizer->($_->cp_name) => 'db' ) } @{ $vc->contacts } };
88
      }
89
    }
90
  }
91

  
92
  foreach my $entry (@{ $self->controller->data }) {
93
    next if @{ $entry->{errors} };
94

  
95
    my $name = $normalizer->($entry->{object}->cp_name);
96

  
97
    $by_id_and_name{ $entry->{vc}->id } ||= { };
98
    if (!$by_id_and_name{ $entry->{vc}->id }->{ $name }) {
99
      $by_id_and_name{ $entry->{vc}->id }->{ $name } = 'csv';
100

  
101
    } else {
102
      push @{ $entry->{errors} }, $by_id_and_name{ $entry->{vc}->id }->{ $name } eq 'db' ? $::locale->text('Duplicate in database') : $::locale->text('Duplicate in CSV file');
103
    }
104
  }
105
}
106

  
107
sub field_lengths {
108
  return ( cp_title     => 75,
109
           cp_givenname => 75,
110
           cp_name      => 75,
111
           cp_phone1    => 75,
112
           cp_phone2    => 75,
113
           cp_gender    =>  1,
114
         );
115
}
116

  
117
1;
menu.ini
784 784
action=CsvImport/new
785 785
profile.type=customers_vendors
786 786

  
787
# [System--Import CSV2--Contacts]
788
# module=controller.pl
789
# action=CsvImport/new
790
# profile.type=contacts
787
[System--Import CSV2--Contacts]
788
module=controller.pl
789
action=CsvImport/new
790
profile.type=contacts
791 791

  
792 792
# [System--Import CSV2--Shipto]
793 793
# module=controller.pl

Auch abrufbar als: Unified diff