OOM/Object/Base.pm

filter: Statementstmt 12.1% 30 / 248 Branchbran 0.0% 0 / 98 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 23.0% 9 / 39 Podpod 100% 18 / 18 totaltotal 12.8% 57 / 443 CC 64 SCAR 42.4
CC
64
Cov
8%
create_related
210.0
update
156.0
search_related
110.0
CRAP 69.3
↑ Summary OOM/Class/Base.pm »
1 package OpenERP::OOM::Object::Base;
2  
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use List::MoreUtils qw/uniq/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Try::Tiny::Retry;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Time::HiRes qw/usleep/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Switch::Plain;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 extends 'Moose::Object';
14 2 with 'OpenERP::OOM::DynamicUtils';
15  
16 =head1 NAME
17  
18 OpenERP::OOM::Class::Base
19  
20 =head1 SYNOPSYS
21  
22 my $obj = $schema->class('Name')->create(\%args);
23  
24 :say $obj->id;
25  
26 $obj->name('New name');
27 $obj->update;
28  
29 $obj->delete;
30  
31 =head1 DESCRIPTION
32  
33 Provides a base set of properties and methods for OpenERP::OOM objects (update, delete, etc).
34  
35 =head1 PROPERTIES
36  
37 =head2 id
38  
39 Returns the OpenERP ID of an object.
40  
41 say $obj->id;
42  
43 =head2 BUILD
44  
45 The BUILD method sets up the methods for the links to the attached objects.
46  
47 =cut
48  
49 2 has 'id' => (
50 isa => 'Int',
51 is => 'ro',
52 );
53  
54 0 ▶sub BUILD {
Subroutine BUILD: not called CC=4 SCAR=30.0
Pod documented
55 0 my $self = shift;
56  
57 # Add methods to follow links
58 0 my $links = $self->meta->link;
59 0 ▶ while (my ($name, $link) = each %$links) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$links) 0 0 0
60 sswitch ($link->{type}) {
61 case ('single'): {
62 $self->meta->add_method(
63 $name,
64 0 ▶ sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
65 0 my $obj = shift;
66 0 ▶ $obj->{"_$name"} //= $obj->class->schema->link($link->{class})->retrieve($link->{args}, $obj->{$link->{key}});
Condition0%0/3
$$obj{"_$name"} //= $obj->class->schema->link($link->{"class"})->retrieve($link->{"args"}, $$obj{$link->{"key"}})
l!l&&r!l&&!r
000
MC/DC0%0/2
$$obj{"_$name"} //= $obj->class->schema->link($link->{"class"})->retrieve($link->{"args"}, $$obj{$link->{"key"}})
$$obj{"_$name"} $obj->class->schema->link($link->{"class"})->retrieve($link->{"args"}, $$obj{$link->{"key"}})
Truth table0%0/3
A //= B
A= $$obj{"_$name"}B= $obj->class->schema->link($link->{"class"})->retrieve($link->{"args"}, $$obj{$link->{"key"}})
ABresult
1X1
011
000
67  
68 0 ▶ unless ($obj->{"_$name"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$obj{"_$name"}) 0 0 0
69 # FIXME: If $obj->{"_$name"} is undefined, we have a data integrity problem.
70 # Either the linked data is missing, or the key in the OpenERP object is missing.
71 0 die "Error linking to OpenERP object " . $obj->id . " of class " . ref($obj);
72 }
73  
74 # NOTE: this only links up the object from the linked object
75 # if it has a _source attribute
76 #
77 # has _source => (is => 'rw');
78  
79 0 ▶ if ($obj->{"_$name"}->can('_source')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$obj{"_$name"}->can("_source")) 0 0 0
80 # set the _source attribute to point back
81 # to the linked object.
82 0 $obj->{"_$name"}->_source($obj);
83 }
84  
85 0 return $obj->{"_$name"};
86 }
87 )
88 0 ▶ }
Condition0%0/3
$_ eq "multiple" and do { $self->meta->add_method($name, sub { return $self->class->schema->link($link->{"class"})->retrieve_list($link->{"args"}, $$self{$link->{"key"}}); } ) }
!ll&&!rl&&r
000
MC/DC0%0/2
$_ eq "multiple" and do { $self->meta->add_method($name, sub { return $self->class->schema->link($link->{"class"})->retrieve_list($link->{"args"}, $$self{$link->{"key"}}); } ) }
$_ eq "multiple" do { $self->meta->add_method($name, sub { return $self->class->schema->link($link->{"class"})->retrieve_list($link->{"args"}, $$self{$link->{"key"}}); } ) }
Truth table0%0/3
A and B
A= $_ eq "multiple"B= do { $self->meta->add_method($name, sub { return $self->class->schema->link($link->{"class"})->retrieve_list($link->{"args"}, $$self{$link->{"key"}}); } ) }
ABresult
0X0
100
111
89 0 ▶ case ('multiple'): {
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'single' ? : 0 0 0
90 $self->meta->add_method(
91 $name,
92 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
93 0 return $self->class->schema->link($link->{class})->retrieve_list($link->{args}, $self->{$link->{key}});
94 }
95 )
96 0 }
97 0 }
98 }
99 }
100  
101  
102 #-------------------------------------------------------------------------------
103  
104 =head1 METHODS
105  
106 =head2 update
107  
108 Updates an object in OpenERP after its properties have been changed.
109  
110 $obj->name('New name');
111 $obj->update;
112  
113 Also allows a hashref to be passed to update multiple properties:
114  
115 $obj->update({
116 name => 'new name',
117 ref => 'new reference',
118 price => 'new price',
119 });
120  
121 =cut
122  
123 0 ▶sub update {
Subroutine update: not called CC=12 SCAR=50.5
Pod documented
124 0 my $self = shift;
125  
126 0 ▶ if (my $update = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $update = shift()) 0 0 0
127 0 ▶ while (my ($param, $value) = each %$update) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$update) 0 0 0
128 0 $self->$param($value);
129 }
130 }
131 0 my $context = $self->class->_get_context(shift);
132  
133 0 my $object;
134 0 foreach my $attribute ($self->dirty_attributes) {
135 0 ▶ next if ($attribute eq 'id');
Branch: 1 branch
BranchTrueFalseTotal
if $attribute eq "id" 0 0 0
136 0 ▶ next if ($attribute =~ '^_');
Branch: 1 branch
BranchTrueFalseTotal
if $attribute =~ /^_/ 0 0 0
137  
138 0 $object->{$attribute} = $self->{$attribute};
139 }
140  
141 0 my $relationships = $self->meta->relationship;
142 0 ▶ while (my ($name, $rel) = each %$relationships) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$relationships) 0 0 0
143 0 ▶ if ($object->{$rel->{key}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$object{$rel->{"key"}}) 0 0 0
144 sswitch ($rel->{type}) {
145 case ('one2many'): {
146 0 ▶ delete $object->{$rel->{key}}; # Don't update one2many relationships
Condition0%0/3
$_ eq "many2many" and do { $$object{$rel->{"key"}} = [[6, 0, $$object{$rel->{"key"}}]] }
!ll&&!rl&&r
000
MC/DC0%0/2
$_ eq "many2many" and do { $$object{$rel->{"key"}} = [[6, 0, $$object{$rel->{"key"}}]] }
$_ eq "many2many" do { $$object{$rel->{"key"}} = [[6, 0, $$object{$rel->{"key"}}]] }
Truth table0%0/3
A and B
A= $_ eq "many2many"B= do { $$object{$rel->{"key"}} = [[6, 0, $$object{$rel->{"key"}}]] }
ABresult
0X0
100
111
147 }
148 0 ▶ case ('many2many'): {
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'one2many' ? : 0 0 0
149 0 $object->{$rel->{key}} = [[6,0,$object->{$rel->{key}}]];
150 }
151 0 }
152 }
153 }
154  
155 # Force Str parameters to be object type RPC::XML::string
156 0 foreach my $attribute ($self->meta->get_all_attributes) {
157 0 ▶ if (exists $object->{$attribute->name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$object{$attribute->name}) 0 0 0
158 0 $object->{$attribute->name} = $self->prepare_attribute_for_send($attribute->type_constraint, $object->{$attribute->name});
159 }
160 }
161  
162 0 ▶ $self->class->_with_retries(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
163 0 $self->class->schema->client->object_execute_kw(
164 'write',
165 $self->model,
166 [ # positional args
167 $self->id,
168 $object,
169 ],
170 { # keyword args
171 context => $context,
172 },
173 );
174 0 });
175 0 $self->refresh;
176  
177 0 return $self;
178 }
179  
180 #-------------------------------------------------------------------------------
181  
182 =head2 update_single
183  
184 Updates OpenERP with a single property of an object.
185  
186 $obj->name('New name');
187 $obj->status('Active');
188  
189 $obj->update_single('name'); # Only the 'name' property is updated
190  
191 =cut
192  
193 0 ▶sub update_single {
Subroutine update_single: not called CC=5 SCAR=34.0
Pod documented
194 0 my ($self, $property) = @_;
195 0 my $value = $self->{$property};
196  
197 # Check to see if the property is the key to a many2many relationship
198 0 my $relationships = $self->meta->relationship;
199 0 my ($key) = grep { $relationships->{$_}->{key} eq $property } keys %$relationships;
200 0 ▶ if($key)
Branch: 1 branch
BranchTrueFalseTotal
if ($key) 0 0 0
201 {
202 0 my $rel = $relationships->{$key};
203 0 ▶ if ($rel->{type} eq 'many2many') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel->{"type"} eq "many2many") 0 0 0
204 0 $value = [[6,0,$value]];
205 }
206 }
207  
208 # Force Str parameters to be object type RPC::XML::string
209 0 foreach my $attribute ($self->meta->get_all_attributes) {
210 0 ▶ if ($attribute->name eq $property) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute->name eq $property) 0 0 0
211 0 $value = $self->prepare_attribute_for_send($attribute->type_constraint, $value);
212 }
213 }
214  
215 0 $self->class->schema->client->update($self->model, $self->id, {$property => $value});
216 0 return $self;
217 }
218  
219 #-------------------------------------------------------------------------------
220  
221 =head2 refresh
222  
223 Reloads an object's properties from OpenERP.
224  
225 $obj->refresh;
226  
227 =cut
228  
229 0 ▶sub refresh {
Subroutine refresh: not called CC=2 SCAR=17.9
Pod documented
230 0 my $self = shift;
231  
232 0 my $new = $self->class->retrieve($self->id);
233  
234 0 foreach my $attribute ($self->meta->get_all_attributes) {
235 0 my $name = $attribute->name;
236 0 $self->{$name} = ($new->$name);
237 }
238 0 $self->mark_all_clean; # reset the dirty attribute
239  
240 0 return $self;
241 }
242  
243  
244 #-------------------------------------------------------------------------------
245  
246 =head2 delete
247  
248 Deletes an object from OpenERP.
249  
250 my $obj = $schema->class('Partner')->retrieve(60);
251 $obj->delete;
252  
253 =cut
254  
255 0 ▶sub delete {
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
256 0 my $self = shift;
257  
258 0 $self->class->schema->client->delete($self->model, $self->id);
259 }
260  
261 sub _copy
262 0 ▶{
Subroutine _copy: not called CC=1 SCAR=6.9
263 0 my $self = shift;
264  
265 0 my $id = $self->class->schema->client->copy($self->model, $self->id);
266 # now load the new invoice and return it
267 0 return $id;
268 }
269  
270 =head2 copy
271  
272 Clone the current object, returning the new object.
273  
274 This is equivalent to pressing duplicate in the OpenERP user interface.
275  
276 =cut
277  
278 sub copy
279 0 ▶{
Subroutine copy: not called CC=1 SCAR=6.9
Pod documented
280 0 my ($self, @args) = @_;
281 0 my $args = shift;
282 0 my $id = $self->_copy;
283 # passing args through allows for field refinement.
284 0 my $clone = $self->class->retrieve($id, @args);
285 0 return $clone;
286 }
287  
288 #-------------------------------------------------------------------------------
289  
290 =head2 print
291  
292 This is a debug method.
293  
294 =cut
295  
296 0 ▶sub print {
Subroutine print: not called CC=1 SCAR=6.9
Pod documented
297 0 my $self = shift;
298  
299 0 say "Print called";
300 }
301  
302  
303 #-------------------------------------------------------------------------------
304  
305 =head2 real_create_related
306  
307 This actually does the create related via OpenERP.
308  
309 I'm not sure in what scenarios you should use it versus the scenario's you
310 shouldn't. Suck it and see.
311  
312 It will create calls like this,
313  
314 # DEBUG_RPC:rpc.request:('execute', 'db', 1, '*', ('stock.partial.picking', 'write', [1], {'product_moves_out': [(0, 0, {'prodlot_id': False, 'product_id': 16, 'product_uom': 1, 'quantity': 10.0})]}, {'lang': 'en_GB', 'search_default_available': 1, 'project_id': False, 'tz': False, '__last_update': {'stock.partial.picking,1': False}, 'active_model': 'ir.ui.menu', 'section_id': False, 'contact_display': 'partner_address', 'active_ids': [3], 'active_id': 316}))
315  
316 Note that it will not return the object created.
317  
318 =cut
319  
320 sub real_create_related
321 0 ▶{
Subroutine real_create_related: not called CC=1 SCAR=6.9
Pod documented
322 0 my $self = shift;
323 0 my $relation_name = shift;
324 0 my $object = shift;
325 0 my $context = $self->class->_get_context(shift);
326  
327 # find relationship class
328 0 my $class = $self->relationship_class($relation_name);
329 0 my $data = $class->_collapse_data_to_ids($object);
330  
331 0 $self->class->schema->client->update($self->model, $self->id, {$relation_name => [[ 0, 0, $data ]]}, $context);
332  
333 # FIXME: need to check what happens to existing data
334 # how do you add multiple objects ?
335 0 return;
336 }
337  
338 =head2 create_related
339  
340 Creates a related or linked object.
341  
342 $obj->create_related('address',{
343 street => 'Drury Lane',
344 postcode => 'CV21 3DE',
345 });
346  
347 =cut
348  
349 0 ▶sub create_related {
Subroutine create_related: not called CC=14 SCAR=53.5
Pod documented
350 0 my ($self, $relation_name, $object) = @_;
351  
352 ### Creating related object
353 ### $relation_name
354 ### with initial data:
355 ### $object
356 0 my $created_obj;
357  
358 0 ▶ if (my $relation = $self->meta->relationship->{$relation_name}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $relation = $self->meta->relationship->{$relation_name}) { } 0 0 0
elsif ($relation = $self->meta->link->{$relation_name}) { } 0 0 0
359 sswitch ($relation->{type}) {
360 case ('one2many'): {
361 0 my $class = $self->meta->name;
362 0 ▶ if ($class =~ m/(.*?)::(\w+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class =~ /(.*?)::(\w+)$/) 0 0 0
363 0 my ($base, $name) = ($1, $2);
364 0 my $related_class = $base . "::" . $relation->{class};
365  
366 0 $self->ensure_class_loaded($related_class);
367 0 my $related_meta = $related_class->meta->relationship;
368  
369 0 my $far_end_relation;
370 0 REL: for my $key (keys %$related_meta) {
371 0 my $value = $related_meta->{$key};
372 0 ▶ if ($value->{class} eq $name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value->{"class"} eq $name) 0 0 0
373 0 $far_end_relation = $key;
374 0 last REL;
375 }
376 }
377  
378 0 ▶ if ($far_end_relation) {
Branch: 1 branch
BranchTrueFalseTotal
if ($far_end_relation) { } 0 0 0
379 0 my $foreign_key = $related_meta->{$far_end_relation}->{key};
380  
381 ### Far end relation exists
382 0 $created_obj = $self->class->schema->class($relation->{class})->create({
383 %$object,
384 $foreign_key => $self->id,
385 });
386  
387 0 $self->refresh;
388 } else {
389 0 my $new_object = $self->class->schema->class($relation->{class})->create($object);
390  
391 0 $created_obj = $new_object;
392 0 $self->refresh;
393  
394 0 ▶ unless (grep {$new_object->id} @{$self->{$relation->{key}}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$new_object->id;} @{$$self{$relation->{"key"}};}) 0 0 0
395 0 push @{$self->{$relation->{key}}}, $new_object->id;
396 0 ▶ $self->update;
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'many2many' ? : 0 0 0
397 }
398 }
399 }
400 }
401 case ('many2many'): {
402 0 ▶ say "create_related many2many";
Condition0%0/3
$_ eq "many2one" and do { say "create_related many2one" }
!ll&&!rl&&r
000
MC/DC0%0/2
$_ eq "many2one" and do { say "create_related many2one" }
$_ eq "many2one" do { say "create_related many2one" }
Truth table0%0/3
A and B
A= $_ eq "many2one"B= do { say "create_related many2one" }
ABresult
0X0
100
111
403 }
404 0 ▶ case ('many2one'): {
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'one2many' ? : 0 0 0
405 0 say "create_related many2one";
406 }
407 0 }
408 } elsif ($relation = $self->meta->link->{$relation_name}) {
409 sswitch ($relation->{type}) {
410 case ('single'): {
411 ### Creating linked object
412 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
413 0 my $id = $self->class->schema->link($relation->{class})->create($relation->{args}, $object);
414 0 $created_obj = $id;
415 ### Linked object created with key $id
416 0 $self->{$relation->{key}} = $id;
417 0 $self->update_single($relation->{key});
418 0 undef $self->{"_$relation_name"};
419 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
420 0 die "Error creating linked object: $_[0]";
421 0 ▶ };
Condition0%0/3
$_ eq "multiple" and do { say "create_linked multiple" }
!ll&&!rl&&r
000
MC/DC0%0/2
$_ eq "multiple" and do { say "create_linked multiple" }
$_ eq "multiple" do { say "create_linked multiple" }
Truth table0%0/3
A and B
A= $_ eq "multiple"B= do { say "create_linked multiple" }
ABresult
0X0
100
111
422 }
423 0 ▶ case ('multiple'): {
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'single' ? : 0 0 0
424 0 say "create_linked multiple";
425 }
426 0 }
427 }
428 else {
429 0 croak "Can not find relation $relation_name";
430 }
431 0 ▶ return $created_obj if $created_obj;
Branch: 1 branch
BranchTrueFalseTotal
if $created_obj 0 0 0
432 }
433  
434 sub _id
435 0 ▶{
Subroutine _id: not called CC=2 SCAR=17.9
436 0 my $val = shift;
437 0 ▶ return ref $val ? $val->id : $val;
Branch: 1 branch
BranchTrueFalseTotal
ref $val ? : 0 0 0
438 }
439  
440 =head2 find_related
441  
442 Finds a property related to the current object.
443  
444 my $line = $po->find_related('order_lines', [ 'id', '=', 1 ]);
445  
446 This only works with relationships to OpenERP objects (i.e. not DBIC) and
447 to one2many relationships where the other side of the relationship has a field
448 pointing back to the object you are searching from.
449  
450 In any other case the method will croak.
451  
452 If the search criteria return more than one result it will whine.
453  
454 =cut
455  
456 0 ▶sub find_related {
Subroutine find_related: not called CC=3 SCAR=24.8
Pod documented
457 0 my ($self) = shift;
458 0 my @results = $self->search_related(@_);
459 0 ▶ if(scalar @results > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @results > 1) 0 0 0
460 {
461 # should this just croak?
462 0 carp 'find_related returned more than 1 result';
463 }
464 0 ▶ if(@results)
Branch: 1 branch
BranchTrueFalseTotal
if (@results) 0 0 0
465 {
466 0 return $results[0];
467 }
468 }
469  
470 =head2 relationship_class
471  
472 Returns the OpenERP::OOM::Class object for the relationship passed in.
473  
474 Obviously this only works for the OpenERP relationships. It will croak
475 if you ask for a relationship to a DBIC object.
476  
477 =cut
478  
479 sub relationship_class
480 0 ▶{
Subroutine relationship_class: not called CC=4 SCAR=30.0
Pod documented
481 0 my ($self, $relationship) = @_;
482 0 ▶ if (my $relation = $self->meta->relationship->{$relationship}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $relation = $self->meta->relationship->{$relationship}) 0 0 0
483 0 my $type = $relation->{type};
484 0 ▶ croak 'Cannot get a class for a DBIC relationship' if $type eq 'single'
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "single" or $type eq "multiple" 0 0 0
Condition0%0/3
$type eq "single" or $type eq "multiple"
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "single" or $type eq "multiple"
$type eq "single" $type eq "multiple"
Truth table0%0/3
A or B
A= $type eq "single"B= $type eq "multiple"
ABresult
1X1
011
000
485 || $type eq 'multiple';
486 0 my $class = $relation->{class};
487 0 return $self->class->schema->class($class);
488 }
489 0 croak "Unable to find relation $relationship";
490 }
491  
492 =head2 search_related
493  
494 Searches for objects of a relation associated with this object.
495  
496 my @lines = $po->search_related('order_lines', [ 'state', '=', 'draft' ]);
497  
498 This only works with relationships to OpenERP objects (i.e. not DBIC) and
499 to one2many relationships where the other side of the relationship has a field
500 pointing back to the object you are searching from.
501  
502 In any other case the method will croak.
503  
504 =cut
505  
506 0 ▶sub search_related {
Subroutine search_related: not called CC=10 SCAR=47.0
Pod documented
507 0 my ($self, $relation_name, @search) = @_;
508  
509 # find the relation details and add it to the search criteria.
510 0 ▶ if (my $relation = $self->meta->relationship->{$relation_name}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $relation = $self->meta->relationship->{$relation_name}) { } 0 0 0
elsif ($relation = $self->meta->link->{$relation_name}) { } 0 0 0
511 sswitch ($relation->{type}) {
512 case ('one2many'): {
513 0 my $class = $self->meta->name;
514 0 ▶ if ($class =~ m/(.*?)::(\w+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class =~ /(.*?)::(\w+)$/) 0 0 0
515 0 my ($base, $name) = ($1, $2);
516 0 my $related_class = $self->class->schema->class($relation->{class});
517 0 my $related_meta = $related_class->object->meta->relationship;
518  
519 0 my $far_end_relation;
520 0 REL: for my $key (keys %$related_meta) {
521 0 my $value = $related_meta->{$key};
522 0 ▶ if ($value->{class} eq $name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value->{"class"} eq $name) 0 0 0
523 0 $far_end_relation = $key;
524 0 last REL;
525 }
526 }
527  
528 0 ▶ if ($far_end_relation) {
Branch: 1 branch
BranchTrueFalseTotal
if ($far_end_relation) { } 0 0 0
529  
530 0 my $foreign_key = $related_meta->{$far_end_relation}->{key};
531  
532 0 push @search, [ $foreign_key, '=', $self->id ];
533 0 return $related_class->search(@search);
534  
535 } else {
536 # well, perhaps we could fix this, but I can't be bothered at the moment.
537 0 ▶ croak 'Unable to search_related without relationship back';
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'many2many' ? : 0 0 0
538 }
539 }
540 }
541 case ('many2many'): {
542 0 ▶ croak 'Unable to search_related many2many relationships';
Condition0%0/3
$_ eq "many2one" and do { croak("Unable to search_related many2one relationships") }
!ll&&!rl&&r
000
MC/DC0%0/2
$_ eq "many2one" and do { croak("Unable to search_related many2one relationships") }
$_ eq "many2one" do { croak("Unable to search_related many2one relationships") }
Truth table0%0/3
A and B
A= $_ eq "many2one"B= do { croak("Unable to search_related many2one relationships") }
ABresult
0X0
100
111
543 }
544 0 ▶ case ('many2one'): {
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'one2many' ? : 0 0 0
545 0 croak 'Unable to search_related many2one relationships';
546 }
547 0 }
548 } elsif ($relation = $self->meta->link->{$relation_name}) {
549 0 croak 'Unable to search_related outside NonOpenERP';
550 }
551  
552 0 croak 'Unable to search_related'; # beat up the lame programmer who did this.
553 }
554  
555  
556 #-------------------------------------------------------------------------------
557  
558 =head2 add_related
559  
560 Adds a related or linked object to a one2many, many2many, or multiple relationship.
561  
562 my $partner = $schema->class('Partner')->find(...);
563 my $category = $schema->class('PartnerCategory')->find(...);
564  
565 $partner->add_related('category', $category);
566  
567 =cut
568  
569 0 ▶sub add_related {
Subroutine add_related: not called CC=6 SCAR=37.4
Pod documented
570 0 my ($self, $relation_name, $object) = @_;
571  
572 0 ▶ if (my $relation = $self->meta->relationship->{$relation_name}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $relation = $self->meta->relationship->{$relation_name}) { } 0 0 0
elsif ($relation = $self->meta->link->{$relation_name}) { } 0 0 0
573 sswitch ($relation->{type}) {
574 case ('one2many'): {
575 # FIXME - is this the same process as adding a many2many relationship?
576 }
577 0 ▶ case ('many2many'): {
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'one2many' ? : 0 0 0
Condition0%0/3
$_ eq "many2many" and do { push @{$$self{$relation->{"key"}};}, _id($object); $$self{$relation->{"key"}} = [&uniq(@{$$self{$relation->{"key"}};})]; $self->update_single($relation->{"key"}) }
!ll&&!rl&&r
000
MC/DC0%0/2
$_ eq "many2many" and do { push @{$$self{$relation->{"key"}};}, _id($object); $$self{$relation->{"key"}} = [&uniq(@{$$self{$relation->{"key"}};})]; $self->update_single($relation->{"key"}) }
$_ eq "many2many" do { push @{$$self{$relation->{"key"}};}, _id($object); $$self{$relation->{"key"}} = [&uniq(@{$$self{$relation->{"key"}};})]; $self->update_single($relation->{"key"}) }
Truth table0%0/3
A and B
A= $_ eq "many2many"B= do { push @{$$self{$relation->{"key"}};}, _id($object); $$self{$relation->{"key"}} = [&uniq(@{$$self{$relation->{"key"}};})]; $self->update_single($relation->{"key"}) }
ABresult
0X0
100
111
578 0 push @{$self->{$relation->{key}}}, _id($object);
579 0 $self->{$relation->{key}} = [uniq @{$self->{$relation->{key}}}];
580 0 $self->update_single($relation->{key});
581 }
582 0 }
583 } elsif ($relation = $self->meta->link->{$relation_name}) {
584 sswitch ($relation->{type}) {
585 0 ▶ case ('multiple'): {
Branch: 1 branch
BranchTrueFalseTotal
$_ eq "multiple" and do { () } 0 0 0
586 # FIXME - handle linked as well as related objects
587 }
588 0 }
589 }
590 }
591  
592  
593 #-------------------------------------------------------------------------------
594  
595 =head2 set_related
596  
597 Like the DBIx::Class set_related. Sets up a link to a related object.
598  
599 =cut
600  
601 0 ▶sub set_related {
Subroutine set_related: not called CC=7 SCAR=40.3
Pod documented
602 0 my ($self, $relation_name, $object) = @_;
603  
604 0 ▶ if (my $relation = $self->meta->relationship->{$relation_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $relation = $self->meta->relationship->{$relation_name}) { } 0 0 0
605 sswitch ($relation->{type}) {
606 case ('many2one'): {
607 0 ▶ $self->{$relation->{key}} = $object ? _id($object) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$object ? : 0 0 0
608 0 ▶ $self->update_single($relation->{key});
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'many2many' ? : 0 0 0
609 }
610 case ('many2many'): {
611 0 my @array;
612 0 ▶ if($object)
Branch: 1 branch
BranchTrueFalseTotal
if ($object) 0 0 0
613 {
614 0 ▶ if(ref $object eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $object eq 'ARRAY') { } 0 0 0
615 {
616 0 @array = map { _id($_) } @$object;
617 }
618 else
619 {
620 0 push @array, _id($object);
621 }
622 }
623 0 $self->{$relation->{key}} = \@array;
624 0 $self->update_single($relation->{key});
625 }
626 0 ▶ default: {
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'many2one' ? : 0 0 0
627 0 carp "Cannot use set_related() on a $_ relationship";
628 }
629 0 }
630 } else {
631 0 carp "Relation '$relation_name' does not exist!";
632 }
633 }
634  
635 =head2 execute_workflow
636  
637 Performs an exec_workflow in OpenERP.
638  
639 $self->execute_workflow('purchase_confirm');
640  
641 Is likely to translate to something like this,
642  
643 # DEBUG_RPC:rpc.request:('exec_workflow', 'db', 1, '*', ('purchase.order', 'purchase_confirm', 24))
644  
645 The 24 is the id of the object.
646  
647 =cut
648  
649 sub execute_workflow
650 0 ▶{
Subroutine execute_workflow: not called CC=1 SCAR=6.9
Pod documented
651 0 my ($self, $workflow) = @_;
652  
653 retry
654 0 ▶ {
Subroutine __ANON__: not called CC=1 SCAR=6.9
655 0 $self->class->schema->client->object_exec_workflow($workflow, $self->model, $self->id);
656 }
657 0 ▶ retry_if {/current transaction is aborted, commands ignored until end of transaction block/}
Subroutine __ANON__: not called CC=1 SCAR=6.9
658 catch
659 0 ▶ {
Subroutine __ANON__: not called CC=1 SCAR=6.9
660 0 die $_; # rethrow the unhandled exception
661 0 };
662 }
663  
664 =head2 execute
665  
666 Performs an execute in OpenERP.
667  
668 $self->execute('action_process');
669  
670 Is likely to translate to something like this,
671  
672 # DEBUG_RPC:rpc.request:('execute', 'gooner', 1, '*', ('stock.picking', 'action_process', [26], {'lang': 'en_GB', 'search_default_available': 1, 'active_ids': [316], 'tz': False, 'active_model': 'ir.ui.menu', 'section_id': False, 'contact_display': 'partner_address', 'project_id': False, 'active_id': 316}))
673  
674 The 26 is the id of the object.
675  
676 =cut
677  
678 sub execute
679 0 ▶{
Subroutine execute: not called CC=1 SCAR=6.9
Pod documented
680 0 my $self = shift;
681 0 my $action = shift;
682 0 my @params = @_;
683  
684 0 my @args = ($action, $self->model, [$self->id], @params);
685 0 my $retval;
686 0 ▶ $self->class->_with_retries(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
687 0 $retval = $self->class->schema->client->object_execute(@args);
688 0 });
689 0 return $retval;
690 }
691  
692 =head2 executex
693  
694 Similar to execute but it allows you to specify any number of parameters.
695  
696 Primarily created to prevent any compatibility problems with other callers.
697 Although I'm not entirely sure if there are any.
698  
699 $self->executex('add_invoices_to_payment', [1,2], [3,4]);
700  
701 Translates roughly to
702  
703 execute_kw(..., 'payment.order', 'add_invoices_to_payment', [5], [1, 2], [3, 4])
704  
705 Stick a hash on the end of the list of params to pass a context object.
706  
707 =cut
708  
709 sub executex
710 0 ▶{
Subroutine executex: not called CC=2 SCAR=17.9
Pod documented
711 0 my ($self, $action, @rest) = @_;
712  
713 0 my @args = ($action, $self->model, [$self->id]);
714 0 ▶ push @args, @rest if @rest;
Branch: 1 branch
BranchTrueFalseTotal
if @rest 0 0 0
715 0 my $retval;
716 0 ▶ $self->class->_with_retries(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
717 0 $retval = $self->class->schema->client->object_execute(@args);
718 0 });
719 0 return $retval;
720 }
721  
722 =head2 get_report
723  
724 To print a purchase order we need to send a report, then get it, then display it, then print it (and you don't want to know about all the traffic behind the scenes...)
725  
726 The first step looks like this:
727  
728 # DEBUG_RPC:rpc.request:('report', 'aquarius_openerp_jj_staging', 1, '*', (u'purchase.quotation', [1], {'model': u'purchase.order', 'id': 1, 'report_type': u'pdf'}, {'lang': u'en_GB', 'active_ids': [1], 'tz': False, 'active_model': u'purchase.order', 'section_id': False, 'search_default_draft': 1, 'project_id': False, 'active_id': 1}))
729  
730 =cut
731  
732 sub get_report
733 0 ▶{
Subroutine get_report: not called CC=2 SCAR=17.9
Pod documented
734 0 my $self = shift;
735 0 my $report_id = shift;
736  
737 0 my $id = $self->class->schema->client->report_report($report_id, $self->id,
738 {
739 model => $self->model,
740 id => $self->id,
741 report_type => 'pdf',
742 }, @_);
743  
744 # the report_report function returns only a report id, which is all we need to pass to the next function call
745 # but report_report_get don't work first time (?!) so we need to call it recursively until with get an answer
746 0 my $data;
747 0 ▶ while(!$data)
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 0 0 0
748 {
749 0 $data = $self->class->schema->client->report_report_get($id);
750 0 sleep 1;
751 }
752 0 return $data;
753 }
754  
755  
756 2 1;
↑ Summary OOM/Class/Base.pm »