| 1 | package XTaTIK::Model::Products; | ||
| 2 | |||
| 3 | our $VERSION = '0.005003'; # VERSION | ||
| 4 | |||
| 5 | 5 | ▶ | use Mojo::Base -base; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 0 | ▶ | use Mojo::Pg; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use Text::Markdown 'markdown'; | ||
| 8 | use List::AllUtils qw/uniq/; | ||
| 9 | use List::UtilsBy qw/sort_by extract_by/; | ||
| 10 | use Scalar::Util qw/blessed/; | ||
| 11 | use experimental qw/postderef/; | ||
| 12 | use JSON::Meth qw/$json/; | ||
| 13 | |||
| 14 | use autobox; | ||
| 15 | sub SCALAR::split_comma { [ split /\s*,\s*/, $_[0] ] } | ||
| 16 | |||
| 17 | has [qw/pg pricing_region custom_cat_sorting site/]; | ||
| 18 | |||
| 19 | sub exists { | ||
| 20 | my $self = shift; | ||
| 21 | my $number = shift; | ||
| 22 | return $self->get_by_number( $number ); | ||
| 23 | } | ||
| 24 | |||
| 25 | sub get_by_number { | ||
| 26 | my $self = shift; | ||
| 27 | my @numbers = @_; | ||
| 28 | |||
| 29 | return unless @numbers; | ||
| 30 | |||
| 31 | my $result = $self->pg->db->query( | ||
| 32 | 'SELECT * FROM products WHERE sites ~ ? AND number IN (' . | ||
| 33 | ( join ',', ('?')x@numbers ) | ||
| 34 | . ')', | ||
| 35 | '(^|,)' . quotemeta($self->site) . '(,|$)', | ||
| 36 | @numbers, | ||
| 37 | )->hashes; | ||
| 38 | |||
| 39 | $result = $self->_process_products( $result ); | ||
| 40 | return wantarray ? @$result : $result->[0]; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub get_by_id { | ||
| 44 | my $self = shift; | ||
| 45 | my @ids = @_; | ||
| 46 | |||
| 47 | return unless @ids; | ||
| 48 | |||
| 49 | my $result = $self->pg->db->query( | ||
| 50 | 'SELECT * FROM products WHERE sites ~ ? AND id IN (' . | ||
| 51 | ( join ',', ('?')x@ids ) | ||
| 52 | . ')', | ||
| 53 | '(^|,)' . quotemeta($self->site) . '(,|$)', | ||
| 54 | @ids, | ||
| 55 | )->hashes; | ||
| 56 | |||
| 57 | $result = $self->_process_products( $result ); | ||
| 58 | return wantarray ? @$result : $result->[0]; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub get_by_url { | ||
| 62 | my $self = shift; | ||
| 63 | my $url = shift; | ||
| 64 | |||
| 65 | my $product = $self->pg->db->query( | ||
| 66 | 'SELECT * FROM products WHERE sites ~ ? AND url = ?', | ||
| 67 | '(^|,)' . quotemeta($self->site) . '(,|$)', | ||
| 68 | $url, | ||
| 69 | )->hash; | ||
| 70 | |||
| 71 | return $self->_fill_grouped( $product )->_process_products( $product ); | ||
| 72 | } | ||
| 73 | |||
| 74 | sub _fill_grouped { | ||
| 75 | my ( $self, $prod ) = @_; | ||
| 76 | return $self unless length $prod->{group_master}; | ||
| 77 | |||
| 78 | my $group = $self->pg->db->query( | ||
| 79 | 'SELECT number, url, group_desc | ||
| 80 | FROM products WHERE sites ~ ? AND group_master = ?', | ||
| 81 | '(^|,)' . quotemeta($self->site) . '(,|$)', | ||
| 82 | $prod->{group_master}, | ||
| 83 | )->hashes; | ||
| 84 | |||
| 85 | for ( sort_by { $_->{group_desc} } @$group ) { | ||
| 86 | push $prod->{options}->@*, +{ | ||
| 87 | url => $_->{url}, | ||
| 88 | desc => $_->{group_desc}, | ||
| 89 | is_self => $_->{number} eq $prod->{number}, | ||
| 90 | }; | ||
| 91 | } | ||
| 92 | |||
| 93 | return $self; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub add { | ||
| 97 | my $self = shift; | ||
| 98 | my %values = @_; | ||
| 99 | my $url = "$values{title} $values{number}" =~ s/\W+/-/gr; | ||
| 100 | |||
| 101 | for ( keys %values ) { length $values{$_} or delete $values{$_} } | ||
| 102 | |||
| 103 | $values{sites} //= 'default'; | ||
| 104 | $values{price} //= { default => { '00' => '0.00' } }->$json; | ||
| 105 | |||
| 106 | return $self->pg->db->query( | ||
| 107 | 'INSERT INTO products (number, image, title, | ||
| 108 | category, group_master, group_desc, | ||
| 109 | unit, description, tip_description, | ||
| 110 | quote_description, recommended, price, sites, url ) | ||
| 111 | VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) | ||
| 112 | RETURNING id', | ||
| 113 | @values{qw/number image title category group_master | ||
| 114 | group_desc unit description tip_description quote_description recommended/}, | ||
| 115 | $values{price}, | ||
| 116 | $values{sites}, | ||
| 117 | $url, | ||
| 118 | )->hash->{id}; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub delete { | ||
| 122 | my $self = shift; | ||
| 123 | my @to_delete = @_; | ||
| 124 | |||
| 125 | s/^\s+|\s+$//g for @to_delete; | ||
| 126 | |||
| 127 | return $self->pg->db->query( | ||
| 128 | 'DELETE FROM products WHERE number IN(' . | ||
| 129 | (join ',', ('?')x@to_delete ) | ||
| 130 | .') RETURNING id', | ||
| 131 | @to_delete, | ||
| 132 | )->hashes->map(sub { $_->{id} }); | ||
| 133 | } | ||
| 134 | |||
| 135 | sub update { | ||
| 136 | my $self = shift; | ||
| 137 | my $id = shift; | ||
| 138 | my %values = @_; | ||
| 139 | my $url = "$values{title} $values{number}" =~ s/\W+/-/gr; | ||
| 140 | |||
| 141 | $self->pg->db->query( | ||
| 142 | 'UPDATE products | ||
| 143 | SET number = ?, image = ?, title = ?, | ||
| 144 | category = ?, group_master = ?, group_desc = ?, | ||
| 145 | unit = ?, description = ?, tip_description = ?, | ||
| 146 | quote_description = ?, recommended = ?, price = ?, | ||
| 147 | url = ? | ||
| 148 | WHERE id = ?', | ||
| 149 | @values{qw/number image title category group_master | ||
| 150 | group_desc unit description tip_description quote_description recommended price/}, | ||
| 151 | $url, | ||
| 152 | $id, | ||
| 153 | ); | ||
| 154 | |||
| 155 | return 1; | ||
| 156 | } | ||
| 157 | |||
| 158 | sub get_all { | ||
| 159 | my $self = shift; | ||
| 160 | my $site = shift; | ||
| 161 | |||
| 162 | my $prods = $self->pg->db->query( | ||
| 163 | $site eq '*' ? 'SELECT * FROM products ORDER BY number' | ||
| 164 | : ( | ||
| 165 | 'SELECT * FROM products WHERE sites ~ ? ORDER BY number', | ||
| 166 | '(^|,)' . quotemeta($self->site) . '(,|$)', | ||
| 167 | ) | ||
| 168 | )->hashes; | ||
| 169 | |||
| 170 | return $site eq '*' ? $prods : $self->_process_products($prods); | ||
| 171 | } | ||
| 172 | |||
| 173 | sub get_category { | ||
| 174 | my $self = shift; | ||
| 175 | my $category = shift; | ||
| 176 | |||
| 177 | $category =~ s{^/}{}; | ||
| 178 | my $cat_line = $category =~ s{/}{*::*}gr; | ||
| 179 | |||
| 180 | my $data = $self->pg->db->query( | ||
| 181 | q{SELECT * FROM products WHERE sites ~ ? AND category ~ ? | ||
| 182 | AND ( | ||
| 183 | group_master IS NULL | ||
| 184 | OR group_master = '' | ||
| 185 | OR group_master = number | ||
| 186 | )}, | ||
| 187 | '(^|,)' . quotemeta($self->site) . '(,|$)', | ||
| 188 | '\[' . quotemeta($cat_line), | ||
| 189 | )->hashes; | ||
| 190 | |||
| 191 | $data = $self->_process_products($data); | ||
| 192 | |||
| 193 | # Right now we might have products that should not show up, since | ||
| 194 | # they are deeper than where we are right now. We need to | ||
| 195 | # get just cat names that lead to them and we'll only show them | ||
| 196 | # at the current level | ||
| 197 | |||
| 198 | # We basically have 3 cases: | ||
| 199 | # 1) Products at current level | ||
| 200 | # 2) Products in 1 category below that we're listing under | ||
| 201 | # subcat headers | ||
| 202 | # 3) Products in >1 category below and we'll just | ||
| 203 | # show subcats for them | ||
| 204 | |||
| 205 | my @cat_bits = split /\Q*::*\E/, $cat_line; | ||
| 206 | splice @cat_bits, -2 if @cat_bits > 2; | ||
| 207 | |||
| 208 | my $current_level_re = qr/\Q[$cat_line]\E/; | ||
| 209 | |||
| 210 | # we have a special case of being at the top-most of the chain | ||
| 211 | # solve it like this for now: | ||
| 212 | my $top_most_sep = length $cat_line ? '\*::\*' : ''; | ||
| 213 | |||
| 214 | my $one_below_re = qr/ | ||
| 215 | \Q[$cat_line\E # our current location in the chain | ||
| 216 | $top_most_sep # separator for a subcat | ||
| 217 | ((?:.(?!\*::\*))*?) # ensure there are no more cat separators | ||
| 218 | # ... but test only until the closing category | ||
| 219 | # ... block, since we can have multiple category | ||
| 220 | # ... blocks past our current point | ||
| 221 | \] # end of current category block | ||
| 222 | /x; | ||
| 223 | my $sub_only_re = qr/ | ||
| 224 | \Q[$cat_line\E # our current location in the chain | ||
| 225 | $top_most_sep | ||
| 226 | ( # grab both, sub cat and sub-sub cat | ||
| 227 | (?:.*?)\*::\* | ||
| 228 | .*? | ||
| 229 | )(?:\*::\*|\]) # we check we have more than one separator | ||
| 230 | # ... which means there's more than one subcat | ||
| 231 | # ... below us in this category block | ||
| 232 | /x; | ||
| 233 | |||
| 234 | my %cats; | ||
| 235 | for ( @$data ) { | ||
| 236 | if ( $_->{category} =~ /$current_level_re/ ) { | ||
| 237 | $_->{display_product} = 1; | ||
| 238 | } | ||
| 239 | elsif ( $_->{category} =~ /$one_below_re/ ) { | ||
| 240 | $_->{display_sub_cat} = $1; | ||
| 241 | $cats{ $1 }++; | ||
| 242 | } | ||
| 243 | elsif ( $_->{category} =~ /$sub_only_re/ ) { | ||
| 244 | $_->{display_sub_only} = $1; | ||
| 245 | $cats{ (split /\*::\*/, $_->{display_sub_only})[0] }++; | ||
| 246 | } | ||
| 247 | } | ||
| 248 | |||
| 249 | my @return = sort_by { $_->{title} } | ||
| 250 | extract_by { $_->{display_product} } @$data; | ||
| 251 | |||
| 252 | for my $cat ( sort { $self->_custom_sort($cat_line) } | ||
| 253 | sort keys %cats | ||
| 254 | ) { | ||
| 255 | push @return, { | ||
| 256 | is_subcat => 1, | ||
| 257 | title => $cat, | ||
| 258 | cat_url => ( | ||
| 259 | length $category ? "$category/$cat" : $cat | ||
| 260 | ), | ||
| 261 | contents => [ | ||
| 262 | sort_by { $_->{title} } extract_by { | ||
| 263 | ($_->{display_sub_cat}//'') eq $cat | ||
| 264 | or ($_->{display_sub_only}//'') =~ /^\Q$cat\E/ | ||
| 265 | } @$data | ||
| 266 | ], | ||
| 267 | } | ||
| 268 | } | ||
| 269 | |||
| 270 | for my $c ( @return ) { | ||
| 271 | $c->{contents} or next; | ||
| 272 | |||
| 273 | my %sub_cats; | ||
| 274 | $sub_cats{ (split /\Q*::*\E/, $_->{display_sub_only})[1] }++ | ||
| 275 | for extract_by { $_->{display_sub_only} } @{ $c->{contents} }; | ||
| 276 | |||
| 277 | push @{ $c->{contents} }, map +{ | ||
| 278 | title => $_, | ||
| 279 | cat_url => "$c->{cat_url}/$_", | ||
| 280 | is_subsub_cat => 1, | ||
| 281 | }, sort { $self->_custom_sort($cat_line, 'sub', $c->{title}) } | ||
| 282 | sort keys %sub_cats; | ||
| 283 | } | ||
| 284 | |||
| 285 | my ( $return_path, $return_name ); | ||
| 286 | if ( length $category ) { | ||
| 287 | $return_path = $category =~ s{(^|/)[^/]+$}{}r; | ||
| 288 | $return_name = (split '/', $return_path)[-1]; | ||
| 289 | } | ||
| 290 | |||
| 291 | $category =~ s{(^|/)[^/]+}{}; | ||
| 292 | |||
| 293 | my @top_no_cat = extract_by { $_->{display_product} } @return; | ||
| 294 | |||
| 295 | unshift @return, { | ||
| 296 | contents => \@top_no_cat, | ||
| 297 | 'is_subcat' => 1, | ||
| 298 | no_cat => 1, | ||
| 299 | } if @top_no_cat; | ||
| 300 | |||
| 301 | return ( \@return, $return_path, $return_name ); | ||
| 302 | } | ||
| 303 | |||
| 304 | sub _custom_sort { | ||
| 305 | my ( $self, $cat_line, $is_subcat, $subcat ) = @_; | ||
| 306 | |||
| 307 | # TODO: refactor this mess | ||
| 308 | my @cats; | ||
| 309 | if ( $is_subcat ) { | ||
| 310 | my $top_bit = length $cat_line ? $cat_line . '*::*' : ''; | ||
| 311 | @cats = grep /^\Q$top_bit$subcat\E/, | ||
| 312 | @{ $self->custom_cat_sorting || [] }; | ||
| 313 | s/^\Q$top_bit$subcat\E(?:\Q*::*\E)?// for @cats; | ||
| 314 | } | ||
| 315 | else { | ||
| 316 | my $strip_line = $cat_line; | ||
| 317 | $cat_line =~ s/\Q*::*\E(?!\Q*::*\E).*//; | ||
| 318 | @cats = grep /^\Q$cat_line\E/, @{ $self->custom_cat_sorting || [] }; | ||
| 319 | s/^\Q$strip_line\E(?:\Q*::*\E)?// for @cats; | ||
| 320 | } | ||
| 321 | |||
| 322 | @cats = grep length, uniq @cats; | ||
| 323 | |||
| 324 | my $counter = 1; | ||
| 325 | my %order = map +( $_ => $counter++ ), @cats; | ||
| 326 | |||
| 327 | return 0 if ( not $order{$b} | ||
| 328 | and not $order{$b} | ||
| 329 | ) or not $order{$b}; | ||
| 330 | |||
| 331 | return 1 if not $order{$a}; | ||
| 332 | return $order{$a} <=> $order{$b} | ||
| 333 | } | ||
| 334 | |||
| 335 | sub unset_site { | ||
| 336 | my ( $self, $site, $products ) = @_; | ||
| 337 | return unless length $site; | ||
| 338 | |||
| 339 | my $prods = $self->pg->db->query( | ||
| 340 | 'SELECT sites, price, number FROM products' | ||
| 341 | . ( scalar(@{$products||[]}) ? ' WHERE number = ANY (?)' : '' ), | ||
| 342 | |||
| 343 | scalar(@{$products||[]}) ? $products : (), | ||
| 344 | )->hashes; | ||
| 345 | |||
| 346 | for ( @$prods ) { | ||
| 347 | $_->{sites} =~ s/(^|,)\Q$site\E(,|$)//g; | ||
| 348 | |||
| 349 | # Remove site's pricing | ||
| 350 | my $p = $_->{price}->$json; | ||
| 351 | delete $p->{ $site }; | ||
| 352 | $_->{price} = $p->$json; | ||
| 353 | |||
| 354 | $self->pg->db->query( | ||
| 355 | q{UPDATE products SET sites = ?, price = ? WHERE number = ?}, | ||
| 356 | @$_{qw/sites price number/} | ||
| 357 | ); | ||
| 358 | } | ||
| 359 | } | ||
| 360 | |||
| 361 | sub set_site { | ||
| 362 | my ( $self, $site, $products ) = @_; | ||
| 363 | return unless length $site; | ||
| 364 | |||
| 365 | $self->unset_site( $site, $products ); | ||
| 366 | return unless $products and @$products; | ||
| 367 | |||
| 368 | $self->pg->db->query( | ||
| 369 | 'UPDATE products SET sites = sites || ? | ||
| 370 | WHERE number = ANY (?)', | ||
| 371 | ",$site", | ||
| 372 | $products, | ||
| 373 | ); | ||
| 374 | |||
| 375 | # TODO: get rid of this stupid hack | ||
| 376 | $self->pg->db->query( q{UPDATE products SET sites | ||
| 377 | = regexp_replace(sites, '(^,)|(,,)|(,$)', '', 'g')}, | ||
| 378 | ); | ||
| 379 | } | ||
| 380 | |||
| 381 | sub set_pricing { | ||
| 382 | my ( $self, $prods ) = @_; | ||
| 383 | return unless $prods and @$prods; | ||
| 384 | |||
| 385 | my $db = $self->pg->db; | ||
| 386 | for my $prod ( @$prods ) { | ||
| 387 | my $prod_price = $db->query( | ||
| 388 | 'SELECT price FROM products WHERE number = ?', | ||
| 389 | $prod->{num}, | ||
| 390 | )->hash->{price}->$json; | ||
| 391 | |||
| 392 | for ( $prod->{price}->split_comma->@* ) { | ||
| 393 | my ( $region, $price ) = split /_/; | ||
| 394 | $prod_price->{ $self->site }{ $region } = $price; | ||
| 395 | } | ||
| 396 | |||
| 397 | $db->query( | ||
| 398 | 'UPDATE products SET price = ? WHERE number = ?', | ||
| 399 | $prod_price->$json, | ||
| 400 | $prod->{num}, | ||
| 401 | ); | ||
| 402 | } | ||
| 403 | } | ||
| 404 | |||
| 405 | sub _process_products { | ||
| 406 | my ( $self, $data ) = @_; | ||
| 407 | return unless $data; | ||
| 408 | |||
| 409 | my %units = ( | ||
| 410 | each => 'eaches', | ||
| 411 | box => 'boxes', | ||
| 412 | pair => 'pairs', | ||
| 413 | case => 'cases', | ||
| 414 | pack => 'packs', | ||
| 415 | ); | ||
| 416 | |||
| 417 | my $region = $self->pricing_region; | ||
| 418 | for my $product ( blessed($data) ? @$data : $data ) { | ||
| 419 | $_ = markdown $_//'' for $product->{description}; | ||
| 420 | |||
| 421 | $product->{price_raw} = $product->{price}; | ||
| 422 | |||
| 423 | my $pr = $product->{price}->$json->{ $self->site }; | ||
| 424 | $pr = $pr->{ $region } // $pr->{ '00' } | ||
| 425 | if ref $pr; | ||
| 426 | $product->{price} = $pr // -1; | ||
| 427 | |||
| 428 | $product->{contact_for_pricing} = 1 | ||
| 429 | if $product->{price} == -1; | ||
| 430 | |||
| 431 | $product->{freebie} = 1 | ||
| 432 | if $product->{price} == 0; | ||
| 433 | |||
| 434 | $product->{price} = sprintf '%.2f', $product->{price}; | ||
| 435 | @$product{qw/price_dollars price_cents/} | ||
| 436 | = split /\./, $product->{price}; | ||
| 437 | |||
| 438 | for ( qw/unit image/ ) { | ||
| 439 | length $product->{$_} or delete $product->{$_}; | ||
| 440 | } | ||
| 441 | $product->{unit} //= 'each'; | ||
| 442 | my ( $unit_noun ) = $product->{unit} =~ /(\w+)/; | ||
| 443 | $product->{unit_multi} = $product->{unit} | ||
| 444 | =~ s/\Q$unit_noun\E/$units{ $unit_noun }/gr; | ||
| 445 | } | ||
| 446 | |||
| 447 | return $data; | ||
| 448 | } | ||
| 449 | |||
| 450 | 1; | ||
| 451 | |||
| 452 | __END__ | ||