compilation.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /**
  2. * 编办管理相关js
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/7/28
  6. * @version
  7. */
  8. $(document).ready(function() {
  9. let isAdding = false;
  10. let model = '';
  11. let section = 'bill';
  12. let id = $("#compilation-id").val();
  13. // 计价规则页面初始化数据
  14. if ($("#save-lib").length > 0) {
  15. initCompilation();
  16. }
  17. // 计价类型选择
  18. $(".nav-tabs li > a").click(function() {
  19. section = $(this).attr("aria-controls");
  20. });
  21. // 新增编办
  22. $("#add-compilation").click(function() {
  23. try {
  24. let data = getAndValidData(model);
  25. let url = '/compilation/add';
  26. if (model === 'all') {
  27. // 新增编办操作
  28. $.ajax({
  29. url: url,
  30. type: 'post',
  31. data: {name: data.name},
  32. error: function() {
  33. isAdding = false;
  34. },
  35. beforeSend: function() {
  36. isAdding = true;
  37. },
  38. success: function(response) {
  39. isAdding = false;
  40. if (response.err === 0) {
  41. window.location.reload();
  42. } else {
  43. let msg = response.msg === undefined ? '未知错误' : response.msg;
  44. alert(msg);
  45. }
  46. }
  47. });
  48. } else {
  49. // 新增标准清单/定额库
  50. let addLib = {
  51. name: data[model].name,
  52. id: data[model].id
  53. };
  54. // 判断是否有重复的数据
  55. if ($("input:hidden[name='"+ model +"_lib'][data-id='"+ addLib.id +"']").length > 0) {
  56. alert('重复添加数据!');
  57. return false;
  58. }
  59. let removeHtml = '<a class="pull-right text-danger remove-lib" data-model="bill" ' +
  60. 'title="移除"><span class="glyphicon glyphicon-remove"></span></a>';
  61. let tmpHtml = '<p class="form-control-static">' + removeHtml + addLib.name +
  62. '<input type="hidden" data-id="'+ addLib.id +'" name=\'' + model + '_lib\' value=\'' + JSON.stringify(addLib) + '\'>' + '</p>';
  63. $("." + model + "-list").append(tmpHtml);
  64. $('#addcompilation').modal('hide');
  65. }
  66. } catch (error) {
  67. alert(error);
  68. }
  69. });
  70. // 新增计价规则
  71. $("#add-valuation").click(function() {
  72. try {
  73. if (id === '') {
  74. throw '页面数据有误';
  75. }
  76. let name = $("input[name='valuation_name']").val();
  77. if (name === '') {
  78. throw '请填写计价规则名称';
  79. }
  80. $.ajax({
  81. url: '/compilation/add-valuation',
  82. type: 'post',
  83. data: {name: name, id: id, section: section},
  84. error: function() {
  85. isAdding = false;
  86. },
  87. beforeSend: function() {
  88. isAdding = true;
  89. },
  90. success: function(response) {
  91. isAdding = false;
  92. if (response.err === 0) {
  93. window.location.reload();
  94. } else {
  95. let msg = response.msg === undefined ? '未知错误' : response.msg;
  96. alert(msg);
  97. }
  98. }
  99. });
  100. } catch (error) {
  101. alert(error);
  102. return false;
  103. }
  104. });
  105. // 添加
  106. $(".add-compilation").click(function() {
  107. model = $(this).data('model');
  108. $("#addcompilation .modal-body > div").hide();
  109. switch (model) {
  110. case 'all':
  111. $("#name-area").show();
  112. $("#add-compilation-title").text('添加新编办');
  113. break;
  114. case 'bill':
  115. $("#bill-area").show();
  116. $("#add-compilation-title").text('添加标准清单');
  117. break;
  118. case 'ration':
  119. $("#ration-area").show();
  120. $("#add-compilation-title").text('添加定额库');
  121. break;
  122. case 'glj':
  123. $("#glj-area").show();
  124. $("#add-compilation-title").text('添加定额库');
  125. break;
  126. case 'fee':
  127. $("#fee-area").show();
  128. $("#add-compilation-title").text('添加费率标准');
  129. break;
  130. case 'artificial':
  131. $("#artificial-area").show();
  132. $("#add-compilation-title").text('添加人工系数');
  133. break;
  134. }
  135. $("#addcompilation").modal('show');
  136. });
  137. // 保存专业工程标准库
  138. $("#save-lib").click(function() {
  139. if (validLib()) {
  140. $("form").submit();
  141. }
  142. });
  143. // 保存计价规则
  144. $("#save-valuation").click(function() {
  145. $("form").submit();
  146. });
  147. // 移除操作
  148. $(".bill-list, .ration-list, .glj-list, .fee-list").on("click", ".remove-lib", function() {
  149. $(this).parent().remove();
  150. });
  151. // 计价规则启用/禁止
  152. $(".enable").click(function() {
  153. let goingChangeStatus = switchChange($(this));
  154. let id = $(this).data('id');
  155. if (id === undefined || id === '' || isAdding) {
  156. return false;
  157. }
  158. $.ajax({
  159. url: '/compilation/valuation/' + section + '/enable',
  160. type: 'post',
  161. dataType: "json",
  162. data: {id: id, enable: goingChangeStatus},
  163. error: function() {
  164. isAdding = false;
  165. switchChange($(this));
  166. },
  167. beforeSend: function() {
  168. isAdding = true;
  169. },
  170. success: function(response) {
  171. isAdding = false;
  172. if (response.err !== 0) {
  173. switchChange($(this));
  174. alert('更改失败');
  175. }
  176. }
  177. });
  178. });
  179. // 发布编办
  180. $("#release").click(function() {
  181. let id = $(this).data("id");
  182. let status = $(this).data("status");
  183. status = parseInt(status);
  184. if (isAdding || id === '' || isNaN(status)) {
  185. return false;
  186. }
  187. $.ajax({
  188. url: '/compilation/release',
  189. type: 'post',
  190. data: {id: id, status: status},
  191. dataType: "json",
  192. error: function() {
  193. isAdding = false;
  194. },
  195. beforeSend: function() {
  196. isAdding = true;
  197. },
  198. success: function(response) {
  199. isAdding = false;
  200. if (response.err === 0) {
  201. window.location.reload();
  202. } else {
  203. let msg = response.msg === undefined ? "未知错误" : response.msg;
  204. alert(msg);
  205. }
  206. }
  207. });
  208. });
  209. });
  210. /**
  211. * 初始化
  212. *
  213. * @return {void|boolean}
  214. */
  215. function initCompilation() {
  216. let billListData = billList === undefined ? [] : JSON.parse(billList);
  217. let rationLibData = rationList === undefined ? [] : JSON.parse(rationList);
  218. let gljLibData = gljList === undefined ? [] : JSON.parse(gljList);
  219. let feeLibData = feeRateList === undefined ? [] : JSON.parse(feeRateList);
  220. mainTreeCol = mainTreeCol === '' ? '' : JSON.parse(mainTreeCol);
  221. let artificialCoefficientData = artificialCoefficientList === undefined ? [] : JSON.parse(artificialCoefficientList);
  222. mainTreeCol = mainTreeCol.replace(/\n/g, '\\n');
  223. billsTemplateData = billsTemplateData.replace(/\n/g, '\\n');
  224. // 初始化 造价书列设置
  225. colSpread = TREE_SHEET_HELPER.createNewSpread($('#main-tree-col')[0]);
  226. let billsTemplateTree = idTree.createNew({id: 'ID', pid: 'ParentID', nid: 'NextSiblingID', rootId: -1});
  227. billsTemplateTree.loadDatas(JSON.parse(billsTemplateData));
  228. if (mainTreeCol !== '') {
  229. TREE_SHEET_HELPER.loadSheetHeader(mainTreeCol, colSpread.getActiveSheet());
  230. TREE_SHEET_HELPER.showTreeData(mainTreeCol, colSpread.getActiveSheet(), billsTemplateTree);
  231. }
  232. if (billListData.length <= 0 || rationLibData.length <= 0 || gljLibData.length <= 0) {
  233. return false;
  234. }
  235. // 标准清单
  236. let html = '';
  237. for(let tmp of billListData) {
  238. let tmpHtml = '<option value="' + tmp.id + '">' + tmp.name + '</option>';
  239. html += tmpHtml;
  240. }
  241. $("select[name='standard_bill']").children("option").first().after(html);
  242. // 定额库
  243. html = '';
  244. for(let tmp of rationLibData) {
  245. let tmpHtml = '<option value="' + tmp.id + '">' + tmp.name + '</option>';
  246. html += tmpHtml;
  247. }
  248. $("select[name='ration_lib']").children("option").first().after(html);
  249. // 工料机库
  250. html = '';
  251. for(let tmp of gljLibData) {
  252. let tmpHtml = '<option value="' + tmp.id + '">' + tmp.name + '</option>';
  253. html += tmpHtml;
  254. }
  255. $("select[name='glj_lib']").children("option").first().after(html);
  256. // 费率标准库
  257. html = '';
  258. for(let tmp of feeLibData) {
  259. let tmpHtml = '<option value="' + tmp.id + '">' + tmp.name + '</option>';
  260. html += tmpHtml;
  261. }
  262. $("select[name='fee_lib']").children("option").first().after(html);
  263. // 人工系数标准库
  264. html = '';
  265. for(let tmp of artificialCoefficientData) {
  266. let tmpHtml = '<option value="' + tmp.id + '">' + tmp.name + '</option>';
  267. html += tmpHtml;
  268. }
  269. $("select[name='artificial_lib']").children("option").first().after(html);
  270. }
  271. /**
  272. * 校验数据
  273. *
  274. * @param {String} model
  275. * @return {Object}
  276. */
  277. function getAndValidData(model) {
  278. let name = $("input[name='compilation_name']").val();
  279. let standardBill = $("select[name='standard_bill']").children("option:selected").val();
  280. let rationLib = $("select[name='ration_lib']").children("option:selected").val();
  281. let gljLib = $("select[name='glj_lib']").children("option:selected").val();
  282. let feeLib = $("select[name='fee_lib']").children("option:selected").val();
  283. let artificialLib = $("select[name='artificial_lib']").children("option:selected").val();
  284. if (name === '' && model === 'all') {
  285. throw '编办名字不能为空';
  286. }
  287. if ( model === 'bill' && (standardBill === '' || standardBill === undefined)) {
  288. throw '请选择标准清单库';
  289. }
  290. if (model === 'ration' && (rationLib === '' || rationLib === undefined)) {
  291. throw '请选择定额库';
  292. }
  293. if (model === 'glj' && (gljLib === '' || gljLib === undefined)) {
  294. throw '请选择工料机库';
  295. }
  296. if (model === 'fee' && (feeLib === '' || feeLib === undefined)) {
  297. throw '请选择费率标准';
  298. }
  299. if (model === 'artificial' && (artificialLib === '' || artificialLib === undefined)) {
  300. throw '请选择费率库';
  301. }
  302. let standardBillString = $("select[name='standard_bill']").children("option:selected").text();
  303. let rationLibString = $("select[name='ration_lib']").children("option:selected").text();
  304. let gljLibString = $("select[name='glj_lib']").children("option:selected").text();
  305. let feeLibString = $("select[name='fee_lib']").children("option:selected").text();
  306. let artificialString = $("select[name='artificial_lib']").children("option:selected").text();
  307. let result = {
  308. name: name,
  309. bill: {
  310. id: standardBill,
  311. name: standardBillString
  312. },
  313. ration: {
  314. id: rationLib,
  315. name: rationLibString
  316. },
  317. glj: {
  318. id: gljLib,
  319. name: gljLibString
  320. },
  321. fee: {
  322. id: feeLib,
  323. name: feeLibString
  324. },
  325. artificial: {
  326. id: artificialLib,
  327. name: artificialString
  328. }
  329. };
  330. return result;
  331. }
  332. /**
  333. * 验证标准库数据
  334. *
  335. * @return {boolean}
  336. */
  337. function validLib() {
  338. let result = false;
  339. try {
  340. let valuationName = $("input[name='name']").val();
  341. if (valuationName === '') {
  342. throw '请填写计价规则名称';
  343. }
  344. let engineering = $("select[name='engineering']").val();
  345. if (engineering === '' || engineering <= 0) {
  346. throw '请选择工程专业';
  347. }
  348. if ($("input:hidden[name='bill_lib']").length <= 0) {
  349. throw '请添加标准清单';
  350. }
  351. if ($("input:hidden[name='ration_lib']").length <= 0) {
  352. throw '请添加定额库';
  353. }
  354. if ($("input:hidden[name='fee_lib']").length <= 0) {
  355. throw '请添加费率标准';
  356. }
  357. if ($("input:hidden[name='artificial_lib']").length <= 0) {
  358. throw '请添加人工系数';
  359. }
  360. result = true;
  361. } catch (error) {
  362. alert(error);
  363. result = false;
  364. }
  365. return result;
  366. }
  367. /**
  368. * 切换switch效果
  369. *
  370. * @param {Object} element
  371. * @return {boolean}
  372. */
  373. function switchChange(element) {
  374. // 第一个元素判断当前的状态
  375. let firstButton = element.children("button").first();
  376. let secondButton = element.children("button").eq(1);
  377. let currentStatus = firstButton.is(":disabled");
  378. if (currentStatus) {
  379. // 当前为true切换到false
  380. firstButton.removeClass('btn-success').removeClass('disabled').addClass('btn-default').removeAttr("disabled");
  381. firstButton.text('开启');
  382. secondButton.removeClass("btn-default").addClass("btn-danger").addClass("disabled").attr("disabled", "disabled");
  383. secondButton.text('已禁用');
  384. } else {
  385. // 当前false切换到true
  386. firstButton.removeClass("btn-default").addClass("btn-success").addClass("disabled").attr("disabled", "disabled");
  387. firstButton.text('已开启');
  388. secondButton.removeClass('btn-danger').removeClass('disabled').addClass('btn-default').removeAttr("disabled");
  389. secondButton.text('禁用');
  390. }
  391. return !currentStatus;
  392. }