LivingAllowance.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. namespace app\enterprise\controller;
  3. use app\enterprise\common\EnterpriseController;
  4. use think\facade\Db;
  5. use think\facade\Log;
  6. use app\common\api\EnterpriseApi;
  7. use app\common\api\TalentLogApi;
  8. use think\exception\ValidateException;
  9. use app\enterprise\validate\LivingAllowance as LivingAllowanceValidator;
  10. use app\common\state\ProjectState;
  11. use app\common\state\LivingAllowanceState as LaState;
  12. use app\common\api\BatchApi;
  13. use app\enterprise\api\LivingAllowanceApi as EnterpriseLaApi;
  14. use app\common\api\LivingAllowanceApi as CommonLaApi;
  15. use app\common\model\LivingAllowance as LaModel;
  16. use app\common\model\TalentLog;
  17. /**
  18. * Description of LivingAllowance
  19. * 硕博人才生活补贴申请
  20. * @author sgq
  21. */
  22. class LivingAllowance extends EnterpriseController {
  23. public function index() {
  24. return view("", ['type' => session("user")['type']]);
  25. }
  26. public function list() {
  27. $res = EnterpriseLaApi::getList($this->request);
  28. return json($res);
  29. }
  30. /**
  31. * 申请
  32. */
  33. public function apply(\think\Request $request) {
  34. $type = $this->user["type"];
  35. $param = $request->param();
  36. $id = isset($param["id"]) ? $param["id"] : 0;
  37. $info = CommonLaApi::getInfoById($id);
  38. $ep = EnterpriseApi::getOne($this->user["uid"]);
  39. if (!chkEnterpriseFull($ep))
  40. return;
  41. /* if ($info && !in_array($info["checkState"], [LaState::LA_SAVE, LaState::LA_FIRST_REJECT])) {
  42. return view("", ["row" => $info, "enterprise" => $ep, "hand" => "select"]);
  43. } */
  44. if ($request->isPost()) {
  45. return $this->save($info, $request);
  46. }
  47. $hand = $info ? "update" : "add";
  48. $batch = $info["year"] ?: BatchApi::getValidBatch(ProjectState::LIVINGALLOWANCE, $this->user["type"])["batch"];
  49. return view("", ["year" => $batch, "row" => $info, "enterprise" => $ep, "hand" => $hand]);
  50. }
  51. public function detail(\think\Request $request) {
  52. $param = $request->param();
  53. $id = $param["id"];
  54. $info = CommonLaApi::getInfoById($id);
  55. $ep = EnterpriseApi::getOne($this->user["uid"]);
  56. if (!chkEnterpriseFull($ep))
  57. return;
  58. return view("apply", ["row" => $info, "enterprise" => $ep, "hand" => "select"]);
  59. }
  60. private function other_validate($info) {
  61. $responseObj = new \stdClass();
  62. /* 校验姓名是否与人才库一致 */
  63. $where = [];
  64. $where[] = ["checkState", ">=", \app\common\api\TalentState::REVERIFY_PASS];
  65. $where[] = ["checkState", "not in", [\app\common\api\TalentState::REVERIFY_REJECT, \app\common\api\TalentState::REVERIFY_FAIL]];
  66. $where[] = ["card_number", "=", $info["idCard"]];
  67. $talentInfo = Db::table("new_talent_info")->where($where)->order("createTime desc")->find();
  68. if ($talentInfo && $talentInfo["name"] != $info["name"]) {
  69. $responseObj->msg = "该证件号码的姓名与人才库姓名不匹配!";
  70. return $responseObj;
  71. }
  72. $year = substr($info["year"], 0, 4);
  73. /* 查询当前批次审核通过的数量以及当前批次未审核完成的数量 */
  74. $where = [];
  75. $where[] = ["year", "like", "{$year}%"];
  76. $where[] = ["idCard", "=", $info["idCard"]];
  77. $where[] = ["checkState", "<>", LaState::LA_NOTPASS];
  78. if ($info["id"]) {
  79. $where[] = ["id", "<>", $info["id"]];
  80. }
  81. $nowYearApplyCount = LaModel::where($where)->count();
  82. if ($nowYearApplyCount > 0) {
  83. $responseObj->msg = "不能重复申报";
  84. return $responseObj;
  85. }
  86. if (!$info["id"]) {
  87. $passYears = CommonLaApi::getPassYearsByIdCard($info["idCard"]);
  88. if (in_array($year, $passYears)) {
  89. return $responseObj->msg = "您今年度已经申报过了,每年度仅能享受一次生活补贴!";
  90. }
  91. if (count($passYears) >= 3) {
  92. return $responseObj->msg = "一个人最多享受三次!";
  93. }
  94. }
  95. $responseObj->code = 200;
  96. return $responseObj;
  97. }
  98. private function validateIsEdit($checkState) {
  99. $responseObj = new \stdClass();
  100. if ($checkState != LaState::LA_SAVE && $checkState != LaState::LA_BEFORE_REJECT && $checkState != LaState::LA_FIRST_REJECT) {
  101. if ($checkState == LaState::LA_NOTPASS) {
  102. $responseObj->msg = "您的申报审核不通过,无法操作";
  103. } else if ($checkState >= LaState::LA_PASS) {
  104. $responseObj->msg = "您的申报已审核通过,无法操作";
  105. } else {
  106. $responseObj->msg = "您的申报正在审核中,请耐心等待";
  107. }
  108. $responseObj->code = 500;
  109. return $responseObj;
  110. }
  111. $responseObj->code = 200;
  112. return $responseObj;
  113. }
  114. /**
  115. * 提交表单
  116. */
  117. public function submitToCheck() {
  118. try {
  119. $id = $this->request["id"];
  120. $info = CommonLaApi::getInfoById($id);
  121. if (!$info) {
  122. throw new ValidateException("提交审核失败,请先填写基础信息");
  123. }
  124. if ($info["enterpriseId"] != $this->user["uid"]) {
  125. throw new ValidateException("没有对应的人才认定申报信息");
  126. }
  127. $batch = BatchApi::checkBatchValid(["type" => ProjectState::LIVINGALLOWANCE, "year" => $info["year"], "first_submit_time" => $info["firstSubmitTime"]], $this->user["type"]);
  128. if ($batch["code"] != 200) {
  129. throw new ValidateException($batch["msg"]);
  130. }
  131. $response = $this->validateIsEdit($info["checkState"]);
  132. if ($response->code != 200) {
  133. throw new ValidateException($response->msg);
  134. }
  135. validate(LivingAllowanceValidator::class)->check($info);
  136. $response = $this->other_validate($info);
  137. if ($response->code != 200) {
  138. throw new ValidateException($response->msg);
  139. }
  140. $where = [];
  141. $where[] = ["mainId", "=", $id];
  142. $where[] = ["type", "=", ProjectState::LIVINGALLOWANCE];
  143. $uploadedFileTypes = Db::table("new_talent_file")->where($where)->column("distinct typeId");
  144. $where = [];
  145. $where[] = ["project", "=", ProjectState::LIVINGALLOWANCE];
  146. $where[] = ["type", "=", $this->user["type"]];
  147. $where[] = ["must", "=", 1];
  148. $where[] = ["active", "=", 1];
  149. $where[] = ["delete", "=", 0];
  150. $where[] = ["id", "not in", $uploadedFileTypes];
  151. $unUploadfiletypes = Db::table("new_common_filetype")->where($where)->select()->toArray();
  152. if ($unUploadfiletypes) {
  153. $msg = "以下附件为必传:<br>";
  154. foreach ($unUploadfiletypes as $ft) {
  155. $msg .= "<span style='color:red;'>*</span>" . $ft["name"] . "<br>";
  156. }
  157. throw new ValidateException($msg);
  158. }
  159. $data["id"] = $id;
  160. $data["checkState"] = LaState::LA_NEED_FIRST_CHECK;
  161. if (!$info["firstSubmitTime"]) {
  162. $data["firstSubmitTime"] = date("Y-m-d H:i:s");
  163. }
  164. $data["newSubmitTime"] = date("Y-m-d H:i:s");
  165. $data["checkMsg"] = "";
  166. $data["files"] = "";
  167. $data["fields"] = "";
  168. $res = LaModel::update($data);
  169. if ($res) {
  170. $user = session("user");
  171. $log["id"] = getStringId();
  172. $log["active"] = 1;
  173. $log["state"] = 1;
  174. $log["step"] = 0;
  175. $log["stateChange"] = LaState::getStateDesc($info["checkState"]) . "->" . LaState::getStateDesc($data["checkState"]);
  176. $log["type"] = ProjectState::LIVINGALLOWANCE;
  177. $log["mainId"] = $id;
  178. $log["description"] = "确认提交审核";
  179. $log["createUser"] = $user ? sprintf("%s(%s)", $user["account"], $user["companyName"] ?: $user["rolename"]) : "系统";
  180. $log["createTime"] = date("Y-m-d H:i:s");
  181. TalentLog::create($log);
  182. return json(["code" => 200, "msg" => "提交审核成功"]);
  183. }
  184. throw new ValidateException("提交审核失败");
  185. } catch (ValidateException $e) {
  186. $responseObj = new \stdClass();
  187. $responseObj->code = 500;
  188. $responseObj->msg = $e->getMessage();
  189. return json($responseObj);
  190. } catch (\think\Exception $e) {
  191. $res = ["code" => 500, "msg" => "发生预料外错误,请联系管理员处理,错误代码:" . $e->getCode()];
  192. $logInfo = [
  193. "enterprise_id" => $this->user["uid"],
  194. "data" => $data,
  195. "controller" => $this->request->controller(),
  196. "action" => $this->request->action(),
  197. "errCode" => $e->getCode(),
  198. "errMsg" => $e->getMessage()
  199. ];
  200. Log::write($logInfo, "error");
  201. return json($res);
  202. }
  203. }
  204. public function save($info, \think\Request $request) {
  205. try {
  206. $batch = BatchApi::checkBatchValid(["type" => ProjectState::LIVINGALLOWANCE, "year" => $info["year"], "first_submit_time" => $info["firstSubmitTime"]], $this->user["type"]);
  207. if ($batch["code"] != 200) {
  208. throw new ValidateException($batch["msg"]);
  209. }
  210. if ($info) {
  211. $response = $this->validateIsEdit($info["checkState"]);
  212. if ($response->code != 200) {
  213. throw new ValidateException($response->msg);
  214. }
  215. }
  216. $data = $request->param();
  217. $data["year"] = $batch["batch"];
  218. validate(LivingAllowanceValidator::class)->check($data);
  219. $response = $this->other_validate($data);
  220. if ($response->code != 200) {
  221. throw new ValidateException($response->msg);
  222. }
  223. $id = $data["id"];
  224. if ($id) {
  225. if (!$info || $info["id"] != $id || $info["enterpriseId"] != $this->user["uid"]) {
  226. throw new ValidateException("没有对应的人才认定申报信息");
  227. }
  228. $data["checkState"] = $info["checkState"];
  229. } else {
  230. $data["id"] = getStringId();
  231. $data["active"] = 1;
  232. $data["checkState"] = LaState::LA_SAVE;
  233. $data["isPublic"] = 1;
  234. $data["recommendIsPay"] = 0;
  235. $data["isPay"] = 0;
  236. $data["createTime"] = date("Y-m-d H:i:s");
  237. $data["createUser"] = $this->user["uid"];
  238. $data["creditStatus"] = 1;
  239. $count = count(CommonLaApi::getPassYearsByIdCard($data["idCard"]));
  240. $data["applyCount"] = $count + 1;
  241. if ($data["applyCount"] > 1 && !$data["personalTax"]) {
  242. throw new ValidateException("非首年申报必须填写个税缴交情况!");
  243. }
  244. }
  245. $checkState = $data["checkState"];
  246. $data["photo"] = $info["photo"];
  247. if ($request->file()) {
  248. $headimg = $request->file("file");
  249. $upload = new \app\common\api\UploadApi();
  250. $result = $upload->uploadOne($headimg, "image", "talent/photo");
  251. $file = imagecreatefromstring(file_get_contents("storage/" . $result->filepath));
  252. $width = imagesx($file);
  253. $height = imagesy($file);
  254. //免冠二寸照长宽413:579
  255. if ($width * 579 != $height * 413) {
  256. @unlink("storage/" . $result->filepath); //像素不符合,删除上传文件
  257. throw new ValidateException("近期免冠半身彩照(二寸)不符合二寸像素标准。*<span style='color:#ff0000;'>二寸像素标准[413*579]</span>");
  258. }
  259. if ($info && $info["photo"]) {
  260. //如果新照片符合像素要求,则删除旧照片
  261. $old_head_url = "storage/" . $info["photo"];
  262. if (file_exists($old_head_url))
  263. @unlink($old_head_url);
  264. }
  265. $data["photo"] = $result->filepath;
  266. }
  267. if (!$data["photo"])
  268. throw new ValidateException("请上传头像。*<span style='color:#ff0000;'>二寸像素标准[413*579]</span>");
  269. if ($info["checkState"] == LaState::LA_FIRST_REJECT) {
  270. //驳回,需要判断什么字段可以提交
  271. $modify_fields = array_filter(explode(",", $info["fields"]));
  272. $tmp_data = $data;
  273. $data = [];
  274. foreach ($modify_fields as $field) {
  275. $data[$field] = $tmp_data[$field];
  276. }
  277. $data["id"] = $id;
  278. }
  279. if ($id) {
  280. $res = LaModel::update($data);
  281. } else {
  282. $res = LaModel::insertGetId($data);
  283. }
  284. if ($res) {
  285. return json(["code" => 200, "msg" => "保存成功", "obj" => ["id" => $data["id"], "checkState" => $checkState]]);
  286. }
  287. throw new ValidateException("保存失败");
  288. } catch (ValidateException $e) {
  289. $responseObj = new \stdClass();
  290. $responseObj->code = 500;
  291. $responseObj->msg = $e->getMessage();
  292. return json($responseObj);
  293. } catch (\think\Exception $e) {
  294. $res = ["code" => 500, "msg" => "发生预料外错误,请联系管理员处理,错误代码:" . $e->getCode()];
  295. $logInfo = [
  296. "enterprise_id" => $this->user["uid"],
  297. "data" => $data,
  298. "controller" => $this->request->controller(),
  299. "action" => $this->request->action(),
  300. "errCode" => $e->getCode(),
  301. "errMsg" => $e->getMessage()
  302. ];
  303. Log::write($logInfo, "error");
  304. return json($res);
  305. }
  306. }
  307. public function delete() {
  308. $id = $this->request->param("id");
  309. $info = CommonLaApi::getInfoById($id);
  310. if (!$info || $info["enterpriseId"] != $this->user["uid"]) {
  311. return json(["msg" => "操作失败"]);
  312. }
  313. $checkState = $info["checkState"];
  314. if ($checkState != LaState::LA_SAVE) {
  315. return json(["msg" => "该申报已提交审核,无法删除"]);
  316. }
  317. TalentModel::delete($id);
  318. $where = [["mainId", "=", $id], ["type", "=", ProjectState::LIVINGALLOWANCE]];
  319. $list = Db::table("new_talent_file")->where($where)->select()->toArray();
  320. foreach ($list as $key => $file) {
  321. if (!empty($file["url"])) {
  322. $filepath = "storage/" . $file["url"];
  323. if (file_exists($filepath)) {
  324. @unlink($filepath);
  325. }
  326. }
  327. Db::table("new_talent_file")->delete($file["id"]);
  328. }
  329. return json(["msg" => "删除成功"]);
  330. }
  331. }