1 use Test::More tests => 29;
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,
31 foreach (qw(DeliveryOrderItem DeliveryOrder InvoiceItem Invoice Part Customer Vendor Employee Department PaymentTerm)) {
32 "SL::DB::Manager::${_}"->delete_all(all => 1);
41 $buchungsgruppe = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 19%', %{ $params{buchungsgruppe} }) || croak "No accounting group 19\%";
42 $buchungsgruppe7 = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 7%', %{ $params{buchungsgruppe} }) || croak "No accounting group 7\%";
43 $taxzone = SL::DB::Manager::TaxZone->find_by( description => 'Inland') || croak "No taxzone";
44 $tax = SL::DB::Manager::Tax->find_by(taxkey => 3, rate => 0.19, %{ $params{tax} }) || croak "No tax for 19\%";
45 $tax7 = SL::DB::Manager::Tax->find_by(taxkey => 2, rate => 0.07) || croak "No tax for 7\%";
46 $unit = SL::DB::Manager::Unit->find_by(name => 'kg', %{ $params{unit} }) || croak "No unit";
47 $currency_id = $::instance_conf->get_currency_id;
49 $customer = SL::DB::Customer->new(
50 name => '520484567dfaedc9e60fc',
51 currency_id => $currency_id,
52 taxzone_id => $taxzone->id,
53 %{ $params{customer} }
56 # some od.rnr real anonym data
57 my $employee_bk = SL::DB::Employee->new(
59 'login' => 'barbuschka.kappes',
60 'name' => 'Barbuschka Kappes',
63 my $department_do = SL::DB::Department->new(
64 'description' => 'Maisenhaus-Versand',
70 my $payment_do = SL::DB::PaymentTerm->new(
71 'description' => '14Tage 2%Skonto, 30Tage netto',
72 '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.",
76 'percent_skonto' => '0.02',
80 'auto_calculation' => undef,
86 push @parts, SL::DB::Part->new(
89 'lastcost' => '49.95000',
90 'listprice' => '0.00000',
91 'onhand' => '5.00000',
92 'partnumber' => 'v-519160549',
93 #'partsgroup_id' => 111645,
95 'sellprice' => '242.20000',
96 #'warehouse_id' => 64702,
98 description => "Nussbaum, Gr.5, Unterfilz weinrot, genietet[[Aufschnittbreite: 11,0, Kernform: US]]\"" ,
99 buchungsgruppen_id => $buchungsgruppe->id,
104 push @parts, SL::DB::Part->new(
105 'description' => "[[0640]]Flügel Hammerstiele bestehend aus:
106 70 Stielen Standard in Weißbuche und
107 20 Stielen Diskant abgekehlt in Weißbuche
108 mit Röllchen aus Synthetikleder,
109 Kapseln mit Yamaha Profil, Kerbenabstand 3,6 mm mit eingedrehten Abnickschrauben",
111 'lastcost' => '153.00000',
112 'listprice' => '0.00000',
113 'onhand' => '9.00000',
114 'partnumber' => 'v-120160086',
115 # 'partsgroup_id' => 111639,
117 'sellprice' => '344.30000',
119 buchungsgruppen_id => $buchungsgruppe->id,
124 sub new_delivery_order {
127 return SL::DB::DeliveryOrder->new(
128 currency_id => $currency_id,
129 taxzone_id => $taxzone->id,
134 Support::TestSetup::login();
138 # we create L20199 with two items
139 my $do1 = new_delivery_order('department_id' => 32149,
140 'donumber' => 'L20199',
141 'employee_id' => 31915,
142 'intnotes' => 'Achtung: Neue Lieferadresse ab 16.02.2015 in der Carl-von-Ossietzky-Str.32! 13.02.2015/MH
144 Steinway-Produkte (201...) immer plus 25% dazu rechnen / BK 13.02.2014',
145 'ordnumber' => 'A16399',
146 'payment_id' => 11276,
147 'salesman_id' => 31915,
148 'shippingpoint' => 'Maisenhaus',
149 # 'shipto_id' => 451463,
150 'is_sales' => 'true',
151 'shipvia' => 'DHL, Versand am 06.03.2015, 1 Paket 17,00 kg',
154 # 'currency_id' => 1,
155 'cusordnumber' => 'b84da',
156 'customer_id' => $customer->id,
160 my $do1_item1 = SL::DB::DeliveryOrderItem->new('delivery_order_id' => 464003,
161 'description' => "Flügel Hammerkopf bestehend aus:
162 Bass/Diskant 26/65 Stück, Gesamtlänge 80/72, Bohrlänge 56/48
163 Nussbaum, Gr.5, Unterfilz weinrot, genietet[[Aufschnittbreite: 11,0, Kernform: US]]",
164 'discount' => '0.25',
166 'lastcost' => '49.95000',
167 'longdescription' => '',
168 'marge_price_factor' => 1,
170 'ordnumber' => 'A16399',
175 'sellprice' => '242.20000',
176 'transdate' => '06.03.2015',
177 'unit' => 'kg')->save;
179 my $do1_item2 = SL::DB::DeliveryOrderItem->new('delivery_order_id' => 464003,
180 'description' => "[[0640]]Flügel Hammerstiele bestehend aus:
181 70 Stielen Standard in Weißbuche und
182 20 Stielen Diskant abgekehlt in Weißbuche
183 mit Röllchen aus Synthetikleder,
184 Kapseln mit Yamaha Profil, Kerbenabstand 3,6 mm mit eingedrehten Abnickschrauben",
185 'discount' => '0.25',
188 'lastcost' => '153.00000',
189 'longdescription' => '',
190 'marge_price_factor' => 1,
192 'ordnumber' => 'A16399',
196 'price_factor_id' => undef,
197 'pricegroup_id' => undef,
198 'project_id' => undef,
201 'sellprice' => '344.30000',
202 'serialnumber' => '',
203 'transdate' => '06.03.2015',
204 'unit' => 'kg')->save;
209 # test delivery order before any conversion
210 ok($do1->donumber eq "L20199", 'Delivery Order Number created');
211 ok((not $do1->closed) , 'Delivery Order is not closed');
212 ok($do1_item1->parts_id eq '26321', 'doi linked with part');
213 ok($do1_item1->qty == 2, 'qty check doi');
214 ok($do1_item2->position == 2, 'doi2 position check');
215 ok(2 == scalar@{ SL::DB::Manager::DeliveryOrderItem->get_all(where => [ delivery_order_id => $do1->id ]) }, 'two doi linked');
218 # convert this do to invoice
219 my $invoice = $do1->convert_to_invoice();
221 # test invoice afterwards
223 ok ($invoice->shipvia eq "DHL, Versand am 06.03.2015, 1 Paket 17,00 kg", "ship via check");
224 ok ($invoice->shippingpoint eq "Maisenhaus", "shipping point check");
225 ok ($invoice->ordnumber eq "A16399", "ordnumber check");
226 ok ($invoice->donumber eq "L20199", "donumber check");
227 ok(($do1->closed) , 'Delivery Order is closed after conversion');
228 ok (SL::DB::PaymentTerm->new(id => $invoice->{payment_id})->load->description eq "14Tage 2%Skonto, 30Tage netto", 'payment term description check');
230 # some test data from original client invoice console (!)
231 # my $invoice3 = SL::DB::Manager::Invoice->find_by( ordnumber => 'A16399' );
232 # which will fail due to PTC Calculation differs from GUI-Calculation, see issue: http://redmine.kivitendo-premium.de/issues/82
234 # values from gui should be:
235 #ok($invoice->amount == 1354.20000, 'amount check');
236 #ok($invoice->marge_percent == 50.88666, 'marge percent check');
237 #ok($invoice->marge_total == 579.08000, 'marge total check');
238 #ok($invoice->netamount == 1137.98000, 'netamount check');
241 # the values change if one reloads the object
242 # without reloading we get this failures
243 #not ok 17 - amount check
244 # Failed test 'amount check'
245 # at t/db_helper/convert_invoice.t line 272.
247 # expected: '1354.17000'
248 #not ok 18 - marge percent check
249 # Failed test 'marge percent check'
250 # at t/db_helper/convert_invoice.t line 273.
251 # got: '50.8857956342929'
252 # expected: '50.88580'
253 #not ok 19 - marge total check
254 # Failed test 'marge total check'
255 # at t/db_helper/convert_invoice.t line 274.
257 # expected: '579.06000'
258 #not ok 20 - netamount check
259 # Failed test 'netamount check'
260 # at t/db_helper/convert_invoice.t line 275.
262 # expected: '1137.96000'
266 ok($invoice->currency_id eq '1', 'currency_id');
267 ok($invoice->cusordnumber eq 'b84da', 'cusordnumber check');
268 ok(SL::DB::Department->new(id => $invoice->{department_id})->load->description eq "Maisenhaus-Versand", 'department description');
269 is($invoice->amount, '1354.17000', 'amount check');
270 is($invoice->marge_percent, '50.88580', 'marge percent check');
271 is($invoice->marge_total, '579.06000', 'marge total check');
272 is($invoice->netamount, '1137.96000', 'netamount check');
275 ok(@ {$invoice->items_sorted}[0]->parts_id eq '26321', 'invoiceitem 1 linked with part');
276 ok(2 == scalar@{ $invoice->invoiceitems }, 'two invoice items linked with invoice');
277 is(@ {$invoice->items_sorted}[0]->position, 1, "position 1 order correct");
278 is(@ {$invoice->items_sorted}[1]->position, 2, "position 2 order correct");
279 is(@ {$invoice->items_sorted}[0]->part->partnumber, 'v-519160549', "partnumber 1 correct");
280 is(@ {$invoice->items_sorted}[1]->part->partnumber, 'v-120160086', "partnumber 2 correct");
281 is(@ {$invoice->items_sorted}[0]->qty, '2.00000', "pos 1 qty");
282 is(@ {$invoice->items_sorted}[1]->qty, '3.00000', "pos 2 qty");
283 is(@ {$invoice->items_sorted}[0]->discount, 0.25, "pos 1 discount");
284 is(@ {$invoice->items_sorted}[1]->discount, 0.25, "pos 2 discount");
286 # more ideas: check onhand, lastcost (parsed lastcost)