1 use Test::More tests => 75;
8 use Support::TestSetup;
12 use Support::TestSetup;
15 use SL::DB::Buchungsgruppe;
21 use SL::DB::DeliveryOrder;
25 use SL::Dev::ALL qw(:ALL);
27 my ($customer, $employee, $payment_do, $unit, @parts, $department);
30 my $VISUAL_TEST = 0; # just a sleep to click around
33 foreach (qw(DeliveryOrderItem DeliveryOrder OrderItem Order InvoiceItem Invoice Part Customer Department PaymentTerm)) {
34 "SL::DB::Manager::${_}"->delete_all(all => 1);
36 SL::DB::Manager::Employee->delete_all(where => [ login => 'testuser' ]);
44 $transdate = DateTime->today_local;
45 $transdate->set_year(2019) if $transdate->year == 2020; # use year 2019 in 2020, because of tax rate change in Germany
47 $unit = SL::DB::Manager::Unit->find_by(name => 'kg') || die "Can't find unit 'kg'";
48 $customer = new_customer()->save;
50 $employee = SL::DB::Employee->new(
51 'login' => 'testuser',
52 'name' => 'Test User',
55 $department = SL::DB::Department->new(
56 'description' => 'Test Department',
59 $payment_do = create_payment_terms(
60 'description' => '14Tage 2%Skonto, 30Tage netto',
61 '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.",
62 'percent_skonto' => '0.02',
69 push @parts, new_part(
70 description => "description 1",
71 lastcost => '49.95000',
72 listprice => '0.00000',
73 partnumber => 'v-519160549',
74 sellprice => '242.20000',
79 push @parts, new_part(
80 description => "description 2",
81 lastcost => '153.00000',
82 listprice => '0.00000',
83 partnumber => 'v-120160086',
84 sellprice => '344.30000',
91 Support::TestSetup::login();
95 # we create L20199 with two items
96 my $do1 = create_sales_delivery_order(
97 'department_id' => $department->id,
98 'transdate' => $transdate,
99 'donumber' => 'L20199',
100 'employee_id' => $employee->id,
101 'intnotes' => 'some intnotes',
102 'ordnumber' => 'A16399',
103 'payment_id' => $payment_do->id,
104 'salesman_id' => $employee->id,
105 'shippingpoint' => 'sendtome',
106 'shipvia' => 'DHL, Versand am 06.03.2015, 1 Paket 17,00 kg',
107 'cusordnumber' => 'b84da',
108 'customer_id' => $customer->id,
109 'notes' => '<ul><li><strong>fett</strong></li><li><strong>und</strong></li><li><strong>mit</strong></li><li><strong>bullets</strong></li><li> </li></ul>',
111 create_delivery_order_item(
114 lastcost => '49.95000',
115 longdescription => "<ol><li>27</li><li>28</li><li>29</li><li><sub>asdf</sub></li><li><sub>asdf</sub></li><li><sup>oben</sup></li></ol><p><s>kommt nicht mehr vor</s></p>",
116 marge_price_factor => 1,
118 sellprice => '242.20000',
121 create_delivery_order_item(
124 lastcost => '153.00000',
126 sellprice => '344.30000',
127 transdate => '06.03.2015',
131 transaction_description => 'Liefervorgang',
137 my $do1_item1 = $do1->orderitems->[0];
138 my $do1_item2 = $do1->orderitems->[1];
140 # test delivery order before any conversion
141 ok($do1->donumber eq "L20199", 'Delivery Order Number created');
142 ok($do1->notes eq '<ul><li><strong>fett</strong></li><li><strong>und</strong></li><li><strong>mit</strong></li><li><strong>bullets</strong></li><li> </li></ul>', "do RichText notes saved");
143 ok((not $do1->closed) , 'Delivery Order is not closed');
144 is($do1_item1->parts_id, $parts[0]->id, 'doi linked with part');
145 ok($do1_item1->qty == 2, 'qty check doi');
146 ok($do1_item1->longdescription eq "<ol><li>27</li><li>28</li><li>29</li><li><sub>asdf</sub></li><li><sub>asdf</sub></li><li><sup>oben</sup></li></ol><p><s>kommt nicht mehr vor</s></p>",
147 "do item1 rich text longdescripition");
148 ok ($do1_item2->position == 2, 'doi2 position check');
149 is (SL::DB::Manager::DeliveryOrderItem->get_all_count(where => [ delivery_order_id => $do1->id ]), 2 , 'two doi linked');
152 # convert this do to invoice
153 my $invoice = $do1->convert_to_invoice(transdate => $transdate, attributes => {transaction_description => 'Rechnungsvorgang'});
155 sleep (300) if $VISUAL_TEST; # we can do a real visual test via gui login
156 # test invoice afterwards
158 ok ($invoice->shipvia eq "DHL, Versand am 06.03.2015, 1 Paket 17,00 kg", "ship via check");
159 ok ($invoice->shippingpoint eq "sendtome", "shipping point check");
160 ok ($invoice->ordnumber eq "A16399", "ordnumber check");
161 ok ($invoice->donumber eq "L20199", "donumber check");
162 ok ($invoice->notes eq '<ul><li><strong>fett</strong></li><li><strong>und</strong></li><li><strong>mit</strong></li><li><strong>bullets</strong></li><li> </li></ul>', "do RichText notes saved");
163 ok(($do1->closed) , 'Delivery Order is closed after conversion');
164 is($invoice->payment_terms->description, "14Tage 2%Skonto, 30Tage netto", 'payment term description check');
168 is($invoice->cusordnumber , 'b84da' , 'cusordnumber check');
169 is($invoice->transaction_description, 'Rechnungsvorgang', 'transaction description (changed on conversion) check');
170 is($invoice->department->description , "Test Department" , 'department description ok');
171 is($invoice->amount , '1354.20000' , 'amount check');
172 is($invoice->marge_percent , '50.88666' , 'marge percent check');
173 is($invoice->marge_total , '579.08000' , 'marge total check');
174 is($invoice->netamount , '1137.98000' , 'netamount check');
177 is($invoice->items_sorted->[0]->parts_id , $parts[0]->id , 'invoiceitem 1 linked with part');
178 is(scalar @{ $invoice->invoiceitems } , 2 , 'two invoice items linked with invoice');
179 is($invoice->items_sorted->[0]->position , 1 , "position 1 order correct");
180 is($invoice->items_sorted->[1]->position , 2 , "position 2 order correct");
181 is($invoice->items_sorted->[0]->part->partnumber , 'v-519160549' , "partnumber 1 correct");
182 is($invoice->items_sorted->[1]->part->partnumber , 'v-120160086' , "partnumber 2 correct");
183 is($invoice->items_sorted->[0]->qty , '2.00000' , "pos 1 qty");
184 is($invoice->items_sorted->[1]->qty , '3.00000' , "pos 2 qty");
185 is($invoice->items_sorted->[0]->discount , 0.25 , "pos 1 discount");
186 is($invoice->items_sorted->[1]->discount , 0.25 , "pos 2 discount");
187 is($invoice->items_sorted->[0]->longdescription , "<ol><li>27</li><li>28</li><li>29</li><li><sub>asdf</sub></li><li><sub>asdf</sub></li><li><sup>oben</sup></li></ol><p><s>kommt nicht mehr vor</s></p>",
188 "invoice item1 rich text longdescripition");
189 # more ideas: check onhand, lastcost (parsed lastcost)
193 # check linked records AND linked items
195 # we expect something like this in record links:
196 # delivery_order_items | 144736 | invoice | 9 | 2015-09-02 16:29:32.362562 | 5
197 # delivery_order_items | 144737 | invoice | 10 | 2015-09-02 16:29:32.362562 | 6
198 # delivery_orders | 464003 | ar | 5 | 2015-09-02 16:29:32.362562 | 7
200 # verkn�pfter beleg$VAR1 = {
201 # 'from_id' => 464003,
202 # 'from_table' => 'delivery_orders',
206 # verkn�pfte positionen$VAR1 = {
207 # 'from_id' => 144737,
208 # 'from_table' => 'delivery_order_items',
210 # 'to_table' => 'invoice'
213 # 'from_id' => 144736,
214 # 'from_table' => 'delivery_order_items',
216 # 'to_table' => 'invoice'
220 my @links_record = RecordLinks->get_links('from_table' => 'delivery_orders',
222 'from_id' => $do1->id,
225 is($links_record[0]->{from_id} , $do1->id , "record from id check");
226 is($links_record[0]->{from_table} , 'delivery_orders' , "record from table check");
227 is($links_record[0]->{to_table} , 'ar' , "record to table check");
229 foreach ( $do1_item1->id, $do1_item2->id ) {
230 my @links_record_item1 = RecordLinks->get_links('from_table' => 'delivery_order_items',
231 'to_table' => 'invoice',
235 is($links_record_item1[0]->{from_id} , $_ , "record from id check $_");
236 is($links_record_item1[0]->{from_table} , 'delivery_order_items' , "record from table check $_");
237 is($links_record_item1[0]->{to_table} , 'invoice' , "record to table check $_");
241 # test conversion from order to invoice
246 # we create A16399 with two items
247 my $o1 = create_sales_order(
249 'department_id' => $department->id,
250 'transdate' => $transdate,
251 'employee_id' => $employee->id,
252 'intnotes' => 'some intnotes',
253 'ordnumber' => 'A16399',
254 'payment_id' => $payment_do->id,
255 'salesman_id' => $employee->id,
256 'shippingpoint' => 'sendtome',
257 'shipvia' => 'DHL, Versand am 06.03.2015, 1 Paket 17,00 kg',
258 'cusordnumber' => 'b84da',
259 'customer_id' => $customer->id,
260 'notes' => '<ul><li><strong>fett</strong></li><li><strong>und</strong></li><li><strong>mit</strong></li><li><strong>bullets</strong></li><li> </li></ul>',
265 lastcost => '49.95000',
266 longdescription => "<ol><li>27</li><li>28</li><li>29</li><li><sub>asdf</sub></li><li><sub>asdf</sub></li><li><sup>oben</sup></li></ol><p><s>kommt nicht mehr vor</s></p>",
267 marge_price_factor => 1,
269 sellprice => '242.20000',
275 lastcost => '153.00000',
277 sellprice => '344.30000',
278 transdate => '06.03.2015',
282 transaction_description => 'Auftragsvorgang',
288 my $o1_item1 = $o1->orderitems->[0];
289 my $o1_item2 = $o1->orderitems->[1];
291 # convert this order to invoice
292 $invoice = $o1->convert_to_invoice(transdate => $transdate, attributes => {transaction_description => 'Rechnungsvorgang'});
295 # test invoice afterwards
296 ok ($invoice->shipvia eq "DHL, Versand am 06.03.2015, 1 Paket 17,00 kg", "convert form order: ship via check");
297 ok ($invoice->shippingpoint eq "sendtome", "convert form order: shipping point check");
298 ok ($invoice->ordnumber eq "A16399", "convert form order: ordnumber check");
299 ok ($invoice->notes eq '<ul><li><strong>fett</strong></li><li><strong>und</strong></li><li><strong>mit</strong></li><li><strong>bullets</strong></li><li> </li></ul>', "convert form order: do RichText notes saved");
300 ok(($o1->closed) , 'convert form order: Order is closed after conversion');
301 is($invoice->payment_terms->description, "14Tage 2%Skonto, 30Tage netto", 'convert form order: payment term description check');
303 is($invoice->cusordnumber, 'b84da', 'convert form order: cusordnumber check');
304 is($invoice->transaction_description, 'Rechnungsvorgang', 'convert form order: transaction description (changed on conversion) check');
305 is($invoice->department->description, "Test Department", 'convert form order: department description ok');
306 is($invoice->amount, '1354.20000', 'convert form order: amount check');
307 is($invoice->marge_percent, '50.88666', 'convert form order: marge percent check');
308 is($invoice->marge_total, '579.08000', 'convert form order: marge total check');
309 is($invoice->netamount, '1137.98000', 'convert form order: netamount check');
312 is($invoice->items_sorted->[0]->parts_id, $parts[0]->id , 'convert form order: invoiceitem 1 linked with part');
313 is(scalar @{ $invoice->invoiceitems }, 2, 'convert form order: two invoice items linked with invoice');
314 is($invoice->items_sorted->[0]->position, 1, "convert form order: position 1 order correct");
315 is($invoice->items_sorted->[1]->position, 2, "convert form order: position 2 order correct");
316 is($invoice->items_sorted->[0]->part->partnumber, 'v-519160549' , "convert form order: partnumber 1 correct");
317 is($invoice->items_sorted->[1]->part->partnumber, 'v-120160086' , "convert form order: partnumber 2 correct");
318 is($invoice->items_sorted->[0]->qty, '2.00000', "convert form order: pos 1 qty");
319 is($invoice->items_sorted->[1]->qty, '3.00000', "convert form order: pos 2 qty");
320 is($invoice->items_sorted->[0]->discount, 0.25, "convert form order: pos 1 discount");
321 is($invoice->items_sorted->[1]->discount, 0.25, "convert form order: pos 2 discount");
322 is($invoice->items_sorted->[0]->longdescription , "<ol><li>27</li><li>28</li><li>29</li><li><sub>asdf</sub></li><li><sub>asdf</sub></li><li><sup>oben</sup></li></ol><p><s>kommt nicht mehr vor</s></p>",
323 "convert form order: invoice item1 rich text longdescripition");
325 # check linked records AND linked items
326 @links_record = RecordLinks->get_links('from_table' => 'oe',
328 'from_id' => $o1->id,
331 is($links_record[0]->{from_id}, $o1->id, "convert form order: record from id check");
332 is($links_record[0]->{from_table}, 'oe', "convert form order: record from table check");
333 is($links_record[0]->{to_table}, 'ar', "convert form order: record to table check");
336 foreach ( $o1_item1->id, $o1_item2->id ) {
338 my @links_record_item = RecordLinks->get_links('from_table' => 'orderitems',
339 'to_table' => 'invoice',
343 is($links_record_item[0]->{from_id}, $_ , "convert form order: record from id check item $i");
344 is($links_record_item[0]->{from_table}, 'orderitems', "convert form order: record from table check item $i");
345 is($links_record_item[0]->{to_table}, 'invoice', "convert form order: record to table check item $i");
353 # set emacs to perl mode