From 18be372d0e707ca97b1dfa596917884d0f382e3a Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Sat, 28 May 2016 21:44:26 +0100 Subject: [PATCH] Add delivery CSV for myHermes to the orders display --- Makefile.PL | 1 + README | 1 + lib/App/Web/Oof.pm | 13 +++++++++++++ tmpl/display.html | 2 ++ 4 files changed, 17 insertions(+) diff --git a/Makefile.PL b/Makefile.PL index fb896ce..e50ee44 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -19,6 +19,7 @@ WriteMakefile( HTML::TreeBuilder 0 HTML::Element::Library 0 JSON::MaybeXS 0 + Text::CSV 0 Try::Tiny 0/, }, META_ADD => { diff --git a/README b/README index 5a4876c..857267c 100644 --- a/README +++ b/README @@ -29,6 +29,7 @@ This module requires these other modules and libraries: * HTML::Element * HTML::Element::Library * JSON::MaybeXS +* Text::CSV * Try::Tiny COPYRIGHT AND LICENCE diff --git a/lib/App/Web/Oof.pm b/lib/App/Web/Oof.pm index 1294c16..2fba9be 100644 --- a/lib/App/Web/Oof.pm +++ b/lib/App/Web/Oof.pm @@ -17,6 +17,7 @@ use HTML::Element::Library; use JSON::MaybeXS qw/encode_json decode_json/; use Plack::Builder; use Plack::Request; +use Text::CSV; use Try::Tiny; sub HTML::Element::iter3 { @@ -310,6 +311,16 @@ sub display_order { $div->fclass('stripe_token')->replace_content($data->{stripe_token}) if $data->{stripe_token}; } +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"; + +sub make_hermes_csv { + my ($order) = @_; + my $csv = Text::CSV->new; + my @fields = map { $order->{$_} } qw/address1 address2 address3 address4 postcode first_name last_name email/; + $csv->combine(@fields, '', '', 'n', '', '', '', $order->{phone}, $order->{safe_place}, $order->{instructions}); + $csv->string +} + sub display_app { my ($env) = @_; $db{$$} //= DBIx::Simple->connect($ENV{OOF_DSN} // 'dbi:Pg:'); @@ -318,6 +329,8 @@ sub display_app { my @orders = $db{$$}->query("SELECT * FROM orders ORDER BY date DESC LIMIT $n")->hashes; my $tree = $display->clone; $tree->fclass('order')->iter3(\@orders, \&display_order); + my $csv = join "\n", map { make_hermes_csv $_ } @orders; + $tree->fid('hermes_csv')->replace_content($csv_header . $csv); [200, ['Content-type' => 'text/html; charset=utf-8'], [$tree->as_HTML]]; } diff --git a/tmpl/display.html b/tmpl/display.html index 12323a2..95da05f 100644 --- a/tmpl/display.html +++ b/tmpl/display.html @@ -15,3 +15,5 @@ From

Stripe token: [None]


+ +

-- 
2.39.2