where(['type' => Admin::ADMIN_TYPE_FRONT_AGENT, 'is_delete' => 0]); if($this->admin['username'] != 'admin' && $this->admin['type'] != Admin::ADMIN_TYPE_SUPPLIER && $this->admin['type'] != Admin::ADMIN_TYPE_SAAS_STAFF){ $query1 = (new Query())->select('admin_id')->from(AgentBind::tableName())->where(['type' => 2, 'parent_admin_id' => $this->admin_id]); $query->andWhere(['id' => $query1]); unset($query1); } if($this->name){ $query->andWhere(['like', 'name', $this->name]); } $query->orderBy('id desc'); if($getNameList){ return [ 'code' => 0, 'data' => $query->select(['id', 'name'])->all(), // 'sql' => $query->createCommand()->getRawSql(), ]; } $res = pagination_make($query); foreach ($res['list'] as &$item) { $district = District::find()->where(['id' => [$item['province_id'], $item['city_id'], $item['district_id']]])->select('name')->column(); $district = implode('', $district); $item['address'] = $district . $item['address']; unset($item['password']); $item['supplier_count'] = AgentFrontBind::find()->where(['is_delete' => 0, 'type' => 0, 'status' => 1, 'front_agent_admin_id' => $item['id']])->count(); } return [ 'code' => 0, 'msg' => 'success', 'data' => $res, // 'sql' => $query->createCommand()->getRawSql(), ]; } //前置仓绑定关系(商家)列表 public function bindStoreList() { $agentId = $this->admin['id']; if($this->admin['username'] == 'admin'){ $agentId = null; } if($this->admin['type'] == Admin::ADMIN_TYPE_FRONT_AGENT){ $agentId = null; $this->front_agent_admin_id = $this->admin['id']; } $query = AgentFrontBind::find()->where(['type' => 1, 'is_delete' => 0]); if($this->status >= 0){ $query->andWhere(['status' => $this->status]); } if($this->store_id > 0){ $query->andWhere(['type_id' => $this->store_id]); } if($this->admin['type'] == Admin::ADMIN_TYPE_STORE){ $query->andWhere(['type_id' => $this->admin['type_id']]); } if($this->type == 1){ $query->andWhere(['>', 'md_id', 0]); } if($this->front_agent_admin_id){ $front_agent_admin_id = intval($this->front_agent_admin_id); }else{ if($this->admin['username'] != 'admin' && $this->admin['type'] != Admin::ADMIN_TYPE_STORE){ $front_agent_admin_id = (new Query())->select('admin_id')->from(AgentBind::tableName())->where(['type' => 2, 'parent_admin_id' => $this->admin_id]); } } isset($front_agent_admin_id) && $query->andWhere(['front_agent_admin_id' => $front_agent_admin_id]); if($this->front_agent_admin_name){ $front_agent_admin_name = (new Query())->select('id')->from(Admin::tableName()) ->where(['type' => Admin::ADMIN_TYPE_FRONT_AGENT]) ->andWhere(['like', 'name', $this->front_agent_admin_name]); $query->andWhere(['front_agent_admin_id' => $front_agent_admin_name]); } $query1 = (new Query())->select('id')->from(Store::tableName()); $query1->andWhere(['is_delete' => 0]); if($this->store_name){ $query1->andWhere(['like', 'name', $this->store_name]); } if($query1->where){ $query->andWhere(['type_id' => $query1]); } unset($query1); // $query1 = (new Query())->select('id')->from(Md::tableName()); // $query1->andWhere(['is_delete' => 0]); // if($this->md_name){ // $query1->andWhere(['like', 'name', $this->md_name]); // } // if($query1->where){ // $query->andWhere(['md_id' => $query1]); // } // unset($query1); $query->orderBy('id desc'); $res = pagination_make($query); foreach ($res['list'] as &$item) { $item['store_info'] = Store::find()->where(['id' => $item['type_id']])->asArray()->one(); $item['front_agent'] = Admin::findOne($item['front_agent_admin_id']); $item['md_info'] = Md::find()->where(['id' => $item['md_id']])->select('id, name, cover_url, mobile')->asArray()->one(); // if($this->type == 1){ // //做兼容前端 没法区分修改还是添加 // $item['front_agent_admin_id'] = $item['id']; // } $item['is_cloud_delivery'] = intval($item['is_cloud_delivery']); } return [ 'code' => 0, 'msg' => 'success', 'data' => $res, // 'sql' => $query->createCommand()->getRawSql(), ]; } //前置仓绑定关系(供货商)列表 public function bindList() { if($this->admin['type'] == Admin::ADMIN_TYPE_FRONT_AGENT){ $this->front_agent_admin_id = $this->admin['id']; } $query = AgentFrontBind::find()->where(['type' => 0, 'is_delete' => 0]); if($this->status >= 0){ $query->andWhere(['status' => $this->status]); } if($this->supplier_id > 0){ $query->andWhere(['type_id' => $this->supplier_id]); } if($this->store_id > 0){ $query->andWhere(['type_id' => $this->store_id]); } if($this->admin['type'] == Admin::ADMIN_TYPE_SUPPLIER){ $query->andWhere(['type_id' => $this->admin['type_id']]); } if($this->front_agent_admin_id){ $front_agent_admin_id = intval($this->front_agent_admin_id); }else{ if($this->admin['username'] != 'admin' && $this->admin['type'] != Admin::ADMIN_TYPE_SUPPLIER){ $front_agent_admin_id = (new Query())->select('admin_id')->from(AgentBind::tableName())->where(['type' => 2, 'parent_admin_id' => $this->admin_id]); } } isset($front_agent_admin_id) && $query->andWhere(['front_agent_admin_id' => $front_agent_admin_id]); if($this->front_agent_admin_name){ $front_agent_admin_name = (new Query())->select('id')->from(Admin::tableName()) ->where(['type' => Admin::ADMIN_TYPE_FRONT_AGENT]) ->andWhere(['like', 'name', $this->front_agent_admin_name]); $query->andWhere(['front_agent_admin_id' => $front_agent_admin_name]); } $query1 = (new Query())->select('id')->from(Supplier::tableName()); if($this->supplier_name){ $query1->andWhere(['like', 'supplier_name', $this->supplier_name]); } if($this->supplier_phone){ $query1->andWhere(['like', 'phone', $this->supplier_phone]); } if($query1->where){ $query->andWhere(['type_id' => $query1]); } unset($query1); $query->orderBy('id desc'); $res = pagination_make($query); foreach ($res['list'] as &$item) { $item['supplier_info'] = Supplier::findOne($item['type_id']); $item['front_agent'] = Admin::findOne($item['front_agent_admin_id']); } return [ 'code' => 0, 'msg' => 'success', 'data' => $res, // 'sql' => $query->createCommand()->getRawSql(), ]; } //前置仓绑定关系(供货商/商家) type = 1店铺 0供货商 public function bind($id = 0, $status = 0, $type = AgentFrontBind::TYPE_SUPPLIER) { try{ if(empty($this->front_agent_admin_id)){ throw new \Exception('前置仓不能为空'); } $is_cloud_delivery = $this->is_cloud_delivery; $type_id = 0; if($type == AgentFrontBind::TYPE_SUPPLIER){ //供货商后台操作 if(empty($this->supplier_id)){ if($this->admin['type'] == Admin::ADMIN_TYPE_SUPPLIER){ $this->supplier_id = $this->admin['type_id']; } } if(empty($this->supplier_id)){ throw new \Exception('供货商不能为空'); } $type_id = intval($this->supplier_id); } if($type == AgentFrontBind::TYPE_STORE){ if(empty($this->store_id)){ throw new \Exception('店铺不能为空'); } $type_id = intval($this->store_id); } $hasWhere = [ 'front_agent_admin_id' => $this->front_agent_admin_id, 'type_id' => $type_id, 'type' => $type, 'is_delete' => 0, // 'status' => 1, ]; //需求需要一个店铺绑定多个仓库 // if($type == 1){ // unset($hasWhere['front_agent_admin_id']); // } if($id){ $hasWhere = ['and', $hasWhere, ['!=', 'id', $id]]; } $has = AgentFrontBind::find()->where($hasWhere)->one(); if($has){ //需求需要一个店铺绑定多个仓库 // if($type == 1){ // $has->front_agent_admin_id = $this->front_agent_admin_id; // $save = $has->save(); // if(!$save){ // throw new \Exception('操作失败,' . array_shift($has->getFirstErrors())); // } // return [ // 'code' => 0, // 'msg' => '操作成功', // ]; // } return [ 'code' => 0, 'msg' => '操作成功,无需重复绑定,之前已经绑定过', ]; } $model = $id ? AgentFrontBind::findOne($id) : new AgentFrontBind(); $model->front_agent_admin_id = $this->front_agent_admin_id; $model->type_id = $type_id; $model->type = $type; $model->status = intval($status); $model->is_cloud_delivery = intval($is_cloud_delivery); if($type == 1){ $model->status = 1; } $save = $model->save(); if(!$save){ throw new \Exception('操作失败,' . array_shift($model->getFirstErrors())); } return [ 'code' => 0, 'msg' => '操作成功', ]; }catch(\Exception $e){ \Yii::error([__METHOD__, $e]); return [ 'code' => 1, 'msg' => $e->getMessage(), ]; } } //前置仓绑定关系店铺门店 一个仓库可以绑定多个门店 public function agentFrontBindStoreMd() { try{ $id = $this->id; $store_id = $this->store_id; $front_agent_admin_id = $this->front_agent_admin_id; $md_id = $this->md_id; if(empty($front_agent_admin_id)){ throw new \Exception('未选择绑定仓库'); } if(empty($this->md_id)){ throw new \Exception('门店不能为空'); } $hasWhere = [ 'type' => AgentFrontBind::TYPE_STORE, 'is_delete' => 0, 'md_id' => $md_id ]; if($id){ $hasWhere = ['and', $hasWhere, ['!=', 'id', $id]]; } $has = AgentFrontBind::find()->where($hasWhere)->one(); if($has){ return [ 'code' => 1, 'msg' => '无需重复绑定,之前已经绑定过', ]; } $store_bind_model = AgentFrontBind::findOne([ 'type' => AgentFrontBind::TYPE_STORE, 'type_id' => $store_id, 'status' => 1, 'front_agent_admin_id' => $front_agent_admin_id, 'is_delete' => 0 ]); if (!$store_bind_model) { throw new \Exception('所选店铺未与所选仓库绑定'); } $mdInfo = Md::findOne(['id' => $md_id, 'is_delete' => 0, 'store_id' => $store_bind_model->type_id]); if (!$mdInfo) { throw new \Exception('门店信息查询失败'); } $model = AgentFrontBind::findOne([ 'type' => AgentFrontBind::TYPE_STORE, 'type_id' => $store_id, 'status' => 1, 'front_agent_admin_id' => $front_agent_admin_id, 'md_id' => $md_id, ]); if (!$model) { $model = new AgentFrontBind(); $model->md_id = $md_id; $model->type = AgentFrontBind::TYPE_STORE; $model->type_id = $store_id; $model->front_agent_admin_id = $front_agent_admin_id; } $model->status = 1; $model->is_delete = 0; if(!$model->save()){ throw new \Exception('操作失败,' . json_encode($model->errors, JSON_UNESCAPED_UNICODE)); } return [ 'code' => 0, 'msg' => '操作成功', ]; }catch(\Exception $e){ return [ 'code' => 1, 'msg' => $e->getMessage(), ]; } } public function bindStatus ($ids, $status) { try { if ($ids) { is_string($ids) && $ids = explode(',', $ids); if (in_array($status, [0, 1, 2])) { AgentFrontBind::updateAll(['status' => $status], ['and', ['in', 'id', $ids], ['is_delete' => 0]]); } } return [ 'code' => 0, 'msg' => '操作成功!' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function bindDel ($ids) { try { if ($ids) { is_string($ids) && $ids = explode(',', $ids); AgentFrontBind::updateAll(['is_delete' => 1], ['and', ['in', 'id', $ids], ['is_delete' => 0]]); } return [ 'code' => 0, 'msg' => '操作成功!' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //仓库对应的仓库订单 public function agentFrontDeliveryOrder() { $front_agent_admin_id = $this->front_agent_admin_id; $supplier_id = $this->supplier_id; $order_no = $this->order_no; $supplier_name = $this->supplier_name; $store_name = $this->store_name; $front_agent_name = $this->front_agent_name; $cloud_supplier_id = 0; if (isset($supplier_id)) { $supplier = Supplier::findOne($supplier_id); if (!$supplier) { return [ 'code' => 1, 'msg' => '供货商信息不存在' ]; } $cloud_supplier_id = $supplier->cloud_supplier_id; } $query = FrontDeliveryOrder::find()->alias('fdo') ->leftJoin(['di' => DeliveryInfo::tableName()], 'fdo.delivery_info_id = di.id') ->leftJoin(['o' => Order::tableName()], 'di.order_no = o.order_no') ->leftJoin(['sp' => Supplier::tableName()], 'fdo.cloud_supplier_id = sp.cloud_supplier_id AND sp.is_delete = 0') ->leftJoin(['s' => Store::tableName()], 'fdo.store_id = s.id') ->leftJoin(['a' => Admin::tableName()], 'fdo.front_agent_admin_id = a.id') ->where(['di.is_delete' => 0, 'di.is_store_delivery_type' => 0, 'di.is_front_delivery' => 1]); if ($cloud_supplier_id) { $query->andWhere(['fdo.cloud_supplier_id' => $cloud_supplier_id]); } if (isset($front_agent_admin_id)) { $query->andWhere(['fdo.front_agent_admin_id' => $front_agent_admin_id]); } if (trim($order_no)) { $query->andWhere(['LIKE', 'o.order_no', $order_no]); } if (trim($supplier_name)) { $query->andWhere(['LIKE', 'sp.supplier_name', $supplier_name]); } if (trim($store_name)) { $query->andWhere(['LIKE', 's.name', $store_name]); } if (trim($front_agent_name)) { $query->andWhere(['LIKE', 'a.name', $front_agent_name]); } $query->select('fdo.id, di.order_no, fdo.front_agent_admin_id, fdo.cloud_supplier_id, di.local_status, di.rider_id, fdo.store_id, o.created_at, o.is_offline, o.user_id, o.name, o.mobile, o.address, o.md_id, fdo.cloud_order_id'); $query->orderBy('fdo.id DESC'); // var_dump($query->createCommand()->getRawSql());die; //分页 $pagination = pagination_make($query); $list = $pagination['list']; foreach ($list as $index => &$item) { $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']); $item['local_status'] = intval($item['local_status']); $item['local_status_name'] = DeliveryInfo::LOCAL_STATUS_ARR[$item['local_status']]; //仓库信息 $admin = Admin::findOne($item['front_agent_admin_id']); if ($admin) { [$province, $city, $district] = District::find()->where(['id' => [$admin->province_id, $admin->city_id, $admin->district_id]]) ->select('name')->column(); } $item['front_agent_info'] = [ 'name' => $admin->name ?? '', 'address' => ($province ?? '') . ($city ?? '') . ($district ?? '') . $admin->address ?? '', 'mobile' => $admin->mobile ?? '', 'latitude' => $admin->lat ?? 0, 'longitude' => $admin->lng ?? 0, ]; //商城信息 $store = Store::findOne($item['store_id']); $item['store_info'] = [ 'name' => $store->name ?? '', 'logo' => $store->logo ?? '' ]; //派送地址信息 $user = User::findOne($item['user_id']); $item['user_address'] = [ 'name' => $item['name'] ?? '', 'mobile' => $item['mobile'] ?: $user->binding, 'address' => $item['address'] ]; if (intval($item['is_offline'])) { $md = Md::findOne($item['md_id']); $item['user_address']['address'] = $md->address ?? ''; } //供货商信息 $supplier = Supplier::findOne(['cloud_supplier_id' => $item['cloud_supplier_id'], 'is_delete' => 0]); $item['supplier_info'] = [ 'name' => $supplier->supplier_name ?? '', 'logo' => $supplier->logo ?? '', 'mobile' => $supplier->phone ?? '', ]; //骑手信息 $courier = LocalDeliveryCourier::findOne(['id' => $item['rider_id'], 'is_delete' => 0]); $item['rider_info'] = [ 'name' => $courier->real_name ?? '', 'logo' => $courier->avatar ?? '', 'mobile' => $courier->mobile ?? '', ]; //商品信息 $item['goods_info'] = AgentFrontErpInventoryLog::find()->alias('ael') ->leftJoin(['ei' => AgentFrontErpInventory::tableName()], 'ael.inventory_id = ei.id') ->leftJoin(['g' => AgentFrontErpCloudGoods::tableName()], 'g.cloud_goods_id = ei.goods_id') ->where(['ael.type' => AgentFrontErpInventoryLog::LOG_TYPE_ORDER, 'ael.order_id' => $item['cloud_order_id']]) ->groupBy('ael.id')->select('ael.num, g.goods_name, g.cover_pic, ei.attr_info') ->asArray()->all(); foreach ($item['goods_info'] as &$goods_item) { $goods_item['num'] = abs($goods_item['num']); $goods_item['attr_info'] = json_decode($goods_item['attr_info'] , true); } unset($list[$index]['name'], $list[$index]['mobile'], $list[$index]['address']); } return [ 'code' => 0, 'msg' => '', 'data' => [ 'data' => $list, 'pageNo' => $pagination['pageNo'], 'totalCount' => $pagination['totalCount'], ] ]; } /** * 仓库对应的仓库快递订单 */ public function agentFrontExpressOrder() { try { $front_agent_admin_id = $this->front_agent_admin_id; $supplier_id = $this->supplier_id; $order_no = $this->order_no; $supplier_name = $this->supplier_name; $store_name = $this->store_name; $front_agent_name = $this->front_agent_name; $cloud_supplier_id = 0; if (isset($supplier_id)) { $supplier = Supplier::findOne($supplier_id); if (!$supplier) { return [ 'code' => 1, 'msg' => '供货商信息不存在' ]; } $cloud_supplier_id = $supplier->cloud_supplier_id; } $query = FrontExpressOrder::find()->alias('feo') ->leftJoin(['o' => Order::tableName()], 'feo.order_id = o.id') ->leftJoin(['sp' => Supplier::tableName()], 'feo.cloud_supplier_id = sp.cloud_supplier_id AND sp.is_delete = 0') ->leftJoin(['s' => Store::tableName()], 'feo.store_id = s.id') ->leftJoin(['a' => Admin::tableName()], 'feo.front_agent_admin_id = a.id'); if ($cloud_supplier_id) { $query->andWhere(['feo.cloud_supplier_id' => $cloud_supplier_id]); } if (isset($front_agent_admin_id)) { $query->andWhere(['feo.front_agent_admin_id' => $front_agent_admin_id]); } if (trim($order_no)) { $query->andWhere(['LIKE', 'o.order_no', $order_no]); } if (trim($supplier_name)) { $query->andWhere(['LIKE', 'sp.supplier_name', $supplier_name]); } if (trim($store_name)) { $query->andWhere(['LIKE', 's.name', $store_name]); } if (trim($front_agent_name)) { $query->andWhere(['LIKE', 'a.name', $front_agent_name]); } $query->select('feo.id, o.order_no, feo.front_agent_admin_id, feo.cloud_supplier_id, feo.store_id, o.created_at, o.is_offline, o.user_id, o.name, o.mobile, o.address, o.md_id, feo.cloud_order_id, o.trade_status'); $query->orderBy('feo.id DESC'); //分页 $pagination = pagination_make($query); $list = $pagination['list']; foreach ($list as $index => &$item) { $item['trade_status'] = intval($item['trade_status']); $item['trade_status_text'] = Order::TRADE_STATUS_TEXT[$item['trade_status']]; $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']); //仓库信息 $admin = Admin::findOne($item['front_agent_admin_id']); if ($admin) { [$province, $city, $district] = District::find()->where(['id' => [$admin->province_id, $admin->city_id, $admin->district_id]]) ->select('name')->column(); } $item['front_agent_info'] = [ 'name' => $admin->name ?? '', 'address' => ($province ?? '') . ($city ?? '') . ($district ?? '') . $admin->address ?? '', 'mobile' => $admin->mobile ?? '', 'latitude' => $admin->lat ?? 0, 'longitude' => $admin->lng ?? 0, ]; //商城信息 $store = Store::findOne($item['store_id']); $item['store_info'] = [ 'name' => $store->name ?? '', 'logo' => $store->logo ?? '' ]; //派送地址信息 $user = User::findOne($item['user_id']); $item['user_address'] = [ 'name' => $item['name'] ?? '', 'mobile' => $item['mobile'] ?: $user->binding, 'address' => $item['address'] ]; if (intval($item['is_offline'])) { $md = Md::findOne($item['md_id']); $item['user_address']['address'] = $md->address ?? ''; } //供货商信息 $supplier = Supplier::findOne(['cloud_supplier_id' => $item['cloud_supplier_id'], 'is_delete' => 0]); $item['supplier_info'] = [ 'name' => $supplier->supplier_name ?? '', 'logo' => $supplier->logo ?? '', 'mobile' => $supplier->phone ?? '', ]; //商品信息 $item['goods_info'] = AgentFrontErpInventoryLog::find()->alias('ael') ->leftJoin(['ei' => AgentFrontErpInventory::tableName()], 'ael.inventory_id = ei.id') ->leftJoin(['g' => AgentFrontErpCloudGoods::tableName()], 'g.cloud_goods_id = ei.goods_id') ->where(['ael.type' => AgentFrontErpInventoryLog::LOG_TYPE_ORDER, 'ael.order_id' => $item['cloud_order_id']])->andWhere(['<=', 'ael.num', 0]) ->groupBy('ael.id')->select('ael.num, g.goods_name, g.cover_pic, ei.attr_info') ->asArray()->all(); foreach ($item['goods_info'] as &$goods_item) { $goods_item['num'] = abs($goods_item['num']); $goods_item['attr_info'] = json_decode($goods_item['attr_info'] , true); } unset($list[$index]['name'], $list[$index]['mobile'], $list[$index]['address']); } $f = new ExportList(); $exportList = $f->getList(); return [ 'code' => 0, 'msg' => '', 'data' => [ 'data' => $list, 'pageNo' => $pagination['pageNo'], 'totalCount' => $pagination['totalCount'], 'express_list' => Express::getExpressList(), ] ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 仓库发货仓库快递订单 */ public function agentFrontExpressSendOrder() { try { $front_agent_admin_id = $this->front_agent_admin_id; $frontExpressOrder = FrontExpressOrder::findOne([ 'cloud_order_id' => $this->order_id, 'front_agent_admin_id' => $front_agent_admin_id ]); if (!$frontExpressOrder) { throw new \Exception('订单不存在'); } $order = Order::findOne($frontExpressOrder->order_id); if (!$order) { throw new \Exception('商城订单不存在'); } if (intval($order->trade_status)) { throw new \Exception('订单已发货'); } // $supplier_token = get_supplier_token($frontExpressOrder->cloud_supplier_id); $cloud_order_id = (string)$frontExpressOrder->cloud_order_id; $express_name = $this->express_name; $express_no = $this->express_no; $zip_code = $this->zip_code; $words = $this->words; if (empty(trim($express_name))) { throw new \Exception('快递名称不能为空'); } if (empty(trim($express_no))) { throw new \Exception('快递单号不能为空'); } //云仓订单发货 云仓自动调用商城发货接口 $supplierForm = new SupplierForm(); $supplierForm->order_id = $cloud_order_id; $supplierForm->express_name = $express_name; $supplierForm->express_no = $express_no; $supplierForm->send_type = 1; $supplierForm->words = $words; $supplierForm->zip_code = $zip_code; return $supplierForm->supplierPurchaseSend($frontExpressOrder->cloud_supplier_id); } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } }