1 use Test::More tests => 42;
8 use Support::TestSetup;
12 use Support::TestSetup;
14 use List::Util qw(max);
16 use SL::DB::Buchungsgruppe;
22 use SL::DB::DeliveryOrder;
27 my ($customer, $currency_id, $buchungsgruppe, $employee, $vendor, $taxzone, $buchungsgruppe7, $tax, $tax7,
30 my $VISUAL_TEST = 0; # just a sleep to click around
33 foreach (qw(DeliveryOrderItem DeliveryOrder InvoiceItem Invoice Part Customer Vendor Department PaymentTerm)) {
34 "SL::DB::Manager::${_}"->delete_all(all => 1);
36 SL::DB::Manager::Employee->delete_all(where => [ id => 31915 ]);
44 if ($::lx_office_conf{system}->{default_manager} eq "swiss") {
45 $buchungsgruppe = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 8%', %{ $params{buchungsgruppe} }) || croak "No accounting group 8\%";
46 $buchungsgruppe7 = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 2.5%', %{ $params{buchungsgruppe} })|| croak "No accounting group 2.5\%";
47 $taxzone = SL::DB::Manager::TaxZone->find_by( description => 'Schweiz') || croak "No taxzone";
48 $tax = SL::DB::Manager::Tax->find_by(taxkey => 2, rate => 0.08, %{ $params{tax} }) || croak "No tax for 8\%";
49 $tax7 = SL::DB::Manager::Tax->find_by(taxkey => 3, rate => 0.025) || croak "No tax for 2.5\%";
50 $unit = SL::DB::Manager::Unit->find_by(name => 'kg', %{ $params{unit} }) || croak "No unit";
51 $currency_id = $::instance_conf->get_currency_id;
53 $buchungsgruppe = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 19%', %{ $params{buchungsgruppe} }) || croak "No accounting group 19\%";
54 $buchungsgruppe7 = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 7%', %{ $params{buchungsgruppe} }) || croak "No accounting group 7\%";
55 $taxzone = SL::DB::Manager::TaxZone->find_by( description => 'Inland') || croak "No taxzone";
56 $tax = SL::DB::Manager::Tax->find_by(taxkey => 3, rate => 0.19, %{ $params{tax} }) || croak "No tax for 19\%";
57 $tax7 = SL::DB::Manager::Tax->find_by(taxkey => 2, rate => 0.07) || croak "No tax for 7\%";
58 $unit = SL::DB::Manager::Unit->find_by(name => 'kg', %{ $params{unit} }) || croak "No unit";
59 $currency_id = $::instance_conf->get_currency_id;
62 $customer = SL::DB::Customer->new(
63 name => '520484567dfaedc9e60fc',
64 currency_id => $currency_id,
65 taxzone_id => $taxzone->id,
66 %{ $params{customer} }
69 # some od.rnr real anonym data
70 my $employee_bk = SL::DB::Employee->new(
72 'login' => 'barbuschka.kappes',
73 'name' => 'Barbuschka Kappes',
76 my $department_do = SL::DB::Department->new(
77 'description' => 'Maisenhaus-Versand',
83 my $payment_do = SL::DB::PaymentTerm->new(
84 'description' => '14Tage 2%Skonto, 30Tage netto',
85 '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.",
89 'percent_skonto' => '0.02',
93 'auto_calculation' => undef,
99 push @parts, SL::DB::Part->new(
102 'lastcost' => '49.95000',
103 'listprice' => '0.00000',
104 'onhand' => '5.00000',
105 'partnumber' => 'v-519160549',
106 #'partsgroup_id' => 111645,
108 'sellprice' => '242.20000',
109 #'warehouse_id' => 64702,
111 description => "Pflaumenbaum, Gr.5, Unterfilz weinrot, genietet[[Aufschnittbreite: 11,0, Kernform: US]]\"" ,
112 buchungsgruppen_id => $buchungsgruppe->id,
117 push @parts, SL::DB::Part->new(
118 'description' => "[[0640]]Flügel Hammerstiele bestehend aus:
119 70 Stielen Standard in Weißbuche und
120 20 Stielen Diskant abgekehlt in Weißbuche
121 mit Röllchen aus Synthetikleder,
122 Kapseln mit Yamaha Profil, Kerbenabstand 3,6 mm mit eingedrehten Abnickschrauben",
124 'lastcost' => '153.00000',
125 'listprice' => '0.00000',
126 'onhand' => '9.00000',
127 'partnumber' => 'v-120160086',
128 # 'partsgroup_id' => 111639,
130 'sellprice' => '344.30000',
132 buchungsgruppen_id => $buchungsgruppe->id,
137 sub new_delivery_order {
140 return SL::DB::DeliveryOrder->new(
141 currency_id => $currency_id,
142 taxzone_id => $taxzone->id,
147 Support::TestSetup::login();
151 # we create L20199 with two items
152 my $do1 = new_delivery_order('department_id' => 32149,
153 'donumber' => 'L20199',
154 'employee_id' => 31915,
155 'intnotes' => 'Achtung: Neue Lieferadresse ab 16.02.2015 in der Otto-Merck-Str. 7a! 13.02.2015/MH
157 Yamaha-Produkte (201...) immer plus 25% dazu rechnen / BK 13.02.2014',
158 'ordnumber' => 'A16399',
159 'payment_id' => 11276,
160 'salesman_id' => 31915,
161 'shippingpoint' => 'Maisenhaus',
162 # 'shipto_id' => 451463,
163 'is_sales' => 'true',
164 'shipvia' => 'DHL, Versand am 06.03.2015, 1 Paket 17,00 kg',
167 # 'currency_id' => 1,
168 'cusordnumber' => 'b84da',
169 'customer_id' => $customer->id,
171 '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>',
174 my $do1_item1 = SL::DB::DeliveryOrderItem->new('delivery_order_id' => 464003,
175 'description' => "Flügel Hammerkopf bestehend aus:
176 Bass/Diskant 26/65 Stück, Gesamtlänge 80/72, Bohrlänge 56/48
177 Pflaumenbaum, Gr.5, Unterfilz weinrot, genietet[[Aufschnittbreite: 11,0, Kernform: US]]",
178 'discount' => '0.25',
180 'lastcost' => '49.95000',
181 '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>",
182 'marge_price_factor' => 1,
184 'ordnumber' => 'A16399',
189 'sellprice' => '242.20000',
190 'transdate' => '06.03.2015',
191 'unit' => 'kg')->save;
193 my $do1_item2 = SL::DB::DeliveryOrderItem->new('delivery_order_id' => 464003,
194 'description' => "[[0640]]Flügel Hammerstiele bestehend aus:
195 70 Stielen Standard in Weißbuche und
196 20 Stielen Diskant abgekehlt in Weißbuche
197 mit Röllchen aus Synthetikleder,
198 Kapseln mit Yamaha Profil, Kerbenabstand 3,6 mm mit eingedrehten Abnickschrauben",
199 'discount' => '0.25',
202 'lastcost' => '153.00000',
203 'longdescription' => '',
204 'marge_price_factor' => 1,
206 'ordnumber' => 'A16399',
210 'price_factor_id' => undef,
211 'pricegroup_id' => undef,
212 'project_id' => undef,
215 'sellprice' => '344.30000',
216 'serialnumber' => '',
217 'transdate' => '06.03.2015',
218 'unit' => 'kg')->save;
223 # test delivery order before any conversion
224 ok($do1->donumber eq "L20199", 'Delivery Order Number created');
225 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");
226 ok((not $do1->closed) , 'Delivery Order is not closed');
227 ok($do1_item1->parts_id eq '26321', 'doi linked with part');
228 ok($do1_item1->qty == 2, 'qty check doi');
229 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>",
230 "do item1 rich text longdescripition");
231 ok ($do1_item2->position == 2, 'doi2 position check');
232 ok (2 == scalar@{ SL::DB::Manager::DeliveryOrderItem->get_all(where => [ delivery_order_id => $do1->id ]) }, 'two doi linked');
235 # convert this do to invoice
236 my $invoice = $do1->convert_to_invoice();
238 sleep (300) if $VISUAL_TEST; # we can do a real visual test via gui login
239 # test invoice afterwards
241 ok ($invoice->shipvia eq "DHL, Versand am 06.03.2015, 1 Paket 17,00 kg", "ship via check");
242 ok ($invoice->shippingpoint eq "Maisenhaus", "shipping point check");
243 ok ($invoice->ordnumber eq "A16399", "ordnumber check");
244 ok ($invoice->donumber eq "L20199", "donumber check");
245 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");
246 ok(($do1->closed) , 'Delivery Order is closed after conversion');
247 ok (SL::DB::PaymentTerm->new(id => $invoice->{payment_id})->load->description eq "14Tage 2%Skonto, 30Tage netto", 'payment term description check');
249 # some test data from original client invoice console (!)
250 # my $invoice3 = SL::DB::Manager::Invoice->find_by( ordnumber => 'A16399' );
251 # which will fail due to PTC Calculation differs from GUI-Calculation, see issue: http://redmine.kivitendo-premium.de/issues/82
253 # values from gui should be:
254 #ok($invoice->amount == 1354.20000, 'amount check');
255 #ok($invoice->marge_percent == 50.88666, 'marge percent check');
256 #ok($invoice->marge_total == 579.08000, 'marge total check');
257 #ok($invoice->netamount == 1137.98000, 'netamount check');
260 # the values change if one reloads the object
261 # without reloading we get this failures
262 #not ok 17 - amount check
263 # Failed test 'amount check'
264 # at t/db_helper/convert_invoice.t line 272.
266 # expected: '1354.17000'
267 #not ok 18 - marge percent check
268 # Failed test 'marge percent check'
269 # at t/db_helper/convert_invoice.t line 273.
270 # got: '50.8857956342929'
271 # expected: '50.88580'
272 #not ok 19 - marge total check
273 # Failed test 'marge total check'
274 # at t/db_helper/convert_invoice.t line 274.
276 # expected: '579.06000'
277 #not ok 20 - netamount check
278 # Failed test 'netamount check'
279 # at t/db_helper/convert_invoice.t line 275.
281 # expected: '1137.96000'
285 ok($invoice->currency_id eq '1', 'currency_id');
286 ok($invoice->cusordnumber eq 'b84da', 'cusordnumber check');
287 ok(SL::DB::Department->new(id => $invoice->{department_id})->load->description eq "Maisenhaus-Versand", 'department description');
288 if ($::lx_office_conf{system}->{default_manager} eq "swiss") {
289 is($invoice->amount, '1229.00000', 'amount check');
291 is($invoice->amount, '1354.17000', 'amount check');
293 is($invoice->marge_percent, '50.88580', 'marge percent check');
294 is($invoice->marge_total, '579.06000', 'marge total check');
295 is($invoice->netamount, '1137.96000', 'netamount check');
298 ok(@ {$invoice->items_sorted}[0]->parts_id eq '26321', 'invoiceitem 1 linked with part');
299 ok(2 == scalar@{ $invoice->invoiceitems }, 'two invoice items linked with invoice');
300 is(@ {$invoice->items_sorted}[0]->position, 1, "position 1 order correct");
301 is(@ {$invoice->items_sorted}[1]->position, 2, "position 2 order correct");
302 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>",
303 "invoice item1 rich text longdescripition");
304 is(@ {$invoice->items_sorted}[0]->part->partnumber, 'v-519160549', "partnumber 1 correct");
305 is(@ {$invoice->items_sorted}[1]->part->partnumber, 'v-120160086', "partnumber 2 correct");
306 is(@ {$invoice->items_sorted}[0]->qty, '2.00000', "pos 1 qty");
307 is(@ {$invoice->items_sorted}[1]->qty, '3.00000', "pos 2 qty");
308 is(@ {$invoice->items_sorted}[0]->discount, 0.25, "pos 1 discount");
309 is(@ {$invoice->items_sorted}[1]->discount, 0.25, "pos 2 discount");
311 # more ideas: check onhand, lastcost (parsed lastcost)
315 # check linked records AND linked items
317 # we expect something like this in record links:
318 # delivery_order_items | 144736 | invoice | 9 | 2015-09-02 16:29:32.362562 | 5
319 # delivery_order_items | 144737 | invoice | 10 | 2015-09-02 16:29:32.362562 | 6
320 # delivery_orders | 464003 | ar | 5 | 2015-09-02 16:29:32.362562 | 7
322 # verkn�pfter beleg$VAR1 = {
323 # 'from_id' => 464003,
324 # 'from_table' => 'delivery_orders',
328 # verkn�pfte positionen$VAR1 = {
329 # 'from_id' => 144737,
330 # 'from_table' => 'delivery_order_items',
332 # 'to_table' => 'invoice'
335 # 'from_id' => 144736,
336 # 'from_table' => 'delivery_order_items',
338 # 'to_table' => 'invoice'
343 my @links_record = RecordLinks->get_links('from_table' => 'delivery_orders',
345 'from_id' => 464003);
346 is($links_record[0]->{from_id}, '464003', "record from id check");
347 is($links_record[0]->{from_table}, 'delivery_orders', "record from table check");
348 is($links_record[0]->{to_table}, 'ar', "record to table check");
350 foreach (qw(144736 144737)) {
351 my @links_record_item1 = RecordLinks->get_links('from_table' => 'delivery_order_items',
352 'to_table' => 'invoice',
354 is($links_record_item1[0]->{from_id}, $_, "record from id check $_");
355 is($links_record_item1[0]->{from_table}, 'delivery_order_items', "record from table check $_");
356 is($links_record_item1[0]->{to_table}, 'invoice', "record to table check $_");