10 use Support::TestSetup;
12 use List::MoreUtils qw(pairwise);
14 use SL::DB::Reclamation;
15 use SL::DB::ReclamationReason;
18 use SL::DB::Department;
20 use SL::DB::PaymentTerm;
21 use SL::DB::DeliveryTerm;
25 use SL::Model::Record;
27 use Rose::DB::Object::Helpers qw(clone);
29 use SL::Dev::ALL qw(:ALL);
45 Reclamation ReclamationItem
49 Department PaymentTerm DeliveryTerm
51 "SL::DB::Manager::${_}"->delete_all(all => 1);
53 SL::DB::Manager::Employee->delete_all(where => [ login => 'testuser' ]);
61 $unit = SL::DB::Manager::Unit->find_by(name => 'kg') || die "Can't find unit 'kg'";
63 $employee = SL::DB::Employee->new(
64 'login' => 'testuser',
65 'name' => 'Test User',
68 $department = SL::DB::Department->new(
69 'description' => 'Test Department',
72 $payment_term = create_payment_terms(
73 'description' => '14Tage 2%Skonto, 30Tage netto',
74 'description_long' => "Innerhalb von 14 Tagen abzüglich 2 % Skonto, innerhalb von 30 Tagen rein netto.|Bei einer Zahlung bis zum <%skonto_date%> gewähren wir 2 % Skonto (EUR <%skonto_amount%>) entspricht EUR <%total_wo_skonto%>.Bei einer Zahlung bis zum <%netto_date%> ist der fällige Betrag in Höhe von <%total%> <%currency%> zu überweisen.",
75 'percent_skonto' => '0.02',
80 $delivery_term = SL::DB::DeliveryTerm->new(
81 'description' => 'Test Delivey Term',
82 'description_long' => 'Test Delivey Term Test Delivey Term',
87 push @parts, new_part(
88 partnumber => 'Part_1_KG',
91 push @parts, new_service(
92 partnumber => 'Serv_1',
94 push @parts, new_part(
95 partnumber => 'Part_2',
97 push @parts, new_service(
98 partnumber => 'Serv_2'
101 $reclamation_reason = SL::DB::ReclamationReason->new(
102 name => "test_reason",
108 Support::TestSetup::login();
114 my $sales_reclamation = create_sales_reclamation(
116 employee => $employee,
117 shippingpoint => "sp",
118 transaction_description => "td1",
119 payment => $payment_term,
120 delivery_term => $delivery_term,
122 reclamation_items => [
123 create_reclamation_item(
124 part => $parts[0], qty => 3, sellprice => 70,
125 reason => $reclamation_reason,
127 create_reclamation_item(
128 part => $parts[1], qty => 10, sellprice => 50,
129 reason => $reclamation_reason,
134 my $purchase_reclamation = create_purchase_reclamation(
136 employee => $employee,
137 shippingpoint => "sp",
138 transaction_description => "td2",
139 payment => $payment_term,
140 delivery_term => $delivery_term,
142 reclamation_items => [
143 create_reclamation_item(
144 part => $parts[0], qty => 3, sellprice => 70,
145 reason => $reclamation_reason,
147 create_reclamation_item(
148 part => $parts[1], qty => 10, sellprice => 50,
149 reason => $reclamation_reason,
155 my $new_sales_reclamation = SL::Model::Record->new_from_workflow($sales_reclamation, 'sales_reclamation')->save->load;
156 my $new_purchase_reclamation = SL::Model::Record->new_from_workflow($purchase_reclamation, 'purchase_reclamation')->save->load;
159 my $converted_purchase_reclamation = SL::Model::Record->new_from_workflow($sales_reclamation, 'purchase_reclamation');
160 $converted_purchase_reclamation->vendor_id($purchase_reclamation->{vendor_id});
161 $converted_purchase_reclamation->save->load;
162 my $converted_sales_reclamation = SL::Model::Record->new_from_workflow($purchase_reclamation, 'sales_reclamation');
163 $converted_sales_reclamation->customer_id($sales_reclamation->{customer_id});
164 $converted_sales_reclamation->save->load;
166 #get items before strip
167 my @purchase_reclamation_items = @{$purchase_reclamation->items_sorted};
168 my @sales_reclamation_items = @{$sales_reclamation->items_sorted};
169 my @new_purchase_reclamation_items = @{$new_purchase_reclamation->items_sorted};
170 my @new_sales_reclamation_items = @{$new_sales_reclamation->items_sorted};
171 my @converted_purchase_reclamation_items = @{$converted_purchase_reclamation->items_sorted};
172 my @converted_sales_reclamation_items = @{$converted_sales_reclamation->items_sorted};
175 ### TESTS #####################################################################
177 my @different_record_values = qw(
178 record_type customer_id vendor_id
179 id record_number transaction_description employee_id
180 transdate itime mtime
183 my @different_record_item_values = qw(
188 ## created sales und purchase reclamation should be nearly the same
189 my $sales_tmp = clone($sales_reclamation);
190 my $purchase_tmp = clone($purchase_reclamation);
191 # clean different values
192 foreach (@different_record_values) {
193 $sales_tmp->$_(undef);
194 $purchase_tmp->$_(undef);
197 pairwise { my $first_tmp = clone($a); my $second_tmp = clone($b);
198 foreach (@different_record_item_values) {
199 $first_tmp->$_(undef);
200 $second_tmp->$_(undef);
202 is_deeply($first_tmp->strip->as_tree, $second_tmp->strip->as_tree);
203 } @purchase_reclamation_items, @sales_reclamation_items;
204 is_deeply($purchase_tmp->strip->as_tree, $sales_tmp->strip->as_tree);
207 ## converted have to be linked to parent
209 my $linked_sales_reclamation_n = $new_sales_reclamation->linked_records->[0];
210 my $linked_purchase_reclamation_n = $new_purchase_reclamation->linked_records->[0];
211 is_deeply($linked_sales_reclamation_n->strip->as_tree, $sales_reclamation->load->strip->as_tree);
212 is_deeply($linked_purchase_reclamation_n->strip->as_tree, $purchase_reclamation->load->strip->as_tree);
215 my $linked_sales_reclamation_c = $converted_purchase_reclamation->linked_records->[0];
216 my $linked_purchase_reclamation_c = $converted_sales_reclamation->linked_records->[0];
217 is_deeply($linked_sales_reclamation_c->strip->as_tree, $sales_reclamation->load->strip->as_tree);
218 is_deeply($linked_purchase_reclamation_c->strip->as_tree, $purchase_reclamation->load->strip->as_tree);
221 ## new reclamations should be nearly the same
222 my $new_sales_tmp = clone($new_sales_reclamation);
223 my $sales_tmp2 = clone($sales_reclamation);
224 my $new_purchase_tmp = clone($new_purchase_reclamation);
225 my $purchase_tmp2 = clone($purchase_reclamation);
226 # clean different values
227 foreach (@different_record_values) {
228 $new_sales_tmp->$_(undef);
229 $sales_tmp2->$_(undef);
230 $new_purchase_tmp->$_(undef);
231 $purchase_tmp2->$_(undef);
234 pairwise { my $first_tmp = clone($a); my $second_tmp = clone($b);
235 foreach (@different_record_item_values) {
236 $first_tmp->$_(undef);
237 $second_tmp->$_(undef);
239 is_deeply($first_tmp->strip->as_tree, $second_tmp->strip->as_tree);
240 } @sales_reclamation_items, @new_sales_reclamation_items;
241 is_deeply($sales_tmp2->strip->as_tree, $new_sales_tmp->strip->as_tree);
243 pairwise { my $first_tmp = clone($a); my $second_tmp = clone($b);
244 foreach (@different_record_item_values) {
245 $first_tmp->$_(undef);
246 $second_tmp->$_(undef);
248 is_deeply($first_tmp->strip->as_tree, $second_tmp->strip->as_tree);
249 } @purchase_reclamation_items, @new_purchase_reclamation_items;
250 is_deeply($purchase_tmp2->strip->as_tree, $new_purchase_tmp->strip->as_tree);
253 ## converted reclamation should be nearly the same
254 my $sales_tmp3 = clone($sales_reclamation);
255 my $converted_sales_tmp = clone($converted_sales_reclamation);
256 my $purchase_tmp3 = clone($purchase_reclamation);
257 my $converted_purchase_tmp = clone($converted_purchase_reclamation);
259 my @different_converted_record_values = qw(
262 customer_id vendor_id
271 my @different_converted_record_item_values = qw(
273 sellprice discount lastcost
277 # clean changing values
278 foreach (@different_converted_record_values) {
279 $sales_tmp3->$_(undef);
280 $converted_sales_tmp->$_(undef);
281 $purchase_tmp3->$_(undef);
282 $converted_purchase_tmp->$_(undef);
285 # from sales to purchase
286 pairwise { my $first_tmp = clone($a); my $second_tmp = clone($b);
287 foreach (@different_converted_record_item_values) {
288 $first_tmp->$_(undef);
289 $second_tmp->$_(undef);
291 is_deeply($first_tmp->strip->as_tree, $second_tmp->strip->as_tree);
292 } @sales_reclamation_items, @converted_purchase_reclamation_items;
293 is_deeply($sales_tmp3->strip->as_tree, $converted_purchase_tmp->strip->as_tree);
296 # from purchase to sales
297 pairwise { my $first_tmp = clone($a); my $second_tmp = clone($b);
298 foreach (@different_converted_record_item_values) {
299 $first_tmp->$_(undef);
300 $second_tmp->$_(undef);
302 is_deeply($first_tmp->strip->as_tree, $second_tmp->strip->as_tree);
303 } @purchase_reclamation_items, @converted_sales_reclamation_items;
304 is_deeply($purchase_tmp3->strip->as_tree, $converted_sales_tmp->strip->as_tree);
306 #diag Dumper($first->strip->as_tree);
307 #diag Dumper($second->strip->as_tree);
316 # set emacs to perl mode