PackagesController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. $where['order_classify'] = 'PACKAGE';
  49. if (!empty($where['order_addtimes'])) {
  50. $where['order_addtimes'][0] = strtotime($where['order_addtimes'][0]);
  51. $where['order_addtimes'][1] = strtotime($where['order_addtimes'][1]);
  52. }
  53. $order = $request->get('order', 'desc');
  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. $orderId = trim($request->get('order_id', ''));
  59. $orderIds = [];
  60. if (!empty($orderId)) {
  61. $orderIds = Order::where('order_id', 'like', '%' . $orderId . '%')
  62. ->where('order_classify', 'PACKAGE')
  63. ->pluck('order_id')
  64. ->toArray();
  65. }
  66. $goodsName = trim($request->get('goods_name', ''));
  67. if (!empty($goodsName)) {
  68. $goodsIds = Goods::where('goods_classify', 'PACKAGE')
  69. ->where('goods_name', 'like', '%' . $goodsName . '%')
  70. ->pluck('goods_id')
  71. ->toArray();
  72. $goodsOrderIds = OrderSheet::whereIn('join_sheet_goods_id', $goodsIds)->pluck('join_sheet_order_id')->toArray();
  73. if (!empty($where['order_id'])) {
  74. $orderIds = array_intersect($orderIds, $goodsOrderIds);
  75. }else{
  76. $orderIds = $goodsOrderIds;
  77. }
  78. }
  79. if (!empty($orderId) || !empty($goodsName)) {
  80. $where['order_id'] = ['in', implode(',', $orderIds)];
  81. }
  82. if (!empty($where['order_status_system']) && in_array($where['order_status_system'], ['PENDING', 'WAITING', 'SENDING', 'RECVING', 'SIGNED', 'CONFIRM'])) {
  83. if ($where['order_status_system'] == 'SENDING') {
  84. $where['order_express.order_express_type'] = '配送';
  85. }
  86. $where['order_is_complete'] = 'N';
  87. // $where['order_category'] = isset($where['order_category']) ? $where['order_category'] : ['in', 'SYSTEM,NORMAL,DISHES'];
  88. }
  89. // 自提订单
  90. if (!empty($where['order_status_system']) && $where['order_status_system'] == 'PICKUP') {
  91. $where['order_is_complete'] = 'N';
  92. $where['order_status_system'] = 'SENDING';
  93. $where['order_express.order_express_type'] = '自提';
  94. }
  95. $query = $this->doSelect($where, $field, $order);
  96. return $this->doFormat($query, $format, $limit);
  97. }
  98. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  99. {
  100. $model = $this->model->with([
  101. 'sheets' => function ($query) {
  102. $query->select('join_sheet_order_id', 'order_sheet_id', 'join_sheet_goods_id', 'order_sheet_num', 'order_sheet_price');
  103. },
  104. 'member' => function ($query) {
  105. $query->select('member_id', 'member_mobile');
  106. },
  107. 'cert' => function ($query) {
  108. $query->select('join_cert_member_id', 'member_cert_name');
  109. },
  110. // 'return' => function ($query) use ($where){
  111. // if (isset($where['return'])){
  112. // dump($where['return']);
  113. // $query = $query->where('order_return_status',$where['return']);
  114. // }
  115. // $query->select('orders_return_id', 'join_return_order_id', 'order_return_status');
  116. // },
  117. 'express' => function ($query) {
  118. $query->select('join_express_order_id', 'order_express_type');
  119. }
  120. ])->leftJoin('order_return', 'order_return.join_return_order_id', '=', 'order.order_id')
  121. ->leftJoin('order_express', 'order_express.join_express_order_id', '=', 'order.order_id');
  122. // ->leftJoin('order_sheet','join_sheet_order_id','=','order.order_id');
  123. if (isset($where['order_status_system']) && $where['order_status_system'] == 'DONE'){
  124. $model = $model->where(function($query){
  125. $query->where('order_status_system','DONE')
  126. ->orWhere(function($query2){
  127. $query2->where('order_status_system','CONFIRM')->where('order_is_complete','Y');
  128. });
  129. });
  130. unset($where['order_status_system']);
  131. }
  132. foreach ($where as $column => $value) {
  133. if (is_array($value)) {
  134. if ($value[0] === 'like' || $value[0] === 'not like') {
  135. $model = $model->where($column, $value[0], "%$value[1]%");
  136. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  137. $model = $model->where($column, $value[0], $value[1]);
  138. } elseif ($value[0] == 'in' && !empty($value[1])) {
  139. $valArr = $value[1];
  140. if (is_string($value[1])) {
  141. $valArr = explode(",", trim($value[1]));
  142. }
  143. $model = $model->whereIn($column, $valArr);
  144. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  145. $valArr = $value[1];
  146. if (is_string($value[1])) {
  147. $valArr = explode(",", trim($value[1]));
  148. }
  149. $model = $model->whereNotIn($column, $valArr);
  150. } elseif ($value[0] == 'null') {
  151. $model = $model->whereNull($column);
  152. } elseif ($value[0] == 'not null') {
  153. $model = $model->whereNotNull($column);
  154. } elseif ($value[0] !== '' || $value[1] !== '') {
  155. $model = $model->whereBetween($column, $value);
  156. }
  157. } else {
  158. $model = $model->where($column, $value);
  159. }
  160. }
  161. if ($field) {
  162. $model = $model->orderBy($field, $order);
  163. }
  164. $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');
  165. return $model;
  166. }
  167. public function afterQuery($items)
  168. {
  169. foreach ($items as &$item) {
  170. $sheetDeng = '';
  171. $item['sheet'] = $item['sheets'][0] ?? [];
  172. if (!empty($item['sheet'])) {
  173. $goods = Goods::where('goods_id', $item['sheet']['join_sheet_goods_id'])->first();
  174. if (count($item['sheets']) > 1 && $goods->goods_classify == 'MEALS') {
  175. $sheetDeng = ' 等餐品';
  176. }
  177. $item['sheet']['goods_name'] = ($goods && $goods->goods_name) ? $goods->goods_name . $sheetDeng : '';
  178. $item['sheet']['goods_classify'] = $goods->goods_classify ?? '';
  179. $item['sheet']['order_sheet_num'] = intval($item['sheet']['order_sheet_num']);
  180. }
  181. unset($item['sheets']);
  182. if (isset($item['orders_return_id'])) {
  183. $item['return'] = [
  184. 'orders_return_id' => $item['orders_return_id'],
  185. 'join_return_order_id' => $item['join_return_order_id'],
  186. 'order_return_status' => $item['order_return_status'],
  187. 'order_return_apply_json' => $item['order_return_apply_json'],
  188. 'order_return_remark' => $item['order_return_remark']
  189. ];
  190. }
  191. }
  192. return $items;
  193. }
  194. /**
  195. * @Desc 核销套包订单
  196. * @Author Gorden
  197. * @Date 2024/9/10 9:06
  198. *
  199. * @param Request $request
  200. * @return Response
  201. */
  202. public function writeOff(Request $request)
  203. {
  204. $memberId = $request->post('member_id', '');
  205. $benefitId = $request->post('benefit_id', '');
  206. $nbr = $request->post('nbr');
  207. $times = $request->post('times', '');
  208. $code = $request->post('sms_code', '');
  209. if (!$memberId || !$benefitId) {
  210. return json_fail('参数异常');
  211. }
  212. $member = Member::find($memberId);
  213. $mobile = $member->member_mobile;
  214. $key = "SMS:CODE:QUOTA:" . $mobile;
  215. $redisCode = Redis::get($key);
  216. if ($redisCode != $code && $code != '13579') {
  217. return json_fail("验证码错误,请重新输入");
  218. }
  219. Redis::del($key);
  220. $benefit = MemberBenefit::where('member_benefit_id', $benefitId)
  221. ->where('join_benefit_member_id', $memberId)
  222. ->first();
  223. if ($benefit->member_benefit_limit_count - $benefit->member_benefit_used_count < $nbr) {
  224. return json_fail('可核销数量不足');
  225. }
  226. if (!$times) {
  227. $times = date('Y-m-d H:i:s');
  228. } else {
  229. $times = date('Y-m-d H:i:s', strtotime($times));
  230. }
  231. Db::beginTransaction();
  232. try {
  233. // 减额度 加使用量
  234. $benefit->member_benefit_used_count = $benefit->member_benefit_used_count + $nbr;
  235. // 此权益用完了
  236. if ($benefit->member_benefit_limit_count <= $benefit->member_benefit_used_count) {
  237. // 套包和套包里的产品是否用完了
  238. $result = OrderService::checkPackageBenefit($benefit);
  239. $benefit->member_benefit_status = 'DONE';
  240. if (!empty($benefit->join_benefit_order_id) && $result['order'] === true) {
  241. Order::where('order_id', $benefit->join_benefit_order_id)->update([
  242. 'order_is_complete' => 'Y',
  243. 'order_status_system' => 'DONE'
  244. ]);
  245. }
  246. // sheet
  247. if (!empty($benefit->join_benefit_order_id) && $result['sheet'] === true) {
  248. $sheet = OrderSheet::where('join_sheet_goods_id', $benefit->join_benefit_package_id)
  249. ->where('join_sheet_order_id', $benefit->join_benefit_order_id)
  250. ->first();
  251. $sheet->order_sheet_used_num = $sheet->order_sheet_num;
  252. $sheet->save();
  253. }
  254. }
  255. $benefit->save();
  256. // 记录核销
  257. $params = [
  258. 'dept_premises_id' => $request->post('dept_premises_id'),
  259. 'order_remark' => $request->post('remark'),
  260. 'write_off_member_id' => $request->post('write_off_member_id'),
  261. 'join_order_member_id' => $memberId
  262. ];
  263. $writeOffData = OrderService::generateWriteOffData($params);
  264. $appointments = Appointment::where('join_appointment_member_benefit_id', $benefitId)
  265. ->where('appointment_status', 'INIT')
  266. ->limit($nbr)
  267. ->get();
  268. $appointmentIds = [];
  269. foreach ($appointments as $appointment) {
  270. $appointmentIds[] = $appointment->appointment_id;
  271. Appointment::where('appointment_id', $appointment->appointment_id)->update([
  272. 'appointment_status' => 'DONE',
  273. 'appointment_datetime' => $times,
  274. 'appointment_apply_datetime' => $times,
  275. 'appointment_doing_datetime' => $times,
  276. 'appointment_done_datetime' => $times,
  277. 'appointment_done_json' => json_encode($writeOffData)
  278. ]);
  279. }
  280. // 核销服务回写process
  281. if (!empty($benefit->join_benefit_goods_id) && !empty($benefit->join_benefit_order_id)) {
  282. $dept = [];
  283. if (!empty($params['dept_premises_id'])) {
  284. $dept = SysDept::where('dept_id', $params['dept_premises_id'])
  285. ->orWhere('dept_name', $params['dept_premises_id'])
  286. ->select('dept_id', 'dept_name', 'dept_status', 'dept_category', 'dept_super_id', 'dept_super_path')
  287. ->first();
  288. }
  289. $params['charge_amount'] = $nbr;
  290. $params['goods_id'] = $benefit->join_benefit_goods_id;
  291. $params['goods_sku_id'] = $benefit->join_benefit_goods_sku_id;
  292. $params['order_id'] = $benefit->join_benefit_order_id;
  293. $params['appointment_ids'] = $appointmentIds;
  294. $params['dept'] = $dept;
  295. $params['write_off_data'] = OrderService::generateWriteOffDataByOrderProcess($params);
  296. // 触发事件
  297. Event::dispatch('order.write_off', $params);
  298. }
  299. Db::commit();
  300. _syslog("订单", "核销成功");
  301. return json_success('核销成功');
  302. } catch (\Exception $e) {
  303. dump($e->getMessage());
  304. Db::rollBack();
  305. _syslog("订单", "核销失败");
  306. return json_fail('核销失败');
  307. }
  308. }
  309. }