Revision 637 (by ahitrov, 2017/01/01 20:51:20) Coupon usage report

% if ( $show eq 'html' ) {
<& "/contenido/components/title.msn", %ARGS &>
% }
% if ( $errstr ) {
%	if ( $show eq 'html' ) {
<p style="color:red;"><% $errstr %></p>
%	} else {
%		$m->out($errstr);
%	}
% } else {
%	if ( $show eq 'html' ) {
<fieldset>
	<legend>Отобраны купоны</legend>
	<table width="100%" cellspacing="5" cellpadding="0" class="tform">
	<tr><td>
	С даты: <b><% $from->dmy('.') %></b><br>
	По дату: <b><% $to->dmy('.') %></b><br>
%		if ( $status ) {
	Со статусом: <b><% $status_opts{$status} %></b><br>
%		}
	</td></tr>
	</table>
</fieldset>
%	} else {
С даты	<% $from->dmy('.') %>
По дату	<% $to->dmy('.') %>
%		if ( $status ) {
Со статусом	<% $status_opts{$status} %>
%		}
%	}
%	if ( @coupons ) {
%		if ( $show eq 'html' ) {
<div style="padding:10px;">
<table border="0" cellpadding="4" cellspacing="0" class="tlistdocs">
	<tr valign="top">
	<th rowspan="2">#</th>
	<th rowspan="2">Название</th>
	<th rowspan="2">Статус</th>
	<th colspan="2">Активен</th>
	<th rowspan="2">К-во заказов</th>
	<th rowspan="2">Сумма заказов</th>
	<th rowspan="2">Сумма скидки</th>
	</tr>
	<tr>
	<th>&nbsp; с &nbsp;</th>
	<th>&nbsp; по &nbsp;</th>
	</tr>
%		} else {
#       Имя заказчика   E-mail  Тел.    Сумма заказа    Скидка  Доставка        Сумма общая     Купоны
%		}
%		foreach my $coupon ( @coupons ) {
%			my $begin = Contenido::DateTime->new( postgres => $coupon->dtime );
%			my $end = Contenido::DateTime->new( postgres => $coupon->etime );
%			my $status = exists $status_opts{$coupon->status} ? $status_opts{$coupon->status} : 'неизвестный ('.$coupon->status.')';
%			if ( $show eq 'html' ) {
	<tr>
	<td><a<% $coupon->contenido_status_style ? ' style="' . $coupon->contenido_status_style . '"' : '' %> class="<% $coupon->status ? '':'hiddensect' %>"
 href="/contenido/webshop/coupon.html?id=<% $coupon->id %>"><% $coupon->id %></a></td>
	<td><a<% $coupon->contenido_status_style ? ' style="' . $coupon->contenido_status_style . '"' : '' %> class="<% $coupon->status ? '':'hiddensect' %>"
 href="/contenido/webshop/coupon.html?id=<% $coupon->id %>"><% $coupon->name %></a></td>
	<td><% $status %></td>
	<td><% $begin->dmy('.') %></td>
	<td><% $end->dmy('.') %></td>
	<td align="right"><% $coupon->{order_amount} %></td>
	<td align="right"><% $coupon->{sum_total} %></td>
	<td align="right"><% $coupon->{discount_total} %></td>
	</tr>
%			} else {
%				$m->out( join ("\t", ($coupon->id, $coupon->name, $status, $begin->dmy('.'), $end->dmy('.'), $coupon->{order_amount}, $coupon->{sum_total}, $coupon->{discount_total}))."\t" );
%				$m->out("\n");
%			}
%		}
%		if ( $show eq 'html' ) {
</table>
</div>
%		}
%	} else {
%		if ( $show eq 'html' ) {
<p>В данном диапазоне дат с учетом выбранных условий купоны не найдены</p>
%		} else {
В данном диапазоне дат с учетом выбранных условий купоны не найдены
%		}
%	}
% }

% if ( $show eq 'html' ) {
</body>
</html>
% }
<%args>

	$show	=> 'html'
	$status	=> undef

	$from_day	=> undef
	$from_month	=> undef
	$from_year	=> undef

	$to_day		=> undef
	$to_month	=> undef
	$to_year	=> undef

</%args>
<%init>

    if ( $show eq 'html' ) {
            $r->content_type ("text/html; charset=utf-8");
    } else {
            $r->content_type ("text/plain; charset=utf-8");
    }
    my ($from, $to);

    my $errstr;
    my @props = webshop::Coupon->new( $keeper )->structure;
    my ($status_prop) = grep { $_->{attr} eq 'status' } @props;
    my %status_opts = map { $_->[0] => $_->[1] } @{$status_prop->{cases}};
    my $now = Contenido::DateTime->new;
    if ( $from_day && $from_month && $from_year ) {
	eval { $from = Contenido::DateTime->new( datetime => { day => $from_day, month => $from_month, year => $from_year } ) };
    }
    if ( $to_day && $to_month && $to_year ) {
	eval { $to = Contenido::DateTime->new( datetime => { day => $to_day, month => $to_month, year => $to_year } ) };
    }
    $from = $now        unless ref $from;
    $to = $now          unless ref $to;
    if ( $from > $to ) {
	$errstr = 'Неверно указан диапазон дат';
    }
    my %opts = ( interval => [$from->ymd, $to->ymd] );
    if ( $status ) {
	$opts{status} = $status;
    }

    my @coupons;
    unless ( $errstr ) {
	@coupons = $keeper->get_documents(
			class	=> 'webshop::Coupon',
			%opts,
			order_by	=> 'etime desc',
		);
	foreach my $coupon ( @coupons ) {
		my $orders = $keeper->get_documents(
				class	=> 'webshop::Order',
				lclass	=> 'webshop::OrderCouponLink',
				lsource	=> 'positive',
				ldest	=> $coupon->id,
				no_limit	=> 1,
				order_by	=> 'ctime desc',
				return_mode	=> 'array_ref',
			);
		my ($sum, $discount) = (0,0);
		foreach my $order( @$orders ) {
			$sum += $order->sum;
			$discount += $order->sum_discount;
			
		}
		$coupon->{order_amount} = scalar @$orders;
		$coupon->{sum_total} = $sum;
		$coupon->{discount_total} = $discount;
	}
    }

</%init>