material.js 111 KB

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