id, $this->authStore)) { $store = Store::find() ->where(['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'status' => StatusEnum::ENABLED]) ->one(); if (empty($store)) { \Yii::$app->response->data = ['code' => 1, 'msg' => '您非门店身份']; \Yii::$app->response->send(); return; } $this->store_id = $store->id; } return $action; } //扫码后请求 public function actionScanCode() { $params = Yii::$app->request->get(); $params['mall_id'] = $this->mall_id; $params['user_id'] = $this->user_id; $params['user'] = $this->user; $res = StoreCashierOrder::scanCode($params); if ($res === false) return $this->error(StoreCashierOrder::getStaticError()); return $this->success('操作成功', $res); } //预览 public function actionPreview() { try { $params = Yii::$app->request->post(); $store = Store::findOne(['id' => $params['store_id']]); $params['mall_id'] = $this->mall_id; $params['user_id'] = $this->user_id; $params['title'] = $store['store_name'] . ':收银台订单'; $params['user'] = $this->user; $res = StoreCashierOrder::preview($params); return $this->success('操作成功', $res); } catch (\Exception $e) { return $this->error($e->getMessage().$e->getFile().$e->getLine()); } } //添加订单 public function actionCreate() { $params = Yii::$app->request->post(); $store = Store::findOne(['id' => $params['store_id'],'status' => StatusEnum::ENABLED]); if (empty($store)) return $this->error('门店不存在'); $cashier = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'cashier'); $cashier_amount_info = json_decode($cashier['cashier_amount_info'], true) ?? []; if (!empty($cashier_amount_info)) { foreach ($cashier_amount_info as $info) { if ($info['type'] == 'cashier_amount') { if ($params['money'] > $info['value']) { return $this->error('超出商家单笔收款额度'); } } if ($info['type'] == 'cashier_amount_month') { $cashier_total = StoreCashierOrder::find() ->where(['mall_id' => $this->mall_id, 'store_id' => $params['store_id'], 'pay_status' => 1]) ->andWhere(['>=', 'created_at', strtotime(date('Y-m-01 00:00:00'))]) ->andWhere(['<=', 'created_at', strtotime(date('Y-m-t 00:00:00'))]) ->sum('pay_money'); if ($params['money'] + $cashier_total > $info['value']) { return $this->error('超出商家当月收款额度'); } } } } $params['mall_id'] = $this->mall_id; $params['user_id'] = $this->user_id; $params['title'] = $store['store_name'] . ':收银台订单'; $params['user'] = $this->user; $res = StoreCashierOrder::createOrder($params); if ($res === false) return $this->error(StoreCashierOrder::getStaticError()); return $this->success('操作成功', $res); } //收款码 public function actionPaymentCode() { $params = Yii::$app->request->get(); $H5Url = \Yii::$app->services->setting->platform->get('system.h5_url'); $cashier = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'cashier'); $returnData = [ 'store_id' => $this->store_id, 'cashier_bg_img' => $cashier['cashier_bg_img'] ?? '', 'h5_url' => $H5Url, ]; return $this->success('请求成功', $returnData); } //收款额度 public function actionCashierAmount() { $params = Yii::$app->request->get(); $cashier_amount = 0; $cashier_amount_month = 0; $wait_amount = 0; $used_amount = 0; $cashier = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'cashier'); $cashier_amount_info = json_decode($cashier['cashier_amount_info'], true) ?? []; if (!empty($cashier_amount_info)) { foreach ($cashier_amount_info as $info) { if ($info['type'] == 'cashier_amount') { $cashier_amount = $info['value']; } if ($info['type'] == 'cashier_amount_month') { $cashier_amount_month = $info['value']; $wait_amount = $info['value']; } } } $cashier_total = StoreCashierOrder::find() ->where(['mall_id' => $this->mall_id, 'store_id' => $this->store_id, 'pay_status' => 1]) ->andWhere(['>=', 'created_at', strtotime(date('Y-m-01 00:00:00'))]) ->andWhere(['<=', 'created_at', strtotime(date('Y-m-t 00:00:00'))]) ->sum('pay_money'); if ($cashier_total) { $used_amount = $cashier_total; $wait_amount = bcsub($cashier_amount_month, $used_amount, 2); if ($wait_amount < 0) { $wait_amount = 0; } } $returnData = [ 'cashier_amount' => $cashier_amount, 'cashier_amount_month' => $cashier_amount_month, 'wait_amount' => $wait_amount, 'used_amount' => $used_amount, ]; return $this->success('请求成功', $returnData); } //收款记录 public function actionCashierLog() { $params = \Yii::$app->request->get(); $where[] = ['mall_id' => $this->mall_id]; $where[] = ['store_id' => $this->store_id]; $where[] = ['pay_status' => 1]; $params['where'] = $where; $params['with'] = ['user']; $params['order'] = 'id desc'; $params['page'] = $params['page'] ?? 1; $params['limit'] = $params['limit'] ?? $this->pageSize; $params['select'] = 'user_id,remark,amount_received,created_at'; $page_data = StoreCashierOrder::listPage($params); if (!empty($page_data['list'])) { foreach ($page_data['list'] as &$list) { $list['created_at'] = date('Y-m-d H:i:s', $list['created_at']); $list['nickname'] = $list['user']['nickname'] ?? ''; $list['money'] = $list['amount_received']; unset($list['user']); } } unset($page_data['sql'], $page_data['pagination']); if ($page_data === false) return $this->error(StoreCashierOrder::getStaticError()); return $this->success('请求成功', $page_data); } public function actionCashierGoodsList() { $params = \Yii::$app->request->get(); $res = \Yii::$app->services->validate->validate($params, [ [['store_id'], 'required'], ]); if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage()); $store_id = $params['store_id']; $where[] = ['store_id' => $store_id, 'mall_id' => $this->mall_id, 'check_status' => 1, 'is_on_sale' => 1, 'status' => StatusEnum::ENABLED]; $params['where'] = $where; $params['select'] = 'id,goods_name,cover_pic,price,(sales_num+virtual_sales) as sales_num,original_price,status,stock,attr_groups,attr_groups_format,is_attr,goods_type'; $params['order'] = 'sort asc'; $params['with'] = [ 'attr' => function ($query) { $query->select('id,goods_id,name,price,sign_id,stock,pic_url,original_price'); }, 'extra' ]; $goods_list = StoreGoods::listPage($params); $list = StoreCart::getCartList($this->user_id); $goods_num_arr = []; $goods_attr_ids = []; foreach ($list as $v) { $row = json_decode($v, true); if (is_array($row)) { $goods_attr_ids[] = $row['attr_id']; } } $goods_attr_arr = StoreGoodsAttr::lists(['where' => [['id' => $goods_attr_ids], ['>', 'stock', 0], ['status' => StatusEnum::ENABLED]], 'select' => 'goods_id,id', 'index' => 'goods_id']); foreach ($list as $v) { $row = json_decode($v, true); if (!is_array($row)) continue; if (isset($goods_num_arr[$row['goods_id']])) { if (isset($goods_attr_arr[$row['goods_id']])) { $goods_num_arr[$row['goods_id']] += $row['num']; } } else { $goods_num_arr[$row['goods_id']] = $row['num']; } } if (!empty($goods_list['list'])) { foreach ($goods_list['list'] as &$item) { $item['member_buy_limit_setting'] = empty($item['extra']['member_buy_limit_setting']) ? '' : json_decode($item['extra']['member_buy_limit_setting'], true); unset($item['extra']); $item['num'] = !empty($goods_num_arr[$item['id']]) ? $goods_num_arr[$item['id']] : 0; $item['status'] = empty($item['stock']) ? 0 : 1; } } return $this->success('操作成功', $goods_list); } public function actionGetGoodsByAttrId() { $params = \Yii::$app->request->post(); $res = \Yii::$app->services->validate->validate($params, [ [['attr_ids'], 'safe'], [['store_id'], 'required'], ]); if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage()); $attr_ids = $params['attr_ids']; $goods_attr = StoreGoodsAttr::lists([ 'where' => [ ['id' => array_column($attr_ids, 'attr_id')], ['>', 'stock', 0], ['status' => StatusEnum::ENABLED] ], 'select' => 'id,goods_id, name, sign_id, pic_url,stock', 'with' => [ 'goods' => function ($query){ return $query->select('id, goods_name, cover_pic, price, is_attr'); } ] ]); if ($goods_attr === false) return $this->error(StoreGoods::getStaticError()); $attr_ids = array_column($attr_ids, null, 'attr_id'); foreach($goods_attr as &$v){ $v['num'] = $attr_ids[$v['id']]['num']; } unset($v); return $this->success('操作成功', $goods_attr); } /** * 关闭收银台订单 * @return mixed|null */ public function actionClose() { $params = \Yii::$app->request->post(); $res = \Yii::$app->services->validate->validate($params, [ [['trade_id'], 'required'], ]); if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage()); $payment_order = PaymentTradeOrder::findOne(['trade_id'=>$params['trade_id'],'is_pay'=>0]); $data = [ 'order_no' => $payment_order['order_no'], 'user_id' => $this->user_id, 'mall_id' => $this->mall_id, 'operator_id' => $this->user_id, 'operator_name' => $this->user['nickname']??'', ]; StoreCashierOrder::close($data); return $this->success('操作成功'); } }