3 use Data::Dumper; # maybe in Tests available?
4 use Test::Deep qw(cmp_deeply superhashof ignore);
10 use SL::Dev::Part qw(new_part new_assembly new_service);
11 use SL::Dev::Inventory qw(create_warehouse_and_bins set_stock);
13 use_ok 'Support::TestSetup';
14 use_ok 'SL::Controller::CsvImport';
16 use_ok 'SL::DB::Part';
17 use_ok 'SL::DB::Warehouse';
18 use_ok 'SL::DB::Inventory';
20 use_ok 'SL::Helper::Inventory';
22 Support::TestSetup::login();
24 my ($wh, $bin1, $bin2, $assembly1, $assembly_service, $part1, $part2, $wh_moon, $bin_moon, $service1);
30 create_standard_stock();
39 my ($file,$settings) = @_;
41 my $controller = SL::Controller::CsvImport->new(
44 $controller->load_default_profile;
45 $controller->profile->set(
49 numberformat => $::myconfig{numberformat},
51 my $csv_inventory_import = SL::Controller::CsvImport::Inventory->new(
52 settings => $settings,
53 controller => $controller,
56 #print "profile param type=".$csv_part_import->settings->{parts_type}."\n";
58 $csv_inventory_import->run(test => 0);
60 # don't try and save objects that have errors
61 $csv_inventory_import->save_objects unless scalar @{$csv_inventory_import->controller->data->[0]->{errors}};
63 return $csv_inventory_import->controller->data;
66 $::myconfig{numberformat} = '1000.00';
67 my $old_locale = $::locale;
68 # set locale to en so we can match errors
69 $::locale = Locale->new('en');
72 my ($entries, $entry, $file);
74 # different settings for tests
78 apply_comment => 'missing',
79 comment => 'Lager Inventur Standard',
83 # starting test of csv imports
84 # to debug errors in certain tests, run after test_import:
85 # die Dumper($entry->{errors});
88 ##### create complete bullshit
90 bin,chargenumber,comment,employee_id,partnumber,qty,shippingdate,target_qty,warehouse
91 P1000;100.10;90.20;95.30;kg;111.11;122.22;133.33
93 $entries = test_import($file, $settings1);
94 $entry = $entries->[0];
95 is scalar @{ $entry->{errors} }, 3, "Three errors occurred";
97 cmp_deeply(\@{ $entry->{errors} }, [
98 'Error: Warehouse not found',
99 'Error: Bin not found',
100 'Error: Part not found'
102 "Errors for bullshit import are ok"
105 ##### create minor bullshit
107 warehouse,bin,partnumber,qty,chargenumber,comment,employee_id,qty,shippingdate,target_qty
108 Warehouse,"Bin 1","ap 1",3.4
111 $entries = test_import($file, $settings1);
112 $entry = $entries->[0];
113 is scalar @{ $entry->{errors} }, 1, "One error for minor bullshit occurred";
115 cmp_deeply(\@{ $entry->{errors} }, [
116 'Error: A quantity and a target quantity could not be given both.'
118 "Error for minor bullshit import are ok"
122 ##### add some qty on earth, but we have something already stocked
129 is(SL::Helper::Inventory::get_stock(part => $part1), "25.00000", 'simple get_stock works');
130 is(SL::Helper::Inventory::get_onhand(part => $part1), "25.00000", 'simple get_onhand works');
133 warehouse,bin,partnumber,qty,chargenumber,comment,employee_id,shippingdate
134 Warehouse,"Bin 1","ap 1",3.4
136 $entries = test_import($file, $settings1);
137 $entry = $entries->[0];
138 diag Dumper($entry->{object}->trans_id);
139 is scalar @{ $entry->{errors} }, 0, "No error for valid data occurred";
140 is $entry->{object}->qty, "3.4", "Valid qty accepted"; # evals to text
141 is(SL::Helper::Inventory::get_stock(part => $part1), "28.40000", 'simple add (stock) qty works');
142 is(SL::Helper::Inventory::get_onhand(part => $part1), "28.40000", 'simple add (onhand) qty works');
144 # now check the real Inventory entry
145 my $trans_id = $entry->{object}->trans_id;
146 my $inv_obj = SL::DB::Manager::Inventory->find_by(trans_id => $trans_id);
148 # we expect one entry for one trans_id
149 is ref $inv_obj, "SL::DB::Inventory", "One inventory object, no array or undef";
150 is $inv_obj->qty == 3.4, 1, "Valid qty accepted"; # evals to text
151 is $inv_obj->comment, 'Lager Inventur Standard', "Valid comment accepted"; # evals to text
152 is $inv_obj->employee_id, 1, "Employee valid"; # evals to text
153 is ref $inv_obj->shippingdate, 'DateTime', "Valid DateTime for shippingdate";
154 is $inv_obj->shippingdate, DateTime->today_local, "Default shippingdate set";
156 my $tt = SL::DB::Manager::TransferType->find_by(id => $inv_obj->trans_type_id);
158 is ref $tt, 'SL::DB::TransferType', "Valid TransferType, no undef";
159 is $tt->direction, 'in', "Transfer direction correct";
160 is $tt->description, 'correction', "Transfer description correct";
162 clear_up(); # remove all data at end of tests
169 SL::DB::Manager::Inventory->delete_all(all => 1);
170 SL::DB::Manager::Assembly->delete_all(all => 1);
171 SL::DB::Manager::Part->delete_all(all => 1);
172 SL::DB::Manager::Bin->delete_all(all => 1);
173 SL::DB::Manager::Warehouse->delete_all(all => 1);
176 sub create_standard_stock {
177 ($wh, $bin1) = create_warehouse_and_bins();
178 ($wh_moon, $bin_moon) = create_warehouse_and_bins(
179 warehouse_description => 'Our warehouse location at the moon',
180 bin_description => 'Lunar crater',
182 $bin2 = SL::DB::Bin->new(description => "Bin 2", warehouse => $wh)->save;
185 $assembly1 = new_assembly(number_of_parts => 2)->save;
186 ($part1, $part2) = map { $_->part } $assembly1->assemblies;
188 $service1 = new_service(partnumber => "service number 1",
189 description => "We really need this service",
192 push( @{$assembly_items}, SL::DB::Assembly->new(parts_id => $part1->id,
196 push( @{$assembly_items}, SL::DB::Assembly->new(parts_id => $part2->id,
200 push( @{$assembly_items}, SL::DB::Assembly->new(parts_id => $service1->id,
204 $assembly_service = new_assembly(description => 'Ein Erzeugnis mit Dienstleistungen',
205 assembly_items => $assembly_items
216 # set emacs to perl mode