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

77 lines
2.0 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 App\Models\Supplier;
  7. use Dcat\Admin\Admin;
  8. use Dcat\Admin\Grid\RowAction;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\DB;
  11. /**
  12. * 供应商审核
  13. * Class AuditSupplier
  14. * @package App\Admin\Extensions\Grid
  15. */
  16. class IndustryOrderStatus extends RowAction
  17. {
  18. protected $title = '设为已付款';
  19. protected function html()
  20. {
  21. $class = 'btn btn-sm btn-success';
  22. $this->appendHtmlAttribute('class', $class);
  23. $this->defaultHtmlAttribute('href', 'javascript:;');
  24. return "<a {$this->formatHtmlAttributes()}>{$this->title}</a>";
  25. }
  26. public function handle(Request $request)
  27. {
  28. $id = $this->getKey();
  29. DB::beginTransaction();
  30. try {
  31. $order = IndustryOrder::where(['id' => $id, 'status' => OrderStatus::OFFLINE_UNPAID])->find($id);
  32. if (!$order) {
  33. throw new \Exception('订单不存在或者已审核过了');
  34. }
  35. //操作订单表
  36. $order->status = OrderStatus::OFFLINE_PAID;
  37. $order->paid_at = now();
  38. $order->verify_code = uniqid();
  39. $order->save();
  40. //减库存
  41. $affect_row = IndustryProduct::where([
  42. ['id', '=', $order->industry_product_id],
  43. ['stock', '>=', $order->num],
  44. ])->decrement('stock', $order->num);
  45. if (!$affect_row) {
  46. throw new \Exception('库存不足,请先增加库存');
  47. }
  48. //扣除交易金
  49. $supplier = Supplier::find(Admin::user()->id); //不能使用Admin::user()修改,必须使用Supplier模型才能正确记录资金变动日志
  50. $supplier->deposit_used = $supplier->deposit_used + $order->deposit;
  51. $supplier->deposit_frozen = $supplier->deposit_frozen - $order->deposit;
  52. $supplier->save();
  53. DB::commit();
  54. return $this->response()->success('操作成功');
  55. } catch (\Exception $e) {
  56. DB::rollBack();
  57. return $this->response()->error($e->getMessage());
  58. }
  59. }
  60. public function confirm()
  61. {
  62. return ['确定要设置为已付款吗?', ''];
  63. }
  64. }