-1], [['page',], 'default', 'value' => 1], [['dateStart', 'dateEnd', 'express_type', 'md_name', 'order_no', 'goods_name', 'mobile'], 'trim'], [['seller_comments', 'flag', 'user_name'], 'string'], [['export'], 'safe'], [['fields', 'is_delivery', 'shop_list', 'book_name', 'book_mobile', 'name'], 'safe'] ]; } public function search($is_scan_pay = false) { if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; } $query = Order::find()->alias('o')->where(['o.order_origin'=>Order::ORDER_SOURCE_CASHIER])->andWhere(['<>','o.trade_status',Order::ORDER_FLOW_CANCEL]); if (get_md_id()) { $query->andWhere(['o.md_id' => get_md_id()]); } if (!is_null($this->order_type) && in_array($this->order_type, [0, 1, 2, 3])) { $query->andWhere([ 'o.order_type' => $this->order_type ]); } $admin = get_admin(); $admin_id = $admin->id; if ($admin->username == 'admin') { $admin_id = null; } if ($admin_id) { $salesman_id = $this->salesman_id; $salesman_where = [ 'admin_id' => $admin_id, 'is_delete' => 0 ]; if ($salesman_id) { $salesman_where['id'] = $salesman_id; } $ids = Salesman::find()->where($salesman_where)->select('id')->asArray()->all(); $admin_model = Admin::findOne($admin_id); $area_level = $admin_model->area_level; $store_query = Store::find()->where(['is_delete' => 0]); if ($this->salesman_id) { $store_query->andWhere(['in', 'salesman_id', array_column($ids, 'id')]); } else { if($area_level == 1){ $store_query->andWhere([ 'or', ['province_id' => $admin_model->province_id, 'city_id' => $admin_model->city_id, 'district_id' => $admin_model->district_id], ['admin_id' => $admin_id], ['in', 'salesman_id', array_column($ids, 'id')] ]); } elseif ($area_level == 2){ $store_query->andWhere([ 'or', ['province_id' => $admin_model->province_id, 'city_id' => $admin_model->city_id], ['admin_id' => $admin_id], ['in', 'salesman_id', array_column($ids, 'id')] ]); } elseif ($area_level == 3){ $store_query->andWhere([ 'or', ['province_id' => $admin_model->province_id], ['admin_id' => $admin_id], ['in', 'salesman_id', array_column($ids, 'id')] ]); } else { $store_query->andWhere([ 'or', ['admin_id' => $admin_id], ['in', 'salesman_id', array_column($ids, 'id')] ]); } } $ids = $store_query->select('id')->column(); } if (!empty($this->name)) { $query->andWhere(['o.name' => $this->name]); } if (!empty($ids)) { $query->andWhere(['o.store_id' => $ids]); } else { $query->andWhere([ 'o.store_id' => $this->store_id ]); } $query->leftJoin(['u' => User::tableName()], 'u.id = o.user_id') ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding') ->leftJoin(['od' => OrderDetail::tableName()], 'od.order_id=o.id'); switch ($this->status) { case 0: $query->andWhere(['o.is_delete' => Order::IS_DELETE_FALSE, 'is_pay' => Order::IS_PAY_FALSE]); break; case 1: $query->andWhere([ 'o.trade_status' => Order::ORDER_FLOW_NO_SEND, 'o.is_delete' => Order::IS_DELETE_FALSE ])->andWhere(['or', ['o.is_pay' => Order::IS_PAY_TRUE], ['o.pay_type' => Order::PAY_TYPE_COD]]); break; case 2: $query->andWhere([ 'o.trade_status' => Order::ORDER_FLOW_SEND, 'o.is_delete' => Order::IS_DELETE_FALSE ])->andWhere(['or', ['o.is_pay' => Order::IS_PAY_TRUE], ['o.pay_type' => Order::PAY_TYPE_COD]]); break; case 3: $query->andWhere([ 'o.trade_status' => Order::ORDER_FLOW_CONFIRM, 'o.is_delete' => Order::IS_DELETE_FALSE ])->andWhere(['or', ['o.is_pay' => Order::IS_PAY_TRUE], ['o.pay_type' => Order::PAY_TYPE_COD]]); break; case 4: break; case 5: $query->andWhere(['or', ['o.trade_status' => Order::ORDER_FLOW_CANCEL], ['o.is_delete' => Order::IS_DELETE_TRUE]]); break; case 6: $query->andWhere(['and',['o.apply_delete' => Order::ORDER_APPLY_DELETE], ['!=', 'o.trade_status', Order::ORDER_FLOW_CANCEL]]); break; default: if (empty($this->order_id)) { $query->andWhere([ 'o.is_delete' => Order::IS_DELETE_FALSE ]); } break; } if (get_params('is_recycle')) { $query->andWhere([ 'o.is_recycle' => Order::IS_DELETE_TRUE ]); } else { $query->andWhere([ 'o.is_recycle' => Order::IS_DELETE_FALSE ]); } if ($this->order_id && $this->order_id > 0) { $query->andWhere(['o.id' => $this->order_id]); } // TODO: $commonOrderSearch = new CommonOrderSearch(); if (!$this->order_id) { // $query = $commonOrderSearch->search($query, $this); // $query = $commonOrderSearch->keyword($query, $this->keywordType, $this->keyword); if ($this->md_name) { $query->leftJoin(['md' => Md::tableName()], 'md.id=o.md_id')->andWhere(['like', 'md.name', $this->md_name]); } if ($this->order_no) { $query->andWhere(['OR', ['like', 'o.order_no', $this->order_no], ['like', 'su.name', $this->order_no], ['like', 'su.mobile', $this->order_no], ['like', 'o.address', $this->order_no] ]); } if ($this->mobile) { $query->andWhere([ 'or', ['like', 'o.mobile', $this->mobile], ['like', 'u.binding', $this->mobile], ]); } if ($this->goods_name) { $query->andWhere(['like', 'od.goods_name', $this->goods_name]); } if ($this->user_name) { $query->andWhere(['or', ['like', 'o.name', $this->user_name], ['like', 'su.name', $this->user_name]]); } } if ($this->dateStart) { $query->andWhere(['>=', 'o.created_at', strtotime($this->dateStart)]); } if ($this->dateEnd) { $query->andWhere(['<=', 'o.created_at', strtotime($this->dateEnd)]); } if (!$this->order_id) { // $query->andWhere(['o.is_offline' => $this->is_offline]); } if ($this->platform !== null && $this->platform !== '') { $query->andWhere([ 'u.platform' => $this->platform ]); } if($this->book_name){ $query->andWhere(['like', 'o.book_info', $this->book_name]); } if($this->book_mobile){ $query->andWhere(['like', 'o.book_info', $this->book_mobile]); } if($this->md_id){ $query->andWhere(['o.md_id' => $this->md_id]); } $refundQuery1 = OrderRefund::find()->alias('or') ->select('or.status, or.order_id, or.created_at') ->where(['or.store_id' => $this->store_id, 'or.is_delete' => 0]); $refundQuery = (new Query())->from(['or' => $refundQuery1])->where('`or`.order_id=o.id') ->select('or.status') ->orderBy(['or.created_at' => SORT_DESC]) ->limit(1); $query->orderBy('o.id DESC') ->select(['o.*', 'su.name as nickname', 'u.nickname as de_name', 'u.platform', 'u.binding', 'u.avatar_url', 'refund' => $refundQuery, 'od.delivery_type', 'od.attr', 'o.name user_name', 'o.mobile user_mobile', 'o.address user_address', 'su.avatar', 'od.order_transit_id']); $query->groupBy('o.id'); $pagination = pagination_make($query); $listArray = $pagination['list']; foreach ($listArray as $i => &$item) { $item['name'] = $item['name'] ?: $item['nickname']; $item['mobile'] = $item['mobile'] ?: $item['binding']; $item['book'] = []; if ($item['order_type'] == 1) { $attr = Json::decode($item['attr']); if (count($attr) > 1) { $date_data = current($attr)['date'] . '/' . end($attr)['date']; } else { $date_data = $attr[0]['date']; } $item['book'] = [ 'date' => $date_data ]; } if (empty($item['binding'])) { $item['nickname'] = $item['de_name']; $item['avatar'] = $item['avatar_url']; } if ($item['order_type'] == 2) { $tmp_attr = Json::decode($item['attr']); $item['book']['date'] = $tmp_attr['date'] . ' ' . $tmp_attr['time']; } $rmList = []; if ($this->order_id && $this->order_id > 0) { $rmQuery = \app\models\OrderRefundMoney::find()->where(['is_delete' => 0, 'order_id' => $this->order_id]); $rmList = $rmQuery->orderBy('id DESC')->asArray()->all(); foreach($rmList as &$k){ $k['created_at'] = date('Y-m-d H:i:s', $k['created_at']); } } $item['rmList'] = $rmList; if (!empty($item['food_flag_id'])) { $food_flag = FoodFlag::findOne($item['food_flag_id']); $item['food_table_num'] = $food_flag->table_num; } $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']); $item['goods_list'] = $this->getOrderGoodsList($item['id']); $item['clerk_name'] = ''; $shop = Shop::findOne($item['shop_id']); if ($shop) { $user = User::findOne(Shop::findOne($item['shop_id'])->user_id); if ($user) { $item['clerk_name'] = $user->nickname; } } $item['integral'] = json_decode($item['integral'], true); if (isset($item['address_data'])) { $item['address_data'] = json_decode($item['address_data']); } $item['flag'] = 0; $item['delivery_info'] = []; // 同城配送配送信息 if ($item['delivery_type'] == OrderDetail::GOODS_DELIVERY_IM) { $delivery_info = DeliveryInfo::findOne(['order_no' => $item['order_no']]); $item['delivery_info'] = [ 'waybill_id' => $delivery_info->waybill_id, 'status' => Delivery::$validOrderStatus[$delivery_info->status], 'reason' => $delivery_info->reason_id > 0 ? Delivery::$validReason[$delivery_info->reason_id] : '', 'fee' => $delivery_info->fee, 'duct_fee' => $delivery_info->deduct_fee, 'delivery_type' => Delivery::$deliveryType[$delivery_info->delivery_type], 'rider_name' => $delivery_info->rider_name, 'rider_mobile' => $delivery_info->rider_mobile ]; } $item['md_info'] = null; if ($item['md_id']) { $item['md_info'] = Md::find()->where(['id' => $item['md_id']]) ->select('id, name, address, cover_url, start_time, end_time, mobile, open_status, shop_time_type, is_single')->one(); } $item['pay_user_info'] = null; if ($item['pay_user_id'] > 0) { $pay_saas_user = SaasUser::findOne(['mobile' => User::findOne($item['pay_user_id'])->binding]); $item['pay_user_info'] = [ 'name' => $pay_saas_user->name, 'mobile' => $pay_saas_user->mobile ]; } $item['all_discount'] = sprintf("%.2f", ($item['integral']['forehead'] + $item['coupon_sub_price'])); $item['alipay_activity_voucher'] = AlipayAcitvityVoucherOrder::getByOrder($item['alipay_trade_no']); if ($item['store_id']) { $item['store'] = Store::find()->where(['id' => $item['store_id']])->select('id, name, province_id, city_id, district_id, address')->one(); } if (empty($item['address']) && (int)$item['is_offline']) { if (!empty($item['store'])) { $item['address'] = '配送到门店:' . $item['store']['name']; } if (!empty($item['md_info'])) { $item['address'] = '配送到门店:' . $item['md_info']['name']; } } } //处理订单是否存在云仓转单商品结束 return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'data' => $listArray, 'pageNo' => $pagination['pageNo'], 'totalCount' => $pagination['totalCount'], ], ]; } public function orderRefund(){ $order = Order::find()->where(['order_origin'=>Order::ORDER_SOURCE_CASHIER,'id'=>$this->order_id])->one(); if(empty($order)){ return ['code' => 1,'msg' => '订单不存在']; } if($order->trade_status == Order::ORDER_FLOW_CANCEL){ return ['code' => 1,'msg' => '订单已取消']; } if($order->is_pay != 1){ return ['code' => 1,'msg' => '订单未支付']; } $order_detail_list = OrderDetail::find()->where(['order_id' => $order->id, 'is_delete' => Order::IS_DELETE_FALSE])->all(); $t = \Yii::$app->db->beginTransaction(); $other_msg = " "; // 恢复库存 foreach ($order_detail_list as $order_detail) { if($order->order_type == 1){ continue; } $goods = Goods::findOne($order_detail->goods_id); $attr_id_list = []; foreach (json_decode($order_detail->attr) as $item) { array_push($attr_id_list, $item->attr_id); } // $goods->numAdd($attr_id_list, $order_detail->num); if (!in_array($order->order_type, [1, 2])) { if (!$goods->numAdd($attr_id_list, $order_detail->num)) { // $t->rollBack(); // return [ // 'code' => 1, // 'msg' => '订单取消失败,库存操作失败', // ]; $other_msg .= "库存操作失败"; } } } // 用户积分恢复 if ($order->pay_type !== Order::PAY_TYPE_CASHIER_PURSE) { $integral = json_decode($order->integral)->forehead_integral; $user = User::findOne(['id' => $order->user_id]); if (! $user) { return [ 'code' => 1, 'msg' => '用户数据未找到', ]; } if ($integral > 0) { $desc = "商城订单取消, 订单号:{$order->order_no}"; AccountLog::saveLog($user->id, $order->pay_price, AccountLog::TYPE_INTEGRAL, AccountLog::LOG_TYPE_INCOME, AccountLog::TYPE_PLATFORM_ORDER, $order->id, $desc); } } // 余额支付 退换余额 if ($order->is_pay == Order::IS_PAY_TRUE && $order->pay_type == Order::PAY_TYPE_BALANCE_PAID) { $desc = "商城订单退款,订单号:{$order->order_no}"; AccountLog::saveLog($user->id, $order->pay_price, AccountLog::TYPE_BALANCE, AccountLog::LOG_TYPE_INCOME, AccountLog::TYPE_PLATFORM_REFUND_ORDER, $order->id, $desc); } $order->trade_status = Order::ORDER_FLOW_CANCEL; if($order->pay_type == 1 || $order->pay_type == 4){ $refund_res = Refund::refund($order, $order->order_union_id ? OrderNo::ORDER_UNION : '',$order->order_no, $order->pay_price); if ($refund_res !== true) { $t->rollBack(); return $refund_res; } } if($order->save()){ // 记录操作日志 CashierActionLog::setLog($this->store_id,get_user_id(),CashierActionLog::REFUND_ORDER,'订单'.$order->order_no.'退款', $order->md_id); $t->commit(); return [ 'code' => 0, 'msg' => '订单退款成功' . $other_msg ]; }else{ $t->rollBack(); return [ 'code' => 1, 'msg' => '订单退款失败' ]; } } private function export($list) { $rows = [[ 'ID', '会员信息', '订单号', '订单状态', '总金额(元)', '运费', '优惠金额', '实际支付(元)', '是否已付款', '快递名称', '快递单号', '快递状态', '收货人', '收货人电话', '收货人地址', '下单门店', '商品', '时间', ]]; foreach($list as $item){ $tradStatus = ''; if($item['order_type'] == 1){ if(in_array($item['trade_status'], [0,2])){ $tradStatus = '待使用'; } } if ($item['trade_status'] == 1) { $tradStatus = '已取消'; } if ($item['is_pay'] == 0) { $tradStatus = '未支付'; } if ($item['is_pay'] == 1 && $item['trade_status']== 0) { $tradStatus = '待发货'; } if ($item['is_pay'] == 1 && $item['trade_status'] == 2) { $tradStatus = '待收货'; } if ($item['is_pay'] == 1 && $item['trade_status'] == 3) { $tradStatus = '已完成'; } $goods = []; foreach ($item['goods_list'] as $i => $gitem) { $attr = []; foreach ($gitem['attr_list'] as $atitem) { $attr[] = isset($atitem['attr_name']) ? $atitem['attr_name'] : ''; } $goods[] = $gitem['name'] . '(' . implode(',', $attr) . ')' . '(' . $gitem['num'] . $gitem['unit'] . ')' . '(' . $gitem['total_price'] . '元)'; } $r = [ $item['id'], $item['nickname'], $item['order_no'], $tradStatus, $item['total_price'], $item['express_price'], $item['all_discount'], $item['pay_price'], $item['is_pay'] ? '已付款' : '未付款', $item['express'], $item['express_no'], $item['status_text'], $item['name'], $item['mobile'], $item['address'], (($item['md_info'] && $item['md_info']['name']) ? $item['md_info']['name'] : '') . '-' . $item['store']['name'], '【' . implode('】,【', $goods) . '】', $item['created_at'], ]; $rows[] = $r; } $writer = \Spatie\SimpleExcel\SimpleExcelWriter::streamDownload(time() . '.xlsx')->noHeaderRow() ->addRows($rows)->toBrowser(); } //获取快递信息 public function getExpressData() { try { $order_id = $this->order_id; $order = Order::find()->where(['id' => $order_id, 'is_pay' => 1])->select("express, express_no, mobile")->one(); if ($order) { $OrderTransit = OrderTransit::findOne(['order_id' => $order->id]); if ($OrderTransit) { $order['express_name'] = $OrderTransit->express_name; $order['express_no'] = $OrderTransit->express_no; } } if (!empty($order['express']) && !empty($order['express_no'])) { // 查询物流信息 $model = new ExpressDetail(); $model->express = $order['express']; $model->express_no = $order['express_no']; $model->receive_mobile = $order['mobile']; $model->store_id = $this->store_id; $res = $model->search(); if ($res['code'] != 0) { $res['code'] = 0; if (!$res['data']) { $res['data'] = []; } $res['data']['status'] = 0; $res['data']['status_text'] = '未知'; } $res['data']['express'] = $order['express']; $res['data']['express_no'] = $order['express_no']; return $res; } return [ 'code' => 0, 'msg' => "没有快递信息", 'data' => null ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * @param $data array 需要处理的数据 */ public function dataTransform($data) { //TODO 测试数据 需要换成真实的字段 $newFields = []; foreach ($this->fields as &$item) { if ($this->is_offline == 1) { if (in_array($item['key'], ['clerk_name', 'shop_name'])) { $item['selected'] = 1; } } else { if (in_array($item['key'], ['express_price', 'express_no', 'express'])) { $item['selected'] = 1; } } if (isset($item['selected']) && $item['selected'] == 1) { $newFields[$item['key']] = $item['value']; } } $newList = []; foreach ($data as $datum) { $newItem = []; $newItem['order_no'] = $datum->order_no; $newItem['nickname'] = $datum->user->nickname; $newItem['name'] = $datum->name; $newItem['mobile'] = $datum->mobile; $newItem['address'] = $datum->address; $newItem['total_price'] = $datum->total_price; $newItem['pay_price'] = $datum->pay_price; $newItem['pay_time'] = $datum->pay_time ? date('Y-m-d H:i', $datum->pay_time) : ''; $newItem['send_time'] = $datum->send_time ? date('Y-m-d H:i', $datum->send_time) : ''; $newItem['confirm_time'] = $datum->confirm_time ? date('Y-m-d H:i', $datum->confirm_time) : ''; $newItem['words'] = $datum->words; $newItem['goods_list'] = $this->getOrderGoodsList($datum['id']); $newItem['is_pay'] = $datum['is_pay'] == 1 ? "已付款" : "未付款"; $newItem['addtime'] = date('Y-m-d H:i', $datum['created_at']); $newItem['express_price'] = $datum['express_price'] . "元"; $newItem['apply_delete'] = ($datum['apply_delete'] == Order::ORDER_APPLY_DELETE) ? ($datum['trade_status'] == Order::ORDER_FLOW_CANCEL || $datum['is_delete'] == Order::IS_DELETE_TRUE ? '取消成功' : '取消中') : "无"; $newItem['trade_status'] = OrderStateFlow::getFlowWords($datum['trade_status']); // 是否到店自提 0--否 1--是 if ($datum['is_offline']) { $newItem['clerk_name'] = $datum->clerk ? $datum->clerk->nickname : ''; $newItem['shop_name'] = $datum->shop ? $datum->shop->name : ''; } else { $newItem['express_price'] = $datum->express_price; $newItem['express_no'] = $datum->express_no; $newItem['express'] = $datum->express; } if ($datum->orderForm) { $str = ''; foreach ($datum->orderForm as $key => $item) { $str .= $item['key'] . ':' . $item['value'] . ','; } $newItem['content'] = rtrim($str, ','); } else { $newItem['content'] = $datum->content; } $newList[] = $newItem; } //Export::order_3($newList, $newFields); } public function getOrderGoodsList($order_id) { $orderDetailList = OrderDetail::find()->alias('od') ->where([ 'od.is_delete' => 0, 'od.order_id' => $order_id, ])->select(['od.num', 'od.total_price', 'od.attr', 'od.is_level', 'od.batch_price_tips', 'name' => 'od.goods_name','od.pic', 'od.goods_info', 'od.delivery_type', 'od.shop_id', 'od.goods_id'])->asArray()->all(); foreach ($orderDetailList as &$item) { $item['form'] = OrderForm::findAll(['goods_id' => $item['goods_id'], 'order_id' => $order_id]); $item['attr_list'] = json_decode($item['attr'], true); $item['unit'] = Json::decode($item['goods_info'])['unit']; $item['price'] = $item['total_price'] == 0 ? 0 : bcdiv($item['total_price'],$item['num'],2); $goods = Goods::findOne($item['goods_id']); $warn_goods_timeout = Goods::warn_goods_timeout($goods->store_id); $item['timeout_day'] = $goods->time_made_day ? date('Y-m-d', $goods->time_made_day + 86400 * $goods->time_shelf_life) : '--'; $item['is_warn_goods_timeout'] = ($goods->time_made_day && ($goods->time_made_day + 86400 * $goods->time_shelf_life) < (time() + $warn_goods_timeout * 86400)) ? 1 : 0; $item['shop'] = []; if ($item['delivery_type'] == OrderDetail::GOODS_DELIVERY_SHOP) { $shop_info = Shop::find()->where(['id' => $item['shop_id']])->asArray()->one(); if(!$shop_info) continue; $item['shop']['address'] = $shop_info['address']; $item['shop']['id'] = $shop_info['id']; $item['shop']['name'] = $shop_info['name']; $item['shop']['mobile'] = $shop_info['mobile']; } } return $orderDetailList; } public static function getCountData($store_id) { $form = new OrderListForm(); $form->limit = 0; $form->store_id = $store_id; $data = []; $form->status = -1; $res = $form->search(); $data['all'] = $res['row_count']; $form->status = 0; $res = $form->search(); $data['status_0'] = $res['row_count']; $form->status = 1; $res = $form->search(); $data['status_1'] = $res['row_count']; $form->status = 2; $res = $form->search(); $data['status_2'] = $res['row_count']; $form->status = 3; $res = $form->search(); $data['status_3'] = $res['row_count']; $form->status = 5; $res = $form->search(); $data['status_5'] = $res['row_count']; return $data; } /** * @return array */ private function getExpressList() { $storeExpressList = Order::find() ->select('express') ->where([ 'and', ['store_id' => $this->store_id], ['is_pay' => Order::IS_PAY_TRUE], ['in', 'trade_status', [Order::ORDER_FLOW_SEND, Order::ORDER_FLOW_CONFIRM]], ['!=', 'express', ''], ])->groupBy('express, send_time')->orderBy('send_time DESC')->limit(5)->asArray()->all(); $expressLst = Express::getExpressList(); $newStoreExpressList = []; foreach ($storeExpressList as $i => $item) { foreach ($expressLst as $value) { if ($value['name'] == $item['express']) { $newStoreExpressList[] = $item['express']; break; } } } $newPublicExpressList = []; foreach ($expressLst as $i => $item) { $newPublicExpressList[] = $item['name']; } return array_merge($newStoreExpressList, $newPublicExpressList); } }