Revision 422 (by ahitrov, 2014/02/28 20:20:43) Order accounting reports
% 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>
%		}
%		if ( $sort && exists $sorts{$sort} ) {
	Сортировка <b><% $sorts{$sort}{name} %></b>
%		}
	</td></tr>
	</table>
</fieldset>
%	} else {
С даты	<% $from->dmy('.') %>
По дату	<% $to->dmy('.') %>
%		if ( $status ) {
Со статусом	<% $status_opts{$status} %>
%		}
%		if ( $sort && exists $sorts{$sort} ) {
Сортировка	<% $sorts{$sort}{name} %>
%		}
%	}
%	if ( @orders ) {
%		if ( $show eq 'html' ) {
<div style="padding:10px;">
<table border="0" cellpadding="4" cellspacing="0" class="tlistdocs">
	<tr>
	<th>#</th>
	<th>Имя заказчика</th>
	<th>E-mail</th>
	<th>Тел.</th>
	<th>Сумма заказа</th>
	<th>Скидка</th>
	<th>Доставка</th>
	<th>Сумма общая</th>
	<th>Купоны</th>
	</tr>
%		} else {
#	Имя заказчика	E-mail	Тел.	Сумма заказа	Скидка	Доставка	Сумма общая	Купоны
%		}
%		foreach my $order ( @orders ) {
%			my $profile = $order->{profile};
%			my $coupons = $order->{coupons};
%			if ( $show eq 'html' ) {
	<tr>
	<td><% $order->id %></td>
	<td><% $order->name %></td>
	<td><% $order->email %></td>
	<td><% $order->phone %></td>
	<td><% $order->sum %></td>
	<td><% $order->sum_discount %></td>
	<td><% $order->sum_delivery %></td>
	<td><% $order->sum_total %></td>
	<td>
%				if ( ref $coupons eq 'ARRAY'&& @$coupons ) {
%					foreach my $coupon ( @$coupons ) {
		<% $coupon->name %><br>
%					}
%				}
	</td>
	</tr>
%			} else {
%				$m->out( join ("\t", ($order->id, $order->name, $order->email, $order->phone, $order->sum, $order->sum_discount, $order->sum_delivery, $order->sum_total))."\t" );
%				if ( ref $coupons eq 'ARRAY'&& @$coupons ) {
%					my $str = join ', ', map { my $name = $_->name; $name =~ s/\t/\ /g; $name } @$coupons;
%					$m->out( $str );
%				}
%				$m->out("\n");
%			}
%		}
%		if ( $show eq 'html' ) {
</table>
</div>
%		}
%	} else {
%		if ( $show eq 'html' ) {
<p>В данном диапазоне дат с учетом выбранных условий заказы не найдены</p>
%		} else {
В данном диапазоне дат с учетом выбранных условий заказы не найдены
%		}
%	}
% }
% if ( $show eq 'html' ) {
</body>
</html>
% }
<%once>

    my %sorts = (
	'id'	=> { name => 'по номеру заказа', order_by => 'id' },
	'uid'	=> { name => 'по пользователю', order_by => 'name, uid' },
    );

</%once>
<%args>

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

	$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::Order->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 = ( date => [$from->ymd, $to->ymd] );
    if ( $status ) {
	$opts{status} = $status;
    }
    if ( $sort && exists $sorts{$sort} ) {
	$opts{order_by} = $sorts{$sort}{order_by};
    }
    my @orders;
    unless ( $errstr ) {
	@orders = $keeper->get_documents(
			class	=> 'webshop::Order',
			%opts
		);
	if ( @orders ) {
		my %uids = map { $_->uid => 1 } @orders;
		my @uids = keys %uids;
		my $profiles = @uids ? $keeper->get_documents(
				id	=> \@uids,
				class	=> $state->{users}->profile_document_class,
				return_mode	=> 'hash_ref',
			) : {};
		foreach my $order ( @orders ) {
			$order->{profile} = exists $profiles->{$order->uid} ? $profiles->{$order->uid} : undef;
			if ( $order->sum_discount ) {
				my @coupons = $keeper->get_documents(
						class	=> 'webshop::Coupon',
						lclass	=> 'webshop::OrderCouponLink',
						lsource	=> $order->id,
					);
				$order->{coupons} = \@coupons;
			}
		}
	}
    }

</%init>