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

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