//清理购物车 $db->autoDelete('cart', '`business_account`=\'' . $cart['business_account'] . '\' and `account`=\'' . $_SESSION['account'] . '\''); $db->commit(); $response['count']++; $response['error'] = 0; $_SESSION['order_sn'] = $order_sn; //订单结算 if ($status == 4) { $log->record('计算三级分销'); //计算三级分销 $log->record('计算分销奖金:' . $cart['total_reward']); distribution_settle($cart['total_reward'], $user_info['path'], $order_sn); //计算赠送积分 if ($total_integral_given) { add_memeber_exchange_log($_SESSION['account'], 0, 0, 0, $cart['total_integral_given'], 0, 'settle', '系统结算'); add_member_reward($_SESSION['account'], 0, $cart['total_integral_given'], '订单' . $order_sn . '赠送积分'); } //计入商家收入 $business_income = $cart['total_product_amount'] + $cart['total_delivery_fee'] - $cart['total_reward']; if (add_business_exchange($cart['business_account'], 0, $business_income, $_SESSION['account'], $order_sn . '支付成功')) { add_business_trade($cart['business_account'], $business_income, $order_sn); } else { //增加商家收入失败 $log->record($order_sn . '计入商家收入失败'); } //如果会员购买了activity=4的产品,则升级 $check_can_levelup = 'select am.`activity_id` from ' . $db->table('activity_mapper') . ' as am left join ' . $db->table('order_detail') . ' using (`product_sn`) where `order_sn`=\'' . $order_sn . '\' and `activity_id`=4'; if ($db->fetchOne($check_can_levelup)) { $member_data = array('level_id' => 1); $db->autoUpdate('member', $member_data, '`account`=\'' . $_SESSION['account'] . '\''); }
/** * 三级分销结算 * @param float $reward * @param string $path * @return bool */ function distribution_settle($reward, $path, $order_sn = '') { global $db; global $config; global $log; $log->record('结算开始'); $ids_arr = explode(',', $path); array_pop($ids_arr); $current_id = array_pop($ids_arr); /* * $level = array( * 0 => '消费者', * 1 => '合伙人', * 2 => '高级合伙人' * ); */ $get_account = 'select `account`,`level_id` from ' . $db->table('member') . ' where `id`=' . $current_id; $member = $db->fetchRow($get_account); $refund = $reward * $config['refund_rate']; //合伙人以上级别享受返利 if ($member['level_id'] > 0 && $refund) { add_memeber_exchange_log($member['account'], 0, 0, 0, 0, $refund, 'settle', $order_sn . '合伙人返利'); add_member_reward($member['account'], $refund, 0, $order_sn, $order_sn); $refund = 0; } $ids_arr_str = implode(',', $ids_arr); //获取合伙人 $get_senior_member = 'select `id`,`account`,`level_id` from ' . $db->table('member') . ' where `id` in (' . $ids_arr_str . ') and `level_id`>=1 order by `path` DESC'; $senior_member = $db->fetchAll($get_senior_member); while (count($ids_arr) > 3) { array_shift($ids_arr); } //三级分销部分 $level = 1; while ($id = array_pop($ids_arr)) { $reward_tmp = $reward * $config['level_' . $level]; $get_account = 'select `account` from ' . $db->table('member') . ' where `id`=' . $id; $account = $db->fetchOne($get_account); if ($reward_tmp) { add_memeber_exchange_log($account, 0, 0, 0, 0, $reward_tmp, 'settle', $order_sn . '推广奖'); add_member_reward($account, $reward_tmp, 0, $order_sn); } $level++; } //这种计算方法仅适用于子节点的id大于父节点 //团队奖部分 if ($senior_member) { foreach ($senior_member as $member) { //团队奖 if ($member['level_id'] > 1) { $reward_tmp = $reward * $config['group_prize_rate']; if ($reward_tmp) { add_memeber_exchange_log($member['account'], 0, 0, 0, 0, $reward_tmp, 'settle', $order_sn . '团队奖'); add_member_reward($member['account'], $reward_tmp, 0, $order_sn); } } //返利 if ($refund) { add_memeber_exchange_log($member['account'], 0, 0, 0, 0, $refund, 'settle', $order_sn . '合伙人返利'); add_member_reward($member['account'], $refund, 0, $order_sn); $refund = 0; } } } $log->record('结算结束'); return true; }