Schema/ResultSet/Product.pm

filter: Statementstmt 92.3% 48 / 52 Branchbran 62.5% 10 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 100% 8 / 8 totaltotal 88.6% 78 / 88 CC 9 SCAR 14.5
CC
9
Cov
85%
with_lowest_selling_price
8.9
with_highest_price
2.0
BEGIN
1.0
CRAP 4.3
« Schema/Result/Order.pm ↑ Summary Schema/Base/Attribute.pm »
1 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Interchange6::Schema::ResultSet::Product;
4  
5 =head1 NAME
6  
7 Interchange6::Schema::ResultSet::Product
8  
9 =cut
10  
11 =head1 SYNOPSIS
12  
13 Provides extra accessor methods for L<Interchange6::Schema::Result::Product>
14  
15 =cut
16  
17 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 ▶use parent 'Interchange6::Schema::ResultSet';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 =head1 METHODS
23  
24 See also L<DBIx::Class::Helper::ResultSet::Shortcut> which is loaded by this
25 result set.
26  
27 =head2 active
28  
29 Returns all rows where L<Interchange6::Schema::Result::Product/active> is true.
30  
31 =cut
32  
33 sub active {
34 1 ▶ return $_[0]->search({ $_[0]->me.'active' => 1 });
Subroutine active: called CC=1 SCAR=0.0
Pod documented
35 }
36  
37 =head2 canonical_only
38  
39 Returns all rows where L<Interchange6::Schema::Result::Product/canonical_sku>
40 is null, i.e. only canonical products.
41  
42 =cut
43  
44 sub canonical_only {
45 1 ▶ return $_[0]->search({ $_[0]->me.'canonical_sku' => undef });
Subroutine canonical_only: called CC=1 SCAR=0.0
Pod documented
46 }
47  
48 =head2 listing
49  
50 This is just a shortcut for:
51  
52 $self->columns( [ 'sku', 'name', 'uri', 'price', 'short_description' ] )
53 ->with_average_rating
54 ->with_lowest_selling_price
55 ->with_highest_price
56 ->with_quantity_in_stock
57 ->with_variant_count
58  
59 Though in addition if you pass in arguments these are passed through to the
60 appropriate with_* method so you can do:
61  
62 $self->listing({ quantity => 10, users_id => 43 })
63  
64 And the result will be:
65  
66 $self->columns( [ 'sku', 'name', 'uri', 'price', 'short_description' ] )
67 ->with_average_rating
68 ->with_lowest_selling_price({ quantity => 10, users_id => 43 })
69 ->with_highest_price
70 ->with_quantity_in_stock
71 ->with_variant_count
72  
73 =cut
74  
75 sub listing {
76 5 ▶ my ( $self, $args ) = @_;
Subroutine listing: called CC=1 SCAR=0.0
Pod documented
77 return $self->columns(
78 [ 'sku', 'name', 'uri', 'price', 'short_description' ] )
79 ->with_average_rating->with_lowest_selling_price(
80 {
81 quantity => $args->{quantity},
82 users_id => $args->{users_id},
83 roles => $args->{roles}
84 }
85 5 )->with_highest_price->with_quantity_in_stock->with_variant_count;
86 }
87  
88 =head2 with_average_rating
89  
90 Adds C<average_rating> column which is available to order_by clauses and
91 whose value can be retrieved via
92 L<Interchange6::Schema::Result::Product/average_rating>.
93  
94 This is the average rating across all public and approved product reviews or
95 undef if there are no reviews. Product reviews are only related to canonical
96 products so for variants the value returned is that of the canonical product.
97  
98 =cut
99  
100 sub with_average_rating {
101 6 ▶ my $self = shift;
Subroutine with_average_rating: called CC=1 SCAR=0.0
Pod documented
102  
103 6 my $me = $self->me;
104  
105 6 return $self->search(
106 undef,
107 {
108 '+select' => [
109 {
110 coalesce => [
111  
112 $self->correlate('canonical')
113 ->related_resultset('_product_reviews')
114 ->search_related(
115 'message',
116 { 'message.approved' => 1, 'message.public' => 1 }
117 )->get_column('rating')->func_rs('avg')->as_query,
118  
119 $self->correlate('_product_reviews')
120 ->search_related(
121 'message',
122 { 'message.approved' => 1, 'message.public' => 1 }
123 )->get_column('rating')->func_rs('avg')->as_query,
124  
125 ],
126 -as => 'average_rating'
127 }
128 ],
129 '+as' => ['average_rating'],
130 }
131 );
132 }
133  
134 =head2 with_quantity_in_stock
135  
136 Adds C<quantity_in_stock> column which is available to order_by clauses and
137 whose value can be retrieved via
138 L<Interchange6::Schema::Result::Product/quantity_in_stock>.
139  
140 The value is retrieved is L<Interchange6::Schema::Result::Inventory/quantity>.
141  
142 For a product variant and for a canonical product with no variants the
143 quantity returned is for the product itself.
144  
145 For a canonical (parent) product the quantity returned is the total for all its
146 variants.
147  
148 =cut
149  
150 sub with_quantity_in_stock {
151 6 ▶ my $self = shift;
Subroutine with_quantity_in_stock: called CC=1 SCAR=0.0
Pod documented
152  
153 6 return $self->search(
154 undef,
155 {
156 '+select' => [
157 {
158 coalesce => [
159  
160 $self->correlate('variants')
161 ->related_resultset('inventory')
162 ->get_column('quantity')->sum_rs->as_query,
163  
164 $self->correlate('inventory')->get_column('quantity')
165 ->as_query,
166  
167 ],
168 -as => 'quantity_in_stock',
169 }
170 ],
171 '+as' => ['quantity_in_stock'],
172 }
173 );
174 }
175  
176 =head2 with_lowest_selling_price
177  
178 The lowest of L<Interchange6::Schema::Result::PriceModifier/price> and
179 L<Interchange6::Schema::Result::Product/price>.
180  
181 For products with variants this is the lowest variant selling_price.
182  
183 Value is placed in the column C<selling_price>.
184  
185 =cut
186  
187 sub with_lowest_selling_price {
188 30 ▶ my ( $self, $args ) = @_;
Subroutine with_lowest_selling_price: called CC=8 SCAR=21.9
Pod documented
189  
190 30 ▶ if ( defined($args) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args) 8 22 30
191 8 ▶ $self->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "HASH" 0 8 8
192 "argument to listing must be a hash reference")
193 unless ref($args) eq "HASH";
194 }
195  
196 30 my $schema = $self->result_source->schema;
197  
198 30 ▶ $args->{quantity} = 1 unless defined $args->{quantity};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"quantity"} 28 2 30
199  
200 30 my $roles_id = undef;
201 30 my @roles_cond = ( undef );
202  
203 30 ▶ if ( $args->{users_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"users_id"}) 2 28 30
204  
205 my $subquery =
206 $schema->resultset('UserRole')
207 ->search( { "role.users_id" => $args->{users_id} },
208 2 { alias => 'role' } )->get_column('roles_id')->as_query;
209  
210 2 push @roles_cond, { -in => $subquery };
211 }
212  
213 30 ▶ if ( $args->{roles} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"roles"}) 0 30 30
214  
215 $self->throw_exception(
216 "Argument roles to selling price must be an array reference")
217 0 ▶ unless ref( $args->{roles} ) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args->{"roles"} eq "ARRAY" 0 0 0
218  
219 my $subquery =
220 $schema->resultset('Role')
221 0 ->search( { "role.name" => { -in => \@{$args->{roles}} } },
222 { alias => 'role' } )->get_column('roles_id')->as_query;
223  
224 0 push @roles_cond, { -in => $subquery };
225 }
226  
227 30 my $today = $schema->format_datetime(DateTime->today);
228  
229 30 my $least = 'least';
230 30 ▶ $least = 'min' if $schema->storage->sqlt_type eq 'SQLite';
Branch: 1 branch
BranchTrueFalseTotal
if $schema->storage->sqlt_type eq "SQLite" 30 0 30
231  
232 # much hoop jumping required to make sure we don't trip over nulls
233 #
234 # see:
235 # https://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_least
236 #
237 # which states:
238 # Before MySQL 5.0.13, LEAST() returns NULL only if all arguments are NULL.
239 # As of 5.0.13, it returns NULL if any argument is NULL.
240 #
241 # Complete madness!
242 #
243 # Compare to the sanity of PostgreSQL:
244 # NULL values in the list are ignored. The result will be NULL only if all
245 # the expressions evaluate to NULL.
246  
247 my $search_cond = {
248 'start_date' => [ undef, { '<=', $today } ],
249 'end_date' => [ undef, { '>=', $today } ],
250 'quantity' => { '<=' => $args->{quantity} },
251 30 'roles_id' => \@roles_cond,
252 };
253  
254 30 my $variant_price_modifiers =
255 $self->correlate('variants')
256 ->search_related( 'price_modifiers', $search_cond )->get_column('price')
257 ->min_rs->as_query;
258  
259 30 my $variant_prices =
260 $self->correlate('variants')->get_column('price')->min_rs->as_query;
261  
262 30 my $self_price_modifiers =
263 $self->correlate('price_modifiers')->search( $search_cond )
264 ->get_column('price')->min_rs->as_query;
265  
266 30 return $self->search(
267 undef,
268 {
269 '+select' => [
270 {
271 coalesce => [
272 {
273 $least => [
274 {
275 coalesce => [
276 $variant_price_modifiers,
277 $variant_prices
278 ]
279 },
280 $variant_prices
281 ]
282 },
283 {
284 coalesce =>
285 [ $self_price_modifiers, $self->me('price') ],
286  
287 },
288 ],
289 -as => 'selling_price'
290 }
291 ],
292 '+as' => ['selling_price'],
293 }
294 );
295 }
296  
297 =head2 with_highest_price
298  
299 For canonical products with no variants and for variant products
300 C<highest_price> is always undef. For canonical products that have variants
301 this is the highest of L<Interchange6::Schema::Result::Product/price> of
302 the variants.
303  
304 =cut
305  
306 sub with_highest_price {
307 5 ▶ my $self = shift;
Subroutine with_highest_price: called CC=2 SCAR=7.0
Pod documented
308  
309 5 my $schema = $self->result_source->schema;
310  
311 5 my $greatest = 'greatest';
312 5 ▶ $greatest = 'max' if $schema->storage->sqlt_type eq 'SQLite';
Branch: 1 branch
BranchTrueFalseTotal
if $schema->storage->sqlt_type eq "SQLite" 5 0 5
313  
314 5 return $self->search(
315 undef,
316 {
317 '+columns' => {
318 highest_price => {
319 coalesce => [
320 $self->correlate('variants')->get_column('price')
321 ->max_rs->as_query,
322 $self->me('price')
323 ]
324 }
325 }
326 }
327 );
328 }
329  
330 =head2 with_variant_count
331  
332 Adds column C<variant_count> which is a count of variants of each product.
333  
334 =cut
335  
336 sub with_variant_count {
337 6 ▶ my $self = shift;
Subroutine with_variant_count: called CC=1 SCAR=0.0
Pod documented
338 6 return $self->search(
339 undef,
340 {
341 '+columns' => {
342 variant_count =>
343 $self->correlate('variants')->count_rs->as_query
344 }
345 }
346 );
347 }
348  
349 2 1;
« Schema/Result/Order.pm ↑ Summary Schema/Base/Attribute.pm »