CashierIndexController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\controllers\cashier;
  8. use app\models\Address;
  9. use app\models\CashierActionLog;
  10. use app\models\CashierGoodsPriceLog;
  11. use app\models\District;
  12. use app\models\Goods;
  13. use app\models\Order;
  14. use app\models\SaasUser;
  15. use app\models\User;
  16. use app\models\UserLabel;
  17. use app\modules\admin\controllers\BaseController;
  18. use app\modules\admin\models\cashier\CashierUserForm;
  19. use app\modules\admin\models\cashier\HangingOrderForm;
  20. use app\modules\admin\models\order\MchOrderPayDataForm;
  21. use app\modules\admin\models\order\MchOrderSubmitForm;
  22. use app\modules\admin\models\order\MchOrderSubmitPreviewForm;
  23. use app\modules\admin\models\cashier\GoodsAttrInfoForm;
  24. use app\modules\admin\models\cashier\GoodsForm;
  25. use app\modules\admin\models\cashier\OrderListForm;
  26. use app\modules\admin\models\order\ReuseOrderGoodsPriceForm;
  27. use app\modules\admin\models\UserForm;
  28. class CashierIndexController extends BaseController
  29. {
  30. /**
  31. * 收银台添加用户
  32. * @return \yii\web\Response
  33. * User: hankaige
  34. * DATE TIME: 2022/12/5 10:16
  35. */
  36. public function actionUserAdd(){
  37. $param = post_params();
  38. $form = new CashierUserForm();
  39. $form->scenario = $form::SCENARIO_ADD;
  40. $form->attributes = $param;
  41. $form->store_id = get_store_id();
  42. $res = $form->cashierUserAdd();
  43. return $this->asJson($res);
  44. }
  45. /**
  46. * 搜索用户
  47. * @return \yii\web\Response
  48. * User: hankaige
  49. * DATE TIME: 2022/12/6 15:50
  50. */
  51. public function actionSearchUser(){
  52. $keyword = get_params('keyword');
  53. if(empty($keyword)){
  54. return $this->asJson(['code' => 0,'data'=>[]]);
  55. }
  56. if(substr($keyword, 0, 2) == '00'){
  57. $keyword *= 1;
  58. $data = ['id' => $keyword, 'store_id' => get_store_id()];
  59. } else {
  60. $data = ['AND', ['or',['like','binding',$keyword],['like','nickname',$keyword]], ['store_id' => get_store_id()]];
  61. }
  62. $userList = User::find()->where($data)->orderBy('created_at DESC')->select('id,nickname,binding, user_label_id')->asArray()->all();
  63. foreach ($userList as &$userItem) {
  64. $userLabel = UserLabel::getLabel(get_store_id(), $userItem['user_label_id']);
  65. $userItem['user_label_name'] = $userLabel->label_name ?: '';
  66. }
  67. return $this->asJson(['code'=>0,'data'=>$userList]);
  68. }
  69. //获取对应商品的规格信息
  70. public function actionGetGoodsAttr(){
  71. $form = new GoodsForm();
  72. $form->attributes = get_params();
  73. $form->user_id = get_params('user_id',0);
  74. $form->store_id = get_store_id();
  75. return $this->asJson($form->search());
  76. }
  77. /**
  78. * 获取规格详情
  79. * @return mixed
  80. */
  81. public function actionGoodsAttrInfo()
  82. {
  83. $form = new GoodsAttrInfoForm();
  84. $form->attributes = get_params();
  85. return $this->asJson($form->search());
  86. }
  87. /**
  88. * 申请挂单
  89. * @return \yii\web\Response
  90. * User: hankaige
  91. * DATE TIME: 2022/12/5 15:06
  92. */
  93. public function actionHangingOrder(){
  94. // 需要用户ID 挂单上皮ID
  95. $param = post_params();
  96. $form = new HangingOrderForm();
  97. $form->user_id = $param['user_id'];
  98. $form->store_id = get_store_id();
  99. $form->md_id = get_md_id();
  100. $form->goods_list = json_decode($param['goods_list'],true);
  101. $form->id = $param['id'] ?? 0;
  102. $res = $form->hangingOrder();
  103. return $this->asJson($res);
  104. }
  105. /**
  106. * 获取挂单列表 可查询指定用户的挂单信息
  107. * @return \yii\web\Response
  108. * User: hankaige
  109. * DATE TIME: 2022/12/6 09:29
  110. */
  111. public function actionGetHangingOrderList(){
  112. $form = new HangingOrderForm();
  113. $form->user_id = get_params('user_id',0);
  114. $form->order_no = get_params('order_no','');
  115. $form->dateStart = get_params('dateStart','');
  116. $form->dateEnd = get_params('dateEnd','');
  117. $form->store_id = get_store_id();
  118. $form->md_id = get_md_id();
  119. $res = $form->getList();
  120. return $this->asJson($res);
  121. }
  122. // 获取挂单订单商品
  123. public function actionGetHangingOrderDetail(){
  124. $form = new HangingOrderForm();
  125. $form->goods_name = get_params('goods_name','');
  126. $form->id = get_params('id');
  127. $res = $form->getOrderDetail();
  128. return $this->asJson($res);
  129. }
  130. /**
  131. * 删除挂单订单
  132. * @return \yii\web\Response
  133. * User: hankaige
  134. * DATE TIME: 2022/12/6 09:36
  135. */
  136. public function actionDelHangingOrder(){
  137. $form = new HangingOrderForm();
  138. $form->id = get_params('id');
  139. $res = $form->delHanging();
  140. return $this->asJson($res);
  141. }
  142. /**
  143. * 删除挂单订单商品
  144. * @return \yii\web\Response
  145. * User: hankaige
  146. * DATE TIME: 2022/12/6 14:16
  147. */
  148. public function actionDelHangingOrderDetail(){
  149. $form = new HangingOrderForm();
  150. $form->id = get_params('id');
  151. $res = $form->delHangingOrderDetail();
  152. return $this->asJson($res);
  153. }
  154. /**
  155. * 取出挂单
  156. * @return \yii\web\Response
  157. * User: hankaige
  158. * DATE TIME: 2022/12/6 14:47
  159. */
  160. public function actionGetHangingOrder(){
  161. $form = new HangingOrderForm();
  162. $form->id = post_params('id');
  163. $res = $form->getHangingOrder();
  164. return $this->asJson($res);
  165. }
  166. //结算预览
  167. public function actionOrderPreview(){
  168. $form = new MchOrderSubmitPreviewForm();
  169. $form->attributes = post_params();
  170. $form->user_id = post_params('user_id');
  171. $form->store_id = get_store_id();
  172. $form->md_id = get_md_id();
  173. return $this->asJson($form->search());
  174. }
  175. // todo 生成待支付订单
  176. public function actionSubmitOrder(){
  177. $form = new MchOrderSubmitForm();
  178. $form->attributes = post_params();
  179. $form->user_id = post_params('user_id');
  180. $form->store_id = get_store_id();
  181. $form->md_id = get_md_id();
  182. // $form->is_offline = 1;
  183. return $this->asJson($form->save());
  184. }
  185. // todo 支付订单
  186. public function actionPayData(){
  187. $form = new MchOrderPayDataForm();
  188. $form->attributes = post_params();
  189. $form->user_id = post_params('user_id');
  190. $form->store_id = get_store_id();
  191. return $this->asJson($form->search());
  192. }
  193. //订单列表
  194. public function actionOrderList(){
  195. $store_id = get_store_id();
  196. $md_id = get_md_id();
  197. $form = new OrderListForm();
  198. $form->attributes = get_params();
  199. $form->is_offline = Order::IS_OFFLINE_FALSE;
  200. $form->store_id = $store_id;
  201. $form->md_id = $md_id;
  202. //$form->order_id = get_params('order_id');
  203. return $this->asJson($form->search());
  204. }
  205. public function actionOrderRefund(){
  206. // 获取订单
  207. $store_id = get_store_id();
  208. $form = new OrderListForm();
  209. $form->attributes = get_params();
  210. $form->is_offline = Order::IS_OFFLINE_FALSE;
  211. $form->store_id = $store_id;
  212. $form->order_id = get_params('order_id');
  213. return $this->asJson($form->orderRefund());
  214. }
  215. public function actionAddAddress() {
  216. $form = new UserForm();
  217. $form->attributes = post_params();
  218. $form->is_default = post_params('is_default', 0);
  219. $form->store_id = get_store_id();
  220. return $this->asJson($form->addAddress());
  221. }
  222. //修改购物车商品价格
  223. public function actionSetGoodsPrice() {
  224. try {
  225. $goods_id = post_params('goods_id');
  226. $attr_ = post_params('attr');
  227. $price = post_params('price');
  228. $user_id = post_params('user_id');
  229. $md_id = post_params('md_id');
  230. if (!$price || !$attr_) {
  231. throw new \Exception('修改价格失败,缺少规格或价格参数');
  232. }
  233. $goods = Goods::findOne($goods_id);
  234. if ($goods) {
  235. $attr = json_decode($attr_, true);
  236. $attr_id = array_column($attr['attr_list'], 'attr_id');
  237. sort($attr_id);
  238. $open = false;
  239. $goods_attr = json_decode($goods->attr, true);
  240. foreach ($goods_attr as &$attr_item) {
  241. $gods_attr_id = array_column($attr_item['attr_list'], 'attr_id');
  242. sort($gods_attr_id);
  243. if (!array_diff($gods_attr_id, $attr_id)) {
  244. $open = true;
  245. }
  246. }
  247. if ($open) {
  248. $goods_price_log = new CashierGoodsPriceLog();
  249. $goods_price_log->goods_id = $goods_id;
  250. $goods_price_log->attr = $attr_;
  251. $goods_price_log->price = $price;
  252. $goods_price_log->user_id = $user_id;
  253. $goods_price_log->md_id = $md_id;
  254. if (!$goods_price_log->save()) {
  255. throw new \Exception(json_encode($goods_price_log->errors, JSON_UNESCAPED_UNICODE));
  256. }
  257. } else {
  258. throw new \Exception('修改价格失败,商品规格信息查询失败');
  259. }
  260. } else {
  261. throw new \Exception('修改价格失败,商品信息未找到');
  262. }
  263. return $this->asJson([
  264. 'code' => 0,
  265. 'msg' => '设置成功'
  266. ]);
  267. } catch (\Exception $e) {
  268. return $this->asJson([
  269. 'code' => 1,
  270. 'msg' => $e->getMessage()
  271. ]);
  272. }
  273. }
  274. public function actionGetActionType(){
  275. $result = CashierActionLog::TEXT;
  276. $result[0] = '全部';
  277. return $this->asJson(['code' => 0,'data' => $result]);
  278. }
  279. public function actionGetActionLog()
  280. {
  281. $keyword = get_params('keyword');
  282. $actionType = get_params('action_type');
  283. $startTime = get_params('start_time');
  284. $endTime = get_params('end_time');
  285. $query = CashierActionLog::find()->alias('cal')->leftJoin(['u' => User::tableName()],'cal.user_id=u.id')->leftJoin(['su' => SaasUser::tableName()],'u.binding=su.mobile')->where(['cal.store_id' => get_store_id(), 'md_id' => get_md_id()]);
  286. if(!empty($keyword)){
  287. $query->andWhere(['like','su.name',$keyword]);
  288. }
  289. if($actionType > 0){
  290. $query->andWhere(['cal.action' => $actionType]);
  291. }
  292. if(!empty($startTime)){
  293. $query->andWhere(['>','cal.created_at',$startTime]);
  294. }
  295. if(!empty($endTime)){
  296. $query->andWhere(['<','cal.created_at',$endTime]);
  297. }
  298. $query->orderBy('cal.created_at DESC')->select('cal.*,su.name,su.avatar,su.mobile');
  299. $result = pagination_make($query,true);
  300. foreach($result['list'] as &$item){
  301. $item['created_at'] = date('Y-m-d H:i:s',$item['created_at']);
  302. $item['action_text'] = CashierActionLog::TEXT[$item['action']];
  303. }
  304. return $this->asJson(['code' => 0,'data' => $result]);
  305. }
  306. public function actionGetReuseGoodsPrice(){
  307. $form = new ReuseOrderGoodsPriceForm();
  308. $form->user_id = post_params('user_id');
  309. $form->goods_id = post_params('goods_id');
  310. $form->store_id = get_store_id();
  311. return $this->asJson($form->search());
  312. }
  313. }