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

73 lines
1.9 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. //操作订单表
  33. $order->status = OrderStatus::OFFLINE_PAID;
  34. $order->paid_at = now();
  35. $order->verify_code = uniqid();
  36. $order->save();
  37. //减库存
  38. $affect_row = IndustryProduct::where([
  39. ['id', '=', $order->industry_product_id],
  40. ['stock', '>=', $order->num],
  41. ])->decrement('stock', $order->num);
  42. if (!$affect_row) {
  43. throw new \Exception('库存不足,请先增加库存');
  44. }
  45. //扣除交易金
  46. $supplier = Supplier::find(Admin::user()->id); //不能使用Admin::user()修改,必须使用Supplier模型才能正确记录资金变动日志
  47. $supplier->deposit_used = $supplier->deposit_used + $order->deposit;
  48. $supplier->deposit_frozen = $supplier->deposit_frozen - $order->deposit;
  49. $supplier->save();
  50. DB::commit();
  51. return $this->response()->success('操作成功');
  52. } catch (\Exception $e) {
  53. DB::rollBack();
  54. return $this->response()->error($e->getMessage());
  55. }
  56. }
  57. public function confirm()
  58. {
  59. return ['确定要设置为已付款吗?', ''];
  60. }
  61. }