295 lines
13 KiB
PHP
Executable File
295 lines
13 KiB
PHP
Executable File
<?php
|
|
|
|
class AntStats extends ObjectModel
|
|
{
|
|
|
|
public static function getOrdersTotalByDate($date_from = null, $date_to = null)
|
|
{
|
|
$id_shop = 1;
|
|
$default_array = array(
|
|
'total_products' => 0,
|
|
'total_order' => 0,
|
|
'total_products_price' => 0,
|
|
'total_products_price_tax_excl' => 0,
|
|
'total_wholesale_price' => 0,
|
|
'subscribe' => 0,
|
|
'visits' => 0,
|
|
'cout_order' => 0,
|
|
'cout_subscribe' => 0,
|
|
'cout_visits' => 0,
|
|
);
|
|
|
|
foreach(Db::getInstance()->executeS('SELECT * FROM '._DB_PREFIX_.'ant_marketing_tracking') as $tracking) {
|
|
$totals = array();
|
|
//VISITS
|
|
$visits = Db::getInstance()->executeS('SELECT DATE_FORMAT(atv.`date`, "%Y-%m-%d") AS date, SUM(nb_visits) as visits
|
|
FROM `'._DB_PREFIX_.'ant_marketing_tracking_visits` atv
|
|
WHERE atv.id_tracking = '.(int)$tracking['id_tracking'].'
|
|
GROUP BY DATE_FORMAT(atv.`date`, "%Y-%m-%d")');
|
|
foreach($visits as $visit) {
|
|
|
|
if(!isset($totals[$visit['date']])) {
|
|
$totals[$visit['date']] = $default_array;
|
|
}
|
|
|
|
$totals[$visit['date']]['visits'] = $visit['visits'];
|
|
}
|
|
|
|
//SUBSCRIBE
|
|
$subscribes = Db::getInstance()->executeS('SELECT COUNT(*) as count_subscribe, DATE_FORMAT(atu.`date`, "%Y-%m-%d") AS date
|
|
FROM `'._DB_PREFIX_.'ant_marketing_tracking_users` atu
|
|
WHERE atu.id_tracking = '.(int)$tracking['id_tracking'].'
|
|
GROUP BY DATE_FORMAT(atu.`date`, "%Y-%m-%d")');
|
|
foreach($subscribes as $subscribe) {
|
|
|
|
if(!isset($totals[$subscribe['date']])) {
|
|
$totals[$subscribe['date']] = $default_array;
|
|
}
|
|
|
|
$totals[$subscribe['date']]['subscribe'] = $subscribe['count_subscribe'];
|
|
}
|
|
|
|
|
|
//ORDER
|
|
$orders = Db::getInstance()->executeS('SELECT id_order, DATE_FORMAT(o.`date_add`, "%Y-%m-%d") AS date
|
|
FROM `'._DB_PREFIX_.'ant_marketing_tracking_users` atu
|
|
LEFT JOIN `'._DB_PREFIX_.'orders` o
|
|
ON o.id_customer = atu.id_customer
|
|
WHERE o.current_state NOT IN ("'.implode('","', self::getStateNull()).'")
|
|
AND atu.id_tracking = '.(int)$tracking['id_tracking']);
|
|
|
|
if($orders) {
|
|
|
|
$orders_infos = array();
|
|
foreach($orders as $order) {
|
|
$orders_infos[(int)$order['id_order']] = $order;
|
|
|
|
if(!isset($totals[$order['date']])) {
|
|
$totals[$order['date']] = $default_array;
|
|
}
|
|
}
|
|
|
|
foreach(Db::getInstance()->executeS('SELECT
|
|
id_order,
|
|
SUM(d.`product_quantity`) AS `total_products`,
|
|
SUM(
|
|
ROUND(
|
|
d.unit_price_tax_incl * d.`product_quantity`, 6
|
|
)
|
|
) AS `total_products_price`,
|
|
SUM(
|
|
ROUND(
|
|
d.unit_price_tax_excl * d.`product_quantity`, 6
|
|
)
|
|
) AS `total_products_price_tax_excl`,
|
|
CASE
|
|
WHEN pas.wholesale_price IS NULL AND d.product_attribute_id != 0
|
|
THEN SUM(pas.wholesale_price * d.`product_quantity`)
|
|
ELSE SUM(ps.wholesale_price * d.`product_quantity`)
|
|
END AS total_wholesale_price,
|
|
NULL AS subscribe,
|
|
NULL AS visits
|
|
FROM `'._DB_PREFIX_.'order_detail` d
|
|
LEFT OUTER JOIN `'._DB_PREFIX_.'product_shop` ps
|
|
ON ps.id_shop = '.(int)$id_shop.' AND ps.id_product = d.product_id
|
|
LEFT OUTER JOIN `'._DB_PREFIX_.'product_attribute_shop` pas
|
|
ON (d.product_attribute_id != 0 AND pas.id_product_attribute = d.product_attribute_id)
|
|
WHERE id_order IN ('.implode(',', array_keys($orders_infos)).')
|
|
GROUP BY id_order') as $order_detail) {
|
|
$current_order = $orders_infos[$order_detail['id_order']];
|
|
|
|
$totals[$current_order['date']]['total_products'] += $order_detail['total_products'];
|
|
$totals[$current_order['date']]['total_order'] += 1;
|
|
$totals[$current_order['date']]['total_products_price'] += $order_detail['total_products_price'];
|
|
$totals[$current_order['date']]['total_products_price_tax_excl'] += $order_detail['total_products_price_tax_excl'];
|
|
$totals[$current_order['date']]['total_wholesale_price'] += $order_detail['total_wholesale_price'];
|
|
}
|
|
}
|
|
|
|
$query = array();
|
|
foreach($totals as $date => $total) {
|
|
$total['id_tracking'] = $tracking['id_tracking'];
|
|
$total['date'] = $date;
|
|
if(!empty($tracking['cout'])
|
|
&& $tracking['cout'] > 0) {
|
|
$total['cout_order'] = $total['total_order']/$tracking['cout'];
|
|
$total['cout_subscribe'] = $total['subscribe']/$tracking['cout'];
|
|
$total['cout_visits'] = $total['visits']/$tracking['cout'];
|
|
}
|
|
$query[] = $total;
|
|
}
|
|
|
|
Db::getInstance()->delete('ant_marketing_stats', 'id_tracking = '.$tracking['id_tracking']);
|
|
Db::getInstance()->insert('ant_marketing_stats', $query);
|
|
}
|
|
|
|
// $req = 'SELECT
|
|
// a.id_tracking,
|
|
// a.date,
|
|
// SUM(a.total_products) AS total_products,
|
|
// SUM(a.total_order) AS total_order,
|
|
// SUM(a.total_products_price) AS total_products_price,
|
|
// SUM(a.total_products_price_tax_excl) AS total_products_price_tax_excl,
|
|
// SUM(a.total_wholesale_price) AS total_wholesale_price,
|
|
// SUM(a.subscribe) AS subscribe,
|
|
// SUM(a.visits) AS visits,
|
|
// (total_order/cout) as cout_order,
|
|
// (subscribe/cout) as cout_subscribe,
|
|
// (visits/cout) as cout_visits
|
|
// FROM
|
|
// '._DB_PREFIX_.'ant_marketing_tracking at
|
|
// LEFT OUTER JOIN
|
|
// (
|
|
// (
|
|
// SELECT
|
|
// atu.id_tracking AS id_tracking,
|
|
// DATE_FORMAT(o.`date_add`, "%Y-%m-%d") AS date,
|
|
// SUM(d.`product_quantity`) AS `total_products`,
|
|
// COUNT(DISTINCT(d.id_order)) AS total_order,
|
|
// SUM(
|
|
// ROUND(
|
|
// d.unit_price_tax_incl * d.`product_quantity`, 6
|
|
// )
|
|
// ) AS `total_products_price`,
|
|
// SUM(
|
|
// ROUND(
|
|
// d.unit_price_tax_excl * d.`product_quantity`, 6
|
|
// )
|
|
// ) AS `total_products_price_tax_excl`,
|
|
// CASE
|
|
// WHEN pas.wholesale_price IS NULL AND d.product_attribute_id != 0
|
|
// THEN SUM(pas.wholesale_price * d.`product_quantity`)
|
|
// ELSE SUM(ps.wholesale_price * d.`product_quantity`)
|
|
// END AS total_wholesale_price,
|
|
// NULL AS subscribe,
|
|
// NULL AS visits
|
|
// FROM `'._DB_PREFIX_.'orders` o
|
|
// LEFT JOIN `'._DB_PREFIX_.'ant_marketing_tracking_users` atu
|
|
// ON o.id_customer = atu.id_customer
|
|
// RIGHT JOIN `'._DB_PREFIX_.'order_detail` d
|
|
// ON o.id_order = d.id_order
|
|
// LEFT OUTER JOIN `'._DB_PREFIX_.'product_shop` ps
|
|
// ON ps.id_shop = o.id_shop AND ps.id_product = d.product_id
|
|
// LEFT OUTER JOIN `'._DB_PREFIX_.'product_attribute_shop` pas
|
|
// ON (d.product_attribute_id != 0 AND pas.id_product_attribute = d.product_attribute_id)
|
|
// WHERE EXISTS (
|
|
// SELECT
|
|
// id_order_state
|
|
// FROM
|
|
// '._DB_PREFIX_.'order_history oh
|
|
// WHERE
|
|
// o.id_order = oh.id_order
|
|
// ORDER BY
|
|
// id_order_history
|
|
// DESC
|
|
// LIMIT 1
|
|
// )
|
|
// NOT IN ("'.implode('","', self::getStateNull()).'")
|
|
// GROUP BY atu.id_tracking, date
|
|
// )
|
|
// UNION
|
|
// (
|
|
// SELECT
|
|
// atc.id_tracking,
|
|
// DATE_FORMAT(atc.`date`, "%Y-%m-%d") AS date,
|
|
// NULL AS total_products,
|
|
// NULL AS total_order,
|
|
// NULL AS total_products_price,
|
|
// NULL AS total_products_price_tax_excl,
|
|
// NULL AS total_wholesale_price,
|
|
// COUNT(id_customer) AS subscribe,
|
|
// NULL AS visits
|
|
// FROM
|
|
// `'._DB_PREFIX_.'ant_marketing_tracking_users` atc
|
|
// GROUP BY id_tracking, DATE_FORMAT(atc.`date`, "%Y-%m-%d")
|
|
// )
|
|
// UNION
|
|
// (
|
|
// SELECT
|
|
// atv.id_tracking,
|
|
// DATE_FORMAT(atv.`date`, "%Y-%m-%d") AS date,
|
|
// NULL AS total_products,
|
|
// NULL AS total_order,
|
|
// NULL AS total_products_price,
|
|
// NULL AS total_products_price_tax_excl,
|
|
// NULL AS total_wholesale_price,
|
|
// NULL AS subscribe,
|
|
// atv.nb_visits AS visits
|
|
// FROM
|
|
// `'._DB_PREFIX_.'ant_marketing_tracking_visits` atv
|
|
// )
|
|
// ) a
|
|
// ON a.id_tracking = at.id_tracking
|
|
// GROUP BY a.id_tracking, a.date
|
|
// ';
|
|
|
|
// $total = Db::getInstance()->executeS($req);
|
|
|
|
// Db::getInstance()->delete('ant_marketing_stats');
|
|
// Db::getInstance()->insert('ant_marketing_stats', $total);
|
|
|
|
return true;
|
|
}
|
|
|
|
public static function getStateNull()
|
|
{
|
|
return $state_null = array(6, 7, 8);
|
|
}
|
|
|
|
public static function getTotalCategories(AntTrackingCategories $tracking_category)
|
|
{
|
|
$childrens = $tracking_category->getChildrens();
|
|
$trackings = $tracking_category->getTrackingIds();
|
|
|
|
foreach($childrens as $children)
|
|
$trackings = array_merge($trackings, self::getTotalCategories($children));
|
|
|
|
Db::getInstance()->delete('ant_marketing_stats_category', 'id_tracking_category = '.$tracking_category->id);
|
|
if(count($trackings) > 0)
|
|
{
|
|
$req = '
|
|
SELECT
|
|
'.(int)$tracking_category->id.' as id_tracking_category,
|
|
a.*,
|
|
ROUND(total_products_price_tax_excl - total_wholesale_price, 2) as rate_amount,
|
|
(1 - ROUND(total_wholesale_price/total_products_price_tax_excl,2)) as rate,
|
|
(total_order/cout) as cout_order,
|
|
(subscribe/cout) as cout_subscribe,
|
|
(visits/cout) as cout_visit
|
|
FROM
|
|
(
|
|
SELECT
|
|
ats.date as date,
|
|
SUM(total_products) as total_products,
|
|
SUM(total_order) as total_order,
|
|
SUM(total_products_price) as total_products_price,
|
|
SUM(total_products_price_tax_excl) as total_products_price_tax_excl,
|
|
SUM(total_wholesale_price) as total_wholesale_price,
|
|
( SELECT SUM(cout)
|
|
FROM '._DB_PREFIX_.'ant_marketing_tracking at
|
|
WHERE at.id_tracking IN ("'.implode('","', $trackings).'")
|
|
) as cout,
|
|
SUM(subscribe) as subscribe,
|
|
SUM(visits) as visits
|
|
FROM '._DB_PREFIX_.'ant_marketing_stats ats
|
|
WHERE
|
|
ats.id_tracking IN ("'.implode('","', $trackings).'")
|
|
GROUP BY date
|
|
) a
|
|
';
|
|
$totals = Db::getInstance()->executeS($req);
|
|
|
|
Db::getInstance()->insert('ant_marketing_stats_category', $totals);
|
|
}
|
|
return $trackings;
|
|
}
|
|
|
|
public static function getOrdersCategoryTrackingTotal()
|
|
{
|
|
$childrens = AntTrackingCategories::getStaticChildren(0);
|
|
foreach($childrens as $children)
|
|
self::getTotalCategories($children);
|
|
|
|
return true;
|
|
}
|
|
} |