Page MenuHomeIn-Portal Phabricator

D548.id1432.diff
No OneTemporary

File Metadata

Created
Fri, Jan 9, 1:55 PM

D548.id1432.diff

Index: modules/in-commerce/constants.php
===================================================================
--- modules/in-commerce/constants.php
+++ modules/in-commerce/constants.php
@@ -135,3 +135,13 @@
const CATEGORY = 2;
const WHOLE_ORDER = 0;
}
+
+class SalesReportConstants
+{
+
+ const TYPE_OVERALL = 12;
+ const TYPE_BY_CATEGORY = 1; // With eBay support.
+ const TYPE_BY_USER = 2;
+ const TYPE_BY_PRODUCT = 5; // With eBay support.
+
+}
Index: modules/in-commerce/units/reports/SalesReportGenerator/AbstractProductCategorySalesReportGenerator.php
===================================================================
--- /dev/null
+++ modules/in-commerce/units/reports/SalesReportGenerator/AbstractProductCategorySalesReportGenerator.php
@@ -0,0 +1,278 @@
+<?php
+
+
+abstract class AbstractProductCategorySalesReportGenerator extends AbstractSalesReportGenerator
+{
+
+ /**
+ * Name field.
+ *
+ * @var string
+ */
+ protected $nameField;
+
+ /**
+ * Name field phrase.
+ *
+ * @var string
+ */
+ protected $nameFieldPhrase;
+
+ /**
+ * Returns e-Bay data.
+ *
+ * @return string[]
+ */
+ protected function getEbayConfig()
+ {
+ if ( !$this->Application->isModuleEnabled('in-auction') ) {
+ return array('', '', '');
+ }
+
+ $ebay_table_fields = ',
+ StoreQty int(11) NOT NULL DEFAULT 0,
+ eBayQty int(11) NOT NULL DEFAULT 0,
+ StoreAmount double(10,4) NOT NULL DEFAULT 0,
+ eBayAmount double(10,4) NOT NULL DEFAULT 0,
+ StoreProfit double(10,4) NOT NULL DEFAULT 0,
+ eBayProfit double(10,4) NOT NULL DEFAULT 0';
+
+ $ebay_joins = '
+ LEFT JOIN ' . TABLE_PREFIX . 'eBayOrderItems AS eod
+ ON od.OptionsSalt = eod.OptionsSalt
+ ';
+
+ $ebay_query_fields = ',
+ SUM(IF(ISNULL(eod.OptionsSalt), od.Quantity, 0)) as StoreQty,
+ SUM(IF(ISNULL(eod.OptionsSalt), 0, od.Quantity)) as eBayQty,
+ SUM(IF(ISNULL(eod.OptionsSalt), od.Price * od.Quantity, 0)) as StoreAmount,
+ SUM(IF(ISNULL(eod.OptionsSalt), 0, od.Price * od.Quantity)) as eBayAmount,
+ SUM(IF(ISNULL(eod.OptionsSalt), (od.Price - od.Cost) * od.Quantity, 0)) as StoreProfit,
+ SUM(IF(ISNULL(eod.OptionsSalt), 0, (od.Price - od.Cost) * od.Quantity)) as eBayProfit
+ ';
+
+ return array($ebay_table_fields, $ebay_joins, $ebay_query_fields);
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function updateUnitConfig(array $report_options)
+ {
+ parent::updateUnitConfig($report_options);
+
+ if ( $this->Application->isModuleEnabled('in-auction') ) {
+ $integer_field_config = array(
+ 'type' => 'int',
+ 'formatter' => 'kFormatter', 'format' => '%d',
+ 'default' => 0,
+ 'totals' => 'sum',
+ );
+ $float_field_config = array(
+ 'type' => 'float',
+ 'formatter' => 'kFormatter', 'format' => '%.2f',
+ 'default' => 0,
+ 'totals' => 'sum',
+ );
+
+ $new_fields = array(
+ 'StoreQty' => $integer_field_config,
+ 'StoreAmount' => $float_field_config,
+ 'StoreProfit' => $float_field_config,
+ 'eBayQty' => $integer_field_config,
+ 'eBayAmount' => $float_field_config,
+ 'eBayProfit' => $float_field_config,
+ );
+ }
+
+ $new_fields[$this->idField] = array('type' => 'int', 'default' => null);
+
+ /** @var array $fields */
+ $fields = $this->getUnitOption('Fields');
+ $this->setUnitOption('Fields', $fields + $new_fields);
+
+ /** @var array $grids */
+ $grids = $this->getUnitOption('Grids');
+
+ if ( $this->Application->isModuleEnabled('in-auction') ) {
+ $grids[$this->grid] = array(
+ 'Icons' => array(
+ 'default' => 'icon16_item.png',
+ 'module' => 'core',
+ ),
+ 'Fields' => array(
+ $this->idField => array(
+ 'title' => 'column:la_fld_Id',
+ 'filter_block' => 'grid_range_filter',
+ 'hidden' => 1,
+ ),
+ $this->nameField => array(
+ 'title' => $this->nameFieldPhrase,
+ 'filter_block' => 'grid_like_filter',
+ ),
+ 'Qty' => array(
+ 'filter_block' => 'grid_range_filter',
+ 'td_style' => 'text-align: center',
+ 'total' => 'sum',
+ ),
+ 'StoreQty' => array(
+ 'title' => 'la_col_StoreQty',
+ 'filter_block' => 'grid_range_filter',
+ 'td_style' => 'text-align: center',
+ 'total' => 'sum',
+ ),
+ 'eBayQty' => array(
+ 'title' => 'la_col_eBayQty',
+ 'filter_block' => 'grid_range_filter',
+ 'td_style' => 'text-align: center',
+ 'total' => 'sum',
+ ),
+ 'Cost' => array(
+ 'filter_block' => 'grid_range_filter',
+ 'td_style' => 'text-align: right',
+ 'total' => 'sum',
+ 'hidden' => 1,
+ ),
+ 'Amount' => array(
+ 'title' => 'la_col_GMV',
+ 'filter_block' => 'grid_range_filter',
+ 'td_style' => 'text-align: right',
+ 'total' => 'sum',
+ ),
+ 'StoreAmount' => array(
+ 'title' => 'la_col_StoreGMV',
+ 'filter_block' => 'grid_range_filter',
+ 'td_style' => 'text-align: right',
+ 'total' => 'sum',
+ ),
+ 'eBayAmount' => array(
+ 'title' => 'la_col_eBayGMV',
+ 'filter_block' => 'grid_range_filter',
+ 'td_style' => 'text-align: right',
+ 'total' => 'sum',
+ ),
+ 'Tax' => array(
+ 'title' => 'la_col_Tax',
+ 'filter_block' => 'grid_range_filter',
+ 'td_style' => 'text-align: right',
+ 'total' => 'sum',
+ 'hidden' => 1,
+ ),
+ 'Shipping' => array(
+ 'title' => 'la_col_Shipping',
+ 'filter_block' => 'grid_range_filter',
+ 'td_style' => 'text-align: right',
+ 'total' => 'sum',
+ 'hidden' => 1,
+ ),
+ 'Processing' => array(
+ 'title' => 'la_col_Processing',
+ 'filter_block' => 'grid_range_filter',
+ 'td_style' => 'text-align: right',
+ 'total' => 'sum',
+ 'hidden' => 1,
+ ),
+ 'Profit' => array(
+ 'title' => 'la_col_Profit',
+ 'filter_block' => 'grid_range_filter',
+ 'td_style' => 'text-align: right',
+ 'total' => 'sum',
+ ),
+ 'StoreProfit' => array(
+ 'title' => 'la_col_StoreProfit',
+ 'filter_block' => 'grid_range_filter',
+ 'td_style' => 'text-align: right',
+ 'total' => 'sum',
+ ),
+ 'eBayProfit' => array(
+ 'title' => 'la_col_eBayProfit',
+ 'filter_block' => 'grid_range_filter',
+ 'td_style' => 'text-align: right',
+ 'total' => 'sum',
+ ),
+ ),
+ );
+ }
+ else {
+ $grids[$this->grid] = array(
+ 'Icons' => array(
+ 'default' => 'icon16_item.png',
+ 'module' => 'core',
+ ),
+ 'Fields' => array(
+ $this->idField => array(
+ 'title' => 'column:la_fld_Id',
+ 'filter_block' => 'grid_range_filter',
+ 'hidden' => 1,
+ ),
+ $this->nameField => array(
+ 'title' => $this->nameFieldPhrase,
+ 'filter_block' => 'grid_like_filter',
+ ),
+ 'Qty' => array(
+ 'filter_block' => 'grid_range_filter',
+ 'td_style' => 'text-align: center',
+ 'total' => 'sum',
+ ),
+ 'Cost' => array(
+ 'filter_block' => 'grid_range_filter',
+ 'td_style' => 'text-align: right',
+ 'total' => 'sum',
+ 'hidden' => 1,
+ ),
+ 'Amount' => array(
+ 'title' => 'la_col_GMV',
+ 'filter_block' => 'grid_range_filter',
+ 'td_style' => 'text-align: right',
+ 'total' => 'sum',
+ ),
+ 'Tax' => array(
+ 'title' => 'la_col_Tax',
+ 'filter_block' => 'grid_range_filter',
+ 'td_style' => 'text-align: right',
+ 'total' => 'sum',
+ 'hidden' => 1,
+ ),
+ 'Shipping' => array(
+ 'title' => 'la_col_Shipping',
+ 'filter_block' => 'grid_range_filter',
+ 'td_style' => 'text-align: right',
+ 'total' => 'sum',
+ 'hidden' => 1,
+ ),
+ 'Processing' => array(
+ 'title' => 'la_col_Processing',
+ 'filter_block' => 'grid_range_filter',
+ 'td_style' => 'text-align: right',
+ 'total' => 'sum',
+ 'hidden' => 1,
+ ),
+ 'Profit' => array(
+ 'title' => 'la_col_Profit',
+ 'filter_block' => 'grid_range_filter',
+ 'td_style' => 'text-align: right',
+ 'total' => 'sum',
+ ),
+ ),
+ );
+ }
+
+ $this->setUnitOption('Grids', $grids);
+
+ $new_virtual_fields = array(
+ $this->nameField => array('type' => 'string', 'default' => ''),
+ 'Metric' => array(
+ 'type' => 'int',
+ 'formatter' => 'kOptionsFormatter',
+ 'options' => $this->getMetricOptions($grids, array($this->idField, $this->nameField)),
+ 'use_phrases' => 1,
+ 'default' => 0,
+ ),
+ );
+
+ /** @var array $virtual_fields */
+ $virtual_fields = $this->getUnitOption('VirtualFields');
+ $this->setUnitOption('VirtualFields', $virtual_fields + $new_virtual_fields);
+ }
+
+}
Index: modules/in-commerce/units/reports/SalesReportGenerator/AbstractSalesReportGenerator.php
===================================================================
--- /dev/null
+++ modules/in-commerce/units/reports/SalesReportGenerator/AbstractSalesReportGenerator.php
@@ -0,0 +1,260 @@
+<?php
+
+
+abstract class AbstractSalesReportGenerator extends kBase
+{
+
+ /**
+ * Backup of the changed unit config options.
+ *
+ * @var array
+ */
+ static $unitConfigBackup = array();
+
+ /**
+ * Report type.
+ *
+ * @var integer
+ */
+ protected $reportType;
+
+ /**
+ * ID field.
+ *
+ * @var string
+ */
+ protected $idField;
+
+ /**
+ * Table name.
+ *
+ * @var string
+ */
+ protected $tableName;
+
+ /**
+ * Export ignore.
+ *
+ * @var string
+ */
+ protected $exportIgnore;
+
+ /**
+ * Grid name.
+ *
+ * @var string
+ */
+ protected $grid;
+
+ /**
+ * Returns filter defaults.
+ *
+ * @return array
+ */
+ protected function getFilterDefaults()
+ {
+ return array(
+ 'FromDateTime' => null, // Timestamp.
+ 'ToDateTime' => null, // Timestamp.
+ );
+ }
+
+ /**
+ * Binds to given fields.
+ *
+ * @param array $field_values Field values.
+ *
+ * @return self
+ */
+ public function bind(array &$field_values)
+ {
+ $field_values['id_field'] = $this->idField;
+ $field_values['export_ignore'] = $this->exportIgnore;
+ $field_values['grid'] = $this->grid;
+
+ $total = $this->getTotal();
+
+ if ( $total !== null ) {
+ $field_values['total'] = $total;
+ }
+
+ return $this;
+ }
+
+ /**
+ * Prepares the data.
+ *
+ * @param string $table_name Table name.
+ * @param array $filters Filters.
+ *
+ * @return self
+ */
+ public function prepareData($table_name, array $filters)
+ {
+ $this->tableName = $table_name;
+
+ $this->createTable();
+ $this->fillTable(array_merge($this->getFilterDefaults(), $filters));
+
+ return $this;
+ }
+
+ /**
+ * Creates a table.
+ *
+ * @return void
+ */
+ abstract protected function createTable();
+
+ /**
+ * Fills a table.
+ *
+ * @param array $filters Filters.
+ *
+ * @return void
+ */
+ abstract protected function fillTable(array $filters);
+
+ /**
+ * Builds a date range filter.
+ *
+ * @param string $field Field.
+ * @param array $filters Filters.
+ *
+ * @return string
+ */
+ protected function getDateRangeFilter($field, array $filters)
+ {
+ if ( !$filters['FromDateTime'] || !$filters['ToDateTime'] ) {
+ return '';
+ }
+
+ return $field . ' BETWEEN ' . $filters['FromDateTime'] . ' AND ' . $filters['ToDateTime'];
+ }
+
+ /**
+ * Returns total record count in the report.
+ *
+ * @return integer|null
+ */
+ protected function getTotal()
+ {
+ return null;
+ }
+
+ /**
+ * Updates unit config.
+ *
+ * @param array $report_options Report options.
+ *
+ * @return void
+ */
+ public function updateUnitConfig(array $report_options)
+ {
+ // Restore unit config options from the backup.
+ foreach ( self::$unitConfigBackup as $option_name => $option_value ) {
+ $this->setUnitOption($option_name, $option_value);
+ }
+
+ // Set new unit config options.
+ $this->setUnitOption('TableName', $report_options['table_name']);
+
+ $float_field_config = array(
+ 'type' => 'float',
+ 'formatter' => 'kFormatter', 'format' => '%.2f',
+ 'default' => 0,
+ 'totals' => 'sum',
+ );
+ $integer_field_config = array(
+ 'type' => 'float',
+ 'formatter' => 'kFormatter', 'format' => '%d',
+ 'default' => 0,
+ 'totals' => 'sum',
+ );
+ $new_fields = array(
+ 'Qty' => $integer_field_config,
+ 'Cost' => $float_field_config,
+ 'Amount' => $float_field_config,
+ 'Tax' => $float_field_config,
+ 'Shipping' => $float_field_config,
+ 'Processing' => $float_field_config,
+ 'Profit' => $float_field_config,
+ );
+ $this->setUnitOption('Fields', $new_fields);
+
+ /** @var array $list_sortings */
+ $list_sortings = $this->getUnitOption('ListSortings');
+
+ $list_sortings[''] = array(
+ 'Sorting' => array('Amount' => 'desc'),
+ );
+
+ $this->setUnitOption('ListSortings', $list_sortings);
+ }
+
+ /**
+ * Gets report unit option value.
+ *
+ * @param string $name Option name.
+ * @param mixed $default Default value.
+ *
+ * @return string
+ */
+ public function getUnitOption($name, $default = false)
+ {
+ return $this->Application->getUnitOption('rep', $name, $default);
+ }
+
+ /**
+ * Set a new report unit option value.
+ *
+ * @param string $name Option name.
+ * @param string $value Option value.
+ *
+ * @return void
+ */
+ protected function setUnitOption($name, $value)
+ {
+ if ( !array_key_exists($name, self::$unitConfigBackup) ) {
+ self::$unitConfigBackup[$name] = $this->Application->getUnitOption('rep', $name);
+ }
+
+ $this->Application->setUnitOption('rep', $name, $value);
+ }
+
+ /**
+ * Generate Metric Field Options
+ *
+ * @param array $grids Grids.
+ * @param array $exclude_fields Exclude fields.
+ *
+ * @return array
+ */
+ protected function getMetricOptions(array $grids, array $exclude_fields)
+ {
+ $ret = array();
+
+ foreach ( $grids[$this->grid]['Fields'] as $field => $a_options ) {
+ if ( in_array($field, $exclude_fields) ) {
+ continue;
+ }
+
+ $ret[$field] = isset($a_options['title']) ? $a_options['title'] : 'column:la_fld_' . $field;
+ }
+
+ return $ret;
+ }
+
+ /**
+ * Returns export filename.
+ *
+ * @param array $report_options Report options.
+ *
+ * @return string
+ */
+ public function getExportFilename(array $report_options)
+ {
+ return 'SalesReport-' . date('d-M-Y') . '.csv';
+ }
+
+}
Index: modules/in-commerce/units/reports/SalesReportGenerator/ByCategorySalesReportGenerator.php
===================================================================
--- /dev/null
+++ modules/in-commerce/units/reports/SalesReportGenerator/ByCategorySalesReportGenerator.php
@@ -0,0 +1,136 @@
+<?php
+
+
+class ByCategorySalesReportGenerator extends AbstractProductCategorySalesReportGenerator
+{
+
+ /**
+ * @inheritDoc
+ */
+ public function __construct()
+ {
+ parent::__construct();
+
+ $this->nameField = 'CategoryName';
+ $this->nameFieldPhrase = 'la_col_CategoryName';
+
+ $this->reportType = SalesReportConstants::TYPE_BY_CATEGORY;
+ $this->idField = 'CategoryId';
+ $this->exportIgnore = 'CategoryId';
+ $this->grid = 'ByCategory';
+ }
+
+ /**
+ * @inheritDoc
+ */
+ protected function createTable()
+ {
+ list($ebay_table_fields, ,) = $this->getEbayConfig();
+
+ $sql = 'CREATE TABLE ' . $this->tableName . ' (
+ CategoryId int(11) NOT NULL DEFAULT 0,
+ Qty int(11) NOT NULL DEFAULT 0,
+ Cost double(10,4) NOT NULL DEFAULT 0,
+ Amount double(10,4) NOT NULL DEFAULT 0,
+ Tax double(10,4) NOT NULL DEFAULT 0,
+ Shipping double(10,4) NOT NULL DEFAULT 0,
+ Processing double(10,4) NOT NULL DEFAULT 0,
+ Profit double(10,4) NOT NULL DEFAULT 0
+ ' . $ebay_table_fields . '
+ )';
+ $this->Conn->Query($sql);
+ }
+
+ /**
+ * @inheritDoc
+ */
+ protected function fillTable(array $filters)
+ {
+ list(, $ebay_joins, $ebay_query_fields) = $this->getEbayConfig();
+
+ $date_range_filter = $this->getDateRangeFilter('o.OrderDate', $filters);
+
+ $sql = 'INSERT INTO ' . $this->tableName . '
+ SELECT
+ c.CategoryId,
+ SUM(od.Quantity) as Qty,
+ SUM(od.Cost * od.Quantity) as Cost,
+ SUM(od.Price * od.Quantity) as SaleAmount,
+ SUM(o.VAT * od.Price * od.Quantity / o.SubTotal) as Tax,
+ SUM(o.ShippingCost * od.Price * od.Quantity / o.SubTotal) as Shipping,
+ SUM(o.ProcessingFee * od.Price * od.Quantity / o.SubTotal) as Processing,
+ SUM((od.Price - od.Cost) * od.Quantity) as Profit'
+ . $ebay_query_fields . '
+ FROM ' . TABLE_PREFIX . 'Orders AS o
+ LEFT JOIN ' . TABLE_PREFIX . 'OrderItems AS od
+ ON od.OrderId = o.OrderId
+ LEFT JOIN ' . TABLE_PREFIX . 'Products AS p
+ ON p.ProductId = od.ProductId
+ LEFT JOIN ' . TABLE_PREFIX . 'CategoryItems AS ci
+ ON ci.ItemResourceId = p.ResourceId
+ LEFT JOIN ' . TABLE_PREFIX . 'Categories AS c
+ ON c.CategoryId = ci.CategoryId
+ ' . $ebay_joins . '
+ WHERE
+ o.Status IN (' . ORDER_STATUS_PROCESSED . ',' . ORDER_STATUS_ARCHIVED . ')
+ AND
+ ci.PrimaryCat = 1
+ ' . ($date_range_filter ? ' AND ' . $date_range_filter : '') . '
+ GROUP BY c.CategoryId
+ HAVING NOT ISNULL(CategoryId)';
+ $this->Conn->Query($sql);
+ }
+
+ /**
+ * @inheritDoc
+ */
+ protected function getTotal()
+ {
+ $sql = 'SELECT COUNT(*)
+ FROM ' . TABLE_PREFIX . 'Categories';
+
+ return $this->Conn->GetOne($sql);
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function getExportFilename(array $report_options)
+ {
+ return 'SalesReport-ByCategory-' . date('d-M-Y') . '.csv';
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function updateUnitConfig(array $report_options)
+ {
+ parent::updateUnitConfig($report_options);
+
+ /** @var array $list_sqls */
+ $list_sqls = $this->getUnitOption('ListSQLs');
+ $list_sqls[''] = ' SELECT %1$s.* %2$s
+ FROM %1$s
+ LEFT JOIN ' . TABLE_PREFIX . 'Categories AS c ON c.CategoryId = %1$s.CategoryId';
+ $this->setUnitOption('ListSQLs', $list_sqls);
+
+ $lang = $this->Application->GetVar('m_lang');
+
+ // Products root category.
+ $products_category_id = $this->Application->findModule('Name', 'In-Commerce', 'RootCat');
+
+ // Get a root category name.
+ $sql = 'SELECT LENGTH(l' . $lang . '_CachedNavbar)
+ FROM ' . TABLE_PREFIX . 'Categories
+ WHERE CategoryId = ' . $products_category_id;
+ $root_length = $this->Conn->GetOne($sql) + 4;
+
+ /** @var array $calculated_fields */
+ $calculated_fields = $this->getUnitOption('CalculatedFields');
+ $calculated_fields[''] = array(
+ 'CategoryName' => 'REPLACE(SUBSTR(c.l' . $lang . '_CachedNavbar, ' . $root_length . '), "&|&", " > ")',
+ );
+ $this->setUnitOption('CalculatedFields', $calculated_fields);
+ }
+
+}
Index: modules/in-commerce/units/reports/SalesReportGenerator/ByProductSalesReportGenerator.php
===================================================================
--- /dev/null
+++ modules/in-commerce/units/reports/SalesReportGenerator/ByProductSalesReportGenerator.php
@@ -0,0 +1,123 @@
+<?php
+
+
+class ByProductSalesReportGenerator extends AbstractProductCategorySalesReportGenerator
+{
+
+ /**
+ * @inheritDoc
+ */
+ public function __construct()
+ {
+ parent::__construct();
+
+ $this->nameField = 'ProductName';
+ $this->nameFieldPhrase = 'la_col_ProductName';
+
+ $this->reportType = SalesReportConstants::TYPE_BY_PRODUCT;
+ $this->idField = 'ProductId';
+ $this->exportIgnore = 'ProductId';
+ $this->grid = 'ByProduct';
+ }
+
+ /**
+ * @inheritDoc
+ */
+ protected function createTable()
+ {
+ list($ebay_table_fields, ,) = $this->getEbayConfig();
+
+ $sql = 'CREATE TABLE ' . $this->tableName . ' (
+ ProductId int(11) NOT NULL DEFAULT 0,
+ Qty int(11) NOT NULL DEFAULT 0,
+ Cost double(10,4) NOT NULL DEFAULT 0,
+ Amount double(10,4) NOT NULL DEFAULT 0,
+ Tax double(10,4) NOT NULL DEFAULT 0,
+ Shipping double(10,4) NOT NULL DEFAULT 0,
+ Processing double(10,4) NOT NULL DEFAULT 0,
+ Profit double(10,4) NOT NULL DEFAULT 0'
+ . $ebay_table_fields . '
+ )';
+ $this->Conn->Query($sql);
+ }
+
+ /**
+ * @inheritDoc
+ */
+ protected function fillTable(array $filters)
+ {
+ list(, $ebay_joins, $ebay_query_fields) = $this->getEbayConfig();
+
+ $date_range_filter = $this->getDateRangeFilter('o.OrderDate', $filters);
+
+ $sql = 'INSERT INTO ' . $this->tableName . '
+ SELECT
+ p.ProductId,
+ SUM(od.Quantity) as Qty,
+ SUM(od.Cost * od.Quantity) as Cost,
+ SUM(od.Price * od.Quantity) as SaleAmount,
+ SUM(o.VAT * od.Price * od.Quantity / o.SubTotal) as Tax,
+ SUM(o.ShippingCost * od.Price * od.Quantity / o.SubTotal) as Shipping,
+ SUM(o.ProcessingFee * od.Price * od.Quantity / o.SubTotal) as Processing,
+ SUM((od.Price - od.Cost) * od.Quantity) as Profit'
+ . $ebay_query_fields . '
+ FROM ' . TABLE_PREFIX . 'Orders AS o
+ LEFT JOIN ' . TABLE_PREFIX . 'OrderItems AS od
+ ON od.OrderId = o.OrderId
+ LEFT JOIN ' . TABLE_PREFIX . 'Products AS p
+ ON p.ProductId = od.ProductId
+ ' . $ebay_joins . '
+ WHERE
+ o.Status IN (' . ORDER_STATUS_PROCESSED . ',' . ORDER_STATUS_ARCHIVED . ')
+ ' . ($date_range_filter ? ' AND ' . $date_range_filter : '') . '
+ GROUP BY p.ProductId
+ HAVING NOT ISNULL(ProductId)';
+ $this->Conn->Query($sql);
+ }
+
+ /**
+ * @inheritDoc
+ */
+ protected function getTotal()
+ {
+ $sql = 'SELECT COUNT(*)
+ FROM ' . TABLE_PREFIX . 'Products';
+
+ return $this->Conn->GetOne($sql);
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function updateUnitConfig(array $report_options)
+ {
+ parent::updateUnitConfig($report_options);
+
+ /** @var array $list_sqls */
+ $list_sqls = $this->getUnitOption('ListSQLs');
+ $list_sqls[''] = ' SELECT %1$s.* %2$s
+ FROM %1$s
+ LEFT JOIN ' . TABLE_PREFIX . 'Products AS p ON p.ProductId = %1$s.ProductId';
+ $this->setUnitOption('ListSQLs', $list_sqls);
+
+ $lang = $this->Application->GetVar('m_lang');
+
+ /** @var array $calculated_fields */
+ $calculated_fields = $this->getUnitOption('CalculatedFields');
+
+ $calculated_fields[''] = array(
+ 'ProductName' => 'p.l' . $lang . '_Name',
+ );
+
+ $this->setUnitOption('CalculatedFields', $calculated_fields);
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function getExportFilename(array $report_options)
+ {
+ return 'SalesReport-ByProduct-' . date('d-M-Y') . '.csv';
+ }
+
+}
Index: modules/in-commerce/units/reports/SalesReportGenerator/ByUserSalesReportGenerator.php
===================================================================
--- /dev/null
+++ modules/in-commerce/units/reports/SalesReportGenerator/ByUserSalesReportGenerator.php
@@ -0,0 +1,199 @@
+<?php
+
+
+class ByUserSalesReportGenerator extends AbstractSalesReportGenerator
+{
+
+ /**
+ * @inheritDoc
+ */
+ public function __construct()
+ {
+ parent::__construct();
+
+ $this->reportType = SalesReportConstants::TYPE_BY_USER;
+ $this->idField = 'PortalUserId';
+ $this->exportIgnore = 'PortalUserId';
+ $this->grid = 'ByUser';
+ }
+
+ /**
+ * @inheritDoc
+ */
+ protected function createTable()
+ {
+ $sql = 'CREATE TABLE ' . $this->tableName . ' (
+ PortalUserId int(11) NOT NULL DEFAULT 0,
+ Qty int(11) NOT NULL DEFAULT 0,
+ Cost double(10,4) NOT NULL DEFAULT 0,
+ Amount double(10,4) NOT NULL DEFAULT 0,
+ Tax double(10,4) NOT NULL DEFAULT 0,
+ Shipping double(10,4) NOT NULL DEFAULT 0,
+ Processing double(10,4) NOT NULL DEFAULT 0,
+ Profit double(10,4) NOT NULL DEFAULT 0
+ )';
+ $this->Conn->Query($sql);
+ }
+
+ /**
+ * @inheritDoc
+ */
+ protected function fillTable(array $filters)
+ {
+ $date_range_filter = $this->getDateRangeFilter('o.OrderDate', $filters);
+
+ $sql = 'INSERT INTO ' . $this->tableName . '
+ SELECT
+ u.PortalUserId,
+ SUM(od.Quantity) as Qty,
+ SUM(od.Cost * od.Quantity) as Cost,
+ SUM(od.Price * od.Quantity) as SaleAmount,
+ SUM(o.VAT * od.Price * od.Quantity / o.SubTotal) as Tax,
+ SUM(o.ShippingCost * od.Price * od.Quantity / o.SubTotal) as Shipping,
+ SUM(o.ProcessingFee * od.Price * od.Quantity / o.SubTotal) as Processing,
+ SUM((od.Price - od.Cost) * od.Quantity) as Profit
+ FROM ' . TABLE_PREFIX . 'Orders AS o
+ LEFT JOIN ' . TABLE_PREFIX . 'OrderItems AS od
+ ON od.OrderId = o.OrderId
+ LEFT JOIN ' . TABLE_PREFIX . 'Users AS u
+ ON u.PortalUserId = o.PortalUserId
+ WHERE
+ o.Status IN (' . ORDER_STATUS_PROCESSED . ',' . ORDER_STATUS_ARCHIVED . ')
+ ' . ($date_range_filter ? ' AND ' . $date_range_filter : '') . '
+ GROUP BY u.PortalUserId
+ HAVING NOT ISNULL(PortalUserId)';
+ $this->Conn->Query($sql);
+ }
+
+ /**
+ * @inheritDoc
+ */
+ protected function getTotal()
+ {
+ $sql = 'SELECT COUNT(*)
+ FROM ' . TABLE_PREFIX . 'Categories';
+
+ return $this->Conn->GetOne($sql);
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function updateUnitConfig(array $report_options)
+ {
+ parent::updateUnitConfig($report_options);
+
+ /** @var array $list_sqls */
+ $list_sqls = $this->getUnitOption('ListSQLs');
+ $list_sqls[''] = ' SELECT %1$s.* %2$s
+ FROM %1$s
+ LEFT JOIN ' . TABLE_PREFIX . 'Users AS u ON u.PortalUserId = %1$s.PortalUserId';
+ $this->setUnitOption('ListSQLs', $list_sqls);
+
+ $new_fields = array(
+ 'PortalUserId' => array('type' => 'int', 'default' => null),
+ );
+
+ /** @var array $fields */
+ $fields = $this->getUnitOption('Fields');
+ $this->setUnitOption('Fields', $fields + $new_fields);
+
+ /** @var array $grids */
+ $grids = $this->getUnitOption('Grids');
+
+ $grids[$this->grid] = array(
+ 'Icons' => array(
+ 'default' => 'icon16_item.png',
+ 'module' => 'core',
+ ),
+ 'Fields' => array(
+ 'PortalUserId' => array(
+ 'title' => 'column:la_fld_Id',
+ 'filter_block' => 'grid_range_filter',
+ 'hidden' => 1,
+ ),
+ 'Login' => array(
+ 'filter_block' => 'grid_like_filter',
+ ),
+ 'FirstName' => array(
+ 'filter_block' => 'grid_like_filter',
+ ),
+ 'LastName' => array(
+ 'filter_block' => 'grid_like_filter',
+ ),
+ 'Qty' => array(
+ 'td_style' => 'text-align: center',
+ 'filter_block' => 'grid_range_filter',
+ 'total' => 'sum',
+ ),
+ 'Cost' => array(
+ 'td_style' => 'text-align: right',
+ 'filter_block' => 'grid_range_filter',
+ 'total' => 'sum',
+ ),
+ 'Amount' => array(
+ 'title' => 'la_col_GMV',
+ 'filter_block' => 'grid_range_filter',
+ 'td_style' => 'text-align: right',
+ 'total' => 'sum',
+ ),
+ 'Tax' => array(
+ 'title' => 'la_col_Tax',
+ 'filter_block' => 'grid_range_filter',
+ 'td_style' => 'text-align: right',
+ 'total' => 'sum',
+ ),
+ 'Shipping' => array(
+ 'title' => 'la_col_Shipping',
+ 'filter_block' => 'grid_range_filter',
+ 'td_style' => 'text-align: right',
+ 'total' => 'sum',
+ ),
+ 'Processing' => array(
+ 'title' => 'la_col_Processing',
+ 'filter_block' => 'grid_range_filter',
+ 'td_style' => 'text-align: right',
+ 'total' => 'sum',
+ ),
+ 'Profit' => array(
+ 'title' => 'la_col_Profit',
+ 'filter_block' => 'grid_range_filter',
+ 'td_style' => 'text-align: right',
+ 'total' => 'sum',
+ ),
+ ),
+ );
+
+ $this->setUnitOption('Grids', $grids);
+
+ $new_virtual_fields = array(
+ 'Login' => array('type' => 'string', 'default' => ''),
+ 'FirstName' => array('type' => 'string', 'default' => ''),
+ 'LastName' => array('type' => 'string', 'default' => ''),
+ );
+
+ /** @var array $virtual_fields */
+ $virtual_fields = $this->getUnitOption('VirtualFields');
+ $this->setUnitOption('VirtualFields', $virtual_fields + $new_virtual_fields);
+
+ /** @var array $calculated_fields */
+ $calculated_fields = $this->getUnitOption('CalculatedFields');
+
+ $calculated_fields[''] = array(
+ 'Login' => 'u.Username',
+ 'FirstName' => 'u.FirstName',
+ 'LastName' => 'u.LastName',
+ );
+
+ $this->setUnitOption('CalculatedFields', $calculated_fields);
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function getExportFilename(array $report_options)
+ {
+ return 'SalesReport-ByUser-' . date('d-M-Y') . '.csv';
+ }
+
+}
Index: modules/in-commerce/units/reports/SalesReportGenerator/OverallSalesReportGenerator.php
===================================================================
--- /dev/null
+++ modules/in-commerce/units/reports/SalesReportGenerator/OverallSalesReportGenerator.php
@@ -0,0 +1,230 @@
+<?php
+
+
+class OverallSalesReportGenerator extends AbstractSalesReportGenerator
+{
+
+ /**
+ * @inheritDoc
+ */
+ public function __construct()
+ {
+ parent::__construct();
+
+ $this->reportType = SalesReportConstants::TYPE_OVERALL;
+ $this->idField = 'Marketplace';
+ $this->grid = 'Overall';
+ }
+
+ /**
+ * @inheritDoc
+ */
+ protected function createTable()
+ {
+ $sql = 'CREATE TABLE ' . $this->tableName . ' (
+ Marketplace tinyint(1) NOT NULL DEFAULT 0,
+ Qty int(11) NOT NULL DEFAULT 0,
+ Cost double(10,4) NOT NULL DEFAULT 0,
+ Amount double(10,4) NOT NULL DEFAULT 0,
+ Tax double(10,4) NOT NULL DEFAULT 0,
+ Shipping double(10,4) NOT NULL DEFAULT 0,
+ Processing double(10,4) NOT NULL DEFAULT 0,
+ Profit double(10,4) NOT NULL DEFAULT 0
+ )';
+ $this->Conn->Query($sql);
+ }
+
+ /**
+ * @inheritDoc
+ */
+ protected function fillTable(array $filters)
+ {
+ $date_range_filter = $this->getDateRangeFilter('o.OrderDate', $filters);
+
+ if ( $this->Application->isModuleEnabled('in-auction') ) {
+ $sql = 'INSERT INTO ' . $this->tableName . '
+ SELECT
+ 1 AS Marketplace,
+ SUM(IF(ISNULL(eod.OptionsSalt), od.Quantity, 0)) as Qty,
+ SUM(IF(ISNULL(eod.OptionsSalt), od.Cost * od.Quantity, 0)) as Cost,
+ SUM(IF(ISNULL(eod.OptionsSalt), od.Price * od.Quantity, 0)) as SaleAmount,
+ SUM(IF(ISNULL(eod.OptionsSalt), o.VAT * od.Price * od.Quantity / o.SubTotal, 0)) as Tax,
+ SUM(
+ IF(ISNULL(eod.OptionsSalt), o.ShippingCost * od.Price * od.Quantity / o.SubTotal, 0)
+ ) as Shipping,
+ SUM(
+ IF(
+ ISNULL(eod.OptionsSalt),
+ o.ProcessingFee * od.Price * od.Quantity / o.SubTotal,
+ 0
+ )
+ ) as Processing,
+ SUM(IF(ISNULL(eod.OptionsSalt), (od.Price - od.Cost) * od.Quantity, 0)) as Profit
+ FROM ' . TABLE_PREFIX . 'Orders AS o
+ LEFT JOIN ' . TABLE_PREFIX . 'OrderItems AS od
+ ON od.OrderId = o.OrderId
+ LEFT JOIN ' . TABLE_PREFIX . 'eBayOrderItems AS eod
+ ON od.OptionsSalt = eod.OptionsSalt
+ WHERE
+ o.Status IN (' . ORDER_STATUS_PROCESSED . ',' . ORDER_STATUS_ARCHIVED . ')
+ ' . ($date_range_filter ? ' AND ' . $date_range_filter : '');
+ $this->Conn->Query($sql);
+
+ $sql = 'INSERT INTO ' . $this->tableName . '
+ SELECT
+ 2 AS Marketplace,
+ SUM(IF(ISNULL(eod.OptionsSalt), 0, od.Quantity)) as Qty,
+ SUM(IF(ISNULL(eod.OptionsSalt), 0, od.Cost * od.Quantity)) as Cost,
+ SUM(IF(ISNULL(eod.OptionsSalt), 0, od.Price * od.Quantity)) as SaleAmount,
+ SUM(IF(ISNULL(eod.OptionsSalt), 0, o.VAT * od.Price * od.Quantity / o.SubTotal)) as Tax,
+ SUM(
+ IF(ISNULL(eod.OptionsSalt), 0, o.ShippingCost * od.Price * od.Quantity / o.SubTotal)
+ ) as Shipping,
+ SUM(
+ IF(
+ ISNULL(eod.OptionsSalt),
+ 0,
+ o.ProcessingFee * od.Price * od.Quantity / o.SubTotal)
+ ) as Processing,
+ SUM(IF(ISNULL(eod.OptionsSalt), 0, (od.Price - od.Cost) * od.Quantity)) as Profit
+ FROM ' . TABLE_PREFIX . 'Orders AS o
+ LEFT JOIN ' . TABLE_PREFIX . 'OrderItems AS od
+ ON od.OrderId = o.OrderId
+ LEFT JOIN ' . TABLE_PREFIX . 'eBayOrderItems AS eod
+ ON od.OptionsSalt = eod.OptionsSalt
+ WHERE
+ o.Status IN (' . ORDER_STATUS_PROCESSED . ',' . ORDER_STATUS_ARCHIVED . ')
+ ' . ($date_range_filter ? ' AND ' . $date_range_filter : '');
+ $this->Conn->Query($sql);
+
+ return;
+ }
+
+ $sql = 'INSERT INTO ' . $this->tableName . '
+ SELECT
+ 1 AS Marketplace,
+ SUM(od.Quantity) as Qty,
+ SUM(od.Cost * od.Quantity) as Cost,
+ SUM(od.Price * od.Quantity) as SaleAmount,
+ SUM(o.VAT * od.Price * od.Quantity / o.SubTotal) as Tax,
+ SUM(o.ShippingCost * od.Price * od.Quantity / o.SubTotal) as Shipping,
+ SUM(o.ProcessingFee * od.Price * od.Quantity / o.SubTotal) as Processing,
+ SUM((od.Price - od.Cost) * od.Quantity) as Profit
+ FROM ' . TABLE_PREFIX . 'Orders AS o
+ LEFT JOIN ' . TABLE_PREFIX . 'OrderItems AS od
+ ON od.OrderId = o.OrderId
+ WHERE
+ o.Status IN (' . ORDER_STATUS_PROCESSED . ',' . ORDER_STATUS_ARCHIVED . ')
+ ' . ($date_range_filter ? ' AND ' . $date_range_filter : '');
+ $this->Conn->Query($sql);
+ }
+
+ /**
+ * @inheritDoc
+ */
+ protected function getTotal()
+ {
+ return $this->Application->isModuleEnabled('in-auction') ? 2 : 1;
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function updateUnitConfig(array $report_options)
+ {
+ parent::updateUnitConfig($report_options);
+
+ /** @var array $list_sqls */
+ $list_sqls = $this->getUnitOption('ListSQLs');
+ $list_sqls[''] = ' SELECT %1$s.* %2$s
+ FROM %1$s';
+ $this->setUnitOption('ListSQLs', $list_sqls);
+
+ $new_fields = array(
+ 'Marketplace' => array(
+ 'type' => 'int',
+ 'formatter' => 'kOptionsFormatter',
+ 'options' => array(1 => 'la_OnlineStore', 2 => 'la_eBayMarketplace'), 'use_phrases' => 1,
+ 'default' => 1,
+ ),
+ );
+
+ /** @var array $fields */
+ $fields = $this->getUnitOption('Fields');
+ $this->setUnitOption('Fields', $fields + $new_fields);
+
+ /** @var array $grids */
+ $grids = $this->getUnitOption('Grids');
+
+ $grids[$this->grid] = array(
+ 'Icons' => array(
+ 'default' => 'icon16_item.png',
+ 'module' => 'core',
+ ),
+ 'Fields' => array(
+ 'Marketplace' => array(
+ 'title' => 'la_col_Marketplace',
+ 'filter_block' => 'grid_options_filter',
+ ),
+ 'Qty' => array(
+ 'td_style' => 'text-align: center',
+ 'filter_block' => 'grid_range_filter',
+ 'total' => 'sum',
+ ),
+ 'Cost' => array(
+ 'td_style' => 'text-align: right',
+ 'filter_block' => 'grid_range_filter',
+ 'total' => 'sum',
+ ),
+ 'Amount' => array(
+ 'title' => 'la_col_GMV',
+ 'filter_block' => 'grid_range_filter',
+ 'td_style' => 'text-align: right',
+ 'total' => 'sum',
+ ),
+ 'Tax' => array(
+ 'title' => 'la_col_Tax',
+ 'filter_block' => 'grid_range_filter',
+ 'td_style' => 'text-align: right',
+ 'total' => 'sum',
+ ),
+ 'Shipping' => array(
+ 'title' => 'la_col_Shipping',
+ 'filter_block' => 'grid_range_filter',
+ 'td_style' => 'text-align: right',
+ 'total' => 'sum',
+ ),
+ 'Processing' => array(
+ 'title' => 'la_col_Processing',
+ 'filter_block' => 'grid_range_filter',
+ 'td_style' => 'text-align: right',
+ 'total' => 'sum',
+ ),
+ 'Profit' => array(
+ 'title' => 'la_col_Profit',
+ 'filter_block' => 'grid_range_filter',
+ 'td_style' => 'text-align: right',
+ 'total' => 'sum',
+ ),
+ ),
+ );
+
+ $this->setUnitOption('Grids', $grids);
+
+ /** @var array $virtual_fields */
+ $virtual_fields = $this->getUnitOption('VirtualFields');
+
+ $new_virtual_fields = array(
+ 'Metric' => array(
+ 'type' => 'int',
+ 'formatter' => 'kOptionsFormatter',
+ 'options' => $this->getMetricOptions($grids, array('Marketplace')),
+ 'use_phrases' => 1,
+ 'default' => 0,
+ ),
+ );
+
+ $this->setUnitOption('VirtualFields', $virtual_fields + $new_virtual_fields);
+ }
+
+}
Index: modules/in-commerce/units/reports/SalesReportGenerator/SalesReportGeneratorManager.php
===================================================================
--- /dev/null
+++ modules/in-commerce/units/reports/SalesReportGenerator/SalesReportGeneratorManager.php
@@ -0,0 +1,39 @@
+<?php
+
+
+class SalesReportGeneratorManager extends kBase
+{
+
+ /**
+ * Mapping between report types and report generator classes.
+ *
+ * @var array
+ */
+ protected $mapping = array(
+ SalesReportConstants::TYPE_OVERALL => OverallSalesReportGenerator::class,
+ SalesReportConstants::TYPE_BY_CATEGORY => ByCategorySalesReportGenerator::class,
+ SalesReportConstants::TYPE_BY_USER => ByUserSalesReportGenerator::class,
+ SalesReportConstants::TYPE_BY_PRODUCT => ByProductSalesReportGenerator::class,
+ );
+
+ /**
+ * Returns report by type.
+ *
+ * @param integer $report_type Report type.
+ *
+ * @return AbstractSalesReportGenerator
+ * @throws InvalidArgumentException When an unknown report type is given.
+ */
+ public function get($report_type)
+ {
+ if ( !array_key_exists($report_type, $this->mapping) ) {
+ throw new InvalidArgumentException('Invalid report type.');
+ }
+
+ /** @var AbstractSalesReportGenerator $sales_report_generator */
+ $sales_report_generator = $this->Application->recallObject($this->mapping[$report_type]);
+
+ return $sales_report_generator;
+ }
+
+}
Index: modules/in-commerce/units/reports/reports_config.php
===================================================================
--- modules/in-commerce/units/reports/reports_config.php
+++ modules/in-commerce/units/reports/reports_config.php
@@ -19,6 +19,17 @@
'ListClass' => Array ('class' => 'kDBList', 'file' => '', 'build_event' => 'OnListBuild'),
'EventHandlerClass' => Array ('class' => 'ReportsEventHandler', 'file' => 'reports_event_handler.php', 'build_event' => 'OnBuild'),
'TagProcessorClass' => Array ('class' => 'ReportsTagProcessor', 'file' => 'reports_tag_processor.php', 'build_event' => 'OnBuild'),
+
+ 'RegisterClasses' => Array (
+ array('pseudo' => 'SalesReportGeneratorManager', 'class' => 'SalesReportGeneratorManager', 'file' => 'SalesReportGenerator/SalesReportGeneratorManager.php', 'build_event' => ''),
+ array('pseudo' => 'AbstractSalesReportGeneratorGenerator', 'class' => 'AbstractSalesReportGenerator', 'file' => 'SalesReportGenerator/AbstractSalesReportGenerator.php', 'build_event' => ''),
+ array('pseudo' => 'AbstractProductCategorySalesReportGenerator', 'class' => 'AbstractProductCategorySalesReportGenerator', 'file' => 'SalesReportGenerator/AbstractProductCategorySalesReportGenerator.php', 'build_event' => ''),
+ array('pseudo' => 'OverallSalesReportGenerator', 'class' => 'OverallSalesReportGenerator', 'file' => 'SalesReportGenerator/OverallSalesReportGenerator.php', 'build_event' => '', 'require_classes' => 'AbstractSalesReportGenerator'),
+ array('pseudo' => 'ByCategorySalesReportGenerator', 'class' => 'ByCategorySalesReportGenerator', 'file' => 'SalesReportGenerator/ByCategorySalesReportGenerator.php', 'build_event' => '', 'require_classes' => 'AbstractProductCategorySalesReportGenerator'),
+ array('pseudo' => 'ByUserSalesReportGenerator', 'class' => 'ByUserSalesReportGenerator', 'file' => 'SalesReportGenerator/ByUserSalesReportGenerator.php', 'build_event' => '', 'require_classes' => 'AbstractSalesReportGenerator'),
+ array('pseudo' => 'ByProductSalesReportGenerator', 'class' => 'ByProductSalesReportGenerator', 'file' => 'SalesReportGenerator/ByProductSalesReportGenerator.php', 'build_event' => '', 'require_classes' => 'AbstractProductCategorySalesReportGenerator'),
+ ),
+
'AutoLoad' => true,
'QueryString' => Array (
@@ -29,19 +40,6 @@
5 => 'mode',
),
- 'Hooks' => Array (
- Array (
- 'Mode' => hBEFORE,
- 'Conditional' => false,
- 'HookToPrefix' => 'rep',
- 'HookToSpecial' => '*',
- 'HookToEvent' => Array ('OnAfterConfigRead'),
- 'DoPrefix' => '',
- 'DoSpecial' => '',
- 'DoEvent' => 'OnUpdateConfig',
- ),
- ),
-
'IDField' => '__DYNAMIC__',
'TitlePresets' => Array (
Index: modules/in-commerce/units/reports/reports_event_handler.php
===================================================================
--- modules/in-commerce/units/reports/reports_event_handler.php
+++ modules/in-commerce/units/reports/reports_event_handler.php
@@ -60,7 +60,6 @@
$field_values['table_name'] = $table_name;
$this->Conn->Query('DROP TABLE IF EXISTS '.$table_name);
- $filter_value = '';
$from = $object->GetDBField('FromDateTime');
$to = $object->GetDBField('ToDateTime');
@@ -72,13 +71,10 @@
$from = $to - $day_seconds;
}
- if ($from && $to) {
- $filter_value = 'AND o.OrderDate >= '.$from.' AND o.OrderDate <= '.$to;
- }
-
- $ebay_table_fields = '';
- $ebay_joins = '';
- $ebay_query_fields = '';
+ $filters = array(
+ 'FromDateTime' => $from,
+ 'ToDateTime' => $to,
+ );
$user_id = $this->Application->RecallVar('user_id');
$sql = 'DELETE FROM '.TABLE_PREFIX.'UserPersistentSessionData
@@ -87,245 +83,11 @@
AND VariableName LIKE \'rep_columns_%\'';
$this->Conn->Query($sql);
- if ($this->Application->isModuleEnabled('in-auction'))
- {
- if (in_array($field_values['ReportType'], Array(1,5))) // not overall.
- {
- $ebay_table_fields = ',
- StoreQty int(11) NOT NULL DEFAULT 0,
- eBayQty int(11) NOT NULL DEFAULT 0,
- StoreAmount double(10,4) NOT NULL DEFAULT 0,
- eBayAmount double(10,4) NOT NULL DEFAULT 0,
- StoreProfit double(10,4) NOT NULL DEFAULT 0,
- eBayProfit double(10,4) NOT NULL DEFAULT 0';
-
- $ebay_joins = '
- LEFT JOIN '.TABLE_PREFIX.'eBayOrderItems AS eod
- ON od.OptionsSalt = eod.OptionsSalt
- ';
-
- $ebay_query_fields = ',
- SUM(IF(ISNULL(eod.OptionsSalt), od.Quantity, 0)) as StoreQty,
- SUM(IF(ISNULL(eod.OptionsSalt), 0, od.Quantity)) as eBayQty,
- SUM(IF(ISNULL(eod.OptionsSalt), od.Price * od.Quantity, 0)) as StoreAmount,
- SUM(IF(ISNULL(eod.OptionsSalt), 0, od.Price * od.Quantity)) as eBayAmount,
- SUM(IF(ISNULL(eod.OptionsSalt), (od.Price - od.Cost) * od.Quantity, 0)) as StoreProfit,
- SUM(IF(ISNULL(eod.OptionsSalt), 0, (od.Price - od.Cost) * od.Quantity)) as eBayProfit
- ';
- }
-
- }
-
- if ($field_values['ReportType'] == 1) { // by Category
- $field_values['id_field'] = $field_values['export_ignore'] = 'CategoryId';
- $field_values['grid'] = 'ByCategory';
- $q = 'CREATE TABLE '.$table_name.' (
- CategoryId int(11) NOT NULL DEFAULT 0,
- Qty int(11) NOT NULL DEFAULT 0,
- Cost double(10,4) NOT NULL DEFAULT 0,
- Amount double(10,4) NOT NULL DEFAULT 0,
- Tax double(10,4) NOT NULL DEFAULT 0,
- Shipping double(10,4) NOT NULL DEFAULT 0,
- Processing double(10,4) NOT NULL DEFAULT 0,
- Profit double(10,4) NOT NULL DEFAULT 0
- '.$ebay_table_fields.'
- )';
- $field_values['total'] = $this->Conn->GetOne('SELECT COUNT(*) FROM '.TABLE_PREFIX.'Categories');
- $this->Conn->Query($q);
-
- $q = 'INSERT INTO '.$field_values['table_name'].'
- SELECT
- c.CategoryId,
- SUM(od.Quantity) as Qty,
- SUM(od.Cost * od.Quantity) as Cost,
- SUM(od.Price * od.Quantity) as SaleAmount,
- SUM(o.VAT * od.Price * od.Quantity / o.SubTotal) as Tax,
- SUM(o.ShippingCost * od.Price * od.Quantity / o.SubTotal) as Shipping,
- SUM(o.ProcessingFee * od.Price * od.Quantity / o.SubTotal) as Processing,
- SUM((od.Price - od.Cost) * od.Quantity) as Profit'
- .$ebay_query_fields.'
- FROM '.TABLE_PREFIX.'Orders AS o
- LEFT JOIN '.TABLE_PREFIX.'OrderItems AS od
- ON od.OrderId = o.OrderId
- LEFT JOIN '.TABLE_PREFIX.'Products AS p
- ON p.ProductId = od.ProductId
- LEFT JOIN '.TABLE_PREFIX.'CategoryItems AS ci
- ON ci.ItemResourceId = p.ResourceId
- LEFT JOIN '.TABLE_PREFIX.'Categories AS c
- ON c.CategoryId = ci.CategoryId
- '.$ebay_joins.'
- WHERE
- o.Status IN (4,6)
- AND
- ci.PrimaryCat = 1
- '.$filter_value.'
- GROUP BY c.CategoryId
- HAVING NOT ISNULL(CategoryId)
- ';
- $this->Conn->Query($q);
- }
- elseif ($field_values['ReportType'] == 2) { // by User
- $field_values['id_field'] = $field_values['export_ignore'] = 'PortalUserId';
- $field_values['grid'] = 'ByUser';
- $q = 'CREATE TABLE '.$table_name.' (
- PortalUserId int(11) NOT NULL DEFAULT 0,
- Qty int(11) NOT NULL DEFAULT 0,
- Cost double(10,4) NOT NULL DEFAULT 0,
- Amount double(10,4) NOT NULL DEFAULT 0,
- Tax double(10,4) NOT NULL DEFAULT 0,
- Shipping double(10,4) NOT NULL DEFAULT 0,
- Processing double(10,4) NOT NULL DEFAULT 0,
- Profit double(10,4) NOT NULL DEFAULT 0
- )';
- $field_values['total'] = $this->Conn->GetOne('SELECT COUNT(*) FROM '.TABLE_PREFIX.'Categories');
- $this->Conn->Query($q);
-
- $q = 'INSERT INTO '.$field_values['table_name'].'
- SELECT
- u.PortalUserId,
- SUM(od.Quantity) as Qty,
- SUM(od.Cost * od.Quantity) as Cost,
- SUM(od.Price * od.Quantity) as SaleAmount,
- SUM(o.VAT * od.Price * od.Quantity / o.SubTotal) as Tax,
- SUM(o.ShippingCost * od.Price * od.Quantity / o.SubTotal) as Shipping,
- SUM(o.ProcessingFee * od.Price * od.Quantity / o.SubTotal) as Processing,
- SUM((od.Price - od.Cost) * od.Quantity) as Profit
- FROM '.TABLE_PREFIX.'Orders AS o
- LEFT JOIN '.TABLE_PREFIX.'OrderItems AS od
- ON od.OrderId = o.OrderId
- LEFT JOIN '.TABLE_PREFIX.'Users AS u
- ON u.PortalUserId = o.PortalUserId
- WHERE
- o.Status IN (4,6)
- '.$filter_value.'
- GROUP BY u.PortalUserId
- HAVING NOT ISNULL(PortalUserId)
- ';
- $this->Conn->Query($q);
- }
- elseif ($field_values['ReportType'] == 5) { // by Product
- $field_values['id_field'] = $field_values['export_ignore'] = 'ProductId';
- $field_values['grid'] = 'ByProduct';
- $q = 'CREATE TABLE '.$table_name.' (
- ProductId int(11) NOT NULL DEFAULT 0,
- Qty int(11) NOT NULL DEFAULT 0,
- Cost double(10,4) NOT NULL DEFAULT 0,
- Amount double(10,4) NOT NULL DEFAULT 0,
- Tax double(10,4) NOT NULL DEFAULT 0,
- Shipping double(10,4) NOT NULL DEFAULT 0,
- Processing double(10,4) NOT NULL DEFAULT 0,
- Profit double(10,4) NOT NULL DEFAULT 0'
- .$ebay_table_fields.'
- )';
- $field_values['total'] = $this->Conn->GetOne('SELECT COUNT(*) FROM '.TABLE_PREFIX.'Products');
- $this->Conn->Query($q);
-
- $q = 'INSERT INTO '.$field_values['table_name'].'
- SELECT
- p.ProductId,
- SUM(od.Quantity) as Qty,
- SUM(od.Cost * od.Quantity) as Cost,
- SUM(od.Price * od.Quantity) as SaleAmount,
- SUM(o.VAT * od.Price * od.Quantity / o.SubTotal) as Tax,
- SUM(o.ShippingCost * od.Price * od.Quantity / o.SubTotal) as Shipping,
- SUM(o.ProcessingFee * od.Price * od.Quantity / o.SubTotal) as Processing,
- SUM((od.Price - od.Cost) * od.Quantity) as Profit'
- .$ebay_query_fields.'
- FROM '.TABLE_PREFIX.'Orders AS o
- LEFT JOIN '.TABLE_PREFIX.'OrderItems AS od
- ON od.OrderId = o.OrderId
- LEFT JOIN '.TABLE_PREFIX.'Products AS p
- ON p.ProductId = od.ProductId
- '.$ebay_joins.'
- WHERE
- o.Status IN (4,6)
- '.$filter_value.'
- GROUP BY p.ProductId
- HAVING NOT ISNULL(ProductId)
- ';
- $this->Conn->Query($q);
- }
- elseif ($field_values['ReportType'] == 12) { // Overall
- $field_values['id_field'] = 'Marketplace';
- $field_values['grid'] = 'Overall';
- $q = 'CREATE TABLE '.$table_name.' (
- Marketplace tinyint(1) NOT NULL DEFAULT 0,
- Qty int(11) NOT NULL DEFAULT 0,
- Cost double(10,4) NOT NULL DEFAULT 0,
- Amount double(10,4) NOT NULL DEFAULT 0,
- Tax double(10,4) NOT NULL DEFAULT 0,
- Shipping double(10,4) NOT NULL DEFAULT 0,
- Processing double(10,4) NOT NULL DEFAULT 0,
- Profit double(10,4) NOT NULL DEFAULT 0
- )';
- $this->Conn->Query($q);
-
- if ($this->Application->isModuleEnabled('in-auction'))
- {
- $field_values['total'] = 2;
+ /** @var SalesReportGeneratorManager $sales_report_generator_manager */
+ $sales_report_generator_manager = $this->Application->recallObject('SalesReportGeneratorManager');
- $q = 'INSERT INTO '.$field_values['table_name'].'
- SELECT
- 1 AS Marketplace,
- SUM(IF(ISNULL(eod.OptionsSalt), od.Quantity, 0)) as Qty,
- SUM(IF(ISNULL(eod.OptionsSalt), od.Cost * od.Quantity, 0)) as Cost,
- SUM(IF(ISNULL(eod.OptionsSalt), od.Price * od.Quantity, 0)) as SaleAmount,
- SUM(IF(ISNULL(eod.OptionsSalt), o.VAT * od.Price * od.Quantity / o.SubTotal, 0)) as Tax,
- SUM(IF(ISNULL(eod.OptionsSalt), o.ShippingCost * od.Price * od.Quantity / o.SubTotal, 0)) as Shipping,
- SUM(IF(ISNULL(eod.OptionsSalt), o.ProcessingFee * od.Price * od.Quantity / o.SubTotal, 0)) as Processing,
- SUM(IF(ISNULL(eod.OptionsSalt), (od.Price - od.Cost) * od.Quantity, 0)) as Profit
- FROM '.TABLE_PREFIX.'Orders AS o
- LEFT JOIN '.TABLE_PREFIX.'OrderItems AS od
- ON od.OrderId = o.OrderId
- LEFT JOIN '.TABLE_PREFIX.'eBayOrderItems AS eod
- ON od.OptionsSalt = eod.OptionsSalt
- WHERE
- o.Status IN (4,6)
- '.$filter_value;
- $this->Conn->Query($q);
-
-
- $q = 'INSERT INTO '.$field_values['table_name'].'
- SELECT
- 2 AS Marketplace,
- SUM(IF(ISNULL(eod.OptionsSalt), 0, od.Quantity)) as Qty,
- SUM(IF(ISNULL(eod.OptionsSalt), 0, od.Cost * od.Quantity)) as Cost,
- SUM(IF(ISNULL(eod.OptionsSalt), 0, od.Price * od.Quantity)) as SaleAmount,
- SUM(IF(ISNULL(eod.OptionsSalt), 0, o.VAT * od.Price * od.Quantity / o.SubTotal)) as Tax,
- SUM(IF(ISNULL(eod.OptionsSalt), 0, o.ShippingCost * od.Price * od.Quantity / o.SubTotal)) as Shipping,
- SUM(IF(ISNULL(eod.OptionsSalt), 0, o.ProcessingFee * od.Price * od.Quantity / o.SubTotal)) as Processing,
- SUM(IF(ISNULL(eod.OptionsSalt), 0, (od.Price - od.Cost) * od.Quantity)) as Profit
- FROM '.TABLE_PREFIX.'Orders AS o
- LEFT JOIN '.TABLE_PREFIX.'OrderItems AS od
- ON od.OrderId = o.OrderId
- LEFT JOIN '.TABLE_PREFIX.'eBayOrderItems AS eod
- ON od.OptionsSalt = eod.OptionsSalt
- WHERE
- o.Status IN (4,6)
- '.$filter_value;
- $this->Conn->Query($q);
- } else {
- $field_values['total'] = 1;
- $q = 'INSERT INTO '.$field_values['table_name'].'
- SELECT
- 1 AS Marketplace,
- SUM(od.Quantity) as Qty,
- SUM(od.Cost * od.Quantity) as Cost,
- SUM(od.Price * od.Quantity) as SaleAmount,
- SUM(o.VAT * od.Price * od.Quantity / o.SubTotal) as Tax,
- SUM(o.ShippingCost * od.Price * od.Quantity / o.SubTotal) as Shipping,
- SUM(o.ProcessingFee * od.Price * od.Quantity / o.SubTotal) as Processing,
- SUM((od.Price - od.Cost) * od.Quantity) as Profit
- FROM '.TABLE_PREFIX.'Orders AS o
- LEFT JOIN '.TABLE_PREFIX.'OrderItems AS od
- ON od.OrderId = o.OrderId
- WHERE
- o.Status IN (4,6)
- '.$filter_value;
- $this->Conn->Query($q);
-
- }
- }
+ $sales_report_generator = $sales_report_generator_manager->get($field_values['ReportType']);
+ $sales_report_generator->bind($field_values)->prepareData($table_name, $filters);
$event->CallSubEvent('OnResetSorting');
@@ -338,270 +100,6 @@
$this->Application->StoreVar('report_options', serialize($field_values));
}
- function OnUpdateConfig($event)
- {
- $report = $this->Application->RecallVar('report_options');
- if (!$report) {
- return ;
- }
-
- $field_values = unserialize($report);
- $grid = $field_values['grid'];
-
- $rep_options = $this->Application->getUnitOptions('rep');
- $new_options = Array ();
-
- $new_options['TableName'] = $field_values['table_name'];
-
- $new_options['Fields'] = Array (
- 'Qty' => Array ('type' => 'float', 'formatter' => 'kFormatter', 'format' => '%d', 'default' => 0, 'totals' => 'sum'),
- 'Cost' => Array ('type' => 'float', 'formatter' => 'kFormatter', 'format' => '%.2f', 'default' => 0, 'totals' => 'sum'),
- 'Amount' => Array ('type' => 'float', 'formatter' => 'kFormatter', 'format' => '%.2f', 'default' => 0, 'totals' => 'sum'),
- 'Tax' => Array ('type' => 'float', 'formatter' => 'kFormatter', 'format' => '%.2f', 'default' => 0, 'totals' => 'sum'),
- 'Shipping' => Array ('type' => 'float', 'formatter' => 'kFormatter', 'format' => '%.2f', 'default' => 0, 'totals' => 'sum'),
- 'Processing' => Array ('type' => 'float', 'formatter' => 'kFormatter', 'format' => '%.2f', 'default' => 0, 'totals' => 'sum'),
- 'Profit' => Array ('type' => 'float', 'formatter' => 'kFormatter', 'format' => '%.2f', 'default' => 0, 'totals' => 'sum'),
- );
-
- if ( $this->Application->isModuleEnabled('in-auction') ) {
- if ( in_Array ($field_values['ReportType'], Array (1, 5)) ) {
- $new_options['Fields'] += Array (
- 'StoreQty' => Array ('type' => 'int', 'formatter' => 'kFormatter', 'format' => '%d', 'default' => 0, 'totals' => 'sum'),
- 'StoreAmount' => Array ('type' => 'float', 'formatter' => 'kFormatter', 'format' => '%.2f', 'default' => 0, 'totals' => 'sum'),
- 'StoreProfit' => Array ('type' => 'float', 'formatter' => 'kFormatter', 'format' => '%.2f', 'default' => 0, 'totals' => 'sum'),
- 'eBayQty' => Array ('type' => 'int', 'formatter' => 'kFormatter', 'format' => '%d', 'default' => 0, 'totals' => 'sum'),
- 'eBayAmount' => Array ('type' => 'float', 'formatter' => 'kFormatter', 'format' => '%.2f', 'default' => 0, 'totals' => 'sum'),
- 'eBayProfit' => Array ('type' => 'float', 'formatter' => 'kFormatter', 'format' => '%.2f', 'default' => 0, 'totals' => 'sum'),
- );
- }
- }
-
- if ($field_values['ReportType'] == 1) { // by Category
-
- $new_options['ListSQLs'][''] =
- 'SELECT %1$s.* %2$s FROM %1$s
- LEFT JOIN '.TABLE_PREFIX.'Categories AS c
- ON c.CategoryId = %1$s.CategoryId';
- $new_options['Fields']['CategoryId'] = Array ('type' => 'int', 'default' => null);
- $new_options['Grids'][$grid] = Array (
- 'Icons' => Array (
- 'default' => 'icon16_item.png',
- 'module' => 'core',
- ),
- 'Fields' => Array (
- 'CategoryId' => Array ('title' => 'column:la_fld_Id', 'filter_block' => 'grid_range_filter', 'hidden' => 1),
- 'CategoryName' => Array ('title' => 'la_col_CategoryName', 'filter_block' => 'grid_like_filter'),
- 'Qty' => Array ('td_style' => 'text-align: center', 'total' => 'sum', 'filter_block' => 'grid_range_filter'),
- 'StoreQty' => Array ('title' => 'la_col_StoreQty', 'td_style' => 'text-align: center', 'total' => 'sum', 'filter_block' => 'grid_range_filter'),
- 'eBayQty' => Array ('title' => 'la_col_eBayQty', 'td_style' => 'text-align: center', 'total' => 'sum', 'filter_block' => 'grid_range_filter'),
- 'Cost' => Array ('td_style' => 'text-align: right', 'total' => 'sum', 'hidden' => 1, 'filter_block' => 'grid_range_filter'),
- 'Amount' => Array ('title' => 'la_col_GMV', 'td_style' => 'text-align: right', 'total' => 'sum', 'filter_block' => 'grid_range_filter'),
- 'StoreAmount' => Array ('title' => 'la_col_StoreGMV', 'td_style' => 'text-align: right', 'total' => 'sum', 'filter_block' => 'grid_range_filter'),
- 'eBayAmount' => Array ('title' => 'la_col_eBayGMV', 'td_style' => 'text-align: right', 'total' => 'sum', 'filter_block' => 'grid_range_filter'),
- 'Tax' => Array ('title' => 'la_col_Tax', 'td_style' => 'text-align: right', 'total' => 'sum', 'hidden' => 1, 'filter_block' => 'grid_range_filter'),
- 'Shipping' => Array ('title' => 'la_col_Shipping', 'td_style' => 'text-align: right', 'total' => 'sum', 'hidden' => 1, 'filter_block' => 'grid_range_filter'),
- 'Processing' => Array ('title' => 'la_col_Processing', 'td_style' => 'text-align: right', 'total' => 'sum', 'hidden' => 1, 'filter_block' => 'grid_range_filter'),
- 'Profit' => Array ('title' => 'la_col_Profit', 'td_style' => 'text-align: right', 'total' => 'sum', 'filter_block' => 'grid_range_filter'),
- 'StoreProfit' => Array ('title' => 'la_col_StoreProfit', 'td_style' => 'text-align: right', 'total' => 'sum', 'filter_block' => 'grid_range_filter'),
- 'eBayProfit' => Array ('title' => 'la_col_eBayProfit', 'td_style' => 'text-align: right', 'total' => 'sum', 'filter_block' => 'grid_range_filter'),
- ),
-
- );
-
- if (!$this->Application->isModuleEnabled('in-auction')) {
- $a_fields =& $new_options['Grids'][$grid]['Fields'];
- unset($a_fields['StoreQty']);
- unset($a_fields['eBayQty']);
- unset($a_fields['StoreAmount']);
- unset($a_fields['eBayAmount']);
- unset($a_fields['StoreProfit']);
- unset($a_fields['eBayProfit']);
- }
-
- $new_options['VirtualFields'] = array_merge($rep_options['VirtualFields'], Array (
- 'CategoryName' => Array ('type' => 'string', 'default' => ''),
- 'Metric' => Array (
- 'type' => 'int',
- 'formatter' => 'kOptionsFormatter',
- 'options' => $this->GetMetricOptions($new_options, array('CategoryId', 'CategoryName'), $grid),
- 'use_phrases' => 1,
- 'default' => 0,
- ),
- ));
-
- $lang = $this->Application->GetVar('m_lang');
-
- // products root category
- $products_category_id = $this->Application->findModule('Name', 'In-Commerce', 'RootCat');
- // get root category name
- $sql = 'SELECT LENGTH(l' . $lang . '_CachedNavbar)
- FROM ' . TABLE_PREFIX . 'Categories
- WHERE CategoryId = '.$products_category_id;
- $root_length = $this->Conn->GetOne($sql) + 4;
-
- $new_options['CalculatedFields'][''] = array(
- 'CategoryName' => 'REPLACE(SUBSTR(c.l'.$lang.'_CachedNavbar, '.$root_length.'), "&|&", " > ")',
- );
- }
- elseif ($field_values['ReportType'] == 2) { // by User
- $new_options['ListSQLs'][''] =
- 'SELECT %1$s.* %2$s FROM %1$s
- LEFT JOIN '.TABLE_PREFIX.'Users AS u
- ON u.PortalUserId = %1$s.PortalUserId';
- $new_options['Fields']['PortalUserId'] = Array ('type' => 'int', 'default' => null);
- $new_options['Grids'][$grid] = Array (
- 'Icons' => Array (
- 'default' => 'icon16_item.png',
- 'module' => 'core',
- ),
- 'Fields' => Array (
- 'PortalUserId' => Array ('title' => 'column:la_fld_Id', 'filter_block' => 'grid_range_filter', 'hidden' => 1),
- 'Login' => Array ('filter_block' => 'grid_like_filter'),
- 'FirstName' => Array ('filter_block' => 'grid_like_filter'),
- 'LastName' => Array ('filter_block' => 'grid_like_filter'),
- 'Qty' => Array ('td_style' => 'text-align: center', 'total' => 'sum', 'filter_block' => 'grid_range_filter'),
- 'Cost' => Array ('td_style' => 'text-align: right', 'total' => 'sum', 'filter_block' => 'grid_range_filter'),
- 'Amount' => Array ('title' => 'la_col_GMV', 'td_style' => 'text-align: right', 'total' => 'sum', 'filter_block' => 'grid_range_filter'),
- 'Tax' => Array ('title' => 'la_col_Tax', 'td_style' => 'text-align: right', 'total' => 'sum', 'filter_block' => 'grid_range_filter'),
- 'Shipping' => Array ('title' => 'la_col_Shipping', 'td_style' => 'text-align: right', 'total' => 'sum', 'filter_block' => 'grid_range_filter'),
- 'Processing' => Array ('title' => 'la_col_Processing', 'td_style' => 'text-align: right', 'total' => 'sum', 'filter_block' => 'grid_range_filter'),
- 'Profit' => Array ('title' => 'la_col_Profit', 'td_style' => 'text-align: right', 'total' => 'sum', 'filter_block' => 'grid_range_filter'),
- ),
- );
-
- $new_options['VirtualFields'] = array_merge($rep_options['VirtualFields'], Array (
- 'Login' => Array ('type' => 'string', 'default' => ''),
- 'FirstName' => Array ('type' => 'string', 'default' => ''),
- 'LastName' => Array ('type' => 'string', 'default' => ''),
- ));
-
- $new_options['CalculatedFields'][''] = Array (
- 'Login' => 'u.Username',
- 'FirstName' => 'u.FirstName',
- 'LastName' => 'u.LastName',
- );
- }
- elseif ($field_values['ReportType'] == 5) { // by Product
-
- $new_options['ListSQLs'][''] =
- 'SELECT %1$s.* %2$s FROM %1$s
- LEFT JOIN '.TABLE_PREFIX.'Products AS p
- ON p.ProductId = %1$s.ProductId';
- $new_options['Fields']['ProductId'] = Array ('type' => 'int', 'default' => null);
- $new_options['Grids'][$grid] = Array (
- 'Icons' => Array (
- 'default' => 'icon16_item.png',
- 'module' => 'core',
- ),
- 'Fields' => Array (
- 'ProductId' => Array ('title' => 'column:la_fld_Id', 'filter_block' => 'grid_range_filter', 'hidden' => 1),
- 'ProductName' => Array ('title' => 'la_col_ProductName', 'filter_block' => 'grid_like_filter'),
- 'Qty' => Array ('td_style' => 'text-align: center', 'total' => 'sum', 'filter_block' => 'grid_range_filter'),
- 'StoreQty' => Array ('title' => 'la_col_StoreQty', 'td_style' => 'text-align: center', 'total' => 'sum', 'filter_block' => 'grid_range_filter'),
- 'eBayQty' => Array ('title' => 'la_col_eBayQty', 'td_style' => 'text-align: center', 'total' => 'sum', 'filter_block' => 'grid_range_filter'),
- 'Cost' => Array ('td_style' => 'text-align: right', 'total' => 'sum', 'hidden' => 1, 'filter_block' => 'grid_range_filter'),
- 'Amount' => Array ('title' => 'la_col_GMV', 'td_style' => 'text-align: right', 'total' => 'sum', 'filter_block' => 'grid_range_filter'),
- 'StoreAmount' => Array ('title' => 'la_col_StoreGMV', 'td_style' => 'text-align: right', 'total' => 'sum', 'filter_block' => 'grid_range_filter'),
- 'eBayAmount' => Array ('title' => 'la_col_eBayGMV', 'td_style' => 'text-align: right', 'total' => 'sum', 'filter_block' => 'grid_range_filter'),
- 'Tax' => Array ('title' => 'la_col_Tax', 'td_style' => 'text-align: right', 'total' => 'sum', 'hidden' => 1, 'filter_block' => 'grid_range_filter'),
- 'Shipping' => Array ('title' => 'la_col_Shipping', 'td_style' => 'text-align: right', 'total' => 'sum', 'hidden' => 1, 'filter_block' => 'grid_range_filter'),
- 'Processing' => Array ('title' => 'la_col_Processing', 'td_style' => 'text-align: right', 'total' => 'sum', 'hidden' => 1, 'filter_block' => 'grid_range_filter'),
- 'Profit' => Array ('title' => 'la_col_Profit', 'td_style' => 'text-align: right', 'total' => 'sum', 'filter_block' => 'grid_range_filter'),
- 'StoreProfit' => Array ('title' => 'la_col_StoreProfit', 'td_style' => 'text-align: right', 'total' => 'sum', 'filter_block' => 'grid_range_filter'),
- 'eBayProfit' => Array ('title' => 'la_col_eBayProfit', 'td_style' => 'text-align: right', 'total' => 'sum', 'filter_block' => 'grid_range_filter'),
- ),
-
- );
-
- if (!$this->Application->isModuleEnabled('in-auction'))
- {
- $a_fields =& $new_options['Grids'][$grid]['Fields'];
- unset($a_fields['StoreQty']);
- unset($a_fields['eBayQty']);
- unset($a_fields['StoreAmount']);
- unset($a_fields['eBayAmount']);
- unset($a_fields['StoreProfit']);
- unset($a_fields['eBayProfit']);
- }
-
-
- $new_options['VirtualFields'] = array_merge($rep_options['VirtualFields'], Array (
- 'ProductName' => Array ('type' => 'string', 'default' => ''),
- 'Metric' => Array (
- 'type' => 'int',
- 'formatter' => 'kOptionsFormatter',
- 'options' => $this->GetMetricOptions($new_options, array('ProductId', 'ProductName'), $grid),
- 'use_phrases' => 1,
- 'default' => 0
- ),
- ));
-
- $lang = $this->Application->GetVar('m_lang');
-
- $new_options['CalculatedFields'][''] = Array (
- 'ProductName' => 'p.l'.$lang.'_Name',
- );
- }
- elseif ($field_values['ReportType'] == 12) { // Overall
-
- $new_options['ListSQLs'][''] =
- 'SELECT %1$s.* %2$s FROM %1$s';
-
- $new_options['Fields']['Marketplace'] = Array (
- 'type' => 'int',
- 'formatter' => 'kOptionsFormatter',
- 'options' => Array (
- 1 => 'la_OnlineStore',
- 2 => 'la_eBayMarketplace',
- ),
- 'use_phrases' => 1,
- 'default' => 1
- );
-
- $new_options['Grids'][$grid] = Array(
- 'Icons' => Array(
- 'default' => 'icon16_item.png',
- 'module' => 'core',
- ),
- 'Fields' => Array(
- 'Marketplace' => Array ('title' => 'la_col_Marketplace', 'filter_block' => 'grid_options_filter'),
- 'Qty' => Array ('td_style' => 'text-align: center', 'total' => 'sum', 'filter_block' => 'grid_range_filter'),
- 'Cost' => Array ('td_style' => 'text-align: right', 'total' => 'sum', 'filter_block' => 'grid_range_filter'),
- 'Amount' => Array ('title' => 'la_col_GMV', 'td_style' => 'text-align: right', 'total' => 'sum', 'filter_block' => 'grid_range_filter'),
- 'Tax' => Array ('title' => 'la_col_Tax', 'td_style' => 'text-align: right', 'total' => 'sum', 'filter_block' => 'grid_range_filter'),
- 'Shipping' => Array ('title' => 'la_col_Shipping', 'td_style' => 'text-align: right', 'total' => 'sum', 'filter_block' => 'grid_range_filter'),
- 'Processing' => Array ('title' => 'la_col_Processing', 'td_style' => 'text-align: right', 'total' => 'sum', 'filter_block' => 'grid_range_filter'),
- 'Profit' => Array ('title' => 'la_col_Profit', 'td_style' => 'text-align: right', 'total' => 'sum', 'filter_block' => 'grid_range_filter'),
- ),
-
- );
-
-
- $new_options['VirtualFields'] = array_merge($rep_options['VirtualFields'], array(
- 'Metric' => Array (
- 'type' => 'int',
- 'formatter' => 'kOptionsFormatter',
- 'options' => $this->GetMetricOptions($new_options, array('Marketplace'), $grid),
- 'use_phrases' => 1,
- 'default' => 0
- ),
- ));
-
- $lang = $this->Application->GetVar('m_lang');
-
- }
-
- $new_options['ListSortings'] = Array(
- '' => Array(
- 'Sorting' => Array('Amount' => 'desc'),
- )
- );
-
- foreach ($new_options as $key => $val) {
- $this->Application->setUnitOption('rep', $key, $val);
- }
- }
-
/**
* Enter description here...
*
@@ -639,30 +137,6 @@
return $value_ts;
}
- /**
- * Generate Metric Field Options
- *
- * @param array $a_config_options Config options.
- * @param array $exclude_fields Exclude fields.
- * @param string $grid Grid.
- *
- * @return array
- */
- function GetMetricOptions(&$a_config_options, array $exclude_fields, $grid)
- {
- $a_ret = array();
-
- foreach ( $a_config_options['Grids'][$grid]['Fields'] as $field => $a_options ) {
- if ( in_array($field, $exclude_fields) ) {
- continue;
- }
-
- $a_ret[$field] = isset($a_options['title']) ? $a_options['title'] : 'column:la_fld_' . $field;
- }
-
- return $a_ret;
- }
-
function OnChangeStatistics($event)
{
$this->Application->StoreVar('ChartMetric', $this->Application->GetVar('metric'));
@@ -843,27 +317,38 @@
$ret = str_replace('<tab>',',', $ret);
$ret = str_replace('<cr>',"\r", $ret);
- switch ($report_options['ReportType'])
- {
- case 1:
- $file_name = '-ByCategory';
- break;
- case 2:
- $file_name = '-ByUser';
- break;
- case 5:
- $file_name = '-ByProduct';
- break;
- case 12:
- $file_name = '';
- break;
- }
+ /** @var SalesReportGeneratorManager $sales_report_generator_manager */
+ $sales_report_generator_manager = $this->Application->recallObject('SalesReportGeneratorManager');
+ $sales_report_generator = $sales_report_generator_manager->get($report_options['ReportType']);
+ $file_name = $sales_report_generator->getExportFilename($report_options);
+
+ $event->status = kEvent::erSTOP;
header("Content-type: application/txt");
header("Content-length: ".(string)strlen($ret));
- header("Content-Disposition: attachment; filename=\"".html_entity_decode('SalesReport'.$file_name.'-'.date('d-M-Y').'.csv')."\"");
+ header("Content-Disposition: attachment; filename=\"".html_entity_decode($file_name)."\"");
header("Pragma: no-cache"); //some IE-fixing stuff
echo $ret;
- exit();
}
+
+ /**
+ * @inheritDoc
+ */
+ protected function OnAfterConfigRead(kEvent $event)
+ {
+ parent::OnAfterConfigRead($event);
+
+ $report_options = $this->Application->RecallVar('report_options');
+
+ if ( $report_options ) {
+ $field_values = unserialize($report_options);
+
+ /** @var SalesReportGeneratorManager $sales_report_generator_manager */
+ $sales_report_generator_manager = $this->Application->recallObject('SalesReportGeneratorManager');
+
+ $sales_report_generator = $sales_report_generator_manager->get($field_values['ReportType']);
+ $sales_report_generator->updateUnitConfig($field_values);
+ }
+ }
+
}

Event Timeline