File Coverage

blib/lib/App/Web/Oof.pm
Criterion Covered Total %
statement 48 226 21.2
branch 0 56 0.0
condition 0 30 0.0
subroutine 16 37 43.2
pod 0 17 0.0
total 64 366 17.4


line stmt bran cond sub pod time code
1             package App::Web::Oof;
2              
3 1     1   13424 use 5.014000;
  1         2  
4 1     1   3 use strict;
  1         1  
  1         16  
5 1     1   3 use warnings;
  1         3  
  1         24  
6 1     1   517 use utf8;
  1         7  
  1         3  
7 1     1   384 use parent qw/Plack::Component/;
  1         214  
  1         3  
8              
9             our $VERSION = '0.000_006';
10              
11 1     1   10364 use DBIx::Simple;
  1         17072  
  1         25  
12 1     1   448 use Email::Sender::Simple 'sendmail';
  1         103648  
  1         7  
13 1     1   220 use Email::Simple;
  1         1  
  1         17  
14 1     1   471 use File::Slurp;
  1         9447  
  1         57  
15 1     1   680 use HTML::TreeBuilder;
  1         21011  
  1         7  
16 1     1   556 use HTML::Element::Library;
  1         22872  
  1         31  
17 1     1   409 use JSON::MaybeXS qw/encode_json decode_json/;
  1         3957  
  1         49  
18 1     1   385 use Plack::Builder;
  1         3346  
  1         54  
19 1     1   418 use Plack::Request;
  1         39841  
  1         27  
20 1     1   6 use Try::Tiny;
  1         1  
  1         2090  
21              
22             sub HTML::Element::iter3 {
23 0     0 0 0 my ($self, $data, $code) = @_;
24 0         0 my $orig = $self;
25 0         0 my $prev = $orig;
26 0         0 for my $el (@$data) {
27 0         0 my $current = $orig->clone;
28 0         0 $code->($el, $current);
29 0         0 $prev->postinsert($current);
30 0         0 $prev = $current;
31             }
32 0         0 $orig->detach;
33             }
34              
35 0     0 0 0 sub HTML::Element::fid { shift->look_down(id => shift) }
36 0     0 0 0 sub HTML::Element::fclass { shift->look_down(class => qr/\b$_[0]\b/) }
37              
38             ##################################################
39              
40             my %db;
41             my ($form, $continue, $order, $details, $pay);
42              
43             {
44             sub parse_html {
45 5     5 0 20 my $builder = HTML::TreeBuilder->new;
46 5         686 $builder->ignore_unknown(0);
47 5         37 $builder->parse_file("tmpl/$_[0].html");
48 5         24992 $builder
49             }
50              
51             $form = parse_html 'form';
52             $continue = parse_html 'continue';
53             $order = parse_html 'order';
54             $details = parse_html 'details';
55             $pay = parse_html 'pay';
56             }
57              
58 0     0 0   sub stringify_money { sprintf "£%.2f", $_[0] / 100 }
59              
60             sub make_slug {
61 0     0 0   my $slug = $_[0];
62 0           $slug =~ y/ /-/;
63 0           $slug =~ y/a-zA-Z0-9-//cd;
64 0           $slug
65             }
66              
67             sub product_to_schemaorg {
68 0     0 0   my ($include_url, %data) = @_;
69 0 0         my $stock = $data{stock} > 0 ? 'InStock' : 'OutOfStock';
70             +{
71             '@context' => 'http://schema.org/',
72             '@type' => 'Product',
73             name => $data{title},
74             image => "/static/fullpics/$data{product}-1.jpg",
75             description => $data{summary},
76             offers => {
77             '@type' => 'Offer',
78             price => ($data{price} =~ s/(..)$/\.$1/r),
79             priceCurrency => 'GBP',
80             availability => "http://schema.org/$stock",
81 0 0         ($include_url ? (url => "/details/$data{product}/" . make_slug $data{title}) : ())
82             }
83             }
84             }
85              
86             our %highlight;
87             sub form_table_row {
88 0     0 0   my ($data, $tr) = @_;
89 0 0         $tr->attr(class => 'highlight') if $highlight{$data->{product}};
90 0           $tr->fclass($_)->replace_content($data->{$_}) for qw/title subtitle stock/;
91 0           $tr->fclass('price')->replace_content(stringify_money $data->{price});
92 0 0         $tr->fclass('freepost')->detach unless $data->{freepost};
93 0           $tr->fclass('title')->attr('data-product', $data->{product});
94 0           $tr->fclass('title')->attr('href', '/details/'.$data->{product}.'/'.make_slug $data->{title});
95             # $tr->fclass('title')->attr('data-summary', $data->{summary});
96 0           $tr->look_down(_tag => 'input')->attr(max => $data->{stock});
97 0           $tr->look_down(_tag => 'input')->attr(name => 'quant'.$data->{product});
98             }
99              
100             sub form_app {
101 0     0 0   my ($env) = @_;
102 0   0       $db{$$} //= DBIx::Simple->connect($ENV{OOF_DSN} // 'dbi:Pg:');
      0        
103 0           my $req = Plack::Request->new($env);
104              
105 0           local %highlight = map { $_ => 1 } $req->param('highlight');
  0            
106 0           my $data = $db{$$}->select(products => '*', {}, 'product')->hashes;
107 0           my $tree = $form->clone;
108 0           $tree->find('tbody')->find('tr')->iter3($data, \&form_table_row);
109              
110 0           [200, ['Content-type' => 'text/html; charset=utf-8'], [$tree->as_HTML]]
111             }
112              
113             sub continue_table_row {
114 0     0 0   my ($data, $tr) = @_;
115 0           $tr->fclass($_)->replace_content($data->{$_}) for qw/title subtitle quantity/;
116 0 0         $tr->fclass('freepost')->detach unless $data->{freepost};
117 0           $tr->fclass('price')->replace_content(stringify_money $data->{subtotal});
118 0           $tr->fclass('title')->attr('data-product', $data->{product});
119             }
120              
121             sub continue_app {
122 0     0 0   my ($env) = @_;
123 0   0       $db{$$} //= DBIx::Simple->connect($ENV{OOF_DSN} // 'dbi:Pg:');
      0        
124 0           my $tree = $continue->clone;
125 0           my $req = Plack::Request->new($env);
126 0           my $params = $req->body_parameters;
127              
128 0           my ($quant, $quant_freepost, $total, @data, @notes) = (0) x 3;
129 0           for (sort keys %$params) {
130 0 0         next unless /^quant/;
131 0 0         next unless $params->{$_};
132 0           my $data = $db{$$}->select(products => '*', {product => substr $_, 5})->hash;
133 0           $data->{quantity} = $params->{$_};
134 0 0         if ($data->{stock} == 0) {
135 0           push @notes, 'Item is out of stock and was removed from order: '.$data->{title};
136             next
137 0           }
138 0 0         if ($data->{quantity} > $data->{stock}) {
139 0           $data->{quantity} = $data->{stock};
140             push @notes, 'Not enough units of "'.$data->{title}.'" available. Quantity reduced to '.$data->{quantity}
141 0           }
142 0           $data->{subtotal} = $data->{price} * $data->{quantity};
143 0           $quant += $data->{quantity};
144 0 0         $quant_freepost += $data->{quantity} if $data->{freepost};
145 0           $total += $data->{subtotal};
146 0           push @data, $data
147             }
148              
149 0 0         return [500, ['Content-type' => 'text/plain'], ['Error: no items in order.']] unless $quant;
150              
151 0           $tree->fid('subtotal')->replace_content(stringify_money $total);
152 0           my $dvalue;
153 0 0         if ($params->{discount}) {
154 0           my $discount = $db{$$}->select(discounts => '*', {discount => $params->{discount}})->hash;
155 0 0         if (!defined $discount) {
    0          
156 0           push @notes, 'Discount code incorrect. No discount applied.'
157             } elsif ($db{$$}->select(orders => 'COUNT(*)', {discount => $params->{discount}})->list) {
158 0           push @notes, 'Discount code already used once. No discount applied.'
159             } else {
160 0 0         $dvalue = int (0.5 + $discount->{fraction} * $total) if $discount->{fraction};
161 0 0         $dvalue = $discount->{flat} if $discount->{flat};
162 0           $tree->fid('discount')->replace_content('-'.stringify_money $dvalue);
163 0           $total -= $dvalue;
164 0           $tree->look_down(name => 'discount')->attr(value => $params->{discount});
165 0           push @notes, 'Discount applied.'
166             }
167             }
168 0 0         $tree->look_down(name => 'discount')->detach unless $dvalue;
169 0 0         $tree->fid('discount_tr')->detach unless $dvalue;
170 0           my $postage = 220 + 50 * ($quant - $quant_freepost);
171 0 0         $postage = 0 if $quant == $quant_freepost;
172 0           $tree->fid('postage')->replace_content(stringify_money $postage);
173 0           $total += $postage;
174 0           $tree->fid('total')->replace_content(stringify_money $total);
175              
176 0           $tree->fid('order')->find('tbody')->find('tr')->iter3(\@data, \&continue_table_row);
177 0           $tree->iter($tree->fid('notes')->find('li') => @notes);
178              
179 0           $tree->look_down(name => 'products')->attr(value => encode_json \@data);
180 0           $tree->look_down(name => 'total')->attr(value => $total);
181              
182 0           [200, ['Content-type' => 'text/html; charset=utf-8'], [$tree->as_HTML]]
183             }
184              
185             sub order_app {
186 0     0 0   my ($env) = @_;
187 0   0       $db{$$} //= DBIx::Simple->connect($ENV{OOF_DSN} // 'dbi:Pg:');
      0        
188 0           my $tree = $order->clone;
189 0           my $req = Plack::Request->new($env);
190 0           my ($id) = $env->{PATH_INFO} =~ m,^/([0-9A-F]+),;
191 0 0         if ($id) {
192 0 0         my $total = $db{$$}->select(orders => 'total', {id => $id})->list or
193             return [500, ['Content-type', 'text/plain'], ['Order not found']];
194 0           $tree->fid('orderid')->replace_content($id);
195 0           $tree->look_down(name => 'order')->attr(value => $id);
196 0           $tree->fid('total')->replace_content(stringify_money $total);
197 0           $tree->find('script')->attr('data-amount', $total);
198 0           return [200, ['Content-type' => 'text/html; charset=utf-8'], [$tree->as_HTML]]
199             } else {
200 0           my %parms = %{$req->body_parameters};
  0            
201 0           my $id = sprintf "%X%04X", time, $$;
202 0           my $err;
203             try {
204 0     0     $db{$$}->begin_work;
205 0           my $products = decode_json $req->body_parameters->{products};
206 0           for my $prod (@$products) {
207 0           my $stock = $db{$$}->select(products => 'stock', {product => $prod->{product}})->list;
208 0 0         die "Not enough of " .$prod->{title}."\n" if $prod->{quantity} > $stock;
209 0           $db{$$}->update(products => {stock => $stock - $prod->{quantity}}, {product => $prod->{product}});
210             }
211 0           $db{$$}->insert(orders => {id => $id, %parms});
212 0           $db{$$}->commit;
213             sendmail (Email::Simple->create(
214             header => [
215             From => $ENV{OOF_EMAIL_FROM},
216             To => $ENV{OOF_EMAIL_TO},
217             Subject => "Order $id placed",
218             ],
219             body => 'A new order was placed.',
220 0 0         )) if $ENV{OOF_EMAIL_TO};
221             } catch {
222 0     0     $db{$$}->rollback;
223 0           $err = [500, ['Content-type', 'text/plain'], ["Error: $_"]]
224 0           };
225 0 0         return $err if $err;
226 0           return [303, [Location => "/order/$id"], []]
227             }
228             }
229              
230             sub cancel {
231 0     0 0   my ($order) = @_;
232 0   0       $db{$$} //= DBIx::Simple->connect($ENV{OOF_DSN} // 'dbi:Pg:');
      0        
233 0           $order = $db{$$}->select(orders => '*', {id => $order})->hash;
234 0           my $products = decode_json $order->{products};
235 0           $db{$$}->begin_work;
236             try {
237 0     0     for my $prod (@$products) {
238 0           my $stock = $db{$$}->select(products => 'stock', {product => $prod->{product}})->list;
239 0           $db{$$}->update(products => {stock => $stock + $prod->{quantity}}, {product => $prod->{product}});
240             }
241 0           $db{$$}->delete(orders => {id => $order->{id}});
242 0           $db{$$}->commit;
243             } catch {
244 0     0     $db{$$}->rollback;
245 0           die $_
246             }
247 0           }
248              
249             sub details_list_element {
250 0     0 0   my ($data, $li) = @_;
251 0           $li->find('a')->attr(href => "/$data");
252 0           my $thumb = $data =~ s/fullpics/thumbs/r;
253 0 0         $thumb = $data unless -f $thumb;
254 0           $li->find('img')->attr(src => "/$thumb");
255             }
256              
257             sub details_app {
258 0     0 0   my ($env) = @_;
259 0   0       $db{$$} //= DBIx::Simple->connect($ENV{OOF_DSN} // 'dbi:Pg:');
      0        
260 0           my $tree = $details->clone;
261 0           my ($id) = $env->{PATH_INFO} =~ m,^/(\d+),;
262 0           my %data = %{$db{$$}->select(products => '*', {product => $id})->hash};
  0            
263 0           my @pics = ;
264 0           my $slug = make_slug $data{title};
265 0           $tree->find('title')->replace_content("$data{title} | ledparts4you");
266 0           $tree->find('h2')->replace_content($data{title});
267 0           $tree->fid('summary')->replace_content($data{summary});
268 0           $tree->look_down(rel => 'canonical')->attr(href => "/details/$id/$slug");
269 0           $tree->fid('pictures')->find('li')->iter3(\@pics, \&details_list_element);
270 0           $tree->fid('jsonld')->replace_content(encode_json product_to_schemaorg '', %data);
271              
272 0           for my $ahref ($tree->find('a')) {
273 0 0         $ahref->attr(href => "/form?highlight=$id") if $ahref->attr('href') eq '/';
274             }
275              
276 0           [200, ['Content-type' => 'text/html; charset=utf-8'], [$tree->as_HTML]]
277             }
278              
279             sub pay_app {
280 0     0 0   my ($env) = @_;
281 0           my $req = Plack::Request->new($env);
282 0   0       $db{$$} //= DBIx::Simple->connect($ENV{OOF_DSN} // 'dbi:Pg:');
      0        
283 0           my $order = $req->body_parameters->{order};
284 0           my $token = $req->body_parameters->{stripeToken};
285 0 0         return [500, ['Content-type' => 'text/html; charset=utf-8'], ['No token received, payment did not succeed.']] unless $token;
286 0           $db{$$}->update(orders => {stripe_token => $token}, {id => $order});
287 0           [200, ['Content-type' => 'text/html; charset=utf-8'], [$pay->as_HTML]];
288             }
289              
290             sub app {
291 0     0 0   my $footer = read_file 'tmpl/footer.html';
292             builder {
293             enable sub {
294 0           my $app = shift;
295             sub {
296 0           my $res = $app->(@_);
297 0 0         $res->[2][0] =~ s,,$footer, if $res->[0] == 200;
298 0           $res;
299             }
300 0     0     };
  0            
301 0           mount '/' => sub { [301, [Location => '/form'], []] };
  0            
302 0           mount '/form' => \&form_app;
303 0           mount '/continue' => \&continue_app;
304 0           mount '/order' => \&order_app;
305 0           mount '/details' => \&details_app;
306 0           mount '/pay' => \&pay_app;
307             }
308 0           }
309              
310             1;
311             __END__