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

102 lines
3.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
  1. <?php
  2. namespace App\AdminAgent\Extensions\Grid;
  3. use App\Common\OrderStatus;
  4. use App\Common\StatementType;
  5. use App\Models\Order;
  6. use App\Models\Agent;
  7. use App\Models\OrderProductItem;
  8. use App\Models\Supplier;
  9. use App\Service\WithdrawalService;
  10. use App\Traits\DemandTraits;
  11. use App\Traits\StatementTraits;
  12. use Dcat\Admin\Admin;
  13. use Dcat\Admin\Grid\RowAction;
  14. use Illuminate\Support\Facades\DB;
  15. /**
  16. * 改变订单状态
  17. * Class ChangeOrderStatus
  18. * @package App\AdminAgent\Extensions\Grid
  19. */
  20. class ChangeOrderStatus extends RowAction
  21. {
  22. protected $title = '设为已付款';
  23. protected function html()
  24. {
  25. $this->appendHtmlAttribute('class', 'btn btn-sm btn-success');
  26. $this->defaultHtmlAttribute('href', 'javascript:;');
  27. return "<a {$this->formatHtmlAttributes()}>{$this->title}</a>";
  28. }
  29. public function handle()
  30. {
  31. DB::beginTransaction();
  32. try {
  33. $order = Order::firstWhere(['id' => $this->getKey(), 'agent_id' => Admin::user()->id, 'status' => OrderStatus::OFFLINE_UNPAID]);
  34. if (!$order) {
  35. return $this->response()->error("订单不存在或已处理过了")->refresh();
  36. }
  37. $agent = Agent::query()->where('id', Admin::user()->id)->lockForUpdate()->first();
  38. if ($agent->balance < $order->price) {
  39. return $this->response()->error("账户余额不足,请先进行充值")->refresh();
  40. }
  41. $order->status = OrderStatus::OFFLINE_PAID;
  42. $order->verify_code = uniqid(); //生成核销码
  43. $order->save();
  44. //扣供应商余额
  45. $service = new WithdrawalService();
  46. $agentPrice = bcsub($agent->balance,$order->price,6);
  47. $agent->balance = $agentPrice;
  48. $agent->save();
  49. $statementCreate[] = $service->createByOrder(
  50. bcmul($agentPrice, -1, 6),
  51. StatementType::ORDER,
  52. $order->agent_id,
  53. DemandTraits::$col[0],
  54. $order->id,
  55. StatementTraits::$type[0]
  56. );
  57. $orderItem = OrderProductItem::query()->where('order_id',$order->id)->get();
  58. foreach ($orderItem as $item) {
  59. if (!$item->supplier_id) { //自营产品供应商为空
  60. continue;
  61. }
  62. $supplier = Supplier::query()->where('id',$item->supplier_id)->lockForUpdate()->first();
  63. if (!$supplier) { //自营产品供应商为空
  64. continue;
  65. }
  66. $supplier->balance = bcadd($supplier->balance,$item->price,6);
  67. $supplier->save();
  68. $statementCreate[] = $service->createByOrder(
  69. $item->price,
  70. StatementType::ORDER,
  71. $item->supplier_id,
  72. DemandTraits::$col[1],
  73. $order->id,
  74. StatementTraits::$type[0]
  75. );
  76. }
  77. if (!empty($statementCreate)) {
  78. $order->statement()->createMany($statementCreate);
  79. }
  80. DB::commit();
  81. return $this->response()->success("操作成功,已设置为“线下已付款”")->refresh();
  82. } catch (\Exception $e) {
  83. DB::rollBack();
  84. return $this->response()->error($e->getMessage());
  85. }
  86. }
  87. public function confirm()
  88. {
  89. return ['确定要设置为“线下已付款”吗?', ''];
  90. }
  91. }