'待付款', self::ORDER_STATUS_FINISHING => '等待接单', self::ORDER_STATUS_DELIVERING => '等待送达', self::ORDER_STATUS_FINISHED => '完成', self::ORDER_STATUS_COMMENT => '已评价', self::ORDER_STATUS_CANCEL => '已取消', self::ORDER_STATUS_REJECT => '已拒绝', self::ORDER_STATUS_REFUNDING => '退款中', self::ORDER_STATUS_REFUNDED => '退款成功', self::ORDER_STATUS_REJECT_FAIL => '退款失败' ]; if ('' === $state) { return $arr ; } if (isset($arr[$state])) { return $arr[$state]; } else { return null; } } /* * 支付用的code */ static function payCode($userId) { return 'N'.time() . rand(1000, 9999) . $userId; } /** * @param $order * @param $postData */ static function afterOrderPay($out_trade_no, $postData) { $order = pdo_get('cjdc_order', array('code' => $out_trade_no)); if (empty($order)) { return false; } // checkPostData 校验数据是否正确 sign // $tt = time(); $orderId = $order['id']; $sign = Config::genSign(['i' => $order['uniacid'], 'id' => $orderId, 'timeStamp' => $tt]); $signStr = "&sign={$sign}&timeStamp={$tt}"; $callBackHeadURL = "https://{$_SERVER['HTTP_HOST']}/app/index.php?i={$order['uniacid']}&c=entry&a=wxapp&m=zh_cjdianc{$signStr}"; if (self::TYPE_TAKE_OUT == $order['type'] and (self::ORDER_STATUS_WAIT_PAY == $order['state'] || self::ORDER_STATUS_CANCEL == $order['state'])) {//外卖 Http::httpGet("{$callBackHeadURL}&do=NewOrderMessage&order_id=".$orderId); //模板消息 Http::httpGet("{$callBackHeadURL}&do=WeChatMsg&id=".$orderId); //模板消息 $store = pdo_get('cjdc_storeset', array('store_id' => $order['store_id'])); $sys = pdo_get('cjdc_system', array('uniacid' => $order['uniacid']), 'ps_name'); $ps_name = empty($sys['ps_name']) ? '超级跑腿' : $sys['ps_name']; if ($store['is_jd'] == 1) { $wmorder['state'] = 3; $wmorder['pay_time'] = date('Y-m-d H:i:s'); $wmorder['jd_time'] = date('Y-m-d H:i:s'); $res = pdo_update('cjdc_order', $wmorder, array('id' => $orderId)); Http::httpGet("{$callBackHeadURL}&do=jdnotice&order_id=" . $orderId);//达达 } else { $wmorder['state'] = 2; $wmorder['pay_time'] = date('Y-m-d H:i:s'); $res = pdo_update('cjdc_order', $wmorder, array('id' => $orderId)); } if ($store['is_jd'] == 1 && $order['order_type'] == 1) {//自动接单 if ($store['ps_mode'] == '达达配送') { Http::httpGet("{$callBackHeadURL}&do=TestDada&order_id=" . $orderId); //达达 } if ($store['ps_mode'] == '快服务配送') { $res = Http::httpGet("{$callBackHeadURL}&do=kfw&order_id=" . $orderId); //快服务 $kfw['ship_id'] = $res; pdo_update('cjdc_order', $kfw, array('id' => $orderId)); } if ($store['ps_mode'] == $ps_name) { Http::httpGet("{$callBackHeadURL}&do=cjpt&order_id=" . $orderId); //跑腿 } } pdo_update('cjdc_store', array('score +=' => 1), array('id' => $order['store_id'])); $good = pdo_getall('cjdc_order_goods', array('order_id' => $orderId)); for ($i = 0; $i < count($good); $i++) { pdo_update('cjdc_goods', array('inventory -=' => $good[$i]['number']), array('id' => $good[$i]['dishes_id'])); pdo_update('cjdc_goods', array('sales +=' => $good[$i]['number']), array('id' => $good[$i]['dishes_id'])); } //Http::httpGet("https://".$_SERVER['HTTP_HOST'] . "/app/index.php?i=".$order['uniacid']."&c=entry&a=wxapp&do=payorder&order_id=".$orderId); $sms = Http::httpGet("{$callBackHeadURL}&do=Message&order_id=".$orderId); //模板消息 Http::httpGet("{$callBackHeadURL}&do=QtPrint&order_id=".$orderId);//打印机 Http::httpGet("{$callBackHeadURL}&do=HcPrint&order_id=".$orderId);//打印机 Http::httpGet("{$callBackHeadURL}&do=sms&type=1&store_id=".$order['store_id']);//短信 //分销佣金 Http::httpGet("{$callBackHeadURL}&do=JsCommission&order_id=".$orderId); } if (self::TYPE_IN_STORE == $order['type'] and 1 == $order['dn_state']) {//店内 $dnorder['dn_state'] = 2; $dnorder['pay_time'] = date('Y-m-d H:i:s'); $res = pdo_update('cjdc_order', $dnorder, array('id' => $orderId)); pdo_update('cjdc_store', array('score +=' => 1), array('id' => $order['store_id'])); $good = pdo_getall('cjdc_order_goods', array('order_id' => $orderId)); for ($i = 0; $i < count($good); $i++) { pdo_update('cjdc_goods', array('inventory -=' => $good[$i]['number']), array('id' => $good[$i]['dishes_id'])); pdo_update('cjdc_goods', array('sales +=' => $good[$i]['number']), array('id' => $good[$i]['dishes_id'])); } //Http::httpGet("{$callBackHeadURL}&do=payorder&order_id=".$orderId); Http::httpGet("{$callBackHeadURL}&do=sms&type=2&store_id=".$order['store_id']);//短信 Http::httpGet("{$callBackHeadURL}&do=QtPrint&order_id=".$orderId);//打印机 Http::httpGet("{$callBackHeadURL}&do=HcPrint&order_id=".$orderId);//打印机 Http::httpGet("{$callBackHeadURL}&do=addintegral&type=2&order_id=".$orderId);//短信 //分销佣金 Http::httpGet("{$callBackHeadURL}&do=JsCommission&order_id=".$orderId); } if (self::TYPE_BOOKING == $order['type'] and 1 == $order['yy_state']) {//预约 pdo_update('cjdc_order',array('yy_state'=>2),array('code'=>$out_trade_no)); Http::httpGet("{$callBackHeadURL}&do=sms&type=3&store_id=".$order['store_id']);//短信 //分销佣金 Http::httpGet("{$callBackHeadURL}&do=JsCommission&order_id=".$orderId); Http::httpGet("{$callBackHeadURL}&do=storeYyOrderMessage&order_id=".$orderId);//模板消息 Http::httpGet("{$callBackHeadURL}&do=QtPrint&order_id=".$orderId);// } if (self::TYPE_DANG_MIAN == $order['type'] and 1 == $order['dm_state']) {//当面付 Http::httpGet("{$callBackHeadURL}&do=NewDmOrderMessage&order_id=".$orderId);//模板消息 $res = pdo_update('cjdc_order',array('dm_state'=>2),array('id'=>$orderId)); Http::httpGet("{$callBackHeadURL}&do=sms&type=2&store_id=".$order['store_id']);//短信 Http::httpGet("{$callBackHeadURL}&do=QtPrint&order_id=".$orderId);//打印机 Http::httpGet("{$callBackHeadURL}&do=addintegral&type=5&order_id=".$orderId);//短信 //分销佣金 Http::httpGet("{$callBackHeadURL}&do=JsCommission&order_id=".$orderId); } $store = pdo_get('cjdc_store',array('code'=>$out_trade_no)); if($store['zf_state']==1) { $res = pdo_update('cjdc_store',array('zf_state'=>2),array('id'=>$store['id'])); Http::httpGet("{$callBackHeadURL}&do=SaveRzLog&store_id=".$store['id']."&money=".$store['money']); } return true; } /** * 店家订单列表 */ static function storeAdminOrderPageList($_W, $_GPC, $storeid) { $pageindex = max(1, intval($_GPC['page'])); $pagesize = 8; $type = isset($_GPC['type']) ? $_GPC['type'] : 'now'; $type2 = isset($_GPC['type2']) ? $_GPC['type2'] : 'today'; $orderType = self::TYPE_GROUP; $where = " where a.uniacid=:uniacid and a.type={$orderType} and a.store_id=" . $storeid; $data[':uniacid'] = $_W['uniacid']; if (isset($_GPC['keywords'])) { $where .= " and (a.name LIKE concat('%', :name,'%') || a.order_num LIKE concat('%', :name,'%') || b.name LIKE concat('%', :name,'%'))"; $data[':name'] = $_GPC['keywords']; $type = 'all'; } if ($_GPC['time']) { $start = $_GPC['time']['start']; $end = $_GPC['time']['end']; $where .= " and a.time >='{$start}' and a.time<='{$end}'"; $type = 'all'; } else { if ($type == 'wait') { $where .= " and a.state=1"; } if ($type == 'now') { $where .= " and a.state=2"; } if ($type == 'cancel') { $where .= " and a.state in (6,7,8,9,10)"; } if ($type == 'complete') { $where .= " and a.state=4"; } if ($type == 'delivery') { $where .= " and a.state=3"; } if ($type == 'zt') { $where .= " and a.order_type=2"; } if ($type == 'dd') { $where .= " and a.order_type=3"; } if ($type2 == 'today') { $time = date("Y-m-d", time()); $where .= " and a.time LIKE '%{$time}%' "; } if ($type2 == 'yesterday') { $time = date("Y-m-d", strtotime("-1 day")); $where .= " and a.time LIKE '%{$time}%' "; } if ($type2 == 'week') { $time = strtotime(date("Y-m-d", strtotime("-7 day"))); $where .= " and UNIX_TIMESTAMP(a.time) >" . $time; } if ($type2 == 'month') { $time = date("Y-m"); $where .= " and a.time LIKE '%{$time}%' "; } } $userOrderCountSql = "(select count(*) ". " from ". tablename("cjdc_order") ." as uo ". " WHERE uo.store_id={$storeid} and uo.user_id=a.user_id and uo.id<=a.id and uo.state!=1) as order_count"; $sql = "SELECT {selectFields} " . " FROM ". tablename('cjdc_order') . " a " . " left join " . tablename("cjdc_store") . " b on a.store_id=b.id " . " left join " . tablename("cjdc_storetype") . " c on b.md_type=c.id " . " left join " . tablename("cjdc_storeset") . " d on b.id=d.store_id " . " left join " . tablename("cjdc_user") . " u on u.id=a.user_id " . $where . " ORDER BY a.id DESC"; $countSql = str_replace("{selectFields}", "count(*)", $sql); $total = pdo_fetchcolumn($countSql, $data); $fields = "a.*,b.name as md_name,c.poundage as md_poundage,d.poundage,d.is_poundage,d.ps_mode,b.ps_poundage,u.name as userName, {$userOrderCountSql}"; $pageSql = str_replace("{selectFields}", $fields, $sql); $select_sql = $pageSql . " LIMIT " . ($pageindex - 1) * $pagesize . "," . $pagesize; echo $select_sql; $list = pdo_fetchall($select_sql, $data); $pageData = array(); foreach($list as $k => $order ) { $orderGoods = array(); $goodsArr = pdo_getall('cjdc_order_goods', array('order_id' => $order['id'])); foreach($goodsArr as $kk => $goods ) { if ($order['id'] == $goods['order_id']) { $orderGoods[] = array( 'name' => $goods['name'], 'num' => $goods['number'], 'img' => $goods['img'], 'money' => $goods['money'], 'spec' => $goods['spec'], 'dishes_id' => $goods['dishes_id'], ); } } $pageData[] = array( 'order' => $order, 'goods' => $orderGoods, ); } $pager = pagination($total, $pageindex, $pagesize); return ['pageData' => $pageData, 'pager' => $pager]; } /** * 删除订单 * @param $orderId * @return bool */ static function storeAdminOrderDelete($orderId) { $res = pdo_delete('cjdc_order', array('id' => $orderId)); return $res; } /** * 取消订单,同时退款拼团订单 * @param $uniacid * @param $orderId * @return array */ static function storeAdminOrderCancel($uniacid, $orderId) { $order = pdo_get('cjdc_order', array('id' => $orderId)); $result = null ; $tkres = null; if (self::ORDER_STATUS_DELIVERING == $order['state']) { if (self::PAY_TYPE_WX == $order['pay_type']) { //微信退款 $result = WxPay::orderRefund($orderId, self::TYPE_GROUP); } if (self::PAY_TYPE_BALANCE == $order['pay_type']) { //余额退款 pdo_update('cjdc_user', array('wallet +=' => $order['money']), array('id' => $order['user_id'])); $tk['money'] = $order['money']; $tk['user_id'] = $order['user_id']; $tk['type'] = 1; $tk['note'] = '取消订单'; $tk['time'] = date('Y-m-d H:i:s'); $tkres = pdo_insert('cjdc_qbmx', $tk); } } if ((!empty($result) && $result['result_code'] == 'SUCCESS') || $tkres) { //退款成功 if ($order['coupon_id']) { pdo_update('cjdc_usercoupons', array('state' => 2), array('id' => $order['coupon_id'])); } if ($order['coupon_id2']) { pdo_update('cjdc_usercoupons', array('state' => 2), array('id' => $order['coupon_id2'])); } $updateResult = pdo_update('cjdc_order', array('state' => self::ORDER_STATUS_CANCEL), array('id' => $orderId)); // 冻结佣金,暂时作废 Commission::invalidOrderFee($orderId); // 更新退款记录 Finance::updateOrderPriceStat($order['store_id'], $orderId, $order['money']); $set = pdo_get('cjdc_storeset', array('store_id' => $order['store_id']), 'ps_mode'); $sys = pdo_get('cjdc_system', array('uniacid' => $uniacid), 'ps_name'); $ps_name = empty($sys['ps_name']) ? '超级跑腿' : $sys['ps_name']; if ($updateResult) { return ['success' => true, 'storeSet' => $set, 'delivery' => $ps_name, 'order' => $order]; } else { return ['success' => false]; } } else { // 如果$result 返回前不够应该通知平台管理员 return ['success' => false, 'refundResult' => $result]; } } /** * 店家接订单 * @param $uniacid * @param $orderId * @return array */ static function storeOwnerPickOrder($uniacid, $orderId) { $updateData['state'] = self::ORDER_STATUS_DELIVERING; $updateData['jd_time'] = date('Y-m-d H:i:s'); $orderInfo = pdo_get('cjdc_order', array('id' => $orderId)); $store = pdo_get('cjdc_storeset', array('store_id' => $orderInfo['store_id'])); $sys = pdo_get('cjdc_system', array('uniacid' => $uniacid), 'ps_name'); $ps_name = empty($sys['ps_name']) ? '超级跑腿' : $sys['ps_name']; $updateRes = pdo_update('cjdc_order', $updateData, array('id' => $orderId)); if ($updateRes) { $orderInfo['state'] = $updateData['state']; return ['storeSet' => $store, 'delivery' => $ps_name, "order" => $orderInfo]; } else { return []; } } /** * 拒接接单 * @param $orderId */ static function storeOwnerRejectOrder($uniacId, $orderId) { $data2['state'] = self::ORDER_STATUS_REJECT; $order = pdo_get('cjdc_order', array('id' => $orderId)); $result = null ; $tkres = null; if ((self::PAY_TYPE_WX == $order['pay_type'] || self::PAY_TYPE_BALANCE == $order['pay_type']) and $order['money'] > 0 and self::ORDER_STATUS_FINISHING == $order['state']) { if (self::PAY_TYPE_WX == $order['pay_type']) { //微信退款 $result = WxPay::orderRefund($orderId, self::TYPE_GROUP); } if (self::PAY_TYPE_BALANCE == $order['pay_type']) { //余额退款 pdo_update('cjdc_user', array('wallet +=' => $order['money']), array('id' => $order['user_id'])); $tk['money'] = $order['money']; $tk['user_id'] = $order['user_id']; $tk['type'] = 1; $tk['note'] = '订单拒绝'; $tk['time'] = date('Y-m-d H:i:s'); $tkres = pdo_insert('cjdc_qbmx', $tk); } if ((!empty($result) && $result['result_code'] == 'SUCCESS') || $tkres) { //退款成功 //更改订单操作 pdo_update('cjdc_order', array('state' => self::ORDER_STATUS_REJECT), array('id' => $orderId)); if ($order['coupon_id']) { pdo_update('cjdc_usercoupons', array('state' => 2), array('id' => $order['coupon_id'])); } if ($order['coupon_id2']) { pdo_update('cjdc_usercoupons', array('state' => 2), array('id' => $order['coupon_id2'])); } Commission::invalidOrderFee($order['id']); Finance::updateOrderPriceStat($order['store_id'], $orderId, $order['money']); pdo_delete('cjdc_formid', array('time <=' => time() - 60 * 60 * 24 * 7)); return ['success' => true, 'order' => $order]; } else { // 钱不够应该返回通知管理员 return ['success' => false, 'refundResult' => $result]; } } else { return ['success' => false, 'message' => '订单状态不对无法拒接']; $updateResult = pdo_update('cjdc_order', array('state' => self::ORDER_STATUS_REJECT), array('id' => $orderId)); if ($updateResult) { if ($order['coupon_id']) { pdo_update('cjdc_usercoupons', array('state' => 2), array('id' => $order['coupon_id'])); } if ($order['coupon_id2']) { pdo_update('cjdc_usercoupons', array('state' => 2), array('id' => $order['coupon_id2'])); } return ['success' => false, 'message' => '订单状态不对无法拒接']; } else { return ['success' => false, 'message' => '订单状态不对无法拒接']; } } } /** * @param $uniacid * @param $orderId * @return bool */ static function storeOwnerFinishOrder($uniacid, $orderId) { $data2['state'] = self::ORDER_STATUS_FINISHED; $res = pdo_update('cjdc_order', $data2, array('id' => $orderId)); Commission::availableOrderFee($orderId); return $res; } /** * 直接退款操作和取消有点不同 * @param $uniacid * @param $orderId */ static function storeOwnerOrderRefundOp($uniacid, $orderId) { $order = pdo_get('cjdc_order', array('id' => $orderId)); $result = null; $tkres = null; if (self::ORDER_STATUS_REFUNDING == $order['state']) { if (self::PAY_TYPE_WX == $order['pay_type']) { //微信退款 $result = WxPay::orderRefund($orderId, self::TYPE_GROUP); } if (self::PAY_TYPE_BALANCE == $order['pay_type']) { //余额退款 $rst = pdo_get('cjdc_qbmx', array('user_id' => $order['user_id'], 'order_id' => $orderId)); if (!$rst) { $tk['money'] = $order['money']; $tk['order_id'] = $order['id']; $tk['user_id'] = $order['user_id']; $tk['type'] = 1; $tk['note'] = '订单退款'; $tk['time'] = date('Y-m-d H:i:s'); $tkres = pdo_insert('cjdc_qbmx', $tk); pdo_update('cjdc_user', array('wallet +=' => $order['money']), array('id' => $order['user_id'])); } } } if ($result['result_code'] == 'SUCCESS' || $tkres) { //退款成功 //更改订单操作 $updateResult = pdo_update('cjdc_order', array('state' => self::ORDER_STATUS_REFUNDED), array('id' => $orderId)); Commission::invalidOrderFee($orderId); Finance::updateOrderPriceStat($order['store_id'], $orderId, $order['money']); pdo_delete('cjdc_formid', array('time <=' => time() - 60 * 60 * 24 * 7)); $set = pdo_get('cjdc_storeset', array('store_id' => $order['store_id']), 'ps_mode'); $sys = pdo_get('cjdc_system', array('uniacid' => $uniacid), 'ps_name'); $ps_name = empty($sys['ps_name']) ? '超级跑腿' : $sys['ps_name']; if ($updateResult) { return ['success' => true, 'storeSet' => $set, 'delivery' => $ps_name, 'order' => $order]; } else { return ['success' => false]; } } else { // 如果$result 返回前不够应该通知平台管理员 return ['success' => false, 'refundResult' => $result]; } } /** * 拒绝顾客退款 * @param $orderId */ static function storeOwnerRejectRefund($orderId) { $order = pdo_get('cjdc_order', array('id' => $orderId)); if (self::ORDER_STATUS_REFUNDING != $order['state']) { return false; } $rst = pdo_update('cjdc_order', array('state' => self::ORDER_STATUS_REJECT_FAIL), array('id' => $orderId)); Commission::invalidOrderFee($orderId); return $rst; } /** * 导出订单 * @param $_GPC * @param $storeId * @return string */ static function exportOrderData($_GPC, $storeId) { $start = $_GPC['time']['start']; $end = $_GPC['time']['end']; $count = pdo_fetchcolumn("SELECT COUNT(*) FROM" . tablename("cjdc_order") . " WHERE type=1 and store_id={$storeId} and time >='{$start}' and time<='{$end}'"); $pagesize = ceil($count / 5000); $header = array( 'item' => '序号', 'md_name' => '门店名称', 'order_num' => '订单号', 'name' => '联系人', 'tel' => '联系电话', 'address' => '联系地址', 'time' => '下单时间', 'money' => '金额', 'state' => '外卖状态', 'pay_type' => '支付方式', 'order_type' => '订单类型', 'goods' => '商品', 'box_money' => '餐盒费', ); $keys = array_keys($header); $html = "\xEF\xBB\xBF"; foreach ($header as $li) { $html .= $li . "\t ,"; } $html .= "\n"; for ($j = 1; $j <= $pagesize; $j++) { $sql = "select a.*,b.name as md_name ". " from ". tablename("cjdc_order") . " a" . " inner join " . tablename("cjdc_store") . " b on a.store_id=b.id ". " WHERE a.type=1 and a. time >='{$start}' and a.time<='{$end}' and a.store_id={$storeId} ". " limit " . ($j - 1) * 5000 . ",5000 "; $list = pdo_fetchall($sql); } if (!empty($list)) { $size = ceil(count($list) / 500); for ($i = 0; $i < $size; $i++) { $buffer = array_slice($list, $i * 500, 500); $user = array(); foreach ($buffer as $k => $row) { $row['item'] = $k + 1; $row['state'] = self::getStatusLabel($row['state']); if (self::PAY_TYPE_WX == $row['pay_type']) { $row['pay_type'] = '微信支付'; } elseif (self::PAY_TYPE_BALANCE == $row['pay_type']) { $row['pay_type'] = '余额支付'; } elseif (self::PAY_TYPE_POINTS == $row['pay_type']) { $row['pay_type'] = '积分支付'; } elseif (self::PAY_TYPE_CASH == $row['pay_type']) { $row['pay_type'] = '货到付款'; } if ($row['order_type'] == 1) { $row['order_type'] = '外卖配送'; } elseif ($row['order_type'] == 2) { $row['order_type'] = '到店自提'; } $good = pdo_getall('cjdc_order_goods', array('order_id' => $row['id'])); $date6 = array(); for ($i = 0; $i < count($good); $i++) { if ($good[$i]['spec']) { $date6[$i] .= $good[$i]['name'] . '(' . $good[$i]['spec'] . ')*' . $good[$i]['number'] . " "; } else { $date6[$i] .= $good[$i]['name'] . '*' . $good[$i]['number'] . " "; } } $goodsInfo = implode(" ", $date6); $goodsInfo = str_replace(",", ",", $goodsInfo); $row['goods'] = $goodsInfo; foreach ($keys as $key) { $data5[] = $row[$key]; } $user[] = implode("\t ,", $data5) . "\t ,"; unset($data5); } $html .= implode("\n", $user) . "\n"; } } return $html; } }