summaryrefslogtreecommitdiff
path: root/perl/count_repeat_customers.pl
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2017-04-21 11:31:50 -0400
committerJules Laplace <jules@okfoc.us>2017-04-21 11:31:50 -0400
commitbf9bdaa3e56adcac8abc955cace439d0092033e3 (patch)
tree21a3dcef044b8c4ef0b54663c0b68c23a2e8941e /perl/count_repeat_customers.pl
init with perl stuff
Diffstat (limited to 'perl/count_repeat_customers.pl')
-rw-r--r--perl/count_repeat_customers.pl106
1 files changed, 106 insertions, 0 deletions
diff --git a/perl/count_repeat_customers.pl b/perl/count_repeat_customers.pl
new file mode 100644
index 0000000..f2873f4
--- /dev/null
+++ b/perl/count_repeat_customers.pl
@@ -0,0 +1,106 @@
+#!/usr/bin/perl
+
+use Text::CSV;
+use Data::Dumper;
+
+# cw_products
+# 0 id 1 sku 2 product_name
+
+# cw_skus
+# 0 sku_id 1 sku 2 product_id
+
+# cw_orders
+# 0 order_id 1 date 2 status 3 customer_id
+
+# cw_order_skus
+# 0 id 1 order_id 2 sku_id
+
+# cw_customers
+# 0 customer_id 1 customer_type 2 date_added 3 date_modified 4 first_name 5 last_name
+
+@products = load_csv("cw_products.csv");
+@skus = load_csv("cw_skus.csv");
+@orders = load_csv("cw_orders.csv");
+@order_skus = load_csv("cw_order_skus.csv");
+@customers = load_csv("cw_customers.csv");
+
+$product_name_lookup = {};
+for $product (@products) {
+ $sku = $product->[1];
+ $name = $product->[2];
+ $product_name_lookup->{ $sku } = $name;
+}
+
+$customer_name_lookup = {};
+for $customer (@customers) {
+ $id = $customer->[0];
+ $name = $customer->[5] . " " . $customer->[4];
+ $customer_name_lookup->{ $id } = $name;
+}
+
+$sku_id_lookup = {};
+for $sku (@skus) {
+ $sku_id = $sku->[0];
+ $sku_name = $sku->[1];
+ $sku_id_lookup->{ $sku_id } = $sku_name;
+}
+
+$customers_seen = {};
+for $order (@orders) {
+ $order_id = $order->[0];
+ $customer_id = $order->[3];
+ if (exists( $customers_seen->{ $customer_id } )) {
+ # print $customer_name_lookup->{ $customer_id } . "\n";
+ $customers_seen->{ $customer_id } += 1;
+ }
+ else {
+ $customers_seen->{ $customer_id } = 1;
+ }
+}
+
+$repeat_customers = 0;
+$total_customers = 0;
+for $customer_id (keys %$customers_seen) {
+ $count = $customers_seen->{ $customer_id };
+ $total_customers += 1;
+ if ( $count > 1 ) {
+ # print $count . ": " . $customer_name_lookup->{ $customer_id } . "\n";
+ $repeat_customers += 1;
+ }
+}
+
+print "total customers: " . $total_customers . "\n";
+print "repeat customers: " . $repeat_customers . "\n";
+
+
+# print Dumper( $sku_id_lookup );
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+sub load_csv () {
+ my $filename = shift;
+ my @rows;
+ my $csv = Text::CSV->new ( { binary => 1 } ) # should set binary attribute.
+ or die "Cannot use CSV: ".Text::CSV->error_diag ();
+
+ open my $fh, "<:encoding(utf8)", $filename or die "$filename: $!";
+ while ( my $row = $csv->getline( $fh ) ) {
+ push @rows, $row;
+ }
+ $csv->eof or $csv->error_diag();
+ close $fh;
+ return @rows;
+}
+