Revision 9e152755
Von Moritz Bunkus vor mehr als 13 Jahren hinzugefügt
SL/Controller/CsvImport.pm | ||
---|---|---|
8 | 8 |
use SL::SessionFile; |
9 | 9 |
use SL::Controller::CsvImport::Contact; |
10 | 10 |
use SL::Controller::CsvImport::CustomerVendor; |
11 |
use SL::Controller::CsvImport::Shipto; |
|
11 | 12 |
|
12 | 13 |
use List::MoreUtils qw(none); |
13 | 14 |
|
... | ... | |
144 | 145 |
|
145 | 146 |
my $worker = $self->{type} eq 'customers_vendors' ? SL::Controller::CsvImport::CustomerVendor->new(controller => $self, file => $file) |
146 | 147 |
: $self->{type} eq 'contacts' ? SL::Controller::CsvImport::Contact->new( controller => $self, file => $file) |
148 |
: $self->{type} eq 'addresses' ? SL::Controller::CsvImport::Shipto->new( controller => $self, file => $file) |
|
147 | 149 |
: die "Program logic error"; |
148 | 150 |
|
149 | 151 |
$worker->run; |
SL/Controller/CsvImport/Shipto.pm | ||
---|---|---|
1 |
package SL::Controller::CsvImport::Shipto; |
|
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::Shipto'); |
|
18 |
} |
|
19 |
|
|
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 |
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 |
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 }; |
|
62 |
} |
|
63 |
} |
|
64 |
|
|
65 |
sub check_duplicates { |
|
66 |
my ($self, %params) = @_; |
|
67 |
|
|
68 |
my $normalizer = sub { my $name = $_[0]; $name =~ s/[\s,\.\-]//g; return $name; }; |
|
69 |
my $name_maker = sub { return $normalizer->($_[0]->shiptoname) . '--' . $normalizer->($_[0]->shiptostreet) }; |
|
70 |
|
|
71 |
my %by_id_and_name; |
|
72 |
if ('check_db' eq $self->controller->profile->get('duplicates')) { |
|
73 |
foreach my $type (qw(customers vendors)) { |
|
74 |
foreach my $vc (@{ $self->all_vc->{$type} }) { |
|
75 |
$by_id_and_name{ $vc->id } = { map { ( $name_maker->($_) => 'db' ) } @{ $vc->shipto } }; |
|
76 |
} |
|
77 |
} |
|
78 |
} |
|
79 |
|
|
80 |
foreach my $entry (@{ $self->controller->data }) { |
|
81 |
next if @{ $entry->{errors} }; |
|
82 |
|
|
83 |
my $name = $name_maker->($entry->{object}); |
|
84 |
|
|
85 |
$by_id_and_name{ $entry->{vc}->id } ||= { }; |
|
86 |
if (!$by_id_and_name{ $entry->{vc}->id }->{ $name }) { |
|
87 |
$by_id_and_name{ $entry->{vc}->id }->{ $name } = 'csv'; |
|
88 |
|
|
89 |
} else { |
|
90 |
push @{ $entry->{errors} }, $by_id_and_name{ $entry->{vc}->id }->{ $name } eq 'db' ? $::locale->text('Duplicate in database') : $::locale->text('Duplicate in CSV file'); |
|
91 |
} |
|
92 |
} |
|
93 |
} |
|
94 |
|
|
95 |
sub field_lengths { |
|
96 |
return ( shiptoname => 75, |
|
97 |
shiptodepartment_1 => 75, |
|
98 |
shiptodepartment_2 => 75, |
|
99 |
shiptostreet => 75, |
|
100 |
shiptozipcode => 75, |
|
101 |
shiptocity => 75, |
|
102 |
shiptocountry => 75, |
|
103 |
shiptocontact => 75, |
|
104 |
shiptophone => 30, |
|
105 |
shiptofax => 30, |
|
106 |
); |
|
107 |
} |
|
108 |
|
|
109 |
1; |
SL/DB/Customer.pm | ||
---|---|---|
14 | 14 |
class => 'SL::DB::Shipto', |
15 | 15 |
column_map => { id => 'trans_id' }, |
16 | 16 |
manager_args => { sort_by => 'lower(shipto.shiptoname)' }, |
17 |
query_args => [ 'module' => 'CT' ], |
|
17 |
query_args => [ module => 'CT' ], |
|
18 |
}, |
|
19 |
contacts => { |
|
20 |
type => 'one to many', |
|
21 |
class => 'SL::DB::Contact', |
|
22 |
column_map => { id => 'cp_cv_id' }, |
|
23 |
manager_args => { sort_by => 'lower(contacts.cp_name)' }, |
|
18 | 24 |
}, |
19 | 25 |
business => { |
20 | 26 |
type => 'one to one', |
SL/DB/Vendor.pm | ||
---|---|---|
13 | 13 |
class => 'SL::DB::Shipto', |
14 | 14 |
column_map => { id => 'trans_id' }, |
15 | 15 |
manager_args => { sort_by => 'lower(shipto.shiptoname)' }, |
16 |
query_args => [ 'shipto.module' => 'CT' ],
|
|
16 |
query_args => [ module => 'CT' ],
|
|
17 | 17 |
}, |
18 | 18 |
contacts => { |
19 | 19 |
type => 'one to many', |
menu.ini | ||
---|---|---|
789 | 789 |
action=CsvImport/new |
790 | 790 |
profile.type=contacts |
791 | 791 |
|
792 |
# [System--Import CSV2--Shipto]
|
|
793 |
# module=controller.pl
|
|
794 |
# action=CsvImport/new
|
|
795 |
# profile.type=addresses
|
|
792 |
[System--Import CSV2--Shipto] |
|
793 |
module=controller.pl |
|
794 |
action=CsvImport/new |
|
795 |
profile.type=addresses |
|
796 | 796 |
|
797 | 797 |
# [System--Import CSV2--Parts] |
798 | 798 |
# module=controller.pl |
Auch abrufbar als: Unified diff
Import von Lieferadressen.