海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
7.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Common\StatementType;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\Agent;
  6. use App\Models\Guide;
  7. use App\Models\Order;
  8. use App\Models\Supplier;
  9. use App\Models\Product;
  10. use App\Models\OrderProductItem;
  11. use App\Models\User;
  12. use App\Common\OrderStatus;
  13. use App\Service\SmsService;
  14. use App\Traits\DemandTraits;
  15. use App\Traits\SmsTraits;
  16. use EasyWeChat\Factory;
  17. use Illuminate\Support\Facades\DB;
  18. use Illuminate\Support\Facades\Storage;
  19. class VerificationController extends Controller
  20. {
  21. //核销订单
  22. public function verify()
  23. {
  24. $input_verify_code = request()->input('verify_code'); //订单ID
  25. $code_arr = explode('-', $input_verify_code);
  26. if (count($code_arr) != 2) {
  27. return $this->error('参数错误');
  28. }
  29. list($id, $verify_code) = $code_arr;
  30. $order = Order::with(['agentProduct:id,verifier', 'user', 'agent', 'guide'])
  31. ->where(['verify_code' => $verify_code])
  32. ->whereIn('status', [OrderStatus::PAID, OrderStatus::PAID_RETAINAGE, OrderStatus::OFFLINE_PAID])
  33. ->find($id);
  34. if (!$order) {
  35. return $this->error('订单不存在或订单状态不允许核销');
  36. }
  37. //$mobile = User::where('id', $this->user_id)->value('mobile');
  38. //
  39. //$checkMobile = Product::query()->whereIn('id', explode(',', $order->product_ids))->where('verify_mobile', $mobile)->doesntExist();
  40. //if ($checkMobile) {
  41. // return $this->error('对不起,你没有核销权限,请联系管理员');
  42. //}
  43. $order->status = OrderStatus::SUCCESS;
  44. if ($order->save()) {
  45. //分账
  46. $this->fund($order);
  47. //短信
  48. if (env('SMS_SWITCH', '') == true) {
  49. if (!empty($order->user->mobile)) {
  50. (new SmsService)->send('verify', [$order->order_no, SmsTraits::$systeaNameText['user']], [$order->user->mobile]);//用户
  51. }
  52. $supplierIds = OrderProductItem::query()->with('supplier')->where('order_id', $order->id)->distinct()->pluck('supplier_id');
  53. $phone = Supplier::query()->whereIn('id', $supplierIds)->pluck('contact_phone')->toArray();
  54. (new SmsService)->send('verify', [$order->order_no, SmsTraits::$systeaNameText['supplier']], $phone);//供应商
  55. (new SmsService)->send('verify', [$order->order_no, SmsTraits::$systeaNameText['agent']], [$order->agent->contact_phone]);//代理商
  56. }
  57. }
  58. return $this->success();
  59. }
  60. public function fund($order)
  61. {
  62. DB::beginTransaction();
  63. try {
  64. //最后批量插入
  65. $adminCreate = $statementCreate = [];
  66. $cost = 0;
  67. //如果有地接价格 分帐给地接
  68. if ($order->guide_price > 0) {
  69. $guidePrice = $order->guide_price;
  70. $cost = bcadd($cost, $order->guide_price, 6);
  71. //成本价 加上地接价格
  72. $statementCreate[] = [
  73. 'price' => $order->guide_price,
  74. 'type' => StatementType::ORDER,
  75. 'user_id' => $order->guide->id,
  76. 'user_type' => DemandTraits::$col[2],
  77. 'order_id' => $order->id
  78. ];
  79. //抽成
  80. if ($order->guide->rate > 0) {
  81. //计算抽成金额
  82. $guideCut = bcmul($order->guide_price, $order->guide->rate, 6);
  83. $cutPrice = $guideCut > 0 ? bcdiv($guideCut, 100, 6) : 0;
  84. //总后台抽成流水
  85. if ($cutPrice > 0) {
  86. $adminCreate[] = [
  87. 'price' => $cutPrice,
  88. 'type' => StatementType::CUT,
  89. 'cut_user_id' => $order->guide->id,
  90. 'cut_user_type' => DemandTraits::$col[2],
  91. 'order_id' => $order->id
  92. ];
  93. //地接被抽成流水
  94. $statementCreate[] = [
  95. 'price' => bcmul($cutPrice, -1, 2),
  96. 'type' => StatementType::CUT,
  97. 'user_id' => $order->guide->id,
  98. 'user_type' => DemandTraits::$col[2],
  99. 'order_id' => $order->id
  100. ];
  101. $guidePrice = bcsub($order->guide_price, $cutPrice, 6);
  102. $guide = Guide::query()->where('id', $order->guide->id)->lockForUpdate()->first();
  103. $guide->balance = bcadd($guide->balance, $guidePrice, 6);
  104. $guide->save();
  105. }
  106. }
  107. }
  108. //分账给供应商
  109. $orderItem = OrderProductItem::query()
  110. ->where('order_id', $order->id)
  111. ->with('supplier')
  112. ->select('*')
  113. ->selectRaw('sum(price) as sum_price')
  114. ->groupBy('supplier_id')
  115. ->get();
  116. foreach ($orderItem as $v) {
  117. $cost = bcadd($cost, $v->sum_price, 6);
  118. $supplierPrice = $v->sum_price;
  119. $statementCreate[] = [
  120. 'price' => $v->sum_price,
  121. 'type' => StatementType::ORDER,
  122. 'user_id' => $v->supplier_id,
  123. 'user_type' => DemandTraits::$col[1],
  124. 'order_id' => $order->id
  125. ];
  126. if ($v->supplier->rate > 0) {
  127. //计算抽成金额
  128. $supplierCut = bcmul($v->sum_price, $v->supplier->rate, 6);
  129. $cutPrice = $supplierCut > 0 ? bcdiv($supplierCut, 100, 6) : 0;
  130. if ($cutPrice > 0) {
  131. //总后台抽成流水
  132. $adminCreate[] = [
  133. 'price' => $cutPrice,
  134. 'type' => StatementType::CUT,
  135. 'cut_user_id' => $v->supplier_id,
  136. 'cut_user_type' => DemandTraits::$col[1],
  137. 'order_id' => $order->id
  138. ];
  139. //供应商被抽成流水
  140. $statementCreate[] = [
  141. 'price' => bcmul($cutPrice, -1, 6),
  142. 'type' => StatementType::CUT,
  143. 'user_id' => $v->supplier_id,
  144. 'user_type' => DemandTraits::$col[1],
  145. 'order_id' => $order->id
  146. ];
  147. $supplierPrice = bcsub($supplierPrice, $cutPrice, 6);
  148. }
  149. }
  150. $supplier = Supplier::query()->where('id', $v->supplier_id)->lockForUpdate()->first();
  151. $supplier->balance = bcadd($supplier->balance, $supplierPrice, 6);
  152. $supplier->save();
  153. }
  154. //分账给代理商
  155. //成本价 加上地接价格
  156. $agentPrice = bcsub($order->price, $cost, 2);
  157. if ($agentPrice > 0) {
  158. $statementCreate[] = [
  159. 'price' => $agentPrice,
  160. 'type' => StatementType::ORDER,
  161. 'user_id' => $order->agent_id,
  162. 'user_type' => DemandTraits::$col[0],
  163. 'order_id' => $order->id
  164. ];
  165. //抽成
  166. if ($order->agent->rate > 0) {
  167. //计算抽成金额
  168. $agentCut = bcmul($agentPrice, $order->agent->rate, 6);
  169. $cutPrice = $agentCut > 0 ? bcdiv($agentCut, 100, 6) : 0;
  170. //总后台抽成流水
  171. if ($cutPrice > 0) {
  172. $adminCreate[] = [
  173. 'price' => $cutPrice,
  174. 'type' => StatementType::CUT,
  175. 'cut_user_id' => $order->agent->id,
  176. 'cut_user_type' => DemandTraits::$col[0],
  177. 'order_id' => $order->id
  178. ];
  179. //代理商被抽成流水
  180. $statementCreate[] = [
  181. 'price' => bcmul($cutPrice, -1, 6),
  182. 'type' => StatementType::CUT,
  183. 'user_id' => $order->agent->id,
  184. 'user_type' => DemandTraits::$col[0],
  185. 'order_id' => $order->id
  186. ];
  187. $agentPrice = bcsub($agentPrice, $cutPrice, 6);
  188. }
  189. }
  190. //扣除微信支付手续费
  191. $chargePrice = bcmul($order->price, 0.006, 6);
  192. $statementCreate[] = [
  193. 'price' => bcmul($chargePrice, -1, 6),
  194. 'type' => StatementType::CHARGE,
  195. 'user_id' => $order->agent_id,
  196. 'user_type' => DemandTraits::$col[0],
  197. 'order_id' => $order->id
  198. ];
  199. $agentPrice = bcsub($agentPrice, $chargePrice, 6);
  200. $agent = Agent::query()->where('id', $order->agent->id)->lockForUpdate()->first();
  201. $agent->balance = bcadd($agent->balance, $agentPrice, 6);
  202. $agent->save();
  203. }
  204. //dd($adminCreate,$guideCreate);
  205. if (!empty($adminCreate)) {
  206. $order->statementAdmin()->createMany($adminCreate);
  207. }
  208. if (!empty($statementCreate)) {
  209. $order->statement()->createMany($statementCreate);
  210. }
  211. DB::commit();
  212. } catch (\Exception $e) {
  213. DB::rollBack();
  214. return $this->error($e->getMessage());
  215. }
  216. }
  217. }