-1], [['page',], 'default', 'value' => 1], [['dateStart', 'dateEnd', 'express_type', 'md_name', 'order_no', 'goods_name', 'mobile'], 'trim'], [['seller_comments', 'flag', 'user_name'], 'string'], [['fields', 'is_delivery', 'shop_list', 'book_name', 'book_mobile'], 'safe'] ]; } public function search($is_scan_pay = false) { if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; } $query = OrderPospal::find()->alias('o')->where([ 'o.store_id' => $this->store_id ])->andWhere(['!=','order_type', Order::ORDER_TYPE_Adopt]); 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 ]); } $query->leftJoin(['u' => User::tableName()], 'u.id = o.user_id') ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding') ->leftJoin(['od' => OrderPospalDetail::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(['like', 'o.order_no', $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(['like', 'o.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->flag == Export::EXPORT) { $query_ex = clone $query; $list_ex = $query_ex; $export = new ExportList(); // $export->is_offline = $this->is_offline; $export->order_type = 0; $export->fields = $this->fields; $export->dataTransform_new($list_ex); } // 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]); } // $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.created_at DESC') ->select(['o.*', 'su.name as nickname', 'u.nickname as de_name', 'u.platform', '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.order_no'); $pagination = pagination_make($query); $listArray = $pagination['list']; foreach ($listArray as $i => &$item) { $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 ($item['order_type'] == 2) { $tmp_attr = Json::decode($item['attr']); $item['book']['date'] = $tmp_attr['date'] . ' ' . $tmp_attr['time']; } 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::findOne($item['md_id']); } $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'])); } // 获取可导出数据 $f = new ExportList(); $exportList = $f->getList(); //处理订单是否存在云仓转单商品开始 foreach($listArray as $listkey=>&$listgoodsInfo){ //添加转单详情 //一个订单一个供货商只能创建一次转单 // $transitInfo = OrderTransit::find()->where(['order_id'=>$listgoodsInfo['id'],'is_delete'=>0])->all(); // if($transitInfo){ // $orderTransitInfo = []; // foreach($transitInfo as $transitInfoVal){ // $orderTransitInfo[] = $transitInfoVal->attributes; // } // $listArray[$listkey]['orderTransitInfo'] = $orderTransitInfo; // } $verify_card = []; $integral = 0; if ((int)$listgoodsInfo['is_pay'] !== 0) { foreach($listgoodsInfo['goods_list'] as $goodskey=>&$goods){ if($goods['goods_id'] && $goods['attr']){ $goods_attr = json_decode($goods['attr'],true); //获取商品组对比属性信息 $cloud_attrs = ''; foreach($goods_attr as $val){ if(isset($val['attr_id'])){ $cloud_attrs .= $val['attr_id']."-"; } } $cards = Goods::find()->where(['id' => $goods['goods_id']])->select('verify_card_id, integral')->one(); if (!empty($cards->verify_card_id)) { $goods_model = Goods::findOne($goods['goods_id']); $ids = explode(',', $cards->verify_card_id); $order = OrderPospal::findOne($listgoodsInfo['id']); $card = Tools::getVerifyList($ids, $order, $goods_model); $verify_card = array_merge($verify_card, $card); } if (!empty($cards->integral)) { $goods_integral = json_decode($cards->integral, true); $integral += $goods_integral['give']; } $cloudGoodsInfo = CloudGoodsBind::find()->where(['goods_id'=>$goods['goods_id'],'is_delete'=>0])->one(); if($cloudGoodsInfo && isset($cloudGoodsInfo['cloud_goods_id'])){ $cloudInfo = []; $cloudInfo['cloud_goods_id'] = $cloudGoodsInfo['cloud_goods_id']; $cloudInfo['cloud_attr_id'] = $cloudGoodsInfo['cloud_attr_id']; $cloudInfo['cloud_supplier_id'] = $cloudGoodsInfo['cloud_supplier_id']; if( $cloudGoodsInfo['cloud_supplier_id'] > 0){ $supplierInfo = Supplier::find()->select('name,logo')->where(['cloud_supplier_id'=>$cloudGoodsInfo['cloud_supplier_id'],'is_delete'=>0])->one(); if($supplierInfo && $supplierInfo['name'] && $supplierInfo['logo']){ $cloudInfo['cloud_supplier_name'] = $supplierInfo['name']; $cloudInfo['cloud_supplier_logo'] = $supplierInfo['logo']; } } $listArray[$listkey]['goods_list'][$goodskey]['cloud_bind'] = json_encode($cloudInfo); $listArray[$listkey]['is_cloud_bind'] = true; } } } $listgoodsInfo['verify_card'] = $verify_card; $listgoodsInfo['integral'] = $integral; } } $smf = new ShareOrderForm(['yinbao' => 1]); $smf->store_id = $this->store_id; foreach($listArray as &$item){ $item['order_type'] = 's'; $item['parent_id_3'] = $item['parent_id_2']; $item['parent_id_2'] = $item['parent_id_1']; $item['parent_id_1'] = $item['parent_id']; } $list = $smf->transform($listArray); foreach($list as &$item){ $item['goods_list'] = $this->getOrderGoodsList($item['id']); } //处理订单是否存在云仓转单商品结束 return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'export_list' => $exportList, 'express_list' => $this->getExpressList(), 'data' => $list, 'pageNo' => $pagination['pageNo'], 'totalCount' => $pagination['totalCount'], // 'sql' => $query->createCommand()->getRawSql(), ], ]; } //获取快递信息 public function getExpressData() { try { $order_id = $this->order_id; $order = OrderPospal::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 = OrderPospalDetail::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'] = []; $item['attr_list'] = json_decode($item['attr'], true); $item['unit'] = Json::decode($item['goods_info'])['unit']; $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']; } } if(!$orderDetailList){ $orderDetailList = [ [ "goods_name" => "未知", "num" => "1", "total_price" => "0.00", "integral_price" => "0", "integral_difference_price" => "0.00", "created_at" => "0", "is_delete" => "0", "attr" => "[{\"attr_group_id\":\"2\",\"attr_group_name\":\"规格\",\"attr_id\":\"3\",\"attr_name\":\"默认\"}]", "pic" => "https://www.gqp58.com/web/v1/statics/images/yinbao.png", "integral" => "0", "is_level" => "1", "batch_price_tips" => null, "supplier_price" => "0.00", "goods_info" => "", "delivery_type" => "1", "shop_id" => "0", "is_refund" => "0", "profit" => "0.00", "user_id" => "0", "order_transit_id" => "0", "video_id" => "0", "name" => "未知", "unit" => "件", "attr_list" => [ [ "attr_group_id" => "2", "attr_group_name" => "规格", "attr_id" => "3", "attr_name" => "默认" ] ] ] ]; } return $orderDetailList; } public static function getCountData($store_id) { $form = new OrderPospalListForm(); $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 = OrderPospal::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); } }