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

96 lines
3.1 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
  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. $supplier = Supplier::query()->where('id',$item->supplier_id)->lockForUpdate()->first();
  60. $supplier->balance = bcadd($supplier->balance,$item->price,6);
  61. $supplier->save();
  62. $statementCreate[] = $service->createByOrder(
  63. $item->price,
  64. StatementType::ORDER,
  65. $item->supplier_id,
  66. DemandTraits::$col[1],
  67. $order->id,
  68. StatementTraits::$type[0]
  69. );
  70. }
  71. if (!empty($statementCreate)) {
  72. $order->statement()->createMany($statementCreate);
  73. }
  74. DB::commit();
  75. return $this->response()->success("操作成功,已设置为“线下已付款”")->refresh();
  76. } catch (\Exception $e) {
  77. DB::rollBack();
  78. return $this->response()->error($e->getMessage());
  79. }
  80. }
  81. public function confirm()
  82. {
  83. return ['确定要设置为“线下已付款”吗?', ''];
  84. }
  85. }