File Coverage

blib/lib/App/Web/Oof.pm
Criterion Covered Total %
statement 51 259 19.6
branch 0 60 0.0
condition 0 37 0.0
subroutine 17 42 40.4
pod 0 21 0.0
total 68 419 16.2


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