material.js 89 KB

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