Application.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. namespace app\admin\controller\inspection;
  3. use app\common\controller\Backend;
  4. use app\common\Service\InspectionService;
  5. use app\common\model\inspection\InspectionApplication;
  6. /**
  7. * 验货员申请管理
  8. */
  9. class Application extends Backend
  10. {
  11. /**
  12. * InspectionApplication模型对象
  13. */
  14. protected $model = null;
  15. protected $relationSearch = true;
  16. protected $searchFields = 'name,phone';
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new InspectionApplication;
  21. $this->view->assign("auditStatusList", $this->model->getAuditStatusList());
  22. }
  23. /**
  24. * 查看列表
  25. */
  26. public function index()
  27. {
  28. if ($this->request->isAjax()) {
  29. // 设置关联查询
  30. $this->relationSearch = true;
  31. // 构建查询条件
  32. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  33. $list = $this->model
  34. ->with(['user', 'supplier'])
  35. ->where($where)
  36. ->order($sort, $order)
  37. ->paginate($limit);
  38. foreach ($list as $item) {
  39. $item->append(['audit_status_text', 'apply_time_text', 'audit_time_text']);
  40. $item->user->visible(['id', 'username', 'nickname', 'mobile']);
  41. if ($item->supplier) {
  42. $item->supplier->visible(['id', 'name', 'contact', 'mobile']);
  43. }
  44. }
  45. $result = array("total" => $list->total(), "rows" => $list->items());
  46. return json($result);
  47. }
  48. return $this->view->fetch();
  49. }
  50. /**
  51. * 查看详情
  52. */
  53. public function detail($ids = null)
  54. {
  55. $row = $this->model->get($ids);
  56. if (!$row) {
  57. $this->error(__('No Results were found'));
  58. }
  59. $row->append(['audit_status_text', 'apply_time_text', 'audit_time_text','supplier_name']);
  60. $this->view->assign("row", $row);
  61. return $this->view->fetch();
  62. }
  63. /**
  64. * 审核申请
  65. */
  66. public function audit($ids = null)
  67. {
  68. $row = $this->model->get($ids);
  69. if (!$row) {
  70. $this->error(__('No Results were found'));
  71. }
  72. if ($row->audit_status != InspectionApplication::AUDIT_STATUS_PENDING) {
  73. $this->error('该申请已被审核,无法重复操作');
  74. }
  75. if ($this->request->isPost()) {
  76. $auditStatus = $this->request->post('audit_status');
  77. $rejectReason = $this->request->post('reject_reason', '', 'trim');
  78. $supplierId = $this->request->post('supplier_id', 0, 'intval');
  79. // 审核通过时必须选择供应商
  80. if ($auditStatus == InspectionApplication::AUDIT_STATUS_PASSED && empty($supplierId)) {
  81. $this->error('审核通过时必须选择绑定的供应商');
  82. }
  83. $result = InspectionService::auditApplication($ids, $auditStatus, $rejectReason, $supplierId);
  84. if ($result) {
  85. $this->success('审核成功');
  86. } else {
  87. $this->error('审核失败');
  88. }
  89. }
  90. $row->append(['audit_status_text', 'apply_time_text']);
  91. // 加载供应商信息
  92. if ($row->supplier_id) {
  93. $row->load(['supplier']);
  94. }
  95. $this->view->assign("row", $row);
  96. $this->view->assign("auditStatusList", $this->model->getAuditStatusList());
  97. return $this->view->fetch();
  98. }
  99. /**
  100. * 批量审核通过
  101. */
  102. public function batchPass($ids = null)
  103. {
  104. if (!$this->request->isPost()) {
  105. $this->error(__("Invalid parameters"));
  106. }
  107. $ids = $ids ? $ids : $this->request->post("ids");
  108. if (!$ids) {
  109. $this->error(__('Parameter %s can not be empty', 'ids'));
  110. }
  111. $pk = $this->model->getPk();
  112. $adminIds = $this->getDataLimitAdminIds();
  113. if (is_array($adminIds)) {
  114. $this->model->where($this->dataLimitField, 'in', $adminIds);
  115. }
  116. $list = $this->model->where($pk, 'in', $ids)->select();
  117. $count = 0;
  118. $successIds = [];
  119. \think\Db::startTrans();
  120. try {
  121. foreach ($list as $item) {
  122. if ($item->audit_status == InspectionApplication::AUDIT_STATUS_PENDING) {
  123. $item->audit_status = InspectionApplication::AUDIT_STATUS_PASSED;
  124. $item->audit_time = time();
  125. $item->reject_reason = '';
  126. $item->save();
  127. $count++;
  128. $successIds[] = $item->id;
  129. }
  130. }
  131. \think\Db::commit();
  132. } catch (\Exception $e) {
  133. \think\Db::rollback();
  134. $this->error($e->getMessage());
  135. }
  136. $this->success("成功审核通过{$count}个申请");
  137. }
  138. /**
  139. * 批量审核驳回
  140. */
  141. public function batchReject($ids = null)
  142. {
  143. if (!$this->request->isPost()) {
  144. $this->error(__("Invalid parameters"));
  145. }
  146. $ids = $ids ? $ids : $this->request->post("ids");
  147. $rejectReason = $this->request->post("reject_reason", '', 'trim');
  148. if (!$ids) {
  149. $this->error(__('Parameter %s can not be empty', 'ids'));
  150. }
  151. if (empty($rejectReason)) {
  152. $this->error('驳回原因不能为空');
  153. }
  154. $pk = $this->model->getPk();
  155. $adminIds = $this->getDataLimitAdminIds();
  156. if (is_array($adminIds)) {
  157. $this->model->where($this->dataLimitField, 'in', $adminIds);
  158. }
  159. $list = $this->model->where($pk, 'in', $ids)->select();
  160. $count = 0;
  161. \think\Db::startTrans();
  162. try {
  163. foreach ($list as $item) {
  164. if ($item->audit_status == InspectionApplication::AUDIT_STATUS_PENDING) {
  165. $item->audit_status = InspectionApplication::AUDIT_STATUS_REJECTED;
  166. $item->audit_time = time();
  167. $item->reject_reason = $rejectReason;
  168. $item->save();
  169. $count++;
  170. }
  171. }
  172. \think\Db::commit();
  173. } catch (\Exception $e) {
  174. \think\Db::rollback();
  175. $this->error($e->getMessage());
  176. }
  177. $this->success("成功驳回{$count}个申请");
  178. }
  179. /**
  180. * 删除
  181. */
  182. public function del($ids = "")
  183. {
  184. if (!$this->request->isPost()) {
  185. $this->error(__("Invalid parameters"));
  186. }
  187. $ids = $ids ? $ids : $this->request->post("ids");
  188. if ($ids) {
  189. $pk = $this->model->getPk();
  190. $adminIds = $this->getDataLimitAdminIds();
  191. if (is_array($adminIds)) {
  192. $this->model->where($this->dataLimitField, 'in', $adminIds);
  193. }
  194. $list = $this->model->where($pk, 'in', $ids)->select();
  195. $count = 0;
  196. \think\Db::startTrans();
  197. try {
  198. foreach ($list as $k => $v) {
  199. $count += $v->delete();
  200. }
  201. \think\Db::commit();
  202. } catch (\PDOException $e) {
  203. \think\Db::rollback();
  204. $this->error($e->getMessage());
  205. } catch (\Exception $e) {
  206. \think\Db::rollback();
  207. $this->error($e->getMessage());
  208. }
  209. if ($count) {
  210. $this->success();
  211. } else {
  212. $this->error(__('No rows were deleted'));
  213. }
  214. }
  215. $this->error(__('Parameter %s can not be empty', 'ids'));
  216. }
  217. }