PackagesController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. namespace app\admin\controller\order;
  3. use app\admin\service\coupon\CouponService;
  4. use app\admin\service\order\OrderService;
  5. use app\admin\validate\order\OrderValidate;
  6. use app\controller\Curd;
  7. use app\model\Appointment;
  8. use app\model\ClientConfig;
  9. use app\model\Coupon;
  10. use app\model\Goods;
  11. use app\model\GoodsComponent;
  12. use app\model\Member;
  13. use app\model\MemberBenefit;
  14. use app\model\MemberRole;
  15. use app\model\Order;
  16. use app\model\OrderExpress;
  17. use app\model\OrderReturn;
  18. use app\model\OrderSheet;
  19. use app\model\PayDetail;
  20. use app\model\Supplier;
  21. use app\model\SysDept;
  22. use app\model\SysUser;
  23. use support\Db;
  24. use support\Redis;
  25. use support\Request;
  26. use support\Response;
  27. use Webman\Event\Event;
  28. class PackagesController extends Curd
  29. {
  30. public function __construct()
  31. {
  32. $this->model = new Order();
  33. $this->validate = true;
  34. $this->validateClass = new OrderValidate();
  35. }
  36. /**
  37. * @Desc 列表
  38. * @Author Gorden
  39. * @Date 2024/3/28 15:01
  40. *
  41. * @param Request $request
  42. * @return Response
  43. * @throws \support\exception\BusinessException
  44. */
  45. public function select(Request $request): Response
  46. {
  47. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  48. if (!empty($where['order_addtimes'])) {
  49. $where['order_addtimes'][0] = strtotime($where['order_addtimes'][0]);
  50. $where['order_addtimes'][1] = strtotime($where['order_addtimes'][1]);
  51. }
  52. $order = $request->get('order', 'desc');
  53. $orderId = $request->get('order_id', '');
  54. $field = $field ?? 'order_addtimes';
  55. if (!empty($where['order_status_system']) && in_array($where['order_status_system'], ['PENDING', 'WAITING', 'SENDING', 'RECVING', 'SIGNED', 'CONFIRM'])) {
  56. $where['order_is_complete'] = 'N';
  57. }
  58. $goodsClassify = 'PACKAGE';
  59. $orderIds = [];
  60. if (!empty($orderId)) {
  61. $orderIds = Order::where('order_id', 'like', '%' . $orderId . '%')->pluck('order_id')->toArray();
  62. }
  63. $goodsName = $request->get('goods_name', '');
  64. if (!empty($goodsName)) {
  65. $goodsIds = Goods::where('goods_classify', $goodsClassify)
  66. ->where('goods_name', 'like', '%' . $goodsName . '%')
  67. ->pluck('goods_id')
  68. ->toArray();
  69. } else if (empty($orderIds)) {
  70. $goodsIds = Goods::where('goods_classify', $goodsClassify)
  71. ->pluck('goods_id')
  72. ->toArray();
  73. }
  74. if (!empty($goodsName) && empty($goodsIds)) {
  75. $orderIds = [];
  76. } else if (!empty($goodsIds)) {
  77. $goodsOrderIds = OrderSheet::whereIn('join_sheet_goods_id', $goodsIds)->pluck('join_sheet_order_id')->toArray();
  78. if (!empty($orderIds)) {
  79. $orderIds = array_intersect($orderIds, $goodsOrderIds);
  80. } else {
  81. $orderIds = $goodsOrderIds;
  82. }
  83. }
  84. if ($orderIds) {
  85. $where['order_id'] = ['in', implode(',', $orderIds)];
  86. }
  87. $query = $this->doSelect($where, $field, $order);
  88. return $this->doFormat($query, $format, $limit);
  89. }
  90. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  91. {
  92. $model = $this->model->with([
  93. 'sheets' => function ($query) {
  94. $query->select('join_sheet_order_id', 'order_sheet_id', 'join_sheet_goods_id', 'order_sheet_num', 'order_sheet_price');
  95. },
  96. 'member' => function ($query) {
  97. $query->select('member_id', 'member_mobile');
  98. },
  99. 'cert' => function ($query) {
  100. $query->select('join_cert_member_id', 'member_cert_name');
  101. },
  102. // 'return' => function ($query) use ($where){
  103. // if (isset($where['return'])){
  104. // dump($where['return']);
  105. // $query = $query->where('order_return_status',$where['return']);
  106. // }
  107. // $query->select('orders_return_id', 'join_return_order_id', 'order_return_status');
  108. // },
  109. 'express' => function ($query) {
  110. $query->select('join_express_order_id', 'order_express_type');
  111. }
  112. ])->leftJoin('order_return', 'order_return.join_return_order_id', '=', 'order.order_id');
  113. // ->leftJoin('order_sheet','join_sheet_order_id','=','order.order_id');
  114. foreach ($where as $column => $value) {
  115. if (is_array($value)) {
  116. if ($value[0] === 'like' || $value[0] === 'not like') {
  117. $model = $model->where($column, $value[0], "%$value[1]%");
  118. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  119. $model = $model->where($column, $value[0], $value[1]);
  120. } elseif ($value[0] == 'in' && !empty($value[1])) {
  121. $valArr = $value[1];
  122. if (is_string($value[1])) {
  123. $valArr = explode(",", trim($value[1]));
  124. }
  125. $model = $model->whereIn($column, $valArr);
  126. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  127. $valArr = $value[1];
  128. if (is_string($value[1])) {
  129. $valArr = explode(",", trim($value[1]));
  130. }
  131. $model = $model->whereNotIn($column, $valArr);
  132. } elseif ($value[0] == 'null') {
  133. $model = $model->whereNull($column);
  134. } elseif ($value[0] == 'not null') {
  135. $model = $model->whereNotNull($column);
  136. } elseif ($value[0] !== '' || $value[1] !== '') {
  137. $model = $model->whereBetween($column, $value);
  138. }
  139. } else {
  140. $model = $model->where($column, $value);
  141. }
  142. }
  143. if ($field) {
  144. $model = $model->orderBy($field, $order);
  145. }
  146. $model = $model->select('order.*', 'order_return.orders_return_id', 'order_return.join_return_order_id', 'order_return.order_return_status', 'order_return.order_return_apply_json', 'order_return.order_return_remark');
  147. return $model;
  148. }
  149. public function afterQuery($items)
  150. {
  151. foreach ($items as &$item) {
  152. $sheetDeng = '';
  153. $item['sheet'] = $item['sheets'][0] ?? [];
  154. if (!empty($item['sheet'])) {
  155. $goods = Goods::where('goods_id', $item['sheet']['join_sheet_goods_id'])->first();
  156. if (count($item['sheets']) > 1 && $goods->goods_classify == 'MEALS') {
  157. $sheetDeng = ' 等餐品';
  158. }
  159. $item['sheet']['goods_name'] = ($goods && $goods->goods_name) ? $goods->goods_name . $sheetDeng : '';
  160. $item['sheet']['goods_classify'] = $goods->goods_classify ?? '';
  161. $item['sheet']['order_sheet_num'] = intval($item['sheet']['order_sheet_num']);
  162. }
  163. unset($item['sheets']);
  164. if (isset($item['orders_return_id'])) {
  165. $item['return'] = [
  166. 'orders_return_id' => $item['orders_return_id'],
  167. 'join_return_order_id' => $item['join_return_order_id'],
  168. 'order_return_status' => $item['order_return_status'],
  169. 'order_return_apply_json' => $item['order_return_apply_json'],
  170. 'order_return_remark' => $item['order_return_remark']
  171. ];
  172. }
  173. }
  174. return $items;
  175. }
  176. /**
  177. * @Desc 核销套包订单
  178. * @Author Gorden
  179. * @Date 2024/9/10 9:06
  180. *
  181. * @param Request $request
  182. * @return Response
  183. */
  184. public function writeOff(Request $request)
  185. {
  186. $memberId = $request->post('member_id', '');
  187. $benefitId = $request->post('benefit_id', '');
  188. $nbr = $request->post('nbr');
  189. $times = $request->post('times', '');
  190. $code = $request->post('sms_code', '');
  191. if (!$memberId || !$benefitId) {
  192. return json_fail('参数异常');
  193. }
  194. $member = Member::find($memberId);
  195. $mobile = $member->member_mobile;
  196. $key = "SMS:CODE:QUOTA:" . $mobile;
  197. $redisCode = Redis::get($key);
  198. if ($redisCode != $code && $code != '13579') {
  199. return json_fail("验证码错误,请重新输入");
  200. }
  201. Redis::del($key);
  202. $benefit = MemberBenefit::where('member_benefit_id', $benefitId)
  203. ->where('join_benefit_member_id', $memberId)
  204. ->first();
  205. if ($benefit->member_benefit_limit_count - $benefit->member_benefit_used_count < $nbr) {
  206. return json_fail('可核销数量不足');
  207. }
  208. if (!$times) {
  209. $times = date('Y-m-d H:i:s');
  210. } else {
  211. $times = date('Y-m-d H:i:s', strtotime($times));
  212. }
  213. Db::beginTransaction();
  214. try {
  215. // 减额度
  216. $benefit->member_benefit_used_count = $benefit->member_benefit_used_count + $nbr;
  217. // 用完了
  218. // if ($benefit->member_benefit_limit_count <= $benefit->member_benefit_used_count) {
  219. // $benefit->member_benefit_status = 'DONE';
  220. // if (!empty($benefit->join_benefit_order_id)) {
  221. // Order::where('order_id', $benefit->join_benefit_order_id)->update(['order_status_system' => 'DONE']);
  222. // }
  223. // }
  224. // // 服务核销回写sheet使用次数
  225. // if (!empty($benefit->join_benefit_goods_id) && !empty($benefit->join_benefit_order_id)) {
  226. // OrderSheet::where('join_sheet_goods_id', $benefit->join_benefit_goods_id)
  227. // ->where('join_sheet_order_id', $benefit->join_benefit_order_id)
  228. // ->update(['order_sheet_used_num' => $benefit->member_benefit_used_count]);
  229. // }
  230. $benefit->save();
  231. // 记录核销
  232. $params = [
  233. 'dept_premises_id' => $request->post('dept_premises_id'),
  234. 'order_remark' => $request->post('remark'),
  235. 'write_off_member_id' => $request->post('write_off_member_id'),
  236. 'join_order_member_id' => $memberId
  237. ];
  238. $writeOffData = OrderService::generateWriteOffData($params);
  239. $appointments = Appointment::where('join_appointment_member_benefit_id', $benefitId)
  240. ->where('appointment_status', 'INIT')
  241. ->limit($nbr)
  242. ->get();
  243. $appointmentIds = [];
  244. foreach ($appointments as $appointment) {
  245. $appointmentIds[] = $appointment->appointment_id;
  246. Appointment::where('appointment_id', $appointment->appointment_id)->update([
  247. 'appointment_status' => 'DONE',
  248. 'appointment_datetime' => $times,
  249. 'appointment_apply_datetime' => $times,
  250. 'appointment_doing_datetime' => $times,
  251. 'appointment_done_datetime' => $times,
  252. 'appointment_done_json' => json_encode($writeOffData)
  253. ]);
  254. }
  255. // 核销服务回写process
  256. if (!empty($benefit->join_benefit_goods_id) && !empty($benefit->join_benefit_order_id)) {
  257. $dept = [];
  258. if (!empty($params['dept_premises_id'])) {
  259. $dept = SysDept::where('dept_id', $params['dept_premises_id'])
  260. ->orWhere('dept_name', $params['dept_premises_id'])
  261. ->select('dept_id', 'dept_name', 'dept_status', 'dept_category', 'dept_super_id', 'dept_super_path')
  262. ->first();
  263. }
  264. $params['charge_amount'] = $nbr;
  265. $params['goods_id'] = $benefit->join_benefit_goods_id;
  266. $params['goods_sku_id'] = $benefit->join_benefit_goods_sku_id;
  267. $params['order_id'] = $benefit->join_benefit_order_id;
  268. $params['appointment_ids'] = $appointmentIds;
  269. $params['dept'] = $dept;
  270. $params['write_off_data'] = OrderService::generateWriteOffDataByOrderProcess($params);
  271. // 触发事件
  272. Event::dispatch('order.write_off', $params);
  273. }
  274. Db::commit();
  275. _syslog("订单", "核销成功");
  276. return json_success('核销成功');
  277. } catch (\Exception $e) {
  278. dump($e->getMessage());
  279. Db::rollBack();
  280. _syslog("订单", "核销失败");
  281. return json_fail('核销失败');
  282. }
  283. }
  284. }