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

64 lines
1.5 KiB

  1. <?php
  2. namespace App\AdminSupplier\Extensions\Grid;
  3. use App\Common\OrderStatus;
  4. use App\Models\IndustryOrder;
  5. use App\Models\IndustryProduct;
  6. use Dcat\Admin\Grid\RowAction;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\DB;
  9. class IndustryOrderStatus extends RowAction
  10. {
  11. protected $title = '设为已付款';
  12. protected function html()
  13. {
  14. $class = 'btn btn-sm btn-success';
  15. $this->appendHtmlAttribute('class', $class);
  16. $this->defaultHtmlAttribute('href', 'javascript:;');
  17. return "<a {$this->formatHtmlAttributes()}>{$this->title}</a>";
  18. }
  19. public function handle(Request $request)
  20. {
  21. $id = $this->getKey();
  22. DB::beginTransaction();
  23. try {
  24. $order = IndustryOrder::where(['id' => $id, 'status' => OrderStatus::OFFLINE_UNPAID])->find($id);
  25. if (!$order) {
  26. throw new \Exception('订单不存在或者已审核过了');
  27. }
  28. //操作订单表
  29. $order->status = OrderStatus::OFFLINE_PAID;
  30. $order->paid_at = now();
  31. $order->verify_code = uniqid();
  32. $order->save();
  33. //减库存
  34. $affect_row = IndustryProduct::where([
  35. ['id', '=', $order->industry_product_id],
  36. ['stock', '>=', $order->num],
  37. ])->decrement('stock', $order->num);
  38. if (!$affect_row) {
  39. throw new \Exception('库存不足,请先增加库存');
  40. }
  41. DB::commit();
  42. return $this->response()->success('操作成功');
  43. } catch (\Exception $e) {
  44. DB::rollBack();
  45. return $this->response()->error($e->getMessage());
  46. }
  47. }
  48. public function confirm()
  49. {
  50. return ['确定要设置为已付款吗?', ''];
  51. }
  52. }