180 lines
7.2 KiB
PHP
Raw Normal View History

2016-04-14 16:14:31 +02:00
<?php
class AntStats extends ObjectModel {
public static function getOrdersTotalByDate($date_from = null, $date_to = null)
{
$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
ato.id_tracking AS id_tracking,
DATE_FORMAT(o.`date_add`, "%Y-%m-%d") AS date,
SUM(d.`product_quantity`) AS `total_products`,
COUNT(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_.'ant_marketing_tracking_orders` ato
RIGHT JOIN `'._DB_PREFIX_.'order_detail` d
ON ato.id_orders = d.id_order
RIGHT JOIN `'._DB_PREFIX_.'orders` o
ON d.`id_order` = o.`id_order`
AND (
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()).'")
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)
GROUP BY ato.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
)
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;
}
}