EventRegistration.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\merchant\controller\ump;
  12. use app\merchant\controller\AuthController;
  13. use app\merchant\model\ump\EventPrice;
  14. use service\JsonService as Json;
  15. use think\Request;
  16. use think\Url;
  17. use app\merchant\model\user\User;
  18. use app\admin\model\user\UserBill;
  19. use service\FormBuilder as Form;
  20. use app\merchant\model\ump\EventRegistration as EventRegistrationModel;
  21. use app\merchant\model\ump\EventSignUp as EventSignUpModel;
  22. use app\merchant\model\ump\EventData as EventDataModel;
  23. use app\merchant\model\ump\EventPrice as EventPriceModel;
  24. use app\admin\model\merchant\MerchantFlowingWater;
  25. use service\AlipayTradeWapService;
  26. use behavior\wechat\PaymentBehavior;
  27. use service\HookService;
  28. /**活动控制器
  29. * Class EventRegistration
  30. * @package app\merchant\controller\ump
  31. */
  32. class EventRegistration extends AuthController
  33. {
  34. public function index()
  35. {
  36. return $this->fetch();
  37. }
  38. /**
  39. * 活动列表
  40. */
  41. public function event_registration_list()
  42. {
  43. $where = parent::getMore([
  44. ['title', ''],
  45. ['status', ''],
  46. ['is_show', ''],
  47. ['page', 1],
  48. ['limit', 20],
  49. ], $this->request);
  50. $where['mer_id'] = $this->merchantId;
  51. return Json::successlayui(EventRegistrationModel::systemPage($where));
  52. }
  53. /**活动审核
  54. * @return mixed
  55. */
  56. public function examine()
  57. {
  58. return $this->fetch();
  59. }
  60. /**
  61. * 异步查找产品
  62. *
  63. * @return json
  64. */
  65. public function event_examine_ist()
  66. {
  67. $where = parent::getMore([
  68. ['title', ''],
  69. ['page', 1],
  70. ['limit', 20],
  71. ['status', ''],
  72. ]);
  73. return Json::successlayui(EventRegistrationModel::eventExamineList($where));
  74. }
  75. /**编辑
  76. * @param string $is_show
  77. * @param string $id
  78. */
  79. public function set_show($is_show = '', $id = '')
  80. {
  81. if ($is_show == '' || $id == '') return Json::fail('缺少参数');
  82. $res = parent::getDataModification('event', $id, 'is_show', (int)$is_show);
  83. if ($res)
  84. return Json::successful($is_show == 1 ? '显示成功' : '隐藏成功');
  85. else
  86. return Json::fail($is_show == 1 ? '显示失败' : '隐藏失败');
  87. }
  88. /**获得添加
  89. * @return mixed|void
  90. * @throws \think\db\exception\DataNotFoundException
  91. * @throws \think\db\exception\ModelNotFoundException
  92. * @throws \think\exception\DbException
  93. */
  94. public function create($id = 0)
  95. {
  96. $news = [];
  97. $event = [];
  98. $price = [];
  99. if ($id) {
  100. $news = EventRegistrationModel::eventRegistrationOne($id);
  101. $event = EventDataModel::eventDataList($id);
  102. $price = EventPriceModel::eventPriceList($id);
  103. if (!count($price)) {
  104. $price[0] = [
  105. 'event_id' => $id,
  106. 'event_number' => 1,
  107. 'event_price' => $news['price'],
  108. 'event_mer_price' => $news['member_price'],
  109. 'sort' => 0
  110. ];
  111. }
  112. }
  113. $this->assign(['id' => $id, 'news' => json_encode($news), 'event' => json_encode($event), 'price' => json_encode($price)]);
  114. return $this->fetch();
  115. }
  116. /**
  117. * 删除活动
  118. * */
  119. public function delete($id)
  120. {
  121. $res = EventRegistrationModel::delArticleCategory($id);
  122. if (!$res)
  123. return Json::fail(EventRegistrationModel::getErrorInfo('删除失败,请稍候再试!'));
  124. else {
  125. EventDataModel::delEventData($id);
  126. EventPriceModel::delEventPrice($id);
  127. return Json::successful('删除成功!');
  128. }
  129. }
  130. /**
  131. * 添加和修改活动
  132. */
  133. public function add_new()
  134. {
  135. $data = parent::postMore([
  136. ['id', 0],
  137. 'title',
  138. 'image',
  139. 'qrcode_img',
  140. 'activity_rules',
  141. 'content',
  142. 'number',
  143. 'province',
  144. 'city',
  145. 'district',
  146. 'detail',
  147. 'signup_start_time',
  148. 'signup_end_time',
  149. 'start_time',
  150. 'end_time',
  151. ['sort', 0],
  152. ['event', ''],//资料
  153. ['event_price', ''],//价格
  154. ['restrictions', 0],
  155. ['is_fill', 1],
  156. ['is_show', 0],
  157. ['pay_type', 1],
  158. 'price',
  159. ['member_pay_type', 1],
  160. 'member_price'
  161. ]);
  162. $data['signup_start_time'] = strtotime($data['signup_start_time']);
  163. $data['signup_end_time'] = strtotime($data['signup_end_time']);
  164. $data['start_time'] = strtotime($data['start_time']);
  165. $data['end_time'] = strtotime($data['end_time']);
  166. if (bcsub($data['signup_end_time'], $data['signup_start_time'], 0) <= 0) return Json::fail('报名结束时间不能小于等于开始时间');
  167. if (bcsub($data['start_time'], $data['signup_end_time'], 0) <= 0) return Json::fail('活动开始时间不能小于等于报名结束时间');
  168. if (bcsub($data['end_time'], $data['start_time'], 0) <= 0) return Json::fail('活动结束时间不能小于等于开始时间');
  169. $data['content'] = htmlspecialchars($data['content']);
  170. $data['activity_rules'] = htmlspecialchars($data['activity_rules']);
  171. if (isset($data['event']) && $data['event'] != '') {
  172. $event = json_decode($data['event'], true);
  173. } else {
  174. $event = [];
  175. }
  176. if (isset($data['event_price']) && $data['event_price'] != '') {
  177. $price = json_decode($data['event_price'], true);
  178. } else {
  179. $price = [];
  180. }
  181. $number = $data['number'];
  182. if ($data['id']) {
  183. $id = $data['id'];
  184. $data['status'] = $this->isAudit == 1 ? 0 : 1;
  185. unset($data['id'], $data['event'], $data['event_price']);
  186. EventRegistrationModel::beginTrans();
  187. $res1 = EventRegistrationModel::edit($data, $id, 'id');
  188. $res2 = EventDataModel::eventDataAdd($id, $event);
  189. $res3 = EventPriceModel::eventPriceAdd($id, $price, $number);
  190. $res = $res1 && $res2 && $res3;
  191. EventRegistrationModel::checkTrans($res);
  192. if ($res) {
  193. return Json::successful('修改活动成功!', $id);
  194. } else
  195. return Json::fail('修改活动失败,您并没有修改什么!', $id);
  196. } else {
  197. $data['mer_id'] = $this->merchantId;
  198. $data['status'] = $this->isAudit == 1 ? 0 : 1;
  199. $data['add_time'] = time();
  200. EventRegistrationModel::beginTrans();
  201. $id = EventRegistrationModel::insertGetId($data);
  202. $res2 = EventDataModel::eventDataAdd($id, $event);
  203. $res3 = EventPriceModel::eventPriceAdd($id, $price, $number);
  204. $res = $id && $res2 && $res3;
  205. EventRegistrationModel::checkTrans($res);
  206. if ($res)
  207. return Json::successful('添加活动成功!', $id);
  208. else
  209. return Json::successful('添加活动失败!', $id);
  210. }
  211. }
  212. /**
  213. * 查看报名人员
  214. */
  215. public function viewStaff($id)
  216. {
  217. if (!$id) return Json::fail('参数错误!');
  218. $activity = EventRegistrationModel::where('id', $id)->find();
  219. if (!$activity) return Json::fail('活动不存在!');
  220. $this->assign(['aid' => $id]);
  221. return $this->fetch('view_staff');
  222. }
  223. /**报名订单订单
  224. * @return mixed
  225. */
  226. public function order()
  227. {
  228. $this->assign([
  229. 'year' => getMonth('y'),
  230. 'orderCount' => EventSignUpModel::orderCount($this->merchantId)
  231. ]);
  232. return $this->fetch();
  233. }
  234. /**
  235. * 查看活动报名订单列表
  236. */
  237. public function get_sign_up_list()
  238. {
  239. $where = parent::getMore([
  240. ['id', $this->request->param('id')],
  241. ['page', 1],
  242. ['limit', 20],
  243. ['status', ''],
  244. ['type', $this->request->param('type')],
  245. ['real_name', ''],
  246. ['data', ''],
  247. ['excel', 0],
  248. ]);
  249. $where['mer_id'] = $this->merchantId;
  250. return Json::successlayui(EventSignUpModel::getUserSignUpAll($where));
  251. }
  252. /**
  253. * 统计
  254. */
  255. public function getBadge()
  256. {
  257. $where = parent::postMore([
  258. ['id', $this->request->param('id')],
  259. ['status', ''],
  260. ['type', ''],
  261. ['data', ''],
  262. ['real_name', '']
  263. ]);
  264. $where['mer_id'] = $this->merchantId;
  265. return Json::successful(EventSignUpModel::getBadge($where));
  266. }
  267. /**用户活动核销
  268. * @param string $order_id
  269. * @param int $aid
  270. * @param string $code
  271. * @throws \think\db\exception\DataNotFoundException
  272. * @throws \think\db\exception\ModelNotFoundException
  273. * @throws \think\exception\DbException
  274. */
  275. public function scanCodeSignIn($id)
  276. {
  277. if (!$id) $this->failed('参数有误!');
  278. $order = EventSignUpModel::where('id', $id)->find();
  279. if (!$order) $this->failed('订单不存在!');
  280. if ($order['status']) $this->failed('订单已核销!');
  281. $res = EventSignUpModel::where(['id' => $id, 'paid' => 1, 'is_del' => 0])->update(['status' => 1]);
  282. if ($res) return Json::successful('核销成功');
  283. else return Json::fail('核销失败');
  284. }
  285. /**
  286. * 修改退款状态
  287. * @param $id
  288. * @return \think\response\Json|void
  289. */
  290. public function refund_y($id)
  291. {
  292. if (!$id) return $this->failed('数据不存在');
  293. $product = EventSignUpModel::get($id);
  294. if (!$product) return Json::fail('数据不存在!');
  295. if ($product['paid'] == 1) {
  296. $f = array();
  297. $f[] = Form::input('order_id', '退款单号', $product->getData('order_id'))->disabled(1);
  298. $f[] = Form::number('refund_price', '退款金额', $product->getData('pay_price'))->precision(2)->min(0.01);
  299. $form = Form::make_post_form('退款处理', $f, Url::build('updateRefundY', array('id' => $id)), 4);
  300. $this->assign(compact('form'));
  301. return $this->fetch('public/form-builder');
  302. } else return Json::fail('数据不存在!');
  303. }
  304. /**退款处理
  305. * @param Request $request
  306. * @param $id
  307. */
  308. public function updateRefundY(Request $request, $id)
  309. {
  310. $data = parent::postMore([
  311. 'refund_price',
  312. ], $request);
  313. if (!$id) return Json::fail('数据不存在');
  314. $product = EventSignUpModel::get($id);
  315. if (!$product) return Json::fail('数据不存在!');
  316. if ($product['pay_price'] == $product['refund_price']) return Json::fail('已退完支付金额!不能再退款了');
  317. if (!$data['refund_price']) return Json::fail('请输入退款金额');
  318. $refund_price = $data['refund_price'];
  319. $data['refund_price'] = bcadd($data['refund_price'], $product['refund_price'], 2);
  320. $bj = bccomp((float)$product['pay_price'], (float)$data['refund_price'], 2);
  321. if ($bj < 0) return Json::fail('退款金额大于支付金额,请修改退款金额');
  322. $data['refund_status'] = 2;
  323. $refund_data['pay_price'] = $product['pay_price'];
  324. $refund_data['refund_price'] = $refund_price;
  325. if ($product['pay_type'] == 'weixin') {
  326. try {
  327. HookService::listen('wechat_pay_order_refund', $product['order_id'], $refund_data, true, PaymentBehavior::class);
  328. } catch (\Exception $e) {
  329. return Json::fail($e->getMessage());
  330. }
  331. } else if ($product['pay_type'] == 'yue') {
  332. EventSignUpModel::beginTrans();
  333. $res = User::bcInc($product['uid'], 'now_money', $refund_price, 'uid');
  334. EventSignUpModel::checkTrans($res);
  335. if (!$res) return Json::fail('余额退款失败!');
  336. } else if ($product['pay_type'] == 'zhifubao') {
  337. $res = AlipayTradeWapService::init()->AliPayRefund($product['order_id'], $product['trade_no'], $refund_price, '活动订单退款', 'refund');
  338. if (empty($res) || $res != 10000) {
  339. return Json::fail('支付宝退款失败!');
  340. }
  341. }
  342. $data['refund_reason_time'] = time();
  343. $resEdit = EventSignUpModel::edit($data, $id);
  344. if ($resEdit) {
  345. $pay_type = $product['pay_type'] == 'yue' ? 'now_money' : $product['pay_type'];
  346. if ($product['pay_type'] == 'yue') {
  347. $balance = User::where(['uid' => $product['uid']])->value('now_money');
  348. } else {
  349. $balance = 0;
  350. }
  351. UserBill::income('活动订单退款', $product['uid'], $pay_type, 'pay_sign_up_refund', $refund_price, $product['id'], $balance, '活动订单退款' . floatval($refund_price) . '元');
  352. MerchantFlowingWater::orderRefund($id, $product['mer_id'], 4);
  353. return Json::successful('修改成功!');
  354. } else {
  355. return Json::successful('修改失败!');
  356. }
  357. }
  358. /**订单详情
  359. * @param string $oid
  360. * @return mixed|void
  361. * @throws \think\exception\DbException
  362. */
  363. public function order_info($oid = '')
  364. {
  365. if (!$oid || !($orderInfo = EventSignUpModel::get($oid)))
  366. return $this->failed('订单不存在!');
  367. $userInfo = User::getAllUserinfo($orderInfo['uid']);
  368. $this->assign(compact('orderInfo', 'userInfo'));
  369. return $this->fetch();
  370. }
  371. /**订单删除
  372. * @param int $id
  373. */
  374. public function order_delete($id = 0)
  375. {
  376. if (!$id) return Json::fail('参数错误!');
  377. $data['is_del'] = 1;
  378. EventSignUpModel::edit($data, $id);
  379. return Json::successful('删除成功!');
  380. }
  381. }