#!/p/kd/fdc/solaris9/wermit +
#!/opt/local/bin/kermit +
#
# fdc, Tue Apr  6 15:11:48 2010
# Updated Sat May  1 12:40:18 2010 to fix two typos.
#
# Summarizes product orders or product clicks from an Amazon ORDERS
# (not EARNINGS) report, which has been downloaded from the Amazon 
# Associate site as a TSV (Tab-separated value) file.
#
# Usage: tally.ksc tsvfilename [ { orders, clicks } ] > outputfile
#
# The optional second argument is either ORDERS or CLICKS.  If you leave
# off the second argument it defaults to ORDERS.  The program reads the
# file and calculates how many orders (or clicks) were received for each
# product, and then lists the products in descending order of orders
# (or clicks) so you can see which were the most ordered or the most
# looked at.
#
# Requires C-Kermit 9.0 or later.
#
# Amazon Order report fields (separated by Tab) are:
#  1. Product Line
#  2. Item Name
#  3. ASIN
#  4. Date (yyyy/mm/dd)
#  5. Link Type
#  6. Tracking ID
#  7. Product Link Conversion
#  8. Product Link Clicks
#  9. Items Ordered Through Product Links
# 10. All Other Items Ordered
# 11. Total Items Ordered

.myname := \fbasename(\%0)              # Name of this script
def usage exit 1 {                      # Usage message
    "Usage: \m(myname) filename.tsv [{orders,clicks}] > outputfile"
}
if not def \%1 usage                    # First argument is required
.what = orders                          # Count orders by default
if def \%2 .what := \%2                 # Or count what arg2 says

fopen /read \%c \fcontents(\%1)         # Open TSV file
if fail exit 1                          # Exit upon failure

set case off                            # Set up orders or clicks report
switch \m(what) {
  :orders
     .field = 11, .orders = 1, .clicks = 0
     echo ORDERS report...
     break
  :clicks
     .field =  8, .orders = 0, .clicks = 1
     echo CLICKS report...
     break
  :default
     usage
}
.items = 0                              # Initialize items counter

while true {                            # Loop through records
    fread /line \%c line                # Read one record
    if fail break                       # Check for EOF
    .\%n := \fsplit(\m(line),&a,\9,ALL,,1) # Split record into fields (\9=Tab)
    if equ "" "\&a[2]" continue         # If there is no item skip this record
    if < \%n field continue             # Skip record if not enough fields 
    if not num \&a[field] continue      # Skip if count field is not numeric
    increment items                     # Count an item
    .\&a[2] := \fsubstitute(\&a[2],\40\41,) # Avoid an obscure syntax problem
    _incr count<\&a[2]> \&a[field]      # Count the quantity
    incr t \&a[field]                   # Accumulate total quantity
}
fclose \%c                              # Close TSV file

.m := \faaconvert(count,&x,&y)          # Convert associative array

array sort /num /rev &y &x              # Sort result arrays
for i 1 m 1 {                           # Display results
    echo "\fsubstr(\flpad(\&y[i],6)\32\32\&x[i],1,78)"
}
if orders {                             # For orders...
  echo Distinct items ordered: \m(items)
  echo Total items ordered: \m(t)
} else {                                # For clicks...
  echo Distinct items clicked: \m(items)
  echo Total items clicked: \m(t)
}
exit 0

; Local Variables:
; comment-column:40
; comment-start:"# "
; End:
