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

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