File Coverage

blib/lib/App/Web/Oof.pm
Criterion Covered Total %
statement 51 278 18.3
branch 0 68 0.0
condition 0 37 0.0
subroutine 17 42 40.4
pod 0 21 0.0
total 68 446 15.2


line stmt bran cond sub pod time code
1             package App::Web::Oof;
2              
3 1     1   14052 use 5.014000;
  1         4  
4 1     1   4 use strict;
  1         1  
  1         18  
5 1     1   3 use warnings;
  1         5  
  1         26  
6 1     1   502 use utf8;
  1         7  
  1         4  
7 1     1   371 use parent qw/Plack::Component/;
  1         269  
  1         3  
8              
9             our $VERSION = '0.000_008';
10              
11 1     1   10103 use DBIx::Simple;
  1         17050  
  1         26  
12 1     1   509 use Email::Sender::Simple 'sendmail';
  1         105263  
  1         6  
13 1     1   248 use Email::Simple;
  1         1  
  1         19  
14 1     1   463 use File::Slurp;
  1         9544  
  1         74  
15 1     1   670 use HTML::TreeBuilder;
  1         21468  
  1         9  
16 1     1   643 use HTML::Element::Library;
  1         23248  
  1         38  
17 1     1   429 use JSON::MaybeXS qw/encode_json decode_json/;
  1         4031  
  1         48  
18 1     1   374 use Plack::Builder;
  1         3264  
  1         55  
19 1     1   405 use Plack::Request;
  1         40462  
  1         29  
20 1     1   463 use Text::CSV;
  1         7284  
  1         6  
21 1     1   33 use Try::Tiny;
  1         1  
  1         2736  
22              
23             sub HTML::Element::iter3 {
24 0     0 0 0 my ($self, $data, $code) = @_;
25 0         0 my $orig = $self;
26 0         0 my $prev = $orig;
27 0         0 for my $el (@$data) {
28 0         0 my $current = $orig->clone;
29 0         0 $code->($el, $current);
30 0         0 $prev->postinsert($current);
31 0         0 $prev = $current;
32             }
33 0         0 $orig->detach;
34             }
35              
36 0     0 0 0 sub HTML::Element::fid { shift->look_down(id => shift) }
37 0     0 0 0 sub HTML::Element::fclass { shift->look_down(class => qr/\b$_[0]\b/) }
38              
39             ##################################################
40              
41             my $postage_base = $ENV{OOF_POSTAGE_BASE} // 225;
42             my $postage_per_item = $ENV{OOF_POSTAGE_PER_ITEM} // 50;
43              
44             ##################################################
45              
46             my %db;
47             my ($form, $continue, $order, $details, $pay, $display, $down);
48              
49             {
50             sub parse_html {
51 7     7 0 32 my $builder = HTML::TreeBuilder->new;
52 7         988 $builder->ignore_unknown(0);
53 7         58 $builder->parse_file("tmpl/$_[0].html");
54 7         32953 $builder
55             }
56              
57             $form = parse_html 'form';
58             $continue = parse_html 'continue';
59             $order = parse_html 'order';
60             $details = parse_html 'details';
61             $pay = parse_html 'pay';
62             $display = parse_html 'display';
63             $down = parse_html 'down';
64             }
65              
66 0     0 0   sub stringify_money { sprintf "£%.2f", $_[0] / 100 }
67              
68             sub make_slug {
69 0     0 0   my $slug = $_[0];
70 0           $slug =~ y/ /-/;
71 0           $slug =~ y/a-zA-Z0-9-//cd;
72 0           $slug
73             }
74              
75             sub product_to_schemaorg {
76 0     0 0   my ($include_url, %data) = @_;
77 0 0         my $stock = $data{stock} > 0 ? 'InStock' : 'OutOfStock';
78 0           my @extra;
79 0 0         push @extra, (brand => {'@type' => 'Brand', name => $data{brand}}) if $data{brand};
80 0 0         push @extra, (model => $data{model}) if $data{model};
81             +{
82             '@context' => 'http://schema.org/',
83             '@type' => 'Product',
84             name => $data{title},
85             image => "/static/fullpics/$data{product}-1.jpg",
86             description => $data{subtitle},
87             @extra,
88             offers => {
89             '@type' => 'Offer',
90             price => ($data{price} =~ s/(..)$/\.$1/r),
91             priceCurrency => 'GBP',
92             availability => "http://schema.org/$stock",
93 0 0         ($include_url ? (url => "/details/$data{product}/" . make_slug $data{title}) : ())
94             }
95             }
96             }
97              
98             our %highlight;
99             sub form_table_row {
100 0     0 0   my ($data, $tr) = @_;
101 0 0         $tr->attr(class => 'highlight') if $highlight{$data->{product}};
102 0           $tr->fclass($_)->replace_content($data->{$_}) for qw/title subtitle stock/;
103 0           $tr->fclass('price')->replace_content(stringify_money $data->{price});
104 0 0         $tr->fclass('freepost')->detach unless $data->{freepost};
105 0           $tr->fclass('title')->attr('data-product', $data->{product});
106 0           $tr->fclass('title')->attr('href', '/details/'.$data->{product}.'/'.make_slug $data->{title});
107             # $tr->fclass('title')->attr('data-summary', $data->{summary});
108 0           $tr->look_down(_tag => 'input')->attr(max => $data->{stock});
109 0           $tr->look_down(_tag => 'input')->attr(name => 'quant'.$data->{product});
110             }
111              
112             sub form_app {
113 0     0 0   my ($env) = @_;
114 0   0       $db{$$} //= DBIx::Simple->connect($ENV{OOF_DSN} // 'dbi:Pg:');
      0        
115 0           my $req = Plack::Request->new($env);
116              
117 0           local %highlight = map { $_ => 1 } $req->param('highlight');
  0            
118 0           my $data = $db{$$}->select(products => '*', {stock => {'>', 0}}, 'product')->hashes;
119 0           my $tree = $form->clone;
120 0           $tree->find('tbody')->find('tr')->iter3($data, \&form_table_row);
121              
122 0           [200, ['Content-type' => 'text/html; charset=utf-8'], [$tree->as_HTML]]
123             }
124              
125             sub continue_table_row {
126 0     0 0   my ($data, $tr) = @_;
127 0           $tr->fclass($_)->replace_content($data->{$_}) for qw/title subtitle quantity/;
128 0 0         $tr->fclass('freepost')->detach unless $data->{freepost};
129 0           $tr->fclass('price')->replace_content(stringify_money $data->{subtotal});
130 0           $tr->fclass('title')->attr('data-product', $data->{product});
131             }
132              
133             sub continue_app {
134 0     0 0   my ($env) = @_;
135 0   0       $db{$$} //= DBIx::Simple->connect($ENV{OOF_DSN} // 'dbi:Pg:');
      0        
136 0           my $tree = $continue->clone;
137 0           my $req = Plack::Request->new($env);
138 0           my $params = $req->body_parameters;
139              
140 0           my ($quant, $quant_freepost, $total, @data, @notes) = (0) x 3;
141 0           for (sort keys %$params) {
142 0 0         next unless /^quant/;
143 0 0         next unless $params->{$_};
144 0           my $data = $db{$$}->select(products => '*', {product => substr $_, 5})->hash;
145 0           $data->{quantity} = $params->{$_};
146 0 0         if ($data->{stock} == 0) {
147 0           push @notes, 'Item is out of stock and was removed from order: '.$data->{title};
148             next
149 0           }
150 0 0         if ($data->{quantity} > $data->{stock}) {
151 0           $data->{quantity} = $data->{stock};
152             push @notes, 'Not enough units of "'.$data->{title}.'" available. Quantity reduced to '.$data->{quantity}
153 0           }
154 0           $data->{subtotal} = $data->{price} * $data->{quantity};
155 0           $quant += $data->{quantity};
156 0 0         $quant_freepost += $data->{quantity} if $data->{freepost};
157 0           $total += $data->{subtotal};
158 0           push @data, $data
159             }
160              
161 0 0         return [500, ['Content-type' => 'text/plain'], ['Error: no items in order.']] unless $quant;
162              
163 0           $tree->fid('subtotal')->replace_content(stringify_money $total);
164 0           my $dvalue;
165 0 0         if ($params->{discount}) {
166 0           my $discount = $db{$$}->select(discounts => '*', {discount => $params->{discount}})->hash;
167 0 0         if (!defined $discount) {
    0          
168 0           push @notes, 'Discount code incorrect. No discount applied.'
169             } elsif ($db{$$}->select(orders => 'COUNT(*)', {discount => $params->{discount}})->list) {
170 0           push @notes, 'Discount code already used once. No discount applied.'
171             } else {
172 0 0         $dvalue = int (0.5 + $discount->{fraction} * $total) if $discount->{fraction};
173 0 0         $dvalue = $discount->{flat} if $discount->{flat};
174 0           $tree->fid('discount')->replace_content('-'.stringify_money $dvalue);
175 0           $total -= $dvalue;
176 0           $tree->look_down(name => 'discount')->attr(value => $params->{discount});
177 0           push @notes, 'Discount applied.'
178             }
179             }
180 0 0         $tree->look_down(name => 'discount')->detach unless $dvalue;
181 0 0         $tree->fid('discount_tr')->detach unless $dvalue;
182 0           my $postage = $postage_base + $postage_per_item * ($quant - $quant_freepost);
183 0 0         $postage = 0 if $quant == $quant_freepost;
184 0           $tree->fid('postage')->replace_content(stringify_money $postage);
185 0           $total += $postage;
186 0           $tree->fid('total')->replace_content(stringify_money $total);
187              
188 0           $tree->fid('order')->find('tbody')->find('tr')->iter3(\@data, \&continue_table_row);
189 0           $tree->iter($tree->fid('notes')->find('li') => @notes);
190              
191 0           $tree->look_down(name => 'products')->attr(value => encode_json \@data);
192 0           $tree->look_down(name => 'total')->attr(value => $total);
193              
194 0           [200, ['Content-type' => 'text/html; charset=utf-8'], [$tree->as_HTML]]
195             }
196              
197             sub order_app {
198 0     0 0   my ($env) = @_;
199 0   0       $db{$$} //= DBIx::Simple->connect($ENV{OOF_DSN} // 'dbi:Pg:');
      0        
200 0           my $tree = $order->clone;
201 0           my $req = Plack::Request->new($env);
202 0           my ($id) = $env->{PATH_INFO} =~ m,^/([0-9A-F]+),;
203 0 0         if ($id) {
204 0 0         my $total = $db{$$}->select(orders => 'total', {id => $id})->list or
205             return [500, ['Content-type', 'text/plain'], ['Order not found']];
206 0           $tree->fid('orderid')->replace_content($id);
207 0           $tree->look_down(name => 'order')->attr(value => $id);
208 0           $tree->fid('total')->replace_content(stringify_money $total);
209 0           $tree->find('script')->attr('data-amount', $total);
210 0           return [200, ['Content-type' => 'text/html; charset=utf-8'], [$tree->as_HTML]]
211             } else {
212 0           my %parms = %{$req->body_parameters};
  0            
213 0           my $id = sprintf "%X%04X", time, $$;
214 0           my $err;
215             try {
216 0     0     $db{$$}->begin_work;
217 0           my $products = decode_json $req->body_parameters->{products};
218 0           for my $prod (@$products) {
219 0           my $stock = $db{$$}->select(products => 'stock', {product => $prod->{product}})->list;
220 0 0         die "Not enough of " .$prod->{title}."\n" if $prod->{quantity} > $stock;
221 0           $db{$$}->update(products => {stock => $stock - $prod->{quantity}}, {product => $prod->{product}});
222             }
223 0           $db{$$}->insert(orders => {id => $id, date => time, %parms});
224 0           $db{$$}->commit;
225             sendmail (Email::Simple->create(
226             header => [
227             From => $ENV{OOF_EMAIL_FROM},
228             To => $ENV{OOF_EMAIL_TO},
229             Subject => "Order $id placed for ".stringify_money($parms{total}),
230             ],
231             body => 'A new order was placed.',
232 0 0         )) if $ENV{OOF_EMAIL_TO};
233             } catch {
234 0     0     $db{$$}->rollback;
235 0           $err = [500, ['Content-type', 'text/plain'], ["Error: $_"]]
236 0           };
237 0 0         return $err if $err;
238 0           return [303, [Location => "/order/$id"], []]
239             }
240             }
241              
242             sub cancel {
243 0     0 0   my ($order) = @_;
244 0   0       $db{$$} //= DBIx::Simple->connect($ENV{OOF_DSN} // 'dbi:Pg:');
      0        
245 0           $order = $db{$$}->select(orders => '*', {id => $order})->hash;
246 0           my $products = decode_json $order->{products};
247 0           $db{$$}->begin_work;
248             try {
249 0     0     for my $prod (@$products) {
250 0           my $stock = $db{$$}->select(products => 'stock', {product => $prod->{product}})->list;
251 0           $db{$$}->update(products => {stock => $stock + $prod->{quantity}}, {product => $prod->{product}});
252             }
253 0           $db{$$}->delete(orders => {id => $order->{id}});
254 0           $db{$$}->commit;
255             } catch {
256 0     0     $db{$$}->rollback;
257 0           die $_
258             }
259 0           }
260              
261             sub details_list_element {
262 0     0 0   my ($data, $li) = @_;
263 0           $li->find('a')->attr(href => "/$data");
264 0           my $thumb = $data =~ s/fullpics/thumbs/r;
265 0 0         $thumb = $data unless -f $thumb;
266 0           $li->find('img')->attr(src => "/$thumb");
267             }
268              
269             sub details_app {
270 0     0 0   my ($env) = @_;
271 0   0       $db{$$} //= DBIx::Simple->connect($ENV{OOF_DSN} // 'dbi:Pg:');
      0        
272 0           my $tree = $details->clone;
273 0           my ($id) = $env->{PATH_INFO} =~ m,^/(\d+),;
274 0           my %data = %{$db{$$}->select(products => '*', {product => $id})->hash};
  0            
275 0           my @pics = ;
276 0           my $slug = make_slug $data{title};
277 0           $tree->find('title')->replace_content("$data{title} | ledparts4you");
278 0           $tree->find('h2')->replace_content($data{title});
279 0           my $summary_literal = HTML::Element::Library::super_literal $data{summary};
280 0           $tree->fid('summary')->replace_content($summary_literal);
281 0           $tree->look_down(rel => 'canonical')->attr(href => "/details/$id/$slug");
282 0           $tree->fid('pictures')->find('li')->iter3(\@pics, \&details_list_element);
283 0           $tree->fid('jsonld')->replace_content(encode_json product_to_schemaorg '', %data);
284              
285 0           $tree->fid('dd_stock')->replace_content($data{stock});
286 0           $tree->fid('dd_price')->replace_content(stringify_money $data{price});
287 0           for (qw/brand model/) {
288 0 0         if ($data{$_}) {
289 0           $tree->fid("dd_$_")->replace_content($data{$_});
290             } else {
291 0           $tree->fid("dt_$_")->detach;
292 0           $tree->fid("dd_$_")->detach;
293             }
294             }
295              
296 0           for my $ahref ($tree->find('a')) {
297 0 0         $ahref->attr(href => "/form?highlight=$id") if $ahref->attr('href') eq '/';
298             }
299              
300 0           [200, ['Content-type' => 'text/html; charset=utf-8'], [$tree->as_HTML]]
301             }
302              
303             sub pay_app {
304 0     0 0   my ($env) = @_;
305 0           my $req = Plack::Request->new($env);
306 0   0       $db{$$} //= DBIx::Simple->connect($ENV{OOF_DSN} // 'dbi:Pg:');
      0        
307 0           my $order = $req->body_parameters->{order};
308 0           my $token = $req->body_parameters->{stripeToken};
309 0 0         return [500, ['Content-type' => 'text/html; charset=utf-8'], ['No token received, payment did not succeed.']] unless $token;
310 0           $db{$$}->update(orders => {stripe_token => $token}, {id => $order});
311 0           [200, ['Content-type' => 'text/html; charset=utf-8'], [$pay->as_HTML]];
312             }
313              
314             sub display_table_row {
315 0     0 0   my ($data, $tr) = @_;
316 0           $tr->fclass($_)->replace_content($data->{$_}) for qw/title subtitle quantity/;
317 0 0         $tr->fclass('freepost')->detach unless $data->{freepost};
318 0           $tr->fclass('price')->replace_content(stringify_money $data->{subtotal});
319 0           $tr->fclass('title')->attr('data-product', $data->{product});
320             }
321              
322             sub display_order {
323 0     0 0   my ($data, $div) = @_;
324 0           my @products = @{decode_json $data->{products}};
  0            
325 0           $div->find('table')->iter3(\@products, \&display_table_row);
326 0           $div->fclass('name')->replace_content($data->{first_name} . ' ' . $data->{last_name});
327 0 0         $div->fclass('stripe_token')->replace_content($data->{stripe_token}) if $data->{stripe_token};
328             }
329              
330             my $csv_header = 'Address_line_1,Address_line_2,Address_line_3,Address_line_4,Postcode,First_name,Last_name,Email,Weight(Kg),Compensation(�),Signature(y/n),Reference,Contents,Parcel_value(�),Delivery_phone,Delivery_safe_place,Delivery_instructions' . "\n";
331              
332             sub make_hermes_csv {
333 0     0 0   my ($order) = @_;
334 0           my $csv = Text::CSV->new;
335 0           my @fields = map { $order->{$_} } qw/address1 address2 address3 address4 postcode first_name last_name email/;
  0            
336 0           $csv->combine(@fields, '', '', 'n', '', '', '', $order->{phone}, $order->{safe_place}, $order->{instructions});
337 0           $csv->string
338             }
339              
340             sub display_app {
341 0     0 0   my ($env) = @_;
342 0   0       $db{$$} //= DBIx::Simple->connect($ENV{OOF_DSN} // 'dbi:Pg:');
      0        
343 0           my $req = Plack::Request->new($env);
344 0   0       my $n = int ($req->param('n') // 10);
345 0           my @orders = $db{$$}->query("SELECT * FROM orders ORDER BY date DESC LIMIT $n")->hashes;
346 0           my $tree = $display->clone;
347 0           $tree->fclass('order')->iter3(\@orders, \&display_order);
348 0           my $csv = join "\n", map { make_hermes_csv $_ } @orders;
  0            
349 0           $tree->fid('hermes_csv')->replace_content($csv_header . $csv);
350 0           [200, ['Content-type' => 'text/html; charset=utf-8'], [$tree->as_HTML]];
351             }
352              
353             sub app {
354 0     0 0   my $footer = read_file 'tmpl/footer.html';
355             builder {
356             enable sub {
357 0           my $app = shift;
358             sub {
359 0           my $res = $app->(@_);
360 0 0         $res->[2][0] =~ s,,$footer, if $res->[0] == 200;
361 0           $res;
362             }
363 0     0     };
  0            
364             enable sub {
365 0           my $app = shift;
366             sub {
367 0 0         if (-f 'down.html') {
368 0           my $down_lit = HTML::Element::Library::super_literal read_file 'down.html';
369 0           my $tree = $down->clone;
370 0           $tree->fid('content')->replace_content($down_lit);
371 0           return [503, ['Content-type' => 'text/html; charset=utf-8'], [$tree->as_HTML]]
372             }
373 0           $app->(@_)
374             }
375 0           };
  0            
376 0           mount '/' => sub { [301, [Location => '/form'], []] };
  0            
377 0           mount '/form' => \&form_app;
378 0           mount '/continue' => \&continue_app;
379 0           mount '/order' => \&order_app;
380 0           mount '/details' => \&details_app;
381 0           mount '/pay' => \&pay_app;
382 0           mount '/display' => \&display_app;
383             }
384 0           }
385              
386             1;
387             __END__