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

212 lines
6.2 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\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. $guide = Guide::query()->where('id', $order->guide->id)->lockForUpdate()->first();
  119. $guide->balance = bcadd($guide->balance, $guidePrice, 6);
  120. $guide->save();
  121. }
  122. /**
  123. * 线下付款已经在app/AdminAgent/Extensions/Grid/ChangeOrderStatus.php扣过了
  124. * 这里不能再重复扣除。如果需要解除,同时还要注意下退款的情况
  125. */
  126. if ($order->pay_type != PayType::OFFLINE) {
  127. //分账给供应商
  128. $orderItem = OrderProductItem::query()
  129. ->where([
  130. ['order_id', '=', $order->id],
  131. ['supplier_id', '<>', 0], //supplier_id=0是代理商自营产品,供应商没有分账权利
  132. ])
  133. ->with('supplier')
  134. ->select('*')
  135. ->selectRaw('sum(price) as sum_price,sum(single_deposit * num) as sum_persons')
  136. ->groupBy('supplier_id')
  137. ->get();
  138. foreach ($orderItem as $v) {
  139. $cost = bcadd($cost, $v->sum_price, 6);
  140. $supplierPrice = $v->sum_price;
  141. $statementCreate[] = $service->createByOrder(
  142. $v->sum_price,
  143. StatementType::ORDER,
  144. $v->supplier_id,
  145. DemandTraits::$col[1],
  146. $order->id,
  147. StatementTraits::$type[0]
  148. );
  149. $supplier = Supplier::query()->where('id', $v->supplier_id)->lockForUpdate()->first();
  150. //处理交易金
  151. if ($v->sum_persons > 0) {
  152. //计算交易金
  153. $deposit = $v->sum_persons;
  154. //扣
  155. $supplier->trade_balance = bcsub($supplier->trade_balance, $deposit, 6);
  156. //$supplier->balance = bcadd($supplier->deposit_used, $supplierPrice, 6);
  157. //$supplier->balance = bcsub($supplier->deposit_frozen, $supplierPrice, 6);
  158. }
  159. $supplier->balance = bcadd($supplier->balance, $supplierPrice, 6);
  160. $supplier->save();
  161. }
  162. //分账给代理商
  163. //成本价 加上地接价格
  164. $agentPrice = bcsub($order->price, $cost, 2);
  165. $statementCreate[] = $service->createByOrder(
  166. $agentPrice,
  167. StatementType::ORDER,
  168. $order->agent_id,
  169. DemandTraits::$col[0],
  170. $order->id,
  171. StatementTraits::$type[0]
  172. );
  173. $agent = Agent::query()->where('id', $order->agent->id)->lockForUpdate()->first();
  174. $agent->balance = bcadd($agent->balance, $agentPrice, 6);
  175. $agent->save();
  176. }
  177. if (!empty($statementCreate)) {
  178. $order->statement()->createMany($statementCreate);
  179. }
  180. DB::commit();
  181. } catch (\Exception $e) {
  182. DB::rollBack();
  183. return $this->error($e->getMessage());
  184. }
  185. }
  186. }