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

294 lines
8.4 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
  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('订单不存在或订单状态不允许核销');
  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('订单不存在或订单状态不允许核销');
  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. try {
  77. //改变订单状态为已完成
  78. $order->status = OrderStatus::SUCCESS;
  79. $order->save();
  80. return $this->success();
  81. } catch (\Exception $e) {
  82. return $this->error($e->getMessage());
  83. }
  84. }
  85. public function fund($order)
  86. {
  87. $service = new WithdrawalService();
  88. DB::beginTransaction();
  89. try {
  90. //最后批量插入
  91. $statementCreate = [];
  92. $cost = 0;
  93. //如果有地接价格 分帐给地接
  94. if ($order->guide_price > 0) {
  95. $guidePrice = $order->guide_price;
  96. $cost = bcadd($cost, $order->guide_price, 6);
  97. //成本价 加上地接价格
  98. $statementCreate[] = $service->createByOrder(
  99. $order->guide_price,
  100. StatementType::ORDER,
  101. $order->guide->id,
  102. DemandTraits::$col[2],
  103. $order->id,
  104. StatementTraits::$type[0]
  105. );
  106. //抽成
  107. //if ($order->guide->rate > 0) {
  108. // //计算抽成金额
  109. // $guideCut = bcmul($order->guide_price, $order->guide->rate, 6);
  110. // $cutPrice = $guideCut > 0 ? bcdiv($guideCut, 100, 6) : 0;
  111. // //总后台抽成流水
  112. // if ($cutPrice > 0) {
  113. // $adminCreate[] = $service->createByOrderFormAdmin(
  114. // $cutPrice,
  115. // StatementType::CUT,
  116. // $order->guide->id,
  117. // DemandTraits::$col[2],
  118. // $order->id,
  119. // );
  120. // //地接被抽成流水
  121. // $statementCreate[] = $service->createByOrder(
  122. // bcmul($cutPrice, -1, 2),
  123. // StatementType::CUT,
  124. // $order->guide->id,
  125. // DemandTraits::$col[2],
  126. // $order->id,
  127. // StatementTraits::$type[0]
  128. // );
  129. //$guidePrice = bcsub($order->guide_price, $cutPrice, 6);
  130. $guide = Guide::query()->where('id', $order->guide->id)->lockForUpdate()->first();
  131. $guide->balance = bcadd($guide->balance, $guidePrice, 6);
  132. $guide->save();
  133. // }
  134. //}
  135. }
  136. //分账给供应商
  137. $orderItem = OrderProductItem::query()
  138. ->where('order_id', $order->id)
  139. ->with('supplier')
  140. ->select('*')
  141. ->selectRaw('sum(price) as sum_price,sum(service_persons) as sum_persons')
  142. ->groupBy('supplier_id')
  143. ->get();
  144. foreach ($orderItem as $v) {
  145. $cost = bcadd($cost, $v->sum_price, 6);
  146. $supplierPrice = $v->sum_price;
  147. $statementCreate[] = $service->createByOrder(
  148. $v->sum_price,
  149. StatementType::ORDER,
  150. $v->supplier_id,
  151. DemandTraits::$col[1],
  152. $order->id,
  153. StatementTraits::$type[0]
  154. );
  155. //if ($v->supplier->rate > 0) {
  156. // //计算抽成金额
  157. // $supplierCut = bcmul($v->sum_price, $v->supplier->rate, 6);
  158. // $cutPrice = $supplierCut > 0 ? bcdiv($supplierCut, 100, 6) : 0;
  159. // if ($cutPrice > 0) {
  160. // //总后台抽成流水
  161. // $adminCreate[] = $service->createByOrderFormAdmin(
  162. // $cutPrice,
  163. // StatementType::CUT,
  164. // $v->supplier_id,
  165. // DemandTraits::$col[1],
  166. // $order->id,
  167. // );
  168. // //供应商被抽成流水
  169. // $statementCreate[] = $service->createByOrder(
  170. // bcmul($cutPrice, -1, 6),
  171. // StatementType::CUT,
  172. // $v->supplier_id,
  173. // DemandTraits::$col[1],
  174. // $order->id,
  175. // StatementTraits::$type[0]
  176. // );
  177. // $supplierPrice = bcsub($supplierPrice, $cutPrice, 6);
  178. //
  179. // }
  180. //}
  181. $supplier = Supplier::query()->where('id', $v->supplier_id)->lockForUpdate()->first();
  182. //处理交易金
  183. if ($order->single_price > 0) {
  184. //计算交易金
  185. $deposit = bcmul($order->single_price,$v->sum_persons,6);
  186. //流水
  187. $statementCreate[] = $service->createByOrder(
  188. $deposit,
  189. StatementType::DEPOSIT,
  190. $v->supplier_id,
  191. DemandTraits::$col[1],
  192. $order->id,
  193. StatementTraits::$type[0]
  194. );
  195. //扣
  196. $supplierPrice = bcsub($supplierPrice,$deposit,6);
  197. $supplier->balance = bcadd($supplier->deposit_used, $supplierPrice, 6);
  198. $supplier->balance = bcsub($supplier->deposit_frozen, $supplierPrice, 6);
  199. }
  200. $supplier->balance = bcadd($supplier->balance, $supplierPrice, 6);
  201. $supplier->save();
  202. }
  203. //分账给代理商
  204. //成本价 加上地接价格
  205. $agentPrice = bcsub($order->price, $cost, 2);
  206. $statementCreate[] = $service->createByOrder(
  207. $agentPrice,
  208. StatementType::ORDER,
  209. $order->agent_id,
  210. DemandTraits::$col[0],
  211. $order->id,
  212. StatementTraits::$type[0]
  213. );
  214. //
  215. ////抽成
  216. //if ($order->agent->rate > 0) {
  217. // //计算抽成金额
  218. // $agentCut = bcmul($agentPrice, $order->agent->rate, 6);
  219. // $cutPrice = $agentCut > 0 ? bcdiv($agentCut, 100, 6) : 0;
  220. //
  221. // //总后台抽成流水
  222. // if ($cutPrice > 0) {
  223. // $adminCreate[] = $service->createByOrderFormAdmin(
  224. // $cutPrice,
  225. // StatementType::CUT,
  226. // $order->agent->id,
  227. // DemandTraits::$col[0],
  228. // $order->id,
  229. // );
  230. // //代理商被抽成流水
  231. // $statementCreate[] = $service->createByOrder(
  232. // bcmul($cutPrice, -1, 6),
  233. // StatementType::CUT,
  234. // $order->agent->id,
  235. // DemandTraits::$col[0],
  236. // $order->id,
  237. // StatementTraits::$type[0]
  238. // );
  239. // $agentPrice = bcsub($agentPrice, $cutPrice, 6);
  240. // }
  241. //扣除微信支付手续费
  242. //$chargePrice = bcmul($order->price, 0.006, 6);
  243. //$statementCreate[] = $service->createByOrder(
  244. // bcmul($chargePrice, -1, 6),
  245. // StatementType::CHARGE,
  246. // $order->agent_id,
  247. // DemandTraits::$col[0],
  248. // $order->id,
  249. // StatementTraits::$type[0]
  250. //);
  251. //$agentPrice = bcsub($agentPrice, $chargePrice, 6);
  252. $agent = Agent::query()->where('id', $order->agent->id)->lockForUpdate()->first();
  253. $agent->balance = bcadd($agent->balance, $agentPrice, 6);
  254. $agent->save();
  255. //}
  256. //if (!empty($adminCreate)) {
  257. // $order->statementAdmin()->createMany($adminCreate);
  258. //}
  259. if (!empty($statementCreate)) {
  260. $order->statement()->createMany($statementCreate);
  261. }
  262. DB::commit();
  263. } catch (\Exception $e) {
  264. DB::rollBack();
  265. return $this->error($e->getMessage());
  266. }
  267. }
  268. }