[if-mm !advanced orderstats] [calc] $CGI->{affiliate} = $Session->{username}; return; [/calc] [/if-mm] [seti page_title][L]Orders by Affiliate[/L][if cgi affiliate]: [cgi affiliate][/if][/seti] [set icon_name]icon_stats.gif[/set] [set help_name]orderstats.view[/set] [tmpn third_menu]OrderStats[/tmpn] @_UI_STD_HEAD_@

[L]Date[/L] | [L]Affiliate[/L] | [L]Number of Orders[/L] | [L]Revenue[/L]
[calc] 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; [/calc] [query st=db ml=100000 table=transactions hashref=main sql=" select affiliate, campaign, total_cost, order_date from transactions WHERE deleted <> '1' AND status <> 'canceled' [scratch date_limit] [scratch synd_limit] order by affiliate, campaign, order_date "][/query] [tmp ALL][L]ALL[/L][/tmp] [tmp TOTAL][L]GRAND TOTAL[/L][/tmp] [perl tables="transactions"] my %sales; $master = {}; if($Scratch->{synd_limit}) { $syndstring = "&affiliate=$CGI->{affiliate}"; } else { $syndstring = ""; } #Log("query returned " . $Tag->uneval( {ref => $Tmp->{main} } )); foreach $line (@{$Tmp->{main}}) { 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}; ($syn, $camp) = split /-/, $id, 2; my $synlabel = $syn || errmsg('(none)'); my $burl = $Tag->area('__UI_BASE__/reports/order/Monthly', $month); my $url = qq{$mname $year} if $mname; $out .= < | $url | $synlabel $camp | $record->{orders} | $sales EOF $mname = $year = ''; } $total_sales += $subtotal_sales; $total_quantity += $subtotal_quantity; $subtotal_sales = $Tag->currency({}, $subtotal_sales); $out .= < |  | $Scratch->{ALL} | $subtotal_quantity | $subtotal_sales EOF } $total_sales = $Tag->currency({}, $total_sales); $out .= < | $Scratch->{TOTAL} | $Scratch->{ALL} | $total_quantity | $total_sales
EOF [/perl]

@_UI_STD_FOOTER_@