86a3c78ca8fc74f5b6aefe1113e3866b03e74d0a
[kivitendo-erp.git] / t / controllers / csvimport / parts.t
1 use Test::More tests => 47;
2
3 use strict;
4
5 use lib 't';
6
7 use Carp;
8 use Data::Dumper;
9 use Support::TestSetup;
10 use Test::Exception;
11
12 use List::MoreUtils qw(pairwise);
13 use SL::Controller::CsvImport;
14
15 my $DEBUG = 0;
16
17 use_ok 'SL::Controller::CsvImport::Part';
18
19 use SL::DB::Buchungsgruppe;
20 use SL::DB::Currency;
21 use SL::DB::Customer;
22 use SL::DB::Language;
23 use SL::DB::Warehouse;
24 use SL::DB::Pricegroup;
25 use SL::DB::Price;
26 use SL::DB::Bin;
27
28 my ($translation, $bin1_1, $bin1_2, $bin2_1, $bin2_2, $wh1, $wh2, $bugru, $cvarconfig );
29 my ($pg1_id, $pg2_id, $pg3_id);
30
31 Support::TestSetup::login();
32
33 sub reset_state {
34   # Create test data
35
36   clear_up();
37
38   $translation     = SL::DB::Language->new(
39     description    => 'Englisch',
40     article_code   => 'EN',
41     template_code  => 'EN',
42   )->save;
43   $translation     = SL::DB::Language->new(
44     description    => 'Italienisch',
45     article_code   => 'IT',
46     template_code  => 'IT',
47   )->save;
48   $wh1 = SL::DB::Warehouse->new(
49     description    => 'Lager1',
50     sortkey        => 1,
51   )->save;
52   $bin1_1 = SL::DB::Bin->new(
53     description    => 'Ort1_von_Lager1',
54     warehouse_id   => $wh1->id,
55   )->save;
56   $bin1_2 = SL::DB::Bin->new(
57     description    => 'Ort2_von_Lager1',
58     warehouse_id   => $wh1->id,
59   )->save;
60   $wh2 = SL::DB::Warehouse->new(
61     description    => 'Lager2',
62     sortkey        => 2,
63   )->save;
64   $bin2_1 = SL::DB::Bin->new(
65     description    => 'Ort1_von_Lager2',
66     warehouse_id   => $wh2->id,
67   )->save;
68   $bin2_2 = SL::DB::Bin->new(
69     description    => 'Ort2_von_Lager2',
70     warehouse_id   => $wh2->id,
71   )->save;
72
73   $cvarconfig = SL::DB::CustomVariableConfig->new(
74     module   => 'IC',
75     name     => 'mycvar',
76     type     => 'text',
77     description => 'mein Schatz',
78     searchable  => 1,
79     sortkey => 1,
80     includeable => 0,
81     included_by_default => 0,
82   )->save;
83
84   foreach ( { id => 1, pricegroup => 'A', sortkey => 1 },
85             { id => 2, pricegroup => 'B', sortkey => 2 },
86             { id => 3, pricegroup => 'C', sortkey => 3 },
87             { id => 4, pricegroup => 'D', sortkey => 4 } ) {
88     SL::DB::Pricegroup->new(%{$_})->save;
89   }
90 }
91
92 $bugru = SL::DB::Manager::Buchungsgruppe->find_by(description => { like => 'Standard%19%' });
93
94 reset_state();
95
96 #####
97 sub test_import {
98   my ($file,$settings) = @_;
99
100   my $controller = SL::Controller::CsvImport->new(
101     type => 'parts'
102   );
103   $controller->load_default_profile;
104   $controller->profile->set(
105     charset      => 'utf-8',
106     sep_char     => ';',
107     quote_char   => '"',
108     numberformat => $::myconfig{numberformat},
109   );
110
111   my $csv_part_import = SL::Controller::CsvImport::Part->new(
112     settings   => $settings,
113     controller => $controller,
114     file       => $file,
115   );
116   #print "profile param type=".$csv_part_import->settings->{parts_type}."\n";
117
118   $csv_part_import->run(test => 0);
119
120   # don't try and save objects that have errors
121   $csv_part_import->save_objects unless scalar @{$csv_part_import->controller->data->[0]->{errors}};
122
123   return $csv_part_import->controller->data;
124 }
125
126 $::myconfig{numberformat} = '1000.00';
127 my $old_locale = $::locale;
128 # set locale to en so we can match errors
129 $::locale = Locale->new('en');
130
131
132 my ($entries, $entry, $file);
133
134 # different settings for tests
135 #
136
137 my $settings1 = {
138                        sellprice_places          => 2,
139                        sellprice_adjustment      => 0,
140                        sellprice_adjustment_type => 'percent',
141                        article_number_policy     => 'update_prices',
142                        shoparticle_if_missing    => '0',
143                        part_type                 => 'part',
144                        part_classification       => 3,
145                        default_buchungsgruppe    => ($bugru ? $bugru->id : undef),
146                        apply_buchungsgruppe      => 'all',
147                 };
148 my $settings2 = {
149                        sellprice_places          => 2,
150                        sellprice_adjustment      => 0,
151                        sellprice_adjustment_type => 'percent',
152                        article_number_policy     => 'update_parts',
153                        shoparticle_if_missing    => '0',
154                        part_type                 => 'mixed',
155                        part_classification       => 4,
156                        default_buchungsgruppe    => ($bugru ? $bugru->id : undef),
157                        apply_buchungsgruppe      => 'missing',
158                        default_unit              => 'Stck',
159                 };
160
161 #
162 #
163 # starting test of csv imports
164 # to debug errors in certain tests, run after test_import:
165 #   die Dumper($entry->{errors});
166
167
168 ##### create part with prices and 3 pricegroup prices
169 $file = \<<EOL;
170 partnumber;sellprice;lastcost;listprice;unit;pricegroup_1;pricegroup_2;pricegroup_3
171 P1000;100.10;90.20;95.30;kg;111.11;122.22;133.33
172 EOL
173 $entries = test_import($file,$settings1);
174 $entry = $entries->[0];
175 #foreach my $err ( @{ $entry->{errors} } ) {
176 #  print $err;
177 #}
178 is $entry->{object}->partnumber,'P1000', 'partnumber';
179 is $entry->{object}->sellprice, '100.1', 'sellprice';
180 is $entry->{object}->lastcost,   '90.2', 'lastcost';
181 is $entry->{object}->listprice,  '95.3', 'listprice';
182 is $entry->{object}->find_prices( { pricegroup_id => 2 } )->[0]->price,  '122.22000', 'pricegroup_2 price';
183
184 ##### update prices of part, and price of pricegroup_2, keeping pricegroup_1 and pricegroup_3
185 $file = \<<EOL;
186 partnumber;sellprice;lastcost;listprice;unit;pricegroup_2;pricegroup_4
187 P1000;110.10;95.20;97.30;kg;123.45;144.44
188 EOL
189 $entries = test_import($file,$settings1);
190 $entry = $entries->[0];
191 is $entry->{object}->sellprice, '110.1', 'updated sellprice';
192 is $entry->{object}->lastcost,   '95.2', 'updated lastcost';
193 is $entry->{object}->listprice,  '97.3', 'updated listprice';
194 # $entry->{object}->prices currently only contains prices pricegroup_2 and pricegroup_4, reload object from db
195 # printf("%s %s: %s\n", $_->pricegroup_id, $_->pricegroup->pricegroup, $_->price) foreach @{$entry->{object}->prices};
196 $entry->{object}->load;
197 is $entry->{object}->find_prices( { pricegroup_id => 1 } )->[0]->price,  '111.11000', 'pricegroup_1 price didn\'t change';
198 is $entry->{object}->find_prices( { pricegroup_id => 2 } )->[0]->price,  '123.45000', 'pricegroup_2 price was updated';
199 is $entry->{object}->find_prices( { pricegroup_id => 4 } )->[0]->price,  '144.44000', 'pricegroup_4 price was added';
200
201 ##### insert parts with warehouse,bin name
202
203 $file = \<<EOL;
204 partnumber;description;warehouse;bin;part_type
205 P1000;Teil 1000;Lager1;Ort1_von_Lager1;part
206 P1001;Teil 1001;Lager1;Ort2_von_Lager1;service
207 P1002;Teil 1002;Lager2;Ort1_von_Lager2;service
208 P1003;Teil 1003;Lager2;Ort2_von_Lager2;part
209 EOL
210 $entries = test_import($file,$settings2);
211 $entry = $entries->[0];
212 is $entry->{object}->description, 'Teil 1000', 'Teil 1000 set';
213 is $entry->{object}->warehouse_id, $wh1->id, 'Lager1';
214 is $entry->{object}->bin_id, $bin1_1->id, 'Lagerort1';
215 is $entry->{object}->part_type, 'part', 'Typ ist part';
216 $entry = $entries->[2];
217 is $entry->{object}->description, 'Teil 1002', 'Teil 1002 set';
218 is $entry->{object}->warehouse_id, $wh2->id, 'Lager2';
219 is $entry->{object}->bin_id, $bin2_1->id, 'Lagerort1';
220 is $entry->{object}->part_type, 'service', 'Typ ist service';
221
222 ##### update warehouse and bin
223 $file = \<<EOL;
224 partnumber;description;warehouse;bin;part_type
225 P1000;Teil 1000;Lager2;Ort1_von_Lager2;part
226 P1001;Teil 1001;Lager1;Ort1_von_Lager1;part
227 P1002;Teil 1002;Lager2;Ort1_von_Lager1;part
228 P1003;Teil 1003;Lager2;kein Lagerort;part
229 EOL
230 $entries = test_import($file,$settings2);
231 $entry = $entries->[0];
232 is $entry->{object}->description, 'Teil 1000', 'Teil 1000 set';
233 is $entry->{object}->warehouse_id, $wh2->id, 'Lager2';
234 is $entry->{object}->bin_id, $bin2_1->id, 'Lagerort1';
235 $entry = $entries->[2];
236 my $err1 = @{ $entry->{errors} }[0];
237 #print "'".$err1."'\n";
238 is $entry->{object}->description, 'Teil 1002', 'Teil 1002 set';
239 is $entry->{object}->warehouse_id, $wh2->id, 'Lager2';
240 is $err1, 'Error: Bin Ort1_von_Lager1 is not from warehouse Lager2','kein Lager von Lager2';
241 $entry = $entries->[3];
242 $err1 = @{ $entry->{errors} }[0];
243 #print "'".$err1."'\n";
244 is $entry->{object}->description, 'Teil 1003', 'Teil 1003 set';
245 is $entry->{object}->warehouse_id, $wh2->id, 'Lager2';
246 is $err1, 'Error: Invalid bin name kein Lagerort','kein Lagerort';
247
248 ##### add translations
249 $file = \<<EOL;
250 partnumber;description;description_EN;notes_EN;description_IT;notes_IT
251 P1000;Teil 1000;descr EN 1000;notes EN;descr IT 1000;notes IT
252 P1001;Teil 1001;descr EN 1001;notes EN;descr IT 1001;notes IT
253 P1002;Teil 1002;descr EN 1002;notes EN;descr IT 1002;notes IT
254 P1003;Teil 1003;descr EN 1003;notes EN;descr IT 1003;notes IT
255 EOL
256 $entries = test_import($file,$settings2);
257 $entry = $entries->[0];
258 is $entry->{object}->description, 'Teil 1000', 'Teil 1000 set';
259 is $entry->{raw_data}->{description_EN},'descr EN 1000','EN set';
260 is $entry->{raw_data}->{description_IT},'descr IT 1000','IT set';
261 my $l = @{$entry->{object}->translations}[0];
262 is $l->translation,'descr EN 1000','EN trans set';
263 is $l->longdescription, 'notes EN','EN notes set';
264 $l = @{$entry->{object}->translations}[1];
265 is $l->translation,'descr IT 1000','IT trans set';
266 is $l->longdescription, 'notes IT','IT notes set';
267
268 ##### add customvar
269 $file = \<<EOL;
270 partnumber;cvar_mycvar
271 P1000;das ist der Ring
272 P1001;nicht der Nibelungen
273 P1002;sondern vom
274 P1003;Herr der Ringe
275 EOL
276 $entries = test_import($file,$settings2);
277 $entry = $entries->[0];
278 is $entry->{object}->partnumber, 'P1000', 'P1000 set';
279 is $entry->{raw_data}->{cvar_mycvar},'das ist der Ring','CVAR set';
280 is @{$entry->{object}->custom_variables}[0]->text_value,'das ist der Ring','Cvar mit richtigem Wert';
281
282 # set locale to de so we can match abbreviations
283 $::locale = $old_locale;
284 ##### import part classification
285 $file = \<<EOL;
286 partnumber;pclass;description
287 W1000;WE;Teil 1000
288 W1001;WV;Teil 1001
289 D1002;DV;Dienstleistung 1002
290 D1003;DH;Dienstleistung 1003
291 EOL
292 $entries = test_import($file,$settings2);
293 $entry = $entries->[0];
294 is $entry->{object}->classification_id, '1', 'W1000 von Klasse Einkauf';
295 is $entry->{object}->type, 'part', 'W1000 vom Type part';
296 $entry = $entries->[1];
297 is $entry->{object}->classification_id, '2', 'W1001 von Klasse Verkauf';
298 is $entry->{object}->type, 'part', 'W1001 vom Type part';
299 $entry = $entries->[2];
300 is $entry->{object}->classification_id, '2', 'D1002 von Klasse Verkauf';
301 is $entry->{object}->type, 'service', 'D1002 vom Type service';
302 $entry = $entries->[3];
303 is $entry->{object}->classification_id, '3', 'D1003 von Klasse Handelsware';
304 is $entry->{object}->type, 'service', 'D1003 vom Type service';
305
306
307 clear_up(); # remove all data at end of tests
308
309 # end of tests
310
311
312 sub clear_up {
313   SL::DB::Manager::Part       ->delete_all(all => 1);
314   SL::DB::Manager::Pricegroup ->delete_all(all => 1);
315   SL::DB::Manager::Price      ->delete_all(all => 1);
316   SL::DB::Manager::Translation->delete_all(all => 1);
317   SL::DB::Manager::Language   ->delete_all(all => 1);
318   SL::DB::Manager::Bin        ->delete_all(all => 1);
319   SL::DB::Manager::Warehouse  ->delete_all(all => 1);
320   SL::DB::Manager::CustomVariableConfig->delete_all(all => 1);
321 }
322
323
324 1;
325
326 #####
327 # vim: ft=perl
328 # set emacs to perl mode
329 # Local Variables:
330 # mode: perl
331 # End: