StoreAssetsController.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace addons\Mch\backend\controllers;
  3. use addons\Mch\common\models\Mch;
  4. use addons\Mch\common\models\MchCommission;
  5. use common\enums\DownloadEnum;
  6. use common\enums\StatusEnum;
  7. use common\helpers\csv\CsvExportHelper;
  8. use common\models\user\User;
  9. class StoreAssetsController extends BaseController
  10. {
  11. public function actionIndex()
  12. {
  13. if (!\Yii::$app->request->isAjax) {
  14. return $this->render('/store-assets/index');
  15. }
  16. if (\Yii::$app->request->isGet) {
  17. $tabname = \Yii::$app->request->get('tabname');
  18. if (empty($tabname) || !in_array($tabname, ['order-detail', 'cashier'])) {
  19. return $this->error('参数错误: tabname');
  20. }
  21. if ('order-detail' == $tabname) {
  22. return $this->orderDetailList();
  23. }
  24. }
  25. if (\Yii::$app->request->post('flag') == 'EXPORT') {
  26. $post = \Yii::$app->request->post();
  27. $post['mall_id'] = $this->mall_id;
  28. $filename = '平台-多商户订单明细列表-' . date('YmdHis') . '_' . rand(10000, 99999);
  29. $res = CsvExportHelper::exportDownLoadCenter($this->mall_id, $filename, DownloadEnum::TYPE_ASSETS, MchCommission::class, 'orderDetailExportHandle', $post);
  30. if ($res === false) return $this->error('加入导出任务失败' . CsvExportHelper::getStaticError());
  31. return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
  32. }
  33. }
  34. public function actionTotal()
  35. {
  36. $total_order_amounts = MchCommission::find()->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'type' => 1])->sum('order_money');
  37. $settle_order_amounts = MchCommission::find()->where(['mall_id' => $this->mall_id, 'is_settlement'=>1,'status' => StatusEnum::ENABLED, 'type' => 1])->sum('order_money');
  38. $not_settle_order_amounts = MchCommission::find()->where(['mall_id' => $this->mall_id, 'is_settlement'=>0,'status' => StatusEnum::ENABLED, 'type' => 1])->sum('order_money');
  39. $log_list['total_order_amounts'] = !empty($total_order_amounts)?bcadd($total_order_amounts,0,2):0;
  40. $log_list['settle_order_amounts'] = !empty($settle_order_amounts)?bcadd($settle_order_amounts,0,2):0;
  41. $log_list['not_settle_order_amounts'] = !empty($not_settle_order_amounts)?bcadd($not_settle_order_amounts,0,2):0;
  42. return $this->success('',$log_list);
  43. }
  44. /**
  45. * @name:
  46. * @msg: 门店订单明细
  47. * @param {*}
  48. * @return {*}
  49. */
  50. private function orderDetailList()
  51. {
  52. $page = \Yii::$app->request->get('page', 1);
  53. $limit = \Yii::$app->request->get('limit', $this->pageSize);
  54. $store_name = \Yii::$app->request->get('store_name');
  55. $user_id = \Yii::$app->request->get('user_id');
  56. $nickname = \Yii::$app->request->get('nickname');
  57. $mobile = \Yii::$app->request->get('mobile');
  58. $order_no = \Yii::$app->request->get('order_no');
  59. $order_status = \Yii::$app->request->get('order_status');
  60. if (!in_array($order_status, [0, 1, 2, 3])) {
  61. return $this->error('参数错误:order_status');
  62. }
  63. $wheres[] = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'type' => 1];
  64. $stores = [];
  65. if (!empty($store_name)) {
  66. $stores = Mch::find()
  67. ->select('id,store_name,logo_img')
  68. ->where(['mall_id' => $this->mall_id, 'status' =>[StatusEnum::ENABLED,StatusEnum::DISABLED]])
  69. ->andWhere(['like', 'store_name', $store_name])
  70. ->asArray()
  71. ->indexBy('id')
  72. ->all();
  73. $store_ids = array_column($stores, 'id');
  74. $wheres[] = ['mch_id' => $store_ids];
  75. }
  76. if (!empty($user_id)) {
  77. $wheres[] = ['user_id' => $user_id];
  78. }
  79. $user_wheres = [];
  80. $users = [];
  81. if (!empty($nickname)) {
  82. $user_wheres[] = ['like', 'nickname', $nickname];
  83. }
  84. if (!empty($mobile)) {
  85. $user_wheres[] = ['like', 'mobile', $mobile];
  86. }
  87. if (!empty($user_wheres)) {
  88. $user_wheres[] = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED];
  89. $query = User::find()->select('id,nickname,mobile,avatar_url');
  90. foreach ($user_wheres as $where) {
  91. $query->andWhere($where);
  92. }
  93. $users = $query->asArray()->indexBy('id')->all();
  94. $user_ids = array_column($users, 'id');
  95. $wheres[] = ['user_id' => $user_ids];
  96. }
  97. if (!empty($order_no)) {
  98. $wheres[] = ['like', 'order_no', $order_no];
  99. }
  100. if (!empty($order_status)) {
  101. if (1 == $order_status) {
  102. $wheres[] = ['is_settlement' => 0];
  103. } elseif (2 == $order_status) {
  104. $wheres[] = ['is_settlement' => 1];
  105. } else {
  106. $wheres[] = ['!=', 'status', StatusEnum::ENABLED];
  107. }
  108. }
  109. $params = [
  110. 'select' => 'id,mall_id,mch_id,user_id,order_no,order_money,pay_money,discount_money,addons_bonus,service_charge,amount_received,is_settlement,created_at,status',
  111. 'where' => $wheres,
  112. 'order' => 'created_at desc',
  113. 'page' => $page,
  114. 'limit' => $limit
  115. ];
  116. $log_list = MchCommission::listPage($params);
  117. $log_list['orderDetailFieldsList'] = MchCommission::orderDetailFieldsList();
  118. if (empty($log_list['list'])) {
  119. if (false === $log_list['list']) {
  120. return $this->error(MchCommission::getStaticError());
  121. }
  122. return $this->success('success', $log_list);
  123. }
  124. if (empty($stores)) {
  125. $stores = Mch::find()
  126. ->select('id,store_name,logo_img')
  127. ->where(['mall_id' => $this->mall_id, 'status' => [StatusEnum::ENABLED,StatusEnum::DISABLED]])
  128. ->andWhere(['like', 'store_name', $store_name])
  129. ->asArray()
  130. ->indexBy('id')
  131. ->all();
  132. }
  133. if (empty($users)) {
  134. $user_ids = array_column($log_list['list'], 'user_id');
  135. $users = User::find()
  136. ->select('id,nickname,mobile,avatar_url')
  137. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $user_ids])
  138. ->asArray()
  139. ->indexBy('id')
  140. ->all();
  141. }
  142. foreach ($log_list['list'] as &$list) {
  143. $store = $stores[$list['mch_id']] ?? [];
  144. $user = $users[$list['user_id']] ?? [];
  145. $list['nickname'] = $user['nickname'] ?? '';
  146. $list['mobile'] = $user['mobile'] ?? '';
  147. $list['avatar_url'] = !empty($user['avatar_url']) ? $user['avatar_url'] : \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png';
  148. $list['store_name'] = $store['store_name'] ?? '';
  149. $list['store_logo'] = $store['logo_img'] ?? \Yii::$app->request->hostInfo . '/resources/img/common/o2o-store-default-logo.png';
  150. if ($list['status'] != StatusEnum::ENABLED) {
  151. $list['settle_status'] = 3;
  152. } else {
  153. if ($list['is_settlement'] == 0) {
  154. $list['settle_status'] = 1;
  155. } else {
  156. $list['settle_status'] = 2;
  157. }
  158. }
  159. }
  160. // $total_order_amounts = $settle_order_amounts = $not_settle_order_amounts = 0;
  161. // $page = 1;
  162. // $limit = 200;
  163. // do {
  164. // $offset = ($page - 1) * $limit;
  165. // $page++;
  166. // $commissions = MchCommission::find()->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'type' => 1])->limit($limit)->offset($offset)->all();
  167. // if (!empty($commissions)) {
  168. // foreach ($commissions as $data) {
  169. // $total_order_amounts += $data->order_money;
  170. // if (1 == $data->is_settlement) {
  171. // $settle_order_amounts += $data->order_money;
  172. // } else {
  173. // $not_settle_order_amounts += $data->order_money;
  174. // }
  175. // }
  176. // }
  177. // } while ($commissions);
  178. // $log_list['total_order_amounts'] = round($total_order_amounts, 2);
  179. // $log_list['settle_order_amounts'] = round($settle_order_amounts, 2);
  180. // $log_list['not_settle_order_amounts'] = round($not_settle_order_amounts, 2);
  181. return $this->success('success', $log_list);
  182. }
  183. }