海南旅游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
1.6 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. /**
  10. * 供应商审核
  11. * Class AuditSupplier
  12. * @package App\Admin\Extensions\Grid
  13. */
  14. class IndustryOrderStatus extends RowAction
  15. {
  16. protected $title = '设为已付款';
  17. protected function html()
  18. {
  19. $class = 'btn btn-sm btn-success';
  20. $this->appendHtmlAttribute('class', $class);
  21. $this->defaultHtmlAttribute('href', 'javascript:;');
  22. return "<a {$this->formatHtmlAttributes()}>{$this->title}</a>";
  23. }
  24. public function handle(Request $request)
  25. {
  26. $id = $this->getKey();
  27. DB::beginTransaction();
  28. try {
  29. $order = IndustryOrder::where(['id' => $id, 'status' => OrderStatus::OFFLINE_UNPAID])->find($id);
  30. if (!$order) {
  31. throw new \Exception('订单不存在或者已审核过了');
  32. }
  33. //操作订单表
  34. $order->status = OrderStatus::OFFLINE_PAID;
  35. $order->paid_at = now();
  36. $order->verify_code = uniqid();
  37. $order->save();
  38. //减库存
  39. $affect_row = IndustryProduct::where([
  40. ['id', '=', $order->industry_product_id],
  41. ['stock', '>=', $order->num],
  42. ])->decrement('stock', $order->num);
  43. if (!$affect_row) {
  44. throw new \Exception('库存不足,请先增加库存');
  45. }
  46. DB::commit();
  47. return $this->response()->success('操作成功');
  48. } catch (\Exception $e) {
  49. DB::rollBack();
  50. return $this->response()->error($e->getMessage());
  51. }
  52. }
  53. public function confirm()
  54. {
  55. return ['确定要设置为已付款吗?', ''];
  56. }
  57. }