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