/** * 退款执行流: * 1. 退还金额至会员账户 * 2. 更新商家账户 * 3. 更新订单及订单关联表的相关状态 * 3. 更新平台报表 * 4. 更新订单缓存 * 5. 为订单重新分片 */ public function do_refund() { $order_item_id = intval($_REQUEST['order_item_id']); $coupon_id = intval($_REQUEST['coupon_id']); $price = floatval($_REQUEST['price']); $balance_price = floatval($_REQUEST['balance_price']); $content = strim($_REQUEST['content']); if ($price < 0 || $balance_price < 0) { $this->error("金额出错", 1); } if ($order_item_id) { $oi = $order_item_id; $data = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "deal_order_item where id = " . $order_item_id); if ($data['refund_status'] == 2) { $this->error("已退款", 1); } if ($data) { $order_id = $data['order_id']; $supplier_id = $data['supplier_id']; } $GLOBALS['db']->query("update " . DB_PREFIX . "deal_order_item set refund_status = 2,is_arrival = 0 where id = " . $order_item_id); $refund_item_count = $GLOBALS['db']->getOne("select count(*) from " . DB_PREFIX . "deal_order_item where (refund_status = 1 or is_arrival = 2) and order_id = " . $order_id); $coupon_item_count = $GLOBALS['db']->getOne("select count(*) from " . DB_PREFIX . "deal_coupon where refund_status = 1 and order_id = " . $order_id); if ($refund_item_count == 0 && $coupon_item_count == 0) { $GLOBALS['db']->query("update " . DB_PREFIX . "deal_order set refund_amount = refund_amount + " . $price . ",refund_money = refund_money + " . $price . ",refund_status = 2,after_sale = 1,is_refuse_delivery=0 where id = " . $order_id); } else { $GLOBALS['db']->query("update " . DB_PREFIX . "deal_order set refund_amount = refund_amount + " . $price . ",refund_money = refund_money + " . $price . ",is_refuse_delivery=0 where id = " . $order_id); } } elseif ($coupon_id) { $data = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "deal_coupon where id = " . $coupon_id); if ($data['refund_status'] == 2) { $this->error("已退款", 1); } if ($data) { $oi = $data['order_deal_id']; $order_item = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "deal_order_item where id = " . $data['order_deal_id']); $data['name'] = $order_item['name']; $order_id = $data['order_id']; $supplier_id = $data['supplier_id']; } $GLOBALS['db']->query("update " . DB_PREFIX . "deal_coupon set refund_status = 2 where id = " . $coupon_id); $GLOBALS['db']->query("update " . DB_PREFIX . "deal_order_item set refund_status = 2 where id = " . $data['order_deal_id']); $refund_item_count = $GLOBALS['db']->getOne("select count(*) from " . DB_PREFIX . "deal_order_item where (refund_status = 1 or is_arrival = 2) and order_id = " . $order_id); $coupon_item_count = $GLOBALS['db']->getOne("select count(*) from " . DB_PREFIX . "deal_coupon where refund_status = 1 and order_id = " . $order_id); if ($refund_item_count == 0 && $coupon_item_count == 0) { $GLOBALS['db']->query("update " . DB_PREFIX . "deal_order set refund_amount = refund_amount + " . $price . ",refund_money = refund_money + " . $price . ",refund_status = 2,after_sale = 1 where id = " . $order_id); } else { $GLOBALS['db']->query("update " . DB_PREFIX . "deal_order set refund_amount = refund_amount + " . $price . ",refund_money = refund_money + " . $price . " where id = " . $order_id); } } $order_info = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "deal_order where id = " . $order_id); if ($price > 0) { require_once APP_ROOT_PATH . "system/model/user.php"; modify_account(array("money" => $price), $order_info['user_id'], $data['name'] . "退款成功"); modify_statements($price, 6, $data['name'] . "用户退款"); } if ($balance_price > 0) { require_once APP_ROOT_PATH . "system/model/supplier.php"; modify_supplier_account("-" . $balance_price, $supplier_id, 1, $data['name'] . "用户退款"); //冻结资金减少 modify_supplier_account($balance_price, $supplier_id, 4, $data['name'] . "用户退款"); //退款增加 modify_statements($balance_price, 7, $data['name'] . "用户退款"); } require_once APP_ROOT_PATH . "system/model/deal_order.php"; order_log($data['name'] . "退款成功 " . format_price($price) . " " . $content, $order_id); auto_over_status($order_id); update_order_cache($order_id); distribute_order($order_id); send_msg($order_info['user_id'], $data['name'] . "退款成功 " . format_price($price) . " " . $content, "orderitem", $oi); $this->success("退款成功", 1); }
public function do_withdrawal() { $id = intval($_REQUEST['id']); $log = strim($_REQUEST['log']); require_once APP_ROOT_PATH . "system/model/user.php"; $withdrawal_info = M("Withdraw")->getById($id); $user_info = M("User")->getById($withdrawal_info['user_id']); $withdrawal_info['money'] = floatval($_REQUEST['money']); if ($withdrawal_info['money'] <= 0) { $this->error("提现金额必须大于0"); } if ($withdrawal_info['money'] > $user_info['money']) { $this->error("提现超额"); } if ($withdrawal_info['is_paid'] == 0) { M("Withdraw")->where("id=" . $id)->setField("is_paid", 1); M("Withdraw")->where("id=" . $id)->setField("money", $withdrawal_info['money']); modify_account(array('money' => "-" . $withdrawal_info['money']), $withdrawal_info['user_id'], $user_info['user_name'] . "提现" . format_price($withdrawal_info['money']) . "元审核通过。" . $log); modify_statements($withdrawal_info['money'], 3, $user_info['user_name'] . "提现" . format_price($withdrawal_info['money']) . "元审核通过。" . $log); modify_statements($withdrawal_info['money'], 4, $user_info['user_name'] . "提现" . format_price($withdrawal_info['money']) . "元审核通过。" . $log); //发短信与邮件 send_user_withdraw_sms($user_info['id'], $withdrawal_info['money']); send_user_withdraw_mail($user_info['id'], $withdrawal_info['money']); save_log($user_info['user_name'] . "提现" . format_price($withdrawal_info['money']) . "元审核通过。" . $log, 1); $this->success("确认提现成功"); } else { $this->error("已提现过,无需再次提现"); } }
/** * 收货操作:收货后发放积分,钱的返还,更新商家的结算 * @param unknown_type $delivery_sn * @param unknown_type $order_item_id 订单商品ID,将会确认相关的所有订单的同序号发货号。 */ function confirm_delivery($delivery_sn, $order_item_id) { $order_id = $GLOBALS['db']->getOne("select order_id from " . DB_PREFIX . "deal_order_item where id = '" . $order_item_id . "'"); $order_info = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "deal_order where id = " . intval($order_id)); if ($order_info) { $delivery_notices = $GLOBALS['db']->getAll("select * from " . DB_PREFIX . "delivery_notice where order_id = " . $order_info['id'] . " and notice_sn = '" . $delivery_sn . "'"); $order_item_ids = array(0); foreach ($delivery_notices as $k => $v) { $order_item_ids[] = $v['order_item_id']; } $sql = "update " . DB_PREFIX . "deal_order_item set is_arrival = 1,consume_count = consume_count + 1 where is_arrival <> 1 and id in (" . implode(",", $order_item_ids) . ")"; $GLOBALS['db']->query($sql); if ($GLOBALS['db']->affected_rows() || true) { $GLOBALS['db']->query("update " . DB_PREFIX . "delivery_notice set is_arrival = 1,arrival_time = '" . NOW_TIME . "' where notice_sn = '" . $delivery_sn . "' and is_arrival <> 1 and order_id = " . $order_info['id']); $return_total = $GLOBALS['db']->getRow("select sum(return_total_score) as return_total_score,\n\t\t\t\t\tsum(return_total_money) as return_total_money,\n\t\t\t\t\tsum(total_price) as total_price,\n\t\t\t\t\tsum(balance_total_price) as balance_total_price,\n\t\t\t\t\tsum(add_balance_price_total) as add_balance_price_total from " . DB_PREFIX . "deal_order_item where id in (" . implode(",", $order_item_ids) . ")"); $log = $order_info['order_sn'] . "订单已收货"; if ($return_total['return_total_score'] > 0 || $return_total['return_total_money'] > 0) { $money = $return_total['return_total_money']; $score = $return_total['return_total_score']; require_once APP_ROOT_PATH . "system/model/user.php"; modify_account(array("money" => $money, "score" => $score), $order_info['user_id'], $log); } //订单商品 $sql = "update " . DB_PREFIX . "deal_order_item set is_balance = 1 where id in (" . implode(",", $order_item_ids) . ") and is_balance = 0"; $GLOBALS['db']->query($sql); $is_refuse_delivery = $GLOBALS['db']->getOne("select count(*) from " . DB_PREFIX . "delivery_notice where is_arrival = 2 and order_id = " . $order_id); if (!$is_refuse_delivery) { $GLOBALS['db']->query("update " . DB_PREFIX . "deal_order set is_refuse_delivery = 0 where id = " . $order_id); } $balance_list = $GLOBALS['db']->getAll("select sum(balance_total_price) as balance_total_price,sum(add_balance_price_total) as add_balance_price_total,supplier_id from " . DB_PREFIX . "deal_order_item where id in (" . implode(",", $order_item_ids) . ") group by supplier_id"); foreach ($balance_list as $k => $v) { $balance_price = $v['balance_total_price'] + $v['add_balance_price_total']; require_once APP_ROOT_PATH . "system/model/supplier.php"; modify_supplier_account("-" . $balance_price, $v['supplier_id'], 1, $log); //解冻资金 modify_supplier_account($balance_price, $v['supplier_id'], 2, $log); //等结算金额增加 } $stat_balance_price = $return_total['balance_total_price'] + $return_total['add_balance_price_total']; modify_statements($return_total['total_price'], 11, $log); //增加消费额 modify_statements($stat_balance_price, 12, $log); //增加消费额成本 auto_over_status($order_info['id']); //检测自动结单 update_order_cache($order_info['id']); distribute_order($order_info['id']); return true; } } return false; }
public function docharge() { $charge_id = intval($_REQUEST['charge_id']); $supplier_id = intval($_REQUEST['supplier_id']); $log = strim($_REQUEST['log']); require_once APP_ROOT_PATH . "system/model/supplier.php"; if ($charge_id > 0) { $charge = M("SupplierMoneySubmit")->getById($charge_id); $supplier_info = M("Supplier")->getById($charge['supplier_id']); $charge['money'] = floatval($_REQUEST['money']); if ($charge['money'] <= 0) { $this->error("提现金额必须大于0"); } if ($charge['money'] > $supplier_info['money']) { $this->error("提现超额"); } if ($charge['status'] == 0) { M("SupplierMoneySubmit")->where("id=" . $charge['id'])->setField("status", 1); M("SupplierMoneySubmit")->where("id=" . $charge['id'])->setField("money", $charge['money']); modify_supplier_account($charge['money'], $charge['supplier_id'], 5, $supplier_info['name'] . "提现" . format_price($charge['money']) . "元审核通过。" . $log); //.提现增加 modify_supplier_account("-" . $charge['money'], $charge['supplier_id'], 3, $supplier_info['name'] . "提现" . format_price($charge['money']) . "元审核通过。" . $log); //已结算减少 modify_statements($charge['money'], 3, $supplier_info['name'] . "提现" . format_price($charge['money']) . "元审核通过。" . $log); modify_statements($charge['money'], 5, $supplier_info['name'] . "提现" . format_price($charge['money']) . "元审核通过。" . $log); send_supplier_withdraw_sms($supplier_info['id'], $charge['money']); save_log($supplier_info['name'] . "提现" . format_price($charge['money']) . "元审核通过。" . $log, 1); $this->success("确认提现成功"); } else { $this->error("已提现过,无需再次提现"); } } if ($supplier_id > 0) { $supplier_info = M("Supplier")->getById($supplier_id); $remittance_num = floatval($_REQUEST['money']); if ($remittance_num <= 0) { $this->error("打款金额必须大于0"); } if ($remittance_num > $supplier_info['money']) { $this->error("打款超额"); } modify_supplier_account($remittance_num, $supplier_id, 5, "成功打款给" . $supplier_info['name'] . format_price($remittance_num) . "元。" . $log); //.提现增加 modify_supplier_account("-" . $remittance_num, $supplier_id, 3, "成功打款给" . $supplier_info['name'] . format_price($remittance_num) . "元。" . $log); //已结算减少 modify_statements($remittance_num, 3, "成功打款给" . $supplier_info['name'] . format_price($remittance_num) . "元。" . $log); modify_statements($remittance_num, 5, "成功打款给" . $supplier_info['name'] . format_price($remittance_num) . "元。" . $log); send_supplier_withdraw_sms($supplier_info['id'], $remittance_num); save_log("成功打款给" . $supplier_info['name'] . format_price($remittance_num) . "元。" . $log, 1); $this->success("打款成功"); } }
/** * 变更商家账户资金 * @param unknown_type $money * @param unknown_type $supplier_id 商家ID * @param unknown_type $type 0:销售额增加 1:资金冻结 2.待结算增加 3.已结算增加 4.退款增加 5.提现增加 * 修改:放弃3类型,所有2类型表示已结算,即变为可提现金额,字段功能不变,传参时,2,3都变为3 * @param unknown_type $info 日志内容 *`sale_money` '销售总额', *`lock_money` '冻结资金(即已销售,未验证,未收货的金额)', *`balance_money` '待结算金额(即每验证,收货一个,增加此金额,同时扣除冻结金额), *`money` '商户余额(可提现余额,已结算金额,结算后,待结算减少,已结算增加)', *`refund_money` '已退款金额(退款后增加此金额,同时减少lock_money冻结金额), *`wd_money` '已提现金额:(已提走的金额,提现成功后,增加,同时减少money)'; */ function modify_supplier_account($money, $supplier_id, $type, $info) { if ($type >= 0 && $type < 6) { if ($type == 2) { $type = 3; } //直接结算 $supplier_info = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "supplier where id = '" . $supplier_id . "'"); if ($supplier_info) { $field_array = array('sale_money', 'lock_money', 'balance_money', 'money', 'refund_money', 'wd_money'); $GLOBALS['db']->query("update " . DB_PREFIX . "supplier set " . $field_array[$type] . " = " . $field_array[$type] . " + " . floatval($money) . " where id =" . $supplier_id); $date = to_date(NOW_TIME, "Y-m-d"); $date_month = to_date(NOW_TIME, "Y-m"); //商家日报只记录 销售额,消费额,退款,提现(money为消费,与财务总报不一样,提现不减少) if ($type == 0 || $type == 3 && $money > 0 || $type == 4 || $type == 5) { $supplier_stat = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "supplier_statements where supplier_id = " . $supplier_id . " and stat_time = '" . $date . "'"); if ($supplier_stat) { $GLOBALS['db']->query("update " . DB_PREFIX . "supplier_statements set " . $field_array[$type] . " = " . $field_array[$type] . " + " . floatval($money) . " where supplier_id =" . $supplier_id . " and stat_time = '" . $date . "'"); } else { $supplier_stat = array(); $supplier_stat[$field_array[$type]] = floatval($money); $supplier_stat['stat_time'] = $date; $supplier_stat['stat_month'] = $date_month; $supplier_stat['supplier_id'] = $supplier_id; $GLOBALS['db']->autoExecute(DB_PREFIX . "supplier_statements", $supplier_stat); } } $log_data = array(); $log_data['log_info'] = $info; $log_data['supplier_id'] = $supplier_id; $log_data['create_time'] = NOW_TIME; $log_data['money'] = floatval($money); $log_data['type'] = $type; $GLOBALS['db']->autoExecute(DB_PREFIX . "supplier_money_log", $log_data); if ($type == 3 && $money > 0) { //当商家余额增加时,即表示结算 modify_statements($money, 10, $info); } } } }
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); //自动结单 } }