]> wagnertech.de Git - mfinanz.git/blob - t/workflow/reclamation_reclamation.t
date error in mapping
[mfinanz.git] / t / workflow / reclamation_reclamation.t
1 use Test::More;
2
3 use strict;
4
5 use lib 't';
6 use utf8;
7
8 use Carp;
9 use Data::Dumper;
10 use Support::TestSetup;
11 use Test::Exception;
12 use List::MoreUtils qw(pairwise);
13
14 use SL::DB::Reclamation;
15 use SL::DB::ReclamationReason;
16 use SL::DB::Customer;
17 use SL::DB::Vendor;
18 use SL::DB::Department;
19 use SL::DB::Currency;
20 use SL::DB::PaymentTerm;
21 use SL::DB::DeliveryTerm;
22 use SL::DB::Employee;
23 use SL::DB::Part;
24 use SL::DB::Unit;
25 use SL::Model::Record;
26
27 use Rose::DB::Object::Helpers qw(clone);
28
29 use SL::Dev::ALL qw(:ALL);
30
31 my (
32   $customer, $vendor,
33   $employee,
34   $payment_term,
35   $delivery_term,
36   $unit,
37   @parts,
38   $department,
39   $reclamation_reason,
40 );
41
42
43 sub clear_up {
44   foreach (qw(
45     Reclamation ReclamationItem
46     ReclamationReason
47     Part
48     Customer Vendor
49     Department PaymentTerm DeliveryTerm
50     )) {
51     "SL::DB::Manager::${_}"->delete_all(all => 1);
52   }
53   SL::DB::Manager::Employee->delete_all(where => [ login => 'testuser' ]);
54 };
55
56 sub reset_state {
57   my %params = @_;
58
59   clear_up();
60
61   $unit     = SL::DB::Manager::Unit->find_by(name => 'kg') || die "Can't find unit 'kg'";
62
63   $employee = SL::DB::Employee->new(
64     'login' => 'testuser',
65     'name'  => 'Test User',
66   )->save;
67
68   $department = SL::DB::Department->new(
69     'description' => 'Test Department',
70   )->save;
71
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',
76     'terms_netto'      => 30,
77     'terms_skonto'     => 14
78   );
79
80   $delivery_term = SL::DB::DeliveryTerm->new(
81     'description'      => 'Test Delivey Term',
82     'description_long' => 'Test Delivey Term Test Delivey Term',
83   )->save;
84
85   # some parts/services
86   @parts = ();
87   push @parts, new_part(
88     partnumber => 'Part_1_KG',
89     unit        => $unit->name,
90   )->save;
91   push @parts, new_service(
92     partnumber => 'Serv_1',
93   )->save;
94   push @parts, new_part(
95     partnumber => 'Part_2',
96   )->save;
97   push @parts, new_service(
98     partnumber => 'Serv_2'
99   )->save;
100
101   $reclamation_reason = SL::DB::ReclamationReason->new(
102     name => "test_reason",
103     description => "",
104     position => 1,
105   );
106 }
107
108 Support::TestSetup::login();
109
110 reset_state();
111
112 #####
113
114 my $sales_reclamation = create_sales_reclamation(
115   save                    => 1,
116   employee                => $employee,
117   shippingpoint           => "sp",
118   transaction_description => "td1",
119   payment                 => $payment_term,
120   delivery_term           => $delivery_term,
121   taxincluded             => 0,
122   reclamation_items       => [
123     create_reclamation_item(
124       part => $parts[0], qty =>  3, sellprice => 70,
125       reason => $reclamation_reason,
126     ),
127     create_reclamation_item(
128       part => $parts[1], qty => 10, sellprice => 50,
129       reason => $reclamation_reason,
130     ),
131   ],
132 )->load;
133
134 my $purchase_reclamation = create_purchase_reclamation(
135   save                    => 1,
136   employee                => $employee,
137   shippingpoint           => "sp",
138   transaction_description => "td2",
139   payment                 => $payment_term,
140   delivery_term           => $delivery_term,
141   taxincluded             => 0,
142   reclamation_items       => [
143     create_reclamation_item(
144       part => $parts[0], qty =>  3, sellprice => 70,
145       reason => $reclamation_reason,
146     ),
147     create_reclamation_item(
148       part => $parts[1], qty => 10, sellprice => 50,
149       reason => $reclamation_reason,
150     ),
151   ],
152 )->load;
153
154 # new
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;
157
158 # convert
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;
165
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};
173
174
175 ### TESTS #####################################################################
176
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
181 );
182
183 my @different_record_item_values = qw(
184   id reclamation_id
185   itime mtime
186 );
187
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);
195 }
196
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);
201   }
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);
205
206
207 ## converted have to be linked to parent
208 # new
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);
213
214 # converted
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);
219
220
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);
232 }
233
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);
238   }
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);
242
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);
247   }
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);
251
252
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);
258
259 my @different_converted_record_values = qw(
260   transdate
261   record_type
262   customer_id vendor_id
263   id record_number
264   employee_id reqdate
265   itime mtime
266
267   delivery_term_id
268   payment_id
269 );
270
271 my @different_converted_record_item_values = qw(
272   id reclamation_id
273   sellprice discount lastcost
274   itime mtime
275 );
276
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);
283 }
284
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);
290   }
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);
294
295
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);
301   }
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);
305
306 #diag Dumper($first->strip->as_tree);
307 #diag Dumper($second->strip->as_tree);
308
309 ####
310 clear_up();
311
312 done_testing;
313
314 1;
315
316 # set emacs to perl mode
317 # Local Variables:
318 # mode: perl
319 # End: