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