alias('ogs') ->leftJoin('order_info o','o.order_id=ogs.order_id') ->leftJoin('order_goods og','og.id=ogs.order_goods_id') ->leftJoin('goods g','g.goods_id=og.goods_id') ->leftJoin('goods_batch gb','og.batch_id=gb.batch_id') ->leftJoin('manufacturer m','m.id=g.manufacturer_id') ->field($_field) ->where($_where) ->order('ogs.id','desc') ->paginate($search_data['limit'], false, ['page' => $search_data['page']])->each(function($item, $key){ if(!$item['name']){ $extend_data = json_decode($item['extend_data'],true); $item['name'] = $extend_data['name']; $item['spec_desc'] = $extend_data['spec_desc']; $item['retail_flag'] = $extend_data['retail_flag']; $item['manufacturer_name'] = $extend_data['manufacturer_name']; } $item['total_price'] = $item['unit_price'] * $item['goods_number']; $item['retail_flag'] = $item['retail_flag']?'零':'整'; $item['status_text'] = ''; $item['app_thumb'] = $item['app_thumb']?$item['app_thumb']:$item['pc_thumb']; $item['app_thumb'] = 'http://'.$item['app_thumb']; $item['status_text'] = ''; switch ($item['status']) { case 1: $item['status_text'] = '待审核'; break; case 2: case 3: $item['status_text'] = '审核通过'; break; case 4: $item['status_text'] = '审核拒绝'; break; case 5: $item['status_text'] = '客服待收货'; break; case 6: $item['status_text'] = '财务待退款'; break; case 7: $item['status_text'] = '已完成'; break; case 8: $item['status_text'] = '拒绝退款'; break; case 99: $item['status_text'] = '已取消'; break; } $item['create_time'] = date("Y-m-d",$item['create_time']); $item['update_time'] = date("Y-m-d",$item['update_time']); return $item; }); return $service_list; } //售后详情 public function getServiceInfo($parames) { $_field = ''; //order $_field.='o.order_sn,'; //goods $_field.='g.name,g.app_thumb,g.pc_thumb,g.spec_desc,'; //order_goods $_field.='og.extend_data,og.goods_number,og.unit_price,'; //goods_batch $_field.='gb.retail_flag,'; //manufacturer $_field.='m.name as manufacturer_name,'; //order_goods_service $_field.='ogs.*'; $service_info = Db::name('order_goods_service')->alias('ogs') ->leftJoin('order_info o','o.order_id=ogs.order_id') ->leftJoin('order_goods og','og.id=ogs.order_goods_id') ->leftJoin('goods g','g.goods_id=og.goods_id') ->leftJoin('goods_batch gb','og.batch_id=gb.batch_id') ->leftJoin('manufacturer m','m.id=g.manufacturer_id') ->field($_field) ->where('ogs.id',$parames['id']) ->find(); $service_info['status_text'] = ''; if(!$service_info['name']){ $extend_data = json_decode($service_info['extend_data'],true); $service_info['name'] = $extend_data['name']; $service_info['spec_desc'] = $extend_data['spec_desc']; $service_info['retail_flag'] = $extend_data['retail_flag']; $service_info['manufacturer_name'] = $extend_data['manufacturer_name']; } $service_info['total_price'] = $service_info['unit_price'] * $service_info['goods_number']; $service_info['retail_flag'] = $service_info['retail_flag']?'零':'整'; $service_info['status_text'] = ''; $service_info['app_thumb'] = $service_info['app_thumb']?$service_info['app_thumb']:$service_info['pc_thumb']; $service_info['app_thumb'] = 'http://'.$service_info['app_thumb']; $service_info['status_text'] = ''; switch ($service_info['status']) { case 1: $service_info['status_text'] = '待审核'; break; case 2: case 3: $service_info['status_text'] = '审核通过'; break; case 4: $service_info['status_text'] = '审核拒绝'; break; case 5: $service_info['status_text'] = '客服待收货'; break; case 6: $service_info['status_text'] = '财务待退款'; break; case 7: $service_info['status_text'] = '已完成'; break; case 8: $service_info['status_text'] = '拒绝退款'; break; case 99: $service_info['status_text'] = '已取消'; break; } $service_info['type_text'] = ''; switch ($service_info['type']) { case 1: $service_info['type_text'] = '仅退款'; break; case 2: $service_info['type_text'] = '退货退款'; break; } $service_info['reason_text'] = ''; switch ($service_info['reason']) { case 1: $service_info['reason_text'] = '质量问题'; break; case 2: $service_info['reason_text'] = '发货错'; break; case 3: $service_info['reason_text'] = '未按照约定时间发货'; break; case 3: $service_info['reason_text'] = '其他'; break; } $service_info['create_time'] = date("Y-m-d",$service_info['create_time']); $service_info['update_time'] = date("Y-m-d",$service_info['update_time']); return $service_info; } //申请售后 public function addService($parames) { if(!isset($parames['user_id']) || !$parames['user_id']){ throw new Exception('用户不存在'); } if(!isset($parames['order_id']) || !$parames['order_goods_id']){ throw new Exception('订单不存在'); } if(!isset($parames['order_goods_id']) || !$parames['order_goods_id']){ throw new Exception('订单商品不存在'); } if(!isset($parames['type']) || !$parames['type']){ throw new Exception('类型不能为空'); } if(!isset($parames['reason']) || !$parames['reason']){ throw new Exception('原因不能为空'); } if(!isset($parames['refund_price']) || !$parames['refund_price']){ throw new Exception('退款金额不能为空'); } if(!isset($parames['refund_content']) || !$parames['refund_content']){ throw new Exception('退款说明不能为空'); } try{ Db::startTrans(); $parames['status'] = 1; $parames['create_time'] = time(); $parames['update_time'] = time(); $service_id = Db::name('order_goods_service')->insertGetId($parames); $order_goods = Db::name('order_goods')->where('id',$parames['order_goods_id'])->update(['service_id'=>$service_id]); Db::commit(); return $this->getServiceInfo($parames); }catch (Exception $exception){ Db::rollback(); throw new Exception($exception->getMessage()); } } //审核售后 public function auditService($id,$status,$admin_user_id) { try{ Db::startTrans(); $service = Db::name('order_goods_service')->find($id); if($service){ $update['id'] = $id; $update['status'] = $status; $update['admin_user_id'] = $admin_user_id; $update['update_time'] = time(); $count = Db::name('order_goods_service')->update($update); if($count){ if($status == 7){ //todo 支付宝和微信现金退款操作 //todo 扣除订单提成总额 $order = Db::name('order_info')->where('order_id',$service['order_id'])->find(); $order_goods = Db::name('order_goods')->where('id',$service['order_goods_id'])->find(); $order['cs_percentage'] = $order['cs_percentage'] - $order_goods['cs_proportion_price']; Db::name('order_info')->where('order_id',$service['order_id'])->update(['cs_percentage'=>$order['cs_percentage']]); } $admin_log['type'] = 'update'; $admin_log['mark_type'] = 'process_order'; $admin_log['mark_key'] = $service['order_id']; switch ($status) { case 2: $admin_log['content'] = "
审核通过订单售后需退货服务单, 订单ID为" . $service['order_id'] . "。服务单ID为".$id."。
"; break; case 3: $admin_log['content'] = "审核通过订单售后直接退款服务单, 订单ID为" . $service['order_id'] . "。服务单ID为".$id."。
"; break; case 4: $admin_log['content'] = "审核拒绝订单售后服务单, 订单ID为" . $service['order_id'] . "。服务单ID为".$id."。
"; break; case 6: $admin_log['content'] = "客服收到售后退货商品, 订单ID为" . $service['order_id'] . "。服务单ID为".$id."。
"; break; case 7: $admin_log['content'] = "财务已退款订单售后服务单, 订单ID为" . $service['order_id'] . "。服务单ID为".$id."。
"; break; case 8: $admin_log['content'] = "财务拒绝退款订单售后服务单, 订单ID为" . $service['order_id'] . "。服务单ID为".$id."。
"; break; } AdminLogModel::create($admin_log); Db::commit(); return true; } } Db::rollback(); throw new Exception('售后单不存在'); }catch (Exception $exception){ Db::rollback(); throw new Exception($exception->getMessage()); } } //提交退货物流 public function submitService($parames) { if(!isset($parames['user_id']) || !$parames['user_id']){ throw new Exception('用户不存在'); } if(!isset($parames['id']) || !$parames['id']){ throw new Exception('服务不存在'); } if(!isset($parames['express']) || !$parames['express']){ throw new Exception('物流公司不能为空'); } if(!isset($parames['express_sn']) || !$parames['express_sn']){ throw new Exception('物流单号不能为空'); } $parames['status'] = 5; $count = Db::name('order_goods_service')->update($parames); if($count){ return $this->getServiceInfo($parames); } throw new Exception('操作失败'); } //取消售后 public function cancelService($parames) { if(!isset($parames['user_id']) || !$parames['user_id']){ throw new Exception('用户不存在'); } if(!isset($parames['id']) || !$parames['id']){ throw new Exception('服务不存在'); } $parames['status'] = 99; $count = Db::name('order_goods_service')->update($parames); if($count){ return $this->getServiceInfo($parames); } throw new Exception('操作失败'); } //ajax获取订单列单 public function getOrderList($parames){ if(!isset($parames['user_id']) || !$parames['user_id']){ throw new Exception('用户不存在'); } $_where[] = ['user_id','=',$parames['user_id']]; $where_string = ''; $now_time = date("Y-m-d H:i:s"); $agent = $parames['agent']??2; $_order = ["order_id" => 'desc']; $order_status = $parames['order_status']??''; switch ($order_status){ case 1://待付款 $where_string = "(pay_status = 0 AND status = 0) or (pay_status = 0 AND status = 6)"; break; case 2://待发货 case 5://待确认 $where_string = "(status = 6 AND ((pay_type in (2,3,4,5) AND express_status = 0 AND pay_status = 1) or (pay_type = 1 AND express_status = 0) or (pay_type = 2 AND express_status = 0 AND pay_status = 1)))"; break; // case 5://待确认 // $_where[] = ['status','=',0]; // $_where[] = ['deadline_time','>',$now_time]; case 3://已完成 $_where[] = ['status','in',[1,7]]; break; case 4://已取消 $where_string = "status in (2,3,4) or (status = 0 and deadline_time < '".$now_time."')"; break; case 6://已发货 $_where[] = ['express_status','=',1]; $_where[] = ['status','not in',[1,7]]; break; } $search_data['page'] = 1; $search_data['limit'] = 8; if(isset($parames['page']) && $parames['page']){ $search_data['page'] = $parames['page']; } if(isset($parames['limit']) && $parames['limit']){ $search_data['limit'] = $parames['limit']; } $order_list = []; $_field = "order_id,order_sn,status,pay_type,pay_status,express_status,express_name,express_sn,shipping_fee,pay_voucher,goods_amount,total_amount,create_time,deadline_time,order_contract_url"; $order_list = Db::name("order_info")->field($_field)->where($_where)->where($where_string)->order($_order)->paginate($search_data['limit'], false, ['page' => $search_data['page']]); $order_id_group = []; foreach ($order_list as $order) { $order_id_group[] = $order['order_id']; } unset($_where); $_where[] = ['og.order_id','in',$order_id_group]; $order_goods = Db::name("order_goods")->alias("og") ->leftJoin('goods g','g.goods_id = og.goods_id') ->leftJoin('manufacturer m','m.id=g.manufacturer_id') ->where($_where)->field("og.id,og.order_id,og.batch_id,og.type,og.extend_data,og.goods_number,og.unit_price,g.name,g.unit,g.spec_desc,g.pc_thumb,g.app_thumb,m.name as manufacturer_name")->select(); $goods_list = []; foreach ($order_goods as $item){ if (!empty($item['type']) && $item['type'] == 'extend'){ $json_data = json_decode($item['extend_data'],true); $item['name'] = $json_data['name']; $item['unit'] = $json_data['unit']; $item['spec_desc'] = $json_data['spec_desc']; $item['app_thumb'] = $json_data['gallery'][0]??''; $item['manufacturer_name'] = $json_data['manufacturer_name']; }else{ $item['app_thumb'] = $item['app_thumb']??$item['pc_thumb']; } if(!$item['app_thumb']){ $item['app_thumb'] = $item['pc_thumb']; } if(strpos($item['app_thumb'],'null') !== false && $item['app_thumb']){ $item['app_thumb'] = ''; } if(strpos($item['app_thumb'],'http') === false && $item['app_thumb']){ $item['app_thumb'] = 'https://'.$item['app_thumb']; } $goods_list[$item['order_id']]['list'][] = $item; if(!isset($goods_list[$item['order_id']]['num'])){ $goods_list[$item['order_id']]['num'] = 0; } $goods_list[$item['order_id']]['num'] += $item['goods_number']; } $order_arr = []; foreach ($order_list as $key => $item){ if($item['pay_voucher'] && $item['pay_voucher'] != '[""]' && $item['pay_voucher'] != 'null'){ $item['pay_voucher'] = implode(',',json_decode($item['pay_voucher'])); }else{ $item['pay_voucher'] = ''; } $item['need_cs_confirm'] = 0; if ($item['status'] == 0 && $item['deadline_time'] > date("Y-m-d H:i:s")){ $item['need_cs_confirm'] = 1; } $item['cancel_flag'] = 0; if(($item['status'] == 6 && $item['pay_status'] == 0 && $item['express_status'] == 0) || ($item['status'] == 0 && $item['deadline_time'] > date("Y-m-d H:i:s"))){ $item['cancel_flag'] = 1; } $item['need_pay'] = 0; if($item['pay_status'] == 0 && ($item['status'] == 6 || ($item['status'] == 0 && $item['deadline_time'] > date("Y-m-d H:i:s")))){ $item['need_pay'] = 1; } $item['agent'] = $agent; $item['order_status'] = get_order_icon_status($item,$now_time); $item['order_status_str'] = get_order_status_text($item['order_status']); $item['goods_list'] = $goods_list[$item['order_id']]['list']??[]; $item['total_num'] = $goods_list[$item['order_id']]['num']??0; $order_list[$key] = $item; } return $order_list; } //ajax获取订单列单 public function getOrderCount($parames){ if(!isset($parames['user_id']) || !$parames['user_id']){ throw new Exception('用户不存在'); } $_where[] = ['user_id','=',$parames['user_id']]; $where_string = ''; $now_time = date("Y-m-d H:i:s"); $agent = $parames['agent']??2; $_order = ["order_id" => 'desc']; $order_status = $parames['order_status']??''; switch ($order_status){ case 1://待付款 $where_string = "(pay_status = 0 AND status = 0) or (pay_status = 0 AND status = 6)"; break; case 2://待发货 case 5://待确认 $where_string = "(status = 6 AND ((pay_type in (2,3,4,5) AND express_status = 0 AND pay_status = 1) or (pay_type = 1 AND express_status = 0) or (pay_type = 2 AND express_status = 0 AND pay_status = 1)))"; break; // case 5://待确认 // $_where[] = ['status','=',0]; // $_where[] = ['deadline_time','>',$now_time]; case 3://已完成 $_where[] = ['status','in',[1,7]]; break; case 4://已取消 $where_string = "status in (2,3,4) or (status = 0 and deadline_time < '".$now_time."')"; break; case 6://已发货 $_where[] = ['express_status','=',1]; $_where[] = ['status','not in',[1,7]]; break; } $_field = "order_id"; $count = Db::name("order_info")->field($_field)->where($_where)->where($where_string)->order($_order)->count(); return $count; } //获取订单详情 public function orderDetail($parames){ if(!isset($parames['user_id']) || !$parames['user_id']){ throw new Exception('用户不存在'); } if(!isset($parames['order_id']) || !$parames['order_id']){ throw new Exception('订单不存在'); } $order_id = $parames['order_id']??''; $agent = $parames['agent']??2; $_where[] = ['user_id',"=",$parames['user_id']]; $_where[] = ['order_id',"=",$parames['order_id']]; $_field = "order_id,order_sn,status,pay_type,pay_status,express_status,express_name,shipping_fee,goods_amount,total_amount,address_id,express_name,express_sn,create_time,shipping_time,pay_time,finished_time,deadline_time,remark"; $order_detail = Db::name("order_info")->field($_field)->where($_where)->find(); if (empty($order_detail)){ throw new Exception('订单不存在'); } $now_time = time(); $order_detail['count_down'] = 0; if ($order_detail['status'] == 0 && $order_detail['pay_type'] == 2 && ($order_detail['deadline_time'] > date("Y-m-d H:i:s"))){ $order_detail['count_down'] = 1; $left_time = strtotime($order_detail['deadline_time']) - $now_time; $order_detail['left_time'] = get_left_time($left_time); } $order_detail['agent'] = $agent; $order_detail['order_status'] = get_order_icon_status($order_detail,$now_time); $order_detail['order_status_str'] = get_order_status_text($order_detail['order_status']); $order_detail['can_pay'] = 0; if($order_detail['pay_status'] == 0 && (($order_detail['status'] == 0 && $order_detail['deadline_time'] > date("Y-m-d H:i:s")) || ($order_detail['status'] == 6))){ $order_detail['can_pay'] = 1; } $order_detail['need_cs_confirm'] = 0; if ($order_detail['status'] == 0 && $order_detail['deadline_time'] > date("Y-m-d H:i:s")){ $order_detail['need_cs_confirm'] = 1; } $order_detail['cancel_flag'] = 0; if(($order_detail['status'] == 6 && $order_detail['pay_status'] == 0 && $order_detail['express_status'] == 0) || ($order_detail['status'] == 0 && $order_detail['deadline_time'] > date("Y-m-d H:i:s"))){ $order_detail['cancel_flag'] = 1; } switch ($order_detail['pay_type']) { case 1: $order_detail['pay_type_text'] = '货到付款'; break; case 2: $order_detail['pay_type_text'] = '线下支付'; break; case 3: $order_detail['pay_type_text'] = '小程序支付'; break; case 4: $order_detail['pay_type_text'] = '微信支付'; break; case 5: $order_detail['pay_type_text'] = '支付宝支付'; break; } //订单商品 $_field = 'og.goods_id,og.batch_id,og.unit_price,og.spread_unit_price,og.spread_flag,og.type,og.goods_number,og.is_delete,g.name,g.spec_desc,g.pc_thumb,g.app_thumb,gb.batch_number,m.name as manufacturer_name,gb.term_validity,gb.retail_flag'; $_order_where[] = ["og.order_id","=",$order_id]; $_order_where[] = ["og.type","=",""]; $order_goods = Db::name("order_goods")->alias("og")->field($_field)->join('goods_batch gb','og.batch_id = gb.batch_id') ->join('goods g','g.goods_id = gb.goods_id')->join('manufacturer m','m.id = g.manufacturer_id')->where($_order_where)->select(); //已无效商品初始化 $invalid_order_goods = []; foreach ($order_goods as $k => $item){ if ($item['spread_flag']){ $order_goods[$k]['total'] = sprintf("%.2f",$item['goods_number'] * $item['spread_unit_price']); }else{ $order_goods[$k]['total'] = sprintf("%.2f",$item['goods_number'] * $item['unit_price']); } if ($item['is_delete']){ $invalid_order_goods[] = $order_goods[$k]; unset($order_goods[$k]); } $order_goods[$k]['app_thumb'] = $order_goods[$k]['app_thumb']?:$order_goods[$k]['pc_thumb']; if(strpos($order_goods[$k]['app_thumb'],'null') !== false && $order_goods[$k]['app_thumb']){ $order_goods[$k]['app_thumb'] = ''; } if(strpos($order_goods[$k]['app_thumb'],'http') === false && $order_goods[$k]['app_thumb']){ $order_goods[$k]['app_thumb'] = 'https://'.$order_goods[$k]['app_thumb']; } } /****************************/ $extend_where[] = ["order_id","=",$order_id]; $extend_where[] = ["type","=","extend"]; $extend_goods_list = Db::name("order_goods")->field("id,goods_id,batch_id,unit_price,spread_unit_price,spread_flag,goods_number,extend_data,is_delete")->where($extend_where)->select(); //已无效拓展商品初始化 $invalid_extend_order_goods = []; foreach ($extend_goods_list as $k=>$item){ $json_data = json_decode($item['extend_data'],true); $extend_goods_list[$k]['name'] = $json_data['name']; $extend_goods_list[$k]['type'] = 'extend'; $extend_goods_list[$k]['retail_flag'] = $json_data['retail_flag']; $extend_goods_list[$k]['unit'] = $json_data['unit']; $extend_goods_list[$k]['batch_number'] = $json_data['batch_number']; $extend_goods_list[$k]['manufacturer_name'] = $json_data['manufacturer_name']; $extend_goods_list[$k]['spec_desc'] = $json_data['spec_desc']; $extend_goods_list[$k]['total'] = sprintf("%.2f",$item['unit_price'] * $item['goods_number']); $extend_goods_list[$k]['app_thumb'] = $json_data['gallery'][0]??''; $extend_goods_list[$k]['term_validity'] = $json_data['term_validity']; if ($item['is_delete']){ $invalid_extend_order_goods[] = $extend_goods_list[$k]; unset($extend_goods_list[$k]); } } $order_goods = array_values(array_merge($order_goods, $extend_goods_list)); $invalid_order_goods = array_values(array_merge($invalid_order_goods, $invalid_extend_order_goods)); /*******************************/ //收货地址 $user_address = Db::name("user_address")->field("area_id,address,consignee,tel")->where('id',$order_detail['address_id'])->find(); $area_list = AppConstant::getAreaList(); $area_id = $user_address['area_id']; $area_name = ''; if($area_id){ if (isset($area_list[$area_id]['grand_pa_id'])){ $grand_pa_id = $area_list[$area_id]['grand_pa_id']; $area_name .= $area_list[$grand_pa_id]['area_name']; } if (isset($area_list[$area_id]['parent_area_id'])){ $parent_area_id = $area_list[$area_id]['parent_area_id']; $area_name .= $area_list[$parent_area_id]['area_name']; } $area_name .= $area_list[$area_id]['area_name']; $area_name .= $user_address['address']; } $user_address['address'] = $area_name; // 买家信息 $buyer_info = Db::name("buyer")->field("organization_code,invoice_title")->where('buyer_user_id',$parames['user_id'])->find(); $res_return['invalid_order_goods'] = $invalid_order_goods; $res_return['buyer_info'] = $buyer_info; $res_return['user_address'] = $user_address; $res_return['order_goods'] = $order_goods; $res_return['order_detail'] = $order_detail; $res_return['cancel_reason'] = config('SYSTEM_CONSTANT')['PRE_SET_CANCEL_ORDER_REASON']; return $res_return; } //提交处理 public function submitOrder($parames) { if(!isset($parames['user_id']) || !$parames['user_id']){ throw new Exception('用户不存在'); } if (!isset($parames['id_group']) || !$parames['id_group']){ throw new Exception('参数错误'); } $id_group = explode(",", $parames['id_group']); //关于用户资质到期--初始化处理,此处不处理 //收货信息:地址、电话、联系人 //1.客户选择前文系统返回的收货信息 2.客户新增收货信息 $address = $parames['address']??''; if (!$address){ throw new Exception('收货地址未设置'); } //收货地址-逻辑对应前文预购单页面返回唯一地址(默认收货地址唯一 or 默认收货地址),@todo 多收货地址后期处理 $area_list = AppConstant::getAreaList(); $user_address = Db::name("user_address")->field("id,area_id,address,consignee,tel")->where("user_id",$parames['user_id'])->find(); if(!$user_address){ throw new Exception('系统异常,请稍后重试!'); } $area_id = $user_address['area_id']; $area_name = ''; if (isset($area_list[$area_id]['grand_pa_id'])){ $grand_pa_id = $area_list[$area_id]['grand_pa_id']; $area_name .= $area_list[$grand_pa_id]['area_name']; } if (isset($area_list[$area_id]['parent_area_id'])){ $parent_area_id = $area_list[$area_id]['parent_area_id']; $area_name .= $area_list[$parent_area_id]['area_name']; } $area_name .= $area_list[$area_id]['area_name'].$user_address['address']; //购物车表是否存在该用户当前提交的数据;联结价格表获取价格以及提成设置 $where = []; $where[] = ['uc.id','in',$id_group]; $where[] = ['uc.user_id','=',$parames['user_id']]; $cart_check = Db::name('user_cart')->alias('uc')->where($where)->leftJoin('goods_batch_area_relation gba','gba.id=uc.price_id') ->join('goods_batch gb','gba.batch_id=gb.batch_id') ->column('uc.id,uc.goods_number,uc.unit_price,uc.batch_id,uc.type,uc.extend_data,gba.price,gba.platform_royalty,gb.goods_id,uc.price_id','uc.id'); if (count($cart_check)!=count($id_group)){ throw new Exception('商品状态已变化,请检查购物车再提交!'); } //数量、金额校验 $total_amount = 0; $total_number = 0; $plat_product_data = []; $extend_product_data = [];//下文流通商品校验预设数据 $cs_percentage = 0.00;//提成金额预设--order_info foreach ($cart_check as $uid=>$item){ $total_amount += sprintf("%.2f",$item['unit_price']*$item['goods_number']); $total_number += $item['goods_number']; if ($item['type']=='extend'){ $tmp1 = json_decode($item['extend_data'],true); $tmp2 = $item; $tmp2['extend_data'] = $tmp1; $extend_product_data[] = $tmp2; $cs_percentage+= sprintf("%.2f",$item['unit_price']*$item['goods_number']*0.005); }else{ $cs_percentage+= sprintf("%.4f",$item['unit_price']*$item['goods_number']*$item['platform_royalty']); $item['name'] = ''; $item['spec_desc'] = ''; $item['manufacturer_name'] = ''; $plat_product_data[] = $item; } } //商品详情校验:优势+流通 //优势 $fields = 'uc.id,uc.price_type,uc.price_id,uc.goods_number,uc.unit_price,uc.type,uc.extend_data,g.name as goods_name,g.country_code,g.drug_form,g.unit,g.spec_desc,m.name as manufacturer_name,'; $fields .= 'gb.batch_id,gb.goods_id,gb.supplier_id,gb.retail_flag,gb.batch_number,gb.retail_num,gb.entirety_num,gb.term_validity,gb.stock_num,gb.sale_num,gb.total_num,'; $fields .= 'gba.price,gba.platform_royalty,gba.new_price,gba.discount_price,gba.discount_num,gba.discount_start_time,gba.discount_end_time'; $where = []; $where[] = ['uc.id','in',$id_group]; $where[] = ['gba.is_onshelf','=',1]; $where[] = ['gba.is_delete','=',0]; $where[] = ['gb.batch_status','=',1]; $where[] = ['g.status','=',1]; $where[] = ['g.is_online_sale','=',0]; $where[] = ['uc.type','=',''];//限定检测优势商品 $user_cart_data = Db::name('user_cart')->alias('uc') ->join('goods_batch_area_relation gba','uc.price_id=gba.id') ->join('goods_batch gb','gb.batch_id=uc.batch_id') ->join('goods g','g.goods_id=gb.goods_id') ->join('manufacturer m','m.id=g.manufacturer_id') ->where($where)->column($fields,'uc.id'); $cart_check_status = true; $cart_check_msg = ''; //数量 if (count($user_cart_data)!=count($plat_product_data)){ $cart_check_status = false; $cart_check_msg = '商品数量不一致!'; } //价格+库存; add:活动商品减去活动商品数量(仅优势商品) if ($cart_check_status){ $goods_batch_stock_update = []; $price_change_up = false;//更新价格 foreach ($user_cart_data as $uid=>$value){ //价格更新:优势+流通 $price_query = model(GoodsService::class)->getGoodsDetail($parames['user_info'],$value['price_id']); if ($price_query['code']!=200){ throw new Exception($price_query['message'],$price_query['code']); }else{ $price_compare = $price_query['data']['price']??0; if ((!$price_compare) || (!($price_compare*10000))){ throw new Exception('部分商品状态已变化,请查看后重新下单!',-1015); } if ($price_compare!=$value['unit_price']){ //先更新价格,循环结束后再抛异常 $_where_tmp = []; $_where_tmp[] = ['id','=',$value['id']]; $_where_tmp[] = ['user_id','=',$parames['user_id']]; $up_data_tmp = []; $up_data_tmp['unit_price'] = $price_compare; $up_data_tmp['update_time'] = date('Y-m-d H:i:s',time()); $up_res_tmp = Db::name('user_cart')->where($_where_tmp)->update($up_data_tmp); if ($up_res_tmp){ $price_change_up = true; } } } if ((!($value['unit_price']*10000)) || ($value['goods_number']>$value['stock_num'])){ $cart_check_status = false; $cart_check_msg .= $value['goods_name'].':价格/库存异常!'; continue; } //减库存 $tmp_data = []; $tmp_data['batch_id'] = $value['batch_id']; $tmp_data['stock_num'] = $value['stock_num']-$value['goods_number']; $tmp_data['sale_num'] = $value['sale_num']+$value['goods_number']; $tmp_data['total_num'] = $value['total_num']+$value['goods_number']; $tmp_data['update_time'] = date('Y-m-d H:i:s'); $tmp_data['update_ip'] = \request()->ip(); $goods_batch_stock_update[] = $tmp_data; if ($value['supplier_id']!=10006 && $value['price_type']==0){ $ac_where = []; $ac_where[] = ['price_id','=',$value['price_id']]; $ac_where[] = ['goods_id','=',$value['goods_id']]; $is_activity_goods = Db::name('activity_goods_relation')->where($ac_where)->find(); if($is_activity_goods && $value['discount_num']){ //减活动商品数量 $ac_goods_num_now = $value['discount_num']; if ($ac_goods_num_now){ if ($ac_goods_num_now-$value['goods_number'] >= 0){ $price_up_tmp = []; $price_up_tmp['discount_num'] = $ac_goods_num_now-$value['goods_number']; Db::name('goods_batch_area_relation')->where('id',$value['price_id'])->update($price_up_tmp); } } } } } if ($price_change_up){ throw new Exception('部分商品在售价格已变化,请到购物车或商品页查看后重新下单!',-1019); } if ($goods_batch_stock_update){ $goodsBatchObj = new GoodsBatchModel(); $goodsBatchObj->isUpdate(true)->saveAll($goods_batch_stock_update);//异常自动抛出 } } if (!$cart_check_status){ throw new Exception($cart_check_msg); } //事务,写入order_info+order_goods;其他buyer采购历史总量+goods商品销售累积在收到货款以后实现 $order_sn = generate_order_sn($parames['user_id']);//生成单号 Db::startTrans(); try{ //order_info;未处理字段如pay_status、express_xx(快递字段)数据表默认值实现 //订单合同生成业务其他位置处理 $shipping_fee_amount = 0;//运费暂时单独客服跟进沟通方式处理,@todo 后期规划运费计算模型 $shipping_fee_amount = getShippingFee($total_amount,$total_number); $valid_duration = config('SYSTEM_CONSTANT')['ORDER_VALID_DURATION'];//截止期 //@todo 收货地址及收件人写入订单信息 $orderInfoData = []; $orderInfoData['order_sn'] = $order_sn; $orderInfoData['user_id'] = $parames['user_id']; $orderInfoData['address_id'] = $parames['address_id']??0; $orderInfoData['shipping_fee'] = $shipping_fee_amount; $orderInfoData['goods_amount'] = $total_amount; $orderInfoData['total_amount'] = $total_amount+$shipping_fee_amount; $orderInfoData['remark'] = $parames['remark']; $orderInfoData['pay_type'] = 0; // $custom_service_id = $data['custom_service_id']??0; // $orderInfoData['custom_service_id'] = $custom_service_id ; $orderInfoData['cs_percentage'] = number_format(ceil($cs_percentage*100)/100,2,'.','')??'0.00';//完善提成金额 $orderInfoData['create_time'] = date('Y-m-d H:i:s'); $orderInfoData['deadline_time'] = date('Y-m-d H:i:s', (time() + (20 * 86400))); //$order_info_res = OrderInfoModel::create($orderInfoData); $order_info_res =Db::name('order_info')->insertGetId($orderInfoData); //此处模型操作没有自动抛出异常,手动抛出 if (!$order_info_res){ throw new \think\Exception('fail to insert into table oi',-114); } //order_goods $order_id = $order_info_res; $order_goods_insert = []; $order_goods_data = array_merge($plat_product_data,$extend_product_data); foreach ($order_goods_data as $k=>$v){ $tmp = []; $tmp['order_id'] = $order_id; $tmp['goods_id'] = $v['goods_id']??$v['extend_data']['extend_goods_id']; $tmp['batch_id'] = $v['batch_id']; $tmp['price_id'] = $v['price_id']; //$tmp['batch_type'] = '';//直销、控销、分销 $tmp['unit_price'] = $v['unit_price']; $tmp['spread_unit_price'] = $v['unit_price'];//业务员推广价--暂不请用设置为销售价 $tmp['spread_flag'] = 0;//暂不启用业务员推广价/代理价 $tmp['goods_number'] = $v['goods_number']; $tmp['first_buy'] = 0;//@todo 首营检测,默认0-否 //配合数据库修改 $tmp['cs_proportion'] = sprintf("%.2f",($v['platform_royalty']??0.0050)); $tmp['cs_proportion_price'] = sprintf("%.2f",($v['platform_royalty']??0.0050)*$v['unit_price']*$v['goods_number']); $tmp['create_time'] = date('Y-m-d H:i:s'); $tmp['type'] = $v['type']; $tmp['extend_data'] = $v['extend_data']; if ($v['extend_data']){ // $v['extend_data'] = json_decode($v['extend_data'],true); $tmp['name'] = $v['extend_data']['name']; $tmp['spec_desc'] = $v['extend_data']['spec_desc']; $tmp['manufacturer_name'] = $v['extend_data']['manufacturer_name']; }else{ $tmp['name'] = ''; $tmp['spec_desc'] = ''; $tmp['manufacturer_name'] = ''; } $order_goods_insert[] = $tmp; } if ($order_goods_insert){ try{ $og_insert = Db::name('order_goods')->json(['extend_data'])->insertAll($order_goods_insert); }catch (Exception $e){ throw new Exception($e->getMessage()); } } //购物车更新-删除已下单商品 $uc_where = []; $uc_where[] = ['user_id','=',$parames['user_id']]; $uc_where[] = ['id','in',$id_group]; $uc_result = Db::name('user_cart')->where($uc_where)->delete(); if ($uc_result!=count($id_group)){ throw new \think\Exception('error in uc',-189); } //更新购物车缓存数据,处理优势商品即可,流通商品实时更新 UserCart::instance($parames['user_id'])->reCreateCartCache(true); Db::commit(); $res['order_id'] = $order_info_res; return $res; }catch (\think\Exception $e){ Db::rollback(); throw new \think\Exception($e->getMessage()); } } //预览订单 public function preOrder($parames){ if(!isset($parames['user_id']) || !$parames['user_id']){ throw new Exception('用户不存在'); } if (!isset($parames['id_group']) || !$parames['id_group']){ throw new Exception('参数不合法'); } $auth = model(UserService::class)->checkAuth($parames); if($auth != 'finish'){ throw new Exception('请先进行企业认证'); } $cart_id_group = explode(',',$parames['id_group']); $product_plat = [];//优势 $product_extend = [];//流通 //用户状态检测初始化已处理,此处无需处理 $fields = 'id,user_id,batch_id,price_id,unit_price,goods_number,type,extend_data'; $user_id = $parames['user_id']; $where = []; $where[] = ['user_id','=',$user_id]; $where[] = ['id','in',$cart_id_group]; //@todo 改为从缓存获取(购物车数据维护类实现缓存) $pre_data = Db::name('user_cart')->alias('uc')->field($fields)->where($where)->column($fields,'id'); if (!$pre_data){ throw new Exception('请检查购物车!'); } foreach ($pre_data as $id=>$v){ if ($v['type']=='extend'){ $product_extend[] = $v; $product_extend['batch_id'][] = $v['batch_id']; }else{ $product_plat[] = $v; $product_plat['price_id'][] = $v['price_id']; } } $errorIds = []; //优势商品,返回错误,小程序页面主动刷新,并更新到最新状态 if (isset($product_plat['price_id'])&&$product_plat['price_id']){ $_where = []; $_where[] = ['gba.id','in',$product_plat['price_id']]; $_where[] = ['gba.is_onshelf','=',1]; $_where[] = ['gba.is_delete','=',0]; $_where[] = ['gb.batch_status','=',1]; $_where[] = ['g.status','=',1]; $_where[] = ['g.is_online_sale','=',0]; $fields = 'gba.price,gba.is_onshelf,batch_number,gb.batch_status,gb.batch_id,g.status,g.name'; $plat_product_now = Db::name('goods_batch_area_relation')->alias('gba') ->join('goods_batch gb','gb.batch_id=gba.batch_id') ->join('goods g','g.goods_id=gb.goods_id') ->where($_where)->column($fields,'gba.id'); if (count($plat_product_now)!=count($product_plat['price_id'])){ $priceArr = array_column($plat_product_now, null,'price_id'); foreach ($product_plat['price_id'] as $k=>$v) { if(!isset($priceArr[$v])){ $batch_id = Db::name('goods_batch_area_relation')->alias('gba') ->join('goods_batch gb','gb.batch_id=gba.batch_id') ->where('gba.id',$v)->value('gb.batch_id'); $errorIds[] = $batch_id; } } }else{ foreach ($plat_product_now as $pid=>$item){ foreach ($product_plat as $kk=>$vv){ //@todo 此处较乱,后期更改;数量上述if判断已经校验 if ($kk!='price_id' && $vv['price_id']==$pid){ if ($vv['unit_price']!=$item['price']){ $errorIds[] = $item['batch_id']; } } } } } } //预提交订单页面所需数据 $res = []; //收货地址 $area_list = AppConstant::getAreaList(); $user_address = Db::name("user_address")->field("id,area_id,address,consignee,tel")->where("user_id",$parames['user_id'])->find(); if ($user_address){ $area_id = $user_address['area_id']; $area_name = ''; if (isset($area_list[$area_id]['grand_pa_id'])){ $grand_pa_id = $area_list[$area_id]['grand_pa_id']; $area_name .= $area_list[$grand_pa_id]['area_name']; } if (isset($area_list[$area_id]['parent_area_id'])){ $parent_area_id = $area_list[$area_id]['parent_area_id']; $area_name .= $area_list[$parent_area_id]['area_name']; } $area_name .= $area_list[$area_id]['area_name']; $user_address['address'] = $area_name.$user_address['address']; } $res['address'] = $user_address;//多个收货地址前端展示select //商品信息(名称、数量、单价、合计金额) $res['goods_list'] = []; if ($product_plat){ $plat_where = []; $plat_where[] = ['uc.price_id','in',$product_plat['price_id']]; $plat_where[] = ['uc.user_id','=',$parames['user_id']]; $fields_plat = 'uc.id,uc.goods_number,uc.unit_price,g.name as goods_name,g.country_code,g.drug_form,g.unit,g.spec_desc,m.name as manufacturer_name,g.pc_thumb,g.app_thumb,'; $fields_plat .= 'gb.batch_id,gb.retail_flag,gb.batch_number,gb.retail_num,gb.entirety_num,gb.term_validity,gb.stock_num'; $plat_product_info = Db::name('user_cart')->alias('uc')->field($fields_plat) ->join('goods_batch gb','gb.batch_id=uc.batch_id') ->join('goods g','g.goods_id=gb.goods_id') ->join('manufacturer m','m.id=g.manufacturer_id')->where($plat_where)->select(); $res['goods_list']['plat'] = $plat_product_info; } $res['goods_list']['extend']=[]; if ($product_extend){ $res['goods_list']['extend']=[]; foreach ($pre_data as $k=>$v){ if ($v['type']=='extend'){ $tmp = json_decode($v['extend_data'],true); $pc_thumb_tmp = ''; if (isset($tmp['pc_thumb'])&&$tmp['pc_thumb']){ $pc_thumb_tmp = $tmp['pc_thumb']; }else{ if (isset($tmp['gallery'])&&$tmp['gallery']){ $pc_thumb_tmp = json_decode($tmp['gallery'],JSON_UNESCAPED_UNICODE)[0]??'ydyd-develop-file.oss-cn-hangzhou.aliyuncs.com/pc_shop_pic_url_1588925532218.jpg'; }else{ $pc_thumb_tmp = 'ydyd-develop-file.oss-cn-hangzhou.aliyuncs.com/pc_shop_pic_url_1588925532218.jpg'; } } if($pc_thumb_tmp == 'www.hyey.cn'){ $pc_thumb_tmp = 'ydyd-develop-file.oss-cn-hangzhou.aliyuncs.com/pc_shop_pic_url_1588925532218.jpg'; } $res['goods_list']['extend'][] = [ 'id'=>$v['id'], 'batch_id'=>$tmp['extend_goods_id'], 'goods_name'=>$tmp['name'], 'unit_price'=>$v['unit_price'], 'goods_number'=>$v['goods_number'], 'unit'=>$tmp['unit'], 'spec_desc'=>$tmp['spec_desc'], 'drug_form'=>$tmp['drug_form'], 'retail_flag'=>$tmp['retail_flag'], 'retail_num'=>$tmp['retail_num'], 'entirety_num'=>$tmp['entirety_num'], 'term_validity'=>$tmp['term_validity'], 'stock_num'=>$tmp['stock_num'], 'manufacturer_name'=>$tmp['manufacturer_name'], 'app_thumb'=>$pc_thumb_tmp, 'pc_thumb' => $pc_thumb_tmp ]; } } } //运费 线下沟通 此处不处理 //$res['shipping_fee'] = '';//shipping_type送货方式? //应付总额 $total_amount = 0; $total_number = 0; foreach ($res['goods_list'] as $key=>$value){ if ($value){ foreach ($value as $k=>$v){ $total_amount+=sprintf("%.2f",$v['unit_price']*$v['goods_number']); $total_number+=$v['goods_number']; if(in_array($v['batch_id'],$errorIds)){ $res['goods_list'][$key][$k]['is_buy'] = 0; }else{ $res['goods_list'][$key][$k]['is_buy'] = 1; } $res['goods_list'][$key][$k]['app_thumb'] = $res['goods_list'][$key][$k]['app_thumb']?:$res['goods_list'][$key][$k]['pc_thumb']; if(strpos($res['goods_list'][$key][$k]['app_thumb'],'http') === false && $res['goods_list'][$key][$k]['app_thumb']){ $res['goods_list'][$key][$k]['app_thumb'] = 'https://'.$res['goods_list'][$key][$k]['app_thumb']; } } } } $res['goods_amount'] = number_format($total_amount,2,".",""); $res['total_number'] = $total_number; $shipping_fee = getShippingFee($total_amount,$total_number); $res['shipping_fee'] = number_format($shipping_fee,2,".",""); $res['shipping_fee_text'] = '运费规则: 满2000元包邮, 不满2000元,数量每满300运费增加20元,数量不满300按300算,以此类推。'; $res['total_amount'] = number_format(($total_amount + $shipping_fee),2,".",""); $res['is_buy'] = 0; if(!$errorIds){ $res['is_buy'] = 1; } return $res; } //取消订单 public function cancelOrder($parames) { if(!isset($parames['user_id']) || !$parames['user_id']){ throw new Exception('用户不存在'); } if(!isset($parames['order_id']) || !$parames['order_id']){ throw new Exception('订单不存在'); } try{ Db::startTrans(); $parames['status'] = 2; $count = Db::name('order_info')->update($parames); if($count){ $goods_batch_stock_update = []; $goods_activity_stock_update = [];//活动商品数量回滚 $fields = 'og.goods_number,og.price_id,og.unit_price,'; $fields .= 'gb.supplier_id,gb.goods_id,gb.batch_id,gb.stock_num,gb.sale_num,gb.total_num'; $orderGoodsArray = Db::name('order_goods')->alias('og')->field($fields) ->join('goods_batch gb','gb.batch_id=og.batch_id') ->where('og.order_id',$parames['order_id']) //->where('og.type','')//hy商品库存同样操作 ->select(); foreach ($orderGoodsArray as $orderGoods){ //库存回滚 //1.总库存:goods_batch表stock_num字段 $tmp_data = []; $tmp_data['batch_id'] = $orderGoods['batch_id']; $tmp_data['stock_num'] = $orderGoods['stock_num']+$orderGoods['goods_number']; $tmp_data['sale_num'] = $orderGoods['sale_num']-$orderGoods['goods_number']; $tmp_data['total_num'] = $orderGoods['total_num']-$orderGoods['goods_number']; $tmp_data['update_time'] = date('Y-m-d H:i:s'); $tmp_data['update_ip'] = \request()->ip(); $goods_batch_stock_update[] = $tmp_data; //2.活动商品数量回滚 if ($orderGoods['supplier_id']!=10006){ $ac_where = []; $ac_where[] = ['goods_id','=',$orderGoods['goods_id']]; $ac_where[] = ['price_id','=',$orderGoods['price_id']]; $ac_exist = Db::name('activity_goods_relation')->where($ac_where)->find(); if ($ac_exist){ Db::name('goods_batch_area_relation')->where('id',$orderGoods['price_id'])->inc('discount_num',$orderGoods['goods_number'])->update(); } } } if ($goods_batch_stock_update){ $goodsBatchObj = new GoodsBatchModel(); $goodsBatchObj->isUpdate(true)->saveAll($goods_batch_stock_update);//异常自动抛出 } Db::commit(); return $count; }else{ Db::rollback(); throw new Exception('订单状态异常'); } }catch (Exception $exception){ Db::rollback(); throw new Exception($exception->getMessage()); } } //去支付 public function toPay($parames){ if(!isset($parames['user_id']) || !$parames['user_id']){ throw new Exception('用户不存在'); } if (!isset($parames['order_id']) || !$parames['order_id']){ throw new Exception('参数不合法'); } $order = Db::name('order_info') ->where('user_id',$parames['user_id']) ->where('order_id',$parames['order_id']) ->field('order_id,order_sn,shipping_fee,goods_amount,total_amount,create_time,deadline_time') ->find(); if(!$order){ throw new Exception('数据异常'); } $is_new_user = Db::name('users')->where('user_id',$parames['user_id'])->value('is_new_user'); $order['is_new_user'] = $is_new_user; return $order; } }