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

311 lines
9.0 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
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([
  151. ['order_id', '=', $order->id],
  152. ['supplier_id', '<>', 0], //supplier_id=0是代理商自营产品,供应商没有分账权利
  153. ])
  154. ->with('supplier')
  155. ->select('*')
  156. ->selectRaw('sum(price) as sum_price,sum(single_deposit * num) as sum_persons')
  157. ->groupBy('supplier_id')
  158. ->get();
  159. foreach ($orderItem as $v) {
  160. $cost = bcadd($cost, $v->sum_price, 6);
  161. $supplierPrice = $v->sum_price;
  162. $statementCreate[] = $service->createByOrder(
  163. $v->sum_price,
  164. StatementType::ORDER,
  165. $v->supplier_id,
  166. DemandTraits::$col[1],
  167. $order->id,
  168. StatementTraits::$type[0]
  169. );
  170. //if ($v->supplier->rate > 0) {
  171. // //计算抽成金额
  172. // $supplierCut = bcmul($v->sum_price, $v->supplier->rate, 6);
  173. // $cutPrice = $supplierCut > 0 ? bcdiv($supplierCut, 100, 6) : 0;
  174. // if ($cutPrice > 0) {
  175. // //总后台抽成流水
  176. // $adminCreate[] = $service->createByOrderFormAdmin(
  177. // $cutPrice,
  178. // StatementType::CUT,
  179. // $v->supplier_id,
  180. // DemandTraits::$col[1],
  181. // $order->id,
  182. // );
  183. // //供应商被抽成流水
  184. // $statementCreate[] = $service->createByOrder(
  185. // bcmul($cutPrice, -1, 6),
  186. // StatementType::CUT,
  187. // $v->supplier_id,
  188. // DemandTraits::$col[1],
  189. // $order->id,
  190. // StatementTraits::$type[0]
  191. // );
  192. // $supplierPrice = bcsub($supplierPrice, $cutPrice, 6);
  193. //
  194. // }
  195. //}
  196. $supplier = Supplier::query()->where('id', $v->supplier_id)->lockForUpdate()->first();
  197. //处理交易金
  198. if ($v->sum_persons > 0) {
  199. //计算交易金
  200. $deposit = $v->sum_persons;
  201. //流水
  202. // $statementCreate[] = $service->createByOrder(
  203. // bcmul($deposit, -1, 6),
  204. // StatementType::DEPOSIT,
  205. // $v->supplier_id,
  206. // DemandTraits::$col[1],
  207. // $order->id,
  208. // StatementTraits::$type[0]
  209. // );
  210. //扣
  211. $supplier->trade_balance = bcsub($supplier->trade_balance,$deposit,6);
  212. //$supplier->balance = bcadd($supplier->deposit_used, $supplierPrice, 6);
  213. //$supplier->balance = bcsub($supplier->deposit_frozen, $supplierPrice, 6);
  214. }
  215. $supplier->balance = bcadd($supplier->balance, $supplierPrice, 6);
  216. $supplier->save();
  217. }
  218. //分账给代理商
  219. //成本价 加上地接价格
  220. $agentPrice = bcsub($order->price, $cost, 2);
  221. $statementCreate[] = $service->createByOrder(
  222. $agentPrice,
  223. StatementType::ORDER,
  224. $order->agent_id,
  225. DemandTraits::$col[0],
  226. $order->id,
  227. StatementTraits::$type[0]
  228. );
  229. //
  230. ////抽成
  231. //if ($order->agent->rate > 0) {
  232. // //计算抽成金额
  233. // $agentCut = bcmul($agentPrice, $order->agent->rate, 6);
  234. // $cutPrice = $agentCut > 0 ? bcdiv($agentCut, 100, 6) : 0;
  235. //
  236. // //总后台抽成流水
  237. // if ($cutPrice > 0) {
  238. // $adminCreate[] = $service->createByOrderFormAdmin(
  239. // $cutPrice,
  240. // StatementType::CUT,
  241. // $order->agent->id,
  242. // DemandTraits::$col[0],
  243. // $order->id,
  244. // );
  245. // //代理商被抽成流水
  246. // $statementCreate[] = $service->createByOrder(
  247. // bcmul($cutPrice, -1, 6),
  248. // StatementType::CUT,
  249. // $order->agent->id,
  250. // DemandTraits::$col[0],
  251. // $order->id,
  252. // StatementTraits::$type[0]
  253. // );
  254. // $agentPrice = bcsub($agentPrice, $cutPrice, 6);
  255. // }
  256. //扣除微信支付手续费
  257. //$chargePrice = bcmul($order->price, 0.006, 6);
  258. //$statementCreate[] = $service->createByOrder(
  259. // bcmul($chargePrice, -1, 6),
  260. // StatementType::CHARGE,
  261. // $order->agent_id,
  262. // DemandTraits::$col[0],
  263. // $order->id,
  264. // StatementTraits::$type[0]
  265. //);
  266. //$agentPrice = bcsub($agentPrice, $chargePrice, 6);
  267. $agent = Agent::query()->where('id', $order->agent->id)->lockForUpdate()->first();
  268. $agent->balance = bcadd($agent->balance, $agentPrice, 6);
  269. $agent->save();
  270. //}
  271. //if (!empty($adminCreate)) {
  272. // $order->statementAdmin()->createMany($adminCreate);
  273. //}
  274. if (!empty($statementCreate)) {
  275. $order->statement()->createMany($statementCreate);
  276. }
  277. DB::commit();
  278. } catch (\Exception $e) {
  279. DB::rollBack();
  280. return $this->error($e->getMessage());
  281. }
  282. }
  283. }