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

102 lines
2.4 KiB

  1. <?php
  2. namespace App\AdminSupplier\Extensions\Grid;
  3. use App\Common\ProductStatus;
  4. use App\Models\IndustryOrder;
  5. use App\Models\IndustryProduct;
  6. use App\Models\IndustryProductSpec;
  7. use Dcat\Admin\Grid\RowAction;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\DB;
  10. /**
  11. * 行业订单审核
  12. * Class AuditSupplier
  13. * @package App\Admin\Extensions\Grid
  14. */
  15. class IndustryOrderAudit extends RowAction
  16. {
  17. private $action;
  18. public function __construct($title = null, $action = 1)
  19. {
  20. parent::__construct($title);
  21. $this->action = $action; //$action:1=通过;2=拒绝
  22. $this->title = $action == 1 ? '通过' : '拒绝';
  23. }
  24. protected function html()
  25. {
  26. $class = $this->action == 1 ? 'btn btn-sm btn-success' : 'btn btn-sm btn-danger';
  27. $this->appendHtmlAttribute('class', $class);
  28. $this->defaultHtmlAttribute('href', 'javascript:;');
  29. return "<a {$this->formatHtmlAttributes()}>{$this->title}</a>";
  30. }
  31. protected function actionScript()
  32. {
  33. if ($this->action == 2) {
  34. return <<<JS
  35. function (data, target, action) {
  36. data.audit_opinion = prompt('请输入拒绝理由');
  37. if (!data.audit_opinion) {
  38. return false;
  39. }
  40. }
  41. JS;
  42. }
  43. return parent::actionScript();
  44. }
  45. public function handle(Request $request)
  46. {
  47. $id = $this->getKey();
  48. $status = $request->action == 1 ? ProductStatus::ON_SALE : ProductStatus::REFUSE;
  49. DB::beginTransaction();
  50. try {
  51. $order = IndustryOrder::with(['industryProduct', 'spec'])
  52. ->where('audit_status', 0)->find($id);
  53. if (!$order) {
  54. throw new \Exception('订单不存在或已经审核过了');
  55. }
  56. if (!is_null($request->audit_opinion)) {
  57. $order->audit_opinion = $request->audit_opinion;
  58. }
  59. $order->audit_status = $status;
  60. $order->save();
  61. # 如果是拒绝,产品及规格加回库存
  62. if ($status == ProductStatus::REFUSE && !empty($order->industryProduct) && !empty($order->spec)) {
  63. $order->industryProduct->stock += $order->num;
  64. $order->industryProduct->save();
  65. $order->spec->stock += $order->num;
  66. $order->spec->save();
  67. }
  68. DB::commit();
  69. return $this->response()->success('操作成功')->refresh();
  70. } catch (\Exception $e) {
  71. DB::rollBack();
  72. return $this->response()->error($e->getMessage())->refresh();
  73. }
  74. }
  75. public function confirm()
  76. {
  77. if ($this->action == 1) {
  78. return ['确定要设置为已通过吗?', ''];
  79. }
  80. }
  81. public function parameters()
  82. {
  83. return [
  84. 'action' => $this->action,
  85. ];
  86. }
  87. }