function fn_delete_affiliate_commissions($commission_ids, $plan_id) { $plan_data = fn_get_affiliate_plan_data($plan_id, DESCR_SL); if (!empty($plan_data)) { foreach ($commission_ids as $com_id) { unset($plan_data['commissions'][$com_id]); } return fn_update_affiliate_plan($plan_data, $plan_id, DESCR_SL); } return $plan_id; }
function fn_add_partner_action($action, $banner_id, $partner_id, $customer_id = '', $additional_data = '', $amount = '0', $multi_tier_account = false) { $auth =& $_SESSION['auth']; $payout_types =& Registry::get('payout_types'); if (empty($action) || empty($partner_id)) { return false; } if (!$multi_tier_account && AREA != 'A') { fn_set_partner_cookie($partner_id); } $data = array(); $data['banner_id'] = $banner_id; $data['partner_id'] = $partner_id; $data['customer_id'] = $customer_id; $data['date'] = TIME; $data['ip'] = $action == 'init_balance' ? '' : $_SERVER['REMOTE_ADDR']; $data['action'] = $action; $data['approved'] = Registry::get('addons.affiliate.automatic_approval_commissions') == 'Y' && $action != 'sale' ? 'Y' : 'N'; $data['plan_id'] = db_get_field("SELECT plan_id FROM ?:aff_partner_profiles WHERE user_id = ?i", $partner_id); if (empty($data['plan_id'])) { return false; } if ($multi_tier_account) { $data['amount'] = round($amount, 2); } elseif (!empty($payout_types[$action])) { $plan_data = fn_get_affiliate_plan_data($data['plan_id']); $payout_values = $plan_data['payout_types']; $p_value = false; if ($action == 'sale' && !empty($additional_data['P'])) { $p_value = fn_get_payout_value($data['plan_id'], $additional_data['P'], 'product'); if ($p_value === false) { $product_data = fn_get_product_data($additional_data['P'], $auth); if (!empty($product_data['main_category'])) { $p_value = fn_get_payout_value($data['plan_id'], $product_data['main_category'], 'category'); } } } elseif ($action == 'use_coupon' && !empty($additional_data['D'])) { $p_value = fn_get_payout_value($data['plan_id'], $additional_data['D'], 'promotion'); } if ($p_value === false) { if (empty($payout_values[$action]) || $action != 'init_balance' && (empty($payout_types[$action]['default']) || $payout_types[$action]['default'] != 'Y')) { $p_value = array('value' => 0, 'value_type' => 'A'); } else { $p_value = $payout_values[$action]; } } $data['amount'] = @$p_value['value_type'] == 'A' ? @$p_value['value'] : $amount * $p_value['value'] / 100; $data['amount'] = round($data['amount'], 2); } $data = fn_check_table_fields($data, 'aff_partner_actions'); $action_id = db_query("INSERT INTO ?:aff_partner_actions ?e", $data); if (!empty($action_id)) { if (Registry::get('addons.affiliate.automatic_approval_commissions') == 'Y' && $action != 'sale') { fn_update_partner_balance($data['partner_id'], $data['amount'], '+'); } if (!empty($additional_data) && is_array($additional_data)) { foreach ($additional_data as $object_type => $object_data) { $object_type = substr($object_type, 0, 1); $object_type = strtoupper($object_type); $_data = array('action_id' => $action_id, 'object_data' => $object_data, 'object_type' => $object_type); $_data = fn_check_table_fields($_data, 'aff_action_links'); db_query("INSERT INTO ?:aff_action_links ?e", $_data); } } if (!$multi_tier_account && $action != 'init_balance') { fn_add_commissions_to_multi_tier_affiliates($action, $data['partner_id'], array('commission' => $data['amount'], 'price' => round($amount, 2)), $action_id, $data['customer_id'], $data['banner_id'], $additional_data); } } return true; }
// FIXME: page $payouts = db_get_hash_array("SELECT ?:affiliate_payouts.*, ?:users.user_login, ?:users.firstname, ?:users.lastname FROM ?:affiliate_payouts LEFT JOIN ?:users ON ?:affiliate_payouts.partner_id=?:users.user_id WHERE ?p AND ?:users.user_id = ?i ORDER BY {$sorting} {$limit}", 'payout_id', $payout_search_condition, $auth['user_id']); $view->assign('payouts', $payouts); } elseif ($mode == 'update') { if (empty($_REQUEST['payout_id'])) { return array(CONTROLLER_STATUS_NO_PAGE); } else { $payout_data = db_get_row("SELECT * FROM ?:affiliate_payouts WHERE payout_id = ?i", $_REQUEST['payout_id']); if (empty($payout_data)) { return array(CONTROLLER_STATUS_NO_PAGE); } else { if (!empty($payout_data['partner_id'])) { $payout_data['partner'] = fn_get_partner_data($payout_data['partner_id']); } if (!empty($payout_data['partner']['plan_id'])) { $payout_data['plan'] = fn_get_affiliate_plan_data($payout_data['partner']['plan_id']); } $payout_data['actions'] = fn_get_affiliate_actions($_REQUEST); $payout_data['date_range']['min'] = db_get_field("SELECT MIN(date) FROM ?:aff_partner_actions WHERE payout_id = ?i", $_REQUEST['payout_id']); $payout_data['date_range']['max'] = db_get_field("SELECT MAX(date) FROM ?:aff_partner_actions WHERE payout_id = ?i", $_REQUEST['payout_id']); // [Breadcrumbs] fn_add_breadcrumb(fn_get_lang_var('payouts'), "payouts.manage"); // [/Breadcrumbs] $view->assign('affiliate_plan', fn_get_affiliate_plan_data_by_partner_id($auth['user_id'])); $view->assign('payouts', array($payout_data['partner_id'] => $payout_data)); } $view->assign('sort_order', empty($sort_order) || $sort_order == 'asc' ? 'desc' : 'asc'); $view->assign('sort_by', empty($sort_by) ? 'date' : $sort_by); } } /** /Body **/
function fn_add_payouts($params, $lang_code = CART_LANGUAGE) { // Init filter $params = fn_init_view('pay_affiliates', $params); // Set default values to input params $params['page'] = empty($params['page']) ? 1 : $params['page']; // default page is 1 // Define sort fields $sortings = array('action' => "actions.action", 'date' => "actions.date", 'cost' => "actions.amount", 'banner' => "banner"); $directions = array('asc' => 'asc', 'desc' => 'desc'); if (empty($params['sort_order']) || empty($directions[$params['sort_order']])) { $params['sort_order'] = 'desc'; } 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']]; // Reverse sorting (for usage in view) $params['sort_order'] = $params['sort_order'] == 'asc' ? 'desc' : 'asc'; $new_payouts = array(); if (!empty($params['partner_ids'])) { $date_condition = ""; if (!empty($params['last_payout']) && !empty($params['time_from'])) { $date_condition = db_quote(" AND date < ?i", $params['time_from']); } foreach ($params['partner_ids'] as $_partner_id) { $condition = "1 " . $date_condition . db_quote(" AND approved = 'Y' AND payout_id = 0 AND partner_id = ?i", $_partner_id); $new_payouts[$_partner_id]['amount'] = db_get_field("SELECT SUM(amount) FROM ?:aff_partner_actions WHERE {$condition}"); if (empty($new_payouts[$_partner_id]['amount'])) { unset($new_payouts[$_partner_id]); } else { $new_payouts[$_partner_id]['partner'] = fn_get_partner_data($_partner_id); if (!empty($new_payouts[$_partner_id]['partner']['plan_id'])) { $new_payouts[$_partner_id]['plan'] = fn_get_affiliate_plan_data($new_payouts[$_partner_id]['partner']['plan_id'], $lang_code); } $_params = array('sort_by' => $params['sort_by'], 'sort_order' => $params['sort_order'] == 'asc' ? 'desc' : 'asc'); $new_payouts[$_partner_id]['actions'] = fn_get_affiliate_actions($condition, $_params, true, @$params['page']); $new_payouts[$_partner_id]['date_range']['min'] = db_get_field("SELECT MIN(date) FROM ?:aff_partner_actions WHERE {$condition}"); $new_payouts[$_partner_id]['date_range']['max'] = db_get_field("SELECT MAX(date) FROM ?:aff_partner_actions WHERE {$condition}"); } } } return array($new_payouts, $params); }