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

225 lines
6.7 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\Statement;
  12. use App\Models\Supplier;
  13. use App\Models\Product;
  14. use App\Models\OrderProductItem;
  15. use App\Models\User;
  16. use App\Common\OrderStatus;
  17. use App\Service\WithdrawalService;
  18. use App\Traits\DemandTraits;
  19. use App\Traits\StatementTraits;
  20. use Illuminate\Support\Facades\DB;
  21. /**
  22. * 订单核销
  23. * Class VerificationController
  24. * @package App\Http\Controllers\Api
  25. */
  26. class VerificationController extends Controller
  27. {
  28. //核销小程序订单
  29. public function verify()
  30. {
  31. $input_verify_code = request()->input('verify_code'); //核销码
  32. $code_arr = explode('-', $input_verify_code);
  33. if (count($code_arr) != 2) {
  34. return $this->error('参数错误');
  35. }
  36. list($id, $verify_code) = $code_arr;
  37. $order = Order::where(['verify_code' => $verify_code])
  38. ->whereIn('status', [OrderStatus::PAID, OrderStatus::PAID_RETAINAGE, OrderStatus::OFFLINE_PAID, OrderStatus::REFUSED_REFUND])
  39. ->find($id);
  40. if (!$order) {
  41. return $this->error($input_verify_code . "核销码不存在或订单状态不允许核销");
  42. }
  43. $mobile = User::where('id', $this->user_id)->value('mobile');
  44. if ($order->product_id) {
  45. $checkMobile = Product::query()->whereIn('id', explode(',', $order->product_ids))->where('verify_mobile', $mobile)->doesntExist();
  46. } else {
  47. $checkMobile = AgentProduct::where(['id' => $order->agent_product_id, 'verify_mobile' => $mobile])->doesntExist();
  48. }
  49. if ($checkMobile) {
  50. return $this->error('对不起,你没有核销权限,请联系管理员');
  51. }
  52. $order->status = OrderStatus::SUCCESS;
  53. if ($order->save()) {
  54. //分账
  55. //线下订单不分账
  56. if ($order->pay_type != PayType::OFFLINE) {
  57. $this->fund($order);
  58. }
  59. }
  60. return $this->success();
  61. }
  62. //行业产品订单核销
  63. public function industry_verify()
  64. {
  65. $input_verify_code = request()->input('verify_code'); //核销码
  66. $code_arr = explode('-', $input_verify_code);
  67. if (count($code_arr) != 2) {
  68. return $this->error('参数错误');
  69. }
  70. list($id, $verify_code) = $code_arr;
  71. $order = IndustryOrder::with('industryProduct:id,verify_mobile')
  72. ->whereIn('status', [OrderStatus::PAID, OrderStatus::PAID_RETAINAGE, OrderStatus::OFFLINE_PAID, OrderStatus::REFUSED_REFUND])
  73. ->where(['verify_code' => $verify_code])->find($id);
  74. if (!$order) {
  75. return $this->error($input_verify_code . "核销码不存在或订单状态不允许核销");
  76. }
  77. $user = User::find($this->user_id);
  78. if (!$user->mobile) {
  79. return $this->error('手机号与核销手机号不一致');
  80. } else if ($user->mobile != $order->industryProduct->verify_mobile) {
  81. return $this->error('对不起,你没有该订单的核销权限');
  82. }
  83. DB::beginTransaction();
  84. try {
  85. //改变订单状态为已完成
  86. $order->status = OrderStatus::SUCCESS;
  87. $order->save();
  88. //扣除供应商的交易金
  89. $supplier = Supplier::find($order->supplier_id);
  90. $supplier->trade_balance = $supplier->trade_balance - $order->trade_deposit;
  91. $supplier->balance = DB::raw('`balance` + ' . $order->paid_money);
  92. $supplier->save(); //需要用save才能执行模型事件记录日志
  93. //记录余额日志
  94. Statement::query()->forceCreate([
  95. 'price' => $order->paid_money,
  96. 'type' => StatementType::INDUSTRY_ORDER,
  97. 'user_id' => $order->supplier_id,
  98. 'user_type' => Supplier::class,
  99. 'access_id' => $order->id,
  100. 'access_type' => IndustryOrder::class,
  101. ]);
  102. DB::commit();
  103. return $this->success('核销成功');
  104. } catch (\Exception $e) {
  105. DB::rollBack();
  106. return $this->error($e->getMessage());
  107. }
  108. }
  109. public function fund($order)
  110. {
  111. $service = new WithdrawalService();
  112. DB::beginTransaction();
  113. try {
  114. //最后批量插入
  115. $statementCreate = [];
  116. $cost = 0;
  117. //如果有地接价格 分帐给地接
  118. if ($order->guide_price > 0) {
  119. $guidePrice = $order->guide_price;
  120. $cost = bcadd($cost, $order->guide_price, 6);
  121. //成本价 加上地接价格
  122. $statementCreate[] = $service->createByOrder(
  123. $order->guide_price,
  124. StatementType::ORDER,
  125. $order->guide->id,
  126. DemandTraits::$col[2],
  127. $order->id,
  128. StatementTraits::$type[0]
  129. );
  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. * 线下付款已经在app/AdminAgent/Extensions/Grid/ChangeOrderStatus.php扣过了
  136. * 这里不能再重复扣除。如果需要解除,同时还要注意下退款的情况
  137. */
  138. if ($order->pay_type != PayType::OFFLINE) {
  139. //分账给供应商
  140. $orderItem = OrderProductItem::query()
  141. ->where([
  142. ['order_id', '=', $order->id],
  143. ['supplier_id', '<>', 0], //supplier_id=0是代理商自营产品,供应商没有分账权利
  144. ])
  145. ->with('supplier')
  146. ->select('*')
  147. ->selectRaw('sum(price) as sum_price,sum(single_deposit * num) as sum_persons')
  148. ->groupBy('supplier_id')
  149. ->get();
  150. foreach ($orderItem as $v) {
  151. $cost = bcadd($cost, $v->sum_price, 6);
  152. $supplierPrice = $v->sum_price;
  153. $statementCreate[] = $service->createByOrder(
  154. $v->sum_price,
  155. StatementType::ORDER,
  156. $v->supplier_id,
  157. DemandTraits::$col[1],
  158. $order->id,
  159. StatementTraits::$type[0]
  160. );
  161. $supplier = Supplier::query()->where('id', $v->supplier_id)->lockForUpdate()->first();
  162. //处理交易金
  163. if ($v->sum_persons > 0) {
  164. //计算交易金
  165. $deposit = $v->sum_persons;
  166. //扣
  167. $supplier->trade_balance = bcsub($supplier->trade_balance, $deposit, 6);
  168. //$supplier->balance = bcadd($supplier->deposit_used, $supplierPrice, 6);
  169. //$supplier->balance = bcsub($supplier->deposit_frozen, $supplierPrice, 6);
  170. }
  171. $supplier->balance = bcadd($supplier->balance, $supplierPrice, 6);
  172. $supplier->save();
  173. }
  174. //分账给代理商
  175. //成本价 加上地接价格
  176. $agentPrice = bcsub($order->price, $cost, 2);
  177. $statementCreate[] = $service->createByOrder(
  178. $agentPrice,
  179. StatementType::ORDER,
  180. $order->agent_id,
  181. DemandTraits::$col[0],
  182. $order->id,
  183. StatementTraits::$type[0]
  184. );
  185. $agent = Agent::query()->where('id', $order->agent->id)->lockForUpdate()->first();
  186. $agent->balance = bcadd($agent->balance, $agentPrice, 6);
  187. $agent->save();
  188. }
  189. if (!empty($statementCreate)) {
  190. $order->statement()->createMany($statementCreate);
  191. }
  192. DB::commit();
  193. } catch (\Exception $e) {
  194. DB::rollBack();
  195. return $this->error($e->getMessage());
  196. }
  197. }
  198. }