PropertyCommissionLogic.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace app\api\logic;
  3. use app\common\enum\GoodsEnum;
  4. use app\common\enum\PayEnum;
  5. use app\common\enum\WorkEnum;
  6. use app\common\logic\BaseLogic;
  7. use app\common\model\coupon\UserCoupon;
  8. use app\common\model\dict\DictData;
  9. use app\common\model\goods\Goods;
  10. use app\common\model\master_worker\MasterWorker;
  11. use app\common\model\orders\RechargeOrder;
  12. use app\common\model\property\PropertyCommission;
  13. use app\common\model\property\PropertyHead;
  14. use app\common\model\property\PropertyOrder;
  15. use app\common\model\property\PropertySurplusLog;
  16. use app\common\model\property\PropertyUser;
  17. use app\common\model\recharge\OrderGoods;
  18. use app\common\model\works\ServiceWork;
  19. use app\workerapi\logic\ServiceWorkLogLogic;
  20. use think\Exception;
  21. use think\facade\Db;
  22. /**
  23. * 物业分成收益逻辑
  24. * Class PropertyCommissionLogic
  25. * @package app\api\logic
  26. */
  27. class PropertyCommissionLogic extends BaseLogic
  28. {
  29. /**
  30. * 确认服务完成物业分成
  31. * @param $params
  32. * @return false|true
  33. */
  34. public static function commissionSurplus($params)
  35. {
  36. Db::startTrans();
  37. try {
  38. $work_id = $params['id'];
  39. // 判断工单是否用户已完结
  40. $service_work = ServiceWork::find($work_id);
  41. if($service_work->user_confirm_status!=5){
  42. throw new Exception('未完结订单,无法完成分成');
  43. }
  44. $propertyUserId = PropertyUser::where('user_id',$service_work->user_id)->value('id');
  45. // 非物业负责人-无需分成
  46. if(empty($propertyUserId)){
  47. throw new Exception("非物业负责人,该工单无需物业分成:".json_encode($service_work->toArray()));
  48. }
  49. $isPropertyOrder = PropertyOrder::where(['property_user_id' => $propertyUserId,'work_id'=>$work_id])->findOrEmpty();
  50. if($isPropertyOrder->isEmpty()){
  51. throw new Exception("非物业单:".json_encode(['property_user_id' => $propertyUserId,'work_id'=>$work_id]));
  52. }
  53. // 以工单服务费-优惠费 为基准,计算物业分成金额
  54. $coupon_price = RechargeOrder::where('work_id',$work_id)->sum('coupon_price');
  55. $work_amount = $service_work->service_fee - $coupon_price;
  56. $propertyOrderInfo = PropertyOrder::where(['property_user_id' => $propertyUserId,'order_status' => 1,'work_id'=>$work_id])->findOrEmpty();
  57. if($propertyOrderInfo->isEmpty()){
  58. throw new Exception("未接单不存在:".json_encode(['property_user_id' => $propertyUserId,'work_id'=>$work_id]));
  59. }
  60. $propertyOrderInfo = $propertyOrderInfo->toArray();
  61. //计算分成金额
  62. $ratio = PropertyHead::where('id',$propertyOrderInfo['property_head_id'])->value('ratio');
  63. $commission_amount = bcmul($work_amount, bcdiv($ratio, 100, 4),2);
  64. // 添加分成记录
  65. $propertyCommissionInfo = PropertyCommission::create([
  66. 'property_head_id' => $propertyOrderInfo['property_head_id'],
  67. 'property_user_id' => $propertyUserId,
  68. 'property_order_id' => $propertyOrderInfo['id'],
  69. 'work_id' => $work_id,
  70. 'order_amount' => $work_amount,
  71. 'ratio' => $ratio,
  72. 'commission_amount' => $commission_amount,
  73. ]);
  74. // 更新物业收益/订单状态已完结
  75. PropertyHead::where(['id' => $propertyOrderInfo['property_head_id']])->update([
  76. 'all_profit_amount' => Db::raw('all_profit_amount+'.$commission_amount),
  77. 'surplus_profit_amount' => Db::raw('surplus_profit_amount+'.$commission_amount)
  78. ]);
  79. PropertyOrder::where(['id' => $propertyOrderInfo['id']])->update(['order_status' => 3]);
  80. // 进出账记录
  81. PropertySurplusLog::create([
  82. 'in_out' => 1,
  83. 'property_head_id' => $propertyOrderInfo['property_head_id'],
  84. 'property_commission_id' => $propertyCommissionInfo['id'],
  85. 'amount' => $commission_amount,
  86. 'status' => 1,
  87. 'remark' => '订单完成,分成金额入账'
  88. ]);
  89. Db::commit();
  90. return true;
  91. }catch (\Exception $e) {
  92. Db::rollback();
  93. self::setError($e->getMessage());
  94. return false;
  95. }
  96. }
  97. /**
  98. * 物业负责人用户申请提现
  99. * @param $params
  100. * @return false|true
  101. */
  102. public static function withdrawCash($params)
  103. {
  104. Db::startTrans();
  105. try {
  106. //提现状态0无申请中 1有申请中
  107. $isId = PropertySurplusLog::where(['in_out' => 2,'status' => 0])->value('id');
  108. if($isId){
  109. throw new Exception('存在申请中订单,不可重复申请提现');
  110. }
  111. $propertyHeadInfo = PropertyHead::where('user_id',$params['user_id'])->findOrEmpty();
  112. if($propertyHeadInfo->isEmpty()){
  113. throw new Exception('物业负责人不存在');
  114. }
  115. $propertyHeadInfo = $propertyHeadInfo->toArray();
  116. $propertyHeadId = $propertyHeadInfo['id'];
  117. $surplusProfitAmount = $propertyHeadInfo['surplus_profit_amount'];
  118. if(($surplusProfitAmount - $params['amount']) < 0){
  119. throw new Exception('余额不足');
  120. }
  121. // 出账记录 - 提现
  122. PropertySurplusLog::create([
  123. 'in_out' => 2,
  124. 'property_head_id' => $propertyHeadId,
  125. 'amount' => $params['amount'],
  126. 'status' => 0
  127. ]);
  128. // 更新 物业负责人余额收益
  129. PropertyHead::where(['id' => $propertyHeadId])->update([
  130. 'surplus_profit_amount' => Db::raw('surplus_profit_amount-'.$params['amount'])
  131. ]);
  132. Db::commit();
  133. return true;
  134. }catch (\Exception $e) {
  135. Db::rollback();
  136. self::setError($e->getMessage());
  137. return false;
  138. }
  139. }
  140. /**
  141. * 物业负责人用户申请提现
  142. * @param $params
  143. * @return false|true
  144. */
  145. public static function assetAccount($params) : array
  146. {
  147. $propertyHeadInfo = PropertyHead::where('user_id',$params['user_id'])->findOrEmpty();
  148. if($propertyHeadInfo->isEmpty()){
  149. self::setError('物业负责人不存在');
  150. return [];
  151. }
  152. $resData = $propertyHeadInfo->toArray();
  153. $resData['surplus_status'] = 0;//提现状态0无申请中 1有申请中
  154. $isId = PropertySurplusLog::where(['in_out' => 2,'status' => 0])->value('id');
  155. if($isId){
  156. $resData['surplus_status'] = 1;
  157. }
  158. return $resData;
  159. }
  160. }