name; $mobile = $this->mobile; $username = $this->username; $status = $this->status; $start_date = $this->start_date; $end_date = $this->end_date; $admin = get_admin(); $query = StoreReOrder::find()->alias('sro')->leftJoin(['s' => Store::tableName()], 's.id = sro.store_id') ->leftJoin(['a' => Admin::tableName()], 's.admin_id = a.id') ->leftJoin(['sm' => Salesman::tableName()], 'sm.id = s.salesman_id') ->leftJoin(['su' => SaasUser::tableName()], 'su.id = sm.saas_user_id') ->leftJoin(['sp' => StorePrice::tableName()], 'sp.id = sro.store_price_id') ->where(['sro.is_delete' => 0, 's.is_delete' => 0])->select('sro.*, s.name, s.logo, a.mobile, s.category_id, su.name username, sp.name as store_price_name'); if ($admin->username !== "admin") { //获取当前代理下的 $sales_man_ids = Salesman::find()->where(['admin_id'=>$admin->id,'is_delete'=>0])->select("id")->column(); $store_ids =Store::find()->where(['is_delete'=>0]); if ($admin->id) { $store_ids->andWhere([ 'or', ['admin_id' => $admin->id], ['in', 'salesman_id', $sales_man_ids] ]); } $store_ids = $store_ids->select('id')->column(); $query->andWhere(['s.admin_id' => $store_ids]); } if ($name) { $query->andWhere(['like' ,'s.name', $name]); } if ($mobile) { $query->andWhere(['a.mobile' => $mobile]); } if ($username) { $query->andWhere(['like' ,'su.name', $username]); } if ($start_date) { $query->andWhere(['>=' ,'sro.created_at', $start_date]); } if ($end_date) { $query->andWhere(['<=' ,'sro.created_at', $end_date]); } if (in_array($status, [1, 2])) { $query->andWhere(['sro.is_pay' => $status == 1 ? 1 : 0]); } $query->orderBy('sro.created_at DESC'); $pagination = pagination_make($query); $listArray = $pagination['list']; foreach ($listArray as &$item) { $item['cat_name'] = '-'; if ($item['category_id'] > 0) { $cat = SaasCategory::findOne($item['category_id']); if ($cat) { $item['cat_name'] = $cat->name; } } $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']); $item['pay_time'] = $item['pay_time'] ? date('Y-m-d H:i:s', $item['pay_time']) : ''; } return [ 'code' => 0, 'data' => [ 'data' => $listArray, 'pageNo' => $pagination['pageNo'], 'totalCount' => $pagination['totalCount'] ] ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function audit() { try { $admin = get_admin(); $saas_user_id = $admin->saas_user_id; $saas_user = SaasUser::findOne($saas_user_id); $rate = $admin->rate; if (!$saas_user) { throw new \Exception("非代理商禁止操作"); } $amount = $this->amount; $type = (int)$this->type; $account_name = $this->account_name; $account = $this->account; $bank = $this->bank; $service_money = ((($rate ?: 2) / 1000) * $amount); if ($saas_user->share_profit < ($amount + $service_money)) { throw new \Exception("提现金额不足"); } if (!in_array($type, [0, 1, 2])) { throw new \Exception("提现方式错误"); } if ((empty($account_name) || empty($account)) || ($type === 2 && empty($bank))) { throw new \Exception("请填写提现账户信息"); } $cash = new SaasProfitCash(); $cash->order_no = OrderNo::getOrderNo(OrderNo::ORDER_SAAS_PROFIT_CASH); $cash->user_id = $saas_user->id; $cash->mobile = $saas_user->mobile; $cash->amount = $amount; $cash->status = SaasProfitCash::CASH_STATUS_WAIT; $cash->created_at = time(); $cash->type = $type; $cash->account_name = $account_name; $cash->account = $account; $cash->bank = $bank; $cash->service_money = $service_money; $cash->before_price = $saas_user->share_profit; $cash->after_price = ($saas_user->share_profit - $amount - $service_money); if (!$cash->save()) { throw new \Exception(json_encode($cash->errors)); } $saas_user->share_profit = ($saas_user->share_profit - $amount - $service_money); if (!$saas_user->save()) { throw new \Exception(json_encode($saas_user->errors)); } return [ 'code' => 0, 'msg' => "开始审核,请耐心等待" ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function moneyLog() { try { $type = (int)$this->type; $admin = get_admin(); $saas_user_id = $admin->saas_user_id; $saas_user = SaasUser::findOne($saas_user_id); $start_date = strtotime($this->start_date); $end_date = strtotime($this->end_date); $order_no = $this->order_no; $salesman_id = $this->salesman_id; if (!$saas_user) { throw new \Exception("用户信息获取失败"); } // $query = StoreShareMoney::find()->alias('ssm')->leftJoin(['o' => ])->where(['user_id' => $saas_user_id, 'is_delete' => 0]); $sql = "SELECT ssm.*, o.order_no FROM cyy_store_share_money ssm LEFT JOIN ( "; $sql .= "SELECT 0 AS order_type, 1 AS `type`, order_no, id FROM cyy_order WHERE is_delete = 0 UNION ALL "; $sql .= "SELECT 1 AS order_type, 1 AS `type`, order_no, id FROM cyy_purchase_order WHERE is_delete = 0 UNION ALL "; $sql .= "SELECT 0 AS order_type, 2 AS `type`, order_no, id FROM cyy_saas_profit_cash WHERE is_delete = 0 UNION ALL "; $sql .= "SELECT 0 AS order_type, 0 AS `type`, order_no, id FROM cyy_store_re_order WHERE is_delete = 0 AND is_pay = 1 "; $sql .= ") AS o ON o.order_type = ssm.order_type AND o.id = ssm.order_id AND o.type = ssm.`status` WHERE is_delete = 0 AND user_id = {$saas_user_id}"; $where = ""; if ($start_date) { $where .= " AND ssm.created_at >= {$start_date}"; } if ($end_date) { $where .= " AND ssm.created_at <= {$end_date}"; } if ($type === 1) { $where .= " AND ssm.status in (0, 1)"; } if ($type === 2) { $where .= " AND ssm.status = {$type}"; } if ($order_no) { $where .= " AND o.order_no LIKE '%{$order_no}%'"; } if($salesman_id){ $storeIds = $this->storeIdList($salesman_id); $where .= " AND ssm.store_id in (". implode(',', $storeIds) .")"; } $sql .= $where; $pageNo = get_params('pageNo', get_params('page', 1)); $pageSize = get_params('pageSize', \Yii::$app->params['pageSize']); $pageInitNum = ($pageNo - 1) * $pageSize; $count = \Yii::$app->db->createCommand($sql)->query()->count(); $sql .= " ORDER BY created_at desc LIMIT {$pageSize} OFFSET {$pageInitNum}"; $list = \Yii::$app->db->createCommand($sql)->queryAll(); foreach ($list as $index => &$item) { $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']); if (intval($item['type']) === 2) { $item['commission'] = $item['total_price']; } } return [ 'code' => 0, 'msg' => "获取成功", 'data' => [ 'data' => $list, 'pageNo' => $pageNo, 'totalCount' => $count, // 'q' => $sql, ] ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function storeIdList($salesman_id = 0, $admin_id = 0) { $stores = []; if($salesman_id <= 0){ $salesman_id = Salesman::find()->alias('s')->select('s.id') ->leftJoin(['a' => Admin::tableName()], 'a.id = s.admin_id') ->where(['a.type' => Admin::ADMIN_TYPE_BD_AGENT, 'a.id' => $admin_id]); } $stores = Store::find()->select('id') ->where(['salesman_id' => $salesman_id]) ->all(); $storeIds = [-2]; foreach ($stores as $store) { $storeIds[] = $store['id']; } return $storeIds; } //订单统计 public function storeOrderList() { $admin = get_admin(); $queryScanCodeOrder = \app\plugins\scanCodePay\models\Order::find(); $queryOrder = Order::find()->where(['is_delete' => 0, 'type' => 0, 'trade_status' => Order::ORDER_FLOW_CONFIRM]); $queryOrder->andWhere(['not in', 'id', \app\models\VideoShopOrderExt::find()->select('order_id')]); $storeIds = $this->storeIdList($this->salesman_id, $admin['id']); $queryOrder->andWhere(['store_id' => $storeIds]); $queryScanCodeOrder->andWhere(['store_id' => $storeIds]); if($this->start_date){ $queryOrder->andWhere(['>=', 'pay_time', strtotime($this->start_date)]); $queryScanCodeOrder->andWhere(['>=', 'pay_time', strtotime($this->start_date)]); } if($this->end_date){ $queryOrder->andWhere(['<=', 'pay_time', strtotime($this->end_date)]); $queryScanCodeOrder->andWhere(['<=', 'pay_time', strtotime($this->end_date)]); } $select = ['order_no', 'store_id', 'total_price', 'pay_time']; $queryOrder->select($select); $queryScanCodeOrder->select($select); $queryOrder->union($queryScanCodeOrder, true); $query = new \yii\db\Query(); $query->from(['t' => $queryOrder]); // var_dump($query->createCommand()->getRawSql());die; $countQuery = clone $query; $count = $countQuery->count(); $sumQuery = clone $query; $sum = $sumQuery->sum('total_price'); $countGroupQuery = clone $query; $countGroup = $countGroupQuery->select('store_id, count(*) as count')->groupBy('store_id')->all(); $sumGroupQuery = clone $query; $sumGroup = $sumGroupQuery->select('store_id, sum(total_price) as sum')->groupBy('store_id')->all(); // $data = pagination_make($query); $pageNo = get_params('pageNo', get_params('page', 1)); $pageSize = get_params('pageSize', \Yii::$app->params['pageSize']); $query->offset(($pageNo - 1) * $pageSize)->limit($pageSize); $query->orderBy('pay_time DESC'); $list = $query->all(); return [ 'code' => 0, 'data' => [ 'totalCount' => $count, 'totalSum' => $sum ?? 0, 'countGroup' => $countGroup, 'sumGroup' => $sumGroup, 'list' => $list, 'pageNo' => (int)$pageNo, 'pageSize' => (int)$pageSize, ], 'sql' => $query->createCommand()->getRawSql(), ]; } }