예제 #1
0
/**
 * Promotions post processing
 *
 * @param string $status_to new order status
 * @param string $status_from original order status
 * @param array $order_info order information
 * @param array $force_notification Array with notification rules
 * @return boolean always true
 */
function fn_promotion_post_processing($status_to, $status_from, $order_info, $force_notification = array())
{
    $order_statuses = fn_get_statuses(STATUSES_ORDER, array(), true);
    $notify_user = isset($force_notification['C']) ? $force_notification['C'] : (!empty($order_statuses[$status_to]['params']['notify']) && $order_statuses[$status_to]['params']['notify'] == 'Y' ? true : false);
    $status_from_is_positive = fn_status_is_positive($order_statuses[$status_from]);
    $status_to_is_positive = fn_status_is_positive($order_statuses[$status_to]);
    if (empty($order_info['promotions'])) {
        return false;
    }
    // Process numbers of usage for Open statuses
    if ($status_to != $status_from && fn_status_is_positive($order_statuses[$status_from], true) != fn_status_is_positive($order_statuses[$status_to], true)) {
        // Post processing
        if (fn_status_is_positive($order_statuses[$status_to], true)) {
            db_query("UPDATE ?:promotions SET number_of_usages = number_of_usages + 1 WHERE promotion_id IN (?n)", array_keys($order_info['promotions']));
        } else {
            db_query("UPDATE ?:promotions SET number_of_usages = number_of_usages - 1 WHERE promotion_id IN (?n)", array_keys($order_info['promotions']));
        }
    }
    if ($status_to != $status_from && $status_from_is_positive != $status_to_is_positive) {
        // Apply pending actions
        foreach ($order_info['promotions'] as $k => $v) {
            if (!empty($v['bonuses'])) {
                foreach ($v['bonuses'] as $bonus) {
                    // Assign usergroup
                    if ($bonus['bonus'] == 'give_usergroup') {
                        $is_ug_already_assigned = false;
                        if (empty($order_info['user_id'])) {
                            continue;
                        }
                        // Don't assing a disabled usergroup
                        $system_usergroups = fn_get_usergroups(array('type' => 'C', 'status' => array('A', 'H')), CART_LANGUAGE);
                        if (!empty($system_usergroups[$bonus['value']]['status']) && in_array($system_usergroups[$bonus['value']]['status'], array('A', 'H'))) {
                            if ($order_statuses[$status_to]['params']['inventory'] == 'D') {
                                // Don't assing the usergroup to the user if it's already assigned
                                $current_user_usergroups = fn_get_user_usergroups($order_info['user_id']);
                                foreach ($current_user_usergroups as $ug) {
                                    if (isset($ug['usergroup_id']) && $bonus['value'] == $ug['usergroup_id'] && in_array($ug['status'], array('A', 'H'))) {
                                        $is_ug_already_assigned = true;
                                        break;
                                    }
                                }
                                if (!$is_ug_already_assigned) {
                                    db_query("REPLACE INTO ?:usergroup_links SET user_id = ?i, usergroup_id = ?i, status = 'A'", $order_info['user_id'], $bonus['value']);
                                    $activated = true;
                                }
                            } else {
                                db_query("UPDATE ?:usergroup_links SET status = 'F' WHERE user_id = ?i AND usergroup_id = ?i", $order_info['user_id'], $bonus['value']);
                                $activated = false;
                            }
                            if ($notify_user == true && !$is_ug_already_assigned) {
                                $prefix = $activated == true ? 'activation' : 'disactivation';
                                Mailer::sendMail(array('to' => $order_info['email'], 'from' => 'company_users_department', 'data' => array('user_data' => fn_get_user_info($order_info['user_id']), 'usergroups' => fn_get_usergroups(array('status' => array('A', 'H')), $order_info['lang_code']), 'usergroup_ids' => (array) $bonus['value']), 'tpl' => 'profiles/usergroup_' . $prefix . '.tpl', 'company_id' => $order_info['company_id']), 'C', $order_info['lang_code']);
                            }
                        } else {
                            if (AREA == 'C') {
                                fn_set_notification('E', __('error'), __('unable_to_assign_usergroup'));
                            }
                        }
                    } elseif ($bonus['bonus'] == 'give_coupon') {
                        $promotion_data = fn_get_promotion_data($bonus['value']);
                        if (empty($promotion_data)) {
                            continue;
                        }
                        if ($status_to_is_positive) {
                            fn_promotion_update_condition($promotion_data['conditions']['conditions'], 'add', 'auto_coupons', $bonus['coupon_code']);
                            if ($notify_user == true) {
                                Mailer::sendMail(array('to' => $order_info['email'], 'from' => 'company_users_department', 'data' => array('promotion_data' => $promotion_data, 'bonus_data' => $bonus, 'order_info' => $order_info), 'tpl' => 'promotions/give_coupon.tpl', 'company_id' => $order_info['company_id']), 'C', $order_info['lang_code']);
                            }
                        } else {
                            fn_promotion_update_condition($promotion_data['conditions']['conditions'], 'remove', 'auto_coupons', $bonus['coupon_code']);
                        }
                        db_query("UPDATE ?:promotions SET conditions = ?s, conditions_hash = ?s, users_conditions_hash = ?s WHERE promotion_id = ?i", serialize($promotion_data['conditions']), fn_promotion_serialize($promotion_data['conditions']['conditions']), fn_promotion_serialize_users_conditions($promotion_data['conditions']['conditions']), $bonus['value']);
                    }
                }
            }
        }
    }
    return true;
}
예제 #2
0
    //
    // Delete selected promotions
    //
    if ($mode == 'm_delete') {
        if (!empty($_REQUEST['promotion_ids'])) {
            fn_delete_promotions($_REQUEST['promotion_ids']);
        }
        $suffix = ".manage";
    }
    return array(CONTROLLER_STATUS_OK, "promotions{$suffix}");
}
// ----------------------------- GET routines -------------------------------------------------
// promotion data
if ($mode == 'update') {
    Registry::set('navigation.tabs', array('details' => array('title' => __('general'), 'href' => "promotions.update?promotion_id={$_REQUEST['promotion_id']}&selected_section=details", 'js' => true), 'conditions' => array('title' => __('conditions'), 'href' => "promotions.update?promotion_id={$_REQUEST['promotion_id']}&selected_section=conditions", 'js' => true), 'bonuses' => array('title' => __('bonuses'), 'href' => "promotions.update?promotion_id={$_REQUEST['promotion_id']}&selected_section=bonuses", 'js' => true)));
    $promotion_data = fn_get_promotion_data($_REQUEST['promotion_id']);
    if (empty($promotion_data)) {
        return array(CONTROLLER_STATUS_NO_PAGE);
    }
    Registry::get('view')->assign('promotion_data', $promotion_data);
    Registry::get('view')->assign('zone', $promotion_data['zone']);
    Registry::get('view')->assign('schema', fn_promotion_get_schema());
    if (fn_allowed_for('ULTIMATE') && !Registry::get('runtime.company_id')) {
        Registry::get('view')->assign('picker_selected_companies', fn_ult_get_controller_shared_companies($_REQUEST['promotion_id']));
    }
    // Add promotion
} elseif ($mode == 'add') {
    $zone = !empty($_REQUEST['zone']) ? $_REQUEST['zone'] : 'catalog';
    if (fn_allowed_for('ULTIMATE:FREE') && $zone == 'cart') {
        return array(CONTROLLER_STATUS_NO_PAGE);
    }
예제 #3
0
function fn_get_affiliate_actions($params, $sorting = 'date DESC', $do_pagination = false, $page = 1, $lang_code = CART_LANGUAGE)
{
    $payout_types =& Registry::get('payout_types');
    // Define sort fields
    $sortings = array('action' => "actions.action", 'date' => "actions.date", 'cost' => "actions.amount", 'customer' => array("customers.lastname", "customers.firstname"), 'partner' => "partners.firstname", 'banner' => "?:aff_banner_descriptions.title", 'status' => "actions.payout_id, actions.approved");
    $directions = array('asc' => 'asc', 'desc' => 'desc');
    if (is_array($params)) {
        $condition = '1';
        if (!empty($params['action_id'])) {
            $condition .= db_quote(" AND actions.action_id = ?i", $params['action_id']);
        }
        if (!empty($params['object_type'])) {
            $condition .= db_quote(" AND alinks.object_type = ?s", $params['object_type']);
        }
        if (!empty($params['object_data'])) {
            $condition .= db_quote(" AND alinks.object_data = ?s", $params['object_data']);
        }
        if (!empty($params['payout_id'])) {
            $condition .= db_quote(" AND payout_id = ?i", $params['payout_id']);
        }
        if (empty($params['sort_order']) || empty($directions[$params['sort_order']])) {
            $params['sort_order'] = 'asc';
        }
        if (empty($params['sort_by']) || empty($sortings[$params['sort_by']])) {
            $params['sort_by'] = 'date';
        }
        $sorting = (is_array($sortings[$params['sort_by']]) ? implode(' ' . $directions[$params['sort_order']] . ', ', $sortings[$params['sort_by']]) : $sortings[$params['sort_by']]) . " " . $directions[$params['sort_order']];
    } else {
        $condition = $params;
        // FIXME
        if (is_array($sorting)) {
            $sorting = (is_array($sortings[$sorting['sort_by']]) ? implode(' ' . $directions[$sorting['sort_order']] . ', ', $sortings[$sorting['sort_by']]) : $sortings[$sorting['sort_by']]) . " " . $directions[$sorting['sort_order']];
        }
    }
    if ($do_pagination) {
        if (empty($page)) {
            $page = 1;
        }
        $cnt_list_stats = db_get_field("SELECT COUNT(*) FROM ?:aff_partner_actions as actions WHERE {$condition}");
        $limit = fn_paginate($page, $cnt_list_stats);
    } else {
        $limit = '';
    }
    $actions = db_get_hash_array("SELECT actions.*, alinks.object_data as parent_action_id, customers.firstname as customer_firstname, customers.lastname as customer_lastname, partners.firstname as partner_firstname, partners.lastname as partner_lastname, ?:common_descriptions.object as plan, ?:common_descriptions.description as plan_description, ?:aff_banners.type as banner_type, ?:aff_banner_descriptions.title as banner FROM ?:aff_partner_actions as actions LEFT JOIN ?:users as customers ON customers.user_id = actions.customer_id LEFT JOIN ?:users as partners ON partners.user_id = actions.partner_id LEFT JOIN ?:common_descriptions ON ?:common_descriptions.object_holder = 'affiliate_plans' AND ?:common_descriptions.object_id = actions.plan_id AND ?:common_descriptions.lang_code = ?s LEFT JOIN ?:aff_banners ON ?:aff_banners.banner_id = actions.banner_id LEFT JOIN ?:aff_banner_descriptions ON ?:aff_banner_descriptions.banner_id = actions.banner_id AND ?:aff_banner_descriptions.lang_code = ?s LEFT JOIN ?:aff_action_links as alinks ON alinks.action_id = actions.action_id AND alinks.object_type = 'A' WHERE ?p ORDER BY {$sorting} {$limit}", 'action_id', $lang_code, $lang_code, $condition);
    if (!empty($actions)) {
        $extra_data = array();
        foreach ($actions as $action_id => $action_data) {
            $action_data['data'] = db_get_hash_single_array("SELECT object_data, object_type FROM ?:aff_action_links WHERE action_id = ?i", array('object_type', 'object_data'), $action_id);
            $actions[$action_id]['data'] = $action_data['data'];
            if (!empty($action_data['data'])) {
                if (!empty($actions[$action_id]['data']['P'])) {
                    $actions[$action_id]['data']['product_name'] = fn_get_product_name($actions[$action_id]['data']['P'], $lang_code);
                }
                if (!empty($actions[$action_id]['data']['O'])) {
                    $tmp_order = fn_get_order_info($actions[$action_id]['data']['O']);
                    $actions[$action_id]['data']['order_status'] = $tmp_order['status'];
                }
                if (!empty($actions[$action_id]['data']['D'])) {
                    $actions[$action_id]['data']['coupon'] = fn_get_promotion_data($actions[$action_id]['data']['D']);
                }
            } else {
                $actions[$action_id]['data'] = array();
            }
            if (!empty($action_data['parent_action_id']) && empty($extra_data[$action_data['parent_action_id']])) {
                $extra_data[$action_data['parent_action_id']] = db_get_hash_array("SELECT actions.action_id, actions.action, actions.amount, actions.partner_id, alinks.object_data as tier, partners.firstname, partners.lastname FROM ?:aff_partner_actions as actions LEFT JOIN ?:aff_action_links as alinks ON alinks.action_id = actions.action_id AND alinks.object_type = 'L' LEFT JOIN ?:users as partners ON partners.user_id = actions.partner_id WHERE actions.action_id = ?i OR actions.action_id IN(SELECT links.action_id FROM ?:aff_action_links as links WHERE links.object_type = 'A' AND links.object_data = ?i) ORDER BY actions.action_id ASC", 'action_id', $action_data['parent_action_id'], $action_data['parent_action_id']);
                if (!empty($extra_data[$action_data['parent_action_id']])) {
                    foreach ($extra_data[$action_data['parent_action_id']] as $related_id => $related_data) {
                        $extra_data[$action_data['parent_action_id']][$related_id]['title'] = empty($payout_types[$related_data['action']]['title']) ? '' : fn_get_lang_var($payout_types[$related_data['action']]['title'], $lang_code);
                    }
                }
            }
            if (!empty($extra_data[$action_data['parent_action_id']])) {
                $actions[$action_id]['extra_data'] = $extra_data[$action_data['parent_action_id']];
            }
            $actions[$action_id]['title'] = empty($payout_types[$action_data['action']]['title']) ? '' : fn_get_lang_var($payout_types[$action_data['action']]['title'], $lang_code);
            if (!empty($actions[$action_id]['data']['L']) && !empty($actions[$action_id]['title'])) {
                $_level = strval($actions[$action_id]['data']['L']);
                $actions[$action_id]['title'] .= ' (' . $_level . ' ' . fn_get_lang_var('tier_account', $lang_code) . ')';
            }
        }
    }
    return empty($actions) ? false : $actions;
}
예제 #4
0
/**
 * Get promotion dynamic properties
 *
 * @param array $promotion_id promotion ID
 * @param array $promotion promotion condition
 * @param array $condition condition
 * @param array $cart cart
 * @param array $auth auth information
 * @return mixed
 */
function fn_promotion_get_dynamic($promotion_id, $promotion, $condition, &$cart, &$auth = NULL)
{
    if ($condition == 'number_of_usages') {
        $usages = db_get_field("SELECT number_of_usages FROM ?:promotions WHERE promotion_id = ?i", $promotion_id);
        return intval($usages) + 1;
    } elseif ($condition == 'once_per_customer') {
        fn_define('PROMOTION_MIN_MATCHES', 5);
        $order_statuses = fn_get_statuses(STATUSES_ORDER, false, true);
        $_statuses = array();
        foreach ($order_statuses as $v) {
            if ($v['inventory'] == 'D') {
                // decreasing (positive) status
                $_statuses[] = $v['status'];
            }
        }
        if (empty($cart['user_data'])) {
            return 'Y';
        }
        $udata = $cart['user_data'];
        fn_fill_user_fields($udata);
        $exists = db_get_field("SELECT ((firstname = ?s) + (lastname = ?s) + (b_city = ?s) + (b_state = ?s) + (b_country = ?s) + (b_zipcode = ?s) + (email = ?s) * 6) as r FROM ?:orders WHERE FIND_IN_SET(promotion_ids, ?i) AND status IN (?a) HAVING r >= ?i LIMIT 1", $udata['firstname'], $udata['lastname'], $udata['b_city'], $udata['b_state'], $udata['b_country'], $udata['b_zipcode'], $udata['email'], $promotion_id, $_statuses, PROMOTION_MIN_MATCHES);
        $promotion_data = fn_get_promotion_data($promotion_id);
        $coupon_exist = false;
        if (!empty($promotion_data['conditions']['conditions'])) {
            foreach ($promotion_data['conditions']['conditions'] as $val) {
                if ($val['condition'] == 'coupon_code') {
                    $coupon_exist = fn_promotion_validate_coupon($val, $cart);
                    if (!empty($coupon_exist) && !empty($exists)) {
                        fn_set_notification('E', fn_get_lang_var('error'), fn_get_lang_var('text_can_be_used_once'), false, 'error_coupon_already_used');
                    }
                    break;
                }
            }
        }
        if (!empty($exists)) {
            return 'N';
        }
        return 'Y';
        // this is checkbox with values (Y/N), so we need to return appropriate values
    }
}
예제 #5
0
/**
 * Get promotion dynamic properties
 *
 * @param array $promotion_id promotion ID
 * @param array $promotion promotion condition
 * @param array $condition condition
 * @param array $cart cart
 * @param array $auth auth information
 * @return mixed
 */
function fn_promotion_get_dynamic($promotion_id, $promotion, $condition, &$cart, &$auth = NULL)
{
    if ($condition == 'number_of_usages') {
        $usages = db_get_field("SELECT number_of_usages FROM ?:promotions WHERE promotion_id = ?i", $promotion_id);
        return intval($usages) + 1;
    } elseif ($condition == 'once_per_customer') {
        $exists = fn_promotion_check_existence($promotion_id, $cart);
        if (empty($cart['user_data'])) {
            return 'Y';
        }
        $promotion_data = fn_get_promotion_data($promotion_id);
        $coupon_exist = false;
        if (!empty($promotion_data['conditions']['conditions'])) {
            foreach ($promotion_data['conditions']['conditions'] as $val) {
                if ($val['condition'] == 'coupon_code') {
                    $coupon_exist = fn_promotion_validate_coupon($val, $cart);
                    if (!empty($coupon_exist) && $exists) {
                        fn_set_notification('W', __('warning'), __('text_can_be_used_once'), "K", 'error_coupon_already_used');
                        $_SESSION['promotion_notices']['promotion']['messages'][] = 'coupon_already_used';
                    }
                    break;
                }
            }
        }
        if ($exists) {
            return 'N';
        }
        return 'Y';
        // this is checkbox with values (Y/N), so we need to return appropriate values
    }
}