1 use Test::More tests => 47;
 
   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;
 
  24 use SL::DB::Pricegroup;
 
  28 my ($translation, $bin1_1, $bin1_2, $bin2_1, $bin2_2, $wh1, $wh2, $bugru, $cvarconfig );
 
  29 my ($pg1_id, $pg2_id, $pg3_id);
 
  31 Support::TestSetup::login();
 
  38   $translation     = SL::DB::Language->new(
 
  39     description    => 'Englisch',
 
  41     template_code  => 'EN',
 
  43   $translation     = SL::DB::Language->new(
 
  44     description    => 'Italienisch',
 
  46     template_code  => 'IT',
 
  48   $wh1 = SL::DB::Warehouse->new(
 
  49     description    => 'Lager1',
 
  52   $bin1_1 = SL::DB::Bin->new(
 
  53     description    => 'Ort1_von_Lager1',
 
  54     warehouse_id   => $wh1->id,
 
  56   $bin1_2 = SL::DB::Bin->new(
 
  57     description    => 'Ort2_von_Lager1',
 
  58     warehouse_id   => $wh1->id,
 
  60   $wh2 = SL::DB::Warehouse->new(
 
  61     description    => 'Lager2',
 
  64   $bin2_1 = SL::DB::Bin->new(
 
  65     description    => 'Ort1_von_Lager2',
 
  66     warehouse_id   => $wh2->id,
 
  68   $bin2_2 = SL::DB::Bin->new(
 
  69     description    => 'Ort2_von_Lager2',
 
  70     warehouse_id   => $wh2->id,
 
  73   $cvarconfig = SL::DB::CustomVariableConfig->new(
 
  77     description => 'mein Schatz',
 
  81     included_by_default => 0,
 
  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;
 
  92 $bugru = SL::DB::Manager::Buchungsgruppe->find_by(description => { like => 'Standard%19%' });
 
  98   my ($file,$settings) = @_;
 
 100   my $controller = SL::Controller::CsvImport->new(
 
 103   $controller->load_default_profile;
 
 104   $controller->profile->set(
 
 108     numberformat => $::myconfig{numberformat},
 
 111   my $csv_part_import = SL::Controller::CsvImport::Part->new(
 
 112     settings   => $settings,
 
 113     controller => $controller,
 
 116   #print "profile param type=".$csv_part_import->settings->{parts_type}."\n";
 
 118   $csv_part_import->run(test => 0);
 
 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}};
 
 123   return $csv_part_import->controller->data;
 
 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');
 
 132 my ($entries, $entry, $file);
 
 134 # different settings for tests
 
 138                        sellprice_places          => 2,
 
 139                        sellprice_adjustment      => 0,
 
 140                        sellprice_adjustment_type => 'percent',
 
 141                        article_number_policy     => 'update_prices',
 
 142                        shoparticle_if_missing    => '0',
 
 144                        part_classification       => 3,
 
 145                        default_buchungsgruppe    => ($bugru ? $bugru->id : undef),
 
 146                        apply_buchungsgruppe      => 'all',
 
 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',
 
 163 # starting test of csv imports
 
 164 # to debug errors in certain tests, run after test_import:
 
 165 #   die Dumper($entry->{errors});
 
 168 ##### create part with prices and 3 pricegroup prices
 
 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
 
 173 $entries = test_import($file,$settings1);
 
 174 $entry = $entries->[0];
 
 175 #foreach my $err ( @{ $entry->{errors} } ) {
 
 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';
 
 184 ##### update prices of part, and price of pricegroup_2, keeping pricegroup_1 and pricegroup_3
 
 186 partnumber;sellprice;lastcost;listprice;unit;pricegroup_2;pricegroup_4
 
 187 P1000;110.10;95.20;97.30;kg;123.45;144.44
 
 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';
 
 201 ##### insert parts with warehouse,bin name
 
 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
 
 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';
 
 222 ##### update warehouse and bin
 
 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
 
 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';
 
 248 ##### add translations
 
 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
 
 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';
 
 270 partnumber;cvar_mycvar
 
 271 P1000;das ist der Ring
 
 272 P1001;nicht der Nibelungen
 
 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';
 
 282 # set locale to de so we can match abbreviations
 
 283 $::locale = $old_locale;
 
 284 ##### import part classification
 
 286 partnumber;pclass;description
 
 289 D1002;DV;Dienstleistung 1002
 
 290 D1003;DH;Dienstleistung 1003
 
 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';
 
 307 clear_up(); # remove all data at end of tests
 
 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);
 
 328 # set emacs to perl mode