]> wagnertech.de Git - mfinanz.git/blob - t/workflow/invoice_to_reclamation.t
date error in mapping
[mfinanz.git] / t / workflow / invoice_to_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     Invoice PurchaseInvoice InvoiceItem
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 = 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     create_reclamation_item(
127       part => $parts[0], qty =>  3, sellprice => 70,
128       reason => $reclamation_reason,
129     ),
130     create_reclamation_item(
131       part => $parts[1], qty => 10, sellprice => 50,
132       reason => $reclamation_reason,
133     ),
134   ],
135 )->load;
136
137 my $purchase_reclamation = 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     create_reclamation_item(
147       part => $parts[0], qty =>  3, sellprice => 70,
148       reason => $reclamation_reason,
149     ),
150     create_reclamation_item(
151       part => $parts[1], qty => 10, sellprice => 50,
152       reason => $reclamation_reason,
153     ),
154   ],
155 )->load;
156
157
158 my $sales_invoice = create_sales_invoice(
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   invoiceitems => [ create_invoice_item(part => $parts[0], qty =>  3, sellprice => 70),
167                     create_invoice_item(part => $parts[1], qty => 10, sellprice => 50),
168   ]
169 )->load;
170
171 my $purchase_invoice = create_minimal_purchase_invoice(
172   save                    => 1,
173   employee                => $employee,
174   invnumber               => "t1",
175   transaction_description => "td4",
176   payment_terms           => $payment_term,
177   delivery_term           => $delivery_term,
178   taxincluded             => 0,
179   invoiceitems => [ create_invoice_item(part => $parts[0], qty =>  3, sellprice => 70),
180                     create_invoice_item(part => $parts[1], qty => 10, sellprice => 50),
181   ]
182 )->load;
183
184 # convert invoice → reclamation
185 my $converted_sales_reclamation =SL::Model::Record->new_from_workflow($sales_invoice, "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_invoice, "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 #get items before strip
195 my @purchase_reclamation_items           = @{$purchase_reclamation->items_sorted};
196 my @sales_reclamation_items              = @{$sales_reclamation->items_sorted};
197 my @converted_purchase_reclamation_items = @{$converted_purchase_reclamation->items_sorted};
198 my @converted_sales_reclamation_items    = @{$converted_sales_reclamation->items_sorted};
199 my @purchase_invoice_items               = @{$purchase_invoice->items_sorted};
200 my @sales_invoice_items                  = @{$sales_invoice->items_sorted};
201
202
203 ### TESTS #####################################################################
204
205 ## created sales und purchase reclamation should be nearly the same
206 my $sales_reclamation_tmp = clone($sales_reclamation);
207 my $purchase_reclamation_tmp = clone($purchase_reclamation);
208 # clean different values
209 foreach (qw(
210   record_type customer_id vendor_id
211   id record_number
212   salesman_id
213   transaction_description
214   itime mtime
215   )) {
216   $sales_reclamation_tmp->$_(undef);
217   $purchase_reclamation_tmp->$_(undef);
218 }
219 pairwise { my $first_tmp = clone($a); my $second_tmp = clone($b);
220   foreach (qw(
221     id reclamation_id
222     itime mtime
223     )) {
224     $first_tmp->$_(undef);
225     $second_tmp->$_(undef);
226   }
227   is_deeply($first_tmp->strip->as_tree, $second_tmp->strip->as_tree);
228 } @purchase_reclamation_items, @sales_reclamation_items;
229 is_deeply($purchase_reclamation_tmp->strip->as_tree, $sales_reclamation_tmp->strip->as_tree);
230
231 ## converted have to be linked to parent
232 my $linked_sales_invoice = $converted_sales_reclamation->linked_records->[0];
233 is_deeply($linked_sales_invoice->strip->as_tree, $sales_invoice->strip->as_tree);
234 my $linked_purchase_invoice = $converted_purchase_reclamation->linked_records->[0];
235 is_deeply($linked_purchase_invoice->strip->as_tree, $purchase_invoice->strip->as_tree);
236
237
238 ## converted should be nearly the same
239 my @different_record_values = qw(
240   id employee_id itime mtime transdate
241   datepaid delivery_customer_id delivery_vendor_id deliverydate direct_debit donumber duedate dunning_config_id gldate invnumber_for_credit_note invoice marge_percent marge_total orddate ordnumber paid qr_reference qr_unstructured_message qrbill_without_amount quodate quonumber storno storno_id type
242   delivered closed exchangerate record_type reqdate vendor_id
243   cp_id contact_id
244   cusordnumber cv_record_number
245   invnumber record_number
246 );
247 my @different_record_item_values = qw(
248   id trans_id reclamation_id itime mtime
249   allocated assemblyitem cusordnumber deliverydate donumber fxsellprice marge_percent marge_price_factor marge_total optional ordnumber subtotal transdate expense_chart_id tax_id inventory_chart_id
250   reason_description_ext reason_description_int reason_id reqdate
251   tax_chart_type
252 );
253 pairwise {
254   test_deeply($a->strip->as_tree, $b->strip->as_tree,
255     "sales_invoice_items to sales_reclamation_items",
256     @different_record_item_values
257   );
258 } @sales_invoice_items, @converted_sales_reclamation_items;
259 test_deeply($sales_invoice->strip->as_tree, $converted_sales_reclamation->strip->as_tree,
260   "sales_invoice to sales_reclamation",
261   @different_record_values
262 );
263
264
265 my @different_record_values2 = qw(
266     id employee_id itime mtime transdate
267     datepaid deliverydate direct_debit duedate gldate invoice orddate ordnumber paid quodate quonumber storno storno_id type is_sepa_blocked
268     billing_address_id customer_id cv_record_number delivered closed exchangerate record_type reqdate salesman_id shippingpoint shipto_id
269     cp_id contact_id
270     invnumber record_number qrbill_data
271 );
272 pairwise {
273   test_deeply($a->strip->as_tree, $b->strip->as_tree,
274     "purchase_invoice_items to purchase_reclamation_items",
275     @different_record_item_values
276   );
277 } @purchase_invoice_items, @converted_purchase_reclamation_items;
278 test_deeply($purchase_invoice->strip->as_tree, $converted_purchase_reclamation->strip->as_tree,
279   "purchase_invoice to purchase_reclamation",
280   @different_record_values2
281 );
282
283 # diag Dumper($sales_invoice->strip->as_tree);
284 # diag Dumper($converted_sales_reclamation->strip->as_tree);
285
286 ####
287 clear_up();
288
289 done_testing;
290
291 1;