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

258 lines
7.4 KiB

4 years ago
4 years ago
4 years ago
4 years ago
5 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. $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,sum(service_persons) as sum_persons')
  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. //}
  151. $supplier = Supplier::query()->where('id', $v->supplier_id)->lockForUpdate()->first();
  152. //处理交易金
  153. if ($order->single_price > 0) {
  154. //计算交易金
  155. $deposit = bcmul($order->single_price,$v->sum_persons,6);
  156. //流水
  157. $statementCreate[] = $service->createByOrder(
  158. $deposit,
  159. StatementType::DEPOSIT,
  160. $v->supplier_id,
  161. DemandTraits::$col[1],
  162. $order->id,
  163. StatementTraits::$type[0]
  164. );
  165. //扣
  166. $supplierPrice = bcsub($supplierPrice,$deposit,6);
  167. $supplier->balance = bcadd($supplier->deposit_used, $supplierPrice, 6);
  168. $supplier->balance = bcsub($supplier->deposit_frozen, $supplierPrice, 6);
  169. }
  170. $supplier->balance = bcadd($supplier->balance, $supplierPrice, 6);
  171. $supplier->save();
  172. }
  173. //分账给代理商
  174. //成本价 加上地接价格
  175. $agentPrice = bcsub($order->price, $cost, 2);
  176. $statementCreate[] = $service->createByOrder(
  177. $agentPrice,
  178. StatementType::ORDER,
  179. $order->agent_id,
  180. DemandTraits::$col[0],
  181. $order->id,
  182. StatementTraits::$type[0]
  183. );
  184. //
  185. ////抽成
  186. //if ($order->agent->rate > 0) {
  187. // //计算抽成金额
  188. // $agentCut = bcmul($agentPrice, $order->agent->rate, 6);
  189. // $cutPrice = $agentCut > 0 ? bcdiv($agentCut, 100, 6) : 0;
  190. //
  191. // //总后台抽成流水
  192. // if ($cutPrice > 0) {
  193. // $adminCreate[] = $service->createByOrderFormAdmin(
  194. // $cutPrice,
  195. // StatementType::CUT,
  196. // $order->agent->id,
  197. // DemandTraits::$col[0],
  198. // $order->id,
  199. // );
  200. // //代理商被抽成流水
  201. // $statementCreate[] = $service->createByOrder(
  202. // bcmul($cutPrice, -1, 6),
  203. // StatementType::CUT,
  204. // $order->agent->id,
  205. // DemandTraits::$col[0],
  206. // $order->id,
  207. // StatementTraits::$type[0]
  208. // );
  209. // $agentPrice = bcsub($agentPrice, $cutPrice, 6);
  210. // }
  211. //扣除微信支付手续费
  212. //$chargePrice = bcmul($order->price, 0.006, 6);
  213. //$statementCreate[] = $service->createByOrder(
  214. // bcmul($chargePrice, -1, 6),
  215. // StatementType::CHARGE,
  216. // $order->agent_id,
  217. // DemandTraits::$col[0],
  218. // $order->id,
  219. // StatementTraits::$type[0]
  220. //);
  221. //$agentPrice = bcsub($agentPrice, $chargePrice, 6);
  222. $agent = Agent::query()->where('id', $order->agent->id)->lockForUpdate()->first();
  223. $agent->balance = bcadd($agent->balance, $agentPrice, 6);
  224. $agent->save();
  225. //}
  226. //if (!empty($adminCreate)) {
  227. // $order->statementAdmin()->createMany($adminCreate);
  228. //}
  229. if (!empty($statementCreate)) {
  230. $order->statement()->createMany($statementCreate);
  231. }
  232. DB::commit();
  233. } catch (\Exception $e) {
  234. DB::rollBack();
  235. return $this->error($e->getMessage());
  236. }
  237. }
  238. }