Schema/Result/Order.pm

filter: Statementstmt 100% 73 / 73 Branchbran 100% 20 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 6 / 6 totaltotal 100% 107 / 107 CC 12 SCAR 12.3
CC
12
Cov
100%
add_to_comments
5.0
set_comments
4.0
status
4.0
CRAP 3.4
« Schema/ResultSet/User.pm ↑ Summary Test/Role/Database.pm »
1 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Interchange6::Schema::Result::Order;
4  
5 =head1 NAME
6  
7 Interchange6::Schema::Result::Order
8  
9 =cut
10  
11 2 ▶use Interchange6::Schema::Candy -components => [qw(InflateColumn::DateTime)];
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 ACCESSORS
14  
15 =head2 orders_id
16  
17 Primary key.
18  
19 =cut
20  
21 2 primary_column orders_id => {
22 data_type => "integer",
23 is_auto_increment => 1,
24 sequence => "orders_orders_id_seq",
25 };
26  
27 =head2 order_number
28  
29 Unique representation of the order.
30  
31 =cut
32  
33 2 unique_column order_number => {
34 data_type => "varchar",
35 size => 24
36 };
37  
38 =head2 order_date
39  
40 Timestamp of when the order was placed. Is nullable.
41  
42 =cut
43  
44 2 column order_date => {
45 data_type => "timestamp",
46 is_nullable => 1
47 };
48  
49 =head2 users_id
50  
51 Foreign key constraint on L<Interchange6::Schema::Result::User/users_id>
52 via L</user> relationship.
53  
54 =cut
55  
56 2 column users_id => {
57 data_type => "integer",
58 };
59  
60 =head2 email
61  
62 Email address used for the order. Default is empty string
63  
64 =cut
65  
66 2 column email => {
67 data_type => "varchar",
68 default_value => "",
69 size => 255
70 };
71  
72 =head2 shipping_addresses_id
73  
74 Foreign key constraint on L<Interchange6::Schema::Result::Address/addresses_id>
75 via L</shipping_address> relationship.
76  
77 =cut
78  
79 2 column shipping_addresses_id => {
80 data_type => "integer",
81 };
82  
83 =head2 billing_addresses_id
84  
85 Foreign key constraint on L<Interchange6::Schema::Result::Address/addresses_id>
86 via L</billing_address> relationship.
87  
88 =cut
89  
90 2 column billing_addresses_id => {
91 data_type => "integer",
92 };
93  
94 =head2 weight
95  
96 Total numeric weight of the order. Default is 0
97  
98 =cut
99  
100 2 column weight => {
101 data_type => "numeric",
102 default_value => 0,
103 size => [ 11, 3 ]
104 };
105  
106 =head2 payment_method
107  
108 This is the type of payment used for the order.
109  
110 =cut
111  
112 2 column payment_method => {
113 data_type => "varchar",
114 default_value => "",
115 size => 255
116 };
117  
118 =head2 payment_number
119  
120 A validation record for the payment.
121  
122 =cut
123  
124 2 column payment_number => {
125 data_type => "varchar",
126 default_value => "",
127 size => 255
128 };
129  
130 =head2 payment_status
131  
132 The status of the payment for the current order.
133  
134 =cut
135  
136 2 column payment_status => {
137 data_type => "varchar",
138 default_value => "",
139 size => 255
140 };
141  
142 =head2 shipping_method
143  
144 What shipping method was used for the order.
145  
146 =cut
147  
148 2 column shipping_method => {
149 data_type => "varchar",
150 default_value => "",
151 size => 255
152 };
153  
154 =head2 subtotal
155  
156 The sum of all items in the cart before tax and shipping costs.
157  
158 Defaults to 0.
159  
160 =cut
161  
162 2 column subtotal => {
163 data_type => "numeric",
164 default_value => 0,
165 size => [ 21, 3 ],
166 };
167  
168 =head2 shipping
169  
170 The numeric cost associated with shipping the order. Default is 0
171  
172 =cut
173  
174 2 column shipping => {
175 data_type => "numeric",
176 default_value => 0,
177 size => [ 21, 3 ],
178 };
179  
180 =head2 handling
181  
182 The numeric cost associated with handling the order. Default is 0.
183  
184 =cut
185  
186 2 column handling => {
187 data_type => "numeric",
188 default_value => 0,
189 size => [ 21, 3 ],
190 };
191  
192 =head2 salestax
193  
194 The total tax applied to the order. Default is 0
195  
196 =cut
197  
198 2 column salestax => {
199 data_type => "numeric",
200 default_value => 0,
201 size => [ 21, 3 ],
202 };
203  
204 =head2 total_cost
205  
206 The total cost of all items shipping handling and tax for the order.
207 Default is 0.
208  
209 =cut
210  
211 2 column total_cost => {
212 data_type => "numeric",
213 default_value => 0,
214 size => [ 21, 3 ],
215 };
216  
217 =head1 RELATIONS
218  
219 =head2 shipping_address
220  
221 Type: belongs_to
222  
223 Related object: L<Interchange6::Schema::Result::Address>
224  
225 =cut
226  
227 2 belongs_to
228 shipping_address => "Interchange6::Schema::Result::Address",
229 { addresses_id => "shipping_addresses_id" };
230  
231 =head2 billing_address
232  
233 Type: belongs_to
234  
235 Related object: L<Interchange6::Schema::Result::Address>
236  
237 =cut
238  
239 2 belongs_to
240 billing_address => "Interchange6::Schema::Result::Address",
241 { addresses_id => "billing_addresses_id" };
242  
243 =head2 orderlines
244  
245 Type: has_many
246  
247 Related object: L<Interchange6::Schema::Result::Orderline>
248  
249 =cut
250  
251 2 has_many
252 orderlines => "Interchange6::Schema::Result::Orderline",
253 "orders_id",
254 { cascade_copy => 0, cascade_delete => 0 };
255  
256 =head2 payment_orders
257  
258 Type: has_many
259  
260 Related object: L<Interchange6::Schema::Result::PaymentOrder>
261  
262 =cut
263  
264 2 has_many
265 payment_orders => "Interchange6::Schema::Result::PaymentOrder",
266 "orders_id",
267 { cascade_copy => 0, cascade_delete => 0 };
268  
269 =head2 user
270  
271 Type: belongs_to
272  
273 Related object: L<Interchange6::Schema::Result::User>
274  
275 =cut
276  
277 2 belongs_to
278 user => "Interchange6::Schema::Result::User",
279 "users_id",
280 { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" };
281  
282 =head2 order_comments
283  
284 Type: has_many
285  
286 Related object: L<Interchange6::Schema::Result::OrderComment>
287  
288 =cut
289  
290 2 has_many
291 order_comments => 'Interchange6::Schema::Result::OrderComment',
292 'orders_id';
293  
294 =head2 _comments
295  
296 Type: many_to_many
297  
298 This is considered a private method. Please see public L</comments> and L</add_to_comments> methods.
299  
300 =cut
301  
302 2 many_to_many _comments => "order_comments", "message";
303  
304 =head2 statuses
305  
306 Type: has_many
307  
308 Related object: L<Interchange6::Schema::Result::OrderStatus>
309  
310 =cut
311  
312 2 has_many
313 statuses => 'Interchange6::Schema::Result::OrderStatus',
314 'orders_id';
315  
316  
317 =head1 METHODS
318  
319 =head2 comments
320  
321 =over 4
322
323 =item Arguments: none
324  
325 =item Return Value: L<Interchange6::Schema::Result::Message> resultset.
326  
327 =back
328
329 =cut
330  
331 sub comments {
332 3 ▶ return shift->_comments(@_);
Subroutine comments: called CC=1 SCAR=0.0
Pod documented
333 }
334  
335 =head2 add_to_comments
336  
337 =over 4
338
339 =item Arguments: \%col_data
340
341 =item Return Value: L<Interchange6::Schema::Result::Message>
342
343 =back
344  
345 See L<DBIx::Class::Relationship::Base/add_to_$rel> many_to_many for further details.
346  
347 =cut
348  
349 # much of this was cargo-culted from DBIx::Class::Relationship::ManyToMany
350  
351 sub add_to_comments {
352 12 ▶ my $self = shift;
Subroutine add_to_comments: called CC=5 SCAR=16.1
Pod documented
353 12 ▶ @_ > 0
Branch: 1 branch
BranchTrueFalseTotal
@_ > 0 or $self->throw_exception("add_to_comments needs an object or hashref") 1 11 12
354 or $self->throw_exception("add_to_comments needs an object or hashref");
355 11 my $rset_message = $self->result_source->schema->resultset("Message");
356 11 my $obj;
357 11 ▶ if ( ref $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 10 1 11
358 10 ▶ if ( ref $_[0] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 7 3 10
359 7 $_[0]->{type} = "order_comment";
360 7 $obj = $rset_message->create( $_[0] );
361 }
362 else {
363 3 $obj = $_[0];
364 3 ▶ unless ( my $type = $obj->message_type->name eq "order_comment" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (my $type = $obj->message_type->name eq "order_comment") 1 2 3
365 1 $self->throw_exception(
366 "cannot add message type $type to comments");
367 }
368 }
369 }
370 else {
371 1 push @_, type => "order_comment";
372 1 $obj = $rset_message->create( {@_} );
373 }
374 9 $self->create_related( 'order_comments', { messages_id => $obj->id } );
375 9 return $obj;
376 }
377  
378 =head2 set_comments
379  
380 =over 4
381
382 =item Arguments: (\@hashrefs_of_col_data | \@result_objs)
383
384 =item Return Value: not defined
385
386 =back
387  
388 Similar to L<DBIx::Class::Relationship::Base/set_$rel> except that this method D
389 OES delete objects in the table on the right side of the relation.
390  
391 =cut
392  
393 sub set_comments {
394 5 ▶ my $self = shift;
Subroutine set_comments: called CC=4 SCAR=13.9
Pod documented
395 5 ▶ @_ > 0
Branch: 1 branch
BranchTrueFalseTotal
@_ > 0 or $self->throw_exception("set_comments needs a list of objects or hashrefs") 1 4 5
396 or $self->throw_exception(
397 "set_comments needs a list of objects or hashrefs");
398 4 ▶ my @to_set = ( ref( $_[0] ) eq 'ARRAY' ? @{ $_[0] } : @_ );
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 2 2 4
399 4 $self->order_comments->delete_all;
400 4 $self->add_to_comments( $_ ) for (@to_set);
401 }
402  
403 =head2 delete
404  
405 Overload delete to force removal of any order comments.
406  
407 =cut
408  
409 # FIXME: (SysPete) There ought to be a way to force this with cascade delete.
410  
411 sub delete {
412 2 ▶ my ( $self, @args ) = @_;
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
413 2 my $guard = $self->result_source->schema->txn_scope_guard;
414 2 $self->order_comments->delete_all;
415 2 $self->next::method(@args);
416 2 $guard->commit;
417 }
418  
419 =head2 insert
420  
421 Override insert so that if no L<Interchange6::Schema::Result::OrderStatus> has
422 been provided via multicreate then create a single status named C<new>.
423  
424 =cut
425  
426 sub insert {
427 12 ▶ my ( $self, @args ) = @_;
Subroutine insert: called CC=2 SCAR=6.9
Pod documented
428 12 my $guard = $self->result_source->schema->txn_scope_guard;
429 12 my $ret = $self->next::method(@args);
430 12 ▶ if ( $self->statuses->count == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->statuses->count == 0) 11 1 12
431 11 $self->create_related('statuses', { status => 'new' });
432 }
433 12 $guard->commit;
434 12 return $ret;
435 }
436  
437 =head2 status
438  
439 Option argument C<$status> will cause creation of a new related entry in
440 L<Interchange6::Schema::Result::OrderStatus>.
441  
442 Returns the most recent L<Interchange6::Schema::Result::OrderStatus/status> or
443 undef if none are found.
444  
445 If initial result set was created using
446 L<Interchange6::Schema::ResultSet::Order/with_status> then the status added
447 by that method will be returned so that a new query is not required.
448  
449 =cut
450  
451 sub status {
452 6 ▶ my $self = shift;
Subroutine status: called CC=4 SCAR=13.9
Pod documented
453 6 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 5 6
454 1 return $self->statuses->create( { status => shift } )->status;
455 }
456 5 ▶ if ( $self->has_column_loaded('status')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_column_loaded('status')) { } 1 4 5
457 1 return $self->get_column('status');
458 }
459 else {
460 4 my $status =
461 $self->statuses->order_by('!created,!order_status_id')->rows(1)
462 ->single;
463 4 ▶ return $status ? $status->status : undef;
Branch: 1 branch
BranchTrueFalseTotal
$status ? : 3 1 4
464 }
465 }
466  
467 2 1;
« Schema/ResultSet/User.pm ↑ Summary Test/Role/Database.pm »