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

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