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

303 lines
8.8 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
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\IndustryOrder;
  9. use App\Models\Order;
  10. use App\Models\Supplier;
  11. use App\Models\Product;
  12. use App\Models\OrderProductItem;
  13. use App\Models\User;
  14. use App\Common\OrderStatus;
  15. use App\Service\WithdrawalService;
  16. use App\Traits\DemandTraits;
  17. use App\Traits\StatementTraits;
  18. use Illuminate\Support\Facades\DB;
  19. /**
  20. * 订单核销
  21. * Class VerificationController
  22. * @package App\Http\Controllers\Api
  23. */
  24. class VerificationController extends Controller
  25. {
  26. //核销小程序订单
  27. public function verify()
  28. {
  29. $input_verify_code = request()->input('verify_code'); //核销码
  30. $code_arr = explode('-', $input_verify_code);
  31. if (count($code_arr) != 2) {
  32. return $this->error('参数错误');
  33. }
  34. list($id, $verify_code) = $code_arr;
  35. $order = Order::where(['verify_code' => $verify_code])
  36. ->whereIn('status', [OrderStatus::PAID, OrderStatus::PAID_RETAINAGE, OrderStatus::OFFLINE_PAID, OrderStatus::REFUSED_REFUND])
  37. ->find($id);
  38. if (!$order) {
  39. return $this->error($input_verify_code . "核销码不存在或订单状态不允许核销");
  40. }
  41. $mobile = User::where('id', $this->user_id)->value('mobile');
  42. $checkMobile = Product::query()->whereIn('id', explode(',', $order->product_ids))->where('verify_mobile', $mobile)->doesntExist();
  43. if ($checkMobile) {
  44. return $this->error('对不起,你没有核销权限,请联系管理员');
  45. }
  46. $order->status = OrderStatus::SUCCESS;
  47. if ($order->save()) {
  48. //分账
  49. //线下订单不分账
  50. if ($order->pay_type != PayType::OFFLINE) {
  51. $this->fund($order);
  52. }
  53. }
  54. return $this->success();
  55. }
  56. //行业产品订单核销
  57. public function industry_verify()
  58. {
  59. $input_verify_code = request()->input('verify_code'); //核销码
  60. $code_arr = explode('-', $input_verify_code);
  61. if (count($code_arr) != 2) {
  62. return $this->error('参数错误');
  63. }
  64. list($id, $verify_code) = $code_arr;
  65. $order = IndustryOrder::with('industryProduct:id,verify_mobile')
  66. ->where(['status' => OrderStatus::OFFLINE_PAID, 'verify_code' => $verify_code])->find($id);
  67. if (!$order) {
  68. return $this->error($input_verify_code . "核销码不存在或订单状态不允许核销");
  69. }
  70. $user = User::find($this->user_id);
  71. if (!$user->mobile) {
  72. return $this->error('手机号与核销手机号不一致');
  73. } else if ($user->mobile != $order->industryProduct->verify_mobile) {
  74. return $this->error('对不起,你没有该订单的核销权限');
  75. }
  76. DB::beginTransaction();
  77. try {
  78. //改变订单状态为已完成
  79. $order->status = OrderStatus::SUCCESS;
  80. $order->save();
  81. //扣除供应商冻结的交易金
  82. $supplier = Supplier::find($order->supplier_id);
  83. $supplier->deposit_used = $supplier->deposit_used + $order->deposit;
  84. $supplier->deposit_frozen = $supplier->deposit_frozen - $order->deposit;
  85. $supplier->save(); //需要用save才能执行模型事件记录日志
  86. DB::commit();
  87. return $this->success('核销成功');
  88. } catch (\Exception $e) {
  89. DB::rollBack();
  90. return $this->error($e->getMessage());
  91. }
  92. }
  93. public function fund($order)
  94. {
  95. $service = new WithdrawalService();
  96. DB::beginTransaction();
  97. try {
  98. //最后批量插入
  99. $statementCreate = [];
  100. $cost = 0;
  101. //如果有地接价格 分帐给地接
  102. if ($order->guide_price > 0) {
  103. $guidePrice = $order->guide_price;
  104. $cost = bcadd($cost, $order->guide_price, 6);
  105. //成本价 加上地接价格
  106. $statementCreate[] = $service->createByOrder(
  107. $order->guide_price,
  108. StatementType::ORDER,
  109. $order->guide->id,
  110. DemandTraits::$col[2],
  111. $order->id,
  112. StatementTraits::$type[0]
  113. );
  114. //抽成
  115. //if ($order->guide->rate > 0) {
  116. // //计算抽成金额
  117. // $guideCut = bcmul($order->guide_price, $order->guide->rate, 6);
  118. // $cutPrice = $guideCut > 0 ? bcdiv($guideCut, 100, 6) : 0;
  119. // //总后台抽成流水
  120. // if ($cutPrice > 0) {
  121. // $adminCreate[] = $service->createByOrderFormAdmin(
  122. // $cutPrice,
  123. // StatementType::CUT,
  124. // $order->guide->id,
  125. // DemandTraits::$col[2],
  126. // $order->id,
  127. // );
  128. // //地接被抽成流水
  129. // $statementCreate[] = $service->createByOrder(
  130. // bcmul($cutPrice, -1, 2),
  131. // StatementType::CUT,
  132. // $order->guide->id,
  133. // DemandTraits::$col[2],
  134. // $order->id,
  135. // StatementTraits::$type[0]
  136. // );
  137. //$guidePrice = bcsub($order->guide_price, $cutPrice, 6);
  138. $guide = Guide::query()->where('id', $order->guide->id)->lockForUpdate()->first();
  139. $guide->balance = bcadd($guide->balance, $guidePrice, 6);
  140. $guide->save();
  141. // }
  142. //}
  143. }
  144. //分账给供应商
  145. $orderItem = OrderProductItem::query()
  146. ->where('order_id', $order->id)
  147. ->with('supplier')
  148. ->select('*')
  149. ->selectRaw('sum(price) as sum_price,sum(service_persons) as sum_persons')
  150. ->groupBy('supplier_id')
  151. ->get();
  152. foreach ($orderItem as $v) {
  153. $cost = bcadd($cost, $v->sum_price, 6);
  154. $supplierPrice = $v->sum_price;
  155. $statementCreate[] = $service->createByOrder(
  156. $v->sum_price,
  157. StatementType::ORDER,
  158. $v->supplier_id,
  159. DemandTraits::$col[1],
  160. $order->id,
  161. StatementTraits::$type[0]
  162. );
  163. //if ($v->supplier->rate > 0) {
  164. // //计算抽成金额
  165. // $supplierCut = bcmul($v->sum_price, $v->supplier->rate, 6);
  166. // $cutPrice = $supplierCut > 0 ? bcdiv($supplierCut, 100, 6) : 0;
  167. // if ($cutPrice > 0) {
  168. // //总后台抽成流水
  169. // $adminCreate[] = $service->createByOrderFormAdmin(
  170. // $cutPrice,
  171. // StatementType::CUT,
  172. // $v->supplier_id,
  173. // DemandTraits::$col[1],
  174. // $order->id,
  175. // );
  176. // //供应商被抽成流水
  177. // $statementCreate[] = $service->createByOrder(
  178. // bcmul($cutPrice, -1, 6),
  179. // StatementType::CUT,
  180. // $v->supplier_id,
  181. // DemandTraits::$col[1],
  182. // $order->id,
  183. // StatementTraits::$type[0]
  184. // );
  185. // $supplierPrice = bcsub($supplierPrice, $cutPrice, 6);
  186. //
  187. // }
  188. //}
  189. $supplier = Supplier::query()->where('id', $v->supplier_id)->lockForUpdate()->first();
  190. //处理交易金
  191. if ($order->single_price > 0) {
  192. //计算交易金
  193. $deposit = bcmul($order->single_price,$v->sum_persons,6);
  194. //流水
  195. $statementCreate[] = $service->createByOrder(
  196. $deposit,
  197. StatementType::DEPOSIT,
  198. $v->supplier_id,
  199. DemandTraits::$col[1],
  200. $order->id,
  201. StatementTraits::$type[0]
  202. );
  203. //扣
  204. $supplierPrice = bcsub($supplierPrice,$deposit,6);
  205. $supplier->balance = bcadd($supplier->deposit_used, $supplierPrice, 6);
  206. $supplier->balance = bcsub($supplier->deposit_frozen, $supplierPrice, 6);
  207. }
  208. $supplier->balance = bcadd($supplier->balance, $supplierPrice, 6);
  209. $supplier->save();
  210. }
  211. //分账给代理商
  212. //成本价 加上地接价格
  213. $agentPrice = bcsub($order->price, $cost, 2);
  214. $statementCreate[] = $service->createByOrder(
  215. $agentPrice,
  216. StatementType::ORDER,
  217. $order->agent_id,
  218. DemandTraits::$col[0],
  219. $order->id,
  220. StatementTraits::$type[0]
  221. );
  222. //
  223. ////抽成
  224. //if ($order->agent->rate > 0) {
  225. // //计算抽成金额
  226. // $agentCut = bcmul($agentPrice, $order->agent->rate, 6);
  227. // $cutPrice = $agentCut > 0 ? bcdiv($agentCut, 100, 6) : 0;
  228. //
  229. // //总后台抽成流水
  230. // if ($cutPrice > 0) {
  231. // $adminCreate[] = $service->createByOrderFormAdmin(
  232. // $cutPrice,
  233. // StatementType::CUT,
  234. // $order->agent->id,
  235. // DemandTraits::$col[0],
  236. // $order->id,
  237. // );
  238. // //代理商被抽成流水
  239. // $statementCreate[] = $service->createByOrder(
  240. // bcmul($cutPrice, -1, 6),
  241. // StatementType::CUT,
  242. // $order->agent->id,
  243. // DemandTraits::$col[0],
  244. // $order->id,
  245. // StatementTraits::$type[0]
  246. // );
  247. // $agentPrice = bcsub($agentPrice, $cutPrice, 6);
  248. // }
  249. //扣除微信支付手续费
  250. //$chargePrice = bcmul($order->price, 0.006, 6);
  251. //$statementCreate[] = $service->createByOrder(
  252. // bcmul($chargePrice, -1, 6),
  253. // StatementType::CHARGE,
  254. // $order->agent_id,
  255. // DemandTraits::$col[0],
  256. // $order->id,
  257. // StatementTraits::$type[0]
  258. //);
  259. //$agentPrice = bcsub($agentPrice, $chargePrice, 6);
  260. $agent = Agent::query()->where('id', $order->agent->id)->lockForUpdate()->first();
  261. $agent->balance = bcadd($agent->balance, $agentPrice, 6);
  262. $agent->save();
  263. //}
  264. //if (!empty($adminCreate)) {
  265. // $order->statementAdmin()->createMany($adminCreate);
  266. //}
  267. if (!empty($statementCreate)) {
  268. $order->statement()->createMany($statementCreate);
  269. }
  270. DB::commit();
  271. } catch (\Exception $e) {
  272. DB::rollBack();
  273. return $this->error($e->getMessage());
  274. }
  275. }
  276. }