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

239 lines
6.6 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
  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, OrderStatus::REFUSED_REFUND])
  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. return $this->success();
  54. }
  55. public function fund($order)
  56. {
  57. $service = new WithdrawalService();
  58. DB::beginTransaction();
  59. try {
  60. //最后批量插入
  61. $adminCreate = $statementCreate = [];
  62. $cost = 0;
  63. //如果有地接价格 分帐给地接
  64. if ($order->guide_price > 0) {
  65. $guidePrice = $order->guide_price;
  66. $cost = bcadd($cost, $order->guide_price, 6);
  67. //成本价 加上地接价格
  68. $statementCreate[] = $service->createByOrder(
  69. $order->guide_price,
  70. StatementType::ORDER,
  71. $order->guide->id,
  72. DemandTraits::$col[2],
  73. $order->id,
  74. StatementTraits::$type[0]
  75. );
  76. //抽成
  77. if ($order->guide->rate > 0) {
  78. //计算抽成金额
  79. $guideCut = bcmul($order->guide_price, $order->guide->rate, 6);
  80. $cutPrice = $guideCut > 0 ? bcdiv($guideCut, 100, 6) : 0;
  81. //总后台抽成流水
  82. if ($cutPrice > 0) {
  83. $adminCreate[] = $service->createByOrderFormAdmin(
  84. $cutPrice,
  85. StatementType::CUT,
  86. $order->guide->id,
  87. DemandTraits::$col[2],
  88. $order->id,
  89. );
  90. //地接被抽成流水
  91. $statementCreate[] = $service->createByOrder(
  92. bcmul($cutPrice, -1, 2),
  93. StatementType::CUT,
  94. $order->guide->id,
  95. DemandTraits::$col[2],
  96. $order->id,
  97. StatementTraits::$type[0]
  98. );
  99. $guidePrice = bcsub($order->guide_price, $cutPrice, 6);
  100. $guide = Guide::query()->where('id', $order->guide->id)->lockForUpdate()->first();
  101. $guide->balance = bcadd($guide->balance, $guidePrice, 6);
  102. $guide->save();
  103. }
  104. }
  105. }
  106. //分账给供应商
  107. $orderItem = OrderProductItem::query()
  108. ->where('order_id', $order->id)
  109. ->with('supplier')
  110. ->select('*')
  111. ->selectRaw('sum(price) as sum_price')
  112. ->groupBy('supplier_id')
  113. ->get();
  114. foreach ($orderItem as $v) {
  115. $cost = bcadd($cost, $v->sum_price, 6);
  116. $supplierPrice = $v->sum_price;
  117. $statementCreate[] = $service->createByOrder(
  118. $v->sum_price,
  119. StatementType::ORDER,
  120. $v->supplier_id,
  121. DemandTraits::$col[1],
  122. $order->id,
  123. StatementTraits::$type[0]
  124. );
  125. if ($v->supplier->rate > 0) {
  126. //计算抽成金额
  127. $supplierCut = bcmul($v->sum_price, $v->supplier->rate, 6);
  128. $cutPrice = $supplierCut > 0 ? bcdiv($supplierCut, 100, 6) : 0;
  129. if ($cutPrice > 0) {
  130. //总后台抽成流水
  131. $adminCreate[] = $service->createByOrderFormAdmin(
  132. $cutPrice,
  133. StatementType::CUT,
  134. $v->supplier_id,
  135. DemandTraits::$col[1],
  136. $order->id,
  137. );
  138. //供应商被抽成流水
  139. $statementCreate[] = $service->createByOrder(
  140. bcmul($cutPrice, -1, 6),
  141. StatementType::CUT,
  142. $v->supplier_id,
  143. DemandTraits::$col[1],
  144. $order->id,
  145. StatementTraits::$type[0]
  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[] = $service->createByOrder(
  159. $agentPrice,
  160. StatementType::ORDER,
  161. $order->agent_id,
  162. DemandTraits::$col[0],
  163. $order->id,
  164. StatementTraits::$type[0]
  165. );
  166. //抽成
  167. if ($order->agent->rate > 0) {
  168. //计算抽成金额
  169. $agentCut = bcmul($agentPrice, $order->agent->rate, 6);
  170. $cutPrice = $agentCut > 0 ? bcdiv($agentCut, 100, 6) : 0;
  171. //总后台抽成流水
  172. if ($cutPrice > 0) {
  173. $adminCreate[] = $service->createByOrderFormAdmin(
  174. $cutPrice,
  175. StatementType::CUT,
  176. $order->agent->id,
  177. DemandTraits::$col[0],
  178. $order->id,
  179. );
  180. //代理商被抽成流水
  181. $statementCreate[] = $service->createByOrder(
  182. bcmul($cutPrice, -1, 6),
  183. StatementType::CUT,
  184. $order->agent->id,
  185. DemandTraits::$col[0],
  186. $order->id,
  187. StatementTraits::$type[0]
  188. );
  189. $agentPrice = bcsub($agentPrice, $cutPrice, 6);
  190. }
  191. }
  192. //扣除微信支付手续费
  193. $chargePrice = bcmul($order->price, 0.006, 6);
  194. $statementCreate[] = $service->createByOrder(
  195. bcmul($chargePrice, -1, 6),
  196. StatementType::CHARGE,
  197. $order->agent_id,
  198. DemandTraits::$col[0],
  199. $order->id,
  200. StatementTraits::$type[0]
  201. );
  202. $agentPrice = bcsub($agentPrice, $chargePrice, 6);
  203. $agent = Agent::query()->where('id', $order->agent->id)->lockForUpdate()->first();
  204. $agent->balance = bcadd($agent->balance, $agentPrice, 6);
  205. $agent->save();
  206. }
  207. if (!empty($adminCreate)) {
  208. $order->statementAdmin()->createMany($adminCreate);
  209. }
  210. if (!empty($statementCreate)) {
  211. $order->statement()->createMany($statementCreate);
  212. }
  213. DB::commit();
  214. } catch (\Exception $e) {
  215. DB::rollBack();
  216. return $this->error($e->getMessage());
  217. }
  218. }
  219. }