OOM/Object/Base.pm

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