t/db_helper/convert_invoice.t mit SL::Dev refactored
[kivitendo-erp.git] / t / db_helper / convert_invoice.t
1 use Test::More tests => 41;
2
3 use strict;
4
5 use lib 't';
6 use utf8;
7
8 use Support::TestSetup;
9
10 use Carp;
11 use Data::Dumper;
12 use Support::TestSetup;
13 use Test::Exception;
14
15 use SL::DB::Buchungsgruppe;
16 use SL::DB::Currency;
17 use SL::DB::Customer;
18 use SL::DB::Employee;
19 use SL::DB::Invoice;
20 use SL::DB::Order;
21 use SL::DB::DeliveryOrder;
22 use SL::DB::Part;
23 use SL::DB::Unit;
24
25 use SL::Dev::ALL;
26
27 my ($customer, $employee, $payment_do, $unit, @parts, $department);
28
29 my $VISUAL_TEST = 0;  # just a sleep to click around
30
31 sub clear_up {
32   foreach (qw(DeliveryOrderItem DeliveryOrder InvoiceItem Invoice Part Customer Department PaymentTerm)) {
33     "SL::DB::Manager::${_}"->delete_all(all => 1);
34   }
35   SL::DB::Manager::Employee->delete_all(where => [ login => 'testuser' ]);
36 };
37
38 sub reset_state {
39   my %params = @_;
40
41   clear_up();
42
43   $unit     = SL::DB::Manager::Unit->find_by(name => 'kg') || die "Can't find unit 'kg'";
44   $customer = SL::Dev::CustomerVendor::create_customer->save;
45
46   $employee = SL::DB::Employee->new(
47     'login' => 'testuser',
48     'name'  => 'Test User',
49   )->save;
50
51   $department = SL::DB::Department->new(
52     'description' => 'Test Department',
53   )->save;
54
55   $payment_do = SL::Dev::Payment::create_payment_terms(
56      'description'      => '14Tage 2%Skonto, 30Tage netto',
57      '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.",
58      'percent_skonto'   => '0.02',
59      'terms_netto'      => 30,
60      'terms_skonto'     => 14
61   );
62
63   # two real parts
64   @parts = ();
65   push @parts, SL::Dev::Part::create_part(
66     description => "description 1",
67     lastcost    => '49.95000',
68     listprice   => '0.00000',
69     partnumber  => 'v-519160549',
70     sellprice   => '242.20000',
71     unit        => $unit->name,
72     weight      => '0.79',
73   )->save;
74
75   push @parts, SL::Dev::Part::create_part(
76     description => "description 2",
77     lastcost    => '153.00000',
78     listprice   => '0.00000',
79     partnumber  => 'v-120160086',
80     sellprice   => '344.30000',
81     unit        => $unit->name,
82     weight      => '0.9',
83   )->save;
84
85 }
86
87 Support::TestSetup::login();
88
89 reset_state();
90
91 # we create L20199 with two items
92 my $do1 = SL::Dev::Record::create_sales_delivery_order(
93   'department_id' => $department->id,
94   'donumber'      => 'L20199',
95   'employee_id'   => $employee->id,
96   'intnotes'      => 'some intnotes',
97   'ordnumber'     => 'A16399',
98   'payment_id'    => $payment_do->id,
99   'salesman_id'   => $employee->id,
100   'shippingpoint' => 'sendtome',
101   'shipvia'       => 'DHL, Versand am 06.03.2015, 1 Paket  17,00 kg',
102   'cusordnumber'  => 'b84da',
103   'customer_id'   => $customer->id,
104   'notes'         => '<ul><li><strong>fett</strong></li><li><strong>und</strong></li><li><strong>mit</strong></li><li><strong>bullets</strong></li><li>&nbsp;</li></ul>',
105   orderitems => [
106                   SL::Dev::Record::create_delivery_order_item(
107                     part               => $parts[0],
108                     discount           => '0.25',
109                     lastcost           => '49.95000',
110                     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>",
111                     marge_price_factor => 1,
112                     qty                => '2.00000',
113                     sellprice          => '242.20000',
114                     unit               => $unit->name,
115                   ),
116                   SL::Dev::Record::create_delivery_order_item(
117                     part            => $parts[1],
118                     discount        => '0.25',
119                     lastcost        => '153.00000',
120                     qty             => '3.00000',
121                     sellprice       => '344.30000',
122                     transdate       => '06.03.2015',
123                     unit            => $unit->name,
124                   )
125                 ]
126 );
127
128
129 # TESTS
130
131 my $do1_item1 = $do1->orderitems->[0];
132 my $do1_item2 = $do1->orderitems->[1];
133
134 # test delivery order before any conversion
135 ok($do1->donumber eq "L20199", 'Delivery Order Number created');
136 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>&nbsp;</li></ul>', "do RichText notes saved");
137 ok((not $do1->closed) , 'Delivery Order is not closed');
138 is($do1_item1->parts_id, $parts[0]->id, 'doi linked with part');
139 ok($do1_item1->qty == 2, 'qty check doi');
140 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>",
141      "do item1 rich text longdescripition");
142 ok ($do1_item2->position == 2, 'doi2 position check');
143 is (SL::DB::Manager::DeliveryOrderItem->get_all_count(where => [ delivery_order_id => $do1->id ]), 2 , 'two doi linked');
144
145
146 # convert this do to invoice
147 my $invoice = $do1->convert_to_invoice();
148
149 sleep (300) if $VISUAL_TEST; # we can do a real visual test via gui login
150 # test invoice afterwards
151
152 ok ($invoice->shipvia eq "DHL, Versand am 06.03.2015, 1 Paket  17,00 kg", "ship via check");
153 ok ($invoice->shippingpoint eq "sendtome", "shipping point check");
154 ok ($invoice->ordnumber eq "A16399", "ordnumber check");
155 ok ($invoice->donumber eq "L20199", "donumber check");
156 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>&nbsp;</li></ul>', "do RichText notes saved");
157 ok(($do1->closed) , 'Delivery Order is closed after conversion');
158 is($invoice->payment_terms->description, "14Tage 2%Skonto, 30Tage netto", 'payment term description check');
159
160 # some test data from original client invoice console (!)
161 # my $invoice3 = SL::DB::Manager::Invoice->find_by( ordnumber => 'A16399' );
162 # which will fail due to PTC Calculation differs from GUI-Calculation, see issue: http://redmine.kivitendo-premium.de/issues/82
163 # pp $invoice3
164 # values from gui should be:
165 #ok($invoice->amount == 1354.20000, 'amount check');
166 #ok($invoice->marge_percent == 50.88666, 'marge percent check');
167 #ok($invoice->marge_total == 579.08000, 'marge total check');
168 #ok($invoice->netamount == 1137.98000, 'netamount check');
169
170
171 # the values change if one reloads the object
172 # without reloading we get this failures
173 #not ok 17 - amount check
174 #   Failed test 'amount check'
175 #   at t/db_helper/convert_invoice.t line 272.
176 #          got: '1354.17'
177 #     expected: '1354.17000'
178 #not ok 18 - marge percent check
179 #   Failed test 'marge percent check'
180 #   at t/db_helper/convert_invoice.t line 273.
181 #          got: '50.8857956342929'
182 #     expected: '50.88580'
183 #not ok 19 - marge total check
184 #   Failed test 'marge total check'
185 #   at t/db_helper/convert_invoice.t line 274.
186 #          got: '579.06'
187 #     expected: '579.06000'
188 #not ok 20 - netamount check
189 #   Failed test 'netamount check'
190 #   at t/db_helper/convert_invoice.t line 275.
191 #          got: '1137.96'
192 #     expected: '1137.96000'
193
194 $invoice->load;
195
196 is($invoice->cusordnumber            , 'b84da'           , 'cusordnumber check');
197 is($invoice->department->description , "Test Department" , 'department description ok');
198 is($invoice->amount                  , '1354.17000'      , 'amount check');
199 is($invoice->marge_percent           , '50.88580'        , 'marge percent check');
200 is($invoice->marge_total             , '579.06000'       , 'marge total check');
201 is($invoice->netamount               , '1137.96000'      , 'netamount check');
202
203 # some item checks
204 is($invoice->items_sorted->[0]->parts_id         , $parts[0]->id , 'invoiceitem 1 linked with part');
205 is(scalar @{ $invoice->invoiceitems }            , 2             , 'two invoice items linked with invoice');
206 is($invoice->items_sorted->[0]->position         , 1             , "position 1 order correct");
207 is($invoice->items_sorted->[1]->position         , 2             , "position 2 order correct");
208 is($invoice->items_sorted->[0]->part->partnumber , 'v-519160549' , "partnumber 1 correct");
209 is($invoice->items_sorted->[1]->part->partnumber , 'v-120160086' , "partnumber 2 correct");
210 is($invoice->items_sorted->[0]->qty              , '2.00000'     , "pos 1 qty");
211 is($invoice->items_sorted->[1]->qty              , '3.00000'     , "pos 2 qty");
212 is($invoice->items_sorted->[0]->discount         , 0.25          , "pos 1 discount");
213 is($invoice->items_sorted->[1]->discount         , 0.25          , "pos 2 discount");
214 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>",
215      "invoice item1 rich text longdescripition");
216 # more ideas: check onhand, lastcost (parsed lastcost)
217
218
219
220 # check linked records AND linked items
221
222 # we expect something like this in record links:
223 # delivery_order_items |  144736 | invoice  |     9 | 2015-09-02 16:29:32.362562 |  5
224 # delivery_order_items |  144737 | invoice  |    10 | 2015-09-02 16:29:32.362562 |  6
225 # delivery_orders      |  464003 | ar       |     5 | 2015-09-02 16:29:32.362562 |  7
226 # wir erwarten:
227 # verkn�pfter beleg$VAR1 = {
228 #           'from_id' => 464003,
229 #           'from_table' => 'delivery_orders',
230 #           'to_id' => 11,
231 #           'to_table' => 'ar'
232 #         };
233 # verkn�pfte positionen$VAR1 = {
234 #           'from_id' => 144737,
235 #           'from_table' => 'delivery_order_items',
236 #           'to_id' => 22,
237 #           'to_table' => 'invoice'
238 #         };
239 # $VAR2 = {
240 #           'from_id' => 144736,
241 #           'from_table' => 'delivery_order_items',
242 #           'to_id' => 21,
243 #           'to_table' => 'invoice'
244 #         };
245
246
247 my @links_record    = RecordLinks->get_links('from_table' => 'delivery_orders',
248                                              'to_table'   => 'ar',
249                                              'from_id'    => $do1->id,
250                                             );
251
252 is($links_record[0]->{from_id}    , $do1->id          , "record from id check");
253 is($links_record[0]->{from_table} , 'delivery_orders' , "record from table check");
254 is($links_record[0]->{to_table}   , 'ar'              , "record to table check");
255
256 foreach ( $do1_item1->id, $do1_item2->id ) {
257   my @links_record_item1 = RecordLinks->get_links('from_table' => 'delivery_order_items',
258                                                   'to_table'   => 'invoice',
259                                                   'from_id'    => $_,
260                                                  );
261
262   is($links_record_item1[0]->{from_id}    , $_                     , "record from id check $_");
263   is($links_record_item1[0]->{from_table} , 'delivery_order_items' , "record from table check $_");
264   is($links_record_item1[0]->{to_table}   , 'invoice'              , "record to table check $_");
265 }
266
267 clear_up();
268
269 1;