test action
[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 qw(:ALL);
26
27 my ($customer, $employee, $payment_do, $unit, @parts, $department);
28 my ($transdate);
29
30 my $VISUAL_TEST = 0;  # just a sleep to click around
31
32 sub clear_up {
33   foreach (qw(DeliveryOrderItem DeliveryOrder InvoiceItem Invoice Part Customer Department PaymentTerm)) {
34     "SL::DB::Manager::${_}"->delete_all(all => 1);
35   }
36   SL::DB::Manager::Employee->delete_all(where => [ login => 'testuser' ]);
37 };
38
39 sub reset_state {
40   my %params = @_;
41
42   clear_up();
43
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
46
47   $unit     = SL::DB::Manager::Unit->find_by(name => 'kg') || die "Can't find unit 'kg'";
48   $customer = new_customer()->save;
49
50   $employee = SL::DB::Employee->new(
51     'login' => 'testuser',
52     'name'  => 'Test User',
53   )->save;
54
55   $department = SL::DB::Department->new(
56     'description' => 'Test Department',
57   )->save;
58
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',
63      'terms_netto'      => 30,
64      'terms_skonto'     => 14
65   );
66
67   # two real parts
68   @parts = ();
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',
75     unit        => $unit->name,
76     weight      => '0.79',
77   )->save;
78
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',
85     unit        => $unit->name,
86     weight      => '0.9',
87   )->save;
88
89 }
90
91 Support::TestSetup::login();
92
93 reset_state();
94
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>&nbsp;</li></ul>',
110   orderitems => [
111                   create_delivery_order_item(
112                     part               => $parts[0],
113                     discount           => '0.25',
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,
117                     qty                => '2.00000',
118                     sellprice          => '242.20000',
119                     unit               => $unit->name,
120                   ),
121                   create_delivery_order_item(
122                     part            => $parts[1],
123                     discount        => '0.25',
124                     lastcost        => '153.00000',
125                     qty             => '3.00000',
126                     sellprice       => '344.30000',
127                     transdate       => '06.03.2015',
128                     unit            => $unit->name,
129                   )
130                 ]
131 );
132
133
134 # TESTS
135
136 my $do1_item1 = $do1->orderitems->[0];
137 my $do1_item2 = $do1->orderitems->[1];
138
139 # test delivery order before any conversion
140 ok($do1->donumber eq "L20199", 'Delivery Order Number created');
141 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");
142 ok((not $do1->closed) , 'Delivery Order is not closed');
143 is($do1_item1->parts_id, $parts[0]->id, 'doi linked with part');
144 ok($do1_item1->qty == 2, 'qty check doi');
145 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>",
146      "do item1 rich text longdescripition");
147 ok ($do1_item2->position == 2, 'doi2 position check');
148 is (SL::DB::Manager::DeliveryOrderItem->get_all_count(where => [ delivery_order_id => $do1->id ]), 2 , 'two doi linked');
149
150
151 # convert this do to invoice
152 my $invoice = $do1->convert_to_invoice(transdate => $transdate);
153
154 sleep (300) if $VISUAL_TEST; # we can do a real visual test via gui login
155 # test invoice afterwards
156
157 ok ($invoice->shipvia eq "DHL, Versand am 06.03.2015, 1 Paket  17,00 kg", "ship via check");
158 ok ($invoice->shippingpoint eq "sendtome", "shipping point check");
159 ok ($invoice->ordnumber eq "A16399", "ordnumber check");
160 ok ($invoice->donumber eq "L20199", "donumber check");
161 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");
162 ok(($do1->closed) , 'Delivery Order is closed after conversion');
163 is($invoice->payment_terms->description, "14Tage 2%Skonto, 30Tage netto", 'payment term description check');
164
165 $invoice->load;
166
167 is($invoice->cusordnumber            , 'b84da'           , 'cusordnumber check');
168 is($invoice->department->description , "Test Department" , 'department description ok');
169 is($invoice->amount                  , '1354.20000'      , 'amount check');
170 is($invoice->marge_percent           , '50.88666'        , 'marge percent check');
171 is($invoice->marge_total             , '579.08000'       , 'marge total check');
172 is($invoice->netamount               , '1137.98000'      , 'netamount check');
173
174 # some item checks
175 is($invoice->items_sorted->[0]->parts_id         , $parts[0]->id , 'invoiceitem 1 linked with part');
176 is(scalar @{ $invoice->invoiceitems }            , 2             , 'two invoice items linked with invoice');
177 is($invoice->items_sorted->[0]->position         , 1             , "position 1 order correct");
178 is($invoice->items_sorted->[1]->position         , 2             , "position 2 order correct");
179 is($invoice->items_sorted->[0]->part->partnumber , 'v-519160549' , "partnumber 1 correct");
180 is($invoice->items_sorted->[1]->part->partnumber , 'v-120160086' , "partnumber 2 correct");
181 is($invoice->items_sorted->[0]->qty              , '2.00000'     , "pos 1 qty");
182 is($invoice->items_sorted->[1]->qty              , '3.00000'     , "pos 2 qty");
183 is($invoice->items_sorted->[0]->discount         , 0.25          , "pos 1 discount");
184 is($invoice->items_sorted->[1]->discount         , 0.25          , "pos 2 discount");
185 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>",
186      "invoice item1 rich text longdescripition");
187 # more ideas: check onhand, lastcost (parsed lastcost)
188
189
190
191 # check linked records AND linked items
192
193 # we expect something like this in record links:
194 # delivery_order_items |  144736 | invoice  |     9 | 2015-09-02 16:29:32.362562 |  5
195 # delivery_order_items |  144737 | invoice  |    10 | 2015-09-02 16:29:32.362562 |  6
196 # delivery_orders      |  464003 | ar       |     5 | 2015-09-02 16:29:32.362562 |  7
197 # wir erwarten:
198 # verkn�pfter beleg$VAR1 = {
199 #           'from_id' => 464003,
200 #           'from_table' => 'delivery_orders',
201 #           'to_id' => 11,
202 #           'to_table' => 'ar'
203 #         };
204 # verkn�pfte positionen$VAR1 = {
205 #           'from_id' => 144737,
206 #           'from_table' => 'delivery_order_items',
207 #           'to_id' => 22,
208 #           'to_table' => 'invoice'
209 #         };
210 # $VAR2 = {
211 #           'from_id' => 144736,
212 #           'from_table' => 'delivery_order_items',
213 #           'to_id' => 21,
214 #           'to_table' => 'invoice'
215 #         };
216
217
218 my @links_record    = RecordLinks->get_links('from_table' => 'delivery_orders',
219                                              'to_table'   => 'ar',
220                                              'from_id'    => $do1->id,
221                                             );
222
223 is($links_record[0]->{from_id}    , $do1->id          , "record from id check");
224 is($links_record[0]->{from_table} , 'delivery_orders' , "record from table check");
225 is($links_record[0]->{to_table}   , 'ar'              , "record to table check");
226
227 foreach ( $do1_item1->id, $do1_item2->id ) {
228   my @links_record_item1 = RecordLinks->get_links('from_table' => 'delivery_order_items',
229                                                   'to_table'   => 'invoice',
230                                                   'from_id'    => $_,
231                                                  );
232
233   is($links_record_item1[0]->{from_id}    , $_                     , "record from id check $_");
234   is($links_record_item1[0]->{from_table} , 'delivery_order_items' , "record from table check $_");
235   is($links_record_item1[0]->{to_table}   , 'invoice'              , "record to table check $_");
236 }
237
238 clear_up();
239
240 1;
241
242 # vim: ft=perl
243 # set emacs to perl mode
244 # Local Variables:
245 # mode: perl
246 # End: