Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision ea507623

Von Kivitendo Admin vor etwa 8 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

  

Auch abrufbar als: Unified diff