function order_paid_done($order_id) { //处理支付成功后的操作 /** * 1. 发货 * 2. 超量发货的存到会员中心 * 3. 发券 * 4. 发放抽奖 */ require_once APP_ROOT_PATH . "system/libs/deal.php"; $order_id = intval($order_id); $stock_status = true; //团购状态 $order_info = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "deal_order where id = " . $order_id); if ($order_info['type'] == 0) { //首先验证所有的规格库存 $order_goods_list = $GLOBALS['db']->getAll("select * from " . DB_PREFIX . "deal_order_item where order_id = " . $order_id); foreach ($order_goods_list as $k => $v) { if ($GLOBALS['db']->getOne("select count(*) from " . DB_PREFIX . "attr_stock where deal_id = " . $v['deal_id'] . " and locate(attr_str,'" . $v['attr_str'] . "') > 0")) { $sql = "update " . DB_PREFIX . "attr_stock set buy_count = buy_count + " . $v['number'] . " where deal_id = " . $v['deal_id'] . " and ((buy_count + " . $v['number'] . " <= stock_cfg) or stock_cfg = 0 )" . " and locate(attr_str,'" . $v['attr_str'] . "') > 0 "; $GLOBALS['db']->query($sql); //增加商品的发货量 $rs = $GLOBALS['db']->affected_rows(); if ($rs) { $affect_attr_list[] = $v; } else { $stock_status = false; break; } } } if ($stock_status) { $goods_list = $GLOBALS['db']->getAll("select deal_id,sum(number) as num from " . DB_PREFIX . "deal_order_item where order_id = " . $order_id . " group by deal_id"); foreach ($goods_list as $k => $v) { $sql = "update " . DB_PREFIX . "deal set buy_count = buy_count + " . $v['num'] . ",user_count = user_count + 1 where id=" . $v['deal_id'] . " and ((buy_count + " . $v['num'] . "<= max_bought) or max_bought = 0) " . " and time_status = 1 and buy_status <> 2"; $GLOBALS['db']->query($sql); //增加商品的发货量 $rs = $GLOBALS['db']->affected_rows(); if ($rs) { $affect_list[] = $v; //记录下更新成功的团购商品,用于回滚 } else { //失败成功,即过期支付,超量支付 $stock_status = false; break; } } } if ($stock_status) { //发货成功,发券 foreach ($goods_list as $k => $v) { //为相应团购发券 $deal_info = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "deal where id = " . intval($v['deal_id'])); if ($deal_info['is_coupon'] == 1) { if ($deal_info['deal_type'] == 1) { $deal_order_item_list = $GLOBALS['db']->getAll("select * from " . DB_PREFIX . "deal_order_item where order_id = " . $order_info['id'] . " and deal_id = " . $v['deal_id']); foreach ($deal_order_item_list as $item) { // for($i=0;$i<$item['number'];$i++) //按单 // { //需要发券 /** * 1. 先从已有团购券中发送 * 2. 无有未发送的券,自动发送 * 3. 发送状态的is_valid 都是 0, 该状态的激活在syn_deal_status中处理 */ /*修正后台手动建团购劵,购买的时候按单发送团购劵,数量不一致*/ $sql = "update " . DB_PREFIX . "deal_coupon set user_id=" . $order_info['user_id'] . ",order_id = " . $order_info['id'] . ",order_deal_id = " . $item['id'] . ",expire_refund = " . $deal_info['expire_refund'] . ",any_refund = " . $deal_info['any_refund'] . ",coupon_price = " . $item['total_price'] . ",coupon_score = " . $item['return_total_score'] . ",coupon_money = " . $item['return_total_money'] . ",deal_type = " . $deal_info['deal_type'] . " where deal_id = " . $v['deal_id'] . " and user_id = 0 " . " and is_delete = 0 order by id ASC limit 1"; $GLOBALS['db']->query($sql); $exist_coupon = $GLOBALS['db']->affected_rows(); if (!$exist_coupon) { //未发送成功,即无可发放的预设团购券 add_coupon($v['deal_id'], $order_info['user_id'], 0, '', '', 0, 0, $item['id'], $order_info['id']); } // } } } else { $deal_order_item_list = $GLOBALS['db']->getAll("select * from " . DB_PREFIX . "deal_order_item where order_id = " . $order_info['id'] . " and deal_id = " . $v['deal_id']); foreach ($deal_order_item_list as $item) { for ($i = 0; $i < $item['number']; $i++) { //需要发券 /** * 1. 先从已有团购券中发送 * 2. 无有未发送的券,自动发送 * 3. 发送状态的is_valid 都是 0, 该状态的激活在syn_deal_status中处理 */ $sql = "update " . DB_PREFIX . "deal_coupon set user_id=" . $order_info['user_id'] . ",order_id = " . $order_info['id'] . ",order_deal_id = " . $item['id'] . ",expire_refund = " . $deal_info['expire_refund'] . ",any_refund = " . $deal_info['any_refund'] . ",coupon_price = " . $item['unit_price'] . ",coupon_score = " . $item['return_score'] . ",coupon_money = " . $item['return_money'] . ",deal_type = " . $deal_info['deal_type'] . " where deal_id = " . $v['deal_id'] . " and user_id = 0 " . " and is_delete = 0 limit 1"; $GLOBALS['db']->query($sql); $exist_coupon = $GLOBALS['db']->affected_rows(); if (!$exist_coupon) { //未发送成功,即无可发放的预设团购券 add_coupon($v['deal_id'], $order_info['user_id'], 0, '', '', 0, 0, $item['id'], $order_info['id']); } } } } } //发券结束 } //开始处理返还的积分或现金 require_once APP_ROOT_PATH . "system/libs/user.php"; if ($order_info['return_total_money'] != 0) { $msg = sprintf($GLOBALS['lang']['ORDER_RETURN_MONEY'], $order_info['order_sn']); modify_account(array('money' => $order_info['return_total_money'], 'score' => 0), $order_info['user_id'], $msg); } if ($order_info['return_total_score'] != 0) { $msg = sprintf($GLOBALS['lang']['ORDER_RETURN_SCORE'], $order_info['order_sn']); modify_account(array('money' => 0, 'score' => $order_info['return_total_score']), $order_info['user_id'], $msg); send_score_sms($order_info['id']); send_score_mail($order_info['id']); } //开始处理返利,只创建返利, 发放将与msg_list的自动运行一起执行 $user_info = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "user where id = " . $order_info['user_id']); //开始查询所购买的列表中支不支持促销 $is_referrals = 1; //默认为返利 foreach ($goods_list as $k => $v) { $is_referrals = $GLOBALS['db']->getOne("select is_referral from " . DB_PREFIX . "deal where id = " . $v['deal_id']); if ($is_referrals == 0) { break; } } if ($user_info['referral_count'] < app_conf("REFERRAL_LIMIT") && $is_referrals == 1) { //开始返利给推荐人 $parent_info = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "user where id = " . $user_info['pid']); if ($parent_info) { if (app_conf("REFERRAL_IP_LIMIT") == 1 && $parent_info['login_ip'] != get_client_ip() || app_conf("REFERRAL_IP_LIMIT") == 0) { if (app_conf("INVITE_REFERRALS_TYPE") == 0) { $referral_data['user_id'] = $parent_info['id']; //初返利的会员ID $referral_data['rel_user_id'] = $user_info['id']; //被推荐且发生购买的会员ID $referral_data['create_time'] = get_gmtime(); $referral_data['money'] = app_conf("INVITE_REFERRALS"); $referral_data['order_id'] = $order_info['id']; $GLOBALS['db']->autoExecute(DB_PREFIX . "referrals", $referral_data); //插入 } else { $referral_data['user_id'] = $parent_info['id']; //初返利的会员ID $referral_data['rel_user_id'] = $user_info['id']; //被推荐且发生购买的会员ID $referral_data['create_time'] = get_gmtime(); $referral_data['score'] = app_conf("INVITE_REFERRALS"); $referral_data['order_id'] = $order_info['id']; $GLOBALS['db']->autoExecute(DB_PREFIX . "referrals", $referral_data); //插入 } $GLOBALS['db']->query("update " . DB_PREFIX . "user set referral_count = referral_count + 1 where id = " . $user_info['id']); } } } //超出充值 if ($order_info['pay_amount'] > $order_info['total_price']) { require_once APP_ROOT_PATH . "system/libs/user.php"; if ($order_info['total_price'] < 0) { $msg = sprintf($GLOBALS['lang']['MONEYORDER_INCHARGE'], $order_info['order_sn']); } else { $msg = sprintf($GLOBALS['lang']['OUTOFMONEY_INCHARGE'], $order_info['order_sn']); } $refund_money = $order_info['pay_amount'] - $order_info['total_price']; if ($order_info['account_money'] > $refund_money) { $account_money_now = $order_info['account_money'] - $refund_money; } else { $account_money_now = 0; } $GLOBALS['db']->query("update " . DB_PREFIX . "deal_order set account_money = " . $account_money_now . " where id = " . $order_info['id']); if ($order_info['ecv_money'] > $refund_money) { $ecv_money_now = $order_info['ecv_money'] - $refund_money; } else { $ecv_money_now = 0; } $GLOBALS['db']->query("update " . DB_PREFIX . "deal_order set ecv_money = " . $ecv_money_now . " where id = " . $order_info['id']); modify_account(array('money' => $order_info['pay_amount'] - $order_info['total_price'], 'score' => 0), $order_info['user_id'], $msg); } //生成抽奖 $lottery_list = $GLOBALS['db']->getAll("select d.id as did,doi.number from " . DB_PREFIX . "deal_order_item as doi left join " . DB_PREFIX . "deal_order as do on doi.order_id = do.id left join " . DB_PREFIX . "deal as d on doi.deal_id = d.id where d.is_lottery = 1 and do.id = " . $order_info['id']); $lottery_user = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "user where id = " . intval($order_info['user_id'])); //如为首次抽奖,先为推荐人生成抽奖号 $lottery_count = $GLOBALS['db']->getOne("select count(*) from " . DB_PREFIX . "lottery where user_id = " . intval($order_info['user_id'])); if ($lottery_count == 0 && $lottery_user['pid'] != 0) { $lottery_puser = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "user where id = " . intval($lottery_user['pid'])); foreach ($lottery_list as $lottery) { $k = 0; do { if ($k > 10) { break; } $buy_count = $GLOBALS['db']->getOne("select buy_count from " . DB_PREFIX . "deal where id = " . $lottery['did']); $max_sn = $buy_count - $lottery['number'] + intval($GLOBALS['db']->getOne("select count(*) from " . DB_PREFIX . "lottery where deal_id = " . intval($lottery['did']) . " and buyer_id <> 0 ")); //$max_sn = intval($GLOBALS['db']->getOne("select lottery_sn from ".DB_PREFIX."lottery where deal_id = '".$lottery['did']."' order by lottery_sn desc limit 1")); $sn = $max_sn + 1; $sn = str_pad($sn, "6", "0", STR_PAD_LEFT); $sql = "insert into " . DB_PREFIX . "lottery (`lottery_sn`,`deal_id`,`user_id`,`mobile`,`create_time`,`buyer_id`) select '" . $sn . "','" . $lottery['did'] . "'," . $lottery_puser['id'] . ",'" . $lottery_puser['lottery_mobile'] . "'," . get_gmtime() . "," . $order_info['user_id'] . " from dual where not exists( select * from " . DB_PREFIX . "lottery where deal_id = " . $lottery['did'] . " and lottery_sn = '" . $sn . "')"; $GLOBALS['db']->query($sql); send_lottery_sms(intval($GLOBALS['db']->insert_id())); $k++; } while (intval($GLOBALS['db']->insert_id()) == 0); } } foreach ($lottery_list as $lottery) { for ($i = 0; $i < $lottery['number']; $i++) { $k = 0; do { if ($k > 10) { break; } $buy_count = $GLOBALS['db']->getOne("select buy_count from " . DB_PREFIX . "deal where id = " . $lottery['did']); $max_sn = $buy_count - $lottery['number'] + intval($GLOBALS['db']->getOne("select count(*) from " . DB_PREFIX . "lottery where deal_id = " . intval($lottery['did']) . " and buyer_id <> 0 ")); //$max_sn = intval($GLOBALS['db']->getOne("select lottery_sn from ".DB_PREFIX."lottery where deal_id = '".$lottery['did']."' order by lottery_sn desc limit 1")); $sn = $max_sn + $i + 1; $sn = str_pad($sn, "6", "0", STR_PAD_LEFT); $sql = "insert into " . DB_PREFIX . "lottery (`lottery_sn`,`deal_id`,`user_id`,`mobile`,`create_time`,`buyer_id`) select '" . $sn . "','" . $lottery['did'] . "'," . $order_info['user_id'] . "," . $lottery_user['lottery_mobile'] . "," . get_gmtime() . ",0 from dual where not exists( select * from " . DB_PREFIX . "lottery where deal_id = " . $lottery['did'] . " and lottery_sn = '" . $sn . "')"; $GLOBALS['db']->query($sql); send_lottery_sms(intval($GLOBALS['db']->insert_id())); $k++; } while (intval($GLOBALS['db']->insert_id()) == 0); } } } else { //开始模拟事务回滚 foreach ($affect_attr_list as $k => $v) { $sql = "update " . DB_PREFIX . "attr_stock set buy_count = buy_count - " . $v['number'] . " where deal_id = " . $v['deal_id'] . " and locate(attr_str,'" . $v['attr_str'] . "') > 0 "; $GLOBALS['db']->query($sql); //回滚已发的货量 } foreach ($affect_list as $k => $v) { $sql = "update " . DB_PREFIX . "deal set buy_count = buy_count - " . $v['num'] . ",user_count = user_count - 1 where id=" . $v['deal_id']; $GLOBALS['db']->query($sql); //回滚已发的货量 } //超出充值 require_once APP_ROOT_PATH . "system/libs/user.php"; $msg = sprintf($GLOBALS['lang']['OUTOFSTOCK_INCHARGE'], $order_info['order_sn']); modify_account(array('money' => $order_info['total_price'], 'score' => 0), $order_info['user_id'], $msg); //将订单的extra_status 状态更新为2,并自动退款,结单 $GLOBALS['db']->query("update " . DB_PREFIX . "deal_order set extra_status = 2, after_sale = 1, refund_money = pay_amount, order_status = 1 where id = " . intval($order_info['id'])); //记录退款的订单日志 $log['log_info'] = $msg; $log['log_time'] = get_gmtime(); $log['order_id'] = intval($order_info['id']); $GLOBALS['db']->autoExecute(DB_PREFIX . "deal_order_log", $log); } //同步所有未过期的团购状态 foreach ($goods_list as $item) { syn_deal_status($item['deal_id'], true); } } else { //订单充值 $GLOBALS['db']->query("update " . DB_PREFIX . "deal_order set order_status = 1 where id = " . $order_info['id']); //充值单自动结单 require_once APP_ROOT_PATH . "system/libs/user.php"; $msg = sprintf($GLOBALS['lang']['USER_INCHARGE_DONE'], $order_info['order_sn']); modify_account(array('money' => $order_info['total_price'] - $order_info['payment_fee'], 'score' => 0), $order_info['user_id'], $msg); } }
function order_paid_done($order_id) { //处理支付成功后的操作 /** * 1. 发货 * 2. 超量发货的存到会员中心 * 3. 发券 * 4. 发放抽奖 */ require_once APP_ROOT_PATH . "system/model/deal.php"; require_once APP_ROOT_PATH . "system/model/supplier.php"; require_once APP_ROOT_PATH . "system/model/deal_order.php"; $order_id = intval($order_id); $stock_status = true; //团购状态 $order_info = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "deal_order where id = " . $order_id); if ($order_info['type'] == 0) { //首先验证所有的规格库存 $order_goods_list = $GLOBALS['db']->getAll("select * from " . DB_PREFIX . "deal_order_item where order_id = " . $order_id); foreach ($order_goods_list as $k => $v) { if ($GLOBALS['db']->getOne("select count(*) from " . DB_PREFIX . "attr_stock where deal_id = " . $v['deal_id'] . " and locate(attr_str,'" . $v['attr_str'] . "') > 0")) { $sql = "update " . DB_PREFIX . "attr_stock set buy_count = buy_count + " . $v['number'] . " where deal_id = " . $v['deal_id'] . " and ((buy_count + " . $v['number'] . " <= stock_cfg) or stock_cfg = 0 )" . " and locate(attr_str,'" . $v['attr_str'] . "') > 0 "; $GLOBALS['db']->query($sql); //增加商品的发货量 $rs = $GLOBALS['db']->affected_rows(); if ($rs) { $affect_attr_list[] = $v; } else { $stock_status = false; break; } } } if ($stock_status) { $goods_list = $GLOBALS['db']->getAll("select buy_type,deal_id,sum(number) as num,sum(add_balance_price_total) as add_balance_price_total,sum(balance_total_price) as balance_total_price,sum(return_total_money) as return_total_money,sum(return_total_score) as return_total_score from " . DB_PREFIX . "deal_order_item where order_id = " . $order_id . " group by deal_id"); foreach ($goods_list as $k => $v) { $sql = "update " . DB_PREFIX . "deal set buy_count = buy_count + " . $v['num'] . ",user_count = user_count + 1 where id=" . $v['deal_id'] . " and ((buy_count + " . $v['num'] . "<= max_bought) or max_bought = 0) " . " and time_status = 1 and buy_status <> 2"; $GLOBALS['db']->query($sql); //增加商品的发货量 $rs = $GLOBALS['db']->affected_rows(); if ($rs) { $affect_list[] = $v; //记录下更新成功的团购商品,用于回滚 } else { //失败成功,即过期支付,超量支付 $stock_status = false; break; } } } $return_money = 0; //非发券非配送的即时返还 $return_score = 0; //非发券非配送的即时返还 $use_score = 0; //积分商品所耗费的积分 if ($stock_status) { foreach ($goods_list as $k => $v) { $deal_info = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "deal where id = " . intval($v['deal_id'])); //统计商户销售额 $supplier_log = "ID:" . $deal_info['id'] . " " . $deal_info['sub_name'] . " 订单:" . $order_info['order_sn']; modify_supplier_account($v['balance_total_price'] + $v['add_balance_price_total'], $deal_info['supplier_id'], 0, $supplier_log); if ($deal_info['is_coupon'] == 0 && $deal_info['is_delivery'] == 0) { //更新订单中相关产品的消费状态 $GLOBALS['db']->query("update " . DB_PREFIX . "deal_order_item set consume_count = consume_count + 1 where order_id = " . $order_info['id'] . " and deal_id = " . $deal_info['id']); update_order_cache($order_info['id']); distribute_order($order_info['id']); modify_supplier_account($v['balance_total_price'] + $v['add_balance_price_total'], $deal_info['supplier_id'], 2, $supplier_log); //等结算金额增加 } else { modify_supplier_account($v['balance_total_price'] + $v['add_balance_price_total'], $deal_info['supplier_id'], 1, $supplier_log); //冻结资金 } //不发货不发券的实时返还 if ($deal_info['is_coupon'] == 0 && $deal_info['is_delivery'] == 0 && $v['buy_type'] == 0) { $return_money += $v['return_total_money']; $return_score += $v['return_total_score']; } if ($v['buy_type'] == 1) { $use_score += $v['return_total_score']; } $balance_price += $v['balance_total_price']; $add_balance_price += $v['add_balance_price_total']; //发券 if ($deal_info['is_coupon'] == 1) { if ($deal_info['deal_type'] == 1) { $deal_order_item_list = $GLOBALS['db']->getAll("select * from " . DB_PREFIX . "deal_order_item where order_id = " . $order_info['id'] . " and deal_id = " . $v['deal_id']); foreach ($deal_order_item_list as $item) { // for($i=0;$i<$item['number'];$i++) //按单 // { //需要发券 /** * 1. 先从已有团购券中发送 * 2. 无有未发送的券,自动发送 * 3. 发送状态的is_valid 都是 0, 该状态的激活在syn_deal_status中处理 */ /*修正后台手动建团购劵,购买的时候按单发送团购劵,数量不一致*/ $sql = "update " . DB_PREFIX . "deal_coupon set user_id=" . $order_info['user_id'] . ",order_id = " . $order_info['id'] . ",order_deal_id = " . $item['id'] . ",expire_refund = " . $deal_info['expire_refund'] . ",any_refund = " . $deal_info['any_refund'] . ",coupon_price = " . $item['total_price'] . ",coupon_score = " . $item['return_total_score'] . ",coupon_money = " . $item['return_total_money'] . ",add_balance_price = " . $item['add_balance_price'] . ",deal_type = " . $deal_info['deal_type'] . ",balance_price = " . $item['balance_total_price'] . " where deal_id = " . $v['deal_id'] . " and user_id = 0 " . " and is_delete = 0 order by id ASC limit 1"; $GLOBALS['db']->query($sql); $exist_coupon = $GLOBALS['db']->affected_rows(); if (!$exist_coupon) { //未发送成功,即无可发放的预设团购券 add_coupon($v['deal_id'], $order_info['user_id'], 0, '', '', 0, 0, $item['id'], $order_info['id']); } // } } } else { $deal_order_item_list = $GLOBALS['db']->getAll("select * from " . DB_PREFIX . "deal_order_item where order_id = " . $order_info['id'] . " and deal_id = " . $v['deal_id']); foreach ($deal_order_item_list as $item) { for ($i = 0; $i < $item['number']; $i++) { //需要发券 /** * 1. 先从已有团购券中发送 * 2. 无有未发送的券,自动发送 * 3. 发送状态的is_valid 都是 0, 该状态的激活在syn_deal_status中处理 */ $sql = "update " . DB_PREFIX . "deal_coupon set user_id=" . $order_info['user_id'] . ",order_id = " . $order_info['id'] . ",order_deal_id = " . $item['id'] . ",expire_refund = " . $deal_info['expire_refund'] . ",any_refund = " . $deal_info['any_refund'] . ",coupon_price = " . $item['unit_price'] . ",coupon_score = " . $item['return_score'] . ",coupon_money = " . $item['return_money'] . ",add_balance_price = " . $item['add_balance_price'] . ",deal_type = " . $deal_info['deal_type'] . ",balance_price = " . $item['balance_unit_price'] . " where deal_id = " . $v['deal_id'] . " and user_id = 0 " . " and is_delete = 0 limit 1"; $GLOBALS['db']->query($sql); $exist_coupon = $GLOBALS['db']->affected_rows(); if (!$exist_coupon) { //未发送成功,即无可发放的预设团购券 add_coupon($v['deal_id'], $order_info['user_id'], 0, '', '', 0, 0, $item['id'], $order_info['id']); } } } } } //发券结束 } //开始处理返还的积分或现金,此处返还不用发货不用配送不用发券的产品返还 require_once APP_ROOT_PATH . "system/model/user.php"; if ($return_money != 0) { $msg = sprintf($GLOBALS['lang']['ORDER_RETURN_MONEY'], $order_info['order_sn']); modify_account(array('money' => $return_money, 'score' => 0), $order_info['user_id'], $msg); } if ($return_score != 0) { $msg = sprintf($GLOBALS['lang']['ORDER_RETURN_SCORE'], $order_info['order_sn']); modify_account(array('money' => 0, 'score' => $return_score), $order_info['user_id'], $msg); send_score_sms($order_info['id']); send_score_mail($order_info['id']); } if ($use_score != 0) { $user_score = $GLOBALS['db']->getOne("select score from " . DB_PREFIX . "user where id = " . $order_info['user_id']); if ($user_score + $use_score < 0) { //积分不足,不能支付 $msg = $order_info['order_sn'] . "订单支付失败,积分不足"; $refund_money = $order_info['pay_amount']; if ($order_info['account_money'] > $refund_money) { $account_money_now = $order_info['account_money'] - $refund_money; } else { $account_money_now = 0; } $GLOBALS['db']->query("update " . DB_PREFIX . "deal_order set account_money = " . $account_money_now . " where id = " . $order_info['id']); if ($order_info['ecv_money'] > $refund_money) { $ecv_money_now = $order_info['ecv_money'] - $refund_money; } else { $ecv_money_now = 0; } $GLOBALS['db']->query("update " . DB_PREFIX . "deal_order set ecv_money = " . $ecv_money_now . " where id = " . $order_info['id']); if ($refund_money > 0) { modify_account(array('money' => $refund_money, 'score' => 0), $order_info['user_id'], $msg); $GLOBALS['db']->query("update " . DB_PREFIX . "deal_order set refund_money = refund_money + " . $refund_money . ",refund_amount = refund_amount + " . $refund_money . ",after_sale = 1,refund_status = 2 where id = " . $order_info['id']); $GLOBALS['db']->query("update " . DB_PREFIX . "deal_order_item set refund_status = 2 where order_id = " . $order_info['id']); order_log($order_info['order_sn'] . "积分不足," . format_price($refund_money) . "已退到会员余额", $order_info['id']); modify_statements("-" . $refund_money, 1, $order_info['order_sn'] . "积分不足,退款"); modify_statements($refund_money, 2, $order_info['order_sn'] . "积分不足,退款"); } else { order_log($order_info['order_sn'] . "积分不足", $order_info['id']); } require_once APP_ROOT_PATH . "system/model/deal_order.php"; update_order_cache($order_info['id']); over_order($order_info['id']); } else { modify_account(array('score' => $use_score), $order_info['user_id'], "积分商品兑换"); send_score_sms($order_info['id']); send_score_mail($order_info['id']); order_log($order_info['order_sn'] . "积分订单支付成功", $order_info['id']); send_msg($order_info['user_id'], "订单" . $order_info['order_sn'] . "兑换成功", "orderitem", $order_goods_list[0]['id']); if ($order_info['total_price'] > 0) { modify_statements($order_info['total_price'], 8, $order_info['order_sn'] . "订单成功付款"); } //增加营业额 } } else { //开始处理返利,只创建返利, 发放将与msg_list的自动运行一起执行 // $user_info = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."user where id = ".$order_info['user_id']); // //开始查询所购买的列表中支不支持促销 // $is_referrals = 1; //默认为返利 // foreach($goods_list as $k=>$v) // { // $is_referrals = $GLOBALS['db']->getOne("select is_referral from ".DB_PREFIX."deal where id = ".$v['deal_id']); // if($is_referrals == 0) // { // break; // } // } // if($user_info['referral_count']<app_conf("REFERRAL_LIMIT")&&$is_referrals == 1) // { // //开始返利给推荐人 // $parent_info = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."user where id = ".$user_info['pid']); // if($parent_info) // { // if((app_conf("REFERRAL_IP_LIMIT")==1&&$parent_info['login_ip']!=CLIENT_IP)||app_conf("REFERRAL_IP_LIMIT")==0) //IP限制 // { // if(app_conf("INVITE_REFERRALS_TYPE")==0) //现金返利 // { // $referral_data['user_id'] = $parent_info['id']; //初返利的会员ID // $referral_data['rel_user_id'] = $user_info['id']; //被推荐且发生购买的会员ID // $referral_data['create_time'] = NOW_TIME; // $referral_data['money'] = app_conf("INVITE_REFERRALS"); // $referral_data['order_id'] = $order_info['id']; // $GLOBALS['db']->autoExecute(DB_PREFIX."referrals",$referral_data); //插入 // } // else // { // $referral_data['user_id'] = $parent_info['id']; //初返利的会员ID // $referral_data['rel_user_id'] = $user_info['id']; //被推荐且发生购买的会员ID // $referral_data['create_time'] = NOW_TIME; // $referral_data['score'] = app_conf("INVITE_REFERRALS"); // $referral_data['order_id'] = $order_info['id']; // $GLOBALS['db']->autoExecute(DB_PREFIX."referrals",$referral_data); //插入 // } // $GLOBALS['db']->query("update ".DB_PREFIX."user set referral_count = referral_count + 1 where id = ".$user_info['id']); // } // } // } //超出充值 if ($order_info['pay_amount'] > $order_info['total_price']) { require_once APP_ROOT_PATH . "system/model/user.php"; if ($order_info['total_price'] < 0) { $msg = sprintf($GLOBALS['lang']['MONEYORDER_INCHARGE'], $order_info['order_sn']); } else { $msg = sprintf($GLOBALS['lang']['OUTOFMONEY_INCHARGE'], $order_info['order_sn']); } $refund_money = $order_info['pay_amount'] - $order_info['total_price']; if ($order_info['account_money'] > $refund_money) { $account_money_now = $order_info['account_money'] - $refund_money; } else { $account_money_now = 0; } $GLOBALS['db']->query("update " . DB_PREFIX . "deal_order set account_money = " . $account_money_now . " where id = " . $order_info['id']); if ($order_info['ecv_money'] > $refund_money) { $ecv_money_now = $order_info['ecv_money'] - $refund_money; } else { $ecv_money_now = 0; } $GLOBALS['db']->query("update " . DB_PREFIX . "deal_order set ecv_money = " . $ecv_money_now . " where id = " . $order_info['id']); modify_account(array('money' => $refund_money, 'score' => 0), $order_info['user_id'], $msg); $GLOBALS['db']->query("update " . DB_PREFIX . "deal_order set refund_money = refund_money + " . $refund_money . ",refund_amount = refund_amount + " . $refund_money . " where id = " . $order_info['id']); order_log($order_info['order_sn'] . "订单超额支付," . format_price($refund_money) . "已退到会员余额", $order_info['id']); modify_statements("-" . $refund_money, 1, $order_info['order_sn'] . "订单超额"); modify_statements($refund_money, 2, $order_info['order_sn'] . "订单超额"); } modify_statements($order_info['total_price'], 8, $order_info['order_sn'] . "订单成功付款"); //增加营业额 $balance_total = $GLOBALS['db']->getOne("select sum(balance_total_price)+sum(add_balance_price_total) from " . DB_PREFIX . "deal_order_item where order_id = " . $order_info['id']); modify_statements($balance_total, 9, $order_info['order_sn'] . "订单成功付款"); //增加营业额中的成本 //生成抽奖 $lottery_list = $GLOBALS['db']->getAll("select d.id as did,doi.number from " . DB_PREFIX . "deal_order_item as doi left join " . DB_PREFIX . "deal_order as do on doi.order_id = do.id left join " . DB_PREFIX . "deal as d on doi.deal_id = d.id where d.is_lottery = 1 and do.id = " . $order_info['id']); $lottery_user = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "user where id = " . intval($order_info['user_id'])); //如为首次抽奖,先为推荐人生成抽奖号 $lottery_count = $GLOBALS['db']->getOne("select count(*) from " . DB_PREFIX . "lottery where user_id = " . intval($order_info['user_id'])); if ($lottery_count == 0 && $lottery_user['pid'] != 0) { $lottery_puser = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "user where id = " . intval($lottery_user['pid'])); foreach ($lottery_list as $lottery) { $k = 0; do { if ($k > 10) { break; } $buy_count = $GLOBALS['db']->getOne("select buy_count from " . DB_PREFIX . "deal where id = " . $lottery['did']); $max_sn = $buy_count - $lottery['number'] + intval($GLOBALS['db']->getOne("select count(*) from " . DB_PREFIX . "lottery where deal_id = " . intval($lottery['did']) . " and buyer_id <> 0 ")); //$max_sn = intval($GLOBALS['db']->getOne("select lottery_sn from ".DB_PREFIX."lottery where deal_id = '".$lottery['did']."' order by lottery_sn desc limit 1")); $sn = $max_sn + 1; $sn = str_pad($sn, "6", "0", STR_PAD_LEFT); $sql = "insert into " . DB_PREFIX . "lottery (`lottery_sn`,`deal_id`,`user_id`,`mobile`,`create_time`,`buyer_id`) select '" . $sn . "','" . $lottery['did'] . "'," . $lottery_puser['id'] . ",'" . $lottery_puser['lottery_mobile'] . "'," . NOW_TIME . "," . $order_info['user_id'] . " from dual where not exists( select * from " . DB_PREFIX . "lottery where deal_id = " . $lottery['did'] . " and lottery_sn = '" . $sn . "')"; $GLOBALS['db']->query($sql); send_lottery_sms(intval($GLOBALS['db']->insert_id())); $k++; } while (intval($GLOBALS['db']->insert_id()) == 0); } } foreach ($lottery_list as $lottery) { for ($i = 0; $i < $lottery['number']; $i++) { $k = 0; do { if ($k > 10) { break; } $buy_count = $GLOBALS['db']->getOne("select buy_count from " . DB_PREFIX . "deal where id = " . $lottery['did']); $max_sn = $buy_count - $lottery['number'] + intval($GLOBALS['db']->getOne("select count(*) from " . DB_PREFIX . "lottery where deal_id = " . intval($lottery['did']) . " and buyer_id <> 0 ")); //$max_sn = intval($GLOBALS['db']->getOne("select lottery_sn from ".DB_PREFIX."lottery where deal_id = '".$lottery['did']."' order by lottery_sn desc limit 1")); $sn = $max_sn + $i + 1; $sn = str_pad($sn, "6", "0", STR_PAD_LEFT); $sql = "insert into " . DB_PREFIX . "lottery (`lottery_sn`,`deal_id`,`user_id`,`mobile`,`create_time`,`buyer_id`) select '" . $sn . "','" . $lottery['did'] . "'," . $order_info['user_id'] . ",'" . $lottery_user['mobile'] . "'," . NOW_TIME . ",0 from dual where not exists( select * from " . DB_PREFIX . "lottery where deal_id = " . $lottery['did'] . " and lottery_sn = '" . $sn . "')"; $GLOBALS['db']->query($sql); send_lottery_sms(intval($GLOBALS['db']->insert_id())); $k++; } while (intval($GLOBALS['db']->insert_id()) == 0); } } send_msg($order_info['user_id'], "订单" . $order_info['order_sn'] . "付款成功", "orderitem", $order_goods_list[0]['id']); } } else { //开始模拟事务回滚 foreach ($affect_attr_list as $k => $v) { $sql = "update " . DB_PREFIX . "attr_stock set buy_count = buy_count - " . $v['number'] . " where deal_id = " . $v['deal_id'] . " and locate(attr_str,'" . $v['attr_str'] . "') > 0 "; $GLOBALS['db']->query($sql); //回滚已发的货量 } foreach ($affect_list as $k => $v) { $sql = "update " . DB_PREFIX . "deal set buy_count = buy_count - " . $v['num'] . ",user_count = user_count - 1 where id=" . $v['deal_id']; $GLOBALS['db']->query($sql); //回滚已发的货量 } $GLOBALS['db']->query("update " . DB_PREFIX . "deal_order_item set refund_status = 2 where order_id = " . $order_info['id']); //超出充值 require_once APP_ROOT_PATH . "system/model/user.php"; $msg = sprintf($GLOBALS['lang']['OUTOFSTOCK_INCHARGE'], $order_info['order_sn']); modify_account(array('money' => $order_info['total_price'], 'score' => 0), $order_info['user_id'], $msg); order_log($order_info['order_sn'] . "订单库存不足," . format_price($order_info['total_price']) . "已退到会员余额", $order_info['id']); modify_statements("-" . $order_info['total_price'], 1, $order_info['order_sn'] . "订单库存不足"); modify_statements($order_info['total_price'], 2, $order_info['order_sn'] . "订单库存不足"); //将订单的extra_status 状态更新为2,并自动退款 $GLOBALS['db']->query("update " . DB_PREFIX . "deal_order set extra_status = 2, after_sale = 1, refund_money = pay_amount where id = " . intval($order_info['id'])); update_order_cache($order_info['id']); //记录退款的订单日志 $log['log_info'] = $msg; $log['log_time'] = NOW_TIME; $log['order_id'] = intval($order_info['id']); $GLOBALS['db']->autoExecute(DB_PREFIX . "deal_order_log", $log); } //同步所有未过期的团购状态 foreach ($goods_list as $item) { syn_deal_status($item['deal_id'], true); } auto_over_status($order_id); //自动结单 } else { //订单充值 // $GLOBALS['db']->query("update ".DB_PREFIX."deal_order set order_status = 1 where id = ".$order_info['id']); //充值单自动结单 require_once APP_ROOT_PATH . "system/model/user.php"; $msg = sprintf($GLOBALS['lang']['USER_INCHARGE_DONE'], $order_info['order_sn']); modify_account(array('money' => $order_info['total_price'] - $order_info['payment_fee'], 'score' => 0), $order_info['user_id'], $msg); modify_statements("-" . $order_info['total_price'], 1, $order_info['order_sn'] . "会员充值"); modify_statements($order_info['total_price'], 2, $order_info['order_sn'] . "会员充值,含手续费"); send_msg($order_info['user_id'], "成功充值" . format_price($order_info['total_price'] - $order_info['payment_fee']), "notify", $order_id); auto_over_status($order_id); //自动结单 } }
public function importInsert() { require_once APP_ROOT_PATH . "/system/libs/deal.php"; $file = $_FILES['file']; $deal_id = intval($_REQUEST["deal_id"]); $content = @file_get_contents($file['tmp_name']); $content = explode("\n", $content); unset($content[0]); $count = 0; if ($content) { foreach ($content as $k => $v) { if ($v != '') { $imp_row = explode(",", $v); $password = strim($imp_row[0]); $begin_time = strim($imp_row[1]) == "" ? 0 : to_timespan(strim($imp_row[1])); $end_time = strim($imp_row[2]) == "" ? 0 : to_timespan(strim($imp_row[2])); $res = add_coupon($deal_id, 0, 0, "", $password, $begin_time, $end_time); if ($res['status']) { $count++; } } } save_log(sprintf(L("IMPORT_COUPON_SUCCESS"), $count), 1); $this->success(sprintf(L("IMPORT_COUPON_SUCCESS"), $count)); } else { $this->error(L("COUPON_FILE_REQUIRE")); } }
function handle() { global $section, $sub_section, $feedback_message, $item_for_edit, $connected_user_name; /*WORKS!!!!!!!!!!!!!!!!!*/ if ($section === "bussinesses" && $sub_section === "add_action" && $_SERVER["REQUEST_METHOD"] == "POST") { include_once 'model/business_logic.php'; $manager_username = $connected_user_name; $location_latitude = $_POST["latitude"]; $location_longitude = $_POST["longitude"]; $category = $_POST["category"]; $name = $_POST["business_name"]; $address = $_POST["business_address"]; $city = $_POST["business_city"]; if ($manager_username != "" & $location_latitude != "" & $location_longitude != "" & $category != "" & $name != "" & $address != "" & $city != "") { add_business($manager_username, $location_latitude, $location_longitude, $category, $name, $address, $city); $feedback_message = '<span class="label label-success">' . $name . ' Added Successfully</span>'; } else { $feedback_message = '<span class="label label-warning">All Fields Must Be Entered</span>'; } $sub_section = "show_edit_gallery"; } else { if ($section === "coupons" && $sub_section === "add_action" && $_SERVER["REQUEST_METHOD"] == "POST") { include_once 'model/coupon_logic.php'; $business_id = $_POST["coupon_business_id"]; $name = $_POST["coupon_name"]; $description = $_POST["coupon_description"]; $category = $_POST["coupon_category"]; $offered_quantity = $_POST["coupon_offered_quantity"]; $discount_price = $_POST["coupon_discount_price"]; $original_price = $_POST["coupon_original_price"]; $expiration_date = $_POST["coupon_expiration_date"]; if ($business_id != "" & $name != "" & $description != "" & $category != "" & $offered_quantity != "" & $discount_price != "" & $original_price != "" & $expiration_date != "") { add_coupon($business_id, $name, $description, $category, $offered_quantity, $discount_price, $original_price, $expiration_date); $feedback_message = '<span class="label label-success">' . $firstname . " " . $lastname . ' Added Successfully</span>'; } else { $feedback_message = '<span class="label label-warning">All Fields Must Be Entered</span>'; } $sub_section = "view"; } else { if ($section === "coupons" && $sub_section === "delete_action") { include_once 'model/coupon_logic.php'; delete_coupon($item_for_edit); $feedback_message = '<span class="label label-success">' . $name . ' Deleted Successfully</span>'; $sub_section = ""; $item_for_edit == ""; } else { if ($section === "bussinesses" & $sub_section === "delete_action") { include_once 'model/business_logic.php'; include_once 'model/coupon_logic.php'; delete_business($item_for_edit); $feedback_message = '<span class="label label-success">' . $name . ' Deleted Successfully</span>'; $sub_section = ""; $item_for_edit == ""; } else { if ($section === "manager" & $sub_section === "delete_action") { include_once 'model/business_logic.php'; delete_manager($item_for_edit); $feedback_message = '<span class="label label-success">' . $name . ' Deleted Successfully</span>'; $item_for_edit == ""; $sub_section = ""; } } } } } }