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