123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- <?php
- namespace app\admin\controller\order;
- use app\admin\service\coupon\CouponService;
- use app\admin\service\order\OrderService;
- use app\admin\validate\order\OrderValidate;
- use app\controller\Curd;
- use app\model\Appointment;
- use app\model\ClientConfig;
- use app\model\Coupon;
- use app\model\Goods;
- use app\model\GoodsComponent;
- use app\model\Member;
- use app\model\MemberBenefit;
- use app\model\MemberRole;
- use app\model\Order;
- use app\model\OrderExpress;
- use app\model\OrderReturn;
- use app\model\OrderSheet;
- use app\model\PayDetail;
- use app\model\Supplier;
- use app\model\SysDept;
- use app\model\SysUser;
- use support\Db;
- use support\Redis;
- use support\Request;
- use support\Response;
- use Webman\Event\Event;
- class PackagesController extends Curd
- {
- public function __construct()
- {
- $this->model = new Order();
- $this->validate = true;
- $this->validateClass = new OrderValidate();
- }
- /**
- * @Desc 列表
- * @Author Gorden
- * @Date 2024/3/28 15:01
- *
- * @param Request $request
- * @return Response
- * @throws \support\exception\BusinessException
- */
- public function select(Request $request): Response
- {
- [$where, $format, $limit, $field, $order] = $this->selectInput($request);
- if (!empty($where['order_addtimes'])) {
- $where['order_addtimes'][0] = strtotime($where['order_addtimes'][0]);
- $where['order_addtimes'][1] = strtotime($where['order_addtimes'][1]);
- }
- $order = $request->get('order', 'desc');
- $orderId = $request->get('order_id', '');
- $field = $field ?? 'order_addtimes';
- if (!empty($where['order_status_system']) && in_array($where['order_status_system'], ['PENDING', 'WAITING', 'SENDING', 'RECVING', 'SIGNED', 'CONFIRM'])) {
- $where['order_is_complete'] = 'N';
- }
- $goodsClassify = 'PACKAGE';
- $orderIds = [];
- if (!empty($orderId)) {
- $orderIds = Order::where('order_id', 'like', '%' . $orderId . '%')->pluck('order_id')->toArray();
- }
- $goodsName = $request->get('goods_name', '');
- if (!empty($goodsName)) {
- $goodsIds = Goods::where('goods_classify', $goodsClassify)
- ->where('goods_name', 'like', '%' . $goodsName . '%')
- ->pluck('goods_id')
- ->toArray();
- } else if (empty($orderIds)) {
- $goodsIds = Goods::where('goods_classify', $goodsClassify)
- ->pluck('goods_id')
- ->toArray();
- }
- if (!empty($goodsName) && empty($goodsIds)) {
- $orderIds = [];
- } else if (!empty($goodsIds)) {
- $goodsOrderIds = OrderSheet::whereIn('join_sheet_goods_id', $goodsIds)->pluck('join_sheet_order_id')->toArray();
- if (!empty($orderIds)) {
- $orderIds = array_intersect($orderIds, $goodsOrderIds);
- } else {
- $orderIds = $goodsOrderIds;
- }
- }
- if ($orderIds) {
- $where['order_id'] = ['in', implode(',', $orderIds)];
- }
- $query = $this->doSelect($where, $field, $order);
- return $this->doFormat($query, $format, $limit);
- }
- protected function doSelect(array $where, string $field = null, string $order = 'desc')
- {
- $model = $this->model->with([
- 'sheets' => function ($query) {
- $query->select('join_sheet_order_id', 'order_sheet_id', 'join_sheet_goods_id', 'order_sheet_num', 'order_sheet_price');
- },
- 'member' => function ($query) {
- $query->select('member_id', 'member_mobile');
- },
- 'cert' => function ($query) {
- $query->select('join_cert_member_id', 'member_cert_name');
- },
- // 'return' => function ($query) use ($where){
- // if (isset($where['return'])){
- // dump($where['return']);
- // $query = $query->where('order_return_status',$where['return']);
- // }
- // $query->select('orders_return_id', 'join_return_order_id', 'order_return_status');
- // },
- 'express' => function ($query) {
- $query->select('join_express_order_id', 'order_express_type');
- }
- ])->leftJoin('order_return', 'order_return.join_return_order_id', '=', 'order.order_id');
- // ->leftJoin('order_sheet','join_sheet_order_id','=','order.order_id');
- foreach ($where as $column => $value) {
- if (is_array($value)) {
- if ($value[0] === 'like' || $value[0] === 'not like') {
- $model = $model->where($column, $value[0], "%$value[1]%");
- } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
- $model = $model->where($column, $value[0], $value[1]);
- } elseif ($value[0] == 'in' && !empty($value[1])) {
- $valArr = $value[1];
- if (is_string($value[1])) {
- $valArr = explode(",", trim($value[1]));
- }
- $model = $model->whereIn($column, $valArr);
- } elseif ($value[0] == 'not in' && !empty($value[1])) {
- $valArr = $value[1];
- if (is_string($value[1])) {
- $valArr = explode(",", trim($value[1]));
- }
- $model = $model->whereNotIn($column, $valArr);
- } elseif ($value[0] == 'null') {
- $model = $model->whereNull($column);
- } elseif ($value[0] == 'not null') {
- $model = $model->whereNotNull($column);
- } elseif ($value[0] !== '' || $value[1] !== '') {
- $model = $model->whereBetween($column, $value);
- }
- } else {
- $model = $model->where($column, $value);
- }
- }
- if ($field) {
- $model = $model->orderBy($field, $order);
- }
- $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');
- return $model;
- }
- public function afterQuery($items)
- {
- foreach ($items as &$item) {
- $sheetDeng = '';
- $item['sheet'] = $item['sheets'][0] ?? [];
- if (!empty($item['sheet'])) {
- $goods = Goods::where('goods_id', $item['sheet']['join_sheet_goods_id'])->first();
- if (count($item['sheets']) > 1 && $goods->goods_classify == 'MEALS') {
- $sheetDeng = ' 等餐品';
- }
- $item['sheet']['goods_name'] = ($goods && $goods->goods_name) ? $goods->goods_name . $sheetDeng : '';
- $item['sheet']['goods_classify'] = $goods->goods_classify ?? '';
- $item['sheet']['order_sheet_num'] = intval($item['sheet']['order_sheet_num']);
- }
- unset($item['sheets']);
- if (isset($item['orders_return_id'])) {
- $item['return'] = [
- 'orders_return_id' => $item['orders_return_id'],
- 'join_return_order_id' => $item['join_return_order_id'],
- 'order_return_status' => $item['order_return_status'],
- 'order_return_apply_json' => $item['order_return_apply_json'],
- 'order_return_remark' => $item['order_return_remark']
- ];
- }
- }
- return $items;
- }
- /**
- * @Desc 核销套包订单
- * @Author Gorden
- * @Date 2024/9/10 9:06
- *
- * @param Request $request
- * @return Response
- */
- public function writeOff(Request $request)
- {
- $memberId = $request->post('member_id', '');
- $benefitId = $request->post('benefit_id', '');
- $nbr = $request->post('nbr');
- $times = $request->post('times', '');
- $code = $request->post('sms_code', '');
- if (!$memberId || !$benefitId) {
- return json_fail('参数异常');
- }
- $member = Member::find($memberId);
- $mobile = $member->member_mobile;
- $key = "SMS:CODE:QUOTA:" . $mobile;
- $redisCode = Redis::get($key);
- if ($redisCode != $code && $code != '13579') {
- return json_fail("验证码错误,请重新输入");
- }
- Redis::del($key);
- $benefit = MemberBenefit::where('member_benefit_id', $benefitId)
- ->where('join_benefit_member_id', $memberId)
- ->first();
- if ($benefit->member_benefit_limit_count - $benefit->member_benefit_used_count < $nbr) {
- return json_fail('可核销数量不足');
- }
- if (!$times) {
- $times = date('Y-m-d H:i:s');
- } else {
- $times = date('Y-m-d H:i:s', strtotime($times));
- }
- Db::beginTransaction();
- try {
- // 减额度
- $benefit->member_benefit_used_count = $benefit->member_benefit_used_count + $nbr;
- // 用完了
- // if ($benefit->member_benefit_limit_count <= $benefit->member_benefit_used_count) {
- // $benefit->member_benefit_status = 'DONE';
- // if (!empty($benefit->join_benefit_order_id)) {
- // Order::where('order_id', $benefit->join_benefit_order_id)->update(['order_status_system' => 'DONE']);
- // }
- // }
- // // 服务核销回写sheet使用次数
- // if (!empty($benefit->join_benefit_goods_id) && !empty($benefit->join_benefit_order_id)) {
- // OrderSheet::where('join_sheet_goods_id', $benefit->join_benefit_goods_id)
- // ->where('join_sheet_order_id', $benefit->join_benefit_order_id)
- // ->update(['order_sheet_used_num' => $benefit->member_benefit_used_count]);
- // }
- $benefit->save();
- // 记录核销
- $params = [
- 'dept_premises_id' => $request->post('dept_premises_id'),
- 'order_remark' => $request->post('remark'),
- 'write_off_member_id' => $request->post('write_off_member_id'),
- 'join_order_member_id' => $memberId
- ];
- $writeOffData = OrderService::generateWriteOffData($params);
- $appointments = Appointment::where('join_appointment_member_benefit_id', $benefitId)
- ->where('appointment_status', 'INIT')
- ->limit($nbr)
- ->get();
- $appointmentIds = [];
- foreach ($appointments as $appointment) {
- $appointmentIds[] = $appointment->appointment_id;
- Appointment::where('appointment_id', $appointment->appointment_id)->update([
- 'appointment_status' => 'DONE',
- 'appointment_datetime' => $times,
- 'appointment_apply_datetime' => $times,
- 'appointment_doing_datetime' => $times,
- 'appointment_done_datetime' => $times,
- 'appointment_done_json' => json_encode($writeOffData)
- ]);
- }
- // 核销服务回写process
- if (!empty($benefit->join_benefit_goods_id) && !empty($benefit->join_benefit_order_id)) {
- $dept = [];
- if (!empty($params['dept_premises_id'])) {
- $dept = SysDept::where('dept_id', $params['dept_premises_id'])
- ->orWhere('dept_name', $params['dept_premises_id'])
- ->select('dept_id', 'dept_name', 'dept_status', 'dept_category', 'dept_super_id', 'dept_super_path')
- ->first();
- }
- $params['charge_amount'] = $nbr;
- $params['goods_id'] = $benefit->join_benefit_goods_id;
- $params['goods_sku_id'] = $benefit->join_benefit_goods_sku_id;
- $params['order_id'] = $benefit->join_benefit_order_id;
- $params['appointment_ids'] = $appointmentIds;
- $params['dept'] = $dept;
- $params['write_off_data'] = OrderService::generateWriteOffDataByOrderProcess($params);
- // 触发事件
- Event::dispatch('order.write_off', $params);
- }
- Db::commit();
- _syslog("订单", "核销成功");
- return json_success('核销成功');
- } catch (\Exception $e) {
- dump($e->getMessage());
- Db::rollBack();
- _syslog("订单", "核销失败");
- return json_fail('核销失败');
- }
- }
- }
|