海南旅游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.

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