]>
iEval git - app-web-oof.git/blob - Oof.pm
e7d93f5d856a10621d526b79a6a3194eb86130c4
7 use parent qw
/Plack::Component/;
9 our $VERSION = '0.000_004';
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('freepost')->detach unless $data->{freepost
};
70 $tr->fclass('title')->attr('data-product', $data->{product
});
71 $tr->fclass('title')->attr('href', '/details/'.$data->{product
}.'/'.make_slug
$data->{title
});
72 # $tr->fclass('title')->attr('data-summary', $data->{summary});
73 $tr->look_down(_tag
=> 'input')->attr(max
=> $data->{stock
});
74 $tr->look_down(_tag
=> 'input')->attr(name
=> 'quant'.$data->{product
});
79 $db{$$} //= DBIx
::Simple
->connect($ENV{OOF_DSN
} // 'dbi:Pg:');
81 my $data = $db{$$}->select(products
=> '*', {}, 'product')->hashes;
82 my $tree = $form->clone;
83 $tree->find('tbody')->find('tr')->iter3($data, \
&form_table_row
);
85 [200, ['Content-type' => 'text/html; charset=utf-8'], [$tree->as_HTML]]
88 sub continue_table_row
{
90 $tr->fclass($_)->replace_content($data->{$_}) for qw
/title subtitle quantity/;
91 $tr->fclass('freepost')->detach unless $data->{freepost
};
92 $tr->fclass('price')->replace_content(stringify_money
$data->{subtotal
});
93 $tr->fclass('title')->attr('data-product', $data->{product
});
98 $db{$$} //= DBIx
::Simple
->connect($ENV{OOF_DSN
} // 'dbi:Pg:');
99 my $tree = $continue->clone;
100 my $req = Plack
::Request
->new($env);
101 my $params = $req->body_parameters;
103 my ($quant, $quant_freepost, $total, @data, @notes) = (0) x
3;
104 for (sort keys %$params) {
105 next unless /^quant/;
106 next unless $params->{$_};
107 my $data = $db{$$}->select(products
=> '*', {product
=> substr $_, 5})->hash;
108 $data->{quantity
} = $params->{$_};
109 if ($data->{stock
} == 0) {
110 push @notes, 'Item is out of stock and was removed from order: '.$data->{title
};
113 if ($data->{quantity
} > $data->{stock
}) {
114 $data->{quantity
} = $data->{stock
};
115 push @notes, 'Not enough units of "'.$data->{title
}.'" available. Quantity reduced to '.$data->{quantity
}
117 $data->{subtotal
} = $data->{price
} * $data->{quantity
};
118 $quant += $data->{quantity
};
119 $quant_freepost += $data->{quantity
} if $data->{freepost
};
120 $total += $data->{subtotal
};
124 return [500, ['Content-type' => 'text/plain'], ['Error: no items in order.']] unless $quant;
126 $tree->fid('subtotal')->replace_content(stringify_money
$total);
128 if ($params->{discount
}) {
129 my $discount = $db{$$}->select(discounts
=> '*', {discount
=> $params->{discount
}})->hash;
130 if (!defined $discount) {
131 push @notes, 'Discount code incorrect. No discount applied.'
132 } elsif ($db{$$}->select(orders
=> 'COUNT(*)', {discount
=> $params->{discount
}})->list) {
133 push @notes, 'Discount code already used once. No discount applied.'
135 $dvalue = int (0.5 + $discount->{fraction
} * $total) if $discount->{fraction
};
136 $dvalue = $discount->{flat
} if $discount->{flat
};
137 $tree->fid('discount')->replace_content('-'.stringify_money
$dvalue);
139 $tree->look_down(name
=> 'discount')->attr(value
=> $params->{discount
});
140 push @notes, 'Discount applied.'
143 $tree->look_down(name
=> 'discount')->detach unless $dvalue;
144 $tree->fid('discount_tr')->detach unless $dvalue;
145 my $postage = 220 + 50 * ($quant - $quant_freepost);
146 $postage = 0 if $quant == $quant_freepost;
147 $tree->fid('postage')->replace_content(stringify_money
$postage);
149 $tree->fid('total')->replace_content(stringify_money
$total);
151 $tree->fid('order')->find('tbody')->find('tr')->iter3(\
@data, \
&continue_table_row
);
152 $tree->iter($tree->fid('notes')->find('li') => @notes);
154 $tree->look_down(name
=> 'products')->attr(value
=> encode_json \
@data);
155 $tree->look_down(name
=> 'total')->attr(value
=> $total);
157 [200, ['Content-type' => 'text/html; charset=utf-8'], [$tree->as_HTML]]
162 $db{$$} //= DBIx
::Simple
->connect($ENV{OOF_DSN
} // 'dbi:Pg:');
163 my $tree = $order->clone;
164 my $req = Plack
::Request
->new($env);
165 my ($id) = $env->{PATH_INFO
} =~ m
,^/([0-9A
-F
]+),;
167 my $total = $db{$$}->select(orders
=> 'total', {id
=> $id})->list or
168 return [500, ['Content-type', 'text/plain'], ['Order not found']];
169 $tree->fid('orderid')->replace_content($id);
170 $tree->look_down(name
=> 'order')->attr(value
=> $id);
171 $tree->fid('total')->replace_content(stringify_money
$total);
172 $tree->find('script')->attr('data-amount', $total);
173 return [200, ['Content-type' => 'text/html; charset=utf-8'], [$tree->as_HTML]]
175 my %parms = %{$req->body_parameters};
176 my $id = sprintf "%X%04X", time, $$;
180 my $products = decode_json
$req->body_parameters->{products
};
181 for my $prod (@
$products) {
182 my $stock = $db{$$}->select(products
=> 'stock', {product
=> $prod->{product
}})->list;
183 die "Not enough of " .$prod->{title
}."\n" if $prod->{quantity
} > $stock;
184 $db{$$}->update(products
=> {stock
=> $stock - $prod->{quantity
}}, {product
=> $prod->{product
}});
186 $db{$$}->insert(orders
=> {id
=> $id, %parms});
190 $err = [500, ['Content-type', 'text/plain'], ["Error: $_"]]
193 return [303, [Location
=> "/order/$id"], []]
199 $db{$$} //= DBIx
::Simple
->connect($ENV{OOF_DSN
} // 'dbi:Pg:');
200 $order = $db{$$}->select(orders
=> '*', {id
=> $order})->hash;
201 my $products = decode_json
$order->{products
};
204 for my $prod (@
$products) {
205 my $stock = $db{$$}->select(products
=> 'stock', {product
=> $prod->{product
}})->list;
206 $db{$$}->update(products
=> {stock
=> $stock + $prod->{quantity
}}, {product
=> $prod->{product
}});
208 $db{$$}->delete(orders
=> {id
=> $order->{id
}});
216 sub details_list_element
{
217 my ($data, $li) = @_;
218 $li->find('a')->attr(href
=> "/$data");
219 my $thumb = $data =~ s/fullpics/thumbs/r;
220 $thumb = $data unless -f
$thumb;
221 $li->find('img')->attr(src
=> "/$thumb");
226 $db{$$} //= DBIx
::Simple
->connect($ENV{OOF_DSN
} // 'dbi:Pg:');
227 my $tree = $details->clone;
228 my ($id) = $env->{PATH_INFO
} =~ m
,^/(\d
+),;
229 my $title = $db{$$}->select(products
=> 'title', {product
=> $id})->list;
230 my @pics = <static
/fullpics/$id-*>;
231 my $slug = make_slug
$title;
232 $tree->find('title')->replace_content("Pictures of $title | ledparts4you");
233 $tree->find('h2')->replace_content($title);
234 $tree->look_down(rel
=> 'canonical')->attr(href
=> "/details/$id/$slug");
235 $tree->fid('pictures')->find('li')->iter3(\
@pics, \
&details_list_element
);
237 [200, ['Content-type' => 'text/html; charset=utf-8'], [$tree->as_HTML]]
242 my $req = Plack
::Request
->new($env);
243 $db{$$} //= DBIx
::Simple
->connect($ENV{OOF_DSN
} // 'dbi:Pg:');
244 my $order = $req->body_parameters->{order
};
245 my $token = $req->body_parameters->{stripeToken
};
246 return [500, ['Content-type' => 'text/html; charset=utf-8'], ['No token received, payment did not succeed.']] unless $token;
247 $db{$$}->update(orders
=> {stripe_token
=> $token}, {id
=> $order});
248 [200, ['Content-type' => 'text/html; charset=utf-8'], [$pay->as_HTML]];
252 my $footer = read_file
'tmpl/footer.html';
257 my $res = $app->(@_);
258 $res->[2][0] =~ s
,</body>,$footer</body
>, if $res->[0] == 200;
262 mount
'/' => sub { [301, [Location
=> '/form'], []] };
263 mount
'/form' => \
&form_app
;
264 mount
'/continue' => \
&continue_app
;
265 mount
'/order' => \
&order_app
;
266 mount
'/details' => \
&details_app
;
267 mount
'/pay' => \
&pay_app
;
276 App::Web::Oof - Oversimplified order form / ecommerce website
284 Oof (Oversimplified order form) is a very simple ecommerce website.
285 It is the code behind L<https://ledparts4you.uk.to>.
287 This version is reasonably functional, yet not very reusable, hence
292 Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
294 =head1 COPYRIGHT AND LICENSE
296 Copyright (C) 2016 by Marius Gavrilescu
298 This library is free software; you can redistribute it and/or modify
299 it under the same terms as Perl itself, either Perl version 5.22.1 or,
300 at your option, any later version of Perl 5 you may have available.
This page took 0.087164 seconds and 3 git commands to generate.