material.js 88 KB

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