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

275 lines
7.6 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
  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. $adminCreate = $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')
  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. $supplier = Supplier::query()->where('id', $v->supplier_id)->lockForUpdate()->first();
  181. $supplier->balance = bcadd($supplier->balance, $supplierPrice, 6);
  182. $supplier->save();
  183. }
  184. //分账给代理商
  185. //成本价 加上地接价格
  186. $agentPrice = bcsub($order->price, $cost, 2);
  187. if ($agentPrice > 0) {
  188. $statementCreate[] = $service->createByOrder(
  189. $agentPrice,
  190. StatementType::ORDER,
  191. $order->agent_id,
  192. DemandTraits::$col[0],
  193. $order->id,
  194. StatementTraits::$type[0]
  195. );
  196. //抽成
  197. if ($order->agent->rate > 0) {
  198. //计算抽成金额
  199. $agentCut = bcmul($agentPrice, $order->agent->rate, 6);
  200. $cutPrice = $agentCut > 0 ? bcdiv($agentCut, 100, 6) : 0;
  201. //总后台抽成流水
  202. if ($cutPrice > 0) {
  203. $adminCreate[] = $service->createByOrderFormAdmin(
  204. $cutPrice,
  205. StatementType::CUT,
  206. $order->agent->id,
  207. DemandTraits::$col[0],
  208. $order->id,
  209. );
  210. //代理商被抽成流水
  211. $statementCreate[] = $service->createByOrder(
  212. bcmul($cutPrice, -1, 6),
  213. StatementType::CUT,
  214. $order->agent->id,
  215. DemandTraits::$col[0],
  216. $order->id,
  217. StatementTraits::$type[0]
  218. );
  219. $agentPrice = bcsub($agentPrice, $cutPrice, 6);
  220. }
  221. }
  222. //扣除微信支付手续费
  223. $chargePrice = bcmul($order->price, 0.006, 6);
  224. $statementCreate[] = $service->createByOrder(
  225. bcmul($chargePrice, -1, 6),
  226. StatementType::CHARGE,
  227. $order->agent_id,
  228. DemandTraits::$col[0],
  229. $order->id,
  230. StatementTraits::$type[0]
  231. );
  232. $agentPrice = bcsub($agentPrice, $chargePrice, 6);
  233. $agent = Agent::query()->where('id', $order->agent->id)->lockForUpdate()->first();
  234. $agent->balance = bcadd($agent->balance, $agentPrice, 6);
  235. $agent->save();
  236. }
  237. if (!empty($adminCreate)) {
  238. $order->statementAdmin()->createMany($adminCreate);
  239. }
  240. if (!empty($statementCreate)) {
  241. $order->statement()->createMany($statementCreate);
  242. }
  243. DB::commit();
  244. } catch (\Exception $e) {
  245. DB::rollBack();
  246. return $this->error($e->getMessage());
  247. }
  248. }
  249. }