material.js 111 KB

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