CashierController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. namespace addons\Store\api\modules\v1\controllers;
  3. use addons\Store\common\models\Store;
  4. use addons\Store\common\models\StoreCashierOrder;
  5. use common\models\common\PaymentTradeOrder;
  6. use Yii;
  7. use api\controllers\OnAuthController;
  8. use addons\Store\common\enums\StoreEnum;
  9. use addons\Store\common\models\StoreCart;
  10. use addons\Store\common\models\StoreGoods;
  11. use addons\Store\common\models\StoreGoodsAttr;
  12. use common\enums\StatusEnum;
  13. /**
  14. * 默认控制器
  15. *
  16. * Class DefaultController
  17. * @package addons\Store\api\modules\v1\controllers
  18. */
  19. class CashierController extends OnAuthController
  20. {
  21. public $modelClass = '';
  22. /**
  23. * 不用进行登录验证的方法
  24. *
  25. * 例如: ['index', 'update', 'create', 'view', 'delete']
  26. * 默认全部需要验证
  27. *
  28. * @var array
  29. */
  30. protected $authOptional = ['index'];
  31. /**
  32. * 需要门店验证的方法
  33. */
  34. protected $authStore = ['payment-code', 'cashier-amount', 'cashier-log'];
  35. protected $store_id = 0;
  36. public function beforeAction($action)
  37. {
  38. parent::beforeAction($action);
  39. //检测门店身份
  40. if (in_array($action->id, $this->authStore)) {
  41. $store = Store::find()
  42. ->where(['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'status' => StatusEnum::ENABLED])
  43. ->one();
  44. if (empty($store)) {
  45. \Yii::$app->response->data = ['code' => 1, 'msg' => '您非门店身份'];
  46. \Yii::$app->response->send();
  47. return;
  48. }
  49. $this->store_id = $store->id;
  50. }
  51. return $action;
  52. }
  53. //扫码后请求
  54. public function actionScanCode()
  55. {
  56. $params = Yii::$app->request->get();
  57. $params['mall_id'] = $this->mall_id;
  58. $params['user_id'] = $this->user_id;
  59. $params['user'] = $this->user;
  60. $res = StoreCashierOrder::scanCode($params);
  61. if ($res === false) return $this->error(StoreCashierOrder::getStaticError());
  62. return $this->success('操作成功', $res);
  63. }
  64. //预览
  65. public function actionPreview()
  66. {
  67. try {
  68. $params = Yii::$app->request->post();
  69. $store = Store::findOne(['id' => $params['store_id']]);
  70. $params['mall_id'] = $this->mall_id;
  71. $params['user_id'] = $this->user_id;
  72. $params['title'] = $store['store_name'] . ':收银台订单';
  73. $params['user'] = $this->user;
  74. $res = StoreCashierOrder::preview($params);
  75. return $this->success('操作成功', $res);
  76. } catch (\Exception $e) {
  77. return $this->error($e->getMessage().$e->getFile().$e->getLine());
  78. }
  79. }
  80. //添加订单
  81. public function actionCreate()
  82. {
  83. $params = Yii::$app->request->post();
  84. $store = Store::findOne(['id' => $params['store_id'],'status' => StatusEnum::ENABLED]);
  85. if (empty($store)) return $this->error('门店不存在');
  86. $cashier = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'cashier');
  87. $cashier_amount_info = json_decode($cashier['cashier_amount_info'], true) ?? [];
  88. if (!empty($cashier_amount_info)) {
  89. foreach ($cashier_amount_info as $info) {
  90. if ($info['type'] == 'cashier_amount') {
  91. if ($params['money'] > $info['value']) {
  92. return $this->error('超出商家单笔收款额度');
  93. }
  94. }
  95. if ($info['type'] == 'cashier_amount_month') {
  96. $cashier_total = StoreCashierOrder::find()
  97. ->where(['mall_id' => $this->mall_id, 'store_id' => $params['store_id'], 'pay_status' => 1])
  98. ->andWhere(['>=', 'created_at', strtotime(date('Y-m-01 00:00:00'))])
  99. ->andWhere(['<=', 'created_at', strtotime(date('Y-m-t 00:00:00'))])
  100. ->sum('pay_money');
  101. if ($params['money'] + $cashier_total > $info['value']) {
  102. return $this->error('超出商家当月收款额度');
  103. }
  104. }
  105. }
  106. }
  107. $params['mall_id'] = $this->mall_id;
  108. $params['user_id'] = $this->user_id;
  109. $params['title'] = $store['store_name'] . ':收银台订单';
  110. $params['user'] = $this->user;
  111. $res = StoreCashierOrder::createOrder($params);
  112. if ($res === false) return $this->error(StoreCashierOrder::getStaticError());
  113. return $this->success('操作成功', $res);
  114. }
  115. //收款码
  116. public function actionPaymentCode()
  117. {
  118. $params = Yii::$app->request->get();
  119. $H5Url = \Yii::$app->services->setting->platform->get('system.h5_url');
  120. $cashier = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'cashier');
  121. $returnData = [
  122. 'store_id' => $this->store_id,
  123. 'cashier_bg_img' => $cashier['cashier_bg_img'] ?? '',
  124. 'h5_url' => $H5Url,
  125. ];
  126. return $this->success('请求成功', $returnData);
  127. }
  128. //收款额度
  129. public function actionCashierAmount()
  130. {
  131. $params = Yii::$app->request->get();
  132. $cashier_amount = 0;
  133. $cashier_amount_month = 0;
  134. $wait_amount = 0;
  135. $used_amount = 0;
  136. $cashier = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'cashier');
  137. $cashier_amount_info = json_decode($cashier['cashier_amount_info'], true) ?? [];
  138. if (!empty($cashier_amount_info)) {
  139. foreach ($cashier_amount_info as $info) {
  140. if ($info['type'] == 'cashier_amount') {
  141. $cashier_amount = $info['value'];
  142. }
  143. if ($info['type'] == 'cashier_amount_month') {
  144. $cashier_amount_month = $info['value'];
  145. $wait_amount = $info['value'];
  146. }
  147. }
  148. }
  149. $cashier_total = StoreCashierOrder::find()
  150. ->where(['mall_id' => $this->mall_id, 'store_id' => $this->store_id, 'pay_status' => 1])
  151. ->andWhere(['>=', 'created_at', strtotime(date('Y-m-01 00:00:00'))])
  152. ->andWhere(['<=', 'created_at', strtotime(date('Y-m-t 00:00:00'))])
  153. ->sum('pay_money');
  154. if ($cashier_total) {
  155. $used_amount = $cashier_total;
  156. $wait_amount = bcsub($cashier_amount_month, $used_amount, 2);
  157. if ($wait_amount < 0) {
  158. $wait_amount = 0;
  159. }
  160. }
  161. $returnData = [
  162. 'cashier_amount' => $cashier_amount,
  163. 'cashier_amount_month' => $cashier_amount_month,
  164. 'wait_amount' => $wait_amount,
  165. 'used_amount' => $used_amount,
  166. ];
  167. return $this->success('请求成功', $returnData);
  168. }
  169. //收款记录
  170. public function actionCashierLog()
  171. {
  172. $params = \Yii::$app->request->get();
  173. $where[] = ['mall_id' => $this->mall_id];
  174. $where[] = ['store_id' => $this->store_id];
  175. $where[] = ['pay_status' => 1];
  176. $params['where'] = $where;
  177. $params['with'] = ['user'];
  178. $params['order'] = 'id desc';
  179. $params['page'] = $params['page'] ?? 1;
  180. $params['limit'] = $params['limit'] ?? $this->pageSize;
  181. $params['select'] = 'user_id,remark,amount_received,created_at';
  182. $page_data = StoreCashierOrder::listPage($params);
  183. if (!empty($page_data['list'])) {
  184. foreach ($page_data['list'] as &$list) {
  185. $list['created_at'] = date('Y-m-d H:i:s', $list['created_at']);
  186. $list['nickname'] = $list['user']['nickname'] ?? '';
  187. $list['money'] = $list['amount_received'];
  188. unset($list['user']);
  189. }
  190. }
  191. unset($page_data['sql'], $page_data['pagination']);
  192. if ($page_data === false) return $this->error(StoreCashierOrder::getStaticError());
  193. return $this->success('请求成功', $page_data);
  194. }
  195. public function actionCashierGoodsList()
  196. {
  197. $params = \Yii::$app->request->get();
  198. $res = \Yii::$app->services->validate->validate($params, [
  199. [['store_id'], 'required'],
  200. ]);
  201. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  202. $store_id = $params['store_id'];
  203. $where[] = ['store_id' => $store_id, 'mall_id' => $this->mall_id, 'check_status' => 1, 'is_on_sale' => 1, 'status' => StatusEnum::ENABLED];
  204. $params['where'] = $where;
  205. $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';
  206. $params['order'] = 'sort asc';
  207. $params['with'] = [
  208. 'attr' => function ($query) {
  209. $query->select('id,goods_id,name,price,sign_id,stock,pic_url,original_price');
  210. }, 'extra'
  211. ];
  212. $goods_list = StoreGoods::listPage($params);
  213. $list = StoreCart::getCartList($this->user_id);
  214. $goods_num_arr = [];
  215. $goods_attr_ids = [];
  216. foreach ($list as $v) {
  217. $row = json_decode($v, true);
  218. if (is_array($row)) {
  219. $goods_attr_ids[] = $row['attr_id'];
  220. }
  221. }
  222. $goods_attr_arr = StoreGoodsAttr::lists(['where' => [['id' => $goods_attr_ids], ['>', 'stock', 0], ['status' => StatusEnum::ENABLED]], 'select' => 'goods_id,id', 'index' => 'goods_id']);
  223. foreach ($list as $v) {
  224. $row = json_decode($v, true);
  225. if (!is_array($row)) continue;
  226. if (isset($goods_num_arr[$row['goods_id']])) {
  227. if (isset($goods_attr_arr[$row['goods_id']])) {
  228. $goods_num_arr[$row['goods_id']] += $row['num'];
  229. }
  230. } else {
  231. $goods_num_arr[$row['goods_id']] = $row['num'];
  232. }
  233. }
  234. if (!empty($goods_list['list'])) {
  235. foreach ($goods_list['list'] as &$item) {
  236. $item['member_buy_limit_setting'] = empty($item['extra']['member_buy_limit_setting']) ? '' : json_decode($item['extra']['member_buy_limit_setting'], true);
  237. unset($item['extra']);
  238. $item['num'] = !empty($goods_num_arr[$item['id']]) ? $goods_num_arr[$item['id']] : 0;
  239. $item['status'] = empty($item['stock']) ? 0 : 1;
  240. }
  241. }
  242. return $this->success('操作成功', $goods_list);
  243. }
  244. public function actionGetGoodsByAttrId()
  245. {
  246. $params = \Yii::$app->request->post();
  247. $res = \Yii::$app->services->validate->validate($params, [
  248. [['attr_ids'], 'safe'],
  249. [['store_id'], 'required'],
  250. ]);
  251. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  252. $attr_ids = $params['attr_ids'];
  253. $goods_attr = StoreGoodsAttr::lists([
  254. 'where' => [
  255. ['id' => array_column($attr_ids, 'attr_id')], ['>', 'stock', 0], ['status' => StatusEnum::ENABLED]
  256. ],
  257. 'select' => 'id,goods_id, name, sign_id, pic_url,stock',
  258. 'with' => [
  259. 'goods' => function ($query){
  260. return $query->select('id, goods_name, cover_pic, price, is_attr');
  261. }
  262. ]
  263. ]);
  264. if ($goods_attr === false) return $this->error(StoreGoods::getStaticError());
  265. $attr_ids = array_column($attr_ids, null, 'attr_id');
  266. foreach($goods_attr as &$v){
  267. $v['num'] = $attr_ids[$v['id']]['num'];
  268. }
  269. unset($v);
  270. return $this->success('操作成功', $goods_attr);
  271. }
  272. /**
  273. * 关闭收银台订单
  274. * @return mixed|null
  275. */
  276. public function actionClose()
  277. {
  278. $params = \Yii::$app->request->post();
  279. $res = \Yii::$app->services->validate->validate($params, [
  280. [['trade_id'], 'required'],
  281. ]);
  282. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  283. $payment_order = PaymentTradeOrder::findOne(['trade_id'=>$params['trade_id'],'is_pay'=>0]);
  284. $data = [
  285. 'order_no' => $payment_order['order_no'],
  286. 'user_id' => $this->user_id,
  287. 'mall_id' => $this->mall_id,
  288. 'operator_id' => $this->user_id,
  289. 'operator_name' => $this->user['nickname']??'',
  290. ];
  291. StoreCashierOrder::close($data);
  292. return $this->success('操作成功');
  293. }
  294. }