]> wagnertech.de Git - mfinanz.git/blob - t/workflow/order_reclamation.t
date error in mapping
[mfinanz.git] / t / workflow / order_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 Support::TestRoutines qw(test_deeply);
12 use Test::Exception;
13 use List::MoreUtils qw(pairwise);
14
15 use SL::DB::Order;
16 use SL::DB::Reclamation;
17 use SL::DB::ReclamationReason;
18 use SL::DB::Customer;
19 use SL::DB::Vendor;
20 use SL::DB::Department;
21 use SL::DB::Currency;
22 use SL::DB::PaymentTerm;
23 use SL::DB::DeliveryTerm;
24 use SL::DB::Employee;
25 use SL::DB::Part;
26 use SL::DB::Unit;
27 use SL::Model::Record;
28
29 use Rose::DB::Object::Helpers qw(clone);
30
31 use SL::Dev::ALL qw(:ALL);
32
33 my (
34   $customer, $vendor,
35   $employee,
36   $payment_term,
37   $delivery_term,
38   $unit,
39   @parts,
40   $department,
41   $reclamation_reason,
42 );
43
44
45 sub clear_up {
46   foreach (qw(
47     Order OrderItem
48     Reclamation ReclamationItem
49     ReclamationReason
50     Part
51     Customer Vendor
52     Department PaymentTerm DeliveryTerm
53     )) {
54     "SL::DB::Manager::${_}"->delete_all(all => 1);
55   }
56   SL::DB::Manager::Employee->delete_all(where => [ login => 'testuser' ]);
57 };
58
59 sub reset_state {
60   my %params = @_;
61
62   clear_up();
63
64   $unit     = SL::DB::Manager::Unit->find_by(name => 'kg') || die "Can't find unit 'kg'";
65
66   $employee = SL::DB::Employee->new(
67     'login' => 'testuser',
68     'name'  => 'Test User',
69   )->save;
70
71   $department = SL::DB::Department->new(
72     'description' => 'Test Department',
73   )->save;
74
75   $payment_term = create_payment_terms(
76      'description'      => '14Tage 2%Skonto, 30Tage netto',
77      '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.",
78      'percent_skonto'   => '0.02',
79      'terms_netto'      => 30,
80      'terms_skonto'     => 14
81   );
82
83   $delivery_term = SL::DB::DeliveryTerm->new(
84     'description'      => 'Test Delivey Term',
85     'description_long' => 'Test Delivey Term Test Delivey Term',
86   )->save;
87
88   # some parts/services
89   @parts = ();
90   push @parts, new_part(
91     partnumber => 'Part_1_KG',
92     unit        => $unit->name,
93   )->save;
94   push @parts, new_service(
95     partnumber => 'Serv_1',
96   )->save;
97   push @parts, new_part(
98     partnumber => 'Part_2',
99   )->save;
100   push @parts, new_service(
101     partnumber => 'Serv_2'
102   )->save;
103
104   $reclamation_reason = SL::DB::ReclamationReason->new(
105     name => "test_reason",
106     description => "",
107     position => 1,
108   );
109 }
110
111 Support::TestSetup::login();
112
113 reset_state();
114
115 #####
116
117 my $sales_reclamation = SL::Dev::Record::create_sales_reclamation(
118   save                    => 1,
119   employee                => $employee,
120   shippingpoint           => "sp",
121   transaction_description => "td1",
122   payment                 => $payment_term,
123   delivery_term           => $delivery_term,
124   taxincluded             => 0,
125   reclamation_items       => [
126     SL::Dev::Record::create_reclamation_item(
127       part => $parts[0], qty =>  3, sellprice => 70,
128       reason => $reclamation_reason,
129     ),
130     SL::Dev::Record::create_reclamation_item(
131       part => $parts[1], qty => 10, sellprice => 50,
132       reason => $reclamation_reason,
133     ),
134   ],
135 )->load;
136
137 my $purchase_reclamation = SL::Dev::Record::create_purchase_reclamation(
138   save                    => 1,
139   employee                => $employee,
140   shippingpoint           => "sp",
141   transaction_description => "td2",
142   payment                 => $payment_term,
143   delivery_term           => $delivery_term,
144   taxincluded             => 0,
145   reclamation_items       => [
146     SL::Dev::Record::create_reclamation_item(
147       part => $parts[0], qty =>  3, sellprice => 70,
148       reason => $reclamation_reason,
149     ),
150     SL::Dev::Record::create_reclamation_item(
151       part => $parts[1], qty => 10, sellprice => 50,
152       reason => $reclamation_reason,
153     ),
154   ],
155 )->load;
156
157
158 my $sales_order = SL::Dev::Record::create_sales_order(
159   save                    => 1,
160   employee                => $employee,
161   shippingpoint           => "sp",
162   transaction_description => "td3",
163   payment_terms           => $payment_term,
164   delivery_term           => $delivery_term,
165   taxincluded             => 0,
166   orderitems => [ SL::Dev::Record::create_order_item(part => $parts[0], qty =>  3, sellprice => 70),
167                   SL::Dev::Record::create_order_item(part => $parts[1], qty => 10, sellprice => 50),
168   ]
169 )->load;
170
171 my $purchase_order = SL::Dev::Record::create_purchase_order(
172   save                    => 1,
173   employee                => $employee,
174   shippingpoint           => "sp",
175   transaction_description => "td4",
176   payment_terms           => $payment_term,
177   delivery_term           => $delivery_term,
178   taxincluded             => 0,
179   orderitems => [ SL::Dev::Record::create_order_item(part => $parts[0], qty =>  3, sellprice => 70),
180                   SL::Dev::Record::create_order_item(part => $parts[1], qty => 10, sellprice => 50),
181   ]
182 )->load;
183
184 # convert order → reclamation
185 my $converted_sales_reclamation = SL::Model::Record->new_from_workflow($sales_order, "sales_reclamation");
186 $converted_sales_reclamation->items_sorted->[0]->reason($reclamation_reason);
187 $converted_sales_reclamation->items_sorted->[1]->reason($reclamation_reason);
188 $converted_sales_reclamation->save->load;
189 my $converted_purchase_reclamation = SL::Model::Record->new_from_workflow($purchase_order, "purchase_reclamation");
190 $converted_purchase_reclamation->items_sorted->[0]->reason($reclamation_reason);
191 $converted_purchase_reclamation->items_sorted->[1]->reason($reclamation_reason);
192 $converted_purchase_reclamation->save->load;
193
194 # convert reclamation → order
195 my $converted_sales_order = SL::Model::Record->new_from_workflow($sales_reclamation, 'sales_order')->save->load;
196 my $converted_purchase_order = SL::Model::Record->new_from_workflow($purchase_reclamation, 'purchase_order')->save->load;
197
198
199 #get items before strip
200 my @purchase_reclamation_items           = @{$purchase_reclamation->items_sorted};
201 my @sales_reclamation_items              = @{$sales_reclamation->items_sorted};
202 my @converted_purchase_reclamation_items = @{$converted_purchase_reclamation->items_sorted};
203 my @converted_sales_reclamation_items    = @{$converted_sales_reclamation->items_sorted};
204 my @purchase_order_items                 = @{$purchase_order->items_sorted};
205 my @sales_order_items                    = @{$sales_order->items_sorted};
206 my @converted_purchase_order_items       = @{$converted_purchase_order->items_sorted};
207 my @converted_sales_order_items          = @{$converted_sales_order->items_sorted};
208
209
210 ### TESTS #####################################################################
211
212 ## created sales und purchase reclamation should be nearly the same
213 my $sales_reclamation_tmp = clone($sales_reclamation);
214 my $purchase_reclamation_tmp = clone($purchase_reclamation);
215 # clean different values
216 foreach (qw(
217   record_type customer_id vendor_id
218   id record_number
219   salesman_id
220   transaction_description
221   itime mtime
222   )) {
223   $sales_reclamation_tmp->$_(undef);
224   $purchase_reclamation_tmp->$_(undef);
225 }
226
227 pairwise { my $first_tmp = clone($a); my $second_tmp = clone($b);
228   foreach (qw(
229     id reclamation_id
230     itime mtime
231     )) {
232     $first_tmp->$_(undef);
233     $second_tmp->$_(undef);
234   }
235   is_deeply($first_tmp->strip->as_tree, $second_tmp->strip->as_tree);
236 } @purchase_reclamation_items, @sales_reclamation_items;
237 is_deeply($purchase_reclamation_tmp->strip->as_tree, $sales_reclamation_tmp->strip->as_tree);
238
239 ## created sales und purchase order should be nearly the same
240 my $sales_order_tmp = clone($sales_order);
241 my $purchase_order_tmp = clone($purchase_order);
242 # clean different values
243 foreach (qw(
244   record_type customer_id vendor_id
245   id
246   ordnumber salesman_id
247   transaction_description
248   itime mtime
249   )) {
250   $sales_order_tmp->$_(undef);
251   $purchase_order_tmp->$_(undef);
252 }
253 pairwise { my $first_tmp = clone($a); my $second_tmp = clone($b);
254   foreach (qw(
255     id trans_id
256     itime mtime
257     )) {
258     $first_tmp->$_(undef);
259     $second_tmp->$_(undef);
260   }
261   is_deeply($first_tmp->strip->as_tree, $second_tmp->strip->as_tree);
262 } @purchase_order_items, @sales_order_items;
263 is_deeply($purchase_order_tmp->strip->as_tree, $sales_order_tmp->strip->as_tree);
264
265
266 ## converted have to be linked to parent
267 # sales
268 my $linked_sales_order = $converted_sales_reclamation->linked_records->[0];
269 my $linked_sales_reclamation = $converted_sales_order->linked_records->[0];
270 is_deeply($linked_sales_order->strip->as_tree, $sales_order->strip->as_tree);
271 is_deeply($linked_sales_reclamation->strip->as_tree, $sales_reclamation->load->strip->as_tree);
272
273 # purchase
274 my $linked_purchase_order = $converted_purchase_reclamation->linked_records->[0];
275 my $linked_purchase_reclamation = $converted_purchase_order->linked_records->[0];
276 is_deeply($linked_purchase_order->strip->as_tree, $purchase_order->strip->as_tree);
277 is_deeply($linked_purchase_reclamation->strip->as_tree, $purchase_reclamation->load->strip->as_tree);
278
279
280 ## converted should be nealy the same
281 my @different_record_values = qw(
282   id record_type employee_id itime mtime reqdate transdate
283   delivery_customer_id delivery_vendor_id expected_billing_date marge_percent marge_total order_probability order_status_id proforma quonumber quotation
284   cp_id contact_id
285   cusordnumber cv_record_number
286   ordnumber record_number
287   intake
288   vendor_confirmation_number
289 );
290 my @different_record_item_values = qw(
291   id trans_id reclamation_id itime mtime
292   cusordnumber marge_percent marge_price_factor marge_total optional ordnumber ship subtotal transdate orderer_id
293   reason_description_ext reason_description_int reason_id
294   recurring_billing_mode recurring_billing_invoice_id
295 );
296 # sales
297 pairwise {
298   test_deeply( $a->strip->as_tree, $b->strip->as_tree,
299     "sales_order_items to sales_reclamation_items",
300     @different_record_item_values
301   );
302 } @sales_order_items, @converted_sales_reclamation_items;
303 test_deeply( $sales_order->strip->as_tree, $converted_sales_reclamation->strip->as_tree,
304   "sales_order to sales_reclamation",
305   @different_record_values
306 );
307
308 pairwise {
309   test_deeply( $a->strip->as_tree, $b->strip->as_tree,
310     "sales_reclamation_items to sales_order_items",
311     @different_record_item_values
312   );
313 } @sales_reclamation_items, @converted_sales_order_items;
314 test_deeply($sales_reclamation->strip->as_tree, $converted_sales_order->strip->as_tree,
315   "sales_reclamation to sales_order",
316   @different_record_values
317 );
318
319 # purchase
320 pairwise {
321   test_deeply($a->strip->as_tree, $b->strip->as_tree,
322     "purchase_order_items to purchase_reclamation_items",
323     @different_record_item_values
324   );
325 } @purchase_order_items, @converted_purchase_reclamation_items;
326 test_deeply($purchase_order->strip->as_tree, $converted_purchase_reclamation->strip->as_tree,
327   "purchase_order to purchase_reclamation",
328   @different_record_values
329 );
330
331 pairwise {
332   test_deeply($a->strip->as_tree, $b->strip->as_tree,
333     "purchase_reclamation_items to purchase_order_items",
334     @different_record_item_values
335   );
336 } @purchase_reclamation_items, @converted_purchase_order_items;
337 test_deeply($purchase_reclamation->strip->as_tree, $converted_purchase_order->strip->as_tree,
338   "purchase_reclamation to purchase_order",
339   @different_record_values
340 );
341
342 # diag Dumper($sales_order->strip->as_tree);
343 # diag Dumper($converted_sales_reclamation->strip->as_tree);
344
345 ####
346 clear_up();
347
348 done_testing;
349
350 1;