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