Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision ea507623

Von Kivitendo Admin vor fast 9 Jahren hinzugefügt

  • ID ea507623073ea48951f1c1ba38f2a0841b14d47a
  • Vorgänger 8917f20a
  • Nachfolger bce4bcf8

Auftragsimport - Methoden ausgelagert

für Prüfung von Abteilung, Projekt, Bearbeiter und Verkäufer

Vorbereitung für Debitorenbuchungsimport

Unterschiede anzeigen:

SL/Controller/CsvImport/Helper/Consistency.pm
2 2

  
3 3
use strict;
4 4

  
5
use Data::Dumper;
5 6
use SL::DB::Default;
6 7
use SL::DB::Currency;
7 8
use SL::DB::TaxZone;
9
use SL::DB::Project;
10
use SL::DB::Department;
8 11

  
9 12
use SL::Helper::Csv::Error;
10 13

  
11 14
use parent qw(Exporter);
12
our @EXPORT = qw(check_currency check_taxzone);
15
our @EXPORT = qw(check_currency check_taxzone check_project check_department check_customer_vendor handle_salesman handle_employee);
13 16

  
14 17
#
15 18
# public functions
......
92 95
  if (!$object->taxzone_id && $params{take_default}) {
93 96
    # my $default_id = $self->settings->{'default_taxzone'};
94 97
    my $default_id = $self->controller->profile->get('default_taxzone');
98
    $default_id ||= _default_taxzone_id($self);
95 99
    $object->taxzone_id($default_id);
96 100
    # check if default taxzone_id is valid just to be sure
97 101
    if (! _taxzones_by($self)->{id}->{ $object->taxzone_id }) {
98 102
      push @{ $entry->{errors} }, $::locale->text('Error with default taxzone');
99 103
      return 0;
100
    };
104
    }
101 105
  };
102 106

  
103 107
  # for the order import at this stage $object->taxzone_id may still not be
104
  # defined, in this case the customer/vendor taxzone will be used.
108
  # defined, in this case the customer/vendor taxzone will be used later
109
  if ( defined $object->taxzone_id ) {
110
    $entry->{info_data}->{taxzone} = _taxzones_by($self)->{id}->{ $object->taxzone_id }->description;
111
  };
112

  
113
  return 1;
114
}
115

  
116
sub check_project {
117
  my ($self, $entry, %params) = @_;
118

  
119
  my $id_column          = ($params{global} ? 'global' : '') . 'project_id';
120
  my $number_column      = ($params{global} ? 'global' : '') . 'projectnumber';
121
  my $description_column = ($params{global} ? 'global' : '') . 'project';
122

  
123
  my $object = $entry->{object};
124

  
125
  # Check whether or not project ID is valid.
126
  if ($object->$id_column) {
127
    if (! _projects_by($self)->{id}->{ $object->$id_column }) {
128
      push @{ $entry->{errors} }, $::locale->text('Error: Invalid project');
129
      return 0;
130
    } else {
131
      $entry->{info_data}->{$number_column} = _projects_by($self)->{id}->{ $object->$id_column }->description;
132
    };
133
  }
134

  
135
  my $proj;
136
  # Map number to ID if given.
137
  if (!$object->$id_column && $entry->{raw_data}->{$number_column}) {
138
    $proj = _projects_by($self)->{projectnumber}->{ $entry->{raw_data}->{$number_column} };
139
    if (!$proj) {
140
      push @{ $entry->{errors} }, $::locale->text('Error: Invalid project');
141
      return 0;
142
    }
143

  
144
    $object->$id_column($proj->id);
145
  }
146

  
147
  # Map description to ID if given.
148
  if (!$object->$id_column && $entry->{raw_data}->{$description_column}) {
149
    $proj = _projects_by($self)->{description}->{ $entry->{raw_data}->{$description_column} };
150
    if (!$proj) {
151
      push @{ $entry->{errors} }, $::locale->text('Error: Invalid project');
152
      return 0;
153
    }
154

  
155
    $object->$id_column($proj->id);
156
  }
157

  
158
  if ( $proj ) {
159
    $entry->{info_data}->{"$description_column"} = $proj->description;
160
    $entry->{info_data}->{"$number_column"}      = $proj->projectnumber;
161
  };
105 162

  
106 163
  return 1;
107 164
}
108 165

  
166
sub check_department {
167
  my ($self, $entry) = @_;
168

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

  
171
  # Check whether or not department ID was assigned and is valid.
172
  if ($object->department_id) {
173
    if (!_departments_by($self)->{id}->{ $object->department_id }) {
174
      push @{ $entry->{errors} }, $::locale->text('Error: Invalid department');
175
      return 0;
176
    } else {
177
      # add department description as well, more feedback for user
178
      $entry->{info_data}->{department} = _departments_by($self)->{id}->{ $object->department_id }->description;
179
    };
180
  }
181

  
182
  # Map department description to ID if given.
183
  if (!$object->department_id && $entry->{raw_data}->{department}) {
184
    $entry->{info_data}->{department} = $entry->{raw_data}->{department};
185
    my $dep = _departments_by($self)->{description}->{ $entry->{raw_data}->{department} };
186
    if (!$dep) {
187
      push @{ $entry->{errors} }, $::locale->text('Error: Invalid department');
188
      return 0;
189
    }
190
    $entry->{info_data}->{department} = $dep->description;
191
    $object->department_id($dep->id);
192
  }
193

  
194
  return 1;
195
}
196

  
197
# ToDo: salesman by name
198
sub handle_salesman {
199
  my ($self, $entry) = @_;
200

  
201
  my $object = $entry->{object};
202
  my $vc_obj;
203
  $vc_obj    = SL::DB::Customer->new(id => $object->customer_id)->load if $object->can('customer') && $object->customer_id;
204
  $vc_obj    = SL::DB::Vendor->new(id   => $object->vendor_id)->load   if (!$vc_obj && $object->can('vendor') && $object->vendor_id);
205

  
206
  # salesman from customer/vendor or login if not given
207
  if (!$object->salesman) {
208
    if ($vc_obj && $vc_obj->salesman_id) {
209
      $object->salesman(SL::DB::Manager::Employee->find_by(id => $vc_obj->salesman_id));
210
    } else {
211
      $object->salesman(SL::DB::Manager::Employee->find_by(login => $::myconfig{login}));
212
    }
213
  }
214
}
215

  
216
# ToDo: employee by name
217
sub handle_employee {
218
  my ($self, $entry) = @_;
219

  
220
  my $object = $entry->{object};
221

  
222
  # employee from login if not given
223
  if (!$object->employee_id) {
224
    $object->employee_id(SL::DB::Manager::Employee->find_by(login => $::myconfig{login})->id);
225
  }
226
}
227

  
228

  
229

  
109 230
#
110 231
# private functions
111 232
#
......
128 249
  return SL::DB::Default->get->currency_id;
129 250
}
130 251

  
252
sub _default_taxzone_id {
253
  my ($self) = @_;
254

  
255
  return SL::DB::Manager::TaxZone->get_all_sorted(query => [ obsolete => 0 ])->[0]->id;
256
}
257

  
258
sub _departments_by {
259
  my ($self) = @_;
260

  
261
  my $all_departments = SL::DB::Manager::Department->get_all;
262
  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_departments } } ) } qw(id description) };
263
}
264

  
265
sub _projects_by {
266
  my ($self) = @_;
267

  
268
  my $all_projects = SL::DB::Manager::Project->get_all;
269
  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_projects } } ) } qw(id projectnumber description) };
270
}
271

  
131 272
sub _taxzones_by {
132 273
  my ($self) = @_;
133 274

  
SL/Controller/CsvImport/Order.pm
25 25

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

  
31 31

  
......
211 211
  return $cby;
212 212
}
213 213

  
214
sub init_departments_by {
215
  my ($self) = @_;
216

  
217
  my $all_departments = SL::DB::Manager::Department->get_all;
218
  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_departments } } ) } qw(id description) };
219
}
220

  
221
sub init_projects_by {
222
  my ($self) = @_;
223

  
224
  my $all_projects = SL::DB::Manager::Project->get_all;
225
  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_projects } } ) } qw(id projectnumber description) };
226
}
227

  
228 214
sub init_ct_shiptos_by {
229 215
  my ($self) = @_;
230 216

  
......
345 331
  $self->handle_employee($entry);
346 332
}
347 333

  
348
# ToDo: salesman by name
349
sub handle_salesman {
350
  my ($self, $entry) = @_;
351

  
352
  my $object = $entry->{object};
353
  my $vc_obj;
354
  $vc_obj    = SL::DB::Customer->new(id => $object->customer_id)->load if $object->customer_id;
355
  $vc_obj    = SL::DB::Vendor->new(id   => $object->vendor_id)->load   if (!$vc_obj && $object->vendor_id);
356

  
357
  # salesman from customer/vendor or login if not given
358
  if (!$object->salesman) {
359
    if ($vc_obj && $vc_obj->salesman_id) {
360
      $object->salesman(SL::DB::Manager::Employee->find_by(id => $vc_obj->salesman_id));
361
    } else {
362
      $object->salesman(SL::DB::Manager::Employee->find_by(login => $::myconfig{login}));
363
    }
364
  }
365
}
366

  
367
# ToDo: employee by name
368
sub handle_employee {
369
  my ($self, $entry) = @_;
370

  
371
  my $object = $entry->{object};
372

  
373
  # employee from login if not given
374
  if (!$object->employee_id) {
375
    $object->employee_id(SL::DB::Manager::Employee->find_by(login => $::myconfig{login})->id);
376
  }
377
}
378

  
379 334
sub check_language {
380 335
  my ($self, $entry) = @_;
381 336

  
......
506 461
  return 1;
507 462
}
508 463

  
509
sub check_department {
510
  my ($self, $entry) = @_;
511

  
512
  my $object = $entry->{object};
513

  
514
  # Check whether or not department ID is valid.
515
  if ($object->department_id && !$self->departments_by->{id}->{ $object->department_id }) {
516
    push @{ $entry->{errors} }, $::locale->text('Error: Invalid department');
517
    return 0;
518
  }
519

  
520
  # Map description to ID if given.
521
  if (!$object->department_id && $entry->{raw_data}->{department}) {
522
    my $dep = $self->departments_by->{description}->{ $entry->{raw_data}->{department} };
523
    if (!$dep) {
524
      push @{ $entry->{errors} }, $::locale->text('Error: Invalid department');
525
      return 0;
526
    }
527

  
528
    $object->department_id($dep->id);
529
  }
530

  
531
  return 1;
532
}
533

  
534
sub check_project {
535
  my ($self, $entry, %params) = @_;
536

  
537
  my $id_column          = ($params{global} ? 'global' : '') . 'project_id';
538
  my $number_column      = ($params{global} ? 'global' : '') . 'projectnumber';
539
  my $description_column = ($params{global} ? 'global' : '') . 'project';
540

  
541
  my $object = $entry->{object};
542

  
543
  # Check whether or not projetc ID is valid.
544
  if ($object->$id_column && !$self->projects_by->{id}->{ $object->$id_column }) {
545
    push @{ $entry->{errors} }, $::locale->text('Error: Invalid project');
546
    return 0;
547
  }
548

  
549
  # Map number to ID if given.
550
  if (!$object->$id_column && $entry->{raw_data}->{$number_column}) {
551
    my $proj = $self->projects_by->{projectnumber}->{ $entry->{raw_data}->{$number_column} };
552
    if (!$proj) {
553
      push @{ $entry->{errors} }, $::locale->text('Error: Invalid project');
554
      return 0;
555
    }
556

  
557
    $object->$id_column($proj->id);
558
  }
559

  
560
  # Map description to ID if given.
561
  if (!$object->$id_column && $entry->{raw_data}->{$description_column}) {
562
    my $proj = $self->projects_by->{description}->{ $entry->{raw_data}->{$description_column} };
563
    if (!$proj) {
564
      push @{ $entry->{errors} }, $::locale->text('Error: Invalid project');
565
      return 0;
566
    }
567

  
568
    $object->$id_column($proj->id);
569
  }
570

  
571
  return 1;
572
}
573

  
574 464
sub check_ct_shipto {
575 465
  my ($self, $entry) = @_;
576 466

  

Auch abrufbar als: Unified diff