]>
iEval git - app-web-oof.git/blob - Oof.pm
61c4ecf166cb58d391f590b389673139e11e3f65
7 use parent qw
/Plack::Component/;
9 our $VERSION = '0.000_002';
13 use HTML
::TreeBuilder
;
14 use HTML
::Element
::Library
;
15 use JSON
::MaybeXS qw
/encode_json decode_json/;
20 sub HTML
::Element
::iter3
{
21 my ($self, $data, $code) = @_;
25 my $current = $orig->clone;
26 $code->($el, $current);
27 $prev->postinsert($current);
33 sub HTML
::Element
::fid
{ shift->look_down(id
=> shift) }
34 sub HTML
::Element
::fclass
{ shift->look_down(class => qr/\b$_[0]\b/) }
36 ##################################################
39 my ($form, $continue, $order, $details, $pay);
43 my $builder = HTML
::TreeBuilder
->new;
44 $builder->ignore_unknown(0);
45 $builder->parse_file("tmpl/$_[0].html");
49 $form = parse_html
'form';
50 $continue = parse_html
'continue';
51 $order = parse_html
'order';
52 $details = parse_html
'details';
53 $pay = parse_html
'pay';
56 sub stringify_money
{ sprintf "£%.2f", $_[0] / 100 }
61 $slug =~ y/a-zA-Z0-9-//cd;
67 $tr->fclass($_)->replace_content($data->{$_}) for qw
/title subtitle stock/;
68 $tr->fclass('price')->replace_content(stringify_money
$data->{price
});
69 $tr->fclass('title')->attr('data-product', $data->{product
});
70 $tr->fclass('title')->attr('href', '/details/'.$data->{product
}.'/'.make_slug
$data->{title
});
71 # $tr->fclass('title')->attr('data-summary', $data->{summary});
72 $tr->look_down(_tag
=> 'input')->attr(max
=> $data->{stock
});
73 $tr->look_down(_tag
=> 'input')->attr(name
=> 'quant'.$data->{product
});
78 $db{$$} //= DBIx
::Simple
->connect($ENV{OOF_DSN
} // 'dbi:Pg:');
80 my $data = $db{$$}->select(products
=> '*', {}, 'product')->hashes;
81 my $tree = $form->clone;
82 $tree->find('tbody')->find('tr')->iter3($data, \
&form_table_row
);
84 [200, ['Content-type' => 'text/html; charset=utf-8'], [$tree->as_HTML]]
87 sub continue_table_row
{
89 $tr->fclass($_)->replace_content($data->{$_}) for qw
/title subtitle quantity/;
90 $tr->fclass('price')->replace_content(stringify_money
$data->{subtotal
});
91 $tr->fclass('title')->attr('data-product', $data->{product
});
96 $db{$$} //= DBIx
::Simple
->connect($ENV{OOF_DSN
} // 'dbi:Pg:');
97 my $tree = $continue->clone;
98 my $req = Plack
::Request
->new($env);
99 my $params = $req->body_parameters;
101 my ($quant, $total, @data, @notes);
102 for (sort keys %$params) {
103 next unless /^quant/;
104 next unless $params->{$_};
105 my $data = $db{$$}->select(products
=> '*', {product
=> substr $_, 5})->hash;
106 $data->{quantity
} = $params->{$_};
107 if ($data->{stock
} == 0) {
108 push @notes, 'Item is out of stock and was removed from order: '.$data->{title
};
111 if ($data->{quantity
} > $data->{stock
}) {
112 $data->{quantity
} = $data->{stock
};
113 push @notes, 'Not enough units of "'.$data->{title
}.'" available. Quantity reduced to '.$data->{quantity
}
115 $data->{subtotal
} = $data->{price
} * $data->{quantity
};
116 $quant += $data->{quantity
};
117 $total += $data->{subtotal
};
121 return [500, ['Content-type' => 'text/plain'], ['Error: no items in order.']] unless $quant;
123 $tree->fid('subtotal')->replace_content(stringify_money
$total);
125 if ($params->{discount
}) {
126 my $discount = $db{$$}->select(discounts
=> '*', {discount
=> $params->{discount
}})->hash;
127 if (!defined $discount) {
128 push @notes, 'Discount code incorrect. No discount applied.'
129 } elsif ($db{$$}->select(orders
=> 'COUNT(*)', {discount
=> $params->{discount
}})->list) {
130 push @notes, 'Discount code already used once. No discount applied.'
132 $dvalue = int (0.5 + $discount->{fraction
} * $total) if $discount->{fraction
};
133 $dvalue = $discount->{flat
} if $discount->{flat
};
134 $tree->fid('discount')->replace_content('-'.stringify_money
$dvalue);
136 $tree->look_down(name
=> 'discount')->attr(value
=> $params->{discount
});
137 push @notes, 'Discount applied.'
140 $tree->look_down(name
=> 'discount')->detach unless $dvalue;
141 $tree->fid('discount_tr')->detach unless $dvalue;
142 my $postage = 220 + 50 * $quant;
143 $tree->fid('postage')->replace_content(stringify_money
$postage);
145 $tree->fid('total')->replace_content(stringify_money
$total);
147 $tree->fid('order')->find('tbody')->find('tr')->iter3(\
@data, \
&continue_table_row
);
148 $tree->iter($tree->fid('notes')->find('li') => @notes);
150 $tree->look_down(name
=> 'products')->attr(value
=> encode_json \
@data);
151 $tree->look_down(name
=> 'total')->attr(value
=> $total);
153 [200, ['Content-type' => 'text/html; charset=utf-8'], [$tree->as_HTML]]
158 $db{$$} //= DBIx
::Simple
->connect($ENV{OOF_DSN
} // 'dbi:Pg:');
159 my $tree = $order->clone;
160 my $req = Plack
::Request
->new($env);
161 my ($id) = $env->{PATH_INFO
} =~ m
,^/([0-9A
-F
]+),;
163 my $total = $db{$$}->select(orders
=> 'total', {id
=> $id})->list;
164 $tree->fid('orderid')->replace_content($id);
165 $tree->look_down(name
=> 'order')->attr(value
=> $id);
166 $tree->fid('total')->replace_content(stringify_money
$total);
167 $tree->find('script')->attr('data-amount', $total);
168 return [200, ['Content-type' => 'text/html; charset=utf-8'], [$tree->as_HTML]]
170 my %parms = %{$req->body_parameters};
171 my $id = sprintf "%X%04X", time, $$;
175 my $products = decode_json
$req->body_parameters->{products
};
176 for my $prod (@
$products) {
177 my $stock = $db{$$}->select(products
=> 'stock', {product
=> $prod->{product
}})->list;
178 die "Not enough of " .$prod->{title
}."\n" if $prod->{quantity
} > $stock;
179 $db{$$}->update(products
=> {stock
=> $stock - $prod->{quantity
}}, {product
=> $prod->{product
}});
181 $db{$$}->insert(orders
=> {id
=> $id, %parms});
185 $err = [500, ['Content-type', 'text/plain'], ["Error: $_"]]
188 return [303, [Location
=> "/order/$id"], []]
192 sub details_list_element
{
193 my ($data, $li) = @_;
194 $li->find('a')->attr(href
=> "/$data");
195 my $thumb = $data =~ s/fullpics/thumbs/r;
196 $thumb = $data unless -f
$thumb;
197 $li->find('img')->attr(src
=> "/$thumb");
202 $db{$$} //= DBIx
::Simple
->connect($ENV{OOF_DSN
} // 'dbi:Pg:');
203 my $tree = $details->clone;
204 my ($id) = $env->{PATH_INFO
} =~ m
,^/(\d
+),;
205 my $title = $db{$$}->select(products
=> 'title', {product
=> $id})->list;
206 my @pics = <static
/fullpics/$id-*>;
207 my $slug = make_slug
$title;
208 $tree->find('title')->replace_content("Pictures of $title | ledparts4you");
209 $tree->find('h2')->replace_content($title);
210 $tree->look_down(rel
=> 'canonical')->attr(href
=> "/details/$id/$slug");
211 $tree->fid('pictures')->iter3(\
@pics, \
&details_list_element
);
213 [200, ['Content-type' => 'text/html; charset=utf-8'], [$tree->as_HTML]]
218 my $req = Plack
::Request
->new($env);
219 $db{$$} //= DBIx
::Simple
->connect($ENV{OOF_DSN
} // 'dbi:Pg:');
220 my $order = $req->body_parameters->{order
};
221 my $token = $req->body_parameters->{stripeToken
};
222 return [500, ['Content-type' => 'text/html; charset=utf-8'], ['No token received, payment did not succeed.']] unless $token;
223 $db{$$}->update(orders
=> {stripe_token
=> $token}, {id
=> $order});
224 [200, ['Content-type' => 'text/html; charset=utf-8'], [$pay->as_HTML]];
228 my $footer = read_file
'tmpl/footer.html';
233 my $res = $app->(@_);
234 push @
{$res->[2]}, $footer if $res->[0] == 200;
238 mount
'/' => sub { [301, [Location
=> '/form'], []] };
239 mount
'/form' => \
&form_app
;
240 mount
'/continue' => \
&continue_app
;
241 mount
'/order' => \
&order_app
;
242 mount
'/details' => \
&details_app
;
243 mount
'/pay' => \
&pay_app
;
252 App::Web::Oof - Oversimplified order form / ecommerce website
260 Oof (Oversimplified order form) is a very simple ecommerce website.
261 It is the code behind L<https://ledparts4you.uk.to>.
263 This version is reasonably functional, yet not very reusable, hence
268 Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
270 =head1 COPYRIGHT AND LICENSE
272 Copyright (C) 2016 by Marius Gavrilescu
274 This library is free software; you can redistribute it and/or modify
275 it under the same terms as Perl itself, either Perl version 5.22.1 or,
276 at your option, any later version of Perl 5 you may have available.
This page took 0.071234 seconds and 3 git commands to generate.