| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337 |
- <?php
- namespace app\api\logic;
- use app\adminapi\service\DistributeLeafletsService;
- use app\common\enum\GoodsEnum;
- use app\common\enum\PayEnum;
- use app\common\enum\RefundEnum;
- use app\common\enum\WorkEnum;
- use app\common\enum\YesNoEnum;
- use app\common\logic\BaseLogic;
- use app\common\logic\PaymentLogic;
- use app\common\logic\PayNotifyLogic;
- use app\common\logic\RefundLogic;
- use app\common\model\coupon\CouponCategory;
- use app\common\model\coupon\UserCoupon;
- use app\common\model\dict\DictData;
- use app\common\model\goods\Goods;
- use app\common\model\master_commission\MasterWorkerCommissionConfig;
- use app\common\model\master_commission\MasterWorkerCommissionNotice;
- use app\common\model\master_worker\MasterWorker;
- use app\common\model\master_worker\MasterWorkerRule;
- use app\common\model\orders\OrderEffectiveLog;
- use app\common\model\orders\RechargeOrder;
- use app\common\model\property\PropertyActivity;
- use app\common\model\recharge\OrderGoods;
- use app\common\model\refund\RefundRecord;
- use app\common\model\service_area\ServiceArea;
- use app\common\model\spare_part\SparePart;
- use app\common\model\works\ServiceWork;
- use app\common\model\works\ServiceWorkAppointmentLog;
- use app\common\model\works\ServiceWorkSpare;
- use app\workerapi\logic\ServiceWorkLogLogic;
- use think\Exception;
- use think\facade\Db;
- use think\facade\Log;
- /**
- * 订单逻辑层
- * Class ServiceOrderLogic
- * @package app\api\logic
- */
- class ServiceOrderLogic extends BaseLogic
- {
- /**
- * 判断是否在某服务区 返回服务区城市信息
- * @param $params
- * @return bool
- */
- public static function isService($params):array
- {
- // 查询服务区所有的地点
- $rules = ServiceArea::field(['id', 'province', 'city','county', 'area_name', 'electronic_fence'])->select()->toArray();
- $point=['lng'=> $params['lon'],'lat'=> $params['lat']];
- foreach ($rules as $value){
- foreach ($value['electronic_fence'] as $polygon) {
- if (isPointInPolygon($point, $polygon)) {
- return [
- 'id'=>$value['id'],
- 'city'=>$value['city'],
- 'county'=>$value['county'],
- 'area_name'=>$value['area_name']
- ];
- }
- }
- }
- throw new Exception('已超出服务区域!-1001');
- }
- /**
- * 提交订单
- * @param array $params
- * @return array|false
- */
- public static function submitOrder($params)
- {
- Db::startTrans();
- try {
- // 订单位置是否在服务区内
- $areas = self::isService($params);
- $goods = Goods::findOrEmpty($params['goods_id']);
- if($goods->isEmpty()){
- throw new Exception('产品不存在!');
- }
- if(empty($params['user_info']['mobile'])){
- throw new Exception('请先补充您的联系方式后在提交订单');
- }
- //根据服务工单计算当前订单应支付金额
- if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE){
- //一口价订单
- $order_total = $goods['service_fee'];
- $order_amount = $goods['service_fee'];
- }else if ($goods['goods_payment_type'] == GoodsEnum::DEP_GOODS_PAYMENT_TYPE){
- $order_total = $goods['service_fee'];
- $order_amount = $goods['service_fee'];
- }
- else{
- $order_total = $goods['base_service_fee'];
- $order_amount = $goods['service_fee'];
- }
- //优惠券验证
- if(!empty($params['coupon_id'])){
- $user_coupon = UserCoupon::where(['id'=>$params['coupon_id'],'user_id'=>$params['user_id'],'voucher_status'=>0])
- ->where('voucher_count','>',0)
- ->where('expire_time','>=',time())
- ->where('begin_use','<',time())
- ->findOrEmpty();
- if($user_coupon->isEmpty()){
- throw new Exception('该优惠券无法使用');
- }
- if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE and $order_amount<$user_coupon['amount_require']){
- throw new Exception('该优惠劵不满足满减使用条件');
- }
- if($goods['goods_payment_type'] != GoodsEnum::ISGOODS_PAYMENT_TYPE){
- throw new Exception('请在支付尾款的时候使用该优惠券');
- }
- //优惠券折扣
- if($user_coupon['mold_type'] == 1){
- //按比例折扣
- if($user_coupon['discount_ratio']>=1){
- throw new Exception('优惠券有误,请联系客服');
- }
- $order_coupon_amount = $order_amount*(1-$user_coupon['discount_ratio']);
- }else{
- $order_coupon_amount = $user_coupon['amount'];
- }
- if(!empty($user_coupon['max_deductible_price'])){
- $order_amount = ($order_coupon_amount>$user_coupon['max_deductible_price'])?($order_amount-$user_coupon['max_deductible_price']):($order_amount-$order_coupon_amount);
- }else{
- $order_amount = $order_amount-$order_coupon_amount;
- }
- $user_coupon->voucher_status = 1;
- $user_coupon->voucher_count = $user_coupon->voucher_count-1;
- $user_coupon->save();
- }
- //生成服务工单
- $work_data = [
- 'work_sn' => generate_sn(ServiceWork::class, 'work_sn'),
- 'real_name' => $params['contact_people'],
- 'mobile' => $params['contact_number'],
- 'address' => $params['address'],
- 'province' => $areas['province']??0,
- 'city' => $areas['city']??0,
- 'area_name' => $areas['area_name']??'',
- 'service_area_id' => $areas['id']??0,
- 'title' => $goods->goods_name,
- 'category_type' => $goods['category_type'],
- 'goods_category_ids' => $goods['goods_category_ids'],
- 'goods_category_id' => $goods['goods_category_id'],
- 'base_service_fee' => $goods['base_service_fee'],
- 'service_fee' => $goods['service_fee'],
- 'work_pay_status'=>WorkEnum::UN_PAY_STATUS,
- 'appointment_time' => strtotime($params['appointment_time']),
- 'user_id'=>$params['user_id'],
- 'lon'=>!empty($params['lon'])?$params['lon']:0,
- 'lat'=>!empty($params['lat'])?$params['lat']:0,
- 'property_activity_id'=>!empty($params['property_activity_id'])?$params['property_activity_id']:0,
- 'user_equity_id'=>$params['user_equity_id']??0,
- ];
- //判断是否是加单
- if(!empty($params['worker'])){
- $worker_id = MasterWorker::where('worker_number',$params['worker'])->value('id');
- $work_data['master_worker_id'] = $worker_id;
- $work_data['work_status'] = 1;
- $work_data['dispatch_time'] = time();
- $work_data['work_type'] = 2;
- $work_data['data_type'] = 1;
- }
- //判断是否是复购单
- $is_work = ServiceWork::where(['user_id'=>$params['user_id'],'service_status'=>3])->findOrEmpty();
- if(!$is_work->isEmpty()){
- $work_data['data_type'] = 1;
- }
- $service_work = ServiceWork::create($work_data);
- //使用权益卡时订单应支付金额=0
- if(isset($params['user_equity_id']) && $params['user_equity_id']){
- $order_total = 0;
- $order_amount = 0;
- }
- //生成服务订单
- $data = [
- 'work_id'=> $service_work['id'],
- 'sn' => generate_sn(RechargeOrder::class, 'sn'),
- 'order_type'=>0,//服务订单
- 'order_terminal' => $params['terminal'],
- 'payment_type'=>$goods['goods_payment_type']==GoodsEnum::ISGOODS_PAYMENT_TYPE?0:1,
- 'user_id' => $params['user_id'],
- 'pay_status' => PayEnum::UNPAID,
- 'coupon_id'=>!empty($params['coupon_id'])?$params['coupon_id']:0,
- 'coupon_price'=>!empty($order_coupon_amount)?$order_coupon_amount:0,
- 'pay_way' => $params['pay_way'],
- 'order_total' => $order_total,
- 'order_amount' => $order_amount,
- ];
- $order = RechargeOrder::create($data);
- //生成订单服务详情
- OrderGoods::create([
- 'sn' => $order['sn'],
- 'goods_id' => $params['goods_id'],
- 'category_type' => $goods['category_type'],
- 'goods_category_ids' => $goods['goods_category_ids'],
- 'goods_category_id' => $goods['goods_category_id'],
- 'goods_name' => $goods['goods_name'],
- 'goods_image' => $goods['goods_image'],
- 'goods_video' => $goods['goods_video'],
- 'goods_number' => $goods['goods_number'],
- 'good_unit' => $goods['good_unit'],
- 'goods_size' => $goods['goods_size'],
- 'goods_type' => $goods['goods_type'],
- 'goods_brand' => $goods['goods_brand'],
- 'install_guide' => $goods['install_guide'],
- 'goods_payment_type'=>$goods['goods_payment_type'],
- 'base_service_fee' => $goods['base_service_fee'],
- 'service_total' => $goods['service_total'],
- 'service_fee' => $goods['service_fee'],
- 'service_image' => $goods['service_image'],
- 'warranty_period'=>$goods['warranty_period'],
- 'fee_schedule' => $goods['fee_schedule'],
- 'goods_status' => $goods['goods_status'],
- ]);
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- return [
- 'order_id' => (int)$order['id'],
- 'work_id' => (int)$order['work_id'],
- ];
- }
- /**
- * 提交尾款订单
- * @param array $params
- * @return array|false
- */
- public static function submitFinalOrder($params)
- {
- Db::startTrans();
- try {
- $order = \app\common\model\recharge\RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
- if ($order->isEmpty()) {
- throw new Exception('订单不存在');
- }
- //判断订单类型.服务订单尾款处理
- if($order['order_type'] == 0 and $order['pay_status'] == PayEnum::ISPAID)//服务工单
- {
- $order = RechargeOrder::where(['work_id'=>$order['work_id'],'pay_status'=>0])->findOrEmpty();
- if($order->isEmpty()){
- throw new Exception('订单已支付');
- }
- }
- if ($order['pay_status'] == PayEnum::ISPAID) {
- throw new Exception('订单已支付');
- }
- $order_goods = OrderGoods::where('sn',$params['sn'])->findOrEmpty();
- $goods = Goods::findOrEmpty($order_goods['goods_id']);
- //判断是否存在优惠券
- if(!empty($params['coupon_id']) && empty($order['coupon_id'])){
- $user_coupon = UserCoupon::where(['id'=>$params['coupon_id'],'user_id'=>$params['user_id'],'voucher_status'=>0])
- ->where('voucher_count','>',0)
- ->where('expire_time','>=',time())
- ->where('begin_use','<',time())
- ->findOrEmpty();
- if($user_coupon->isEmpty()){
- throw new Exception('该优惠券不满足使用条件');
- }
- if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE and $order['order_amount']<$user_coupon['amount_require']){
- throw new Exception('该优惠劵不满足满减使用条件');
- }
- //优惠券折扣
- if($user_coupon['mold_type'] == 1){
- //按比例折扣
- if($user_coupon['discount_ratio']>=1){
- throw new Exception('优惠券有误,请联系客服');
- }
- $order_coupon_amount = $order['order_amount']*(1-$user_coupon['discount_ratio']);
- }else{
- $order_coupon_amount = $user_coupon['amount'];
- }
- if(!empty($user_coupon['max_deductible_price'])){
- $order_amount = ($order_coupon_amount>$user_coupon['max_deductible_price'])?($order['order_amount']-$user_coupon['max_deductible_price']):($order['order_amount']-$order_coupon_amount);
- }else{
- $order_amount = $order['order_amount']-$order_coupon_amount;
- }
- $user_coupon->voucher_status = 1;
- $user_coupon->voucher_count = $user_coupon->voucher_count-1;
- $user_coupon->save();
- $order->coupon_id = !empty($params['coupon_id'])?$params['coupon_id']:0;
- $order->coupon_price = !empty($order_coupon_amount)?$order_coupon_amount:0;
- $order->order_amount = !empty($order_amount)?$order_amount:$order->order_amount;
- $order->save();
- }
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- return [
- 'order_id' => (int)$order['id'],
- 'work_id' => (int)$order['work_id'],
- ];
- }
- /**
- * 重置订单优惠券
- * * @param $params
- * * * @return array|false
- */
- public static function cancelOrderCoupon($params)
- {
- Db::startTrans();
- try {
- $order = \app\common\model\recharge\RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
- if ($order->isEmpty()) {
- throw new Exception('订单不存在');
- }
- //判断订单类型.服务订单尾款处理
- if($order['order_type'] == 0 and $order['pay_status'] == PayEnum::ISPAID)//服务工单
- {
- $order = RechargeOrder::where(['work_id'=>$order['work_id'],'pay_status'=>0])->findOrEmpty();
- if($order->isEmpty()){
- throw new Exception('订单已支付');
- }
- }
- if ($order['pay_status'] == PayEnum::ISPAID) {
- throw new Exception('订单已支付');
- }
- if(!empty($order['coupon_id'])){
- $user_coupon_ed = UserCoupon::findOrEmpty($order['coupon_id']);
- if(!$user_coupon_ed->isEmpty()){
- $user_coupon_ed->voucher_count = $user_coupon_ed->voucher_count+1;
- $user_coupon_ed->voucher_status = 0;
- $user_coupon_ed->save();
- $order->order_amount = $order->order_amount+$order->coupon_price;
- $order->coupon_id = 0;
- $order->coupon_price = 0;
- $order->save();
- }
- }
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * 获取订单工程师信息
- * * @param $params
- * * @return array|false
- */
- public static function getMasterWorker($params)
- {
- try {
- $order_info = \app\common\model\recharge\RechargeOrder::with(['order_goods'=>function ($query) {
- $query->visible(['goods_name','goods_image','goods_number','good_unit']);
- },'service_work'=>function ($query) {
- $query->visible(['real_name','mobile','address','service_status','appointment_time','master_worker_id','work_images','finished_images','finished_time'])->append(['service_status_text','user_service_status','user_service_status_text']);
- }])
- ->visible(['id','sn','payment_type','order_total','order_amount','pay_status','create_time','title','work_id'])
- ->where([
- 'order_type' => 0,
- 'user_id' => $params['user_id'],
- 'sn'=>$params['sn']
- ])->findOrEmpty()->toArray();
- $data = [];
- //获取工程师参数
- if(!empty($order_info['service_work']['master_worker_id'])){
- $worker = MasterWorker::find($order_info['service_work']['master_worker_id']);
- $data['avatar'] = $worker['avatar'];
- $data['real_name'] = $worker['real_name'];
- $data['worker_number'] = $worker['worker_number'];
- $data['mobile'] = $worker['mobile'];
- $maintain_exp_type = !empty($worker->worker_register->maintain_exp_type)?$worker->worker_register->maintain_exp_type:'';
- $data['worker_exp'] = DictData::where(['type_value'=>'worker_exp_type','value'=>$maintain_exp_type])->value('name');
- $data['appointment_time'] = $order_info['service_work']['appointment_time'];
- }
- return $data;
- }
- catch (\Exception $e) {
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * 获取订单详情
- * @param $params
- * @return array|false
- */
- public static function detail($params)
- {
- try {
- $order_info = \app\common\model\recharge\RechargeOrder::with(['order_goods'=>function ($query) {
- $query->visible(['goods_id','goods_name','goods_image','goods_number','good_unit']);
- },'service_work'=>function ($query) {
- $query->visible(['work_sn','real_name','mobile','address','service_status','appointment_time','master_worker_id','work_images','explanation','finished_images','finished_time','spare_total','service_work_spare_id','refund_approval','property_activity_id'])->append(['service_status_text','user_service_status','user_service_status_text']);
- }])
- ->visible(['id','sn','payment_type','order_total','order_amount','paid_amount','pay_status','create_time','title','work_id'])
- ->where([
- 'order_type' => 0,
- 'user_id' => $params['user_id'],
- 'sn'=>$params['sn']
- ])->findOrEmpty()->toArray();
- if(empty($order_info)){
- throw new Exception('订单不存在');
- }
- $order_info['master_worker'] = [
- 'avatar' => '',
- 'real_name'=>'',
- 'worker_number'=>'',
- 'mobile'=>'',
- 'worker_exp'=>''
- ];
- //查询总价
- $order_info['order_amount'] = \app\common\model\recharge\RechargeOrder::where(['order_type'=>0,'user_id'=>$params['user_id'],'work_id'=>$order_info['work_id']])->sum('order_amount');
- $order_info['paid_amount'] = \app\common\model\recharge\RechargeOrder::where(['order_type'=>0,'user_id'=>$params['user_id'],'work_id'=>$order_info['work_id']])->sum('paid_amount');
- //退款金额
- $order_refund_amount = 0;
- //获取工程师参数
- if(!empty($order_info['service_work']['master_worker_id'])){
- $worker = MasterWorker::find($order_info['service_work']['master_worker_id']);
- $order_info['master_worker']['avatar'] = !empty($worker)?$worker['avatar']:'';
- $order_info['master_worker']['real_name'] = !empty($worker)?$worker['real_name']:'';
- $order_info['master_worker']['worker_number'] = !empty($worker)?$worker['worker_number']:'';
- $order_info['master_worker']['mobile'] = !empty($worker)?$worker['mobile']:'';
- $maintain_exp_type = !empty($worker->worker_register->maintain_exp_type)?$worker->worker_register->maintain_exp_type:'';
- $order_info['master_worker']['worker_exp'] = DictData::where(['type_value'=>'worker_exp_type','value'=>$maintain_exp_type])->value('name');
- }
- //搜索当前工单下的所有订单记录
- $order_info['pay_orders'] = \app\common\model\recharge\RechargeOrder::where(['work_id'=>$order_info['work_id']])->field('id as order_id, pay_status ,refund_status,payment_type,pay_way,pay_time,order_total,order_amount,coupon_price')->order('id asc')->select()->toArray();
- $pay_status_data = DictData::where('type_value','pay_status')->column('name','value');
- $payment_type_data = DictData::where('type_value','payment_type')->column('name','value');
- $pay_way_data = DictData::where('type_value','pay_way')->column('name','value');
- $coupon_price = 0;
- foreach ($order_info['pay_orders'] as $k=>&$v){
- $v['pay_status_name'] = $pay_status_data[$v['pay_status']];
- $v['payment_type_name'] = $payment_type_data[$v['payment_type']];
- $v['pay_way_name'] = $pay_way_data[$v['pay_way']];
- if($v['refund_status']==1){
- $order_refund_amount += $v['order_amount'];
- $refund_status = RefundRecord::where('order_id',$v['order_id'])->value('refund_status');
- }
- if($v['payment_type']!=1 and !empty($order_info['service_work']['spare_total'])){
- $v['order_amount_total'] = $v['order_amount'];
- $v['order_amount'] = $v['order_amount'] - $order_info['service_work']['spare_total'];
- }
- $coupon_price += $v['coupon_price'];
- }
- //汇总优惠卷额度
- $order_info['coupon_price'] = $coupon_price;
- //退款汇总
- $order_info['refund_amount'] = $order_refund_amount;
- if(isset($refund_status)){
- switch ($refund_status){
- case 0:
- $order_info['refund_status'] = '退款中';
- break;
- case 1:
- $order_info['refund_status'] = '退款成功';
- break;
- case 2:
- $order_info['refund_status'] = '退款失败';
- break;
- }
- }else{
- $order_info['refund_status'] = '未退款';
- }
- // 所有配件
- $order_info['spare_total'] = $order_info['service_work']['spare_total']??0;
- $order_info['spare_parts'] = [];
- if($order_info['service_work']['service_work_spare_id']){
- $work_spare_parts = json_decode(ServiceWorkSpare::where('id',$order_info['service_work']['service_work_spare_id'])->value('spare_parts'),true);
- $spare_parts = SparePart::where('id','in',array_column($work_spare_parts,'id'))
- ->field(['id', 'goods_category_id', 'spare_name', 'spare_image', 'spare_number', 'spare_unit','spare_status'])
- ->select()
- ->toArray();
- $spare_parts = array_column($spare_parts,null,'id');
- foreach (array_column($work_spare_parts,null,'id') as $k=>&$v){
- $spare_parts[$k] = array_merge($spare_parts[$k],$v);
- }
- $order_info['spare_parts'] = array_values($spare_parts)??[];
- }
- //获取所有的改约记录
- $order_info['appoint_list'] = [];
- $appoint_log = ServiceWorkAppointmentLog::where('work_id',$order_info['work_id'])->order('id desc')->select()->toArray();
- if(!empty($appoint_log)){
- $order_info['appoint_list'][0]['appointment_time'] = date('Y-m-d H:i:s',$appoint_log[0]['this_appointment_time']);
- foreach ($appoint_log as $k1=>$v1){
- $order_info['appoint_list'][$k1+1]['appointment_time'] = date('Y-m-d H:i:s',$v1['last_appointment_time']);
- }
- }
- //获取是否是活动工单
- if(!empty($order_info['service_work']['property_activity_id'])){
- $propertyActivity = PropertyActivity::findOrEmpty($order_info['service_work']['property_activity_id']);
- $order_info['service_work']['url_page'] = rawurlencode($params['domain'].'/static/wxapp/H5/'.$propertyActivity['url_page'].'/index.html?property_activity_id='.$propertyActivity['id'].'&token='.$params['user_info']['token']);
- }
- //保修工单时的信息
- $order_info['effective_income_amount'] = 0;
- if(!empty($order_info['service_work']['order_effective_id'])){
- $order_effective = OrderEffectiveLog:: findOrEmpty($order_info['service_work']['order_effective_id']);
- $serviceWork = ServiceWork::findOrEmpty($order_effective->work_id);
- if($serviceWork->master_worker_id != $order_info['service_work']['master_worker_id']){
- $order_info['effective_income_amount'] = \app\adminapi\logic\effective\OrderEffectiveLogLogic::commissionAndAssuranceDeposit($serviceWork);
- }
- }
- return $order_info;
- }
- catch (\Exception $e) {
- self::setError($e->getMessage());
- return false;
- }
- }
- public static function getDetailStatus($params)
- {
- try {
- $order_info = \app\common\model\recharge\RechargeOrder::with(['service_work'=>function ($query) {
- $query->visible(['work_sn'])->append(['service_status_text','user_service_status','user_service_status_text']);
- }])
- ->visible(['id','sn'])
- ->where([
- 'order_type' => 0,
- 'user_id' => $params['user_id'],
- 'sn'=>$params['sn']
- ])->findOrEmpty()->toArray();
- if(empty($order_info)){
- throw new Exception('订单不存在');
- }
- return $order_info;
- }
- catch (\Exception $e) {
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * 获取订单支付详情--这个用于尾款支付的时候,自动使用了优惠券,不要随便调用
- * @param $params
- * @return array|false
- */
- public static function orderPayInfo($params)
- {
- Db::startTrans();
- try {
- $order_info = \app\common\model\recharge\RechargeOrder::with(['order_goods'=>function ($query) {
- $query->visible(['goods_id','goods_payment_type','goods_category_id']);
- },'service_work'=>function ($query) {
- $query->visible(['service_fee','spare_total','service_work_spare_id','goods_category_id']);
- }])
- ->visible(['id','pay_status','sn','work_id','coupon_id','coupon_price'])
- ->where([
- 'order_type' => 0,
- 'user_id' => $params['user_id'],
- 'sn'=>$params['sn']
- ])->findOrEmpty()->toArray();
- if(empty($order_info)){
- throw new Exception('订单不存在');
- }
- //查询总价
- $order_info['order_amount'] = \app\common\model\recharge\RechargeOrder::where(['order_type'=>0,'user_id'=>$params['user_id'],'work_id'=>$order_info['work_id']])->sum('order_amount');
- $order_info['paid_amount'] = \app\common\model\recharge\RechargeOrder::where(['order_type'=>0,'user_id'=>$params['user_id'],'work_id'=>$order_info['work_id']])->sum('paid_amount');
- //搜索当前工单下的所有订单记录
- $order_info['pay_orders'] = \app\common\model\recharge\RechargeOrder::where(['work_id'=>$order_info['work_id']])->field('id as order_id, pay_status,refund_status,payment_type,pay_way,pay_time,order_total,order_amount,coupon_id,coupon_price')->order('id asc')->select()->toArray();
- $pay_status_data = DictData::where('type_value','pay_status')->column('name','value');
- $payment_type_data = DictData::where('type_value','payment_type')->column('name','value');
- $pay_way_data = DictData::where('type_value','pay_way')->column('name','value');
- $coupon_price = 0;
- //退款金额
- $order_refund_amount = 0;
- $payment_type = 0;
- foreach ($order_info['pay_orders'] as $k=>&$v){
- $v['pay_status_name'] = $pay_status_data[$v['pay_status']];
- $v['payment_type_name'] = $payment_type_data[$v['payment_type']];
- $v['pay_way_name'] = $pay_way_data[$v['pay_way']];
- if($v['refund_status']==1){
- $order_refund_amount += $v['order_amount'];
- $refund_status = RefundRecord::where('order_id',$v['order_id'])->value('refund_status');
- }
- if($v['payment_type']!=1 and !empty($order_info['service_work']['spare_total'])){
- $v['order_total'] = $v['order_total'] - $order_info['service_work']['spare_total'];
- $v['order_amount'] = $v['order_amount'] - $order_info['service_work']['spare_total'] + $v['coupon_price'];
- }
- if($v['pay_status']!=0){
- $coupon_price += $v['coupon_price'];
- }
- if($v['pay_status'] == 0){
- $order_info['coupon_id'] = $v['coupon_id'];
- $order_info['coupon_price'] = $v['coupon_price'];
- }
- $payment_type = $v['payment_type'];
- }
- $order_info['payment_type'] = $payment_type;
- //汇总优惠卷额度
- $order_info['coupon_price'] = $coupon_price;
- //退款汇总
- $order_info['refund_amount'] = $order_refund_amount;
- if(isset($refund_status)){
- switch ($refund_status){
- case 0:
- $order_info['refund_status'] = '退款中';
- break;
- case 1:
- $order_info['refund_status'] = '退款成功';
- break;
- case 2:
- $order_info['refund_status'] = '退款失败';
- break;
- }
- }else{
- $order_info['refund_status'] = '未退款';
- }
- // 所有配件
- $order_info['spare_total'] = $order_info['service_work']['spare_total']??0;
- $order_info['spare_parts'] = [];
- if($order_info['service_work']['service_work_spare_id']){
- $work_spare_parts = json_decode(ServiceWorkSpare::where('id',$order_info['service_work']['service_work_spare_id'])->value('spare_parts'),true);
- $spare_parts = SparePart::where('id','in',array_column($work_spare_parts,'id'))
- ->field(['id', 'goods_category_id', 'spare_name', 'spare_image', 'spare_number', 'spare_unit','spare_status'])
- ->select()
- ->toArray();
- $spare_parts = array_column($spare_parts,null,'id');
- foreach (array_column($work_spare_parts,null,'id') as $k=>&$v){
- $spare_parts[$k] = array_merge($spare_parts[$k],$v);
- }
- $order_info['spare_parts'] = array_values($spare_parts)??[];
- }
- //获取是否存在可使用优惠券
- if(!empty($order_info['coupon_id'])){
- $order_info['order_amount'] = $order_info['order_amount'] + $order_info['coupon_price'];
- $order_info['coupon'] = UserCoupon::where('id',$order_info['coupon_id'])->findOrEmpty()->toArray();
- $usable_coupon = true;
- }else{
- if(!empty($order_info['order_goods'][0])){
- $usable_coupon = UserCouponLogic::categoryWithAmountLists(['user_id'=>$params['user_id'],'amount'=>$order_info['order_amount'],'goods_category_id'=>$order_info['order_goods'][0]['goods_category_id'],'goods_id'=>$order_info['order_goods'][0]['goods_id']]);
- if(!empty($usable_coupon[0]['id'])){
- //绑定最新可使用优惠券
- $final_order = \app\common\model\recharge\RechargeOrder::where(['order_type'=>0,'user_id'=>$params['user_id'],'work_id'=>$order_info['work_id'],'pay_status'=>0])->findOrEmpty();
- if(!empty($final_order)){
- $user_coupon = UserCoupon::where('id',$usable_coupon[0]['id'])->findOrEmpty();
- $user_coupon->voucher_status = 1;
- $user_coupon->voucher_count = $user_coupon->voucher_count-1;
- $user_coupon->save();
- $final_order->coupon_id = $usable_coupon[0]['id'];
- $final_order->coupon_price = $usable_coupon[0]['amount'];
- $final_order->order_amount = $final_order->order_amount - $usable_coupon[0]['amount'];
- $final_order->save();
- $order_info['coupon'] = $usable_coupon[0];
- }
- }
- }
- }
- $order_info['usable_coupon'] = !empty($usable_coupon)?true:false;
- Db::commit();
- return $order_info;
- } catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * 取消订单
- * @param $params
- * @return false|void
- */
- public static function cancelOrder($params)
- {
- Db::startTrans();
- try {
- $work_id = \app\common\model\recharge\RechargeOrder::where([
- 'order_type' => 0,
- 'user_id' => $params['user_id'],
- 'sn'=>$params['sn']
- ])->value('work_id');
- if(empty($work_id)){
- throw new Exception('订单不存在');
- }
- $payed_order = \app\common\model\recharge\RechargeOrder::where(['user_id'=>$params['user_id'],'work_id'=>$work_id,'pay_status'=>1])->findOrEmpty();
- if(!$payed_order->isEmpty()){
- throw new Exception('存在已支付订单,不允许取消订单,请联系客服');
- }
- //软删除订单
- $cancel_order = \app\common\model\recharge\RechargeOrder::where(['user_id'=>$params['user_id'],'work_id'=>$work_id])->findOrEmpty();
- $cancel_order->pay_status = 2;
- $cancel_order->save();
- //更新工单状态为已取消
- $service_work = ServiceWork::find($work_id);
- $service_work->service_status = 4;
- $service_work->save();
- //判断如果存在优惠券,返还优惠券
- if(!empty($payed_order->coupon_id)){
- $user_coupon = UserCoupon::where(['user_id'=>$payed_order->user_id,'id'=>$payed_order->coupon_id])->findOrEmpty();
- $user_coupon->voucher_status = 0;
- $user_coupon->voucher_count = $user_coupon->voucher_count+1;
- $user_coupon->save();
- }
- Db::commit();
- if($cancel_order->pay_time){
- // 订单取消通知【给用户】
- event('Notice', [
- 'scene_id' => 122,
- 'params' => [
- 'user_id' => $params['user_id']
- ]
- ]);
- }
- }
- catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * 用户确认尾款报价单
- * @param $params
- * @return false|void
- */
- public static function confirmOrder($params)
- {
- Db::startTrans();
- try {
- $work_id = \app\common\model\recharge\RechargeOrder::where([
- 'order_type' => 0,
- 'user_id' => $params['user_id'],
- 'sn'=>$params['sn']
- ])->value('work_id');
- if(empty($work_id)){
- throw new Exception('订单不存在');
- }
- //更新工单状态为已取消
- $service_work = ServiceWork::find($work_id);
- if($service_work->user_confirm_status==2){
- throw new Exception('请勿重复操作');
- }
- $service_work->work_status = 5;
- $service_work->user_confirm_status = 2;
- $service_work->save();
- $work_log = [
- 'work_id'=>$work_id,
- 'master_worker_id'=>$service_work->master_worker_id,
- 'opera_log'=>'用户'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'确认了报价单',
- ];
- ServiceWorkLogLogic::add($work_log);
- Db::commit();
- }
- catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * 用户确认服务完成
- * @param $params
- * @return false|void
- */
- public static function confirmServiceFinish($params)
- {
- Db::startTrans();
- try {
- $work_id = \app\common\model\recharge\RechargeOrder::where([
- 'order_type' => 0,
- 'user_id' => $params['user_id'],
- 'sn'=>$params['sn']
- ])->value('work_id');
- if(empty($work_id)){
- throw new Exception('订单不存在');
- }
- $service_work = ServiceWork::find($work_id);
- if(!isset($params['admin_id']) && $service_work->user_confirm_status!=3){
- throw new Exception('请勿重复操作');
- }
- $orders = RechargeOrder::where(['work_id'=>$work_id,'user_id'=>$params['user_id']])->select()->toArray();
- //确认所有订单总金额和结算金额
- //若订单是全款已支付订单
- if(count($orders)==1 and $orders[0]['payment_type']==0 and $orders[0]['pay_status']==1){
- $service_work->work_status = 7;
- $service_work->user_confirm_status = 5;
- $service_work->service_status = 3;
- $service_work->work_pay_status = 1;
- }else{
- $service_work->work_status = 6;
- $service_work->user_confirm_status = 4;
- }
- $service_work->finished_time = time();
- $service_work->save();
- //保修工单时,且尾款=0,直接支付无需用户操作支付
- if($service_work->order_effective_id >0){
- $paid_amount = 0;
- $order_id = 0;
- foreach ($orders as $order) {
- if($order['payment_type'] == 2) $order_id = $order['id'];
- $paid_amount += $order['order_amount'];
- }
- if($paid_amount == 0){
- // 直接支付
- $order = PaymentLogic::getPayOrderInfo(['order_id'=>$order_id]);
- if ($order) {
- //支付流程
- $result = PaymentLogic::workerPay(1, 'goods', $order, ['terminal'=>0], '');
- /*if ($result && ($result['need_pay'] == 0)) {
- $service_work->work_status = 7;
- $service_work->user_confirm_status = 5;
- $service_work->service_status = 3;
- $service_work->work_pay_status = 1;
- }*/
- }
- }
- }
- //更新工程师的进行工单数量
- MasterWorker::setWorktotal('dec',$service_work->master_worker_id);
- $work_log = [
- 'work_id'=>$work_id,
- 'master_worker_id'=>$service_work->master_worker_id,
- 'opera_log'=>'用户'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'确认服务完成',
- ];
- ServiceWorkLogLogic::add($work_log);
- Db::commit();
- }
- catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- public static function approvalChangePrice($params)
- {
- Db::startTrans();
- try {
- $order = RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
- if($order->isEmpty()){
- throw new Exception('订单不存在');
- }
- $work = ServiceWork::findOrEmpty($order->work_id);
- if($work->isEmpty()){
- throw new Exception('工单不存在');
- }
- $work->work_status = 4;
- $work->user_confirm_status = 0;
- $work->price_approval = 1;
- $work->save();
- Db::commit();
- }
- catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- public static function approvalChangeAppointment($params)
- {
- Db::startTrans();
- try {
- $order = RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
- if($order->isEmpty()){
- throw new Exception('订单不存在');
- }
- $work = ServiceWork::findOrEmpty($order->work_id);
- if($work->isEmpty()){
- throw new Exception('工单不存在');
- }
- if($work->master_worker_id!=0){
- //更新工单未确认上门的状态
- $work->work_status = 3;
- $work->user_confirm_status = 0;
- $work->appoint_approval = 1;
- }
- ServiceWorkAppointmentLog::create([
- 'work_id'=>$work->id,
- 'last_appointment_time'=>strtotime($work->appointment_time),
- 'this_appointment_time'=>strtotime($params['appointment_time']),
- ]);
- //修改预计完成时间
- if ($work->estimated_finish_time) {
- $work->estimated_finish_time = strtotime($params['appointment_time']) + (strtotime($work->estimated_finish_time) - strtotime($work->appointment_time));
- }
- $work->appointment_time = strtotime($params['appointment_time']);
- $work->save();
- Db::commit();
- }
- catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- return $work;
- }
- public static function approvalRefund($params)
- {
- Db::startTrans();
- try {
- $order = RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
- if($order->isEmpty()){
- throw new Exception('订单不存在');
- }
- $work = ServiceWork::findOrEmpty($order->work_id);
- if($work->isEmpty()){
- throw new Exception('工单不存在');
- }
- $work->refund_approval = 1;
- $work->save();
- //判断是否已下单时间过了两小时,并且师傅暂未上门,距离预约时间两小时以上
- if(($order['pay_time']+7200)>time() and $work['work_status']<4 and (strtotime($work['appointment_time'])-7200)>time()){
- //生成退款订单
- \app\common\model\recharge\RechargeOrder::update([
- 'id' => $order['id'],
- 'refund_status' => YesNoEnum::YES,
- ]);
- // 生成退款记录
- $recordSn = generate_sn(RefundRecord::class, 'sn');
- $record = RefundRecord::create([
- 'sn' => $recordSn,
- 'user_id' => $order['user_id'],
- 'order_id' => $order['id'],
- 'order_sn' => $order['sn'],
- 'order_type' => RefundEnum::ORDER_TYPE_ORDER,
- 'order_amount' => $order['order_amount'],
- 'refund_amount' => $order['order_amount'],
- 'refund_type' => RefundEnum::TYPE_ADMIN,
- 'transaction_id' => $order['transaction_id'] ?? '',
- 'refund_way' => RefundEnum::getRefundWayByPayWay($order['pay_way']),
- ]);
- //更新工单状态
- ServiceWork::update([
- 'id'=>$order['work_id'],
- 'service_status'=>5
- ]);
- // 退款
- RefundLogic::refund($order, $record['id'], $order['order_amount'], 1);
- }
- Db::commit();
- }
- catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * 订单完成通知【给用户】 - 全款 -通知
- * @param $params
- * @return bool
- */
- public static function serviceFinishNotice($params)
- {
- try {
- $order = RechargeOrder::where('sn', $params['sn'])
- ->where('payment_type','=',0)
- ->where('pay_status','=',1)
- ->findOrEmpty();
- if(!$order->isEmpty()){
- event('Notice', [
- 'scene_id' => 120,
- 'params' => [
- 'user_id' => $order['user_id']
- ]
- ]);
- }
- return true;
- }catch (\Exception $e) {
- return false;
- }
- }
- /**
- * 工程师完单的时候设置单量规则 - 通知
- * @param $params
- * @return bool
- */
- public static function orderQuantityRule($params)
- {
- try {
- $work_id = \app\common\model\recharge\RechargeOrder::where([
- 'order_type' => 0,
- 'user_id' => $params['user_id'],
- 'sn'=>$params['sn']
- ])->value('work_id');
- if(empty($work_id)){
- throw new Exception('订单不存在');
- }
- $service_work = ServiceWork::find($work_id);
- $commission_config_id = MasterWorkerCommissionConfig::where('voucher_status','=',2)
- ->where('master_worker_id', $service_work['master_worker_id'])
- ->value('id');
- if($commission_config_id){
- // 工程师完单的时候设置该规则关闭,以及短信通知工程师
- $noticeRule = Db::name('master_worker_commission_notice')->alias('n')
- ->leftJoin('service_work sw', 'n.master_worker_id = sw.master_worker_id AND sw.create_time >= n.signing_date AND sw.create_time <= n.signing_date_end')
- ->field([
- 'n.id', 'n.commission_config_id', 'n.master_worker_id', 'n.day_num', 'n.order_num', 'n.signing_date', 'n.signing_date_end','n.is_notice',
- Db::raw("COUNT(sw.id) AS order_count")
- ])
- ->where('n.commission_config_id', $commission_config_id)
- ->group('n.master_worker_id, n.id')
- ->select()->toArray();
- $is_off = true;
- foreach ($noticeRule as $item) {
- if($item['order_num'] <= $item['order_count'] && empty($item['is_notice'])){
- Log::info('orderQuantityRule:'.json_encode($item));
- MasterWorkerCommissionNotice::where('id',$item['id'])->update([
- 'is_notice'=>1
- ]);
- event('Notice', [
- 'scene_id' => 128,
- 'params' => [
- 'user_id' => $service_work['master_worker_id'],
- 'number1' => $item['day_num'],
- 'number' => $item['order_num']
- ]
- ]);
- }
- if($item['order_num'] > $item['order_count']){
- $is_off = false;
- }
- }
- if($is_off){
- MasterWorkerCommissionConfig::where('id',$commission_config_id)->update([
- 'voucher_status'=>1
- ]);
- }
- }
- return true;
- }catch (\Exception $e) {
- Log::info('orderQuantityRule:'.$e->getMessage());
- return false;
- }
- }
- /**
- * 提交订单
- * @param array $params
- * @return array|false
- */
- public static function firmSubmitOrder($params)
- {
- Db::startTrans();
- try {
- // 订单位置是否在服务区内
- $areas = self::isService($params);
- $goods = Goods::findOrEmpty($params['goods_id']);
- if($goods->isEmpty()){
- throw new Exception('产品不存在!');
- }
- if(empty($params['user_info']['mobile'])){
- throw new Exception('请先补充您的联系方式后在提交订单');
- }
- //根据服务工单计算当前订单应支付金额
- if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE){
- //一口价订单
- $order_total = $goods['service_fee'];
- $order_amount = $goods['service_fee'];
- }else if ($goods['goods_payment_type'] == GoodsEnum::DEP_GOODS_PAYMENT_TYPE){
- $order_total = $goods['service_fee'];
- $order_amount = $goods['service_fee'];
- }
- else{
- $order_total = $goods['base_service_fee'];
- $order_amount = $goods['service_fee'];
- }
- //生成服务工单
- $work_data = [
- 'work_sn' => generate_sn(ServiceWork::class, 'work_sn'),
- 'real_name' => $params['contact_people'],
- 'mobile' => $params['contact_number'],
- 'address' => $params['address'],
- 'title' => $goods->goods_name . '*' . $goods->goods_number.$goods->good_unit,
- 'category_type' => $goods['category_type'],
- 'goods_category_ids' => $goods['goods_category_ids'],
- 'goods_category_id' => $goods['goods_category_id'],
- 'base_service_fee' => $goods['base_service_fee'],
- 'service_fee' => $goods['service_fee'],
- 'work_pay_status'=>WorkEnum::UN_PAY_STATUS,
- 'work_type'=> 1,
- 'appointment_time' => strtotime($params['appointment_time']),
- 'user_id'=>$params['user_id'],
- 'lon'=>!empty($params['lon'])?$params['lon']:0,
- 'lat'=>!empty($params['lat'])?$params['lat']:0,
- 'province' => $areas['province']??0,
- 'city' => $areas['city']??0,
- 'area_name' => $areas['area_name']??'',
- 'service_area_id' => $areas['id']??0,
- ];
- $service_work = ServiceWork::create($work_data);
- //生成服务订单
- $data = [
- 'work_id'=> $service_work['id'],
- 'sn' => generate_sn(RechargeOrder::class, 'sn'),
- 'order_type'=>0,//服务订单
- 'order_terminal' => $params['terminal'],
- 'payment_type'=>$goods['goods_payment_type']==GoodsEnum::ISGOODS_PAYMENT_TYPE?0:1,
- 'user_id' => $params['user_id'],
- 'pay_status' => PayEnum::UNPAID,
- 'coupon_id'=>!empty($params['coupon_id'])?$params['coupon_id']:0,
- 'coupon_price'=>!empty($order_coupon_amount)?$order_coupon_amount:0,
- 'pay_way' => $params['pay_way'],
- 'order_total' => $order_total,
- 'order_amount' => $order_amount,
- ];
- $order = RechargeOrder::create($data);
- //生成订单服务详情
- OrderGoods::create([
- 'sn' => $order['sn'],
- 'goods_id' => $params['goods_id'],
- 'category_type' => $goods['category_type'],
- 'goods_category_ids' => $goods['goods_category_ids'],
- 'goods_category_id' => $goods['goods_category_id'],
- 'goods_name' => $goods['goods_name'],
- 'goods_image' => $goods['goods_image'],
- 'goods_video' => $goods['goods_video'],
- 'goods_number' => $goods['goods_number'],
- 'good_unit' => $goods['good_unit'],
- 'goods_size' => $goods['goods_size'],
- 'goods_type' => $goods['goods_type'],
- 'goods_brand' => $goods['goods_brand'],
- 'install_guide' => $goods['install_guide'],
- 'goods_payment_type'=>$goods['goods_payment_type'],
- 'base_service_fee' => $goods['base_service_fee'],
- 'service_total' => $goods['service_total'],
- 'service_fee' => $goods['service_fee'],
- 'service_image' => $goods['service_image'],
- 'warranty_period'=>$goods['warranty_period'],
- 'fee_schedule' => $goods['fee_schedule'],
- 'goods_status' => $goods['goods_status'],
- ]);
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- return [
- 'order_id' => (int)$order['id'],
- 'work_id' => (int)$order['work_id'],
- ];
- }
- /**
- * 绑定工程师-设置为加单
- * * @param array $params
- * * @return array|false
- */
- public static function bindWorkerAndWork(array $params)
- {
- Db::startTrans();
- try {
- $order = RechargeOrder::where(['sn'=>$params['sn'],'user_id'=>$params['user_id']])->findOrEmpty();
- if($order->isEmpty()){
- throw new Exception('订单不存在');
- }
- $work = ServiceWork::where(['id'=>$order['work_id']])->findOrEmpty();
- if($work->isEmpty()){
- throw new Exception('工单不存在');
- }
- /*if($work['work_status']!=WorkEnum::WORK_STATUS_WAIT_SERVICE){
- throw new Exception('工单状态不正确,无法绑定');
- }*/
- // 20250310 未上门状态强转新工程师
- if((int)$work['work_status'] > 3){
- throw new Exception('工单状态不正确,无法绑定');
- }
- $worker = MasterWorker::where('id',$params['worker_id'])->findOrEmpty();
- if($worker->isEmpty()){
- throw new Exception('工程师不存在');
- }
- $work->master_worker_id = $params['worker_id'];
- $work->work_status = WorkEnum::WORK_STATUS_WORKER;
- $work->receive_time = time();
- $work->work_type = 2;//加单状态
- $work->save();
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * 0元支付直接完成支付
- * @param array $params attach out_trade_no
- * @return bool|void
- */
- public static function zeroDirectlyPayment(array $params, $extra = [])
- {
- try {
- $message['out_trade_no'] = mb_substr($params['out_trade_no'], 0, 18);
- $order = RechargeOrder::where(['sn' => $params['out_trade_no']])->findOrEmpty();
- if($order->isEmpty() || $order->pay_status == PayEnum::ISPAID) {
- return true;
- }
- switch ($params['attach']) {
- case 'recharge':
- PayNotifyLogic::handle('recharge', $params['out_trade_no'], $extra);
- break;
- case 'goods':
- $res = PayNotifyLogic::handle('goods', $params['out_trade_no'], $extra);
- if($res === true){
- // 用户下单后,给订单运营专员(配置固定ID)发送公众号提醒(订单信息)
- $order = RechargeOrder::where('sn', $message['out_trade_no'])
- ->where('payment_type','IN',[0,1])
- ->where('pay_status','=',1)
- ->findOrEmpty();
- if(!$order->isEmpty()){
- $workDetail = ServiceWork::findOrEmpty($order->work_id);
- if(!$workDetail->isEmpty()){
- event('Notice', [
- 'scene_id' => 100,
- 'params' => [
- 'user_id' => 0,
- 'order_id' => $workDetail['id'],
- 'thing3' => $workDetail['title'],
- 'time6' => $workDetail['appointment_time'],
- 'phone_number8' => asteriskString($workDetail['mobile']),
- 'thing5' => (iconv_strlen($workDetail['address'])>15)?(mb_substr($workDetail['address'],0,15,'UTF-8').'...'):$workDetail['address'],
- ]
- ]);
- }
- }
- // 订单完成通知【给用户】 - 尾款 -通知
- $order = RechargeOrder::where('sn', $message['out_trade_no'])
- ->where('payment_type','=',2)
- ->where('pay_status','=',1)
- ->findOrEmpty();
- if(!$order->isEmpty()){
- event('Notice', [
- 'scene_id' => 120,
- 'params' => [
- 'user_id' => $order['user_id']
- ]
- ]);
- }
- }
- break;
- }
- return true;
- } catch (\Exception $e) {
- self::setError($e->getMessage());
- return false;
- }
- }
- }
|