| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- <?php
- namespace addons\Store\api\modules\v1\controllers;
- use addons\Store\common\models\Store;
- use addons\Store\common\models\StoreCashierOrder;
- use common\models\common\PaymentTradeOrder;
- use Yii;
- use api\controllers\OnAuthController;
- use addons\Store\common\enums\StoreEnum;
- use addons\Store\common\models\StoreCart;
- use addons\Store\common\models\StoreGoods;
- use addons\Store\common\models\StoreGoodsAttr;
- use common\enums\StatusEnum;
- /**
- * 默认控制器
- *
- * Class DefaultController
- * @package addons\Store\api\modules\v1\controllers
- */
- class CashierController extends OnAuthController
- {
- public $modelClass = '';
- /**
- * 不用进行登录验证的方法
- *
- * 例如: ['index', 'update', 'create', 'view', 'delete']
- * 默认全部需要验证
- *
- * @var array
- */
- protected $authOptional = ['index'];
- /**
- * 需要门店验证的方法
- */
- protected $authStore = ['payment-code', 'cashier-amount', 'cashier-log'];
- protected $store_id = 0;
- public function beforeAction($action)
- {
- parent::beforeAction($action);
- //检测门店身份
- if (in_array($action->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('操作成功');
- }
- }
|