View source
<?php
namespace Drupal\uc_report\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Database\Connection;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\Response;
define('UC_REPORT_MAX_RECORDS', PHP_INT_MAX);
class Reports extends ControllerBase implements ContainerInjectionInterface {
protected $database;
public static function create(ContainerInterface $container) {
return new static($container
->get('database'));
}
public function __construct(Connection $database) {
$this->database = $database;
}
public function customers() {
$address_preference = $this
->config('uc_store.settings')
->get('customer_address');
$first_name = $address_preference == 'billing' ? 'billing_first_name' : 'delivery_first_name';
$last_name = $address_preference == 'billing' ? 'billing_last_name' : 'delivery_last_name';
$page = isset($_GET['page']) ? intval($_GET['page']) : 0;
$page_size = isset($_GET['nopage']) ? UC_REPORT_MAX_RECORDS : $this
->config('uc_report.settings')
->get('table_size');
$order_statuses = uc_report_order_statuses();
$rows = [];
$csv_rows = [];
$header = [
[
'data' => $this
->t('#'),
],
[
'data' => $this
->t('Customer'),
'field' => "ou.{$last_name}",
],
[
'data' => $this
->t('Username'),
'field' => "u.name",
],
[
'data' => $this
->t('Orders'),
'field' => 'orders',
],
[
'data' => $this
->t('Products'),
'field' => 'products',
],
[
'data' => $this
->t('Total'),
'field' => 'total',
'sort' => 'desc',
],
[
'data' => $this
->t('Average'),
'field' => 'average',
],
];
$csv_rows[] = [
$this
->t('#'),
$this
->t('Customer'),
$this
->t('Username'),
$this
->t('Orders'),
$this
->t('Products'),
$this
->t('Total'),
$this
->t('Average'),
];
$query = $this->database
->select('users_field_data', 'u', [
'fetch' => \PDO::FETCH_ASSOC,
])
->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')
->extend('Drupal\\Core\\Database\\Query\\TableSortExtender');
$query
->leftJoin('uc_orders', 'ou', 'u.uid = ou.uid');
$query
->fields('u', [
'uid',
'name',
])
->fields('ou', [
$first_name,
$last_name,
])
->condition('u.uid', 0, '>')
->groupBy('u.uid');
$query
->addExpression("(SELECT COUNT(DISTINCT(order_id)) FROM {uc_orders} o WHERE o.uid = u.uid AND o.order_status IN (:statuses[]))", 'orders', [
':statuses[]' => $order_statuses,
]);
$query
->addExpression("(SELECT SUM(qty) FROM {uc_order_products} ps LEFT JOIN {uc_orders} os ON ps.order_id = os.order_id WHERE os.order_status IN (:statuses2[]) AND os.uid = u.uid)", 'products', [
':statuses2[]' => $order_statuses,
]);
$query
->addExpression("(SELECT SUM(ot.order_total) FROM {uc_orders} ot WHERE ot.uid = u.uid AND ot.order_status IN (:statuses3[]))", 'total', [
':statuses3[]' => $order_statuses,
]);
$query
->addExpression("ROUND((SELECT SUM(ot.order_total) FROM {uc_orders} ot WHERE ot.uid = u.uid AND ot.order_status IN (:sum_statuses[]))/(SELECT COUNT(DISTINCT(order_id)) FROM {uc_orders} o WHERE o.uid = u.uid AND o.order_status IN (:count_statuses[])), 2)", 'average', [
':sum_statuses[]' => $order_statuses,
':count_statuses[]' => $order_statuses,
]);
$count_query = $this->database
->select('users_field_data', 'u');
$count_query
->leftJoin('uc_orders', 'ou', 'u.uid = ou.uid');
$count_query
->addExpression('COUNT(DISTINCT u.uid)');
$count_query
->condition('u.uid', 0, '>');
$query
->setCountQuery($count_query);
$query
->groupBy('u.uid')
->groupBy('u.name')
->groupBy("ou.{$first_name}")
->groupBy("ou.{$last_name}")
->orderByHeader($header)
->limit($page_size);
$customers = $query
->execute();
foreach ($customers as $customer) {
$name = !empty($customer[$last_name]) || !empty($customer[$first_name]) ? Link::fromTextAndUrl($customer[$last_name] . ', ' . $customer[$first_name], Url::fromUri('base:admin/store/customers/orders/' . $customer['uid']))
->toString() : Link::fromTextAndUrl($customer['name'], Url::fromUri('base:admin/store/customers/orders/' . $customer['uid']))
->toString();
$customer_number = $page * $this
->config('uc_report.settings')
->get('table_size') + (count($rows) + 1);
$customer_order_name = !empty($customer[$last_name]) || !empty($customer[$first_name]) ? $customer[$last_name] . ', ' . $customer[$first_name] : $customer['name'];
$customer_name = $customer['name'];
$orders = !empty($customer['orders']) ? $customer['orders'] : 0;
$products = !empty($customer['products']) ? $customer['products'] : 0;
$total_revenue = uc_currency_format($customer['total']);
$average_revenue = uc_currency_format($customer['average']);
$rows[] = [
[
'data' => $customer_number,
],
[
'data' => $name,
],
[
'data' => Link::createFromRoute($customer_name, 'entity.user.canonical', [
'user' => $customer['uid'],
])
->toString(),
],
[
'data' => $orders,
],
[
'data' => $products,
],
[
'data' => $total_revenue,
],
[
'data' => $average_revenue,
],
];
$csv_rows[] = [
$customer_number,
$customer_order_name,
$customer_name,
$orders,
$products,
$customer['total'],
$customer['average'],
];
}
$csv_data = $this
->store_csv('uc_customers', $csv_rows);
$build['report'] = [
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#attributes' => [
'width' => '100%',
'class' => [
'uc-sales-table',
],
],
'#empty' => $this
->t('No customers found'),
];
$build['pager'] = [
'#type' => 'pager',
];
$build['links'] = [
'#prefix' => '<div class="uc-reports-links">',
'#suffix' => '</div>',
];
$build['links']['export_csv'] = [
'#markup' => Link::createFromRoute($this
->t('Export to CSV file.'), 'uc_report.getcsv', [
'report_id' => $csv_data['report'],
'user_id' => $csv_data['user'],
])
->toString(),
'#suffix' => ' ',
];
if (isset($_GET['nopage'])) {
$build['links']['toggle_pager'] = [
'#markup' => Link::createFromRoute($this
->t('Show paged records'), 'uc_report.customers')
->toString(),
];
}
else {
$build['links']['toggle_pager'] = [
'#markup' => Link::createFromRoute($this
->t('Show all records'), 'uc_report.customers', [
'query' => [
'nopage' => '1',
],
])
->toString(),
];
}
return $build;
}
public function products() {
$views_column = $this
->moduleHandler()
->moduleExists('statistics') && $this
->config('statistics.settings')
->get('count_content_views');
$page = isset($_GET['page']) ? intval($_GET['page']) : 0;
$page_size = isset($_GET['nopage']) ? UC_REPORT_MAX_RECORDS : $this
->config('uc_report.settings')
->get('table_size');
$order_statuses = uc_report_order_statuses();
$row_cell = $page * $this
->config('uc_report.settings')
->get('table_size') + 1;
$rows = [];
$csv_rows = [];
$ignored_types = [
'product_kit',
];
$product_types = [];
foreach (uc_product_types() as $type) {
if (!in_array($type, $ignored_types)) {
$product_types[] = $type;
}
}
$query = $this->database
->select('node_field_data', 'n', [
'fetch' => \PDO::FETCH_ASSOC,
])
->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')
->extend('Drupal\\Core\\Database\\Query\\TableSortExtender')
->limit($page_size);
$query
->addField('n', 'nid');
$query
->addField('n', 'title');
$query
->addExpression("(SELECT SUM(uop.qty) FROM {uc_order_products} uop LEFT JOIN {uc_orders} uo ON uop.order_id = uo.order_id WHERE uo.order_status IN (:statuses[]) AND uop.nid = n.nid)", 'sold', [
':statuses[]' => $order_statuses,
]);
$query
->addExpression("(SELECT (SUM(uop.price * uop.qty) - SUM(uop.cost * uop.qty)) FROM {uc_order_products} uop LEFT JOIN {uc_orders} uo ON uop.order_id = uo.order_id WHERE uo.order_status IN (:statuses2[]) AND uop.nid = n.nid)", 'gross', [
':statuses2[]' => $order_statuses,
]);
$query
->addExpression("(SELECT (SUM(uop.price * uop.qty)) FROM {uc_order_products} uop LEFT JOIN {uc_orders} uo ON uop.order_id = uo.order_id WHERE uo.order_status IN (:statuses3[]) AND uop.nid = n.nid)", 'revenue', [
':statuses3[]' => $order_statuses,
]);
$header = [
[
'data' => $this
->t('#'),
],
[
'data' => $this
->t('Product'),
'field' => 'n.title',
],
[
'data' => $this
->t('Sold'),
'field' => 'sold',
],
[
'data' => $this
->t('Revenue'),
'field' => 'revenue',
'sort' => 'desc',
],
[
'data' => $this
->t('Gross'),
'field' => 'gross',
],
];
$csv_rows[] = [
$this
->t('#'),
$this
->t('Product'),
$this
->t('Sold'),
$this
->t('Revenue'),
$this
->t('Gross'),
];
if ($views_column) {
$header[] = [
'data' => $this
->t('Views'),
'field' => 'nc.totalcount',
];
$csv_rows[0][] = $this
->t('Views');
}
$query
->orderByHeader($header);
if ($views_column) {
$query
->leftJoin('node_counter', 'nc', 'n.nid = nc.nid');
$query
->addField('nc', 'totalcount');
}
$query
->condition('n.type', $product_types, 'IN')
->groupBy('n.nid')
->groupBy('n.title');
$products = $query
->execute();
foreach ($products as $product) {
$product_cell = Link::createFromRoute($product['title'], 'entity.node.canonical', [
'node' => $product['nid'],
])
->toString();
$product_csv = $product['title'];
$sold_cell = empty($product['sold']) ? 0 : $product['sold'];
$sold_csv = $sold_cell;
$revenue_csv = empty($product['revenue']) ? 0 : $product['revenue'];
$revenue_cell = uc_currency_format($revenue_csv);
$gross_csv = empty($product['gross']) ? 0 : $product['gross'];
$gross_cell = uc_currency_format($gross_csv);
$row = [
'data' => [
$row_cell,
$product_cell,
[
'data' => [
'#prefix' => '<strong>',
'#markup' => $sold_cell,
'#suffix' => '</strong>',
],
],
[
'data' => [
'#prefix' => '<strong>',
'#markup' => $revenue_cell,
'#suffix' => '</strong>',
],
],
[
'data' => [
'#prefix' => '<strong>',
'#markup' => $gross_cell,
'#suffix' => '</strong>',
],
],
],
'primary' => TRUE,
];
$csv_row = [
$row_cell,
$product_csv,
$sold_csv,
$revenue_csv,
$gross_csv,
];
if ($views_column) {
$views = isset($product['totalcount']) ? $product['totalcount'] : 0;
$row['data'][] = $views;
$csv_row[] = $views;
}
$rows[] = $row;
$csv_rows[] = $csv_row;
if ($this
->moduleHandler()
->moduleExists('uc_attribute')) {
$models = $this
->product_get_skus($product['nid']);
foreach ($models as $model) {
$sold = $this->database
->query("SELECT SUM(qty) FROM {uc_order_products} p LEFT JOIN {uc_orders} o ON p.order_id = o.order_id WHERE o.order_status IN (:statuses[]) AND p.model = :model AND p.nid = :nid", [
':statuses[]' => $order_statuses,
':model' => $model,
':nid' => $product['nid'],
])
->fetchField();
$revenue = $this->database
->query("SELECT SUM(p.price * p.qty) FROM {uc_order_products} p LEFT JOIN {uc_orders} o ON p.order_id = o.order_id WHERE o.order_status IN (:statuses[]) AND p.model = :model AND p.nid = :nid", [
':statuses[]' => $order_statuses,
':model' => $model,
':nid' => $product['nid'],
])
->fetchField();
$gross = $this->database
->query("SELECT (SUM(p.price * p.qty) - SUM(p.cost * p.qty)) FROM {uc_order_products} p LEFT JOIN {uc_orders} o ON p.order_id = o.order_id WHERE o.order_status IN (:statuses[]) AND p.model = :model AND p.nid = :nid", [
':statuses[]' => $order_statuses,
':model' => $model,
':nid' => $product['nid'],
])
->fetchField();
$breakdown_product = [
'#markup' => " {$model}",
];
$product_csv = " {$model}";
$sold_csv = !empty($sold) ? $sold : 0;
$breakdown_sold = $sold_csv;
$revenue_csv = !empty($revenue) ? $revenue : 0;
$breakdown_revenue = [
'#theme' => 'uc_price',
'#price' => $revenue_csv,
];
$gross_csv = !empty($gross) ? $gross : 0;
$breakdown_gross = [
'#theme' => 'uc_price',
'#price' => $gross_csv,
];
$row = [
'data' => [
'',
[
'data' => $breakdown_product,
],
$breakdown_sold,
[
'data' => $breakdown_revenue,
],
[
'data' => $breakdown_gross,
],
],
];
$csv_row = [
'',
$product_csv,
$sold_csv,
$revenue_csv,
$gross_csv,
];
if ($views_column) {
$row['data'][] = '';
$csv_row[] = '';
}
$rows[] = $row;
$csv_rows[] = $csv_row;
}
}
$row_cell++;
}
$csv_data = $this
->store_csv('uc_products', $csv_rows);
$build['report'] = [
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#attributes' => [
'width' => '100%',
'class' => [
'uc-sales-table',
],
],
'#empty' => $this
->t('No products found'),
];
$build['pager'] = [
'#type' => 'pager',
];
$build['links'] = [
'#prefix' => '<div class="uc-reports-links">',
'#suffix' => '</div>',
];
$build['links']['export_csv'] = [
'#markup' => Link::createFromRoute($this
->t('Export to CSV file.'), 'uc_report.getcsv', [
'report_id' => $csv_data['report'],
'user_id' => $csv_data['user'],
])
->toString(),
'#suffix' => ' ',
];
if (isset($_GET['nopage'])) {
$build['links']['toggle_pager'] = [
'#markup' => Link::createFromRoute($this
->t('Show paged records'), 'uc_report.products')
->toString(),
];
}
else {
$build['links']['toggle_pager'] = [
'#markup' => Link::createFromRoute($this
->t('Show all records'), 'uc_report.products', [
'query' => [
'nopage' => '1',
],
])
->toString(),
];
}
$build['instructions'] = [
'#markup' => '<small>*' . $this
->t('Make sure %setting_name is set to %state in the <a href=":url">access log settings page</a> to enable views column.', [
'%setting_name' => 'count content views',
'%state' => 'enabled',
':url' => Url::fromUri('base:admin/config/system/statistics', [
'query' => [
'destination' => 'admin/store/reports/products',
],
])
->toString(),
]) . '</small>',
];
return $build;
}
public function product_get_skus($nid) {
$models = [
$this->database
->query("SELECT model FROM {uc_products} WHERE nid = :nid", [
':nid' => $nid,
])
->fetchField(),
];
$models = array_merge($models, $this->database
->query("SELECT model FROM {uc_product_adjustments} WHERE nid = :nid", [
':nid' => $nid,
])
->fetchCol());
$models = array_merge($models, $this->database
->query("SELECT DISTINCT model FROM {uc_order_products} WHERE nid = :nid", [
':nid' => $nid,
])
->fetchCol());
$models = array_unique($models);
asort($models);
return $models;
}
public function customProducts() {
$views_column = $this
->moduleHandler()
->moduleExists('statistics') && $this
->config('statistics.settings')
->get('count_content_views');
$page = isset($_GET['page']) ? intval($_GET['page']) : 0;
$page_size = isset($_GET['nopage']) ? UC_REPORT_MAX_RECORDS : $this
->config('uc_report.settings')
->get('table_size');
$rows = [];
$csv_rows = [];
$ignored_types = [
'product_kit',
];
$product_types = [];
foreach (uc_product_types() as $type) {
if (!in_array($type, $ignored_types)) {
$product_types[] = $type;
}
}
if (arg(5) == '') {
$args = [
'start_date' => mktime(0, 0, 0, date('n'), 1, date('Y') - 1),
'end_date' => \Drupal::time()
->getRequestTime(),
'status' => uc_report_order_statuses(),
];
}
else {
$args = [
'start_date' => arg(5),
'end_date' => arg(6),
'status' => explode(',', arg(7)),
];
}
$query = $this->database
->select('node', 'n', [
'fetch' => \PDO::FETCH_ASSOC,
])
->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')
->extend('Drupal\\Core\\Database\\Query\\TableSortExtender')
->limit($page_size)
->fields('n', [
'nid',
'title',
])
->condition('type', $product_types, 'IN')
->groupBy('n.nid');
$query
->addExpression("(SELECT SUM(qty) FROM {uc_order_products} p LEFT JOIN {uc_orders} o ON p.order_id = o.order_id WHERE o.order_status IN (:statuses[]) AND p.nid = n.nid AND o.created >= :start AND o.created <= :end)", 'sold', [
':statuses[]' => $args['status'],
':start' => $args['start_date'],
':end' => $args['end_date'],
]);
$query
->addExpression("(SELECT (SUM(p2.price * p2.qty)) FROM {uc_order_products} p2 LEFT JOIN {uc_orders} o ON p2.order_id = o.order_id WHERE o.order_status IN (:statuses[]) AND p2.nid = n.nid AND o.created >= :start AND o.created <= :end)", 'revenue', [
':statuses[]' => $args['status'],
':start' => $args['start_date'],
':end' => $args['end_date'],
]);
$query
->addExpression("(SELECT (SUM(p3.price * p3.qty) - SUM(p3.cost * p3.qty)) FROM {uc_order_products} p3 LEFT JOIN {uc_orders} o ON p3.order_id = o.order_id WHERE o.order_status IN (:statuses[]) AND p3.nid = n.nid AND o.created >= :start AND o.created <= :end)", 'gross', [
':statuses[]' => $args['status'],
':start' => $args['start_date'],
':end' => $args['end_date'],
]);
$header = [
[
'data' => $this
->t('#'),
],
[
'data' => $this
->t('Product'),
'field' => 'n.title',
],
[
'data' => $this
->t('Sold'),
'field' => 'sold',
],
[
'data' => $this
->t('Revenue'),
'field' => 'revenue',
'sort' => 'desc',
],
[
'data' => $this
->t('Gross'),
'field' => 'gross',
],
];
$csv_rows[] = [
$this
->t('#'),
$this
->t('Product'),
$this
->t('Sold'),
$this
->t('Revenue'),
$this
->t('Gross'),
];
if ($views_column) {
$header[] = [
'data' => $this
->t('Views'),
'field' => 'nc.totalcount',
];
$csv_rows[0][] = $this
->t('Views');
}
$query
->orderByHeader($header);
if ($views_column) {
$query
->leftJoin('node_counter', 'c', 'n.nid = c.nid');
$query
->addField('c', 'totalcount');
}
$products = $query
->execute();
foreach ($products as $product) {
$row_cell = $page * $this
->config('uc_report.settings')
->get('table_size') + count($rows) + 1;
$product_cell = Link::createFromRoute($product['title'], 'entity.node.canonical', [
'node' => $product['nid'],
])
->toString();
$product_csv = $product['title'];
$sold_cell = empty($product['sold']) ? 0 : $product['sold'];
$sold_csv = $sold_cell;
$revenue_csv = empty($product['revenue']) ? 0 : $product['revenue'];
$revenue_cell = uc_currency_format($revenue_csv);
$gross_csv = empty($product['gross']) ? 0 : $product['gross'];
$gross_cell = uc_currency_format($gross_csv);
if ($this
->moduleHandler()
->moduleExists('uc_attribute')) {
$breakdown_product = $breakdown_sold = $breakdown_revenue = $breakdown_gross = '';
$models = $this
->product_get_skus($product['nid']);
foreach ($models as $model) {
$sold = $this->database
->query("SELECT SUM(qty) FROM {uc_order_products} p LEFT JOIN {uc_orders} o ON p.order_id = o.order_id WHERE o.order_status IN (:statuses[]) AND p.model = :model AND p.nid = :nid AND o.created >= :start AND o.created <= :end", [
':statuses[]' => $args['status'],
':start' => $args['start_date'],
':end' => $args['end_date'],
':model' => $model,
':nid' => $product['nid'],
])
->fetchField();
$sold = empty($sold) ? 0 : $sold;
$revenue = $this->database
->query("SELECT SUM(p.price * p.qty) FROM {uc_order_products} p LEFT JOIN {uc_orders} o ON p.order_id = o.order_id WHERE o.order_status IN (:statuses[]) AND p.model = :model AND p.nid = :nid AND o.created >= :start AND o.created <= :end", [
':statuses[]' => $args['status'],
':start' => $args['start_date'],
':end' => $args['end_date'],
':model' => $model,
':nid' => $product['nid'],
])
->fetchField();
$revenue = empty($revenue) ? 0 : $revenue;
$gross = $this->database
->query("SELECT (SUM(p.price * p.qty) - SUM(p.cost * p.qty)) FROM {uc_order_products} p LEFT JOIN {uc_orders} o ON p.order_id = o.order_id WHERE o.order_status IN (:statuses[]) AND p.model = :model AND p.nid = :nid AND o.created >= :start AND o.created <= :end", [
':statuses[]' => $args['status'],
':start' => $args['start_date'],
':end' => $args['end_date'],
':model' => $model,
':nid' => $product['nid'],
])
->fetchField();
$gross = empty($gross) ? 0 : $gross;
$breakdown_product .= "<br /> {$model}";
$product_csv .= "\n {$model}";
$breakdown_sold .= "<br />" . $sold;
$sold_csv .= "\n " . $sold;
$breakdown_revenue .= "<br />" . uc_currency_format($revenue);
$revenue_csv .= "\n " . $revenue;
$breakdown_gross .= "<br />" . uc_currency_format($gross);
$gross_csv .= "\n " . $gross;
}
$product_cell = $product_cell . $breakdown_product;
$sold_cell = '<strong>' . $sold_cell . '</strong>' . $breakdown_sold;
$revenue_cell = '<strong>' . $revenue_cell . '</strong>' . $breakdown_revenue;
$gross_cell = '<strong>' . $gross_cell . '</strong>' . $breakdown_gross;
}
if ($views_column) {
$views = empty($product['totalcount']) ? 0 : $product['totalcount'];
$rows[] = [
[
'data' => $row_cell,
],
[
'data' => $product_cell,
],
[
'data' => $sold_cell,
],
[
'data' => $revenue_cell,
],
[
'data' => $gross_cell,
],
[
'data' => $views,
],
];
$csv_rows[] = [
$row_cell,
$product_csv,
$sold_csv,
$revenue_csv,
$gross_csv,
$views,
];
}
else {
$rows[] = [
[
'data' => $row_cell,
],
[
'data' => $product_cell,
],
[
'data' => $sold_cell,
],
[
'data' => $revenue_cell,
],
[
'data' => $gross_cell,
],
];
$csv_rows[] = [
$row_cell,
$product_csv,
$sold_csv,
$revenue_csv,
$gross_csv,
];
}
}
$csv_data = $this
->store_csv('uc_products', $csv_rows);
$build['form'] = $this
->formBuilder()
->getForm('uc_report_products_custom_form', $args);
$build['report'] = [
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#attributes' => [
'width' => '100%',
'class' => [
'uc-sales-table',
],
],
'#empty' => $this
->t('No products found'),
];
$build['pager'] = [
'#type' => 'pager',
];
$build['links'] = [
'#prefix' => '<div class="uc-reports-links">',
'#suffix' => '</div>',
];
$build['links']['export_csv'] = [
'#markup' => Link::createFromRoute($this
->t('Export to CSV file.'), 'uc_report.getcsv', [
'report_id' => $csv_data['report'],
'user_id' => $csv_data['user'],
])
->toString(),
'#suffix' => ' ',
];
if (isset($_GET['nopage'])) {
$build['links']['toggle_pager'] = [
'#markup' => Link::createFromRoute($this
->t('Show paged records'), 'uc_reports.custom_report')
->toString(),
];
}
else {
$build['links']['toggle_pager'] = [
'#markup' => Link::createFromRoute($this
->t('Show all records'), 'uc_reports.custom_report', [
'query' => [
'nopage' => '1',
],
])
->toString(),
];
}
$build['instructions'] = [
'#markup' => '<small>*' . $this
->t('Make sure %setting_name is set to %state in the <a href=":url">access log settings page</a> to enable views column.', [
'%setting_name' => 'count content views',
'%state' => 'enabled',
':url' => Url::fromUri('base:admin/config/system/statistics', [
'query' => [
'destination' => 'admin/store/reports/products/custom',
],
])
->toString(),
]) . '</small>',
];
return $build;
}
public function sales() {
$order_statuses = uc_report_order_statuses();
$date_day_of_month = date('j');
$date_month = date('n');
$month_start = mktime(0, 0, 0, $date_month, 1);
$month_end = mktime(0, 0, 0, $date_month + 1, 1) - 1;
$today_start = mktime(0, 0, 0);
$today_end = mktime(23, 59, 59);
$header = [
$this
->t('Sales data'),
$this
->t('Number of orders'),
$this
->t('Total revenue'),
$this
->t('Average order'),
];
$today = self::get_sales($today_start);
$rows[] = [
Link::fromTextAndUrl($this
->t('Today, @date', [
'@date' => \Drupal::service('date.formatter')
->format($today_start, 'uc_store'),
]), Url::fromUri('base:admin/store/orders/search/results/0/0/0/0/0/0/' . $today_start . '/' . $today_end))
->toString(),
$today['total'],
[
'data' => [
'#theme' => 'uc_price',
'#price' => $today['income'],
],
],
[
'data' => [
'#theme' => 'uc_price',
'#price' => $today['average'],
],
],
];
$yesterday = self::get_sales($today_start - 86400);
$rows[] = [
Link::fromTextAndUrl($this
->t('Yesterday, @date', [
'@date' => \Drupal::service('date.formatter')
->format($today_start - 86400, 'uc_store'),
]), Url::fromUri('base:admin/store/orders/search/results/0/0/0/0/0/0/' . ($today_start - 86400) . '/' . ($today_end - 86400)))
->toString(),
$yesterday['total'],
[
'data' => [
'#theme' => 'uc_price',
'#price' => $yesterday['income'],
],
],
[
'data' => [
'#theme' => 'uc_price',
'#price' => $yesterday['average'],
],
],
];
$month = self::get_sales($month_start, 'month');
$month_title = \Drupal::service('date.formatter')
->format($month_start, 'custom', 'M Y');
$rows[] = [
Link::fromTextAndUrl($this
->t('Month-to-date, @month', [
'@month' => $month_title,
]), Url::fromUri('base:admin/store/orders/search/results/0/0/0/0/0/0/' . $month_start . '/' . $month_end))
->toString(),
$month['total'],
[
'data' => [
'#theme' => 'uc_price',
'#price' => $month['income'],
],
],
[
'data' => [
'#theme' => 'uc_price',
'#price' => $month['average'],
],
],
];
$daily_orders = round($month['total'] / $date_day_of_month, 2);
$daily_revenue = round($month['income'] / $date_day_of_month, 2);
if ($daily_orders > 0) {
$daily_average = round($daily_revenue / $daily_orders, 2);
}
else {
$daily_average = 0;
}
$rows[] = [
$this
->t('Daily average for @month', [
'@month' => $month_title,
]),
$daily_orders,
[
'data' => [
'#theme' => 'uc_price',
'#price' => $daily_revenue,
],
],
'',
];
$remaining_days = date('t') - $date_day_of_month;
$rows[] = [
$this
->t('Projected totals for @date', [
'@date' => $month_title,
]),
round($month['total'] + $daily_orders * $remaining_days, 2),
[
'data' => [
'#theme' => 'uc_price',
'#price' => round($month['income'] + $daily_revenue * $remaining_days, 2),
],
],
'',
];
$build['sales'] = [
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#attributes' => [
'class' => [
'uc-sales-table',
],
],
];
$header = [
[
'data' => $this
->t('Statistics'),
'width' => '50%',
],
'',
];
$rows = [
[
[
'data' => $this
->t('Grand total sales'),
],
[
'data' => [
'#theme' => 'uc_price',
'#price' => $this->database
->query("SELECT SUM(order_total) FROM {uc_orders} WHERE order_status IN (:statuses[])", [
':statuses[]' => $order_statuses,
])
->fetchField(),
],
],
],
[
[
'data' => $this
->t('Customers total'),
],
[
'data' => $this->database
->query("SELECT COUNT(DISTINCT uid) FROM {uc_orders} WHERE order_status IN (:statuses[])", [
':statuses[]' => $order_statuses,
])
->fetchField(),
],
],
[
[
'data' => $this
->t('New customers today'),
],
[
'data' => $this->database
->query("SELECT COUNT(DISTINCT uid) FROM {uc_orders} WHERE order_status IN (:statuses[]) AND :start <= created AND created <= :end", [
':statuses[]' => $order_statuses,
':start' => $today_start,
':end' => $today_end,
])
->fetchField(),
],
],
[
[
'data' => $this
->t('Online customers'),
],
[
'data' => $this->database
->query("SELECT COUNT(DISTINCT s.uid) FROM {sessions} s LEFT JOIN {uc_orders} o ON s.uid = o.uid WHERE s.uid > 0 AND o.order_status IN (:statuses[])", [
':statuses[]' => $order_statuses,
])
->fetchField(),
],
],
];
$build['statistics'] = [
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#attributes' => [
'width' => '100%',
'class' => [
'uc-sales-table',
],
],
];
$header = [
[
'data' => $this
->t('Total orders by status'),
'width' => '50%',
],
'',
];
$rows = [];
$unknown = 0;
if ($unknown > 0) {
$rows[] = [
$this
->t('Unknown status'),
$unknown,
];
}
$build['orders'] = [
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#attributes' => [
'class' => [
'uc-sales-table',
],
],
];
return $build;
}
public function yearSales() {
if (intval(arg(5)) == 0) {
$year = date('Y');
}
else {
$year = arg(5);
}
$header = [
$this
->t('Month'),
$this
->t('Number of orders'),
$this
->t('Total revenue'),
$this
->t('Average order'),
];
$csv_rows = [
[
$this
->t('Month'),
$this
->t('Number of orders'),
$this
->t('Total revenue'),
$this
->t('Average order'),
],
];
for ($i = 1; $i <= 12; $i++) {
$month_start = mktime(0, 0, 0, $i, 1, $year);
$month_end = mktime(23, 59, 59, $i + 1, 0, $year);
$month_sales = self::get_sales($month_start, 'month');
if ($month_sales['total'] != 0) {
$month_average = round($month_sales['income'] / $month_sales['total'], 2);
}
else {
$month_average = 0;
}
$rows[] = [
Link::fromTextAndUrl(date('M Y', $month_start), Url::fromUri('base:admin/store/orders/search/results/0/0/0/0/0/0/' . $month_start . '/' . $month_end))
->toString(),
$month_sales['total'],
uc_currency_format($month_sales['income']),
uc_currency_format($month_average),
];
$csv_rows[] = [
date('M Y', $month_start),
$month_sales['total'],
$month_sales['income'],
$month_average,
];
}
$year_start = mktime(0, 0, 0, 1, 1, $year);
$year_end = mktime(23, 59, 59, 1, 0, $year + 1);
$year_sales = self::get_sales($year_start, 'year');
if ($year_sales['total'] != 0) {
$year_average = round($year_sales['income'] / $year_sales['total'], 2);
}
else {
$year_average = 0;
}
$rows[] = [
Link::fromTextAndUrl($this
->t('Total @year', [
'@year' => $year,
]), Url::fromUri('base:admin/store/orders/search/results/0/0/0/0/0/0/' . $year_start . '/' . $year_end))
->toString(),
$year_sales['total'],
uc_currency_format($year_sales['income']),
uc_currency_format($year_average),
];
$csv_rows[] = [
$this
->t('Total @year', [
'@year' => $year,
]),
$year_sales['total'],
$year_sales['income'],
$year_average,
];
$csv_data = $this
->store_csv('uc_sales_yearly', $csv_rows);
$build['form'] = $this
->formBuilder()
->getForm('uc_report_sales_year_form', $year);
$build['report'] = [
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#attributes' => [
'width' => '100%',
'class' => [
'uc-sales-table',
],
],
];
$build['links'] = [
'#prefix' => '<div class="uc-reports-links">',
'#suffix' => '</div>',
];
$build['links']['export_csv'] = [
'#markup' => Link::createFromRoute($this
->t('Export to CSV file.'), 'uc_report.getcsv', [
'report_id' => $csv_data['report'],
'user_id' => $csv_data['user'],
])
->toString(),
];
return $build;
}
public function customSales() {
if (arg(5) == '') {
$args = [
'start_date' => mktime(0, 0, 0, date('n'), 1, date('Y') - 1),
'end_date' => \Drupal::time()
->getRequestTime(),
'length' => 'month',
'status' => uc_report_order_statuses(),
'detail' => FALSE,
];
}
else {
$args = [
'start_date' => arg(5),
'end_date' => arg(6),
'length' => arg(7),
'status' => explode(',', arg(8)),
'detail' => arg(9),
];
}
$header = [
$this
->t('Date'),
$this
->t('Number of orders'),
$this
->t('Products sold'),
$this
->t('Total revenue'),
];
$csv_rows = [
[
$this
->t('Date'),
$this
->t('Number of orders'),
$this
->t('Products sold'),
$this
->t('Total revenue'),
],
];
$subreports = $this
->subreport_intervals($args['start_date'], $args['end_date'], $args['length']);
foreach ($subreports as $subreport) {
$product_data = '';
$product_csv = '';
if ($args['length'] == 'day') {
$date = \Drupal::service('date.formatter')
->format($subreport['start'], 'uc_store');
}
else {
$date = \Drupal::service('date.formatter')
->format($subreport['start'], 'uc_store') . ' - ' . \Drupal::service('date.formatter')
->format($subreport['end'], 'uc_store');
}
$result = $this->database
->query("SELECT COUNT(*) as count, title FROM {uc_orders} LEFT JOIN {uc_order_statuses} ON order_status_id = order_status WHERE :start <= created AND created <= :end AND order_status IN (:statuses[]) GROUP BY order_status, {uc_order_statuses}.title, {uc_order_statuses}.weight ORDER BY weight ASC", [
':statuses[]' => $args['status'],
':start' => $subreport['start'],
':end' => $subreport['end'],
]);
$statuses = [];
foreach ($result as $status) {
$statuses[] = $this
->t('@count - @title', [
'@count' => $status->count,
'@title' => $status->title,
]);
}
$order_data = implode('<br />', $statuses);
$order_csv = implode("\n", $statuses);
if ($args['detail']) {
$result = $this->database
->query("SELECT SUM(op.qty) as count, n.title, n.nid FROM {uc_order_products} op LEFT JOIN {uc_orders} o ON o.order_id = op.order_id LEFT JOIN {node_field_data} n ON n.nid = op.nid WHERE :start <= o.created AND o.created <= :end AND o.order_status IN (:statuses[]) GROUP BY n.nid ORDER BY count DESC, n.title ASC", [
':statuses[]' => $args['status'],
':start' => $subreport['start'],
':end' => $subreport['end'],
]);
foreach ($result as $product_breakdown) {
$product_data .= $product_breakdown->count . ' x ' . Link::createFromRoute($product_breakdown->title, 'entity.node.canonical', [
'node' => $product_breakdown->nid,
])
->toString() . "<br />\n";
$product_csv .= $product_breakdown->count . ' x ' . $product_breakdown->title . "\n";
}
}
else {
$product_data = $this->database
->query("SELECT SUM(qty) FROM {uc_orders} o LEFT JOIN {uc_order_products} op ON o.order_id = op.order_id WHERE :start <= created AND created <= :end AND order_status IN (:statuses[])", [
':statuses[]' => $args['status'],
':start' => $subreport['start'],
':end' => $subreport['end'],
])
->fetchField();
$product_csv = $product_data;
}
$revenue_count = $this->database
->query("SELECT SUM(order_total) FROM {uc_orders} WHERE :start <= created AND created <= :end AND order_status IN (:statuses[])", [
':statuses[]' => $args['status'],
':start' => $subreport['start'],
':end' => $subreport['end'],
])
->fetchField();
$rows[] = [
$date,
empty($order_data) ? '0' : $order_data,
empty($product_data) ? '0' : $product_data,
uc_currency_format($revenue_count),
];
$csv_rows[] = [
$date,
empty($order_csv) ? '0' : $order_csv,
empty($product_csv) ? '0' : $product_csv,
$revenue_count,
];
}
$order_total = $this->database
->query("SELECT COUNT(*) FROM {uc_orders} WHERE :start <= created AND created <= :end AND order_status IN (:statuses[])", [
':statuses[]' => $args['status'],
':start' => $args['start_date'],
':end' => $args['end_date'],
])
->fetchField();
$product_total = $this->database
->query("SELECT SUM(qty) FROM {uc_orders} o LEFT JOIN {uc_order_products} op ON o.order_id = op.order_id WHERE :start <= created AND created <= :end AND order_status IN (:statuses[])", [
':statuses[]' => $args['status'],
':start' => $args['start_date'],
':end' => $args['end_date'],
])
->fetchField();
$revenue_total = $this->database
->query("SELECT SUM(order_total) FROM {uc_orders} WHERE :start <= created AND created <= :end AND order_status IN (:statuses[])", [
':statuses[]' => $args['status'],
':start' => $args['start_date'],
':end' => $args['end_date'],
])
->fetchField();
$rows[] = [
$this
->t('Total'),
$order_total,
$product_total,
uc_currency_format($revenue_total),
];
$csv_rows[] = [
$this
->t('Total'),
$order_total,
$product_total,
$revenue_total,
];
$csv_data = $this
->store_csv('uc_sales_custom', $csv_rows);
$build['form'] = $this
->formBuilder()
->getForm('uc_report_sales_custom_form', $args, $args['status']);
$build['report'] = [
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#attributes' => [
'width' => '100%',
'class' => [
'uc-sales-table',
],
],
];
$build['links'] = [
'#prefix' => '<div class="uc-reports-links">',
'#suffix' => '</div>',
];
$build['links']['export_csv'] = [
'#markup' => Link::createFromRoute($this
->t('Export to CSV file.'), 'uc_report.getcsv', [
'report_id' => $csv_data['report'],
'user_id' => $csv_data['user'],
])
->toString(),
];
return $build;
}
public function store_csv($report_id, $rows) {
$account = $this
->currentUser();
$csv_output = '';
$user_id = $account
->isAnonymous() ? session_id() : $account
->id();
foreach ($rows as $row) {
foreach ($row as $index => $column) {
$row[$index] = '"' . str_replace('"', '""', $column) . '"';
}
$csv_output .= implode(',', $row) . "\n";
}
\Drupal::cache()
->set('uc_report_' . $report_id . '_' . $user_id, $csv_output, \Drupal::time()
->getRequestTime() + 86400);
return [
'user' => $user_id,
'report' => $report_id,
'csv' => $csv_output,
];
}
public function getCSV($report_id, $user_id) {
$account = $this
->currentUser();
$user_check = $account
->isAnonymous() ? session_id() : $account
->id();
$csv_data = \Drupal::cache()
->get('uc_report_' . $report_id . '_' . $user_id);
if (!$csv_data || $user_id != $user_check) {
$this
->messenger()
->addError($this
->t("The CSV data could not be retrieved. It's possible the data might have expired. Refresh the report page and try to retrieve the CSV file again."));
throw new NotFoundHttpException();
}
else {
$response = new Response($csv_data->data);
$http_headers = [
'Pragma' => 'private',
'Expires' => '0',
'Cache-Control' => 'private, must-revalidate',
'Content-Transfer-Encoding' => 'binary',
'Content-Length' => strlen($csv_data->data),
'Content-Disposition' => 'attachment; filename="' . $report_id . '.csv"',
'Content-Type' => 'text/csv',
];
foreach ($http_headers as $header => $value) {
$value = preg_replace('/\\r?\\n(?!\\t| )/', '', $value);
$response->headers
->set($header, $value);
}
return $response;
}
}
public function get_sales($start, $interval = 'day') {
$end = strtotime('+1 ' . $interval, $start) - 1;
$order_statuses = uc_report_order_statuses();
$output = [
'income' => 0,
];
$orders = $this->database
->query("SELECT o.order_total FROM {uc_orders} o WHERE o.order_status IN (:statuses[]) AND :start <= created AND created <= :end", [
':statuses[]' => $order_statuses,
':start' => $start,
':end' => $end,
]);
while ($order = $orders
->fetchObject()) {
$output['income'] += $order->order_total;
}
$count = $this->database
->query("SELECT COUNT(o.order_total) FROM {uc_orders} o WHERE o.order_status IN (:statuses[]) AND :start <= created AND created <= :end", [
':statuses[]' => $order_statuses,
':start' => $start,
':end' => $end,
])
->fetchField();
$output['total'] = $count;
$output['average'] = $count != 0 ? round($output['income'] / $count, 2) : 0;
return $output;
}
public function subreport_intervals($start, $report_end, $interval) {
$subreports = [];
while ($start < $report_end) {
$end = strtotime('+1 ' . $interval, $start) - 1;
$subreports[] = [
'start' => $start,
'end' => min($end, $report_end),
];
$start = $end + 1;
}
return $subreports;
}
}