[if-mm !advanced orderstats] [calc] $CGI->{affiliate} = $Session->{username}; return; [/calc] [/if-mm] [seti page_title][L]Orders by SKU[/L] [calc] my $sku = $CGI->{sku} or return ''; $sku =~ s/\0/,/g; return $sku; [/calc][/seti] [set icon_name]icon_stats.gif[/set] [set ui_class]Reports[/set] [set help_name]orderstats.view[/set] [tmpn third_menu]OrderStats[/tmpn] @_UI_STD_HEAD_@ [tmpn tmp_large][/tmpn] [perl tables=orderline] # These will be used in widget below $display_widget = 'text_50'; $display_options = undef; $display_width = undef; my $large; my $die = sub { my $msg = errmsg(@_); $Tag->error( name => 'BySKU report', set => $msg ); my $url = $Tag->area('admin/error'); $Tag->deliver({ location => $url }); return undef; }; my $odb = $Db{orderline} or return $die->("This page requires SQL orderline database."); if($odb->config('LARGE')) { $display_widget = 'text_50'; } else { my $tname = $odb->name(); my $ary = $odb->query("select distinct(sku) from $tname order by sku"); ($ary and $ary->[0]) or return $die->("Unable to get any records from %s.", $tname); my $count = scalar(@$ary); if($count <= 50) { $display_widget = 'checkbox_left_8'; $display_options = $ary; } elsif($count <= 200) { $display_widget = 'movecombo'; $display_options = $ary; $display_width = 50; } else { $display_widget = 'text_50'; } if($count <= 40) { $Tag->tmpn('tmp_addlinks', 1); } } my @skus = grep /\S/, split /[\s,\0]+/, $CGI->{sku}; my @qsku = map { $Tag->filter('sql', $_) } @skus; my $sku_query; if(@skus > 1) { $sku_query = "sku IN ('"; $sku_query .= join("','", @qsku); $sku_query .= "')"; } else { $sku_query = "sku = '$qsku[0]'"; } $Tag->tmpn('tmp_sku_query', $sku_query); if($Session->{arg}) { $Scratch->{date_limit} = "AND order_date like '$Session->{arg}%'"; } elsif ($CGI->{ui_begin_date}) { for (qw/ ui_begin_date ui_end_date /) { $CGI->{$_} = $Tag->filter( { op => 'date_change', body => $CGI->{$_} }); } $Scratch->{date_limit} = < '$CGI->{ui_begin_date}' AND order_date < '$CGI->{ui_end_date}Z' EOF } else { $Scratch->{date_limit} = ""; } $Scratch->{synd_limit} = ''; return unless $CGI->{affiliate}; $Scratch->{synd_limit} = "AND affiliate = '$CGI->{affiliate}'"; $Scratch->{synd_limit} .= " AND campaign = '$CGI->{campaign}'" if $CGI->{campaign}; return; [/perl] [if cgi sku] [query st=db ml=100000 table=transactions arrayref=qual sql=" SELECT DISTINCT order_number FROM orderline WHERE [scratch tmp_sku_query] "][/query] [query st=db ml=100000 table=transactions hashref=main sql=" select affiliate, campaign, total_cost, order_date, order_number from transactions WHERE deleted <> '1' and status <> 'canceled' [scratch date_limit] [scratch synd_limit] order by affiliate, campaign, order_date "][/query] [/if] [if scratch tmp_addlinks]

Quick links to report on a single SKU
[table-organize cols=8 columnize=1] | [display name=sku type=links value="[value sku]" joiner=" | " options=`$display_options` ] [/table-organize]

[/if] [form-session-id] SKU to report on: [display type=`$display_widget` name=sku value=`$CGI->{sku}` width=`$display_width` options=`$display_options` ] Only items that have been ordered are displayed

[L]Date[/L] | [L]SKU[/L] | [L]Number of Orders[/L] | [L]Revenue[/L]
[tmp ALL][L]ALL[/L][/tmp] [tmp TOTAL][L]GRAND TOTAL[/L][/tmp] [perl tables="transactions orderline"] my $mary = $Tmp->{main} or return; # do nothing if no query $master = {}; my $skustring = '&sku='; my @skus = split /[\s,\0]+/, $CGI->{sku}; $skustring .= join('&sku=', @skus); my $skudisplay .= join(', ', @skus); if(my $qary = $Tmp->{qual}) { my %apply; for(@$qary) { $apply{$_->[0]} = 1; } @$mary = grep $apply{$_->{order_number}}, @$mary; my $odb = $Db{orderline}; if(! $odb->config('HAS_LIMIT')) { $Tag->error({ name => 'Totals', set => 'amounts will be wrong with no SQL', }); } else { for my $t (@$mary) { my $q = "SELECT subtotal FROM orderline"; $q .= " WHERE order_number = '$t->{order_number}'"; $q .= " AND $Scratch->{tmp_sku_query}"; my $tary = $odb->query($q); my $cost = 0; for(@$tary) { $cost += $_->[0]; } $t->{total_cost} = $cost; } } } if($Scratch->{synd_limit}) { $syndstring = "&affiliate=$CGI->{affiliate}$skustring"; } else { $syndstring = $skustring; } #Log("query returned " . $Tag->uneval( {ref => $Tmp->{main} } )); foreach $line (@$mary) { my ($month) = substr($line->{order_date}, 0, 6); my $id = $line->{affiliate}; $id .= "-$line->{campaign}"; $month{$month}++; $master->{$month} = { } if ! $master->{$month}; $master->{$month}{$id} = { } if ! $master->{$month}{$id}; my $ref = $master->{$month}{$id}; $ref->{sales} += $line->{total_cost}; $ref->{orders}++; } %names = qw/ 01 January 02 February 03 March 04 April 05 May 06 June 07 July 08 August 09 September 10 October 11 November 12 December /; my $out = ''; foreach $month (sort { $b <=> $a } keys %$master) { my $year = $month; $year =~ s/(\d\d\d\d)(\d\d)/$1/; my $mname = errmsg($names{$2}); my $subtotal_sales = 0; my $subtotal_quantity = 0; my $ref = $master->{$month}; foreach $id (sort keys %$ref) { my $record = $ref->{$id}; $sales = $Tag->currency({}, $record->{sales}); $subtotal_sales += $record->{sales}; $subtotal_quantity += $record->{orders}; my $burl = $Tag->area('__UI_BASE__/reports/order/Monthly', $month); my $url = qq{$mname $year} if $mname; $out .= < | $url | $skudisplay | $record->{orders} | $sales EOF $mname = $year = ''; } $total_sales += $subtotal_sales; $total_quantity += $subtotal_quantity; $subtotal_sales = $Tag->currency({}, $subtotal_sales); } $total_sales = $Tag->currency({}, $total_sales); $out .= < | $Scratch->{TOTAL} | $Scratch->{ALL} | $total_quantity | $total_sales
EOF [/perl]

@_UI_STD_FOOTER_@