bebeboutik/modules/ant_customgroup/models/CustomGroup.php
2017-09-28 10:39:08 +02:00

514 lines
21 KiB
PHP

<?php
class CustomGroup extends ObjectModel
{
public $name;
public $register_date;
public $last_order_date;
public $nb_retention_days;
public $minimum_order_amount;
public $customer_order_stats;
public $sales;
public $active;
public $date_add;
public $date_upd;
protected $fieldsRequired = array('name', 'nb_retention_days');
protected $fieldsValidate = array(
'name' => 'isName',
'register_date' => 'isUnsignedId',
'last_order_date' => 'isUnsignedId',
'nb_retention_days' => 'isUnsignedId',
'minimum_order_amount' => 'isFloat',
'active' => 'isBool',
'date_add' => 'isDate',
'date_upd' => 'isDate',
);
protected $table = 'custom_group';
protected $identifier = 'id_custom_group';
public function getFields()
{
parent::validateFields();
$fields['name'] = pSQL($this->name);
$fields['register_date'] = (int)$this->register_date;
$fields['last_order_date'] = (int)$this->last_order_date;
$fields['nb_retention_days'] = (int)$this->nb_retention_days;
$fields['minimum_order_amount'] = (float)$this->minimum_order_amount;
$fields['active'] = (int)$this->active;
$fields['date_add'] = pSQL($this->date_add);
$fields['date_upd'] = pSQL($this->date_upd);
return $fields;
}
public function delete()
{
$res = parent::delete();
if ($res) {
return (Db::getInstance()->execute('DELETE FROM `' . _DB_PREFIX_ . 'custom_group_customer`
WHERE `id_custom_group` = ' . (int)$this->id) &&
// Db::getInstance()->execute('DELETE FROM `' . _DB_PREFIX_ . 'custom_group_order_stats`
// WHERE `id_custom_group` = ' . (int)$this->id) &&
// Db::getInstance()->execute('DELETE FROM `' . _DB_PREFIX_ . 'custom_group_sales`
// WHERE `id_custom_group` = ' . (int)$this->id) &&
Db::getInstance()->execute('DELETE FROM `' . _DB_PREFIX_ . 'custom_group_discount`
WHERE `id_custom_group` = ' . (int)$this->id));
}
return $res;
}
/**
* Get groups
* @param $only_active Bool get only active groups
* @return Array Groups
*/
public static function getGroups($only_active = true)
{
return Db::getInstance()->executeS('
SELECT *
FROM `' . _DB_PREFIX_ . 'custom_group` cg
WHERE 1
' . ($only_active ? 'AND cg.`active` = 1' : '') . '
ORDER BY cg.id_custom_group;
');
}
public function save($nullValues = false, $autodate = true)
{
if (parent::save()) {
$return = true;
if (isset($this->customer_order_stats)) {
$return = $return && self::setOrderStats($this->id, $this->customer_order_stats);
}
if (isset($this->sales)) {
$return = $return && self::setSales($this->id, $this->sales);
}
return $return;
}
}
/**
* Check if the custom group order stats is selected for the given custom group
* @param $id_custom_group integer
* @return Array Customer order stats Ids
*/
public static function getOrderStatsIds($id_custom_group)
{
if (!isset($id_custom_group)) {
return false;
}
$result = Db::getInstance()->executeS('
SELECT id_order_stats
FROM `' . _DB_PREFIX_ . 'custom_group_order_stats`
WHERE id_custom_group = ' . (int)$id_custom_group . '
');
if ($result) {
$tmparray = array();
foreach ($result as $row) {
$tmparray[] = (int)$row['id_order_stats'];
}
$result = $tmparray;
}
return $result;
}
/**
* Check if the custom group order stats is selected for the given custom group
* @param $id_custom_group integer
* @return Array Customer order stats Ids
*/
public static function getSalesIds($id_custom_group)
{
if (!isset($id_custom_group)) {
return false;
}
$result = Db::getInstance()->executeS('
SELECT id_sale
FROM `' . _DB_PREFIX_ . 'custom_group_sales`
WHERE id_custom_group = ' . (int)$id_custom_group . '
');
if ($result) {
$tmparray = array();
foreach ($result as $row) {
$tmparray[] = (int)$row['id_sale'];
}
$result = $tmparray;
}
return $result;
}
public static function setOrderStats($id_custom_group, $custom_group_order_stats_array)
{
if (!isset($id_custom_group)) {
return false;
}
if (!is_array($custom_group_order_stats_array)) {
return false;
}
if (Db::getInstance()->Execute('DELETE
FROM `' . _DB_PREFIX_ . 'custom_group_order_stats`
WHERE id_custom_group = ' . (int)$id_custom_group)
) {
foreach ($custom_group_order_stats_array as $custom_group_order_stats_id) {
Db::getInstance()->ExecuteS('INSERT INTO `' . _DB_PREFIX_ . 'custom_group_order_stats`
VALUES (' .
(int)$id_custom_group . ',' .
(int)$custom_group_order_stats_id .
')'
);
}
return true;
}
}
public static function setSales($id_custom_group, $custom_group_sales_array)
{
if (!isset($id_custom_group)) {
return false;
}
if (!is_array($custom_group_sales_array)) {
return false;
}
if (Db::getInstance()->Execute('DELETE
FROM `' . _DB_PREFIX_ . 'custom_group_sales`
WHERE id_custom_group = ' . (int)$id_custom_group)
) {
foreach ($custom_group_sales_array as $custom_group_sale_id) {
Db::getInstance()->ExecuteS('INSERT INTO `' . _DB_PREFIX_ . 'custom_group_sales`
VALUES (' .
(int)$id_custom_group . ',' .
(int)$custom_group_sale_id .
')'
);
}
return true;
}
}
public function refreshCustomGroup()
{
return self::refreshCustomGroupStatic($this->id);
}
public static function refreshCustomGroupStatic($id_custom_group)
{
$where_clause = "";
$join_clause = "";
if (!isset($id_custom_group)) {
return false;
}
$customGroup = new CustomGroup((int)$id_custom_group);
if (Validate::isLoadedObject($customGroup)) {
$retention_days = 0;
if ($customGroup->nb_retention_days > 0) {
$retention_days = $customGroup->nb_retention_days;
}
//if the group is linked to orders data, we get the id_customer array from here.
if ($customGroup->last_order_date != 0 OR $customGroup->minimum_order_amount > 0.0) {
if($retention_days != 0) {
if ($customGroup->last_order_date != 0) {
$where_clause .= ' AND DATEDIFF(CURDATE(), o.date_add) < ' . ((int)$customGroup->last_order_date + (int)$retention_days);
$where_clause .= ' AND DATEDIFF(CURDATE(), o.date_add) > ' . (int)$customGroup->last_order_date;
}
if ($customGroup->register_date != 0) {
$join_clause .= ' JOIN `' . _DB_PREFIX_ . 'customer` pc ON pc.id_customer = o.id_customer ';
$where_clause .= ' AND DATEDIFF(CURDATE(), pc.date_add) < ' . ((int)$customGroup->register_date + (int)$retention_days);
$where_clause .= ' AND DATEDIFF(CURDATE(), pc.date_add) > ' . ((int)$customGroup->register_date);
}
}elseif($retention_days == 0){
if ($customGroup->last_order_date != 0) {
$where_clause .= ' AND DATEDIFF(CURDATE(), o.date_add) > ' . (int)$customGroup->last_order_date;
}
if ($customGroup->register_date != 0) {
$join_clause .= ' JOIN `' . _DB_PREFIX_ . 'customer` pc ON pc.id_customer = o.id_customer ';
$where_clause .= ' AND DATEDIFF(CURDATE(), pc.date_add) > ' . (int)$customGroup->register_date;
}
}
if ($customGroup->minimum_order_amount > 0.0) {
$where_clause .= ' AND o.total_paid_real > ' . round($customGroup->minimum_order_amount, 2);
}
$query = 'SELECT o.id_customer
FROM `' . _DB_PREFIX_ . 'orders` o
' . $join_clause . '
WHERE o.valid = 1
' . $where_clause . '
GROUP BY o.id_customer';
// echo $query."\n";
$customer_ids = Db::getInstance()->executeS($query);
} else {
if($retention_days != 0) {
if ($customGroup->register_date != 0) {
$where_clause .= ' AND DATEDIFF(CURDATE(), pc.date_add) < ' . ((int)$customGroup->register_date + (int)$retention_days);
$where_clause .= ' AND DATEDIFF(CURDATE(), pc.date_add) > ' . ((int)$customGroup->register_date);
}
} elseif ($retention_days == 0){
if ($customGroup->register_date != 0) {
$where_clause .= ' AND DATEDIFF(CURDATE(), pc.date_add) > ' . (int)$customGroup->register_date;
}
}
$query = 'SELECT pc.id_customer
FROM `' . _DB_PREFIX_ . 'customer` pc
' . $join_clause . '
WHERE 1
' . $where_clause . '';
// echo $query."\n";
$customer_ids = Db::getInstance()->executeS($query);
}
$batch_size = 1000;
$buffer_key_values = array();
if (Db::getInstance()->Execute('DELETE FROM `' . _DB_PREFIX_ . 'custom_group_customer`
WHERE id_custom_group = ' . (int)$customGroup->id)
) {
$insert = 'INSERT INTO `' . _DB_PREFIX_ . 'custom_group_customer` (id_custom_group, id_customer) VALUES ';
for ($i = 0; $i < count($customer_ids); $i++) {
$buffer_key_values[] = '(' . $customGroup->id . ',' . $customer_ids[$i]['id_customer'] . ')';
if (($i % $batch_size) == 0 and $i > 0) {
Db::getInstance()->Execute($insert . implode(',', $buffer_key_values));
$buffer_key_values = array();
}
}
if (count($buffer_key_values) > 0) {
Db::getInstance()->Execute($insert . implode(',', $buffer_key_values));
}
}
return true;
} else {
return false;
}
}
public function refreshCustomers()
{
return self::refreshCustomersStatic($this->id);
}
public static function refreshCustomersStatic($id_custom_group)
{
$where_clause = "";
$join_clause = "";
if (!isset($id_custom_group)) {
return false;
}
$customGroup = new CustomGroup((int)$id_custom_group);
if (Validate::isLoadedObject($customGroup)) {
$retention_days = 0;
if ($customGroup->nb_retention_days > 0) {
$retention_days = $customGroup->nb_retention_days;
}
//if the group is linked to orders data, we get the id_customer array from here.
if ((CustomGroup::getSalesIds($customGroup->id))
OR $customGroup->last_order_date != 0
OR $customGroup->minimum_order_amount > 0.0
) {
if($retention_days != 0) {
if ($customGroup->last_order_date != 0) {
$where_clause .= ' AND DATEDIFF(CURDATE(), o.date_add) < ' . ((int)$customGroup->last_order_date + (int)$retention_days);
$where_clause .= ' AND DATEDIFF(CURDATE(), o.date_add) > ' . (int)$customGroup->last_order_date;
}
if ($customGroup->register_date != 0) {
$join_clause .= ' JOIN `' . _DB_PREFIX_ . 'customer` pc ON pc.id_customer = o.id_customer ';
$where_clause .= ' AND DATEDIFF(CURDATE(), pc.date_add) < ' . ((int)$customGroup->register_date + (int)$retention_days);
$where_clause .= ' AND DATEDIFF(CURDATE(), pc.date_add) > ' . ((int)$customGroup->register_date);
}
}elseif($retention_days == 0){
if ($customGroup->last_order_date != 0) {
$where_clause .= ' AND DATEDIFF(CURDATE(), o.date_add) > ' . (int)$customGroup->last_order_date;
}
if ($customGroup->register_date != 0) {
$join_clause .= ' JOIN `' . _DB_PREFIX_ . 'customer` pc ON pc.id_customer = o.id_customer ';
$where_clause .= ' AND DATEDIFF(CURDATE(), pc.date_add) > ' . (int)$customGroup->register_date;
}
}
if ($customGroup->minimum_order_amount > 0.0) {
$where_clause .= ' AND o.total_paid_real > ' . round($customGroup->minimum_order_amount, 2);
}
if ((CustomGroup::getOrderStatsIds($customGroup->id))) {
$join_clause .= ' JOIN `' . _DB_PREFIX_ . 'customer_order_stats` cos
ON cos.id_customer = o.id_customer';
$where_clause .= ' AND cos.id_order_stats IN (' . implode(',', CustomGroup::getOrderStatsIds($customGroup->id)) . ')';
}
if (CustomGroup::getSalesIds($customGroup->id)) {
$query = 'SELECT o.id_customer
FROM `' . _DB_PREFIX_ . 'product_ps_cache` ppsc
JOIN `' . _DB_PREFIX_ . 'order_detail` od ON od.product_id = ppsc.id_product
JOIN `' . _DB_PREFIX_ . 'orders` o ON o.id_order = od.id_order
' . $join_clause . '
WHERE ppsc.id_sale IN (' . implode(',', CustomGroup::getSalesIds($customGroup->id)) . ' )
AND o.valid = 1
' . $where_clause . '
GROUP BY o.id_customer
';
//echo $query."\n";
$customer_ids = Db::getInstance()->executeS($query);
} else {
$query = 'SELECT o.id_customer
FROM `' . _DB_PREFIX_ . 'orders` o
' . $join_clause . '
WHERE o.valid = 1
' . $where_clause . '
GROUP BY o.id_customer
';
//echo $query."\n";
$customer_ids = Db::getInstance()->executeS($query);
}
} else {
if ((CustomGroup::getOrderStatsIds($customGroup->id))) {
$join_clause .= ' JOIN `' . _DB_PREFIX_ . 'customer_order_stats` cos
ON cos.id_customer = pc.id_customer';
$where_clause .= ' AND cos.id_order_stats IN (' . implode(',', CustomGroup::getOrderStatsIds($customGroup->id)) . ')';
}
if($retention_days != 0) {
if ($customGroup->register_date != 0) {
$where_clause .= ' AND DATEDIFF(CURDATE(), pc.date_add) < ' . ((int)$customGroup->register_date + (int)$retention_days);
$where_clause .= ' AND DATEDIFF(CURDATE(), pc.date_add) > ' . ((int)$customGroup->register_date);
}
}elseif($retention_days == 0){
if ($customGroup->register_date != 0) {
$where_clause .= ' AND DATEDIFF(CURDATE(), pc.date_add) > ' . (int)$customGroup->register_date;
}
}
$query = 'SELECT pc.id_customer
FROM `' . _DB_PREFIX_ . 'customer` pc
' . $join_clause . '
WHERE 1
' . $where_clause . '
';
//echo $query."\n";
$customer_ids = Db::getInstance()->executeS($query);
}
$batch_size = 1000;
$buffer_key_values = array();
if (Db::getInstance()->Execute('DELETE FROM `' . _DB_PREFIX_ . 'custom_group_customer`
WHERE id_custom_group = ' . (int)$customGroup->id)
) {
$insert = 'INSERT INTO `' . _DB_PREFIX_ . 'custom_group_customer` (id_custom_group, id_customer) VALUES ';
for ($i = 0; $i < count($customer_ids); $i++) {
$buffer_key_values[] = '(' . $customGroup->id . ',' . $customer_ids[$i]['id_customer'] . ')';
if (($i % $batch_size) == 0 and $i > 0) {
Db::getInstance()->Execute($insert . implode(',', $buffer_key_values));
$buffer_key_values = array();
}
}
if (count($buffer_key_values) > 0) {
Db::getInstance()->Execute($insert . implode(',', $buffer_key_values));
}
}
return true;
} else {
return false;
}
}
public static function getCount($id_custom_group)
{
return Db::getInstance()->getValue("SELECT COUNT(id_customer)
FROM `" . _DB_PREFIX_ . "custom_group_customer`
WHERE id_custom_group = " . (int)$id_custom_group);
}
public static function inGroup($id_custom_group, $id_customer)
{
return (Db::getInstance()->getValue('SELECT count(*)
FROM `' . _DB_PREFIX_ . 'custom_group_customer`
WHERE id_customer = ' . (int)$id_customer . '
AND id_custom_group = ' . (int)$id_custom_group) > 0);
}
public static function getCustomers($id_custom_group)
{
return Db::getInstance()->executeS("SELECT id_customer
FROM `" . _DB_PREFIX_ . "custom_group_customer`
WHERE id_custom_group = " . (int)$id_custom_group);
}
public static function isLinkedToDiscount($id_custom_group, $id_discount)
{
return (Db::getInstance()->getValue('SELECT count(*)
FROM `' . _DB_PREFIX_ . 'custom_group_discount`
WHERE id_discount = ' . (int)$id_discount . '
AND id_custom_group = ' . (int)$id_custom_group) > 0);
}
public static function getCustomGroupIdByDiscount($id_discount)
{
/*echo 'getCustomGroupIdByDiscount'.$id_discount.' SELECT id_custom_group
FROM `' . _DB_PREFIX_ . 'custom_group_discount`
WHERE id_discount = ' . (int)$id_discount;*/
if ($id_custom_group = Db::getInstance()->getValue('SELECT id_custom_group
FROM `' . _DB_PREFIX_ . 'custom_group_discount`
WHERE id_discount = ' . (int)$id_discount)
) {
return (int)$id_custom_group;
} else {
return false;
}
}
public static function linkToDiscount($id_custom_group, $id_discount)
{
if (Db::getInstance()->execute('DELETE FROM `' . _DB_PREFIX_ . 'custom_group_discount`
WHERE id_discount = ' . (int)$id_discount)
) {
if (isset($id_custom_group)) {
return Db::getInstance()->execute('INSERT INTO `' . _DB_PREFIX_ . 'custom_group_discount`
(id_custom_group, id_discount) VALUES
(' . (int)$id_custom_group . ',' . (int)$id_discount . ')');
} else {
return true;
}
} else {
return false;
}
}
public static function isValidDiscount($id_discount, $id_customer)
{
if (!isset($id_discount) OR !isset($id_customer)) {
return false;
}
if ($id_custom_group = self::getCustomGroupIdByDiscount($id_discount)) {
//echo 'isValidDiscount'.$id_custom_group." - ".$id_customer.self::inGroup($id_custom_group,$id_customer);
return self::inGroup($id_custom_group, $id_customer);
} else {
return true;
}
}
}