compilation.js 13 KB

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