TalentAllowanceApi.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php
  2. namespace app\common\api;
  3. use app\common\model\TalentAllowance as TaModel;
  4. use app\common\state\MainState;
  5. use think\facade\Db;
  6. use app\common\state\CommonConst;
  7. use app\common\state\AllowanceProjectEnum;
  8. use app\common\model\TalentAllowanceArrange;
  9. use app\common\model\AmountStandard as AsModel;
  10. /**
  11. * Description of TalentAllowanceApi
  12. *
  13. * @author sgq
  14. */
  15. class TalentAllowanceApi {
  16. public static function getList($params) {
  17. $user = session("user");
  18. $order = trim($params["order"]) ?: "desc";
  19. $offset = trim($params["offset"]) ?: 0;
  20. $limit = trim($params["limit"]) ?: 10;
  21. $where = [];
  22. $where[] = ["enterpriseId", "=", $user["uid"]];
  23. if ($_where = self::setTalentAllowanceInfo($params)) {
  24. $where = array_merge($where, $_where);
  25. }
  26. $count = TaModel::where($where)->count();
  27. $list = TaModel::where($where)->limit($offset, $limit)->order("year {$order},createTime {$order}")->select()->toArray();
  28. $levelList = DictApi::selectByParentCode("talent_arrange");
  29. $talentTypeList = DictApi::selectByParentCode("enterprise_tag");
  30. $streetList = DictApi::selectByParentCode("street");
  31. $identifyConditionIds = array_filter(array_unique(array_column($list, "identifyCondition")));
  32. $whr[] = ["id", "in", $identifyConditionIds];
  33. $whr[] = ["type", "=", $user["type"]];
  34. $identifyConditionKvList = TalentConditionApi::getKvList($whr);
  35. foreach ($list as $key => $item) {
  36. $list[$key]["talentArrangeName"] = $levelList[$item["talentArrange"]];
  37. $list[$key]["talentTypeName"] = $talentTypeList[$item["talentType"]];
  38. $list[$key]["addressName"] = $streetList[$item["address"]];
  39. $list[$key]["identifyConditionText"] = $identifyConditionKvList[$item["identifyCondition"]];
  40. }
  41. return ["total" => $count, "rows" => $list];
  42. }
  43. public static function setTalentAllowanceInfo($params) {
  44. $where = [];
  45. if (\StrUtil::isNotEmpAndNull($params["year"])) {
  46. $where[] = ["year", "=", $params["year"]];
  47. }
  48. if (\StrUtil::isNotEmpAndNull($params["enterpriseName"])) {
  49. $where[] = ["enterpriseName", "like", "%" . $params["enterpriseName"] . "%"];
  50. }
  51. if (\StrUtil::isNotEmpAndNull($params["name"])) {
  52. $where[] = ["name", "like", "%" . $params["name"] . "%"];
  53. }
  54. if (\StrUtil::isNotEmpAndNull($params["talentType"])) {
  55. $where[] = ["talentType", "=", $params["talentType"]];
  56. }
  57. if (\StrUtil::isNotEmpAndNull($params["talentArrange"])) {
  58. $where[] = ["talentArrange", "=", $params["talentArrange"]];
  59. }
  60. if (\StrUtil::isNotEmpAndNull($params["identiryCondition"])) {
  61. $where[] = ["identifyCondition", "=", $params["identifyCondition"]];
  62. }
  63. if (\StrUtil::isNotEmpAndNull($params["allowanceType"])) {
  64. $where[] = ["allowanceType", "=", $params["allowanceType"]];
  65. }
  66. if (\StrUtil::isNotEmpAndNull($params["address"])) {
  67. $where[] = ["address", "=", $params["address"]];
  68. }
  69. return $where;
  70. }
  71. public static function getInfoById($id) {
  72. return TaModel::findOrEmpty($id)->toArray();
  73. }
  74. public static function getApplyCountByIdCard($idCard) {
  75. $where = [];
  76. $where[] = ["idCard", "=", $idCard];
  77. $where[] = ["checkState", "<>", MainState::NOTPASS];
  78. $list = TaModel::where($where)->distinct(true)->field("substr(year,1,4) as year")->select()->toArray();
  79. $years = array_column($list, "year");
  80. return $years;
  81. }
  82. public static function getPassYearsByIdCard($idCard) {
  83. $where = [];
  84. $where[] = ["idCard", "=", $idCard];
  85. $where[] = ["checkState", "=", MainState::PASS];
  86. $list = TaModel::where($where)->distinct(true)->field("substr(year,1,4) as year")->select()->toArray();
  87. $passYears = array_column($list, "year");
  88. return $passYears;
  89. }
  90. public static function validateAllowanceType($id) {
  91. $info = ["id" => $id];
  92. $old = self::getInfoById($id);
  93. $info["type"] = $old["type"];
  94. $enterpriseMap = \app\common\model\Enterprise::where("type", $old["type"])->column("name", "id");
  95. /* * 查询工作单位记录 */
  96. $initDetailList = \app\common\model\TalentAllowancecontractDetail::where("mainId", $info["id"])->select()->toArray();
  97. $detaiPdList = []; //用于判定类型
  98. $detailMonthList = []; //用于计算月份
  99. foreach ($initDetailList as $detail) {
  100. $projectList = \app\common\model\TalentAllowanceProject::where("baseId", $detail["id"])->select()->toArray();
  101. $projectMap = [];
  102. foreach ($projectList as $project) {
  103. $projectMap[$project["project"]] = $project;
  104. }
  105. $detail["list"] = $projectList;
  106. $detail["projectMap"] = $projectMap;
  107. $detail["enterpriseName"] = $enterpriseMap[$detail["enterpriseId"]];
  108. /* * 筛选符合条件的人才标签 */
  109. $detaiPdList[] = $detail;
  110. $detailMonthList[] = $detail;
  111. }
  112. $monthMap = self::mergeMonth($detailMonthList);
  113. $monthAndDayMap = self::mergeMonthNeedDay($detailMonthList);
  114. $info["recommendAllowanceType"] = 1;
  115. $info["recommendAllowanceMsg"] = [];
  116. $projectList = \app\common\model\TalentAllowanceProject::where("mainId", $id)->select()->toArray();
  117. $set = self::valideAllowanceType($info, $projectList, $monthMap, $monthAndDayMap);
  118. $info["recommendAllowanceMsg"][] = "综合以上所有判断得到最终补贴类型为:";
  119. if ($info["recommendAllowanceType"] == 1) {
  120. $info["recommendAllowanceMsg"][] = "<span style='color:red;font-weight:bold;'>工作津贴</span>;";
  121. $info["recommendAllowanceMsg"][] = "可享受月份为:";
  122. $info["recommendAllowanceMsg"][] = "<span style='color:red;font-weight:bold;'>" . implode(",", $set) . "</span>;";
  123. }
  124. if ($info["recommendAllowanceType"] == 2)
  125. $info["recommendAllowanceMsg"][] = "<span style='color:red;font-weight:bold;'>一次性交通补贴</span>;";
  126. if ($info["recommendAllowanceType"] == 3)
  127. $info["recommendAllowanceMsg"][] = "<span style='color:red;font-weight:bold;'>不予兑现</span>;";
  128. usort($set, function($a, $b) {
  129. return (int) $a - (int) $b;
  130. });
  131. $info["recommendMonths"] = implode(",", $set);
  132. self::calculateAllowance($info, $set, $detailMonthList);
  133. return $info;
  134. }
  135. /**
  136. * 集成电路津补贴总校验
  137. * */
  138. private static function valideAllowanceType(&$info, $projectList, $monthMap, $monthAndDayMap) {
  139. $set = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"];
  140. /* * 2.判定工作月份、五险和个税是否满足重叠6个月要求* */
  141. $workdaySet = self::chkMonths($monthMap[AllowanceProjectEnum::PROJECT_ATTENDANCE], $info, "上年度工作月份", "①");
  142. $pensionSet = self::chkMonths($monthMap[AllowanceProjectEnum::PROJECT_SB_PENSION], $info, "养老保险", "②");
  143. $unemploymentSet = self::chkMonths($monthMap[AllowanceProjectEnum::PROJECT_SB_UNEMPLOYMENT], $info, "失业保险", "③");
  144. $medicaSet = self::chkMonths($monthMap[AllowanceProjectEnum::PROJECT_SB_MEDICA], $info, "医疗保险", "④");
  145. $taxSet = self::chkMonths($monthMap[AllowanceProjectEnum::PROJECT_TAX], $info, "个税", "⑤");
  146. $set = array_intersect($set, $workdaySet);
  147. $set = array_intersect($set, $pensionSet);
  148. $set = array_intersect($set, $unemploymentSet);
  149. $set = array_intersect($set, $medicaSet);
  150. $set = array_intersect($set, $taxSet);
  151. usort($set, function($a, $b) {
  152. return (int) $a - (int) $b;
  153. });
  154. if ($info["recommendAllowanceType"] == 1) {
  155. if (count($set) < 6) {
  156. //如果全部满足6个月,但是重叠时间不满足6个月,开始检测是否符合交通补贴要求
  157. $info["recommendAllowanceType"] = 2;
  158. $info["recommendAllowanceMsg"][] = "社会保险、个税、上年度工作月份交集月份不满足6个月(" . implode(",", $set) . "),无法享受工作津贴(×)";
  159. } else {
  160. //检查连续缴纳月份是否满足6个月
  161. $count = 1;
  162. foreach ($set as $s) {
  163. $currentVal = intval($s);
  164. $nextVal = $currentVal + 1;
  165. $nextKey = str_pad($nextVal, 2, "0", STR_PAD_LEFT);
  166. if (in_array($nextKey, $set)) {
  167. $count++;
  168. if ($count >= 6) {
  169. break;
  170. }
  171. } else {
  172. $count = 1;
  173. }
  174. }
  175. if ($count < 6) {
  176. $info["recommendAllowanceType"] = 2;
  177. $info["recommendAllowanceMsg"][] = "社会保险、个税、上年度工作月份交集月份不满足连续缴纳6个月(" . implode(",", $set) . "),无法享受工作津贴(×)";
  178. }
  179. }
  180. }
  181. if ($info["recommendAllowanceType"] == 2) {
  182. //判断境内工作时间是否大于30天
  183. if ($info["allowanceType"] == 2) {
  184. $totalDays = 0;
  185. $workmonths = $monthAndDayMap[AllowanceProjectEnum::PROJECT_ATTENDANCE];
  186. foreach ($workmonths as $days) {
  187. $totalDays += $days;
  188. }
  189. if ($totalDays < 30) {
  190. $info["recommendAllowanceType"] = 3;
  191. $info["recommendAllowanceMsg"][] = "全年在我市工作仅{$totalDays}天,未达到30天,无法享受一次性交通津贴(×)";
  192. }
  193. } else {
  194. if (count($workdaySet) == 0) {
  195. $info["recommendAllowanceType"] = 3;
  196. $info["recommendAllowanceMsg"][] = "全年在我市工作未达到30天,无法享受一次性交通津贴(×)";
  197. } else {
  198. $info["recommendAllowanceType"] = 2;
  199. $info["recommendAllowanceMsg"][] = "*首选为工作津贴,未录入实际工作天数,需要进一步核实提交的相关附件是否满足全年在我市工作30天";
  200. }
  201. }
  202. }
  203. return $set;
  204. }
  205. /**
  206. * 计算津补贴
  207. */
  208. private static function calculateAllowance(&$info, $retainMonths, $detailMonthList) {
  209. /* * 查询人才层次变更记录 */
  210. $arrangeList = TalentAllowanceArrange::where("mainId", $info["id"])->order("talentArrange")->select()->toArray();
  211. foreach ($arrangeList as &$arrange) {
  212. $where = [];
  213. $where[] = ["type", "=", $info["type"]];
  214. $where[] = ["allowanceType", "in", [1, 2]];
  215. $where[] = ["talentArrange", "=", $arrange["talentArrange"]];
  216. $list = AsModel::where($where)->field("money,allowanceType")->select()->toArray();
  217. foreach ($list as $amount) {
  218. if ($amount["allowanceType"] == 1) {
  219. $arrange["jobMoney"] = $amount["money"];
  220. }
  221. if ($amount["allowanceType"] == 2) {
  222. $arrange["jtMoney"] = $amount["money"];
  223. }
  224. }
  225. }unset($arrange);
  226. /* * * 容器 */
  227. $jobMoney = 0.00; //计算所得工作津贴
  228. $jtMoney = 0.00; //计算所得一次性交通补贴
  229. $recommendMonths = []; //推荐月份
  230. $talentArrange = null;
  231. /* * *********计算************* */
  232. $msgBulider = [];
  233. switch ($info["recommendAllowanceType"]) {
  234. case 1:
  235. foreach ($arrangeList as &$arrange) {
  236. if (\StrUtil::isNotEmpAndNull($arrange["prepareMonths"])) {
  237. $levelList = array_filter(explode(",", $arrange["prepareMonths"]));
  238. $levelList = array_intersect($levelList, $retainMonths);
  239. $total = round($arrange["jobMoney"] * count($levelList), 2);
  240. $jobMoney += $total;
  241. $msgBulider[] = sprintf("%d(%s)x%s(第%d层次)", count($levelList), $levelList ? implode(",", $levelList) : "", $arrange["jobMoney"], $arrange["talentArrange"]);
  242. $recommendMonths = array_merge($recommendMonths, $levelList);
  243. usort($recommendMonths, function($a, $b) {
  244. return (int) $a - (int) $b;
  245. });
  246. $arrange["months"] = implode(",", $levelList);
  247. $arrange["count"] = count($levelList);
  248. $arrange["total"] = $total;
  249. } else {
  250. $msgBulider[] = sprintf("0()x%s(第%d层次)", $arrange["jobMoney"], $arrange["talentArrange"]);
  251. $arrange["count"] = 0;
  252. $arrange["total"] = 0.00;
  253. }
  254. }unset($arrange);
  255. $info["recommendMonths"] = implode(",", $recommendMonths);
  256. $info["recommendAllowanceMsg"][] = "通过与人才证书有效期取交集得到最终可享受月份:";
  257. $info["recommendAllowanceMsg"][] = "<span style='color:red;font-weight:bold;'>" . implode(",", $recommendMonths) . "</span>;";
  258. $info["recommendAllowanceMsg"][] = "经过计算:人才津贴为<span style='color:red;font-weight:bold;'>" . $jobMoney . "</span>;";
  259. $info["recommendMoney"] = $jobMoney;
  260. $info["recommendMoneyDesc"] = implode("+", $msgBulider);
  261. break;
  262. case 2:
  263. foreach ($arrangeList as $arrange) {
  264. $jtMoney = $arrange["jtMoney"];
  265. $talentArrange = $arrange["talentArrange"];
  266. }
  267. $info["recommendMoney"] = $jtMoney;
  268. $info["recommendMonths"] = "";
  269. $info["recommendMoneyDesc"] = "一次性交通补贴";
  270. $info["workAllowanceMoney"] = 0.00;
  271. $info["developAllowanceMoney"] = 0.00;
  272. $info["recommendTalentArrange"] = $talentArrange;
  273. break;
  274. case 3:
  275. $info["recommendMoney"] = 0.00;
  276. $info["recommendMonths"] = "";
  277. $info["recommendMoneyDesc"] = "不予兑现";
  278. $info["workAllowanceMoney"] = 0.00;
  279. $info["developAllowanceMoney"] = 0.00;
  280. break;
  281. }
  282. return $arrangeList;
  283. }
  284. /**
  285. * 判定是否交足6个月(集成电路)
  286. * @param set
  287. * @param info
  288. * @param name
  289. */
  290. private static function chkMonths($set, &$info, $name, $sort) {
  291. if ($name == "上年度工作月份") {
  292. if ($info["allowanceType"] == 2) {
  293. $tmp = [];
  294. foreach ($set as $s) {
  295. $_s = explode("=", $s);
  296. $month = $_s[0];
  297. $days = $_s[1];
  298. if ($days > 0) {
  299. $tmp[] = $month;
  300. }
  301. }
  302. $set = $tmp;
  303. }
  304. }
  305. if ($info["recommendAllowanceType"] == 1 && (!$set || count($set) < 6)) {
  306. $info["recommendAllowanceType"] = 2;
  307. $info["recommendAllowanceMsg"][] = $sort . $name . "不足6个月,无法享受工作津贴(×)";
  308. }
  309. return $set;
  310. }
  311. //合并所有项目的月份
  312. private static function mergeMonth($detailList) {
  313. $taxList = [];
  314. $wagesList = [];
  315. $pensionList = [];
  316. $unemploymentList = [];
  317. $medicaList = [];
  318. $attendanceList = [];
  319. $workdayList = [];
  320. foreach ($detailList as $detail) {
  321. $projectMap = $detail["projectMap"];
  322. if ($projectMap[AllowanceProjectEnum::PROJECT_TAX] && \StrUtil::isNotEmpAndNull($projectMap[AllowanceProjectEnum::PROJECT_TAX]["months"])) {
  323. $tmp = array_filter(explode(",", $projectMap[AllowanceProjectEnum::PROJECT_TAX]["months"]));
  324. $taxList = array_merge($taxList, $tmp);
  325. }
  326. if ($projectMap[AllowanceProjectEnum::PROJECT_WAGES] && \StrUtil::isNotEmpAndNull($projectMap[AllowanceProjectEnum::PROJECT_WAGES]["months"])) {
  327. $tmp = array_filter(explode(",", $projectMap[AllowanceProjectEnum::PROJECT_WAGES]["months"]));
  328. $wagesList = array_merge($wagesList, $tmp);
  329. }
  330. if ($projectMap[AllowanceProjectEnum::PROJECT_SB_PENSION] && \StrUtil::isNotEmpAndNull($projectMap[AllowanceProjectEnum::PROJECT_SB_PENSION]["months"])) {
  331. $tmp = array_filter(explode(",", $projectMap[AllowanceProjectEnum::PROJECT_SB_PENSION]["months"]));
  332. $pensionList = array_merge($pensionList, $tmp);
  333. }
  334. if ($projectMap[AllowanceProjectEnum::PROJECT_SB_UNEMPLOYMENT] && \StrUtil::isNotEmpAndNull($projectMap[AllowanceProjectEnum::PROJECT_SB_UNEMPLOYMENT]["months"])) {
  335. $tmp = array_filter(explode(",", $projectMap[AllowanceProjectEnum::PROJECT_SB_UNEMPLOYMENT]["months"]));
  336. $unemploymentList = array_merge($unemploymentList, $tmp);
  337. }
  338. if ($projectMap[AllowanceProjectEnum::PROJECT_SB_MEDICA] && \StrUtil::isNotEmpAndNull($projectMap[AllowanceProjectEnum::PROJECT_SB_MEDICA]["months"])) {
  339. $tmp = array_filter(explode(",", $projectMap[AllowanceProjectEnum::PROJECT_SB_MEDICA]["months"]));
  340. $medicaList = array_merge($medicaList, $tmp);
  341. }
  342. if ($projectMap[AllowanceProjectEnum::PROJECT_ATTENDANCE] && \StrUtil::isNotEmpAndNull($projectMap[AllowanceProjectEnum::PROJECT_ATTENDANCE]["months"])) {
  343. $tmp = array_filter(explode(",", $projectMap[AllowanceProjectEnum::PROJECT_ATTENDANCE]["months"]));
  344. $attendanceList = array_merge($attendanceList, $tmp);
  345. }
  346. if ($projectMap[AllowanceProjectEnum::PROJECT_WORKDAY] && \StrUtil::isNotEmpAndNull($projectMap[AllowanceProjectEnum::PROJECT_WORKDAY]["months"])) {
  347. $tmp = array_filter(explode(",", $projectMap[AllowanceProjectEnum::PROJECT_WORKDAY]["months"]));
  348. $workdayList = array_merge($workdayList, $tmp);
  349. }
  350. }
  351. $map = [
  352. AllowanceProjectEnum::PROJECT_TAX => $taxList,
  353. AllowanceProjectEnum::PROJECT_WAGES => $wagesList,
  354. AllowanceProjectEnum::PROJECT_SB_PENSION => $pensionList,
  355. AllowanceProjectEnum::PROJECT_SB_UNEMPLOYMENT => $unemploymentList,
  356. AllowanceProjectEnum::PROJECT_SB_MEDICA => $medicaList,
  357. AllowanceProjectEnum::PROJECT_ATTENDANCE => $attendanceList,
  358. AllowanceProjectEnum::PROJECT_WORKDAY => $workdayList,
  359. ];
  360. return $map;
  361. }
  362. /**
  363. * 合并多个单位带有天数的项目的月份如考勤1月30天,
  364. * */
  365. private static function mergeMonthNeedDay($detailList) {
  366. $attendMap = [];
  367. $workDayMap = [];
  368. foreach ($detailList as $detail) {
  369. $attendMap = self::getMergeMonthNeedDayMap($detail["projectMap"][AllowanceProjectEnum::PROJECT_ATTENDANCE], $attendMap);
  370. $workDayMap = self::getMergeMonthNeedDayMap($detail["projectMap"][AllowanceProjectEnum::PROJECT_WORKDAY], $workDayMap);
  371. }
  372. $res = [
  373. AllowanceProjectEnum::PROJECT_ATTENDANCE => $attendMap,
  374. AllowanceProjectEnum::PROJECT_WORKDAY => $workDayMap
  375. ];
  376. return $res;
  377. }
  378. private static function getMergeMonthNeedDayMap($project, $map) {
  379. if (\StrUtil::isNotEmpAndNull($project["months"])) {
  380. $monthAndDayList = array_filter(explode(",", $project["months"]));
  381. for ($i = 0; $i < count($monthAndDayList); $i++) {
  382. $obj = explode("=", $monthAndDayList[$i]);
  383. $month = $obj[0];
  384. $day = $obj[1];
  385. $count = $map[$month];
  386. if ($count == 0) {
  387. $map[$month] = $day;
  388. } else {
  389. $map[$month] = $day + $count;
  390. }
  391. }
  392. }
  393. return $map;
  394. }
  395. }