Merge branch 'master' of github.com:kivitendo/kivitendo-erp
[kivitendo-erp.git] / t / db_helper / record_links.t
1 use Test::More tests => 49;
2
3 use strict;
4
5 use lib 't';
6 use utf8;
7
8 use Carp;
9 use Data::Dumper;
10 use Support::TestSetup;
11 use Test::Exception;
12 use List::Util qw(max);
13
14 use SL::DB::Buchungsgruppe;
15 use SL::DB::Currency;
16 use SL::DB::Customer;
17 use SL::DB::Employee;
18 use SL::DB::Invoice;
19 use SL::DB::Order;
20 use SL::DB::DeliveryOrder;
21 use SL::DB::Part;
22 use SL::DB::Unit;
23 use SL::DB::TaxZone;
24
25 my ($customer, $currency_id, $buchungsgruppe, $employee, $vendor, $taxzone);
26 my ($link, $links, $o1, $o2, $d, $i);
27
28 sub reset_state {
29   my %params = @_;
30
31   $params{$_} ||= {} for qw(buchungsgruppe unit customer part tax);
32
33   SL::DB::Manager::DeliveryOrder->delete_all(all => 1);
34   SL::DB::Manager::Order->delete_all(all => 1);
35   SL::DB::Manager::Invoice->delete_all(all => 1);
36   SL::DB::Manager::Customer->delete_all(all => 1);
37   SL::DB::Manager::Vendor->delete_all(all => 1);
38
39   $buchungsgruppe  = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 19%', %{ $params{buchungsgruppe} }) || croak "No accounting group";
40   $employee        = SL::DB::Manager::Employee->current                                                                    || croak "No employee";
41   $taxzone         = SL::DB::Manager::TaxZone->find_by( description => 'Inland')                                           || croak "No taxzone";
42
43   $currency_id     = $::instance_conf->get_currency_id;
44
45   $customer     = SL::DB::Customer->new(
46     name        => 'Test Customer',
47     currency_id => $currency_id,
48     %{ $params{customer} }
49   )->save;
50
51   $vendor     = SL::DB::Vendor->new(
52     name        => 'Test Vendor',
53     currency_id => $currency_id,
54     %{ $params{vendor} }
55   )->save;
56 }
57
58 sub new_order {
59   my %params  = @_;
60
61   return SL::DB::Order->new(
62     customer_id => $customer->id,
63     currency_id => $currency_id,
64     employee_id => $employee->id,
65     salesman_id => $employee->id,
66     taxzone_id  => $taxzone->id,
67     quotation   => 0,
68     %params,
69   )->save;
70 }
71
72 sub new_delivery_order {
73   my %params  = @_;
74
75   return SL::DB::DeliveryOrder->new(
76     customer_id => $customer->id,
77     currency_id => $currency_id,
78     employee_id => $employee->id,
79     salesman_id => $employee->id,
80     taxzone_id  => $taxzone->id,
81     %params,
82   )->save;
83 }
84
85 sub new_invoice {
86   my %params  = @_;
87
88   return SL::DB::Invoice->new(
89     customer_id => $customer->id,
90     currency_id => $currency_id,
91     employee_id => $employee->id,
92     salesman_id => $employee->id,
93     gldate      => DateTime->today_local->to_kivitendo,
94     invoice     => 1,
95     taxzone_id  => $taxzone->id,
96     type        => 'invoice',
97     %params,
98   )->save;
99 }
100
101 Support::TestSetup::login();
102
103 reset_state();
104
105
106 $o1 = new_order();
107 $i  = new_invoice();
108
109 $link = $o1->link_to_record($i);
110
111 # try to add a link
112 is ref $link, 'SL::DB::RecordLink', 'link_to_record returns new link';
113 is $link->from_table, 'oe', 'from_table';
114 is $link->from_id, $o1->id, 'from_id';
115 is $link->to_table, 'ar', 'to_table';
116 is $link->to_id, $i->id, 'to_id';
117
118 # retrieve link
119 $links = $o1->linked_records;
120 is $links->[0]->id, $i->id, 'simple retrieve';
121
122 $links = $o1->linked_records(direction => 'to', to => 'Invoice');
123 is $links->[0]->id, $i->id, 'direct retrieve 1';
124
125 $links = $o1->linked_records(direction => 'to', to => 'SL::DB::Invoice');
126 is $links->[0]->id, $i->id, 'direct retrieve 2 (with SL::DB::)';
127
128 $links = $o1->linked_records(direction => 'to', to => [ 'Invoice', 'Order' ]);
129 is $links->[0]->id, $i->id, 'direct retrieve 3 (array target)';
130
131 $links = $o1->linked_records(direction => 'both', both => 'Invoice');
132 is $links->[0]->id, $i->id, 'direct retrieve 4 (direction both)';
133
134 $links = $i->linked_records(direction => 'from', from => 'Order');
135 is $links->[0]->id, $o1->id, 'direct retrieve 4 (direction from)';
136
137 # what happens if we delete a linked record?
138 $o1->delete;
139
140 $links = $i->linked_records(direction => 'from', from => 'Order');
141 is @$links, 0, 'no dangling link after delete';
142
143 # can we distinguish between types?
144 $o1 = new_order(quotation => 1);
145 $o2 = new_order();
146 $o1->link_to_record($o2);
147
148 $links = $o2->linked_records(direction => 'from', from => 'Order', query => [ quotation => 1 ]);
149 is $links->[0]->id, $o1->id, 'query restricted retrieve 1';
150
151 $links = $o2->linked_records(direction => 'from', from => 'Order', query => [ quotation => 0 ]);
152 is @$links, 0, 'query restricted retrieve 2';
153
154 # try bidirectional linking
155 $o1 = new_order();
156 $o2 = new_order();
157 $o1->link_to_record($o2, bidirectional => 1);
158
159 $links = $o1->linked_records(direction => 'to', to => 'Order');
160 is $links->[0]->id, $o2->id, 'bidi 1';
161 $links = $o1->linked_records(direction => 'from', from => 'Order');
162 is $links->[0]->id, $o2->id, 'bidi 2';
163 $links = $o1->linked_records(direction => 'both', both => 'Order');
164 is $links->[0]->id, $o2->id, 'bidi 3';
165
166 # funky stuff with both
167 #
168 $d = new_delivery_order();
169 $i = new_invoice();
170
171 $o2->link_to_record($d);
172 $d->link_to_record($i);
173 # at this point the structure is:
174 #
175 #   o1 <--> o2 ---> d ---> i
176 #
177
178 $links = $d->linked_records(direction => 'both', to => 'Invoice', from => 'Order', sort_by => 'customer_id', sort_dir => 1);
179 is $links->[0]->id, $o2->id, 'both with different from/to 1';
180 is $links->[1]->id, $i->id,  'both with different from/to 2';
181
182 # what happens if we double link?
183 #
184 $o2->link_to_record($d);
185
186 $links = $o2->linked_records(direction => 'to', to => 'DeliveryOrder');
187 is @$links, 1, 'double link is only added once 1';
188
189 $d->link_to_record($o2, bidirectional => 1);
190 # at this point the structure is:
191 #
192 #   o1 <--> o2 <--> d ---> i
193 #
194
195 $links = $o2->linked_records(direction => 'to', to => 'DeliveryOrder');
196 is @$links, 1, 'double link is only added once 2';
197
198 # doc states that to/from ae optional. test that
199 $links = $o2->linked_records(direction => 'both');
200 is @$links, 2, 'links without from/to get all';
201
202 # doc states you can limit with direction when giving excess params
203 $links = $d->linked_records(direction => 'to', to => 'Invoice', from => 'Order');
204 is $links->[0]->id, $i->id, 'direction to limit params  1';
205 is @$links, 1, 'direction to limit params 2';
206
207 # doc says there will be special values set... lets see
208 $links = $o1->linked_records(direction => 'to', to => 'Order');
209 is $links->[0]->{_record_link_direction}, 'to',  '_record_link_direction to';
210 is $links->[0]->{_record_link}->to_id, $o2->id,  '_record_link to';
211
212 $links = $o1->linked_records(direction => 'from', from => 'Order');
213 is $links->[0]->{_record_link_direction}, 'from',  '_record_link_direction from';
214 is $links->[0]->{_record_link}->to_id, $o1->id,  '_record_link from';
215
216 # check if bidi returns an array of links even if aready existing
217 my @links = $d->link_to_record($o2, bidirectional => 1);
218 # at this point the structure is:
219 #
220 #   o1 <--> o2 <--> d ---> i
221 #
222 is @links, 2, 'bidi returns array of links in array context';
223
224 #  via
225 $links = $o2->linked_records(direction => 'to', to => 'Invoice', via => 'DeliveryOrder');
226 is $links->[0]->id, $i->id,  'simple case via links (string)';
227
228 $links = $o2->linked_records(direction => 'to', to => 'Invoice', via => [ 'DeliveryOrder' ]);
229 is $links->[0]->id, $i->id,  'simple case via links (arrayref)';
230
231 $links = $o1->linked_records(direction => 'to', to => 'Invoice', via => [ 'Order', 'DeliveryOrder' ]);
232 is $links->[0]->id, $i->id,  'simple case via links (2 hops)';
233
234 # multiple links in the same direction from one object
235 $o1->link_to_record($d);
236 # at this point the structure is:
237 #
238 #   o1 <--> o2 <--> d ---> i
239 #     \____________,^
240 #
241
242 $links = $o2->linked_records(direction => 'to', to => 'Invoice', via => 'DeliveryOrder');
243 is $links->[0]->id, $i->id,  'simple case via links (string)';
244
245
246 # o1 must have 2 linked records now:
247 $links = $o1->linked_records(direction => 'to');
248 is @$links, 2,  'more than one link';
249
250 # as a special funny case, o1 via Order, Order will now yield o2, because it bounces back over itself
251 { local $TODO = 'no idea if this is desired';
252 $links = $o2->linked_records(direction => 'to', to => 'Order', via => [ 'Order', 'Order' ]);
253 is @$links, 2,  'via links with bidirectional hop over starting object';
254 }
255
256 # for sorting, get all don't bother with the links, we'll just take our records
257 my @records = ($o2, $i, $o1, $d);
258 my $sorted;
259 $sorted = SL::DB::Helper::LinkedRecords->sort_linked_records('type', 1, @records);
260 is_deeply $sorted, [$o1, $o2, $d, $i], 'sorting by type';
261 $sorted = SL::DB::Helper::LinkedRecords->sort_linked_records('type', 0, @records);
262 is_deeply $sorted, [$i, $d, $o2, $o1], 'sorting by type desc';
263
264 $d->donumber(1);
265 $o1->ordnumber(2);
266 $i->invnumber(3);
267 $o2->ordnumber(4);
268
269 $sorted = SL::DB::Helper::LinkedRecords->sort_linked_records('number', 1, @records);
270 is_deeply $sorted, [$d, $o1, $i, $o2], 'sorting by number';
271 $sorted = SL::DB::Helper::LinkedRecords->sort_linked_records('number', 0, @records);
272 is_deeply $sorted, [$o2, $i, $o1, $d], 'sorting by number desc';
273
274 # again with natural sorting
275 $d->donumber("a1");
276 $o1->ordnumber("a3");
277 $i->invnumber("a7");
278 $o2->ordnumber("a10");
279
280 $sorted = SL::DB::Helper::LinkedRecords->sort_linked_records('number', 1, @records);
281 is_deeply $sorted, [$d, $o1, $i, $o2], 'sorting naturally by number';
282 $sorted = SL::DB::Helper::LinkedRecords->sort_linked_records('number', 0, @records);
283 is_deeply $sorted, [$o2, $i, $o1, $d], 'sorting naturally by number desc';
284
285 $o2->transdate(DateTime->new(year => 2010, month => 3, day => 1));
286 $i->transdate(DateTime->new(year => 2014, month => 3, day => 19));
287 $o1->transdate(DateTime->new(year => 2014, month => 5, day => 1));
288 $d->transdate(DateTime->new(year => 2014, month => 5, day => 2));
289
290 # transdate should be used before itime
291 $sorted = SL::DB::Helper::LinkedRecords->sort_linked_records('date', 1, @records);
292 is_deeply $sorted, [$o2, $i, $o1, $d], 'sorting by transdate';
293 $sorted = SL::DB::Helper::LinkedRecords->sort_linked_records('date', 0, @records);
294 is_deeply $sorted, [$d, $o1, $i, $o2], 'sorting by transdate desc';
295
296 # now recursive stuff 2, with backlinks
297 $links = $o1->linked_records(direction => 'to', recursive => 1, save_path => 1);
298 is @$links, 4, 'recursive finds all 4 (backlink to self because of bidi o1<->o2)';
299
300 # because of the link o1->d the longest path should be legth 2. test that
301 is max(map { $_->{_record_link_depth} } @$links), 2, 'longest path is 2';
302
303 $links = $o2->linked_records(direction => 'to', recursive => 1);
304 is @$links, 4, 'recursive from o2 finds 4';
305
306 $links = $o1->linked_records(direction => 'from', recursive => 1, save_path => 1);
307 is @$links, 3, 'recursive from o1 finds 3 (not i)';
308
309 $links = $i->linked_records(direction => 'from', recursive => 1, save_path => 1);
310 is @$links, 3, 'recursive from i finds 3 (not i)';
311
312 $links = $o1->linked_records(direction => 'both', recursive => 1, save_path => 1);
313 is @$links, 4, 'recursive dir=both does not give duplicates';
314 1;