10 use Support::TestSetup;
11 use Support::TestRoutines qw(test_deeply);
13 use List::MoreUtils qw(pairwise);
16 use SL::DB::Reclamation;
17 use SL::DB::ReclamationReason;
20 use SL::DB::Department;
22 use SL::DB::PaymentTerm;
23 use SL::DB::DeliveryTerm;
27 use SL::Model::Record;
29 use Rose::DB::Object::Helpers qw(clone);
31 use SL::Dev::ALL qw(:ALL);
47 Invoice PurchaseInvoice InvoiceItem
48 Reclamation ReclamationItem
52 Department PaymentTerm DeliveryTerm
54 "SL::DB::Manager::${_}"->delete_all(all => 1);
56 SL::DB::Manager::Employee->delete_all(where => [ login => 'testuser' ]);
64 $unit = SL::DB::Manager::Unit->find_by(name => 'kg') || die "Can't find unit 'kg'";
66 $employee = SL::DB::Employee->new(
67 'login' => 'testuser',
68 'name' => 'Test User',
71 $department = SL::DB::Department->new(
72 'description' => 'Test Department',
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',
83 $delivery_term = SL::DB::DeliveryTerm->new(
84 'description' => 'Test Delivey Term',
85 'description_long' => 'Test Delivey Term Test Delivey Term',
90 push @parts, new_part(
91 partnumber => 'Part_1_KG',
94 push @parts, new_service(
95 partnumber => 'Serv_1',
97 push @parts, new_part(
98 partnumber => 'Part_2',
100 push @parts, new_service(
101 partnumber => 'Serv_2'
104 $reclamation_reason = SL::DB::ReclamationReason->new(
105 name => "test_reason",
111 Support::TestSetup::login();
117 my $sales_reclamation = create_sales_reclamation(
119 employee => $employee,
120 shippingpoint => "sp",
121 transaction_description => "td1",
122 payment => $payment_term,
123 delivery_term => $delivery_term,
125 reclamation_items => [
126 create_reclamation_item(
127 part => $parts[0], qty => 3, sellprice => 70,
128 reason => $reclamation_reason,
130 create_reclamation_item(
131 part => $parts[1], qty => 10, sellprice => 50,
132 reason => $reclamation_reason,
137 my $purchase_reclamation = create_purchase_reclamation(
139 employee => $employee,
140 shippingpoint => "sp",
141 transaction_description => "td2",
142 payment => $payment_term,
143 delivery_term => $delivery_term,
145 reclamation_items => [
146 create_reclamation_item(
147 part => $parts[0], qty => 3, sellprice => 70,
148 reason => $reclamation_reason,
150 create_reclamation_item(
151 part => $parts[1], qty => 10, sellprice => 50,
152 reason => $reclamation_reason,
158 my $sales_invoice = create_sales_invoice(
160 employee => $employee,
161 shippingpoint => "sp",
162 transaction_description => "td3",
163 payment_terms => $payment_term,
164 delivery_term => $delivery_term,
166 invoiceitems => [ create_invoice_item(part => $parts[0], qty => 3, sellprice => 70),
167 create_invoice_item(part => $parts[1], qty => 10, sellprice => 50),
171 my $purchase_invoice = create_minimal_purchase_invoice(
173 employee => $employee,
175 transaction_description => "td4",
176 payment_terms => $payment_term,
177 delivery_term => $delivery_term,
179 invoiceitems => [ create_invoice_item(part => $parts[0], qty => 3, sellprice => 70),
180 create_invoice_item(part => $parts[1], qty => 10, sellprice => 50),
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;
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};
203 ### TESTS #####################################################################
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
210 record_type customer_id vendor_id
213 transaction_description
216 $sales_reclamation_tmp->$_(undef);
217 $purchase_reclamation_tmp->$_(undef);
219 pairwise { my $first_tmp = clone($a); my $second_tmp = clone($b);
224 $first_tmp->$_(undef);
225 $second_tmp->$_(undef);
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);
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);
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
244 cusordnumber cv_record_number
245 invnumber record_number
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
254 test_deeply($a->strip->as_tree, $b->strip->as_tree,
255 "sales_invoice_items to sales_reclamation_items",
256 @different_record_item_values
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
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
270 invnumber record_number qrbill_data
273 test_deeply($a->strip->as_tree, $b->strip->as_tree,
274 "purchase_invoice_items to purchase_reclamation_items",
275 @different_record_item_values
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
283 # diag Dumper($sales_invoice->strip->as_tree);
284 # diag Dumper($converted_sales_reclamation->strip->as_tree);