StoreCasherController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace addons\Happiness\agent\controllers;
  3. use addons\Coupon\common\enums\AddonsCouponEnum;
  4. use addons\Coupon\common\models\AddonsCoupon;
  5. use addons\Happiness\common\models\HappinessCashierOrder;
  6. use addons\Happiness\common\models\HappinessStore;
  7. use addons\Happiness\common\models\HappinessStoreMiniCode;
  8. use addons\Happiness\common\models\HappinessWithdrawLog;
  9. use common\components\export\CsvTool;
  10. use common\enums\AddonsEnum;
  11. use common\enums\StatusEnum;
  12. use common\helpers\csv\CsvExportHelper;
  13. use common\helpers\MiniQrCode;
  14. use common\models\common\CapitalLog;
  15. use common\models\mall\MallAddons;
  16. use common\models\order\OrderDetail;
  17. use common\models\user\User;
  18. use common\models\user\UserLevel;
  19. class StoreCasherController extends BaseController
  20. {
  21. public function actionIndex()
  22. {
  23. if (\Yii::$app->request->isAjax) {
  24. if (\Yii::$app->request->isGet) {
  25. $params = \Yii::$app->request->get();
  26. $where[] = ['mall_id' => $this->mall_id];
  27. $where[] = ['pay_status' => 1];
  28. $where[] = ['status' => StatusEnum::ENABLED];
  29. if (!empty($params['keyword'])) {
  30. $user_list = User::lists(['where' => [['mall_id' => $this->mall_id], [
  31. 'or',
  32. ['like', 'username', $params['keyword']],
  33. ['like', 'nickname', $params['keyword']],
  34. ]], 'select' => 'id']);
  35. $user_ids = array_column($user_list, 'id');
  36. $where[] = ['in', 'user_id', $user_ids];
  37. }
  38. if (!empty($params['start'])) $where[] = ['>=', 'pay_time', strtotime($params['start'])];
  39. if (!empty($params['end'])) $where[] = ['<=', 'pay_time', strtotime($params['end'])];
  40. if (!empty($params['min'])) $where[] = ['>=', 'pay_money', $params['min']];
  41. if (!empty($params['max'])) $where[] = ['<=', 'pay_money', $params['max']];
  42. $params['where'] = $where;
  43. $params['order'] = 'id desc';
  44. $params['with'] = ['user', 'detail'];
  45. $params['limit'] = $params['limit'] ?? $this->pageSize;
  46. $page_data = HappinessCashierOrder::listPage($params);
  47. if ($page_data === false) return $this->error(HappinessWithdrawLog::getStaticError());
  48. if (!empty($page_data['list'])) {
  49. $store_ids = array_column($page_data['list'], 'store_id');
  50. $store_list = HappinessStore::lists([
  51. 'where' => [
  52. ['id' => $store_ids]
  53. ],
  54. 'index' => 'id'
  55. ]);
  56. foreach ($page_data['list'] as &$item) {
  57. $item['extra'] = '';
  58. $item['commission'] = 0;
  59. $item['store_name'] = $store_list[$item['store_id']]['store_name']??'';
  60. }
  61. }
  62. $page_data['export_list'] = HappinessCashierOrder::fieldsList(true);
  63. return $this->success('ok', $page_data);
  64. }
  65. }
  66. if (\Yii::$app->request->post('flag') == 'EXPORT') {
  67. $params = \Yii::$app->request->post();
  68. $params['mall_id'] = $this->mall_id;
  69. $filename = '幸福小店收银订单列表-' . date('YmdHis') . '_' . rand(10000, 99999);
  70. $model = HappinessCashierOrder::find();
  71. $model->where(['mall_id' => $params['mall_id']]);
  72. $model->andWhere(['pay_status' => 1]);
  73. if (!empty($params['start'])) $model->andWhere(['>=', 'pay_time', strtotime($params['start'])]);
  74. if (!empty($params['end'])) $model->andWhere(['<=', 'pay_time', strtotime($params['end'])]);
  75. if (!empty($params['min'])) $model->andWhere(['>=', 'pay_money', $params['min']]);
  76. if (!empty($params['max'])) $model->andWhere(['<=', 'pay_money', $params['max']]);
  77. if (!empty($params['keyword'])) {
  78. $user_list = User::lists(['where' => [['mall_id' => $params['mall_id']], [
  79. 'or',
  80. ['like', 'username', $params['keyword']],
  81. ['like', 'nickname', $params['keyword']],
  82. ]], 'select' => 'id']);
  83. $user_ids = array_column($user_list, 'id');
  84. $model->andWhere(['in', 'user_id', $user_ids]);
  85. }
  86. $model->with(['user']);
  87. $fields = $params['fields'];
  88. $fields = explode(',', $fields);
  89. $fields_arr = HappinessCashierOrder::fieldsList(true);
  90. $have_select_field_arr = [];
  91. foreach ($fields as $value) {
  92. $have_select_field_arr[] = $fields_arr[$value];
  93. }
  94. $csv = new CsvTool();
  95. $csv->filename = $filename;
  96. $csv->export_mode = CsvTool::EXPORT_MODE_SYNC;
  97. $csv->header = $have_select_field_arr;
  98. $orders = $model->all();
  99. empty($csv->header) && $csv->header = array_keys($orders[0]);
  100. $data = [];
  101. $store_ids = array_column($orders, 'store_id');
  102. $store_list = HappinessStore::lists([
  103. 'where' => [
  104. ['id' => $store_ids]
  105. ],
  106. 'index' => 'id'
  107. ]);
  108. foreach ($orders as $order) {
  109. $row = [];
  110. foreach ($fields as $field) {
  111. if ($field == 'nickname') {
  112. $row[] = $order['user']['nickname'];
  113. }
  114. else if ($field == 'pay_time') {
  115. $row[] = !empty($order['pay_time']) ? date('Y-m-d H:i:s', $order['pay_time']) : '';
  116. } else if($field=='store_name'){
  117. $row[] = $store_list[$order['store_id']]['store_name']?? '';
  118. }else {
  119. if ((isset($order[$field]))) $row[] = $order[$field];
  120. }
  121. }
  122. $data[] = $row;
  123. }
  124. $csv->data = $data;
  125. $csv->export();
  126. }
  127. return $this->render($this->action->id);
  128. }
  129. public function actionCommissionAndScoreList()
  130. {
  131. $params = \Yii::$app->request->get();
  132. $wallet_type = 'commission';
  133. if (!empty($params['wallet_type'])) $wallet_type = $params['wallet_type'];
  134. $condition = [
  135. 'where' => [
  136. ['source_table' => ['store_cashier_order',StoreCashierOrder::tableName(),'store_cashier_order_detail']],
  137. ['source_table_id' => $params['order_id']]
  138. ],
  139. ];
  140. CapitalLog::$capitalType = "{$wallet_type}";
  141. $list = CapitalLog::getList($condition, 0);
  142. $level = UserLevel::find()->select("name,level")->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])->asArray()->all();
  143. $level_data = array_column($level, 'name', 'level');
  144. CapitalLog::$capitalType = "{$wallet_type}_frozen";
  145. $condition['where'][] = ['status' => 0];
  146. $frozen_list = CapitalLog::getList($condition, 1);
  147. $list = array_merge($list, $frozen_list);
  148. return $this->success('操作成功', ['list' => $list, 'user_level' => $level_data]);
  149. }
  150. public function actionGetCouponList()
  151. {
  152. $params = \Yii::$app->request->get();
  153. $type = $params['type'] ?? 2;
  154. $store_id = $this->store_id;
  155. $is_install = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_COUPON);
  156. $coupon_list = [];
  157. if ($is_install) {
  158. $coupon_list = AddonsCoupon::lists([
  159. 'where' => [
  160. ['store_id' => $store_id],
  161. ['type' => $type],
  162. ['status' => StatusEnum::ENABLED],
  163. ['coupon_status' => AddonsCouponEnum::STATUS_RECEIVING],
  164. ],
  165. 'select' => 'id,name'
  166. ]);
  167. }
  168. return $this->success('ok', ['coupon_list' => $coupon_list]);
  169. }
  170. public function actionShowMiniPromotionCode()
  171. {
  172. $request = \Yii::$app->request;
  173. if ($request->isAjax) {
  174. if ($request->isPost) {
  175. $params = \Yii::$app->request->post();
  176. // 检查提交的参数
  177. $res = \Yii::$app->services->validate->validate($params, [
  178. [['type'], 'required'],
  179. ]);
  180. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  181. $store = HappinessStore::findOne(['mall_id' => $this->mall_id, 'id' => $this->store_id]);
  182. if (empty($store)) return $this->error('店铺不存在');
  183. $model = StoreMiniCode::findOne(['store_id' => $store['id'], 'mall_id' => $this->mall_id, 'type' => $params['type']]);
  184. if (empty($model)) {
  185. $mall = $this->mall;
  186. $content = [
  187. 'mall_id' => $this->mall_id,
  188. 'store_id' => $store['id'],
  189. 'mall_sign' => $mall['mall_sign'],
  190. 'type' => $params['type'],
  191. ];
  192. $content = json_encode($content);
  193. if ($params['type'] == 1) {
  194. $page = 'plugins2/Store/store/index';
  195. } else {
  196. $page = '/plugins2/Store/pay/cashRegister';
  197. }
  198. $code = md5($content);
  199. $obj = new MiniQrCode();
  200. $applet_code = $obj->getAppletQrCode($this->mall_id, $page, $code);
  201. $params['mall_id'] = $this->mall_id;
  202. $params['code'] = $code;
  203. $params['store_id'] = $store['id'];
  204. $params['content'] = $content;
  205. $params['applet_code'] = $applet_code;
  206. $model = StoreMiniCode::setData($params);
  207. }
  208. $applet_url = $model['applet_code'];
  209. $this->success('获取成功', ['applet_url' => $applet_url]);
  210. }
  211. }
  212. }
  213. }