1 use Test::More tests => 43;
9 use Support::TestSetup;
12 use List::MoreUtils qw(pairwise);
13 use SL::Controller::CsvImport;
17 use_ok 'SL::Controller::CsvImport::Part';
19 use SL::DB::Buchungsgruppe;
23 use SL::DB::Warehouse;
26 my ($translation, $bin1_1, $bin1_2, $bin2_1, $bin2_2, $wh1, $wh2, $bugru, $cvarconfig );
28 Support::TestSetup::login();
35 $translation = SL::DB::Language->new(
36 description => 'Englisch',
38 template_code => 'EN',
40 $translation = SL::DB::Language->new(
41 description => 'Italienisch',
43 template_code => 'IT',
45 $wh1 = SL::DB::Warehouse->new(
46 description => 'Lager1',
49 $bin1_1 = SL::DB::Bin->new(
50 description => 'Ort1_von_Lager1',
51 warehouse_id => $wh1->id,
53 $bin1_2 = SL::DB::Bin->new(
54 description => 'Ort2_von_Lager1',
55 warehouse_id => $wh1->id,
57 $wh2 = SL::DB::Warehouse->new(
58 description => 'Lager2',
61 $bin2_1 = SL::DB::Bin->new(
62 description => 'Ort1_von_Lager2',
63 warehouse_id => $wh2->id,
65 $bin2_2 = SL::DB::Bin->new(
66 description => 'Ort2_von_Lager2',
67 warehouse_id => $wh2->id,
70 $cvarconfig = SL::DB::CustomVariableConfig->new(
74 description => 'mein Schatz',
78 included_by_default => 0,
82 $bugru = SL::DB::Manager::Buchungsgruppe->find_by(description => { like => 'Standard%19%' });
88 my ($file,$settings) = @_;
90 my $controller = SL::Controller::CsvImport->new();
92 my $csv_part_import = SL::Controller::CsvImport::Part->new(
93 settings => $settings,
94 controller => $controller,
97 #print "profile param type=".$csv_part_import->settings->{parts_type}."\n";
99 $csv_part_import->test_run(0);
100 $csv_part_import->csv(SL::Helper::Csv->new(file => $csv_part_import->file,
101 profile => [{ profile => $csv_part_import->profile,
102 class => $csv_part_import->class,
103 mapping => $csv_part_import->controller->mappings_for_profile }],
105 ignore_unknown_columns => 1,
107 case_insensitive_header => 1,
110 ignore_unknown_columns => 1,
113 $csv_part_import->csv->parse;
115 $csv_part_import->controller->errors([ $csv_part_import->csv->errors ]) if $csv_part_import->csv->errors;
117 return if ( !$csv_part_import->csv->header || $csv_part_import->csv->errors );
119 my $headers = { headers => [ grep { $csv_part_import->csv->dispatcher->is_known($_, 0) } @{ $csv_part_import->csv->header } ] };
120 $headers->{methods} = [ map { $_->{path} } @{ $csv_part_import->csv->specs->[0] } ];
121 $headers->{used} = { map { ($_ => 1) } @{ $headers->{headers} } };
122 $csv_part_import->controller->headers($headers);
123 $csv_part_import->controller->raw_data_headers({ used => { }, headers => [ ] });
124 $csv_part_import->controller->info_headers({ used => { }, headers => [ ] });
126 my $objects = $csv_part_import->csv->get_objects;
127 my @raw_data = @{ $csv_part_import->csv->get_data };
129 $csv_part_import->controller->data([ pairwise { no warnings 'once'; { object => $a, raw_data => $b, errors => [], information => [], info_data => {} } } @$objects, @raw_data ]);
131 $csv_part_import->check_objects;
133 # don't try and save objects that have errors
134 $csv_part_import->save_objects unless scalar @{$csv_part_import->controller->data->[0]->{errors}};
136 return $csv_part_import->controller->data;
139 $::myconfig{numberformat} = '1000.00';
140 my $old_locale = $::locale;
141 # set locale to en so we can match errors
142 $::locale = Locale->new('en');
145 my ($entries, $entry, $file);
147 # different settings for tests
151 sellprice_places => 2,
152 sellprice_adjustment => 0,
153 sellprice_adjustment_type => 'percent',
154 article_number_policy => 'update_prices',
155 shoparticle_if_missing => '0',
157 part_classification => 3,
158 default_buchungsgruppe => ($bugru ? $bugru->id : undef),
159 apply_buchungsgruppe => 'all',
162 sellprice_places => 2,
163 sellprice_adjustment => 0,
164 sellprice_adjustment_type => 'percent',
165 article_number_policy => 'update_parts',
166 shoparticle_if_missing => '0',
167 part_type => 'mixed',
168 part_classification => 4,
169 default_buchungsgruppe => ($bugru ? $bugru->id : undef),
170 apply_buchungsgruppe => 'missing',
171 default_unit => 'Stck',
176 # starting test of csv imports
177 # to debug errors in certain tests, run after test_import:
178 # die Dumper($entry->{errors});
183 partnumber;sellprice;lastcost;listprice;unit
184 P1000;100.10;90.20;95.30;kg
186 $entries = test_import($file,$settings1);
187 $entry = $entries->[0];
188 #foreach my $err ( @{ $entry->{errors} } ) {
191 is $entry->{object}->partnumber,'P1000', 'partnumber';
192 is $entry->{object}->sellprice, '100.1', 'sellprice';
193 is $entry->{object}->lastcost, '90.2', 'lastcost';
194 is $entry->{object}->listprice, '95.3', 'listprice';
196 ##### update prices of part
198 partnumber;sellprice;lastcost;listprice;unit
199 P1000;110.10;95.20;97.30;kg
201 $entries = test_import($file,$settings1);
202 $entry = $entries->[0];
203 is $entry->{object}->sellprice, '110.1', 'updated sellprice';
204 is $entry->{object}->lastcost, '95.2', 'updated lastcost';
205 is $entry->{object}->listprice, '97.3', 'updated listprice';
207 ##### insert parts with warehouse,bin name
210 partnumber;description;warehouse;bin;part_type
211 P1000;Teil 1000;Lager1;Ort1_von_Lager1;part
212 P1001;Teil 1001;Lager1;Ort2_von_Lager1;service
213 P1002;Teil 1002;Lager2;Ort1_von_Lager2;service
214 P1003;Teil 1003;Lager2;Ort2_von_Lager2;part
216 $entries = test_import($file,$settings2);
217 $entry = $entries->[0];
218 is $entry->{object}->description, 'Teil 1000', 'Teil 1000 set';
219 is $entry->{object}->warehouse_id, $wh1->id, 'Lager1';
220 is $entry->{object}->bin_id, $bin1_1->id, 'Lagerort1';
221 is $entry->{object}->part_type, 'part', 'Typ ist part';
222 $entry = $entries->[2];
223 is $entry->{object}->description, 'Teil 1002', 'Teil 1002 set';
224 is $entry->{object}->warehouse_id, $wh2->id, 'Lager2';
225 is $entry->{object}->bin_id, $bin2_1->id, 'Lagerort1';
226 is $entry->{object}->part_type, 'service', 'Typ ist service';
228 ##### update warehouse and bin
230 partnumber;description;warehouse;bin;part_type
231 P1000;Teil 1000;Lager2;Ort1_von_Lager2;part
232 P1001;Teil 1001;Lager1;Ort1_von_Lager1;part
233 P1002;Teil 1002;Lager2;Ort1_von_Lager1;part
234 P1003;Teil 1003;Lager2;kein Lagerort;part
236 $entries = test_import($file,$settings2);
237 $entry = $entries->[0];
238 is $entry->{object}->description, 'Teil 1000', 'Teil 1000 set';
239 is $entry->{object}->warehouse_id, $wh2->id, 'Lager2';
240 is $entry->{object}->bin_id, $bin2_1->id, 'Lagerort1';
241 $entry = $entries->[2];
242 my $err1 = @{ $entry->{errors} }[0];
243 #print "'".$err1."'\n";
244 is $entry->{object}->description, 'Teil 1002', 'Teil 1002 set';
245 is $entry->{object}->warehouse_id, $wh2->id, 'Lager2';
246 is $err1, 'Error: Bin Ort1_von_Lager1 is not from warehouse Lager2','kein Lager von Lager2';
247 $entry = $entries->[3];
248 $err1 = @{ $entry->{errors} }[0];
249 #print "'".$err1."'\n";
250 is $entry->{object}->description, 'Teil 1003', 'Teil 1003 set';
251 is $entry->{object}->warehouse_id, $wh2->id, 'Lager2';
252 is $err1, 'Error: Invalid bin name kein Lagerort','kein Lagerort';
254 ##### add translations
256 partnumber;description;description_EN;notes_EN;description_IT;notes_IT
257 P1000;Teil 1000;descr EN 1000;notes EN;descr IT 1000;notes IT
258 P1001;Teil 1001;descr EN 1001;notes EN;descr IT 1001;notes IT
259 P1002;Teil 1002;descr EN 1002;notes EN;descr IT 1002;notes IT
260 P1003;Teil 1003;descr EN 1003;notes EN;descr IT 1003;notes IT
262 $entries = test_import($file,$settings2);
263 $entry = $entries->[0];
264 is $entry->{object}->description, 'Teil 1000', 'Teil 1000 set';
265 is $entry->{raw_data}->{description_EN},'descr EN 1000','EN set';
266 is $entry->{raw_data}->{description_IT},'descr IT 1000','IT set';
267 my $l = @{$entry->{object}->translations}[0];
268 is $l->translation,'descr EN 1000','EN trans set';
269 is $l->longdescription, 'notes EN','EN notes set';
270 $l = @{$entry->{object}->translations}[1];
271 is $l->translation,'descr IT 1000','IT trans set';
272 is $l->longdescription, 'notes IT','IT notes set';
276 partnumber;cvar_mycvar
277 P1000;das ist der Ring
278 P1001;nicht der Nibelungen
282 $entries = test_import($file,$settings2);
283 $entry = $entries->[0];
284 is $entry->{object}->partnumber, 'P1000', 'P1000 set';
285 is $entry->{raw_data}->{cvar_mycvar},'das ist der Ring','CVAR set';
286 is @{$entry->{object}->custom_variables}[0]->text_value,'das ist der Ring','Cvar mit richtigem Wert';
288 # set locale to de so we can match abbreviations
289 $::locale = $old_locale;
290 ##### import part classification
292 partnumber;pclass;description
295 D1002;DV;Dienstleistung 1002
296 D1003;DH;Dienstleistung 1003
298 $entries = test_import($file,$settings2);
299 $entry = $entries->[0];
300 is $entry->{object}->classification_id, '1', 'W1000 von Klasse Einkauf';
301 is $entry->{object}->type, 'part', 'W1000 vom Type part';
302 $entry = $entries->[1];
303 is $entry->{object}->classification_id, '2', 'W1001 von Klasse Verkauf';
304 is $entry->{object}->type, 'part', 'W1001 vom Type part';
305 $entry = $entries->[2];
306 is $entry->{object}->classification_id, '2', 'D1002 von Klasse Verkauf';
307 is $entry->{object}->type, 'service', 'D1002 vom Type service';
308 $entry = $entries->[3];
309 is $entry->{object}->classification_id, '3', 'D1003 von Klasse Handelsware';
310 is $entry->{object}->type, 'service', 'D1003 vom Type service';
313 clear_up(); # remove all data at end of tests
319 SL::DB::Manager::Part ->delete_all(all => 1);
320 SL::DB::Manager::Translation->delete_all(all => 1);
321 SL::DB::Manager::Language ->delete_all(all => 1);
322 SL::DB::Manager::Bin ->delete_all(all => 1);
323 SL::DB::Manager::Warehouse ->delete_all(all => 1);
324 SL::DB::Manager::CustomVariableConfig->delete_all(all => 1);
332 # set emacs to perl mode