material.js 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783
  1. 'use strict';
  2. /**
  3. * 材料调差 - 调差工料
  4. *
  5. * @author Mai
  6. * @date 2019/1/16
  7. * @version
  8. */
  9. function loadUpdateMaterials(newMaterial, fields) {
  10. const newMaterials = newMaterial instanceof Array ? newMaterial : [newMaterial];
  11. for (const nm of newMaterials) {
  12. const om = _.find(materialBillsData, {id: nm.id});
  13. for (const prop in nm) {
  14. if (!fields || fields.indexOf(prop) >= 0) {
  15. om[prop] = nm[prop];
  16. }
  17. }
  18. }
  19. }
  20. function DatePickerCellType() {
  21. }
  22. DatePickerCellType.prototype = new GC.Spread.Sheets.CellTypes.Base();
  23. DatePickerCellType.prototype.createEditorElement = function () {
  24. //Create input presenter.
  25. var input = document.createElement("input");
  26. return input;
  27. };
  28. DatePickerCellType.prototype.activateEditor = function (editorContext, cellStyle, cellRect) {
  29. //Initialize input editor.
  30. if (editorContext) {
  31. const $editor = $(editorContext);
  32. GC.Spread.Sheets.CellTypes.Base.prototype.activateEditor.apply(this, arguments);
  33. $editor.datepicker({});
  34. $editor.css("position", "absolute");
  35. $editor.attr("gcUIElement", "gcEditingInput");
  36. $(".ui-datepicker").attr("gcUIElement", "gcEditingInput");
  37. }
  38. }
  39. DatePickerCellType.prototype.deactivateEditor = function (editorContext) {
  40. //Remove input editor when end editor status.
  41. if (editorContext) {
  42. var element = editorContext;
  43. // $(element).datepicker().data('datepicker').hide();
  44. // $(element).datepicker().data('datepicker').destroy();
  45. $(element).datepicker("hide");
  46. $(element).datepicker("destroy");
  47. }
  48. GC.Spread.Sheets.CellTypes.Base.prototype.deactivateEditor.apply(this, arguments)
  49. };
  50. DatePickerCellType.prototype.setEditorValue = function (editor, value) {
  51. //Sync value from Cell value to editor value.
  52. // console.log($(editor), value);
  53. $(editor).datepicker("setDate", value);
  54. // $(editor).datepicker().data('datepicker').selectDate(value ? new Date(value) : new Date());
  55. };
  56. DatePickerCellType.prototype.getEditorValue = function (editor, context) {
  57. // console.log($(editor).datepicker().data('datepicker'), context);
  58. //Sync value from editor value to cell value.
  59. // console.log($(editor).datepicker("getDate"));
  60. // return $(editor).datepicker("getDate");
  61. return $(editor).val();
  62. };
  63. DatePickerCellType.prototype.updateEditor = function (editorContext, cellStyle, cellRect) {
  64. if (editorContext) {
  65. const $editor = $(editorContext);
  66. $editor.css("width", cellRect.width - 1);
  67. $editor.css("height", cellRect.height - 3);
  68. }
  69. };
  70. function resetTpTable() {
  71. $('#tp_set').find('td').eq(1).text(ZhCalc.round(m_tp, materialDecimal.tp));
  72. $('#tp_set').find('td').eq(2).text(ZhCalc.add(pre_tp, ZhCalc.round(m_tp, materialDecimal.tp)));
  73. if (materialTax) {
  74. $('#tax_rate_set').find('td').eq(1).text(ZhCalc.round(m_tax_tp, materialDecimal.tp));
  75. $('#tax_rate_set').find('td').eq(2).text(ZhCalc.add(m_tax_pre_tp, ZhCalc.round(m_tax_tp, materialDecimal.tp)));
  76. } else {
  77. const rate = $('#rateInput').val();
  78. const bqhs = ZhCalc.round(ZhCalc.mul(m_tp, 1+rate/100), materialDecimal.tp);
  79. const jzbqhs = ZhCalc.add(pre_tp_hs, bqhs);
  80. $('#rate_set').find('td').eq(1).text(bqhs !== 0 ? bqhs : '');
  81. $('#rate_set').find('td').eq(2).text(jzbqhs !== 0 ? jzbqhs : '');
  82. }
  83. if (isStageSelf) {
  84. let html = '';
  85. for (const ms of materialStageData) {
  86. const taxHtml = materialTax ? ' <td class="text-center">' + ms.m_tax_tp + '</td>\n' : '';
  87. html += '<tr><td>第' + ms.order + '期</td><td class="text-center">' + ms.m_tp + '</td>\n' +
  88. taxHtml +
  89. ' </tr>';
  90. }
  91. const allTaxHtml = materialTax ? ' <td class="text-center">' + m_tax_tp + '</td>\n' : '';
  92. html += '<tr><td>合计</td><td class="text-center">' + m_tp + '</td>\n' +
  93. allTaxHtml +
  94. ' </tr>';
  95. $('#materialStageTable').html(html);
  96. }
  97. }
  98. function getPasteHint (str, row = '') {
  99. let returnObj = str;
  100. if (row) {
  101. returnObj.msg = '工料第' + (row+1) + '行' + str.msg;
  102. }
  103. return returnObj;
  104. }
  105. const is_numeric = (value) => {
  106. if (typeof(value) === 'object') {
  107. return false;
  108. } else {
  109. return !Number.isNaN(Number(value)) && value.toString().trim() !== '';
  110. }
  111. };
  112. $(document).ready(() => {
  113. autoFlashHeight();
  114. const materialSpread = SpreadJsObj.createNewSpread($('#material-spread')[0]);
  115. const materialSpreadSetting = {
  116. emptyRows: 0,
  117. headRows: 2,
  118. headRowHeight: [25, 32],
  119. defaultRowHeight: 21,
  120. headerFont: '12px 微软雅黑',
  121. font: '12px 微软雅黑',
  122. readOnly: readOnly,
  123. };
  124. let materialSpreadSettingCols = [
  125. {title: '调差类型', colSpan: '1', rowSpan: '2', field: 't_type', hAlign: 1, width: 60, formatter: '@', readOnly: 'readOnly.isUsed', cellType: 'customizeCombo', comboItems: materialType.t_type, cellTypeKey: 1},
  126. {title: '编号', colSpan: '1', rowSpan: '2', field: 'code', hAlign: 0, width: 60, formatter: '@', readOnly: 'readOnly.isUsed'},
  127. {title: '名称', colSpan: '1', rowSpan: '2', field: 'name', hAlign: 0, width: 150, formatter: '@', readOnly: 'readOnly.isEdit'},
  128. {title: '单位', colSpan: '1', rowSpan: '2', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: 'readOnly.isEdit'},
  129. {title: '规格', colSpan: '1', rowSpan: '2', field: 'spec', hAlign: 0, width: 150, formatter: '@', readOnly: 'readOnly.isEdit'},
  130. {title: '工料分类', colSpan: '1', rowSpan: '2', field: 'm_type', hAlign: 1, width: 60, readOnly: 'readOnly.isEdit', cellType: 'customizeCombo', comboItems: materialType.m_type, cellTypeKey: 2},
  131. ];
  132. if (materialTax) {
  133. materialSpreadSettingCols.push({title: '税率(%)', colSpan: '1', rowSpan: '2', field: 'm_tax', hAlign: 2, width: 50, type: 'Number', readOnly: editTaxPermission ? 'readOnly.remark' : 'readOnly.isEdit'});
  134. }
  135. materialSpreadSettingCols = _.concat(materialSpreadSettingCols, [
  136. {title: '上涨 幅度(%)', colSpan: '1', rowSpan: '2', field: 'm_up_risk', hAlign: 2, width: 50, type: 'Number', readOnly: 'readOnly.isEdit'},
  137. {title: '下跌 幅度(%)', colSpan: '1', rowSpan: '2', field: 'm_down_risk', hAlign: 2, width: 50, type: 'Number', readOnly: 'readOnly.isEdit'},
  138. {title: '基准价', colSpan: '1', rowSpan: '2', field: 'basic_price', hAlign: 2, width: 60, type: 'Number', readOnly: 'readOnly.isEdit'},
  139. {title: '基准时间', colSpan: '1', rowSpan: '2', field: 'basic_times', hAlign: 0, width: 70, formatter: '@', readOnly: 'readOnly.isEdit'},
  140. {title: '本期信息价|单价', colSpan: '3|1', rowSpan: '1|1', field: 'msg_tp', hAlign: 2, width: 60, type: 'Number', readOnly: 'readOnly.msg_tp'},
  141. {title: '|时间', colSpan: '|1', rowSpan: '|1', field: 'msg_times', hAlign: 0, width: 80, formatter: '@', readOnly: 'readOnly.remark'},
  142. {title: '|单价差', colSpan: '|1', rowSpan: '|1', field: 'msg_spread', hAlign: 2, width: 60, type: 'Number', readOnly: true, getValue: 'getValue.msg_spread'},]
  143. );
  144. if (materialTax) {
  145. materialSpreadSettingCols = _.concat(materialSpreadSettingCols, [
  146. {title: '本期材料调差|有效价差', colSpan: '4|1', rowSpan: '1|1', field: 'm_spread', hAlign: 2, width: 60, type: 'Number', readOnly: true, getValue: 'getValue.m_spread'},
  147. {title: '|本期应耗数量', colSpan: '|1', rowSpan: '|1', field: 'quantity', hAlign: 2, width: 90, type: 'Number', readOnly: true},
  148. {title: '|调差金额', colSpan: '|1', rowSpan: '|1', field: 'm_tp', hAlign: 2, width: 100, type: 'Number', readOnly: true, getValue: 'getValue.m_tp'},
  149. {title: '|调差金额(材料税)', colSpan: '|1', rowSpan: '|1', field: 'm_tax_tp', hAlign: 2, width: 100, type: 'Number', readOnly: true, getValue: 'getValue.m_tax_tp'},
  150. {title: '截止上期|调差金额', colSpan: '2|1', rowSpan: '1|1', field: 'pre_tp', hAlign: 2, width: 100, type: 'Number', readOnly: true},
  151. {title: '|调差金额(材料税)', colSpan: '|1', rowSpan: '|1', field: 'tax_pre_tp', hAlign: 2, width: 100, type: 'Number', readOnly: true},
  152. {title: '备注', colSpan: '1', rowSpan: '2', field: 'remark', hAlign: 0, width: 50, formatter: '@', cellType: 'ellipsisAutoTip', readOnly: 'readOnly.remark'},
  153. {title: '是否汇总', colSpan: '1', rowSpan: '2', field: 'is_summary', hAlign: 1, width: 60, cellType: 'checkbox', readOnly: 'readOnly.isEdit'}
  154. ])
  155. } else {
  156. materialSpreadSettingCols = _.concat(materialSpreadSettingCols, [
  157. {title: '本期材料调差|有效价差', colSpan: '3|1', rowSpan: '1|1', field: 'm_spread', hAlign: 2, width: 60, type: 'Number', readOnly: true, getValue: 'getValue.m_spread'},
  158. {title: '|本期应耗数量', colSpan: '|1', rowSpan: '|1', field: 'quantity', hAlign: 2, width: 90, type: 'Number', readOnly: true},
  159. {title: '|调差金额', colSpan: '|1', rowSpan: '|1', field: 'm_tp', hAlign: 2, width: 100, type: 'Number', readOnly: true, getValue: 'getValue.m_tp'},
  160. {title: '截止上期|调差金额', colSpan: '1|1', rowSpan: '1|1', field: 'pre_tp', hAlign: 2, width: 100, type: 'Number', readOnly: true},
  161. {title: '备注', colSpan: '1', rowSpan: '2', field: 'remark', hAlign: 0, width: 50, formatter: '@', cellType: 'ellipsisAutoTip', readOnly: 'readOnly.remark'},
  162. {title: '是否汇总', colSpan: '1', rowSpan: '2', field: 'is_summary', hAlign: 1, width: 60, cellType: 'checkbox', readOnly: 'readOnly.isEdit'}
  163. ])
  164. }
  165. materialSpreadSetting.cols = materialSpreadSettingCols;
  166. const spCol = _.find(materialSpreadSetting.cols, {field: 'quantity'});
  167. spCol.readOnly = true;
  168. spCol.cellType = 'activeImageBtn';
  169. spCol.normalImg = '#ellipsis-icon';
  170. spCol.indent = 5;
  171. spCol.showImage = function (data) {
  172. // console.log(data, data.t_type);
  173. // return !readOnly && data.t_type === 2 && data.mid === materialID;
  174. return data.t_type === 2;
  175. };
  176. materialSpreadSetting.imageClick = function (data) {
  177. if (data.t_type === 2) {
  178. $('#bcyy').modal('show');
  179. $('#materialbillsId').val(data.id);
  180. $('#expr').val(data.expr);
  181. if (!readOnly) {
  182. $('#expr').attr('readOnly', false);
  183. $('#expr_btn').show();
  184. $('#expr_select').show();
  185. } else {
  186. $('#expr').attr('readOnly', true);
  187. $('#expr_btn').hide();
  188. $('#expr_select').hide();
  189. }
  190. }
  191. };
  192. const materialBase = {
  193. isUsed: function (data) {
  194. if (data.t_type === 2) {
  195. return data.mid === materialID;
  196. } else {
  197. const mlInfo = _.find(materialListData, { mb_id: data.id });
  198. return data.mid === materialID && mlInfo === undefined;
  199. }
  200. },
  201. isEdit: function (data) {
  202. // if (data.t_type === 2) {
  203. // return data.mid === materialID;
  204. // } else {
  205. // const mlInfo = _.find(materialListData, { mb_id: data.id });
  206. // return data.mid === materialID && mlInfo === undefined;
  207. // }
  208. return data.mid === materialID;
  209. },
  210. // isStage: function (data) {
  211. // return data.mid === materialID;
  212. // }
  213. }
  214. const materialCol = {
  215. getValue: {
  216. msg_spread: function (data) {
  217. return ZhCalc.round(ZhCalc.sub(data.msg_tp, data.basic_price), materialDecimal.up);
  218. },
  219. m_spread : function (data) {
  220. const msg_spread = materialCol.getValue.msg_spread(data);
  221. const cor = msg_spread >= 0 ? ZhCalc.mul(data.basic_price, ZhCalc.div(data.m_up_risk, 100)) : ZhCalc.mul(data.basic_price, ZhCalc.div(data.m_down_risk, 100));
  222. return Math.abs(msg_spread) > Math.abs(cor) ? (msg_spread > 0 ? ZhCalc.round(ZhCalc.sub(msg_spread, cor), materialDecimal.up) : ZhCalc.round(ZhCalc.add(msg_spread, cor), materialDecimal.up)) : 0;
  223. },
  224. m_tp: function (data) {
  225. return ZhCalc.round(ZhCalc.mul(materialCol.getValue.m_spread(data), data.quantity), materialDecimal.tp);
  226. },
  227. m_tax_tp: function (data) {
  228. const m_tp = ZhCalc.round(ZhCalc.mul(materialCol.getValue.m_spread(data), data.quantity), materialDecimal.tp);
  229. return data.m_tax ? ZhCalc.round(ZhCalc.mul(m_tp, (1+ZhCalc.div(data.m_tax, 100))), materialDecimal.tp) : m_tp;
  230. }
  231. },
  232. readOnly: {
  233. isEdit: function (data) {
  234. return !(!readOnly && materialBase.isEdit(data));
  235. },
  236. isUsed: function (data) {
  237. return !(!readOnly && materialBase.isUsed(data));
  238. },
  239. remark: function () {
  240. return readOnly;
  241. },
  242. msg_tp: function () {
  243. return !(!readOnly && months.length === 0);
  244. }
  245. },
  246. };
  247. const needUpdateArray = ['quantity', 'expr', 'msg_tp', 'msg_times', 'msg_spread', 'm_spread', 'm_tp', 'm_tax_tp', 'is_summary', 'remark'];
  248. const materialSpreadObj = {
  249. getMaterialBillsData: function () {
  250. if (isStageSelf) {
  251. // 当前选中nav id
  252. const ms_id = $('#myTab').find('.active').data('msid');
  253. const msbList = _.filter(materialStageBillsData, { ms_id });
  254. for (const mb of materialBillsData) {
  255. mb.ms_id = ms_id;
  256. const msbInfo = _.find(msbList, { mb_id: mb.mb_id ? mb.mb_id : mb.id });
  257. if (msbInfo) {
  258. for (const nu of needUpdateArray) {
  259. mb[nu] = msbInfo[nu];
  260. }
  261. }
  262. }
  263. }
  264. return materialBillsData;
  265. },
  266. updateOneMaterialBill: function (data) {
  267. const ms_id = $('#myTab').find('.active').data('msid');
  268. const msbInfo = _.find(materialStageBillsData, { ms_id, mb_id: data.id });
  269. console.log(msbInfo);
  270. data.ms_id = ms_id;
  271. for (const nu of needUpdateArray) {
  272. data[nu] = msbInfo[nu];
  273. }
  274. return data;
  275. },
  276. updateMaterialData: function (datas) {
  277. if (datas.stageData && datas.stageData.length > 0) {
  278. if (datas.stageData.length === materialStageData.length) {
  279. // 全体替换
  280. materialStageData = datas.stageData;
  281. } else {
  282. // 替换对应的stage数据
  283. for (const s of datas.stageData) {
  284. const index = _.findIndex(materialStageData, { id: s.id });
  285. materialStageData.splice(index, 1, s);
  286. }
  287. }
  288. }
  289. if (datas.pushStageBillsData && datas.pushStageBillsData.length > 0) {
  290. materialStageBillsData = _.concat(materialStageBillsData, datas.pushStageBillsData);
  291. }
  292. if (datas.stageBillsData && datas.stageBillsData.length > 0) {
  293. if (datas.stageBillsData.length === materialStageBillsData.length) {
  294. // 全体替换
  295. materialStageBillsData = datas.stageBillsData;
  296. } else {
  297. // 替换对应的stage数据
  298. for (const s of datas.stageBillsData) {
  299. const index = _.findIndex(materialStageBillsData, { id: s.id });
  300. materialStageBillsData.splice(index, 1, s);
  301. }
  302. }
  303. }
  304. },
  305. materialSheetReset: function (redo = false) {
  306. let newMaterialBillsData = _.cloneDeep(materialBillsData);
  307. if($('#bills0_list').is(':checked')) {
  308. newMaterialBillsData = _.filter(materialBillsData, function (item) {
  309. return item.quantity !== null && item.quantity !== 0;
  310. });
  311. if (redo) {
  312. SpreadJsObj.initSpreadSettingEvents(materialSpreadSetting, materialCol);
  313. SpreadJsObj.initSheet(materialSpread.getActiveSheet(), materialSpreadSetting);
  314. }
  315. }
  316. console.log(newMaterialBillsData);
  317. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, newMaterialBillsData);
  318. materialSpreadObj.refreshActn();
  319. },
  320. refreshActn: function (rowCount = 1) {
  321. const setObjEnable = function (obj, enable) {
  322. if (enable) {
  323. obj.removeClass('disabled');
  324. } else {
  325. obj.addClass('disabled');
  326. }
  327. };
  328. const sheet = materialSpread.getActiveSheet();
  329. const select = SpreadJsObj.getSelectObject(sheet);
  330. const selection = sheet.getSelections();
  331. const sel = selection ? selection[0] : sheet.getSelections()[0];
  332. const row = sel ? sel.row : -1;
  333. const first = materialBillsData[row];
  334. let last = first;
  335. if (sel.rowCount > 1 && first) {
  336. for (let r = 1; r < sel.rowCount; r++) {
  337. const rNode = materialBillsData[sel.row + r];
  338. if (!rNode) break;
  339. last = rNode;
  340. }
  341. }
  342. const preNode = materialBillsData[row - 1];
  343. // 还需判断是否已被调差清单调用
  344. setObjEnable($('#del'), !readOnly && select && materialBase.isUsed(select) && rowCount === 1);
  345. setObjEnable($('#up-move'), !readOnly && first && preNode && materialBillsData.indexOf(last) > 0 && sel.row + sel.rowCount <= materialBillsData.length && !$('#bills0_list').is(':checked'));
  346. setObjEnable($('#down-move'), !readOnly && first && materialBillsData.indexOf(last) < materialBillsData.length - 1 && sel.row + sel.rowCount <= materialBillsData.length && !$('#bills0_list').is(':checked'));
  347. },
  348. add: function () {
  349. const sheet = materialSpread.getActiveSheet();
  350. postData(window.location.pathname + '/save', {type: 'add'}, function (result) {
  351. if (result) {
  352. materialBillsData.push(result.info);
  353. if (isStageSelf) {
  354. materialSpreadObj.updateMaterialData(result);
  355. materialSpreadObj.getMaterialBillsData();
  356. }
  357. // sheet.addRows(materialBillsData.length - 1, 1);
  358. // SpreadJsObj.reLoadRowData(sheet, materialBillsData.length - 1);
  359. let newMaterialBillsData = materialBillsData;
  360. if($('#bills0_list').is(':checked')) {
  361. newMaterialBillsData = _.filter(materialBillsData, function (item) {
  362. return item.quantity !== null && item.quantity !== 0;
  363. });
  364. }
  365. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, newMaterialBillsData);
  366. // materialSpreadObj.materialSheetReset();
  367. sheet.setSelection(newMaterialBillsData.length - 1, 0, 1, 1);
  368. materialSpreadObj.refreshActn();
  369. // 月信息价需要同时添加空白的list
  370. if (months.length > 0) {
  371. const one_month ={ mb_id: result.info.id, code: '', name: null, origin: null, unit: null, order: result.info.order };
  372. for (const m of months) {
  373. one_month[m] = null;
  374. }
  375. monthsList.push(one_month);
  376. // const newMonthsList = _.filter(monthsList, function (item) {
  377. // return _.findIndex(newMaterialBillsData, { code: item.code }) !== -1;
  378. // });
  379. // materialMonthSpread.getActiveSheet().addRows(newMonthsList.length - 1, 1);
  380. // SpreadJsObj.reLoadRowData(materialMonthSpread.getActiveSheet(), newMonthsList.length - 1);
  381. monthFunGather.monthsListSet();
  382. // materialMonthSpreadObj.monthsListSet();
  383. }
  384. }
  385. });
  386. },
  387. del: function () {
  388. const sheet = materialSpread.getActiveSheet();
  389. const select = SpreadJsObj.getSelectObject(sheet);
  390. postData(window.location.pathname + '/save', {type: 'del', id: select.id}, function (result) {
  391. m_tp = result.m_tp;
  392. if (materialTax) {
  393. m_tax_tp = result.m_tax_tp;
  394. }
  395. const index = materialBillsData.indexOf(select);
  396. let newMaterialBillsData = materialBillsData;
  397. let newIndex = index;
  398. if($('#bills0_list').is(':checked')) {
  399. newMaterialBillsData = _.filter(materialBillsData, function (item) {
  400. return item.quantity !== null && item.quantity !== 0;
  401. });
  402. newIndex = newMaterialBillsData.indexOf(select);
  403. }
  404. materialBillsData.splice(index, 1);
  405. sheet.deleteRows(newIndex, 1);
  406. if (isStageSelf) {
  407. materialSpreadObj.updateMaterialData(result);
  408. materialSpreadObj.getMaterialBillsData();
  409. }
  410. resetTpTable();
  411. // SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  412. const sel = sheet.getSelections();
  413. sheet.setSelection(newIndex > 0 ? newIndex - 1 : 0, sel.length > 0 ? sel[0].col : 0, 1, 1);
  414. materialSpreadObj.refreshActn();
  415. // 月信息价需要同时删除
  416. if (months.length > 0) {
  417. monthsList.splice(index, 1);
  418. materialMonthSpread.getActiveSheet().deleteRows(newIndex, 1);
  419. // SpreadJsObj.reLoadSheetData(materialMonthSpread.getActiveSheet());
  420. }
  421. });
  422. },
  423. upMove: function () {
  424. if($('#bills0_list').is(':checked')) {
  425. return;
  426. }
  427. const data = {
  428. type: 'changeOrder',
  429. postData: [],
  430. };
  431. const sheet = materialSpread.getActiveSheet();
  432. const selection = sheet.getSelections();
  433. const row = selection[0].row, count = selection[0].rowCount;
  434. const first = sheet.zh_data[row];
  435. if (!first) {
  436. materialSpreadObj.refreshActn();
  437. return false;
  438. }
  439. const pre = sheet.zh_data[row - 1], preUpdate = {id: pre.id};
  440. for (let iRow = 0; iRow < count; iRow++) {
  441. const posData = sheet.zh_data[iRow + row];
  442. if (posData) {
  443. data.postData.push({id: posData.id, order: sheet.zh_data[iRow + row - 1].order});
  444. preUpdate.order = posData.order;
  445. }
  446. }
  447. console.log(monthsList);
  448. data.postData.push(preUpdate);
  449. console.log(data);
  450. if (data.postData.length > 0) {
  451. postData(window.location.pathname + '/save', data, function () {
  452. _.forEach(data.postData, function (item) {
  453. const cl = _.find(materialBillsData, { id: item.id });
  454. cl.order = item.order;
  455. });
  456. materialBillsData.sort(function (a, b) {
  457. return a.order - b.order
  458. });
  459. SpreadJsObj.reLoadSheetData(sheet);
  460. const sel = selection[0];
  461. if (sel) {
  462. sheet.setSelection(sheet.zh_data.indexOf(first), sel.col, sel.rowCount, sel.colCount);
  463. }
  464. if (months.length > 0) {
  465. _.forEach(data.postData, function (item) {
  466. const cl = _.find(monthsList, { mb_id: item.id });
  467. cl.order = item.order;
  468. });
  469. monthsList.sort(function (a, b) {
  470. return a.order - b.order
  471. });
  472. const materialMonthSheet = materialMonthSpread.getActiveSheet();
  473. SpreadJsObj.reLoadSheetData(materialMonthSheet);
  474. }
  475. materialSpreadObj.refreshActn();
  476. });
  477. }
  478. },
  479. downMove: function () {
  480. if($('#bills0_list').is(':checked')) {
  481. return;
  482. }
  483. const data = {
  484. type: 'changeOrder',
  485. postData: [],
  486. };
  487. const sheet = materialSpread.getActiveSheet();
  488. const selection = sheet.getSelections();
  489. const row = selection[0].row, count = selection[0].rowCount;
  490. const first = sheet.zh_data[row];
  491. if (!first) {
  492. materialSpreadObj.refreshActn();
  493. return false;
  494. }
  495. const next = sheet.zh_data[row + count], nextUpdate = {id: next.id};
  496. for (let iRow = count - 1; iRow >= 0; iRow--) {
  497. const posData = sheet.zh_data[iRow + row];
  498. if (posData) {
  499. data.postData.push({id: posData.id, order: sheet.zh_data[iRow + row + 1].order});
  500. nextUpdate.order = posData.order;
  501. }
  502. }
  503. data.postData.push(nextUpdate);
  504. console.log(data);
  505. if (data.postData.length > 0) {
  506. postData(window.location.pathname + '/save', data, function () {
  507. _.forEach(data.postData, function (item) {
  508. const cl = _.find(materialBillsData, { id: item.id });
  509. cl.order = item.order;
  510. });
  511. materialBillsData.sort(function (a, b) {
  512. return a.order - b.order
  513. });
  514. SpreadJsObj.reLoadSheetData(sheet);
  515. const sel = selection[0];
  516. if (sel) {
  517. sheet.setSelection(sheet.zh_data.indexOf(first), sel.col, sel.rowCount, sel.colCount);
  518. }
  519. if (months.length > 0) {
  520. _.forEach(data.postData, function (item) {
  521. const cl = _.find(monthsList, { mb_id: item.id });
  522. cl.order = item.order;
  523. });
  524. monthsList.sort(function (a, b) {
  525. return a.order - b.order
  526. });
  527. const materialMonthSheet = materialMonthSpread.getActiveSheet();
  528. SpreadJsObj.reLoadSheetData(materialMonthSheet);
  529. }
  530. materialSpreadObj.refreshActn();
  531. });
  532. }
  533. },
  534. selectionChanged: function (e, info) {
  535. const sel = info.sheet.getSelections()[0];
  536. const col = info.sheet.zh_setting.cols[sel.col];
  537. materialSpreadObj.refreshActn(sel.rowCount);
  538. const data = SpreadJsObj.getSelectObject(info.sheet);
  539. materialSpreadObj.setReadOnly(true);
  540. // console.log(data);
  541. },
  542. editEnded: function (e, info) {
  543. if (info.sheet.zh_setting) {
  544. const select = SpreadJsObj.getSelectObject(info.sheet);
  545. const col = info.sheet.zh_setting.cols[info.col];
  546. if (col.field === 'is_summary') {
  547. return;
  548. }
  549. // 未改变值则不提交
  550. let validText = is_numeric(info.editingText) ? parseFloat(info.editingText) : (info.editingText ? trimInvalidChar(info.editingText) : null);
  551. const orgValue = select[col.field];
  552. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  553. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  554. return;
  555. }
  556. // 判断部分值是否输入的是数字判断和数据计算
  557. if (col.field === 'basic_price') {
  558. if (isNaN(validText)) {
  559. toastr.error('不能输入其它非数字类型字符');
  560. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  561. return;
  562. }
  563. let num = parseFloat(validText);
  564. const reg = materialDecimal.up ? new RegExp("^\\d+(\\.\\d{1,"+ materialDecimal.up +"})?$") : new RegExp("^\\d+?$");
  565. if (validText !== null && (num < 0 || !reg.test(num))) {
  566. toastr.warning('已保留'+ materialDecimal.up +'位小数');
  567. validText = ZhCalc.round(num, materialDecimal.up);
  568. }
  569. }
  570. if (col.field === 'msg_tp') {
  571. if (isNaN(validText)) {
  572. toastr.error('不能输入其它非数字类型字符');
  573. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  574. return;
  575. }
  576. const num = parseFloat(validText);
  577. const reg = materialDecimal.up ? new RegExp("^\\d+(\\.\\d{1,"+ materialDecimal.up +"})?$") : new RegExp("^\\d+?$");
  578. if (validText !== null && (num < 0 || !reg.test(num))) {
  579. toastr.warning('已保留'+ materialDecimal.up +'位小数');
  580. validText = ZhCalc.round(num, materialDecimal.up);
  581. }
  582. }
  583. if (col.field === 'm_up_risk' || col.field === 'm_down_risk' || col.field === 'm_tax') {
  584. // 只能输入正整数
  585. if (isNaN(validText)) {
  586. toastr.error('不能输入其它非数字类型字符');
  587. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  588. return;
  589. }
  590. const num = parseFloat(validText);
  591. if (validText !== null && (num < 0 || num > 100 || !/^\d+$/.test(num))) {
  592. toastr.error('只能输入0-100的正整数');
  593. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  594. return;
  595. }
  596. }
  597. if (col.field === 't_type') {
  598. if (validText === 1) {
  599. select.quantity = null;
  600. select.expr = null;
  601. select.m_tp = null;
  602. }
  603. }
  604. select[col.field] = validText;
  605. select.msg_spread = materialCol.getValue.msg_spread(select);
  606. select.m_spread = materialCol.getValue.m_spread(select);
  607. select.m_tp = materialCol.getValue.m_tp(select);
  608. select.m_tax_tp = materialCol.getValue.m_tax_tp(select);
  609. delete select.waitingLoading;
  610. // console.log(select);
  611. // 更新至服务器
  612. postData(window.location.pathname + '/save', { type:'update', updateData: select, ms_id: $('#myTab').find('.active').data('msid') || null }, function (result) {
  613. m_tp = result.m_tp;
  614. if (materialTax) {
  615. m_tax_tp = result.m_tax_tp;
  616. }
  617. if (isStageSelf) {
  618. materialSpreadObj.updateMaterialData(result);
  619. }
  620. resetTpTable();
  621. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  622. // 判断如果是更改了编号,名称,单位,月信息价需要跟着改变值
  623. if (months.length > 0 && (col.field === 'code' || col.field === 'name' || col.field === 'unit')) {
  624. const index = materialBillsData.indexOf(select);
  625. monthsList[index][col.field] = validText;
  626. monthFunGather.monthsListSet();
  627. }
  628. }, function () {
  629. select[col.field] = orgValue;
  630. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  631. });
  632. }
  633. },
  634. buttonClicked: function (e, info) {
  635. if (info.sheet.zh_setting) {
  636. const select = SpreadJsObj.getSelectObject(info.sheet);
  637. const col = info.sheet.zh_setting.cols[info.col];
  638. if (materialCol.readOnly.isEdit(select)) {
  639. return;
  640. }
  641. if (col.field === 'is_summary') {
  642. if (info.sheet.isEditing()) {
  643. info.sheet.endEdit(true);
  644. }
  645. select.is_summary = info.sheet.getValue(info.row, info.col) ? 1 : 0;
  646. delete select.waitingLoading;
  647. // 更新至服务器
  648. postData(window.location.pathname + '/save', { type:'update', updateData: select, ms_id: $('#myTab').find('.active').data('msid') || null }, function (result) {
  649. m_tp = result.m_tp;
  650. if (materialTax) {
  651. m_tax_tp = result.m_tax_tp;
  652. }
  653. if (isStageSelf) {
  654. materialSpreadObj.updateMaterialData(result);
  655. }
  656. resetTpTable();
  657. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  658. }, function () {
  659. select.is_summary = info.sheet.getValue(info.row, info.col) ? 0 : 1;
  660. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  661. });
  662. }
  663. }
  664. },
  665. deletePress: function (sheet) {
  666. return;
  667. },
  668. clipboardPasted(e, info) {
  669. const hint = {
  670. cellError: {type: 'error', msg: '粘贴内容超出了表格范围'},
  671. codeEmpty: {type: 'error', msg: '请先输入编号,并且不超过15个字符'},
  672. codeUsed: {type: 'error', msg: '该编号已存在,请重新输入。'},
  673. codeError: {type: 'error', msg: '编号为纯数字时,不能为小数'},
  674. numberExpr: {type: 'error', msg: '不能粘贴其它非数字类型字符'},
  675. riskCan: {type: 'error', msg: '只能粘贴0-100的正整数'},
  676. numberCan: {type: 'warning', msg: '已保留'+ materialDecimal.up +'位小数'},
  677. };
  678. const range = info.cellRange;
  679. const sortData = info.sheet.zh_data || [];
  680. if (info.cellRange.row + info.cellRange.rowCount > sortData.length) {
  681. toastMessageUniq(hint.cellError);
  682. // SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  683. SpreadJsObj.reLoadSheetHeader(materialSpread.getActiveSheet());
  684. SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  685. return;
  686. }
  687. if (sortData.length > 0 && range.col + range.colCount > 18) {
  688. toastMessageUniq(hint.cellError);
  689. SpreadJsObj.reLoadSheetHeader(materialSpread.getActiveSheet());
  690. SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  691. return;
  692. }
  693. const data = [];
  694. // const rowData = [];
  695. for (let iRow = 0; iRow < range.rowCount; iRow++) {
  696. let bPaste = true;
  697. const curRow = range.row + iRow;
  698. // const materialData = JSON.parse(JSON.stringify(sortData[curRow]));
  699. const materialData = { id: sortData[curRow].id };
  700. const hintRow = range.rowCount > 1 ? curRow : '';
  701. let sameCol = 0;
  702. for (let iCol = 0; iCol < range.colCount; iCol++) {
  703. const curCol = range.col + iCol;
  704. const colSetting = info.sheet.zh_setting.cols[curCol];
  705. if (!colSetting) continue;
  706. let validText = info.sheet.getText(curRow, curCol);
  707. validText = is_numeric(validText) ? parseFloat(validText) : (validText ? trimInvalidChar(validText) : null);
  708. const orgValue = sortData[curRow][colSetting.field];
  709. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  710. sameCol++;
  711. if (range.colCount === sameCol) {
  712. bPaste = false;
  713. }
  714. continue;
  715. }
  716. if (colSetting.field === 'code' && (validText === '' || validText.length > 15)) {
  717. toastMessageUniq(getPasteHint(hint.codeEmpty, hintRow));
  718. bPaste = false;
  719. continue;
  720. }
  721. if (colSetting.field === 'code' && validText.toString().indexOf('.') !== -1) {
  722. toastMessageUniq(getPasteHint(hint.codeError, hintRow));
  723. bPaste = false;
  724. continue;
  725. }
  726. if (colSetting.field === 'code' && _.find(sortData, { code: validText.toString() })) {
  727. toastMessageUniq(getPasteHint(hint.codeUsed, hintRow));
  728. bPaste = false;
  729. continue;
  730. }
  731. if (colSetting.field === 't_type' || colSetting.field === 'm_type') {
  732. continue;
  733. }
  734. if (colSetting.field === 'basic_price' || colSetting.field === 'msg_tp'
  735. || colSetting.field === 'm_up_risk' || colSetting.field === 'm_down_risk') {
  736. if (isNaN(validText)) {
  737. toastMessageUniq(getPasteHint(hint.numberExpr, hintRow));
  738. bPaste = false;
  739. continue;
  740. }
  741. const num = parseFloat(validText);
  742. if (colSetting.field === 'basic_price' || colSetting.field === 'msg_tp') {
  743. const reg = materialDecimal.up ? new RegExp("^\\d+(\\.\\d{1,"+ materialDecimal.up +"})?$") : new RegExp("^\\d+?$");
  744. if (validText !== null && (num < 0 || !reg.test(num))) {
  745. toastMessageUniq(getPasteHint(hint.numberCan, hintRow));
  746. validText = ZhCalc.round(num, materialDecimal.up);
  747. }
  748. } else if (colSetting.field === 'm_up_risk' || colSetting.field === 'm_down_risk' || colSetting.field === 'm_tax') {
  749. if (validText !== null && (num < 0 || num > 100 || !/^\d+$/.test(num))) {
  750. toastMessageUniq(getPasteHint(hint.riskCan, hintRow));
  751. bPaste = false;
  752. continue;
  753. }
  754. }
  755. }
  756. materialData[colSetting.field] = validText;
  757. sortData[curRow][colSetting.field] = validText;
  758. }
  759. if (materialData.code === '' || materialData.code === null) {
  760. toastMessageUniq(getPasteHint(hint.codeEmpty, hintRow));
  761. bPaste = false;
  762. }
  763. if (bPaste) {
  764. materialData.msg_spread = materialCol.getValue.msg_spread(sortData[curRow]);
  765. materialData.m_spread = materialCol.getValue.m_spread(sortData[curRow]);
  766. materialData.m_tp = materialCol.getValue.m_tp(sortData[curRow]);
  767. materialData.m_tax_tp = materialCol.getValue.m_tax_tp(sortData[curRow]);
  768. data.push(materialData);
  769. // rowData.push(curRow);
  770. } else {
  771. SpreadJsObj.reLoadRowData(info.sheet, curRow);
  772. }
  773. }
  774. if (data.length === 0) {
  775. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  776. return;
  777. }
  778. // console.log(data);
  779. // 更新至服务器
  780. postData(window.location.pathname + '/save', { type:'paste', updateData: data, ms_id: $('#myTab').find('.active').data('msid') || null }, function (result) {
  781. materialBillsData = result.info;
  782. m_tp = result.m_tp;
  783. if (materialTax) {
  784. m_tax_tp = result.m_tax_tp;
  785. }
  786. if (isStageSelf) {
  787. materialSpreadObj.updateMaterialData(result);
  788. materialSpreadObj.getMaterialBillsData();
  789. }
  790. // SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  791. materialSpreadObj.materialSheetReset();
  792. // SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  793. // for (const row in rowData) {
  794. // materialBillsData.splice(index, 1, result.info[row]);
  795. // SpreadJsObj.reLoadRowData(info.sheet, row);
  796. // }
  797. if (months.length > 0) {
  798. for (const [i,m] of monthsList.entries()) {
  799. m.code = materialBillsData[i].code;
  800. m.name = materialBillsData[i].name;
  801. m.unit = materialBillsData[i].unit;
  802. m.origin = materialBillsData[i].origin;
  803. }
  804. // SpreadJsObj.loadSheetData(materialMonthSpread.getActiveSheet(), SpreadJsObj.DataType.Data, monthsList);
  805. monthFunGather.monthsListSet();
  806. // SpreadJsObj.reLoadSheetData(materialMonthSpread.getActiveSheet());
  807. }
  808. resetTpTable();
  809. // materialSpreadObj.refreshActn();
  810. }, function () {
  811. // materialSpreadObj.refreshActn();
  812. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  813. return;
  814. });
  815. },
  816. setReadOnly: function(readOnly) {
  817. // SpreadJsObj.resetFieldReadOnly(materialSpread.getActiveSheet(), 'msg_spread', 'm_spread', 'm_tp', 'pre_tp', readOnly);
  818. }
  819. };
  820. materialSpread.bind(spreadNS.Events.SelectionChanged, materialSpreadObj.selectionChanged);
  821. materialSpread.bind(spreadNS.Events.ClipboardPasted, materialSpreadObj.clipboardPasted);
  822. SpreadJsObj.addDeleteBind(materialSpread, materialSpreadObj.deletePress);
  823. materialSpread.bind(spreadNS.Events.EditEnded, materialSpreadObj.editEnded);
  824. materialSpread.bind(spreadNS.Events.ButtonClicked, materialSpreadObj.buttonClicked);
  825. const sheet = materialSpread.getActiveSheet();
  826. sheet.suspendPaint();
  827. // materialSpreadObj.refreshActn();
  828. // const basic_range = sheet.getRange(-1, 8, -1, 1);
  829. // basic_range.cellType(new DatePickerCellType());
  830. // basic_range.formatter("yyyy-MM-dd");
  831. // const msg_range = sheet.getRange(-1, 10, -1, 1);
  832. // msg_range.cellType(new DatePickerCellType());
  833. // msg_range.formatter("yyyy-MM-dd");
  834. sheet.resumePaint();
  835. const static_cols = [
  836. {title: '编号', colSpan: '1', rowSpan: '2', field: 'code', hAlign: 0, width: 60, formatter: '@', readOnly: true},
  837. {title: '名称', colSpan: '1', rowSpan: '2', field: 'name', hAlign: 0, width: 100, formatter: '@', readOnly: true},
  838. {title: '单位', colSpan: '1', rowSpan: '2', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true},
  839. {title: '来源地', colSpan: '1', rowSpan: '2', field: 'origin', hAlign: 0, width: 70, formatter: '@', readOnly: 'readOnly.isEdit'},
  840. {title: '平均单价', colSpan: '1', rowSpan: '2', field: 'average_msg_tp', hAlign: 2, width: 60, type: 'Number', readOnly: true, getValue:'getValue.average_msg_tp'},
  841. ];
  842. // 月信息价方法集合
  843. const monthFunGather = {
  844. _setMonthHtml: function() {
  845. let html = '';
  846. let qihtml = '';
  847. for (const m of months) {
  848. html += '<div class="custom-control custom-checkbox mb-2">\n' +
  849. ' <input type="checkbox" name="del_month" value="' + m + '" class="custom-control-input" id="month_' + m + '">\n' +
  850. ' <label class="custom-control-label" for="month_' + m + '">' + m + '月</label>\n' +
  851. ' </div>';
  852. qihtml += parseInt(m.split('-')[1]) + '月,';
  853. }
  854. if (months.length > 0) {
  855. qihtml = '<span class="mx-2 text-muted">/</span>本期月信息价:' + qihtml;
  856. qihtml = qihtml.substring(0, qihtml.length-1);
  857. }
  858. $('#show_month').html(html);
  859. $('#qi-month').html(qihtml);
  860. },
  861. _monthHeaderSet: function() {
  862. const newMonths = [];
  863. for (const m of months) {
  864. const year = m.split('-')[0];
  865. const month = parseInt(m.split('-')[1]);
  866. let one = _.find(newMonths, { 'year': year });
  867. let oneIndex = _.findIndex(newMonths, { 'year': year });
  868. if (one) {
  869. one.month.push(month);
  870. newMonths.splice(oneIndex, 1, one);
  871. } else {
  872. one = {
  873. year: year,
  874. month: [ month ],
  875. };
  876. newMonths.push(one);
  877. }
  878. }
  879. const pushMonth = [];
  880. for(const mo of newMonths) {
  881. for (let i in mo.month) {
  882. i = parseInt(i);
  883. const newCols = {
  884. title: (i === 0 ? mo.year: '') + '|' + mo.month[i] + '月',
  885. colSpan: (i === 0 ? (mo.month.length === 1 ? '0' : mo.month.length) : '') + '|1',
  886. rowSpan: i === 0 ? '1|1' : '|1',
  887. field: mo.year + '-' + (mo.month[i] < 10 ? '0' + mo.month[i] : mo.month[i]),
  888. hAlign: 2, width: 60, type: '@', readOnly: 'readOnly.isEdit'};
  889. pushMonth.push(newCols);
  890. }
  891. }
  892. return pushMonth;
  893. },
  894. monthSheetReset: function (ml = monthsList) {
  895. const monthCols = monthFunGather._monthHeaderSet();
  896. const newMonthSpreadHeaderCols = static_cols.concat(monthCols);
  897. materialMonthSpreadSetting.cols = newMonthSpreadHeaderCols;
  898. // 表头变化需要重新绘制,不然报错;
  899. materialMonthSpread.getActiveSheet().reset();
  900. SpreadJsObj.initSpreadSettingEvents(materialMonthSpreadSetting, materialMonthCol);
  901. SpreadJsObj.initSheet(materialMonthSpread.getActiveSheet(), materialMonthSpreadSetting);
  902. SpreadJsObj.loadSheetData(materialMonthSpread.getActiveSheet(), SpreadJsObj.DataType.Data, ml);
  903. monthFunGather._setMonthHtml();
  904. },
  905. monthsListSet: function (ml = materialBillsData, checked = true) {
  906. if($('#bills0_list').is(':checked') && checked) {
  907. ml = _.filter(materialBillsData, function (item) {
  908. return item.quantity === null || item.quantity === 0;
  909. });
  910. }
  911. const newMonthsList = _.filter(monthsList, function (item) {
  912. return _.findIndex(ml, { code: item.code }) !== -1;
  913. });
  914. monthFunGather.monthSheetReset(newMonthsList);
  915. }
  916. };
  917. const materialMonthSpread = SpreadJsObj.createNewSpread($('#material-month-spread')[0]);
  918. const materialMonthSpreadSetting = {
  919. cols: static_cols,
  920. emptyRows: 0,
  921. headRows: 2,
  922. headRowHeight: [25, 25],
  923. defaultRowHeight: 21,
  924. headerFont: '12px 微软雅黑',
  925. font: '12px 微软雅黑',
  926. readOnly: readOnly,
  927. };
  928. if (months.length > 0) {
  929. const monthCols = monthFunGather._monthHeaderSet();
  930. const monthSpreadHeaderCols = static_cols.concat(monthCols);
  931. materialMonthSpreadSetting.cols = monthSpreadHeaderCols;
  932. }
  933. const materialMonthCol = {
  934. getValue: {
  935. average_msg_tp: function (data) {
  936. let msg_tp = 0;
  937. let hadnum = 0;
  938. for (const m of months) {
  939. msg_tp += data[m];
  940. if (data[m] !== null && data[m] !== '' && data[m] !== 0) {
  941. hadnum++;
  942. }
  943. }
  944. const average_tp = hadnum !== 0 ? ZhCalc.round(ZhCalc.div(msg_tp, hadnum), materialDecimal.up) : ZhCalc.round(ZhCalc.div(msg_tp, months.length), materialDecimal.up);
  945. return average_tp;
  946. },
  947. },
  948. readOnly: {
  949. isEdit: function (data) {
  950. return readOnly;
  951. },
  952. },
  953. };
  954. materialMonthSpread.getActiveSheet().frozenColumnCount(5);
  955. materialMonthSpread.getActiveSheet().options.frozenlineColor = '#f1f1f1';
  956. SpreadJsObj.initSpreadSettingEvents(materialMonthSpreadSetting, materialMonthCol);
  957. SpreadJsObj.initSheet(materialMonthSpread.getActiveSheet(), materialMonthSpreadSetting);
  958. SpreadJsObj.loadSheetData(materialMonthSpread.getActiveSheet(), SpreadJsObj.DataType.Data, monthsList);
  959. const materialMonthSpreadObj = {
  960. editEnded: function (e, info) {
  961. if (info.sheet.zh_setting) {
  962. const select = SpreadJsObj.getSelectObject(info.sheet);
  963. const col = info.sheet.zh_setting.cols[info.col];
  964. // 未改变值则不提交
  965. let validText = is_numeric(info.editingText) ? parseFloat(info.editingText) : (info.editingText ? trimInvalidChar(info.editingText) : null);
  966. const orgValue = select[col.field];
  967. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  968. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  969. return;
  970. }
  971. if (col.field === 'origin') {
  972. select[col.field] = validText;
  973. postData(window.location.pathname + '/month/save', { type:'updateOrigin', updateData: { mb_id: select.mb_id, value: validText } }, function (result) {
  974. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  975. materialBillsData = result.materialBillsData;
  976. materialSpreadObj.materialSheetReset();
  977. // SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  978. }, function () {
  979. select[col.field] = orgValue;
  980. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  981. });
  982. } else {
  983. // 判断部分值是否输入的是数字判断和数据计算
  984. if (isNaN(validText)) {
  985. toastr.error('不能输入其它非数字类型字符');
  986. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  987. return;
  988. }
  989. const num = parseFloat(validText);
  990. const reg = materialDecimal.up ? new RegExp("^\\d+(\\.\\d{1,"+ materialDecimal.up +"})?$") : new RegExp("^\\d+?$");
  991. if (validText !== null && (num < 0 || !reg.test(num))) {
  992. toastr.warning('已保留'+ materialDecimal.up +'位小数');
  993. validText = ZhCalc.round(num, materialDecimal.up);
  994. }
  995. select[col.field] = validText;
  996. // 更新至服务器
  997. postData(window.location.pathname + '/month/save', { type:'update', updateData: { mb_id: select.mb_id, yearmonth: col.field, value: validText } }, function (result) {
  998. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  999. materialBillsData = result.materialBillsData;
  1000. materialSpreadObj.materialSheetReset();
  1001. // SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  1002. m_tp = result.m_tp;
  1003. if (materialTax) {
  1004. m_tax_tp = result.m_tax_tp;
  1005. }
  1006. resetTpTable();
  1007. }, function () {
  1008. select[col.field] = orgValue;
  1009. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1010. });
  1011. }
  1012. }
  1013. },
  1014. deletePress: function (sheet) {
  1015. // if (sheet.zh_setting && sheet.zh_data) {
  1016. // const sel = sheet.getSelections()[0];
  1017. // if (!sel) return;
  1018. //
  1019. // const col = sheet.zh_setting.cols[sel.col];
  1020. // const select = SpreadJsObj.getSelectObject(sheet);
  1021. // const orgValue = select[col.field];
  1022. // if (sel.colCount > 1 || sel.rowCount > 1) {
  1023. // toastr.warning('请勿同时删除多行或多列数据');
  1024. // }
  1025. // console.log(sel, select, col);
  1026. // if (orgValue === null || col.field === 'type' || col.field === 'is_summary') {
  1027. // return;
  1028. // }
  1029. // select[col.field] = null;
  1030. // select.calc_num = materialExponentCol.getValue.calc_num(select);
  1031. // console.log(select);
  1032. // 更新至服务器
  1033. // postData(window.location.pathname + '/save', { type:'update', updateData: select }, function (result) {
  1034. // ex_tp = result.ex_tp;
  1035. // ex_expr = result.ex_expr;
  1036. // resetExTpTable();
  1037. // SpreadJsObj.reLoadRowData(sheet, sel.row);
  1038. // materialExponentData.splice(sel.row, 1, select);
  1039. // }, function () {
  1040. // select[col.field] = orgValue;
  1041. // SpreadJsObj.reLoadRowData(sheet, sel.row);
  1042. // });
  1043. // }
  1044. return;
  1045. },
  1046. clipboardPasted(e, info) {
  1047. const hint = {
  1048. cellError: {type: 'error', msg: '粘贴内容超出了表格范围'},
  1049. numberExpr: {type: 'error', msg: '不能粘贴其它非数字类型字符'},
  1050. numberCan: {type: 'warning', msg: '已保留'+ materialDecimal.up +'位小数'},
  1051. };
  1052. const range = info.cellRange;
  1053. const sortData = info.sheet.zh_data || [];
  1054. if (info.cellRange.row + info.cellRange.rowCount > sortData.length) {
  1055. toastMessageUniq(hint.cellError);
  1056. SpreadJsObj.reLoadSheetHeader(materialMonthSpread.getActiveSheet());
  1057. SpreadJsObj.reLoadSheetData(materialMonthSpread.getActiveSheet());
  1058. return;
  1059. }
  1060. if (sortData.length > 0 && range.col + range.colCount > 5 + months.length) {
  1061. toastMessageUniq(hint.cellError);
  1062. SpreadJsObj.reLoadSheetHeader(materialMonthSpread.getActiveSheet());
  1063. SpreadJsObj.reLoadSheetData(materialMonthSpread.getActiveSheet());
  1064. return;
  1065. }
  1066. const data = [];
  1067. let pasteOrigin = false;
  1068. for (let iRow = 0; iRow < range.rowCount; iRow++) {
  1069. let bPaste = true;
  1070. const curRow = range.row + iRow;
  1071. const materialMonthData = sortData[curRow];
  1072. const hintRow = range.rowCount > 1 ? curRow : '';
  1073. let sameCol = 0;
  1074. for (let iCol = 0; iCol < range.colCount; iCol++) {
  1075. const curCol = range.col + iCol;
  1076. const colSetting = info.sheet.zh_setting.cols[curCol];
  1077. if (!colSetting) continue;
  1078. let validText = info.sheet.getText(curRow, curCol);
  1079. validText = is_numeric(validText) ? parseFloat(validText) : (validText ? trimInvalidChar(validText) : null);
  1080. const orgValue = sortData[curRow][colSetting.field];
  1081. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  1082. sameCol++;
  1083. if (range.colCount === sameCol) {
  1084. bPaste = false;
  1085. }
  1086. continue;
  1087. }
  1088. if (colSetting.field === 'origin') {
  1089. pasteOrigin = true;
  1090. } else {
  1091. const num = parseFloat(validText);
  1092. if (isNaN(validText)) {
  1093. toastMessageUniq(getPasteHint(hint.numberExpr, hintRow));
  1094. bPaste = false;
  1095. continue;
  1096. }
  1097. const reg = materialDecimal.up ? new RegExp("^\\d+(\\.\\d{1,"+ materialDecimal.up +"})?$") : new RegExp("^\\d+?$");
  1098. if (validText !== null && (num < 0 || !reg.test(num))) {
  1099. toastMessageUniq(getPasteHint(hint.numberCan, hintRow));
  1100. validText = ZhCalc.round(num, materialDecimal.up);
  1101. }
  1102. }
  1103. materialMonthData[colSetting.field] = validText;
  1104. sortData[curRow][colSetting.field] = validText;
  1105. }
  1106. if (bPaste) {
  1107. data.push(materialMonthData);
  1108. } else {
  1109. SpreadJsObj.reLoadRowData(info.sheet, curRow);
  1110. }
  1111. }
  1112. if (data.length === 0) {
  1113. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  1114. return;
  1115. }
  1116. // console.log(data);
  1117. if (pasteOrigin) {
  1118. // 更新至服务器
  1119. postData(window.location.pathname + '/month/save', { type:'pasteOrigin', updateData: data }, function (result) {
  1120. SpreadJsObj.reLoadSheetData(materialMonthSpread.getActiveSheet());
  1121. materialBillsData = result.materialBillsData;
  1122. }, function () {
  1123. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  1124. return;
  1125. });
  1126. } else {
  1127. // 更新至服务器
  1128. postData(window.location.pathname + '/month/save', { type:'paste', updateData: data }, function (result) {
  1129. SpreadJsObj.reLoadSheetData(materialMonthSpread.getActiveSheet());
  1130. materialBillsData = result.materialBillsData;
  1131. materialSpreadObj.materialSheetReset();
  1132. // SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  1133. m_tp = result.m_tp;
  1134. if (materialTax) {
  1135. m_tax_tp = result.m_tax_tp;
  1136. }
  1137. resetTpTable();
  1138. }, function () {
  1139. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  1140. return;
  1141. });
  1142. }
  1143. },
  1144. };
  1145. SpreadJsObj.initSpreadSettingEvents(materialSpreadSetting, materialCol);
  1146. SpreadJsObj.initSheet(materialSpread.getActiveSheet(), materialSpreadSetting);
  1147. materialSpreadObj.getMaterialBillsData();
  1148. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  1149. materialSpreadObj.refreshActn();
  1150. materialMonthSpread.bind(spreadNS.Events.ClipboardPasted, materialMonthSpreadObj.clipboardPasted);
  1151. SpreadJsObj.addDeleteBind(materialMonthSpread, materialMonthSpreadObj.deletePress);
  1152. materialMonthSpread.bind(spreadNS.Events.EditEnded, materialMonthSpreadObj.editEnded);
  1153. // 期切换
  1154. $('#myTab a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
  1155. e.preventDefault();
  1156. showWaitingView();
  1157. setTimeout(function () {
  1158. materialSpreadObj.getMaterialBillsData();
  1159. materialSpreadObj.materialSheetReset(true);
  1160. // SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  1161. // 消耗量表格更新
  1162. let html = '';
  1163. calcBase = _.find(calcBaseList, { ms_id: $('#myTab').find('.active').data('msid') }).calcBase;
  1164. for (let iBase = 0; iBase < calcBase.length; iBase++) {
  1165. html += '<tr><td>' + calcBase[iBase].name + '</td><td>' + calcBase[iBase].code + '</td><td>' + calcBase[iBase].value + '</td></tr>';
  1166. }
  1167. $('#calcBase').html(html);
  1168. closeWaitingView();
  1169. }, 1000);
  1170. });
  1171. if (!readOnly || editForAudit) {
  1172. $('#expr_select button').on('click', function () {
  1173. const code = $(this).text();
  1174. // $('#expr').val($('#expr').val() + code);
  1175. $('#expr').insertAtCaret(code);
  1176. });
  1177. const ExprObj = {
  1178. _checkExprValid(expr, invalidParam) {
  1179. if (!expr) return [true, null];
  1180. const param = [];
  1181. let num = '', base = '';
  1182. for (let i = 0, iLen = expr.length; i < iLen; i++) {
  1183. if (/^[\d\.%]+/.test(expr[i])) {
  1184. if (base !== '') {
  1185. param.push({type: 'base', value: base});
  1186. base = '';
  1187. }
  1188. num = num + expr[i];
  1189. } else if (/^[a-z]/.test(expr[i])) {
  1190. if (num !== '') {
  1191. param.push({type: 'num', value: num});
  1192. num = '';
  1193. }
  1194. base = base + expr[i];
  1195. } else if (expr[i] === '(') {
  1196. if (num !== '') {
  1197. param.push({type: 'num', value: num});
  1198. num = '';
  1199. }
  1200. if (base !== '') {
  1201. param.push({type: 'base', value: base});
  1202. base = '';
  1203. }
  1204. param.push({type: 'left', value: '('});
  1205. } else if (expr[i] === ')') {
  1206. if (num !== '') {
  1207. param.push({type: 'num', value: num});
  1208. num = '';
  1209. }
  1210. if (base !== '') {
  1211. param.push({type: 'base', value: base});
  1212. base = '';
  1213. }
  1214. param.push({type: 'right', value: ')'});
  1215. } else if (/^[\+\-*\/]/.test(expr[i])) {
  1216. if (num !== '') {
  1217. param.push({type: 'num', value: num});
  1218. num = '';
  1219. }
  1220. if (base !== '') {
  1221. param.push({type: 'base', value: base});
  1222. base = '';
  1223. }
  1224. param.push({type: 'calc', value: expr[i]});
  1225. } else {
  1226. return [false, '输入的表达式含有非法字符: ' + expr[i]];
  1227. }
  1228. }
  1229. if (num !== '') {
  1230. param.push({type: 'num', value: num});
  1231. num = '';
  1232. }
  1233. if (base !== '') {
  1234. param.push({type: 'base', value: base});
  1235. base = '';
  1236. }
  1237. if (param.length === 0) return true;
  1238. if (param.length > 1) {
  1239. if (param[0].value === '-') {
  1240. param[1].value = '-' + param[1];
  1241. }
  1242. param.unshift();
  1243. }
  1244. const iLen = param.length;
  1245. let iLeftCount = 0, iRightCount = 0;
  1246. for (const [i, p] of param.entries()) {
  1247. if (p.type === 'calc') {
  1248. if (i === 0 || i === iLen - 1)
  1249. return [false, '输入的表达式非法:计算符号' + p.value + '前后应有数字或计算基数'];
  1250. }
  1251. if (p.type === 'num') {
  1252. num = p.value.replace('%', '');
  1253. if (p.value.length - num.length > 1)
  1254. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  1255. num = _.toNumber(num);
  1256. if (num === undefined || num === null || _.isNaN(num))
  1257. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  1258. if (i > 0) {
  1259. if (param[i - 1].type !== 'calc') {
  1260. return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
  1261. } else if (param[i - 1].value === '/' && num === 0) {
  1262. return [false, '输入的表达式非法:请勿除0'];
  1263. }
  1264. }
  1265. }
  1266. if (p.type === 'base') {
  1267. const baseParam = _.find(calcBase, {code: p.value});
  1268. if (!baseParam)
  1269. return [false, '输入的表达式非法:不存在计算基数' + p.value];
  1270. if (invalidParam && invalidParam.indexOf(p.value) >= 0)
  1271. return [false, '不可使用计算基数' + p.value];
  1272. if (i > 0 && (param[i - 1].type === 'num' || param[i - 1].type === 'right'))
  1273. return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
  1274. }
  1275. if (p.type === 'left') {
  1276. iLeftCount += 1;
  1277. if (i !== 0 && param[i-1].type !== 'calc')
  1278. return [false, '输入的表达式非法:(前应有运算符'];
  1279. }
  1280. if (p.type === 'right') {
  1281. iRightCount += 1;
  1282. if (i !== iLen - 1 && param[i+1].type !== 'calc')
  1283. return [false, '输入的表达式非法:)后应有运算符'];
  1284. if (iRightCount > iLeftCount)
  1285. return [false, '输入的表达式非法:")"前无对应的"("'];
  1286. }
  1287. }
  1288. if (iLeftCount > iRightCount)
  1289. return [false, '输入的表达式非法:"("后无对应的")"'];
  1290. return [true, ''];
  1291. },
  1292. _checkExpr: function (text) {
  1293. if (text) {
  1294. const num = _.toNumber(text);
  1295. if (num) {
  1296. console.log(num);
  1297. } else {
  1298. const expr = text.replace('=', '').toLowerCase();
  1299. const [valid, msg] = this._checkExprValid(expr);
  1300. if (!valid) return [valid, msg];
  1301. }
  1302. }
  1303. return [true, ''];
  1304. },
  1305. };
  1306. $('#expr_btn').click(function () {
  1307. const expr = $('#expr').val();
  1308. // 判断表达式格式
  1309. const [valid, msg] = ExprObj._checkExpr(expr);
  1310. if (!valid) {
  1311. toastr.error(msg);
  1312. return false;
  1313. }
  1314. postData(window.location.pathname + '/save', { type:'expr', id: $('#materialbillsId').val(), expr: expr, ms_id: $('#myTab').find('.active').data('msid') || null }, function (result) {
  1315. m_tp = result.m_tp;
  1316. if (materialTax) {
  1317. m_tax_tp = result.m_tax_tp;
  1318. }
  1319. const sheet = materialSpread.getActiveSheet();
  1320. const select = SpreadJsObj.getSelectObject(sheet);
  1321. const index = _.findIndex(materialBillsData, { id: select.id });
  1322. let newIndex = index;
  1323. if (isStageSelf) {
  1324. materialSpreadObj.updateMaterialData(result);
  1325. result.info = materialSpreadObj.updateOneMaterialBill(result.info);
  1326. }
  1327. if($('#bills0_list').is(':checked')) {
  1328. const newMaterialBillsData = _.filter(materialBillsData, function (item) {
  1329. return item.quantity !== null && item.quantity !== 0;
  1330. });
  1331. newIndex = newMaterialBillsData.indexOf(select);
  1332. newMaterialBillsData.splice(newIndex, 1, result.info);
  1333. sheet.zh_data[newIndex] = result.info;
  1334. }
  1335. materialBillsData.splice(index, 1, result.info);
  1336. sheet.zh_data.splice(newIndex, 1, result.info);
  1337. SpreadJsObj.reLoadRowData(sheet, newIndex);
  1338. resetTpTable();
  1339. $('#bcyy').modal('hide');
  1340. });
  1341. });
  1342. }
  1343. if (!readOnly && !editForAudit) {
  1344. $('#add').click(materialSpreadObj.add);
  1345. $('#del').click(materialSpreadObj.del);
  1346. $('#up-move').click(materialSpreadObj.upMove);
  1347. $('#down-move').click(materialSpreadObj.downMove);
  1348. // 右键菜单
  1349. $.contextMenu({
  1350. selector: '#material-spread',
  1351. build: function ($trigger, e) {
  1352. const target = SpreadJsObj.safeRightClickSelection($trigger, e, materialSpread);
  1353. return target.hitTestType === GC.Spread.Sheets.SheetArea.viewport || target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader;
  1354. },
  1355. items: {
  1356. 'create': {
  1357. name: '新增材料',
  1358. icon: 'fa-sign-in',
  1359. callback: function (key, opt) {
  1360. materialSpreadObj.add(materialSpread.getActiveSheet());
  1361. },
  1362. },
  1363. 'delete': {
  1364. name: '删除材料',
  1365. icon: 'fa-remove',
  1366. callback: function (key, opt) {
  1367. materialSpreadObj.del(materialSpread.getActiveSheet());
  1368. },
  1369. disabled: function (key, opt) {
  1370. const sheet = materialSpread.getActiveSheet();
  1371. const select = SpreadJsObj.getSelectObject(sheet);
  1372. const sel = sheet.getSelections()[0];
  1373. materialSpreadObj.refreshActn(sel.rowCount);
  1374. if (!readOnly && select && materialBase.isUsed(select) && sel.rowCount === 1) {
  1375. return false;
  1376. } else {
  1377. return true;
  1378. }
  1379. }
  1380. },
  1381. }
  1382. });
  1383. $('.changeRate').click(function () {
  1384. $('#rateInput').val(parseInt($(this).data('value')));
  1385. $('#rateInput').siblings('.dropdown-menu').hide();
  1386. });
  1387. $('#rateInput').click(function () {
  1388. $(this).siblings('.dropdown-menu').show();
  1389. })
  1390. // 回车提交
  1391. $('#rateInput').on('keypress', function () {
  1392. if(window.event.keyCode === 13) {
  1393. $(this).blur();
  1394. }
  1395. });
  1396. $('#rateInput').blur(function () {
  1397. const _self = $(this);
  1398. setTimeout(function () {
  1399. let rate = parseFloat(_self.val());
  1400. if (_.isNaN(rate)) {
  1401. toastr.error('请输入0-100之前的整数值');
  1402. $('#rateInput').val(materialRate);
  1403. return;
  1404. }
  1405. rate = _.round(rate);
  1406. if(rate < 0 || rate > 100) {
  1407. toastr.error('请输入0-100之前的整数值');
  1408. $('#rateInput').val(materialRate);
  1409. return;
  1410. }
  1411. $('#rateInput').siblings('.dropdown-menu').hide();
  1412. console.log(rate, materialRate);
  1413. if (rate !== materialRate) {
  1414. postData(window.location.pathname + '/save', { type:'rate', rate: rate }, function (result) {
  1415. const bqhs = ZhCalc.round(ZhCalc.mul(m_tp, 1+rate/100), materialDecimal.tp);
  1416. // const exbqhs = ZhCalc.round(ZhCalc.mul(ex_tp, 1+rate/100), materialDecimal.tp);
  1417. const jzbqhs = ZhCalc.round(ZhCalc.add(pre_tp_hs, bqhs), materialDecimal.tp);
  1418. // const exjzbqhs = ZhCalc.round(ZhCalc.add(ex_pre_tp_hs, exbqhs), materialDecimal.tp);
  1419. $('#rate_set').find('td').eq(1).text(bqhs !== 0 ? bqhs : '');
  1420. $('#rate_set').find('td').eq(2).text(jzbqhs !== 0 ? jzbqhs : '');
  1421. // $('#rate_set').find('td').eq(3).text(exbqhs !== 0 ? exbqhs : '');
  1422. // $('#rate_set').find('td').eq(4).text(exjzbqhs !== 0 ? exjzbqhs : '');
  1423. materialRate = rate;
  1424. $('#rateInput').val(rate);
  1425. });
  1426. } else {
  1427. $('#rateInput').val(rate);
  1428. }
  1429. }, 500);
  1430. });
  1431. // $('#changeRate').change(function () {
  1432. // const rate = parseInt($(this).val());
  1433. // postData(window.location.pathname + '/save', { type:'rate', rate: rate }, function (result) {
  1434. // const bqhs = ZhCalc.round(ZhCalc.mul(m_tp, 1+rate/100), materialDecimal.tp);
  1435. // const exbqhs = ZhCalc.round(ZhCalc.mul(ex_tp, 1+rate/100), materialDecimal.tp);
  1436. // const jzbqhs = ZhCalc.round(ZhCalc.add(pre_tp_hs, bqhs), materialDecimal.tp);
  1437. // const exjzbqhs = ZhCalc.round(ZhCalc.add(ex_pre_tp_hs, exbqhs), materialDecimal.tp);
  1438. // $('#rate_set').find('td').eq(1).text(bqhs !== 0 ? bqhs : '');
  1439. // $('#rate_set').find('td').eq(2).text(jzbqhs !== 0 ? jzbqhs : '');
  1440. // $('#rate_set').find('td').eq(3).text(exbqhs !== 0 ? exbqhs : '');
  1441. // $('#rate_set').find('td').eq(4).text(exjzbqhs !== 0 ? exjzbqhs : '');
  1442. // });
  1443. // });
  1444. // 创建月信息价
  1445. $('#make-month').click(function() {
  1446. const yearmonth = $('#months').val();
  1447. const result = yearmonth.match(/^(\d{1,4})(-|\/)(\d{1,2})$/);
  1448. if (result === null) {
  1449. toastr.error('请选择正确的信息价月份');
  1450. return false;
  1451. }
  1452. //判断是否已存在当前月份
  1453. if (months.indexOf(yearmonth) !== -1) {
  1454. toastr.error('调差期已创建过本月的信息价');
  1455. return false;
  1456. }
  1457. postData(window.location.pathname + '/month/save', { type: 'add', updateData: { yearmonth: yearmonth } }, function (data) {
  1458. months.push(yearmonth);
  1459. months.sort();
  1460. monthsList = data.monthsList;
  1461. monthFunGather.monthsListSet();
  1462. // 工料表单价显示也要更新
  1463. materialBillsData = data.materialBillsData;
  1464. materialSpreadObj.materialSheetReset();
  1465. // SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  1466. m_tp = data.m_tp;
  1467. if (materialTax) {
  1468. m_tax_tp = data.m_tax_tp;
  1469. }
  1470. resetTpTable();
  1471. $('#add-month').modal('hide');
  1472. });
  1473. });
  1474. // 删除月信息价
  1475. $('#del-month').click(function () {
  1476. const del_month_array = [];
  1477. $("input[name='del_month']:checked").each(function () {
  1478. del_month_array.push($(this).val());
  1479. });
  1480. if (del_month_array.length === 0) {
  1481. toastr.error('请选择要移除的月信息价');
  1482. return false;
  1483. }
  1484. postData(window.location.pathname + '/month/save', { type: 'del', updateData: { del_yearmonth: del_month_array } }, function (data) {
  1485. for (const dm of del_month_array) {
  1486. // _.remove(materialMonthSpreadSetting.cols, function (n) {
  1487. // return n.field === dm;
  1488. // });
  1489. _.remove(months, function (n) {
  1490. return n === dm;
  1491. });
  1492. }
  1493. monthsList = data.monthsList;
  1494. monthFunGather.monthsListSet();
  1495. // 工料表单价显示也要更新
  1496. materialBillsData = data.materialBillsData;
  1497. materialSpreadObj.materialSheetReset();
  1498. // SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  1499. m_tp = data.m_tp;
  1500. if (materialTax) {
  1501. m_tax_tp = data.m_tax_tp;
  1502. }
  1503. resetTpTable();
  1504. $('#remove-month').modal('hide');
  1505. });
  1506. });
  1507. } else {
  1508. // SpreadJsObj.forbiddenSpreadContextMenu('#material-spread', materialSpread);
  1509. }
  1510. // 筛选调差工料本期应耗数量为0
  1511. $('#bills0_list').click(function () {
  1512. const isCheck = $(this).is(':checked');
  1513. let newMaterialBillsData = materialBillsData;
  1514. if (isCheck) {
  1515. newMaterialBillsData = _.filter(materialBillsData, function (item) {
  1516. return item.quantity !== null && item.quantity !== 0;
  1517. });
  1518. SpreadJsObj.initSpreadSettingEvents(materialSpreadSetting, materialCol);
  1519. SpreadJsObj.initSheet(materialSpread.getActiveSheet(), materialSpreadSetting);
  1520. }
  1521. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, newMaterialBillsData);
  1522. monthFunGather.monthsListSet(newMaterialBillsData, false);
  1523. materialSpreadObj.refreshActn();
  1524. });
  1525. let gljLib;
  1526. const gljLibCellDoubleClick = function (e, info) {
  1527. if (readOnly || editForAudit) return;
  1528. const gljSheet = info.sheet;
  1529. if (!gljSheet.zh_setting || !gljSheet.zh_tree) { return; }
  1530. const gljTree = gljSheet.zh_tree;
  1531. const gljNode = gljTree.nodes[info.row];
  1532. const sheet = materialSpread.getActiveSheet();
  1533. const select = SpreadJsObj.getSelectObject(sheet);
  1534. if (!gljNode) return;
  1535. if (gljNode && gljNode.is_leaf === 0) {
  1536. toastr.warning('非最底层项目下,不应添加');
  1537. return;
  1538. }
  1539. postData(window.location.pathname + '/save', { type:'add-glj', postData: gljNode, order: select ? select.order : null }, function (result) {
  1540. if (result) {
  1541. // 比order大的+1
  1542. const maxData = select ? _.filter(materialBillsData, (item) => { return item.order > select.order }) : [];
  1543. _.forEach(maxData, function (item) {
  1544. const cl = _.find(materialBillsData, { id: item.id });
  1545. cl.order = item.order + 1;
  1546. });
  1547. materialBillsData.push(result.info);
  1548. materialBillsData.sort(function (a, b) {
  1549. return a.order - b.order
  1550. });
  1551. console.log(materialBillsData);
  1552. if (isStageSelf) {
  1553. materialSpreadObj.updateMaterialData(result);
  1554. materialSpreadObj.getMaterialBillsData();
  1555. }
  1556. // sheet.addRows(materialBillsData.length - 1, 1);
  1557. // SpreadJsObj.reLoadRowData(sheet, materialBillsData.length - 1);
  1558. let newMaterialBillsData = materialBillsData;
  1559. if($('#bills0_list').is(':checked')) {
  1560. newMaterialBillsData = _.filter(materialBillsData, function (item) {
  1561. return item.quantity !== null && item.quantity !== 0;
  1562. });
  1563. }
  1564. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, newMaterialBillsData);
  1565. // materialSpreadObj.materialSheetReset();
  1566. sheet.setSelection(sheet.zh_data.indexOf(select) + 1, 0, 1, 1);
  1567. materialSpreadObj.refreshActn();
  1568. // 月信息价需要同时添加空白的list
  1569. if (months.length > 0) {
  1570. const one_month ={ mb_id: result.info.id, code: result.info.code, name: result.info.name, origin: result.info.origin, unit: result.info.unit, order: result.info.order };
  1571. for (const m of months) {
  1572. one_month[m] = null;
  1573. }
  1574. _.forEach(maxData, function (item) {
  1575. const cl = _.find(monthsList, { mb_id: item.id });
  1576. cl.order = item.order + 1;
  1577. });
  1578. monthsList.push(one_month);
  1579. monthsList.sort(function (a, b) {
  1580. return a.order - b.order
  1581. });
  1582. console.log(monthsList);
  1583. // const newMonthsList = _.filter(monthsList, function (item) {
  1584. // return _.findIndex(newMaterialBillsData, { code: item.code }) !== -1;
  1585. // });
  1586. // materialMonthSpread.getActiveSheet().addRows(newMonthsList.length - 1, 1);
  1587. // SpreadJsObj.reLoadRowData(materialMonthSpread.getActiveSheet(), newMonthsList.length - 1);
  1588. monthFunGather.monthsListSet();
  1589. // materialMonthSpreadObj.monthsListSet();
  1590. }
  1591. }
  1592. });
  1593. };
  1594. const gljLibSetting = {
  1595. selector: '#material-tab',
  1596. stdType: 'glj',
  1597. libs: gljLists,
  1598. treeSetting: {
  1599. id: 'glj_id',
  1600. pid: 'glj_pid',
  1601. order: 'order',
  1602. level: 'level',
  1603. rootId: -1,
  1604. keys: ['id', 'list_id', 'glj_id'],
  1605. },
  1606. spreadSetting: {
  1607. cols: [
  1608. {title: '编号', field: 'code', hAlign: 0, width: 120, formatter: '@', readOnly: true, cellType: 'tree'},
  1609. {title: '名称', field: 'name', hAlign: 0, width: 150, formatter: '@', readOnly: true},
  1610. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true},
  1611. {title: '参考上涨幅度', field: 'rise_range', hAlign: 1, width: 50, formatter: '@', readOnly: true},
  1612. {title: '参考下跌幅度', field: 'fall_range', hAlign: 1, width: 50, formatter: '@', readOnly: true},
  1613. ],
  1614. treeCol: 0,
  1615. emptyRows: 0,
  1616. headRows: 1,
  1617. headRowHeight: [32],
  1618. defaultRowHeight: 21,
  1619. headerFont: '12px 微软雅黑',
  1620. font: '12px 微软雅黑',
  1621. headColWidth: [30],
  1622. selectedBackColor: '#fffacd',
  1623. },
  1624. spreadClassCss: 'sjs-material',
  1625. cellDoubleClick: gljLibCellDoubleClick,
  1626. page: 'material',
  1627. tid: window.location.pathname.split('/')[2],
  1628. };
  1629. $.subMenu({
  1630. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  1631. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  1632. key: 'menu.1.0.0',
  1633. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  1634. callback: function (info) {
  1635. if (info.mini) {
  1636. $('.panel-title').addClass('fluid');
  1637. $('#sub-menu').removeClass('panel-sidebar');
  1638. } else {
  1639. $('.panel-title').removeClass('fluid');
  1640. $('#sub-menu').addClass('panel-sidebar');
  1641. }
  1642. autoFlashHeight();
  1643. materialSpread.refresh();
  1644. }
  1645. });
  1646. $.divResizer({
  1647. select: '#right-spr',
  1648. callback: function () {
  1649. materialSpread.refresh();
  1650. materialMonthSpread.refresh();
  1651. if (gljLib) gljLib.spread.refresh();
  1652. const width = (($('#right-view').width()/$('#right-view').parent('div').width())*100).toFixed();
  1653. setLocalCache('material_month_' + materialID, width);
  1654. }
  1655. });
  1656. $.divResizer({
  1657. select: '#main-resize',
  1658. callback: function () {
  1659. let bcontent = $(".bcontent-wrap") ? $(".bcontent-wrap").height() : 0;
  1660. $(".sp-wrap").height(bcontent-30);
  1661. const height = $('.bcontent-wrap').height();
  1662. const cutHeight = isStageSelf ? getObjHeight($('#myTab')) : 0;
  1663. $('.sjs-height-material').height($('.sjs-height-1').height() - cutHeight);
  1664. updateSjsHeight();
  1665. materialSpread.refresh();
  1666. materialMonthSpread.refresh();
  1667. if (gljLib) gljLib.spread.refresh();
  1668. setLocalCache('material_bills_' + materialID, height);
  1669. }
  1670. });
  1671. // 展开收起月信息价并浏览器记住本期展开收起
  1672. $('a', '.right-nav').bind('click', function () {
  1673. //const main = $('#main-view'), tool = $('#tools-view');
  1674. const tab = $(this), tabPanel = $(tab.attr('content'));
  1675. if (!tab.hasClass('active')) {
  1676. $('a', '.side-menu').removeClass('active');
  1677. $('.tab-content .tab-pane').removeClass('active');
  1678. tab.addClass('active');
  1679. tabPanel.addClass('active');
  1680. showSideTools(tab.hasClass('active'));
  1681. if (tab.attr('content') === '#month-tab') {
  1682. const width = (($('#right-view').width()/$('#right-view').parent('div').width())*100).toFixed();
  1683. setLocalCache('material_month_' + materialID, width);
  1684. materialMonthSpread.refresh();
  1685. } else if (tab.attr('content') === '#material-tab') {
  1686. if (!gljLib) {
  1687. gljLib = $.gljLib(gljLibSetting);
  1688. }
  1689. updateSjsHeight();
  1690. gljLib.spread.refresh();
  1691. }
  1692. } else {
  1693. removeLocalCache('material_month_' + materialID);
  1694. tab.removeClass('active');
  1695. tabPanel.removeClass('active');
  1696. showSideTools(tab.hasClass('active'));
  1697. }
  1698. materialSpread.refresh();
  1699. // materialMonthSpread.refresh();
  1700. });
  1701. // 根据浏览器记录展开收起
  1702. if (getLocalCache('material_month_' + materialID) && !isStageSelf) {
  1703. const tab = $('.right-nav a[content="#month-tab"]'), tabPanel = $(tab.attr('content'));
  1704. $('a', '.side-menu').removeClass('active');
  1705. $('.tab-content .tab-pane').removeClass('active');
  1706. tab.addClass('active');
  1707. tabPanel.addClass('active');
  1708. $('#right-view').width(getLocalCache('material_month_' + materialID) + '%');
  1709. showSideTools(tab.hasClass('active'));
  1710. materialSpread.refresh();
  1711. materialMonthSpread.refresh();
  1712. }
  1713. const cutHeight = isStageSelf ? getObjHeight($('#myTab')) : 0;
  1714. if (getLocalCache('material_bills_' + materialID)) {
  1715. $('.bcontent-wrap').height(getLocalCache('material_bills_' + materialID));
  1716. const cHeader = getObjHeight($(".c-header"));
  1717. const bcontent = $(".bcontent-wrap") ? $(".bcontent-wrap").height() : 0;
  1718. $(".sp-wrap").height(bcontent-30);
  1719. $('.sjs-height-1').height($(window).height()-cHeader-bcontent-90+53);
  1720. $('.sjs-height-material').height($('.sjs-height-1').height() - cutHeight);
  1721. updateSjsHeight();
  1722. materialSpread.refresh();
  1723. materialMonthSpread.refresh();
  1724. if (gljLib) gljLib.spread.refresh();
  1725. } else {
  1726. $('.sjs-height-material').height($('.sjs-height-1').height() - cutHeight);
  1727. updateSjsHeight();
  1728. materialSpread.refresh();
  1729. materialMonthSpread.refresh();
  1730. if (gljLib) gljLib.spread.refresh();
  1731. }
  1732. function updateSjsHeight() {
  1733. $('.sjs-height-material-month').height($('.sjs-height-1').height() - getObjHeight($('.sjs-bar-1')));
  1734. $('.material-table-height').height($('.bcontent-wrap').height() - getObjHeight($('.bc-bar')));
  1735. $('.sjs-material').height($('.sjs-height-1').height() - getObjHeight($('.sjs-bar')));
  1736. }
  1737. function getObjHeight(select) {
  1738. return select.length > 0 ? select.height() : 0;
  1739. }
  1740. });