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

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