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

213 lines
6.3 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
  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. ->whereIn('status', [OrderStatus::PAID, OrderStatus::PAID_RETAINAGE, OrderStatus::OFFLINE_PAID, OrderStatus::REFUSED_REFUND])
  72. ->where(['verify_code' => $verify_code])->find($id);
  73. if (!$order) {
  74. return $this->error($input_verify_code . "核销码不存在或订单状态不允许核销");
  75. }
  76. $user = User::find($this->user_id);
  77. if (!$user->mobile) {
  78. return $this->error('手机号与核销手机号不一致');
  79. } else if ($user->mobile != $order->industryProduct->verify_mobile) {
  80. return $this->error('对不起,你没有该订单的核销权限');
  81. }
  82. DB::beginTransaction();
  83. try {
  84. //改变订单状态为已完成
  85. $order->status = OrderStatus::SUCCESS;
  86. $order->save();
  87. //扣除供应商的交易金
  88. $supplier = Supplier::find($order->supplier_id);
  89. $supplier->trade_balance = $supplier->trade_balance - $order->trade_deposit;
  90. $supplier->save(); //需要用save才能执行模型事件记录日志
  91. DB::commit();
  92. return $this->success('核销成功');
  93. } catch (\Exception $e) {
  94. DB::rollBack();
  95. return $this->error($e->getMessage());
  96. }
  97. }
  98. public function fund($order)
  99. {
  100. $service = new WithdrawalService();
  101. DB::beginTransaction();
  102. try {
  103. //最后批量插入
  104. $statementCreate = [];
  105. $cost = 0;
  106. //如果有地接价格 分帐给地接
  107. if ($order->guide_price > 0) {
  108. $guidePrice = $order->guide_price;
  109. $cost = bcadd($cost, $order->guide_price, 6);
  110. //成本价 加上地接价格
  111. $statementCreate[] = $service->createByOrder(
  112. $order->guide_price,
  113. StatementType::ORDER,
  114. $order->guide->id,
  115. DemandTraits::$col[2],
  116. $order->id,
  117. StatementTraits::$type[0]
  118. );
  119. $guide = Guide::query()->where('id', $order->guide->id)->lockForUpdate()->first();
  120. $guide->balance = bcadd($guide->balance, $guidePrice, 6);
  121. $guide->save();
  122. }
  123. /**
  124. * 线下付款已经在app/AdminAgent/Extensions/Grid/ChangeOrderStatus.php扣过了
  125. * 这里不能再重复扣除。如果需要解除,同时还要注意下退款的情况
  126. */
  127. if ($order->pay_type != PayType::OFFLINE) {
  128. //分账给供应商
  129. $orderItem = OrderProductItem::query()
  130. ->where([
  131. ['order_id', '=', $order->id],
  132. ['supplier_id', '<>', 0], //supplier_id=0是代理商自营产品,供应商没有分账权利
  133. ])
  134. ->with('supplier')
  135. ->select('*')
  136. ->selectRaw('sum(price) as sum_price,sum(single_deposit * num) as sum_persons')
  137. ->groupBy('supplier_id')
  138. ->get();
  139. foreach ($orderItem as $v) {
  140. $cost = bcadd($cost, $v->sum_price, 6);
  141. $supplierPrice = $v->sum_price;
  142. $statementCreate[] = $service->createByOrder(
  143. $v->sum_price,
  144. StatementType::ORDER,
  145. $v->supplier_id,
  146. DemandTraits::$col[1],
  147. $order->id,
  148. StatementTraits::$type[0]
  149. );
  150. $supplier = Supplier::query()->where('id', $v->supplier_id)->lockForUpdate()->first();
  151. //处理交易金
  152. if ($v->sum_persons > 0) {
  153. //计算交易金
  154. $deposit = $v->sum_persons;
  155. //扣
  156. $supplier->trade_balance = bcsub($supplier->trade_balance, $deposit, 6);
  157. //$supplier->balance = bcadd($supplier->deposit_used, $supplierPrice, 6);
  158. //$supplier->balance = bcsub($supplier->deposit_frozen, $supplierPrice, 6);
  159. }
  160. $supplier->balance = bcadd($supplier->balance, $supplierPrice, 6);
  161. $supplier->save();
  162. }
  163. //分账给代理商
  164. //成本价 加上地接价格
  165. $agentPrice = bcsub($order->price, $cost, 2);
  166. $statementCreate[] = $service->createByOrder(
  167. $agentPrice,
  168. StatementType::ORDER,
  169. $order->agent_id,
  170. DemandTraits::$col[0],
  171. $order->id,
  172. StatementTraits::$type[0]
  173. );
  174. $agent = Agent::query()->where('id', $order->agent->id)->lockForUpdate()->first();
  175. $agent->balance = bcadd($agent->balance, $agentPrice, 6);
  176. $agent->save();
  177. }
  178. if (!empty($statementCreate)) {
  179. $order->statement()->createMany($statementCreate);
  180. }
  181. DB::commit();
  182. } catch (\Exception $e) {
  183. DB::rollBack();
  184. return $this->error($e->getMessage());
  185. }
  186. }
  187. }