ServiceOrderLogic.php 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  1. <?php
  2. namespace app\api\logic;
  3. use app\adminapi\service\DistributeLeafletsService;
  4. use app\common\enum\GoodsEnum;
  5. use app\common\enum\PayEnum;
  6. use app\common\enum\RefundEnum;
  7. use app\common\enum\WorkEnum;
  8. use app\common\enum\YesNoEnum;
  9. use app\common\logic\BaseLogic;
  10. use app\common\logic\PaymentLogic;
  11. use app\common\logic\PayNotifyLogic;
  12. use app\common\logic\RefundLogic;
  13. use app\common\model\coupon\CouponCategory;
  14. use app\common\model\coupon\UserCoupon;
  15. use app\common\model\dict\DictData;
  16. use app\common\model\goods\Goods;
  17. use app\common\model\master_commission\MasterWorkerCommissionConfig;
  18. use app\common\model\master_commission\MasterWorkerCommissionNotice;
  19. use app\common\model\master_worker\MasterWorker;
  20. use app\common\model\master_worker\MasterWorkerRule;
  21. use app\common\model\orders\RechargeOrder;
  22. use app\common\model\property\PropertyActivity;
  23. use app\common\model\recharge\OrderGoods;
  24. use app\common\model\refund\RefundRecord;
  25. use app\common\model\service_area\ServiceArea;
  26. use app\common\model\spare_part\SparePart;
  27. use app\common\model\works\ServiceWork;
  28. use app\common\model\works\ServiceWorkAppointmentLog;
  29. use app\common\model\works\ServiceWorkSpare;
  30. use app\workerapi\logic\ServiceWorkLogLogic;
  31. use think\Exception;
  32. use think\facade\Db;
  33. use think\facade\Log;
  34. /**
  35. * 订单逻辑层
  36. * Class ServiceOrderLogic
  37. * @package app\api\logic
  38. */
  39. class ServiceOrderLogic extends BaseLogic
  40. {
  41. /**
  42. * 判断是否在某服务区 返回服务区城市信息
  43. * @param $params
  44. * @return bool
  45. */
  46. public static function isService($params):array
  47. {
  48. // 查询服务区所有的地点
  49. $rules = ServiceArea::column('*','id');
  50. foreach ($rules as $value){
  51. $distance = round(DistributeLeafletsService::haversineDistance($params['lat'], $params['lon'], $value['lat'], $value['lon']), 2);
  52. if($distance <= (float)$value['distance']){
  53. return [
  54. 'province'=>$value['province'],
  55. 'city'=>$value['city'],
  56. 'county'=>$value['county'],
  57. 'area_name'=>$value['area_name']
  58. ];
  59. }
  60. }
  61. throw new Exception('已超出服务区域!');
  62. }
  63. /**
  64. * 提交订单
  65. * @param array $params
  66. * @return array|false
  67. */
  68. public static function submitOrder($params)
  69. {
  70. Db::startTrans();
  71. try {
  72. // 订单位置是否在服务区内
  73. $areas = self::isService($params);
  74. $goods = Goods::findOrEmpty($params['goods_id']);
  75. if($goods->isEmpty()){
  76. throw new Exception('产品不存在!');
  77. }
  78. if(empty($params['user_info']['mobile'])){
  79. throw new Exception('请先补充您的联系方式后在提交订单');
  80. }
  81. //根据服务工单计算当前订单应支付金额
  82. if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE){
  83. //一口价订单
  84. $order_total = $goods['service_fee'];
  85. $order_amount = $goods['service_fee'];
  86. }else if ($goods['goods_payment_type'] == GoodsEnum::DEP_GOODS_PAYMENT_TYPE){
  87. $order_total = $goods['service_fee'];
  88. $order_amount = $goods['service_fee'];
  89. }
  90. else{
  91. $order_total = $goods['base_service_fee'];
  92. $order_amount = $goods['service_fee'];
  93. }
  94. //优惠券验证
  95. if(!empty($params['coupon_id'])){
  96. $user_coupon = UserCoupon::where(['id'=>$params['coupon_id'],'user_id'=>$params['user_id'],'voucher_status'=>0])
  97. ->where('voucher_count','>',0)
  98. ->where('expire_time','>=',time())
  99. ->where('begin_use','<',time())
  100. ->findOrEmpty();
  101. if($user_coupon->isEmpty()){
  102. throw new Exception('该优惠券无法使用');
  103. }
  104. if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE and $order_amount<$user_coupon['amount_require']){
  105. throw new Exception('该优惠劵不满足满减使用条件');
  106. }
  107. if($goods['goods_payment_type'] != GoodsEnum::ISGOODS_PAYMENT_TYPE){
  108. throw new Exception('请在支付尾款的时候使用该优惠券');
  109. }
  110. //优惠券折扣
  111. if($user_coupon['mold_type'] == 1){
  112. //按比例折扣
  113. if($user_coupon['discount_ratio']>=1){
  114. throw new Exception('优惠券有误,请联系客服');
  115. }
  116. $order_coupon_amount = $order_amount*(1-$user_coupon['discount_ratio']);
  117. }else{
  118. $order_coupon_amount = $user_coupon['amount'];
  119. }
  120. if(!empty($user_coupon['max_deductible_price'])){
  121. $order_amount = ($order_coupon_amount>$user_coupon['max_deductible_price'])?($order_amount-$user_coupon['max_deductible_price']):($order_amount-$order_coupon_amount);
  122. }else{
  123. $order_amount = $order_amount-$order_coupon_amount;
  124. }
  125. $user_coupon->voucher_status = 1;
  126. $user_coupon->voucher_count = $user_coupon->voucher_count-1;
  127. $user_coupon->save();
  128. }
  129. //生成服务工单
  130. $work_data = [
  131. 'work_sn' => generate_sn(ServiceWork::class, 'work_sn'),
  132. 'real_name' => $params['contact_people'],
  133. 'mobile' => $params['contact_number'],
  134. 'address' => $params['address'],
  135. 'province' => $areas['province']??0,
  136. 'city' => $areas['city']??0,
  137. 'area_name' => $areas['area_name']??'',
  138. 'title' => $goods->goods_name,
  139. 'category_type' => $goods['category_type'],
  140. 'goods_category_ids' => $goods['goods_category_ids'],
  141. 'goods_category_id' => $goods['goods_category_id'],
  142. 'base_service_fee' => $goods['base_service_fee'],
  143. 'service_fee' => $goods['service_fee'],
  144. 'work_pay_status'=>WorkEnum::UN_PAY_STATUS,
  145. 'appointment_time' => strtotime($params['appointment_time']),
  146. 'user_id'=>$params['user_id'],
  147. 'lon'=>!empty($params['lon'])?$params['lon']:0,
  148. 'lat'=>!empty($params['lat'])?$params['lat']:0,
  149. 'property_activity_id'=>!empty($params['property_activity_id'])?$params['property_activity_id']:0,
  150. 'user_equity_id'=>$params['user_equity_id']??0,
  151. ];
  152. //判断是否是加单
  153. if(!empty($params['worker'])){
  154. $worker_id = MasterWorker::where('worker_number',$params['worker'])->value('id');
  155. $work_data['master_worker_id'] = $worker_id;
  156. $work_data['work_status'] = 1;
  157. $work_data['dispatch_time'] = time();
  158. $work_data['work_type'] = 2;
  159. $work_data['data_type'] = 1;
  160. }
  161. //判断是否是复购单
  162. $is_work = ServiceWork::where(['user_id'=>$params['user_id'],'service_status'=>3])->findOrEmpty();
  163. if(!$is_work->isEmpty()){
  164. $work_data['data_type'] = 1;
  165. }
  166. $service_work = ServiceWork::create($work_data);
  167. //使用权益卡时订单应支付金额=0
  168. if(isset($params['user_equity_id']) && $params['user_equity_id']){
  169. $order_total = 0;
  170. $order_amount = 0;
  171. }
  172. //生成服务订单
  173. $data = [
  174. 'work_id'=> $service_work['id'],
  175. 'sn' => generate_sn(RechargeOrder::class, 'sn'),
  176. 'order_type'=>0,//服务订单
  177. 'order_terminal' => $params['terminal'],
  178. 'payment_type'=>$goods['goods_payment_type']==GoodsEnum::ISGOODS_PAYMENT_TYPE?0:1,
  179. 'user_id' => $params['user_id'],
  180. 'pay_status' => PayEnum::UNPAID,
  181. 'coupon_id'=>!empty($params['coupon_id'])?$params['coupon_id']:0,
  182. 'coupon_price'=>!empty($order_coupon_amount)?$order_coupon_amount:0,
  183. 'pay_way' => $params['pay_way'],
  184. 'order_total' => $order_total,
  185. 'order_amount' => $order_amount,
  186. ];
  187. $order = RechargeOrder::create($data);
  188. //生成订单服务详情
  189. OrderGoods::create([
  190. 'sn' => $order['sn'],
  191. 'goods_id' => $params['goods_id'],
  192. 'category_type' => $goods['category_type'],
  193. 'goods_category_ids' => $goods['goods_category_ids'],
  194. 'goods_category_id' => $goods['goods_category_id'],
  195. 'goods_name' => $goods['goods_name'],
  196. 'goods_image' => $goods['goods_image'],
  197. 'goods_video' => $goods['goods_video'],
  198. 'goods_number' => $goods['goods_number'],
  199. 'good_unit' => $goods['good_unit'],
  200. 'goods_size' => $goods['goods_size'],
  201. 'goods_type' => $goods['goods_type'],
  202. 'goods_brand' => $goods['goods_brand'],
  203. 'install_guide' => $goods['install_guide'],
  204. 'goods_payment_type'=>$goods['goods_payment_type'],
  205. 'base_service_fee' => $goods['base_service_fee'],
  206. 'service_total' => $goods['service_total'],
  207. 'service_fee' => $goods['service_fee'],
  208. 'service_image' => $goods['service_image'],
  209. 'warranty_period'=>$goods['warranty_period'],
  210. 'fee_schedule' => $goods['fee_schedule'],
  211. 'goods_status' => $goods['goods_status'],
  212. ]);
  213. Db::commit();
  214. } catch (\Exception $e) {
  215. Db::rollback();
  216. self::setError($e->getMessage());
  217. return false;
  218. }
  219. return [
  220. 'order_id' => (int)$order['id'],
  221. 'work_id' => (int)$order['work_id'],
  222. ];
  223. }
  224. /**
  225. * 提交尾款订单
  226. * @param array $params
  227. * @return array|false
  228. */
  229. public static function submitFinalOrder($params)
  230. {
  231. Db::startTrans();
  232. try {
  233. $order = \app\common\model\recharge\RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
  234. if ($order->isEmpty()) {
  235. throw new Exception('订单不存在');
  236. }
  237. //判断订单类型.服务订单尾款处理
  238. if($order['order_type'] == 0 and $order['pay_status'] == PayEnum::ISPAID)//服务工单
  239. {
  240. $order = RechargeOrder::where(['work_id'=>$order['work_id'],'pay_status'=>0])->findOrEmpty();
  241. if($order->isEmpty()){
  242. throw new Exception('订单已支付');
  243. }
  244. }
  245. if ($order['pay_status'] == PayEnum::ISPAID) {
  246. throw new Exception('订单已支付');
  247. }
  248. $order_goods = OrderGoods::where('sn',$params['sn'])->findOrEmpty();
  249. $goods = Goods::findOrEmpty($order_goods['goods_id']);
  250. //判断是否存在优惠券
  251. //优惠券验证
  252. if(!empty($order['coupon_id'])){
  253. $order->coupon_id = 0;
  254. $order->coupon_price = 0;
  255. $order->order_amount = $order->order_total;
  256. $order->save();
  257. $user_coupon_ed = UserCoupon::findOrEmpty($order['coupon_id']);
  258. $user_coupon_ed->voucher_count = $user_coupon_ed->voucher_count+1;
  259. $user_coupon_ed->voucher_status = 0;
  260. $user_coupon_ed->save();
  261. $order = \app\common\model\recharge\RechargeOrder::where('sn',$order['sn'])->findOrEmpty();
  262. }
  263. if(!empty($params['coupon_id']) && empty($order['coupon_id'])){
  264. $user_coupon = UserCoupon::where(['id'=>$params['coupon_id'],'user_id'=>$params['user_id'],'voucher_status'=>0])
  265. ->where('voucher_count','>',0)
  266. ->where('expire_time','>=',time())
  267. ->where('begin_use','<',time())
  268. ->findOrEmpty();
  269. if($user_coupon->isEmpty()){
  270. throw new Exception('该优惠券不满足使用条件');
  271. }
  272. if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE and $order['order_amount']<$user_coupon['amount_require']){
  273. throw new Exception('该优惠劵不满足满减使用条件');
  274. }
  275. //优惠券折扣
  276. if($user_coupon['mold_type'] == 1){
  277. //按比例折扣
  278. if($user_coupon['discount_ratio']>=1){
  279. throw new Exception('优惠券有误,请联系客服');
  280. }
  281. $order_coupon_amount = $order['order_amount']*(1-$user_coupon['discount_ratio']);
  282. }else{
  283. $order_coupon_amount = $user_coupon['amount'];
  284. }
  285. if(!empty($user_coupon['max_deductible_price'])){
  286. $order_amount = ($order_coupon_amount>$user_coupon['max_deductible_price'])?($order['order_amount']-$user_coupon['max_deductible_price']):($order['order_amount']-$order_coupon_amount);
  287. }else{
  288. $order_amount = $order['order_amount']-$order_coupon_amount;
  289. }
  290. $user_coupon->voucher_status = 1;
  291. $user_coupon->voucher_count = $user_coupon->voucher_count-1;
  292. $user_coupon->save();
  293. }
  294. $order->coupon_id = !empty($params['coupon_id'])?$params['coupon_id']:0;
  295. $order->coupon_price = !empty($order_coupon_amount)?$order_coupon_amount:0;
  296. $order->order_amount = !empty($order_amount)?$order_amount:$order->order_amount;
  297. $order->save();
  298. Db::commit();
  299. } catch (\Exception $e) {
  300. Db::rollback();
  301. self::setError($e->getMessage());
  302. return false;
  303. }
  304. return [
  305. 'order_id' => (int)$order['id'],
  306. 'work_id' => (int)$order['work_id'],
  307. ];
  308. }
  309. /**
  310. * 重置订单优惠券
  311. * * @param $params
  312. * * * @return array|false
  313. */
  314. public static function cancelOrderCoupon($params)
  315. {
  316. Db::startTrans();
  317. try {
  318. $order = \app\common\model\recharge\RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
  319. if ($order->isEmpty()) {
  320. throw new Exception('订单不存在');
  321. }
  322. //判断订单类型.服务订单尾款处理
  323. if($order['order_type'] == 0 and $order['pay_status'] == PayEnum::ISPAID)//服务工单
  324. {
  325. $order = RechargeOrder::where(['work_id'=>$order['work_id'],'pay_status'=>0])->findOrEmpty();
  326. if($order->isEmpty()){
  327. throw new Exception('订单已支付');
  328. }
  329. }
  330. if ($order['pay_status'] == PayEnum::ISPAID) {
  331. throw new Exception('订单已支付');
  332. }
  333. if(!empty($order['coupon_id'])){
  334. $user_coupon_ed = UserCoupon::findOrEmpty($order['coupon_id']);
  335. if(!$user_coupon_ed->isEmpty()){
  336. $user_coupon_ed->voucher_count = $user_coupon_ed->voucher_count+1;
  337. $user_coupon_ed->voucher_status = 0;
  338. $user_coupon_ed->save();
  339. $order->order_amount = $order->order_amount+$order->coupon_price;
  340. $order->coupon_id = 0;
  341. $order->coupon_price = 0;
  342. $order->save();
  343. }
  344. }
  345. Db::commit();
  346. } catch (\Exception $e) {
  347. Db::rollback();
  348. self::setError($e->getMessage());
  349. return false;
  350. }
  351. }
  352. /**
  353. * 获取订单工程师信息
  354. * * @param $params
  355. * * @return array|false
  356. */
  357. public static function getMasterWorker($params)
  358. {
  359. try {
  360. $order_info = \app\common\model\recharge\RechargeOrder::with(['order_goods'=>function ($query) {
  361. $query->visible(['goods_name','goods_image','goods_number','good_unit']);
  362. },'service_work'=>function ($query) {
  363. $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']);
  364. }])
  365. ->visible(['id','sn','payment_type','order_total','order_amount','pay_status','create_time','title','work_id'])
  366. ->where([
  367. 'order_type' => 0,
  368. 'user_id' => $params['user_id'],
  369. 'sn'=>$params['sn']
  370. ])->findOrEmpty()->toArray();
  371. $data = [];
  372. //获取工程师参数
  373. if(!empty($order_info['service_work']['master_worker_id'])){
  374. $worker = MasterWorker::find($order_info['service_work']['master_worker_id']);
  375. $data['avatar'] = $worker['avatar'];
  376. $data['real_name'] = $worker['real_name'];
  377. $data['worker_number'] = $worker['worker_number'];
  378. $data['mobile'] = $worker['mobile'];
  379. $maintain_exp_type = !empty($worker->worker_register->maintain_exp_type)?$worker->worker_register->maintain_exp_type:'';
  380. $data['worker_exp'] = DictData::where(['type_value'=>'worker_exp_type','value'=>$maintain_exp_type])->value('name');
  381. $data['appointment_time'] = $order_info['service_work']['appointment_time'];
  382. }
  383. return $data;
  384. }
  385. catch (\Exception $e) {
  386. self::setError($e->getMessage());
  387. return false;
  388. }
  389. }
  390. /**
  391. * 获取订单详情
  392. * @param $params
  393. * @return array|false
  394. */
  395. public static function detail($params)
  396. {
  397. try {
  398. $order_info = \app\common\model\recharge\RechargeOrder::with(['order_goods'=>function ($query) {
  399. $query->visible(['goods_id','goods_name','goods_image','goods_number','good_unit']);
  400. },'service_work'=>function ($query) {
  401. $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']);
  402. }])
  403. ->visible(['id','sn','payment_type','order_total','order_amount','paid_amount','pay_status','create_time','title','work_id'])
  404. ->where([
  405. 'order_type' => 0,
  406. 'user_id' => $params['user_id'],
  407. 'sn'=>$params['sn']
  408. ])->findOrEmpty()->toArray();
  409. if(empty($order_info)){
  410. throw new Exception('订单不存在');
  411. }
  412. $order_info['master_worker'] = [
  413. 'avatar' => '',
  414. 'real_name'=>'',
  415. 'worker_number'=>'',
  416. 'mobile'=>'',
  417. 'worker_exp'=>''
  418. ];
  419. //查询总价
  420. $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');
  421. $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');
  422. //退款金额
  423. $order_refund_amount = 0;
  424. //获取工程师参数
  425. if(!empty($order_info['service_work']['master_worker_id'])){
  426. $worker = MasterWorker::find($order_info['service_work']['master_worker_id']);
  427. $order_info['master_worker']['avatar'] = !empty($worker)?$worker['avatar']:'';
  428. $order_info['master_worker']['real_name'] = !empty($worker)?$worker['real_name']:'';
  429. $order_info['master_worker']['worker_number'] = !empty($worker)?$worker['worker_number']:'';
  430. $order_info['master_worker']['mobile'] = !empty($worker)?$worker['mobile']:'';
  431. $maintain_exp_type = !empty($worker->worker_register->maintain_exp_type)?$worker->worker_register->maintain_exp_type:'';
  432. $order_info['master_worker']['worker_exp'] = DictData::where(['type_value'=>'worker_exp_type','value'=>$maintain_exp_type])->value('name');
  433. }
  434. //搜索当前工单下的所有订单记录
  435. $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();
  436. $pay_status_data = DictData::where('type_value','pay_status')->column('name','value');
  437. $payment_type_data = DictData::where('type_value','payment_type')->column('name','value');
  438. $pay_way_data = DictData::where('type_value','pay_way')->column('name','value');
  439. $coupon_price = 0;
  440. foreach ($order_info['pay_orders'] as $k=>&$v){
  441. $v['pay_status_name'] = $pay_status_data[$v['pay_status']];
  442. $v['payment_type_name'] = $payment_type_data[$v['payment_type']];
  443. $v['pay_way_name'] = $pay_way_data[$v['pay_way']];
  444. if($v['refund_status']==1){
  445. $order_refund_amount += $v['order_amount'];
  446. $refund_status = RefundRecord::where('order_id',$v['order_id'])->value('refund_status');
  447. }
  448. if($v['payment_type']!=1 and !empty($order_info['service_work']['spare_total'])){
  449. $v['order_amount_total'] = $v['order_amount'];
  450. $v['order_amount'] = $v['order_amount'] - $order_info['service_work']['spare_total'];
  451. }
  452. $coupon_price += $v['coupon_price'];
  453. }
  454. //汇总优惠卷额度
  455. $order_info['coupon_price'] = $coupon_price;
  456. //退款汇总
  457. $order_info['refund_amount'] = $order_refund_amount;
  458. if(isset($refund_status)){
  459. switch ($refund_status){
  460. case 0:
  461. $order_info['refund_status'] = '退款中';
  462. break;
  463. case 1:
  464. $order_info['refund_status'] = '退款成功';
  465. break;
  466. case 2:
  467. $order_info['refund_status'] = '退款失败';
  468. break;
  469. }
  470. }else{
  471. $order_info['refund_status'] = '未退款';
  472. }
  473. // 所有配件
  474. $order_info['spare_total'] = $order_info['service_work']['spare_total']??0;
  475. $order_info['spare_parts'] = [];
  476. if($order_info['service_work']['service_work_spare_id']){
  477. $work_spare_parts = json_decode(ServiceWorkSpare::where('id',$order_info['service_work']['service_work_spare_id'])->value('spare_parts'),true);
  478. $spare_parts = SparePart::where('id','in',array_column($work_spare_parts,'id'))
  479. ->field(['id', 'goods_category_id', 'spare_name', 'spare_image', 'spare_number', 'spare_unit','spare_status'])
  480. ->select()
  481. ->toArray();
  482. $spare_parts = array_column($spare_parts,null,'id');
  483. foreach (array_column($work_spare_parts,null,'id') as $k=>&$v){
  484. $spare_parts[$k] = array_merge($spare_parts[$k],$v);
  485. }
  486. $order_info['spare_parts'] = array_values($spare_parts)??[];
  487. }
  488. //获取所有的改约记录
  489. $order_info['appoint_list'] = [];
  490. $appoint_log = ServiceWorkAppointmentLog::where('work_id',$order_info['work_id'])->order('id desc')->select()->toArray();
  491. if(!empty($appoint_log)){
  492. $order_info['appoint_list'][0]['appointment_time'] = date('Y-m-d H:i:s',$appoint_log[0]['this_appointment_time']);
  493. foreach ($appoint_log as $k1=>$v1){
  494. $order_info['appoint_list'][$k1+1]['appointment_time'] = date('Y-m-d H:i:s',$v1['last_appointment_time']);
  495. }
  496. }
  497. //获取是否是活动工单
  498. if(!empty($order_info['service_work']['property_activity_id'])){
  499. $propertyActivity = PropertyActivity::findOrEmpty($order_info['service_work']['property_activity_id']);
  500. $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']);
  501. }
  502. return $order_info;
  503. }
  504. catch (\Exception $e) {
  505. self::setError($e->getMessage());
  506. return false;
  507. }
  508. }
  509. public static function getDetailStatus($params)
  510. {
  511. try {
  512. $order_info = \app\common\model\recharge\RechargeOrder::with(['service_work'=>function ($query) {
  513. $query->visible(['work_sn'])->append(['service_status_text','user_service_status','user_service_status_text']);
  514. }])
  515. ->visible(['id','sn'])
  516. ->where([
  517. 'order_type' => 0,
  518. 'user_id' => $params['user_id'],
  519. 'sn'=>$params['sn']
  520. ])->findOrEmpty()->toArray();
  521. if(empty($order_info)){
  522. throw new Exception('订单不存在');
  523. }
  524. return $order_info;
  525. }
  526. catch (\Exception $e) {
  527. self::setError($e->getMessage());
  528. return false;
  529. }
  530. }
  531. /**
  532. * 获取订单支付详情
  533. * @param $params
  534. * @return array|false
  535. */
  536. public static function orderPayInfo($params)
  537. {
  538. try {
  539. //判断是否存在优惠券
  540. $is_coupon_used_work_id = \app\common\model\recharge\RechargeOrder::where([
  541. 'order_type' => 0,
  542. 'user_id' => $params['user_id'],
  543. 'sn'=>$params['sn']
  544. ])->value('work_id');
  545. $is_coupon_used = \app\common\model\recharge\RechargeOrder::where('work_id',$is_coupon_used_work_id)->where('pay_status',0)->findOrEmpty();
  546. if(!empty($is_coupon_used['coupon_id'])){
  547. $user_coupon = UserCoupon::where(['user_id'=>$is_coupon_used->user_id,'id'=>$is_coupon_used->coupon_id])->findOrEmpty();
  548. $user_coupon->voucher_status = 0;
  549. $user_coupon->voucher_count = $user_coupon->voucher_count+1;
  550. $user_coupon->save();
  551. $is_coupon_used->order_amount = $is_coupon_used->order_amount+$is_coupon_used->coupon_price;
  552. $is_coupon_used->coupon_price = 0;
  553. $is_coupon_used->coupon_id = 0;
  554. $is_coupon_used->save();
  555. }
  556. $order_info = \app\common\model\recharge\RechargeOrder::with(['order_goods'=>function ($query) {
  557. $query->visible(['goods_id','goods_payment_type','goods_category_id']);
  558. },'service_work'=>function ($query) {
  559. $query->visible(['service_fee','spare_total','service_work_spare_id','goods_category_id']);
  560. }])
  561. ->visible(['id','sn','work_id'])
  562. ->where([
  563. 'order_type' => 0,
  564. 'user_id' => $params['user_id'],
  565. 'sn'=>$params['sn']
  566. ])->findOrEmpty()->toArray();
  567. if(empty($order_info)){
  568. throw new Exception('订单不存在');
  569. }
  570. //查询总价
  571. $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');
  572. $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');
  573. //搜索当前工单下的所有订单记录
  574. $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();
  575. $pay_status_data = DictData::where('type_value','pay_status')->column('name','value');
  576. $payment_type_data = DictData::where('type_value','payment_type')->column('name','value');
  577. $pay_way_data = DictData::where('type_value','pay_way')->column('name','value');
  578. $coupon_price = 0;
  579. //退款金额
  580. $order_refund_amount = 0;
  581. $payment_type = 0;
  582. foreach ($order_info['pay_orders'] as $k=>&$v){
  583. $v['pay_status_name'] = $pay_status_data[$v['pay_status']];
  584. $v['payment_type_name'] = $payment_type_data[$v['payment_type']];
  585. $v['pay_way_name'] = $pay_way_data[$v['pay_way']];
  586. if($v['refund_status']==1){
  587. $order_refund_amount += $v['order_amount'];
  588. $refund_status = RefundRecord::where('order_id',$v['order_id'])->value('refund_status');
  589. }
  590. if($v['payment_type']!=1 and !empty($order_info['service_work']['spare_total'])){
  591. $v['order_total'] = $v['order_total'] - $order_info['service_work']['spare_total'];
  592. $v['order_amount'] = $v['order_amount'] - $order_info['service_work']['spare_total'] + $v['coupon_price'];
  593. }
  594. $coupon_price += $v['coupon_price'];
  595. $payment_type = $v['payment_type'];
  596. }
  597. $order_info['payment_type'] = $payment_type;
  598. //汇总优惠卷额度
  599. $order_info['coupon_price'] = $coupon_price;
  600. //退款汇总
  601. $order_info['refund_amount'] = $order_refund_amount;
  602. if(isset($refund_status)){
  603. switch ($refund_status){
  604. case 0:
  605. $order_info['refund_status'] = '退款中';
  606. break;
  607. case 1:
  608. $order_info['refund_status'] = '退款成功';
  609. break;
  610. case 2:
  611. $order_info['refund_status'] = '退款失败';
  612. break;
  613. }
  614. }else{
  615. $order_info['refund_status'] = '未退款';
  616. }
  617. // 所有配件
  618. $order_info['spare_total'] = $order_info['service_work']['spare_total']??0;
  619. $order_info['spare_parts'] = [];
  620. if($order_info['service_work']['service_work_spare_id']){
  621. $work_spare_parts = json_decode(ServiceWorkSpare::where('id',$order_info['service_work']['service_work_spare_id'])->value('spare_parts'),true);
  622. $spare_parts = SparePart::where('id','in',array_column($work_spare_parts,'id'))
  623. ->field(['id', 'goods_category_id', 'spare_name', 'spare_image', 'spare_number', 'spare_unit','spare_status'])
  624. ->select()
  625. ->toArray();
  626. $spare_parts = array_column($spare_parts,null,'id');
  627. foreach (array_column($work_spare_parts,null,'id') as $k=>&$v){
  628. $spare_parts[$k] = array_merge($spare_parts[$k],$v);
  629. }
  630. $order_info['spare_parts'] = array_values($spare_parts)??[];
  631. }
  632. //获取是否存在可使用优惠券
  633. $coupon_ids = CouponCategory::where('goods_category_id',$order_info['service_work']['goods_category_id'])
  634. ->whereIn('coupon_id',UserCoupon::where('user_id',$params['user_id'])
  635. ->where('voucher_count','>',0)
  636. ->where('voucher_status',0)
  637. ->where('expire_time','>',time())
  638. ->where('amount_require','<=',$order_info['order_amount'])
  639. ->column('coupon_id')
  640. )->column('coupon_id');
  641. $usable_coupon = UserCoupon::where('user_id',$params['user_id'])
  642. ->whereIn('coupon_id',$coupon_ids)
  643. ->append(['discount_ratio_text'])
  644. ->where('expire_time','>',time())
  645. ->count();
  646. $order_info['usable_coupon'] = $usable_coupon>0?true:false;
  647. return $order_info;
  648. }
  649. catch (\Exception $e) {
  650. self::setError($e->getMessage());
  651. return false;
  652. }
  653. }
  654. /**
  655. * 取消订单
  656. * @param $params
  657. * @return false|void
  658. */
  659. public static function cancelOrder($params)
  660. {
  661. Db::startTrans();
  662. try {
  663. $work_id = \app\common\model\recharge\RechargeOrder::where([
  664. 'order_type' => 0,
  665. 'user_id' => $params['user_id'],
  666. 'sn'=>$params['sn']
  667. ])->value('work_id');
  668. if(empty($work_id)){
  669. throw new Exception('订单不存在');
  670. }
  671. $payed_order = \app\common\model\recharge\RechargeOrder::where(['user_id'=>$params['user_id'],'work_id'=>$work_id,'pay_status'=>1])->findOrEmpty();
  672. if(!$payed_order->isEmpty()){
  673. throw new Exception('存在已支付订单,不允许取消订单,请联系客服');
  674. }
  675. //软删除订单
  676. $cancel_order = \app\common\model\recharge\RechargeOrder::where(['user_id'=>$params['user_id'],'work_id'=>$work_id])->findOrEmpty();
  677. $cancel_order->pay_status = 2;
  678. $cancel_order->save();
  679. //更新工单状态为已取消
  680. $service_work = ServiceWork::find($work_id);
  681. $service_work->service_status = 4;
  682. $service_work->save();
  683. //判断如果存在优惠券,返还优惠券
  684. if(!empty($payed_order->coupon_id)){
  685. $user_coupon = UserCoupon::where(['user_id'=>$payed_order->user_id,'id'=>$payed_order->coupon_id])->findOrEmpty();
  686. $user_coupon->voucher_status = 0;
  687. $user_coupon->voucher_count = $user_coupon->voucher_count+1;
  688. $user_coupon->save();
  689. }
  690. Db::commit();
  691. if($cancel_order->pay_time){
  692. // 订单取消通知【给用户】
  693. event('Notice', [
  694. 'scene_id' => 122,
  695. 'params' => [
  696. 'user_id' => $params['user_id']
  697. ]
  698. ]);
  699. }
  700. }
  701. catch (\Exception $e) {
  702. Db::rollback();
  703. self::setError($e->getMessage());
  704. return false;
  705. }
  706. }
  707. /**
  708. * 用户确认尾款报价单
  709. * @param $params
  710. * @return false|void
  711. */
  712. public static function confirmOrder($params)
  713. {
  714. Db::startTrans();
  715. try {
  716. $work_id = \app\common\model\recharge\RechargeOrder::where([
  717. 'order_type' => 0,
  718. 'user_id' => $params['user_id'],
  719. 'sn'=>$params['sn']
  720. ])->value('work_id');
  721. if(empty($work_id)){
  722. throw new Exception('订单不存在');
  723. }
  724. //更新工单状态为已取消
  725. $service_work = ServiceWork::find($work_id);
  726. if($service_work->user_confirm_status==2){
  727. throw new Exception('请勿重复操作');
  728. }
  729. $service_work->work_status = 5;
  730. $service_work->user_confirm_status = 2;
  731. $service_work->save();
  732. $work_log = [
  733. 'work_id'=>$work_id,
  734. 'master_worker_id'=>$service_work->master_worker_id,
  735. 'opera_log'=>'用户'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'确认了报价单',
  736. ];
  737. ServiceWorkLogLogic::add($work_log);
  738. Db::commit();
  739. }
  740. catch (\Exception $e) {
  741. Db::rollback();
  742. self::setError($e->getMessage());
  743. return false;
  744. }
  745. }
  746. /**
  747. * 用户确认服务完成
  748. * @param $params
  749. * @return false|void
  750. */
  751. public static function confirmServiceFinish($params)
  752. {
  753. Db::startTrans();
  754. try {
  755. $work_id = \app\common\model\recharge\RechargeOrder::where([
  756. 'order_type' => 0,
  757. 'user_id' => $params['user_id'],
  758. 'sn'=>$params['sn']
  759. ])->value('work_id');
  760. if(empty($work_id)){
  761. throw new Exception('订单不存在');
  762. }
  763. $service_work = ServiceWork::find($work_id);
  764. if($service_work->user_confirm_status!=3){
  765. throw new Exception('请勿重复操作');
  766. }
  767. $orders = RechargeOrder::where(['work_id'=>$work_id,'user_id'=>$params['user_id']])->select()->toArray();
  768. //确认所有订单总金额和结算金额
  769. //若订单是全款已支付订单
  770. if(count($orders)==1 and $orders[0]['payment_type']==0 and $orders[0]['pay_status']==1){
  771. $service_work->work_status = 7;
  772. $service_work->user_confirm_status = 5;
  773. $service_work->service_status = 3;
  774. $service_work->work_pay_status = 1;
  775. }else{
  776. $service_work->work_status = 6;
  777. $service_work->user_confirm_status = 4;
  778. }
  779. $service_work->finished_time = time();
  780. $service_work->save();
  781. //更新工程师的进行工单数量
  782. MasterWorker::setWorktotal('dec',$service_work->master_worker_id);
  783. $work_log = [
  784. 'work_id'=>$work_id,
  785. 'master_worker_id'=>$service_work->master_worker_id,
  786. 'opera_log'=>'用户'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'确认服务完成',
  787. ];
  788. ServiceWorkLogLogic::add($work_log);
  789. Db::commit();
  790. }
  791. catch (\Exception $e) {
  792. Db::rollback();
  793. self::setError($e->getMessage());
  794. return false;
  795. }
  796. }
  797. public static function approvalChangePrice($params)
  798. {
  799. Db::startTrans();
  800. try {
  801. $order = RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
  802. if($order->isEmpty()){
  803. throw new Exception('订单不存在');
  804. }
  805. $work = ServiceWork::findOrEmpty($order->work_id);
  806. if($work->isEmpty()){
  807. throw new Exception('工单不存在');
  808. }
  809. $work->work_status = 4;
  810. $work->user_confirm_status = 0;
  811. $work->price_approval = 1;
  812. $work->save();
  813. Db::commit();
  814. }
  815. catch (\Exception $e) {
  816. Db::rollback();
  817. self::setError($e->getMessage());
  818. return false;
  819. }
  820. }
  821. public static function approvalChangeAppointment($params)
  822. {
  823. Db::startTrans();
  824. try {
  825. $order = RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
  826. if($order->isEmpty()){
  827. throw new Exception('订单不存在');
  828. }
  829. $work = ServiceWork::findOrEmpty($order->work_id);
  830. if($work->isEmpty()){
  831. throw new Exception('工单不存在');
  832. }
  833. //更新工单未确认上门的状态
  834. $work->work_status = 3;
  835. $work->user_confirm_status = 0;
  836. ServiceWorkAppointmentLog::create([
  837. 'work_id'=>$work->id,
  838. 'last_appointment_time'=>strtotime($work->appointment_time),
  839. 'this_appointment_time'=>strtotime($params['appointment_time']),
  840. ]);
  841. $work->appointment_time = strtotime($params['appointment_time']);
  842. $work->appoint_approval = 1;
  843. $work->save();
  844. Db::commit();
  845. }
  846. catch (\Exception $e) {
  847. Db::rollback();
  848. self::setError($e->getMessage());
  849. return false;
  850. }
  851. return $work;
  852. }
  853. public static function approvalRefund($params)
  854. {
  855. Db::startTrans();
  856. try {
  857. $order = RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
  858. if($order->isEmpty()){
  859. throw new Exception('订单不存在');
  860. }
  861. $work = ServiceWork::findOrEmpty($order->work_id);
  862. if($work->isEmpty()){
  863. throw new Exception('工单不存在');
  864. }
  865. $work->refund_approval = 1;
  866. $work->save();
  867. //判断是否已下单时间过了两小时,并且师傅暂未上门,距离预约时间两小时以上
  868. if(($order['pay_time']+7200)>time() and $work['work_status']<4 and (strtotime($work['appointment_time'])-7200)>time()){
  869. //生成退款订单
  870. \app\common\model\recharge\RechargeOrder::update([
  871. 'id' => $order['id'],
  872. 'refund_status' => YesNoEnum::YES,
  873. ]);
  874. // 生成退款记录
  875. $recordSn = generate_sn(RefundRecord::class, 'sn');
  876. $record = RefundRecord::create([
  877. 'sn' => $recordSn,
  878. 'user_id' => $order['user_id'],
  879. 'order_id' => $order['id'],
  880. 'order_sn' => $order['sn'],
  881. 'order_type' => RefundEnum::ORDER_TYPE_ORDER,
  882. 'order_amount' => $order['order_amount'],
  883. 'refund_amount' => $order['order_amount'],
  884. 'refund_type' => RefundEnum::TYPE_ADMIN,
  885. 'transaction_id' => $order['transaction_id'] ?? '',
  886. 'refund_way' => RefundEnum::getRefundWayByPayWay($order['pay_way']),
  887. ]);
  888. //更新工单状态
  889. ServiceWork::update([
  890. 'id'=>$order['work_id'],
  891. 'service_status'=>5
  892. ]);
  893. // 退款
  894. RefundLogic::refund($order, $record['id'], $order['order_amount'], 1);
  895. }
  896. Db::commit();
  897. }
  898. catch (\Exception $e) {
  899. Db::rollback();
  900. self::setError($e->getMessage());
  901. return false;
  902. }
  903. }
  904. /**
  905. * 订单完成通知【给用户】 - 全款 -通知
  906. * @param $params
  907. * @return bool
  908. */
  909. public static function serviceFinishNotice($params)
  910. {
  911. try {
  912. $order = RechargeOrder::where('sn', $params['sn'])
  913. ->where('payment_type','=',0)
  914. ->where('pay_status','=',1)
  915. ->findOrEmpty();
  916. if(!$order->isEmpty()){
  917. event('Notice', [
  918. 'scene_id' => 120,
  919. 'params' => [
  920. 'user_id' => $order['user_id']
  921. ]
  922. ]);
  923. }
  924. return true;
  925. }catch (\Exception $e) {
  926. return false;
  927. }
  928. }
  929. /**
  930. * 工程师完单的时候设置单量规则 - 通知
  931. * @param $params
  932. * @return bool
  933. */
  934. public static function orderQuantityRule($params)
  935. {
  936. try {
  937. $work_id = \app\common\model\recharge\RechargeOrder::where([
  938. 'order_type' => 0,
  939. 'user_id' => $params['user_id'],
  940. 'sn'=>$params['sn']
  941. ])->value('work_id');
  942. if(empty($work_id)){
  943. throw new Exception('订单不存在');
  944. }
  945. $service_work = ServiceWork::find($work_id);
  946. $commission_config_id = MasterWorkerCommissionConfig::where('voucher_status','=',2)
  947. ->where('master_worker_id', $service_work['master_worker_id'])
  948. ->value('id');
  949. if($commission_config_id){
  950. // 工程师完单的时候设置该规则关闭,以及短信通知工程师
  951. $noticeRule = Db::name('master_worker_commission_notice')->alias('n')
  952. ->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')
  953. ->field([
  954. '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',
  955. Db::raw("COUNT(sw.id) AS order_count")
  956. ])
  957. ->where('n.commission_config_id', $commission_config_id)
  958. ->group('n.master_worker_id, n.id')
  959. ->select()->toArray();
  960. $is_off = true;
  961. foreach ($noticeRule as $item) {
  962. if($item['order_num'] <= $item['order_count'] && empty($item['is_notice'])){
  963. Log::info('orderQuantityRule:'.json_encode($item));
  964. MasterWorkerCommissionNotice::where('id',$item['id'])->update([
  965. 'is_notice'=>1
  966. ]);
  967. event('Notice', [
  968. 'scene_id' => 128,
  969. 'params' => [
  970. 'user_id' => $service_work['master_worker_id'],
  971. 'number1' => $item['day_num'],
  972. 'number' => $item['order_num']
  973. ]
  974. ]);
  975. }
  976. if($item['order_num'] > $item['order_count']){
  977. $is_off = false;
  978. }
  979. }
  980. if($is_off){
  981. MasterWorkerCommissionConfig::where('id',$commission_config_id)->update([
  982. 'voucher_status'=>1
  983. ]);
  984. }
  985. }
  986. return true;
  987. }catch (\Exception $e) {
  988. Log::info('orderQuantityRule:'.$e->getMessage());
  989. return false;
  990. }
  991. }
  992. /**
  993. * 提交订单
  994. * @param array $params
  995. * @return array|false
  996. */
  997. public static function firmSubmitOrder($params)
  998. {
  999. Db::startTrans();
  1000. try {
  1001. $goods = Goods::findOrEmpty($params['goods_id']);
  1002. if($goods->isEmpty()){
  1003. throw new Exception('产品不存在!');
  1004. }
  1005. if(empty($params['user_info']['mobile'])){
  1006. throw new Exception('请先补充您的联系方式后在提交订单');
  1007. }
  1008. //根据服务工单计算当前订单应支付金额
  1009. if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE){
  1010. //一口价订单
  1011. $order_total = $goods['service_fee'];
  1012. $order_amount = $goods['service_fee'];
  1013. }else if ($goods['goods_payment_type'] == GoodsEnum::DEP_GOODS_PAYMENT_TYPE){
  1014. $order_total = $goods['service_fee'];
  1015. $order_amount = $goods['service_fee'];
  1016. }
  1017. else{
  1018. $order_total = $goods['base_service_fee'];
  1019. $order_amount = $goods['service_fee'];
  1020. }
  1021. //生成服务工单
  1022. $work_data = [
  1023. 'work_sn' => generate_sn(ServiceWork::class, 'work_sn'),
  1024. 'real_name' => $params['contact_people'],
  1025. 'mobile' => $params['contact_number'],
  1026. 'address' => $params['address'],
  1027. 'title' => $goods->goods_name . '*' . $goods->goods_number.$goods->good_unit,
  1028. 'category_type' => $goods['category_type'],
  1029. 'goods_category_ids' => $goods['goods_category_ids'],
  1030. 'goods_category_id' => $goods['goods_category_id'],
  1031. 'base_service_fee' => $goods['base_service_fee'],
  1032. 'service_fee' => $goods['service_fee'],
  1033. 'work_pay_status'=>WorkEnum::UN_PAY_STATUS,
  1034. 'work_type'=> 1,
  1035. 'appointment_time' => strtotime($params['appointment_time']),
  1036. 'user_id'=>$params['user_id'],
  1037. 'lon'=>!empty($params['lon'])?$params['lon']:0,
  1038. 'lat'=>!empty($params['lat'])?$params['lat']:0,
  1039. ];
  1040. $service_work = ServiceWork::create($work_data);
  1041. //生成服务订单
  1042. $data = [
  1043. 'work_id'=> $service_work['id'],
  1044. 'sn' => generate_sn(RechargeOrder::class, 'sn'),
  1045. 'order_type'=>0,//服务订单
  1046. 'order_terminal' => $params['terminal'],
  1047. 'payment_type'=>$goods['goods_payment_type']==GoodsEnum::ISGOODS_PAYMENT_TYPE?0:1,
  1048. 'user_id' => $params['user_id'],
  1049. 'pay_status' => PayEnum::UNPAID,
  1050. 'coupon_id'=>!empty($params['coupon_id'])?$params['coupon_id']:0,
  1051. 'coupon_price'=>!empty($order_coupon_amount)?$order_coupon_amount:0,
  1052. 'pay_way' => $params['pay_way'],
  1053. 'order_total' => $order_total,
  1054. 'order_amount' => $order_amount,
  1055. ];
  1056. $order = RechargeOrder::create($data);
  1057. //生成订单服务详情
  1058. OrderGoods::create([
  1059. 'sn' => $order['sn'],
  1060. 'goods_id' => $params['goods_id'],
  1061. 'category_type' => $goods['category_type'],
  1062. 'goods_category_ids' => $goods['goods_category_ids'],
  1063. 'goods_category_id' => $goods['goods_category_id'],
  1064. 'goods_name' => $goods['goods_name'],
  1065. 'goods_image' => $goods['goods_image'],
  1066. 'goods_video' => $goods['goods_video'],
  1067. 'goods_number' => $goods['goods_number'],
  1068. 'good_unit' => $goods['good_unit'],
  1069. 'goods_size' => $goods['goods_size'],
  1070. 'goods_type' => $goods['goods_type'],
  1071. 'goods_brand' => $goods['goods_brand'],
  1072. 'install_guide' => $goods['install_guide'],
  1073. 'goods_payment_type'=>$goods['goods_payment_type'],
  1074. 'base_service_fee' => $goods['base_service_fee'],
  1075. 'service_total' => $goods['service_total'],
  1076. 'service_fee' => $goods['service_fee'],
  1077. 'service_image' => $goods['service_image'],
  1078. 'warranty_period'=>$goods['warranty_period'],
  1079. 'fee_schedule' => $goods['fee_schedule'],
  1080. 'goods_status' => $goods['goods_status'],
  1081. ]);
  1082. Db::commit();
  1083. } catch (\Exception $e) {
  1084. Db::rollback();
  1085. self::setError($e->getMessage());
  1086. return false;
  1087. }
  1088. return [
  1089. 'order_id' => (int)$order['id'],
  1090. 'work_id' => (int)$order['work_id'],
  1091. ];
  1092. }
  1093. /**
  1094. * 绑定工程师-设置为加单
  1095. * * @param array $params
  1096. * * @return array|false
  1097. */
  1098. public static function bindWorkerAndWork(array $params)
  1099. {
  1100. Db::startTrans();
  1101. try {
  1102. $order = RechargeOrder::where(['sn'=>$params['sn'],'user_id'=>$params['user_id']])->findOrEmpty();
  1103. if($order->isEmpty()){
  1104. throw new Exception('订单不存在');
  1105. }
  1106. $work = ServiceWork::where(['id'=>$order['work_id']])->findOrEmpty();
  1107. if($work->isEmpty()){
  1108. throw new Exception('工单不存在');
  1109. }
  1110. if($work['work_status']!=WorkEnum::WORK_STATUS_WAIT_SERVICE){
  1111. throw new Exception('工单状态不正确,无法绑定');
  1112. }
  1113. $worker = MasterWorker::where('id',$params['worker_id'])->findOrEmpty();
  1114. if($worker->isEmpty()){
  1115. throw new Exception('工程师不存在');
  1116. }
  1117. $work->master_worker_id = $params['worker_id'];
  1118. $work->work_status = WorkEnum::WORK_STATUS_WORKER;
  1119. $work->receive_time = time();
  1120. $work->work_type = 2;//加单状态
  1121. $work->save();
  1122. Db::commit();
  1123. } catch (\Exception $e) {
  1124. Db::rollback();
  1125. self::setError($e->getMessage());
  1126. return false;
  1127. }
  1128. }
  1129. /**
  1130. * 0元支付直接完成支付
  1131. * @param array $params attach out_trade_no
  1132. * @return bool|void
  1133. */
  1134. public static function zeroDirectlyPayment(array $params, $extra = [])
  1135. {
  1136. try {
  1137. $message['out_trade_no'] = mb_substr($params['out_trade_no'], 0, 18);
  1138. $order = RechargeOrder::where(['sn' => $params['out_trade_no']])->findOrEmpty();
  1139. if($order->isEmpty() || $order->pay_status == PayEnum::ISPAID) {
  1140. return true;
  1141. }
  1142. switch ($params['attach']) {
  1143. case 'recharge':
  1144. PayNotifyLogic::handle('recharge', $params['out_trade_no'], $extra);
  1145. break;
  1146. case 'goods':
  1147. $res = PayNotifyLogic::handle('goods', $params['out_trade_no'], $extra);
  1148. if($res === true){
  1149. // 用户下单后,给订单运营专员(配置固定ID)发送公众号提醒(订单信息)
  1150. $order = RechargeOrder::where('sn', $message['out_trade_no'])
  1151. ->where('payment_type','IN',[0,1])
  1152. ->where('pay_status','=',1)
  1153. ->findOrEmpty();
  1154. if(!$order->isEmpty()){
  1155. $workDetail = ServiceWork::findOrEmpty($order->work_id);
  1156. if(!$workDetail->isEmpty()){
  1157. event('Notice', [
  1158. 'scene_id' => 100,
  1159. 'params' => [
  1160. 'user_id' => 0,
  1161. 'order_id' => $workDetail['id'],
  1162. 'thing3' => $workDetail['title'],
  1163. 'time6' => $workDetail['appointment_time'],
  1164. 'phone_number8' => asteriskString($workDetail['mobile']),
  1165. 'thing5' => (iconv_strlen($workDetail['address'])>15)?(mb_substr($workDetail['address'],0,15,'UTF-8').'...'):$workDetail['address'],
  1166. ]
  1167. ]);
  1168. }
  1169. }
  1170. // 订单完成通知【给用户】 - 尾款 -通知
  1171. $order = RechargeOrder::where('sn', $message['out_trade_no'])
  1172. ->where('payment_type','=',2)
  1173. ->where('pay_status','=',1)
  1174. ->findOrEmpty();
  1175. if(!$order->isEmpty()){
  1176. event('Notice', [
  1177. 'scene_id' => 120,
  1178. 'params' => [
  1179. 'user_id' => $order['user_id']
  1180. ]
  1181. ]);
  1182. }
  1183. }
  1184. break;
  1185. }
  1186. return true;
  1187. } catch (\Exception $e) {
  1188. self::setError($e->getMessage());
  1189. return false;
  1190. }
  1191. }
  1192. }